Tue, 29 Dec 2009 19:08:54 +0100
6986046: C1 valuestack cleanup
Summary: fixes an historical oddity in C1 with inlining where all of the expression stacks are kept in the topmost ValueStack instead of being in their respective ValueStacks.
Reviewed-by: never
Contributed-by: Christian Wimmer <cwimmer@uci.edu>
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 "incls/_precompiled.incl"
26 #include "incls/_c1_Instruction.cpp.incl"
29 // Implementation of Instruction
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 }
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 }
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 }
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 }
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 }
90 #ifndef PRODUCT
91 void Instruction::check_state(ValueStack* state) {
92 if (state != NULL) {
93 state->verify();
94 }
95 }
98 void Instruction::print() {
99 InstructionPrinter ip;
100 print(ip);
101 }
104 void Instruction::print_line() {
105 InstructionPrinter ip;
106 ip.print_line(this);
107 }
110 void Instruction::print(InstructionPrinter& ip) {
111 ip.print_head();
112 ip.print_line(this);
113 tty->cr();
114 }
115 #endif // PRODUCT
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 }
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;
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 }
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 }
162 ciType* LoadField::declared_type() const {
163 return field()->type();
164 }
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 }
183 ciType* NewTypeArray::exact_type() const {
184 return ciTypeArrayKlass::make(elt_type());
185 }
188 ciType* NewObjectArray::exact_type() const {
189 return ciObjArrayKlass::make(klass());
190 }
193 ciType* NewInstance::exact_type() const {
194 return klass();
195 }
198 ciType* CheckCast::declared_type() const {
199 return klass();
200 }
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 }
212 // Implementation of ArithmeticOp
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 }
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 }
240 // Implementation of LogicOp
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 }
259 // Implementation of IfOp
261 bool IfOp::is_commutative() const {
262 return cond() == eql || cond() == neq;
263 }
266 // Implementation of StateSplit
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 }
281 IRScope* StateSplit::scope() const {
282 return _state->scope();
283 }
286 void StateSplit::state_values_do(ValueVisitor* f) {
287 Instruction::state_values_do(f);
288 if (state() != NULL) state()->values_do(f);
289 }
292 void BlockBegin::state_values_do(ValueVisitor* f) {
293 StateSplit::state_values_do(f);
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 }
303 // Implementation of Invoke
306 Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args,
307 int vtable_index, ciMethod* target, ValueStack* state_before)
308 : StateSplit(result_type, state_before)
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());
319 assert(args != NULL, "args must exist");
320 #ifdef ASSERT
321 AssertValues assert_value;
322 values_do(&assert_value);
323 #endif
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()));
329 } else if (is_invokedynamic()) {
330 // Add the synthetic MethodHandle argument to the signature.
331 _signature->append(T_OBJECT);
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 }
341 void Invoke::state_values_do(ValueVisitor* f) {
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 }
348 // Implementation of Contant
349 intx Constant::hash() const {
350 if (state_before() == NULL) {
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 }
374 bool Constant::is_equal(Value v) const {
375 if (v->as_Constant() == NULL) return false;
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 }
419 BlockBegin* Constant::compare(Instruction::Condition cond, Value right,
420 BlockBegin* true_sux, BlockBegin* false_sux) {
421 Constant* rc = right->as_Constant();
422 // other is not a constant
423 if (rc == NULL) return NULL;
425 ValueType* lt = type();
426 ValueType* rt = rc->type();
427 // different types
428 if (lt->base() != rt->base()) return NULL;
429 switch (lt->tag()) {
430 case intTag: {
431 int x = lt->as_IntConstant()->value();
432 int y = rt->as_IntConstant()->value();
433 switch (cond) {
434 case If::eql: return x == y ? true_sux : false_sux;
435 case If::neq: return x != y ? true_sux : false_sux;
436 case If::lss: return x < y ? true_sux : false_sux;
437 case If::leq: return x <= y ? true_sux : false_sux;
438 case If::gtr: return x > y ? true_sux : false_sux;
439 case If::geq: return x >= y ? true_sux : false_sux;
440 }
441 break;
442 }
443 case longTag: {
444 jlong x = lt->as_LongConstant()->value();
445 jlong y = rt->as_LongConstant()->value();
446 switch (cond) {
447 case If::eql: return x == y ? true_sux : false_sux;
448 case If::neq: return x != y ? true_sux : false_sux;
449 case If::lss: return x < y ? true_sux : false_sux;
450 case If::leq: return x <= y ? true_sux : false_sux;
451 case If::gtr: return x > y ? true_sux : false_sux;
452 case If::geq: return x >= y ? true_sux : false_sux;
453 }
454 break;
455 }
456 case objectTag: {
457 ciObject* xvalue = lt->as_ObjectType()->constant_value();
458 ciObject* yvalue = rt->as_ObjectType()->constant_value();
459 assert(xvalue != NULL && yvalue != NULL, "not constants");
460 if (xvalue->is_loaded() && yvalue->is_loaded()) {
461 switch (cond) {
462 case If::eql: return xvalue == yvalue ? true_sux : false_sux;
463 case If::neq: return xvalue != yvalue ? true_sux : false_sux;
464 }
465 }
466 break;
467 }
468 }
469 return NULL;
470 }
473 // Implementation of BlockBegin
475 void BlockBegin::set_end(BlockEnd* end) {
476 assert(end != NULL, "should not reset block end to NULL");
477 BlockEnd* old_end = _end;
478 if (end == old_end) {
479 return;
480 }
481 // Must make the predecessors/successors match up with the
482 // BlockEnd's notion.
483 int i, n;
484 if (old_end != NULL) {
485 // disconnect from the old end
486 old_end->set_begin(NULL);
488 // disconnect this block from it's current successors
489 for (i = 0; i < _successors.length(); i++) {
490 _successors.at(i)->remove_predecessor(this);
491 }
492 }
493 _end = end;
495 _successors.clear();
496 // Now reset successors list based on BlockEnd
497 n = end->number_of_sux();
498 for (i = 0; i < n; i++) {
499 BlockBegin* sux = end->sux_at(i);
500 _successors.append(sux);
501 sux->_predecessors.append(this);
502 }
503 _end->set_begin(this);
504 }
507 void BlockBegin::disconnect_edge(BlockBegin* from, BlockBegin* to) {
508 // disconnect any edges between from and to
509 #ifndef PRODUCT
510 if (PrintIR && Verbose) {
511 tty->print_cr("Disconnected edge B%d -> B%d", from->block_id(), to->block_id());
512 }
513 #endif
514 for (int s = 0; s < from->number_of_sux();) {
515 BlockBegin* sux = from->sux_at(s);
516 if (sux == to) {
517 int index = sux->_predecessors.index_of(from);
518 if (index >= 0) {
519 sux->_predecessors.remove_at(index);
520 }
521 from->_successors.remove_at(s);
522 } else {
523 s++;
524 }
525 }
526 }
529 void BlockBegin::disconnect_from_graph() {
530 // disconnect this block from all other blocks
531 for (int p = 0; p < number_of_preds(); p++) {
532 pred_at(p)->remove_successor(this);
533 }
534 for (int s = 0; s < number_of_sux(); s++) {
535 sux_at(s)->remove_predecessor(this);
536 }
537 }
539 void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
540 // modify predecessors before substituting successors
541 for (int i = 0; i < number_of_sux(); i++) {
542 if (sux_at(i) == old_sux) {
543 // remove old predecessor before adding new predecessor
544 // otherwise there is a dead predecessor in the list
545 new_sux->remove_predecessor(old_sux);
546 new_sux->add_predecessor(this);
547 }
548 }
549 old_sux->remove_predecessor(this);
550 end()->substitute_sux(old_sux, new_sux);
551 }
555 // In general it is not possible to calculate a value for the field "depth_first_number"
556 // of the inserted block, without recomputing the values of the other blocks
557 // in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless.
558 BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) {
559 BlockBegin* new_sux = new BlockBegin(-99);
561 // mark this block (special treatment when block order is computed)
562 new_sux->set(critical_edge_split_flag);
564 // This goto is not a safepoint.
565 Goto* e = new Goto(sux, false);
566 new_sux->set_next(e, end()->state()->bci());
567 new_sux->set_end(e);
568 // setup states
569 ValueStack* s = end()->state();
570 new_sux->set_state(s->copy());
571 e->set_state(s->copy());
572 assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!");
573 assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!");
574 assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!");
576 // link predecessor to new block
577 end()->substitute_sux(sux, new_sux);
579 // The ordering needs to be the same, so remove the link that the
580 // set_end call above added and substitute the new_sux for this
581 // block.
582 sux->remove_predecessor(new_sux);
584 // the successor could be the target of a switch so it might have
585 // multiple copies of this predecessor, so substitute the new_sux
586 // for the first and delete the rest.
587 bool assigned = false;
588 BlockList& list = sux->_predecessors;
589 for (int i = 0; i < list.length(); i++) {
590 BlockBegin** b = list.adr_at(i);
591 if (*b == this) {
592 if (assigned) {
593 list.remove_at(i);
594 // reprocess this index
595 i--;
596 } else {
597 assigned = true;
598 *b = new_sux;
599 }
600 // link the new block back to it's predecessors.
601 new_sux->add_predecessor(this);
602 }
603 }
604 assert(assigned == true, "should have assigned at least once");
605 return new_sux;
606 }
609 void BlockBegin::remove_successor(BlockBegin* pred) {
610 int idx;
611 while ((idx = _successors.index_of(pred)) >= 0) {
612 _successors.remove_at(idx);
613 }
614 }
617 void BlockBegin::add_predecessor(BlockBegin* pred) {
618 _predecessors.append(pred);
619 }
622 void BlockBegin::remove_predecessor(BlockBegin* pred) {
623 int idx;
624 while ((idx = _predecessors.index_of(pred)) >= 0) {
625 _predecessors.remove_at(idx);
626 }
627 }
630 void BlockBegin::add_exception_handler(BlockBegin* b) {
631 assert(b != NULL && (b->is_set(exception_entry_flag)), "exception handler must exist");
632 // add only if not in the list already
633 if (!_exception_handlers.contains(b)) _exception_handlers.append(b);
634 }
636 int BlockBegin::add_exception_state(ValueStack* state) {
637 assert(is_set(exception_entry_flag), "only for xhandlers");
638 if (_exception_states == NULL) {
639 _exception_states = new ValueStackStack(4);
640 }
641 _exception_states->append(state);
642 return _exception_states->length() - 1;
643 }
646 void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) {
647 if (!mark.at(block_id())) {
648 mark.at_put(block_id(), true);
649 closure->block_do(this);
650 BlockEnd* e = end(); // must do this after block_do because block_do may change it!
651 { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); }
652 { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_preorder(mark, closure); }
653 }
654 }
657 void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) {
658 if (!mark.at(block_id())) {
659 mark.at_put(block_id(), true);
660 BlockEnd* e = end();
661 { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); }
662 { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_postorder(mark, closure); }
663 closure->block_do(this);
664 }
665 }
668 void BlockBegin::iterate_preorder(BlockClosure* closure) {
669 boolArray mark(number_of_blocks(), false);
670 iterate_preorder(mark, closure);
671 }
674 void BlockBegin::iterate_postorder(BlockClosure* closure) {
675 boolArray mark(number_of_blocks(), false);
676 iterate_postorder(mark, closure);
677 }
680 void BlockBegin::block_values_do(ValueVisitor* f) {
681 for (Instruction* n = this; n != NULL; n = n->next()) n->values_do(f);
682 }
685 #ifndef PRODUCT
686 #define TRACE_PHI(code) if (PrintPhiFunctions) { code; }
687 #else
688 #define TRACE_PHI(coce)
689 #endif
692 bool BlockBegin::try_merge(ValueStack* new_state) {
693 TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id()));
695 // local variables used for state iteration
696 int index;
697 Value new_value, existing_value;
699 ValueStack* existing_state = state();
700 if (existing_state == NULL) {
701 TRACE_PHI(tty->print_cr("first call of try_merge for this block"));
703 if (is_set(BlockBegin::was_visited_flag)) {
704 // this actually happens for complicated jsr/ret structures
705 return false; // BAILOUT in caller
706 }
708 // copy state because it is altered
709 new_state = new_state->copy(ValueStack::BlockBeginState, bci());
711 // Use method liveness to invalidate dead locals
712 MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci());
713 if (liveness.is_valid()) {
714 assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness");
716 for_each_local_value(new_state, index, new_value) {
717 if (!liveness.at(index) || new_value->type()->is_illegal()) {
718 new_state->invalidate_local(index);
719 TRACE_PHI(tty->print_cr("invalidating dead local %d", index));
720 }
721 }
722 }
724 if (is_set(BlockBegin::parser_loop_header_flag)) {
725 TRACE_PHI(tty->print_cr("loop header block, initializing phi functions"));
727 for_each_stack_value(new_state, index, new_value) {
728 new_state->setup_phi_for_stack(this, index);
729 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));
730 }
732 BitMap requires_phi_function = new_state->scope()->requires_phi_function();
734 for_each_local_value(new_state, index, new_value) {
735 bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1));
736 if (requires_phi || !SelectivePhiFunctions) {
737 new_state->setup_phi_for_local(this, index);
738 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));
739 }
740 }
741 }
743 // initialize state of block
744 set_state(new_state);
746 } else if (existing_state->is_same(new_state)) {
747 TRACE_PHI(tty->print_cr("exisiting state found"));
749 assert(existing_state->scope() == new_state->scope(), "not matching");
750 assert(existing_state->locals_size() == new_state->locals_size(), "not matching");
751 assert(existing_state->stack_size() == new_state->stack_size(), "not matching");
753 if (is_set(BlockBegin::was_visited_flag)) {
754 TRACE_PHI(tty->print_cr("loop header block, phis must be present"));
756 if (!is_set(BlockBegin::parser_loop_header_flag)) {
757 // this actually happens for complicated jsr/ret structures
758 return false; // BAILOUT in caller
759 }
761 for_each_local_value(existing_state, index, existing_value) {
762 Value new_value = new_state->local_at(index);
763 if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) {
764 // The old code invalidated the phi function here
765 // Because dead locals are replaced with NULL, this is a very rare case now, so simply bail out
766 return false; // BAILOUT in caller
767 }
768 }
770 #ifdef ASSERT
771 // check that all necessary phi functions are present
772 for_each_stack_value(existing_state, index, existing_value) {
773 assert(existing_value->as_Phi() != NULL && existing_value->as_Phi()->block() == this, "phi function required");
774 }
775 for_each_local_value(existing_state, index, existing_value) {
776 assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != NULL && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required");
777 }
778 #endif
780 } else {
781 TRACE_PHI(tty->print_cr("creating phi functions on demand"));
783 // create necessary phi functions for stack
784 for_each_stack_value(existing_state, index, existing_value) {
785 Value new_value = new_state->stack_at(index);
786 Phi* existing_phi = existing_value->as_Phi();
788 if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) {
789 existing_state->setup_phi_for_stack(this, index);
790 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));
791 }
792 }
794 // create necessary phi functions for locals
795 for_each_local_value(existing_state, index, existing_value) {
796 Value new_value = new_state->local_at(index);
797 Phi* existing_phi = existing_value->as_Phi();
799 if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) {
800 existing_state->invalidate_local(index);
801 TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index));
802 } else if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) {
803 existing_state->setup_phi_for_local(this, index);
804 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));
805 }
806 }
807 }
809 assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal");
811 } else {
812 assert(false, "stack or locks not matching (invalid bytecodes)");
813 return false;
814 }
816 TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id()));
818 return true;
819 }
822 #ifndef PRODUCT
823 void BlockBegin::print_block() {
824 InstructionPrinter ip;
825 print_block(ip, false);
826 }
829 void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) {
830 ip.print_instr(this); tty->cr();
831 ip.print_stack(this->state()); tty->cr();
832 ip.print_inline_level(this);
833 ip.print_head();
834 for (Instruction* n = next(); n != NULL; n = n->next()) {
835 if (!live_only || n->is_pinned() || n->use_count() > 0) {
836 ip.print_line(n);
837 }
838 }
839 tty->cr();
840 }
841 #endif // PRODUCT
844 // Implementation of BlockList
846 void BlockList::iterate_forward (BlockClosure* closure) {
847 const int l = length();
848 for (int i = 0; i < l; i++) closure->block_do(at(i));
849 }
852 void BlockList::iterate_backward(BlockClosure* closure) {
853 for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i));
854 }
857 void BlockList::blocks_do(void f(BlockBegin*)) {
858 for (int i = length() - 1; i >= 0; i--) f(at(i));
859 }
862 void BlockList::values_do(ValueVisitor* f) {
863 for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f);
864 }
867 #ifndef PRODUCT
868 void BlockList::print(bool cfg_only, bool live_only) {
869 InstructionPrinter ip;
870 for (int i = 0; i < length(); i++) {
871 BlockBegin* block = at(i);
872 if (cfg_only) {
873 ip.print_instr(block); tty->cr();
874 } else {
875 block->print_block(ip, live_only);
876 }
877 }
878 }
879 #endif // PRODUCT
882 // Implementation of BlockEnd
884 void BlockEnd::set_begin(BlockBegin* begin) {
885 BlockList* sux = NULL;
886 if (begin != NULL) {
887 sux = begin->successors();
888 } else if (_begin != NULL) {
889 // copy our sux list
890 BlockList* sux = new BlockList(_begin->number_of_sux());
891 for (int i = 0; i < _begin->number_of_sux(); i++) {
892 sux->append(_begin->sux_at(i));
893 }
894 }
895 _sux = sux;
896 _begin = begin;
897 }
900 void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
901 substitute(*_sux, old_sux, new_sux);
902 }
905 // Implementation of Phi
907 // Normal phi functions take their operands from the last instruction of the
908 // predecessor. Special handling is needed for xhanlder entries because there
909 // the state of arbitrary instructions are needed.
911 Value Phi::operand_at(int i) const {
912 ValueStack* state;
913 if (_block->is_set(BlockBegin::exception_entry_flag)) {
914 state = _block->exception_state_at(i);
915 } else {
916 state = _block->pred_at(i)->end()->state();
917 }
918 assert(state != NULL, "");
920 if (is_local()) {
921 return state->local_at(local_index());
922 } else {
923 return state->stack_at(stack_index());
924 }
925 }
928 int Phi::operand_count() const {
929 if (_block->is_set(BlockBegin::exception_entry_flag)) {
930 return _block->number_of_exception_states();
931 } else {
932 return _block->number_of_preds();
933 }
934 }
938 void ProfileInvoke::state_values_do(ValueVisitor* f) {
939 if (state() != NULL) state()->values_do(f);
940 }