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