Path: blob/master/src/hotspot/share/code/dependencies.cpp
64440 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_unique_implementor(ciInstanceKlass* ctxk, ciInstanceKlass* uniqk) {119check_ctxk(ctxk);120check_unique_implementor(ctxk, uniqk);121assert_common_2(unique_implementor, ctxk, uniqk);122}123124void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {125check_ctxk(ctxk);126assert_common_1(no_finalizable_subclasses, ctxk);127}128129void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {130assert_common_2(call_site_target_value, call_site, method_handle);131}132133#if INCLUDE_JVMCI134135Dependencies::Dependencies(Arena* arena, OopRecorder* oop_recorder, CompileLog* log) {136_oop_recorder = oop_recorder;137_log = log;138_dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);139_using_dep_values = true;140DEBUG_ONLY(_dep_values[end_marker] = NULL);141for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {142_dep_values[i] = new(arena) GrowableArray<DepValue>(arena, 10, 0, DepValue());143}144_content_bytes = NULL;145_size_in_bytes = (size_t)-1;146147assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");148}149150void Dependencies::assert_evol_method(Method* m) {151assert_common_1(evol_method, DepValue(_oop_recorder, m));152}153154void Dependencies::assert_has_no_finalizable_subclasses(Klass* ctxk) {155check_ctxk(ctxk);156assert_common_1(no_finalizable_subclasses, DepValue(_oop_recorder, ctxk));157}158159void Dependencies::assert_leaf_type(Klass* ctxk) {160if (ctxk->is_array_klass()) {161// As a special case, support this assertion on an array type,162// which reduces to an assertion on its element type.163// Note that this cannot be done with assertions that164// relate to concreteness or abstractness.165BasicType elemt = ArrayKlass::cast(ctxk)->element_type();166if (is_java_primitive(elemt)) return; // Ex: int[][]167ctxk = ObjArrayKlass::cast(ctxk)->bottom_klass();168//if (ctxk->is_final()) return; // Ex: String[][]169}170check_ctxk(ctxk);171assert_common_1(leaf_type, DepValue(_oop_recorder, ctxk));172}173174void Dependencies::assert_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck) {175check_ctxk_abstract(ctxk);176DepValue ctxk_dv(_oop_recorder, ctxk);177DepValue conck_dv(_oop_recorder, conck, &ctxk_dv);178assert_common_2(abstract_with_unique_concrete_subtype, ctxk_dv, conck_dv);179}180181void Dependencies::assert_unique_implementor(InstanceKlass* ctxk, InstanceKlass* uniqk) {182check_ctxk(ctxk);183assert(ctxk->is_interface(), "not an interface");184assert(ctxk->implementor() == uniqk, "not a unique implementor");185assert_common_2(unique_implementor, DepValue(_oop_recorder, ctxk), DepValue(_oop_recorder, uniqk));186}187188void Dependencies::assert_unique_concrete_method(Klass* ctxk, Method* uniqm) {189check_ctxk(ctxk);190check_unique_method(ctxk, uniqm);191assert_common_2(unique_concrete_method_2, DepValue(_oop_recorder, ctxk), DepValue(_oop_recorder, uniqm));192}193194void Dependencies::assert_call_site_target_value(oop call_site, oop method_handle) {195assert_common_2(call_site_target_value, DepValue(_oop_recorder, JNIHandles::make_local(call_site)), DepValue(_oop_recorder, JNIHandles::make_local(method_handle)));196}197198#endif // INCLUDE_JVMCI199200201// Helper function. If we are adding a new dep. under ctxk2,202// try to find an old dep. under a broader* ctxk1. If there is203//204bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,205int ctxk_i, ciKlass* ctxk2) {206ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();207if (ctxk2->is_subtype_of(ctxk1)) {208return true; // success, and no need to change209} else if (ctxk1->is_subtype_of(ctxk2)) {210// new context class fully subsumes previous one211deps->at_put(ctxk_i, ctxk2);212return true;213} else {214return false;215}216}217218void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {219assert(dep_args(dept) == 1, "sanity");220log_dependency(dept, x);221GrowableArray<ciBaseObject*>* deps = _deps[dept];222223// see if the same (or a similar) dep is already recorded224if (note_dep_seen(dept, x)) {225assert(deps->find(x) >= 0, "sanity");226} else {227deps->append(x);228}229}230231void Dependencies::assert_common_2(DepType dept,232ciBaseObject* x0, ciBaseObject* x1) {233assert(dep_args(dept) == 2, "sanity");234log_dependency(dept, x0, x1);235GrowableArray<ciBaseObject*>* deps = _deps[dept];236237// see if the same (or a similar) dep is already recorded238bool has_ctxk = has_explicit_context_arg(dept);239if (has_ctxk) {240assert(dep_context_arg(dept) == 0, "sanity");241if (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* y1 = deps->at(i+1);246if (x1 == y1) { // same subject; check the context247if (maybe_merge_ctxk(deps, i+0, x0->as_metadata()->as_klass())) {248return;249}250}251}252}253} else {254if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {255// look in this bucket for redundant assertions256const int stride = 2;257for (int i = deps->length(); (i -= stride) >= 0; ) {258ciBaseObject* y0 = deps->at(i+0);259ciBaseObject* y1 = deps->at(i+1);260if (x0 == y0 && x1 == y1) {261return;262}263}264}265}266267// append the assertion in the correct bucket:268deps->append(x0);269deps->append(x1);270}271272void Dependencies::assert_common_4(DepType dept,273ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2, ciBaseObject* x3) {274assert(has_explicit_context_arg(dept), "sanity");275assert(dep_context_arg(dept) == 0, "sanity");276assert(dep_args(dept) == 4, "sanity");277log_dependency(dept, ctxk, x1, x2, x3);278GrowableArray<ciBaseObject*>* deps = _deps[dept];279280// see if the same (or a similar) dep is already recorded281if (note_dep_seen(dept, x1) && note_dep_seen(dept, x2) && note_dep_seen(dept, x3)) {282// look in this bucket for redundant assertions283const int stride = 4;284for (int i = deps->length(); (i -= stride) >= 0; ) {285ciBaseObject* y1 = deps->at(i+1);286ciBaseObject* y2 = deps->at(i+2);287ciBaseObject* y3 = deps->at(i+3);288if (x1 == y1 && x2 == y2 && x3 == y3) { // same subjects; check the context289if (maybe_merge_ctxk(deps, i+0, ctxk)) {290return;291}292}293}294}295// append the assertion in the correct bucket:296deps->append(ctxk);297deps->append(x1);298deps->append(x2);299deps->append(x3);300}301302#if INCLUDE_JVMCI303bool Dependencies::maybe_merge_ctxk(GrowableArray<DepValue>* deps,304int ctxk_i, DepValue ctxk2_dv) {305Klass* ctxk1 = deps->at(ctxk_i).as_klass(_oop_recorder);306Klass* ctxk2 = ctxk2_dv.as_klass(_oop_recorder);307if (ctxk2->is_subtype_of(ctxk1)) {308return true; // success, and no need to change309} else if (ctxk1->is_subtype_of(ctxk2)) {310// new context class fully subsumes previous one311deps->at_put(ctxk_i, ctxk2_dv);312return true;313} else {314return false;315}316}317318void Dependencies::assert_common_1(DepType dept, DepValue x) {319assert(dep_args(dept) == 1, "sanity");320//log_dependency(dept, x);321GrowableArray<DepValue>* deps = _dep_values[dept];322323// see if the same (or a similar) dep is already recorded324if (note_dep_seen(dept, x)) {325assert(deps->find(x) >= 0, "sanity");326} else {327deps->append(x);328}329}330331void Dependencies::assert_common_2(DepType dept,332DepValue x0, DepValue x1) {333assert(dep_args(dept) == 2, "sanity");334//log_dependency(dept, x0, x1);335GrowableArray<DepValue>* deps = _dep_values[dept];336337// see if the same (or a similar) dep is already recorded338bool has_ctxk = has_explicit_context_arg(dept);339if (has_ctxk) {340assert(dep_context_arg(dept) == 0, "sanity");341if (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 y1 = deps->at(i+1);346if (x1 == y1) { // same subject; check the context347if (maybe_merge_ctxk(deps, i+0, x0)) {348return;349}350}351}352}353} else {354if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {355// look in this bucket for redundant assertions356const int stride = 2;357for (int i = deps->length(); (i -= stride) >= 0; ) {358DepValue y0 = deps->at(i+0);359DepValue y1 = deps->at(i+1);360if (x0 == y0 && x1 == y1) {361return;362}363}364}365}366367// append the assertion in the correct bucket:368deps->append(x0);369deps->append(x1);370}371#endif // INCLUDE_JVMCI372373/// Support for encoding dependencies into an nmethod:374375void Dependencies::copy_to(nmethod* nm) {376address beg = nm->dependencies_begin();377address end = nm->dependencies_end();378guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");379Copy::disjoint_words((HeapWord*) content_bytes(),380(HeapWord*) beg,381size_in_bytes() / sizeof(HeapWord));382assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");383}384385static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) {386for (int i = 0; i < narg; i++) {387int diff = p1[i]->ident() - p2[i]->ident();388if (diff != 0) return diff;389}390return 0;391}392static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2)393{ return sort_dep(p1, p2, 1); }394static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2)395{ return sort_dep(p1, p2, 2); }396static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2)397{ return sort_dep(p1, p2, 3); }398static int sort_dep_arg_4(ciBaseObject** p1, ciBaseObject** p2)399{ return sort_dep(p1, p2, 4); }400401#if INCLUDE_JVMCI402// metadata deps are sorted before object deps403static int sort_dep_value(Dependencies::DepValue* p1, Dependencies::DepValue* p2, int narg) {404for (int i = 0; i < narg; i++) {405int diff = p1[i].sort_key() - p2[i].sort_key();406if (diff != 0) return diff;407}408return 0;409}410static int sort_dep_value_arg_1(Dependencies::DepValue* p1, Dependencies::DepValue* p2)411{ return sort_dep_value(p1, p2, 1); }412static int sort_dep_value_arg_2(Dependencies::DepValue* p1, Dependencies::DepValue* p2)413{ return sort_dep_value(p1, p2, 2); }414static int sort_dep_value_arg_3(Dependencies::DepValue* p1, Dependencies::DepValue* p2)415{ return sort_dep_value(p1, p2, 3); }416#endif // INCLUDE_JVMCI417418void Dependencies::sort_all_deps() {419#if INCLUDE_JVMCI420if (_using_dep_values) {421for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {422DepType dept = (DepType)deptv;423GrowableArray<DepValue>* deps = _dep_values[dept];424if (deps->length() <= 1) continue;425switch (dep_args(dept)) {426case 1: deps->sort(sort_dep_value_arg_1, 1); break;427case 2: deps->sort(sort_dep_value_arg_2, 2); break;428case 3: deps->sort(sort_dep_value_arg_3, 3); break;429default: ShouldNotReachHere(); break;430}431}432return;433}434#endif // INCLUDE_JVMCI435for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {436DepType dept = (DepType)deptv;437GrowableArray<ciBaseObject*>* deps = _deps[dept];438if (deps->length() <= 1) continue;439switch (dep_args(dept)) {440case 1: deps->sort(sort_dep_arg_1, 1); break;441case 2: deps->sort(sort_dep_arg_2, 2); break;442case 3: deps->sort(sort_dep_arg_3, 3); break;443case 4: deps->sort(sort_dep_arg_4, 4); break;444default: ShouldNotReachHere(); break;445}446}447}448449size_t Dependencies::estimate_size_in_bytes() {450size_t est_size = 100;451#if INCLUDE_JVMCI452if (_using_dep_values) {453for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {454DepType dept = (DepType)deptv;455GrowableArray<DepValue>* deps = _dep_values[dept];456est_size += deps->length() * 2; // tags and argument(s)457}458return est_size;459}460#endif // INCLUDE_JVMCI461for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {462DepType dept = (DepType)deptv;463GrowableArray<ciBaseObject*>* deps = _deps[dept];464est_size += deps->length()*2; // tags and argument(s)465}466return est_size;467}468469ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) {470switch (dept) {471case unique_concrete_method_2:472case unique_concrete_method_4:473return x->as_metadata()->as_method()->holder();474default:475return NULL; // let NULL be NULL476}477}478479Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) {480assert(must_be_in_vm(), "raw oops here");481switch (dept) {482case unique_concrete_method_2:483case unique_concrete_method_4:484assert(x->is_method(), "sanity");485return ((Method*)x)->method_holder();486default:487return NULL; // let NULL be NULL488}489}490491void Dependencies::encode_content_bytes() {492sort_all_deps();493494// cast is safe, no deps can overflow INT_MAX495CompressedWriteStream bytes((int)estimate_size_in_bytes());496497#if INCLUDE_JVMCI498if (_using_dep_values) {499for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {500DepType dept = (DepType)deptv;501GrowableArray<DepValue>* deps = _dep_values[dept];502if (deps->length() == 0) continue;503int stride = dep_args(dept);504int ctxkj = dep_context_arg(dept); // -1 if no context arg505assert(stride > 0, "sanity");506for (int i = 0; i < deps->length(); i += stride) {507jbyte code_byte = (jbyte)dept;508int skipj = -1;509if (ctxkj >= 0 && ctxkj+1 < stride) {510Klass* ctxk = deps->at(i+ctxkj+0).as_klass(_oop_recorder);511DepValue x = deps->at(i+ctxkj+1); // following argument512if (ctxk == ctxk_encoded_as_null(dept, x.as_metadata(_oop_recorder))) {513skipj = ctxkj; // we win: maybe one less oop to keep track of514code_byte |= default_context_type_bit;515}516}517bytes.write_byte(code_byte);518for (int j = 0; j < stride; j++) {519if (j == skipj) continue;520DepValue v = deps->at(i+j);521int idx = v.index();522bytes.write_int(idx);523}524}525}526} else {527#endif // INCLUDE_JVMCI528for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {529DepType dept = (DepType)deptv;530GrowableArray<ciBaseObject*>* deps = _deps[dept];531if (deps->length() == 0) continue;532int stride = dep_args(dept);533int ctxkj = dep_context_arg(dept); // -1 if no context arg534assert(stride > 0, "sanity");535for (int i = 0; i < deps->length(); i += stride) {536jbyte code_byte = (jbyte)dept;537int skipj = -1;538if (ctxkj >= 0 && ctxkj+1 < stride) {539ciKlass* ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass();540ciBaseObject* x = deps->at(i+ctxkj+1); // following argument541if (ctxk == ctxk_encoded_as_null(dept, x)) {542skipj = ctxkj; // we win: maybe one less oop to keep track of543code_byte |= default_context_type_bit;544}545}546bytes.write_byte(code_byte);547for (int j = 0; j < stride; j++) {548if (j == skipj) continue;549ciBaseObject* v = deps->at(i+j);550int idx;551if (v->is_object()) {552idx = _oop_recorder->find_index(v->as_object()->constant_encoding());553} else {554ciMetadata* meta = v->as_metadata();555idx = _oop_recorder->find_index(meta->constant_encoding());556}557bytes.write_int(idx);558}559}560}561#if INCLUDE_JVMCI562}563#endif564565// write a sentinel byte to mark the end566bytes.write_byte(end_marker);567568// round it out to a word boundary569while (bytes.position() % sizeof(HeapWord) != 0) {570bytes.write_byte(end_marker);571}572573// check whether the dept byte encoding really works574assert((jbyte)default_context_type_bit != 0, "byte overflow");575576_content_bytes = bytes.buffer();577_size_in_bytes = bytes.position();578}579580581const char* Dependencies::_dep_name[TYPE_LIMIT] = {582"end_marker",583"evol_method",584"leaf_type",585"abstract_with_unique_concrete_subtype",586"unique_concrete_method_2",587"unique_concrete_method_4",588"unique_implementor",589"no_finalizable_subclasses",590"call_site_target_value"591};592593int Dependencies::_dep_args[TYPE_LIMIT] = {594-1,// end_marker5951, // evol_method m5961, // leaf_type ctxk5972, // abstract_with_unique_concrete_subtype ctxk, k5982, // unique_concrete_method_2 ctxk, m5994, // unique_concrete_method_4 ctxk, m, resolved_klass, resolved_method6002, // unique_implementor ctxk, implementor6011, // no_finalizable_subclasses ctxk6022 // call_site_target_value call_site, method_handle603};604605const char* Dependencies::dep_name(Dependencies::DepType dept) {606if (!dept_in_mask(dept, all_types)) return "?bad-dep?";607return _dep_name[dept];608}609610int Dependencies::dep_args(Dependencies::DepType dept) {611if (!dept_in_mask(dept, all_types)) return -1;612return _dep_args[dept];613}614615void Dependencies::check_valid_dependency_type(DepType dept) {616guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, "invalid dependency type: %d", (int) dept);617}618619Dependencies::DepType Dependencies::validate_dependencies(CompileTask* task, char** failure_detail) {620int klass_violations = 0;621DepType result = end_marker;622for (Dependencies::DepStream deps(this); deps.next(); ) {623Klass* witness = deps.check_dependency();624if (witness != NULL) {625if (klass_violations == 0) {626result = deps.type();627if (failure_detail != NULL && klass_violations == 0) {628// Use a fixed size buffer to prevent the string stream from629// resizing in the context of an inner resource mark.630char* buffer = NEW_RESOURCE_ARRAY(char, O_BUFLEN);631stringStream st(buffer, O_BUFLEN);632deps.print_dependency(witness, true, &st);633*failure_detail = st.as_string();634}635}636klass_violations++;637if (xtty == NULL) {638// If we're not logging then a single violation is sufficient,639// otherwise we want to log all the dependences which were640// violated.641break;642}643}644}645646return result;647}648649// for the sake of the compiler log, print out current dependencies:650void Dependencies::log_all_dependencies() {651if (log() == NULL) return;652ResourceMark rm;653for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {654DepType dept = (DepType)deptv;655GrowableArray<ciBaseObject*>* deps = _deps[dept];656int deplen = deps->length();657if (deplen == 0) {658continue;659}660int stride = dep_args(dept);661GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(stride);662for (int i = 0; i < deps->length(); i += stride) {663for (int j = 0; j < stride; j++) {664// flush out the identities before printing665ciargs->push(deps->at(i+j));666}667write_dependency_to(log(), dept, ciargs);668ciargs->clear();669}670guarantee(deplen == deps->length(), "deps array cannot grow inside nested ResoureMark scope");671}672}673674void Dependencies::write_dependency_to(CompileLog* log,675DepType dept,676GrowableArray<DepArgument>* args,677Klass* witness) {678if (log == NULL) {679return;680}681ResourceMark rm;682ciEnv* env = ciEnv::current();683GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(args->length());684for (GrowableArrayIterator<DepArgument> it = args->begin(); it != args->end(); ++it) {685DepArgument arg = *it;686if (arg.is_oop()) {687ciargs->push(env->get_object(arg.oop_value()));688} else {689ciargs->push(env->get_metadata(arg.metadata_value()));690}691}692int argslen = ciargs->length();693Dependencies::write_dependency_to(log, dept, ciargs, witness);694guarantee(argslen == ciargs->length(), "ciargs array cannot grow inside nested ResoureMark scope");695}696697void Dependencies::write_dependency_to(CompileLog* log,698DepType dept,699GrowableArray<ciBaseObject*>* args,700Klass* witness) {701if (log == NULL) {702return;703}704ResourceMark rm;705GrowableArray<int>* argids = new GrowableArray<int>(args->length());706for (GrowableArrayIterator<ciBaseObject*> it = args->begin(); it != args->end(); ++it) {707ciBaseObject* obj = *it;708if (obj->is_object()) {709argids->push(log->identify(obj->as_object()));710} else {711argids->push(log->identify(obj->as_metadata()));712}713}714if (witness != NULL) {715log->begin_elem("dependency_failed");716} else {717log->begin_elem("dependency");718}719log->print(" type='%s'", dep_name(dept));720const int ctxkj = dep_context_arg(dept); // -1 if no context arg721if (ctxkj >= 0 && ctxkj < argids->length()) {722log->print(" ctxk='%d'", argids->at(ctxkj));723}724// write remaining arguments, if any.725for (int j = 0; j < argids->length(); j++) {726if (j == ctxkj) continue; // already logged727if (j == 1) {728log->print( " x='%d'", argids->at(j));729} else {730log->print(" x%d='%d'", j, argids->at(j));731}732}733if (witness != NULL) {734log->object("witness", witness);735log->stamp();736}737log->end_elem();738}739740void Dependencies::write_dependency_to(xmlStream* xtty,741DepType dept,742GrowableArray<DepArgument>* args,743Klass* witness) {744if (xtty == NULL) {745return;746}747Thread* thread = Thread::current();748HandleMark rm(thread);749ttyLocker ttyl;750int ctxkj = dep_context_arg(dept); // -1 if no context arg751if (witness != NULL) {752xtty->begin_elem("dependency_failed");753} else {754xtty->begin_elem("dependency");755}756xtty->print(" type='%s'", dep_name(dept));757if (ctxkj >= 0) {758xtty->object("ctxk", args->at(ctxkj).metadata_value());759}760// write remaining arguments, if any.761for (int j = 0; j < args->length(); j++) {762if (j == ctxkj) continue; // already logged763DepArgument arg = args->at(j);764if (j == 1) {765if (arg.is_oop()) {766xtty->object("x", Handle(thread, arg.oop_value()));767} else {768xtty->object("x", arg.metadata_value());769}770} else {771char xn[12]; sprintf(xn, "x%d", j);772if (arg.is_oop()) {773xtty->object(xn, Handle(thread, arg.oop_value()));774} else {775xtty->object(xn, arg.metadata_value());776}777}778}779if (witness != NULL) {780xtty->object("witness", witness);781xtty->stamp();782}783xtty->end_elem();784}785786void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args,787Klass* witness, outputStream* st) {788ResourceMark rm;789ttyLocker ttyl; // keep the following output all in one block790st->print_cr("%s of type %s",791(witness == NULL)? "Dependency": "Failed dependency",792dep_name(dept));793// print arguments794int ctxkj = dep_context_arg(dept); // -1 if no context arg795for (int j = 0; j < args->length(); j++) {796DepArgument arg = args->at(j);797bool put_star = false;798if (arg.is_null()) continue;799const char* what;800if (j == ctxkj) {801assert(arg.is_metadata(), "must be");802what = "context";803put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value());804} else if (arg.is_method()) {805what = "method ";806put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), NULL);807} else if (arg.is_klass()) {808what = "class ";809} else {810what = "object ";811}812st->print(" %s = %s", what, (put_star? "*": ""));813if (arg.is_klass()) {814st->print("%s", ((Klass*)arg.metadata_value())->external_name());815} else if (arg.is_method()) {816((Method*)arg.metadata_value())->print_value_on(st);817} else if (arg.is_oop()) {818arg.oop_value()->print_value_on(st);819} else {820ShouldNotReachHere(); // Provide impl for this type.821}822823st->cr();824}825if (witness != NULL) {826bool put_star = !Dependencies::is_concrete_klass(witness);827st->print_cr(" witness = %s%s",828(put_star? "*": ""),829witness->external_name());830}831}832833void Dependencies::DepStream::log_dependency(Klass* witness) {834if (_deps == NULL && xtty == NULL) return; // fast cutout for runtime835ResourceMark rm;836const int nargs = argument_count();837GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);838for (int j = 0; j < nargs; j++) {839if (is_oop_argument(j)) {840args->push(argument_oop(j));841} else {842args->push(argument(j));843}844}845int argslen = args->length();846if (_deps != NULL && _deps->log() != NULL) {847if (ciEnv::current() != NULL) {848Dependencies::write_dependency_to(_deps->log(), type(), args, witness);849} else {850// Treat the CompileLog as an xmlstream instead851Dependencies::write_dependency_to((xmlStream*)_deps->log(), type(), args, witness);852}853} else {854Dependencies::write_dependency_to(xtty, type(), args, witness);855}856guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");857}858859void Dependencies::DepStream::print_dependency(Klass* witness, bool verbose, outputStream* st) {860ResourceMark rm;861int nargs = argument_count();862GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);863for (int j = 0; j < nargs; j++) {864if (is_oop_argument(j)) {865args->push(argument_oop(j));866} else {867args->push(argument(j));868}869}870int argslen = args->length();871Dependencies::print_dependency(type(), args, witness, st);872if (verbose) {873if (_code != NULL) {874st->print(" code: ");875_code->print_value_on(st);876st->cr();877}878}879guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");880}881882883/// Dependency stream support (decodes dependencies from an nmethod):884885#ifdef ASSERT886void Dependencies::DepStream::initial_asserts(size_t byte_limit) {887assert(must_be_in_vm(), "raw oops here");888_byte_limit = byte_limit;889_type = (DepType)(end_marker-1); // defeat "already at end" assert890assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");891}892#endif //ASSERT893894bool Dependencies::DepStream::next() {895assert(_type != end_marker, "already at end");896if (_bytes.position() == 0 && _code != NULL897&& _code->dependencies_size() == 0) {898// Method has no dependencies at all.899return false;900}901int code_byte = (_bytes.read_byte() & 0xFF);902if (code_byte == end_marker) {903DEBUG_ONLY(_type = end_marker);904return false;905} else {906int ctxk_bit = (code_byte & Dependencies::default_context_type_bit);907code_byte -= ctxk_bit;908DepType dept = (DepType)code_byte;909_type = dept;910Dependencies::check_valid_dependency_type(dept);911int stride = _dep_args[dept];912assert(stride == dep_args(dept), "sanity");913int skipj = -1;914if (ctxk_bit != 0) {915skipj = 0; // currently the only context argument is at zero916assert(skipj == dep_context_arg(dept), "zero arg always ctxk");917}918for (int j = 0; j < stride; j++) {919_xi[j] = (j == skipj)? 0: _bytes.read_int();920}921DEBUG_ONLY(_xi[stride] = -1); // help detect overruns922return true;923}924}925926inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) {927Metadata* o = NULL;928if (_code != NULL) {929o = _code->metadata_at(i);930} else {931o = _deps->oop_recorder()->metadata_at(i);932}933return o;934}935936inline oop Dependencies::DepStream::recorded_oop_at(int i) {937return (_code != NULL)938? _code->oop_at(i)939: JNIHandles::resolve(_deps->oop_recorder()->oop_at(i));940}941942Metadata* Dependencies::DepStream::argument(int i) {943Metadata* result = recorded_metadata_at(argument_index(i));944945if (result == NULL) { // Explicit context argument can be compressed946int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg947if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) {948result = ctxk_encoded_as_null(type(), argument(ctxkj+1));949}950}951952assert(result == NULL || result->is_klass() || result->is_method(), "must be");953return result;954}955956/**957* Returns a unique identifier for each dependency argument.958*/959uintptr_t Dependencies::DepStream::get_identifier(int i) {960if (is_oop_argument(i)) {961return (uintptr_t)(oopDesc*)argument_oop(i);962} else {963return (uintptr_t)argument(i);964}965}966967oop Dependencies::DepStream::argument_oop(int i) {968oop result = recorded_oop_at(argument_index(i));969assert(oopDesc::is_oop_or_null(result), "must be");970return result;971}972973InstanceKlass* Dependencies::DepStream::context_type() {974assert(must_be_in_vm(), "raw oops here");975976// Most dependencies have an explicit context type argument.977{978int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg979if (ctxkj >= 0) {980Metadata* k = argument(ctxkj);981assert(k != NULL && k->is_klass(), "type check");982return InstanceKlass::cast((Klass*)k);983}984}985986// Some dependencies are using the klass of the first object987// argument as implicit context type.988{989int ctxkj = dep_implicit_context_arg(type());990if (ctxkj >= 0) {991Klass* k = argument_oop(ctxkj)->klass();992assert(k != NULL, "type check");993return InstanceKlass::cast(k);994}995}996997// And some dependencies don't have a context type at all,998// e.g. evol_method.999return NULL;1000}10011002// ----------------- DependencySignature --------------------------------------1003bool DependencySignature::equals(DependencySignature const& s1, DependencySignature const& s2) {1004if ((s1.type() != s2.type()) || (s1.args_count() != s2.args_count())) {1005return false;1006}10071008for (int i = 0; i < s1.args_count(); i++) {1009if (s1.arg(i) != s2.arg(i)) {1010return false;1011}1012}1013return true;1014}10151016/// Checking dependencies10171018// This hierarchy walker inspects subtypes of a given type, trying to find a "bad" class which breaks a dependency.1019// Such a class is called a "witness" to the broken dependency.1020// While searching around, we ignore "participants", which are already known to the dependency.1021class AbstractClassHierarchyWalker {1022public:1023enum { PARTICIPANT_LIMIT = 3 };10241025private:1026// if non-zero, tells how many witnesses to convert to participants1027uint _record_witnesses;10281029// special classes which are not allowed to be witnesses:1030Klass* _participants[PARTICIPANT_LIMIT+1];1031uint _num_participants;10321033#ifdef ASSERT1034uint _nof_requests; // one-shot walker1035#endif // ASSERT10361037static PerfCounter* _perf_find_witness_anywhere_calls_count;1038static PerfCounter* _perf_find_witness_anywhere_steps_count;1039static PerfCounter* _perf_find_witness_in_calls_count;10401041protected:1042virtual Klass* find_witness_in(KlassDepChange& changes) = 0;1043virtual Klass* find_witness_anywhere(InstanceKlass* context_type) = 0;10441045AbstractClassHierarchyWalker(Klass* participant) : _record_witnesses(0), _num_participants(0)1046#ifdef ASSERT1047, _nof_requests(0)1048#endif // ASSERT1049{1050for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) {1051_participants[i] = NULL;1052}1053if (participant != NULL) {1054add_participant(participant);1055}1056}10571058bool is_participant(Klass* k) {1059for (uint i = 0; i < _num_participants; i++) {1060if (_participants[i] == k) {1061return true;1062}1063}1064return false;1065}10661067bool record_witness(Klass* witness) {1068if (_record_witnesses > 0) {1069--_record_witnesses;1070add_participant(witness);1071return false; // not a witness1072} else {1073return true; // is a witness1074}1075}10761077class CountingClassHierarchyIterator : public ClassHierarchyIterator {1078private:1079jlong _nof_steps;1080public:1081CountingClassHierarchyIterator(InstanceKlass* root) : ClassHierarchyIterator(root), _nof_steps(0) {}10821083void next() {1084_nof_steps++;1085ClassHierarchyIterator::next();1086}10871088~CountingClassHierarchyIterator() {1089if (UsePerfData) {1090_perf_find_witness_anywhere_steps_count->inc(_nof_steps);1091}1092}1093};10941095public:1096uint num_participants() { return _num_participants; }1097Klass* participant(uint n) {1098assert(n <= _num_participants, "oob");1099if (n < _num_participants) {1100return _participants[n];1101} else {1102return NULL;1103}1104}11051106void add_participant(Klass* participant) {1107assert(!is_participant(participant), "sanity");1108assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob");1109uint np = _num_participants++;1110_participants[np] = participant;1111}11121113void record_witnesses(uint add) {1114if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT;1115assert(_num_participants + add < PARTICIPANT_LIMIT, "oob");1116_record_witnesses = add;1117}11181119Klass* find_witness(InstanceKlass* context_type, KlassDepChange* changes = NULL);11201121static void init();1122static void print_statistics();1123};11241125PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_calls_count = NULL;1126PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_steps_count = NULL;1127PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_in_calls_count = NULL;11281129void AbstractClassHierarchyWalker::init() {1130if (UsePerfData) {1131EXCEPTION_MARK;1132_perf_find_witness_anywhere_calls_count =1133PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhere", PerfData::U_Events, CHECK);1134_perf_find_witness_anywhere_steps_count =1135PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhereSteps", PerfData::U_Events, CHECK);1136_perf_find_witness_in_calls_count =1137PerfDataManager::create_counter(SUN_CI, "findWitnessIn", PerfData::U_Events, CHECK);1138}1139}11401141Klass* AbstractClassHierarchyWalker::find_witness(InstanceKlass* context_type, KlassDepChange* changes) {1142// Current thread must be in VM (not native mode, as in CI):1143assert(must_be_in_vm(), "raw oops here");1144// Must not move the class hierarchy during this check:1145assert_locked_or_safepoint(Compile_lock);1146assert(_nof_requests++ == 0, "repeated requests are not supported");11471148assert(changes == NULL || changes->involves_context(context_type), "irrelevant dependency");11491150// (Note: Interfaces do not have subclasses.)1151// If it is an interface, search its direct implementors.1152// (Their subclasses are additional indirect implementors. See InstanceKlass::add_implementor().)1153if (context_type->is_interface()) {1154int nof_impls = context_type->nof_implementors();1155if (nof_impls == 0) {1156return NULL; // no implementors1157} else if (nof_impls == 1) { // unique implementor1158assert(context_type != context_type->implementor(), "not unique");1159context_type = InstanceKlass::cast(context_type->implementor());1160} else { // nof_impls >= 21161// Avoid this case: *I.m > { A.m, C }; B.m > C1162// Here, I.m has 2 concrete implementations, but m appears unique1163// as A.m, because the search misses B.m when checking C.1164// The inherited method B.m was getting missed by the walker1165// when interface 'I' was the starting point.1166// %%% Until this is fixed more systematically, bail out.1167return context_type;1168}1169}1170assert(!context_type->is_interface(), "no interfaces allowed");11711172if (changes != NULL) {1173if (UsePerfData) {1174_perf_find_witness_in_calls_count->inc();1175}1176return find_witness_in(*changes);1177} else {1178if (UsePerfData) {1179_perf_find_witness_anywhere_calls_count->inc();1180}1181return find_witness_anywhere(context_type);1182}1183}11841185class ConcreteSubtypeFinder : public AbstractClassHierarchyWalker {1186private:1187bool is_witness(Klass* k);11881189protected:1190virtual Klass* find_witness_in(KlassDepChange& changes);1191virtual Klass* find_witness_anywhere(InstanceKlass* context_type);11921193public:1194ConcreteSubtypeFinder(Klass* participant = NULL) : AbstractClassHierarchyWalker(participant) {}1195};11961197bool ConcreteSubtypeFinder::is_witness(Klass* k) {1198if (Dependencies::is_concrete_klass(k)) {1199return record_witness(k); // concrete subtype1200} else {1201return false; // not a concrete class1202}1203}12041205Klass* ConcreteSubtypeFinder::find_witness_in(KlassDepChange& changes) {1206// When looking for unexpected concrete types, do not look beneath expected ones:1207// * CX > CC > C' is OK, even if C' is new.1208// * CX > { CC, C' } is not OK if C' is new, and C' is the witness.1209Klass* new_type = changes.as_new_klass_change()->new_type();1210assert(!is_participant(new_type), "only old classes are participants");1211// If the new type is a subtype of a participant, we are done.1212for (uint i = 0; i < num_participants(); i++) {1213if (changes.involves_context(participant(i))) {1214// new guy is protected from this check by previous participant1215return NULL;1216}1217}1218if (is_witness(new_type)) {1219return new_type;1220}1221// No witness found. The dependency remains unbroken.1222return NULL;1223}12241225Klass* ConcreteSubtypeFinder::find_witness_anywhere(InstanceKlass* context_type) {1226for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {1227Klass* sub = iter.klass();1228// Do not report participant types.1229if (is_participant(sub)) {1230// Don't walk beneath a participant since it hides witnesses.1231iter.skip_subclasses();1232} else if (is_witness(sub)) {1233return sub; // found a witness1234}1235}1236// No witness found. The dependency remains unbroken.1237return NULL;1238}12391240class ConcreteMethodFinder : public AbstractClassHierarchyWalker {1241private:1242Symbol* _name;1243Symbol* _signature;12441245// cache of method lookups1246Method* _found_methods[PARTICIPANT_LIMIT+1];12471248bool is_witness(Klass* k);12491250protected:1251virtual Klass* find_witness_in(KlassDepChange& changes);1252virtual Klass* find_witness_anywhere(InstanceKlass* context_type);12531254public:1255bool witnessed_reabstraction_in_supers(Klass* k);12561257ConcreteMethodFinder(Method* m, Klass* participant = NULL) : AbstractClassHierarchyWalker(participant) {1258assert(m != NULL && m->is_method(), "sanity");1259_name = m->name();1260_signature = m->signature();12611262for (int i = 0; i < PARTICIPANT_LIMIT+1; i++) {1263_found_methods[i] = NULL;1264}1265}12661267// Note: If n==num_participants, returns NULL.1268Method* found_method(uint n) {1269assert(n <= num_participants(), "oob");1270Method* fm = _found_methods[n];1271assert(n == num_participants() || fm != NULL, "proper usage");1272if (fm != NULL && fm->method_holder() != participant(n)) {1273// Default methods from interfaces can be added to classes. In1274// that case the holder of the method is not the class but the1275// interface where it's defined.1276assert(fm->is_default_method(), "sanity");1277return NULL;1278}1279return fm;1280}12811282void add_participant(Klass* participant) {1283AbstractClassHierarchyWalker::add_participant(participant);1284_found_methods[num_participants()] = NULL;1285}12861287bool record_witness(Klass* witness, Method* m) {1288_found_methods[num_participants()] = m;1289return AbstractClassHierarchyWalker::record_witness(witness);1290}12911292private:1293static PerfCounter* _perf_find_witness_anywhere_calls_count;1294static PerfCounter* _perf_find_witness_anywhere_steps_count;1295static PerfCounter* _perf_find_witness_in_calls_count;12961297public:1298static void init();1299static void print_statistics();1300};13011302bool ConcreteMethodFinder::is_witness(Klass* k) {1303if (is_participant(k)) {1304return false; // do not report participant types1305}1306if (k->is_instance_klass()) {1307InstanceKlass* ik = InstanceKlass::cast(k);1308// Search class hierarchy first, skipping private implementations1309// as they never override any inherited methods1310Method* m = ik->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1311if (Dependencies::is_concrete_method(m, ik)) {1312return record_witness(k, m); // concrete method found1313} else {1314// Check for re-abstraction of method1315if (!ik->is_interface() && m != NULL && m->is_abstract()) {1316// Found a matching abstract method 'm' in the class hierarchy.1317// This is fine iff 'k' is an abstract class and all concrete subtypes1318// of 'k' override 'm' and are participates of the current search.1319ConcreteSubtypeFinder wf;1320for (uint i = 0; i < num_participants(); i++) {1321Klass* p = participant(i);1322wf.add_participant(p);1323}1324Klass* w = wf.find_witness(ik);1325if (w != NULL) {1326Method* wm = InstanceKlass::cast(w)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1327if (!Dependencies::is_concrete_method(wm, w)) {1328// Found a concrete subtype 'w' which does not override abstract method 'm'.1329// Bail out because 'm' could be called with 'w' as receiver (leading to an1330// AbstractMethodError) and thus the method we are looking for is not unique.1331return record_witness(k, m);1332}1333}1334}1335// Check interface defaults also, if any exist.1336Array<Method*>* default_methods = ik->default_methods();1337if (default_methods != NULL) {1338Method* dm = ik->find_method(default_methods, _name, _signature);1339if (Dependencies::is_concrete_method(dm, NULL)) {1340return record_witness(k, dm); // default method found1341}1342}1343return false; // no concrete method found1344}1345} else {1346return false; // no methods to find in an array type1347}1348}13491350Klass* ConcreteMethodFinder::find_witness_in(KlassDepChange& changes) {1351// When looking for unexpected concrete methods, look beneath expected ones, to see if there are overrides.1352// * CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness.1353Klass* new_type = changes.as_new_klass_change()->new_type();1354assert(!is_participant(new_type), "only old classes are participants");1355if (is_witness(new_type)) {1356return new_type;1357} else {1358// No witness found, but is_witness() doesn't detect method re-abstraction in case of spot-checking.1359if (witnessed_reabstraction_in_supers(new_type)) {1360return new_type;1361}1362}1363// No witness found. The dependency remains unbroken.1364return NULL;1365}13661367bool ConcreteMethodFinder::witnessed_reabstraction_in_supers(Klass* k) {1368if (!k->is_instance_klass()) {1369return false; // no methods to find in an array type1370} else {1371// Looking for a case when an abstract method is inherited into a concrete class.1372if (Dependencies::is_concrete_klass(k) && !k->is_interface()) {1373Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1374if (m != NULL) {1375return false; // no reabstraction possible: local method found1376}1377for (InstanceKlass* super = k->java_super(); super != NULL; super = super->java_super()) {1378m = super->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);1379if (m != NULL) { // inherited method found1380if (m->is_abstract() || m->is_overpass()) {1381return record_witness(super, m); // abstract method found1382}1383return false;1384}1385}1386// Miranda.1387return true;1388}1389return false;1390}1391}139213931394Klass* ConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) {1395// Walk hierarchy under a context type, looking for unexpected types.1396for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {1397Klass* sub = iter.klass();1398if (is_witness(sub)) {1399return sub; // found a witness1400}1401}1402// No witness found. The dependency remains unbroken.1403return NULL;1404}14051406// For some method m and some class ctxk (subclass of method holder),1407// enumerate all distinct overrides of m in concrete subclasses of ctxk.1408// It relies on vtable/itable information to perform method selection on each linked subclass1409// and ignores all non yet linked ones (speculatively treat them as "effectively abstract").1410class LinkedConcreteMethodFinder : public AbstractClassHierarchyWalker {1411private:1412InstanceKlass* _resolved_klass; // resolved class (JVMS-5.4.3.1)1413InstanceKlass* _declaring_klass; // the holder of resolved method (JVMS-5.4.3.3)1414int _vtable_index; // vtable/itable index of the resolved method1415bool _do_itable_lookup; // choose between itable and vtable lookup logic14161417// cache of method lookups1418Method* _found_methods[PARTICIPANT_LIMIT+1];14191420bool is_witness(Klass* k);1421Method* select_method(InstanceKlass* recv_klass);1422static int compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, bool& is_itable_index);1423static bool is_concrete_klass(InstanceKlass* ik);14241425void add_participant(Method* m, Klass* participant) {1426uint np = num_participants();1427AbstractClassHierarchyWalker::add_participant(participant);1428assert(np + 1 == num_participants(), "sanity");1429_found_methods[np] = m; // record the method for the participant1430}14311432bool record_witness(Klass* witness, Method* m) {1433for (uint i = 0; i < num_participants(); i++) {1434if (found_method(i) == m) {1435return false; // already recorded1436}1437}1438// Record not yet seen method.1439_found_methods[num_participants()] = m;1440return AbstractClassHierarchyWalker::record_witness(witness);1441}14421443void initialize(Method* participant) {1444for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) {1445_found_methods[i] = NULL;1446}1447if (participant != NULL) {1448add_participant(participant, participant->method_holder());1449}1450}14511452protected:1453virtual Klass* find_witness_in(KlassDepChange& changes);1454virtual Klass* find_witness_anywhere(InstanceKlass* context_type);14551456public:1457// In order to perform method selection, the following info is needed:1458// (1) interface or virtual call;1459// (2) vtable/itable index;1460// (3) declaring class (in case of interface call).1461//1462// It is prepared based on the results of method resolution: resolved class and resolved method (as specified in JVMS-5.4.3.3).1463// Optionally, a method which was previously determined as a unique target (uniqm) is added as a participant1464// to enable dependency spot-checking and speed up the search.1465LinkedConcreteMethodFinder(InstanceKlass* resolved_klass, Method* resolved_method, Method* uniqm = NULL) : AbstractClassHierarchyWalker(NULL) {1466assert(UseVtableBasedCHA, "required");1467assert(resolved_klass->is_linked(), "required");1468assert(resolved_method->method_holder()->is_linked(), "required");1469assert(!resolved_method->can_be_statically_bound(), "no vtable index available");14701471_resolved_klass = resolved_klass;1472_declaring_klass = resolved_method->method_holder();1473_vtable_index = compute_vtable_index(resolved_klass, resolved_method,1474_do_itable_lookup); // out parameter1475assert(_vtable_index >= 0, "invalid vtable index");14761477initialize(uniqm);1478}14791480// Note: If n==num_participants, returns NULL.1481Method* found_method(uint n) {1482assert(n <= num_participants(), "oob");1483assert(participant(n) != NULL || n == num_participants(), "proper usage");1484return _found_methods[n];1485}1486};14871488Klass* LinkedConcreteMethodFinder::find_witness_in(KlassDepChange& changes) {1489Klass* type = changes.type();14901491assert(!is_participant(type), "only old classes are participants");14921493if (is_witness(type)) {1494return type;1495}1496return NULL; // No witness found. The dependency remains unbroken.1497}14981499Klass* LinkedConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) {1500for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {1501Klass* sub = iter.klass();1502if (is_witness(sub)) {1503return sub;1504}1505if (sub->is_instance_klass() && !InstanceKlass::cast(sub)->is_linked()) {1506iter.skip_subclasses(); // ignore not yet linked classes1507}1508}1509return NULL; // No witness found. The dependency remains unbroken.1510}15111512bool LinkedConcreteMethodFinder::is_witness(Klass* k) {1513if (is_participant(k)) {1514return false; // do not report participant types1515} else if (k->is_instance_klass()) {1516InstanceKlass* ik = InstanceKlass::cast(k);1517if (is_concrete_klass(ik)) {1518Method* m = select_method(ik);1519return record_witness(ik, m);1520} else {1521return false; // ignore non-concrete holder class1522}1523} else {1524return false; // no methods to find in an array type1525}1526}15271528Method* LinkedConcreteMethodFinder::select_method(InstanceKlass* recv_klass) {1529Method* selected_method = NULL;1530if (_do_itable_lookup) {1531assert(_declaring_klass->is_interface(), "sanity");1532bool implements_interface; // initialized by method_at_itable_or_null()1533selected_method = recv_klass->method_at_itable_or_null(_declaring_klass, _vtable_index,1534implements_interface); // out parameter1535assert(implements_interface, "not implemented");1536} else {1537selected_method = recv_klass->method_at_vtable(_vtable_index);1538}1539return selected_method; // NULL when corresponding slot is empty (AbstractMethodError case)1540}15411542int LinkedConcreteMethodFinder::compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method,1543// out parameter1544bool& is_itable_index) {1545if (resolved_klass->is_interface() && resolved_method->has_itable_index()) {1546is_itable_index = true;1547return resolved_method->itable_index();1548}1549// Check for default or miranda method first.1550InstanceKlass* declaring_klass = resolved_method->method_holder();1551if (!resolved_klass->is_interface() && declaring_klass->is_interface()) {1552is_itable_index = false;1553return resolved_klass->vtable_index_of_interface_method(resolved_method);1554}1555// At this point we are sure that resolved_method is virtual and not1556// a default or miranda method; therefore, it must have a valid vtable index.1557assert(resolved_method->has_vtable_index(), "");1558is_itable_index = false;1559return resolved_method->vtable_index();1560}15611562bool LinkedConcreteMethodFinder::is_concrete_klass(InstanceKlass* ik) {1563if (!Dependencies::is_concrete_klass(ik)) {1564return false; // not concrete1565}1566if (ik->is_interface()) {1567return false; // interfaces aren't concrete1568}1569if (!ik->is_linked()) {1570return false; // not yet linked classes don't have instances1571}1572return true;1573}15741575#ifdef ASSERT1576// Assert that m is inherited into ctxk, without intervening overrides.1577// (May return true even if this is not true, in corner cases where we punt.)1578bool Dependencies::verify_method_context(InstanceKlass* ctxk, Method* m) {1579if (m->is_private()) {1580return false; // Quick lose. Should not happen.1581}1582if (m->method_holder() == ctxk) {1583return true; // Quick win.1584}1585if (!(m->is_public() || m->is_protected())) {1586// The override story is complex when packages get involved.1587return true; // Must punt the assertion to true.1588}1589Method* lm = ctxk->lookup_method(m->name(), m->signature());1590if (lm == NULL && ctxk->is_instance_klass()) {1591// It might be an interface method1592lm = InstanceKlass::cast(ctxk)->lookup_method_in_ordered_interfaces(m->name(),1593m->signature());1594}1595if (lm == m) {1596// Method m is inherited into ctxk.1597return true;1598}1599if (lm != NULL) {1600if (!(lm->is_public() || lm->is_protected())) {1601// Method is [package-]private, so the override story is complex.1602return true; // Must punt the assertion to true.1603}1604if (lm->is_static()) {1605// Static methods don't override non-static so punt1606return true;1607}1608if (!Dependencies::is_concrete_method(lm, ctxk) &&1609!Dependencies::is_concrete_method(m, ctxk)) {1610// They are both non-concrete1611if (lm->method_holder()->is_subtype_of(m->method_holder())) {1612// Method m is overridden by lm, but both are non-concrete.1613return true;1614}1615if (lm->method_holder()->is_interface() && m->method_holder()->is_interface() &&1616ctxk->is_subtype_of(m->method_holder()) && ctxk->is_subtype_of(lm->method_holder())) {1617// Interface method defined in multiple super interfaces1618return true;1619}1620}1621}1622ResourceMark rm;1623tty->print_cr("Dependency method not found in the associated context:");1624tty->print_cr(" context = %s", ctxk->external_name());1625tty->print( " method = "); m->print_short_name(tty); tty->cr();1626if (lm != NULL) {1627tty->print( " found = "); lm->print_short_name(tty); tty->cr();1628}1629return false;1630}1631#endif // ASSERT16321633bool Dependencies::is_concrete_klass(Klass* k) {1634if (k->is_abstract()) return false;1635// %%% We could treat classes which are concrete but1636// have not yet been instantiated as virtually abstract.1637// This would require a deoptimization barrier on first instantiation.1638//if (k->is_not_instantiated()) return false;1639return true;1640}16411642bool Dependencies::is_concrete_method(Method* m, Klass* k) {1643// NULL is not a concrete method.1644if (m == NULL) {1645return false;1646}1647// Statics are irrelevant to virtual call sites.1648if (m->is_static()) {1649return false;1650}1651// Abstract methods are not concrete.1652if (m->is_abstract()) {1653return false;1654}1655// Overpass (error) methods are not concrete if k is abstract.1656if (m->is_overpass() && k != NULL) {1657return !k->is_abstract();1658}1659// Note "true" is conservative answer: overpass clause is false if k == NULL,1660// implies return true if answer depends on overpass clause.1661return true;1662}16631664Klass* Dependencies::find_finalizable_subclass(InstanceKlass* ik) {1665for (ClassHierarchyIterator iter(ik); !iter.done(); iter.next()) {1666Klass* sub = iter.klass();1667if (sub->has_finalizer() && !sub->is_interface()) {1668return sub;1669}1670}1671return NULL; // not found1672}16731674bool Dependencies::is_concrete_klass(ciInstanceKlass* k) {1675if (k->is_abstract()) return false;1676// We could also return false if k does not yet appear to be1677// instantiated, if the VM version supports this distinction also.1678//if (k->is_not_instantiated()) return false;1679return true;1680}16811682bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) {1683return k->has_finalizable_subclass();1684}16851686// Any use of the contents (bytecodes) of a method must be1687// marked by an "evol_method" dependency, if those contents1688// can change. (Note: A method is always dependent on itself.)1689Klass* Dependencies::check_evol_method(Method* m) {1690assert(must_be_in_vm(), "raw oops here");1691// Did somebody do a JVMTI RedefineClasses while our backs were turned?1692// Or is there a now a breakpoint?1693// (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.)1694if (m->is_old()1695|| m->number_of_breakpoints() > 0) {1696return m->method_holder();1697} else {1698return NULL;1699}1700}17011702// This is a strong assertion: It is that the given type1703// has no subtypes whatever. It is most useful for1704// optimizing checks on reflected types or on array types.1705// (Checks on types which are derived from real instances1706// can be optimized more strongly than this, because we1707// know that the checked type comes from a concrete type,1708// and therefore we can disregard abstract types.)1709Klass* Dependencies::check_leaf_type(InstanceKlass* ctxk) {1710assert(must_be_in_vm(), "raw oops here");1711assert_locked_or_safepoint(Compile_lock);1712Klass* sub = ctxk->subklass();1713if (sub != NULL) {1714return sub;1715} else if (ctxk->nof_implementors() != 0) {1716// if it is an interface, it must be unimplemented1717// (if it is not an interface, nof_implementors is always zero)1718InstanceKlass* impl = ctxk->implementor();1719assert(impl != NULL, "must be set");1720return impl;1721} else {1722return NULL;1723}1724}17251726// Test the assertion that conck is the only concrete subtype* of ctxk.1727// The type conck itself is allowed to have have further concrete subtypes.1728// This allows the compiler to narrow occurrences of ctxk by conck,1729// when dealing with the types of actual instances.1730Klass* Dependencies::check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk,1731Klass* conck,1732NewKlassDepChange* changes) {1733ConcreteSubtypeFinder wf(conck);1734Klass* k = wf.find_witness(ctxk, changes);1735return k;1736}173717381739// Find the unique concrete proper subtype of ctxk, or NULL if there1740// is more than one concrete proper subtype. If there are no concrete1741// proper subtypes, return ctxk itself, whether it is concrete or not.1742// The returned subtype is allowed to have have further concrete subtypes.1743// That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }.1744Klass* Dependencies::find_unique_concrete_subtype(InstanceKlass* ctxk) {1745ConcreteSubtypeFinder wf(ctxk); // Ignore ctxk when walking.1746wf.record_witnesses(1); // Record one other witness when walking.1747Klass* wit = wf.find_witness(ctxk);1748if (wit != NULL) return NULL; // Too many witnesses.1749Klass* conck = wf.participant(0);1750if (conck == NULL) {1751return ctxk; // Return ctxk as a flag for "no subtypes".1752} else {1753#ifndef PRODUCT1754// Make sure the dependency mechanism will pass this discovery:1755if (VerifyDependencies) {1756// Turn off dependency tracing while actually testing deps.1757FlagSetting fs(TraceDependencies, false);1758if (!Dependencies::is_concrete_klass(ctxk)) {1759guarantee(NULL == (void *)1760check_abstract_with_unique_concrete_subtype(ctxk, conck),1761"verify dep.");1762}1763}1764#endif //PRODUCT1765return conck;1766}1767}17681769// Try to determine whether root method in some context is concrete or not based on the information about the unique method1770// in that context. It exploits the fact that concrete root method is always inherited into the context when there's a unique method.1771// Hence, unique method holder is always a supertype of the context class when root method is concrete.1772// Examples for concrete_root_method1773// C (C.m uniqm)1774// |1775// CX (ctxk) uniqm is inherited into context.1776//1777// CX (ctxk) (CX.m uniqm) here uniqm is defined in ctxk.1778// Examples for !concrete_root_method1779// CX (ctxk)1780// |1781// C (C.m uniqm) uniqm is in subtype of ctxk.1782bool Dependencies::is_concrete_root_method(Method* uniqm, InstanceKlass* ctxk) {1783if (uniqm == NULL) {1784return false; // match Dependencies::is_concrete_method() behavior1785}1786// Theoretically, the "direction" of subtype check matters here.1787// On one hand, in case of interface context with a single implementor, uniqm can be in a superclass of the implementor which1788// is not related to context class.1789// On another hand, uniqm could come from an interface unrelated to the context class, but right now it is not possible:1790// it is required that uniqm->method_holder() is the participant (uniqm->method_holder() <: ctxk), hence a default method1791// can't be used as unique.1792if (ctxk->is_interface()) {1793InstanceKlass* implementor = ctxk->implementor();1794assert(implementor != ctxk, "single implementor only"); // should have been invalidated earlier1795ctxk = implementor;1796}1797InstanceKlass* holder = uniqm->method_holder();1798assert(!holder->is_interface(), "no default methods allowed");1799assert(ctxk->is_subclass_of(holder) || holder->is_subclass_of(ctxk), "not related");1800return ctxk->is_subclass_of(holder);1801}18021803// If a class (or interface) has a unique concrete method uniqm, return NULL.1804// Otherwise, return a class that contains an interfering method.1805Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk,1806Method* uniqm,1807NewKlassDepChange* changes) {1808ConcreteMethodFinder wf(uniqm, uniqm->method_holder());1809Klass* k = wf.find_witness(ctxk, changes);1810if (k != NULL) {1811return k;1812}1813if (!Dependencies::is_concrete_root_method(uniqm, ctxk) || changes != NULL) {1814Klass* conck = find_witness_AME(ctxk, uniqm, changes);1815if (conck != NULL) {1816// Found a concrete subtype 'conck' which does not override abstract root method.1817return conck;1818}1819}1820return NULL;1821}18221823Klass* Dependencies::check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes) {1824assert(ctxk->is_interface(), "sanity");1825assert(ctxk->nof_implementors() > 0, "no implementors");1826if (ctxk->nof_implementors() == 1) {1827assert(ctxk->implementor() == uniqk, "sanity");1828return NULL;1829}1830return ctxk; // no unique implementor1831}18321833// Search for AME.1834// There are two version of checks.1835// 1) Spot checking version(Classload time). Newly added class is checked for AME.1836// Checks whether abstract/overpass method is inherited into/declared in newly added concrete class.1837// 2) Compile time analysis for abstract/overpass(abstract klass) root_m. The non uniqm subtrees are checked for concrete classes.1838Klass* Dependencies::find_witness_AME(InstanceKlass* ctxk, Method* m, KlassDepChange* changes) {1839if (m != NULL) {1840if (changes != NULL) {1841// Spot checking version.1842ConcreteMethodFinder wf(m);1843Klass* new_type = changes->as_new_klass_change()->new_type();1844if (wf.witnessed_reabstraction_in_supers(new_type)) {1845return new_type;1846}1847} else {1848// Note: It is required that uniqm->method_holder() is the participant (see ClassHierarchyWalker::found_method()).1849ConcreteSubtypeFinder wf(m->method_holder());1850Klass* conck = wf.find_witness(ctxk);1851if (conck != NULL) {1852Method* cm = InstanceKlass::cast(conck)->find_instance_method(m->name(), m->signature(), Klass::PrivateLookupMode::skip);1853if (!Dependencies::is_concrete_method(cm, conck)) {1854return conck;1855}1856}1857}1858}1859return NULL;1860}18611862// This function is used by find_unique_concrete_method(non vtable based)1863// to check whether subtype method overrides the base method.1864static bool overrides(Method* sub_m, Method* base_m) {1865assert(base_m != NULL, "base method should be non null");1866if (sub_m == NULL) {1867return false;1868}1869/**1870* If base_m is public or protected then sub_m always overrides.1871* If base_m is !public, !protected and !private (i.e. base_m is package private)1872* then sub_m should be in the same package as that of base_m.1873* For package private base_m this is conservative approach as it allows only subset of all allowed cases in1874* the jvm specification.1875**/1876if (base_m->is_public() || base_m->is_protected() ||1877base_m->method_holder()->is_same_class_package(sub_m->method_holder())) {1878return true;1879}1880return false;1881}18821883// Find the set of all non-abstract methods under ctxk that match m.1884// (The method m must be defined or inherited in ctxk.)1885// Include m itself in the set, unless it is abstract.1886// If this set has exactly one element, return that element.1887Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass** participant) {1888// Return NULL if m is marked old; must have been a redefined method.1889if (m->is_old()) {1890return NULL;1891}1892if (m->is_default_method()) {1893return NULL; // not supported1894}1895assert(verify_method_context(ctxk, m), "proper context");1896ConcreteMethodFinder wf(m);1897wf.record_witnesses(1);1898Klass* wit = wf.find_witness(ctxk);1899if (wit != NULL) return NULL; // Too many witnesses.1900Method* fm = wf.found_method(0); // Will be NULL if num_parts == 0.1901if (participant != NULL) {1902(*participant) = wf.participant(0);1903}1904if (!Dependencies::is_concrete_method(fm, NULL)) {1905fm = NULL; // ignore abstract methods1906}1907if (Dependencies::is_concrete_method(m, ctxk)) {1908if (fm == NULL) {1909// It turns out that m was always the only implementation.1910fm = m;1911} else if (fm != m) {1912// Two conflicting implementations after all.1913// (This can happen if m is inherited into ctxk and fm overrides it.)1914return NULL;1915}1916} else if (Dependencies::find_witness_AME(ctxk, fm) != NULL) {1917// Found a concrete subtype which does not override abstract root method.1918return NULL;1919} else if (!overrides(fm, m)) {1920// Found method doesn't override abstract root method.1921return NULL;1922}1923assert(Dependencies::is_concrete_root_method(fm, ctxk) == Dependencies::is_concrete_method(m, ctxk), "mismatch");1924#ifndef PRODUCT1925// Make sure the dependency mechanism will pass this discovery:1926if (VerifyDependencies && fm != NULL) {1927guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm),1928"verify dep.");1929}1930#endif //PRODUCT1931return fm;1932}19331934// If a class (or interface) has a unique concrete method uniqm, return NULL.1935// Otherwise, return a class that contains an interfering method.1936Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk,1937Method* uniqm,1938Klass* resolved_klass,1939Method* resolved_method,1940KlassDepChange* changes) {1941assert(UseVtableBasedCHA, "required");1942assert(!ctxk->is_interface() || ctxk == resolved_klass, "sanity");1943assert(!resolved_method->can_be_statically_bound() || resolved_method == uniqm, "sanity");1944assert(resolved_klass->is_subtype_of(resolved_method->method_holder()), "sanity");19451946if (!InstanceKlass::cast(resolved_klass)->is_linked() ||1947!resolved_method->method_holder()->is_linked() ||1948resolved_method->can_be_statically_bound()) {1949// Dependency is redundant, but benign. Just keep it to avoid unnecessary recompilation.1950return NULL; // no vtable index available1951}19521953LinkedConcreteMethodFinder mf(InstanceKlass::cast(resolved_klass), resolved_method, uniqm);1954return mf.find_witness(ctxk, changes);1955}19561957// Find the set of all non-abstract methods under ctxk that match m.1958// (The method m must be defined or inherited in ctxk.)1959// Include m itself in the set, unless it is abstract.1960// If this set has exactly one element, return that element.1961// Not yet linked subclasses of ctxk are ignored since they don't have any instances yet.1962// Additionally, resolved_klass and resolved_method complete the description of the call site being analyzed.1963Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method) {1964// Return NULL if m is marked old; must have been a redefined method.1965if (m->is_old()) {1966return NULL;1967}1968if (!InstanceKlass::cast(resolved_klass)->is_linked() ||1969!resolved_method->method_holder()->is_linked() ||1970resolved_method->can_be_statically_bound()) {1971return m; // nothing to do: no witness under ctxk1972}1973LinkedConcreteMethodFinder wf(InstanceKlass::cast(resolved_klass), resolved_method);1974assert(Dependencies::verify_method_context(ctxk, m), "proper context");1975wf.record_witnesses(1);1976Klass* wit = wf.find_witness(ctxk);1977if (wit != NULL) {1978return NULL; // Too many witnesses.1979}1980// p == NULL when no participants are found (wf.num_participants() == 0).1981// fm == NULL case has 2 meanings:1982// * when p == NULL: no method found;1983// * when p != NULL: AbstractMethodError-throwing method found.1984// Also, found method should always be accompanied by a participant class.1985Klass* p = wf.participant(0);1986Method* fm = wf.found_method(0);1987assert(fm == NULL || p != NULL, "no participant");1988// Normalize all error-throwing cases to NULL.1989if (fm == Universe::throw_illegal_access_error() ||1990fm == Universe::throw_no_such_method_error() ||1991!Dependencies::is_concrete_method(fm, p)) {1992fm = NULL; // error-throwing method1993}1994if (Dependencies::is_concrete_method(m, ctxk)) {1995if (p == NULL) {1996// It turns out that m was always the only implementation.1997assert(fm == NULL, "sanity");1998fm = m;1999}2000}2001#ifndef PRODUCT2002// Make sure the dependency mechanism will pass this discovery:2003if (VerifyDependencies && fm != NULL) {2004guarantee(NULL == check_unique_concrete_method(ctxk, fm, resolved_klass, resolved_method),2005"verify dep.");2006}2007#endif // PRODUCT2008assert(fm == NULL || !fm->is_abstract(), "sanity");2009// Old CHA conservatively reports concrete methods in abstract classes2010// irrespective of whether they have concrete subclasses or not.2011// Also, abstract root method case is not fully supported.2012#ifdef ASSERT2013Klass* uniqp = NULL;2014Method* uniqm = Dependencies::find_unique_concrete_method(ctxk, m, &uniqp);2015assert(uniqm == NULL || uniqm == fm ||2016m->is_abstract() ||2017uniqm->method_holder()->is_abstract() ||2018(fm == NULL && uniqm != NULL && uniqp != NULL && !InstanceKlass::cast(uniqp)->is_linked()),2019"sanity");2020#endif // ASSERT2021return fm;2022}20232024Klass* Dependencies::check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes) {2025InstanceKlass* search_at = ctxk;2026if (changes != NULL) {2027search_at = changes->new_type(); // just look at the new bit2028}2029return find_finalizable_subclass(search_at);2030}20312032Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {2033assert(call_site != NULL, "sanity");2034assert(method_handle != NULL, "sanity");2035assert(call_site->is_a(vmClasses::CallSite_klass()), "sanity");20362037if (changes == NULL) {2038// Validate all CallSites2039if (java_lang_invoke_CallSite::target(call_site) != method_handle)2040return call_site->klass(); // assertion failed2041} else {2042// Validate the given CallSite2043if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {2044assert(method_handle != changes->method_handle(), "must be");2045return call_site->klass(); // assertion failed2046}2047}2048return NULL; // assertion still valid2049}20502051void Dependencies::DepStream::trace_and_log_witness(Klass* witness) {2052if (witness != NULL) {2053if (TraceDependencies) {2054print_dependency(witness, /*verbose=*/ true);2055}2056// The following is a no-op unless logging is enabled:2057log_dependency(witness);2058}2059}20602061Klass* Dependencies::DepStream::check_new_klass_dependency(NewKlassDepChange* changes) {2062assert_locked_or_safepoint(Compile_lock);2063Dependencies::check_valid_dependency_type(type());20642065Klass* witness = NULL;2066switch (type()) {2067case evol_method:2068witness = check_evol_method(method_argument(0));2069break;2070case leaf_type:2071witness = check_leaf_type(context_type());2072break;2073case abstract_with_unique_concrete_subtype:2074witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes);2075break;2076case unique_concrete_method_2:2077witness = check_unique_concrete_method(context_type(), method_argument(1), changes);2078break;2079case unique_concrete_method_4:2080witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes);2081break;2082case unique_implementor:2083witness = check_unique_implementor(context_type(), type_argument(1), changes);2084break;2085case no_finalizable_subclasses:2086witness = check_has_no_finalizable_subclasses(context_type(), changes);2087break;2088default:2089witness = NULL;2090break;2091}2092trace_and_log_witness(witness);2093return witness;2094}20952096Klass* Dependencies::DepStream::check_klass_init_dependency(KlassInitDepChange* changes) {2097assert_locked_or_safepoint(Compile_lock);2098Dependencies::check_valid_dependency_type(type());20992100// No new types added. Only unique_concrete_method_4 is sensitive to class initialization changes.2101Klass* witness = NULL;2102switch (type()) {2103case unique_concrete_method_4:2104witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes);2105break;2106default:2107witness = NULL;2108break;2109}2110trace_and_log_witness(witness);2111return witness;2112}21132114Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {2115assert_locked_or_safepoint(Compile_lock);2116Dependencies::check_valid_dependency_type(type());21172118if (changes != NULL) {2119if (UseVtableBasedCHA && changes->is_klass_init_change()) {2120return check_klass_init_dependency(changes->as_klass_init_change());2121} else {2122return check_new_klass_dependency(changes->as_new_klass_change());2123}2124} else {2125Klass* witness = check_new_klass_dependency(NULL);2126// check_klass_init_dependency duplicates check_new_klass_dependency checks when class hierarchy change info is absent.2127assert(witness != NULL || check_klass_init_dependency(NULL) == NULL, "missed dependency");2128return witness;2129}2130}21312132Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) {2133assert_locked_or_safepoint(Compile_lock);2134Dependencies::check_valid_dependency_type(type());21352136Klass* witness = NULL;2137switch (type()) {2138case call_site_target_value:2139witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes);2140break;2141default:2142witness = NULL;2143break;2144}2145trace_and_log_witness(witness);2146return witness;2147}214821492150Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {2151// Handle klass dependency2152if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))2153return check_klass_dependency(changes.as_klass_change());21542155// Handle CallSite dependency2156if (changes.is_call_site_change())2157return check_call_site_dependency(changes.as_call_site_change());21582159// irrelevant dependency; skip it2160return NULL;2161}216221632164void DepChange::print() {2165int nsup = 0, nint = 0;2166for (ContextStream str(*this); str.next(); ) {2167Klass* k = str.klass();2168switch (str.change_type()) {2169case Change_new_type:2170tty->print_cr(" dependee = %s", k->external_name());2171break;2172case Change_new_sub:2173if (!WizardMode) {2174++nsup;2175} else {2176tty->print_cr(" context super = %s", k->external_name());2177}2178break;2179case Change_new_impl:2180if (!WizardMode) {2181++nint;2182} else {2183tty->print_cr(" context interface = %s", k->external_name());2184}2185break;2186default:2187break;2188}2189}2190if (nsup + nint != 0) {2191tty->print_cr(" context supers = %d, interfaces = %d", nsup, nint);2192}2193}21942195void DepChange::ContextStream::start() {2196Klass* type = (_changes.is_klass_change() ? _changes.as_klass_change()->type() : (Klass*) NULL);2197_change_type = (type == NULL ? NO_CHANGE : Start_Klass);2198_klass = type;2199_ti_base = NULL;2200_ti_index = 0;2201_ti_limit = 0;2202}22032204bool DepChange::ContextStream::next() {2205switch (_change_type) {2206case Start_Klass: // initial state; _klass is the new type2207_ti_base = InstanceKlass::cast(_klass)->transitive_interfaces();2208_ti_index = 0;2209_change_type = Change_new_type;2210return true;2211case Change_new_type:2212// fall through:2213_change_type = Change_new_sub;2214case Change_new_sub:2215// 6598190: brackets workaround Sun Studio C++ compiler bug 66292772216{2217_klass = _klass->super();2218if (_klass != NULL) {2219return true;2220}2221}2222// else set up _ti_limit and fall through:2223_ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length();2224_change_type = Change_new_impl;2225case Change_new_impl:2226if (_ti_index < _ti_limit) {2227_klass = _ti_base->at(_ti_index++);2228return true;2229}2230// fall through:2231_change_type = NO_CHANGE; // iterator is exhausted2232case NO_CHANGE:2233break;2234default:2235ShouldNotReachHere();2236}2237return false;2238}22392240void KlassDepChange::initialize() {2241// entire transaction must be under this lock:2242assert_lock_strong(Compile_lock);22432244// Mark all dependee and all its superclasses2245// Mark transitive interfaces2246for (ContextStream str(*this); str.next(); ) {2247Klass* d = str.klass();2248assert(!InstanceKlass::cast(d)->is_marked_dependent(), "checking");2249InstanceKlass::cast(d)->set_is_marked_dependent(true);2250}2251}22522253KlassDepChange::~KlassDepChange() {2254// Unmark all dependee and all its superclasses2255// Unmark transitive interfaces2256for (ContextStream str(*this); str.next(); ) {2257Klass* d = str.klass();2258InstanceKlass::cast(d)->set_is_marked_dependent(false);2259}2260}22612262bool KlassDepChange::involves_context(Klass* k) {2263if (k == NULL || !k->is_instance_klass()) {2264return false;2265}2266InstanceKlass* ik = InstanceKlass::cast(k);2267bool is_contained = ik->is_marked_dependent();2268assert(is_contained == type()->is_subtype_of(k),2269"correct marking of potential context types");2270return is_contained;2271}22722273void Dependencies::print_statistics() {2274AbstractClassHierarchyWalker::print_statistics();2275}22762277void AbstractClassHierarchyWalker::print_statistics() {2278if (UsePerfData) {2279jlong deps_find_witness_calls = _perf_find_witness_anywhere_calls_count->get_value();2280jlong deps_find_witness_steps = _perf_find_witness_anywhere_steps_count->get_value();2281jlong deps_find_witness_singles = _perf_find_witness_in_calls_count->get_value();22822283ttyLocker ttyl;2284tty->print_cr("Dependency check (find_witness) "2285"calls=" JLONG_FORMAT ", steps=" JLONG_FORMAT " (avg=%.1f), singles=" JLONG_FORMAT,2286deps_find_witness_calls,2287deps_find_witness_steps,2288(double)deps_find_witness_steps / deps_find_witness_calls,2289deps_find_witness_singles);2290if (xtty != NULL) {2291xtty->elem("deps_find_witness calls='" JLONG_FORMAT "' steps='" JLONG_FORMAT "' singles='" JLONG_FORMAT "'",2292deps_find_witness_calls,2293deps_find_witness_steps,2294deps_find_witness_singles);2295}2296}2297}22982299CallSiteDepChange::CallSiteDepChange(Handle call_site, Handle method_handle) :2300_call_site(call_site),2301_method_handle(method_handle) {2302assert(_call_site()->is_a(vmClasses::CallSite_klass()), "must be");2303assert(_method_handle.is_null() || _method_handle()->is_a(vmClasses::MethodHandle_klass()), "must be");2304}23052306void dependencies_init() {2307AbstractClassHierarchyWalker::init();2308}230923102311