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