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