duke@435: /* trims@1907: * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: //** Dependencies represent assertions (approximate invariants) within duke@435: // the class hierarchy. An example is an assertion that a given duke@435: // method is not overridden; another example is that a type has only duke@435: // one concrete subtype. Compiled code which relies on such duke@435: // assertions must be discarded if they are overturned by changes in duke@435: // the class hierarchy. We can think of these assertions as duke@435: // approximate invariants, because we expect them to be overturned duke@435: // very infrequently. We are willing to perform expensive recovery duke@435: // operations when they are overturned. The benefit, of course, is duke@435: // performing optimistic optimizations (!) on the object code. duke@435: // duke@435: // Changes in the class hierarchy due to dynamic linking or duke@435: // class evolution can violate dependencies. There is enough duke@435: // indexing between classes and nmethods to make dependency duke@435: // checking reasonably efficient. duke@435: duke@435: class ciEnv; duke@435: class nmethod; duke@435: class OopRecorder; duke@435: class xmlStream; duke@435: class CompileLog; duke@435: class DepChange; duke@435: class No_Safepoint_Verifier; duke@435: duke@435: class Dependencies: public ResourceObj { duke@435: public: duke@435: // Note: In the comments on dependency types, most uses of the terms duke@435: // subtype and supertype are used in a "non-strict" or "inclusive" duke@435: // sense, and are starred to remind the reader of this fact. duke@435: // Strict uses of the terms use the word "proper". duke@435: // duke@435: // Specifically, every class is its own subtype* and supertype*. duke@435: // (This trick is easier than continually saying things like "Y is a duke@435: // subtype of X or X itself".) duke@435: // duke@435: // Sometimes we write X > Y to mean X is a proper supertype of Y. duke@435: // The notation X > {Y, Z} means X has proper subtypes Y, Z. duke@435: // The notation X.m > Y means that Y inherits m from X, while duke@435: // X.m > Y.m means Y overrides X.m. A star denotes abstractness, duke@435: // as *I > A, meaning (abstract) interface I is a super type of A, duke@435: // or A.*m > B.m, meaning B.m implements abstract method A.m. duke@435: // duke@435: // In this module, the terms "subtype" and "supertype" refer to duke@435: // Java-level reference type conversions, as detected by duke@435: // "instanceof" and performed by "checkcast" operations. The method duke@435: // Klass::is_subtype_of tests these relations. Note that "subtype" duke@435: // is richer than "subclass" (as tested by Klass::is_subclass_of), duke@435: // since it takes account of relations involving interface and array duke@435: // types. duke@435: // duke@435: // To avoid needless complexity, dependencies involving array types duke@435: // are not accepted. If you need to make an assertion about an duke@435: // array type, make the assertion about its corresponding element duke@435: // types. Any assertion that might change about an array type can duke@435: // be converted to an assertion about its element type. duke@435: // duke@435: // Most dependencies are evaluated over a "context type" CX, which duke@435: // stands for the set Subtypes(CX) of every Java type that is a subtype* duke@435: // of CX. When the system loads a new class or interface N, it is duke@435: // responsible for re-evaluating changed dependencies whose context duke@435: // type now includes N, that is, all super types of N. duke@435: // duke@435: enum DepType { duke@435: end_marker = 0, duke@435: duke@435: // An 'evol' dependency simply notes that the contents of the duke@435: // method were used. If it evolves (is replaced), the nmethod duke@435: // must be recompiled. No other dependencies are implied. duke@435: evol_method, duke@435: FIRST_TYPE = evol_method, duke@435: duke@435: // A context type CX is a leaf it if has no proper subtype. duke@435: leaf_type, duke@435: duke@435: // An abstract class CX has exactly one concrete subtype CC. duke@435: abstract_with_unique_concrete_subtype, duke@435: duke@435: // The type CX is purely abstract, with no concrete subtype* at all. duke@435: abstract_with_no_concrete_subtype, duke@435: duke@435: // The concrete CX is free of concrete proper subtypes. duke@435: concrete_with_no_concrete_subtype, duke@435: duke@435: // Given a method M1 and a context class CX, the set MM(CX, M1) of duke@435: // "concrete matching methods" in CX of M1 is the set of every duke@435: // concrete M2 for which it is possible to create an invokevirtual duke@435: // or invokeinterface call site that can reach either M1 or M2. duke@435: // That is, M1 and M2 share a name, signature, and vtable index. duke@435: // We wish to notice when the set MM(CX, M1) is just {M1}, or duke@435: // perhaps a set of two {M1,M2}, and issue dependencies on this. duke@435: duke@435: // The set MM(CX, M1) can be computed by starting with any matching duke@435: // concrete M2 that is inherited into CX, and then walking the duke@435: // subtypes* of CX looking for concrete definitions. duke@435: duke@435: // The parameters to this dependency are the method M1 and the duke@435: // context class CX. M1 must be either inherited in CX or defined duke@435: // in a subtype* of CX. It asserts that MM(CX, M1) is no greater duke@435: // than {M1}. duke@435: unique_concrete_method, // one unique concrete method under CX duke@435: duke@435: // An "exclusive" assertion concerns two methods or subtypes, and duke@435: // declares that there are at most two (or perhaps later N>2) duke@435: // specific items that jointly satisfy the restriction. duke@435: // We list all items explicitly rather than just giving their duke@435: // count, for robustness in the face of complex schema changes. duke@435: duke@435: // A context class CX (which may be either abstract or concrete) duke@435: // has two exclusive concrete subtypes* C1, C2 if every concrete duke@435: // subtype* of CX is either C1 or C2. Note that if neither C1 or C2 duke@435: // are equal to CX, then CX itself must be abstract. But it is duke@435: // also possible (for example) that C1 is CX (a concrete class) duke@435: // and C2 is a proper subtype of C1. duke@435: abstract_with_exclusive_concrete_subtypes_2, duke@435: duke@435: // This dependency asserts that MM(CX, M1) is no greater than {M1,M2}. duke@435: exclusive_concrete_methods_2, duke@435: duke@435: // This dependency asserts that no instances of class or it's duke@435: // subclasses require finalization registration. duke@435: no_finalizable_subclasses, duke@435: duke@435: TYPE_LIMIT duke@435: }; duke@435: enum { duke@435: LG2_TYPE_LIMIT = 4, // assert(TYPE_LIMIT <= (1<* _dep_seen; // (seen[h->ident] & (1<* _deps[TYPE_LIMIT]; duke@435: duke@435: static const char* _dep_name[TYPE_LIMIT]; duke@435: static int _dep_args[TYPE_LIMIT]; duke@435: duke@435: static bool dept_in_mask(DepType dept, int mask) { duke@435: return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<ident(); duke@435: assert(_dep_seen != NULL, "deps must be writable"); duke@435: int seen = _dep_seen->at_grow(x_id, 0); duke@435: _dep_seen->at_put(x_id, seen | (1<* deps, duke@435: int ctxk_i, ciKlass* ctxk); duke@435: duke@435: void sort_all_deps(); duke@435: size_t estimate_size_in_bytes(); duke@435: duke@435: // Initialize _deps, etc. duke@435: void initialize(ciEnv* env); duke@435: duke@435: // State for making a new set of dependencies: duke@435: OopRecorder* _oop_recorder; duke@435: duke@435: // Logging support duke@435: CompileLog* _log; duke@435: duke@435: address _content_bytes; // everything but the oop references, encoded duke@435: size_t _size_in_bytes; duke@435: duke@435: public: duke@435: // Make a new empty dependencies set. duke@435: Dependencies(ciEnv* env) { duke@435: initialize(env); duke@435: } duke@435: duke@435: private: duke@435: // Check for a valid context type. duke@435: // Enforce the restriction against array types. duke@435: static void check_ctxk(ciKlass* ctxk) { duke@435: assert(ctxk->is_instance_klass(), "java types only"); duke@435: } duke@435: static void check_ctxk_concrete(ciKlass* ctxk) { duke@435: assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete"); duke@435: } duke@435: static void check_ctxk_abstract(ciKlass* ctxk) { duke@435: check_ctxk(ctxk); duke@435: assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract"); duke@435: } duke@435: duke@435: void assert_common_1(DepType dept, ciObject* x); duke@435: void assert_common_2(DepType dept, ciKlass* ctxk, ciObject* x); duke@435: void assert_common_3(DepType dept, ciKlass* ctxk, ciObject* x, ciObject* x2); duke@435: duke@435: public: duke@435: // Adding assertions to a new dependency set at compile time: duke@435: void assert_evol_method(ciMethod* m); duke@435: void assert_leaf_type(ciKlass* ctxk); duke@435: void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck); duke@435: void assert_abstract_with_no_concrete_subtype(ciKlass* ctxk); duke@435: void assert_concrete_with_no_concrete_subtype(ciKlass* ctxk); duke@435: void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm); duke@435: void assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2); duke@435: void assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2); duke@435: void assert_has_no_finalizable_subclasses(ciKlass* ctxk); duke@435: duke@435: // Define whether a given method or type is concrete. duke@435: // These methods define the term "concrete" as used in this module. duke@435: // For this module, an "abstract" class is one which is non-concrete. duke@435: // duke@435: // Future optimizations may allow some classes to remain duke@435: // non-concrete until their first instantiation, and allow some duke@435: // methods to remain non-concrete until their first invocation. duke@435: // In that case, there would be a middle ground between concrete duke@435: // and abstract (as defined by the Java language and VM). duke@435: static bool is_concrete_klass(klassOop k); // k is instantiable duke@435: static bool is_concrete_method(methodOop m); // m is invocable duke@435: static Klass* find_finalizable_subclass(Klass* k); duke@435: duke@435: // These versions of the concreteness queries work through the CI. duke@435: // The CI versions are allowed to skew sometimes from the VM duke@435: // (oop-based) versions. The cost of such a difference is a duke@435: // (safely) aborted compilation, or a deoptimization, or a missed duke@435: // optimization opportunity. duke@435: // duke@435: // In order to prevent spurious assertions, query results must duke@435: // remain stable within any single ciEnv instance. (I.e., they must duke@435: // not go back into the VM to get their value; they must cache the duke@435: // bit in the CI, either eagerly or lazily.) duke@435: static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable duke@435: static bool is_concrete_method(ciMethod* m); // m appears invocable duke@435: static bool has_finalizable_subclass(ciInstanceKlass* k); duke@435: duke@435: // As a general rule, it is OK to compile under the assumption that duke@435: // a given type or method is concrete, even if it at some future duke@435: // point becomes abstract. So dependency checking is one-sided, in duke@435: // that it permits supposedly concrete classes or methods to turn up duke@435: // as really abstract. (This shouldn't happen, except during class duke@435: // evolution, but that's the logic of the checking.) However, if a duke@435: // supposedly abstract class or method suddenly becomes concrete, a duke@435: // dependency on it must fail. duke@435: duke@435: // Checking old assertions at run-time (in the VM only): duke@435: static klassOop check_evol_method(methodOop m); duke@435: static klassOop check_leaf_type(klassOop ctxk); duke@435: static klassOop check_abstract_with_unique_concrete_subtype(klassOop ctxk, klassOop conck, duke@435: DepChange* changes = NULL); duke@435: static klassOop check_abstract_with_no_concrete_subtype(klassOop ctxk, duke@435: DepChange* changes = NULL); duke@435: static klassOop check_concrete_with_no_concrete_subtype(klassOop ctxk, duke@435: DepChange* changes = NULL); duke@435: static klassOop check_unique_concrete_method(klassOop ctxk, methodOop uniqm, duke@435: DepChange* changes = NULL); duke@435: static klassOop check_abstract_with_exclusive_concrete_subtypes(klassOop ctxk, klassOop k1, klassOop k2, duke@435: DepChange* changes = NULL); duke@435: static klassOop check_exclusive_concrete_methods(klassOop ctxk, methodOop m1, methodOop m2, duke@435: DepChange* changes = NULL); duke@435: static klassOop check_has_no_finalizable_subclasses(klassOop ctxk, duke@435: DepChange* changes = NULL); duke@435: // A returned klassOop is NULL if the dependency assertion is still duke@435: // valid. A non-NULL klassOop is a 'witness' to the assertion duke@435: // failure, a point in the class hierarchy where the assertion has duke@435: // been proven false. For example, if check_leaf_type returns duke@435: // non-NULL, the value is a subtype of the supposed leaf type. This duke@435: // witness value may be useful for logging the dependency failure. duke@435: // Note that, when a dependency fails, there may be several possible duke@435: // witnesses to the failure. The value returned from the check_foo duke@435: // method is chosen arbitrarily. duke@435: duke@435: // The 'changes' value, if non-null, requests a limited spot-check duke@435: // near the indicated recent changes in the class hierarchy. duke@435: // It is used by DepStream::spot_check_dependency_at. duke@435: duke@435: // Detecting possible new assertions: duke@435: static klassOop find_unique_concrete_subtype(klassOop ctxk); duke@435: static methodOop find_unique_concrete_method(klassOop ctxk, methodOop m); duke@435: static int find_exclusive_concrete_subtypes(klassOop ctxk, int klen, klassOop k[]); duke@435: static int find_exclusive_concrete_methods(klassOop ctxk, int mlen, methodOop m[]); duke@435: duke@435: // Create the encoding which will be stored in an nmethod. duke@435: void encode_content_bytes(); duke@435: duke@435: address content_bytes() { duke@435: assert(_content_bytes != NULL, "encode it first"); duke@435: return _content_bytes; duke@435: } duke@435: size_t size_in_bytes() { duke@435: assert(_content_bytes != NULL, "encode it first"); duke@435: return _size_in_bytes; duke@435: } duke@435: duke@435: OopRecorder* oop_recorder() { return _oop_recorder; } duke@435: CompileLog* log() { return _log; } duke@435: duke@435: void copy_to(nmethod* nm); duke@435: duke@435: void log_all_dependencies(); duke@435: void log_dependency(DepType dept, int nargs, ciObject* args[]) { duke@435: write_dependency_to(log(), dept, nargs, args); duke@435: } duke@435: void log_dependency(DepType dept, duke@435: ciObject* x0, duke@435: ciObject* x1 = NULL, duke@435: ciObject* x2 = NULL) { duke@435: if (log() == NULL) return; duke@435: ciObject* args[max_arg_count]; duke@435: args[0] = x0; duke@435: args[1] = x1; duke@435: args[2] = x2; duke@435: assert(2 < max_arg_count, ""); duke@435: log_dependency(dept, dep_args(dept), args); duke@435: } duke@435: duke@435: static void write_dependency_to(CompileLog* log, duke@435: DepType dept, duke@435: int nargs, ciObject* args[], duke@435: klassOop witness = NULL); duke@435: static void write_dependency_to(CompileLog* log, duke@435: DepType dept, duke@435: int nargs, oop args[], duke@435: klassOop witness = NULL); duke@435: static void write_dependency_to(xmlStream* xtty, duke@435: DepType dept, duke@435: int nargs, oop args[], duke@435: klassOop witness = NULL); duke@435: static void print_dependency(DepType dept, duke@435: int nargs, oop args[], duke@435: klassOop witness = NULL); duke@435: duke@435: private: duke@435: // helper for encoding common context types as zero: duke@435: static ciKlass* ctxk_encoded_as_null(DepType dept, ciObject* x); duke@435: duke@435: static klassOop ctxk_encoded_as_null(DepType dept, oop x); duke@435: duke@435: public: duke@435: // Use this to iterate over an nmethod's dependency set. duke@435: // Works on new and old dependency sets. duke@435: // Usage: duke@435: // duke@435: // ; duke@435: // Dependencies::DepType dept; duke@435: // for (Dependencies::DepStream deps(nm); deps.next(); ) { duke@435: // ... duke@435: // } duke@435: // duke@435: // The caller must be in the VM, since oops are not wrapped in handles. duke@435: class DepStream { duke@435: private: duke@435: nmethod* _code; // null if in a compiler thread duke@435: Dependencies* _deps; // null if not in a compiler thread duke@435: CompressedReadStream _bytes; duke@435: #ifdef ASSERT duke@435: size_t _byte_limit; duke@435: #endif duke@435: duke@435: // iteration variables: duke@435: DepType _type; duke@435: int _xi[max_arg_count+1]; duke@435: duke@435: void initial_asserts(size_t byte_limit) NOT_DEBUG({}); duke@435: duke@435: inline oop recorded_oop_at(int i); duke@435: // => _code? _code->oop_at(i): *_deps->_oop_recorder->handle_at(i) duke@435: duke@435: klassOop check_dependency_impl(DepChange* changes); duke@435: duke@435: public: duke@435: DepStream(Dependencies* deps) duke@435: : _deps(deps), duke@435: _code(NULL), duke@435: _bytes(deps->content_bytes()) duke@435: { duke@435: initial_asserts(deps->size_in_bytes()); duke@435: } duke@435: DepStream(nmethod* code) duke@435: : _deps(NULL), duke@435: _code(code), duke@435: _bytes(code->dependencies_begin()) duke@435: { duke@435: initial_asserts(code->dependencies_size()); duke@435: } duke@435: duke@435: bool next(); duke@435: duke@435: DepType type() { return _type; } duke@435: int argument_count() { return dep_args(type()); } duke@435: int argument_index(int i) { assert(0 <= i && i < argument_count(), "oob"); duke@435: return _xi[i]; } duke@435: oop argument(int i); // => recorded_oop_at(argument_index(i)) duke@435: klassOop context_type(); duke@435: duke@435: methodOop method_argument(int i) { duke@435: oop x = argument(i); duke@435: assert(x->is_method(), "type"); duke@435: return (methodOop) x; duke@435: } duke@435: klassOop type_argument(int i) { duke@435: oop x = argument(i); duke@435: assert(x->is_klass(), "type"); duke@435: return (klassOop) x; duke@435: } duke@435: duke@435: // The point of the whole exercise: Is this dep is still OK? duke@435: klassOop check_dependency() { duke@435: return check_dependency_impl(NULL); duke@435: } duke@435: // A lighter version: Checks only around recent changes in a class duke@435: // hierarchy. (See Universe::flush_dependents_on.) duke@435: klassOop spot_check_dependency_at(DepChange& changes); duke@435: duke@435: // Log the current dependency to xtty or compilation log. duke@435: void log_dependency(klassOop witness = NULL); duke@435: duke@435: // Print the current dependency to tty. duke@435: void print_dependency(klassOop witness = NULL, bool verbose = false); duke@435: }; duke@435: friend class Dependencies::DepStream; duke@435: duke@435: static void print_statistics() PRODUCT_RETURN; duke@435: }; duke@435: duke@435: // A class hierarchy change coming through the VM (under the Compile_lock). duke@435: // The change is structured as a single new type with any number of supers duke@435: // and implemented interface types. Other than the new type, any of the duke@435: // super types can be context types for a relevant dependency, which the duke@435: // new type could invalidate. duke@435: class DepChange : public StackObj { phh@1558: public: duke@435: enum ChangeType { duke@435: NO_CHANGE = 0, // an uninvolved klass duke@435: Change_new_type, // a newly loaded type duke@435: Change_new_sub, // a super with a new subtype duke@435: Change_new_impl, // an interface with a new implementation duke@435: CHANGE_LIMIT, duke@435: Start_Klass = CHANGE_LIMIT // internal indicator for ContextStream duke@435: }; duke@435: phh@1558: private: duke@435: // each change set is rooted in exactly one new type (at present): duke@435: KlassHandle _new_type; duke@435: duke@435: void initialize(); duke@435: duke@435: public: duke@435: // notes the new type, marks it and all its super-types duke@435: DepChange(KlassHandle new_type) duke@435: : _new_type(new_type) duke@435: { duke@435: initialize(); duke@435: } duke@435: duke@435: // cleans up the marks duke@435: ~DepChange(); duke@435: duke@435: klassOop new_type() { return _new_type(); } duke@435: duke@435: // involves_context(k) is true if k is new_type or any of the super types duke@435: bool involves_context(klassOop k); duke@435: duke@435: // Usage: duke@435: // for (DepChange::ContextStream str(changes); str.next(); ) { duke@435: // klassOop k = str.klass(); duke@435: // switch (str.change_type()) { duke@435: // ... duke@435: // } duke@435: // } duke@435: class ContextStream : public StackObj { duke@435: private: phh@1558: DepChange& _changes; duke@435: friend class DepChange; duke@435: duke@435: // iteration variables: phh@1558: ChangeType _change_type; phh@1558: klassOop _klass; phh@1558: objArrayOop _ti_base; // i.e., transitive_interfaces phh@1558: int _ti_index; phh@1558: int _ti_limit; duke@435: duke@435: // start at the beginning: duke@435: void start() { duke@435: klassOop new_type = _changes.new_type(); duke@435: _change_type = (new_type == NULL ? NO_CHANGE: Start_Klass); duke@435: _klass = new_type; duke@435: _ti_base = NULL; duke@435: _ti_index = 0; duke@435: _ti_limit = 0; duke@435: } duke@435: phh@1558: public: duke@435: ContextStream(DepChange& changes) duke@435: : _changes(changes) duke@435: { start(); } duke@435: duke@435: ContextStream(DepChange& changes, No_Safepoint_Verifier& nsv) duke@435: : _changes(changes) duke@435: // the nsv argument makes it safe to hold oops like _klass duke@435: { start(); } duke@435: duke@435: bool next(); duke@435: phh@1558: ChangeType change_type() { return _change_type; } duke@435: klassOop klass() { return _klass; } duke@435: }; duke@435: friend class DepChange::ContextStream; duke@435: duke@435: void print(); duke@435: };