Path: blob/master/src/hotspot/share/code/compiledIC.hpp
40931 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_CODE_COMPILEDIC_HPP25#define SHARE_CODE_COMPILEDIC_HPP2627#include "code/nativeInst.hpp"28#include "interpreter/linkResolver.hpp"29#include "oops/compiledICHolder.hpp"30#include "runtime/safepointVerifiers.hpp"3132//-----------------------------------------------------------------------------33// The CompiledIC represents a compiled inline cache.34//35// In order to make patching of the inline cache MT-safe, we only allow the following36// transitions (when not at a safepoint):37//38//39// [1] --<-- Clean -->--- [1]40// / (null) \41// / \ /-<-\42// / [2] \ / \43// Interpreted ---------> Monomorphic | [3]44// (CompiledICHolder*) (Klass*) |45// \ / \ /46// [4] \ / [4] \->-/47// \->- Megamorphic -<-/48// (CompiledICHolder*)49//50// The text in parentheses () refers to the value of the inline cache receiver (mov instruction)51//52// The numbers in square brackets refer to the kind of transition:53// [1]: Initial fixup. Receiver it found from debug information54// [2]: Compilation of a method55// [3]: Recompilation of a method (note: only entry is changed. The Klass* must stay the same)56// [4]: Inline cache miss. We go directly to megamorphic call.57//58// The class automatically inserts transition stubs (using the InlineCacheBuffer) when an MT-unsafe59// transition is made to a stub.60//61class CompiledIC;62class CompiledICProtectionBehaviour;63class CompiledMethod;64class ICStub;6566class CompiledICLocker: public StackObj {67CompiledMethod* _method;68CompiledICProtectionBehaviour* _behaviour;69bool _locked;70NoSafepointVerifier _nsv;7172public:73CompiledICLocker(CompiledMethod* method);74~CompiledICLocker();75static bool is_safe(CompiledMethod* method);76static bool is_safe(address code);77};7879class CompiledICInfo : public StackObj {80private:81address _entry; // entry point for call82void* _cached_value; // Value of cached_value (either in stub or inline cache)83bool _is_icholder; // Is the cached value a CompiledICHolder*84bool _is_optimized; // it is an optimized virtual call (i.e., can be statically bound)85bool _to_interpreter; // Call it to interpreter86bool _release_icholder;87public:88address entry() const { return _entry; }89Metadata* cached_metadata() const { assert(!_is_icholder, ""); return (Metadata*)_cached_value; }90CompiledICHolder* claim_cached_icholder() {91assert(_is_icholder, "");92assert(_cached_value != NULL, "must be non-NULL");93_release_icholder = false;94CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;95icholder->claim();96return icholder;97}98bool is_optimized() const { return _is_optimized; }99bool to_interpreter() const { return _to_interpreter; }100101void set_compiled_entry(address entry, Klass* klass, bool is_optimized) {102_entry = entry;103_cached_value = (void*)klass;104_to_interpreter = false;105_is_icholder = false;106_is_optimized = is_optimized;107_release_icholder = false;108}109110void set_interpreter_entry(address entry, Method* method) {111_entry = entry;112_cached_value = (void*)method;113_to_interpreter = true;114_is_icholder = false;115_is_optimized = true;116_release_icholder = false;117}118119void set_icholder_entry(address entry, CompiledICHolder* icholder) {120_entry = entry;121_cached_value = (void*)icholder;122_to_interpreter = true;123_is_icholder = true;124_is_optimized = false;125_release_icholder = true;126}127128CompiledICInfo(): _entry(NULL), _cached_value(NULL), _is_icholder(false),129_is_optimized(false), _to_interpreter(false), _release_icholder(false) {130}131~CompiledICInfo() {132// In rare cases the info is computed but not used, so release any133// CompiledICHolder* that was created134if (_release_icholder) {135assert(_is_icholder, "must be");136CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;137icholder->claim();138delete icholder;139}140}141};142143class NativeCallWrapper: public ResourceObj {144public:145virtual address destination() const = 0;146virtual address instruction_address() const = 0;147virtual address next_instruction_address() const = 0;148virtual address return_address() const = 0;149virtual address get_resolve_call_stub(bool is_optimized) const = 0;150virtual void set_destination_mt_safe(address dest) = 0;151virtual void set_to_interpreted(const methodHandle& method, CompiledICInfo& info) = 0;152virtual void verify() const = 0;153virtual void verify_resolve_call(address dest) const = 0;154155virtual bool is_call_to_interpreted(address dest) const = 0;156virtual bool is_safe_for_patching() const = 0;157158virtual NativeInstruction* get_load_instruction(virtual_call_Relocation* r) const = 0;159160virtual void *get_data(NativeInstruction* instruction) const = 0;161virtual void set_data(NativeInstruction* instruction, intptr_t data) = 0;162};163164class CompiledIC: public ResourceObj {165friend class InlineCacheBuffer;166friend class ICStub;167168private:169NativeCallWrapper* _call;170NativeInstruction* _value; // patchable value cell for this IC171bool _is_optimized; // an optimized virtual call (i.e., no compiled IC)172CompiledMethod* _method;173174CompiledIC(CompiledMethod* cm, NativeCall* ic_call);175CompiledIC(RelocIterator* iter);176177void initialize_from_iter(RelocIterator* iter);178179static bool is_icholder_entry(address entry);180181// low-level inline-cache manipulation. Cannot be accessed directly, since it might not be MT-safe182// to change an inline-cache. These changes the underlying inline-cache directly. They *newer* make183// changes to a transition stub.184void internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder);185void set_ic_destination(ICStub* stub);186void set_ic_destination(address entry_point) {187assert(_is_optimized, "use set_ic_destination_and_value instead");188internal_set_ic_destination(entry_point, false, NULL, false);189}190// This only for use by ICStubs where the type of the value isn't known191void set_ic_destination_and_value(address entry_point, void* value) {192internal_set_ic_destination(entry_point, false, value, is_icholder_entry(entry_point));193}194void set_ic_destination_and_value(address entry_point, Metadata* value) {195internal_set_ic_destination(entry_point, false, value, false);196}197void set_ic_destination_and_value(address entry_point, CompiledICHolder* value) {198internal_set_ic_destination(entry_point, false, value, true);199}200201// Reads the location of the transition stub. This will fail with an assertion, if no transition stub is202// associated with the inline cache.203address stub_address() const;204bool is_in_transition_state() const; // Use InlineCacheBuffer205206public:207// conversion (machine PC to CompiledIC*)208friend CompiledIC* CompiledIC_before(CompiledMethod* nm, address return_addr);209friend CompiledIC* CompiledIC_at(CompiledMethod* nm, address call_site);210friend CompiledIC* CompiledIC_at(Relocation* call_site);211friend CompiledIC* CompiledIC_at(RelocIterator* reloc_iter);212213static bool is_icholder_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm);214215// Return the cached_metadata/destination associated with this inline cache. If the cache currently points216// to a transition stub, it will read the values from the transition stub.217void* cached_value() const;218CompiledICHolder* cached_icholder() const {219assert(is_icholder_call(), "must be");220return (CompiledICHolder*) cached_value();221}222Metadata* cached_metadata() const {223assert(!is_icholder_call(), "must be");224return (Metadata*) cached_value();225}226227void* get_data() const {228return _call->get_data(_value);229}230231void set_data(intptr_t data) {232_call->set_data(_value, data);233}234235address ic_destination() const;236237bool is_optimized() const { return _is_optimized; }238239// State240bool is_clean() const;241bool is_megamorphic() const;242bool is_call_to_compiled() const;243bool is_call_to_interpreted() const;244245bool is_icholder_call() const;246247address end_of_call() { return _call->return_address(); }248249// MT-safe patching of inline caches. Note: Only safe to call is_xxx when holding the CompiledIC_ock250// so you are guaranteed that no patching takes place. The same goes for verify.251//252// Note: We do not provide any direct access to the stub code, to prevent parts of the code253// to manipulate the inline cache in MT-unsafe ways.254//255// They all takes a TRAP argument, since they can cause a GC if the inline-cache buffer is full.256//257bool set_to_clean(bool in_use = true);258bool set_to_monomorphic(CompiledICInfo& info);259void clear_ic_stub();260261// Returns true if successful and false otherwise. The call can fail if memory262// allocation in the code cache fails, or ic stub refill is required.263bool set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, bool& needs_ic_stub_refill, TRAPS);264265static void compute_monomorphic_entry(const methodHandle& method, Klass* receiver_klass,266bool is_optimized, bool static_bound, bool caller_is_nmethod,267CompiledICInfo& info, TRAPS);268269// Location270address instruction_address() const { return _call->instruction_address(); }271272// Misc273void print() PRODUCT_RETURN;274void print_compiled_ic() PRODUCT_RETURN;275void verify() PRODUCT_RETURN;276};277278inline CompiledIC* CompiledIC_before(CompiledMethod* nm, address return_addr) {279CompiledIC* c_ic = new CompiledIC(nm, nativeCall_before(return_addr));280c_ic->verify();281return c_ic;282}283284inline CompiledIC* CompiledIC_at(CompiledMethod* nm, address call_site) {285CompiledIC* c_ic = new CompiledIC(nm, nativeCall_at(call_site));286c_ic->verify();287return c_ic;288}289290inline CompiledIC* CompiledIC_at(Relocation* call_site) {291assert(call_site->type() == relocInfo::virtual_call_type ||292call_site->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");293CompiledIC* c_ic = new CompiledIC(call_site->code(), nativeCall_at(call_site->addr()));294c_ic->verify();295return c_ic;296}297298inline CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {299assert(reloc_iter->type() == relocInfo::virtual_call_type ||300reloc_iter->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");301CompiledIC* c_ic = new CompiledIC(reloc_iter);302c_ic->verify();303return c_ic;304}305306//-----------------------------------------------------------------------------307// The CompiledStaticCall represents a call to a static method in the compiled308//309// Transition diagram of a static call site is somewhat simpler than for an inlined cache:310//311//312// -----<----- Clean ----->-----313// / \314// / \315// compilled code <------------> interpreted code316//317// Clean: Calls directly to runtime method for fixup318// Compiled code: Calls directly to compiled code319// Interpreted code: Calls to stub that set Method* reference320//321//322323class StaticCallInfo {324private:325address _entry; // Entrypoint326methodHandle _callee; // Callee (used when calling interpreter)327bool _to_interpreter; // call to interpreted method (otherwise compiled)328329friend class CompiledStaticCall;330friend class CompiledDirectStaticCall;331friend class CompiledPltStaticCall;332public:333address entry() const { return _entry; }334methodHandle callee() const { return _callee; }335};336337class CompiledStaticCall : public ResourceObj {338public:339// Code340static address emit_to_interp_stub(CodeBuffer &cbuf, address mark = NULL);341static int to_interp_stub_size();342static int to_trampoline_stub_size();343static int reloc_to_interp_stub();344345// Compute entry point given a method346static void compute_entry(const methodHandle& m, bool caller_is_nmethod, StaticCallInfo& info);347348public:349// Clean static call (will force resolving on next use)350virtual address destination() const = 0;351352// Clean static call (will force resolving on next use)353bool set_to_clean(bool in_use = true);354355// Set state. The entry must be the same, as computed by compute_entry.356// Computation and setting is split up, since the actions are separate during357// a OptoRuntime::resolve_xxx.358void set(const StaticCallInfo& info);359360// State361bool is_clean() const;362bool is_call_to_compiled() const;363virtual bool is_call_to_interpreted() const = 0;364365virtual address instruction_address() const = 0;366protected:367virtual address resolve_call_stub() const = 0;368virtual void set_destination_mt_safe(address dest) = 0;369virtual void set_to_interpreted(const methodHandle& callee, address entry) = 0;370virtual const char* name() const = 0;371372void set_to_compiled(address entry);373};374375class CompiledDirectStaticCall : public CompiledStaticCall {376private:377friend class CompiledIC;378friend class DirectNativeCallWrapper;379380// Also used by CompiledIC381void set_to_interpreted(const methodHandle& callee, address entry);382void verify_mt_safe(const methodHandle& callee, address entry,383NativeMovConstReg* method_holder,384NativeJump* jump) PRODUCT_RETURN;385address instruction_address() const { return _call->instruction_address(); }386void set_destination_mt_safe(address dest) { _call->set_destination_mt_safe(dest); }387388NativeCall* _call;389390CompiledDirectStaticCall(NativeCall* call) : _call(call) {}391392public:393static inline CompiledDirectStaticCall* before(address return_addr) {394CompiledDirectStaticCall* st = new CompiledDirectStaticCall(nativeCall_before(return_addr));395st->verify();396return st;397}398399static inline CompiledDirectStaticCall* at(address native_call) {400CompiledDirectStaticCall* st = new CompiledDirectStaticCall(nativeCall_at(native_call));401st->verify();402return st;403}404405static inline CompiledDirectStaticCall* at(Relocation* call_site) {406return at(call_site->addr());407}408409// Delegation410address destination() const { return _call->destination(); }411412// State413virtual bool is_call_to_interpreted() const;414415// Stub support416static address find_stub_for(address instruction);417address find_stub();418static void set_stub_to_clean(static_stub_Relocation* static_stub);419420// Misc.421void print() PRODUCT_RETURN;422void verify() PRODUCT_RETURN;423424protected:425virtual address resolve_call_stub() const;426virtual const char* name() const { return "CompiledDirectStaticCall"; }427};428429#endif // SHARE_CODE_COMPILEDIC_HPP430431432