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