Thu, 24 May 2018 18:41:44 +0800
Merge
aoqi@0 | 1 | /* |
drchase@7499 | 2 | * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_CODE_DEPENDENCIES_HPP |
aoqi@0 | 26 | #define SHARE_VM_CODE_DEPENDENCIES_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "ci/ciCallSite.hpp" |
aoqi@0 | 29 | #include "ci/ciKlass.hpp" |
aoqi@0 | 30 | #include "ci/ciMethodHandle.hpp" |
aoqi@0 | 31 | #include "classfile/systemDictionary.hpp" |
aoqi@0 | 32 | #include "code/compressedStream.hpp" |
aoqi@0 | 33 | #include "code/nmethod.hpp" |
aoqi@0 | 34 | #include "utilities/growableArray.hpp" |
aoqi@0 | 35 | |
aoqi@0 | 36 | //** Dependencies represent assertions (approximate invariants) within |
aoqi@0 | 37 | // the runtime system, e.g. class hierarchy changes. An example is an |
aoqi@0 | 38 | // assertion that a given method is not overridden; another example is |
aoqi@0 | 39 | // that a type has only one concrete subtype. Compiled code which |
aoqi@0 | 40 | // relies on such assertions must be discarded if they are overturned |
aoqi@0 | 41 | // by changes in the runtime system. We can think of these assertions |
aoqi@0 | 42 | // as approximate invariants, because we expect them to be overturned |
aoqi@0 | 43 | // very infrequently. We are willing to perform expensive recovery |
aoqi@0 | 44 | // operations when they are overturned. The benefit, of course, is |
aoqi@0 | 45 | // performing optimistic optimizations (!) on the object code. |
aoqi@0 | 46 | // |
aoqi@0 | 47 | // Changes in the class hierarchy due to dynamic linking or |
aoqi@0 | 48 | // class evolution can violate dependencies. There is enough |
aoqi@0 | 49 | // indexing between classes and nmethods to make dependency |
aoqi@0 | 50 | // checking reasonably efficient. |
aoqi@0 | 51 | |
aoqi@0 | 52 | class ciEnv; |
aoqi@0 | 53 | class nmethod; |
aoqi@0 | 54 | class OopRecorder; |
aoqi@0 | 55 | class xmlStream; |
aoqi@0 | 56 | class CompileLog; |
aoqi@0 | 57 | class DepChange; |
aoqi@0 | 58 | class KlassDepChange; |
aoqi@0 | 59 | class CallSiteDepChange; |
aoqi@0 | 60 | class No_Safepoint_Verifier; |
aoqi@0 | 61 | |
aoqi@0 | 62 | class Dependencies: public ResourceObj { |
aoqi@0 | 63 | public: |
aoqi@0 | 64 | // Note: In the comments on dependency types, most uses of the terms |
aoqi@0 | 65 | // subtype and supertype are used in a "non-strict" or "inclusive" |
aoqi@0 | 66 | // sense, and are starred to remind the reader of this fact. |
aoqi@0 | 67 | // Strict uses of the terms use the word "proper". |
aoqi@0 | 68 | // |
aoqi@0 | 69 | // Specifically, every class is its own subtype* and supertype*. |
aoqi@0 | 70 | // (This trick is easier than continually saying things like "Y is a |
aoqi@0 | 71 | // subtype of X or X itself".) |
aoqi@0 | 72 | // |
aoqi@0 | 73 | // Sometimes we write X > Y to mean X is a proper supertype of Y. |
aoqi@0 | 74 | // The notation X > {Y, Z} means X has proper subtypes Y, Z. |
aoqi@0 | 75 | // The notation X.m > Y means that Y inherits m from X, while |
aoqi@0 | 76 | // X.m > Y.m means Y overrides X.m. A star denotes abstractness, |
aoqi@0 | 77 | // as *I > A, meaning (abstract) interface I is a super type of A, |
aoqi@0 | 78 | // or A.*m > B.m, meaning B.m implements abstract method A.m. |
aoqi@0 | 79 | // |
aoqi@0 | 80 | // In this module, the terms "subtype" and "supertype" refer to |
aoqi@0 | 81 | // Java-level reference type conversions, as detected by |
aoqi@0 | 82 | // "instanceof" and performed by "checkcast" operations. The method |
aoqi@0 | 83 | // Klass::is_subtype_of tests these relations. Note that "subtype" |
aoqi@0 | 84 | // is richer than "subclass" (as tested by Klass::is_subclass_of), |
aoqi@0 | 85 | // since it takes account of relations involving interface and array |
aoqi@0 | 86 | // types. |
aoqi@0 | 87 | // |
aoqi@0 | 88 | // To avoid needless complexity, dependencies involving array types |
aoqi@0 | 89 | // are not accepted. If you need to make an assertion about an |
aoqi@0 | 90 | // array type, make the assertion about its corresponding element |
aoqi@0 | 91 | // types. Any assertion that might change about an array type can |
aoqi@0 | 92 | // be converted to an assertion about its element type. |
aoqi@0 | 93 | // |
aoqi@0 | 94 | // Most dependencies are evaluated over a "context type" CX, which |
aoqi@0 | 95 | // stands for the set Subtypes(CX) of every Java type that is a subtype* |
aoqi@0 | 96 | // of CX. When the system loads a new class or interface N, it is |
aoqi@0 | 97 | // responsible for re-evaluating changed dependencies whose context |
aoqi@0 | 98 | // type now includes N, that is, all super types of N. |
aoqi@0 | 99 | // |
aoqi@0 | 100 | enum DepType { |
aoqi@0 | 101 | end_marker = 0, |
aoqi@0 | 102 | |
aoqi@0 | 103 | // An 'evol' dependency simply notes that the contents of the |
aoqi@0 | 104 | // method were used. If it evolves (is replaced), the nmethod |
aoqi@0 | 105 | // must be recompiled. No other dependencies are implied. |
aoqi@0 | 106 | evol_method, |
aoqi@0 | 107 | FIRST_TYPE = evol_method, |
aoqi@0 | 108 | |
aoqi@0 | 109 | // A context type CX is a leaf it if has no proper subtype. |
aoqi@0 | 110 | leaf_type, |
aoqi@0 | 111 | |
aoqi@0 | 112 | // An abstract class CX has exactly one concrete subtype CC. |
aoqi@0 | 113 | abstract_with_unique_concrete_subtype, |
aoqi@0 | 114 | |
aoqi@0 | 115 | // The type CX is purely abstract, with no concrete subtype* at all. |
aoqi@0 | 116 | abstract_with_no_concrete_subtype, |
aoqi@0 | 117 | |
aoqi@0 | 118 | // The concrete CX is free of concrete proper subtypes. |
aoqi@0 | 119 | concrete_with_no_concrete_subtype, |
aoqi@0 | 120 | |
aoqi@0 | 121 | // Given a method M1 and a context class CX, the set MM(CX, M1) of |
aoqi@0 | 122 | // "concrete matching methods" in CX of M1 is the set of every |
aoqi@0 | 123 | // concrete M2 for which it is possible to create an invokevirtual |
aoqi@0 | 124 | // or invokeinterface call site that can reach either M1 or M2. |
aoqi@0 | 125 | // That is, M1 and M2 share a name, signature, and vtable index. |
aoqi@0 | 126 | // We wish to notice when the set MM(CX, M1) is just {M1}, or |
aoqi@0 | 127 | // perhaps a set of two {M1,M2}, and issue dependencies on this. |
aoqi@0 | 128 | |
aoqi@0 | 129 | // The set MM(CX, M1) can be computed by starting with any matching |
aoqi@0 | 130 | // concrete M2 that is inherited into CX, and then walking the |
aoqi@0 | 131 | // subtypes* of CX looking for concrete definitions. |
aoqi@0 | 132 | |
aoqi@0 | 133 | // The parameters to this dependency are the method M1 and the |
aoqi@0 | 134 | // context class CX. M1 must be either inherited in CX or defined |
aoqi@0 | 135 | // in a subtype* of CX. It asserts that MM(CX, M1) is no greater |
aoqi@0 | 136 | // than {M1}. |
aoqi@0 | 137 | unique_concrete_method, // one unique concrete method under CX |
aoqi@0 | 138 | |
aoqi@0 | 139 | // An "exclusive" assertion concerns two methods or subtypes, and |
aoqi@0 | 140 | // declares that there are at most two (or perhaps later N>2) |
aoqi@0 | 141 | // specific items that jointly satisfy the restriction. |
aoqi@0 | 142 | // We list all items explicitly rather than just giving their |
aoqi@0 | 143 | // count, for robustness in the face of complex schema changes. |
aoqi@0 | 144 | |
aoqi@0 | 145 | // A context class CX (which may be either abstract or concrete) |
aoqi@0 | 146 | // has two exclusive concrete subtypes* C1, C2 if every concrete |
aoqi@0 | 147 | // subtype* of CX is either C1 or C2. Note that if neither C1 or C2 |
aoqi@0 | 148 | // are equal to CX, then CX itself must be abstract. But it is |
aoqi@0 | 149 | // also possible (for example) that C1 is CX (a concrete class) |
aoqi@0 | 150 | // and C2 is a proper subtype of C1. |
aoqi@0 | 151 | abstract_with_exclusive_concrete_subtypes_2, |
aoqi@0 | 152 | |
aoqi@0 | 153 | // This dependency asserts that MM(CX, M1) is no greater than {M1,M2}. |
aoqi@0 | 154 | exclusive_concrete_methods_2, |
aoqi@0 | 155 | |
aoqi@0 | 156 | // This dependency asserts that no instances of class or it's |
aoqi@0 | 157 | // subclasses require finalization registration. |
aoqi@0 | 158 | no_finalizable_subclasses, |
aoqi@0 | 159 | |
aoqi@0 | 160 | // This dependency asserts when the CallSite.target value changed. |
aoqi@0 | 161 | call_site_target_value, |
aoqi@0 | 162 | |
aoqi@0 | 163 | TYPE_LIMIT |
aoqi@0 | 164 | }; |
aoqi@0 | 165 | enum { |
aoqi@0 | 166 | LG2_TYPE_LIMIT = 4, // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT)) |
aoqi@0 | 167 | |
aoqi@0 | 168 | // handy categorizations of dependency types: |
aoqi@0 | 169 | all_types = ((1 << TYPE_LIMIT) - 1) & ((-1) << FIRST_TYPE), |
aoqi@0 | 170 | |
aoqi@0 | 171 | non_klass_types = (1 << call_site_target_value), |
aoqi@0 | 172 | klass_types = all_types & ~non_klass_types, |
aoqi@0 | 173 | |
aoqi@0 | 174 | non_ctxk_types = (1 << evol_method), |
aoqi@0 | 175 | implicit_ctxk_types = (1 << call_site_target_value), |
aoqi@0 | 176 | explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types), |
aoqi@0 | 177 | |
aoqi@0 | 178 | max_arg_count = 3, // current maximum number of arguments (incl. ctxk) |
aoqi@0 | 179 | |
aoqi@0 | 180 | // A "context type" is a class or interface that |
aoqi@0 | 181 | // provides context for evaluating a dependency. |
aoqi@0 | 182 | // When present, it is one of the arguments (dep_context_arg). |
aoqi@0 | 183 | // |
aoqi@0 | 184 | // If a dependency does not have a context type, there is a |
aoqi@0 | 185 | // default context, depending on the type of the dependency. |
aoqi@0 | 186 | // This bit signals that a default context has been compressed away. |
aoqi@0 | 187 | default_context_type_bit = (1<<LG2_TYPE_LIMIT) |
aoqi@0 | 188 | }; |
aoqi@0 | 189 | |
aoqi@0 | 190 | static const char* dep_name(DepType dept); |
aoqi@0 | 191 | static int dep_args(DepType dept); |
aoqi@0 | 192 | |
aoqi@0 | 193 | static bool is_klass_type( DepType dept) { return dept_in_mask(dept, klass_types ); } |
aoqi@0 | 194 | |
aoqi@0 | 195 | static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); } |
aoqi@0 | 196 | static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); } |
aoqi@0 | 197 | |
aoqi@0 | 198 | static int dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; } |
aoqi@0 | 199 | static int dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; } |
aoqi@0 | 200 | |
aoqi@0 | 201 | static void check_valid_dependency_type(DepType dept); |
aoqi@0 | 202 | |
aoqi@0 | 203 | private: |
aoqi@0 | 204 | // State for writing a new set of dependencies: |
aoqi@0 | 205 | GrowableArray<int>* _dep_seen; // (seen[h->ident] & (1<<dept)) |
aoqi@0 | 206 | GrowableArray<ciBaseObject*>* _deps[TYPE_LIMIT]; |
aoqi@0 | 207 | |
aoqi@0 | 208 | static const char* _dep_name[TYPE_LIMIT]; |
aoqi@0 | 209 | static int _dep_args[TYPE_LIMIT]; |
aoqi@0 | 210 | |
aoqi@0 | 211 | static bool dept_in_mask(DepType dept, int mask) { |
aoqi@0 | 212 | return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | bool note_dep_seen(int dept, ciBaseObject* x) { |
aoqi@0 | 216 | assert(dept < BitsPerInt, "oob"); |
aoqi@0 | 217 | int x_id = x->ident(); |
aoqi@0 | 218 | assert(_dep_seen != NULL, "deps must be writable"); |
aoqi@0 | 219 | int seen = _dep_seen->at_grow(x_id, 0); |
aoqi@0 | 220 | _dep_seen->at_put(x_id, seen | (1<<dept)); |
aoqi@0 | 221 | // return true if we've already seen dept/x |
aoqi@0 | 222 | return (seen & (1<<dept)) != 0; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps, |
aoqi@0 | 226 | int ctxk_i, ciKlass* ctxk); |
aoqi@0 | 227 | |
aoqi@0 | 228 | void sort_all_deps(); |
aoqi@0 | 229 | size_t estimate_size_in_bytes(); |
aoqi@0 | 230 | |
aoqi@0 | 231 | // Initialize _deps, etc. |
aoqi@0 | 232 | void initialize(ciEnv* env); |
aoqi@0 | 233 | |
aoqi@0 | 234 | // State for making a new set of dependencies: |
aoqi@0 | 235 | OopRecorder* _oop_recorder; |
aoqi@0 | 236 | |
aoqi@0 | 237 | // Logging support |
aoqi@0 | 238 | CompileLog* _log; |
aoqi@0 | 239 | |
aoqi@0 | 240 | address _content_bytes; // everything but the oop references, encoded |
aoqi@0 | 241 | size_t _size_in_bytes; |
aoqi@0 | 242 | |
aoqi@0 | 243 | public: |
aoqi@0 | 244 | // Make a new empty dependencies set. |
aoqi@0 | 245 | Dependencies(ciEnv* env) { |
aoqi@0 | 246 | initialize(env); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | |
aoqi@0 | 249 | private: |
aoqi@0 | 250 | // Check for a valid context type. |
aoqi@0 | 251 | // Enforce the restriction against array types. |
aoqi@0 | 252 | static void check_ctxk(ciKlass* ctxk) { |
aoqi@0 | 253 | assert(ctxk->is_instance_klass(), "java types only"); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | static void check_ctxk_concrete(ciKlass* ctxk) { |
aoqi@0 | 256 | assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete"); |
aoqi@0 | 257 | } |
aoqi@0 | 258 | static void check_ctxk_abstract(ciKlass* ctxk) { |
aoqi@0 | 259 | check_ctxk(ctxk); |
aoqi@0 | 260 | assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract"); |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | void assert_common_1(DepType dept, ciBaseObject* x); |
aoqi@0 | 264 | void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1); |
aoqi@0 | 265 | void assert_common_3(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2); |
aoqi@0 | 266 | |
aoqi@0 | 267 | public: |
aoqi@0 | 268 | // Adding assertions to a new dependency set at compile time: |
aoqi@0 | 269 | void assert_evol_method(ciMethod* m); |
aoqi@0 | 270 | void assert_leaf_type(ciKlass* ctxk); |
aoqi@0 | 271 | void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck); |
aoqi@0 | 272 | void assert_abstract_with_no_concrete_subtype(ciKlass* ctxk); |
aoqi@0 | 273 | void assert_concrete_with_no_concrete_subtype(ciKlass* ctxk); |
aoqi@0 | 274 | void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm); |
aoqi@0 | 275 | void assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2); |
aoqi@0 | 276 | void assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2); |
aoqi@0 | 277 | void assert_has_no_finalizable_subclasses(ciKlass* ctxk); |
aoqi@0 | 278 | void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle); |
aoqi@0 | 279 | |
aoqi@0 | 280 | // Define whether a given method or type is concrete. |
aoqi@0 | 281 | // These methods define the term "concrete" as used in this module. |
aoqi@0 | 282 | // For this module, an "abstract" class is one which is non-concrete. |
aoqi@0 | 283 | // |
aoqi@0 | 284 | // Future optimizations may allow some classes to remain |
aoqi@0 | 285 | // non-concrete until their first instantiation, and allow some |
aoqi@0 | 286 | // methods to remain non-concrete until their first invocation. |
aoqi@0 | 287 | // In that case, there would be a middle ground between concrete |
aoqi@0 | 288 | // and abstract (as defined by the Java language and VM). |
aoqi@0 | 289 | static bool is_concrete_klass(Klass* k); // k is instantiable |
drchase@7499 | 290 | static bool is_concrete_method(Method* m, Klass* k); // m is invocable |
aoqi@0 | 291 | static Klass* find_finalizable_subclass(Klass* k); |
aoqi@0 | 292 | |
aoqi@0 | 293 | // These versions of the concreteness queries work through the CI. |
aoqi@0 | 294 | // The CI versions are allowed to skew sometimes from the VM |
aoqi@0 | 295 | // (oop-based) versions. The cost of such a difference is a |
aoqi@0 | 296 | // (safely) aborted compilation, or a deoptimization, or a missed |
aoqi@0 | 297 | // optimization opportunity. |
aoqi@0 | 298 | // |
aoqi@0 | 299 | // In order to prevent spurious assertions, query results must |
aoqi@0 | 300 | // remain stable within any single ciEnv instance. (I.e., they must |
aoqi@0 | 301 | // not go back into the VM to get their value; they must cache the |
aoqi@0 | 302 | // bit in the CI, either eagerly or lazily.) |
aoqi@0 | 303 | static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable |
aoqi@0 | 304 | static bool has_finalizable_subclass(ciInstanceKlass* k); |
aoqi@0 | 305 | |
aoqi@0 | 306 | // As a general rule, it is OK to compile under the assumption that |
aoqi@0 | 307 | // a given type or method is concrete, even if it at some future |
aoqi@0 | 308 | // point becomes abstract. So dependency checking is one-sided, in |
aoqi@0 | 309 | // that it permits supposedly concrete classes or methods to turn up |
aoqi@0 | 310 | // as really abstract. (This shouldn't happen, except during class |
aoqi@0 | 311 | // evolution, but that's the logic of the checking.) However, if a |
aoqi@0 | 312 | // supposedly abstract class or method suddenly becomes concrete, a |
aoqi@0 | 313 | // dependency on it must fail. |
aoqi@0 | 314 | |
aoqi@0 | 315 | // Checking old assertions at run-time (in the VM only): |
aoqi@0 | 316 | static Klass* check_evol_method(Method* m); |
aoqi@0 | 317 | static Klass* check_leaf_type(Klass* ctxk); |
aoqi@0 | 318 | static Klass* check_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck, |
aoqi@0 | 319 | KlassDepChange* changes = NULL); |
aoqi@0 | 320 | static Klass* check_abstract_with_no_concrete_subtype(Klass* ctxk, |
aoqi@0 | 321 | KlassDepChange* changes = NULL); |
aoqi@0 | 322 | static Klass* check_concrete_with_no_concrete_subtype(Klass* ctxk, |
aoqi@0 | 323 | KlassDepChange* changes = NULL); |
aoqi@0 | 324 | static Klass* check_unique_concrete_method(Klass* ctxk, Method* uniqm, |
aoqi@0 | 325 | KlassDepChange* changes = NULL); |
aoqi@0 | 326 | static Klass* check_abstract_with_exclusive_concrete_subtypes(Klass* ctxk, Klass* k1, Klass* k2, |
aoqi@0 | 327 | KlassDepChange* changes = NULL); |
aoqi@0 | 328 | static Klass* check_exclusive_concrete_methods(Klass* ctxk, Method* m1, Method* m2, |
aoqi@0 | 329 | KlassDepChange* changes = NULL); |
aoqi@0 | 330 | static Klass* check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes = NULL); |
aoqi@0 | 331 | static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL); |
aoqi@0 | 332 | // A returned Klass* is NULL if the dependency assertion is still |
aoqi@0 | 333 | // valid. A non-NULL Klass* is a 'witness' to the assertion |
aoqi@0 | 334 | // failure, a point in the class hierarchy where the assertion has |
aoqi@0 | 335 | // been proven false. For example, if check_leaf_type returns |
aoqi@0 | 336 | // non-NULL, the value is a subtype of the supposed leaf type. This |
aoqi@0 | 337 | // witness value may be useful for logging the dependency failure. |
aoqi@0 | 338 | // Note that, when a dependency fails, there may be several possible |
aoqi@0 | 339 | // witnesses to the failure. The value returned from the check_foo |
aoqi@0 | 340 | // method is chosen arbitrarily. |
aoqi@0 | 341 | |
aoqi@0 | 342 | // The 'changes' value, if non-null, requests a limited spot-check |
aoqi@0 | 343 | // near the indicated recent changes in the class hierarchy. |
aoqi@0 | 344 | // It is used by DepStream::spot_check_dependency_at. |
aoqi@0 | 345 | |
aoqi@0 | 346 | // Detecting possible new assertions: |
aoqi@0 | 347 | static Klass* find_unique_concrete_subtype(Klass* ctxk); |
aoqi@0 | 348 | static Method* find_unique_concrete_method(Klass* ctxk, Method* m); |
aoqi@0 | 349 | static int find_exclusive_concrete_subtypes(Klass* ctxk, int klen, Klass* k[]); |
aoqi@0 | 350 | |
aoqi@0 | 351 | // Create the encoding which will be stored in an nmethod. |
aoqi@0 | 352 | void encode_content_bytes(); |
aoqi@0 | 353 | |
aoqi@0 | 354 | address content_bytes() { |
aoqi@0 | 355 | assert(_content_bytes != NULL, "encode it first"); |
aoqi@0 | 356 | return _content_bytes; |
aoqi@0 | 357 | } |
aoqi@0 | 358 | size_t size_in_bytes() { |
aoqi@0 | 359 | assert(_content_bytes != NULL, "encode it first"); |
aoqi@0 | 360 | return _size_in_bytes; |
aoqi@0 | 361 | } |
aoqi@0 | 362 | |
aoqi@0 | 363 | OopRecorder* oop_recorder() { return _oop_recorder; } |
aoqi@0 | 364 | CompileLog* log() { return _log; } |
aoqi@0 | 365 | |
aoqi@0 | 366 | void copy_to(nmethod* nm); |
aoqi@0 | 367 | |
aoqi@0 | 368 | void log_all_dependencies(); |
morris@7030 | 369 | |
morris@7030 | 370 | void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) { |
morris@7030 | 371 | ResourceMark rm; |
morris@7030 | 372 | int argslen = args->length(); |
morris@7030 | 373 | write_dependency_to(log(), dept, args); |
morris@7030 | 374 | guarantee(argslen == args->length(), |
morris@7030 | 375 | "args array cannot grow inside nested ResoureMark scope"); |
aoqi@0 | 376 | } |
morris@7030 | 377 | |
aoqi@0 | 378 | void log_dependency(DepType dept, |
aoqi@0 | 379 | ciBaseObject* x0, |
aoqi@0 | 380 | ciBaseObject* x1 = NULL, |
aoqi@0 | 381 | ciBaseObject* x2 = NULL) { |
morris@7030 | 382 | if (log() == NULL) { |
morris@7030 | 383 | return; |
morris@7030 | 384 | } |
morris@7030 | 385 | ResourceMark rm; |
morris@7030 | 386 | GrowableArray<ciBaseObject*>* ciargs = |
morris@7030 | 387 | new GrowableArray<ciBaseObject*>(dep_args(dept)); |
morris@7030 | 388 | assert (x0 != NULL, "no log x0"); |
morris@7030 | 389 | ciargs->push(x0); |
morris@7030 | 390 | |
morris@7030 | 391 | if (x1 != NULL) { |
morris@7030 | 392 | ciargs->push(x1); |
morris@7030 | 393 | } |
morris@7030 | 394 | if (x2 != NULL) { |
morris@7030 | 395 | ciargs->push(x2); |
morris@7030 | 396 | } |
morris@7030 | 397 | assert(ciargs->length() == dep_args(dept), ""); |
morris@7030 | 398 | log_dependency(dept, ciargs); |
aoqi@0 | 399 | } |
aoqi@0 | 400 | |
aoqi@0 | 401 | class DepArgument : public ResourceObj { |
aoqi@0 | 402 | private: |
aoqi@0 | 403 | bool _is_oop; |
aoqi@0 | 404 | bool _valid; |
aoqi@0 | 405 | void* _value; |
aoqi@0 | 406 | public: |
aoqi@0 | 407 | DepArgument() : _is_oop(false), _value(NULL), _valid(false) {} |
aoqi@0 | 408 | DepArgument(oop v): _is_oop(true), _value(v), _valid(true) {} |
aoqi@0 | 409 | DepArgument(Metadata* v): _is_oop(false), _value(v), _valid(true) {} |
aoqi@0 | 410 | |
aoqi@0 | 411 | bool is_null() const { return _value == NULL; } |
aoqi@0 | 412 | bool is_oop() const { return _is_oop; } |
aoqi@0 | 413 | bool is_metadata() const { return !_is_oop; } |
aoqi@0 | 414 | bool is_klass() const { return is_metadata() && metadata_value()->is_klass(); } |
aoqi@0 | 415 | bool is_method() const { return is_metadata() && metadata_value()->is_method(); } |
aoqi@0 | 416 | |
aoqi@0 | 417 | oop oop_value() const { assert(_is_oop && _valid, "must be"); return (oop) _value; } |
aoqi@0 | 418 | Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; } |
aoqi@0 | 419 | }; |
aoqi@0 | 420 | |
aoqi@0 | 421 | static void print_dependency(DepType dept, |
morris@7030 | 422 | GrowableArray<DepArgument>* args, |
aoqi@0 | 423 | Klass* witness = NULL); |
aoqi@0 | 424 | |
aoqi@0 | 425 | private: |
aoqi@0 | 426 | // helper for encoding common context types as zero: |
aoqi@0 | 427 | static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x); |
aoqi@0 | 428 | |
aoqi@0 | 429 | static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x); |
aoqi@0 | 430 | |
morris@7030 | 431 | static void write_dependency_to(CompileLog* log, |
morris@7030 | 432 | DepType dept, |
morris@7030 | 433 | GrowableArray<ciBaseObject*>* args, |
morris@7030 | 434 | Klass* witness = NULL); |
morris@7030 | 435 | static void write_dependency_to(CompileLog* log, |
morris@7030 | 436 | DepType dept, |
morris@7030 | 437 | GrowableArray<DepArgument>* args, |
morris@7030 | 438 | Klass* witness = NULL); |
morris@7030 | 439 | static void write_dependency_to(xmlStream* xtty, |
morris@7030 | 440 | DepType dept, |
morris@7030 | 441 | GrowableArray<DepArgument>* args, |
morris@7030 | 442 | Klass* witness = NULL); |
aoqi@0 | 443 | public: |
aoqi@0 | 444 | // Use this to iterate over an nmethod's dependency set. |
aoqi@0 | 445 | // Works on new and old dependency sets. |
aoqi@0 | 446 | // Usage: |
aoqi@0 | 447 | // |
aoqi@0 | 448 | // ; |
aoqi@0 | 449 | // Dependencies::DepType dept; |
aoqi@0 | 450 | // for (Dependencies::DepStream deps(nm); deps.next(); ) { |
aoqi@0 | 451 | // ... |
aoqi@0 | 452 | // } |
aoqi@0 | 453 | // |
aoqi@0 | 454 | // The caller must be in the VM, since oops are not wrapped in handles. |
aoqi@0 | 455 | class DepStream { |
aoqi@0 | 456 | private: |
aoqi@0 | 457 | nmethod* _code; // null if in a compiler thread |
aoqi@0 | 458 | Dependencies* _deps; // null if not in a compiler thread |
aoqi@0 | 459 | CompressedReadStream _bytes; |
aoqi@0 | 460 | #ifdef ASSERT |
aoqi@0 | 461 | size_t _byte_limit; |
aoqi@0 | 462 | #endif |
aoqi@0 | 463 | |
aoqi@0 | 464 | // iteration variables: |
aoqi@0 | 465 | DepType _type; |
aoqi@0 | 466 | int _xi[max_arg_count+1]; |
aoqi@0 | 467 | |
aoqi@0 | 468 | void initial_asserts(size_t byte_limit) NOT_DEBUG({}); |
aoqi@0 | 469 | |
aoqi@0 | 470 | inline Metadata* recorded_metadata_at(int i); |
aoqi@0 | 471 | inline oop recorded_oop_at(int i); |
aoqi@0 | 472 | |
aoqi@0 | 473 | Klass* check_klass_dependency(KlassDepChange* changes); |
aoqi@0 | 474 | Klass* check_call_site_dependency(CallSiteDepChange* changes); |
aoqi@0 | 475 | |
aoqi@0 | 476 | void trace_and_log_witness(Klass* witness); |
aoqi@0 | 477 | |
aoqi@0 | 478 | public: |
aoqi@0 | 479 | DepStream(Dependencies* deps) |
aoqi@0 | 480 | : _deps(deps), |
aoqi@0 | 481 | _code(NULL), |
aoqi@0 | 482 | _bytes(deps->content_bytes()) |
aoqi@0 | 483 | { |
aoqi@0 | 484 | initial_asserts(deps->size_in_bytes()); |
aoqi@0 | 485 | } |
aoqi@0 | 486 | DepStream(nmethod* code) |
aoqi@0 | 487 | : _deps(NULL), |
aoqi@0 | 488 | _code(code), |
aoqi@0 | 489 | _bytes(code->dependencies_begin()) |
aoqi@0 | 490 | { |
aoqi@0 | 491 | initial_asserts(code->dependencies_size()); |
aoqi@0 | 492 | } |
aoqi@0 | 493 | |
aoqi@0 | 494 | bool next(); |
aoqi@0 | 495 | |
aoqi@0 | 496 | DepType type() { return _type; } |
aoqi@0 | 497 | int argument_count() { return dep_args(type()); } |
aoqi@0 | 498 | int argument_index(int i) { assert(0 <= i && i < argument_count(), "oob"); |
aoqi@0 | 499 | return _xi[i]; } |
aoqi@0 | 500 | Metadata* argument(int i); // => recorded_oop_at(argument_index(i)) |
aoqi@0 | 501 | oop argument_oop(int i); // => recorded_oop_at(argument_index(i)) |
aoqi@0 | 502 | Klass* context_type(); |
aoqi@0 | 503 | |
aoqi@0 | 504 | bool is_klass_type() { return Dependencies::is_klass_type(type()); } |
aoqi@0 | 505 | |
aoqi@0 | 506 | Method* method_argument(int i) { |
aoqi@0 | 507 | Metadata* x = argument(i); |
aoqi@0 | 508 | assert(x->is_method(), "type"); |
aoqi@0 | 509 | return (Method*) x; |
aoqi@0 | 510 | } |
aoqi@0 | 511 | Klass* type_argument(int i) { |
aoqi@0 | 512 | Metadata* x = argument(i); |
aoqi@0 | 513 | assert(x->is_klass(), "type"); |
aoqi@0 | 514 | return (Klass*) x; |
aoqi@0 | 515 | } |
aoqi@0 | 516 | |
aoqi@0 | 517 | // The point of the whole exercise: Is this dep still OK? |
aoqi@0 | 518 | Klass* check_dependency() { |
aoqi@0 | 519 | Klass* result = check_klass_dependency(NULL); |
aoqi@0 | 520 | if (result != NULL) return result; |
aoqi@0 | 521 | return check_call_site_dependency(NULL); |
aoqi@0 | 522 | } |
aoqi@0 | 523 | |
aoqi@0 | 524 | // A lighter version: Checks only around recent changes in a class |
aoqi@0 | 525 | // hierarchy. (See Universe::flush_dependents_on.) |
aoqi@0 | 526 | Klass* spot_check_dependency_at(DepChange& changes); |
aoqi@0 | 527 | |
aoqi@0 | 528 | // Log the current dependency to xtty or compilation log. |
aoqi@0 | 529 | void log_dependency(Klass* witness = NULL); |
aoqi@0 | 530 | |
aoqi@0 | 531 | // Print the current dependency to tty. |
aoqi@0 | 532 | void print_dependency(Klass* witness = NULL, bool verbose = false); |
aoqi@0 | 533 | }; |
aoqi@0 | 534 | friend class Dependencies::DepStream; |
aoqi@0 | 535 | |
aoqi@0 | 536 | static void print_statistics() PRODUCT_RETURN; |
aoqi@0 | 537 | }; |
aoqi@0 | 538 | |
aoqi@0 | 539 | |
aoqi@0 | 540 | // Every particular DepChange is a sub-class of this class. |
aoqi@0 | 541 | class DepChange : public StackObj { |
aoqi@0 | 542 | public: |
aoqi@0 | 543 | // What kind of DepChange is this? |
aoqi@0 | 544 | virtual bool is_klass_change() const { return false; } |
aoqi@0 | 545 | virtual bool is_call_site_change() const { return false; } |
aoqi@0 | 546 | |
aoqi@0 | 547 | // Subclass casting with assertions. |
aoqi@0 | 548 | KlassDepChange* as_klass_change() { |
aoqi@0 | 549 | assert(is_klass_change(), "bad cast"); |
aoqi@0 | 550 | return (KlassDepChange*) this; |
aoqi@0 | 551 | } |
aoqi@0 | 552 | CallSiteDepChange* as_call_site_change() { |
aoqi@0 | 553 | assert(is_call_site_change(), "bad cast"); |
aoqi@0 | 554 | return (CallSiteDepChange*) this; |
aoqi@0 | 555 | } |
aoqi@0 | 556 | |
aoqi@0 | 557 | void print(); |
aoqi@0 | 558 | |
aoqi@0 | 559 | public: |
aoqi@0 | 560 | enum ChangeType { |
aoqi@0 | 561 | NO_CHANGE = 0, // an uninvolved klass |
aoqi@0 | 562 | Change_new_type, // a newly loaded type |
aoqi@0 | 563 | Change_new_sub, // a super with a new subtype |
aoqi@0 | 564 | Change_new_impl, // an interface with a new implementation |
aoqi@0 | 565 | CHANGE_LIMIT, |
aoqi@0 | 566 | Start_Klass = CHANGE_LIMIT // internal indicator for ContextStream |
aoqi@0 | 567 | }; |
aoqi@0 | 568 | |
aoqi@0 | 569 | // Usage: |
aoqi@0 | 570 | // for (DepChange::ContextStream str(changes); str.next(); ) { |
aoqi@0 | 571 | // Klass* k = str.klass(); |
aoqi@0 | 572 | // switch (str.change_type()) { |
aoqi@0 | 573 | // ... |
aoqi@0 | 574 | // } |
aoqi@0 | 575 | // } |
aoqi@0 | 576 | class ContextStream : public StackObj { |
aoqi@0 | 577 | private: |
aoqi@0 | 578 | DepChange& _changes; |
aoqi@0 | 579 | friend class DepChange; |
aoqi@0 | 580 | |
aoqi@0 | 581 | // iteration variables: |
aoqi@0 | 582 | ChangeType _change_type; |
aoqi@0 | 583 | Klass* _klass; |
aoqi@0 | 584 | Array<Klass*>* _ti_base; // i.e., transitive_interfaces |
aoqi@0 | 585 | int _ti_index; |
aoqi@0 | 586 | int _ti_limit; |
aoqi@0 | 587 | |
aoqi@0 | 588 | // start at the beginning: |
aoqi@0 | 589 | void start(); |
aoqi@0 | 590 | |
aoqi@0 | 591 | public: |
aoqi@0 | 592 | ContextStream(DepChange& changes) |
aoqi@0 | 593 | : _changes(changes) |
aoqi@0 | 594 | { start(); } |
aoqi@0 | 595 | |
aoqi@0 | 596 | ContextStream(DepChange& changes, No_Safepoint_Verifier& nsv) |
aoqi@0 | 597 | : _changes(changes) |
aoqi@0 | 598 | // the nsv argument makes it safe to hold oops like _klass |
aoqi@0 | 599 | { start(); } |
aoqi@0 | 600 | |
aoqi@0 | 601 | bool next(); |
aoqi@0 | 602 | |
aoqi@0 | 603 | ChangeType change_type() { return _change_type; } |
aoqi@0 | 604 | Klass* klass() { return _klass; } |
aoqi@0 | 605 | }; |
aoqi@0 | 606 | friend class DepChange::ContextStream; |
aoqi@0 | 607 | }; |
aoqi@0 | 608 | |
aoqi@0 | 609 | |
aoqi@0 | 610 | // A class hierarchy change coming through the VM (under the Compile_lock). |
aoqi@0 | 611 | // The change is structured as a single new type with any number of supers |
aoqi@0 | 612 | // and implemented interface types. Other than the new type, any of the |
aoqi@0 | 613 | // super types can be context types for a relevant dependency, which the |
aoqi@0 | 614 | // new type could invalidate. |
aoqi@0 | 615 | class KlassDepChange : public DepChange { |
aoqi@0 | 616 | private: |
aoqi@0 | 617 | // each change set is rooted in exactly one new type (at present): |
aoqi@0 | 618 | KlassHandle _new_type; |
aoqi@0 | 619 | |
aoqi@0 | 620 | void initialize(); |
aoqi@0 | 621 | |
aoqi@0 | 622 | public: |
aoqi@0 | 623 | // notes the new type, marks it and all its super-types |
aoqi@0 | 624 | KlassDepChange(KlassHandle new_type) |
aoqi@0 | 625 | : _new_type(new_type) |
aoqi@0 | 626 | { |
aoqi@0 | 627 | initialize(); |
aoqi@0 | 628 | } |
aoqi@0 | 629 | |
aoqi@0 | 630 | // cleans up the marks |
aoqi@0 | 631 | ~KlassDepChange(); |
aoqi@0 | 632 | |
aoqi@0 | 633 | // What kind of DepChange is this? |
aoqi@0 | 634 | virtual bool is_klass_change() const { return true; } |
aoqi@0 | 635 | |
aoqi@0 | 636 | Klass* new_type() { return _new_type(); } |
aoqi@0 | 637 | |
aoqi@0 | 638 | // involves_context(k) is true if k is new_type or any of the super types |
aoqi@0 | 639 | bool involves_context(Klass* k); |
aoqi@0 | 640 | }; |
aoqi@0 | 641 | |
aoqi@0 | 642 | |
aoqi@0 | 643 | // A CallSite has changed its target. |
aoqi@0 | 644 | class CallSiteDepChange : public DepChange { |
aoqi@0 | 645 | private: |
aoqi@0 | 646 | Handle _call_site; |
aoqi@0 | 647 | Handle _method_handle; |
aoqi@0 | 648 | |
aoqi@0 | 649 | public: |
aoqi@0 | 650 | CallSiteDepChange(Handle call_site, Handle method_handle) |
aoqi@0 | 651 | : _call_site(call_site), |
aoqi@0 | 652 | _method_handle(method_handle) |
aoqi@0 | 653 | { |
aoqi@0 | 654 | assert(_call_site() ->is_a(SystemDictionary::CallSite_klass()), "must be"); |
aoqi@0 | 655 | assert(_method_handle()->is_a(SystemDictionary::MethodHandle_klass()), "must be"); |
aoqi@0 | 656 | } |
aoqi@0 | 657 | |
aoqi@0 | 658 | // What kind of DepChange is this? |
aoqi@0 | 659 | virtual bool is_call_site_change() const { return true; } |
aoqi@0 | 660 | |
aoqi@0 | 661 | oop call_site() const { return _call_site(); } |
aoqi@0 | 662 | oop method_handle() const { return _method_handle(); } |
aoqi@0 | 663 | }; |
aoqi@0 | 664 | |
aoqi@0 | 665 | #endif // SHARE_VM_CODE_DEPENDENCIES_HPP |