1 /* 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2008, 2009, 2010 Red Hat, Inc. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "ci/ciField.hpp" 28 #include "ci/ciInstance.hpp" 29 #include "ci/ciObjArrayKlass.hpp" 30 #include "ci/ciStreams.hpp" 31 #include "ci/ciType.hpp" 32 #include "ci/ciTypeFlow.hpp" 33 #include "interpreter/bytecodes.hpp" 34 #include "memory/allocation.hpp" 35 #include "runtime/deoptimization.hpp" 36 #include "shark/llvmHeaders.hpp" 37 #include "shark/llvmValue.hpp" 38 #include "shark/sharkBuilder.hpp" 39 #include "shark/sharkCacheDecache.hpp" 40 #include "shark/sharkConstant.hpp" 41 #include "shark/sharkInliner.hpp" 42 #include "shark/sharkState.hpp" 43 #include "shark/sharkTopLevelBlock.hpp" 44 #include "shark/sharkValue.hpp" 45 #include "shark/shark_globals.hpp" 46 #include "utilities/debug.hpp" 47 48 using namespace llvm; 49 50 void SharkTopLevelBlock::scan_for_traps() { 51 // If typeflow found a trap then don't scan past it 52 int limit_bci = ciblock()->has_trap() ? ciblock()->trap_bci() : limit(); 53 54 // Scan the bytecode for traps that are always hit 55 iter()->reset_to_bci(start()); 56 while (iter()->next_bci() < limit_bci) { 57 iter()->next(); 58 59 ciField *field; 60 ciMethod *method; 61 ciInstanceKlass *klass; 62 bool will_link; 63 bool is_field; 64 65 switch (bc()) { 66 case Bytecodes::_ldc: 67 case Bytecodes::_ldc_w: 68 case Bytecodes::_ldc2_w: 69 if (!SharkConstant::for_ldc(iter())->is_loaded()) { 70 set_trap( 71 Deoptimization::make_trap_request( 72 Deoptimization::Reason_uninitialized, 73 Deoptimization::Action_reinterpret), bci()); 74 return; 75 } 76 break; 77 78 case Bytecodes::_getfield: 79 case Bytecodes::_getstatic: 80 case Bytecodes::_putfield: 81 case Bytecodes::_putstatic: 82 field = iter()->get_field(will_link); 83 assert(will_link, "typeflow responsibility"); 84 is_field = (bc() == Bytecodes::_getfield || bc() == Bytecodes::_putfield); 85 86 // If the bytecode does not match the field then bail out to 87 // the interpreter to throw an IncompatibleClassChangeError 88 if (is_field == field->is_static()) { 89 set_trap( 90 Deoptimization::make_trap_request( 91 Deoptimization::Reason_unhandled, 92 Deoptimization::Action_none), bci()); 93 return; 94 } 95 96 // Bail out if we are trying to access a static variable 97 // before the class initializer has completed. 98 if (!is_field && !field->holder()->is_initialized()) { 99 if (!static_field_ok_in_clinit(field)) { 100 set_trap( 101 Deoptimization::make_trap_request( 102 Deoptimization::Reason_uninitialized, 103 Deoptimization::Action_reinterpret), bci()); 104 return; 105 } 106 } 107 break; 108 109 case Bytecodes::_invokestatic: 110 case Bytecodes::_invokespecial: 111 case Bytecodes::_invokevirtual: 112 case Bytecodes::_invokeinterface: 113 ciSignature* sig; 114 method = iter()->get_method(will_link, &sig); 115 assert(will_link, "typeflow responsibility"); 116 117 if (!method->holder()->is_linked()) { 118 set_trap( 119 Deoptimization::make_trap_request( 120 Deoptimization::Reason_uninitialized, 121 Deoptimization::Action_reinterpret), bci()); 122 return; 123 } 124 125 if (bc() == Bytecodes::_invokevirtual) { 126 klass = ciEnv::get_instance_klass_for_declared_method_holder( 127 iter()->get_declared_method_holder()); 128 if (!klass->is_linked()) { 129 set_trap( 130 Deoptimization::make_trap_request( 131 Deoptimization::Reason_uninitialized, 132 Deoptimization::Action_reinterpret), bci()); 133 return; 134 } 135 } 136 break; 137 138 case Bytecodes::_new: 139 klass = iter()->get_klass(will_link)->as_instance_klass(); 140 assert(will_link, "typeflow responsibility"); 141 142 // Bail out if the class is unloaded 143 if (iter()->is_unresolved_klass() || !klass->is_initialized()) { 144 set_trap( 145 Deoptimization::make_trap_request( 146 Deoptimization::Reason_uninitialized, 147 Deoptimization::Action_reinterpret), bci()); 148 return; 149 } 150 151 // Bail out if the class cannot be instantiated 152 if (klass->is_abstract() || klass->is_interface() || 153 klass->name() == ciSymbol::java_lang_Class()) { 154 set_trap( 155 Deoptimization::make_trap_request( 156 Deoptimization::Reason_unhandled, 157 Deoptimization::Action_reinterpret), bci()); 158 return; 159 } 160 break; 161 } 162 } 163 164 // Trap if typeflow trapped (and we didn't before) 165 if (ciblock()->has_trap()) { 166 set_trap( 167 Deoptimization::make_trap_request( 168 Deoptimization::Reason_unloaded, 169 Deoptimization::Action_reinterpret, 170 ciblock()->trap_index()), ciblock()->trap_bci()); 171 return; 172 } 173 } 174 175 bool SharkTopLevelBlock::static_field_ok_in_clinit(ciField* field) { 176 assert(field->is_static(), "should be"); 177 178 // This code is lifted pretty much verbatim from C2's 179 // Parse::static_field_ok_in_clinit() in parse3.cpp. 180 bool access_OK = false; 181 if (target()->holder()->is_subclass_of(field->holder())) { 182 if (target()->is_static()) { 183 if (target()->name() == ciSymbol::class_initializer_name()) { 184 // It's OK to access static fields from the class initializer 185 access_OK = true; 186 } 187 } 188 else { 189 if (target()->name() == ciSymbol::object_initializer_name()) { 190 // It's also OK to access static fields inside a constructor, 191 // because any thread calling the constructor must first have 192 // synchronized on the class by executing a "new" bytecode. 193 access_OK = true; 194 } 195 } 196 } 197 return access_OK; 198 } 199 200 SharkState* SharkTopLevelBlock::entry_state() { 201 if (_entry_state == NULL) { 202 assert(needs_phis(), "should do"); 203 _entry_state = new SharkPHIState(this); 204 } 205 return _entry_state; 206 } 207 208 void SharkTopLevelBlock::add_incoming(SharkState* incoming_state) { 209 if (needs_phis()) { 210 ((SharkPHIState *) entry_state())->add_incoming(incoming_state); 211 } 212 else if (_entry_state == NULL) { 213 _entry_state = incoming_state; 214 } 215 else { 216 assert(entry_state()->equal_to(incoming_state), "should be"); 217 } 218 } 219 220 void SharkTopLevelBlock::enter(SharkTopLevelBlock* predecessor, 221 bool is_exception) { 222 // This block requires phis: 223 // - if it is entered more than once 224 // - if it is an exception handler, because in which 225 // case we assume it's entered more than once. 226 // - if the predecessor will be compiled after this 227 // block, in which case we can't simple propagate 228 // the state forward. 229 if (!needs_phis() && 230 (entered() || 231 is_exception || 232 (predecessor && predecessor->index() >= index()))) 233 _needs_phis = true; 234 235 // Recurse into the tree 236 if (!entered()) { 237 _entered = true; 238 239 scan_for_traps(); 240 if (!has_trap()) { 241 for (int i = 0; i < num_successors(); i++) { 242 successor(i)->enter(this, false); 243 } 244 } 245 compute_exceptions(); 246 for (int i = 0; i < num_exceptions(); i++) { 247 SharkTopLevelBlock *handler = exception(i); 248 if (handler) 249 handler->enter(this, true); 250 } 251 } 252 } 253 254 void SharkTopLevelBlock::initialize() { 255 char name[28]; 256 snprintf(name, sizeof(name), 257 "bci_%d%s", 258 start(), is_backedge_copy() ? "_backedge_copy" : ""); 259 _entry_block = function()->CreateBlock(name); 260 } 261 262 void SharkTopLevelBlock::decache_for_Java_call(ciMethod *callee) { 263 SharkJavaCallDecacher(function(), bci(), callee).scan(current_state()); 264 for (int i = 0; i < callee->arg_size(); i++) 265 xpop(); 266 } 267 268 void SharkTopLevelBlock::cache_after_Java_call(ciMethod *callee) { 269 if (callee->return_type()->size()) { 270 ciType *type; 271 switch (callee->return_type()->basic_type()) { 272 case T_BOOLEAN: 273 case T_BYTE: 274 case T_CHAR: 275 case T_SHORT: 276 type = ciType::make(T_INT); 277 break; 278 279 default: 280 type = callee->return_type(); 281 } 282 283 push(SharkValue::create_generic(type, NULL, false)); 284 } 285 SharkJavaCallCacher(function(), callee).scan(current_state()); 286 } 287 288 void SharkTopLevelBlock::decache_for_VM_call() { 289 SharkVMCallDecacher(function(), bci()).scan(current_state()); 290 } 291 292 void SharkTopLevelBlock::cache_after_VM_call() { 293 SharkVMCallCacher(function()).scan(current_state()); 294 } 295 296 void SharkTopLevelBlock::decache_for_trap() { 297 SharkTrapDecacher(function(), bci()).scan(current_state()); 298 } 299 300 void SharkTopLevelBlock::emit_IR() { 301 builder()->SetInsertPoint(entry_block()); 302 303 // Parse the bytecode 304 parse_bytecode(start(), limit()); 305 306 // If this block falls through to the next then it won't have been 307 // terminated by a bytecode and we have to add the branch ourselves 308 if (falls_through() && !has_trap()) 309 do_branch(ciTypeFlow::FALL_THROUGH); 310 } 311 312 SharkTopLevelBlock* SharkTopLevelBlock::bci_successor(int bci) const { 313 // XXX now with Linear Search Technology (tm) 314 for (int i = 0; i < num_successors(); i++) { 315 ciTypeFlow::Block *successor = ciblock()->successors()->at(i); 316 if (successor->start() == bci) 317 return function()->block(successor->pre_order()); 318 } 319 ShouldNotReachHere(); 320 } 321 322 void SharkTopLevelBlock::do_zero_check(SharkValue *value) { 323 if (value->is_phi() && value->as_phi()->all_incomers_zero_checked()) { 324 function()->add_deferred_zero_check(this, value); 325 } 326 else { 327 BasicBlock *continue_block = function()->CreateBlock("not_zero"); 328 SharkState *saved_state = current_state(); 329 set_current_state(saved_state->copy()); 330 zero_check_value(value, continue_block); 331 builder()->SetInsertPoint(continue_block); 332 set_current_state(saved_state); 333 } 334 335 value->set_zero_checked(true); 336 } 337 338 void SharkTopLevelBlock::do_deferred_zero_check(SharkValue* value, 339 int bci, 340 SharkState* saved_state, 341 BasicBlock* continue_block) { 342 if (value->as_phi()->all_incomers_zero_checked()) { 343 builder()->CreateBr(continue_block); 344 } 345 else { 346 iter()->force_bci(start()); 347 set_current_state(saved_state); 348 zero_check_value(value, continue_block); 349 } 350 } 351 352 void SharkTopLevelBlock::zero_check_value(SharkValue* value, 353 BasicBlock* continue_block) { 354 BasicBlock *zero_block = builder()->CreateBlock(continue_block, "zero"); 355 356 Value *a, *b; 357 switch (value->basic_type()) { 358 case T_BYTE: 359 case T_CHAR: 360 case T_SHORT: 361 case T_INT: 362 a = value->jint_value(); 363 b = LLVMValue::jint_constant(0); 364 break; 365 case T_LONG: 366 a = value->jlong_value(); 367 b = LLVMValue::jlong_constant(0); 368 break; 369 case T_OBJECT: 370 case T_ARRAY: 371 a = value->jobject_value(); 372 b = LLVMValue::LLVMValue::null(); 373 break; 374 default: 375 tty->print_cr("Unhandled type %s", type2name(value->basic_type())); 376 ShouldNotReachHere(); 377 } 378 379 builder()->CreateCondBr( 380 builder()->CreateICmpNE(a, b), continue_block, zero_block); 381 382 builder()->SetInsertPoint(zero_block); 383 if (value->is_jobject()) { 384 call_vm( 385 builder()->throw_NullPointerException(), 386 builder()->CreateIntToPtr( 387 LLVMValue::intptr_constant((intptr_t) __FILE__), 388 PointerType::getUnqual(SharkType::jbyte_type())), 389 LLVMValue::jint_constant(__LINE__), 390 EX_CHECK_NONE); 391 } 392 else { 393 call_vm( 394 builder()->throw_ArithmeticException(), 395 builder()->CreateIntToPtr( 396 LLVMValue::intptr_constant((intptr_t) __FILE__), 397 PointerType::getUnqual(SharkType::jbyte_type())), 398 LLVMValue::jint_constant(__LINE__), 399 EX_CHECK_NONE); 400 } 401 402 Value *pending_exception = get_pending_exception(); 403 clear_pending_exception(); 404 handle_exception(pending_exception, EX_CHECK_FULL); 405 } 406 407 void SharkTopLevelBlock::check_bounds(SharkValue* array, SharkValue* index) { 408 BasicBlock *out_of_bounds = function()->CreateBlock("out_of_bounds"); 409 BasicBlock *in_bounds = function()->CreateBlock("in_bounds"); 410 411 Value *length = builder()->CreateArrayLength(array->jarray_value()); 412 // we use an unsigned comparison to catch negative values 413 builder()->CreateCondBr( 414 builder()->CreateICmpULT(index->jint_value(), length), 415 in_bounds, out_of_bounds); 416 417 builder()->SetInsertPoint(out_of_bounds); 418 SharkState *saved_state = current_state()->copy(); 419 420 call_vm( 421 builder()->throw_ArrayIndexOutOfBoundsException(), 422 builder()->CreateIntToPtr( 423 LLVMValue::intptr_constant((intptr_t) __FILE__), 424 PointerType::getUnqual(SharkType::jbyte_type())), 425 LLVMValue::jint_constant(__LINE__), 426 index->jint_value(), 427 EX_CHECK_NONE); 428 429 Value *pending_exception = get_pending_exception(); 430 clear_pending_exception(); 431 handle_exception(pending_exception, EX_CHECK_FULL); 432 433 set_current_state(saved_state); 434 435 builder()->SetInsertPoint(in_bounds); 436 } 437 438 void SharkTopLevelBlock::check_pending_exception(int action) { 439 assert(action & EAM_CHECK, "should be"); 440 441 BasicBlock *exception = function()->CreateBlock("exception"); 442 BasicBlock *no_exception = function()->CreateBlock("no_exception"); 443 444 Value *pending_exception = get_pending_exception(); 445 builder()->CreateCondBr( 446 builder()->CreateICmpEQ(pending_exception, LLVMValue::null()), 447 no_exception, exception); 448 449 builder()->SetInsertPoint(exception); 450 SharkState *saved_state = current_state()->copy(); 451 if (action & EAM_MONITOR_FUDGE) { 452 // The top monitor is marked live, but the exception was thrown 453 // while setting it up so we need to mark it dead before we enter 454 // any exception handlers as they will not expect it to be there. 455 set_num_monitors(num_monitors() - 1); 456 action ^= EAM_MONITOR_FUDGE; 457 } 458 clear_pending_exception(); 459 handle_exception(pending_exception, action); 460 set_current_state(saved_state); 461 462 builder()->SetInsertPoint(no_exception); 463 } 464 465 void SharkTopLevelBlock::compute_exceptions() { 466 ciExceptionHandlerStream str(target(), start()); 467 468 int exc_count = str.count(); 469 _exc_handlers = new GrowableArray<ciExceptionHandler*>(exc_count); 470 _exceptions = new GrowableArray<SharkTopLevelBlock*>(exc_count); 471 472 int index = 0; 473 for (; !str.is_done(); str.next()) { 474 ciExceptionHandler *handler = str.handler(); 475 if (handler->handler_bci() == -1) 476 break; 477 _exc_handlers->append(handler); 478 479 // Try and get this exception's handler from typeflow. We should 480 // do it this way always, really, except that typeflow sometimes 481 // doesn't record exceptions, even loaded ones, and sometimes it 482 // returns them with a different handler bci. Why??? 483 SharkTopLevelBlock *block = NULL; 484 ciInstanceKlass* klass; 485 if (handler->is_catch_all()) { 486 klass = java_lang_Throwable_klass(); 487 } 488 else { 489 klass = handler->catch_klass(); 490 } 491 for (int i = 0; i < ciblock()->exceptions()->length(); i++) { 492 if (klass == ciblock()->exc_klasses()->at(i)) { 493 block = function()->block(ciblock()->exceptions()->at(i)->pre_order()); 494 if (block->start() == handler->handler_bci()) 495 break; 496 else 497 block = NULL; 498 } 499 } 500 501 // If typeflow let us down then try and figure it out ourselves 502 if (block == NULL) { 503 for (int i = 0; i < function()->block_count(); i++) { 504 SharkTopLevelBlock *candidate = function()->block(i); 505 if (candidate->start() == handler->handler_bci()) { 506 if (block != NULL) { 507 NOT_PRODUCT(warning("there may be trouble ahead")); 508 block = NULL; 509 break; 510 } 511 block = candidate; 512 } 513 } 514 } 515 _exceptions->append(block); 516 } 517 } 518 519 void SharkTopLevelBlock::handle_exception(Value* exception, int action) { 520 if (action & EAM_HANDLE && num_exceptions() != 0) { 521 // Clear the stack and push the exception onto it 522 while (xstack_depth()) 523 pop(); 524 push(SharkValue::create_jobject(exception, true)); 525 526 // Work out how many options we have to check 527 bool has_catch_all = exc_handler(num_exceptions() - 1)->is_catch_all(); 528 int num_options = num_exceptions(); 529 if (has_catch_all) 530 num_options--; 531 532 // Marshal any non-catch-all handlers 533 if (num_options > 0) { 534 bool all_loaded = true; 535 for (int i = 0; i < num_options; i++) { 536 if (!exc_handler(i)->catch_klass()->is_loaded()) { 537 all_loaded = false; 538 break; 539 } 540 } 541 542 if (all_loaded) 543 marshal_exception_fast(num_options); 544 else 545 marshal_exception_slow(num_options); 546 } 547 548 // Install the catch-all handler, if present 549 if (has_catch_all) { 550 SharkTopLevelBlock* handler = this->exception(num_options); 551 assert(handler != NULL, "catch-all handler cannot be unloaded"); 552 553 builder()->CreateBr(handler->entry_block()); 554 handler->add_incoming(current_state()); 555 return; 556 } 557 } 558 559 // No exception handler was found; unwind and return 560 handle_return(T_VOID, exception); 561 } 562 563 void SharkTopLevelBlock::marshal_exception_fast(int num_options) { 564 Value *exception_klass = builder()->CreateValueOfStructEntry( 565 xstack(0)->jobject_value(), 566 in_ByteSize(oopDesc::klass_offset_in_bytes()), 567 SharkType::klass_type(), 568 "exception_klass"); 569 570 for (int i = 0; i < num_options; i++) { 571 Value *check_klass = 572 builder()->CreateInlineMetadata(exc_handler(i)->catch_klass(), SharkType::klass_type()); 573 574 BasicBlock *not_exact = function()->CreateBlock("not_exact"); 575 BasicBlock *not_subtype = function()->CreateBlock("not_subtype"); 576 577 builder()->CreateCondBr( 578 builder()->CreateICmpEQ(check_klass, exception_klass), 579 handler_for_exception(i), not_exact); 580 581 builder()->SetInsertPoint(not_exact); 582 builder()->CreateCondBr( 583 builder()->CreateICmpNE( 584 builder()->CreateCall2( 585 builder()->is_subtype_of(), check_klass, exception_klass), 586 LLVMValue::jbyte_constant(0)), 587 handler_for_exception(i), not_subtype); 588 589 builder()->SetInsertPoint(not_subtype); 590 } 591 } 592 593 void SharkTopLevelBlock::marshal_exception_slow(int num_options) { 594 int *indexes = NEW_RESOURCE_ARRAY(int, num_options); 595 for (int i = 0; i < num_options; i++) 596 indexes[i] = exc_handler(i)->catch_klass_index(); 597 598 Value *index = call_vm( 599 builder()->find_exception_handler(), 600 builder()->CreateInlineData( 601 indexes, 602 num_options * sizeof(int), 603 PointerType::getUnqual(SharkType::jint_type())), 604 LLVMValue::jint_constant(num_options), 605 EX_CHECK_NO_CATCH); 606 607 BasicBlock *no_handler = function()->CreateBlock("no_handler"); 608 SwitchInst *switchinst = builder()->CreateSwitch( 609 index, no_handler, num_options); 610 611 for (int i = 0; i < num_options; i++) { 612 switchinst->addCase( 613 LLVMValue::jint_constant(i), 614 handler_for_exception(i)); 615 } 616 617 builder()->SetInsertPoint(no_handler); 618 } 619 620 BasicBlock* SharkTopLevelBlock::handler_for_exception(int index) { 621 SharkTopLevelBlock *successor = this->exception(index); 622 if (successor) { 623 successor->add_incoming(current_state()); 624 return successor->entry_block(); 625 } 626 else { 627 return make_trap( 628 exc_handler(index)->handler_bci(), 629 Deoptimization::make_trap_request( 630 Deoptimization::Reason_unhandled, 631 Deoptimization::Action_reinterpret)); 632 } 633 } 634 635 void SharkTopLevelBlock::maybe_add_safepoint() { 636 if (current_state()->has_safepointed()) 637 return; 638 639 BasicBlock *orig_block = builder()->GetInsertBlock(); 640 SharkState *orig_state = current_state()->copy(); 641 642 BasicBlock *do_safepoint = function()->CreateBlock("do_safepoint"); 643 BasicBlock *safepointed = function()->CreateBlock("safepointed"); 644 645 Value *state = builder()->CreateLoad( 646 builder()->CreateIntToPtr( 647 LLVMValue::intptr_constant( 648 (intptr_t) SafepointSynchronize::address_of_state()), 649 PointerType::getUnqual(SharkType::jint_type())), 650 "state"); 651 652 builder()->CreateCondBr( 653 builder()->CreateICmpEQ( 654 state, 655 LLVMValue::jint_constant(SafepointSynchronize::_synchronizing)), 656 do_safepoint, safepointed); 657 658 builder()->SetInsertPoint(do_safepoint); 659 call_vm(builder()->safepoint(), EX_CHECK_FULL); 660 BasicBlock *safepointed_block = builder()->GetInsertBlock(); 661 builder()->CreateBr(safepointed); 662 663 builder()->SetInsertPoint(safepointed); 664 current_state()->merge(orig_state, orig_block, safepointed_block); 665 666 current_state()->set_has_safepointed(true); 667 } 668 669 void SharkTopLevelBlock::maybe_add_backedge_safepoint() { 670 if (current_state()->has_safepointed()) 671 return; 672 673 for (int i = 0; i < num_successors(); i++) { 674 if (successor(i)->can_reach(this)) { 675 maybe_add_safepoint(); 676 break; 677 } 678 } 679 } 680 681 bool SharkTopLevelBlock::can_reach(SharkTopLevelBlock* other) { 682 for (int i = 0; i < function()->block_count(); i++) 683 function()->block(i)->_can_reach_visited = false; 684 685 return can_reach_helper(other); 686 } 687 688 bool SharkTopLevelBlock::can_reach_helper(SharkTopLevelBlock* other) { 689 if (this == other) 690 return true; 691 692 if (_can_reach_visited) 693 return false; 694 _can_reach_visited = true; 695 696 if (!has_trap()) { 697 for (int i = 0; i < num_successors(); i++) { 698 if (successor(i)->can_reach_helper(other)) 699 return true; 700 } 701 } 702 703 for (int i = 0; i < num_exceptions(); i++) { 704 SharkTopLevelBlock *handler = exception(i); 705 if (handler && handler->can_reach_helper(other)) 706 return true; 707 } 708 709 return false; 710 } 711 712 BasicBlock* SharkTopLevelBlock::make_trap(int trap_bci, int trap_request) { 713 BasicBlock *trap_block = function()->CreateBlock("trap"); 714 BasicBlock *orig_block = builder()->GetInsertBlock(); 715 builder()->SetInsertPoint(trap_block); 716 717 int orig_bci = bci(); 718 iter()->force_bci(trap_bci); 719 720 do_trap(trap_request); 721 722 builder()->SetInsertPoint(orig_block); 723 iter()->force_bci(orig_bci); 724 725 return trap_block; 726 } 727 728 void SharkTopLevelBlock::do_trap(int trap_request) { 729 decache_for_trap(); 730 builder()->CreateRet( 731 builder()->CreateCall2( 732 builder()->uncommon_trap(), 733 thread(), 734 LLVMValue::jint_constant(trap_request))); 735 } 736 737 void SharkTopLevelBlock::call_register_finalizer(Value *receiver) { 738 BasicBlock *orig_block = builder()->GetInsertBlock(); 739 SharkState *orig_state = current_state()->copy(); 740 741 BasicBlock *do_call = function()->CreateBlock("has_finalizer"); 742 BasicBlock *done = function()->CreateBlock("done"); 743 744 Value *klass = builder()->CreateValueOfStructEntry( 745 receiver, 746 in_ByteSize(oopDesc::klass_offset_in_bytes()), 747 SharkType::oop_type(), 748 "klass"); 749 750 Value *access_flags = builder()->CreateValueOfStructEntry( 751 klass, 752 Klass::access_flags_offset(), 753 SharkType::jint_type(), 754 "access_flags"); 755 756 builder()->CreateCondBr( 757 builder()->CreateICmpNE( 758 builder()->CreateAnd( 759 access_flags, 760 LLVMValue::jint_constant(JVM_ACC_HAS_FINALIZER)), 761 LLVMValue::jint_constant(0)), 762 do_call, done); 763 764 builder()->SetInsertPoint(do_call); 765 call_vm(builder()->register_finalizer(), receiver, EX_CHECK_FULL); 766 BasicBlock *branch_block = builder()->GetInsertBlock(); 767 builder()->CreateBr(done); 768 769 builder()->SetInsertPoint(done); 770 current_state()->merge(orig_state, orig_block, branch_block); 771 } 772 773 void SharkTopLevelBlock::handle_return(BasicType type, Value* exception) { 774 assert (exception == NULL || type == T_VOID, "exception OR result, please"); 775 776 if (num_monitors()) { 777 // Protect our exception across possible monitor release decaches 778 if (exception) 779 set_oop_tmp(exception); 780 781 // We don't need to check for exceptions thrown here. If 782 // we're returning a value then we just carry on as normal: 783 // the caller will see the pending exception and handle it. 784 // If we're returning with an exception then that exception 785 // takes priority and the release_lock one will be ignored. 786 while (num_monitors()) 787 release_lock(EX_CHECK_NONE); 788 789 // Reload the exception we're throwing 790 if (exception) 791 exception = get_oop_tmp(); 792 } 793 794 if (exception) { 795 builder()->CreateStore(exception, pending_exception_address()); 796 } 797 798 Value *result_addr = stack()->CreatePopFrame(type2size[type]); 799 if (type != T_VOID) { 800 builder()->CreateStore( 801 pop_result(type)->generic_value(), 802 builder()->CreateIntToPtr( 803 result_addr, 804 PointerType::getUnqual(SharkType::to_stackType(type)))); 805 } 806 807 builder()->CreateRet(LLVMValue::jint_constant(0)); 808 } 809 810 void SharkTopLevelBlock::do_arraylength() { 811 SharkValue *array = pop(); 812 check_null(array); 813 Value *length = builder()->CreateArrayLength(array->jarray_value()); 814 push(SharkValue::create_jint(length, false)); 815 } 816 817 void SharkTopLevelBlock::do_aload(BasicType basic_type) { 818 SharkValue *index = pop(); 819 SharkValue *array = pop(); 820 821 check_null(array); 822 check_bounds(array, index); 823 824 Value *value = builder()->CreateLoad( 825 builder()->CreateArrayAddress( 826 array->jarray_value(), basic_type, index->jint_value())); 827 828 Type *stack_type = SharkType::to_stackType(basic_type); 829 if (value->getType() != stack_type) 830 value = builder()->CreateIntCast(value, stack_type, basic_type != T_CHAR); 831 832 switch (basic_type) { 833 case T_BYTE: 834 case T_CHAR: 835 case T_SHORT: 836 case T_INT: 837 push(SharkValue::create_jint(value, false)); 838 break; 839 840 case T_LONG: 841 push(SharkValue::create_jlong(value, false)); 842 break; 843 844 case T_FLOAT: 845 push(SharkValue::create_jfloat(value)); 846 break; 847 848 case T_DOUBLE: 849 push(SharkValue::create_jdouble(value)); 850 break; 851 852 case T_OBJECT: 853 // You might expect that array->type()->is_array_klass() would 854 // always be true, but it isn't. If ciTypeFlow detects that a 855 // value is always null then that value becomes an untyped null 856 // object. Shark doesn't presently support this, so a generic 857 // T_OBJECT is created. In this case we guess the type using 858 // the BasicType we were supplied. In reality the generated 859 // code will never be used, as the null value will be caught 860 // by the above null pointer check. 861 // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=324 862 push( 863 SharkValue::create_generic( 864 array->type()->is_array_klass() ? 865 ((ciArrayKlass *) array->type())->element_type() : 866 ciType::make(basic_type), 867 value, false)); 868 break; 869 870 default: 871 tty->print_cr("Unhandled type %s", type2name(basic_type)); 872 ShouldNotReachHere(); 873 } 874 } 875 876 void SharkTopLevelBlock::do_astore(BasicType basic_type) { 877 SharkValue *svalue = pop(); 878 SharkValue *index = pop(); 879 SharkValue *array = pop(); 880 881 check_null(array); 882 check_bounds(array, index); 883 884 Value *value; 885 switch (basic_type) { 886 case T_BYTE: 887 case T_CHAR: 888 case T_SHORT: 889 case T_INT: 890 value = svalue->jint_value(); 891 break; 892 893 case T_LONG: 894 value = svalue->jlong_value(); 895 break; 896 897 case T_FLOAT: 898 value = svalue->jfloat_value(); 899 break; 900 901 case T_DOUBLE: 902 value = svalue->jdouble_value(); 903 break; 904 905 case T_OBJECT: 906 value = svalue->jobject_value(); 907 // XXX assignability check 908 break; 909 910 default: 911 tty->print_cr("Unhandled type %s", type2name(basic_type)); 912 ShouldNotReachHere(); 913 } 914 915 Type *array_type = SharkType::to_arrayType(basic_type); 916 if (value->getType() != array_type) 917 value = builder()->CreateIntCast(value, array_type, basic_type != T_CHAR); 918 919 Value *addr = builder()->CreateArrayAddress( 920 array->jarray_value(), basic_type, index->jint_value(), "addr"); 921 922 builder()->CreateStore(value, addr); 923 924 if (basic_type == T_OBJECT) // XXX or T_ARRAY? 925 builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr); 926 } 927 928 void SharkTopLevelBlock::do_return(BasicType type) { 929 if (target()->intrinsic_id() == vmIntrinsics::_Object_init) 930 call_register_finalizer(local(0)->jobject_value()); 931 maybe_add_safepoint(); 932 handle_return(type, NULL); 933 } 934 935 void SharkTopLevelBlock::do_athrow() { 936 SharkValue *exception = pop(); 937 check_null(exception); 938 handle_exception(exception->jobject_value(), EX_CHECK_FULL); 939 } 940 941 void SharkTopLevelBlock::do_goto() { 942 do_branch(ciTypeFlow::GOTO_TARGET); 943 } 944 945 void SharkTopLevelBlock::do_jsr() { 946 push(SharkValue::address_constant(iter()->next_bci())); 947 do_branch(ciTypeFlow::GOTO_TARGET); 948 } 949 950 void SharkTopLevelBlock::do_ret() { 951 assert(local(iter()->get_index())->address_value() == 952 successor(ciTypeFlow::GOTO_TARGET)->start(), "should be"); 953 do_branch(ciTypeFlow::GOTO_TARGET); 954 } 955 956 // All propagation of state from one block to the next (via 957 // dest->add_incoming) is handled by these methods: 958 // do_branch 959 // do_if_helper 960 // do_switch 961 // handle_exception 962 963 void SharkTopLevelBlock::do_branch(int successor_index) { 964 SharkTopLevelBlock *dest = successor(successor_index); 965 builder()->CreateBr(dest->entry_block()); 966 dest->add_incoming(current_state()); 967 } 968 969 void SharkTopLevelBlock::do_if(ICmpInst::Predicate p, 970 SharkValue* b, 971 SharkValue* a) { 972 Value *llvm_a, *llvm_b; 973 if (a->is_jobject()) { 974 llvm_a = a->intptr_value(builder()); 975 llvm_b = b->intptr_value(builder()); 976 } 977 else { 978 llvm_a = a->jint_value(); 979 llvm_b = b->jint_value(); 980 } 981 do_if_helper(p, llvm_b, llvm_a, current_state(), current_state()); 982 } 983 984 void SharkTopLevelBlock::do_if_helper(ICmpInst::Predicate p, 985 Value* b, 986 Value* a, 987 SharkState* if_taken_state, 988 SharkState* not_taken_state) { 989 SharkTopLevelBlock *if_taken = successor(ciTypeFlow::IF_TAKEN); 990 SharkTopLevelBlock *not_taken = successor(ciTypeFlow::IF_NOT_TAKEN); 991 992 builder()->CreateCondBr( 993 builder()->CreateICmp(p, a, b), 994 if_taken->entry_block(), not_taken->entry_block()); 995 996 if_taken->add_incoming(if_taken_state); 997 not_taken->add_incoming(not_taken_state); 998 } 999 1000 void SharkTopLevelBlock::do_switch() { 1001 int len = switch_table_length(); 1002 1003 SharkTopLevelBlock *dest_block = successor(ciTypeFlow::SWITCH_DEFAULT); 1004 SwitchInst *switchinst = builder()->CreateSwitch( 1005 pop()->jint_value(), dest_block->entry_block(), len); 1006 dest_block->add_incoming(current_state()); 1007 1008 for (int i = 0; i < len; i++) { 1009 int dest_bci = switch_dest(i); 1010 if (dest_bci != switch_default_dest()) { 1011 dest_block = bci_successor(dest_bci); 1012 switchinst->addCase( 1013 LLVMValue::jint_constant(switch_key(i)), 1014 dest_block->entry_block()); 1015 dest_block->add_incoming(current_state()); 1016 } 1017 } 1018 } 1019 1020 ciMethod* SharkTopLevelBlock::improve_virtual_call(ciMethod* caller, 1021 ciInstanceKlass* klass, 1022 ciMethod* dest_method, 1023 ciType* receiver_type) { 1024 // If the method is obviously final then we are already done 1025 if (dest_method->can_be_statically_bound()) 1026 return dest_method; 1027 1028 // Array methods are all inherited from Object and are monomorphic 1029 if (receiver_type->is_array_klass() && 1030 dest_method->holder() == java_lang_Object_klass()) 1031 return dest_method; 1032 1033 #ifdef SHARK_CAN_DEOPTIMIZE_ANYWHERE 1034 // This code can replace a virtual call with a direct call if this 1035 // class is the only one in the entire set of loaded classes that 1036 // implements this method. This makes the compiled code dependent 1037 // on other classes that implement the method not being loaded, a 1038 // condition which is enforced by the dependency tracker. If the 1039 // dependency tracker determines a method has become invalid it 1040 // will mark it for recompilation, causing running copies to be 1041 // deoptimized. Shark currently can't deoptimize arbitrarily like 1042 // that, so this optimization cannot be used. 1043 // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=481 1044 1045 // All other interesting cases are instance classes 1046 if (!receiver_type->is_instance_klass()) 1047 return NULL; 1048 1049 // Attempt to improve the receiver 1050 ciInstanceKlass* actual_receiver = klass; 1051 ciInstanceKlass *improved_receiver = receiver_type->as_instance_klass(); 1052 if (improved_receiver->is_loaded() && 1053 improved_receiver->is_initialized() && 1054 !improved_receiver->is_interface() && 1055 improved_receiver->is_subtype_of(actual_receiver)) { 1056 actual_receiver = improved_receiver; 1057 } 1058 1059 // Attempt to find a monomorphic target for this call using 1060 // class heirachy analysis. 1061 ciInstanceKlass *calling_klass = caller->holder(); 1062 ciMethod* monomorphic_target = 1063 dest_method->find_monomorphic_target(calling_klass, klass, actual_receiver); 1064 if (monomorphic_target != NULL) { 1065 assert(!monomorphic_target->is_abstract(), "shouldn't be"); 1066 1067 // Opto has a bunch of type checking here that I don't 1068 // understand. It's to inhibit casting in one direction, 1069 // possibly because objects in Opto can have inexact 1070 // types, but I can't even tell which direction it 1071 // doesn't like. For now I'm going to block *any* cast. 1072 if (monomorphic_target != dest_method) { 1073 if (SharkPerformanceWarnings) { 1074 warning("found monomorphic target, but inhibited cast:"); 1075 tty->print(" dest_method = "); 1076 dest_method->print_short_name(tty); 1077 tty->cr(); 1078 tty->print(" monomorphic_target = "); 1079 monomorphic_target->print_short_name(tty); 1080 tty->cr(); 1081 } 1082 monomorphic_target = NULL; 1083 } 1084 } 1085 1086 // Replace the virtual call with a direct one. This makes 1087 // us dependent on that target method not getting overridden 1088 // by dynamic class loading. 1089 if (monomorphic_target != NULL) { 1090 dependencies()->assert_unique_concrete_method( 1091 actual_receiver, monomorphic_target); 1092 return monomorphic_target; 1093 } 1094 1095 // Because Opto distinguishes exact types from inexact ones 1096 // it can perform a further optimization to replace calls 1097 // with non-monomorphic targets if the receiver has an exact 1098 // type. We don't mark types this way, so we can't do this. 1099 1100 #endif // SHARK_CAN_DEOPTIMIZE_ANYWHERE 1101 1102 return NULL; 1103 } 1104 1105 Value *SharkTopLevelBlock::get_direct_callee(ciMethod* method) { 1106 return builder()->CreateBitCast( 1107 builder()->CreateInlineMetadata(method, SharkType::Method_type()), 1108 SharkType::Method_type(), 1109 "callee"); 1110 } 1111 1112 Value *SharkTopLevelBlock::get_virtual_callee(SharkValue* receiver, 1113 int vtable_index) { 1114 Value *klass = builder()->CreateValueOfStructEntry( 1115 receiver->jobject_value(), 1116 in_ByteSize(oopDesc::klass_offset_in_bytes()), 1117 SharkType::oop_type(), 1118 "klass"); 1119 1120 return builder()->CreateLoad( 1121 builder()->CreateArrayAddress( 1122 klass, 1123 SharkType::Method_type(), 1124 vtableEntry::size() * wordSize, 1125 in_ByteSize(InstanceKlass::vtable_start_offset() * wordSize), 1126 LLVMValue::intptr_constant(vtable_index)), 1127 "callee"); 1128 } 1129 1130 Value* SharkTopLevelBlock::get_interface_callee(SharkValue *receiver, 1131 ciMethod* method) { 1132 BasicBlock *loop = function()->CreateBlock("loop"); 1133 BasicBlock *got_null = function()->CreateBlock("got_null"); 1134 BasicBlock *not_null = function()->CreateBlock("not_null"); 1135 BasicBlock *next = function()->CreateBlock("next"); 1136 BasicBlock *got_entry = function()->CreateBlock("got_entry"); 1137 1138 // Locate the receiver's itable 1139 Value *object_klass = builder()->CreateValueOfStructEntry( 1140 receiver->jobject_value(), in_ByteSize(oopDesc::klass_offset_in_bytes()), 1141 SharkType::klass_type(), 1142 "object_klass"); 1143 1144 Value *vtable_start = builder()->CreateAdd( 1145 builder()->CreatePtrToInt(object_klass, SharkType::intptr_type()), 1146 LLVMValue::intptr_constant( 1147 InstanceKlass::vtable_start_offset() * HeapWordSize), 1148 "vtable_start"); 1149 1150 Value *vtable_length = builder()->CreateValueOfStructEntry( 1151 object_klass, 1152 in_ByteSize(InstanceKlass::vtable_length_offset() * HeapWordSize), 1153 SharkType::jint_type(), 1154 "vtable_length"); 1155 vtable_length = 1156 builder()->CreateIntCast(vtable_length, SharkType::intptr_type(), false); 1157 1158 bool needs_aligning = HeapWordsPerLong > 1; 1159 Value *itable_start = builder()->CreateAdd( 1160 vtable_start, 1161 builder()->CreateShl( 1162 vtable_length, 1163 LLVMValue::intptr_constant(exact_log2(vtableEntry::size() * wordSize))), 1164 needs_aligning ? "" : "itable_start"); 1165 if (needs_aligning) { 1166 itable_start = builder()->CreateAnd( 1167 builder()->CreateAdd( 1168 itable_start, LLVMValue::intptr_constant(BytesPerLong - 1)), 1169 LLVMValue::intptr_constant(~(BytesPerLong - 1)), 1170 "itable_start"); 1171 } 1172 1173 // Locate this interface's entry in the table 1174 Value *iklass = builder()->CreateInlineMetadata(method->holder(), SharkType::klass_type()); 1175 BasicBlock *loop_entry = builder()->GetInsertBlock(); 1176 builder()->CreateBr(loop); 1177 builder()->SetInsertPoint(loop); 1178 PHINode *itable_entry_addr = builder()->CreatePHI( 1179 SharkType::intptr_type(), 0, "itable_entry_addr"); 1180 itable_entry_addr->addIncoming(itable_start, loop_entry); 1181 1182 Value *itable_entry = builder()->CreateIntToPtr( 1183 itable_entry_addr, SharkType::itableOffsetEntry_type(), "itable_entry"); 1184 1185 Value *itable_iklass = builder()->CreateValueOfStructEntry( 1186 itable_entry, 1187 in_ByteSize(itableOffsetEntry::interface_offset_in_bytes()), 1188 SharkType::klass_type(), 1189 "itable_iklass"); 1190 1191 builder()->CreateCondBr( 1192 builder()->CreateICmpEQ(itable_iklass, LLVMValue::nullKlass()), 1193 got_null, not_null); 1194 1195 // A null entry means that the class doesn't implement the 1196 // interface, and wasn't the same as the class checked when 1197 // the interface was resolved. 1198 builder()->SetInsertPoint(got_null); 1199 builder()->CreateUnimplemented(__FILE__, __LINE__); 1200 builder()->CreateUnreachable(); 1201 1202 builder()->SetInsertPoint(not_null); 1203 builder()->CreateCondBr( 1204 builder()->CreateICmpEQ(itable_iklass, iklass), 1205 got_entry, next); 1206 1207 builder()->SetInsertPoint(next); 1208 Value *next_entry = builder()->CreateAdd( 1209 itable_entry_addr, 1210 LLVMValue::intptr_constant(itableOffsetEntry::size() * wordSize)); 1211 builder()->CreateBr(loop); 1212 itable_entry_addr->addIncoming(next_entry, next); 1213 1214 // Locate the method pointer 1215 builder()->SetInsertPoint(got_entry); 1216 Value *offset = builder()->CreateValueOfStructEntry( 1217 itable_entry, 1218 in_ByteSize(itableOffsetEntry::offset_offset_in_bytes()), 1219 SharkType::jint_type(), 1220 "offset"); 1221 offset = 1222 builder()->CreateIntCast(offset, SharkType::intptr_type(), false); 1223 1224 return builder()->CreateLoad( 1225 builder()->CreateIntToPtr( 1226 builder()->CreateAdd( 1227 builder()->CreateAdd( 1228 builder()->CreateAdd( 1229 builder()->CreatePtrToInt( 1230 object_klass, SharkType::intptr_type()), 1231 offset), 1232 LLVMValue::intptr_constant( 1233 method->itable_index() * itableMethodEntry::size() * wordSize)), 1234 LLVMValue::intptr_constant( 1235 itableMethodEntry::method_offset_in_bytes())), 1236 PointerType::getUnqual(SharkType::Method_type())), 1237 "callee"); 1238 } 1239 1240 void SharkTopLevelBlock::do_call() { 1241 // Set frequently used booleans 1242 bool is_static = bc() == Bytecodes::_invokestatic; 1243 bool is_virtual = bc() == Bytecodes::_invokevirtual; 1244 bool is_interface = bc() == Bytecodes::_invokeinterface; 1245 1246 // Find the method being called 1247 bool will_link; 1248 ciSignature* sig; 1249 ciMethod *dest_method = iter()->get_method(will_link, &sig); 1250 1251 assert(will_link, "typeflow responsibility"); 1252 assert(dest_method->is_static() == is_static, "must match bc"); 1253 1254 // Find the class of the method being called. Note 1255 // that the superclass check in the second assertion 1256 // is to cope with a hole in the spec that allows for 1257 // invokeinterface instructions where the resolved 1258 // method is a virtual method in java.lang.Object. 1259 // javac doesn't generate code like that, but there's 1260 // no reason a compliant Java compiler might not. 1261 ciInstanceKlass *holder_klass = dest_method->holder(); 1262 assert(holder_klass->is_loaded(), "scan_for_traps responsibility"); 1263 assert(holder_klass->is_interface() || 1264 holder_klass->super() == NULL || 1265 !is_interface, "must match bc"); 1266 1267 bool is_forced_virtual = is_interface && holder_klass == java_lang_Object_klass(); 1268 1269 ciKlass *holder = iter()->get_declared_method_holder(); 1270 ciInstanceKlass *klass = 1271 ciEnv::get_instance_klass_for_declared_method_holder(holder); 1272 1273 if (is_forced_virtual) { 1274 klass = java_lang_Object_klass(); 1275 } 1276 1277 // Find the receiver in the stack. We do this before 1278 // trying to inline because the inliner can only use 1279 // zero-checked values, not being able to perform the 1280 // check itself. 1281 SharkValue *receiver = NULL; 1282 if (!is_static) { 1283 receiver = xstack(dest_method->arg_size() - 1); 1284 check_null(receiver); 1285 } 1286 1287 // Try to improve non-direct calls 1288 bool call_is_virtual = is_virtual || is_interface; 1289 ciMethod *call_method = dest_method; 1290 if (call_is_virtual) { 1291 ciMethod *optimized_method = improve_virtual_call( 1292 target(), klass, dest_method, receiver->type()); 1293 if (optimized_method) { 1294 call_method = optimized_method; 1295 call_is_virtual = false; 1296 } 1297 } 1298 1299 // Try to inline the call 1300 if (!call_is_virtual) { 1301 if (SharkInliner::attempt_inline(call_method, current_state())) 1302 return; 1303 } 1304 1305 // Find the method we are calling 1306 Value *callee; 1307 if (call_is_virtual) { 1308 if (is_virtual || is_forced_virtual) { 1309 assert(klass->is_linked(), "scan_for_traps responsibility"); 1310 int vtable_index = call_method->resolve_vtable_index( 1311 target()->holder(), klass); 1312 assert(vtable_index >= 0, "should be"); 1313 callee = get_virtual_callee(receiver, vtable_index); 1314 } 1315 else { 1316 assert(is_interface, "should be"); 1317 callee = get_interface_callee(receiver, call_method); 1318 } 1319 } 1320 else { 1321 callee = get_direct_callee(call_method); 1322 } 1323 1324 // Load the SharkEntry from the callee 1325 Value *base_pc = builder()->CreateValueOfStructEntry( 1326 callee, Method::from_interpreted_offset(), 1327 SharkType::intptr_type(), 1328 "base_pc"); 1329 1330 // Load the entry point from the SharkEntry 1331 Value *entry_point = builder()->CreateLoad( 1332 builder()->CreateIntToPtr( 1333 builder()->CreateAdd( 1334 base_pc, 1335 LLVMValue::intptr_constant(in_bytes(ZeroEntry::entry_point_offset()))), 1336 PointerType::getUnqual( 1337 PointerType::getUnqual(SharkType::entry_point_type()))), 1338 "entry_point"); 1339 1340 // Make the call 1341 decache_for_Java_call(call_method); 1342 Value *deoptimized_frames = builder()->CreateCall3( 1343 entry_point, callee, base_pc, thread()); 1344 1345 // If the callee got deoptimized then reexecute in the interpreter 1346 BasicBlock *reexecute = function()->CreateBlock("reexecute"); 1347 BasicBlock *call_completed = function()->CreateBlock("call_completed"); 1348 builder()->CreateCondBr( 1349 builder()->CreateICmpNE(deoptimized_frames, LLVMValue::jint_constant(0)), 1350 reexecute, call_completed); 1351 1352 builder()->SetInsertPoint(reexecute); 1353 builder()->CreateCall2( 1354 builder()->deoptimized_entry_point(), 1355 builder()->CreateSub(deoptimized_frames, LLVMValue::jint_constant(1)), 1356 thread()); 1357 builder()->CreateBr(call_completed); 1358 1359 // Cache after the call 1360 builder()->SetInsertPoint(call_completed); 1361 cache_after_Java_call(call_method); 1362 1363 // Check for pending exceptions 1364 check_pending_exception(EX_CHECK_FULL); 1365 1366 // Mark that a safepoint check has occurred 1367 current_state()->set_has_safepointed(true); 1368 } 1369 1370 bool SharkTopLevelBlock::static_subtype_check(ciKlass* check_klass, 1371 ciKlass* object_klass) { 1372 // If the class we're checking against is java.lang.Object 1373 // then this is a no brainer. Apparently this can happen 1374 // in reflective code... 1375 if (check_klass == java_lang_Object_klass()) 1376 return true; 1377 1378 // Perform a subtype check. NB in opto's code for this 1379 // (GraphKit::static_subtype_check) it says that static 1380 // interface types cannot be trusted, and if opto can't 1381 // trust them then I assume we can't either. 1382 if (object_klass->is_loaded() && !object_klass->is_interface()) { 1383 if (object_klass == check_klass) 1384 return true; 1385 1386 if (check_klass->is_loaded() && object_klass->is_subtype_of(check_klass)) 1387 return true; 1388 } 1389 1390 return false; 1391 } 1392 1393 void SharkTopLevelBlock::do_instance_check() { 1394 // Get the class we're checking against 1395 bool will_link; 1396 ciKlass *check_klass = iter()->get_klass(will_link); 1397 1398 // Get the class of the object we're checking 1399 ciKlass *object_klass = xstack(0)->type()->as_klass(); 1400 1401 // Can we optimize this check away? 1402 if (static_subtype_check(check_klass, object_klass)) { 1403 if (bc() == Bytecodes::_instanceof) { 1404 pop(); 1405 push(SharkValue::jint_constant(1)); 1406 } 1407 return; 1408 } 1409 1410 // Need to check this one at runtime 1411 if (will_link) 1412 do_full_instance_check(check_klass); 1413 else 1414 do_trapping_instance_check(check_klass); 1415 } 1416 1417 bool SharkTopLevelBlock::maybe_do_instanceof_if() { 1418 // Get the class we're checking against 1419 bool will_link; 1420 ciKlass *check_klass = iter()->get_klass(will_link); 1421 1422 // If the class is unloaded then the instanceof 1423 // cannot possibly succeed. 1424 if (!will_link) 1425 return false; 1426 1427 // Keep a copy of the object we're checking 1428 SharkValue *old_object = xstack(0); 1429 1430 // Get the class of the object we're checking 1431 ciKlass *object_klass = old_object->type()->as_klass(); 1432 1433 // If the instanceof can be optimized away at compile time 1434 // then any subsequent checkcasts will be too so we handle 1435 // it normally. 1436 if (static_subtype_check(check_klass, object_klass)) 1437 return false; 1438 1439 // Perform the instance check 1440 do_full_instance_check(check_klass); 1441 Value *result = pop()->jint_value(); 1442 1443 // Create the casted object 1444 SharkValue *new_object = SharkValue::create_generic( 1445 check_klass, old_object->jobject_value(), old_object->zero_checked()); 1446 1447 // Create two copies of the current state, one with the 1448 // original object and one with all instances of the 1449 // original object replaced with the new, casted object. 1450 SharkState *new_state = current_state(); 1451 SharkState *old_state = new_state->copy(); 1452 new_state->replace_all(old_object, new_object); 1453 1454 // Perform the check-and-branch 1455 switch (iter()->next_bc()) { 1456 case Bytecodes::_ifeq: 1457 // branch if not an instance 1458 do_if_helper( 1459 ICmpInst::ICMP_EQ, 1460 LLVMValue::jint_constant(0), result, 1461 old_state, new_state); 1462 break; 1463 1464 case Bytecodes::_ifne: 1465 // branch if an instance 1466 do_if_helper( 1467 ICmpInst::ICMP_NE, 1468 LLVMValue::jint_constant(0), result, 1469 new_state, old_state); 1470 break; 1471 1472 default: 1473 ShouldNotReachHere(); 1474 } 1475 1476 return true; 1477 } 1478 1479 void SharkTopLevelBlock::do_full_instance_check(ciKlass* klass) { 1480 BasicBlock *not_null = function()->CreateBlock("not_null"); 1481 BasicBlock *subtype_check = function()->CreateBlock("subtype_check"); 1482 BasicBlock *is_instance = function()->CreateBlock("is_instance"); 1483 BasicBlock *not_instance = function()->CreateBlock("not_instance"); 1484 BasicBlock *merge1 = function()->CreateBlock("merge1"); 1485 BasicBlock *merge2 = function()->CreateBlock("merge2"); 1486 1487 enum InstanceCheckStates { 1488 IC_IS_NULL, 1489 IC_IS_INSTANCE, 1490 IC_NOT_INSTANCE, 1491 }; 1492 1493 // Pop the object off the stack 1494 Value *object = pop()->jobject_value(); 1495 1496 // Null objects aren't instances of anything 1497 builder()->CreateCondBr( 1498 builder()->CreateICmpEQ(object, LLVMValue::null()), 1499 merge2, not_null); 1500 BasicBlock *null_block = builder()->GetInsertBlock(); 1501 1502 // Get the class we're checking against 1503 builder()->SetInsertPoint(not_null); 1504 Value *check_klass = builder()->CreateInlineMetadata(klass, SharkType::klass_type()); 1505 1506 // Get the class of the object being tested 1507 Value *object_klass = builder()->CreateValueOfStructEntry( 1508 object, in_ByteSize(oopDesc::klass_offset_in_bytes()), 1509 SharkType::klass_type(), 1510 "object_klass"); 1511 1512 // Perform the check 1513 builder()->CreateCondBr( 1514 builder()->CreateICmpEQ(check_klass, object_klass), 1515 is_instance, subtype_check); 1516 1517 builder()->SetInsertPoint(subtype_check); 1518 builder()->CreateCondBr( 1519 builder()->CreateICmpNE( 1520 builder()->CreateCall2( 1521 builder()->is_subtype_of(), check_klass, object_klass), 1522 LLVMValue::jbyte_constant(0)), 1523 is_instance, not_instance); 1524 1525 builder()->SetInsertPoint(is_instance); 1526 builder()->CreateBr(merge1); 1527 1528 builder()->SetInsertPoint(not_instance); 1529 builder()->CreateBr(merge1); 1530 1531 // First merge 1532 builder()->SetInsertPoint(merge1); 1533 PHINode *nonnull_result = builder()->CreatePHI( 1534 SharkType::jint_type(), 0, "nonnull_result"); 1535 nonnull_result->addIncoming( 1536 LLVMValue::jint_constant(IC_IS_INSTANCE), is_instance); 1537 nonnull_result->addIncoming( 1538 LLVMValue::jint_constant(IC_NOT_INSTANCE), not_instance); 1539 BasicBlock *nonnull_block = builder()->GetInsertBlock(); 1540 builder()->CreateBr(merge2); 1541 1542 // Second merge 1543 builder()->SetInsertPoint(merge2); 1544 PHINode *result = builder()->CreatePHI( 1545 SharkType::jint_type(), 0, "result"); 1546 result->addIncoming(LLVMValue::jint_constant(IC_IS_NULL), null_block); 1547 result->addIncoming(nonnull_result, nonnull_block); 1548 1549 // Handle the result 1550 if (bc() == Bytecodes::_checkcast) { 1551 BasicBlock *failure = function()->CreateBlock("failure"); 1552 BasicBlock *success = function()->CreateBlock("success"); 1553 1554 builder()->CreateCondBr( 1555 builder()->CreateICmpNE( 1556 result, LLVMValue::jint_constant(IC_NOT_INSTANCE)), 1557 success, failure); 1558 1559 builder()->SetInsertPoint(failure); 1560 SharkState *saved_state = current_state()->copy(); 1561 1562 call_vm( 1563 builder()->throw_ClassCastException(), 1564 builder()->CreateIntToPtr( 1565 LLVMValue::intptr_constant((intptr_t) __FILE__), 1566 PointerType::getUnqual(SharkType::jbyte_type())), 1567 LLVMValue::jint_constant(__LINE__), 1568 EX_CHECK_NONE); 1569 1570 Value *pending_exception = get_pending_exception(); 1571 clear_pending_exception(); 1572 handle_exception(pending_exception, EX_CHECK_FULL); 1573 1574 set_current_state(saved_state); 1575 builder()->SetInsertPoint(success); 1576 push(SharkValue::create_generic(klass, object, false)); 1577 } 1578 else { 1579 push( 1580 SharkValue::create_jint( 1581 builder()->CreateIntCast( 1582 builder()->CreateICmpEQ( 1583 result, LLVMValue::jint_constant(IC_IS_INSTANCE)), 1584 SharkType::jint_type(), false), false)); 1585 } 1586 } 1587 1588 void SharkTopLevelBlock::do_trapping_instance_check(ciKlass* klass) { 1589 BasicBlock *not_null = function()->CreateBlock("not_null"); 1590 BasicBlock *is_null = function()->CreateBlock("null"); 1591 1592 // Leave the object on the stack so it's there if we trap 1593 builder()->CreateCondBr( 1594 builder()->CreateICmpEQ(xstack(0)->jobject_value(), LLVMValue::null()), 1595 is_null, not_null); 1596 SharkState *saved_state = current_state()->copy(); 1597 1598 // If it's not null then we need to trap 1599 builder()->SetInsertPoint(not_null); 1600 set_current_state(saved_state->copy()); 1601 do_trap( 1602 Deoptimization::make_trap_request( 1603 Deoptimization::Reason_uninitialized, 1604 Deoptimization::Action_reinterpret)); 1605 1606 // If it's null then we're ok 1607 builder()->SetInsertPoint(is_null); 1608 set_current_state(saved_state); 1609 if (bc() == Bytecodes::_checkcast) { 1610 push(SharkValue::create_generic(klass, pop()->jobject_value(), false)); 1611 } 1612 else { 1613 pop(); 1614 push(SharkValue::jint_constant(0)); 1615 } 1616 } 1617 1618 void SharkTopLevelBlock::do_new() { 1619 bool will_link; 1620 ciInstanceKlass* klass = iter()->get_klass(will_link)->as_instance_klass(); 1621 assert(will_link, "typeflow responsibility"); 1622 1623 BasicBlock *got_tlab = NULL; 1624 BasicBlock *heap_alloc = NULL; 1625 BasicBlock *retry = NULL; 1626 BasicBlock *got_heap = NULL; 1627 BasicBlock *initialize = NULL; 1628 BasicBlock *got_fast = NULL; 1629 BasicBlock *slow_alloc_and_init = NULL; 1630 BasicBlock *got_slow = NULL; 1631 BasicBlock *push_object = NULL; 1632 1633 SharkState *fast_state = NULL; 1634 1635 Value *tlab_object = NULL; 1636 Value *heap_object = NULL; 1637 Value *fast_object = NULL; 1638 Value *slow_object = NULL; 1639 Value *object = NULL; 1640 1641 // The fast path 1642 if (!Klass::layout_helper_needs_slow_path(klass->layout_helper())) { 1643 if (UseTLAB) { 1644 got_tlab = function()->CreateBlock("got_tlab"); 1645 heap_alloc = function()->CreateBlock("heap_alloc"); 1646 } 1647 retry = function()->CreateBlock("retry"); 1648 got_heap = function()->CreateBlock("got_heap"); 1649 initialize = function()->CreateBlock("initialize"); 1650 slow_alloc_and_init = function()->CreateBlock("slow_alloc_and_init"); 1651 push_object = function()->CreateBlock("push_object"); 1652 1653 size_t size_in_bytes = klass->size_helper() << LogHeapWordSize; 1654 1655 // Thread local allocation 1656 if (UseTLAB) { 1657 Value *top_addr = builder()->CreateAddressOfStructEntry( 1658 thread(), Thread::tlab_top_offset(), 1659 PointerType::getUnqual(SharkType::intptr_type()), 1660 "top_addr"); 1661 1662 Value *end = builder()->CreateValueOfStructEntry( 1663 thread(), Thread::tlab_end_offset(), 1664 SharkType::intptr_type(), 1665 "end"); 1666 1667 Value *old_top = builder()->CreateLoad(top_addr, "old_top"); 1668 Value *new_top = builder()->CreateAdd( 1669 old_top, LLVMValue::intptr_constant(size_in_bytes)); 1670 1671 builder()->CreateCondBr( 1672 builder()->CreateICmpULE(new_top, end), 1673 got_tlab, heap_alloc); 1674 1675 builder()->SetInsertPoint(got_tlab); 1676 tlab_object = builder()->CreateIntToPtr( 1677 old_top, SharkType::oop_type(), "tlab_object"); 1678 1679 builder()->CreateStore(new_top, top_addr); 1680 builder()->CreateBr(initialize); 1681 1682 builder()->SetInsertPoint(heap_alloc); 1683 } 1684 1685 // Heap allocation 1686 Value *top_addr = builder()->CreateIntToPtr( 1687 LLVMValue::intptr_constant((intptr_t) Universe::heap()->top_addr()), 1688 PointerType::getUnqual(SharkType::intptr_type()), 1689 "top_addr"); 1690 1691 Value *end = builder()->CreateLoad( 1692 builder()->CreateIntToPtr( 1693 LLVMValue::intptr_constant((intptr_t) Universe::heap()->end_addr()), 1694 PointerType::getUnqual(SharkType::intptr_type())), 1695 "end"); 1696 1697 builder()->CreateBr(retry); 1698 builder()->SetInsertPoint(retry); 1699 1700 Value *old_top = builder()->CreateLoad(top_addr, "top"); 1701 Value *new_top = builder()->CreateAdd( 1702 old_top, LLVMValue::intptr_constant(size_in_bytes)); 1703 1704 builder()->CreateCondBr( 1705 builder()->CreateICmpULE(new_top, end), 1706 got_heap, slow_alloc_and_init); 1707 1708 builder()->SetInsertPoint(got_heap); 1709 heap_object = builder()->CreateIntToPtr( 1710 old_top, SharkType::oop_type(), "heap_object"); 1711 1712 Value *check = builder()->CreateAtomicCmpXchg(top_addr, old_top, new_top, llvm::SequentiallyConsistent); 1713 builder()->CreateCondBr( 1714 builder()->CreateICmpEQ(old_top, check), 1715 initialize, retry); 1716 1717 // Initialize the object 1718 builder()->SetInsertPoint(initialize); 1719 if (tlab_object) { 1720 PHINode *phi = builder()->CreatePHI( 1721 SharkType::oop_type(), 0, "fast_object"); 1722 phi->addIncoming(tlab_object, got_tlab); 1723 phi->addIncoming(heap_object, got_heap); 1724 fast_object = phi; 1725 } 1726 else { 1727 fast_object = heap_object; 1728 } 1729 1730 builder()->CreateMemset( 1731 builder()->CreateBitCast( 1732 fast_object, PointerType::getUnqual(SharkType::jbyte_type())), 1733 LLVMValue::jbyte_constant(0), 1734 LLVMValue::jint_constant(size_in_bytes), 1735 LLVMValue::jint_constant(HeapWordSize)); 1736 1737 Value *mark_addr = builder()->CreateAddressOfStructEntry( 1738 fast_object, in_ByteSize(oopDesc::mark_offset_in_bytes()), 1739 PointerType::getUnqual(SharkType::intptr_type()), 1740 "mark_addr"); 1741 1742 Value *klass_addr = builder()->CreateAddressOfStructEntry( 1743 fast_object, in_ByteSize(oopDesc::klass_offset_in_bytes()), 1744 PointerType::getUnqual(SharkType::klass_type()), 1745 "klass_addr"); 1746 1747 // Set the mark 1748 intptr_t mark; 1749 if (UseBiasedLocking) { 1750 Unimplemented(); 1751 } 1752 else { 1753 mark = (intptr_t) markOopDesc::prototype(); 1754 } 1755 builder()->CreateStore(LLVMValue::intptr_constant(mark), mark_addr); 1756 1757 // Set the class 1758 Value *rtklass = builder()->CreateInlineMetadata(klass, SharkType::klass_type()); 1759 builder()->CreateStore(rtklass, klass_addr); 1760 got_fast = builder()->GetInsertBlock(); 1761 1762 builder()->CreateBr(push_object); 1763 builder()->SetInsertPoint(slow_alloc_and_init); 1764 fast_state = current_state()->copy(); 1765 } 1766 1767 // The slow path 1768 call_vm( 1769 builder()->new_instance(), 1770 LLVMValue::jint_constant(iter()->get_klass_index()), 1771 EX_CHECK_FULL); 1772 slow_object = get_vm_result(); 1773 got_slow = builder()->GetInsertBlock(); 1774 1775 // Push the object 1776 if (push_object) { 1777 builder()->CreateBr(push_object); 1778 builder()->SetInsertPoint(push_object); 1779 } 1780 if (fast_object) { 1781 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "object"); 1782 phi->addIncoming(fast_object, got_fast); 1783 phi->addIncoming(slow_object, got_slow); 1784 object = phi; 1785 current_state()->merge(fast_state, got_fast, got_slow); 1786 } 1787 else { 1788 object = slow_object; 1789 } 1790 1791 push(SharkValue::create_jobject(object, true)); 1792 } 1793 1794 void SharkTopLevelBlock::do_newarray() { 1795 BasicType type = (BasicType) iter()->get_index(); 1796 1797 call_vm( 1798 builder()->newarray(), 1799 LLVMValue::jint_constant(type), 1800 pop()->jint_value(), 1801 EX_CHECK_FULL); 1802 1803 ciArrayKlass *array_klass = ciArrayKlass::make(ciType::make(type)); 1804 push(SharkValue::create_generic(array_klass, get_vm_result(), true)); 1805 } 1806 1807 void SharkTopLevelBlock::do_anewarray() { 1808 bool will_link; 1809 ciKlass *klass = iter()->get_klass(will_link); 1810 assert(will_link, "typeflow responsibility"); 1811 1812 ciObjArrayKlass *array_klass = ciObjArrayKlass::make(klass); 1813 if (!array_klass->is_loaded()) { 1814 Unimplemented(); 1815 } 1816 1817 call_vm( 1818 builder()->anewarray(), 1819 LLVMValue::jint_constant(iter()->get_klass_index()), 1820 pop()->jint_value(), 1821 EX_CHECK_FULL); 1822 1823 push(SharkValue::create_generic(array_klass, get_vm_result(), true)); 1824 } 1825 1826 void SharkTopLevelBlock::do_multianewarray() { 1827 bool will_link; 1828 ciArrayKlass *array_klass = iter()->get_klass(will_link)->as_array_klass(); 1829 assert(will_link, "typeflow responsibility"); 1830 1831 // The dimensions are stack values, so we use their slots for the 1832 // dimensions array. Note that we are storing them in the reverse 1833 // of normal stack order. 1834 int ndims = iter()->get_dimensions(); 1835 1836 Value *dimensions = stack()->slot_addr( 1837 stack()->stack_slots_offset() + max_stack() - xstack_depth(), 1838 ArrayType::get(SharkType::jint_type(), ndims), 1839 "dimensions"); 1840 1841 for (int i = 0; i < ndims; i++) { 1842 builder()->CreateStore( 1843 xstack(ndims - 1 - i)->jint_value(), 1844 builder()->CreateStructGEP(dimensions, i)); 1845 } 1846 1847 call_vm( 1848 builder()->multianewarray(), 1849 LLVMValue::jint_constant(iter()->get_klass_index()), 1850 LLVMValue::jint_constant(ndims), 1851 builder()->CreateStructGEP(dimensions, 0), 1852 EX_CHECK_FULL); 1853 1854 // Now we can pop the dimensions off the stack 1855 for (int i = 0; i < ndims; i++) 1856 pop(); 1857 1858 push(SharkValue::create_generic(array_klass, get_vm_result(), true)); 1859 } 1860 1861 void SharkTopLevelBlock::acquire_method_lock() { 1862 Value *lockee; 1863 if (target()->is_static()) { 1864 lockee = builder()->CreateInlineOop(target()->holder()->java_mirror()); 1865 } 1866 else 1867 lockee = local(0)->jobject_value(); 1868 1869 iter()->force_bci(start()); // for the decache in acquire_lock 1870 acquire_lock(lockee, EX_CHECK_NO_CATCH); 1871 } 1872 1873 void SharkTopLevelBlock::do_monitorenter() { 1874 SharkValue *lockee = pop(); 1875 check_null(lockee); 1876 acquire_lock(lockee->jobject_value(), EX_CHECK_FULL); 1877 } 1878 1879 void SharkTopLevelBlock::do_monitorexit() { 1880 pop(); // don't need this (monitors are block structured) 1881 release_lock(EX_CHECK_NO_CATCH); 1882 } 1883 1884 void SharkTopLevelBlock::acquire_lock(Value *lockee, int exception_action) { 1885 BasicBlock *try_recursive = function()->CreateBlock("try_recursive"); 1886 BasicBlock *got_recursive = function()->CreateBlock("got_recursive"); 1887 BasicBlock *not_recursive = function()->CreateBlock("not_recursive"); 1888 BasicBlock *acquired_fast = function()->CreateBlock("acquired_fast"); 1889 BasicBlock *lock_acquired = function()->CreateBlock("lock_acquired"); 1890 1891 int monitor = num_monitors(); 1892 Value *monitor_addr = stack()->monitor_addr(monitor); 1893 Value *monitor_object_addr = stack()->monitor_object_addr(monitor); 1894 Value *monitor_header_addr = stack()->monitor_header_addr(monitor); 1895 1896 // Store the object and mark the slot as live 1897 builder()->CreateStore(lockee, monitor_object_addr); 1898 set_num_monitors(monitor + 1); 1899 1900 // Try a simple lock 1901 Value *mark_addr = builder()->CreateAddressOfStructEntry( 1902 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()), 1903 PointerType::getUnqual(SharkType::intptr_type()), 1904 "mark_addr"); 1905 1906 Value *mark = builder()->CreateLoad(mark_addr, "mark"); 1907 Value *disp = builder()->CreateOr( 1908 mark, LLVMValue::intptr_constant(markOopDesc::unlocked_value), "disp"); 1909 builder()->CreateStore(disp, monitor_header_addr); 1910 1911 Value *lock = builder()->CreatePtrToInt( 1912 monitor_header_addr, SharkType::intptr_type()); 1913 Value *check = builder()->CreateAtomicCmpXchg(mark_addr, disp, lock, llvm::Acquire); 1914 builder()->CreateCondBr( 1915 builder()->CreateICmpEQ(disp, check), 1916 acquired_fast, try_recursive); 1917 1918 // Locking failed, but maybe this thread already owns it 1919 builder()->SetInsertPoint(try_recursive); 1920 Value *addr = builder()->CreateAnd( 1921 disp, 1922 LLVMValue::intptr_constant(~markOopDesc::lock_mask_in_place)); 1923 1924 // NB we use the entire stack, but JavaThread::is_lock_owned() 1925 // uses a more limited range. I don't think it hurts though... 1926 Value *stack_limit = builder()->CreateValueOfStructEntry( 1927 thread(), Thread::stack_base_offset(), 1928 SharkType::intptr_type(), 1929 "stack_limit"); 1930 1931 assert(sizeof(size_t) == sizeof(intptr_t), "should be"); 1932 Value *stack_size = builder()->CreateValueOfStructEntry( 1933 thread(), Thread::stack_size_offset(), 1934 SharkType::intptr_type(), 1935 "stack_size"); 1936 1937 Value *stack_start = 1938 builder()->CreateSub(stack_limit, stack_size, "stack_start"); 1939 1940 builder()->CreateCondBr( 1941 builder()->CreateAnd( 1942 builder()->CreateICmpUGE(addr, stack_start), 1943 builder()->CreateICmpULT(addr, stack_limit)), 1944 got_recursive, not_recursive); 1945 1946 builder()->SetInsertPoint(got_recursive); 1947 builder()->CreateStore(LLVMValue::intptr_constant(0), monitor_header_addr); 1948 builder()->CreateBr(acquired_fast); 1949 1950 // Create an edge for the state merge 1951 builder()->SetInsertPoint(acquired_fast); 1952 SharkState *fast_state = current_state()->copy(); 1953 builder()->CreateBr(lock_acquired); 1954 1955 // It's not a recursive case so we need to drop into the runtime 1956 builder()->SetInsertPoint(not_recursive); 1957 call_vm( 1958 builder()->monitorenter(), monitor_addr, 1959 exception_action | EAM_MONITOR_FUDGE); 1960 BasicBlock *acquired_slow = builder()->GetInsertBlock(); 1961 builder()->CreateBr(lock_acquired); 1962 1963 // All done 1964 builder()->SetInsertPoint(lock_acquired); 1965 current_state()->merge(fast_state, acquired_fast, acquired_slow); 1966 } 1967 1968 void SharkTopLevelBlock::release_lock(int exception_action) { 1969 BasicBlock *not_recursive = function()->CreateBlock("not_recursive"); 1970 BasicBlock *released_fast = function()->CreateBlock("released_fast"); 1971 BasicBlock *slow_path = function()->CreateBlock("slow_path"); 1972 BasicBlock *lock_released = function()->CreateBlock("lock_released"); 1973 1974 int monitor = num_monitors() - 1; 1975 Value *monitor_addr = stack()->monitor_addr(monitor); 1976 Value *monitor_object_addr = stack()->monitor_object_addr(monitor); 1977 Value *monitor_header_addr = stack()->monitor_header_addr(monitor); 1978 1979 // If it is recursive then we're already done 1980 Value *disp = builder()->CreateLoad(monitor_header_addr); 1981 builder()->CreateCondBr( 1982 builder()->CreateICmpEQ(disp, LLVMValue::intptr_constant(0)), 1983 released_fast, not_recursive); 1984 1985 // Try a simple unlock 1986 builder()->SetInsertPoint(not_recursive); 1987 1988 Value *lock = builder()->CreatePtrToInt( 1989 monitor_header_addr, SharkType::intptr_type()); 1990 1991 Value *lockee = builder()->CreateLoad(monitor_object_addr); 1992 1993 Value *mark_addr = builder()->CreateAddressOfStructEntry( 1994 lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()), 1995 PointerType::getUnqual(SharkType::intptr_type()), 1996 "mark_addr"); 1997 1998 Value *check = builder()->CreateAtomicCmpXchg(mark_addr, lock, disp, llvm::Release); 1999 builder()->CreateCondBr( 2000 builder()->CreateICmpEQ(lock, check), 2001 released_fast, slow_path); 2002 2003 // Create an edge for the state merge 2004 builder()->SetInsertPoint(released_fast); 2005 SharkState *fast_state = current_state()->copy(); 2006 builder()->CreateBr(lock_released); 2007 2008 // Need to drop into the runtime to release this one 2009 builder()->SetInsertPoint(slow_path); 2010 call_vm(builder()->monitorexit(), monitor_addr, exception_action); 2011 BasicBlock *released_slow = builder()->GetInsertBlock(); 2012 builder()->CreateBr(lock_released); 2013 2014 // All done 2015 builder()->SetInsertPoint(lock_released); 2016 current_state()->merge(fast_state, released_fast, released_slow); 2017 2018 // The object slot is now dead 2019 set_num_monitors(monitor); 2020 }