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