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