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