compiledIC.hpp revision 5776:de6a9e811145
1261287Sdes/*
2261287Sdes * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3261287Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4261287Sdes *
5261287Sdes * This code is free software; you can redistribute it and/or modify it
6261287Sdes * under the terms of the GNU General Public License version 2 only, as
7261287Sdes * published by the Free Software Foundation.
8261287Sdes *
9261287Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
10261287Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11261287Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12261287Sdes * version 2 for more details (a copy is included in the LICENSE file that
13261287Sdes * accompanied this code).
14261287Sdes *
15261287Sdes * You should have received a copy of the GNU General Public License version
16261287Sdes * 2 along with this work; if not, write to the Free Software Foundation,
17261287Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18261287Sdes *
19261287Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20261287Sdes * or visit www.oracle.com if you need additional information or have any
21261287Sdes * questions.
22261287Sdes *
23261287Sdes */
24261287Sdes
25261287Sdes#ifndef SHARE_VM_CODE_COMPILEDIC_HPP
26261287Sdes#define SHARE_VM_CODE_COMPILEDIC_HPP
27261287Sdes
28261287Sdes#include "interpreter/linkResolver.hpp"
29261287Sdes#include "oops/compiledICHolder.hpp"
30261287Sdes#ifdef TARGET_ARCH_x86
31261287Sdes# include "nativeInst_x86.hpp"
32261287Sdes#endif
33261287Sdes#ifdef TARGET_ARCH_sparc
34261287Sdes# include "nativeInst_sparc.hpp"
35261287Sdes#endif
36261287Sdes#ifdef TARGET_ARCH_zero
37261287Sdes# include "nativeInst_zero.hpp"
38261287Sdes#endif
39261287Sdes#ifdef TARGET_ARCH_arm
40261287Sdes# include "nativeInst_arm.hpp"
41261287Sdes#endif
42261287Sdes#ifdef TARGET_ARCH_ppc
43261287Sdes# include "nativeInst_ppc.hpp"
44261287Sdes#endif
45261287Sdes
46261287Sdes//-----------------------------------------------------------------------------
47261287Sdes// The CompiledIC represents a compiled inline cache.
48261287Sdes//
49261287Sdes// In order to make patching of the inline cache MT-safe, we only allow the following
50261287Sdes// transitions (when not at a safepoint):
51261287Sdes//
52261287Sdes//
53261287Sdes//         [1] --<--  Clean -->---  [1]
54261287Sdes//            /       (null)      \
55261287Sdes//           /                     \      /-<-\
56261287Sdes//          /          [2]          \    /     \
57261287Sdes//      Interpreted  ---------> Monomorphic     | [3]
58261287Sdes//  (CompiledICHolder*)            (Klass*)     |
59261287Sdes//          \                        /   \     /
60261287Sdes//       [4] \                      / [4] \->-/
61261287Sdes//            \->-  Megamorphic -<-/
62261287Sdes//                  (Method*)
63261287Sdes//
64261287Sdes// The text in paranteses () refere to the value of the inline cache receiver (mov instruction)
65261287Sdes//
66261287Sdes// The numbers in square brackets refere to the kind of transition:
67261287Sdes// [1]: Initial fixup. Receiver it found from debug information
68261287Sdes// [2]: Compilation of a method
69261287Sdes// [3]: Recompilation of a method (note: only entry is changed. The Klass* must stay the same)
70261287Sdes// [4]: Inline cache miss. We go directly to megamorphic call.
71261287Sdes//
72261287Sdes// The class automatically inserts transition stubs (using the InlineCacheBuffer) when an MT-unsafe
73261287Sdes// transition is made to a stub.
74261287Sdes//
75261287Sdesclass CompiledIC;
76261287Sdesclass ICStub;
77261287Sdes
78261287Sdesclass CompiledICInfo : public StackObj {
79261287Sdes private:
80261287Sdes  address _entry;              // entry point for call
81261287Sdes  void*   _cached_value;         // Value of cached_value (either in stub or inline cache)
82261287Sdes  bool    _is_icholder;          // Is the cached value a CompiledICHolder*
83261287Sdes  bool    _is_optimized;       // it is an optimized virtual call (i.e., can be statically bound)
84261287Sdes  bool    _to_interpreter;     // Call it to interpreter
85261287Sdes  bool    _release_icholder;
86261287Sdes public:
87261287Sdes  address entry() const        { return _entry; }
88261287Sdes  Metadata*    cached_metadata() const         { assert(!_is_icholder, ""); return (Metadata*)_cached_value; }
89261287Sdes  CompiledICHolder*    claim_cached_icholder() {
90261287Sdes    assert(_is_icholder, "");
91261287Sdes    assert(_cached_value != NULL, "must be non-NULL");
92261287Sdes    _release_icholder = false;
93261287Sdes    CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;
94261287Sdes    icholder->claim();
95261287Sdes    return icholder;
96261287Sdes  }
97261287Sdes  bool    is_optimized() const { return _is_optimized; }
98261287Sdes  bool         to_interpreter() const  { return _to_interpreter; }
99261287Sdes
100261287Sdes  void set_compiled_entry(address entry, Klass* klass, bool is_optimized) {
101261287Sdes    _entry      = entry;
102261287Sdes    _cached_value = (void*)klass;
103261287Sdes    _to_interpreter = false;
104261287Sdes    _is_icholder = false;
105261287Sdes    _is_optimized = is_optimized;
106261287Sdes    _release_icholder = false;
107261287Sdes  }
108261287Sdes
109261287Sdes  void set_interpreter_entry(address entry, Method* method) {
110261287Sdes    _entry      = entry;
111261287Sdes    _cached_value = (void*)method;
112261287Sdes    _to_interpreter = true;
113261287Sdes    _is_icholder = false;
114261287Sdes    _is_optimized = true;
115261287Sdes    _release_icholder = false;
116261287Sdes  }
117261287Sdes
118261287Sdes  void set_icholder_entry(address entry, CompiledICHolder* icholder) {
119261287Sdes    _entry      = entry;
120261287Sdes    _cached_value = (void*)icholder;
121261287Sdes    _to_interpreter = true;
122261287Sdes    _is_icholder = true;
123261287Sdes    _is_optimized = false;
124261287Sdes    _release_icholder = true;
125261287Sdes  }
126261287Sdes
127261287Sdes  CompiledICInfo(): _entry(NULL), _cached_value(NULL), _is_icholder(false),
128261287Sdes                    _to_interpreter(false), _is_optimized(false), _release_icholder(false) {
129261287Sdes  }
130261287Sdes  ~CompiledICInfo() {
131261287Sdes    // In rare cases the info is computed but not used, so release any
132261287Sdes    // CompiledICHolder* that was created
133261287Sdes    if (_release_icholder) {
134261287Sdes      assert(_is_icholder, "must be");
135261287Sdes      CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;
136261287Sdes      icholder->claim();
137261287Sdes      delete icholder;
138261287Sdes    }
139261287Sdes  }
140261287Sdes};
141261287Sdes
142261287Sdesclass CompiledIC: public ResourceObj {
143261287Sdes  friend class InlineCacheBuffer;
144261287Sdes  friend class ICStub;
145261287Sdes
146261287Sdes
147261287Sdes private:
148261287Sdes  NativeCall*   _ic_call;       // the call instruction
149261287Sdes  NativeMovConstReg* _value;    // patchable value cell for this IC
150261287Sdes  bool          _is_optimized;  // an optimized virtual call (i.e., no compiled IC)
151261287Sdes
152261287Sdes  CompiledIC(nmethod* nm, NativeCall* ic_call);
153261287Sdes
154261287Sdes  static bool is_icholder_entry(address entry);
155261287Sdes
156261287Sdes  // low-level inline-cache manipulation. Cannot be accessed directly, since it might not be MT-safe
157261287Sdes  // to change an inline-cache. These changes the underlying inline-cache directly. They *newer* make
158261287Sdes  // changes to a transition stub.
159261287Sdes  void internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder);
160261287Sdes  void set_ic_destination(ICStub* stub);
161261287Sdes  void set_ic_destination(address entry_point) {
162261287Sdes    assert(_is_optimized, "use set_ic_destination_and_value instead");
163261287Sdes    internal_set_ic_destination(entry_point, false, NULL, false);
164261287Sdes  }
165261287Sdes  // This only for use by ICStubs where the type of the value isn't known
166261287Sdes  void set_ic_destination_and_value(address entry_point, void* value) {
167261287Sdes    internal_set_ic_destination(entry_point, false, value, is_icholder_entry(entry_point));
168261287Sdes  }
169261287Sdes  void set_ic_destination_and_value(address entry_point, Metadata* value) {
170261287Sdes    internal_set_ic_destination(entry_point, false, value, false);
171261287Sdes  }
172261287Sdes  void set_ic_destination_and_value(address entry_point, CompiledICHolder* value) {
173261287Sdes    internal_set_ic_destination(entry_point, false, value, true);
174261287Sdes  }
175261287Sdes
176261287Sdes  // Reads the location of the transition stub. This will fail with an assertion, if no transition stub is
177261287Sdes  // associated with the inline cache.
178261287Sdes  address stub_address() const;
179261287Sdes  bool is_in_transition_state() const;  // Use InlineCacheBuffer
180261287Sdes
181261287Sdes public:
182261287Sdes  // conversion (machine PC to CompiledIC*)
183261287Sdes  friend CompiledIC* CompiledIC_before(nmethod* nm, address return_addr);
184261287Sdes  friend CompiledIC* CompiledIC_at(nmethod* nm, address call_site);
185261287Sdes  friend CompiledIC* CompiledIC_at(Relocation* call_site);
186261287Sdes
187261287Sdes  // This is used to release CompiledICHolder*s from nmethods that
188261287Sdes  // are about to be freed.  The callsite might contain other stale
189261287Sdes  // values of other kinds so it must be careful.
190261287Sdes  static void cleanup_call_site(virtual_call_Relocation* call_site);
191261287Sdes  static bool is_icholder_call_site(virtual_call_Relocation* call_site);
192261287Sdes
193261287Sdes  // Return the cached_metadata/destination associated with this inline cache. If the cache currently points
194261287Sdes  // to a transition stub, it will read the values from the transition stub.
195261287Sdes  void* cached_value() const;
196261287Sdes  CompiledICHolder* cached_icholder() const {
197261287Sdes    assert(is_icholder_call(), "must be");
198261287Sdes    return (CompiledICHolder*) cached_value();
199261287Sdes  }
200261287Sdes  Metadata* cached_metadata() const {
201261287Sdes    assert(!is_icholder_call(), "must be");
202261287Sdes    return (Metadata*) cached_value();
203261287Sdes  }
204261287Sdes
205261287Sdes  address ic_destination() const;
206261287Sdes
207261287Sdes  bool is_optimized() const   { return _is_optimized; }
208261287Sdes
209261287Sdes  // State
210261287Sdes  bool is_clean() const;
211261287Sdes  bool is_megamorphic() const;
212261287Sdes  bool is_call_to_compiled() const;
213261287Sdes  bool is_call_to_interpreted() const;
214261287Sdes
215261287Sdes  bool is_icholder_call() const;
216261287Sdes
217261287Sdes  address end_of_call() { return  _ic_call->return_address(); }
218261287Sdes
219261287Sdes  // MT-safe patching of inline caches. Note: Only safe to call is_xxx when holding the CompiledIC_ock
220261287Sdes  // so you are guaranteed that no patching takes place. The same goes for verify.
221261287Sdes  //
222261287Sdes  // Note: We do not provide any direct access to the stub code, to prevent parts of the code
223261287Sdes  // to manipulate the inline cache in MT-unsafe ways.
224261287Sdes  //
225261287Sdes  // They all takes a TRAP argument, since they can cause a GC if the inline-cache buffer is full.
226261287Sdes  //
227261287Sdes  void set_to_clean();  // Can only be called during a safepoint operation
228261287Sdes  void set_to_monomorphic(CompiledICInfo& info);
229261287Sdes
230261287Sdes  // Returns true if successful and false otherwise. The call can fail if memory
231261287Sdes  // allocation in the code cache fails.
232261287Sdes  bool set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS);
233261287Sdes
234261287Sdes  static void compute_monomorphic_entry(methodHandle method, KlassHandle receiver_klass,
235261287Sdes                                        bool is_optimized, bool static_bound, CompiledICInfo& info, TRAPS);
236261287Sdes
237261287Sdes  // Location
238261287Sdes  address instruction_address() const { return _ic_call->instruction_address(); }
239261287Sdes
240261287Sdes  // Misc
241261287Sdes  void print()             PRODUCT_RETURN;
242261287Sdes  void print_compiled_ic() PRODUCT_RETURN;
243261287Sdes  void verify()            PRODUCT_RETURN;
244261287Sdes};
245261287Sdes
246261287Sdesinline CompiledIC* CompiledIC_before(nmethod* nm, address return_addr) {
247261287Sdes  CompiledIC* c_ic = new CompiledIC(nm, nativeCall_before(return_addr));
248261287Sdes  c_ic->verify();
249261287Sdes  return c_ic;
250261287Sdes}
251261287Sdes
252261287Sdesinline CompiledIC* CompiledIC_at(nmethod* nm, address call_site) {
253261287Sdes  CompiledIC* c_ic = new CompiledIC(nm, nativeCall_at(call_site));
254261287Sdes  c_ic->verify();
255261287Sdes  return c_ic;
256261287Sdes}
257261287Sdes
258261287Sdesinline CompiledIC* CompiledIC_at(Relocation* call_site) {
259261287Sdes  assert(call_site->type() == relocInfo::virtual_call_type ||
260261287Sdes         call_site->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
261261287Sdes  CompiledIC* c_ic = new CompiledIC(call_site->code(), nativeCall_at(call_site->addr()));
262261287Sdes  c_ic->verify();
263261287Sdes  return c_ic;
264261287Sdes}
265261287Sdes
266261287Sdes
267261287Sdes//-----------------------------------------------------------------------------
268261287Sdes// The CompiledStaticCall represents a call to a static method in the compiled
269261287Sdes//
270261287Sdes// Transition diagram of a static call site is somewhat simpler than for an inlined cache:
271261287Sdes//
272261287Sdes//
273261287Sdes//           -----<----- Clean ----->-----
274261287Sdes//          /                             \
275261287Sdes//         /                               \
276261287Sdes//    compilled code <------------> interpreted code
277261287Sdes//
278261287Sdes//  Clean:            Calls directly to runtime method for fixup
279261287Sdes//  Compiled code:    Calls directly to compiled code
280261287Sdes//  Interpreted code: Calls to stub that set Method* reference
281261287Sdes//
282261287Sdes//
283261287Sdesclass CompiledStaticCall;
284261287Sdes
285261287Sdesclass StaticCallInfo {
286261287Sdes private:
287261287Sdes  address      _entry;          // Entrypoint
288261287Sdes  methodHandle _callee;         // Callee (used when calling interpreter)
289261287Sdes  bool         _to_interpreter; // call to interpreted method (otherwise compiled)
290261287Sdes
291261287Sdes  friend class CompiledStaticCall;
292261287Sdes public:
293261287Sdes  address      entry() const    { return _entry;  }
294261287Sdes  methodHandle callee() const   { return _callee; }
295261287Sdes};
296261287Sdes
297261287Sdes
298261287Sdesclass CompiledStaticCall: public NativeCall {
299261287Sdes  friend class CompiledIC;
300261287Sdes
301261287Sdes  // Also used by CompiledIC
302261287Sdes  void set_to_interpreted(methodHandle callee, address entry);
303261287Sdes  bool is_optimized_virtual();
304261287Sdes
305261287Sdes public:
306261287Sdes  friend CompiledStaticCall* compiledStaticCall_before(address return_addr);
307261287Sdes  friend CompiledStaticCall* compiledStaticCall_at(address native_call);
308261287Sdes  friend CompiledStaticCall* compiledStaticCall_at(Relocation* call_site);
309261287Sdes
310261287Sdes  // Code
311261287Sdes  static void emit_to_interp_stub(CodeBuffer &cbuf);
312261287Sdes  static int to_interp_stub_size();
313261287Sdes  static int reloc_to_interp_stub();
314261287Sdes
315261287Sdes  // State
316261287Sdes  bool is_clean() const;
317261287Sdes  bool is_call_to_compiled() const;
318261287Sdes  bool is_call_to_interpreted() const;
319261287Sdes
320261287Sdes  // Clean static call (will force resolving on next use)
321261287Sdes  void set_to_clean();
322
323  // Set state. The entry must be the same, as computed by compute_entry.
324  // Computation and setting is split up, since the actions are separate during
325  // a OptoRuntime::resolve_xxx.
326  void set(const StaticCallInfo& info);
327
328  // Compute entry point given a method
329  static void compute_entry(methodHandle m, StaticCallInfo& info);
330
331  // Stub support
332  address find_stub();
333  static void set_stub_to_clean(static_stub_Relocation* static_stub);
334
335  // Misc.
336  void print()  PRODUCT_RETURN;
337  void verify() PRODUCT_RETURN;
338};
339
340
341inline CompiledStaticCall* compiledStaticCall_before(address return_addr) {
342  CompiledStaticCall* st = (CompiledStaticCall*)nativeCall_before(return_addr);
343  st->verify();
344  return st;
345}
346
347inline CompiledStaticCall* compiledStaticCall_at(address native_call) {
348  CompiledStaticCall* st = (CompiledStaticCall*)native_call;
349  st->verify();
350  return st;
351}
352
353inline CompiledStaticCall* compiledStaticCall_at(Relocation* call_site) {
354  return compiledStaticCall_at(call_site->addr());
355}
356
357#endif // SHARE_VM_CODE_COMPILEDIC_HPP
358