- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathcompiledIC.cpp
453 lines (389 loc) · 16.3 KB
/
compiledIC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include"code/codeBehaviours.hpp"
#include"code/codeCache.hpp"
#include"code/compiledIC.hpp"
#include"code/nmethod.hpp"
#include"code/vtableStubs.hpp"
#include"memory/resourceArea.hpp"
#include"memory/universe.hpp"
#include"oops/compressedKlass.hpp"
#include"oops/klass.inline.hpp"
#include"oops/method.inline.hpp"
#include"runtime/atomic.hpp"
#include"runtime/continuationEntry.hpp"
#include"runtime/handles.inline.hpp"
#include"runtime/interfaceSupport.inline.hpp"
#include"runtime/sharedRuntime.hpp"
#include"sanitizers/leak.hpp"
// Every time a compiled IC is changed or its type is being accessed,
// either the CompiledIC_lock must be set or we must be at a safe point.
CompiledICLocker::CompiledICLocker(nmethod* method)
: _method(method),
_behaviour(CompiledICProtectionBehaviour::current()),
_locked(_behaviour->lock(_method)) {
}
CompiledICLocker::~CompiledICLocker() {
if (_locked) {
_behaviour->unlock(_method);
}
}
boolCompiledICLocker::is_safe(nmethod* method) {
returnCompiledICProtectionBehaviour::current()->is_safe(method);
}
boolCompiledICLocker::is_safe(address code) {
CodeBlob* cb = CodeCache::find_blob(code);
assert(cb != nullptr && cb->is_nmethod(), "must be compiled");
nmethod* nm = cb->as_nmethod();
returnCompiledICProtectionBehaviour::current()->is_safe(nm);
}
CompiledICData::CompiledICData()
: _speculated_method(),
_speculated_klass(),
_itable_defc_klass(),
_itable_refc_klass(),
_is_initialized() {}
// Inline cache callsite info is initialized once the first time it is resolved
voidCompiledICData::initialize(CallInfo* call_info, Klass* receiver_klass) {
_speculated_method = call_info->selected_method();
if (UseCompressedClassPointers) {
_speculated_klass = (uintptr_t)CompressedKlassPointers::encode_not_null(receiver_klass);
} else {
_speculated_klass = (uintptr_t)receiver_klass;
}
if (call_info->call_kind() == CallInfo::itable_call) {
assert(call_info->resolved_method() != nullptr, "virtual or interface method must be found");
_itable_defc_klass = call_info->resolved_method()->method_holder();
_itable_refc_klass = call_info->resolved_klass();
}
_is_initialized = true;
}
boolCompiledICData::is_speculated_klass_unloaded() const {
returnis_initialized() && _speculated_klass == 0;
}
voidCompiledICData::clean_metadata() {
if (!is_initialized() || is_speculated_klass_unloaded()) {
return;
}
// GC cleaning doesn't need to change the state of the inline cache,
// only nuke stale speculated metadata if it gets unloaded. If the
// inline cache is monomorphic, the unverified entries will miss, and
// subsequent miss handlers will upgrade the callsite to megamorphic,
// which makes sense as it obviously is megamorphic then.
if (!speculated_klass()->is_loader_alive()) {
Atomic::store(&_speculated_klass, (uintptr_t)0);
Atomic::store(&_speculated_method, (Method*)nullptr);
}
assert(_speculated_method == nullptr || _speculated_method->method_holder()->is_loader_alive(),
"Speculated method is not unloaded despite class being unloaded");
}
voidCompiledICData::metadata_do(MetadataClosure* cl) {
if (!is_initialized()) {
return;
}
if (!is_speculated_klass_unloaded()) {
cl->do_metadata(_speculated_method);
cl->do_metadata(speculated_klass());
}
if (_itable_refc_klass != nullptr) {
cl->do_metadata(_itable_refc_klass);
}
if (_itable_defc_klass != nullptr) {
cl->do_metadata(_itable_defc_klass);
}
}
Klass* CompiledICData::speculated_klass() const {
if (is_speculated_klass_unloaded()) {
returnnullptr;
}
if (UseCompressedClassPointers) {
returnCompressedKlassPointers::decode_not_null((narrowKlass)_speculated_klass);
} else {
return (Klass*)_speculated_klass;
}
}
//-----------------------------------------------------------------------------
// High-level access to an inline cache. Guaranteed to be MT-safe.
CompiledICData* CompiledIC::data() const {
return _data;
}
CompiledICData* data_from_reloc_iter(RelocIterator* iter) {
assert(iter->type() == relocInfo::virtual_call_type, "wrong reloc. info");
virtual_call_Relocation* r = iter->virtual_call_reloc();
NativeMovConstReg* value = nativeMovConstReg_at(r->cached_value());
return (CompiledICData*)value->data();
}
CompiledIC::CompiledIC(RelocIterator* iter)
: _method(iter->code()),
_data(data_from_reloc_iter(iter)),
_call(nativeCall_at(iter->addr()))
{
assert(_method != nullptr, "must pass compiled method");
assert(_method->contains(iter->addr()), "must be in compiled method");
assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
}
CompiledIC* CompiledIC_before(nmethod* nm, address return_addr) {
address call_site = nativeCall_before(return_addr)->instruction_address();
returnCompiledIC_at(nm, call_site);
}
CompiledIC* CompiledIC_at(nmethod* nm, address call_site) {
RelocIterator iter(nm, call_site, call_site + 1);
iter.next();
returnCompiledIC_at(&iter);
}
CompiledIC* CompiledIC_at(Relocation* call_reloc) {
address call_site = call_reloc->addr();
nmethod* nm = CodeCache::find_blob(call_reloc->addr())->as_nmethod();
returnCompiledIC_at(nm, call_site);
}
CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {
CompiledIC* c_ic = newCompiledIC(reloc_iter);
c_ic->verify();
return c_ic;
}
voidCompiledIC::ensure_initialized(CallInfo* call_info, Klass* receiver_klass) {
if (!_data->is_initialized()) {
_data->initialize(call_info, receiver_klass);
}
}
voidCompiledIC::set_to_clean() {
log_debug(inlinecache)("IC@" INTPTR_FORMAT ": set to clean", p2i(_call->instruction_address()));
_call->set_destination_mt_safe(SharedRuntime::get_resolve_virtual_call_stub());
}
voidCompiledIC::set_to_monomorphic() {
assert(data()->is_initialized(), "must be initialized");
Method* method = data()->speculated_method();
nmethod* code = method->code();
address entry;
bool to_compiled = code != nullptr && code->is_in_use() && !code->is_unloading();
if (to_compiled) {
entry = code->entry_point();
} else {
entry = method->get_c2i_unverified_entry();
}
log_trace(inlinecache)("IC@" INTPTR_FORMAT ": monomorphic to %s: %s",
p2i(_call->instruction_address()),
to_compiled ? "compiled" : "interpreter",
method->print_value_string());
_call->set_destination_mt_safe(entry);
}
voidCompiledIC::set_to_megamorphic(CallInfo* call_info) {
assert(data()->is_initialized(), "must be initialized");
address entry;
if (call_info->call_kind() == CallInfo::direct_call) {
// C1 sometimes compiles a callsite before the target method is loaded, resulting in
// dynamically bound callsites that should really be statically bound. However, the
// target method might not have a vtable or itable. We just wait for better code to arrive
return;
} elseif (call_info->call_kind() == CallInfo::itable_call) {
int itable_index = call_info->itable_index();
entry = VtableStubs::find_itable_stub(itable_index);
if (entry == nullptr) {
return;
}
#ifdef ASSERT
assert(call_info->resolved_method() != nullptr, "virtual or interface method must be found");
intindex = call_info->resolved_method()->itable_index();
assert(index == itable_index, "CallInfo pre-computes this");
InstanceKlass* k = call_info->resolved_method()->method_holder();
assert(k->verify_itable_index(itable_index), "sanity check");
#endif//ASSERT
} else {
assert(call_info->call_kind() == CallInfo::vtable_call, "what else?");
// Can be different than selected_method->vtable_index(), due to package-private etc.
int vtable_index = call_info->vtable_index();
assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
entry = VtableStubs::find_vtable_stub(vtable_index);
if (entry == nullptr) {
return;
}
}
assert(call_info->selected_method() != nullptr, "virtual or interface method must be found");
log_trace(inlinecache)("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
p2i(_call->instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
_call->set_destination_mt_safe(entry);
assert(is_megamorphic(), "sanity check");
}
voidCompiledIC::update(CallInfo* call_info, Klass* receiver_klass) {
// If this is the first time we fix the inline cache, we ensure it's initialized
ensure_initialized(call_info, receiver_klass);
if (is_megamorphic()) {
// Terminal state for the inline cache
return;
}
if (is_speculated_klass(receiver_klass)) {
// If the speculated class matches the receiver klass, we can speculate that will
// continue to be the case with a monomorphic inline cache
set_to_monomorphic();
} else {
// If the dynamic type speculation fails, we try to transform to a megamorphic state
// for the inline cache using stubs to dispatch in tables
set_to_megamorphic(call_info);
}
}
boolCompiledIC::is_clean() const {
returndestination() == SharedRuntime::get_resolve_virtual_call_stub();
}
boolCompiledIC::is_monomorphic() const {
return !is_clean() && !is_megamorphic();
}
boolCompiledIC::is_megamorphic() const {
returnVtableStubs::entry_point(destination()) != nullptr;
}
boolCompiledIC::is_speculated_klass(Klass* receiver_klass) {
returndata()->speculated_klass() == receiver_klass;
}
// GC support
voidCompiledIC::clean_metadata() {
data()->clean_metadata();
}
voidCompiledIC::metadata_do(MetadataClosure* cl) {
data()->metadata_do(cl);
}
#ifndef PRODUCT
voidCompiledIC::print() {
tty->print("Inline cache at " INTPTR_FORMAT ", calling " INTPTR_FORMAT " cached_value " INTPTR_FORMAT,
p2i(instruction_address()), p2i(destination()), p2i(data()));
tty->cr();
}
voidCompiledIC::verify() {
_call->verify();
}
#endif
// ----------------------------------------------------------------------------
voidCompiledDirectCall::set_to_clean() {
// in_use is unused but needed to match template function in nmethod
assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
// Reset call site
RelocIterator iter((nmethod*)nullptr, instruction_address(), instruction_address() + 1);
while (iter.next()) {
switch(iter.type()) {
case relocInfo::static_call_type:
_call->set_destination_mt_safe(SharedRuntime::get_resolve_static_call_stub());
break;
case relocInfo::opt_virtual_call_type:
_call->set_destination_mt_safe(SharedRuntime::get_resolve_opt_virtual_call_stub());
break;
default:
ShouldNotReachHere();
}
}
assert(is_clean(), "should be clean after cleaning");
log_debug(inlinecache)("DC@" INTPTR_FORMAT ": set to clean", p2i(_call->instruction_address()));
}
voidCompiledDirectCall::set(const methodHandle& callee_method) {
nmethod* code = callee_method->code();
nmethod* caller = CodeCache::find_nmethod(instruction_address());
assert(caller != nullptr, "did not find caller nmethod");
bool to_interp_cont_enter = caller->method()->is_continuation_enter_intrinsic() &&
ContinuationEntry::is_interpreted_call(instruction_address());
bool to_compiled = !to_interp_cont_enter && code != nullptr && code->is_in_use() && !code->is_unloading();
if (to_compiled) {
_call->set_destination_mt_safe(code->verified_entry_point());
assert(is_call_to_compiled(), "should be compiled after set to compiled");
} else {
// Patch call site to C2I adapter if code is deoptimized or unloaded.
// We also need to patch the static call stub to set the rmethod register
// to the callee_method so the c2i adapter knows how to build the frame
set_to_interpreted(callee_method, callee_method->get_c2i_entry());
assert(is_call_to_interpreted(), "should be interpreted after set to interpreted");
}
log_trace(inlinecache)("DC@" INTPTR_FORMAT ": set to %s: %s: " INTPTR_FORMAT,
p2i(_call->instruction_address()),
to_compiled ? "compiled" : "interpreter",
callee_method->print_value_string(),
p2i(_call->destination()));
}
boolCompiledDirectCall::is_clean() const {
returndestination() == SharedRuntime::get_resolve_static_call_stub() ||
destination() == SharedRuntime::get_resolve_opt_virtual_call_stub();
}
boolCompiledDirectCall::is_call_to_interpreted() const {
// It is a call to interpreted, if it calls to a stub. Hence, the destination
// must be in the stub part of the nmethod that contains the call
nmethod* nm = CodeCache::find_nmethod(instruction_address());
assert(nm != nullptr, "did not find nmethod");
return nm->stub_contains(destination());
}
boolCompiledDirectCall::is_call_to_compiled() const {
nmethod* caller = CodeCache::find_nmethod(instruction_address());
assert(caller != nullptr, "did not find caller nmethod");
CodeBlob* dest_cb = CodeCache::find_blob(destination());
return !caller->stub_contains(destination()) && dest_cb->is_nmethod();
}
address CompiledDirectCall::find_stub_for(address instruction) {
// Find reloc. information containing this call-site
RelocIterator iter((nmethod*)nullptr, instruction);
while (iter.next()) {
if (iter.addr() == instruction) {
switch(iter.type()) {
case relocInfo::static_call_type:
return iter.static_call_reloc()->static_stub();
// We check here for opt_virtual_call_type, since we reuse the code
// from the CompiledIC implementation
case relocInfo::opt_virtual_call_type:
return iter.opt_virtual_call_reloc()->static_stub();
default:
ShouldNotReachHere();
}
}
}
returnnullptr;
}
address CompiledDirectCall::find_stub() {
returnfind_stub_for(instruction_address());
}
#ifndef PRODUCT
voidCompiledDirectCall::print() {
tty->print("direct call at " INTPTR_FORMAT " to " INTPTR_FORMAT " -> ", p2i(instruction_address()), p2i(destination()));
if (is_clean()) {
tty->print("clean");
} elseif (is_call_to_compiled()) {
tty->print("compiled");
} elseif (is_call_to_interpreted()) {
tty->print("interpreted");
}
tty->cr();
}
voidCompiledDirectCall::verify_mt_safe(const methodHandle& callee, address entry,
NativeMovConstReg* method_holder,
NativeJump* jump) {
_call->verify();
// A generated lambda form might be deleted from the Lambdaform
// cache in MethodTypeForm. If a jit compiled lambdaform method
// becomes not entrant and the cache access returns null, the new
// resolve will lead to a new generated LambdaForm.
Method* old_method = reinterpret_cast<Method*>(method_holder->data());
assert(old_method == nullptr || old_method == callee() ||
callee->is_compiled_lambda_form() ||
!old_method->method_holder()->is_loader_alive() ||
old_method->is_old(), // may be race patching deoptimized nmethod due to redefinition.
"a) MT-unsafe modification of inline cache");
address destination = jump->jump_destination();
assert(destination == (address)-1 || destination == entry
|| old_method == nullptr || !old_method->method_holder()->is_loader_alive() // may have a race due to class unloading.
|| old_method->is_old(), // may be race patching deoptimized nmethod due to redefinition.
"b) MT-unsafe modification of inline cache");
}
#endif