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