Thu, 12 Oct 2017 21:27:07 +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(); // Can only be called during a safepoint operation
241 void set_to_monomorphic(CompiledICInfo& info);
243 // Returns true if successful and false otherwise. The call can fail if memory
244 // allocation in the code cache fails.
245 bool set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS);
247 static void compute_monomorphic_entry(methodHandle method, KlassHandle receiver_klass,
248 bool is_optimized, bool static_bound, CompiledICInfo& info, TRAPS);
250 // Location
251 address instruction_address() const { return _ic_call->instruction_address(); }
253 // Misc
254 void print() PRODUCT_RETURN;
255 void print_compiled_ic() PRODUCT_RETURN;
256 void verify() PRODUCT_RETURN;
257 };
259 inline CompiledIC* CompiledIC_before(nmethod* nm, address return_addr) {
260 CompiledIC* c_ic = new CompiledIC(nm, nativeCall_before(return_addr));
261 c_ic->verify();
262 return c_ic;
263 }
265 inline CompiledIC* CompiledIC_at(nmethod* nm, address call_site) {
266 CompiledIC* c_ic = new CompiledIC(nm, nativeCall_at(call_site));
267 c_ic->verify();
268 return c_ic;
269 }
271 inline CompiledIC* CompiledIC_at(Relocation* call_site) {
272 assert(call_site->type() == relocInfo::virtual_call_type ||
273 call_site->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
274 CompiledIC* c_ic = new CompiledIC(call_site->code(), nativeCall_at(call_site->addr()));
275 c_ic->verify();
276 return c_ic;
277 }
279 inline CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {
280 assert(reloc_iter->type() == relocInfo::virtual_call_type ||
281 reloc_iter->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info");
282 CompiledIC* c_ic = new CompiledIC(reloc_iter);
283 c_ic->verify();
284 return c_ic;
285 }
287 //-----------------------------------------------------------------------------
288 // The CompiledStaticCall represents a call to a static method in the compiled
289 //
290 // Transition diagram of a static call site is somewhat simpler than for an inlined cache:
291 //
292 //
293 // -----<----- Clean ----->-----
294 // / \
295 // / \
296 // compilled code <------------> interpreted code
297 //
298 // Clean: Calls directly to runtime method for fixup
299 // Compiled code: Calls directly to compiled code
300 // Interpreted code: Calls to stub that set Method* reference
301 //
302 //
303 class CompiledStaticCall;
305 class StaticCallInfo {
306 private:
307 address _entry; // Entrypoint
308 methodHandle _callee; // Callee (used when calling interpreter)
309 bool _to_interpreter; // call to interpreted method (otherwise compiled)
311 friend class CompiledStaticCall;
312 public:
313 address entry() const { return _entry; }
314 methodHandle callee() const { return _callee; }
315 };
318 class CompiledStaticCall: public NativeCall {
319 friend class CompiledIC;
321 // Also used by CompiledIC
322 void set_to_interpreted(methodHandle callee, address entry);
323 bool is_optimized_virtual();
325 public:
326 friend CompiledStaticCall* compiledStaticCall_before(address return_addr);
327 friend CompiledStaticCall* compiledStaticCall_at(address native_call);
328 friend CompiledStaticCall* compiledStaticCall_at(Relocation* call_site);
330 // Code
331 static void emit_to_interp_stub(CodeBuffer &cbuf);
332 static int to_interp_stub_size();
333 static int reloc_to_interp_stub();
335 // State
336 bool is_clean() const;
337 bool is_call_to_compiled() const;
338 bool is_call_to_interpreted() const;
340 // Clean static call (will force resolving on next use)
341 void set_to_clean();
343 // Set state. The entry must be the same, as computed by compute_entry.
344 // Computation and setting is split up, since the actions are separate during
345 // a OptoRuntime::resolve_xxx.
346 void set(const StaticCallInfo& info);
348 // Compute entry point given a method
349 static void compute_entry(methodHandle m, StaticCallInfo& info);
351 // Stub support
352 address find_stub();
353 static void set_stub_to_clean(static_stub_Relocation* static_stub);
355 // Misc.
356 void print() PRODUCT_RETURN;
357 void verify() PRODUCT_RETURN;
358 };
361 inline CompiledStaticCall* compiledStaticCall_before(address return_addr) {
362 CompiledStaticCall* st = (CompiledStaticCall*)nativeCall_before(return_addr);
363 st->verify();
364 return st;
365 }
367 inline CompiledStaticCall* compiledStaticCall_at(address native_call) {
368 CompiledStaticCall* st = (CompiledStaticCall*)native_call;
369 st->verify();
370 return st;
371 }
373 inline CompiledStaticCall* compiledStaticCall_at(Relocation* call_site) {
374 return compiledStaticCall_at(call_site->addr());
375 }
377 #endif // SHARE_VM_CODE_COMPILEDIC_HPP