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