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