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