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