1 /*
  2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/systemDictionary.hpp"
 27 #include "code/codeBehaviours.hpp"
 28 #include "code/codeCache.hpp"
 29 #include "code/compiledIC.hpp"
 30 #include "code/icBuffer.hpp"
 31 #include "code/nmethod.hpp"
 32 #include "code/vtableStubs.hpp"
 33 #include "interpreter/interpreter.hpp"
 34 #include "interpreter/linkResolver.hpp"
 35 #include "memory/metadataFactory.hpp"
 36 #include "memory/oopFactory.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "oops/method.inline.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "oops/symbol.hpp"
 41 #include "runtime/handles.inline.hpp"
 42 #include "runtime/icache.hpp"
 43 #include "runtime/sharedRuntime.hpp"
 44 #include "runtime/stubRoutines.hpp"
 45 #include "utilities/events.hpp"
 46 
 47 
 48 // Every time a compiled IC is changed or its type is being accessed,
 49 // either the CompiledIC_lock must be set or we must be at a safe point.
 50 
 51 CompiledICLocker::CompiledICLocker(CompiledMethod* method)
 52   : _method(method),
 53     _behaviour(CompiledICProtectionBehaviour::current()),
 54     _locked(_behaviour->lock(_method)){
 55 }
 56 
 57 CompiledICLocker::~CompiledICLocker() {
 58   if (_locked) {
 59     _behaviour->unlock(_method);
 60   }
 61 }
 62 
 63 bool CompiledICLocker::is_safe(CompiledMethod* method) {
 64   return CompiledICProtectionBehaviour::current()->is_safe(method);
 65 }
 66 
 67 bool CompiledICLocker::is_safe(address code) {
 68   CodeBlob* cb = CodeCache::find_blob_unsafe(code);
 69   assert(cb != NULL && cb->is_compiled(), "must be compiled");
 70   CompiledMethod* cm = cb->as_compiled_method();
 71   return CompiledICProtectionBehaviour::current()->is_safe(cm);
 72 }
 73 
 74 //-----------------------------------------------------------------------------
 75 // Low-level access to an inline cache. Private, since they might not be
 76 // MT-safe to use.
 77 
 78 void* CompiledIC::cached_value() const {
 79   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
 80   assert (!is_optimized(), "an optimized virtual call does not have a cached metadata");
 81 
 82   if (!is_in_transition_state()) {
 83     void* data = get_data();
 84     // If we let the metadata value here be initialized to zero...
 85     assert(data != NULL || Universe::non_oop_word() == NULL,
 86            "no raw nulls in CompiledIC metadatas, because of patching races");
 87     return (data == (void*)Universe::non_oop_word()) ? NULL : data;
 88   } else {
 89     return InlineCacheBuffer::cached_value_for((CompiledIC *)this);
 90   }
 91 }
 92 
 93 
 94 void CompiledIC::internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder) {
 95   assert(entry_point != NULL, "must set legal entry point");
 96   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
 97   assert (!is_optimized() || cache == NULL, "an optimized virtual call does not have a cached metadata");
 98   assert (cache == NULL || cache != (Metadata*)badOopVal, "invalid metadata");
 99 
100   assert(!is_icholder || is_icholder_entry(entry_point), "must be");
101 
102   // Don't use ic_destination for this test since that forwards
103   // through ICBuffer instead of returning the actual current state of
104   // the CompiledIC.
105   if (is_icholder_entry(_call->destination())) {
106     // When patching for the ICStub case the cached value isn't
107     // overwritten until the ICStub copied into the CompiledIC during
108     // the next safepoint.  Make sure that the CompiledICHolder* is
109     // marked for release at this point since it won't be identifiable
110     // once the entry point is overwritten.
111     InlineCacheBuffer::queue_for_release((CompiledICHolder*)get_data());
112   }
113 
114   if (TraceCompiledIC) {
115     tty->print("  ");
116     print_compiled_ic();
117     tty->print(" changing destination to " INTPTR_FORMAT, p2i(entry_point));
118     if (!is_optimized()) {
119       tty->print(" changing cached %s to " INTPTR_FORMAT, is_icholder ? "icholder" : "metadata", p2i((address)cache));
120     }
121     if (is_icstub) {
122       tty->print(" (icstub)");
123     }
124     tty->cr();
125   }
126 
127   {
128     CodeBlob* cb = CodeCache::find_blob_unsafe(_call->instruction_address());
129     MutexLockerEx pl(CompiledICLocker::is_safe(cb->as_compiled_method()) ? NULL : Patching_lock, Mutex::_no_safepoint_check_flag);
130     assert(cb != NULL && cb->is_compiled(), "must be compiled");
131     _call->set_destination_mt_safe(entry_point);
132   }
133 
134   if (is_optimized() || is_icstub) {
135     // Optimized call sites don't have a cache value and ICStub call
136     // sites only change the entry point.  Changing the value in that
137     // case could lead to MT safety issues.
138     assert(cache == NULL, "must be null");
139     return;
140   }
141 
142   if (cache == NULL)  cache = (void*)Universe::non_oop_word();
143 
144   set_data((intptr_t)cache);
145 }
146 
147 
148 void CompiledIC::set_ic_destination(ICStub* stub) {
149   internal_set_ic_destination(stub->code_begin(), true, NULL, false);
150 }
151 
152 
153 
154 address CompiledIC::ic_destination() const {
155   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
156   if (!is_in_transition_state()) {
157     return _call->destination();
158   } else {
159     return InlineCacheBuffer::ic_destination_for((CompiledIC *)this);
160   }
161 }
162 
163 
164 bool CompiledIC::is_in_transition_state() const {
165   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
166   return InlineCacheBuffer::contains(_call->destination());;
167 }
168 
169 
170 bool CompiledIC::is_icholder_call() const {
171   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
172   return !_is_optimized && is_icholder_entry(ic_destination());
173 }
174 
175 // Returns native address of 'call' instruction in inline-cache. Used by
176 // the InlineCacheBuffer when it needs to find the stub.
177 address CompiledIC::stub_address() const {
178   assert(is_in_transition_state(), "should only be called when we are in a transition state");
179   return _call->destination();
180 }
181 
182 // Clears the IC stub if the compiled IC is in transition state
183 void CompiledIC::clear_ic_stub() {
184   if (is_in_transition_state()) {
185     ICStub* stub = ICStub_from_destination_address(stub_address());
186     stub->clear();
187   }
188 }
189 
190 //-----------------------------------------------------------------------------
191 // High-level access to an inline cache. Guaranteed to be MT-safe.
192 
193 void CompiledIC::initialize_from_iter(RelocIterator* iter) {
194   assert(iter->addr() == _call->instruction_address(), "must find ic_call");
195 
196   if (iter->type() == relocInfo::virtual_call_type) {
197     virtual_call_Relocation* r = iter->virtual_call_reloc();
198     _is_optimized = false;
199     _value = _call->get_load_instruction(r);
200   } else {
201     assert(iter->type() == relocInfo::opt_virtual_call_type, "must be a virtual call");
202     _is_optimized = true;
203     _value = NULL;
204   }
205 }
206 
207 CompiledIC::CompiledIC(CompiledMethod* cm, NativeCall* call)
208   : _method(cm)
209 {
210   _call = _method->call_wrapper_at((address) call);
211   address ic_call = _call->instruction_address();
212 
213   assert(ic_call != NULL, "ic_call address must be set");
214   assert(cm != NULL, "must pass compiled method");
215   assert(cm->contains(ic_call), "must be in compiled method");
216 
217   // Search for the ic_call at the given address.
218   RelocIterator iter(cm, ic_call, ic_call+1);
219   bool ret = iter.next();
220   assert(ret == true, "relocInfo must exist at this address");
221   assert(iter.addr() == ic_call, "must find ic_call");
222 
223   initialize_from_iter(&iter);
224 }
225 
226 CompiledIC::CompiledIC(RelocIterator* iter)
227   : _method(iter->code())
228 {
229   _call = _method->call_wrapper_at(iter->addr());
230   address ic_call = _call->instruction_address();
231 
232   CompiledMethod* nm = iter->code();
233   assert(ic_call != NULL, "ic_call address must be set");
234   assert(nm != NULL, "must pass compiled method");
235   assert(nm->contains(ic_call), "must be in compiled method");
236 
237   initialize_from_iter(iter);
238 }
239 
240 bool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS) {
241   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
242   assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic");
243   assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?");
244 
245   address entry;
246   if (call_info->call_kind() == CallInfo::itable_call) {
247     assert(bytecode == Bytecodes::_invokeinterface, "");
248     int itable_index = call_info->itable_index();
249     entry = VtableStubs::find_itable_stub(itable_index);
250     if (entry == NULL) {
251       return false;
252     }
253 #ifdef ASSERT
254     int index = call_info->resolved_method()->itable_index();
255     assert(index == itable_index, "CallInfo pre-computes this");
256     InstanceKlass* k = call_info->resolved_method()->method_holder();
257     assert(k->verify_itable_index(itable_index), "sanity check");
258 #endif //ASSERT
259     CompiledICHolder* holder = new CompiledICHolder(call_info->resolved_method()->method_holder(),
260                                                     call_info->resolved_klass(), false);
261     holder->claim();
262     if (!InlineCacheBuffer::create_transition_stub(this, holder, entry)) {
263       delete holder;
264       return false;
265     }
266   } else {
267     assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable");
268     // Can be different than selected_method->vtable_index(), due to package-private etc.
269     int vtable_index = call_info->vtable_index();
270     assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
271     entry = VtableStubs::find_vtable_stub(vtable_index);
272     if (entry == NULL) {
273       return false;
274     }
275     if (!InlineCacheBuffer::create_transition_stub(this, NULL, entry)) {
276       return false;
277     }
278   }
279 
280   if (TraceICs) {
281     ResourceMark rm;
282     assert(!call_info->selected_method().is_null(), "Unexpected null selected method");
283     tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
284                    p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
285   }
286 
287   // We can't check this anymore. With lazy deopt we could have already
288   // cleaned this IC entry before we even return. This is possible if
289   // we ran out of space in the inline cache buffer trying to do the
290   // set_next and we safepointed to free up space. This is a benign
291   // race because the IC entry was complete when we safepointed so
292   // cleaning it immediately is harmless.
293   // assert(is_megamorphic(), "sanity check");
294   return true;
295 }
296 
297 
298 // true if destination is megamorphic stub
299 bool CompiledIC::is_megamorphic() const {
300   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
301   assert(!is_optimized(), "an optimized call cannot be megamorphic");
302 
303   // Cannot rely on cached_value. It is either an interface or a method.
304   return VtableStubs::entry_point(ic_destination()) != NULL;
305 }
306 
307 bool CompiledIC::is_call_to_compiled() const {
308   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
309 
310   // Use unsafe, since an inline cache might point to a zombie method. However, the zombie
311   // method is guaranteed to still exist, since we only remove methods after all inline caches
312   // has been cleaned up
313   CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
314   bool is_monomorphic = (cb != NULL && cb->is_compiled());
315   // Check that the cached_value is a klass for non-optimized monomorphic calls
316   // This assertion is invalid for compiler1: a call that does not look optimized (no static stub) can be used
317   // for calling directly to vep without using the inline cache (i.e., cached_value == NULL).
318   // For JVMCI this occurs because CHA is only used to improve inlining so call sites which could be optimized
319   // virtuals because there are no currently loaded subclasses of a type are left as virtual call sites.
320 #ifdef ASSERT
321   CodeBlob* caller = CodeCache::find_blob_unsafe(instruction_address());
322   bool is_c1_or_jvmci_method = caller->is_compiled_by_c1() || caller->is_compiled_by_jvmci();
323   assert( is_c1_or_jvmci_method ||
324          !is_monomorphic ||
325          is_optimized() ||
326          !caller->is_alive() ||
327          (cached_metadata() != NULL && cached_metadata()->is_klass()), "sanity check");
328 #endif // ASSERT
329   return is_monomorphic;
330 }
331 
332 
333 bool CompiledIC::is_call_to_interpreted() const {
334   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
335   // Call to interpreter if destination is either calling to a stub (if it
336   // is optimized), or calling to an I2C blob
337   bool is_call_to_interpreted = false;
338   if (!is_optimized()) {
339     // must use unsafe because the destination can be a zombie (and we're cleaning)
340     // and the print_compiled_ic code wants to know if site (in the non-zombie)
341     // is to the interpreter.
342     CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
343     is_call_to_interpreted = (cb != NULL && cb->is_adapter_blob());
344     assert(!is_call_to_interpreted || (is_icholder_call() && cached_icholder() != NULL), "sanity check");
345   } else {
346     // Check if we are calling into our own codeblob (i.e., to a stub)
347     address dest = ic_destination();
348 #ifdef ASSERT
349     {
350       _call->verify_resolve_call(dest);
351     }
352 #endif /* ASSERT */
353     is_call_to_interpreted = _call->is_call_to_interpreted(dest);
354   }
355   return is_call_to_interpreted;
356 }
357 
358 bool CompiledIC::set_to_clean(bool in_use) {
359   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
360   if (TraceInlineCacheClearing || TraceICs) {
361     tty->print_cr("IC@" INTPTR_FORMAT ": set to clean", p2i(instruction_address()));
362     print();
363   }
364 
365   address entry = _call->get_resolve_call_stub(is_optimized());
366 
367   // A zombie transition will always be safe, since the metadata has already been set to NULL, so
368   // we only need to patch the destination
369   bool safe_transition = _call->is_safe_for_patching() || !in_use || is_optimized() || CompiledICLocker::is_safe(_method);
370 
371   if (safe_transition) {
372     // Kill any leftover stub we might have too
373     clear_ic_stub();
374     if (is_optimized()) {
375       set_ic_destination(entry);
376     } else {
377       set_ic_destination_and_value(entry, (void*)NULL);
378     }
379   } else {
380     // Unsafe transition - create stub.
381     if (!InlineCacheBuffer::create_transition_stub(this, NULL, entry)) {
382       return false;
383     }
384   }
385   // We can't check this anymore. With lazy deopt we could have already
386   // cleaned this IC entry before we even return. This is possible if
387   // we ran out of space in the inline cache buffer trying to do the
388   // set_next and we safepointed to free up space. This is a benign
389   // race because the IC entry was complete when we safepointed so
390   // cleaning it immediately is harmless.
391   // assert(is_clean(), "sanity check");
392   return true;
393 }
394 
395 bool CompiledIC::is_clean() const {
396   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
397   bool is_clean = false;
398   address dest = ic_destination();
399   is_clean = dest == _call->get_resolve_call_stub(is_optimized());
400   assert(!is_clean || is_optimized() || cached_value() == NULL, "sanity check");
401   return is_clean;
402 }
403 
404 bool CompiledIC::set_to_monomorphic(CompiledICInfo& info) {
405   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
406   // Updating a cache to the wrong entry can cause bugs that are very hard
407   // to track down - if cache entry gets invalid - we just clean it. In
408   // this way it is always the same code path that is responsible for
409   // updating and resolving an inline cache
410   //
411   // The above is no longer true. SharedRuntime::fixup_callers_callsite will change optimized
412   // callsites. In addition ic_miss code will update a site to monomorphic if it determines
413   // that an monomorphic call to the interpreter can now be monomorphic to compiled code.
414   //
415   // In both of these cases the only thing being modifed is the jump/call target and these
416   // transitions are mt_safe
417 
418   Thread *thread = Thread::current();
419   if (info.to_interpreter() || info.to_aot()) {
420     // Call to interpreter
421     if (info.is_optimized() && is_optimized()) {
422        assert(is_clean(), "unsafe IC path");
423        MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
424       // the call analysis (callee structure) specifies that the call is optimized
425       // (either because of CHA or the static target is final)
426       // At code generation time, this call has been emitted as static call
427       // Call via stub
428       assert(info.cached_metadata() != NULL && info.cached_metadata()->is_method(), "sanity check");
429       methodHandle method (thread, (Method*)info.cached_metadata());
430       _call->set_to_interpreted(method, info);
431 
432       if (TraceICs) {
433          ResourceMark rm(thread);
434          tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to %s: %s",
435            p2i(instruction_address()),
436            (info.to_aot() ? "aot" : "interpreter"),
437            method->print_value_string());
438       }
439     } else {
440       // Call via method-klass-holder
441       CompiledICHolder* holder = info.claim_cached_icholder();
442       if (!InlineCacheBuffer::create_transition_stub(this, holder, info.entry())) {
443         delete holder;
444         return false;
445       }
446       if (TraceICs) {
447          ResourceMark rm(thread);
448          tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter via icholder ", p2i(instruction_address()));
449       }
450     }
451   } else {
452     // Call to compiled code
453     bool static_bound = info.is_optimized() || (info.cached_metadata() == NULL);
454 #ifdef ASSERT
455     CodeBlob* cb = CodeCache::find_blob_unsafe(info.entry());
456     assert (cb != NULL && cb->is_compiled(), "must be compiled!");
457 #endif /* ASSERT */
458 
459     // This is MT safe if we come from a clean-cache and go through a
460     // non-verified entry point
461     bool safe = SafepointSynchronize::is_at_safepoint() ||
462                 (!is_in_transition_state() && (info.is_optimized() || static_bound || is_clean()));
463 
464     if (!safe) {
465       if (!InlineCacheBuffer::create_transition_stub(this, info.cached_metadata(), info.entry())) {
466         return false;
467       }
468     } else {
469       if (is_optimized()) {
470         set_ic_destination(info.entry());
471       } else {
472         set_ic_destination_and_value(info.entry(), info.cached_metadata());
473       }
474     }
475 
476     if (TraceICs) {
477       ResourceMark rm(thread);
478       assert(info.cached_metadata() == NULL || info.cached_metadata()->is_klass(), "must be");
479       tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to compiled (rcvr klass) %s: %s",
480         p2i(instruction_address()),
481         ((Klass*)info.cached_metadata())->print_value_string(),
482         (safe) ? "" : "via stub");
483     }
484   }
485   // We can't check this anymore. With lazy deopt we could have already
486   // cleaned this IC entry before we even return. This is possible if
487   // we ran out of space in the inline cache buffer trying to do the
488   // set_next and we safepointed to free up space. This is a benign
489   // race because the IC entry was complete when we safepointed so
490   // cleaning it immediately is harmless.
491   // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
492   return true;
493 }
494 
495 
496 // is_optimized: Compiler has generated an optimized call (i.e. fixed, no inline cache)
497 // static_bound: The call can be static bound. If it isn't also optimized, the property
498 // wasn't provable at time of compilation. An optimized call will have any necessary
499 // null check, while a static_bound won't. A static_bound (but not optimized) must
500 // therefore use the unverified entry point.
501 void CompiledIC::compute_monomorphic_entry(const methodHandle& method,
502                                            Klass* receiver_klass,
503                                            bool is_optimized,
504                                            bool static_bound,
505                                            bool caller_is_nmethod,
506                                            CompiledICInfo& info,
507                                            TRAPS) {
508   CompiledMethod* method_code = method->code();
509 
510   address entry = NULL;
511   if (method_code != NULL && method_code->is_in_use()) {
512     assert(method_code->is_compiled(), "must be compiled");
513     // Call to compiled code
514     //
515     // Note: the following problem exists with Compiler1:
516     //   - at compile time we may or may not know if the destination is final
517     //   - if we know that the destination is final (is_optimized), we will emit
518     //     an optimized virtual call (no inline cache), and need a Method* to make
519     //     a call to the interpreter
520     //   - if we don't know if the destination is final, we emit a standard
521     //     virtual call, and use CompiledICHolder to call interpreted code
522     //     (no static call stub has been generated)
523     //   - In the case that we here notice the call is static bound we
524     //     convert the call into what looks to be an optimized virtual call,
525     //     but we must use the unverified entry point (since there will be no
526     //     null check on a call when the target isn't loaded).
527     //     This causes problems when verifying the IC because
528     //     it looks vanilla but is optimized. Code in is_call_to_interpreted
529     //     is aware of this and weakens its asserts.
530     if (is_optimized) {
531       entry      = method_code->verified_entry_point();
532     } else {
533       entry      = method_code->entry_point();
534     }
535   }
536   bool far_c2a = entry != NULL && caller_is_nmethod && method_code->is_far_code();
537   if (entry != NULL && !far_c2a) {
538     // Call to near compiled code (nmethod or aot).
539     info.set_compiled_entry(entry, is_optimized ? NULL : receiver_klass, is_optimized);
540   } else {
541     if (is_optimized) {
542       if (far_c2a) {
543         // Call to aot code from nmethod.
544         info.set_aot_entry(entry, method());
545       } else {
546         // Use stub entry
547         info.set_interpreter_entry(method()->get_c2i_entry(), method());
548       }
549     } else {
550       // Use icholder entry
551       assert(method_code == NULL || method_code->is_compiled(), "must be compiled");
552       CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass);
553       info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder);
554     }
555   }
556   assert(info.is_optimized() == is_optimized, "must agree");
557 }
558 
559 
560 bool CompiledIC::is_icholder_entry(address entry) {
561   CodeBlob* cb = CodeCache::find_blob_unsafe(entry);
562   if (cb != NULL && cb->is_adapter_blob()) {
563     return true;
564   }
565   // itable stubs also use CompiledICHolder
566   if (cb != NULL && cb->is_vtable_blob()) {
567     VtableStub* s = VtableStubs::entry_point(entry);
568     return (s != NULL) && s->is_itable_stub();
569   }
570 
571   return false;
572 }
573 
574 bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm) {
575   // This call site might have become stale so inspect it carefully.
576   address dest = cm->call_wrapper_at(call_site->addr())->destination();
577   return is_icholder_entry(dest);
578 }
579 
580 // Release the CompiledICHolder* associated with this call site is there is one.
581 void CompiledIC::cleanup_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm) {
582   assert(cm->is_nmethod(), "must be nmethod");
583   // This call site might have become stale so inspect it carefully.
584   NativeCall* call = nativeCall_at(call_site->addr());
585   if (is_icholder_entry(call->destination())) {
586     NativeMovConstReg* value = nativeMovConstReg_at(call_site->cached_value());
587     InlineCacheBuffer::queue_for_release((CompiledICHolder*)value->data());
588   }
589 }
590 
591 // ----------------------------------------------------------------------------
592 
593 bool CompiledStaticCall::set_to_clean(bool in_use) {
594   // in_use is unused but needed to match template function in CompiledMethod
595   assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
596   // Reset call site
597   MutexLockerEx pl(SafepointSynchronize::is_at_safepoint() ? NULL : Patching_lock, Mutex::_no_safepoint_check_flag);
598   set_destination_mt_safe(resolve_call_stub());
599 
600   // Do not reset stub here:  It is too expensive to call find_stub.
601   // Instead, rely on caller (nmethod::clear_inline_caches) to clear
602   // both the call and its stub.
603   return true;
604 }
605 
606 bool CompiledStaticCall::is_clean() const {
607   return destination() == resolve_call_stub();
608 }
609 
610 bool CompiledStaticCall::is_call_to_compiled() const {
611   return CodeCache::contains(destination());
612 }
613 
614 bool CompiledDirectStaticCall::is_call_to_interpreted() const {
615   // It is a call to interpreted, if it calls to a stub. Hence, the destination
616   // must be in the stub part of the nmethod that contains the call
617   CompiledMethod* cm = CodeCache::find_compiled(instruction_address());
618   return cm->stub_contains(destination());
619 }
620 
621 bool CompiledDirectStaticCall::is_call_to_far() const {
622   // It is a call to aot method, if it calls to a stub. Hence, the destination
623   // must be in the stub part of the nmethod that contains the call
624   CodeBlob* desc = CodeCache::find_blob(instruction_address());
625   return desc->as_compiled_method()->stub_contains(destination());
626 }
627 
628 void CompiledStaticCall::set_to_compiled(address entry) {
629   if (TraceICs) {
630     ResourceMark rm;
631     tty->print_cr("%s@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT,
632         name(),
633         p2i(instruction_address()),
634         p2i(entry));
635   }
636   // Call to compiled code
637   assert(CodeCache::contains(entry), "wrong entry point");
638   set_destination_mt_safe(entry);
639 }
640 
641 void CompiledStaticCall::set(const StaticCallInfo& info) {
642   assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
643   MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
644   // Updating a cache to the wrong entry can cause bugs that are very hard
645   // to track down - if cache entry gets invalid - we just clean it. In
646   // this way it is always the same code path that is responsible for
647   // updating and resolving an inline cache
648   assert(is_clean(), "do not update a call entry - use clean");
649 
650   if (info._to_interpreter) {
651     // Call to interpreted code
652     set_to_interpreted(info.callee(), info.entry());
653 #if INCLUDE_AOT
654   } else if (info._to_aot) {
655     // Call to far code
656     set_to_far(info.callee(), info.entry());
657 #endif
658   } else {
659     set_to_compiled(info.entry());
660   }
661 }
662 
663 // Compute settings for a CompiledStaticCall. Since we might have to set
664 // the stub when calling to the interpreter, we need to return arguments.
665 void CompiledStaticCall::compute_entry(const methodHandle& m, bool caller_is_nmethod, StaticCallInfo& info) {
666   CompiledMethod* m_code = m->code();
667   info._callee = m;
668   if (m_code != NULL && m_code->is_in_use()) {
669     if (caller_is_nmethod && m_code->is_far_code()) {
670       // Call to far aot code from nmethod.
671       info._to_aot = true;
672     } else {
673       info._to_aot = false;
674     }
675     info._to_interpreter = false;
676     info._entry  = m_code->verified_entry_point();
677   } else {
678     // Callee is interpreted code.  In any case entering the interpreter
679     // puts a converter-frame on the stack to save arguments.
680     assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics");
681     info._to_interpreter = true;
682     info._entry      = m()->get_c2i_entry();
683   }
684 }
685 
686 address CompiledDirectStaticCall::find_stub_for(address instruction, bool is_aot) {
687   // Find reloc. information containing this call-site
688   RelocIterator iter((nmethod*)NULL, instruction);
689   while (iter.next()) {
690     if (iter.addr() == instruction) {
691       switch(iter.type()) {
692         case relocInfo::static_call_type:
693           return iter.static_call_reloc()->static_stub(is_aot);
694         // We check here for opt_virtual_call_type, since we reuse the code
695         // from the CompiledIC implementation
696         case relocInfo::opt_virtual_call_type:
697           return iter.opt_virtual_call_reloc()->static_stub(is_aot);
698         case relocInfo::poll_type:
699         case relocInfo::poll_return_type: // A safepoint can't overlap a call.
700         default:
701           ShouldNotReachHere();
702       }
703     }
704   }
705   return NULL;
706 }
707 
708 address CompiledDirectStaticCall::find_stub(bool is_aot) {
709   return CompiledDirectStaticCall::find_stub_for(instruction_address(), is_aot);
710 }
711 
712 address CompiledDirectStaticCall::resolve_call_stub() const {
713   return SharedRuntime::get_resolve_static_call_stub();
714 }
715 
716 //-----------------------------------------------------------------------------
717 // Non-product mode code
718 #ifndef PRODUCT
719 
720 void CompiledIC::verify() {
721   _call->verify();
722   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted()
723           || is_optimized() || is_megamorphic(), "sanity check");
724 }
725 
726 void CompiledIC::print() {
727   print_compiled_ic();
728   tty->cr();
729 }
730 
731 void CompiledIC::print_compiled_ic() {
732   tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT,
733              p2i(instruction_address()), is_call_to_interpreted() ? "interpreted " : "", p2i(ic_destination()), p2i(is_optimized() ? NULL : cached_value()));
734 }
735 
736 void CompiledDirectStaticCall::print() {
737   tty->print("static call at " INTPTR_FORMAT " -> ", p2i(instruction_address()));
738   if (is_clean()) {
739     tty->print("clean");
740   } else if (is_call_to_compiled()) {
741     tty->print("compiled");
742   } else if (is_call_to_far()) {
743     tty->print("far");
744   } else if (is_call_to_interpreted()) {
745     tty->print("interpreted");
746   }
747   tty->cr();
748 }
749 
750 #endif // !PRODUCT