Path: blob/master/src/hotspot/share/code/dependencies.cpp
40931 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#include "precompiled.hpp"25#include "ci/ciArrayKlass.hpp"26#include "ci/ciEnv.hpp"27#include "ci/ciKlass.hpp"28#include "ci/ciMethod.hpp"29#include "classfile/javaClasses.inline.hpp"30#include "classfile/vmClasses.hpp"31#include "code/dependencies.hpp"32#include "compiler/compileLog.hpp"33#include "compiler/compileBroker.hpp"34#include "compiler/compileTask.hpp"35#include "memory/resourceArea.hpp"36#include "oops/klass.hpp"37#include "oops/oop.inline.hpp"38#include "oops/objArrayKlass.hpp"39#include "runtime/flags/flagSetting.hpp"40#include "runtime/handles.hpp"41#include "runtime/handles.inline.hpp"42#include "runtime/jniHandles.inline.hpp"43#include "runtime/perfData.hpp"44#include "runtime/thread.inline.hpp"45#include "runtime/vmThread.hpp"46#include "utilities/copy.hpp"474849#ifdef ASSERT50static bool must_be_in_vm() {51Thread* thread = Thread::current();52if (thread->is_Java_thread()) {53return thread->as_Java_thread()->thread_state() == _thread_in_vm;54} else {55return true; // Could be VMThread or GC thread56}57}58#endif //ASSERT5960void Dependencies::initialize(ciEnv* env) {61Arena* arena = env->arena();62_oop_recorder = env->oop_recorder();63_log = env->log();64_dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);65#if INCLUDE_JVMCI66_using_dep_values = false;67#endif68DEBUG_ONLY(_deps[end_marker] = NULL);69for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {70_deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0);71}72_content_bytes = NULL;73_size_in_bytes = (size_t)-1;7475assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");76}7778void Dependencies::assert_evol_method(ciMethod* m) {79assert_common_1(evol_method, m);80}8182void Dependencies::assert_leaf_type(ciKlass* ctxk) {83if (ctxk->is_array_klass()) {84// As a special case, support this assertion on an array type,85// which reduces to an assertion on its element type.86// Note that this cannot be done with assertions that87// relate to concreteness or abstractness.88ciType* elemt = ctxk->as_array_klass()->base_element_type();89if (!elemt->is_instance_klass()) return; // Ex: int[][]90ctxk = elemt->as_instance_klass();91//if (ctxk->is_final()) return; // Ex: String[][]92}93check_ctxk(ctxk);94assert_common_1(leaf_type, ctxk);95}9697void Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) {98check_ctxk_abstract(ctxk);99assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck);100}101102void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) {103check_ctxk(ctxk);104check_unique_method(ctxk, uniqm);105assert_common_2(unique_concrete_method_2, ctxk, uniqm);106}107108void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm, ciKlass* resolved_klass, ciMethod* resolved_method) {109check_ctxk(ctxk);110check_unique_method(ctxk, uniqm);111if (UseVtableBasedCHA) {112assert_common_4(unique_concrete_method_4, ctxk, uniqm, resolved_klass, resolved_method);113} else {114assert_common_2(unique_concrete_method_2, ctxk, uniqm);115}116}117118void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {119check_ctxk(ctxk);120assert_common_1(no_finalizable_subclasses, ctxk);121}122123void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {124assert_common_2(call_site_target_value, call_site, method_handle);125}126127#if INCLUDE_JVMCI128129Dependencies::Dependencies(Arena* arena, OopRecorder* oop_recorder, CompileLog* log) {130_oop_recorder = oop_recorder;131_log = log;132_dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);133_using_dep_values = true;134DEBUG_ONLY(_dep_values[end_marker] = NULL);135for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {136_dep_values[i] = new(arena) GrowableArray<DepValue>(arena, 10, 0, DepValue());137}138_content_bytes = NULL;139_size_in_bytes = (size_t)-1;140141assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");142}143144void Dependencies::assert_evol_method(Method* m) {145assert_common_1(evol_method, DepValue(_oop_recorder, m));146}147148void Dependencies::assert_has_no_finalizable_subclasses(Klass* ctxk) {149check_ctxk(ctxk);150assert_common_1(no_finalizable_subclasses, DepValue(_oop_recorder, ctxk));151}152153void Dependencies::assert_leaf_type(Klass* ctxk) {154if (ctxk->is_array_klass()) {155// As a special case, support this assertion on an array type,156// which reduces to an assertion on its element type.157// Note that this cannot be done with assertions that158// relate to concreteness or abstractness.159BasicType elemt = ArrayKlass::cast(ctxk)->element_type();160if (is_java_primitive(elemt)) return; // Ex: int[][]161ctxk = ObjArrayKlass::cast(ctxk)->bottom_klass();162//if (ctxk->is_final()) return; // Ex: String[][]163}164check_ctxk(ctxk);165assert_common_1(leaf_type, DepValue(_oop_recorder, ctxk));166}167168void Dependencies::assert_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck) {169check_ctxk_abstract(ctxk);170DepValue ctxk_dv(_oop_recorder, ctxk);171DepValue conck_dv(_oop_recorder, conck, &ctxk_dv);172assert_common_2(abstract_with_unique_concrete_subtype, ctxk_dv, conck_dv);173}174175void Dependencies::assert_unique_concrete_method(Klass* ctxk, Method* uniqm) {176check_ctxk(ctxk);177check_unique_method(ctxk, uniqm);178assert_common_2(unique_concrete_method_2, DepValue(_oop_recorder, ctxk), DepValue(_oop_recorder, uniqm));179}180181void Dependencies::assert_call_site_target_value(oop call_site, oop method_handle) {182assert_common_2(call_site_target_value, DepValue(_oop_recorder, JNIHandles::make_local(call_site)), DepValue(_oop_recorder, JNIHandles::make_local(method_handle)));183}184185#endif // INCLUDE_JVMCI186187188// Helper function. If we are adding a new dep. under ctxk2,189// try to find an old dep. under a broader* ctxk1. If there is190//191bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,192int ctxk_i, ciKlass* ctxk2) {193ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();194if (ctxk2->is_subtype_of(ctxk1)) {195return true; // success, and no need to change196} else if (ctxk1->is_subtype_of(ctxk2)) {197// new context class fully subsumes previous one198deps->at_put(ctxk_i, ctxk2);199return true;200} else {201return false;202}203}204205void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {206assert(dep_args(dept) == 1, "sanity");207log_dependency(dept, x);208GrowableArray<ciBaseObject*>* deps = _deps[dept];209210// see if the same (or a similar) dep is already recorded211if (note_dep_seen(dept, x)) {212assert(deps->find(x) >= 0, "sanity");213} else {214deps->append(x);215}216}217218void Dependencies::assert_common_2(DepType dept,219ciBaseObject* x0, ciBaseObject* x1) {220assert(dep_args(dept) == 2, "sanity");221log_dependency(dept, x0, x1);222GrowableArray<ciBaseObject*>* deps = _deps[dept];223224// see if the same (or a similar) dep is already recorded225bool has_ctxk = has_explicit_context_arg(dept);226if (has_ctxk) {227assert(dep_context_arg(dept) == 0, "sanity");228if (note_dep_seen(dept, x1)) {229// look in this bucket for redundant assertions230const int stride = 2;231for (int i = deps->length(); (i -= stride) >= 0; ) {232ciBaseObject* y1 = deps->at(i+1);233if (x1 == y1) { // same subject; check the context234if (maybe_merge_ctxk(deps, i+0, x0->as_metadata()->as_klass())) {235return;236}237}238}239}240} else {241if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {242// look in this bucket for redundant assertions243const int stride = 2;244for (int i = deps->length(); (i -= stride) >= 0; ) {245ciBaseObject* y0 = deps->at(i+0);246ciBaseObject* y1 = deps->at(i+1);247if (x0 == y0 && x1 == y1) {248return;249}250}251}252}253254// append the assertion in the correct bucket:255deps->append(x0);256deps->append(x1);257}258259void Dependencies::assert_common_4(DepType dept,260ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2, ciBaseObject* x3) {261assert(has_explicit_context_arg(dept), "sanity");262assert(dep_context_arg(dept) == 0, "sanity");263assert(dep_args(dept) == 4, "sanity");264log_dependency(dept, ctxk, x1, x2, x3);265GrowableArray<ciBaseObject*>* deps = _deps[dept];266267// see if the same (or a similar) dep is already recorded268if (note_dep_seen(dept, x1) && note_dep_seen(dept, x2) && note_dep_seen(dept, x3)) {269// look in this bucket for redundant assertions270const int stride = 4;271for (int i = deps->length(); (i -= stride) >= 0; ) {272ciBaseObject* y1 = deps->at(i+1);273ciBaseObject* y2 = deps->at(i+2);274ciBaseObject* y3 = deps->at(i+3);275if (x1 == y1 && x2 == y2 && x3 == y3) { // same subjects; check the context276if (maybe_merge_ctxk(deps, i+0, ctxk)) {277return;278}279}280}281}282// append the assertion in the correct bucket:283deps->append(ctxk);284deps->append(x1);285deps->append(x2);286deps->append(x3);287}288289#if INCLUDE_JVMCI290bool Dependencies::maybe_merge_ctxk(GrowableArray<DepValue>* deps,291int ctxk_i, DepValue ctxk2_dv) {292Klass* ctxk1 = deps->at(ctxk_i).as_klass(_oop_recorder);293Klass* ctxk2 = ctxk2_dv.as_klass(_oop_recorder);294if (ctxk2->is_subtype_of(ctxk1)) {295return true; // success, and no need to change296} else if (ctxk1->is_subtype_of(ctxk2)) {297// new context class fully subsumes previous one298deps->at_put(ctxk_i, ctxk2_dv);299return true;300} else {301return false;302}303}304305void Dependencies::assert_common_1(DepType dept, DepValue x) {306assert(dep_args(dept) == 1, "sanity");307//log_dependency(dept, x);308GrowableArray<DepValue>* deps = _dep_values[dept];309310// see if the same (or a similar) dep is already recorded311if (note_dep_seen(dept, x)) {312assert(deps->find(x) >= 0, "sanity");313} else {314deps->append(x);315}316}317318void Dependencies::assert_common_2(DepType dept,319DepValue x0, DepValue x1) {320assert(dep_args(dept) == 2, "sanity");321//log_dependency(dept, x0, x1);322GrowableArray<DepValue>* deps = _dep_values[dept];323324// see if the same (or a similar) dep is already recorded325bool has_ctxk = has_explicit_context_arg(dept);326if (has_ctxk) {327assert(dep_context_arg(dept) == 0, "sanity");328if (note_dep_seen(dept, x1)) {329// look in this bucket for redundant assertions330const int stride = 2;331for (int i = deps->length(); (i -= stride) >= 0; ) {332DepValue y1 = deps->at(i+1);333if (x1 == y1) { // same subject; check the context334if (maybe_merge_ctxk(deps, i+0, x0)) {335return;336}337}338}339}340} else {341if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {342// look in this bucket for redundant assertions343const int stride = 2;344for (int i = deps->length(); (i -= stride) >= 0; ) {345DepValue y0 = deps->at(i+0);346DepValue y1 = deps->at(i+1);347if (x0 == y0 && x1 == y1) {348return;349}350}351}352}353354// append the assertion in the correct bucket:355deps->append(x0);356deps->append(x1);357}358#endif // INCLUDE_JVMCI359360/// Support for encoding dependencies into an nmethod:361362void Dependencies::copy_to(nmethod* nm) {363address beg = nm->dependencies_begin();364address end = nm->dependencies_end();365guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");366Copy::disjoint_words((HeapWord*) content_bytes(),367(HeapWord*) beg,368size_in_bytes() / sizeof(HeapWord));369assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");370}371372static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) {373for (int i = 0; i < narg; i++) {374int diff = p1[i]->ident() - p2[i]->ident();375if (diff != 0) return diff;376}377return 0;378}379static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2)380{ return sort_dep(p1, p2, 1); }381static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2)382{ return sort_dep(p1, p2, 2); }383static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2)384{ return sort_dep(p1, p2, 3); }385static int sort_dep_arg_4(ciBaseObject** p1, ciBaseObject** p2)386{ return sort_dep(p1, p2, 4); }387388#if INCLUDE_JVMCI389// metadata deps are sorted before object deps390static int sort_dep_value(Dependencies::DepValue* p1, Dependencies::DepValue* p2, int narg) {391for (int i = 0; i < narg; i++) {392int diff = p1[i].sort_key() - p2[i].sort_key();393if (diff != 0) return diff;394}395return 0;396}397static int sort_dep_value_arg_1(Dependencies::DepValue* p1, Dependencies::DepValue* p2)398{ return sort_dep_value(p1, p2, 1); }399static int sort_dep_value_arg_2(Dependencies::DepValue* p1, Dependencies::DepValue* p2)400{ return sort_dep_value(p1, p2, 2); }401static int sort_dep_value_arg_3(Dependencies::DepValue* p1, Dependencies::DepValue* p2)402{ return sort_dep_value(p1, p2, 3); }403#endif // INCLUDE_JVMCI404405void Dependencies::sort_all_deps() {406#if INCLUDE_JVMCI407if (_using_dep_values) {408for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {409DepType dept = (DepType)deptv;410GrowableArray<DepValue>* deps = _dep_values[dept];411if (deps->length() <= 1) continue;412switch (dep_args(dept)) {413case 1: deps->sort(sort_dep_value_arg_1, 1); break;414case 2: deps->sort(sort_dep_value_arg_2, 2); break;415case 3: deps->sort(sort_dep_value_arg_3, 3); break;416default: ShouldNotReachHere(); break;417}418}419return;420}421#endif // INCLUDE_JVMCI422for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {423DepType dept = (DepType)deptv;424GrowableArray<ciBaseObject*>* deps = _deps[dept];425if (deps->length() <= 1) continue;426switch (dep_args(dept)) {427case 1: deps->sort(sort_dep_arg_1, 1); break;428case 2: deps->sort(sort_dep_arg_2, 2); break;429case 3: deps->sort(sort_dep_arg_3, 3); break;430case 4: deps->sort(sort_dep_arg_4, 4); break;431default: ShouldNotReachHere(); break;432}433}434}435436size_t Dependencies::estimate_size_in_bytes() {437size_t est_size = 100;438#if INCLUDE_JVMCI439if (_using_dep_values) {440for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {441DepType dept = (DepType)deptv;442GrowableArray<DepValue>* deps = _dep_values[dept];443est_size += deps->length() * 2; // tags and argument(s)444}445return est_size;446}447#endif // INCLUDE_JVMCI448for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {449DepType dept = (DepType)deptv;450GrowableArray<ciBaseObject*>* deps = _deps[dept];451est_size += deps->length()*2; // tags and argument(s)452}453return est_size;454}455456ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) {457switch (dept) {458case unique_concrete_method_2:459case unique_concrete_method_4:460return x->as_metadata()->as_method()->holder();461default:462return NULL; // let NULL be NULL463}464}465466Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) {467assert(must_be_in_vm(), "raw oops here");468switch (dept) {469case unique_concrete_method_2:470case unique_concrete_method_4:471assert(x->is_method(), "sanity");472return ((Method*)x)->method_holder();473default:474return NULL; // let NULL be NULL475}476}477478void Dependencies::encode_content_bytes() {479sort_all_deps();480481// cast is safe, no deps can overflow INT_MAX482CompressedWriteStream bytes((int)estimate_size_in_bytes());483484#if INCLUDE_JVMCI485if (_using_dep_values) {486for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {487DepType dept = (DepType)deptv;488GrowableArray<DepValue>* deps = _dep_values[dept];489if (deps->length() == 0) continue;490int stride = dep_args(dept);491int ctxkj = dep_context_arg(dept); // -1 if no context arg492assert(stride > 0, "sanity");493for (int i = 0; i < deps->length(); i += stride) {494jbyte code_byte = (jbyte)dept;495int skipj = -1;496if (ctxkj >= 0 && ctxkj+1 < stride) {497Klass* ctxk = deps->at(i+ctxkj+0).as_klass(_oop_recorder);498DepValue x = deps->at(i+ctxkj+1); // following argument499if (ctxk == ctxk_encoded_as_null(dept, x.as_metadata(_oop_recorder))) {500skipj = ctxkj; // we win: maybe one less oop to keep track of501code_byte |= default_context_type_bit;502}503}504bytes.write_byte(code_byte);505for (int j = 0; j < stride; j++) {506if (j == skipj) continue;507DepValue v = deps->at(i+j);508int idx = v.index();509bytes.write_int(idx);510}511}512}513} else {514#endif // INCLUDE_JVMCI515for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {516DepType dept = (DepType)deptv;517GrowableArray<ciBaseObject*>* deps = _deps[dept];518if (deps->length() == 0) continue;519int stride = dep_args(dept);520int ctxkj = dep_context_arg(dept); // -1 if no context arg521assert(stride > 0, "sanity");522for (int i = 0; i < deps->length(); i += stride) {523jbyte code_byte = (jbyte)dept;524int skipj = -1;525if (ctxkj >= 0 && ctxkj+1 < stride) {526ciKlass* ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass();527ciBaseObject* x = deps->at(i+ctxkj+1); // following argument528if (ctxk == ctxk_encoded_as_null(dept, x)) {529skipj = ctxkj; // we win: maybe one less oop to keep track of530code_byte |= default_context_type_bit;531}532}533bytes.write_byte(code_byte);534for (int j = 0; j < stride; j++) {535if (j == skipj) continue;536ciBaseObject* v = deps->at(i+j);537int idx;538if (v->is_object()) {539idx = _oop_recorder->find_index(v->as_object()->constant_encoding());540} else {541ciMetadata* meta = v->as_metadata();542idx = _oop_recorder->find_index(meta->constant_encoding());543}544bytes.write_int(idx);545}546}547}548#if INCLUDE_JVMCI549}550#endif551552// write a sentinel byte to mark the end553bytes.write_byte(end_marker);554555// round it out to a word boundary556while (bytes.position() % sizeof(HeapWord) != 0) {557bytes.write_byte(end_marker);558}559560// check whether the dept byte encoding really works561assert((jbyte)default_context_type_bit != 0, "byte overflow");562563_content_bytes = bytes.buffer();564_size_in_bytes = bytes.position();565}566567568const char* Dependencies::_dep_name[TYPE_LIMIT] = {569"end_marker",570"evol_method",571"leaf_type",572"abstract_with_unique_concrete_subtype",573"unique_concrete_method_2",574"unique_concrete_method_4",575"no_finalizable_subclasses",576"call_site_target_value"577};578579int Dependencies::_dep_args[TYPE_LIMIT] = {580-1,// end_marker5811, // evol_method m5821, // leaf_type ctxk5832, // abstract_with_unique_concrete_subtype ctxk, k5842, // unique_concrete_method_2 ctxk, m5854, // unique_concrete_method_4 ctxk, m, resolved_klass, resolved_method5861, // no_finalizable_subclasses ctxk5872 // call_site_target_value call_site, method_handle588};589590const char* Dependencies::dep_name(Dependencies::DepType dept) {591if (!dept_in_mask(dept, all_types)) return "?bad-dep?";592return _dep_name[dept];593}594595int Dependencies::dep_args(Dependencies::DepType dept) {596if (!dept_in_mask(dept, all_types)) return -1;597return _dep_args[dept];598}599600void Dependencies::check_valid_dependency_type(DepType dept) {601guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, "invalid dependency type: %d", (int) dept);602}603604Dependencies::DepType Dependencies::validate_dependencies(CompileTask* task, char** failure_detail) {605int klass_violations = 0;606DepType result = end_marker;607for (Dependencies::DepStream deps(this); deps.next(); ) {608Klass* witness = deps.check_dependency();609if (witness != NULL) {610if (klass_violations == 0) {611result = deps.type();612if (failure_detail != NULL && klass_violations == 0) {613// Use a fixed size buffer to prevent the string stream from614// resizing in the context of an inner resource mark.615char* buffer = NEW_RESOURCE_ARRAY(char, O_BUFLEN);616stringStream st(buffer, O_BUFLEN);617deps.print_dependency(witness, true, &st);618*failure_detail = st.as_string();619}620}621klass_violations++;622if (xtty == NULL) {623// If we're not logging then a single violation is sufficient,624// otherwise we want to log all the dependences which were625// violated.626break;627}628}629}630631return result;632}633634// for the sake of the compiler log, print out current dependencies:635void Dependencies::log_all_dependencies() {636if (log() == NULL) return;637ResourceMark rm;638for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {639DepType dept = (DepType)deptv;640GrowableArray<ciBaseObject*>* deps = _deps[dept];641int deplen = deps->length();642if (deplen == 0) {643continue;644}645int stride = dep_args(dept);646GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(stride);647for (int i = 0; i < deps->length(); i += stride) {648for (int j = 0; j < stride; j++) {649// flush out the identities before printing650ciargs->push(deps->at(i+j));651}652write_dependency_to(log(), dept, ciargs);653ciargs->clear();654}655guarantee(deplen == deps->length(), "deps array cannot grow inside nested ResoureMark scope");656}657}658659void Dependencies::write_dependency_to(CompileLog* log,660DepType dept,661GrowableArray<DepArgument>* args,662Klass* witness) {663if (log == NULL) {664return;665}666ResourceMark rm;667ciEnv* env = ciEnv::current();668GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(args->length());669for (GrowableArrayIterator<DepArgument> it = args->begin(); it != args->end(); ++it) {670DepArgument arg = *it;671if (arg.is_oop()) {672ciargs->push(env->get_object(arg.oop_value()));673} else {674ciargs->push(env->get_metadata(arg.metadata_value()));675}676}677int argslen = ciargs->length();678Dependencies::write_dependency_to(log, dept, ciargs, witness);679guarantee(argslen == ciargs->length(), "ciargs array cannot grow inside nested ResoureMark scope");680}681682void Dependencies::write_dependency_to(CompileLog* log,683DepType dept,684GrowableArray<ciBaseObject*>* args,685Klass* witness) {686if (log == NULL) {687return;688}689ResourceMark rm;690GrowableArray<int>* argids = new GrowableArray<int>(args->length());691for (GrowableArrayIterator<ciBaseObject*> it = args->begin(); it != args->end(); ++it) {692ciBaseObject* obj = *it;693if (obj->is_object()) {694argids->push(log->identify(obj->as_object()));695} else {696argids->push(log->identify(obj->as_metadata()));697}698}699if (witness != NULL) {700log->begin_elem("dependency_failed");701} else {702log->begin_elem("dependency");703}704log->print(" type='%s'", dep_name(dept));705const int ctxkj = dep_context_arg(dept); // -1 if no context arg706if (ctxkj >= 0 && ctxkj < argids->length()) {707log->print(" ctxk='%d'", argids->at(ctxkj));708}709// write remaining arguments, if any.710for (int j = 0; j < argids->length(); j++) {711if (j == ctxkj) continue; // already logged712if (j == 1) {713log->print( " x='%d'", argids->at(j));714} else {715log->print(" x%d='%d'", j, argids->at(j));716}717}718if (witness != NULL) {719log->object("witness", witness);720log->stamp();721}722log->end_elem();723}724725void Dependencies::write_dependency_to(xmlStream* xtty,726DepType dept,727GrowableArray<DepArgument>* args,728Klass* witness) {729if (xtty == NULL) {730return;731}732Thread* thread = Thread::current();733HandleMark rm(thread);734ttyLocker ttyl;735int ctxkj = dep_context_arg(dept); // -1 if no context arg736if (witness != NULL) {737xtty->begin_elem("dependency_failed");738} else {739xtty->begin_elem("dependency");740}741xtty->print(" type='%s'", dep_name(dept));742if (ctxkj >= 0) {743xtty->object("ctxk", args->at(ctxkj).metadata_value());744}745// write remaining arguments, if any.746for (int j = 0; j < args->length(); j++) {747if (j == ctxkj) continue; // already logged748DepArgument arg = args->at(j);749if (j == 1) {750if (arg.is_oop()) {751xtty->object("x", Handle(thread, arg.oop_value()));752} else {753xtty->object("x", arg.metadata_value());754}755} else {756char xn[12]; sprintf(xn, "x%d", j);757if (arg.is_oop()) {758xtty->object(xn, Handle(thread, arg.oop_value()));759} else {760xtty->object(xn, arg.metadata_value());761}762}763}764if (witness != NULL) {765xtty->object("witness", witness);766xtty->stamp();767}768xtty->end_elem();769}770771void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args,772Klass* witness, outputStream* st) {773ResourceMark rm;774ttyLocker ttyl; // keep the following output all in one block775st->print_cr("%s of type %s",776(witness == NULL)? "Dependency": "Failed dependency",777dep_name(dept));778// print arguments779int ctxkj = dep_context_arg(dept); // -1 if no context arg780for (int j = 0; j < args->length(); j++) {781DepArgument arg = args->at(j);782bool put_star = false;783if (arg.is_null()) continue;784const char* what;785if (j == ctxkj) {786assert(arg.is_metadata(), "must be");787what = "context";788put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value());789} else if (arg.is_method()) {790what = "method ";791put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), NULL);792} else if (arg.is_klass()) {793what = "class ";794} else {795what = "object ";796}797st->print(" %s = %s", what, (put_star? "*": ""));798if (arg.is_klass()) {799st->print("%s", ((Klass*)arg.metadata_value())->external_name());800} else if (arg.is_method()) {801((Method*)arg.metadata_value())->print_value_on(st);802} else if (arg.is_oop()) {803arg.oop_value()->print_value_on(st);804} else {805ShouldNotReachHere(); // Provide impl for this type.806}807808st->cr();809}810if (witness != NULL) {811bool put_star = !Dependencies::is_concrete_klass(witness);812st->print_cr(" witness = %s%s",813(put_star? "*": ""),814witness->external_name());815}816}817818void Dependencies::DepStream::log_dependency(Klass* witness) {819if (_deps == NULL && xtty == NULL) return; // fast cutout for runtime820ResourceMark rm;821const int nargs = argument_count();822GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);823for (int j = 0; j < nargs; j++) {824if (is_oop_argument(j)) {825args->push(argument_oop(j));826} else {827args->push(argument(j));828}829}830int argslen = args->length();831if (_deps != NULL && _deps->log() != NULL) {832if (ciEnv::current() != NULL) {833Dependencies::write_dependency_to(_deps->log(), type(), args, witness);834} else {835// Treat the CompileLog as an xmlstream instead836Dependencies::write_dependency_to((xmlStream*)_deps->log(), type(), args, witness);837}838} else {839Dependencies::write_dependency_to(xtty, type(), args, witness);840}841guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");842}843844void Dependencies::DepStream::print_dependency(Klass* witness, bool verbose, outputStream* st) {845ResourceMark rm;846int nargs = argument_count();847GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);848for (int j = 0; j < nargs; j++) {849if (is_oop_argument(j)) {850args->push(argument_oop(j));851} else {852args->push(argument(j));853}854}855int argslen = args->length();856Dependencies::print_dependency(type(), args, witness, st);857if (verbose) {858if (_code != NULL) {859st->print(" code: ");860_code->print_value_on(st);861st->cr();862}863}864guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");865}866867868/// Dependency stream support (decodes dependencies from an nmethod):869870#ifdef ASSERT871void Dependencies::DepStream::initial_asserts(size_t byte_limit) {872assert(must_be_in_vm(), "raw oops here");873_byte_limit = byte_limit;874_type = (DepType)(end_marker-1); // defeat "already at end" assert875assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");876}877#endif //ASSERT878879bool Dependencies::DepStream::next() {880assert(_type != end_marker, "already at end");881if (_bytes.position() == 0 && _code != NULL882&& _code->dependencies_size() == 0) {883// Method has no dependencies at all.884return false;885}886int code_byte = (_bytes.read_byte() & 0xFF);887if (code_byte == end_marker) {888DEBUG_ONLY(_type = end_marker);889return false;890} else {891int ctxk_bit = (code_byte & Dependencies::default_context_type_bit);892code_byte -= ctxk_bit;893DepType dept = (DepType)code_byte;894_type = dept;895Dependencies::check_valid_dependency_type(dept);896int stride = _dep_args[dept];897assert(stride == dep_args(dept), "sanity");898int skipj = -1;899if (ctxk_bit != 0) {900skipj = 0; // currently the only context argument is at zero901assert(skipj == dep_context_arg(dept), "zero arg always ctxk");902}903for (int j = 0; j < stride; j++) {904_xi[j] = (j == skipj)? 0: _bytes.read_int();905}906DEBUG_ONLY(_xi[stride] = -1); // help detect overruns907return true;908}909}910911inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) {912Metadata* o = NULL;913if (_code != NULL) {914o = _code->metadata_at(i);915} else {916o = _deps->oop_recorder()->metadata_at(i);917}918return o;919}920921inline oop Dependencies::DepStream::recorded_oop_at(int i) {922return (_code != NULL)923? _code->oop_at(i)924: JNIHandles::resolve(_deps->oop_recorder()->oop_at(i));925}926927Metadata* Dependencies::DepStream::argument(int i) {928Metadata* result = recorded_metadata_at(argument_index(i));929930if (result == NULL) { // Explicit context argument can be compressed931int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg932if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) {933result = ctxk_encoded_as_null(type(), argument(ctxkj+1));934}935}936937assert(result == NULL || result->is_klass() || result->is_method(), "must be");938return result;939}940941/**942* Returns a unique identifier for each dependency argument.943*/944uintptr_t Dependencies::DepStream::get_identifier(int i) {945if (is_oop_argument(i)) {946return (uintptr_t)(oopDesc*)argument_oop(i);947} else {948return (uintptr_t)argument(i);949}950}951952oop Dependencies::DepStream::argument_oop(int i) {953oop result = recorded_oop_at(argument_index(i));954assert(oopDesc::is_oop_or_null(result), "must be");955return result;956}957958InstanceKlass* Dependencies::DepStream::context_type() {959assert(must_be_in_vm(), "raw oops here");960961// Most dependencies have an explicit context type argument.962{963int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg964if (ctxkj >= 0) {965Metadata* k = argument(ctxkj);966assert(k != NULL && k->is_klass(), "type check");967return InstanceKlass::cast((Klass*)k);968}969}970971// Some dependencies are using the klass of the first object972// argument as implicit context type.973{974int ctxkj = dep_implicit_context_arg(type());975if (ctxkj >= 0) {976Klass* k = argument_oop(ctxkj)->klass();977assert(k != NULL, "type check");978return InstanceKlass::cast(k);979}980}981982// And some dependencies don't have a context type at all,983// e.g. evol_method.984return NULL;985}986987// ----------------- DependencySignature --------------------------------------988bool DependencySignature::equals(DependencySignature const& s1, DependencySignature const& s2) {989if ((s1.type() != s2.type()) || (s1.args_count() != s2.args_count())) {990return false;991}992993for (int i = 0; i < s1.args_count(); i++) {994if (s1.arg(i) != s2.arg(i)) {995return false;996}997}998return true;999}10001001/// Checking dependencies10021003// This hierarchy walker inspects subtypes of a given type, trying to find a "bad" class which breaks a dependency.1004// Such a class is called a "witness" to the broken dependency.1005// While searching around, we ignore "participants", which are already known to the dependency.1006class AbstractClassHierarchyWalker {1007public:1008enum { PARTICIPANT_LIMIT = 3 };10091010private:1011// if non-zero, tells how many witnesses to convert to participants1012uint _record_witnesses;10131014// special classes which are not allowed to be witnesses:1015Klass* _participants[PARTICIPANT_LIMIT+1];1016uint _num_participants;10171018#ifdef ASSERT1019uint _nof_requests; // one-shot walker1020#endif // ASSERT10211022static PerfCounter* _perf_find_witness_anywhere_calls_count;1023static PerfCounter* _perf_find_witness_anywhere_steps_count;1024static PerfCounter* _perf_find_witness_in_calls_count;10251026protected:1027virtual Klass* find_witness_in(KlassDepChange& changes) = 0;1028virtual Klass* find_witness_anywhere(InstanceKlass* context_type) = 0;10291030AbstractClassHierarchyWalker(Klass* participant) : _record_witnesses(0), _num_participants(0)1031#ifdef ASSERT1032, _nof_requests(0)1033#endif // ASSERT1034{1035for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) {1036_participants[i] = NULL;1037}1038if (participant != NULL) {1039add_participant(participant);1040}1041}10421043bool is_participant(Klass* k) {1044for (uint i = 0; i < _num_participants; i++) {1045if (_participants[i] == k) {1046return true;1047}1048}1049return false;1050}10511052bool record_witness(Klass* witness) {1053if (_record_witnesses > 0) {1054--_record_witnesses;1055add_participant(witness);1056return false; // not a witness1057} else {1058return true; // is a witness1059}1060}10611062class CountingClassHierarchyIterator : public ClassHierarchyIterator {1063private:1064jlong _nof_steps;1065public:1066CountingClassHierarchyIterator(InstanceKlass* root) : ClassHierarchyIterator(root), _nof_steps(0) {}10671068void next() {1069_nof_steps++;1070ClassHierarchyIterator::next();1071}10721073~CountingClassHierarchyIterator() {1074if (UsePerfData) {1075_perf_find_witness_anywhere_steps_count->inc(_nof_steps);1076}1077}1078};10791080public:1081uint num_participants() { return _num_participants; }1082Klass* participant(uint n) {1083assert(n <= _num_participants, "oob");1084if (n < _num_participants) {1085return _participants[n];1086} else {1087return NULL;1088}1089}10901091void add_participant(Klass* participant) {1092assert(!is_participant(participant), "sanity");1093assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob");1094uint np = _num_participants++;1095_participants[np] = participant;1096}10971098void record_witnesses(uint add) {1099if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT;1100assert(_num_participants + add < PARTICIPANT_LIMIT, "oob");1101_record_witnesses = add;1102}11031104Klass* find_witness(InstanceKlass* context_type, KlassDepChange* changes = NULL);11051106static void init();1107static void print_statistics();1108};11091110PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_calls_count = NULL;1111PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_steps_count = NULL;1112PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_in_calls_count = NULL;11131114void AbstractClassHierarchyWalker::init() {1115if (UsePerfData) {1116EXCEPTION_MARK;1117_perf_find_witness_anywhere_calls_count =1118PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhere", PerfData::U_Events, CHECK);1119_perf_find_witness_anywhere_steps_count =1120PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhereSteps", PerfData::U_Events, CHECK);1121_perf_find_witness_in_calls_count =1122PerfDataManager::create_counter(SUN_CI, "findWitnessIn", PerfData::U_Events, CHECK);1123}1124}11251126Klass* AbstractClassHierarchyWalker::find_witness(InstanceKlass* context_type, KlassDepChange* changes) {1127// Current thread must be in VM (not native mode, as in CI):1128assert(must_be_in_vm(), "raw oops here");1129// Must not move the class hierarchy during this check:1130assert_locked_or_safepoint(Compile_lock);1131assert(_nof_requests++ == 0, "repeated requests are not supported");11321133assert(changes == NULL || changes->involves_context(context_type), "irrelevant dependency");11341135// (Note: Interfaces do not have subclasses.)1136// If it is an interface, search its direct implementors.1137// (Their subclasses are additional indirect implementors. See InstanceKlass::add_implementor().)1138if (context_type->is_interface()) {1139int nof_impls = context_type->nof_implementors();1140if (nof_impls == 0) {1141return NULL; // no implementors1142} else if (nof_impls == 1) { // unique implementor1143assert(context_type != context_type->implementor(), "not unique");1144context_type = InstanceKlass::cast(context_type->implementor());1145} else { // nof_impls >= 21146// Avoid this case: *I.m > { A.m, C }; B.m > C1147// Here, I.m has 2 concrete implementations, but m appears unique1148// as A.m, because the search misses B.m when checking C.1149// The inherited method B.m was getting missed by the walker1150// when interface 'I' was the starting point.1151// %%% Until this is fixed more systematically, bail out.1152return context_type;1153}1154}1155assert(!context_type->is_interface(), "no interfaces allowed");11561157if (changes != NULL) {1158if (UsePerfData) {1159_perf_find_witness_in_calls_count->inc();1160}1161return find_witness_in(*changes);1162} else {1163if (UsePerfData) {1164_perf_find_witness_anywhere_calls_count->inc();1165}1166return find_witness_anywhere(context_type);1167}1168}11691170class ConcreteSubtypeFinder : public AbstractClassHierarchyWalker {1171private:1172bool is_witness(Klass* k);11731174protected:1175virtual Klass* find_witness_in(KlassDepChange& changes);1176virtual Klass* find_witness_anywhere(InstanceKlass* context_type);11771178public:1179ConcreteSubtypeFinder(Klass* participant = NULL) : AbstractClassHierarchyWalker(participant) {}1180};11811182bool ConcreteSubtypeFinder::is_witness(Klass* k) {1183if (Dependencies::is_concrete_klass(k)) {1184return record_witness(k); // concrete subtype1185} else {1186return false; // not a concrete class1187}1188}11891190Klass* ConcreteSubtypeFinder::find_witness_in(KlassDepChange& changes) {1191// When looking for unexpected concrete types, do not look beneath expected ones:1192// * CX > CC > C' is OK, even if C' is new.1193// * CX > { CC, C' } is not OK if C' is new, and C' is the witness.1194Klass* new_type = changes.as_new_klass_change()->new_type();1195assert(!is_participant(new_type), "only old classes are participants");1196// If the new type is a subtype of a participant, we are done.1197for (uint i = 0; i < num_participants(); i++) {1198if (changes.involves_context(participant(i))) {1199// new guy is protected from this check by previous participant1200return NULL;1201}1202}1203if (is_witness(new_type)) {1204return new_type;1205}1206// No witness found. The dependency remains unbroken.1207return NULL;1208}12091210Klass* ConcreteSubtypeFinder::find_witness_anywhere(InstanceKlass* context_type) {1211for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {1212Klass* sub = iter.klass();1213// Do not report participant types.1214if (is_participant(sub)) {1215// Don't walk beneath a participant since it hides witnesses.1216iter.skip_subclasses();1217} else if (is_witness(sub)) {1218return sub; // found a witness1219}1220}1221// No witness found. The dependency remains unbroken.1222return NULL;1223}12241225class ConcreteMethodFinder : public AbstractClassHierarchyWalker {1226private:1227Symbol* _name;1228Symbol* _signature;12291230// cache of method lookups1231Method* _found_methods[PARTICIPANT_LIMIT+1];12321233bool is_witness(Klass* k);12341235protected:1236virtual Klass* find_witness_in(KlassDepChange& changes);1237virtual Klass* find_witness_anywhere(InstanceKlass* context_type);12381239bool witnessed_reabstraction_in_supers(Klass* k);12401241public:1242ConcreteMethodFinder(Method* m, Klass* participant = NULL) : AbstractClassHierarchyWalker(participant) {1243assert(m != NULL && m->is_method(), "sanity");1244_name = m->name();1245_signature = m->signature();12461247for (int i = 0; i < PARTICIPANT_LIMIT+1; i++) {1248_found_methods[i] = NULL;1249}1250}12511252// Note: If n==num_participants, returns NULL.1253Method* found_method(uint n) {1254assert(n <= num_participants(), "oob");1255Method* fm = _found_methods[n];1256assert(n == num_participants() || fm != NULL, "proper usage");1257if (fm != NULL && fm->method_holder() != participant(n)) {1258// Default methods from interfaces can be added to classes. In1259// that case the holder of the method is not the class but the1260// interface where it's defined.1261assert(fm->is_default_method(), "sanity");1262return NULL;1263}1264return fm;1265}12661267void add_participant(Klass* participant) {1268AbstractClassHierarchyWalker::add_participant(participant);1269_found_methods[num_participants()] = NULL;1270}12711272bool record_witness(Klass* witness, Method* m) {1273_found_methods[num_participants()] = m;1274return AbstractClassHierarchyWalker::record_witness(witness);1275}12761277private:1278static PerfCounter* _perf_find_witness_anywhere_calls_count;1279static PerfCounter* _perf_find_witness_anywhere_steps_count;1280static PerfCounter* _perf_find_witness_in_calls_count;12811282public:1283static void init();1284static void print_statistics();1285};12861287bool ConcreteMethodFinder::is_witness(Klass* k) {1288if (is_participant(k)) {1289return false; // do not report participant types1290}1291if (k->is_instance_klass()) {1292InstanceKlass* ik = InstanceKlass::cast(k);1293// Search class hierarchy first, skipping private implementations1294// as they never override any inherited methods1295Method* m = ik->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1296if (Dependencies::is_concrete_method(m, ik)) {1297return record_witness(k, m); // concrete method found1298} else {1299// Check for re-abstraction of method1300if (!ik->is_interface() && m != NULL && m->is_abstract()) {1301// Found a matching abstract method 'm' in the class hierarchy.1302// This is fine iff 'k' is an abstract class and all concrete subtypes1303// of 'k' override 'm' and are participates of the current search.1304ConcreteSubtypeFinder wf;1305for (uint i = 0; i < num_participants(); i++) {1306Klass* p = participant(i);1307wf.add_participant(p);1308}1309Klass* w = wf.find_witness(ik);1310if (w != NULL) {1311Method* wm = InstanceKlass::cast(w)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1312if (!Dependencies::is_concrete_method(wm, w)) {1313// Found a concrete subtype 'w' which does not override abstract method 'm'.1314// Bail out because 'm' could be called with 'w' as receiver (leading to an1315// AbstractMethodError) and thus the method we are looking for is not unique.1316return record_witness(k, m);1317}1318}1319}1320// Check interface defaults also, if any exist.1321Array<Method*>* default_methods = ik->default_methods();1322if (default_methods != NULL) {1323Method* dm = ik->find_method(default_methods, _name, _signature);1324if (Dependencies::is_concrete_method(dm, NULL)) {1325return record_witness(k, dm); // default method found1326}1327}1328return false; // no concrete method found1329}1330} else {1331return false; // no methods to find in an array type1332}1333}13341335Klass* ConcreteMethodFinder::find_witness_in(KlassDepChange& changes) {1336// When looking for unexpected concrete methods, look beneath expected ones, to see if there are overrides.1337// * CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness.1338Klass* new_type = changes.as_new_klass_change()->new_type();1339assert(!is_participant(new_type), "only old classes are participants");1340if (is_witness(new_type)) {1341return new_type;1342} else {1343// No witness found, but is_witness() doesn't detect method re-abstraction in case of spot-checking.1344if (witnessed_reabstraction_in_supers(new_type)) {1345return new_type;1346}1347}1348// No witness found. The dependency remains unbroken.1349return NULL;1350}13511352bool ConcreteMethodFinder::witnessed_reabstraction_in_supers(Klass* k) {1353if (!k->is_instance_klass()) {1354return false; // no methods to find in an array type1355} else {1356// Looking for a case when an abstract method is inherited into a concrete class.1357if (Dependencies::is_concrete_klass(k) && !k->is_interface()) {1358Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1359if (m != NULL) {1360return false; // no reabstraction possible: local method found1361}1362for (InstanceKlass* super = k->java_super(); super != NULL; super = super->java_super()) {1363m = super->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1364if (m != NULL) { // inherited method found1365if (m->is_abstract() || m->is_overpass()) {1366return record_witness(super, m); // abstract method found1367}1368return false;1369}1370}1371// Miranda.1372return true;1373}1374return false;1375}1376}137713781379Klass* ConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) {1380// Walk hierarchy under a context type, looking for unexpected types.1381for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {1382Klass* sub = iter.klass();1383if (is_witness(sub)) {1384return sub; // found a witness1385}1386}1387// No witness found. The dependency remains unbroken.1388return NULL;1389}13901391// For some method m and some class ctxk (subclass of method holder),1392// enumerate all distinct overrides of m in concrete subclasses of ctxk.1393// It relies on vtable/itable information to perform method selection on each linked subclass1394// and ignores all non yet linked ones (speculatively treat them as "effectively abstract").1395class LinkedConcreteMethodFinder : public AbstractClassHierarchyWalker {1396private:1397InstanceKlass* _resolved_klass; // resolved class (JVMS-5.4.3.1)1398InstanceKlass* _declaring_klass; // the holder of resolved method (JVMS-5.4.3.3)1399int _vtable_index; // vtable/itable index of the resolved method1400bool _do_itable_lookup; // choose between itable and vtable lookup logic14011402// cache of method lookups1403Method* _found_methods[PARTICIPANT_LIMIT+1];14041405bool is_witness(Klass* k);1406Method* select_method(InstanceKlass* recv_klass);1407static int compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, bool& is_itable_index);1408static bool is_concrete_klass(InstanceKlass* ik);14091410void add_participant(Method* m, Klass* participant) {1411uint np = num_participants();1412AbstractClassHierarchyWalker::add_participant(participant);1413assert(np + 1 == num_participants(), "sanity");1414_found_methods[np] = m; // record the method for the participant1415}14161417bool record_witness(Klass* witness, Method* m) {1418for (uint i = 0; i < num_participants(); i++) {1419if (found_method(i) == m) {1420return false; // already recorded1421}1422}1423// Record not yet seen method.1424_found_methods[num_participants()] = m;1425return AbstractClassHierarchyWalker::record_witness(witness);1426}14271428void initialize(Method* participant) {1429for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) {1430_found_methods[i] = NULL;1431}1432if (participant != NULL) {1433add_participant(participant, participant->method_holder());1434}1435}14361437protected:1438virtual Klass* find_witness_in(KlassDepChange& changes);1439virtual Klass* find_witness_anywhere(InstanceKlass* context_type);14401441public:1442// In order to perform method selection, the following info is needed:1443// (1) interface or virtual call;1444// (2) vtable/itable index;1445// (3) declaring class (in case of interface call).1446//1447// It is prepared based on the results of method resolution: resolved class and resolved method (as specified in JVMS-5.4.3.3).1448// Optionally, a method which was previously determined as a unique target (uniqm) is added as a participant1449// to enable dependency spot-checking and speed up the search.1450LinkedConcreteMethodFinder(InstanceKlass* resolved_klass, Method* resolved_method, Method* uniqm = NULL) : AbstractClassHierarchyWalker(NULL) {1451assert(UseVtableBasedCHA, "required");1452assert(resolved_klass->is_linked(), "required");1453assert(resolved_method->method_holder()->is_linked(), "required");1454assert(!resolved_method->can_be_statically_bound(), "no vtable index available");14551456_resolved_klass = resolved_klass;1457_declaring_klass = resolved_method->method_holder();1458_vtable_index = compute_vtable_index(resolved_klass, resolved_method,1459_do_itable_lookup); // out parameter1460assert(_vtable_index >= 0, "invalid vtable index");14611462initialize(uniqm);1463}14641465// Note: If n==num_participants, returns NULL.1466Method* found_method(uint n) {1467assert(n <= num_participants(), "oob");1468assert(participant(n) != NULL || n == num_participants(), "proper usage");1469return _found_methods[n];1470}1471};14721473Klass* LinkedConcreteMethodFinder::find_witness_in(KlassDepChange& changes) {1474Klass* type = changes.type();14751476assert(!is_participant(type), "only old classes are participants");14771478if (is_witness(type)) {1479return type;1480}1481return NULL; // No witness found. The dependency remains unbroken.1482}14831484Klass* LinkedConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) {1485for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {1486Klass* sub = iter.klass();1487if (is_witness(sub)) {1488return sub;1489}1490if (sub->is_instance_klass() && !InstanceKlass::cast(sub)->is_linked()) {1491iter.skip_subclasses(); // ignore not yet linked classes1492}1493}1494return NULL; // No witness found. The dependency remains unbroken.1495}14961497bool LinkedConcreteMethodFinder::is_witness(Klass* k) {1498if (is_participant(k)) {1499return false; // do not report participant types1500} else if (k->is_instance_klass()) {1501InstanceKlass* ik = InstanceKlass::cast(k);1502if (is_concrete_klass(ik)) {1503Method* m = select_method(ik);1504return record_witness(ik, m);1505} else {1506return false; // ignore non-concrete holder class1507}1508} else {1509return false; // no methods to find in an array type1510}1511}15121513Method* LinkedConcreteMethodFinder::select_method(InstanceKlass* recv_klass) {1514Method* selected_method = NULL;1515if (_do_itable_lookup) {1516assert(_declaring_klass->is_interface(), "sanity");1517bool implements_interface; // initialized by method_at_itable_or_null()1518selected_method = recv_klass->method_at_itable_or_null(_declaring_klass, _vtable_index,1519implements_interface); // out parameter1520assert(implements_interface, "not implemented");1521} else {1522selected_method = recv_klass->method_at_vtable(_vtable_index);1523}1524return selected_method; // NULL when corresponding slot is empty (AbstractMethodError case)1525}15261527int LinkedConcreteMethodFinder::compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method,1528// out parameter1529bool& is_itable_index) {1530if (resolved_klass->is_interface() && resolved_method->has_itable_index()) {1531is_itable_index = true;1532return resolved_method->itable_index();1533}1534// Check for default or miranda method first.1535InstanceKlass* declaring_klass = resolved_method->method_holder();1536if (!resolved_klass->is_interface() && declaring_klass->is_interface()) {1537is_itable_index = false;1538return resolved_klass->vtable_index_of_interface_method(resolved_method);1539}1540// At this point we are sure that resolved_method is virtual and not1541// a default or miranda method; therefore, it must have a valid vtable index.1542assert(resolved_method->has_vtable_index(), "");1543is_itable_index = false;1544return resolved_method->vtable_index();1545}15461547bool LinkedConcreteMethodFinder::is_concrete_klass(InstanceKlass* ik) {1548if (!Dependencies::is_concrete_klass(ik)) {1549return false; // not concrete1550}1551if (ik->is_interface()) {1552return false; // interfaces aren't concrete1553}1554if (!ik->is_linked()) {1555return false; // not yet linked classes don't have instances1556}1557return true;1558}15591560#ifdef ASSERT1561// Assert that m is inherited into ctxk, without intervening overrides.1562// (May return true even if this is not true, in corner cases where we punt.)1563bool Dependencies::verify_method_context(InstanceKlass* ctxk, Method* m) {1564if (m->is_private()) {1565return false; // Quick lose. Should not happen.1566}1567if (m->method_holder() == ctxk) {1568return true; // Quick win.1569}1570if (!(m->is_public() || m->is_protected())) {1571// The override story is complex when packages get involved.1572return true; // Must punt the assertion to true.1573}1574Method* lm = ctxk->lookup_method(m->name(), m->signature());1575if (lm == NULL && ctxk->is_instance_klass()) {1576// It might be an interface method1577lm = InstanceKlass::cast(ctxk)->lookup_method_in_ordered_interfaces(m->name(),1578m->signature());1579}1580if (lm == m) {1581// Method m is inherited into ctxk.1582return true;1583}1584if (lm != NULL) {1585if (!(lm->is_public() || lm->is_protected())) {1586// Method is [package-]private, so the override story is complex.1587return true; // Must punt the assertion to true.1588}1589if (lm->is_static()) {1590// Static methods don't override non-static so punt1591return true;1592}1593if (!Dependencies::is_concrete_method(lm, ctxk) &&1594!Dependencies::is_concrete_method(m, ctxk)) {1595// They are both non-concrete1596if (lm->method_holder()->is_subtype_of(m->method_holder())) {1597// Method m is overridden by lm, but both are non-concrete.1598return true;1599}1600if (lm->method_holder()->is_interface() && m->method_holder()->is_interface() &&1601ctxk->is_subtype_of(m->method_holder()) && ctxk->is_subtype_of(lm->method_holder())) {1602// Interface method defined in multiple super interfaces1603return true;1604}1605}1606}1607ResourceMark rm;1608tty->print_cr("Dependency method not found in the associated context:");1609tty->print_cr(" context = %s", ctxk->external_name());1610tty->print( " method = "); m->print_short_name(tty); tty->cr();1611if (lm != NULL) {1612tty->print( " found = "); lm->print_short_name(tty); tty->cr();1613}1614return false;1615}1616#endif // ASSERT16171618bool Dependencies::is_concrete_klass(Klass* k) {1619if (k->is_abstract()) return false;1620// %%% We could treat classes which are concrete but1621// have not yet been instantiated as virtually abstract.1622// This would require a deoptimization barrier on first instantiation.1623//if (k->is_not_instantiated()) return false;1624return true;1625}16261627bool Dependencies::is_concrete_method(Method* m, Klass* k) {1628// NULL is not a concrete method.1629if (m == NULL) {1630return false;1631}1632// Statics are irrelevant to virtual call sites.1633if (m->is_static()) {1634return false;1635}1636// Abstract methods are not concrete.1637if (m->is_abstract()) {1638return false;1639}1640// Overpass (error) methods are not concrete if k is abstract.1641if (m->is_overpass() && k != NULL) {1642return !k->is_abstract();1643}1644// Note "true" is conservative answer: overpass clause is false if k == NULL,1645// implies return true if answer depends on overpass clause.1646return true;1647}16481649Klass* Dependencies::find_finalizable_subclass(InstanceKlass* ik) {1650for (ClassHierarchyIterator iter(ik); !iter.done(); iter.next()) {1651Klass* sub = iter.klass();1652if (sub->has_finalizer() && !sub->is_interface()) {1653return sub;1654}1655}1656return NULL; // not found1657}16581659bool Dependencies::is_concrete_klass(ciInstanceKlass* k) {1660if (k->is_abstract()) return false;1661// We could also return false if k does not yet appear to be1662// instantiated, if the VM version supports this distinction also.1663//if (k->is_not_instantiated()) return false;1664return true;1665}16661667bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) {1668return k->has_finalizable_subclass();1669}16701671// Any use of the contents (bytecodes) of a method must be1672// marked by an "evol_method" dependency, if those contents1673// can change. (Note: A method is always dependent on itself.)1674Klass* Dependencies::check_evol_method(Method* m) {1675assert(must_be_in_vm(), "raw oops here");1676// Did somebody do a JVMTI RedefineClasses while our backs were turned?1677// Or is there a now a breakpoint?1678// (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.)1679if (m->is_old()1680|| m->number_of_breakpoints() > 0) {1681return m->method_holder();1682} else {1683return NULL;1684}1685}16861687// This is a strong assertion: It is that the given type1688// has no subtypes whatever. It is most useful for1689// optimizing checks on reflected types or on array types.1690// (Checks on types which are derived from real instances1691// can be optimized more strongly than this, because we1692// know that the checked type comes from a concrete type,1693// and therefore we can disregard abstract types.)1694Klass* Dependencies::check_leaf_type(InstanceKlass* ctxk) {1695assert(must_be_in_vm(), "raw oops here");1696assert_locked_or_safepoint(Compile_lock);1697Klass* sub = ctxk->subklass();1698if (sub != NULL) {1699return sub;1700} else if (ctxk->nof_implementors() != 0) {1701// if it is an interface, it must be unimplemented1702// (if it is not an interface, nof_implementors is always zero)1703InstanceKlass* impl = ctxk->implementor();1704assert(impl != NULL, "must be set");1705return impl;1706} else {1707return NULL;1708}1709}17101711// Test the assertion that conck is the only concrete subtype* of ctxk.1712// The type conck itself is allowed to have have further concrete subtypes.1713// This allows the compiler to narrow occurrences of ctxk by conck,1714// when dealing with the types of actual instances.1715Klass* Dependencies::check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk,1716Klass* conck,1717NewKlassDepChange* changes) {1718ConcreteSubtypeFinder wf(conck);1719Klass* k = wf.find_witness(ctxk, changes);1720return k;1721}172217231724// Find the unique concrete proper subtype of ctxk, or NULL if there1725// is more than one concrete proper subtype. If there are no concrete1726// proper subtypes, return ctxk itself, whether it is concrete or not.1727// The returned subtype is allowed to have have further concrete subtypes.1728// That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }.1729Klass* Dependencies::find_unique_concrete_subtype(InstanceKlass* ctxk) {1730ConcreteSubtypeFinder wf(ctxk); // Ignore ctxk when walking.1731wf.record_witnesses(1); // Record one other witness when walking.1732Klass* wit = wf.find_witness(ctxk);1733if (wit != NULL) return NULL; // Too many witnesses.1734Klass* conck = wf.participant(0);1735if (conck == NULL) {1736return ctxk; // Return ctxk as a flag for "no subtypes".1737} else {1738#ifndef PRODUCT1739// Make sure the dependency mechanism will pass this discovery:1740if (VerifyDependencies) {1741// Turn off dependency tracing while actually testing deps.1742FlagSetting fs(TraceDependencies, false);1743if (!Dependencies::is_concrete_klass(ctxk)) {1744guarantee(NULL == (void *)1745check_abstract_with_unique_concrete_subtype(ctxk, conck),1746"verify dep.");1747}1748}1749#endif //PRODUCT1750return conck;1751}1752}17531754// If a class (or interface) has a unique concrete method uniqm, return NULL.1755// Otherwise, return a class that contains an interfering method.1756Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk,1757Method* uniqm,1758NewKlassDepChange* changes) {1759// Here is a missing optimization: If uniqm->is_final(),1760// we don't really need to search beneath it for overrides.1761// This is probably not important, since we don't use dependencies1762// to track final methods. (They can't be "definalized".)1763ConcreteMethodFinder wf(uniqm, uniqm->method_holder());1764Klass* k = wf.find_witness(ctxk, changes);1765return k;1766}17671768// Find the set of all non-abstract methods under ctxk that match m.1769// (The method m must be defined or inherited in ctxk.)1770// Include m itself in the set, unless it is abstract.1771// If this set has exactly one element, return that element.1772Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass** participant) {1773// Return NULL if m is marked old; must have been a redefined method.1774if (m->is_old()) {1775return NULL;1776}1777if (m->is_default_method()) {1778return NULL; // not supported1779}1780assert(verify_method_context(ctxk, m), "proper context");1781ConcreteMethodFinder wf(m);1782wf.record_witnesses(1);1783Klass* wit = wf.find_witness(ctxk);1784if (wit != NULL) return NULL; // Too many witnesses.1785Method* fm = wf.found_method(0); // Will be NULL if num_parts == 0.1786if (participant != NULL) {1787(*participant) = wf.participant(0);1788}1789if (Dependencies::is_concrete_method(m, ctxk)) {1790if (fm == NULL) {1791// It turns out that m was always the only implementation.1792fm = m;1793} else if (fm != m) {1794// Two conflicting implementations after all.1795// (This can happen if m is inherited into ctxk and fm overrides it.)1796return NULL;1797}1798}1799#ifndef PRODUCT1800// Make sure the dependency mechanism will pass this discovery:1801if (VerifyDependencies && fm != NULL) {1802guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm),1803"verify dep.");1804}1805#endif //PRODUCT1806return fm;1807}18081809// If a class (or interface) has a unique concrete method uniqm, return NULL.1810// Otherwise, return a class that contains an interfering method.1811Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk,1812Method* uniqm,1813Klass* resolved_klass,1814Method* resolved_method,1815KlassDepChange* changes) {1816assert(UseVtableBasedCHA, "required");1817assert(!ctxk->is_interface() || ctxk == resolved_klass, "sanity");1818assert(!resolved_method->can_be_statically_bound() || resolved_method == uniqm, "sanity");1819assert(resolved_klass->is_subtype_of(resolved_method->method_holder()), "sanity");18201821if (!InstanceKlass::cast(resolved_klass)->is_linked() ||1822!resolved_method->method_holder()->is_linked() ||1823resolved_method->can_be_statically_bound()) {1824// Dependency is redundant, but benign. Just keep it to avoid unnecessary recompilation.1825return NULL; // no vtable index available1826}18271828LinkedConcreteMethodFinder mf(InstanceKlass::cast(resolved_klass), resolved_method, uniqm);1829return mf.find_witness(ctxk, changes);1830}18311832// Find the set of all non-abstract methods under ctxk that match m.1833// (The method m must be defined or inherited in ctxk.)1834// Include m itself in the set, unless it is abstract.1835// If this set has exactly one element, return that element.1836// Not yet linked subclasses of ctxk are ignored since they don't have any instances yet.1837// Additionally, resolved_klass and resolved_method complete the description of the call site being analyzed.1838Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method) {1839// Return NULL if m is marked old; must have been a redefined method.1840if (m->is_old()) {1841return NULL;1842}1843if (!InstanceKlass::cast(resolved_klass)->is_linked() ||1844!resolved_method->method_holder()->is_linked() ||1845resolved_method->can_be_statically_bound()) {1846return m; // nothing to do: no witness under ctxk1847}1848LinkedConcreteMethodFinder wf(InstanceKlass::cast(resolved_klass), resolved_method);1849assert(Dependencies::verify_method_context(ctxk, m), "proper context");1850wf.record_witnesses(1);1851Klass* wit = wf.find_witness(ctxk);1852if (wit != NULL) {1853return NULL; // Too many witnesses.1854}1855// p == NULL when no participants are found (wf.num_participants() == 0).1856// fm == NULL case has 2 meanings:1857// * when p == NULL: no method found;1858// * when p != NULL: AbstractMethodError-throwing method found.1859// Also, found method should always be accompanied by a participant class.1860Klass* p = wf.participant(0);1861Method* fm = wf.found_method(0);1862assert(fm == NULL || p != NULL, "no participant");1863// Normalize all error-throwing cases to NULL.1864if (fm == Universe::throw_illegal_access_error() ||1865fm == Universe::throw_no_such_method_error() ||1866!Dependencies::is_concrete_method(fm, p)) {1867fm = NULL; // error-throwing method1868}1869if (Dependencies::is_concrete_method(m, ctxk)) {1870if (p == NULL) {1871// It turns out that m was always the only implementation.1872assert(fm == NULL, "sanity");1873fm = m;1874}1875}1876#ifndef PRODUCT1877// Make sure the dependency mechanism will pass this discovery:1878if (VerifyDependencies && fm != NULL) {1879guarantee(NULL == check_unique_concrete_method(ctxk, fm, resolved_klass, resolved_method),1880"verify dep.");1881}1882#endif // PRODUCT1883assert(fm == NULL || !fm->is_abstract(), "sanity");1884// Old CHA conservatively reports concrete methods in abstract classes1885// irrespective of whether they have concrete subclasses or not.1886// Also, abstract root method case is not fully supported.1887#ifdef ASSERT1888Klass* uniqp = NULL;1889Method* uniqm = Dependencies::find_unique_concrete_method(ctxk, m, &uniqp);1890assert(uniqm == NULL || uniqm == fm ||1891m->is_abstract() ||1892uniqm->method_holder()->is_abstract() ||1893(fm == NULL && uniqm != NULL && uniqp != NULL && !InstanceKlass::cast(uniqp)->is_linked()),1894"sanity");1895#endif // ASSERT1896return fm;1897}18981899Klass* Dependencies::check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes) {1900InstanceKlass* search_at = ctxk;1901if (changes != NULL) {1902search_at = changes->new_type(); // just look at the new bit1903}1904return find_finalizable_subclass(search_at);1905}19061907Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {1908assert(call_site != NULL, "sanity");1909assert(method_handle != NULL, "sanity");1910assert(call_site->is_a(vmClasses::CallSite_klass()), "sanity");19111912if (changes == NULL) {1913// Validate all CallSites1914if (java_lang_invoke_CallSite::target(call_site) != method_handle)1915return call_site->klass(); // assertion failed1916} else {1917// Validate the given CallSite1918if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {1919assert(method_handle != changes->method_handle(), "must be");1920return call_site->klass(); // assertion failed1921}1922}1923return NULL; // assertion still valid1924}19251926void Dependencies::DepStream::trace_and_log_witness(Klass* witness) {1927if (witness != NULL) {1928if (TraceDependencies) {1929print_dependency(witness, /*verbose=*/ true);1930}1931// The following is a no-op unless logging is enabled:1932log_dependency(witness);1933}1934}19351936Klass* Dependencies::DepStream::check_new_klass_dependency(NewKlassDepChange* changes) {1937assert_locked_or_safepoint(Compile_lock);1938Dependencies::check_valid_dependency_type(type());19391940Klass* witness = NULL;1941switch (type()) {1942case evol_method:1943witness = check_evol_method(method_argument(0));1944break;1945case leaf_type:1946witness = check_leaf_type(context_type());1947break;1948case abstract_with_unique_concrete_subtype:1949witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes);1950break;1951case unique_concrete_method_2:1952witness = check_unique_concrete_method(context_type(), method_argument(1), changes);1953break;1954case unique_concrete_method_4:1955witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes);1956break;1957case no_finalizable_subclasses:1958witness = check_has_no_finalizable_subclasses(context_type(), changes);1959break;1960default:1961witness = NULL;1962break;1963}1964trace_and_log_witness(witness);1965return witness;1966}19671968Klass* Dependencies::DepStream::check_klass_init_dependency(KlassInitDepChange* changes) {1969assert_locked_or_safepoint(Compile_lock);1970Dependencies::check_valid_dependency_type(type());19711972// No new types added. Only unique_concrete_method_4 is sensitive to class initialization changes.1973Klass* witness = NULL;1974switch (type()) {1975case unique_concrete_method_4:1976witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes);1977break;1978default:1979witness = NULL;1980break;1981}1982trace_and_log_witness(witness);1983return witness;1984}19851986Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {1987assert_locked_or_safepoint(Compile_lock);1988Dependencies::check_valid_dependency_type(type());19891990if (changes != NULL) {1991if (UseVtableBasedCHA && changes->is_klass_init_change()) {1992return check_klass_init_dependency(changes->as_klass_init_change());1993} else {1994return check_new_klass_dependency(changes->as_new_klass_change());1995}1996} else {1997Klass* witness = check_new_klass_dependency(NULL);1998// check_klass_init_dependency duplicates check_new_klass_dependency checks when class hierarchy change info is absent.1999assert(witness != NULL || check_klass_init_dependency(NULL) == NULL, "missed dependency");2000return witness;2001}2002}20032004Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) {2005assert_locked_or_safepoint(Compile_lock);2006Dependencies::check_valid_dependency_type(type());20072008Klass* witness = NULL;2009switch (type()) {2010case call_site_target_value:2011witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes);2012break;2013default:2014witness = NULL;2015break;2016}2017trace_and_log_witness(witness);2018return witness;2019}202020212022Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {2023// Handle klass dependency2024if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))2025return check_klass_dependency(changes.as_klass_change());20262027// Handle CallSite dependency2028if (changes.is_call_site_change())2029return check_call_site_dependency(changes.as_call_site_change());20302031// irrelevant dependency; skip it2032return NULL;2033}203420352036void DepChange::print() {2037int nsup = 0, nint = 0;2038for (ContextStream str(*this); str.next(); ) {2039Klass* k = str.klass();2040switch (str.change_type()) {2041case Change_new_type:2042tty->print_cr(" dependee = %s", k->external_name());2043break;2044case Change_new_sub:2045if (!WizardMode) {2046++nsup;2047} else {2048tty->print_cr(" context super = %s", k->external_name());2049}2050break;2051case Change_new_impl:2052if (!WizardMode) {2053++nint;2054} else {2055tty->print_cr(" context interface = %s", k->external_name());2056}2057break;2058default:2059break;2060}2061}2062if (nsup + nint != 0) {2063tty->print_cr(" context supers = %d, interfaces = %d", nsup, nint);2064}2065}20662067void DepChange::ContextStream::start() {2068Klass* type = (_changes.is_klass_change() ? _changes.as_klass_change()->type() : (Klass*) NULL);2069_change_type = (type == NULL ? NO_CHANGE : Start_Klass);2070_klass = type;2071_ti_base = NULL;2072_ti_index = 0;2073_ti_limit = 0;2074}20752076bool DepChange::ContextStream::next() {2077switch (_change_type) {2078case Start_Klass: // initial state; _klass is the new type2079_ti_base = InstanceKlass::cast(_klass)->transitive_interfaces();2080_ti_index = 0;2081_change_type = Change_new_type;2082return true;2083case Change_new_type:2084// fall through:2085_change_type = Change_new_sub;2086case Change_new_sub:2087// 6598190: brackets workaround Sun Studio C++ compiler bug 66292772088{2089_klass = _klass->super();2090if (_klass != NULL) {2091return true;2092}2093}2094// else set up _ti_limit and fall through:2095_ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length();2096_change_type = Change_new_impl;2097case Change_new_impl:2098if (_ti_index < _ti_limit) {2099_klass = _ti_base->at(_ti_index++);2100return true;2101}2102// fall through:2103_change_type = NO_CHANGE; // iterator is exhausted2104case NO_CHANGE:2105break;2106default:2107ShouldNotReachHere();2108}2109return false;2110}21112112void KlassDepChange::initialize() {2113// entire transaction must be under this lock:2114assert_lock_strong(Compile_lock);21152116// Mark all dependee and all its superclasses2117// Mark transitive interfaces2118for (ContextStream str(*this); str.next(); ) {2119Klass* d = str.klass();2120assert(!InstanceKlass::cast(d)->is_marked_dependent(), "checking");2121InstanceKlass::cast(d)->set_is_marked_dependent(true);2122}2123}21242125KlassDepChange::~KlassDepChange() {2126// Unmark all dependee and all its superclasses2127// Unmark transitive interfaces2128for (ContextStream str(*this); str.next(); ) {2129Klass* d = str.klass();2130InstanceKlass::cast(d)->set_is_marked_dependent(false);2131}2132}21332134bool KlassDepChange::involves_context(Klass* k) {2135if (k == NULL || !k->is_instance_klass()) {2136return false;2137}2138InstanceKlass* ik = InstanceKlass::cast(k);2139bool is_contained = ik->is_marked_dependent();2140assert(is_contained == type()->is_subtype_of(k),2141"correct marking of potential context types");2142return is_contained;2143}21442145void Dependencies::print_statistics() {2146AbstractClassHierarchyWalker::print_statistics();2147}21482149void AbstractClassHierarchyWalker::print_statistics() {2150if (UsePerfData) {2151jlong deps_find_witness_calls = _perf_find_witness_anywhere_calls_count->get_value();2152jlong deps_find_witness_steps = _perf_find_witness_anywhere_steps_count->get_value();2153jlong deps_find_witness_singles = _perf_find_witness_in_calls_count->get_value();21542155ttyLocker ttyl;2156tty->print_cr("Dependency check (find_witness) "2157"calls=" JLONG_FORMAT ", steps=" JLONG_FORMAT " (avg=%.1f), singles=" JLONG_FORMAT,2158deps_find_witness_calls,2159deps_find_witness_steps,2160(double)deps_find_witness_steps / deps_find_witness_calls,2161deps_find_witness_singles);2162if (xtty != NULL) {2163xtty->elem("deps_find_witness calls='" JLONG_FORMAT "' steps='" JLONG_FORMAT "' singles='" JLONG_FORMAT "'",2164deps_find_witness_calls,2165deps_find_witness_steps,2166deps_find_witness_singles);2167}2168}2169}21702171CallSiteDepChange::CallSiteDepChange(Handle call_site, Handle method_handle) :2172_call_site(call_site),2173_method_handle(method_handle) {2174assert(_call_site()->is_a(vmClasses::CallSite_klass()), "must be");2175assert(_method_handle.is_null() || _method_handle()->is_a(vmClasses::MethodHandle_klass()), "must be");2176}21772178void dependencies_init() {2179AbstractClassHierarchyWalker::init();2180}218121822183