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