Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/code/dependencies.hpp
64440 views
1
/*
2
* Copyright (c) 2005, 2021, 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
*/
24
25
#ifndef SHARE_CODE_DEPENDENCIES_HPP
26
#define SHARE_CODE_DEPENDENCIES_HPP
27
28
#include "ci/ciCallSite.hpp"
29
#include "ci/ciKlass.hpp"
30
#include "ci/ciMethod.hpp"
31
#include "ci/ciMethodHandle.hpp"
32
#include "code/compressedStream.hpp"
33
#include "code/nmethod.hpp"
34
#include "memory/resourceArea.hpp"
35
#include "runtime/safepointVerifiers.hpp"
36
#include "utilities/growableArray.hpp"
37
#include "utilities/hashtable.hpp"
38
39
//** Dependencies represent assertions (approximate invariants) within
40
// the runtime system, e.g. class hierarchy changes. An example is an
41
// assertion that a given method is not overridden; another example is
42
// that a type has only one concrete subtype. Compiled code which
43
// relies on such assertions must be discarded if they are overturned
44
// by changes in the runtime system. We can think of these assertions
45
// as approximate invariants, because we expect them to be overturned
46
// very infrequently. We are willing to perform expensive recovery
47
// operations when they are overturned. The benefit, of course, is
48
// performing optimistic optimizations (!) on the object code.
49
//
50
// Changes in the class hierarchy due to dynamic linking or
51
// class evolution can violate dependencies. There is enough
52
// indexing between classes and nmethods to make dependency
53
// checking reasonably efficient.
54
55
class ciEnv;
56
class nmethod;
57
class OopRecorder;
58
class xmlStream;
59
class CompileLog;
60
class CompileTask;
61
class DepChange;
62
class KlassDepChange;
63
class NewKlassDepChange;
64
class KlassInitDepChange;
65
class CallSiteDepChange;
66
class NoSafepointVerifier;
67
68
class Dependencies: public ResourceObj {
69
public:
70
// Note: In the comments on dependency types, most uses of the terms
71
// subtype and supertype are used in a "non-strict" or "inclusive"
72
// sense, and are starred to remind the reader of this fact.
73
// Strict uses of the terms use the word "proper".
74
//
75
// Specifically, every class is its own subtype* and supertype*.
76
// (This trick is easier than continually saying things like "Y is a
77
// subtype of X or X itself".)
78
//
79
// Sometimes we write X > Y to mean X is a proper supertype of Y.
80
// The notation X > {Y, Z} means X has proper subtypes Y, Z.
81
// The notation X.m > Y means that Y inherits m from X, while
82
// X.m > Y.m means Y overrides X.m. A star denotes abstractness,
83
// as *I > A, meaning (abstract) interface I is a super type of A,
84
// or A.*m > B.m, meaning B.m implements abstract method A.m.
85
//
86
// In this module, the terms "subtype" and "supertype" refer to
87
// Java-level reference type conversions, as detected by
88
// "instanceof" and performed by "checkcast" operations. The method
89
// Klass::is_subtype_of tests these relations. Note that "subtype"
90
// is richer than "subclass" (as tested by Klass::is_subclass_of),
91
// since it takes account of relations involving interface and array
92
// types.
93
//
94
// To avoid needless complexity, dependencies involving array types
95
// are not accepted. If you need to make an assertion about an
96
// array type, make the assertion about its corresponding element
97
// types. Any assertion that might change about an array type can
98
// be converted to an assertion about its element type.
99
//
100
// Most dependencies are evaluated over a "context type" CX, which
101
// stands for the set Subtypes(CX) of every Java type that is a subtype*
102
// of CX. When the system loads a new class or interface N, it is
103
// responsible for re-evaluating changed dependencies whose context
104
// type now includes N, that is, all super types of N.
105
//
106
enum DepType {
107
end_marker = 0,
108
109
// An 'evol' dependency simply notes that the contents of the
110
// method were used. If it evolves (is replaced), the nmethod
111
// must be recompiled. No other dependencies are implied.
112
evol_method,
113
FIRST_TYPE = evol_method,
114
115
// A context type CX is a leaf it if has no proper subtype.
116
leaf_type,
117
118
// An abstract class CX has exactly one concrete subtype CC.
119
abstract_with_unique_concrete_subtype,
120
121
// Given a method M1 and a context class CX, the set MM(CX, M1) of
122
// "concrete matching methods" in CX of M1 is the set of every
123
// concrete M2 for which it is possible to create an invokevirtual
124
// or invokeinterface call site that can reach either M1 or M2.
125
// That is, M1 and M2 share a name, signature, and vtable index.
126
// We wish to notice when the set MM(CX, M1) is just {M1}, or
127
// perhaps a set of two {M1,M2}, and issue dependencies on this.
128
129
// The set MM(CX, M1) can be computed by starting with any matching
130
// concrete M2 that is inherited into CX, and then walking the
131
// subtypes* of CX looking for concrete definitions.
132
133
// The parameters to this dependency are the method M1 and the
134
// context class CX. M1 must be either inherited in CX or defined
135
// in a subtype* of CX. It asserts that MM(CX, M1) is no greater
136
// than {M1}.
137
unique_concrete_method_2, // one unique concrete method under CX
138
139
// In addition to the method M1 and the context class CX, the parameters
140
// to this dependency are the resolved class RC1 and the
141
// resolved method RM1. It asserts that MM(CX, M1, RC1, RM1)
142
// is no greater than {M1}. RC1 and RM1 are used to improve the precision
143
// of the analysis.
144
unique_concrete_method_4, // one unique concrete method under CX
145
146
// This dependency asserts that interface CX has a unique implementor class.
147
unique_implementor, // one unique implementor under CX
148
149
// This dependency asserts that no instances of class or it's
150
// subclasses require finalization registration.
151
no_finalizable_subclasses,
152
153
// This dependency asserts when the CallSite.target value changed.
154
call_site_target_value,
155
156
TYPE_LIMIT
157
};
158
enum {
159
LG2_TYPE_LIMIT = 4, // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))
160
161
// handy categorizations of dependency types:
162
all_types = ((1 << TYPE_LIMIT) - 1) & ((~0u) << FIRST_TYPE),
163
164
non_klass_types = (1 << call_site_target_value),
165
klass_types = all_types & ~non_klass_types,
166
167
non_ctxk_types = (1 << evol_method) | (1 << call_site_target_value),
168
implicit_ctxk_types = 0,
169
explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),
170
171
max_arg_count = 4, // current maximum number of arguments (incl. ctxk)
172
173
// A "context type" is a class or interface that
174
// provides context for evaluating a dependency.
175
// When present, it is one of the arguments (dep_context_arg).
176
//
177
// If a dependency does not have a context type, there is a
178
// default context, depending on the type of the dependency.
179
// This bit signals that a default context has been compressed away.
180
default_context_type_bit = (1<<LG2_TYPE_LIMIT)
181
};
182
183
static const char* dep_name(DepType dept);
184
static int dep_args(DepType dept);
185
186
static bool is_klass_type( DepType dept) { return dept_in_mask(dept, klass_types ); }
187
188
static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }
189
static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }
190
191
static int dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }
192
static int dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }
193
194
static void check_valid_dependency_type(DepType dept);
195
196
#if INCLUDE_JVMCI
197
// A Metadata* or object value recorded in an OopRecorder
198
class DepValue {
199
private:
200
// Unique identifier of the value within the associated OopRecorder that
201
// encodes both the category of the value (0: invalid, positive: metadata, negative: object)
202
// and the index within a category specific array (metadata: index + 1, object: -(index + 1))
203
int _id;
204
205
public:
206
DepValue() : _id(0) {}
207
DepValue(OopRecorder* rec, Metadata* metadata, DepValue* candidate = NULL) {
208
assert(candidate == NULL || candidate->is_metadata(), "oops");
209
if (candidate != NULL && candidate->as_metadata(rec) == metadata) {
210
_id = candidate->_id;
211
} else {
212
_id = rec->find_index(metadata) + 1;
213
}
214
}
215
DepValue(OopRecorder* rec, jobject obj, DepValue* candidate = NULL) {
216
assert(candidate == NULL || candidate->is_object(), "oops");
217
if (candidate != NULL && candidate->as_object(rec) == obj) {
218
_id = candidate->_id;
219
} else {
220
_id = -(rec->find_index(obj) + 1);
221
}
222
}
223
224
// Used to sort values in ascending order of index() with metadata values preceding object values
225
int sort_key() const { return -_id; }
226
227
bool operator == (const DepValue& other) const { return other._id == _id; }
228
229
bool is_valid() const { return _id != 0; }
230
int index() const { assert(is_valid(), "oops"); return _id < 0 ? -(_id + 1) : _id - 1; }
231
bool is_metadata() const { assert(is_valid(), "oops"); return _id > 0; }
232
bool is_object() const { assert(is_valid(), "oops"); return _id < 0; }
233
234
Metadata* as_metadata(OopRecorder* rec) const { assert(is_metadata(), "oops"); return rec->metadata_at(index()); }
235
Klass* as_klass(OopRecorder* rec) const {
236
Metadata* m = as_metadata(rec);
237
assert(m != NULL, "as_metadata returned NULL");
238
assert(m->is_klass(), "oops");
239
return (Klass*) m;
240
}
241
Method* as_method(OopRecorder* rec) const {
242
Metadata* m = as_metadata(rec);
243
assert(m != NULL, "as_metadata returned NULL");
244
assert(m->is_method(), "oops");
245
return (Method*) m;
246
}
247
jobject as_object(OopRecorder* rec) const { assert(is_object(), "oops"); return rec->oop_at(index()); }
248
};
249
#endif // INCLUDE_JVMCI
250
251
private:
252
// State for writing a new set of dependencies:
253
GrowableArray<int>* _dep_seen; // (seen[h->ident] & (1<<dept))
254
GrowableArray<ciBaseObject*>* _deps[TYPE_LIMIT];
255
#if INCLUDE_JVMCI
256
bool _using_dep_values;
257
GrowableArray<DepValue>* _dep_values[TYPE_LIMIT];
258
#endif
259
260
static const char* _dep_name[TYPE_LIMIT];
261
static int _dep_args[TYPE_LIMIT];
262
263
static bool dept_in_mask(DepType dept, int mask) {
264
return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;
265
}
266
267
bool note_dep_seen(int dept, ciBaseObject* x) {
268
assert(dept < BitsPerInt, "oob");
269
int x_id = x->ident();
270
assert(_dep_seen != NULL, "deps must be writable");
271
int seen = _dep_seen->at_grow(x_id, 0);
272
_dep_seen->at_put(x_id, seen | (1<<dept));
273
// return true if we've already seen dept/x
274
return (seen & (1<<dept)) != 0;
275
}
276
277
#if INCLUDE_JVMCI
278
bool note_dep_seen(int dept, DepValue x) {
279
assert(dept < BitsPerInt, "oops");
280
// place metadata deps at even indexes, object deps at odd indexes
281
int x_id = x.is_metadata() ? x.index() * 2 : (x.index() * 2) + 1;
282
assert(_dep_seen != NULL, "deps must be writable");
283
int seen = _dep_seen->at_grow(x_id, 0);
284
_dep_seen->at_put(x_id, seen | (1<<dept));
285
// return true if we've already seen dept/x
286
return (seen & (1<<dept)) != 0;
287
}
288
#endif
289
290
bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
291
int ctxk_i, ciKlass* ctxk);
292
#if INCLUDE_JVMCI
293
bool maybe_merge_ctxk(GrowableArray<DepValue>* deps,
294
int ctxk_i, DepValue ctxk);
295
#endif
296
297
void sort_all_deps();
298
size_t estimate_size_in_bytes();
299
300
// Initialize _deps, etc.
301
void initialize(ciEnv* env);
302
303
// State for making a new set of dependencies:
304
OopRecorder* _oop_recorder;
305
306
// Logging support
307
CompileLog* _log;
308
309
address _content_bytes; // everything but the oop references, encoded
310
size_t _size_in_bytes;
311
312
public:
313
// Make a new empty dependencies set.
314
Dependencies(ciEnv* env) {
315
initialize(env);
316
}
317
#if INCLUDE_JVMCI
318
Dependencies(Arena* arena, OopRecorder* oop_recorder, CompileLog* log);
319
#endif
320
321
private:
322
// Check for a valid context type.
323
// Enforce the restriction against array types.
324
static void check_ctxk(ciKlass* ctxk) {
325
assert(ctxk->is_instance_klass(), "java types only");
326
}
327
static void check_ctxk_concrete(ciKlass* ctxk) {
328
assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");
329
}
330
static void check_ctxk_abstract(ciKlass* ctxk) {
331
check_ctxk(ctxk);
332
assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");
333
}
334
static void check_unique_method(ciKlass* ctxk, ciMethod* m) {
335
assert(!m->can_be_statically_bound(ctxk->as_instance_klass()) || ctxk->is_interface(), "redundant");
336
}
337
static void check_unique_implementor(ciInstanceKlass* ctxk, ciInstanceKlass* uniqk) {
338
assert(ctxk->implementor() == uniqk, "not a unique implementor");
339
}
340
341
void assert_common_1(DepType dept, ciBaseObject* x);
342
void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);
343
void assert_common_4(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2, ciBaseObject* x3);
344
345
public:
346
// Adding assertions to a new dependency set at compile time:
347
void assert_evol_method(ciMethod* m);
348
void assert_leaf_type(ciKlass* ctxk);
349
void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);
350
void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm);
351
void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm, ciKlass* resolved_klass, ciMethod* resolved_method);
352
void assert_unique_implementor(ciInstanceKlass* ctxk, ciInstanceKlass* uniqk);
353
void assert_has_no_finalizable_subclasses(ciKlass* ctxk);
354
void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);
355
#if INCLUDE_JVMCI
356
private:
357
static void check_ctxk(Klass* ctxk) {
358
assert(ctxk->is_instance_klass(), "java types only");
359
}
360
static void check_ctxk_abstract(Klass* ctxk) {
361
check_ctxk(ctxk);
362
assert(ctxk->is_abstract(), "must be abstract");
363
}
364
static void check_unique_method(Klass* ctxk, Method* m) {
365
assert(!m->can_be_statically_bound(InstanceKlass::cast(ctxk)), "redundant");
366
}
367
368
void assert_common_1(DepType dept, DepValue x);
369
void assert_common_2(DepType dept, DepValue x0, DepValue x1);
370
371
public:
372
void assert_evol_method(Method* m);
373
void assert_has_no_finalizable_subclasses(Klass* ctxk);
374
void assert_leaf_type(Klass* ctxk);
375
void assert_unique_implementor(InstanceKlass* ctxk, InstanceKlass* uniqk);
376
void assert_unique_concrete_method(Klass* ctxk, Method* uniqm);
377
void assert_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck);
378
void assert_call_site_target_value(oop callSite, oop methodHandle);
379
#endif // INCLUDE_JVMCI
380
381
// Define whether a given method or type is concrete.
382
// These methods define the term "concrete" as used in this module.
383
// For this module, an "abstract" class is one which is non-concrete.
384
//
385
// Future optimizations may allow some classes to remain
386
// non-concrete until their first instantiation, and allow some
387
// methods to remain non-concrete until their first invocation.
388
// In that case, there would be a middle ground between concrete
389
// and abstract (as defined by the Java language and VM).
390
static bool is_concrete_klass(Klass* k); // k is instantiable
391
static bool is_concrete_method(Method* m, Klass* k); // m is invocable
392
static Klass* find_finalizable_subclass(InstanceKlass* ik);
393
394
static bool is_concrete_root_method(Method* uniqm, InstanceKlass* ctxk);
395
static Klass* find_witness_AME(InstanceKlass* ctxk, Method* m, KlassDepChange* changes = NULL);
396
397
// These versions of the concreteness queries work through the CI.
398
// The CI versions are allowed to skew sometimes from the VM
399
// (oop-based) versions. The cost of such a difference is a
400
// (safely) aborted compilation, or a deoptimization, or a missed
401
// optimization opportunity.
402
//
403
// In order to prevent spurious assertions, query results must
404
// remain stable within any single ciEnv instance. (I.e., they must
405
// not go back into the VM to get their value; they must cache the
406
// bit in the CI, either eagerly or lazily.)
407
static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable
408
static bool has_finalizable_subclass(ciInstanceKlass* k);
409
410
// As a general rule, it is OK to compile under the assumption that
411
// a given type or method is concrete, even if it at some future
412
// point becomes abstract. So dependency checking is one-sided, in
413
// that it permits supposedly concrete classes or methods to turn up
414
// as really abstract. (This shouldn't happen, except during class
415
// evolution, but that's the logic of the checking.) However, if a
416
// supposedly abstract class or method suddenly becomes concrete, a
417
// dependency on it must fail.
418
419
// Checking old assertions at run-time (in the VM only):
420
static Klass* check_evol_method(Method* m);
421
static Klass* check_leaf_type(InstanceKlass* ctxk);
422
static Klass* check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk, Klass* conck, NewKlassDepChange* changes = NULL);
423
static Klass* check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes = NULL);
424
static Klass* check_unique_concrete_method(InstanceKlass* ctxk, Method* uniqm, NewKlassDepChange* changes = NULL);
425
static Klass* check_unique_concrete_method(InstanceKlass* ctxk, Method* uniqm, Klass* resolved_klass, Method* resolved_method, KlassDepChange* changes = NULL);
426
static Klass* check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes = NULL);
427
static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL);
428
// A returned Klass* is NULL if the dependency assertion is still
429
// valid. A non-NULL Klass* is a 'witness' to the assertion
430
// failure, a point in the class hierarchy where the assertion has
431
// been proven false. For example, if check_leaf_type returns
432
// non-NULL, the value is a subtype of the supposed leaf type. This
433
// witness value may be useful for logging the dependency failure.
434
// Note that, when a dependency fails, there may be several possible
435
// witnesses to the failure. The value returned from the check_foo
436
// method is chosen arbitrarily.
437
438
// The 'changes' value, if non-null, requests a limited spot-check
439
// near the indicated recent changes in the class hierarchy.
440
// It is used by DepStream::spot_check_dependency_at.
441
442
// Detecting possible new assertions:
443
static Klass* find_unique_concrete_subtype(InstanceKlass* ctxk);
444
static Method* find_unique_concrete_method(InstanceKlass* ctxk, Method* m,
445
Klass** participant = NULL); // out parameter
446
static Method* find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method);
447
448
#ifdef ASSERT
449
static bool verify_method_context(InstanceKlass* ctxk, Method* m);
450
#endif // ASSERT
451
452
// Create the encoding which will be stored in an nmethod.
453
void encode_content_bytes();
454
455
address content_bytes() {
456
assert(_content_bytes != NULL, "encode it first");
457
return _content_bytes;
458
}
459
size_t size_in_bytes() {
460
assert(_content_bytes != NULL, "encode it first");
461
return _size_in_bytes;
462
}
463
464
OopRecorder* oop_recorder() { return _oop_recorder; }
465
CompileLog* log() { return _log; }
466
467
void copy_to(nmethod* nm);
468
469
DepType validate_dependencies(CompileTask* task, char** failure_detail = NULL);
470
471
void log_all_dependencies();
472
473
void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) {
474
ResourceMark rm;
475
int argslen = args->length();
476
write_dependency_to(log(), dept, args);
477
guarantee(argslen == args->length(),
478
"args array cannot grow inside nested ResoureMark scope");
479
}
480
481
void log_dependency(DepType dept,
482
ciBaseObject* x0,
483
ciBaseObject* x1 = NULL,
484
ciBaseObject* x2 = NULL,
485
ciBaseObject* x3 = NULL) {
486
if (log() == NULL) {
487
return;
488
}
489
ResourceMark rm;
490
GrowableArray<ciBaseObject*>* ciargs =
491
new GrowableArray<ciBaseObject*>(dep_args(dept));
492
assert (x0 != NULL, "no log x0");
493
ciargs->push(x0);
494
495
if (x1 != NULL) {
496
ciargs->push(x1);
497
}
498
if (x2 != NULL) {
499
ciargs->push(x2);
500
}
501
if (x3 != NULL) {
502
ciargs->push(x3);
503
}
504
assert(ciargs->length() == dep_args(dept), "");
505
log_dependency(dept, ciargs);
506
}
507
508
class DepArgument : public ResourceObj {
509
private:
510
bool _is_oop;
511
bool _valid;
512
void* _value;
513
public:
514
DepArgument() : _is_oop(false), _valid(false), _value(NULL) {}
515
DepArgument(oop v): _is_oop(true), _valid(true), _value(v) {}
516
DepArgument(Metadata* v): _is_oop(false), _valid(true), _value(v) {}
517
518
bool is_null() const { return _value == NULL; }
519
bool is_oop() const { return _is_oop; }
520
bool is_metadata() const { return !_is_oop; }
521
bool is_klass() const { return is_metadata() && metadata_value()->is_klass(); }
522
bool is_method() const { return is_metadata() && metadata_value()->is_method(); }
523
524
oop oop_value() const { assert(_is_oop && _valid, "must be"); return cast_to_oop(_value); }
525
Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }
526
};
527
528
static void print_dependency(DepType dept,
529
GrowableArray<DepArgument>* args,
530
Klass* witness = NULL, outputStream* st = tty);
531
532
private:
533
// helper for encoding common context types as zero:
534
static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);
535
536
static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);
537
538
static void write_dependency_to(CompileLog* log,
539
DepType dept,
540
GrowableArray<ciBaseObject*>* args,
541
Klass* witness = NULL);
542
static void write_dependency_to(CompileLog* log,
543
DepType dept,
544
GrowableArray<DepArgument>* args,
545
Klass* witness = NULL);
546
static void write_dependency_to(xmlStream* xtty,
547
DepType dept,
548
GrowableArray<DepArgument>* args,
549
Klass* witness = NULL);
550
public:
551
// Use this to iterate over an nmethod's dependency set.
552
// Works on new and old dependency sets.
553
// Usage:
554
//
555
// ;
556
// Dependencies::DepType dept;
557
// for (Dependencies::DepStream deps(nm); deps.next(); ) {
558
// ...
559
// }
560
//
561
// The caller must be in the VM, since oops are not wrapped in handles.
562
class DepStream {
563
private:
564
nmethod* _code; // null if in a compiler thread
565
Dependencies* _deps; // null if not in a compiler thread
566
CompressedReadStream _bytes;
567
#ifdef ASSERT
568
size_t _byte_limit;
569
#endif
570
571
// iteration variables:
572
DepType _type;
573
int _xi[max_arg_count+1];
574
575
void initial_asserts(size_t byte_limit) NOT_DEBUG({});
576
577
inline Metadata* recorded_metadata_at(int i);
578
inline oop recorded_oop_at(int i);
579
580
Klass* check_klass_dependency(KlassDepChange* changes);
581
Klass* check_new_klass_dependency(NewKlassDepChange* changes);
582
Klass* check_klass_init_dependency(KlassInitDepChange* changes);
583
Klass* check_call_site_dependency(CallSiteDepChange* changes);
584
585
void trace_and_log_witness(Klass* witness);
586
587
public:
588
DepStream(Dependencies* deps)
589
: _code(NULL),
590
_deps(deps),
591
_bytes(deps->content_bytes())
592
{
593
initial_asserts(deps->size_in_bytes());
594
}
595
DepStream(nmethod* code)
596
: _code(code),
597
_deps(NULL),
598
_bytes(code->dependencies_begin())
599
{
600
initial_asserts(code->dependencies_size());
601
}
602
603
bool next();
604
605
DepType type() { return _type; }
606
bool is_oop_argument(int i) { return type() == call_site_target_value; }
607
uintptr_t get_identifier(int i);
608
609
int argument_count() { return dep_args(type()); }
610
int argument_index(int i) { assert(0 <= i && i < argument_count(), "oob");
611
return _xi[i]; }
612
Metadata* argument(int i); // => recorded_oop_at(argument_index(i))
613
oop argument_oop(int i); // => recorded_oop_at(argument_index(i))
614
InstanceKlass* context_type();
615
616
bool is_klass_type() { return Dependencies::is_klass_type(type()); }
617
618
Method* method_argument(int i) {
619
Metadata* x = argument(i);
620
assert(x->is_method(), "type");
621
return (Method*) x;
622
}
623
Klass* type_argument(int i) {
624
Metadata* x = argument(i);
625
assert(x->is_klass(), "type");
626
return (Klass*) x;
627
}
628
629
// The point of the whole exercise: Is this dep still OK?
630
Klass* check_dependency() {
631
Klass* result = check_klass_dependency(NULL);
632
if (result != NULL) return result;
633
return check_call_site_dependency(NULL);
634
}
635
636
// A lighter version: Checks only around recent changes in a class
637
// hierarchy. (See Universe::flush_dependents_on.)
638
Klass* spot_check_dependency_at(DepChange& changes);
639
640
// Log the current dependency to xtty or compilation log.
641
void log_dependency(Klass* witness = NULL);
642
643
// Print the current dependency to tty.
644
void print_dependency(Klass* witness = NULL, bool verbose = false, outputStream* st = tty);
645
};
646
friend class Dependencies::DepStream;
647
648
static void print_statistics();
649
};
650
651
652
class DependencySignature : public ResourceObj {
653
private:
654
int _args_count;
655
uintptr_t _argument_hash[Dependencies::max_arg_count];
656
Dependencies::DepType _type;
657
658
public:
659
DependencySignature(Dependencies::DepStream& dep) {
660
_args_count = dep.argument_count();
661
_type = dep.type();
662
for (int i = 0; i < _args_count; i++) {
663
_argument_hash[i] = dep.get_identifier(i);
664
}
665
}
666
667
static bool equals(DependencySignature const& s1, DependencySignature const& s2);
668
static unsigned hash (DependencySignature const& s1) { return s1.arg(0) >> 2; }
669
670
int args_count() const { return _args_count; }
671
uintptr_t arg(int idx) const { return _argument_hash[idx]; }
672
Dependencies::DepType type() const { return _type; }
673
674
};
675
676
677
// Every particular DepChange is a sub-class of this class.
678
class DepChange : public StackObj {
679
public:
680
// What kind of DepChange is this?
681
virtual bool is_klass_change() const { return false; }
682
virtual bool is_new_klass_change() const { return false; }
683
virtual bool is_klass_init_change() const { return false; }
684
virtual bool is_call_site_change() const { return false; }
685
686
virtual void mark_for_deoptimization(nmethod* nm) = 0;
687
688
// Subclass casting with assertions.
689
KlassDepChange* as_klass_change() {
690
assert(is_klass_change(), "bad cast");
691
return (KlassDepChange*) this;
692
}
693
NewKlassDepChange* as_new_klass_change() {
694
assert(is_new_klass_change(), "bad cast");
695
return (NewKlassDepChange*) this;
696
}
697
KlassInitDepChange* as_klass_init_change() {
698
assert(is_klass_init_change(), "bad cast");
699
return (KlassInitDepChange*) this;
700
}
701
CallSiteDepChange* as_call_site_change() {
702
assert(is_call_site_change(), "bad cast");
703
return (CallSiteDepChange*) this;
704
}
705
706
void print();
707
708
public:
709
enum ChangeType {
710
NO_CHANGE = 0, // an uninvolved klass
711
Change_new_type, // a newly loaded type
712
Change_new_sub, // a super with a new subtype
713
Change_new_impl, // an interface with a new implementation
714
CHANGE_LIMIT,
715
Start_Klass = CHANGE_LIMIT // internal indicator for ContextStream
716
};
717
718
// Usage:
719
// for (DepChange::ContextStream str(changes); str.next(); ) {
720
// Klass* k = str.klass();
721
// switch (str.change_type()) {
722
// ...
723
// }
724
// }
725
class ContextStream : public StackObj {
726
private:
727
DepChange& _changes;
728
friend class DepChange;
729
730
// iteration variables:
731
ChangeType _change_type;
732
Klass* _klass;
733
Array<InstanceKlass*>* _ti_base; // i.e., transitive_interfaces
734
int _ti_index;
735
int _ti_limit;
736
737
// start at the beginning:
738
void start();
739
740
public:
741
ContextStream(DepChange& changes)
742
: _changes(changes)
743
{ start(); }
744
745
ContextStream(DepChange& changes, NoSafepointVerifier& nsv)
746
: _changes(changes)
747
// the nsv argument makes it safe to hold oops like _klass
748
{ start(); }
749
750
bool next();
751
752
ChangeType change_type() { return _change_type; }
753
Klass* klass() { return _klass; }
754
};
755
friend class DepChange::ContextStream;
756
};
757
758
759
// A class hierarchy change coming through the VM (under the Compile_lock).
760
// The change is structured as a single type with any number of supers
761
// and implemented interface types. Other than the type, any of the
762
// super types can be context types for a relevant dependency, which the
763
// type could invalidate.
764
class KlassDepChange : public DepChange {
765
private:
766
// each change set is rooted in exactly one type (at present):
767
InstanceKlass* _type;
768
769
void initialize();
770
771
protected:
772
// notes the type, marks it and all its super-types
773
KlassDepChange(InstanceKlass* type) : _type(type) {
774
initialize();
775
}
776
777
// cleans up the marks
778
~KlassDepChange();
779
780
public:
781
// What kind of DepChange is this?
782
virtual bool is_klass_change() const { return true; }
783
784
virtual void mark_for_deoptimization(nmethod* nm) {
785
nm->mark_for_deoptimization(/*inc_recompile_counts=*/true);
786
}
787
788
InstanceKlass* type() { return _type; }
789
790
// involves_context(k) is true if k == _type or any of its super types
791
bool involves_context(Klass* k);
792
};
793
794
// A class hierarchy change: new type is loaded.
795
class NewKlassDepChange : public KlassDepChange {
796
public:
797
NewKlassDepChange(InstanceKlass* new_type) : KlassDepChange(new_type) {}
798
799
// What kind of DepChange is this?
800
virtual bool is_new_klass_change() const { return true; }
801
802
InstanceKlass* new_type() { return type(); }
803
};
804
805
// Change in initialization state of a loaded class.
806
class KlassInitDepChange : public KlassDepChange {
807
public:
808
KlassInitDepChange(InstanceKlass* type) : KlassDepChange(type) {}
809
810
// What kind of DepChange is this?
811
virtual bool is_klass_init_change() const { return true; }
812
};
813
814
// A CallSite has changed its target.
815
class CallSiteDepChange : public DepChange {
816
private:
817
Handle _call_site;
818
Handle _method_handle;
819
820
public:
821
CallSiteDepChange(Handle call_site, Handle method_handle);
822
823
// What kind of DepChange is this?
824
virtual bool is_call_site_change() const { return true; }
825
826
virtual void mark_for_deoptimization(nmethod* nm) {
827
nm->mark_for_deoptimization(/*inc_recompile_counts=*/false);
828
}
829
830
oop call_site() const { return _call_site(); }
831
oop method_handle() const { return _method_handle(); }
832
};
833
834
#endif // SHARE_CODE_DEPENDENCIES_HPP
835
836