Mercurial > hg > truffle
annotate src/share/vm/c1/c1_Instruction.cpp @ 1899:42a10fc37986
6991577: add IfOp optimization to C1
Summary: Ifop optimization for c1
Reviewed-by: never, phh, iveresov
author | roland |
---|---|
date | Fri, 15 Oct 2010 09:38:20 +0200 |
parents | f02a8bbe6ed4 |
children | f95d63e2154a |
rev | line source |
---|---|
0 | 1 /* |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1295
diff
changeset
|
2 * Copyright (c) 1999, 2010, 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:
1295
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1295
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:
1295
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 #include "incls/_precompiled.incl" | |
26 #include "incls/_c1_Instruction.cpp.incl" | |
27 | |
28 | |
29 // Implementation of Instruction | |
30 | |
31 | |
32 Instruction::Condition Instruction::mirror(Condition cond) { | |
33 switch (cond) { | |
34 case eql: return eql; | |
35 case neq: return neq; | |
36 case lss: return gtr; | |
37 case leq: return geq; | |
38 case gtr: return lss; | |
39 case geq: return leq; | |
40 } | |
41 ShouldNotReachHere(); | |
42 return eql; | |
43 } | |
44 | |
45 | |
46 Instruction::Condition Instruction::negate(Condition cond) { | |
47 switch (cond) { | |
48 case eql: return neq; | |
49 case neq: return eql; | |
50 case lss: return geq; | |
51 case leq: return gtr; | |
52 case gtr: return leq; | |
53 case geq: return lss; | |
54 } | |
55 ShouldNotReachHere(); | |
56 return eql; | |
57 } | |
58 | |
1819 | 59 void Instruction::update_exception_state(ValueStack* state) { |
60 if (state != NULL && (state->kind() == ValueStack::EmptyExceptionState || state->kind() == ValueStack::ExceptionState)) { | |
61 assert(state->kind() == ValueStack::EmptyExceptionState || Compilation::current()->env()->jvmti_can_access_local_variables(), "unexpected state kind"); | |
62 _exception_state = state; | |
63 } else { | |
64 _exception_state = NULL; | |
65 } | |
66 } | |
67 | |
0 | 68 |
69 Instruction* Instruction::prev(BlockBegin* block) { | |
70 Instruction* p = NULL; | |
71 Instruction* q = block; | |
72 while (q != this) { | |
73 assert(q != NULL, "this is not in the block's instruction list"); | |
74 p = q; q = q->next(); | |
75 } | |
76 return p; | |
77 } | |
78 | |
79 | |
1819 | 80 void Instruction::state_values_do(ValueVisitor* f) { |
81 if (state_before() != NULL) { | |
82 state_before()->values_do(f); | |
83 } | |
84 if (exception_state() != NULL){ | |
85 exception_state()->values_do(f); | |
86 } | |
87 } | |
88 | |
89 | |
0 | 90 #ifndef PRODUCT |
1819 | 91 void Instruction::check_state(ValueStack* state) { |
92 if (state != NULL) { | |
93 state->verify(); | |
94 } | |
95 } | |
96 | |
97 | |
0 | 98 void Instruction::print() { |
99 InstructionPrinter ip; | |
100 print(ip); | |
101 } | |
102 | |
103 | |
104 void Instruction::print_line() { | |
105 InstructionPrinter ip; | |
106 ip.print_line(this); | |
107 } | |
108 | |
109 | |
110 void Instruction::print(InstructionPrinter& ip) { | |
111 ip.print_head(); | |
112 ip.print_line(this); | |
113 tty->cr(); | |
114 } | |
115 #endif // PRODUCT | |
116 | |
117 | |
118 // perform constant and interval tests on index value | |
119 bool AccessIndexed::compute_needs_range_check() { | |
120 Constant* clength = length()->as_Constant(); | |
121 Constant* cindex = index()->as_Constant(); | |
122 if (clength && cindex) { | |
123 IntConstant* l = clength->type()->as_IntConstant(); | |
124 IntConstant* i = cindex->type()->as_IntConstant(); | |
125 if (l && i && i->value() < l->value() && i->value() >= 0) { | |
126 return false; | |
127 } | |
128 } | |
129 return true; | |
130 } | |
131 | |
132 | |
133 ciType* LoadIndexed::exact_type() const { | |
134 ciType* array_type = array()->exact_type(); | |
135 if (array_type == NULL) { | |
136 return NULL; | |
137 } | |
138 assert(array_type->is_array_klass(), "what else?"); | |
139 ciArrayKlass* ak = (ciArrayKlass*)array_type; | |
140 | |
141 if (ak->element_type()->is_instance_klass()) { | |
142 ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type(); | |
143 if (ik->is_loaded() && ik->is_final()) { | |
144 return ik; | |
145 } | |
146 } | |
147 return NULL; | |
148 } | |
149 | |
150 | |
151 ciType* LoadIndexed::declared_type() const { | |
152 ciType* array_type = array()->declared_type(); | |
153 if (array_type == NULL) { | |
154 return NULL; | |
155 } | |
156 assert(array_type->is_array_klass(), "what else?"); | |
157 ciArrayKlass* ak = (ciArrayKlass*)array_type; | |
158 return ak->element_type(); | |
159 } | |
160 | |
161 | |
162 ciType* LoadField::declared_type() const { | |
163 return field()->type(); | |
164 } | |
165 | |
166 | |
167 ciType* LoadField::exact_type() const { | |
168 ciType* type = declared_type(); | |
169 // for primitive arrays, the declared type is the exact type | |
170 if (type->is_type_array_klass()) { | |
171 return type; | |
172 } | |
173 if (type->is_instance_klass()) { | |
174 ciInstanceKlass* ik = (ciInstanceKlass*)type; | |
175 if (ik->is_loaded() && ik->is_final()) { | |
176 return type; | |
177 } | |
178 } | |
179 return NULL; | |
180 } | |
181 | |
182 | |
183 ciType* NewTypeArray::exact_type() const { | |
184 return ciTypeArrayKlass::make(elt_type()); | |
185 } | |
186 | |
187 | |
188 ciType* NewObjectArray::exact_type() const { | |
189 return ciObjArrayKlass::make(klass()); | |
190 } | |
191 | |
192 | |
193 ciType* NewInstance::exact_type() const { | |
194 return klass(); | |
195 } | |
196 | |
197 | |
198 ciType* CheckCast::declared_type() const { | |
199 return klass(); | |
200 } | |
201 | |
202 ciType* CheckCast::exact_type() const { | |
203 if (klass()->is_instance_klass()) { | |
204 ciInstanceKlass* ik = (ciInstanceKlass*)klass(); | |
205 if (ik->is_loaded() && ik->is_final()) { | |
206 return ik; | |
207 } | |
208 } | |
209 return NULL; | |
210 } | |
211 | |
212 // Implementation of ArithmeticOp | |
213 | |
214 bool ArithmeticOp::is_commutative() const { | |
215 switch (op()) { | |
216 case Bytecodes::_iadd: // fall through | |
217 case Bytecodes::_ladd: // fall through | |
218 case Bytecodes::_fadd: // fall through | |
219 case Bytecodes::_dadd: // fall through | |
220 case Bytecodes::_imul: // fall through | |
221 case Bytecodes::_lmul: // fall through | |
222 case Bytecodes::_fmul: // fall through | |
223 case Bytecodes::_dmul: return true; | |
224 } | |
225 return false; | |
226 } | |
227 | |
228 | |
229 bool ArithmeticOp::can_trap() const { | |
230 switch (op()) { | |
231 case Bytecodes::_idiv: // fall through | |
232 case Bytecodes::_ldiv: // fall through | |
233 case Bytecodes::_irem: // fall through | |
234 case Bytecodes::_lrem: return true; | |
235 } | |
236 return false; | |
237 } | |
238 | |
239 | |
240 // Implementation of LogicOp | |
241 | |
242 bool LogicOp::is_commutative() const { | |
243 #ifdef ASSERT | |
244 switch (op()) { | |
245 case Bytecodes::_iand: // fall through | |
246 case Bytecodes::_land: // fall through | |
247 case Bytecodes::_ior : // fall through | |
248 case Bytecodes::_lor : // fall through | |
249 case Bytecodes::_ixor: // fall through | |
250 case Bytecodes::_lxor: break; | |
251 default : ShouldNotReachHere(); | |
252 } | |
253 #endif | |
254 // all LogicOps are commutative | |
255 return true; | |
256 } | |
257 | |
258 | |
259 // Implementation of IfOp | |
260 | |
261 bool IfOp::is_commutative() const { | |
262 return cond() == eql || cond() == neq; | |
263 } | |
264 | |
265 | |
266 // Implementation of StateSplit | |
267 | |
268 void StateSplit::substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block) { | |
269 NOT_PRODUCT(bool assigned = false;) | |
270 for (int i = 0; i < list.length(); i++) { | |
271 BlockBegin** b = list.adr_at(i); | |
272 if (*b == old_block) { | |
273 *b = new_block; | |
274 NOT_PRODUCT(assigned = true;) | |
275 } | |
276 } | |
277 assert(assigned == true, "should have assigned at least once"); | |
278 } | |
279 | |
280 | |
281 IRScope* StateSplit::scope() const { | |
282 return _state->scope(); | |
283 } | |
284 | |
285 | |
1584 | 286 void StateSplit::state_values_do(ValueVisitor* f) { |
1819 | 287 Instruction::state_values_do(f); |
0 | 288 if (state() != NULL) state()->values_do(f); |
289 } | |
290 | |
291 | |
1584 | 292 void BlockBegin::state_values_do(ValueVisitor* f) { |
0 | 293 StateSplit::state_values_do(f); |
294 | |
295 if (is_set(BlockBegin::exception_entry_flag)) { | |
296 for (int i = 0; i < number_of_exception_states(); i++) { | |
297 exception_state_at(i)->values_do(f); | |
298 } | |
299 } | |
300 } | |
301 | |
302 | |
303 // Implementation of Invoke | |
304 | |
305 | |
306 Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args, | |
1295 | 307 int vtable_index, ciMethod* target, ValueStack* state_before) |
1819 | 308 : StateSplit(result_type, state_before) |
0 | 309 , _code(code) |
310 , _recv(recv) | |
311 , _args(args) | |
312 , _vtable_index(vtable_index) | |
313 , _target(target) | |
314 { | |
315 set_flag(TargetIsLoadedFlag, target->is_loaded()); | |
316 set_flag(TargetIsFinalFlag, target_is_loaded() && target->is_final_method()); | |
317 set_flag(TargetIsStrictfpFlag, target_is_loaded() && target->is_strict()); | |
318 | |
319 assert(args != NULL, "args must exist"); | |
320 #ifdef ASSERT | |
1584 | 321 AssertValues assert_value; |
322 values_do(&assert_value); | |
323 #endif | |
0 | 324 |
325 // provide an initial guess of signature size. | |
326 _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0)); | |
327 if (has_receiver()) { | |
328 _signature->append(as_BasicType(receiver()->type())); | |
1295 | 329 } else if (is_invokedynamic()) { |
330 // Add the synthetic MethodHandle argument to the signature. | |
331 _signature->append(T_OBJECT); | |
0 | 332 } |
333 for (int i = 0; i < number_of_arguments(); i++) { | |
334 ValueType* t = argument_at(i)->type(); | |
335 BasicType bt = as_BasicType(t); | |
336 _signature->append(bt); | |
337 } | |
338 } | |
339 | |
340 | |
1584 | 341 void Invoke::state_values_do(ValueVisitor* f) { |
1295 | 342 StateSplit::state_values_do(f); |
343 if (state_before() != NULL) state_before()->values_do(f); | |
344 if (state() != NULL) state()->values_do(f); | |
345 } | |
346 | |
347 | |
0 | 348 // Implementation of Contant |
349 intx Constant::hash() const { | |
1819 | 350 if (state_before() == NULL) { |
0 | 351 switch (type()->tag()) { |
352 case intTag: | |
353 return HASH2(name(), type()->as_IntConstant()->value()); | |
354 case longTag: | |
355 { | |
356 jlong temp = type()->as_LongConstant()->value(); | |
357 return HASH3(name(), high(temp), low(temp)); | |
358 } | |
359 case floatTag: | |
360 return HASH2(name(), jint_cast(type()->as_FloatConstant()->value())); | |
361 case doubleTag: | |
362 { | |
363 jlong temp = jlong_cast(type()->as_DoubleConstant()->value()); | |
364 return HASH3(name(), high(temp), low(temp)); | |
365 } | |
366 case objectTag: | |
367 assert(type()->as_ObjectType()->is_loaded(), "can't handle unloaded values"); | |
368 return HASH2(name(), type()->as_ObjectType()->constant_value()); | |
369 } | |
370 } | |
371 return 0; | |
372 } | |
373 | |
374 bool Constant::is_equal(Value v) const { | |
375 if (v->as_Constant() == NULL) return false; | |
376 | |
377 switch (type()->tag()) { | |
378 case intTag: | |
379 { | |
380 IntConstant* t1 = type()->as_IntConstant(); | |
381 IntConstant* t2 = v->type()->as_IntConstant(); | |
382 return (t1 != NULL && t2 != NULL && | |
383 t1->value() == t2->value()); | |
384 } | |
385 case longTag: | |
386 { | |
387 LongConstant* t1 = type()->as_LongConstant(); | |
388 LongConstant* t2 = v->type()->as_LongConstant(); | |
389 return (t1 != NULL && t2 != NULL && | |
390 t1->value() == t2->value()); | |
391 } | |
392 case floatTag: | |
393 { | |
394 FloatConstant* t1 = type()->as_FloatConstant(); | |
395 FloatConstant* t2 = v->type()->as_FloatConstant(); | |
396 return (t1 != NULL && t2 != NULL && | |
397 jint_cast(t1->value()) == jint_cast(t2->value())); | |
398 } | |
399 case doubleTag: | |
400 { | |
401 DoubleConstant* t1 = type()->as_DoubleConstant(); | |
402 DoubleConstant* t2 = v->type()->as_DoubleConstant(); | |
403 return (t1 != NULL && t2 != NULL && | |
404 jlong_cast(t1->value()) == jlong_cast(t2->value())); | |
405 } | |
406 case objectTag: | |
407 { | |
408 ObjectType* t1 = type()->as_ObjectType(); | |
409 ObjectType* t2 = v->type()->as_ObjectType(); | |
410 return (t1 != NULL && t2 != NULL && | |
411 t1->is_loaded() && t2->is_loaded() && | |
412 t1->constant_value() == t2->constant_value()); | |
413 } | |
414 } | |
415 return false; | |
416 } | |
417 | |
1899 | 418 Constant::CompareResult Constant::compare(Instruction::Condition cond, Value right) const { |
0 | 419 Constant* rc = right->as_Constant(); |
420 // other is not a constant | |
1899 | 421 if (rc == NULL) return not_comparable; |
0 | 422 |
423 ValueType* lt = type(); | |
424 ValueType* rt = rc->type(); | |
425 // different types | |
1899 | 426 if (lt->base() != rt->base()) return not_comparable; |
0 | 427 switch (lt->tag()) { |
428 case intTag: { | |
429 int x = lt->as_IntConstant()->value(); | |
430 int y = rt->as_IntConstant()->value(); | |
431 switch (cond) { | |
1899 | 432 case If::eql: return x == y ? cond_true : cond_false; |
433 case If::neq: return x != y ? cond_true : cond_false; | |
434 case If::lss: return x < y ? cond_true : cond_false; | |
435 case If::leq: return x <= y ? cond_true : cond_false; | |
436 case If::gtr: return x > y ? cond_true : cond_false; | |
437 case If::geq: return x >= y ? cond_true : cond_false; | |
0 | 438 } |
439 break; | |
440 } | |
441 case longTag: { | |
442 jlong x = lt->as_LongConstant()->value(); | |
443 jlong y = rt->as_LongConstant()->value(); | |
444 switch (cond) { | |
1899 | 445 case If::eql: return x == y ? cond_true : cond_false; |
446 case If::neq: return x != y ? cond_true : cond_false; | |
447 case If::lss: return x < y ? cond_true : cond_false; | |
448 case If::leq: return x <= y ? cond_true : cond_false; | |
449 case If::gtr: return x > y ? cond_true : cond_false; | |
450 case If::geq: return x >= y ? cond_true : cond_false; | |
0 | 451 } |
452 break; | |
453 } | |
454 case objectTag: { | |
455 ciObject* xvalue = lt->as_ObjectType()->constant_value(); | |
456 ciObject* yvalue = rt->as_ObjectType()->constant_value(); | |
457 assert(xvalue != NULL && yvalue != NULL, "not constants"); | |
458 if (xvalue->is_loaded() && yvalue->is_loaded()) { | |
459 switch (cond) { | |
1899 | 460 case If::eql: return xvalue == yvalue ? cond_true : cond_false; |
461 case If::neq: return xvalue != yvalue ? cond_true : cond_false; | |
0 | 462 } |
463 } | |
464 break; | |
465 } | |
466 } | |
1899 | 467 return not_comparable; |
0 | 468 } |
469 | |
470 | |
471 // Implementation of BlockBegin | |
472 | |
473 void BlockBegin::set_end(BlockEnd* end) { | |
474 assert(end != NULL, "should not reset block end to NULL"); | |
475 BlockEnd* old_end = _end; | |
476 if (end == old_end) { | |
477 return; | |
478 } | |
479 // Must make the predecessors/successors match up with the | |
480 // BlockEnd's notion. | |
481 int i, n; | |
482 if (old_end != NULL) { | |
483 // disconnect from the old end | |
484 old_end->set_begin(NULL); | |
485 | |
486 // disconnect this block from it's current successors | |
487 for (i = 0; i < _successors.length(); i++) { | |
488 _successors.at(i)->remove_predecessor(this); | |
489 } | |
490 } | |
491 _end = end; | |
492 | |
493 _successors.clear(); | |
494 // Now reset successors list based on BlockEnd | |
495 n = end->number_of_sux(); | |
496 for (i = 0; i < n; i++) { | |
497 BlockBegin* sux = end->sux_at(i); | |
498 _successors.append(sux); | |
499 sux->_predecessors.append(this); | |
500 } | |
501 _end->set_begin(this); | |
502 } | |
503 | |
504 | |
505 void BlockBegin::disconnect_edge(BlockBegin* from, BlockBegin* to) { | |
506 // disconnect any edges between from and to | |
507 #ifndef PRODUCT | |
508 if (PrintIR && Verbose) { | |
509 tty->print_cr("Disconnected edge B%d -> B%d", from->block_id(), to->block_id()); | |
510 } | |
511 #endif | |
512 for (int s = 0; s < from->number_of_sux();) { | |
513 BlockBegin* sux = from->sux_at(s); | |
514 if (sux == to) { | |
515 int index = sux->_predecessors.index_of(from); | |
516 if (index >= 0) { | |
517 sux->_predecessors.remove_at(index); | |
518 } | |
519 from->_successors.remove_at(s); | |
520 } else { | |
521 s++; | |
522 } | |
523 } | |
524 } | |
525 | |
526 | |
527 void BlockBegin::disconnect_from_graph() { | |
528 // disconnect this block from all other blocks | |
529 for (int p = 0; p < number_of_preds(); p++) { | |
530 pred_at(p)->remove_successor(this); | |
531 } | |
532 for (int s = 0; s < number_of_sux(); s++) { | |
533 sux_at(s)->remove_predecessor(this); | |
534 } | |
535 } | |
536 | |
537 void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) { | |
538 // modify predecessors before substituting successors | |
539 for (int i = 0; i < number_of_sux(); i++) { | |
540 if (sux_at(i) == old_sux) { | |
541 // remove old predecessor before adding new predecessor | |
542 // otherwise there is a dead predecessor in the list | |
543 new_sux->remove_predecessor(old_sux); | |
544 new_sux->add_predecessor(this); | |
545 } | |
546 } | |
547 old_sux->remove_predecessor(this); | |
548 end()->substitute_sux(old_sux, new_sux); | |
549 } | |
550 | |
551 | |
552 | |
553 // In general it is not possible to calculate a value for the field "depth_first_number" | |
554 // of the inserted block, without recomputing the values of the other blocks | |
555 // in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless. | |
556 BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) { | |
1819 | 557 BlockBegin* new_sux = new BlockBegin(-99); |
0 | 558 |
559 // mark this block (special treatment when block order is computed) | |
560 new_sux->set(critical_edge_split_flag); | |
561 | |
562 // This goto is not a safepoint. | |
563 Goto* e = new Goto(sux, false); | |
1819 | 564 new_sux->set_next(e, end()->state()->bci()); |
0 | 565 new_sux->set_end(e); |
566 // setup states | |
567 ValueStack* s = end()->state(); | |
568 new_sux->set_state(s->copy()); | |
569 e->set_state(s->copy()); | |
570 assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!"); | |
571 assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!"); | |
572 assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!"); | |
573 | |
574 // link predecessor to new block | |
575 end()->substitute_sux(sux, new_sux); | |
576 | |
577 // The ordering needs to be the same, so remove the link that the | |
578 // set_end call above added and substitute the new_sux for this | |
579 // block. | |
580 sux->remove_predecessor(new_sux); | |
581 | |
582 // the successor could be the target of a switch so it might have | |
583 // multiple copies of this predecessor, so substitute the new_sux | |
584 // for the first and delete the rest. | |
585 bool assigned = false; | |
586 BlockList& list = sux->_predecessors; | |
587 for (int i = 0; i < list.length(); i++) { | |
588 BlockBegin** b = list.adr_at(i); | |
589 if (*b == this) { | |
590 if (assigned) { | |
591 list.remove_at(i); | |
592 // reprocess this index | |
593 i--; | |
594 } else { | |
595 assigned = true; | |
596 *b = new_sux; | |
597 } | |
598 // link the new block back to it's predecessors. | |
599 new_sux->add_predecessor(this); | |
600 } | |
601 } | |
602 assert(assigned == true, "should have assigned at least once"); | |
603 return new_sux; | |
604 } | |
605 | |
606 | |
607 void BlockBegin::remove_successor(BlockBegin* pred) { | |
608 int idx; | |
609 while ((idx = _successors.index_of(pred)) >= 0) { | |
610 _successors.remove_at(idx); | |
611 } | |
612 } | |
613 | |
614 | |
615 void BlockBegin::add_predecessor(BlockBegin* pred) { | |
616 _predecessors.append(pred); | |
617 } | |
618 | |
619 | |
620 void BlockBegin::remove_predecessor(BlockBegin* pred) { | |
621 int idx; | |
622 while ((idx = _predecessors.index_of(pred)) >= 0) { | |
623 _predecessors.remove_at(idx); | |
624 } | |
625 } | |
626 | |
627 | |
628 void BlockBegin::add_exception_handler(BlockBegin* b) { | |
629 assert(b != NULL && (b->is_set(exception_entry_flag)), "exception handler must exist"); | |
630 // add only if not in the list already | |
631 if (!_exception_handlers.contains(b)) _exception_handlers.append(b); | |
632 } | |
633 | |
634 int BlockBegin::add_exception_state(ValueStack* state) { | |
635 assert(is_set(exception_entry_flag), "only for xhandlers"); | |
636 if (_exception_states == NULL) { | |
637 _exception_states = new ValueStackStack(4); | |
638 } | |
639 _exception_states->append(state); | |
640 return _exception_states->length() - 1; | |
641 } | |
642 | |
643 | |
644 void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) { | |
645 if (!mark.at(block_id())) { | |
646 mark.at_put(block_id(), true); | |
647 closure->block_do(this); | |
648 BlockEnd* e = end(); // must do this after block_do because block_do may change it! | |
649 { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); } | |
650 { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_preorder(mark, closure); } | |
651 } | |
652 } | |
653 | |
654 | |
655 void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) { | |
656 if (!mark.at(block_id())) { | |
657 mark.at_put(block_id(), true); | |
658 BlockEnd* e = end(); | |
659 { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); } | |
660 { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_postorder(mark, closure); } | |
661 closure->block_do(this); | |
662 } | |
663 } | |
664 | |
665 | |
666 void BlockBegin::iterate_preorder(BlockClosure* closure) { | |
667 boolArray mark(number_of_blocks(), false); | |
668 iterate_preorder(mark, closure); | |
669 } | |
670 | |
671 | |
672 void BlockBegin::iterate_postorder(BlockClosure* closure) { | |
673 boolArray mark(number_of_blocks(), false); | |
674 iterate_postorder(mark, closure); | |
675 } | |
676 | |
677 | |
1584 | 678 void BlockBegin::block_values_do(ValueVisitor* f) { |
0 | 679 for (Instruction* n = this; n != NULL; n = n->next()) n->values_do(f); |
680 } | |
681 | |
682 | |
683 #ifndef PRODUCT | |
1783 | 684 #define TRACE_PHI(code) if (PrintPhiFunctions) { code; } |
0 | 685 #else |
1783 | 686 #define TRACE_PHI(coce) |
0 | 687 #endif |
688 | |
689 | |
690 bool BlockBegin::try_merge(ValueStack* new_state) { | |
691 TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id())); | |
692 | |
693 // local variables used for state iteration | |
694 int index; | |
695 Value new_value, existing_value; | |
696 | |
697 ValueStack* existing_state = state(); | |
698 if (existing_state == NULL) { | |
699 TRACE_PHI(tty->print_cr("first call of try_merge for this block")); | |
700 | |
701 if (is_set(BlockBegin::was_visited_flag)) { | |
702 // this actually happens for complicated jsr/ret structures | |
703 return false; // BAILOUT in caller | |
704 } | |
705 | |
706 // copy state because it is altered | |
1819 | 707 new_state = new_state->copy(ValueStack::BlockBeginState, bci()); |
0 | 708 |
709 // Use method liveness to invalidate dead locals | |
710 MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci()); | |
711 if (liveness.is_valid()) { | |
712 assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness"); | |
713 | |
714 for_each_local_value(new_state, index, new_value) { | |
715 if (!liveness.at(index) || new_value->type()->is_illegal()) { | |
716 new_state->invalidate_local(index); | |
717 TRACE_PHI(tty->print_cr("invalidating dead local %d", index)); | |
718 } | |
719 } | |
720 } | |
721 | |
722 if (is_set(BlockBegin::parser_loop_header_flag)) { | |
723 TRACE_PHI(tty->print_cr("loop header block, initializing phi functions")); | |
724 | |
725 for_each_stack_value(new_state, index, new_value) { | |
726 new_state->setup_phi_for_stack(this, index); | |
727 TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", new_state->stack_at(index)->type()->tchar(), new_state->stack_at(index)->id(), index)); | |
728 } | |
729 | |
730 BitMap requires_phi_function = new_state->scope()->requires_phi_function(); | |
731 | |
732 for_each_local_value(new_state, index, new_value) { | |
733 bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1)); | |
734 if (requires_phi || !SelectivePhiFunctions) { | |
735 new_state->setup_phi_for_local(this, index); | |
736 TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", new_state->local_at(index)->type()->tchar(), new_state->local_at(index)->id(), index)); | |
737 } | |
738 } | |
739 } | |
740 | |
741 // initialize state of block | |
742 set_state(new_state); | |
743 | |
1819 | 744 } else if (existing_state->is_same(new_state)) { |
0 | 745 TRACE_PHI(tty->print_cr("exisiting state found")); |
746 | |
747 assert(existing_state->scope() == new_state->scope(), "not matching"); | |
748 assert(existing_state->locals_size() == new_state->locals_size(), "not matching"); | |
749 assert(existing_state->stack_size() == new_state->stack_size(), "not matching"); | |
750 | |
751 if (is_set(BlockBegin::was_visited_flag)) { | |
752 TRACE_PHI(tty->print_cr("loop header block, phis must be present")); | |
753 | |
754 if (!is_set(BlockBegin::parser_loop_header_flag)) { | |
755 // this actually happens for complicated jsr/ret structures | |
756 return false; // BAILOUT in caller | |
757 } | |
758 | |
759 for_each_local_value(existing_state, index, existing_value) { | |
760 Value new_value = new_state->local_at(index); | |
761 if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) { | |
762 // The old code invalidated the phi function here | |
763 // Because dead locals are replaced with NULL, this is a very rare case now, so simply bail out | |
764 return false; // BAILOUT in caller | |
765 } | |
766 } | |
767 | |
768 #ifdef ASSERT | |
769 // check that all necessary phi functions are present | |
770 for_each_stack_value(existing_state, index, existing_value) { | |
771 assert(existing_value->as_Phi() != NULL && existing_value->as_Phi()->block() == this, "phi function required"); | |
772 } | |
773 for_each_local_value(existing_state, index, existing_value) { | |
774 assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != NULL && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required"); | |
775 } | |
776 #endif | |
777 | |
778 } else { | |
779 TRACE_PHI(tty->print_cr("creating phi functions on demand")); | |
780 | |
781 // create necessary phi functions for stack | |
782 for_each_stack_value(existing_state, index, existing_value) { | |
783 Value new_value = new_state->stack_at(index); | |
784 Phi* existing_phi = existing_value->as_Phi(); | |
785 | |
786 if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) { | |
787 existing_state->setup_phi_for_stack(this, index); | |
788 TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", existing_state->stack_at(index)->type()->tchar(), existing_state->stack_at(index)->id(), index)); | |
789 } | |
790 } | |
791 | |
792 // create necessary phi functions for locals | |
793 for_each_local_value(existing_state, index, existing_value) { | |
794 Value new_value = new_state->local_at(index); | |
795 Phi* existing_phi = existing_value->as_Phi(); | |
796 | |
797 if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) { | |
798 existing_state->invalidate_local(index); | |
799 TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index)); | |
800 } else if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) { | |
801 existing_state->setup_phi_for_local(this, index); | |
802 TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", existing_state->local_at(index)->type()->tchar(), existing_state->local_at(index)->id(), index)); | |
803 } | |
804 } | |
805 } | |
806 | |
807 assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal"); | |
808 | |
809 } else { | |
810 assert(false, "stack or locks not matching (invalid bytecodes)"); | |
811 return false; | |
812 } | |
813 | |
814 TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id())); | |
815 | |
816 return true; | |
817 } | |
818 | |
819 | |
820 #ifndef PRODUCT | |
821 void BlockBegin::print_block() { | |
822 InstructionPrinter ip; | |
823 print_block(ip, false); | |
824 } | |
825 | |
826 | |
827 void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) { | |
828 ip.print_instr(this); tty->cr(); | |
829 ip.print_stack(this->state()); tty->cr(); | |
830 ip.print_inline_level(this); | |
831 ip.print_head(); | |
832 for (Instruction* n = next(); n != NULL; n = n->next()) { | |
833 if (!live_only || n->is_pinned() || n->use_count() > 0) { | |
834 ip.print_line(n); | |
835 } | |
836 } | |
837 tty->cr(); | |
838 } | |
839 #endif // PRODUCT | |
840 | |
841 | |
842 // Implementation of BlockList | |
843 | |
844 void BlockList::iterate_forward (BlockClosure* closure) { | |
845 const int l = length(); | |
846 for (int i = 0; i < l; i++) closure->block_do(at(i)); | |
847 } | |
848 | |
849 | |
850 void BlockList::iterate_backward(BlockClosure* closure) { | |
851 for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i)); | |
852 } | |
853 | |
854 | |
855 void BlockList::blocks_do(void f(BlockBegin*)) { | |
856 for (int i = length() - 1; i >= 0; i--) f(at(i)); | |
857 } | |
858 | |
859 | |
1584 | 860 void BlockList::values_do(ValueVisitor* f) { |
0 | 861 for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f); |
862 } | |
863 | |
864 | |
865 #ifndef PRODUCT | |
866 void BlockList::print(bool cfg_only, bool live_only) { | |
867 InstructionPrinter ip; | |
868 for (int i = 0; i < length(); i++) { | |
869 BlockBegin* block = at(i); | |
870 if (cfg_only) { | |
871 ip.print_instr(block); tty->cr(); | |
872 } else { | |
873 block->print_block(ip, live_only); | |
874 } | |
875 } | |
876 } | |
877 #endif // PRODUCT | |
878 | |
879 | |
880 // Implementation of BlockEnd | |
881 | |
882 void BlockEnd::set_begin(BlockBegin* begin) { | |
883 BlockList* sux = NULL; | |
884 if (begin != NULL) { | |
885 sux = begin->successors(); | |
886 } else if (_begin != NULL) { | |
887 // copy our sux list | |
888 BlockList* sux = new BlockList(_begin->number_of_sux()); | |
889 for (int i = 0; i < _begin->number_of_sux(); i++) { | |
890 sux->append(_begin->sux_at(i)); | |
891 } | |
892 } | |
893 _sux = sux; | |
894 _begin = begin; | |
895 } | |
896 | |
897 | |
898 void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) { | |
899 substitute(*_sux, old_sux, new_sux); | |
900 } | |
901 | |
902 | |
903 // Implementation of Phi | |
904 | |
905 // Normal phi functions take their operands from the last instruction of the | |
906 // predecessor. Special handling is needed for xhanlder entries because there | |
907 // the state of arbitrary instructions are needed. | |
908 | |
909 Value Phi::operand_at(int i) const { | |
910 ValueStack* state; | |
911 if (_block->is_set(BlockBegin::exception_entry_flag)) { | |
912 state = _block->exception_state_at(i); | |
913 } else { | |
914 state = _block->pred_at(i)->end()->state(); | |
915 } | |
916 assert(state != NULL, ""); | |
917 | |
918 if (is_local()) { | |
919 return state->local_at(local_index()); | |
920 } else { | |
921 return state->stack_at(stack_index()); | |
922 } | |
923 } | |
924 | |
925 | |
926 int Phi::operand_count() const { | |
927 if (_block->is_set(BlockBegin::exception_entry_flag)) { | |
928 return _block->number_of_exception_states(); | |
929 } else { | |
930 return _block->number_of_preds(); | |
931 } | |
932 } | |
933 | |
934 | |
1783 | 935 |
936 void ProfileInvoke::state_values_do(ValueVisitor* f) { | |
937 if (state() != NULL) state()->values_do(f); | |
938 } |