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