Path: blob/master/src/hotspot/share/code/dependencies.hpp
40930 views
/*1* Copyright (c) 2005, 2021, 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_CODE_DEPENDENCIES_HPP25#define SHARE_CODE_DEPENDENCIES_HPP2627#include "ci/ciCallSite.hpp"28#include "ci/ciKlass.hpp"29#include "ci/ciMethod.hpp"30#include "ci/ciMethodHandle.hpp"31#include "code/compressedStream.hpp"32#include "code/nmethod.hpp"33#include "memory/resourceArea.hpp"34#include "runtime/safepointVerifiers.hpp"35#include "utilities/growableArray.hpp"36#include "utilities/hashtable.hpp"3738//** Dependencies represent assertions (approximate invariants) within39// the runtime system, e.g. class hierarchy changes. An example is an40// assertion that a given method is not overridden; another example is41// that a type has only one concrete subtype. Compiled code which42// relies on such assertions must be discarded if they are overturned43// by changes in the runtime system. We can think of these assertions44// as approximate invariants, because we expect them to be overturned45// very infrequently. We are willing to perform expensive recovery46// operations when they are overturned. The benefit, of course, is47// performing optimistic optimizations (!) on the object code.48//49// Changes in the class hierarchy due to dynamic linking or50// class evolution can violate dependencies. There is enough51// indexing between classes and nmethods to make dependency52// checking reasonably efficient.5354class ciEnv;55class nmethod;56class OopRecorder;57class xmlStream;58class CompileLog;59class CompileTask;60class DepChange;61class KlassDepChange;62class NewKlassDepChange;63class KlassInitDepChange;64class CallSiteDepChange;65class NoSafepointVerifier;6667class Dependencies: public ResourceObj {68public:69// Note: In the comments on dependency types, most uses of the terms70// subtype and supertype are used in a "non-strict" or "inclusive"71// sense, and are starred to remind the reader of this fact.72// Strict uses of the terms use the word "proper".73//74// Specifically, every class is its own subtype* and supertype*.75// (This trick is easier than continually saying things like "Y is a76// subtype of X or X itself".)77//78// Sometimes we write X > Y to mean X is a proper supertype of Y.79// The notation X > {Y, Z} means X has proper subtypes Y, Z.80// The notation X.m > Y means that Y inherits m from X, while81// X.m > Y.m means Y overrides X.m. A star denotes abstractness,82// as *I > A, meaning (abstract) interface I is a super type of A,83// or A.*m > B.m, meaning B.m implements abstract method A.m.84//85// In this module, the terms "subtype" and "supertype" refer to86// Java-level reference type conversions, as detected by87// "instanceof" and performed by "checkcast" operations. The method88// Klass::is_subtype_of tests these relations. Note that "subtype"89// is richer than "subclass" (as tested by Klass::is_subclass_of),90// since it takes account of relations involving interface and array91// types.92//93// To avoid needless complexity, dependencies involving array types94// are not accepted. If you need to make an assertion about an95// array type, make the assertion about its corresponding element96// types. Any assertion that might change about an array type can97// be converted to an assertion about its element type.98//99// Most dependencies are evaluated over a "context type" CX, which100// stands for the set Subtypes(CX) of every Java type that is a subtype*101// of CX. When the system loads a new class or interface N, it is102// responsible for re-evaluating changed dependencies whose context103// type now includes N, that is, all super types of N.104//105enum DepType {106end_marker = 0,107108// An 'evol' dependency simply notes that the contents of the109// method were used. If it evolves (is replaced), the nmethod110// must be recompiled. No other dependencies are implied.111evol_method,112FIRST_TYPE = evol_method,113114// A context type CX is a leaf it if has no proper subtype.115leaf_type,116117// An abstract class CX has exactly one concrete subtype CC.118abstract_with_unique_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_2, // one unique concrete method under CX137138// In addition to the method M1 and the context class CX, the parameters139// to this dependency are the resolved class RC1 and the140// resolved method RM1. It asserts that MM(CX, M1, RC1, RM1)141// is no greater than {M1}. RC1 and RM1 are used to improve the precision142// of the analysis.143unique_concrete_method_4, // one unique concrete method under CX144145// This dependency asserts that no instances of class or it's146// subclasses require finalization registration.147no_finalizable_subclasses,148149// This dependency asserts when the CallSite.target value changed.150call_site_target_value,151152TYPE_LIMIT153};154enum {155LG2_TYPE_LIMIT = 4, // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))156157// handy categorizations of dependency types:158all_types = ((1 << TYPE_LIMIT) - 1) & ((~0u) << FIRST_TYPE),159160non_klass_types = (1 << call_site_target_value),161klass_types = all_types & ~non_klass_types,162163non_ctxk_types = (1 << evol_method) | (1 << call_site_target_value),164implicit_ctxk_types = 0,165explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),166167max_arg_count = 4, // current maximum number of arguments (incl. ctxk)168169// A "context type" is a class or interface that170// provides context for evaluating a dependency.171// When present, it is one of the arguments (dep_context_arg).172//173// If a dependency does not have a context type, there is a174// default context, depending on the type of the dependency.175// This bit signals that a default context has been compressed away.176default_context_type_bit = (1<<LG2_TYPE_LIMIT)177};178179static const char* dep_name(DepType dept);180static int dep_args(DepType dept);181182static bool is_klass_type( DepType dept) { return dept_in_mask(dept, klass_types ); }183184static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }185static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }186187static int dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }188static int dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }189190static void check_valid_dependency_type(DepType dept);191192#if INCLUDE_JVMCI193// A Metadata* or object value recorded in an OopRecorder194class DepValue {195private:196// Unique identifier of the value within the associated OopRecorder that197// encodes both the category of the value (0: invalid, positive: metadata, negative: object)198// and the index within a category specific array (metadata: index + 1, object: -(index + 1))199int _id;200201public:202DepValue() : _id(0) {}203DepValue(OopRecorder* rec, Metadata* metadata, DepValue* candidate = NULL) {204assert(candidate == NULL || candidate->is_metadata(), "oops");205if (candidate != NULL && candidate->as_metadata(rec) == metadata) {206_id = candidate->_id;207} else {208_id = rec->find_index(metadata) + 1;209}210}211DepValue(OopRecorder* rec, jobject obj, DepValue* candidate = NULL) {212assert(candidate == NULL || candidate->is_object(), "oops");213if (candidate != NULL && candidate->as_object(rec) == obj) {214_id = candidate->_id;215} else {216_id = -(rec->find_index(obj) + 1);217}218}219220// Used to sort values in ascending order of index() with metadata values preceding object values221int sort_key() const { return -_id; }222223bool operator == (const DepValue& other) const { return other._id == _id; }224225bool is_valid() const { return _id != 0; }226int index() const { assert(is_valid(), "oops"); return _id < 0 ? -(_id + 1) : _id - 1; }227bool is_metadata() const { assert(is_valid(), "oops"); return _id > 0; }228bool is_object() const { assert(is_valid(), "oops"); return _id < 0; }229230Metadata* as_metadata(OopRecorder* rec) const { assert(is_metadata(), "oops"); return rec->metadata_at(index()); }231Klass* as_klass(OopRecorder* rec) const {232Metadata* m = as_metadata(rec);233assert(m != NULL, "as_metadata returned NULL");234assert(m->is_klass(), "oops");235return (Klass*) m;236}237Method* as_method(OopRecorder* rec) const {238Metadata* m = as_metadata(rec);239assert(m != NULL, "as_metadata returned NULL");240assert(m->is_method(), "oops");241return (Method*) m;242}243jobject as_object(OopRecorder* rec) const { assert(is_object(), "oops"); return rec->oop_at(index()); }244};245#endif // INCLUDE_JVMCI246247private:248// State for writing a new set of dependencies:249GrowableArray<int>* _dep_seen; // (seen[h->ident] & (1<<dept))250GrowableArray<ciBaseObject*>* _deps[TYPE_LIMIT];251#if INCLUDE_JVMCI252bool _using_dep_values;253GrowableArray<DepValue>* _dep_values[TYPE_LIMIT];254#endif255256static const char* _dep_name[TYPE_LIMIT];257static int _dep_args[TYPE_LIMIT];258259static bool dept_in_mask(DepType dept, int mask) {260return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;261}262263bool note_dep_seen(int dept, ciBaseObject* x) {264assert(dept < BitsPerInt, "oob");265int x_id = x->ident();266assert(_dep_seen != NULL, "deps must be writable");267int seen = _dep_seen->at_grow(x_id, 0);268_dep_seen->at_put(x_id, seen | (1<<dept));269// return true if we've already seen dept/x270return (seen & (1<<dept)) != 0;271}272273#if INCLUDE_JVMCI274bool note_dep_seen(int dept, DepValue x) {275assert(dept < BitsPerInt, "oops");276// place metadata deps at even indexes, object deps at odd indexes277int x_id = x.is_metadata() ? x.index() * 2 : (x.index() * 2) + 1;278assert(_dep_seen != NULL, "deps must be writable");279int seen = _dep_seen->at_grow(x_id, 0);280_dep_seen->at_put(x_id, seen | (1<<dept));281// return true if we've already seen dept/x282return (seen & (1<<dept)) != 0;283}284#endif285286bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,287int ctxk_i, ciKlass* ctxk);288#if INCLUDE_JVMCI289bool maybe_merge_ctxk(GrowableArray<DepValue>* deps,290int ctxk_i, DepValue ctxk);291#endif292293void sort_all_deps();294size_t estimate_size_in_bytes();295296// Initialize _deps, etc.297void initialize(ciEnv* env);298299// State for making a new set of dependencies:300OopRecorder* _oop_recorder;301302// Logging support303CompileLog* _log;304305address _content_bytes; // everything but the oop references, encoded306size_t _size_in_bytes;307308public:309// Make a new empty dependencies set.310Dependencies(ciEnv* env) {311initialize(env);312}313#if INCLUDE_JVMCI314Dependencies(Arena* arena, OopRecorder* oop_recorder, CompileLog* log);315#endif316317private:318// Check for a valid context type.319// Enforce the restriction against array types.320static void check_ctxk(ciKlass* ctxk) {321assert(ctxk->is_instance_klass(), "java types only");322}323static void check_ctxk_concrete(ciKlass* ctxk) {324assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");325}326static void check_ctxk_abstract(ciKlass* ctxk) {327check_ctxk(ctxk);328assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");329}330static void check_unique_method(ciKlass* ctxk, ciMethod* m) {331assert(!m->can_be_statically_bound(ctxk->as_instance_klass()), "redundant");332}333334void assert_common_1(DepType dept, ciBaseObject* x);335void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);336void assert_common_4(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2, ciBaseObject* x3);337338public:339// Adding assertions to a new dependency set at compile time:340void assert_evol_method(ciMethod* m);341void assert_leaf_type(ciKlass* ctxk);342void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);343void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm);344void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm, ciKlass* resolved_klass, ciMethod* resolved_method);345void assert_has_no_finalizable_subclasses(ciKlass* ctxk);346void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);347348#if INCLUDE_JVMCI349private:350static void check_ctxk(Klass* ctxk) {351assert(ctxk->is_instance_klass(), "java types only");352}353static void check_ctxk_abstract(Klass* ctxk) {354check_ctxk(ctxk);355assert(ctxk->is_abstract(), "must be abstract");356}357static void check_unique_method(Klass* ctxk, Method* m) {358assert(!m->can_be_statically_bound(InstanceKlass::cast(ctxk)), "redundant");359}360361void assert_common_1(DepType dept, DepValue x);362void assert_common_2(DepType dept, DepValue x0, DepValue x1);363364public:365void assert_evol_method(Method* m);366void assert_has_no_finalizable_subclasses(Klass* ctxk);367void assert_leaf_type(Klass* ctxk);368void assert_unique_concrete_method(Klass* ctxk, Method* uniqm);369void assert_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck);370void assert_call_site_target_value(oop callSite, oop methodHandle);371#endif // INCLUDE_JVMCI372373// Define whether a given method or type is concrete.374// These methods define the term "concrete" as used in this module.375// For this module, an "abstract" class is one which is non-concrete.376//377// Future optimizations may allow some classes to remain378// non-concrete until their first instantiation, and allow some379// methods to remain non-concrete until their first invocation.380// In that case, there would be a middle ground between concrete381// and abstract (as defined by the Java language and VM).382static bool is_concrete_klass(Klass* k); // k is instantiable383static bool is_concrete_method(Method* m, Klass* k); // m is invocable384static Klass* find_finalizable_subclass(InstanceKlass* ik);385386// These versions of the concreteness queries work through the CI.387// The CI versions are allowed to skew sometimes from the VM388// (oop-based) versions. The cost of such a difference is a389// (safely) aborted compilation, or a deoptimization, or a missed390// optimization opportunity.391//392// In order to prevent spurious assertions, query results must393// remain stable within any single ciEnv instance. (I.e., they must394// not go back into the VM to get their value; they must cache the395// bit in the CI, either eagerly or lazily.)396static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable397static bool has_finalizable_subclass(ciInstanceKlass* k);398399// As a general rule, it is OK to compile under the assumption that400// a given type or method is concrete, even if it at some future401// point becomes abstract. So dependency checking is one-sided, in402// that it permits supposedly concrete classes or methods to turn up403// as really abstract. (This shouldn't happen, except during class404// evolution, but that's the logic of the checking.) However, if a405// supposedly abstract class or method suddenly becomes concrete, a406// dependency on it must fail.407408// Checking old assertions at run-time (in the VM only):409static Klass* check_evol_method(Method* m);410static Klass* check_leaf_type(InstanceKlass* ctxk);411static Klass* check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk, Klass* conck, NewKlassDepChange* changes = NULL);412static Klass* check_unique_concrete_method(InstanceKlass* ctxk, Method* uniqm, NewKlassDepChange* changes = NULL);413static Klass* check_unique_concrete_method(InstanceKlass* ctxk, Method* uniqm, Klass* resolved_klass, Method* resolved_method, KlassDepChange* changes = NULL);414static Klass* check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes = NULL);415static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL);416// A returned Klass* is NULL if the dependency assertion is still417// valid. A non-NULL Klass* is a 'witness' to the assertion418// failure, a point in the class hierarchy where the assertion has419// been proven false. For example, if check_leaf_type returns420// non-NULL, the value is a subtype of the supposed leaf type. This421// witness value may be useful for logging the dependency failure.422// Note that, when a dependency fails, there may be several possible423// witnesses to the failure. The value returned from the check_foo424// method is chosen arbitrarily.425426// The 'changes' value, if non-null, requests a limited spot-check427// near the indicated recent changes in the class hierarchy.428// It is used by DepStream::spot_check_dependency_at.429430// Detecting possible new assertions:431static Klass* find_unique_concrete_subtype(InstanceKlass* ctxk);432static Method* find_unique_concrete_method(InstanceKlass* ctxk, Method* m,433Klass** participant = NULL); // out parameter434static Method* find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method);435436#ifdef ASSERT437static bool verify_method_context(InstanceKlass* ctxk, Method* m);438#endif // ASSERT439440// Create the encoding which will be stored in an nmethod.441void encode_content_bytes();442443address content_bytes() {444assert(_content_bytes != NULL, "encode it first");445return _content_bytes;446}447size_t size_in_bytes() {448assert(_content_bytes != NULL, "encode it first");449return _size_in_bytes;450}451452OopRecorder* oop_recorder() { return _oop_recorder; }453CompileLog* log() { return _log; }454455void copy_to(nmethod* nm);456457DepType validate_dependencies(CompileTask* task, char** failure_detail = NULL);458459void log_all_dependencies();460461void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) {462ResourceMark rm;463int argslen = args->length();464write_dependency_to(log(), dept, args);465guarantee(argslen == args->length(),466"args array cannot grow inside nested ResoureMark scope");467}468469void log_dependency(DepType dept,470ciBaseObject* x0,471ciBaseObject* x1 = NULL,472ciBaseObject* x2 = NULL,473ciBaseObject* x3 = NULL) {474if (log() == NULL) {475return;476}477ResourceMark rm;478GrowableArray<ciBaseObject*>* ciargs =479new GrowableArray<ciBaseObject*>(dep_args(dept));480assert (x0 != NULL, "no log x0");481ciargs->push(x0);482483if (x1 != NULL) {484ciargs->push(x1);485}486if (x2 != NULL) {487ciargs->push(x2);488}489if (x3 != NULL) {490ciargs->push(x3);491}492assert(ciargs->length() == dep_args(dept), "");493log_dependency(dept, ciargs);494}495496class DepArgument : public ResourceObj {497private:498bool _is_oop;499bool _valid;500void* _value;501public:502DepArgument() : _is_oop(false), _valid(false), _value(NULL) {}503DepArgument(oop v): _is_oop(true), _valid(true), _value(v) {}504DepArgument(Metadata* v): _is_oop(false), _valid(true), _value(v) {}505506bool is_null() const { return _value == NULL; }507bool is_oop() const { return _is_oop; }508bool is_metadata() const { return !_is_oop; }509bool is_klass() const { return is_metadata() && metadata_value()->is_klass(); }510bool is_method() const { return is_metadata() && metadata_value()->is_method(); }511512oop oop_value() const { assert(_is_oop && _valid, "must be"); return cast_to_oop(_value); }513Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }514};515516static void print_dependency(DepType dept,517GrowableArray<DepArgument>* args,518Klass* witness = NULL, outputStream* st = tty);519520private:521// helper for encoding common context types as zero:522static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);523524static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);525526static void write_dependency_to(CompileLog* log,527DepType dept,528GrowableArray<ciBaseObject*>* args,529Klass* witness = NULL);530static void write_dependency_to(CompileLog* log,531DepType dept,532GrowableArray<DepArgument>* args,533Klass* witness = NULL);534static void write_dependency_to(xmlStream* xtty,535DepType dept,536GrowableArray<DepArgument>* args,537Klass* witness = NULL);538public:539// Use this to iterate over an nmethod's dependency set.540// Works on new and old dependency sets.541// Usage:542//543// ;544// Dependencies::DepType dept;545// for (Dependencies::DepStream deps(nm); deps.next(); ) {546// ...547// }548//549// The caller must be in the VM, since oops are not wrapped in handles.550class DepStream {551private:552nmethod* _code; // null if in a compiler thread553Dependencies* _deps; // null if not in a compiler thread554CompressedReadStream _bytes;555#ifdef ASSERT556size_t _byte_limit;557#endif558559// iteration variables:560DepType _type;561int _xi[max_arg_count+1];562563void initial_asserts(size_t byte_limit) NOT_DEBUG({});564565inline Metadata* recorded_metadata_at(int i);566inline oop recorded_oop_at(int i);567568Klass* check_klass_dependency(KlassDepChange* changes);569Klass* check_new_klass_dependency(NewKlassDepChange* changes);570Klass* check_klass_init_dependency(KlassInitDepChange* changes);571Klass* check_call_site_dependency(CallSiteDepChange* changes);572573void trace_and_log_witness(Klass* witness);574575public:576DepStream(Dependencies* deps)577: _code(NULL),578_deps(deps),579_bytes(deps->content_bytes())580{581initial_asserts(deps->size_in_bytes());582}583DepStream(nmethod* code)584: _code(code),585_deps(NULL),586_bytes(code->dependencies_begin())587{588initial_asserts(code->dependencies_size());589}590591bool next();592593DepType type() { return _type; }594bool is_oop_argument(int i) { return type() == call_site_target_value; }595uintptr_t get_identifier(int i);596597int argument_count() { return dep_args(type()); }598int argument_index(int i) { assert(0 <= i && i < argument_count(), "oob");599return _xi[i]; }600Metadata* argument(int i); // => recorded_oop_at(argument_index(i))601oop argument_oop(int i); // => recorded_oop_at(argument_index(i))602InstanceKlass* context_type();603604bool is_klass_type() { return Dependencies::is_klass_type(type()); }605606Method* method_argument(int i) {607Metadata* x = argument(i);608assert(x->is_method(), "type");609return (Method*) x;610}611Klass* type_argument(int i) {612Metadata* x = argument(i);613assert(x->is_klass(), "type");614return (Klass*) x;615}616617// The point of the whole exercise: Is this dep still OK?618Klass* check_dependency() {619Klass* result = check_klass_dependency(NULL);620if (result != NULL) return result;621return check_call_site_dependency(NULL);622}623624// A lighter version: Checks only around recent changes in a class625// hierarchy. (See Universe::flush_dependents_on.)626Klass* spot_check_dependency_at(DepChange& changes);627628// Log the current dependency to xtty or compilation log.629void log_dependency(Klass* witness = NULL);630631// Print the current dependency to tty.632void print_dependency(Klass* witness = NULL, bool verbose = false, outputStream* st = tty);633};634friend class Dependencies::DepStream;635636static void print_statistics();637};638639640class DependencySignature : public ResourceObj {641private:642int _args_count;643uintptr_t _argument_hash[Dependencies::max_arg_count];644Dependencies::DepType _type;645646public:647DependencySignature(Dependencies::DepStream& dep) {648_args_count = dep.argument_count();649_type = dep.type();650for (int i = 0; i < _args_count; i++) {651_argument_hash[i] = dep.get_identifier(i);652}653}654655static bool equals(DependencySignature const& s1, DependencySignature const& s2);656static unsigned hash (DependencySignature const& s1) { return s1.arg(0) >> 2; }657658int args_count() const { return _args_count; }659uintptr_t arg(int idx) const { return _argument_hash[idx]; }660Dependencies::DepType type() const { return _type; }661662};663664665// Every particular DepChange is a sub-class of this class.666class DepChange : public StackObj {667public:668// What kind of DepChange is this?669virtual bool is_klass_change() const { return false; }670virtual bool is_new_klass_change() const { return false; }671virtual bool is_klass_init_change() const { return false; }672virtual bool is_call_site_change() const { return false; }673674virtual void mark_for_deoptimization(nmethod* nm) = 0;675676// Subclass casting with assertions.677KlassDepChange* as_klass_change() {678assert(is_klass_change(), "bad cast");679return (KlassDepChange*) this;680}681NewKlassDepChange* as_new_klass_change() {682assert(is_new_klass_change(), "bad cast");683return (NewKlassDepChange*) this;684}685KlassInitDepChange* as_klass_init_change() {686assert(is_klass_init_change(), "bad cast");687return (KlassInitDepChange*) this;688}689CallSiteDepChange* as_call_site_change() {690assert(is_call_site_change(), "bad cast");691return (CallSiteDepChange*) this;692}693694void print();695696public:697enum ChangeType {698NO_CHANGE = 0, // an uninvolved klass699Change_new_type, // a newly loaded type700Change_new_sub, // a super with a new subtype701Change_new_impl, // an interface with a new implementation702CHANGE_LIMIT,703Start_Klass = CHANGE_LIMIT // internal indicator for ContextStream704};705706// Usage:707// for (DepChange::ContextStream str(changes); str.next(); ) {708// Klass* k = str.klass();709// switch (str.change_type()) {710// ...711// }712// }713class ContextStream : public StackObj {714private:715DepChange& _changes;716friend class DepChange;717718// iteration variables:719ChangeType _change_type;720Klass* _klass;721Array<InstanceKlass*>* _ti_base; // i.e., transitive_interfaces722int _ti_index;723int _ti_limit;724725// start at the beginning:726void start();727728public:729ContextStream(DepChange& changes)730: _changes(changes)731{ start(); }732733ContextStream(DepChange& changes, NoSafepointVerifier& nsv)734: _changes(changes)735// the nsv argument makes it safe to hold oops like _klass736{ start(); }737738bool next();739740ChangeType change_type() { return _change_type; }741Klass* klass() { return _klass; }742};743friend class DepChange::ContextStream;744};745746747// A class hierarchy change coming through the VM (under the Compile_lock).748// The change is structured as a single type with any number of supers749// and implemented interface types. Other than the type, any of the750// super types can be context types for a relevant dependency, which the751// type could invalidate.752class KlassDepChange : public DepChange {753private:754// each change set is rooted in exactly one type (at present):755InstanceKlass* _type;756757void initialize();758759protected:760// notes the type, marks it and all its super-types761KlassDepChange(InstanceKlass* type) : _type(type) {762initialize();763}764765// cleans up the marks766~KlassDepChange();767768public:769// What kind of DepChange is this?770virtual bool is_klass_change() const { return true; }771772virtual void mark_for_deoptimization(nmethod* nm) {773nm->mark_for_deoptimization(/*inc_recompile_counts=*/true);774}775776InstanceKlass* type() { return _type; }777778// involves_context(k) is true if k == _type or any of its super types779bool involves_context(Klass* k);780};781782// A class hierarchy change: new type is loaded.783class NewKlassDepChange : public KlassDepChange {784public:785NewKlassDepChange(InstanceKlass* new_type) : KlassDepChange(new_type) {}786787// What kind of DepChange is this?788virtual bool is_new_klass_change() const { return true; }789790InstanceKlass* new_type() { return type(); }791};792793// Change in initialization state of a loaded class.794class KlassInitDepChange : public KlassDepChange {795public:796KlassInitDepChange(InstanceKlass* type) : KlassDepChange(type) {}797798// What kind of DepChange is this?799virtual bool is_klass_init_change() const { return true; }800};801802// A CallSite has changed its target.803class CallSiteDepChange : public DepChange {804private:805Handle _call_site;806Handle _method_handle;807808public:809CallSiteDepChange(Handle call_site, Handle method_handle);810811// What kind of DepChange is this?812virtual bool is_call_site_change() const { return true; }813814virtual void mark_for_deoptimization(nmethod* nm) {815nm->mark_for_deoptimization(/*inc_recompile_counts=*/false);816}817818oop call_site() const { return _call_site(); }819oop method_handle() const { return _method_handle(); }820};821822#endif // SHARE_CODE_DEPENDENCIES_HPP823824825