src/share/vm/shark/sharkTopLevelBlock.cpp

Fri, 11 Jan 2013 16:47:23 -0800

author
twisti
date
Fri, 11 Jan 2013 16:47:23 -0800
changeset 4442
c566b81b3323
parent 4314
2cd5e15048e6
child 4444
606eada1bf86
permissions
-rw-r--r--

8005817: Shark: implement deoptimization support
Reviewed-by: twisti
Contributed-by: Roman Kennke <rkennke@redhat.com>

     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  */
    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     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;
    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);
    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       }
    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;
   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");
   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       }
   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;
   138     case Bytecodes::_new:
   139       klass = iter()->get_klass(will_link)->as_instance_klass();
   140       assert(will_link, "typeflow responsibility");
   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       }
   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   }
   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 }
   175 bool SharkTopLevelBlock::static_field_ok_in_clinit(ciField* field) {
   176   assert(field->is_static(), "should be");
   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 }
   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 }
   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 }
   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;
   235   // Recurse into the tree
   236   if (!entered()) {
   237     _entered = true;
   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 }
   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 }
   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 }
   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;
   279     default:
   280       type = callee->return_type();
   281     }
   283     push(SharkValue::create_generic(type, NULL, false));
   284   }
   285   SharkJavaCallCacher(function(), callee).scan(current_state());
   286 }
   288 void SharkTopLevelBlock::decache_for_VM_call() {
   289   SharkVMCallDecacher(function(), bci()).scan(current_state());
   290 }
   292 void SharkTopLevelBlock::cache_after_VM_call() {
   293   SharkVMCallCacher(function()).scan(current_state());
   294 }
   296 void SharkTopLevelBlock::decache_for_trap() {
   297   SharkTrapDecacher(function(), bci()).scan(current_state());
   298 }
   300 void SharkTopLevelBlock::emit_IR() {
   301   builder()->SetInsertPoint(entry_block());
   303   // Parse the bytecode
   304   parse_bytecode(start(), limit());
   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 }
   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 }
   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   }
   335   value->set_zero_checked(true);
   336 }
   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 }
   352 void SharkTopLevelBlock::zero_check_value(SharkValue* value,
   353                                           BasicBlock* continue_block) {
   354   BasicBlock *zero_block = builder()->CreateBlock(continue_block, "zero");
   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   }
   379   builder()->CreateCondBr(
   380     builder()->CreateICmpNE(a, b), continue_block, zero_block);
   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   }
   402   Value *pending_exception = get_pending_exception();
   403   clear_pending_exception();
   404   handle_exception(pending_exception, EX_CHECK_FULL);
   405 }
   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");
   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);
   417   builder()->SetInsertPoint(out_of_bounds);
   418   SharkState *saved_state = current_state()->copy();
   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);
   429   Value *pending_exception = get_pending_exception();
   430   clear_pending_exception();
   431   handle_exception(pending_exception, EX_CHECK_FULL);
   433   set_current_state(saved_state);
   435   builder()->SetInsertPoint(in_bounds);
   436 }
   438 void SharkTopLevelBlock::check_pending_exception(int action) {
   439   assert(action & EAM_CHECK, "should be");
   441   BasicBlock *exception    = function()->CreateBlock("exception");
   442   BasicBlock *no_exception = function()->CreateBlock("no_exception");
   444   Value *pending_exception = get_pending_exception();
   445   builder()->CreateCondBr(
   446     builder()->CreateICmpEQ(pending_exception, LLVMValue::null()),
   447     no_exception, exception);
   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);
   462   builder()->SetInsertPoint(no_exception);
   463 }
   465 void SharkTopLevelBlock::compute_exceptions() {
   466   ciExceptionHandlerStream str(target(), start());
   468   int exc_count = str.count();
   469   _exc_handlers = new GrowableArray<ciExceptionHandler*>(exc_count);
   470   _exceptions   = new GrowableArray<SharkTopLevelBlock*>(exc_count);
   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);
   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     }
   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 }
   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));
   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--;
   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       }
   542       if (all_loaded)
   543         marshal_exception_fast(num_options);
   544       else
   545         marshal_exception_slow(num_options);
   546     }
   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");
   553       builder()->CreateBr(handler->entry_block());
   554       handler->add_incoming(current_state());
   555       return;
   556     }
   557   }
   559   // No exception handler was found; unwind and return
   560   handle_return(T_VOID, exception);
   561 }
   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");
   570   for (int i = 0; i < num_options; i++) {
   571     Value *check_klass =
   572       builder()->CreateInlineMetadata(exc_handler(i)->catch_klass(), SharkType::klass_type());
   574     BasicBlock *not_exact   = function()->CreateBlock("not_exact");
   575     BasicBlock *not_subtype = function()->CreateBlock("not_subtype");
   577     builder()->CreateCondBr(
   578       builder()->CreateICmpEQ(check_klass, exception_klass),
   579       handler_for_exception(i), not_exact);
   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);
   589     builder()->SetInsertPoint(not_subtype);
   590   }
   591 }
   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();
   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);
   607   BasicBlock *no_handler = function()->CreateBlock("no_handler");
   608   SwitchInst *switchinst = builder()->CreateSwitch(
   609     index, no_handler, num_options);
   611   for (int i = 0; i < num_options; i++) {
   612     switchinst->addCase(
   613       LLVMValue::jint_constant(i),
   614       handler_for_exception(i));
   615   }
   617   builder()->SetInsertPoint(no_handler);
   618 }
   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 }
   635 void SharkTopLevelBlock::maybe_add_safepoint() {
   636   if (current_state()->has_safepointed())
   637     return;
   639   BasicBlock *orig_block = builder()->GetInsertBlock();
   640   SharkState *orig_state = current_state()->copy();
   642   BasicBlock *do_safepoint = function()->CreateBlock("do_safepoint");
   643   BasicBlock *safepointed  = function()->CreateBlock("safepointed");
   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");
   652   builder()->CreateCondBr(
   653     builder()->CreateICmpEQ(
   654       state,
   655       LLVMValue::jint_constant(SafepointSynchronize::_synchronizing)),
   656     do_safepoint, safepointed);
   658   builder()->SetInsertPoint(do_safepoint);
   659   call_vm(builder()->safepoint(), EX_CHECK_FULL);
   660   BasicBlock *safepointed_block = builder()->GetInsertBlock();
   661   builder()->CreateBr(safepointed);
   663   builder()->SetInsertPoint(safepointed);
   664   current_state()->merge(orig_state, orig_block, safepointed_block);
   666   current_state()->set_has_safepointed(true);
   667 }
   669 void SharkTopLevelBlock::maybe_add_backedge_safepoint() {
   670   if (current_state()->has_safepointed())
   671     return;
   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 }
   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;
   685   return can_reach_helper(other);
   686 }
   688 bool SharkTopLevelBlock::can_reach_helper(SharkTopLevelBlock* other) {
   689   if (this == other)
   690     return true;
   692   if (_can_reach_visited)
   693     return false;
   694   _can_reach_visited = true;
   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   }
   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   }
   709   return false;
   710 }
   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);
   717   int orig_bci = bci();
   718   iter()->force_bci(trap_bci);
   720   do_trap(trap_request);
   722   builder()->SetInsertPoint(orig_block);
   723   iter()->force_bci(orig_bci);
   725   return trap_block;
   726 }
   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 }
   737 void SharkTopLevelBlock::call_register_finalizer(Value *receiver) {
   738   BasicBlock *orig_block = builder()->GetInsertBlock();
   739   SharkState *orig_state = current_state()->copy();
   741   BasicBlock *do_call = function()->CreateBlock("has_finalizer");
   742   BasicBlock *done    = function()->CreateBlock("done");
   744   Value *klass = builder()->CreateValueOfStructEntry(
   745     receiver,
   746     in_ByteSize(oopDesc::klass_offset_in_bytes()),
   747     SharkType::oop_type(),
   748     "klass");
   750   Value *access_flags = builder()->CreateValueOfStructEntry(
   751     klass,
   752     Klass::access_flags_offset(),
   753     SharkType::jint_type(),
   754     "access_flags");
   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);
   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);
   769   builder()->SetInsertPoint(done);
   770   current_state()->merge(orig_state, orig_block, branch_block);
   771 }
   773 void SharkTopLevelBlock::handle_return(BasicType type, Value* exception) {
   774   assert (exception == NULL || type == T_VOID, "exception OR result, please");
   776   if (num_monitors()) {
   777     // Protect our exception across possible monitor release decaches
   778     if (exception)
   779       set_oop_tmp(exception);
   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);
   789     // Reload the exception we're throwing
   790     if (exception)
   791       exception = get_oop_tmp();
   792   }
   794   if (exception) {
   795     builder()->CreateStore(exception, pending_exception_address());
   796   }
   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   }
   807   builder()->CreateRet(LLVMValue::jint_constant(0));
   808 }
   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 }
   817 void SharkTopLevelBlock::do_aload(BasicType basic_type) {
   818   SharkValue *index = pop();
   819   SharkValue *array = pop();
   821   check_null(array);
   822   check_bounds(array, index);
   824   Value *value = builder()->CreateLoad(
   825     builder()->CreateArrayAddress(
   826       array->jarray_value(), basic_type, index->jint_value()));
   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);
   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;
   840   case T_LONG:
   841     push(SharkValue::create_jlong(value, false));
   842     break;
   844   case T_FLOAT:
   845     push(SharkValue::create_jfloat(value));
   846     break;
   848   case T_DOUBLE:
   849     push(SharkValue::create_jdouble(value));
   850     break;
   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;
   870   default:
   871     tty->print_cr("Unhandled type %s", type2name(basic_type));
   872     ShouldNotReachHere();
   873   }
   874 }
   876 void SharkTopLevelBlock::do_astore(BasicType basic_type) {
   877   SharkValue *svalue = pop();
   878   SharkValue *index  = pop();
   879   SharkValue *array  = pop();
   881   check_null(array);
   882   check_bounds(array, index);
   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;
   893   case T_LONG:
   894     value = svalue->jlong_value();
   895     break;
   897   case T_FLOAT:
   898     value = svalue->jfloat_value();
   899     break;
   901   case T_DOUBLE:
   902     value = svalue->jdouble_value();
   903     break;
   905   case T_OBJECT:
   906     value = svalue->jobject_value();
   907     // XXX assignability check
   908     break;
   910   default:
   911     tty->print_cr("Unhandled type %s", type2name(basic_type));
   912     ShouldNotReachHere();
   913   }
   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);
   919   Value *addr = builder()->CreateArrayAddress(
   920     array->jarray_value(), basic_type, index->jint_value(), "addr");
   922   builder()->CreateStore(value, addr);
   924   if (basic_type == T_OBJECT) // XXX or T_ARRAY?
   925     builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr);
   926 }
   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 }
   935 void SharkTopLevelBlock::do_athrow() {
   936   SharkValue *exception = pop();
   937   check_null(exception);
   938   handle_exception(exception->jobject_value(), EX_CHECK_FULL);
   939 }
   941 void SharkTopLevelBlock::do_goto() {
   942   do_branch(ciTypeFlow::GOTO_TARGET);
   943 }
   945 void SharkTopLevelBlock::do_jsr() {
   946   push(SharkValue::address_constant(iter()->next_bci()));
   947   do_branch(ciTypeFlow::GOTO_TARGET);
   948 }
   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 }
   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
   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 }
   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 }
   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);
   992   builder()->CreateCondBr(
   993     builder()->CreateICmp(p, a, b),
   994     if_taken->entry_block(), not_taken->entry_block());
   996   if_taken->add_incoming(if_taken_state);
   997   not_taken->add_incoming(not_taken_state);
   998 }
  1000 void SharkTopLevelBlock::do_switch() {
  1001   int len = switch_table_length();
  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());
  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());
  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;
  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;
  1033   // This code can replace a virtual call with a direct call if this
  1034   // class is the only one in the entire set of loaded classes that
  1035   // implements this method.  This makes the compiled code dependent
  1036   // on other classes that implement the method not being loaded, a
  1037   // condition which is enforced by the dependency tracker.  If the
  1038   // dependency tracker determines a method has become invalid it
  1039   // will mark it for recompilation, causing running copies to be
  1040   // deoptimized.  Shark currently can't deoptimize arbitrarily like
  1041   // that, so this optimization cannot be used.
  1042   // http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=481
  1044   // All other interesting cases are instance classes
  1045   if (!receiver_type->is_instance_klass())
  1046     return NULL;
  1048   // Attempt to improve the receiver
  1049   ciInstanceKlass* actual_receiver = klass;
  1050   ciInstanceKlass *improved_receiver = receiver_type->as_instance_klass();
  1051   if (improved_receiver->is_loaded() &&
  1052       improved_receiver->is_initialized() &&
  1053       !improved_receiver->is_interface() &&
  1054       improved_receiver->is_subtype_of(actual_receiver)) {
  1055     actual_receiver = improved_receiver;
  1058   // Attempt to find a monomorphic target for this call using
  1059   // class heirachy analysis.
  1060   ciInstanceKlass *calling_klass = caller->holder();
  1061   ciMethod* monomorphic_target =
  1062     dest_method->find_monomorphic_target(calling_klass, klass, actual_receiver);
  1063   if (monomorphic_target != NULL) {
  1064     assert(!monomorphic_target->is_abstract(), "shouldn't be");
  1066     function()->dependencies()->assert_unique_concrete_method(actual_receiver, monomorphic_target);
  1068     // Opto has a bunch of type checking here that I don't
  1069     // understand.  It's to inhibit casting in one direction,
  1070     // possibly because objects in Opto can have inexact
  1071     // types, but I can't even tell which direction it
  1072     // doesn't like.  For now I'm going to block *any* cast.
  1073     if (monomorphic_target != dest_method) {
  1074       if (SharkPerformanceWarnings) {
  1075         warning("found monomorphic target, but inhibited cast:");
  1076         tty->print("  dest_method = ");
  1077         dest_method->print_short_name(tty);
  1078         tty->cr();
  1079         tty->print("  monomorphic_target = ");
  1080         monomorphic_target->print_short_name(tty);
  1081         tty->cr();
  1083       monomorphic_target = NULL;
  1087   // Replace the virtual call with a direct one.  This makes
  1088   // us dependent on that target method not getting overridden
  1089   // by dynamic class loading.
  1090   if (monomorphic_target != NULL) {
  1091     dependencies()->assert_unique_concrete_method(
  1092       actual_receiver, monomorphic_target);
  1093     return monomorphic_target;
  1096   // Because Opto distinguishes exact types from inexact ones
  1097   // it can perform a further optimization to replace calls
  1098   // with non-monomorphic targets if the receiver has an exact
  1099   // type.  We don't mark types this way, so we can't do this.
  1102   return NULL;
  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");
  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");
  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");
  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");
  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");
  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");
  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);
  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");
  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);
  1182   Value *itable_entry = builder()->CreateIntToPtr(
  1183     itable_entry_addr, SharkType::itableOffsetEntry_type(), "itable_entry");
  1185   Value *itable_iklass = builder()->CreateValueOfStructEntry(
  1186     itable_entry,
  1187     in_ByteSize(itableOffsetEntry::interface_offset_in_bytes()),
  1188     SharkType::klass_type(),
  1189     "itable_iklass");
  1191   builder()->CreateCondBr(
  1192     builder()->CreateICmpEQ(itable_iklass, LLVMValue::nullKlass()),
  1193     got_null, not_null);
  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();
  1202   builder()->SetInsertPoint(not_null);
  1203   builder()->CreateCondBr(
  1204     builder()->CreateICmpEQ(itable_iklass, iklass),
  1205     got_entry, next);
  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);
  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);
  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");
  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;
  1246   // Find the method being called
  1247   bool will_link;
  1248   ciSignature* sig;
  1249   ciMethod *dest_method = iter()->get_method(will_link, &sig);
  1251   assert(will_link, "typeflow responsibility");
  1252   assert(dest_method->is_static() == is_static, "must match bc");
  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");
  1267   bool is_forced_virtual = is_interface && holder_klass == java_lang_Object_klass();
  1269   ciKlass *holder = iter()->get_declared_method_holder();
  1270   ciInstanceKlass *klass =
  1271     ciEnv::get_instance_klass_for_declared_method_holder(holder);
  1273   if (is_forced_virtual) {
  1274     klass = java_lang_Object_klass();
  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);
  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;
  1299   // Try to inline the call
  1300   if (!call_is_virtual) {
  1301     if (SharkInliner::attempt_inline(call_method, current_state())) {
  1302       return;
  1306   // Find the method we are calling
  1307   Value *callee;
  1308   if (call_is_virtual) {
  1309     if (is_virtual || is_forced_virtual) {
  1310       assert(klass->is_linked(), "scan_for_traps responsibility");
  1311       int vtable_index = call_method->resolve_vtable_index(
  1312         target()->holder(), klass);
  1313       assert(vtable_index >= 0, "should be");
  1314       callee = get_virtual_callee(receiver, vtable_index);
  1316     else {
  1317       assert(is_interface, "should be");
  1318       callee = get_interface_callee(receiver, call_method);
  1321   else {
  1322     callee = get_direct_callee(call_method);
  1325   // Load the SharkEntry from the callee
  1326   Value *base_pc = builder()->CreateValueOfStructEntry(
  1327     callee, Method::from_interpreted_offset(),
  1328     SharkType::intptr_type(),
  1329     "base_pc");
  1331   // Load the entry point from the SharkEntry
  1332   Value *entry_point = builder()->CreateLoad(
  1333     builder()->CreateIntToPtr(
  1334       builder()->CreateAdd(
  1335         base_pc,
  1336         LLVMValue::intptr_constant(in_bytes(ZeroEntry::entry_point_offset()))),
  1337       PointerType::getUnqual(
  1338         PointerType::getUnqual(SharkType::entry_point_type()))),
  1339     "entry_point");
  1341   // Make the call
  1342   decache_for_Java_call(call_method);
  1343   Value *deoptimized_frames = builder()->CreateCall3(
  1344     entry_point, callee, base_pc, thread());
  1346   // If the callee got deoptimized then reexecute in the interpreter
  1347   BasicBlock *reexecute      = function()->CreateBlock("reexecute");
  1348   BasicBlock *call_completed = function()->CreateBlock("call_completed");
  1349   builder()->CreateCondBr(
  1350     builder()->CreateICmpNE(deoptimized_frames, LLVMValue::jint_constant(0)),
  1351     reexecute, call_completed);
  1353   builder()->SetInsertPoint(reexecute);
  1354   builder()->CreateCall2(
  1355     builder()->deoptimized_entry_point(),
  1356     builder()->CreateSub(deoptimized_frames, LLVMValue::jint_constant(1)),
  1357     thread());
  1358   builder()->CreateBr(call_completed);
  1360   // Cache after the call
  1361   builder()->SetInsertPoint(call_completed);
  1362   cache_after_Java_call(call_method);
  1364   // Check for pending exceptions
  1365   check_pending_exception(EX_CHECK_FULL);
  1367   // Mark that a safepoint check has occurred
  1368   current_state()->set_has_safepointed(true);
  1371 bool SharkTopLevelBlock::static_subtype_check(ciKlass* check_klass,
  1372                                               ciKlass* object_klass) {
  1373   // If the class we're checking against is java.lang.Object
  1374   // then this is a no brainer.  Apparently this can happen
  1375   // in reflective code...
  1376   if (check_klass == java_lang_Object_klass())
  1377     return true;
  1379   // Perform a subtype check.  NB in opto's code for this
  1380   // (GraphKit::static_subtype_check) it says that static
  1381   // interface types cannot be trusted, and if opto can't
  1382   // trust them then I assume we can't either.
  1383   if (object_klass->is_loaded() && !object_klass->is_interface()) {
  1384     if (object_klass == check_klass)
  1385       return true;
  1387     if (check_klass->is_loaded() && object_klass->is_subtype_of(check_klass))
  1388       return true;
  1391   return false;
  1394 void SharkTopLevelBlock::do_instance_check() {
  1395   // Get the class we're checking against
  1396   bool will_link;
  1397   ciKlass *check_klass = iter()->get_klass(will_link);
  1399   // Get the class of the object we're checking
  1400   ciKlass *object_klass = xstack(0)->type()->as_klass();
  1402   // Can we optimize this check away?
  1403   if (static_subtype_check(check_klass, object_klass)) {
  1404     if (bc() == Bytecodes::_instanceof) {
  1405       pop();
  1406       push(SharkValue::jint_constant(1));
  1408     return;
  1411   // Need to check this one at runtime
  1412   if (will_link)
  1413     do_full_instance_check(check_klass);
  1414   else
  1415     do_trapping_instance_check(check_klass);
  1418 bool SharkTopLevelBlock::maybe_do_instanceof_if() {
  1419   // Get the class we're checking against
  1420   bool will_link;
  1421   ciKlass *check_klass = iter()->get_klass(will_link);
  1423   // If the class is unloaded then the instanceof
  1424   // cannot possibly succeed.
  1425   if (!will_link)
  1426     return false;
  1428   // Keep a copy of the object we're checking
  1429   SharkValue *old_object = xstack(0);
  1431   // Get the class of the object we're checking
  1432   ciKlass *object_klass = old_object->type()->as_klass();
  1434   // If the instanceof can be optimized away at compile time
  1435   // then any subsequent checkcasts will be too so we handle
  1436   // it normally.
  1437   if (static_subtype_check(check_klass, object_klass))
  1438     return false;
  1440   // Perform the instance check
  1441   do_full_instance_check(check_klass);
  1442   Value *result = pop()->jint_value();
  1444   // Create the casted object
  1445   SharkValue *new_object = SharkValue::create_generic(
  1446     check_klass, old_object->jobject_value(), old_object->zero_checked());
  1448   // Create two copies of the current state, one with the
  1449   // original object and one with all instances of the
  1450   // original object replaced with the new, casted object.
  1451   SharkState *new_state = current_state();
  1452   SharkState *old_state = new_state->copy();
  1453   new_state->replace_all(old_object, new_object);
  1455   // Perform the check-and-branch
  1456   switch (iter()->next_bc()) {
  1457   case Bytecodes::_ifeq:
  1458     // branch if not an instance
  1459     do_if_helper(
  1460       ICmpInst::ICMP_EQ,
  1461       LLVMValue::jint_constant(0), result,
  1462       old_state, new_state);
  1463     break;
  1465   case Bytecodes::_ifne:
  1466     // branch if an instance
  1467     do_if_helper(
  1468       ICmpInst::ICMP_NE,
  1469       LLVMValue::jint_constant(0), result,
  1470       new_state, old_state);
  1471     break;
  1473   default:
  1474     ShouldNotReachHere();
  1477   return true;
  1480 void SharkTopLevelBlock::do_full_instance_check(ciKlass* klass) {
  1481   BasicBlock *not_null      = function()->CreateBlock("not_null");
  1482   BasicBlock *subtype_check = function()->CreateBlock("subtype_check");
  1483   BasicBlock *is_instance   = function()->CreateBlock("is_instance");
  1484   BasicBlock *not_instance  = function()->CreateBlock("not_instance");
  1485   BasicBlock *merge1        = function()->CreateBlock("merge1");
  1486   BasicBlock *merge2        = function()->CreateBlock("merge2");
  1488   enum InstanceCheckStates {
  1489     IC_IS_NULL,
  1490     IC_IS_INSTANCE,
  1491     IC_NOT_INSTANCE,
  1492   };
  1494   // Pop the object off the stack
  1495   Value *object = pop()->jobject_value();
  1497   // Null objects aren't instances of anything
  1498   builder()->CreateCondBr(
  1499     builder()->CreateICmpEQ(object, LLVMValue::null()),
  1500     merge2, not_null);
  1501   BasicBlock *null_block = builder()->GetInsertBlock();
  1503   // Get the class we're checking against
  1504   builder()->SetInsertPoint(not_null);
  1505   Value *check_klass = builder()->CreateInlineMetadata(klass, SharkType::klass_type());
  1507   // Get the class of the object being tested
  1508   Value *object_klass = builder()->CreateValueOfStructEntry(
  1509     object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
  1510     SharkType::klass_type(),
  1511     "object_klass");
  1513   // Perform the check
  1514   builder()->CreateCondBr(
  1515     builder()->CreateICmpEQ(check_klass, object_klass),
  1516     is_instance, subtype_check);
  1518   builder()->SetInsertPoint(subtype_check);
  1519   builder()->CreateCondBr(
  1520     builder()->CreateICmpNE(
  1521       builder()->CreateCall2(
  1522         builder()->is_subtype_of(), check_klass, object_klass),
  1523       LLVMValue::jbyte_constant(0)),
  1524     is_instance, not_instance);
  1526   builder()->SetInsertPoint(is_instance);
  1527   builder()->CreateBr(merge1);
  1529   builder()->SetInsertPoint(not_instance);
  1530   builder()->CreateBr(merge1);
  1532   // First merge
  1533   builder()->SetInsertPoint(merge1);
  1534   PHINode *nonnull_result = builder()->CreatePHI(
  1535     SharkType::jint_type(), 0, "nonnull_result");
  1536   nonnull_result->addIncoming(
  1537     LLVMValue::jint_constant(IC_IS_INSTANCE), is_instance);
  1538   nonnull_result->addIncoming(
  1539     LLVMValue::jint_constant(IC_NOT_INSTANCE), not_instance);
  1540   BasicBlock *nonnull_block = builder()->GetInsertBlock();
  1541   builder()->CreateBr(merge2);
  1543   // Second merge
  1544   builder()->SetInsertPoint(merge2);
  1545   PHINode *result = builder()->CreatePHI(
  1546     SharkType::jint_type(), 0, "result");
  1547   result->addIncoming(LLVMValue::jint_constant(IC_IS_NULL), null_block);
  1548   result->addIncoming(nonnull_result, nonnull_block);
  1550   // Handle the result
  1551   if (bc() == Bytecodes::_checkcast) {
  1552     BasicBlock *failure = function()->CreateBlock("failure");
  1553     BasicBlock *success = function()->CreateBlock("success");
  1555     builder()->CreateCondBr(
  1556       builder()->CreateICmpNE(
  1557         result, LLVMValue::jint_constant(IC_NOT_INSTANCE)),
  1558       success, failure);
  1560     builder()->SetInsertPoint(failure);
  1561     SharkState *saved_state = current_state()->copy();
  1563     call_vm(
  1564       builder()->throw_ClassCastException(),
  1565       builder()->CreateIntToPtr(
  1566         LLVMValue::intptr_constant((intptr_t) __FILE__),
  1567         PointerType::getUnqual(SharkType::jbyte_type())),
  1568       LLVMValue::jint_constant(__LINE__),
  1569       EX_CHECK_NONE);
  1571     Value *pending_exception = get_pending_exception();
  1572     clear_pending_exception();
  1573     handle_exception(pending_exception, EX_CHECK_FULL);
  1575     set_current_state(saved_state);
  1576     builder()->SetInsertPoint(success);
  1577     push(SharkValue::create_generic(klass, object, false));
  1579   else {
  1580     push(
  1581       SharkValue::create_jint(
  1582         builder()->CreateIntCast(
  1583           builder()->CreateICmpEQ(
  1584             result, LLVMValue::jint_constant(IC_IS_INSTANCE)),
  1585           SharkType::jint_type(), false), false));
  1589 void SharkTopLevelBlock::do_trapping_instance_check(ciKlass* klass) {
  1590   BasicBlock *not_null = function()->CreateBlock("not_null");
  1591   BasicBlock *is_null  = function()->CreateBlock("null");
  1593   // Leave the object on the stack so it's there if we trap
  1594   builder()->CreateCondBr(
  1595     builder()->CreateICmpEQ(xstack(0)->jobject_value(), LLVMValue::null()),
  1596     is_null, not_null);
  1597   SharkState *saved_state = current_state()->copy();
  1599   // If it's not null then we need to trap
  1600   builder()->SetInsertPoint(not_null);
  1601   set_current_state(saved_state->copy());
  1602   do_trap(
  1603     Deoptimization::make_trap_request(
  1604       Deoptimization::Reason_uninitialized,
  1605       Deoptimization::Action_reinterpret));
  1607   // If it's null then we're ok
  1608   builder()->SetInsertPoint(is_null);
  1609   set_current_state(saved_state);
  1610   if (bc() == Bytecodes::_checkcast) {
  1611     push(SharkValue::create_generic(klass, pop()->jobject_value(), false));
  1613   else {
  1614     pop();
  1615     push(SharkValue::jint_constant(0));
  1619 void SharkTopLevelBlock::do_new() {
  1620   bool will_link;
  1621   ciInstanceKlass* klass = iter()->get_klass(will_link)->as_instance_klass();
  1622   assert(will_link, "typeflow responsibility");
  1624   BasicBlock *got_tlab            = NULL;
  1625   BasicBlock *heap_alloc          = NULL;
  1626   BasicBlock *retry               = NULL;
  1627   BasicBlock *got_heap            = NULL;
  1628   BasicBlock *initialize          = NULL;
  1629   BasicBlock *got_fast            = NULL;
  1630   BasicBlock *slow_alloc_and_init = NULL;
  1631   BasicBlock *got_slow            = NULL;
  1632   BasicBlock *push_object         = NULL;
  1634   SharkState *fast_state = NULL;
  1636   Value *tlab_object = NULL;
  1637   Value *heap_object = NULL;
  1638   Value *fast_object = NULL;
  1639   Value *slow_object = NULL;
  1640   Value *object      = NULL;
  1642   // The fast path
  1643   if (!Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
  1644     if (UseTLAB) {
  1645       got_tlab          = function()->CreateBlock("got_tlab");
  1646       heap_alloc        = function()->CreateBlock("heap_alloc");
  1648     retry               = function()->CreateBlock("retry");
  1649     got_heap            = function()->CreateBlock("got_heap");
  1650     initialize          = function()->CreateBlock("initialize");
  1651     slow_alloc_and_init = function()->CreateBlock("slow_alloc_and_init");
  1652     push_object         = function()->CreateBlock("push_object");
  1654     size_t size_in_bytes = klass->size_helper() << LogHeapWordSize;
  1656     // Thread local allocation
  1657     if (UseTLAB) {
  1658       Value *top_addr = builder()->CreateAddressOfStructEntry(
  1659         thread(), Thread::tlab_top_offset(),
  1660         PointerType::getUnqual(SharkType::intptr_type()),
  1661         "top_addr");
  1663       Value *end = builder()->CreateValueOfStructEntry(
  1664         thread(), Thread::tlab_end_offset(),
  1665         SharkType::intptr_type(),
  1666         "end");
  1668       Value *old_top = builder()->CreateLoad(top_addr, "old_top");
  1669       Value *new_top = builder()->CreateAdd(
  1670         old_top, LLVMValue::intptr_constant(size_in_bytes));
  1672       builder()->CreateCondBr(
  1673         builder()->CreateICmpULE(new_top, end),
  1674         got_tlab, heap_alloc);
  1676       builder()->SetInsertPoint(got_tlab);
  1677       tlab_object = builder()->CreateIntToPtr(
  1678         old_top, SharkType::oop_type(), "tlab_object");
  1680       builder()->CreateStore(new_top, top_addr);
  1681       builder()->CreateBr(initialize);
  1683       builder()->SetInsertPoint(heap_alloc);
  1686     // Heap allocation
  1687     Value *top_addr = builder()->CreateIntToPtr(
  1688         LLVMValue::intptr_constant((intptr_t) Universe::heap()->top_addr()),
  1689       PointerType::getUnqual(SharkType::intptr_type()),
  1690       "top_addr");
  1692     Value *end = builder()->CreateLoad(
  1693       builder()->CreateIntToPtr(
  1694         LLVMValue::intptr_constant((intptr_t) Universe::heap()->end_addr()),
  1695         PointerType::getUnqual(SharkType::intptr_type())),
  1696       "end");
  1698     builder()->CreateBr(retry);
  1699     builder()->SetInsertPoint(retry);
  1701     Value *old_top = builder()->CreateLoad(top_addr, "top");
  1702     Value *new_top = builder()->CreateAdd(
  1703       old_top, LLVMValue::intptr_constant(size_in_bytes));
  1705     builder()->CreateCondBr(
  1706       builder()->CreateICmpULE(new_top, end),
  1707       got_heap, slow_alloc_and_init);
  1709     builder()->SetInsertPoint(got_heap);
  1710     heap_object = builder()->CreateIntToPtr(
  1711       old_top, SharkType::oop_type(), "heap_object");
  1713     Value *check = builder()->CreateAtomicCmpXchg(top_addr, old_top, new_top, llvm::SequentiallyConsistent);
  1714     builder()->CreateCondBr(
  1715       builder()->CreateICmpEQ(old_top, check),
  1716       initialize, retry);
  1718     // Initialize the object
  1719     builder()->SetInsertPoint(initialize);
  1720     if (tlab_object) {
  1721       PHINode *phi = builder()->CreatePHI(
  1722         SharkType::oop_type(), 0, "fast_object");
  1723       phi->addIncoming(tlab_object, got_tlab);
  1724       phi->addIncoming(heap_object, got_heap);
  1725       fast_object = phi;
  1727     else {
  1728       fast_object = heap_object;
  1731     builder()->CreateMemset(
  1732       builder()->CreateBitCast(
  1733         fast_object, PointerType::getUnqual(SharkType::jbyte_type())),
  1734       LLVMValue::jbyte_constant(0),
  1735       LLVMValue::jint_constant(size_in_bytes),
  1736       LLVMValue::jint_constant(HeapWordSize));
  1738     Value *mark_addr = builder()->CreateAddressOfStructEntry(
  1739       fast_object, in_ByteSize(oopDesc::mark_offset_in_bytes()),
  1740       PointerType::getUnqual(SharkType::intptr_type()),
  1741       "mark_addr");
  1743     Value *klass_addr = builder()->CreateAddressOfStructEntry(
  1744       fast_object, in_ByteSize(oopDesc::klass_offset_in_bytes()),
  1745       PointerType::getUnqual(SharkType::klass_type()),
  1746       "klass_addr");
  1748     // Set the mark
  1749     intptr_t mark;
  1750     if (UseBiasedLocking) {
  1751       Unimplemented();
  1753     else {
  1754       mark = (intptr_t) markOopDesc::prototype();
  1756     builder()->CreateStore(LLVMValue::intptr_constant(mark), mark_addr);
  1758     // Set the class
  1759     Value *rtklass = builder()->CreateInlineMetadata(klass, SharkType::klass_type());
  1760     builder()->CreateStore(rtklass, klass_addr);
  1761     got_fast = builder()->GetInsertBlock();
  1763     builder()->CreateBr(push_object);
  1764     builder()->SetInsertPoint(slow_alloc_and_init);
  1765     fast_state = current_state()->copy();
  1768   // The slow path
  1769   call_vm(
  1770     builder()->new_instance(),
  1771     LLVMValue::jint_constant(iter()->get_klass_index()),
  1772     EX_CHECK_FULL);
  1773   slow_object = get_vm_result();
  1774   got_slow = builder()->GetInsertBlock();
  1776   // Push the object
  1777   if (push_object) {
  1778     builder()->CreateBr(push_object);
  1779     builder()->SetInsertPoint(push_object);
  1781   if (fast_object) {
  1782     PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "object");
  1783     phi->addIncoming(fast_object, got_fast);
  1784     phi->addIncoming(slow_object, got_slow);
  1785     object = phi;
  1786     current_state()->merge(fast_state, got_fast, got_slow);
  1788   else {
  1789     object = slow_object;
  1792   push(SharkValue::create_jobject(object, true));
  1795 void SharkTopLevelBlock::do_newarray() {
  1796   BasicType type = (BasicType) iter()->get_index();
  1798   call_vm(
  1799     builder()->newarray(),
  1800     LLVMValue::jint_constant(type),
  1801     pop()->jint_value(),
  1802     EX_CHECK_FULL);
  1804   ciArrayKlass *array_klass = ciArrayKlass::make(ciType::make(type));
  1805   push(SharkValue::create_generic(array_klass, get_vm_result(), true));
  1808 void SharkTopLevelBlock::do_anewarray() {
  1809   bool will_link;
  1810   ciKlass *klass = iter()->get_klass(will_link);
  1811   assert(will_link, "typeflow responsibility");
  1813   ciObjArrayKlass *array_klass = ciObjArrayKlass::make(klass);
  1814   if (!array_klass->is_loaded()) {
  1815     Unimplemented();
  1818   call_vm(
  1819     builder()->anewarray(),
  1820     LLVMValue::jint_constant(iter()->get_klass_index()),
  1821     pop()->jint_value(),
  1822     EX_CHECK_FULL);
  1824   push(SharkValue::create_generic(array_klass, get_vm_result(), true));
  1827 void SharkTopLevelBlock::do_multianewarray() {
  1828   bool will_link;
  1829   ciArrayKlass *array_klass = iter()->get_klass(will_link)->as_array_klass();
  1830   assert(will_link, "typeflow responsibility");
  1832   // The dimensions are stack values, so we use their slots for the
  1833   // dimensions array.  Note that we are storing them in the reverse
  1834   // of normal stack order.
  1835   int ndims = iter()->get_dimensions();
  1837   Value *dimensions = stack()->slot_addr(
  1838     stack()->stack_slots_offset() + max_stack() - xstack_depth(),
  1839     ArrayType::get(SharkType::jint_type(), ndims),
  1840     "dimensions");
  1842   for (int i = 0; i < ndims; i++) {
  1843     builder()->CreateStore(
  1844       xstack(ndims - 1 - i)->jint_value(),
  1845       builder()->CreateStructGEP(dimensions, i));
  1848   call_vm(
  1849     builder()->multianewarray(),
  1850     LLVMValue::jint_constant(iter()->get_klass_index()),
  1851     LLVMValue::jint_constant(ndims),
  1852     builder()->CreateStructGEP(dimensions, 0),
  1853     EX_CHECK_FULL);
  1855   // Now we can pop the dimensions off the stack
  1856   for (int i = 0; i < ndims; i++)
  1857     pop();
  1859   push(SharkValue::create_generic(array_klass, get_vm_result(), true));
  1862 void SharkTopLevelBlock::acquire_method_lock() {
  1863   Value *lockee;
  1864   if (target()->is_static()) {
  1865     lockee = builder()->CreateInlineOop(target()->holder()->java_mirror());
  1867   else
  1868     lockee = local(0)->jobject_value();
  1870   iter()->force_bci(start()); // for the decache in acquire_lock
  1871   acquire_lock(lockee, EX_CHECK_NO_CATCH);
  1874 void SharkTopLevelBlock::do_monitorenter() {
  1875   SharkValue *lockee = pop();
  1876   check_null(lockee);
  1877   acquire_lock(lockee->jobject_value(), EX_CHECK_FULL);
  1880 void SharkTopLevelBlock::do_monitorexit() {
  1881   pop(); // don't need this (monitors are block structured)
  1882   release_lock(EX_CHECK_NO_CATCH);
  1885 void SharkTopLevelBlock::acquire_lock(Value *lockee, int exception_action) {
  1886   BasicBlock *try_recursive = function()->CreateBlock("try_recursive");
  1887   BasicBlock *got_recursive = function()->CreateBlock("got_recursive");
  1888   BasicBlock *not_recursive = function()->CreateBlock("not_recursive");
  1889   BasicBlock *acquired_fast = function()->CreateBlock("acquired_fast");
  1890   BasicBlock *lock_acquired = function()->CreateBlock("lock_acquired");
  1892   int monitor = num_monitors();
  1893   Value *monitor_addr        = stack()->monitor_addr(monitor);
  1894   Value *monitor_object_addr = stack()->monitor_object_addr(monitor);
  1895   Value *monitor_header_addr = stack()->monitor_header_addr(monitor);
  1897   // Store the object and mark the slot as live
  1898   builder()->CreateStore(lockee, monitor_object_addr);
  1899   set_num_monitors(monitor + 1);
  1901   // Try a simple lock
  1902   Value *mark_addr = builder()->CreateAddressOfStructEntry(
  1903     lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
  1904     PointerType::getUnqual(SharkType::intptr_type()),
  1905     "mark_addr");
  1907   Value *mark = builder()->CreateLoad(mark_addr, "mark");
  1908   Value *disp = builder()->CreateOr(
  1909     mark, LLVMValue::intptr_constant(markOopDesc::unlocked_value), "disp");
  1910   builder()->CreateStore(disp, monitor_header_addr);
  1912   Value *lock = builder()->CreatePtrToInt(
  1913     monitor_header_addr, SharkType::intptr_type());
  1914   Value *check = builder()->CreateAtomicCmpXchg(mark_addr, disp, lock, llvm::Acquire);
  1915   builder()->CreateCondBr(
  1916     builder()->CreateICmpEQ(disp, check),
  1917     acquired_fast, try_recursive);
  1919   // Locking failed, but maybe this thread already owns it
  1920   builder()->SetInsertPoint(try_recursive);
  1921   Value *addr = builder()->CreateAnd(
  1922     disp,
  1923     LLVMValue::intptr_constant(~markOopDesc::lock_mask_in_place));
  1925   // NB we use the entire stack, but JavaThread::is_lock_owned()
  1926   // uses a more limited range.  I don't think it hurts though...
  1927   Value *stack_limit = builder()->CreateValueOfStructEntry(
  1928     thread(), Thread::stack_base_offset(),
  1929     SharkType::intptr_type(),
  1930     "stack_limit");
  1932   assert(sizeof(size_t) == sizeof(intptr_t), "should be");
  1933   Value *stack_size = builder()->CreateValueOfStructEntry(
  1934     thread(), Thread::stack_size_offset(),
  1935     SharkType::intptr_type(),
  1936     "stack_size");
  1938   Value *stack_start =
  1939     builder()->CreateSub(stack_limit, stack_size, "stack_start");
  1941   builder()->CreateCondBr(
  1942     builder()->CreateAnd(
  1943       builder()->CreateICmpUGE(addr, stack_start),
  1944       builder()->CreateICmpULT(addr, stack_limit)),
  1945     got_recursive, not_recursive);
  1947   builder()->SetInsertPoint(got_recursive);
  1948   builder()->CreateStore(LLVMValue::intptr_constant(0), monitor_header_addr);
  1949   builder()->CreateBr(acquired_fast);
  1951   // Create an edge for the state merge
  1952   builder()->SetInsertPoint(acquired_fast);
  1953   SharkState *fast_state = current_state()->copy();
  1954   builder()->CreateBr(lock_acquired);
  1956   // It's not a recursive case so we need to drop into the runtime
  1957   builder()->SetInsertPoint(not_recursive);
  1958   call_vm(
  1959     builder()->monitorenter(), monitor_addr,
  1960     exception_action | EAM_MONITOR_FUDGE);
  1961   BasicBlock *acquired_slow = builder()->GetInsertBlock();
  1962   builder()->CreateBr(lock_acquired);
  1964   // All done
  1965   builder()->SetInsertPoint(lock_acquired);
  1966   current_state()->merge(fast_state, acquired_fast, acquired_slow);
  1969 void SharkTopLevelBlock::release_lock(int exception_action) {
  1970   BasicBlock *not_recursive = function()->CreateBlock("not_recursive");
  1971   BasicBlock *released_fast = function()->CreateBlock("released_fast");
  1972   BasicBlock *slow_path     = function()->CreateBlock("slow_path");
  1973   BasicBlock *lock_released = function()->CreateBlock("lock_released");
  1975   int monitor = num_monitors() - 1;
  1976   Value *monitor_addr        = stack()->monitor_addr(monitor);
  1977   Value *monitor_object_addr = stack()->monitor_object_addr(monitor);
  1978   Value *monitor_header_addr = stack()->monitor_header_addr(monitor);
  1980   // If it is recursive then we're already done
  1981   Value *disp = builder()->CreateLoad(monitor_header_addr);
  1982   builder()->CreateCondBr(
  1983     builder()->CreateICmpEQ(disp, LLVMValue::intptr_constant(0)),
  1984     released_fast, not_recursive);
  1986   // Try a simple unlock
  1987   builder()->SetInsertPoint(not_recursive);
  1989   Value *lock = builder()->CreatePtrToInt(
  1990     monitor_header_addr, SharkType::intptr_type());
  1992   Value *lockee = builder()->CreateLoad(monitor_object_addr);
  1994   Value *mark_addr = builder()->CreateAddressOfStructEntry(
  1995     lockee, in_ByteSize(oopDesc::mark_offset_in_bytes()),
  1996     PointerType::getUnqual(SharkType::intptr_type()),
  1997     "mark_addr");
  1999   Value *check = builder()->CreateAtomicCmpXchg(mark_addr, lock, disp, llvm::Release);
  2000   builder()->CreateCondBr(
  2001     builder()->CreateICmpEQ(lock, check),
  2002     released_fast, slow_path);
  2004   // Create an edge for the state merge
  2005   builder()->SetInsertPoint(released_fast);
  2006   SharkState *fast_state = current_state()->copy();
  2007   builder()->CreateBr(lock_released);
  2009   // Need to drop into the runtime to release this one
  2010   builder()->SetInsertPoint(slow_path);
  2011   call_vm(builder()->monitorexit(), monitor_addr, exception_action);
  2012   BasicBlock *released_slow = builder()->GetInsertBlock();
  2013   builder()->CreateBr(lock_released);
  2015   // All done
  2016   builder()->SetInsertPoint(lock_released);
  2017   current_state()->merge(fast_state, released_fast, released_slow);
  2019   // The object slot is now dead
  2020   set_num_monitors(monitor);

mercurial