Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/dependencies.hpp
32285 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_CODE_DEPENDENCIES_HPP25#define SHARE_VM_CODE_DEPENDENCIES_HPP2627#include "ci/ciCallSite.hpp"28#include "ci/ciKlass.hpp"29#include "ci/ciMethodHandle.hpp"30#include "classfile/systemDictionary.hpp"31#include "code/compressedStream.hpp"32#include "code/nmethod.hpp"33#include "utilities/growableArray.hpp"3435//** Dependencies represent assertions (approximate invariants) within36// the runtime system, e.g. class hierarchy changes. An example is an37// assertion that a given method is not overridden; another example is38// that a type has only one concrete subtype. Compiled code which39// relies on such assertions must be discarded if they are overturned40// by changes in the runtime system. We can think of these assertions41// as approximate invariants, because we expect them to be overturned42// very infrequently. We are willing to perform expensive recovery43// operations when they are overturned. The benefit, of course, is44// performing optimistic optimizations (!) on the object code.45//46// Changes in the class hierarchy due to dynamic linking or47// class evolution can violate dependencies. There is enough48// indexing between classes and nmethods to make dependency49// checking reasonably efficient.5051class ciEnv;52class nmethod;53class OopRecorder;54class xmlStream;55class CompileLog;56class DepChange;57class KlassDepChange;58class CallSiteDepChange;59class No_Safepoint_Verifier;6061class Dependencies: public ResourceObj {62public:63// Note: In the comments on dependency types, most uses of the terms64// subtype and supertype are used in a "non-strict" or "inclusive"65// sense, and are starred to remind the reader of this fact.66// Strict uses of the terms use the word "proper".67//68// Specifically, every class is its own subtype* and supertype*.69// (This trick is easier than continually saying things like "Y is a70// subtype of X or X itself".)71//72// Sometimes we write X > Y to mean X is a proper supertype of Y.73// The notation X > {Y, Z} means X has proper subtypes Y, Z.74// The notation X.m > Y means that Y inherits m from X, while75// X.m > Y.m means Y overrides X.m. A star denotes abstractness,76// as *I > A, meaning (abstract) interface I is a super type of A,77// or A.*m > B.m, meaning B.m implements abstract method A.m.78//79// In this module, the terms "subtype" and "supertype" refer to80// Java-level reference type conversions, as detected by81// "instanceof" and performed by "checkcast" operations. The method82// Klass::is_subtype_of tests these relations. Note that "subtype"83// is richer than "subclass" (as tested by Klass::is_subclass_of),84// since it takes account of relations involving interface and array85// types.86//87// To avoid needless complexity, dependencies involving array types88// are not accepted. If you need to make an assertion about an89// array type, make the assertion about its corresponding element90// types. Any assertion that might change about an array type can91// be converted to an assertion about its element type.92//93// Most dependencies are evaluated over a "context type" CX, which94// stands for the set Subtypes(CX) of every Java type that is a subtype*95// of CX. When the system loads a new class or interface N, it is96// responsible for re-evaluating changed dependencies whose context97// type now includes N, that is, all super types of N.98//99enum DepType {100end_marker = 0,101102// An 'evol' dependency simply notes that the contents of the103// method were used. If it evolves (is replaced), the nmethod104// must be recompiled. No other dependencies are implied.105evol_method,106FIRST_TYPE = evol_method,107108// A context type CX is a leaf it if has no proper subtype.109leaf_type,110111// An abstract class CX has exactly one concrete subtype CC.112abstract_with_unique_concrete_subtype,113114// The type CX is purely abstract, with no concrete subtype* at all.115abstract_with_no_concrete_subtype,116117// The concrete CX is free of concrete proper subtypes.118concrete_with_no_concrete_subtype,119120// Given a method M1 and a context class CX, the set MM(CX, M1) of121// "concrete matching methods" in CX of M1 is the set of every122// concrete M2 for which it is possible to create an invokevirtual123// or invokeinterface call site that can reach either M1 or M2.124// That is, M1 and M2 share a name, signature, and vtable index.125// We wish to notice when the set MM(CX, M1) is just {M1}, or126// perhaps a set of two {M1,M2}, and issue dependencies on this.127128// The set MM(CX, M1) can be computed by starting with any matching129// concrete M2 that is inherited into CX, and then walking the130// subtypes* of CX looking for concrete definitions.131132// The parameters to this dependency are the method M1 and the133// context class CX. M1 must be either inherited in CX or defined134// in a subtype* of CX. It asserts that MM(CX, M1) is no greater135// than {M1}.136unique_concrete_method, // one unique concrete method under CX137138// An "exclusive" assertion concerns two methods or subtypes, and139// declares that there are at most two (or perhaps later N>2)140// specific items that jointly satisfy the restriction.141// We list all items explicitly rather than just giving their142// count, for robustness in the face of complex schema changes.143144// A context class CX (which may be either abstract or concrete)145// has two exclusive concrete subtypes* C1, C2 if every concrete146// subtype* of CX is either C1 or C2. Note that if neither C1 or C2147// are equal to CX, then CX itself must be abstract. But it is148// also possible (for example) that C1 is CX (a concrete class)149// and C2 is a proper subtype of C1.150abstract_with_exclusive_concrete_subtypes_2,151152// This dependency asserts that MM(CX, M1) is no greater than {M1,M2}.153exclusive_concrete_methods_2,154155// This dependency asserts that no instances of class or it's156// subclasses require finalization registration.157no_finalizable_subclasses,158159// This dependency asserts when the CallSite.target value changed.160call_site_target_value,161162TYPE_LIMIT163};164enum {165LG2_TYPE_LIMIT = 4, // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))166167// handy categorizations of dependency types:168all_types = ((1 << TYPE_LIMIT) - 1) & ((-1) << FIRST_TYPE),169170non_klass_types = (1 << call_site_target_value),171klass_types = all_types & ~non_klass_types,172173non_ctxk_types = (1 << evol_method),174implicit_ctxk_types = (1 << call_site_target_value),175explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),176177max_arg_count = 3, // current maximum number of arguments (incl. ctxk)178179// A "context type" is a class or interface that180// provides context for evaluating a dependency.181// When present, it is one of the arguments (dep_context_arg).182//183// If a dependency does not have a context type, there is a184// default context, depending on the type of the dependency.185// This bit signals that a default context has been compressed away.186default_context_type_bit = (1<<LG2_TYPE_LIMIT)187};188189static const char* dep_name(DepType dept);190static int dep_args(DepType dept);191192static bool is_klass_type( DepType dept) { return dept_in_mask(dept, klass_types ); }193194static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }195static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }196197static int dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }198static int dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }199200static void check_valid_dependency_type(DepType dept);201202private:203// State for writing a new set of dependencies:204GrowableArray<int>* _dep_seen; // (seen[h->ident] & (1<<dept))205GrowableArray<ciBaseObject*>* _deps[TYPE_LIMIT];206207static const char* _dep_name[TYPE_LIMIT];208static int _dep_args[TYPE_LIMIT];209210static bool dept_in_mask(DepType dept, int mask) {211return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;212}213214bool note_dep_seen(int dept, ciBaseObject* x) {215assert(dept < BitsPerInt, "oob");216int x_id = x->ident();217assert(_dep_seen != NULL, "deps must be writable");218int seen = _dep_seen->at_grow(x_id, 0);219_dep_seen->at_put(x_id, seen | (1<<dept));220// return true if we've already seen dept/x221return (seen & (1<<dept)) != 0;222}223224bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,225int ctxk_i, ciKlass* ctxk);226227void sort_all_deps();228size_t estimate_size_in_bytes();229230// Initialize _deps, etc.231void initialize(ciEnv* env);232233// State for making a new set of dependencies:234OopRecorder* _oop_recorder;235236// Logging support237CompileLog* _log;238239address _content_bytes; // everything but the oop references, encoded240size_t _size_in_bytes;241242public:243// Make a new empty dependencies set.244Dependencies(ciEnv* env) {245initialize(env);246}247248private:249// Check for a valid context type.250// Enforce the restriction against array types.251static void check_ctxk(ciKlass* ctxk) {252assert(ctxk->is_instance_klass(), "java types only");253}254static void check_ctxk_concrete(ciKlass* ctxk) {255assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");256}257static void check_ctxk_abstract(ciKlass* ctxk) {258check_ctxk(ctxk);259assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");260}261262void assert_common_1(DepType dept, ciBaseObject* x);263void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);264void assert_common_3(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2);265266public:267// Adding assertions to a new dependency set at compile time:268void assert_evol_method(ciMethod* m);269void assert_leaf_type(ciKlass* ctxk);270void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);271void assert_abstract_with_no_concrete_subtype(ciKlass* ctxk);272void assert_concrete_with_no_concrete_subtype(ciKlass* ctxk);273void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm);274void assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2);275void assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2);276void assert_has_no_finalizable_subclasses(ciKlass* ctxk);277void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);278279// Define whether a given method or type is concrete.280// These methods define the term "concrete" as used in this module.281// For this module, an "abstract" class is one which is non-concrete.282//283// Future optimizations may allow some classes to remain284// non-concrete until their first instantiation, and allow some285// methods to remain non-concrete until their first invocation.286// In that case, there would be a middle ground between concrete287// and abstract (as defined by the Java language and VM).288static bool is_concrete_klass(Klass* k); // k is instantiable289static bool is_concrete_method(Method* m, Klass* k); // m is invocable290static Klass* find_finalizable_subclass(Klass* k);291292// These versions of the concreteness queries work through the CI.293// The CI versions are allowed to skew sometimes from the VM294// (oop-based) versions. The cost of such a difference is a295// (safely) aborted compilation, or a deoptimization, or a missed296// optimization opportunity.297//298// In order to prevent spurious assertions, query results must299// remain stable within any single ciEnv instance. (I.e., they must300// not go back into the VM to get their value; they must cache the301// bit in the CI, either eagerly or lazily.)302static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable303static bool has_finalizable_subclass(ciInstanceKlass* k);304305// As a general rule, it is OK to compile under the assumption that306// a given type or method is concrete, even if it at some future307// point becomes abstract. So dependency checking is one-sided, in308// that it permits supposedly concrete classes or methods to turn up309// as really abstract. (This shouldn't happen, except during class310// evolution, but that's the logic of the checking.) However, if a311// supposedly abstract class or method suddenly becomes concrete, a312// dependency on it must fail.313314// Checking old assertions at run-time (in the VM only):315static Klass* check_evol_method(Method* m);316static Klass* check_leaf_type(Klass* ctxk);317static Klass* check_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck,318KlassDepChange* changes = NULL);319static Klass* check_abstract_with_no_concrete_subtype(Klass* ctxk,320KlassDepChange* changes = NULL);321static Klass* check_concrete_with_no_concrete_subtype(Klass* ctxk,322KlassDepChange* changes = NULL);323static Klass* check_unique_concrete_method(Klass* ctxk, Method* uniqm,324KlassDepChange* changes = NULL);325static Klass* check_abstract_with_exclusive_concrete_subtypes(Klass* ctxk, Klass* k1, Klass* k2,326KlassDepChange* changes = NULL);327static Klass* check_exclusive_concrete_methods(Klass* ctxk, Method* m1, Method* m2,328KlassDepChange* changes = NULL);329static Klass* check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes = NULL);330static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL);331// A returned Klass* is NULL if the dependency assertion is still332// valid. A non-NULL Klass* is a 'witness' to the assertion333// failure, a point in the class hierarchy where the assertion has334// been proven false. For example, if check_leaf_type returns335// non-NULL, the value is a subtype of the supposed leaf type. This336// witness value may be useful for logging the dependency failure.337// Note that, when a dependency fails, there may be several possible338// witnesses to the failure. The value returned from the check_foo339// method is chosen arbitrarily.340341// The 'changes' value, if non-null, requests a limited spot-check342// near the indicated recent changes in the class hierarchy.343// It is used by DepStream::spot_check_dependency_at.344345// Detecting possible new assertions:346static Klass* find_unique_concrete_subtype(Klass* ctxk);347static Method* find_unique_concrete_method(Klass* ctxk, Method* m);348static int find_exclusive_concrete_subtypes(Klass* ctxk, int klen, Klass* k[]);349350// Create the encoding which will be stored in an nmethod.351void encode_content_bytes();352353address content_bytes() {354assert(_content_bytes != NULL, "encode it first");355return _content_bytes;356}357size_t size_in_bytes() {358assert(_content_bytes != NULL, "encode it first");359return _size_in_bytes;360}361362OopRecorder* oop_recorder() { return _oop_recorder; }363CompileLog* log() { return _log; }364365void copy_to(nmethod* nm);366367void log_all_dependencies();368369void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) {370ResourceMark rm;371int argslen = args->length();372write_dependency_to(log(), dept, args);373guarantee(argslen == args->length(),374"args array cannot grow inside nested ResoureMark scope");375}376377void log_dependency(DepType dept,378ciBaseObject* x0,379ciBaseObject* x1 = NULL,380ciBaseObject* x2 = NULL) {381if (log() == NULL) {382return;383}384ResourceMark rm;385GrowableArray<ciBaseObject*>* ciargs =386new GrowableArray<ciBaseObject*>(dep_args(dept));387assert (x0 != NULL, "no log x0");388ciargs->push(x0);389390if (x1 != NULL) {391ciargs->push(x1);392}393if (x2 != NULL) {394ciargs->push(x2);395}396assert(ciargs->length() == dep_args(dept), "");397log_dependency(dept, ciargs);398}399400class DepArgument : public ResourceObj {401private:402bool _is_oop;403bool _valid;404void* _value;405public:406DepArgument() : _is_oop(false), _value(NULL), _valid(false) {}407DepArgument(oop v): _is_oop(true), _value(v), _valid(true) {}408DepArgument(Metadata* v): _is_oop(false), _value(v), _valid(true) {}409410bool is_null() const { return _value == NULL; }411bool is_oop() const { return _is_oop; }412bool is_metadata() const { return !_is_oop; }413bool is_klass() const { return is_metadata() && metadata_value()->is_klass(); }414bool is_method() const { return is_metadata() && metadata_value()->is_method(); }415416oop oop_value() const { assert(_is_oop && _valid, "must be"); return (oop) _value; }417Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }418};419420static void print_dependency(DepType dept,421GrowableArray<DepArgument>* args,422Klass* witness = NULL);423424private:425// helper for encoding common context types as zero:426static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);427428static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);429430static void write_dependency_to(CompileLog* log,431DepType dept,432GrowableArray<ciBaseObject*>* args,433Klass* witness = NULL);434static void write_dependency_to(CompileLog* log,435DepType dept,436GrowableArray<DepArgument>* args,437Klass* witness = NULL);438static void write_dependency_to(xmlStream* xtty,439DepType dept,440GrowableArray<DepArgument>* args,441Klass* witness = NULL);442public:443// Use this to iterate over an nmethod's dependency set.444// Works on new and old dependency sets.445// Usage:446//447// ;448// Dependencies::DepType dept;449// for (Dependencies::DepStream deps(nm); deps.next(); ) {450// ...451// }452//453// The caller must be in the VM, since oops are not wrapped in handles.454class DepStream {455private:456nmethod* _code; // null if in a compiler thread457Dependencies* _deps; // null if not in a compiler thread458CompressedReadStream _bytes;459#ifdef ASSERT460size_t _byte_limit;461#endif462463// iteration variables:464DepType _type;465int _xi[max_arg_count+1];466467void initial_asserts(size_t byte_limit) NOT_DEBUG({});468469inline Metadata* recorded_metadata_at(int i);470inline oop recorded_oop_at(int i);471472Klass* check_klass_dependency(KlassDepChange* changes);473Klass* check_call_site_dependency(CallSiteDepChange* changes);474475void trace_and_log_witness(Klass* witness);476477public:478DepStream(Dependencies* deps)479: _deps(deps),480_code(NULL),481_bytes(deps->content_bytes())482{483initial_asserts(deps->size_in_bytes());484}485DepStream(nmethod* code)486: _deps(NULL),487_code(code),488_bytes(code->dependencies_begin())489{490initial_asserts(code->dependencies_size());491}492493bool next();494495DepType type() { return _type; }496int argument_count() { return dep_args(type()); }497int argument_index(int i) { assert(0 <= i && i < argument_count(), "oob");498return _xi[i]; }499Metadata* argument(int i); // => recorded_oop_at(argument_index(i))500oop argument_oop(int i); // => recorded_oop_at(argument_index(i))501Klass* context_type();502503bool is_klass_type() { return Dependencies::is_klass_type(type()); }504505Method* method_argument(int i) {506Metadata* x = argument(i);507assert(x->is_method(), "type");508return (Method*) x;509}510Klass* type_argument(int i) {511Metadata* x = argument(i);512assert(x->is_klass(), "type");513return (Klass*) x;514}515516// The point of the whole exercise: Is this dep still OK?517Klass* check_dependency() {518Klass* result = check_klass_dependency(NULL);519if (result != NULL) return result;520return check_call_site_dependency(NULL);521}522523// A lighter version: Checks only around recent changes in a class524// hierarchy. (See Universe::flush_dependents_on.)525Klass* spot_check_dependency_at(DepChange& changes);526527// Log the current dependency to xtty or compilation log.528void log_dependency(Klass* witness = NULL);529530// Print the current dependency to tty.531void print_dependency(Klass* witness = NULL, bool verbose = false);532};533friend class Dependencies::DepStream;534535static void print_statistics() PRODUCT_RETURN;536};537538539// Every particular DepChange is a sub-class of this class.540class DepChange : public StackObj {541public:542// What kind of DepChange is this?543virtual bool is_klass_change() const { return false; }544virtual bool is_call_site_change() const { return false; }545546// Subclass casting with assertions.547KlassDepChange* as_klass_change() {548assert(is_klass_change(), "bad cast");549return (KlassDepChange*) this;550}551CallSiteDepChange* as_call_site_change() {552assert(is_call_site_change(), "bad cast");553return (CallSiteDepChange*) this;554}555556void print();557558public:559enum ChangeType {560NO_CHANGE = 0, // an uninvolved klass561Change_new_type, // a newly loaded type562Change_new_sub, // a super with a new subtype563Change_new_impl, // an interface with a new implementation564CHANGE_LIMIT,565Start_Klass = CHANGE_LIMIT // internal indicator for ContextStream566};567568// Usage:569// for (DepChange::ContextStream str(changes); str.next(); ) {570// Klass* k = str.klass();571// switch (str.change_type()) {572// ...573// }574// }575class ContextStream : public StackObj {576private:577DepChange& _changes;578friend class DepChange;579580// iteration variables:581ChangeType _change_type;582Klass* _klass;583Array<Klass*>* _ti_base; // i.e., transitive_interfaces584int _ti_index;585int _ti_limit;586587// start at the beginning:588void start();589590public:591ContextStream(DepChange& changes)592: _changes(changes)593{ start(); }594595ContextStream(DepChange& changes, No_Safepoint_Verifier& nsv)596: _changes(changes)597// the nsv argument makes it safe to hold oops like _klass598{ start(); }599600bool next();601602ChangeType change_type() { return _change_type; }603Klass* klass() { return _klass; }604};605friend class DepChange::ContextStream;606};607608609// A class hierarchy change coming through the VM (under the Compile_lock).610// The change is structured as a single new type with any number of supers611// and implemented interface types. Other than the new type, any of the612// super types can be context types for a relevant dependency, which the613// new type could invalidate.614class KlassDepChange : public DepChange {615private:616// each change set is rooted in exactly one new type (at present):617KlassHandle _new_type;618619void initialize();620621public:622// notes the new type, marks it and all its super-types623KlassDepChange(KlassHandle new_type)624: _new_type(new_type)625{626initialize();627}628629// cleans up the marks630~KlassDepChange();631632// What kind of DepChange is this?633virtual bool is_klass_change() const { return true; }634635Klass* new_type() { return _new_type(); }636637// involves_context(k) is true if k is new_type or any of the super types638bool involves_context(Klass* k);639};640641642// A CallSite has changed its target.643class CallSiteDepChange : public DepChange {644private:645Handle _call_site;646Handle _method_handle;647648public:649CallSiteDepChange(Handle call_site, Handle method_handle)650: _call_site(call_site),651_method_handle(method_handle)652{653assert(_call_site() ->is_a(SystemDictionary::CallSite_klass()), "must be");654assert(_method_handle()->is_a(SystemDictionary::MethodHandle_klass()), "must be");655}656657// What kind of DepChange is this?658virtual bool is_call_site_change() const { return true; }659660oop call_site() const { return _call_site(); }661oop method_handle() const { return _method_handle(); }662};663664#endif // SHARE_VM_CODE_DEPENDENCIES_HPP665666667