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