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