1 /*
  2  * Copyright (c) 1999, 2023, 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   int bci = sux->bci();
590   // critical edge splitting may introduce a goto after a if and array
591   // bound check elimination may insert a predicate between the if and
592   // goto. The bci of the goto can't be the one of the if otherwise
593   // the state and bci are inconsistent and a deoptimization triggered
594   // by the predicate would lead to incorrect execution/a crash.
595   BlockBegin* new_sux = new BlockBegin(bci);
596 
597   // mark this block (special treatment when block order is computed)
598   new_sux->set(critical_edge_split_flag);
599 
600   // This goto is not a safepoint.
601   Goto* e = new Goto(sux, false);
602   new_sux->set_next(e, bci);
603   new_sux->set_end(e);
604   // setup states
605   ValueStack* s = end()->state();
606   new_sux->set_state(s->copy(s->kind(), bci));
607   e->set_state(s->copy(s->kind(), bci));
608   assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!");
609   assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!");
610   assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!");
611 
612   // link predecessor to new block
613   end()->substitute_sux(sux, new_sux);
614 
615   // The ordering needs to be the same, so remove the link that the
616   // set_end call above added and substitute the new_sux for this
617   // block.
618   sux->remove_predecessor(new_sux);
619 
620   // the successor could be the target of a switch so it might have
621   // multiple copies of this predecessor, so substitute the new_sux
622   // for the first and delete the rest.
623   bool assigned = false;
624   BlockList& list = sux->_predecessors;
625   for (int i = 0; i < list.length(); i++) {
626     BlockBegin** b = list.adr_at(i);
627     if (*b == this) {
628       if (assigned) {
629         list.remove_at(i);
630         // reprocess this index
631         i--;
632       } else {
633         assigned = true;
634         *b = new_sux;
635       }
636       // link the new block back to it's predecessors.
637       new_sux->add_predecessor(this);
638     }
639   }
640   assert(assigned == true, "should have assigned at least once");
641   return new_sux;
642 }
643 
644 
645 void BlockBegin::add_predecessor(BlockBegin* pred) {
646   _predecessors.append(pred);
647 }
648 
649 
650 void BlockBegin::remove_predecessor(BlockBegin* pred) {
651   int idx;
652   while ((idx = _predecessors.find(pred)) >= 0) {
653     _predecessors.remove_at(idx);
654   }
655 }
656 
657 
658 void BlockBegin::add_exception_handler(BlockBegin* b) {
659   assert(b != nullptr && (b->is_set(exception_entry_flag)), "exception handler must exist");
660   // add only if not in the list already
661   if (!_exception_handlers.contains(b)) _exception_handlers.append(b);
662 }
663 
664 int BlockBegin::add_exception_state(ValueStack* state) {
665   assert(is_set(exception_entry_flag), "only for xhandlers");
666   if (_exception_states == nullptr) {
667     _exception_states = new ValueStackStack(4);
668   }
669   _exception_states->append(state);
670   return _exception_states->length() - 1;
671 }
672 
673 
674 void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) {
675   if (!mark.at(block_id())) {
676     mark.at_put(block_id(), true);
677     closure->block_do(this);
678     BlockEnd* e = end(); // must do this after block_do because block_do may change it!
679     { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); }
680     { for (int i = e->number_of_sux            () - 1; i >= 0; i--) e->sux_at           (i)->iterate_preorder(mark, closure); }
681   }
682 }
683 
684 
685 void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) {
686   if (!mark.at(block_id())) {
687     mark.at_put(block_id(), true);
688     BlockEnd* e = end();
689     { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); }
690     { for (int i = e->number_of_sux            () - 1; i >= 0; i--) e->sux_at           (i)->iterate_postorder(mark, closure); }
691     closure->block_do(this);
692   }
693 }
694 
695 
696 void BlockBegin::iterate_preorder(BlockClosure* closure) {
697   int mark_len = number_of_blocks();
698   boolArray mark(mark_len, mark_len, false);
699   iterate_preorder(mark, closure);
700 }
701 
702 
703 void BlockBegin::iterate_postorder(BlockClosure* closure) {
704   int mark_len = number_of_blocks();
705   boolArray mark(mark_len, mark_len, false);
706   iterate_postorder(mark, closure);
707 }
708 
709 
710 void BlockBegin::block_values_do(ValueVisitor* f) {
711   for (Instruction* n = this; n != nullptr; n = n->next()) n->values_do(f);
712 }
713 
714 
715 #ifndef PRODUCT
716    #define TRACE_PHI(code) if (PrintPhiFunctions) { code; }
717 #else
718    #define TRACE_PHI(coce)
719 #endif
720 
721 
722 bool BlockBegin::try_merge(ValueStack* new_state, bool has_irreducible_loops) {
723   TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id()));
724 
725   // local variables used for state iteration
726   int index;
727   Value new_value, existing_value;
728 
729   ValueStack* existing_state = state();
730   if (existing_state == nullptr) {
731     TRACE_PHI(tty->print_cr("first call of try_merge for this block"));
732 
733     if (is_set(BlockBegin::was_visited_flag)) {
734       // this actually happens for complicated jsr/ret structures
735       return false; // BAILOUT in caller
736     }
737 
738     // copy state because it is altered
739     new_state = new_state->copy(ValueStack::BlockBeginState, bci());
740 
741     // Use method liveness to invalidate dead locals
742     MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci());
743     if (liveness.is_valid()) {
744       assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness");
745 
746       for_each_local_value(new_state, index, new_value) {
747         if (!liveness.at(index) || new_value->type()->is_illegal()) {
748           new_state->invalidate_local(index);
749           TRACE_PHI(tty->print_cr("invalidating dead local %d", index));
750         }
751       }
752     }
753 
754     if (is_set(BlockBegin::parser_loop_header_flag)) {
755       TRACE_PHI(tty->print_cr("loop header block, initializing phi functions"));
756 
757       for_each_stack_value(new_state, index, new_value) {
758         new_state->setup_phi_for_stack(this, index);
759         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));
760       }
761 
762       BitMap& requires_phi_function = new_state->scope()->requires_phi_function();
763       for_each_local_value(new_state, index, new_value) {
764         bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1));
765         if (requires_phi || !SelectivePhiFunctions || has_irreducible_loops) {
766           new_state->setup_phi_for_local(this, index);
767           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));
768         }
769       }
770     }
771 
772     // initialize state of block
773     set_state(new_state);
774 
775   } else if (existing_state->is_same(new_state)) {
776     TRACE_PHI(tty->print_cr("existing state found"));
777 
778     assert(existing_state->scope() == new_state->scope(), "not matching");
779     assert(existing_state->locals_size() == new_state->locals_size(), "not matching");
780     assert(existing_state->stack_size() == new_state->stack_size(), "not matching");
781 
782     if (is_set(BlockBegin::was_visited_flag)) {
783       TRACE_PHI(tty->print_cr("loop header block, phis must be present"));
784 
785       if (!is_set(BlockBegin::parser_loop_header_flag)) {
786         // this actually happens for complicated jsr/ret structures
787         return false; // BAILOUT in caller
788       }
789 
790       for_each_local_value(existing_state, index, existing_value) {
791         Value new_value = new_state->local_at(index);
792         if (new_value == nullptr || new_value->type()->tag() != existing_value->type()->tag()) {
793           Phi* existing_phi = existing_value->as_Phi();
794           if (existing_phi == nullptr) {
795             return false; // BAILOUT in caller
796           }
797           // Invalidate the phi function here. This case is very rare except for
798           // JVMTI capability "can_access_local_variables".
799           // In really rare cases we will bail out in LIRGenerator::move_to_phi.
800           existing_phi->make_illegal();
801           existing_state->invalidate_local(index);
802           TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index));
803         }
804 
805         if (existing_value != new_state->local_at(index) && existing_value->as_Phi() == nullptr) {
806           TRACE_PHI(tty->print_cr("required phi for local %d is missing, irreducible loop?", index));
807           return false; // BAILOUT in caller
808         }
809       }
810 
811 #ifdef ASSERT
812       // check that all necessary phi functions are present
813       for_each_stack_value(existing_state, index, existing_value) {
814         assert(existing_value->as_Phi() != nullptr && existing_value->as_Phi()->block() == this, "phi function required");
815       }
816       for_each_local_value(existing_state, index, existing_value) {
817         assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != nullptr && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required");
818       }
819 #endif
820 
821     } else {
822       TRACE_PHI(tty->print_cr("creating phi functions on demand"));
823 
824       // create necessary phi functions for stack
825       for_each_stack_value(existing_state, index, existing_value) {
826         Value new_value = new_state->stack_at(index);
827         Phi* existing_phi = existing_value->as_Phi();
828 
829         if (new_value != existing_value && (existing_phi == nullptr || existing_phi->block() != this)) {
830           existing_state->setup_phi_for_stack(this, index);
831           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));
832         }
833       }
834 
835       // create necessary phi functions for locals
836       for_each_local_value(existing_state, index, existing_value) {
837         Value new_value = new_state->local_at(index);
838         Phi* existing_phi = existing_value->as_Phi();
839 
840         if (new_value == nullptr || new_value->type()->tag() != existing_value->type()->tag()) {
841           existing_state->invalidate_local(index);
842           TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index));
843         } else if (new_value != existing_value && (existing_phi == nullptr || existing_phi->block() != this)) {
844           existing_state->setup_phi_for_local(this, index);
845           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));
846         }
847       }
848     }
849 
850     assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal");
851 
852   } else {
853     assert(false, "stack or locks not matching (invalid bytecodes)");
854     return false;
855   }
856 
857   TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id()));
858 
859   return true;
860 }
861 
862 
863 #ifndef PRODUCT
864 void BlockBegin::print_block() {
865   InstructionPrinter ip;
866   print_block(ip, false);
867 }
868 
869 
870 void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) {
871   ip.print_instr(this); tty->cr();
872   ip.print_stack(this->state()); tty->cr();
873   ip.print_inline_level(this);
874   ip.print_head();
875   for (Instruction* n = next(); n != nullptr; n = n->next()) {
876     if (!live_only || n->is_pinned() || n->use_count() > 0) {
877       ip.print_line(n);
878     }
879   }
880   tty->cr();
881 }
882 #endif // PRODUCT
883 
884 
885 // Implementation of BlockList
886 
887 void BlockList::iterate_forward (BlockClosure* closure) {
888   const int l = length();
889   for (int i = 0; i < l; i++) closure->block_do(at(i));
890 }
891 
892 
893 void BlockList::iterate_backward(BlockClosure* closure) {
894   for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i));
895 }
896 
897 
898 void BlockList::values_do(ValueVisitor* f) {
899   for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f);
900 }
901 
902 
903 #ifndef PRODUCT
904 void BlockList::print(bool cfg_only, bool live_only) {
905   InstructionPrinter ip;
906   for (int i = 0; i < length(); i++) {
907     BlockBegin* block = at(i);
908     if (cfg_only) {
909       ip.print_instr(block); tty->cr();
910     } else {
911       block->print_block(ip, live_only);
912     }
913   }
914 }
915 #endif // PRODUCT
916 
917 
918 // Implementation of BlockEnd
919 
920 void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
921   substitute(*_sux, old_sux, new_sux);
922 }
923 
924 // Implementation of Phi
925 
926 // Normal phi functions take their operands from the last instruction of the
927 // predecessor. Special handling is needed for xhanlder entries because there
928 // the state of arbitrary instructions are needed.
929 
930 Value Phi::operand_at(int i) const {
931   ValueStack* state;
932   if (_block->is_set(BlockBegin::exception_entry_flag)) {
933     state = _block->exception_state_at(i);
934   } else {
935     state = _block->pred_at(i)->end()->state();
936   }
937   assert(state != nullptr, "");
938 
939   if (is_local()) {
940     return state->local_at(local_index());
941   } else {
942     return state->stack_at(stack_index());
943   }
944 }
945 
946 
947 int Phi::operand_count() const {
948   if (_block->is_set(BlockBegin::exception_entry_flag)) {
949     return _block->number_of_exception_states();
950   } else {
951     return _block->number_of_preds();
952   }
953 }
954 
955 #ifdef ASSERT
956 // Constructor of Assert
957 Assert::Assert(Value x, Condition cond, bool unordered_is_true, Value y) : Instruction(illegalType)
958   , _x(x)
959   , _cond(cond)
960   , _y(y)
961 {
962   set_flag(UnorderedIsTrueFlag, unordered_is_true);
963   assert(x->type()->tag() == y->type()->tag(), "types must match");
964   pin();
965 
966   stringStream strStream;
967   Compilation::current()->method()->print_name(&strStream);
968 
969   stringStream strStream1;
970   InstructionPrinter ip1(1, &strStream1);
971   ip1.print_instr(x);
972 
973   stringStream strStream2;
974   InstructionPrinter ip2(1, &strStream2);
975   ip2.print_instr(y);
976 
977   stringStream ss;
978   ss.print("Assertion %s %s %s in method %s", strStream1.freeze(), ip2.cond_name(cond), strStream2.freeze(), strStream.freeze());
979 
980   _message = ss.as_string();
981 }
982 #endif
983 
984 void RangeCheckPredicate::check_state() {
985   assert(state()->kind() != ValueStack::EmptyExceptionState && state()->kind() != ValueStack::ExceptionState, "will deopt with empty state");
986 }
987 
988 void ProfileInvoke::state_values_do(ValueVisitor* f) {
989   if (state() != nullptr) state()->values_do(f);
990 }