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