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