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