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