Mercurial > hg > graal-jvmci-8
annotate src/share/vm/c1/c1_Optimizer.cpp @ 4757:7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
Summary: fix C1's CEE to take inlining into account when the stacks in states are compared.
Reviewed-by: iveresov, never
author | roland |
---|---|
date | Tue, 13 Dec 2011 10:54:47 +0100 |
parents | 15559220ce79 |
children | e5ac210043cd |
rev | line source |
---|---|
0 | 1 /* |
2166
403dc4c1d7f5
6809483: hotspot:::method_entry are not correctly generated for "method()V"
never
parents:
1972
diff
changeset
|
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
579
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
579
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
579
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "c1/c1_Canonicalizer.hpp" | |
27 #include "c1/c1_Optimizer.hpp" | |
28 #include "c1/c1_ValueMap.hpp" | |
29 #include "c1/c1_ValueSet.hpp" | |
30 #include "c1/c1_ValueStack.hpp" | |
31 #include "utilities/bitMap.inline.hpp" | |
0 | 32 |
33 define_array(ValueSetArray, ValueSet*); | |
34 define_stack(ValueSetList, ValueSetArray); | |
35 | |
36 | |
37 Optimizer::Optimizer(IR* ir) { | |
38 assert(ir->is_valid(), "IR must be valid"); | |
39 _ir = ir; | |
40 } | |
41 | |
42 class CE_Eliminator: public BlockClosure { | |
43 private: | |
44 IR* _hir; | |
45 int _cee_count; // the number of CEs successfully eliminated | |
1899 | 46 int _ifop_count; // the number of IfOps successfully simplified |
0 | 47 int _has_substitution; |
48 | |
49 public: | |
1899 | 50 CE_Eliminator(IR* hir) : _cee_count(0), _ifop_count(0), _hir(hir) { |
0 | 51 _has_substitution = false; |
52 _hir->iterate_preorder(this); | |
53 if (_has_substitution) { | |
1899 | 54 // substituted some ifops/phis, so resolve the substitution |
0 | 55 SubstitutionResolver sr(_hir); |
56 } | |
57 } | |
58 int cee_count() const { return _cee_count; } | |
1899 | 59 int ifop_count() const { return _ifop_count; } |
0 | 60 |
61 void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) { | |
62 int e = sux->number_of_exception_handlers(); | |
63 for (int i = 0; i < e; i++) { | |
64 BlockBegin* xhandler = sux->exception_handler_at(i); | |
65 block->add_exception_handler(xhandler); | |
66 | |
67 assert(xhandler->is_predecessor(sux), "missing predecessor"); | |
68 if (sux->number_of_preds() == 0) { | |
69 // sux is disconnected from graph so disconnect from exception handlers | |
70 xhandler->remove_predecessor(sux); | |
71 } | |
72 if (!xhandler->is_predecessor(block)) { | |
73 xhandler->add_predecessor(block); | |
74 } | |
75 } | |
76 } | |
77 | |
1899 | 78 virtual void block_do(BlockBegin* block); |
79 | |
80 private: | |
81 Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval); | |
82 }; | |
0 | 83 |
1899 | 84 void CE_Eliminator::block_do(BlockBegin* block) { |
85 // 1) find conditional expression | |
86 // check if block ends with an If | |
87 If* if_ = block->end()->as_If(); | |
88 if (if_ == NULL) return; | |
0 | 89 |
1899 | 90 // check if If works on int or object types |
91 // (we cannot handle If's working on long, float or doubles yet, | |
92 // since IfOp doesn't support them - these If's show up if cmp | |
93 // operations followed by If's are eliminated) | |
94 ValueType* if_type = if_->x()->type(); | |
95 if (!if_type->is_int() && !if_type->is_object()) return; | |
0 | 96 |
1899 | 97 BlockBegin* t_block = if_->tsux(); |
98 BlockBegin* f_block = if_->fsux(); | |
99 Instruction* t_cur = t_block->next(); | |
100 Instruction* f_cur = f_block->next(); | |
0 | 101 |
1899 | 102 // one Constant may be present between BlockBegin and BlockEnd |
103 Value t_const = NULL; | |
104 Value f_const = NULL; | |
105 if (t_cur->as_Constant() != NULL && !t_cur->can_trap()) { | |
106 t_const = t_cur; | |
107 t_cur = t_cur->next(); | |
108 } | |
109 if (f_cur->as_Constant() != NULL && !f_cur->can_trap()) { | |
110 f_const = f_cur; | |
111 f_cur = f_cur->next(); | |
112 } | |
0 | 113 |
1899 | 114 // check if both branches end with a goto |
115 Goto* t_goto = t_cur->as_Goto(); | |
116 if (t_goto == NULL) return; | |
117 Goto* f_goto = f_cur->as_Goto(); | |
118 if (f_goto == NULL) return; | |
0 | 119 |
1899 | 120 // check if both gotos merge into the same block |
121 BlockBegin* sux = t_goto->default_sux(); | |
122 if (sux != f_goto->default_sux()) return; | |
0 | 123 |
1899 | 124 // check if at least one word was pushed on sux_state |
4757
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
125 // inlining depths must match |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
126 ValueStack* if_state = if_->state(); |
1899 | 127 ValueStack* sux_state = sux->state(); |
4757
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
128 while (sux_state->scope() != if_state->scope()) { |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
129 if_state = if_state->caller_state(); |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
130 assert(if_state != NULL, "states do not match up"); |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
131 } |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
132 |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
133 if (sux_state->stack_size() <= if_state->stack_size()) return; |
0 | 134 |
1899 | 135 // check if phi function is present at end of successor stack and that |
136 // only this phi was pushed on the stack | |
4757
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
137 Value sux_phi = sux_state->stack_at(if_state->stack_size()); |
1899 | 138 if (sux_phi == NULL || sux_phi->as_Phi() == NULL || sux_phi->as_Phi()->block() != sux) return; |
4757
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
139 if (sux_phi->type()->size() != sux_state->stack_size() - if_state->stack_size()) return; |
0 | 140 |
1899 | 141 // get the values that were pushed in the true- and false-branch |
4757
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
142 Value t_value = t_goto->state()->stack_at(if_state->stack_size()); |
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
143 Value f_value = f_goto->state()->stack_at(if_state->stack_size()); |
1899 | 144 |
145 // backend does not support floats | |
146 assert(t_value->type()->base() == f_value->type()->base(), "incompatible types"); | |
147 if (t_value->type()->is_float_kind()) return; | |
0 | 148 |
1899 | 149 // check that successor has no other phi functions but sux_phi |
150 // this can happen when t_block or f_block contained additonal stores to local variables | |
151 // that are no longer represented by explicit instructions | |
152 for_each_phi_fun(sux, phi, | |
153 if (phi != sux_phi) return; | |
154 ); | |
155 // true and false blocks can't have phis | |
156 for_each_phi_fun(t_block, phi, return; ); | |
157 for_each_phi_fun(f_block, phi, return; ); | |
158 | |
159 // 2) substitute conditional expression | |
160 // with an IfOp followed by a Goto | |
161 // cut if_ away and get node before | |
162 Instruction* cur_end = if_->prev(block); | |
0 | 163 |
1899 | 164 // append constants of true- and false-block if necessary |
165 // clone constants because original block must not be destroyed | |
166 assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch"); | |
167 if (t_value == t_const) { | |
168 t_value = new Constant(t_const->type()); | |
169 NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci())); | |
170 cur_end = cur_end->set_next(t_value); | |
171 } | |
172 if (f_value == f_const) { | |
173 f_value = new Constant(f_const->type()); | |
174 NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci())); | |
175 cur_end = cur_end->set_next(f_value); | |
176 } | |
0 | 177 |
1899 | 178 Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value); |
179 assert(result != NULL, "make_ifop must return a non-null instruction"); | |
180 if (!result->is_linked() && result->can_be_linked()) { | |
1819 | 181 NOT_PRODUCT(result->set_printable_bci(if_->printable_bci())); |
182 cur_end = cur_end->set_next(result); | |
1899 | 183 } |
0 | 184 |
1899 | 185 // append Goto to successor |
186 ValueStack* state_before = if_->is_safepoint() ? if_->state_before() : NULL; | |
187 Goto* goto_ = new Goto(sux, state_before, if_->is_safepoint() || t_goto->is_safepoint() || f_goto->is_safepoint()); | |
188 | |
189 // prepare state for Goto | |
4757
7bca37d28f32
7114106: C1: assert(goto_state->is_same(sux_state)) failed: states must match now
roland
parents:
3792
diff
changeset
|
190 ValueStack* goto_state = if_state; |
1899 | 191 goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci()); |
192 goto_state->push(result->type(), result); | |
193 assert(goto_state->is_same(sux_state), "states must match now"); | |
194 goto_->set_state(goto_state); | |
195 | |
196 cur_end = cur_end->set_next(goto_, goto_state->bci()); | |
197 | |
198 // Adjust control flow graph | |
199 BlockBegin::disconnect_edge(block, t_block); | |
200 BlockBegin::disconnect_edge(block, f_block); | |
201 if (t_block->number_of_preds() == 0) { | |
202 BlockBegin::disconnect_edge(t_block, sux); | |
203 } | |
204 adjust_exception_edges(block, t_block); | |
205 if (f_block->number_of_preds() == 0) { | |
206 BlockBegin::disconnect_edge(f_block, sux); | |
207 } | |
208 adjust_exception_edges(block, f_block); | |
209 | |
210 // update block end | |
211 block->set_end(goto_); | |
212 | |
213 // substitute the phi if possible | |
214 if (sux_phi->as_Phi()->operand_count() == 1) { | |
215 assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi"); | |
216 sux_phi->set_subst(result); | |
217 _has_substitution = true; | |
218 } | |
219 | |
220 // 3) successfully eliminated a conditional expression | |
221 _cee_count++; | |
222 if (PrintCEE) { | |
223 tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id()); | |
224 tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id()); | |
225 } | |
0 | 226 |
1899 | 227 _hir->verify(); |
228 } | |
229 | |
230 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) { | |
231 if (!OptimizeIfOps) { | |
232 return new IfOp(x, cond, y, tval, fval); | |
233 } | |
234 | |
235 tval = tval->subst(); | |
236 fval = fval->subst(); | |
237 if (tval == fval) { | |
238 _ifop_count++; | |
239 return tval; | |
240 } | |
241 | |
242 x = x->subst(); | |
243 y = y->subst(); | |
244 | |
245 Constant* y_const = y->as_Constant(); | |
246 if (y_const != NULL) { | |
247 IfOp* x_ifop = x->as_IfOp(); | |
248 if (x_ifop != NULL) { // x is an ifop, y is a constant | |
249 Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant(); | |
250 Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant(); | |
0 | 251 |
1899 | 252 if (x_tval_const != NULL && x_fval_const != NULL) { |
253 Instruction::Condition x_ifop_cond = x_ifop->cond(); | |
254 | |
255 Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const); | |
256 Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const); | |
257 | |
3362
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
258 // not_comparable here is a valid return in case we're comparing unloaded oop constants |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
259 if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) { |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
260 Value new_tval = t_compare_res == Constant::cond_true ? tval : fval; |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
261 Value new_fval = f_compare_res == Constant::cond_true ? tval : fval; |
0 | 262 |
3362
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
263 _ifop_count++; |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
264 if (new_tval == new_fval) { |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
265 return new_tval; |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
266 } else { |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
267 return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval); |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
268 } |
1899 | 269 } |
270 } | |
271 } else { | |
272 Constant* x_const = x->as_Constant(); | |
273 if (x_const != NULL) { // x and y are constants | |
274 Constant::CompareResult x_compare_res = x_const->compare(cond, y_const); | |
3362
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
275 // not_comparable here is a valid return in case we're comparing unloaded oop constants |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
276 if (x_compare_res != Constant::not_comparable) { |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
277 _ifop_count++; |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
278 return x_compare_res == Constant::cond_true ? tval : fval; |
d4c1fbc3de95
7042153: guarantee(x_compare_res != Constant::not_comparable) failed: incomparable constants in IfOp
iveresov
parents:
2446
diff
changeset
|
279 } |
1899 | 280 } |
0 | 281 } |
282 } | |
1899 | 283 return new IfOp(x, cond, y, tval, fval); |
284 } | |
0 | 285 |
286 void Optimizer::eliminate_conditional_expressions() { | |
287 // find conditional expressions & replace them with IfOps | |
288 CE_Eliminator ce(ir()); | |
289 } | |
290 | |
291 class BlockMerger: public BlockClosure { | |
292 private: | |
293 IR* _hir; | |
294 int _merge_count; // the number of block pairs successfully merged | |
295 | |
296 public: | |
297 BlockMerger(IR* hir) | |
298 : _hir(hir) | |
299 , _merge_count(0) | |
300 { | |
301 _hir->iterate_preorder(this); | |
302 } | |
303 | |
304 bool try_merge(BlockBegin* block) { | |
305 BlockEnd* end = block->end(); | |
306 if (end->as_Goto() != NULL) { | |
307 assert(end->number_of_sux() == 1, "end must have exactly one successor"); | |
308 // Note: It would be sufficient to check for the number of successors (= 1) | |
309 // in order to decide if this block can be merged potentially. That | |
310 // would then also include switch statements w/ only a default case. | |
311 // However, in that case we would need to make sure the switch tag | |
312 // expression is executed if it can produce observable side effects. | |
313 // We should probably have the canonicalizer simplifying such switch | |
314 // statements and then we are sure we don't miss these merge opportunities | |
315 // here (was bug - gri 7/7/99). | |
316 BlockBegin* sux = end->default_sux(); | |
317 if (sux->number_of_preds() == 1 && !sux->is_entry_block() && !end->is_safepoint()) { | |
318 // merge the two blocks | |
319 | |
320 #ifdef ASSERT | |
321 // verify that state at the end of block and at the beginning of sux are equal | |
322 // no phi functions must be present at beginning of sux | |
323 ValueStack* sux_state = sux->state(); | |
324 ValueStack* end_state = end->state(); | |
1819 | 325 |
326 assert(end_state->scope() == sux_state->scope(), "scopes must match"); | |
0 | 327 assert(end_state->stack_size() == sux_state->stack_size(), "stack not equal"); |
328 assert(end_state->locals_size() == sux_state->locals_size(), "locals not equal"); | |
329 | |
330 int index; | |
331 Value sux_value; | |
332 for_each_stack_value(sux_state, index, sux_value) { | |
333 assert(sux_value == end_state->stack_at(index), "stack not equal"); | |
334 } | |
335 for_each_local_value(sux_state, index, sux_value) { | |
336 assert(sux_value == end_state->local_at(index), "locals not equal"); | |
337 } | |
338 assert(sux_state->caller_state() == end_state->caller_state(), "caller not equal"); | |
339 #endif | |
340 | |
341 // find instruction before end & append first instruction of sux block | |
342 Instruction* prev = end->prev(block); | |
343 Instruction* next = sux->next(); | |
344 assert(prev->as_BlockEnd() == NULL, "must not be a BlockEnd"); | |
1819 | 345 prev->set_next(next); |
0 | 346 sux->disconnect_from_graph(); |
347 block->set_end(sux->end()); | |
348 // add exception handlers of deleted block, if any | |
349 for (int k = 0; k < sux->number_of_exception_handlers(); k++) { | |
350 BlockBegin* xhandler = sux->exception_handler_at(k); | |
351 block->add_exception_handler(xhandler); | |
352 | |
353 // also substitute predecessor of exception handler | |
354 assert(xhandler->is_predecessor(sux), "missing predecessor"); | |
355 xhandler->remove_predecessor(sux); | |
356 if (!xhandler->is_predecessor(block)) { | |
357 xhandler->add_predecessor(block); | |
358 } | |
359 } | |
360 | |
361 // debugging output | |
362 _merge_count++; | |
363 if (PrintBlockElimination) { | |
364 tty->print_cr("%d. merged B%d & B%d (stack size = %d)", | |
365 _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size()); | |
366 } | |
367 | |
368 _hir->verify(); | |
369 | |
370 If* if_ = block->end()->as_If(); | |
371 if (if_) { | |
372 IfOp* ifop = if_->x()->as_IfOp(); | |
373 Constant* con = if_->y()->as_Constant(); | |
374 bool swapped = false; | |
375 if (!con || !ifop) { | |
376 ifop = if_->y()->as_IfOp(); | |
377 con = if_->x()->as_Constant(); | |
378 swapped = true; | |
379 } | |
380 if (con && ifop) { | |
381 Constant* tval = ifop->tval()->as_Constant(); | |
382 Constant* fval = ifop->fval()->as_Constant(); | |
383 if (tval && fval) { | |
384 // Find the instruction before if_, starting with ifop. | |
385 // When if_ and ifop are not in the same block, prev | |
386 // becomes NULL In such (rare) cases it is not | |
387 // profitable to perform the optimization. | |
388 Value prev = ifop; | |
389 while (prev != NULL && prev->next() != if_) { | |
390 prev = prev->next(); | |
391 } | |
392 | |
393 if (prev != NULL) { | |
394 Instruction::Condition cond = if_->cond(); | |
395 BlockBegin* tsux = if_->tsux(); | |
396 BlockBegin* fsux = if_->fsux(); | |
397 if (swapped) { | |
398 cond = Instruction::mirror(cond); | |
399 } | |
400 | |
401 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux); | |
402 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux); | |
403 if (tblock != fblock && !if_->is_safepoint()) { | |
404 If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(), | |
405 tblock, fblock, if_->state_before(), if_->is_safepoint()); | |
406 newif->set_state(if_->state()->copy()); | |
407 | |
408 assert(prev->next() == if_, "must be guaranteed by above search"); | |
1819 | 409 NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci())); |
410 prev->set_next(newif); | |
0 | 411 block->set_end(newif); |
412 | |
413 _merge_count++; | |
414 if (PrintBlockElimination) { | |
415 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id()); | |
416 } | |
417 | |
418 _hir->verify(); | |
419 } | |
420 } | |
421 } | |
422 } | |
423 } | |
424 | |
425 return true; | |
426 } | |
427 } | |
428 return false; | |
429 } | |
430 | |
431 virtual void block_do(BlockBegin* block) { | |
432 _hir->verify(); | |
433 // repeat since the same block may merge again | |
434 while (try_merge(block)) { | |
435 _hir->verify(); | |
436 } | |
437 } | |
438 }; | |
439 | |
440 | |
441 void Optimizer::eliminate_blocks() { | |
442 // merge blocks if possible | |
443 BlockMerger bm(ir()); | |
444 } | |
445 | |
446 | |
447 class NullCheckEliminator; | |
448 class NullCheckVisitor: public InstructionVisitor { | |
449 private: | |
450 NullCheckEliminator* _nce; | |
451 NullCheckEliminator* nce() { return _nce; } | |
452 | |
453 public: | |
454 NullCheckVisitor() {} | |
455 | |
456 void set_eliminator(NullCheckEliminator* nce) { _nce = nce; } | |
457 | |
458 void do_Phi (Phi* x); | |
459 void do_Local (Local* x); | |
460 void do_Constant (Constant* x); | |
461 void do_LoadField (LoadField* x); | |
462 void do_StoreField (StoreField* x); | |
463 void do_ArrayLength (ArrayLength* x); | |
464 void do_LoadIndexed (LoadIndexed* x); | |
465 void do_StoreIndexed (StoreIndexed* x); | |
466 void do_NegateOp (NegateOp* x); | |
467 void do_ArithmeticOp (ArithmeticOp* x); | |
468 void do_ShiftOp (ShiftOp* x); | |
469 void do_LogicOp (LogicOp* x); | |
470 void do_CompareOp (CompareOp* x); | |
471 void do_IfOp (IfOp* x); | |
472 void do_Convert (Convert* x); | |
473 void do_NullCheck (NullCheck* x); | |
474 void do_Invoke (Invoke* x); | |
475 void do_NewInstance (NewInstance* x); | |
476 void do_NewTypeArray (NewTypeArray* x); | |
477 void do_NewObjectArray (NewObjectArray* x); | |
478 void do_NewMultiArray (NewMultiArray* x); | |
479 void do_CheckCast (CheckCast* x); | |
480 void do_InstanceOf (InstanceOf* x); | |
481 void do_MonitorEnter (MonitorEnter* x); | |
482 void do_MonitorExit (MonitorExit* x); | |
483 void do_Intrinsic (Intrinsic* x); | |
484 void do_BlockBegin (BlockBegin* x); | |
485 void do_Goto (Goto* x); | |
486 void do_If (If* x); | |
487 void do_IfInstanceOf (IfInstanceOf* x); | |
488 void do_TableSwitch (TableSwitch* x); | |
489 void do_LookupSwitch (LookupSwitch* x); | |
490 void do_Return (Return* x); | |
491 void do_Throw (Throw* x); | |
492 void do_Base (Base* x); | |
493 void do_OsrEntry (OsrEntry* x); | |
494 void do_ExceptionObject(ExceptionObject* x); | |
495 void do_RoundFP (RoundFP* x); | |
496 void do_UnsafeGetRaw (UnsafeGetRaw* x); | |
497 void do_UnsafePutRaw (UnsafePutRaw* x); | |
498 void do_UnsafeGetObject(UnsafeGetObject* x); | |
499 void do_UnsafePutObject(UnsafePutObject* x); | |
500 void do_UnsafePrefetchRead (UnsafePrefetchRead* x); | |
501 void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x); | |
502 void do_ProfileCall (ProfileCall* x); | |
1783 | 503 void do_ProfileInvoke (ProfileInvoke* x); |
2166
403dc4c1d7f5
6809483: hotspot:::method_entry are not correctly generated for "method()V"
never
parents:
1972
diff
changeset
|
504 void do_RuntimeCall (RuntimeCall* x); |
0 | 505 }; |
506 | |
507 | |
508 // Because of a static contained within (for the purpose of iteration | |
509 // over instructions), it is only valid to have one of these active at | |
510 // a time | |
1584 | 511 class NullCheckEliminator: public ValueVisitor { |
0 | 512 private: |
513 Optimizer* _opt; | |
514 | |
515 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block | |
516 BlockList* _work_list; // Basic blocks to visit | |
517 | |
518 bool visitable(Value x) { | |
519 assert(_visitable_instructions != NULL, "check"); | |
520 return _visitable_instructions->contains(x); | |
521 } | |
522 void mark_visited(Value x) { | |
523 assert(_visitable_instructions != NULL, "check"); | |
524 _visitable_instructions->remove(x); | |
525 } | |
526 void mark_visitable(Value x) { | |
527 assert(_visitable_instructions != NULL, "check"); | |
528 _visitable_instructions->put(x); | |
529 } | |
530 void clear_visitable_state() { | |
531 assert(_visitable_instructions != NULL, "check"); | |
532 _visitable_instructions->clear(); | |
533 } | |
534 | |
535 ValueSet* _set; // current state, propagated to subsequent BlockBegins | |
536 ValueSetList _block_states; // BlockBegin null-check states for all processed blocks | |
537 NullCheckVisitor _visitor; | |
538 NullCheck* _last_explicit_null_check; | |
539 | |
540 bool set_contains(Value x) { assert(_set != NULL, "check"); return _set->contains(x); } | |
541 void set_put (Value x) { assert(_set != NULL, "check"); _set->put(x); } | |
542 void set_remove (Value x) { assert(_set != NULL, "check"); _set->remove(x); } | |
543 | |
544 BlockList* work_list() { return _work_list; } | |
545 | |
546 void iterate_all(); | |
547 void iterate_one(BlockBegin* block); | |
548 | |
549 ValueSet* state() { return _set; } | |
550 void set_state_from (ValueSet* state) { _set->set_from(state); } | |
551 ValueSet* state_for (BlockBegin* block) { return _block_states[block->block_id()]; } | |
552 void set_state_for (BlockBegin* block, ValueSet* stack) { _block_states[block->block_id()] = stack; } | |
553 // Returns true if caused a change in the block's state. | |
554 bool merge_state_for(BlockBegin* block, | |
555 ValueSet* incoming_state); | |
556 | |
557 public: | |
558 // constructor | |
559 NullCheckEliminator(Optimizer* opt) | |
560 : _opt(opt) | |
561 , _set(new ValueSet()) | |
562 , _last_explicit_null_check(NULL) | |
563 , _block_states(BlockBegin::number_of_blocks(), NULL) | |
564 , _work_list(new BlockList()) { | |
565 _visitable_instructions = new ValueSet(); | |
566 _visitor.set_eliminator(this); | |
567 } | |
568 | |
569 Optimizer* opt() { return _opt; } | |
570 IR* ir () { return opt()->ir(); } | |
571 | |
572 // Process a graph | |
573 void iterate(BlockBegin* root); | |
574 | |
1584 | 575 void visit(Value* f); |
576 | |
0 | 577 // In some situations (like NullCheck(x); getfield(x)) the debug |
578 // information from the explicit NullCheck can be used to populate | |
579 // the getfield, even if the two instructions are in different | |
580 // scopes; this allows implicit null checks to be used but the | |
581 // correct exception information to be generated. We must clear the | |
582 // last-traversed NullCheck when we reach a potentially-exception- | |
583 // throwing instruction, as well as in some other cases. | |
584 void set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; } | |
585 NullCheck* last_explicit_null_check() { return _last_explicit_null_check; } | |
586 Value last_explicit_null_check_obj() { return (_last_explicit_null_check | |
587 ? _last_explicit_null_check->obj() | |
588 : NULL); } | |
589 NullCheck* consume_last_explicit_null_check() { | |
590 _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck); | |
591 _last_explicit_null_check->set_can_trap(false); | |
592 return _last_explicit_null_check; | |
593 } | |
594 void clear_last_explicit_null_check() { _last_explicit_null_check = NULL; } | |
595 | |
596 // Handlers for relevant instructions | |
597 // (separated out from NullCheckVisitor for clarity) | |
598 | |
599 // The basic contract is that these must leave the instruction in | |
600 // the desired state; must not assume anything about the state of | |
601 // the instruction. We make multiple passes over some basic blocks | |
602 // and the last pass is the only one whose result is valid. | |
603 void handle_AccessField (AccessField* x); | |
604 void handle_ArrayLength (ArrayLength* x); | |
605 void handle_LoadIndexed (LoadIndexed* x); | |
606 void handle_StoreIndexed (StoreIndexed* x); | |
607 void handle_NullCheck (NullCheck* x); | |
608 void handle_Invoke (Invoke* x); | |
609 void handle_NewInstance (NewInstance* x); | |
610 void handle_NewArray (NewArray* x); | |
611 void handle_AccessMonitor (AccessMonitor* x); | |
612 void handle_Intrinsic (Intrinsic* x); | |
613 void handle_ExceptionObject (ExceptionObject* x); | |
614 void handle_Phi (Phi* x); | |
615 }; | |
616 | |
617 | |
618 // NEEDS_CLEANUP | |
619 // There may be other instructions which need to clear the last | |
620 // explicit null check. Anything across which we can not hoist the | |
621 // debug information for a NullCheck instruction must clear it. It | |
622 // might be safer to pattern match "NullCheck ; {AccessField, | |
623 // ArrayLength, LoadIndexed}" but it is more easily structured this way. | |
624 // Should test to see performance hit of clearing it for all handlers | |
625 // with empty bodies below. If it is negligible then we should leave | |
626 // that in for safety, otherwise should think more about it. | |
627 void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); } | |
628 void NullCheckVisitor::do_Local (Local* x) {} | |
629 void NullCheckVisitor::do_Constant (Constant* x) { /* FIXME: handle object constants */ } | |
630 void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); } | |
631 void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); } | |
632 void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); } | |
633 void NullCheckVisitor::do_LoadIndexed (LoadIndexed* x) { nce()->handle_LoadIndexed(x); } | |
634 void NullCheckVisitor::do_StoreIndexed (StoreIndexed* x) { nce()->handle_StoreIndexed(x); } | |
635 void NullCheckVisitor::do_NegateOp (NegateOp* x) {} | |
636 void NullCheckVisitor::do_ArithmeticOp (ArithmeticOp* x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); } | |
637 void NullCheckVisitor::do_ShiftOp (ShiftOp* x) {} | |
638 void NullCheckVisitor::do_LogicOp (LogicOp* x) {} | |
639 void NullCheckVisitor::do_CompareOp (CompareOp* x) {} | |
640 void NullCheckVisitor::do_IfOp (IfOp* x) {} | |
641 void NullCheckVisitor::do_Convert (Convert* x) {} | |
642 void NullCheckVisitor::do_NullCheck (NullCheck* x) { nce()->handle_NullCheck(x); } | |
643 void NullCheckVisitor::do_Invoke (Invoke* x) { nce()->handle_Invoke(x); } | |
644 void NullCheckVisitor::do_NewInstance (NewInstance* x) { nce()->handle_NewInstance(x); } | |
645 void NullCheckVisitor::do_NewTypeArray (NewTypeArray* x) { nce()->handle_NewArray(x); } | |
646 void NullCheckVisitor::do_NewObjectArray (NewObjectArray* x) { nce()->handle_NewArray(x); } | |
647 void NullCheckVisitor::do_NewMultiArray (NewMultiArray* x) { nce()->handle_NewArray(x); } | |
3792
15559220ce79
6478991: C1 NullCheckEliminator yields incorrect exceptions
never
parents:
3362
diff
changeset
|
648 void NullCheckVisitor::do_CheckCast (CheckCast* x) { nce()->clear_last_explicit_null_check(); } |
0 | 649 void NullCheckVisitor::do_InstanceOf (InstanceOf* x) {} |
650 void NullCheckVisitor::do_MonitorEnter (MonitorEnter* x) { nce()->handle_AccessMonitor(x); } | |
651 void NullCheckVisitor::do_MonitorExit (MonitorExit* x) { nce()->handle_AccessMonitor(x); } | |
2446 | 652 void NullCheckVisitor::do_Intrinsic (Intrinsic* x) { nce()->handle_Intrinsic(x); } |
0 | 653 void NullCheckVisitor::do_BlockBegin (BlockBegin* x) {} |
654 void NullCheckVisitor::do_Goto (Goto* x) {} | |
655 void NullCheckVisitor::do_If (If* x) {} | |
656 void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {} | |
657 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {} | |
658 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {} | |
659 void NullCheckVisitor::do_Return (Return* x) {} | |
660 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); } | |
661 void NullCheckVisitor::do_Base (Base* x) {} | |
662 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {} | |
663 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); } | |
664 void NullCheckVisitor::do_RoundFP (RoundFP* x) {} | |
665 void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {} | |
666 void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {} | |
667 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {} | |
668 void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {} | |
669 void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {} | |
670 void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {} | |
671 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check(); } | |
1783 | 672 void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {} |
2166
403dc4c1d7f5
6809483: hotspot:::method_entry are not correctly generated for "method()V"
never
parents:
1972
diff
changeset
|
673 void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {} |
0 | 674 |
675 | |
1584 | 676 void NullCheckEliminator::visit(Value* p) { |
0 | 677 assert(*p != NULL, "should not find NULL instructions"); |
1584 | 678 if (visitable(*p)) { |
679 mark_visited(*p); | |
680 (*p)->visit(&_visitor); | |
0 | 681 } |
682 } | |
683 | |
684 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) { | |
685 ValueSet* state = state_for(block); | |
686 if (state == NULL) { | |
687 state = incoming_state->copy(); | |
688 set_state_for(block, state); | |
689 return true; | |
690 } else { | |
691 bool changed = state->set_intersect(incoming_state); | |
692 if (PrintNullCheckElimination && changed) { | |
693 tty->print_cr("Block %d's null check state changed", block->block_id()); | |
694 } | |
695 return changed; | |
696 } | |
697 } | |
698 | |
699 | |
700 void NullCheckEliminator::iterate_all() { | |
701 while (work_list()->length() > 0) { | |
702 iterate_one(work_list()->pop()); | |
703 } | |
704 } | |
705 | |
706 | |
707 void NullCheckEliminator::iterate_one(BlockBegin* block) { | |
708 clear_visitable_state(); | |
709 // clear out an old explicit null checks | |
710 set_last_explicit_null_check(NULL); | |
711 | |
712 if (PrintNullCheckElimination) { | |
713 tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s", | |
714 block->block_id(), | |
715 ir()->method()->holder()->name()->as_utf8(), | |
716 ir()->method()->name()->as_utf8(), | |
717 ir()->method()->signature()->as_symbol()->as_utf8()); | |
718 } | |
719 | |
720 // Create new state if none present (only happens at root) | |
721 if (state_for(block) == NULL) { | |
722 ValueSet* tmp_state = new ValueSet(); | |
723 set_state_for(block, tmp_state); | |
724 // Initial state is that local 0 (receiver) is non-null for | |
725 // non-static methods | |
726 ValueStack* stack = block->state(); | |
727 IRScope* scope = stack->scope(); | |
728 ciMethod* method = scope->method(); | |
729 if (!method->is_static()) { | |
730 Local* local0 = stack->local_at(0)->as_Local(); | |
731 assert(local0 != NULL, "must be"); | |
732 assert(local0->type() == objectType, "invalid type of receiver"); | |
733 | |
734 if (local0 != NULL) { | |
735 // Local 0 is used in this scope | |
736 tmp_state->put(local0); | |
737 if (PrintNullCheckElimination) { | |
738 tty->print_cr("Local 0 (value %d) proven non-null upon entry", local0->id()); | |
739 } | |
740 } | |
741 } | |
742 } | |
743 | |
744 // Must copy block's state to avoid mutating it during iteration | |
745 // through the block -- otherwise "not-null" states can accidentally | |
746 // propagate "up" through the block during processing of backward | |
747 // branches and algorithm is incorrect (and does not converge) | |
748 set_state_from(state_for(block)); | |
749 | |
750 // allow visiting of Phis belonging to this block | |
751 for_each_phi_fun(block, phi, | |
752 mark_visitable(phi); | |
753 ); | |
754 | |
755 BlockEnd* e = block->end(); | |
756 assert(e != NULL, "incomplete graph"); | |
757 int i; | |
758 | |
759 // Propagate the state before this block into the exception | |
760 // handlers. They aren't true successors since we aren't guaranteed | |
761 // to execute the whole block before executing them. Also putting | |
762 // them on first seems to help reduce the amount of iteration to | |
763 // reach a fixed point. | |
764 for (i = 0; i < block->number_of_exception_handlers(); i++) { | |
765 BlockBegin* next = block->exception_handler_at(i); | |
766 if (merge_state_for(next, state())) { | |
767 if (!work_list()->contains(next)) { | |
768 work_list()->push(next); | |
769 } | |
770 } | |
771 } | |
772 | |
773 // Iterate through block, updating state. | |
774 for (Instruction* instr = block; instr != NULL; instr = instr->next()) { | |
775 // Mark instructions in this block as visitable as they are seen | |
776 // in the instruction list. This keeps the iteration from | |
777 // visiting instructions which are references in other blocks or | |
778 // visiting instructions more than once. | |
779 mark_visitable(instr); | |
1819 | 780 if (instr->is_pinned() || instr->can_trap() || (instr->as_NullCheck() != NULL)) { |
0 | 781 mark_visited(instr); |
1584 | 782 instr->input_values_do(this); |
0 | 783 instr->visit(&_visitor); |
784 } | |
785 } | |
786 | |
787 // Propagate state to successors if necessary | |
788 for (i = 0; i < e->number_of_sux(); i++) { | |
789 BlockBegin* next = e->sux_at(i); | |
790 if (merge_state_for(next, state())) { | |
791 if (!work_list()->contains(next)) { | |
792 work_list()->push(next); | |
793 } | |
794 } | |
795 } | |
796 } | |
797 | |
798 | |
799 void NullCheckEliminator::iterate(BlockBegin* block) { | |
800 work_list()->push(block); | |
801 iterate_all(); | |
802 } | |
803 | |
804 void NullCheckEliminator::handle_AccessField(AccessField* x) { | |
805 if (x->is_static()) { | |
806 if (x->as_LoadField() != NULL) { | |
807 // If the field is a non-null static final object field (as is | |
808 // often the case for sun.misc.Unsafe), put this LoadField into | |
809 // the non-null map | |
810 ciField* field = x->field(); | |
811 if (field->is_constant()) { | |
812 ciConstant field_val = field->constant_value(); | |
813 BasicType field_type = field_val.basic_type(); | |
814 if (field_type == T_OBJECT || field_type == T_ARRAY) { | |
815 ciObject* obj_val = field_val.as_object(); | |
816 if (!obj_val->is_null_object()) { | |
817 if (PrintNullCheckElimination) { | |
818 tty->print_cr("AccessField %d proven non-null by static final non-null oop check", | |
819 x->id()); | |
820 } | |
821 set_put(x); | |
822 } | |
823 } | |
824 } | |
825 } | |
826 // Be conservative | |
827 clear_last_explicit_null_check(); | |
828 return; | |
829 } | |
830 | |
831 Value obj = x->obj(); | |
832 if (set_contains(obj)) { | |
833 // Value is non-null => update AccessField | |
834 if (last_explicit_null_check_obj() == obj && !x->needs_patching()) { | |
835 x->set_explicit_null_check(consume_last_explicit_null_check()); | |
836 x->set_needs_null_check(true); | |
837 if (PrintNullCheckElimination) { | |
838 tty->print_cr("Folded NullCheck %d into AccessField %d's null check for value %d", | |
839 x->explicit_null_check()->id(), x->id(), obj->id()); | |
840 } | |
841 } else { | |
842 x->set_explicit_null_check(NULL); | |
843 x->set_needs_null_check(false); | |
844 if (PrintNullCheckElimination) { | |
845 tty->print_cr("Eliminated AccessField %d's null check for value %d", x->id(), obj->id()); | |
846 } | |
847 } | |
848 } else { | |
849 set_put(obj); | |
850 if (PrintNullCheckElimination) { | |
851 tty->print_cr("AccessField %d of value %d proves value to be non-null", x->id(), obj->id()); | |
852 } | |
853 // Ensure previous passes do not cause wrong state | |
854 x->set_needs_null_check(true); | |
855 x->set_explicit_null_check(NULL); | |
856 } | |
857 clear_last_explicit_null_check(); | |
858 } | |
859 | |
860 | |
861 void NullCheckEliminator::handle_ArrayLength(ArrayLength* x) { | |
862 Value array = x->array(); | |
863 if (set_contains(array)) { | |
864 // Value is non-null => update AccessArray | |
865 if (last_explicit_null_check_obj() == array) { | |
866 x->set_explicit_null_check(consume_last_explicit_null_check()); | |
867 x->set_needs_null_check(true); | |
868 if (PrintNullCheckElimination) { | |
869 tty->print_cr("Folded NullCheck %d into ArrayLength %d's null check for value %d", | |
870 x->explicit_null_check()->id(), x->id(), array->id()); | |
871 } | |
872 } else { | |
873 x->set_explicit_null_check(NULL); | |
874 x->set_needs_null_check(false); | |
875 if (PrintNullCheckElimination) { | |
876 tty->print_cr("Eliminated ArrayLength %d's null check for value %d", x->id(), array->id()); | |
877 } | |
878 } | |
879 } else { | |
880 set_put(array); | |
881 if (PrintNullCheckElimination) { | |
882 tty->print_cr("ArrayLength %d of value %d proves value to be non-null", x->id(), array->id()); | |
883 } | |
884 // Ensure previous passes do not cause wrong state | |
885 x->set_needs_null_check(true); | |
886 x->set_explicit_null_check(NULL); | |
887 } | |
888 clear_last_explicit_null_check(); | |
889 } | |
890 | |
891 | |
892 void NullCheckEliminator::handle_LoadIndexed(LoadIndexed* x) { | |
893 Value array = x->array(); | |
894 if (set_contains(array)) { | |
895 // Value is non-null => update AccessArray | |
896 if (last_explicit_null_check_obj() == array) { | |
897 x->set_explicit_null_check(consume_last_explicit_null_check()); | |
898 x->set_needs_null_check(true); | |
899 if (PrintNullCheckElimination) { | |
900 tty->print_cr("Folded NullCheck %d into LoadIndexed %d's null check for value %d", | |
901 x->explicit_null_check()->id(), x->id(), array->id()); | |
902 } | |
903 } else { | |
904 x->set_explicit_null_check(NULL); | |
905 x->set_needs_null_check(false); | |
906 if (PrintNullCheckElimination) { | |
907 tty->print_cr("Eliminated LoadIndexed %d's null check for value %d", x->id(), array->id()); | |
908 } | |
909 } | |
910 } else { | |
911 set_put(array); | |
912 if (PrintNullCheckElimination) { | |
913 tty->print_cr("LoadIndexed %d of value %d proves value to be non-null", x->id(), array->id()); | |
914 } | |
915 // Ensure previous passes do not cause wrong state | |
916 x->set_needs_null_check(true); | |
917 x->set_explicit_null_check(NULL); | |
918 } | |
919 clear_last_explicit_null_check(); | |
920 } | |
921 | |
922 | |
923 void NullCheckEliminator::handle_StoreIndexed(StoreIndexed* x) { | |
924 Value array = x->array(); | |
925 if (set_contains(array)) { | |
926 // Value is non-null => update AccessArray | |
927 if (PrintNullCheckElimination) { | |
928 tty->print_cr("Eliminated StoreIndexed %d's null check for value %d", x->id(), array->id()); | |
929 } | |
930 x->set_needs_null_check(false); | |
931 } else { | |
932 set_put(array); | |
933 if (PrintNullCheckElimination) { | |
934 tty->print_cr("StoreIndexed %d of value %d proves value to be non-null", x->id(), array->id()); | |
935 } | |
936 // Ensure previous passes do not cause wrong state | |
937 x->set_needs_null_check(true); | |
938 } | |
939 clear_last_explicit_null_check(); | |
940 } | |
941 | |
942 | |
943 void NullCheckEliminator::handle_NullCheck(NullCheck* x) { | |
944 Value obj = x->obj(); | |
945 if (set_contains(obj)) { | |
946 // Already proven to be non-null => this NullCheck is useless | |
947 if (PrintNullCheckElimination) { | |
948 tty->print_cr("Eliminated NullCheck %d for value %d", x->id(), obj->id()); | |
949 } | |
950 // Don't unpin since that may shrink obj's live range and make it unavailable for debug info. | |
951 // The code generator won't emit LIR for a NullCheck that cannot trap. | |
952 x->set_can_trap(false); | |
953 } else { | |
954 // May be null => add to map and set last explicit NullCheck | |
955 x->set_can_trap(true); | |
956 // make sure it's pinned if it can trap | |
957 x->pin(Instruction::PinExplicitNullCheck); | |
958 set_put(obj); | |
959 set_last_explicit_null_check(x); | |
960 if (PrintNullCheckElimination) { | |
961 tty->print_cr("NullCheck %d of value %d proves value to be non-null", x->id(), obj->id()); | |
962 } | |
963 } | |
964 } | |
965 | |
966 | |
967 void NullCheckEliminator::handle_Invoke(Invoke* x) { | |
968 if (!x->has_receiver()) { | |
969 // Be conservative | |
970 clear_last_explicit_null_check(); | |
971 return; | |
972 } | |
973 | |
974 Value recv = x->receiver(); | |
975 if (!set_contains(recv)) { | |
976 set_put(recv); | |
977 if (PrintNullCheckElimination) { | |
978 tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id()); | |
979 } | |
980 } | |
981 clear_last_explicit_null_check(); | |
982 } | |
983 | |
984 | |
985 void NullCheckEliminator::handle_NewInstance(NewInstance* x) { | |
986 set_put(x); | |
987 if (PrintNullCheckElimination) { | |
988 tty->print_cr("NewInstance %d is non-null", x->id()); | |
989 } | |
990 } | |
991 | |
992 | |
993 void NullCheckEliminator::handle_NewArray(NewArray* x) { | |
994 set_put(x); | |
995 if (PrintNullCheckElimination) { | |
996 tty->print_cr("NewArray %d is non-null", x->id()); | |
997 } | |
998 } | |
999 | |
1000 | |
1001 void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) { | |
1002 set_put(x); | |
1003 if (PrintNullCheckElimination) { | |
1004 tty->print_cr("ExceptionObject %d is non-null", x->id()); | |
1005 } | |
1006 } | |
1007 | |
1008 | |
1009 void NullCheckEliminator::handle_AccessMonitor(AccessMonitor* x) { | |
1010 Value obj = x->obj(); | |
1011 if (set_contains(obj)) { | |
1012 // Value is non-null => update AccessMonitor | |
1013 if (PrintNullCheckElimination) { | |
1014 tty->print_cr("Eliminated AccessMonitor %d's null check for value %d", x->id(), obj->id()); | |
1015 } | |
1016 x->set_needs_null_check(false); | |
1017 } else { | |
1018 set_put(obj); | |
1019 if (PrintNullCheckElimination) { | |
1020 tty->print_cr("AccessMonitor %d of value %d proves value to be non-null", x->id(), obj->id()); | |
1021 } | |
1022 // Ensure previous passes do not cause wrong state | |
1023 x->set_needs_null_check(true); | |
1024 } | |
1025 clear_last_explicit_null_check(); | |
1026 } | |
1027 | |
1028 | |
1029 void NullCheckEliminator::handle_Intrinsic(Intrinsic* x) { | |
1030 if (!x->has_receiver()) { | |
2446 | 1031 if (x->id() == vmIntrinsics::_arraycopy) { |
1032 for (int i = 0; i < x->number_of_arguments(); i++) { | |
1033 x->set_arg_needs_null_check(i, !set_contains(x->argument_at(i))); | |
1034 } | |
1035 } | |
1036 | |
0 | 1037 // Be conservative |
1038 clear_last_explicit_null_check(); | |
1039 return; | |
1040 } | |
1041 | |
1042 Value recv = x->receiver(); | |
1043 if (set_contains(recv)) { | |
1044 // Value is non-null => update Intrinsic | |
1045 if (PrintNullCheckElimination) { | |
1046 tty->print_cr("Eliminated Intrinsic %d's null check for value %d", x->id(), recv->id()); | |
1047 } | |
1048 x->set_needs_null_check(false); | |
1049 } else { | |
1050 set_put(recv); | |
1051 if (PrintNullCheckElimination) { | |
1052 tty->print_cr("Intrinsic %d of value %d proves value to be non-null", x->id(), recv->id()); | |
1053 } | |
1054 // Ensure previous passes do not cause wrong state | |
1055 x->set_needs_null_check(true); | |
1056 } | |
1057 clear_last_explicit_null_check(); | |
1058 } | |
1059 | |
1060 | |
1061 void NullCheckEliminator::handle_Phi(Phi* x) { | |
1062 int i; | |
1063 bool all_non_null = true; | |
1064 if (x->is_illegal()) { | |
1065 all_non_null = false; | |
1066 } else { | |
1067 for (i = 0; i < x->operand_count(); i++) { | |
1068 Value input = x->operand_at(i); | |
1069 if (!set_contains(input)) { | |
1070 all_non_null = false; | |
1071 } | |
1072 } | |
1073 } | |
1074 | |
1075 if (all_non_null) { | |
1076 // Value is non-null => update Phi | |
1077 if (PrintNullCheckElimination) { | |
1078 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id()); | |
1079 } | |
1080 x->set_needs_null_check(false); | |
1081 } else if (set_contains(x)) { | |
1082 set_remove(x); | |
1083 } | |
1084 } | |
1085 | |
1086 | |
1087 void Optimizer::eliminate_null_checks() { | |
1088 ResourceMark rm; | |
1089 | |
1090 NullCheckEliminator nce(this); | |
1091 | |
1092 if (PrintNullCheckElimination) { | |
1093 tty->print_cr("Starting null check elimination for method %s::%s%s", | |
1094 ir()->method()->holder()->name()->as_utf8(), | |
1095 ir()->method()->name()->as_utf8(), | |
1096 ir()->method()->signature()->as_symbol()->as_utf8()); | |
1097 } | |
1098 | |
1099 // Apply to graph | |
1100 nce.iterate(ir()->start()); | |
1101 | |
1102 // walk over the graph looking for exception | |
1103 // handlers and iterate over them as well | |
1104 int nblocks = BlockBegin::number_of_blocks(); | |
1105 BlockList blocks(nblocks); | |
1106 boolArray visited_block(nblocks, false); | |
1107 | |
1108 blocks.push(ir()->start()); | |
1109 visited_block[ir()->start()->block_id()] = true; | |
1110 for (int i = 0; i < blocks.length(); i++) { | |
1111 BlockBegin* b = blocks[i]; | |
1112 // exception handlers need to be treated as additional roots | |
1113 for (int e = b->number_of_exception_handlers(); e-- > 0; ) { | |
1114 BlockBegin* excp = b->exception_handler_at(e); | |
1115 int id = excp->block_id(); | |
1116 if (!visited_block[id]) { | |
1117 blocks.push(excp); | |
1118 visited_block[id] = true; | |
1119 nce.iterate(excp); | |
1120 } | |
1121 } | |
1122 // traverse successors | |
1123 BlockEnd *end = b->end(); | |
1124 for (int s = end->number_of_sux(); s-- > 0; ) { | |
1125 BlockBegin* next = end->sux_at(s); | |
1126 int id = next->block_id(); | |
1127 if (!visited_block[id]) { | |
1128 blocks.push(next); | |
1129 visited_block[id] = true; | |
1130 } | |
1131 } | |
1132 } | |
1133 | |
1134 | |
1135 if (PrintNullCheckElimination) { | |
1136 tty->print_cr("Done with null check elimination for method %s::%s%s", | |
1137 ir()->method()->holder()->name()->as_utf8(), | |
1138 ir()->method()->name()->as_utf8(), | |
1139 ir()->method()->signature()->as_symbol()->as_utf8()); | |
1140 } | |
1141 } |