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