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