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