compiledIC.cpp revision 6412:53a41e7cbe05
1178479Sjb/*
2178479Sjb * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3178479Sjb * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4178479Sjb *
5178479Sjb * This code is free software; you can redistribute it and/or modify it
6178479Sjb * under the terms of the GNU General Public License version 2 only, as
7178479Sjb * published by the Free Software Foundation.
8178479Sjb *
9178479Sjb * This code is distributed in the hope that it will be useful, but WITHOUT
10178479Sjb * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11178479Sjb * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12178479Sjb * version 2 for more details (a copy is included in the LICENSE file that
13178479Sjb * accompanied this code).
14178479Sjb *
15178479Sjb * You should have received a copy of the GNU General Public License version
16178479Sjb * 2 along with this work; if not, write to the Free Software Foundation,
17178479Sjb * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18178479Sjb *
19178479Sjb * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20178479Sjb * or visit www.oracle.com if you need additional information or have any
21178479Sjb * questions.
22178479Sjb *
23210767Srpaulo */
24238558Spfg
25253725Spfg#include "precompiled.hpp"
26178479Sjb#include "classfile/systemDictionary.hpp"
27178479Sjb#include "code/codeCache.hpp"
28178479Sjb#include "code/compiledIC.hpp"
29178479Sjb#include "code/icBuffer.hpp"
30178479Sjb#include "code/nmethod.hpp"
31178479Sjb#include "code/vtableStubs.hpp"
32178479Sjb#include "interpreter/interpreter.hpp"
33178479Sjb#include "interpreter/linkResolver.hpp"
34178479Sjb#include "memory/metadataFactory.hpp"
35178479Sjb#include "memory/oopFactory.hpp"
36178479Sjb#include "oops/method.hpp"
37178479Sjb#include "oops/oop.inline.hpp"
38178479Sjb#include "oops/symbol.hpp"
39178479Sjb#include "runtime/icache.hpp"
40178479Sjb#include "runtime/sharedRuntime.hpp"
41178479Sjb#include "runtime/stubRoutines.hpp"
42178479Sjb#include "utilities/events.hpp"
43178479Sjb
44178479Sjb
45178479Sjb// Every time a compiled IC is changed or its type is being accessed,
46178479Sjb// either the CompiledIC_lock must be set or we must be at a safe point.
47178479Sjb
48178479Sjb//-----------------------------------------------------------------------------
49178479Sjb// Low-level access to an inline cache. Private, since they might not be
50178479Sjb// MT-safe to use.
51178479Sjb
52178479Sjbvoid* CompiledIC::cached_value() const {
53178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
54178479Sjb  assert (!is_optimized(), "an optimized virtual call does not have a cached metadata");
55178479Sjb
56178479Sjb  if (!is_in_transition_state()) {
57178479Sjb    void* data = (void*)_value->data();
58178479Sjb    // If we let the metadata value here be initialized to zero...
59178479Sjb    assert(data != NULL || Universe::non_oop_word() == NULL,
60178479Sjb           "no raw nulls in CompiledIC metadatas, because of patching races");
61178479Sjb    return (data == (void*)Universe::non_oop_word()) ? NULL : data;
62178479Sjb  } else {
63178479Sjb    return InlineCacheBuffer::cached_value_for((CompiledIC *)this);
64178479Sjb  }
65178479Sjb}
66178479Sjb
67178479Sjb
68178479Sjbvoid CompiledIC::internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder) {
69178479Sjb  assert(entry_point != NULL, "must set legal entry point");
70178479Sjb  assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
71178479Sjb  assert (!is_optimized() || cache == NULL, "an optimized virtual call does not have a cached metadata");
72178479Sjb  assert (cache == NULL || cache != (Metadata*)badOopVal, "invalid metadata");
73178479Sjb
74178479Sjb  assert(!is_icholder || is_icholder_entry(entry_point), "must be");
75178479Sjb
76178479Sjb  // Don't use ic_destination for this test since that forwards
77178479Sjb  // through ICBuffer instead of returning the actual current state of
78178479Sjb  // the CompiledIC.
79178479Sjb  if (is_icholder_entry(_ic_call->destination())) {
80178479Sjb    // When patching for the ICStub case the cached value isn't
81178479Sjb    // overwritten until the ICStub copied into the CompiledIC during
82178479Sjb    // the next safepoint.  Make sure that the CompiledICHolder* is
83178479Sjb    // marked for release at this point since it won't be identifiable
84178479Sjb    // once the entry point is overwritten.
85178479Sjb    InlineCacheBuffer::queue_for_release((CompiledICHolder*)_value->data());
86178479Sjb  }
87237624Spfg
88178479Sjb  if (TraceCompiledIC) {
89178479Sjb    tty->print("  ");
90178576Sjb    print_compiled_ic();
91178479Sjb    tty->print(" changing destination to " INTPTR_FORMAT, p2i(entry_point));
92178479Sjb    if (!is_optimized()) {
93178479Sjb      tty->print(" changing cached %s to " INTPTR_FORMAT, is_icholder ? "icholder" : "metadata", p2i((address)cache));
94178479Sjb    }
95178479Sjb    if (is_icstub) {
96178479Sjb      tty->print(" (icstub)");
97178479Sjb    }
98178479Sjb    tty->cr();
99178479Sjb  }
100178479Sjb
101178479Sjb  {
102178479Sjb  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
103178479Sjb#ifdef ASSERT
104178479Sjb  CodeBlob* cb = CodeCache::find_blob_unsafe(_ic_call);
105178479Sjb  assert(cb != NULL && cb->is_nmethod(), "must be nmethod");
106178479Sjb#endif
107178479Sjb  _ic_call->set_destination_mt_safe(entry_point);
108178479Sjb}
109178479Sjb
110178479Sjb  if (is_optimized() || is_icstub) {
111178479Sjb    // Optimized call sites don't have a cache value and ICStub call
112178479Sjb    // sites only change the entry point.  Changing the value in that
113178479Sjb    // case could lead to MT safety issues.
114178479Sjb    assert(cache == NULL, "must be null");
115178479Sjb    return;
116178479Sjb  }
117178479Sjb
118178479Sjb  if (cache == NULL)  cache = (void*)Universe::non_oop_word();
119178479Sjb
120178479Sjb  _value->set_data((intptr_t)cache);
121178479Sjb}
122178479Sjb
123178479Sjb
124178479Sjbvoid CompiledIC::set_ic_destination(ICStub* stub) {
125178479Sjb  internal_set_ic_destination(stub->code_begin(), true, NULL, false);
126178479Sjb}
127178479Sjb
128178479Sjb
129178479Sjb
130178479Sjbaddress CompiledIC::ic_destination() const {
131178479Sjb assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
132178479Sjb if (!is_in_transition_state()) {
133178479Sjb   return _ic_call->destination();
134178479Sjb } else {
135178479Sjb   return InlineCacheBuffer::ic_destination_for((CompiledIC *)this);
136178479Sjb }
137178479Sjb}
138178479Sjb
139178479Sjb
140178479Sjbbool CompiledIC::is_in_transition_state() const {
141178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
142178479Sjb  return InlineCacheBuffer::contains(_ic_call->destination());
143178479Sjb}
144178479Sjb
145178479Sjb
146178479Sjbbool CompiledIC::is_icholder_call() const {
147178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
148178479Sjb  return !_is_optimized && is_icholder_entry(ic_destination());
149178479Sjb}
150178479Sjb
151178479Sjb// Returns native address of 'call' instruction in inline-cache. Used by
152178479Sjb// the InlineCacheBuffer when it needs to find the stub.
153178479Sjbaddress CompiledIC::stub_address() const {
154178479Sjb  assert(is_in_transition_state(), "should only be called when we are in a transition state");
155178479Sjb  return _ic_call->destination();
156178479Sjb}
157178479Sjb
158178479Sjb
159178479Sjb//-----------------------------------------------------------------------------
160178479Sjb// High-level access to an inline cache. Guaranteed to be MT-safe.
161178479Sjb
162178479Sjb
163178479Sjbbool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS) {
164178479Sjb  assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
165178479Sjb  assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic");
166178479Sjb  assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?");
167178479Sjb
168178479Sjb  address entry;
169178479Sjb  if (call_info->call_kind() == CallInfo::itable_call) {
170178479Sjb    assert(bytecode == Bytecodes::_invokeinterface, "");
171178479Sjb    int itable_index = call_info->itable_index();
172178479Sjb    entry = VtableStubs::find_itable_stub(itable_index);
173178479Sjb    if (entry == false) {
174178479Sjb      return false;
175178479Sjb    }
176178479Sjb#ifdef ASSERT
177178479Sjb    int index = call_info->resolved_method()->itable_index();
178178479Sjb    assert(index == itable_index, "CallInfo pre-computes this");
179178479Sjb#endif //ASSERT
180178479Sjb    InstanceKlass* k = call_info->resolved_method()->method_holder();
181178479Sjb    assert(k->verify_itable_index(itable_index), "sanity check");
182178479Sjb    InlineCacheBuffer::create_transition_stub(this, k, entry);
183178479Sjb  } else {
184178479Sjb    assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable");
185178479Sjb    // Can be different than selected_method->vtable_index(), due to package-private etc.
186178479Sjb    int vtable_index = call_info->vtable_index();
187178479Sjb    assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
188178479Sjb    entry = VtableStubs::find_vtable_stub(vtable_index);
189178479Sjb    if (entry == NULL) {
190178479Sjb      return false;
191178479Sjb    }
192178479Sjb    InlineCacheBuffer::create_transition_stub(this, NULL, entry);
193178479Sjb  }
194178479Sjb
195178479Sjb  if (TraceICs) {
196178479Sjb    ResourceMark rm;
197178479Sjb    tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
198178479Sjb                   p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
199178479Sjb  }
200178479Sjb
201178479Sjb  // We can't check this anymore. With lazy deopt we could have already
202178479Sjb  // cleaned this IC entry before we even return. This is possible if
203178479Sjb  // we ran out of space in the inline cache buffer trying to do the
204178479Sjb  // set_next and we safepointed to free up space. This is a benign
205178479Sjb  // race because the IC entry was complete when we safepointed so
206178479Sjb  // cleaning it immediately is harmless.
207178479Sjb  // assert(is_megamorphic(), "sanity check");
208178479Sjb  return true;
209178479Sjb}
210178479Sjb
211178479Sjb
212178479Sjb// true if destination is megamorphic stub
213178479Sjbbool CompiledIC::is_megamorphic() const {
214178479Sjb  assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
215178479Sjb  assert(!is_optimized(), "an optimized call cannot be megamorphic");
216178479Sjb
217178479Sjb  // Cannot rely on cached_value. It is either an interface or a method.
218178479Sjb  return VtableStubs::is_entry_point(ic_destination());
219178479Sjb}
220178479Sjb
221178479Sjbbool CompiledIC::is_call_to_compiled() const {
222178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
223178479Sjb
224178479Sjb  // Use unsafe, since an inline cache might point to a zombie method. However, the zombie
225178479Sjb  // method is guaranteed to still exist, since we only remove methods after all inline caches
226178479Sjb  // has been cleaned up
227178479Sjb  CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
228178479Sjb  bool is_monomorphic = (cb != NULL && cb->is_nmethod());
229178479Sjb  // Check that the cached_value is a klass for non-optimized monomorphic calls
230178479Sjb  // This assertion is invalid for compiler1: a call that does not look optimized (no static stub) can be used
231178479Sjb  // for calling directly to vep without using the inline cache (i.e., cached_value == NULL)
232178479Sjb#ifdef ASSERT
233178479Sjb  CodeBlob* caller = CodeCache::find_blob_unsafe(instruction_address());
234178479Sjb  bool is_c1_method = caller->is_compiled_by_c1();
235178479Sjb  assert( is_c1_method ||
236178479Sjb         !is_monomorphic ||
237178479Sjb         is_optimized() ||
238178479Sjb         (cached_metadata() != NULL && cached_metadata()->is_klass()), "sanity check");
239178479Sjb#endif // ASSERT
240178479Sjb  return is_monomorphic;
241178479Sjb}
242178479Sjb
243178479Sjb
244178479Sjbbool CompiledIC::is_call_to_interpreted() const {
245178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
246178479Sjb  // Call to interpreter if destination is either calling to a stub (if it
247178479Sjb  // is optimized), or calling to an I2C blob
248178479Sjb  bool is_call_to_interpreted = false;
249178479Sjb  if (!is_optimized()) {
250178479Sjb    // must use unsafe because the destination can be a zombie (and we're cleaning)
251178479Sjb    // and the print_compiled_ic code wants to know if site (in the non-zombie)
252178479Sjb    // is to the interpreter.
253178479Sjb    CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
254178479Sjb    is_call_to_interpreted = (cb != NULL && cb->is_adapter_blob());
255178479Sjb    assert(!is_call_to_interpreted || (is_icholder_call() && cached_icholder() != NULL), "sanity check");
256178479Sjb  } else {
257178479Sjb    // Check if we are calling into our own codeblob (i.e., to a stub)
258178479Sjb    CodeBlob* cb = CodeCache::find_blob(_ic_call->instruction_address());
259178479Sjb    address dest = ic_destination();
260178479Sjb#ifdef ASSERT
261178479Sjb    {
262178479Sjb      CodeBlob* db = CodeCache::find_blob_unsafe(dest);
263178479Sjb      assert(!db->is_adapter_blob(), "must use stub!");
264178479Sjb    }
265178479Sjb#endif /* ASSERT */
266178479Sjb    is_call_to_interpreted = cb->contains(dest);
267178479Sjb  }
268178479Sjb  return is_call_to_interpreted;
269178479Sjb}
270178479Sjb
271178479Sjb
272178479Sjbvoid CompiledIC::set_to_clean() {
273178479Sjb  assert(SafepointSynchronize::is_at_safepoint() || CompiledIC_lock->is_locked() , "MT-unsafe call");
274178479Sjb  if (TraceInlineCacheClearing || TraceICs) {
275178479Sjb    tty->print_cr("IC@" INTPTR_FORMAT ": set to clean", p2i(instruction_address()));
276178479Sjb    print();
277178479Sjb  }
278178479Sjb
279178479Sjb  address entry;
280178479Sjb  if (is_optimized()) {
281178479Sjb    entry = SharedRuntime::get_resolve_opt_virtual_call_stub();
282178479Sjb  } else {
283178479Sjb    entry = SharedRuntime::get_resolve_virtual_call_stub();
284178479Sjb  }
285178479Sjb
286178479Sjb  // A zombie transition will always be safe, since the metadata has already been set to NULL, so
287178479Sjb  // we only need to patch the destination
288178479Sjb  bool safe_transition = is_optimized() || SafepointSynchronize::is_at_safepoint();
289178479Sjb
290178479Sjb  if (safe_transition) {
291178479Sjb    // Kill any leftover stub we might have too
292178479Sjb    if (is_in_transition_state()) {
293178479Sjb      ICStub* old_stub = ICStub_from_destination_address(stub_address());
294178479Sjb      old_stub->clear();
295178479Sjb    }
296178479Sjb    if (is_optimized()) {
297178479Sjb    set_ic_destination(entry);
298178479Sjb  } else {
299178479Sjb      set_ic_destination_and_value(entry, (void*)NULL);
300178479Sjb    }
301178479Sjb  } else {
302178479Sjb    // Unsafe transition - create stub.
303178479Sjb    InlineCacheBuffer::create_transition_stub(this, NULL, entry);
304178479Sjb  }
305178479Sjb  // We can't check this anymore. With lazy deopt we could have already
306178479Sjb  // cleaned this IC entry before we even return. This is possible if
307178479Sjb  // we ran out of space in the inline cache buffer trying to do the
308178479Sjb  // set_next and we safepointed to free up space. This is a benign
309178479Sjb  // race because the IC entry was complete when we safepointed so
310178479Sjb  // cleaning it immediately is harmless.
311178479Sjb  // assert(is_clean(), "sanity check");
312178479Sjb}
313178479Sjb
314178479Sjb
315178479Sjbbool CompiledIC::is_clean() const {
316178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
317178479Sjb  bool is_clean = false;
318178479Sjb  address dest = ic_destination();
319178479Sjb  is_clean = dest == SharedRuntime::get_resolve_opt_virtual_call_stub() ||
320178479Sjb             dest == SharedRuntime::get_resolve_virtual_call_stub();
321178479Sjb  assert(!is_clean || is_optimized() || cached_value() == NULL, "sanity check");
322178479Sjb  return is_clean;
323178479Sjb}
324178479Sjb
325178479Sjb
326178479Sjbvoid CompiledIC::set_to_monomorphic(CompiledICInfo& info) {
327178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
328178479Sjb  // Updating a cache to the wrong entry can cause bugs that are very hard
329178479Sjb  // to track down - if cache entry gets invalid - we just clean it. In
330178479Sjb  // this way it is always the same code path that is responsible for
331178479Sjb  // updating and resolving an inline cache
332178479Sjb  //
333178479Sjb  // The above is no longer true. SharedRuntime::fixup_callers_callsite will change optimized
334178479Sjb  // callsites. In addition ic_miss code will update a site to monomorphic if it determines
335178479Sjb  // that an monomorphic call to the interpreter can now be monomorphic to compiled code.
336178479Sjb  //
337178479Sjb  // In both of these cases the only thing being modifed is the jump/call target and these
338178479Sjb  // transitions are mt_safe
339178479Sjb
340178479Sjb  Thread *thread = Thread::current();
341178479Sjb  if (info.to_interpreter()) {
342178479Sjb    // Call to interpreter
343178479Sjb    if (info.is_optimized() && is_optimized()) {
344178479Sjb       assert(is_clean(), "unsafe IC path");
345178479Sjb       MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
346178479Sjb      // the call analysis (callee structure) specifies that the call is optimized
347178479Sjb      // (either because of CHA or the static target is final)
348178479Sjb      // At code generation time, this call has been emitted as static call
349178479Sjb      // Call via stub
350178479Sjb      assert(info.cached_metadata() != NULL && info.cached_metadata()->is_method(), "sanity check");
351178479Sjb      CompiledStaticCall* csc = compiledStaticCall_at(instruction_address());
352178479Sjb      methodHandle method (thread, (Method*)info.cached_metadata());
353178479Sjb      csc->set_to_interpreted(method, info.entry());
354178479Sjb      if (TraceICs) {
355178479Sjb         ResourceMark rm(thread);
356178479Sjb         tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter: %s",
357178479Sjb           p2i(instruction_address()),
358178479Sjb           method->print_value_string());
359178479Sjb      }
360178479Sjb    } else {
361178479Sjb      // Call via method-klass-holder
362178479Sjb      InlineCacheBuffer::create_transition_stub(this, info.claim_cached_icholder(), info.entry());
363178479Sjb      if (TraceICs) {
364178479Sjb         ResourceMark rm(thread);
365178479Sjb         tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter via icholder ", p2i(instruction_address()));
366178479Sjb      }
367178479Sjb    }
368178479Sjb  } else {
369178479Sjb    // Call to compiled code
370178479Sjb    bool static_bound = info.is_optimized() || (info.cached_metadata() == NULL);
371178479Sjb#ifdef ASSERT
372178479Sjb    CodeBlob* cb = CodeCache::find_blob_unsafe(info.entry());
373178479Sjb    assert (cb->is_nmethod(), "must be compiled!");
374178479Sjb#endif /* ASSERT */
375178479Sjb
376178479Sjb    // This is MT safe if we come from a clean-cache and go through a
377178479Sjb    // non-verified entry point
378178479Sjb    bool safe = SafepointSynchronize::is_at_safepoint() ||
379178479Sjb                (!is_in_transition_state() && (info.is_optimized() || static_bound || is_clean()));
380178479Sjb
381178479Sjb    if (!safe) {
382178479Sjb      InlineCacheBuffer::create_transition_stub(this, info.cached_metadata(), info.entry());
383178479Sjb    } else {
384178479Sjb      if (is_optimized()) {
385178479Sjb      set_ic_destination(info.entry());
386178479Sjb      } else {
387178479Sjb        set_ic_destination_and_value(info.entry(), info.cached_metadata());
388178479Sjb      }
389178479Sjb    }
390178479Sjb
391178479Sjb    if (TraceICs) {
392178479Sjb      ResourceMark rm(thread);
393178479Sjb      assert(info.cached_metadata() == NULL || info.cached_metadata()->is_klass(), "must be");
394178479Sjb      tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to compiled (rcvr klass) %s: %s",
395178479Sjb        p2i(instruction_address()),
396178479Sjb        ((Klass*)info.cached_metadata())->print_value_string(),
397178479Sjb        (safe) ? "" : "via stub");
398178479Sjb    }
399178479Sjb  }
400178479Sjb  // We can't check this anymore. With lazy deopt we could have already
401178479Sjb  // cleaned this IC entry before we even return. This is possible if
402178479Sjb  // we ran out of space in the inline cache buffer trying to do the
403178479Sjb  // set_next and we safepointed to free up space. This is a benign
404178479Sjb  // race because the IC entry was complete when we safepointed so
405178479Sjb  // cleaning it immediately is harmless.
406178479Sjb  // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
407178479Sjb}
408178479Sjb
409178479Sjb
410178479Sjb// is_optimized: Compiler has generated an optimized call (i.e., no inline
411178479Sjb// cache) static_bound: The call can be static bound (i.e, no need to use
412178479Sjb// inline cache)
413178479Sjbvoid CompiledIC::compute_monomorphic_entry(methodHandle method,
414178479Sjb                                           KlassHandle receiver_klass,
415178479Sjb                                           bool is_optimized,
416178479Sjb                                           bool static_bound,
417178479Sjb                                           CompiledICInfo& info,
418178479Sjb                                           TRAPS) {
419178479Sjb  nmethod* method_code = method->code();
420178479Sjb  address entry = NULL;
421178479Sjb  if (method_code != NULL && method_code->is_in_use()) {
422178479Sjb    // Call to compiled code
423178479Sjb    if (static_bound || is_optimized) {
424178479Sjb      entry      = method_code->verified_entry_point();
425178479Sjb    } else {
426178479Sjb      entry      = method_code->entry_point();
427178479Sjb    }
428178479Sjb  }
429178479Sjb  if (entry != NULL) {
430178479Sjb    // Call to compiled code
431178479Sjb    info.set_compiled_entry(entry, (static_bound || is_optimized) ? NULL : receiver_klass(), is_optimized);
432178479Sjb  } else {
433178479Sjb    // Note: the following problem exists with Compiler1:
434178479Sjb    //   - at compile time we may or may not know if the destination is final
435178479Sjb    //   - if we know that the destination is final, we will emit an optimized
436178479Sjb    //     virtual call (no inline cache), and need a Method* to make a call
437178479Sjb    //     to the interpreter
438178479Sjb    //   - if we do not know if the destination is final, we emit a standard
439178479Sjb    //     virtual call, and use CompiledICHolder to call interpreted code
440178479Sjb    //     (no static call stub has been generated)
441178479Sjb    //     However in that case we will now notice it is static_bound
442178479Sjb    //     and convert the call into what looks to be an optimized
443178479Sjb    //     virtual call. This causes problems in verifying the IC because
444178479Sjb    //     it look vanilla but is optimized. Code in is_call_to_interpreted
445178479Sjb    //     is aware of this and weakens its asserts.
446178479Sjb
447178479Sjb    // static_bound should imply is_optimized -- otherwise we have a
448178479Sjb    // performance bug (statically-bindable method is called via
449178479Sjb    // dynamically-dispatched call note: the reverse implication isn't
450178479Sjb    // necessarily true -- the call may have been optimized based on compiler
451178479Sjb    // analysis (static_bound is only based on "final" etc.)
452178479Sjb#ifdef COMPILER2
453178479Sjb#ifdef TIERED
454178479Sjb#if defined(ASSERT)
455178479Sjb    // can't check the assert because we don't have the CompiledIC with which to
456178479Sjb    // find the address if the call instruction.
457178479Sjb    //
458178479Sjb    // CodeBlob* cb = find_blob_unsafe(instruction_address());
459178479Sjb    // assert(cb->is_compiled_by_c1() || !static_bound || is_optimized, "static_bound should imply is_optimized");
460178479Sjb#endif // ASSERT
461178479Sjb#else
462178479Sjb    assert(!static_bound || is_optimized, "static_bound should imply is_optimized");
463178479Sjb#endif // TIERED
464178479Sjb#endif // COMPILER2
465178479Sjb    if (is_optimized) {
466178479Sjb      // Use stub entry
467178479Sjb      info.set_interpreter_entry(method()->get_c2i_entry(), method());
468178479Sjb    } else {
469178479Sjb      // Use icholder entry
470178479Sjb      CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass());
471178479Sjb      info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder);
472178479Sjb    }
473178479Sjb  }
474178479Sjb  assert(info.is_optimized() == is_optimized, "must agree");
475178479Sjb}
476178479Sjb
477178479Sjb
478178479Sjbbool CompiledIC::is_icholder_entry(address entry) {
479178479Sjb  CodeBlob* cb = CodeCache::find_blob_unsafe(entry);
480178479Sjb  return (cb != NULL && cb->is_adapter_blob());
481178479Sjb}
482178479Sjb
483178479Sjb// ----------------------------------------------------------------------------
484178479Sjb
485178479Sjbvoid CompiledStaticCall::set_to_clean() {
486178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
487178479Sjb  // Reset call site
488178479Sjb  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
489178479Sjb#ifdef ASSERT
490178479Sjb  CodeBlob* cb = CodeCache::find_blob_unsafe(this);
491178479Sjb  assert(cb != NULL && cb->is_nmethod(), "must be nmethod");
492178479Sjb#endif
493178479Sjb  set_destination_mt_safe(SharedRuntime::get_resolve_static_call_stub());
494178479Sjb
495178479Sjb  // Do not reset stub here:  It is too expensive to call find_stub.
496178479Sjb  // Instead, rely on caller (nmethod::clear_inline_caches) to clear
497178479Sjb  // both the call and its stub.
498178479Sjb}
499178479Sjb
500178479Sjb
501178479Sjbbool CompiledStaticCall::is_clean() const {
502178479Sjb  return destination() == SharedRuntime::get_resolve_static_call_stub();
503178479Sjb}
504178479Sjb
505178479Sjbbool CompiledStaticCall::is_call_to_compiled() const {
506178479Sjb  return CodeCache::contains(destination());
507178479Sjb}
508178479Sjb
509178479Sjb
510178479Sjbbool CompiledStaticCall::is_call_to_interpreted() const {
511178479Sjb  // It is a call to interpreted, if it calls to a stub. Hence, the destination
512178479Sjb  // must be in the stub part of the nmethod that contains the call
513178479Sjb  nmethod* nm = CodeCache::find_nmethod(instruction_address());
514178479Sjb  return nm->stub_contains(destination());
515178479Sjb}
516178479Sjb
517178479Sjbvoid CompiledStaticCall::set(const StaticCallInfo& info) {
518178479Sjb  assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
519178479Sjb  MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
520178479Sjb  // Updating a cache to the wrong entry can cause bugs that are very hard
521178479Sjb  // to track down - if cache entry gets invalid - we just clean it. In
522178479Sjb  // this way it is always the same code path that is responsible for
523178479Sjb  // updating and resolving an inline cache
524178479Sjb  assert(is_clean(), "do not update a call entry - use clean");
525178479Sjb
526178479Sjb  if (info._to_interpreter) {
527178479Sjb    // Call to interpreted code
528178479Sjb    set_to_interpreted(info.callee(), info.entry());
529178479Sjb  } else {
530178479Sjb    if (TraceICs) {
531178479Sjb      ResourceMark rm;
532178479Sjb      tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT,
533178479Sjb                    p2i(instruction_address()),
534178479Sjb                    p2i(info.entry()));
535178479Sjb    }
536178479Sjb    // Call to compiled code
537178479Sjb    assert (CodeCache::contains(info.entry()), "wrong entry point");
538178479Sjb    set_destination_mt_safe(info.entry());
539178479Sjb  }
540178479Sjb}
541178479Sjb
542178479Sjb
543178479Sjb// Compute settings for a CompiledStaticCall. Since we might have to set
544178479Sjb// the stub when calling to the interpreter, we need to return arguments.
545178479Sjbvoid CompiledStaticCall::compute_entry(methodHandle m, StaticCallInfo& info) {
546178479Sjb  nmethod* m_code = m->code();
547178479Sjb  info._callee = m;
548178479Sjb  if (m_code != NULL && m_code->is_in_use()) {
549178479Sjb    info._to_interpreter = false;
550178479Sjb    info._entry  = m_code->verified_entry_point();
551178479Sjb  } else {
552178479Sjb    // Callee is interpreted code.  In any case entering the interpreter
553178479Sjb    // puts a converter-frame on the stack to save arguments.
554178479Sjb    info._to_interpreter = true;
555178479Sjb    info._entry      = m()->get_c2i_entry();
556178479Sjb  }
557178479Sjb}
558178479Sjb
559178479Sjbaddress CompiledStaticCall::find_stub() {
560178479Sjb  // Find reloc. information containing this call-site
561178479Sjb  RelocIterator iter((nmethod*)NULL, instruction_address());
562178479Sjb  while (iter.next()) {
563178479Sjb    if (iter.addr() == instruction_address()) {
564178479Sjb      switch(iter.type()) {
565178479Sjb        case relocInfo::static_call_type:
566178479Sjb          return iter.static_call_reloc()->static_stub();
567178479Sjb        // We check here for opt_virtual_call_type, since we reuse the code
568178479Sjb        // from the CompiledIC implementation
569178479Sjb        case relocInfo::opt_virtual_call_type:
570178479Sjb          return iter.opt_virtual_call_reloc()->static_stub();
571178479Sjb        case relocInfo::poll_type:
572178479Sjb        case relocInfo::poll_return_type: // A safepoint can't overlap a call.
573178479Sjb        default:
574178479Sjb          ShouldNotReachHere();
575178479Sjb      }
576178479Sjb    }
577178479Sjb  }
578178479Sjb  return NULL;
579178479Sjb}
580178479Sjb
581178479Sjb
582178479Sjb//-----------------------------------------------------------------------------
583178479Sjb// Non-product mode code
584178479Sjb#ifndef PRODUCT
585178479Sjb
586178479Sjbvoid CompiledIC::verify() {
587178479Sjb  // make sure code pattern is actually a call imm32 instruction
588178479Sjb  _ic_call->verify();
589178479Sjb  if (os::is_MP()) {
590178479Sjb    _ic_call->verify_alignment();
591178479Sjb  }
592178479Sjb  assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted()
593178479Sjb          || is_optimized() || is_megamorphic(), "sanity check");
594178479Sjb}
595178479Sjb
596178479Sjbvoid CompiledIC::print() {
597178479Sjb  print_compiled_ic();
598178479Sjb  tty->cr();
599178479Sjb}
600178479Sjb
601178479Sjbvoid CompiledIC::print_compiled_ic() {
602178479Sjb  tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT,
603178479Sjb             p2i(instruction_address()), is_call_to_interpreted() ? "interpreted " : "", p2i(ic_destination()), p2i(is_optimized() ? NULL : cached_value()));
604178479Sjb}
605178479Sjb
606178479Sjbvoid CompiledStaticCall::print() {
607178479Sjb  tty->print("static call at " INTPTR_FORMAT " -> ", p2i(instruction_address()));
608178479Sjb  if (is_clean()) {
609178479Sjb    tty->print("clean");
610178479Sjb  } else if (is_call_to_compiled()) {
611178479Sjb    tty->print("compiled");
612178479Sjb  } else if (is_call_to_interpreted()) {
613178479Sjb    tty->print("interpreted");
614178479Sjb  }
615178479Sjb  tty->cr();
616178479Sjb}
617178479Sjb
618178479Sjb#endif // !PRODUCT
619178479Sjb