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