src/share/vm/shark/sharkTopLevelBlock.cpp

Mon, 04 Apr 2011 03:02:00 -0700

author
twisti
date
Mon, 04 Apr 2011 03:02:00 -0700
changeset 2729
e863062e521d
parent 2314
f95d63e2154a
child 3391
069ab3f976d3
permissions
-rw-r--r--

7032458: Zero and Shark fixes
Reviewed-by: twisti
Contributed-by: Gary Benson <gbenson@redhat.com>

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

mercurial