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