Thu, 24 May 2018 18:41:44 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2013, 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 */
25 /*
26 * This file has been modified by Loongson Technology in 2015. These
27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
28 * available on the same license terms set forth above.
29 */
31 #ifndef SHARE_VM_CODE_COMPILEDIC_HPP
32 #define SHARE_VM_CODE_COMPILEDIC_HPP
34 #include "interpreter/linkResolver.hpp"
35 #include "oops/compiledICHolder.hpp"
36 #ifdef TARGET_ARCH_x86
37 # include "nativeInst_x86.hpp"
38 #endif
39 #ifdef TARGET_ARCH_sparc
40 # include "nativeInst_sparc.hpp"
41 #endif
42 #ifdef TARGET_ARCH_zero
43 # include "nativeInst_zero.hpp"
44 #endif
45 #ifdef TARGET_ARCH_arm
46 # include "nativeInst_arm.hpp"
47 #endif
48 #ifdef TARGET_ARCH_ppc
49 # include "nativeInst_ppc.hpp"
50 #endif
51 #ifdef TARGET_ARCH_mips
52 # include "nativeInst_mips.hpp"
53 #endif
55 //-----------------------------------------------------------------------------
56 // The CompiledIC represents a compiled inline cache.
57 //
58 // In order to make patching of the inline cache MT-safe, we only allow the following
59 // transitions (when not at a safepoint):
60 //
61 //
62 // [1] --<-- Clean -->--- [1]
63 // / (null) \
64 // / \ /-<-\
65 // / [2] \ / \
66 // Interpreted ---------> Monomorphic | [3]
67 // (CompiledICHolder*) (Klass*) |
68 // \ / \ /
69 // [4] \ / [4] \->-/
70 // \->- Megamorphic -<-/
71 // (Method*)
72 //
73 // The text in paranteses () refere to the value of the inline cache receiver (mov instruction)
74 //
75 // The numbers in square brackets refere to the kind of transition:
76 // [1]: Initial fixup. Receiver it found from debug information
77 // [2]: Compilation of a method
78 // [3]: Recompilation of a method (note: only entry is changed. The Klass* must stay the same)
79 // [4]: Inline cache miss. We go directly to megamorphic call.
80 //
81 // The class automatically inserts transition stubs (using the InlineCacheBuffer) when an MT-unsafe
82 // transition is made to a stub.
83 //
84 class CompiledIC;
85 class ICStub;
87 class CompiledICInfo : public StackObj {
88 private:
89 address _entry; // entry point for call
90 void* _cached_value; // Value of cached_value (either in stub or inline cache)
91 bool _is_icholder; // Is the cached value a CompiledICHolder*
92 bool _is_optimized; // it is an optimized virtual call (i.e., can be statically bound)
93 bool _to_interpreter; // Call it to interpreter
94 bool _release_icholder;
95 public:
96 address entry() const { return _entry; }
97 Metadata* cached_metadata() const { assert(!_is_icholder, ""); return (Metadata*)_cached_value; }
98 CompiledICHolder* claim_cached_icholder() {
99 assert(_is_icholder, "");
100 assert(_cached_value != NULL, "must be non-NULL");
101 _release_icholder = false;
102 CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;
103 icholder->claim();
104 return icholder;
105 }
106 bool is_optimized() const { return _is_optimized; }
107 bool to_interpreter() const { return _to_interpreter; }
109 void set_compiled_entry(address entry, Klass* klass, bool is_optimized) {
110 _entry = entry;
111 _cached_value = (void*)klass;
112 _to_interpreter = false;
113 _is_icholder = false;
114 _is_optimized = is_optimized;
115 _release_icholder = false;
116 }
118 void set_interpreter_entry(address entry, Method* method) {
119 _entry = entry;
120 _cached_value = (void*)method;
121 _to_interpreter = true;
122 _is_icholder = false;
123 _is_optimized = true;
124 _release_icholder = false;
125 }
127 void set_icholder_entry(address entry, CompiledICHolder* icholder) {
128 _entry = entry;
129 _cached_value = (void*)icholder;
130 _to_interpreter = true;
131 _is_icholder = true;
132 _is_optimized = false;
133 _release_icholder = true;
134 }
136 CompiledICInfo(): _entry(NULL), _cached_value(NULL), _is_icholder(false),
137 _to_interpreter(false), _is_optimized(false), _release_icholder(false) {
138 }
139 ~CompiledICInfo() {
140 // In rare cases the info is computed but not used, so release any
141 // CompiledICHolder* that was created
142 if (_release_icholder) {
143 assert(_is_icholder, "must be");
144 CompiledICHolder* icholder = (CompiledICHolder*)_cached_value;
145 icholder->claim();
146 delete icholder;
147 }
148 }
149 };
151 class CompiledIC: public ResourceObj {
152 friend class InlineCacheBuffer;
153 friend class ICStub;
156 private:
157 NativeCall* _ic_call; // the call instruction
158 NativeMovConstReg* _value; // patchable value cell for this IC
159 bool _is_optimized; // an optimized virtual call (i.e., no compiled IC)
161 CompiledIC(nmethod* nm, NativeCall* ic_call);
162 CompiledIC(RelocIterator* iter);
164 void initialize_from_iter(RelocIterator* iter);
166 static bool is_icholder_entry(address entry);
168 // low-level inline-cache manipulation. Cannot be accessed directly, since it might not be MT-safe
169 // to change an inline-cache. These changes the underlying inline-cache directly. They *newer* make
170 // changes to a transition stub.
171 void internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder);
172 void set_ic_destination(ICStub* stub);
173 void set_ic_destination(address entry_point) {
174 assert(_is_optimized, "use set_ic_destination_and_value instead");
175 internal_set_ic_destination(entry_point, false, NULL, false);
176 }
177 // This only for use by ICStubs where the type of the value isn't known
178 void set_ic_destination_and_value(address entry_point, void* value) {
179 internal_set_ic_destination(entry_point, false, value, is_icholder_entry(entry_point));
180 }
181 void set_ic_destination_and_value(address entry_point, Metadata* value) {
182 internal_set_ic_destination(entry_point, false, value, false);
183 }
184 void set_ic_destination_and_value(address entry_point, CompiledICHolder* value) {
185 internal_set_ic_destination(entry_point, false, value, true);
186 }
188 // Reads the location of the transition stub. This will fail with an assertion, if no transition stub is
189 // associated with the inline cache.
190 address stub_address() const;
191 bool is_in_transition_state() const; // Use InlineCacheBuffer
193 public:
194 // conversion (machine PC to CompiledIC*)
195 friend CompiledIC* CompiledIC_before(nmethod* nm, address return_addr);
196 friend CompiledIC* CompiledIC_at(nmethod* nm, address call_site);
197 friend CompiledIC* CompiledIC_at(Relocation* call_site);
198 friend CompiledIC* CompiledIC_at(RelocIterator* reloc_iter);
200 // This is used to release CompiledICHolder*s from nmethods that
201 // are about to be freed. The callsite might contain other stale
202 // values of other kinds so it must be careful.
203 static void cleanup_call_site(virtual_call_Relocation* call_site);
204 static bool is_icholder_call_site(virtual_call_Relocation* call_site);
206 // Return the cached_metadata/destination associated with this inline cache. If the cache currently points
207 // to a transition stub, it will read the values from the transition stub.
208 void* cached_value() const;
209 CompiledICHolder* cached_icholder() const {
210 assert(is_icholder_call(), "must be");
211 return (CompiledICHolder*) cached_value();
212 }
213 Metadata* cached_metadata() const {
214 assert(!is_icholder_call(), "must be");
215 return (Metadata*) cached_value();
216 }
218 address ic_destination() const;
220 bool is_optimized() const { return _is_optimized; }
222 // State
223 bool is_clean() const;
224 bool is_megamorphic() const;
225 bool is_call_to_compiled() const;
226 bool is_call_to_interpreted() const;
228 bool is_icholder_call() const;
230 address end_of_call() { return _ic_call->return_address(); }
232 // MT-safe patching of inline caches. Note: Only safe to call is_xxx when holding the CompiledIC_ock
233 // so you are guaranteed that no patching takes place. The same goes for verify.
234 //
235 // Note: We do not provide any direct access to the stub code, to prevent parts of the code
236 // to manipulate the inline cache in MT-unsafe ways.
237 //
238 // They all takes a TRAP argument, since they can cause a GC if the inline-cache buffer is full.
239 //
240 void set_to_clean(bool in_use = true);
241 void set_to_monomorphic(CompiledICInfo& info);
242 void clear_ic_stub();
244 // Returns true if successful and false otherwise. The call can fail if memory
245 // allocation in the code cache fails.
246 bool set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS);
248 static void compute_monomorphic_entry(methodHandle method, KlassHandle receiver_klass,
249 bool is_optimized, bool static_bound, CompiledICInfo& info, TRAPS);
251 // Location
252 address instruction_address() const { return _ic_call->instruction_address(); }
254 // Misc
255 void print() PRODUCT_RETURN;
256 void print_compiled_ic() PRODUCT_RETURN;
257 void verify() PRODUCT_RETURN;
258 };
260 inline CompiledIC* CompiledIC_before(nmethod* nm, address return_addr) {
261 CompiledIC* c_ic = new CompiledIC(nm, nativeCall_before(return_addr));
262 c_ic->verify();
263 return c_ic;
264 }
266 inline CompiledIC* CompiledIC_at(nmethod* nm, address call_site) {
267 CompiledIC* c_ic = new CompiledIC(nm, nativeCall_at(call_site));
268 c_ic->verify();
269 return c_ic;
270 }
272 inline CompiledIC* CompiledIC_at(Relocation* call_site) {
273 assert(call_site->type() == relocInfo::virtual_call_type ||
274 call_site->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
275 CompiledIC* c_ic = new CompiledIC(call_site->code(), nativeCall_at(call_site->addr()));
276 c_ic->verify();
277 return c_ic;
278 }
280 inline CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {
281 assert(reloc_iter->type() == relocInfo::virtual_call_type ||
282 reloc_iter->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
283 CompiledIC* c_ic = new CompiledIC(reloc_iter);
284 c_ic->verify();
285 return c_ic;
286 }
288 //-----------------------------------------------------------------------------
289 // The CompiledStaticCall represents a call to a static method in the compiled
290 //
291 // Transition diagram of a static call site is somewhat simpler than for an inlined cache:
292 //
293 //
294 // -----<----- Clean ----->-----
295 // / \
296 // / \
297 // compilled code <------------> interpreted code
298 //
299 // Clean: Calls directly to runtime method for fixup
300 // Compiled code: Calls directly to compiled code
301 // Interpreted code: Calls to stub that set Method* reference
302 //
303 //
304 class CompiledStaticCall;
306 class StaticCallInfo {
307 private:
308 address _entry; // Entrypoint
309 methodHandle _callee; // Callee (used when calling interpreter)
310 bool _to_interpreter; // call to interpreted method (otherwise compiled)
312 friend class CompiledStaticCall;
313 public:
314 address entry() const { return _entry; }
315 methodHandle callee() const { return _callee; }
316 };
319 class CompiledStaticCall: public NativeCall {
320 friend class CompiledIC;
322 // Also used by CompiledIC
323 void set_to_interpreted(methodHandle callee, address entry);
324 bool is_optimized_virtual();
326 public:
327 friend CompiledStaticCall* compiledStaticCall_before(address return_addr);
328 friend CompiledStaticCall* compiledStaticCall_at(address native_call);
329 friend CompiledStaticCall* compiledStaticCall_at(Relocation* call_site);
331 // Code
332 static address emit_to_interp_stub(CodeBuffer &cbuf);
333 static int to_interp_stub_size();
334 static int reloc_to_interp_stub();
336 // State
337 bool is_clean() const;
338 bool is_call_to_compiled() const;
339 bool is_call_to_interpreted() const;
341 // Clean static call (will force resolving on next use)
342 void set_to_clean();
344 // Set state. The entry must be the same, as computed by compute_entry.
345 // Computation and setting is split up, since the actions are separate during
346 // a OptoRuntime::resolve_xxx.
347 void set(const StaticCallInfo& info);
349 // Compute entry point given a method
350 static void compute_entry(methodHandle m, StaticCallInfo& info);
352 // Stub support
353 address find_stub();
354 static void set_stub_to_clean(static_stub_Relocation* static_stub);
356 // Misc.
357 void print() PRODUCT_RETURN;
358 void verify() PRODUCT_RETURN;
359 };
362 inline CompiledStaticCall* compiledStaticCall_before(address return_addr) {
363 CompiledStaticCall* st = (CompiledStaticCall*)nativeCall_before(return_addr);
364 st->verify();
365 return st;
366 }
368 inline CompiledStaticCall* compiledStaticCall_at(address native_call) {
369 CompiledStaticCall* st = (CompiledStaticCall*)native_call;
370 st->verify();
371 return st;
372 }
374 inline CompiledStaticCall* compiledStaticCall_at(Relocation* call_site) {
375 return compiledStaticCall_at(call_site->addr());
376 }
378 #endif // SHARE_VM_CODE_COMPILEDIC_HPP