Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/dependencies.cpp
32285 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#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 "code/dependencies.hpp"30#include "compiler/compileLog.hpp"31#include "oops/klass.hpp"32#include "oops/oop.inline.hpp"33#include "runtime/handles.hpp"34#include "runtime/handles.inline.hpp"35#include "runtime/thread.inline.hpp"36#include "utilities/copy.hpp"373839#ifdef ASSERT40static bool must_be_in_vm() {41Thread* thread = Thread::current();42if (thread->is_Java_thread())43return ((JavaThread*)thread)->thread_state() == _thread_in_vm;44else45return true; //something like this: thread->is_VM_thread();46}47#endif //ASSERT4849void Dependencies::initialize(ciEnv* env) {50Arena* arena = env->arena();51_oop_recorder = env->oop_recorder();52_log = env->log();53_dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);54DEBUG_ONLY(_deps[end_marker] = NULL);55for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {56_deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0);57}58_content_bytes = NULL;59_size_in_bytes = (size_t)-1;6061assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");62}6364void Dependencies::assert_evol_method(ciMethod* m) {65assert_common_1(evol_method, m);66}6768void Dependencies::assert_leaf_type(ciKlass* ctxk) {69if (ctxk->is_array_klass()) {70// As a special case, support this assertion on an array type,71// which reduces to an assertion on its element type.72// Note that this cannot be done with assertions that73// relate to concreteness or abstractness.74ciType* elemt = ctxk->as_array_klass()->base_element_type();75if (!elemt->is_instance_klass()) return; // Ex: int[][]76ctxk = elemt->as_instance_klass();77//if (ctxk->is_final()) return; // Ex: String[][]78}79check_ctxk(ctxk);80assert_common_1(leaf_type, ctxk);81}8283void Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) {84check_ctxk_abstract(ctxk);85assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck);86}8788void Dependencies::assert_abstract_with_no_concrete_subtype(ciKlass* ctxk) {89check_ctxk_abstract(ctxk);90assert_common_1(abstract_with_no_concrete_subtype, ctxk);91}9293void Dependencies::assert_concrete_with_no_concrete_subtype(ciKlass* ctxk) {94check_ctxk_concrete(ctxk);95assert_common_1(concrete_with_no_concrete_subtype, ctxk);96}9798void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) {99check_ctxk(ctxk);100assert_common_2(unique_concrete_method, ctxk, uniqm);101}102103void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {104check_ctxk(ctxk);105assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);106}107108void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {109check_ctxk(ctxk);110assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);111}112113void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {114check_ctxk(ctxk);115assert_common_1(no_finalizable_subclasses, ctxk);116}117118void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {119check_ctxk(call_site->klass());120assert_common_2(call_site_target_value, call_site, method_handle);121}122123// Helper function. If we are adding a new dep. under ctxk2,124// try to find an old dep. under a broader* ctxk1. If there is125//126bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,127int ctxk_i, ciKlass* ctxk2) {128ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();129if (ctxk2->is_subtype_of(ctxk1)) {130return true; // success, and no need to change131} else if (ctxk1->is_subtype_of(ctxk2)) {132// new context class fully subsumes previous one133deps->at_put(ctxk_i, ctxk2);134return true;135} else {136return false;137}138}139140void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {141assert(dep_args(dept) == 1, "sanity");142log_dependency(dept, x);143GrowableArray<ciBaseObject*>* deps = _deps[dept];144145// see if the same (or a similar) dep is already recorded146if (note_dep_seen(dept, x)) {147assert(deps->find(x) >= 0, "sanity");148} else {149deps->append(x);150}151}152153void Dependencies::assert_common_2(DepType dept,154ciBaseObject* x0, ciBaseObject* x1) {155assert(dep_args(dept) == 2, "sanity");156log_dependency(dept, x0, x1);157GrowableArray<ciBaseObject*>* deps = _deps[dept];158159// see if the same (or a similar) dep is already recorded160bool has_ctxk = has_explicit_context_arg(dept);161if (has_ctxk) {162assert(dep_context_arg(dept) == 0, "sanity");163if (note_dep_seen(dept, x1)) {164// look in this bucket for redundant assertions165const int stride = 2;166for (int i = deps->length(); (i -= stride) >= 0; ) {167ciBaseObject* y1 = deps->at(i+1);168if (x1 == y1) { // same subject; check the context169if (maybe_merge_ctxk(deps, i+0, x0->as_metadata()->as_klass())) {170return;171}172}173}174}175} else {176assert(dep_implicit_context_arg(dept) == 0, "sanity");177if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {178// look in this bucket for redundant assertions179const int stride = 2;180for (int i = deps->length(); (i -= stride) >= 0; ) {181ciBaseObject* y0 = deps->at(i+0);182ciBaseObject* y1 = deps->at(i+1);183if (x0 == y0 && x1 == y1) {184return;185}186}187}188}189190// append the assertion in the correct bucket:191deps->append(x0);192deps->append(x1);193}194195void Dependencies::assert_common_3(DepType dept,196ciKlass* ctxk, ciBaseObject* x, ciBaseObject* x2) {197assert(dep_context_arg(dept) == 0, "sanity");198assert(dep_args(dept) == 3, "sanity");199log_dependency(dept, ctxk, x, x2);200GrowableArray<ciBaseObject*>* deps = _deps[dept];201202// try to normalize an unordered pair:203bool swap = false;204switch (dept) {205case abstract_with_exclusive_concrete_subtypes_2:206swap = (x->ident() > x2->ident() && x->as_metadata()->as_klass() != ctxk);207break;208case exclusive_concrete_methods_2:209swap = (x->ident() > x2->ident() && x->as_metadata()->as_method()->holder() != ctxk);210break;211}212if (swap) { ciBaseObject* t = x; x = x2; x2 = t; }213214// see if the same (or a similar) dep is already recorded215if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) {216// look in this bucket for redundant assertions217const int stride = 3;218for (int i = deps->length(); (i -= stride) >= 0; ) {219ciBaseObject* y = deps->at(i+1);220ciBaseObject* y2 = deps->at(i+2);221if (x == y && x2 == y2) { // same subjects; check the context222if (maybe_merge_ctxk(deps, i+0, ctxk)) {223return;224}225}226}227}228// append the assertion in the correct bucket:229deps->append(ctxk);230deps->append(x);231deps->append(x2);232}233234/// Support for encoding dependencies into an nmethod:235236void Dependencies::copy_to(nmethod* nm) {237address beg = nm->dependencies_begin();238address end = nm->dependencies_end();239guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");240Copy::disjoint_words((HeapWord*) content_bytes(),241(HeapWord*) beg,242size_in_bytes() / sizeof(HeapWord));243assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");244}245246static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) {247for (int i = 0; i < narg; i++) {248int diff = p1[i]->ident() - p2[i]->ident();249if (diff != 0) return diff;250}251return 0;252}253static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2)254{ return sort_dep(p1, p2, 1); }255static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2)256{ return sort_dep(p1, p2, 2); }257static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2)258{ return sort_dep(p1, p2, 3); }259260void Dependencies::sort_all_deps() {261for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {262DepType dept = (DepType)deptv;263GrowableArray<ciBaseObject*>* deps = _deps[dept];264if (deps->length() <= 1) continue;265switch (dep_args(dept)) {266case 1: deps->sort(sort_dep_arg_1, 1); break;267case 2: deps->sort(sort_dep_arg_2, 2); break;268case 3: deps->sort(sort_dep_arg_3, 3); break;269default: ShouldNotReachHere();270}271}272}273274size_t Dependencies::estimate_size_in_bytes() {275size_t est_size = 100;276for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {277DepType dept = (DepType)deptv;278GrowableArray<ciBaseObject*>* deps = _deps[dept];279est_size += deps->length()*2; // tags and argument(s)280}281return est_size;282}283284ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) {285switch (dept) {286case abstract_with_exclusive_concrete_subtypes_2:287return x->as_metadata()->as_klass();288case unique_concrete_method:289case exclusive_concrete_methods_2:290return x->as_metadata()->as_method()->holder();291}292return NULL; // let NULL be NULL293}294295Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) {296assert(must_be_in_vm(), "raw oops here");297switch (dept) {298case abstract_with_exclusive_concrete_subtypes_2:299assert(x->is_klass(), "sanity");300return (Klass*) x;301case unique_concrete_method:302case exclusive_concrete_methods_2:303assert(x->is_method(), "sanity");304return ((Method*)x)->method_holder();305}306return NULL; // let NULL be NULL307}308309void Dependencies::encode_content_bytes() {310sort_all_deps();311312// cast is safe, no deps can overflow INT_MAX313CompressedWriteStream bytes((int)estimate_size_in_bytes());314315for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {316DepType dept = (DepType)deptv;317GrowableArray<ciBaseObject*>* deps = _deps[dept];318if (deps->length() == 0) continue;319int stride = dep_args(dept);320int ctxkj = dep_context_arg(dept); // -1 if no context arg321assert(stride > 0, "sanity");322for (int i = 0; i < deps->length(); i += stride) {323jbyte code_byte = (jbyte)dept;324int skipj = -1;325if (ctxkj >= 0 && ctxkj+1 < stride) {326ciKlass* ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass();327ciBaseObject* x = deps->at(i+ctxkj+1); // following argument328if (ctxk == ctxk_encoded_as_null(dept, x)) {329skipj = ctxkj; // we win: maybe one less oop to keep track of330code_byte |= default_context_type_bit;331}332}333bytes.write_byte(code_byte);334for (int j = 0; j < stride; j++) {335if (j == skipj) continue;336ciBaseObject* v = deps->at(i+j);337int idx;338if (v->is_object()) {339idx = _oop_recorder->find_index(v->as_object()->constant_encoding());340} else {341ciMetadata* meta = v->as_metadata();342idx = _oop_recorder->find_index(meta->constant_encoding());343}344bytes.write_int(idx);345}346}347}348349// write a sentinel byte to mark the end350bytes.write_byte(end_marker);351352// round it out to a word boundary353while (bytes.position() % sizeof(HeapWord) != 0) {354bytes.write_byte(end_marker);355}356357// check whether the dept byte encoding really works358assert((jbyte)default_context_type_bit != 0, "byte overflow");359360_content_bytes = bytes.buffer();361_size_in_bytes = bytes.position();362}363364365const char* Dependencies::_dep_name[TYPE_LIMIT] = {366"end_marker",367"evol_method",368"leaf_type",369"abstract_with_unique_concrete_subtype",370"abstract_with_no_concrete_subtype",371"concrete_with_no_concrete_subtype",372"unique_concrete_method",373"abstract_with_exclusive_concrete_subtypes_2",374"exclusive_concrete_methods_2",375"no_finalizable_subclasses",376"call_site_target_value"377};378379int Dependencies::_dep_args[TYPE_LIMIT] = {380-1,// end_marker3811, // evol_method m3821, // leaf_type ctxk3832, // abstract_with_unique_concrete_subtype ctxk, k3841, // abstract_with_no_concrete_subtype ctxk3851, // concrete_with_no_concrete_subtype ctxk3862, // unique_concrete_method ctxk, m3873, // unique_concrete_subtypes_2 ctxk, k1, k23883, // unique_concrete_methods_2 ctxk, m1, m23891, // no_finalizable_subclasses ctxk3902 // call_site_target_value call_site, method_handle391};392393const char* Dependencies::dep_name(Dependencies::DepType dept) {394if (!dept_in_mask(dept, all_types)) return "?bad-dep?";395return _dep_name[dept];396}397398int Dependencies::dep_args(Dependencies::DepType dept) {399if (!dept_in_mask(dept, all_types)) return -1;400return _dep_args[dept];401}402403void Dependencies::check_valid_dependency_type(DepType dept) {404guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, err_msg("invalid dependency type: %d", (int) dept));405}406407// for the sake of the compiler log, print out current dependencies:408void Dependencies::log_all_dependencies() {409if (log() == NULL) return;410ResourceMark rm;411for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {412DepType dept = (DepType)deptv;413GrowableArray<ciBaseObject*>* deps = _deps[dept];414int deplen = deps->length();415if (deplen == 0) {416continue;417}418int stride = dep_args(dept);419GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(stride);420for (int i = 0; i < deps->length(); i += stride) {421for (int j = 0; j < stride; j++) {422// flush out the identities before printing423ciargs->push(deps->at(i+j));424}425write_dependency_to(log(), dept, ciargs);426ciargs->clear();427}428guarantee(deplen == deps->length(), "deps array cannot grow inside nested ResoureMark scope");429}430}431432void Dependencies::write_dependency_to(CompileLog* log,433DepType dept,434GrowableArray<DepArgument>* args,435Klass* witness) {436if (log == NULL) {437return;438}439ResourceMark rm;440ciEnv* env = ciEnv::current();441GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(args->length());442for (GrowableArrayIterator<DepArgument> it = args->begin(); it != args->end(); ++it) {443DepArgument arg = *it;444if (arg.is_oop()) {445ciargs->push(env->get_object(arg.oop_value()));446} else {447ciargs->push(env->get_metadata(arg.metadata_value()));448}449}450int argslen = ciargs->length();451Dependencies::write_dependency_to(log, dept, ciargs, witness);452guarantee(argslen == ciargs->length(), "ciargs array cannot grow inside nested ResoureMark scope");453}454455void Dependencies::write_dependency_to(CompileLog* log,456DepType dept,457GrowableArray<ciBaseObject*>* args,458Klass* witness) {459if (log == NULL) {460return;461}462ResourceMark rm;463GrowableArray<int>* argids = new GrowableArray<int>(args->length());464for (GrowableArrayIterator<ciBaseObject*> it = args->begin(); it != args->end(); ++it) {465ciBaseObject* obj = *it;466if (obj->is_object()) {467argids->push(log->identify(obj->as_object()));468} else {469argids->push(log->identify(obj->as_metadata()));470}471}472if (witness != NULL) {473log->begin_elem("dependency_failed");474} else {475log->begin_elem("dependency");476}477log->print(" type='%s'", dep_name(dept));478const int ctxkj = dep_context_arg(dept); // -1 if no context arg479if (ctxkj >= 0 && ctxkj < argids->length()) {480log->print(" ctxk='%d'", argids->at(ctxkj));481}482// write remaining arguments, if any.483for (int j = 0; j < argids->length(); j++) {484if (j == ctxkj) continue; // already logged485if (j == 1) {486log->print( " x='%d'", argids->at(j));487} else {488log->print(" x%d='%d'", j, argids->at(j));489}490}491if (witness != NULL) {492log->object("witness", witness);493log->stamp();494}495log->end_elem();496}497498void Dependencies::write_dependency_to(xmlStream* xtty,499DepType dept,500GrowableArray<DepArgument>* args,501Klass* witness) {502if (xtty == NULL) {503return;504}505ResourceMark rm;506ttyLocker ttyl;507int ctxkj = dep_context_arg(dept); // -1 if no context arg508if (witness != NULL) {509xtty->begin_elem("dependency_failed");510} else {511xtty->begin_elem("dependency");512}513xtty->print(" type='%s'", dep_name(dept));514if (ctxkj >= 0) {515xtty->object("ctxk", args->at(ctxkj).metadata_value());516}517// write remaining arguments, if any.518for (int j = 0; j < args->length(); j++) {519if (j == ctxkj) continue; // already logged520DepArgument arg = args->at(j);521if (j == 1) {522if (arg.is_oop()) {523xtty->object("x", arg.oop_value());524} else {525xtty->object("x", arg.metadata_value());526}527} else {528char xn[12]; sprintf(xn, "x%d", j);529if (arg.is_oop()) {530xtty->object(xn, arg.oop_value());531} else {532xtty->object(xn, arg.metadata_value());533}534}535}536if (witness != NULL) {537xtty->object("witness", witness);538xtty->stamp();539}540xtty->end_elem();541}542543void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args,544Klass* witness) {545ResourceMark rm;546ttyLocker ttyl; // keep the following output all in one block547tty->print_cr("%s of type %s",548(witness == NULL)? "Dependency": "Failed dependency",549dep_name(dept));550// print arguments551int ctxkj = dep_context_arg(dept); // -1 if no context arg552for (int j = 0; j < args->length(); j++) {553DepArgument arg = args->at(j);554bool put_star = false;555if (arg.is_null()) continue;556const char* what;557if (j == ctxkj) {558assert(arg.is_metadata(), "must be");559what = "context";560put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value());561} else if (arg.is_method()) {562what = "method ";563put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), NULL);564} else if (arg.is_klass()) {565what = "class ";566} else {567what = "object ";568}569tty->print(" %s = %s", what, (put_star? "*": ""));570if (arg.is_klass())571tty->print("%s", ((Klass*)arg.metadata_value())->external_name());572else if (arg.is_method())573((Method*)arg.metadata_value())->print_value();574else575ShouldNotReachHere(); // Provide impl for this type.576tty->cr();577}578if (witness != NULL) {579bool put_star = !Dependencies::is_concrete_klass(witness);580tty->print_cr(" witness = %s%s",581(put_star? "*": ""),582witness->external_name());583}584}585586void Dependencies::DepStream::log_dependency(Klass* witness) {587if (_deps == NULL && xtty == NULL) return; // fast cutout for runtime588ResourceMark rm;589const int nargs = argument_count();590GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);591for (int j = 0; j < nargs; j++) {592if (type() == call_site_target_value) {593args->push(argument_oop(j));594} else {595args->push(argument(j));596}597}598int argslen = args->length();599if (_deps != NULL && _deps->log() != NULL) {600Dependencies::write_dependency_to(_deps->log(), type(), args, witness);601} else {602Dependencies::write_dependency_to(xtty, type(), args, witness);603}604guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");605}606607void Dependencies::DepStream::print_dependency(Klass* witness, bool verbose) {608ResourceMark rm;609int nargs = argument_count();610GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);611for (int j = 0; j < nargs; j++) {612args->push(argument(j));613}614int argslen = args->length();615Dependencies::print_dependency(type(), args, witness);616if (verbose) {617if (_code != NULL) {618tty->print(" code: ");619_code->print_value_on(tty);620tty->cr();621}622}623guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");624}625626627/// Dependency stream support (decodes dependencies from an nmethod):628629#ifdef ASSERT630void Dependencies::DepStream::initial_asserts(size_t byte_limit) {631assert(must_be_in_vm(), "raw oops here");632_byte_limit = byte_limit;633_type = (DepType)(end_marker-1); // defeat "already at end" assert634assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");635}636#endif //ASSERT637638bool Dependencies::DepStream::next() {639assert(_type != end_marker, "already at end");640if (_bytes.position() == 0 && _code != NULL641&& _code->dependencies_size() == 0) {642// Method has no dependencies at all.643return false;644}645int code_byte = (_bytes.read_byte() & 0xFF);646if (code_byte == end_marker) {647DEBUG_ONLY(_type = end_marker);648return false;649} else {650int ctxk_bit = (code_byte & Dependencies::default_context_type_bit);651code_byte -= ctxk_bit;652DepType dept = (DepType)code_byte;653_type = dept;654Dependencies::check_valid_dependency_type(dept);655int stride = _dep_args[dept];656assert(stride == dep_args(dept), "sanity");657int skipj = -1;658if (ctxk_bit != 0) {659skipj = 0; // currently the only context argument is at zero660assert(skipj == dep_context_arg(dept), "zero arg always ctxk");661}662for (int j = 0; j < stride; j++) {663_xi[j] = (j == skipj)? 0: _bytes.read_int();664}665DEBUG_ONLY(_xi[stride] = -1); // help detect overruns666return true;667}668}669670inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) {671Metadata* o = NULL;672if (_code != NULL) {673o = _code->metadata_at(i);674} else {675o = _deps->oop_recorder()->metadata_at(i);676}677return o;678}679680inline oop Dependencies::DepStream::recorded_oop_at(int i) {681return (_code != NULL)682? _code->oop_at(i)683: JNIHandles::resolve(_deps->oop_recorder()->oop_at(i));684}685686Metadata* Dependencies::DepStream::argument(int i) {687Metadata* result = recorded_metadata_at(argument_index(i));688689if (result == NULL) { // Explicit context argument can be compressed690int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg691if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) {692result = ctxk_encoded_as_null(type(), argument(ctxkj+1));693}694}695696assert(result == NULL || result->is_klass() || result->is_method(), "must be");697return result;698}699700oop Dependencies::DepStream::argument_oop(int i) {701oop result = recorded_oop_at(argument_index(i));702assert(result == NULL || result->is_oop(), "must be");703return result;704}705706Klass* Dependencies::DepStream::context_type() {707assert(must_be_in_vm(), "raw oops here");708709// Most dependencies have an explicit context type argument.710{711int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg712if (ctxkj >= 0) {713Metadata* k = argument(ctxkj);714assert(k != NULL && k->is_klass(), "type check");715return (Klass*)k;716}717}718719// Some dependencies are using the klass of the first object720// argument as implicit context type (e.g. call_site_target_value).721{722int ctxkj = dep_implicit_context_arg(type());723if (ctxkj >= 0) {724Klass* k = argument_oop(ctxkj)->klass();725assert(k != NULL && k->is_klass(), "type check");726return (Klass*) k;727}728}729730// And some dependencies don't have a context type at all,731// e.g. evol_method.732return NULL;733}734735/// Checking dependencies:736737// This hierarchy walker inspects subtypes of a given type,738// trying to find a "bad" class which breaks a dependency.739// Such a class is called a "witness" to the broken dependency.740// While searching around, we ignore "participants", which741// are already known to the dependency.742class ClassHierarchyWalker {743public:744enum { PARTICIPANT_LIMIT = 3 };745746private:747// optional method descriptor to check for:748Symbol* _name;749Symbol* _signature;750751// special classes which are not allowed to be witnesses:752Klass* _participants[PARTICIPANT_LIMIT+1];753int _num_participants;754755// cache of method lookups756Method* _found_methods[PARTICIPANT_LIMIT+1];757758// if non-zero, tells how many witnesses to convert to participants759int _record_witnesses;760761void initialize(Klass* participant) {762_record_witnesses = 0;763_participants[0] = participant;764_found_methods[0] = NULL;765_num_participants = 0;766if (participant != NULL) {767// Terminating NULL.768_participants[1] = NULL;769_found_methods[1] = NULL;770_num_participants = 1;771}772}773774void initialize_from_method(Method* m) {775assert(m != NULL && m->is_method(), "sanity");776_name = m->name();777_signature = m->signature();778}779780public:781// The walker is initialized to recognize certain methods and/or types782// as friendly participants.783ClassHierarchyWalker(Klass* participant, Method* m) {784initialize_from_method(m);785initialize(participant);786}787ClassHierarchyWalker(Method* m) {788initialize_from_method(m);789initialize(NULL);790}791ClassHierarchyWalker(Klass* participant = NULL) {792_name = NULL;793_signature = NULL;794initialize(participant);795}796ClassHierarchyWalker(Klass* participants[], int num_participants) {797_name = NULL;798_signature = NULL;799initialize(NULL);800for (int i = 0; i < num_participants; ++i) {801add_participant(participants[i]);802}803}804805// This is common code for two searches: One for concrete subtypes,806// the other for concrete method implementations and overrides.807bool doing_subtype_search() {808return _name == NULL;809}810811int num_participants() { return _num_participants; }812Klass* participant(int n) {813assert((uint)n <= (uint)_num_participants, "oob");814return _participants[n];815}816817// Note: If n==num_participants, returns NULL.818Method* found_method(int n) {819assert((uint)n <= (uint)_num_participants, "oob");820Method* fm = _found_methods[n];821assert(n == _num_participants || fm != NULL, "proper usage");822if (fm != NULL && fm->method_holder() != _participants[n]) {823// Default methods from interfaces can be added to classes. In824// that case the holder of the method is not the class but the825// interface where it's defined.826assert(fm->is_default_method(), "sanity");827return NULL;828}829return fm;830}831832#ifdef ASSERT833// Assert that m is inherited into ctxk, without intervening overrides.834// (May return true even if this is not true, in corner cases where we punt.)835bool check_method_context(Klass* ctxk, Method* m) {836if (m->method_holder() == ctxk)837return true; // Quick win.838if (m->is_private())839return false; // Quick lose. Should not happen.840if (!(m->is_public() || m->is_protected()))841// The override story is complex when packages get involved.842return true; // Must punt the assertion to true.843Klass* k = ctxk;844Method* lm = k->lookup_method(m->name(), m->signature());845if (lm == NULL && k->oop_is_instance()) {846// It might be an interface method847lm = ((InstanceKlass*)k)->lookup_method_in_ordered_interfaces(m->name(),848m->signature());849}850if (lm == m)851// Method m is inherited into ctxk.852return true;853if (lm != NULL) {854if (!(lm->is_public() || lm->is_protected())) {855// Method is [package-]private, so the override story is complex.856return true; // Must punt the assertion to true.857}858if (lm->is_static()) {859// Static methods don't override non-static so punt860return true;861}862if ( !Dependencies::is_concrete_method(lm, k)863&& !Dependencies::is_concrete_method(m, ctxk)864&& lm->method_holder()->is_subtype_of(m->method_holder()))865// Method m is overridden by lm, but both are non-concrete.866return true;867}868ResourceMark rm;869tty->print_cr("Dependency method not found in the associated context:");870tty->print_cr(" context = %s", ctxk->external_name());871tty->print( " method = "); m->print_short_name(tty); tty->cr();872if (lm != NULL) {873tty->print( " found = "); lm->print_short_name(tty); tty->cr();874}875return false;876}877#endif878879void add_participant(Klass* participant) {880assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob");881int np = _num_participants++;882_participants[np] = participant;883_participants[np+1] = NULL;884_found_methods[np+1] = NULL;885}886887void record_witnesses(int add) {888if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT;889assert(_num_participants + add < PARTICIPANT_LIMIT, "oob");890_record_witnesses = add;891}892893bool is_witness(Klass* k) {894if (doing_subtype_search()) {895return Dependencies::is_concrete_klass(k);896} else if (!k->oop_is_instance()) {897return false; // no methods to find in an array type898} else {899// Search class hierarchy first, skipping private implementations900// as they never override any inherited methods901Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::skip_private);902if (!Dependencies::is_concrete_method(m, k)) {903// Check for re-abstraction of method904if (!k->is_interface() && m != NULL && m->is_abstract()) {905// Found a matching abstract method 'm' in the class hierarchy.906// This is fine iff 'k' is an abstract class and all concrete subtypes907// of 'k' override 'm' and are participates of the current search.908ClassHierarchyWalker wf(_participants, _num_participants);909Klass* w = wf.find_witness_subtype(k);910if (w != NULL) {911Method* wm = InstanceKlass::cast(w)->find_instance_method(_name, _signature, Klass::skip_private);912if (!Dependencies::is_concrete_method(wm, w)) {913// Found a concrete subtype 'w' which does not override abstract method 'm'.914// Bail out because 'm' could be called with 'w' as receiver (leading to an915// AbstractMethodError) and thus the method we are looking for is not unique.916_found_methods[_num_participants] = m;917return true;918}919}920}921// Check interface defaults also, if any exist.922Array<Method*>* default_methods = InstanceKlass::cast(k)->default_methods();923if (default_methods == NULL)924return false;925m = InstanceKlass::cast(k)->find_method(default_methods, _name, _signature);926if (!Dependencies::is_concrete_method(m, NULL))927return false;928}929_found_methods[_num_participants] = m;930// Note: If add_participant(k) is called,931// the method m will already be memoized for it.932return true;933}934}935936bool is_participant(Klass* k) {937if (k == _participants[0]) {938return true;939} else if (_num_participants <= 1) {940return false;941} else {942return in_list(k, &_participants[1]);943}944}945bool ignore_witness(Klass* witness) {946if (_record_witnesses == 0) {947return false;948} else {949--_record_witnesses;950add_participant(witness);951return true;952}953}954static bool in_list(Klass* x, Klass** list) {955for (int i = 0; ; i++) {956Klass* y = list[i];957if (y == NULL) break;958if (y == x) return true;959}960return false; // not in list961}962963private:964// the actual search method:965Klass* find_witness_anywhere(Klass* context_type,966bool participants_hide_witnesses,967bool top_level_call = true);968// the spot-checking version:969Klass* find_witness_in(KlassDepChange& changes,970Klass* context_type,971bool participants_hide_witnesses);972bool witnessed_reabstraction_in_supers(Klass* k);973public:974Klass* find_witness_subtype(Klass* context_type, KlassDepChange* changes = NULL) {975assert(doing_subtype_search(), "must set up a subtype search");976// When looking for unexpected concrete types,977// do not look beneath expected ones.978const bool participants_hide_witnesses = true;979// CX > CC > C' is OK, even if C' is new.980// CX > { CC, C' } is not OK if C' is new, and C' is the witness.981if (changes != NULL) {982return find_witness_in(*changes, context_type, participants_hide_witnesses);983} else {984return find_witness_anywhere(context_type, participants_hide_witnesses);985}986}987Klass* find_witness_definer(Klass* context_type, KlassDepChange* changes = NULL) {988assert(!doing_subtype_search(), "must set up a method definer search");989// When looking for unexpected concrete methods,990// look beneath expected ones, to see if there are overrides.991const bool participants_hide_witnesses = true;992// CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness.993if (changes != NULL) {994return find_witness_in(*changes, context_type, !participants_hide_witnesses);995} else {996return find_witness_anywhere(context_type, !participants_hide_witnesses);997}998}999};10001001#ifndef PRODUCT1002static int deps_find_witness_calls = 0;1003static int deps_find_witness_steps = 0;1004static int deps_find_witness_recursions = 0;1005static int deps_find_witness_singles = 0;1006static int deps_find_witness_print = 0; // set to -1 to force a final print1007static bool count_find_witness_calls() {1008if (TraceDependencies || LogCompilation) {1009int pcount = deps_find_witness_print + 1;1010bool final_stats = (pcount == 0);1011bool initial_call = (pcount == 1);1012bool occasional_print = ((pcount & ((1<<10) - 1)) == 0);1013if (pcount < 0) pcount = 1; // crude overflow protection1014deps_find_witness_print = pcount;1015if (VerifyDependencies && initial_call) {1016tty->print_cr("Warning: TraceDependencies results may be inflated by VerifyDependencies");1017}1018if (occasional_print || final_stats) {1019// Every now and then dump a little info about dependency searching.1020if (xtty != NULL) {1021ttyLocker ttyl;1022xtty->elem("deps_find_witness calls='%d' steps='%d' recursions='%d' singles='%d'",1023deps_find_witness_calls,1024deps_find_witness_steps,1025deps_find_witness_recursions,1026deps_find_witness_singles);1027}1028if (final_stats || (TraceDependencies && WizardMode)) {1029ttyLocker ttyl;1030tty->print_cr("Dependency check (find_witness) "1031"calls=%d, steps=%d (avg=%.1f), recursions=%d, singles=%d",1032deps_find_witness_calls,1033deps_find_witness_steps,1034(double)deps_find_witness_steps / deps_find_witness_calls,1035deps_find_witness_recursions,1036deps_find_witness_singles);1037}1038}1039return true;1040}1041return false;1042}1043#else1044#define count_find_witness_calls() (0)1045#endif //PRODUCT10461047Klass* ClassHierarchyWalker::find_witness_in(KlassDepChange& changes,1048Klass* context_type,1049bool participants_hide_witnesses) {1050assert(changes.involves_context(context_type), "irrelevant dependency");1051Klass* new_type = changes.new_type();10521053(void)count_find_witness_calls();1054NOT_PRODUCT(deps_find_witness_singles++);10551056// Current thread must be in VM (not native mode, as in CI):1057assert(must_be_in_vm(), "raw oops here");1058// Must not move the class hierarchy during this check:1059assert_locked_or_safepoint(Compile_lock);10601061int nof_impls = InstanceKlass::cast(context_type)->nof_implementors();1062if (nof_impls > 1) {1063// Avoid this case: *I.m > { A.m, C }; B.m > C1064// %%% Until this is fixed more systematically, bail out.1065// See corresponding comment in find_witness_anywhere.1066return context_type;1067}10681069assert(!is_participant(new_type), "only old classes are participants");1070if (participants_hide_witnesses) {1071// If the new type is a subtype of a participant, we are done.1072for (int i = 0; i < num_participants(); i++) {1073Klass* part = participant(i);1074if (part == NULL) continue;1075assert(changes.involves_context(part) == new_type->is_subtype_of(part),1076"correct marking of participants, b/c new_type is unique");1077if (changes.involves_context(part)) {1078// new guy is protected from this check by previous participant1079return NULL;1080}1081}1082}10831084if (is_witness(new_type)) {1085if (!ignore_witness(new_type)) {1086return new_type;1087}1088} else if (!doing_subtype_search()) {1089// No witness found, but is_witness() doesn't detect method re-abstraction in case of spot-checking.1090if (witnessed_reabstraction_in_supers(new_type)) {1091return new_type;1092}1093}10941095return NULL;1096}10971098// Walk hierarchy under a context type, looking for unexpected types.1099// Do not report participant types, and recursively walk beneath1100// them only if participants_hide_witnesses is false.1101// If top_level_call is false, skip testing the context type,1102// because the caller has already considered it.1103Klass* ClassHierarchyWalker::find_witness_anywhere(Klass* context_type,1104bool participants_hide_witnesses,1105bool top_level_call) {1106// Current thread must be in VM (not native mode, as in CI):1107assert(must_be_in_vm(), "raw oops here");1108// Must not move the class hierarchy during this check:1109assert_locked_or_safepoint(Compile_lock);11101111bool do_counts = count_find_witness_calls();11121113// Check the root of the sub-hierarchy first.1114if (top_level_call) {1115if (do_counts) {1116NOT_PRODUCT(deps_find_witness_calls++);1117NOT_PRODUCT(deps_find_witness_steps++);1118}1119if (is_participant(context_type)) {1120if (participants_hide_witnesses) return NULL;1121// else fall through to search loop...1122} else if (is_witness(context_type) && !ignore_witness(context_type)) {1123// The context is an abstract class or interface, to start with.1124return context_type;1125}1126}11271128// Now we must check each implementor and each subclass.1129// Use a short worklist to avoid blowing the stack.1130// Each worklist entry is a *chain* of subklass siblings to process.1131const int CHAINMAX = 100; // >= 1 + InstanceKlass::implementors_limit1132Klass* chains[CHAINMAX];1133int chaini = 0; // index into worklist1134Klass* chain; // scratch variable1135#define ADD_SUBCLASS_CHAIN(k) { \1136assert(chaini < CHAINMAX, "oob"); \1137chain = k->subklass(); \1138if (chain != NULL) chains[chaini++] = chain; }11391140// Look for non-abstract subclasses.1141// (Note: Interfaces do not have subclasses.)1142ADD_SUBCLASS_CHAIN(context_type);11431144// If it is an interface, search its direct implementors.1145// (Their subclasses are additional indirect implementors.1146// See InstanceKlass::add_implementor.)1147// (Note: nof_implementors is always zero for non-interfaces.)1148if (top_level_call) {1149int nof_impls = InstanceKlass::cast(context_type)->nof_implementors();1150if (nof_impls > 1) {1151// Avoid this case: *I.m > { A.m, C }; B.m > C1152// Here, I.m has 2 concrete implementations, but m appears unique1153// as A.m, because the search misses B.m when checking C.1154// The inherited method B.m was getting missed by the walker1155// when interface 'I' was the starting point.1156// %%% Until this is fixed more systematically, bail out.1157// (Old CHA had the same limitation.)1158return context_type;1159}1160if (nof_impls > 0) {1161Klass* impl = InstanceKlass::cast(context_type)->implementor();1162assert(impl != NULL, "just checking");1163// If impl is the same as the context_type, then more than one1164// implementor has seen. No exact info in this case.1165if (impl == context_type) {1166return context_type; // report an inexact witness to this sad affair1167}1168if (do_counts)1169{ NOT_PRODUCT(deps_find_witness_steps++); }1170if (is_participant(impl)) {1171if (!participants_hide_witnesses) {1172ADD_SUBCLASS_CHAIN(impl);1173}1174} else if (is_witness(impl) && !ignore_witness(impl)) {1175return impl;1176} else {1177ADD_SUBCLASS_CHAIN(impl);1178}1179}1180}11811182// Recursively process each non-trivial sibling chain.1183while (chaini > 0) {1184Klass* chain = chains[--chaini];1185for (Klass* sub = chain; sub != NULL; sub = sub->next_sibling()) {1186if (do_counts) { NOT_PRODUCT(deps_find_witness_steps++); }1187if (is_participant(sub)) {1188if (participants_hide_witnesses) continue;1189// else fall through to process this guy's subclasses1190} else if (is_witness(sub) && !ignore_witness(sub)) {1191return sub;1192}1193if (chaini < (VerifyDependencies? 2: CHAINMAX)) {1194// Fast path. (Partially disabled if VerifyDependencies.)1195ADD_SUBCLASS_CHAIN(sub);1196} else {1197// Worklist overflow. Do a recursive call. Should be rare.1198// The recursive call will have its own worklist, of course.1199// (Note that sub has already been tested, so that there is1200// no need for the recursive call to re-test. That's handy,1201// since the recursive call sees sub as the context_type.)1202if (do_counts) { NOT_PRODUCT(deps_find_witness_recursions++); }1203Klass* witness = find_witness_anywhere(sub,1204participants_hide_witnesses,1205/*top_level_call=*/ false);1206if (witness != NULL) return witness;1207}1208}1209}12101211// No witness found. The dependency remains unbroken.1212return NULL;1213#undef ADD_SUBCLASS_CHAIN1214}12151216bool ClassHierarchyWalker::witnessed_reabstraction_in_supers(Klass* k) {1217if (!k->oop_is_instance()) {1218return false; // no methods to find in an array type1219} else {1220// Looking for a case when an abstract method is inherited into a concrete class.1221if (Dependencies::is_concrete_klass(k) && !k->is_interface()) {1222Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::skip_private);1223if (m != NULL) {1224return false; // no reabstraction possible: local method found1225}1226for (InstanceKlass* super = InstanceKlass::cast(k)->java_super(); super != NULL; super = super->java_super()) {1227m = super->find_instance_method(_name, _signature, Klass::skip_private);1228if (m != NULL) { // inherited method found1229if (m->is_abstract() || m->is_overpass()) {1230_found_methods[_num_participants] = m;1231return true; // abstract method found1232}1233return false;1234}1235}1236assert(false, "root method not found");1237return true;1238}1239return false;1240}1241}12421243bool Dependencies::is_concrete_klass(Klass* k) {1244if (k->is_abstract()) return false;1245// %%% We could treat classes which are concrete but1246// have not yet been instantiated as virtually abstract.1247// This would require a deoptimization barrier on first instantiation.1248//if (k->is_not_instantiated()) return false;1249return true;1250}12511252bool Dependencies::is_concrete_method(Method* m, Klass * k) {1253// NULL is not a concrete method,1254// statics are irrelevant to virtual call sites,1255// abstract methods are not concrete,1256// overpass (error) methods are not concrete if k is abstract1257//1258// note "true" is conservative answer --1259// overpass clause is false if k == NULL, implies return true if1260// answer depends on overpass clause.1261return ! ( m == NULL || m -> is_static() || m -> is_abstract() ||1262m->is_overpass() && k != NULL && k -> is_abstract() );1263}126412651266Klass* Dependencies::find_finalizable_subclass(Klass* k) {1267if (k->is_interface()) return NULL;1268if (k->has_finalizer()) return k;1269k = k->subklass();1270while (k != NULL) {1271Klass* result = find_finalizable_subclass(k);1272if (result != NULL) return result;1273k = k->next_sibling();1274}1275return NULL;1276}127712781279bool Dependencies::is_concrete_klass(ciInstanceKlass* k) {1280if (k->is_abstract()) return false;1281// We could also return false if k does not yet appear to be1282// instantiated, if the VM version supports this distinction also.1283//if (k->is_not_instantiated()) return false;1284return true;1285}12861287bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) {1288return k->has_finalizable_subclass();1289}129012911292// Any use of the contents (bytecodes) of a method must be1293// marked by an "evol_method" dependency, if those contents1294// can change. (Note: A method is always dependent on itself.)1295Klass* Dependencies::check_evol_method(Method* m) {1296assert(must_be_in_vm(), "raw oops here");1297// Did somebody do a JVMTI RedefineClasses while our backs were turned?1298// Or is there a now a breakpoint?1299// (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.)1300if (m->is_old()1301|| m->number_of_breakpoints() > 0) {1302return m->method_holder();1303} else {1304return NULL;1305}1306}13071308// This is a strong assertion: It is that the given type1309// has no subtypes whatever. It is most useful for1310// optimizing checks on reflected types or on array types.1311// (Checks on types which are derived from real instances1312// can be optimized more strongly than this, because we1313// know that the checked type comes from a concrete type,1314// and therefore we can disregard abstract types.)1315Klass* Dependencies::check_leaf_type(Klass* ctxk) {1316assert(must_be_in_vm(), "raw oops here");1317assert_locked_or_safepoint(Compile_lock);1318InstanceKlass* ctx = InstanceKlass::cast(ctxk);1319Klass* sub = ctx->subklass();1320if (sub != NULL) {1321return sub;1322} else if (ctx->nof_implementors() != 0) {1323// if it is an interface, it must be unimplemented1324// (if it is not an interface, nof_implementors is always zero)1325Klass* impl = ctx->implementor();1326assert(impl != NULL, "must be set");1327return impl;1328} else {1329return NULL;1330}1331}13321333// Test the assertion that conck is the only concrete subtype* of ctxk.1334// The type conck itself is allowed to have have further concrete subtypes.1335// This allows the compiler to narrow occurrences of ctxk by conck,1336// when dealing with the types of actual instances.1337Klass* Dependencies::check_abstract_with_unique_concrete_subtype(Klass* ctxk,1338Klass* conck,1339KlassDepChange* changes) {1340ClassHierarchyWalker wf(conck);1341return wf.find_witness_subtype(ctxk, changes);1342}13431344// If a non-concrete class has no concrete subtypes, it is not (yet)1345// instantiatable. This can allow the compiler to make some paths go1346// dead, if they are gated by a test of the type.1347Klass* Dependencies::check_abstract_with_no_concrete_subtype(Klass* ctxk,1348KlassDepChange* changes) {1349// Find any concrete subtype, with no participants:1350ClassHierarchyWalker wf;1351return wf.find_witness_subtype(ctxk, changes);1352}135313541355// If a concrete class has no concrete subtypes, it can always be1356// exactly typed. This allows the use of a cheaper type test.1357Klass* Dependencies::check_concrete_with_no_concrete_subtype(Klass* ctxk,1358KlassDepChange* changes) {1359// Find any concrete subtype, with only the ctxk as participant:1360ClassHierarchyWalker wf(ctxk);1361return wf.find_witness_subtype(ctxk, changes);1362}136313641365// Find the unique concrete proper subtype of ctxk, or NULL if there1366// is more than one concrete proper subtype. If there are no concrete1367// proper subtypes, return ctxk itself, whether it is concrete or not.1368// The returned subtype is allowed to have have further concrete subtypes.1369// That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }.1370Klass* Dependencies::find_unique_concrete_subtype(Klass* ctxk) {1371ClassHierarchyWalker wf(ctxk); // Ignore ctxk when walking.1372wf.record_witnesses(1); // Record one other witness when walking.1373Klass* wit = wf.find_witness_subtype(ctxk);1374if (wit != NULL) return NULL; // Too many witnesses.1375Klass* conck = wf.participant(0);1376if (conck == NULL) {1377#ifndef PRODUCT1378// Make sure the dependency mechanism will pass this discovery:1379if (VerifyDependencies) {1380// Turn off dependency tracing while actually testing deps.1381FlagSetting fs(TraceDependencies, false);1382if (!Dependencies::is_concrete_klass(ctxk)) {1383guarantee(NULL ==1384(void *)check_abstract_with_no_concrete_subtype(ctxk),1385"verify dep.");1386} else {1387guarantee(NULL ==1388(void *)check_concrete_with_no_concrete_subtype(ctxk),1389"verify dep.");1390}1391}1392#endif //PRODUCT1393return ctxk; // Return ctxk as a flag for "no subtypes".1394} else {1395#ifndef PRODUCT1396// Make sure the dependency mechanism will pass this discovery:1397if (VerifyDependencies) {1398// Turn off dependency tracing while actually testing deps.1399FlagSetting fs(TraceDependencies, false);1400if (!Dependencies::is_concrete_klass(ctxk)) {1401guarantee(NULL == (void *)1402check_abstract_with_unique_concrete_subtype(ctxk, conck),1403"verify dep.");1404}1405}1406#endif //PRODUCT1407return conck;1408}1409}14101411// Test the assertion that the k[12] are the only concrete subtypes of ctxk,1412// except possibly for further subtypes of k[12] themselves.1413// The context type must be abstract. The types k1 and k2 are themselves1414// allowed to have further concrete subtypes.1415Klass* Dependencies::check_abstract_with_exclusive_concrete_subtypes(1416Klass* ctxk,1417Klass* k1,1418Klass* k2,1419KlassDepChange* changes) {1420ClassHierarchyWalker wf;1421wf.add_participant(k1);1422wf.add_participant(k2);1423return wf.find_witness_subtype(ctxk, changes);1424}14251426// Search ctxk for concrete implementations. If there are klen or fewer,1427// pack them into the given array and return the number.1428// Otherwise, return -1, meaning the given array would overflow.1429// (Note that a return of 0 means there are exactly no concrete subtypes.)1430// In this search, if ctxk is concrete, it will be reported alone.1431// For any type CC reported, no proper subtypes of CC will be reported.1432int Dependencies::find_exclusive_concrete_subtypes(Klass* ctxk,1433int klen,1434Klass* karray[]) {1435ClassHierarchyWalker wf;1436wf.record_witnesses(klen);1437Klass* wit = wf.find_witness_subtype(ctxk);1438if (wit != NULL) return -1; // Too many witnesses.1439int num = wf.num_participants();1440assert(num <= klen, "oob");1441// Pack the result array with the good news.1442for (int i = 0; i < num; i++)1443karray[i] = wf.participant(i);1444#ifndef PRODUCT1445// Make sure the dependency mechanism will pass this discovery:1446if (VerifyDependencies) {1447// Turn off dependency tracing while actually testing deps.1448FlagSetting fs(TraceDependencies, false);1449switch (Dependencies::is_concrete_klass(ctxk)? -1: num) {1450case -1: // ctxk was itself concrete1451guarantee(num == 1 && karray[0] == ctxk, "verify dep.");1452break;1453case 0:1454guarantee(NULL == (void *)check_abstract_with_no_concrete_subtype(ctxk),1455"verify dep.");1456break;1457case 1:1458guarantee(NULL == (void *)1459check_abstract_with_unique_concrete_subtype(ctxk, karray[0]),1460"verify dep.");1461break;1462case 2:1463guarantee(NULL == (void *)1464check_abstract_with_exclusive_concrete_subtypes(ctxk,1465karray[0],1466karray[1]),1467"verify dep.");1468break;1469default:1470ShouldNotReachHere(); // klen > 2 yet supported1471}1472}1473#endif //PRODUCT1474return num;1475}14761477// If a class (or interface) has a unique concrete method uniqm, return NULL.1478// Otherwise, return a class that contains an interfering method.1479Klass* Dependencies::check_unique_concrete_method(Klass* ctxk, Method* uniqm,1480KlassDepChange* changes) {1481// Here is a missing optimization: If uniqm->is_final(),1482// we don't really need to search beneath it for overrides.1483// This is probably not important, since we don't use dependencies1484// to track final methods. (They can't be "definalized".)1485ClassHierarchyWalker wf(uniqm->method_holder(), uniqm);1486return wf.find_witness_definer(ctxk, changes);1487}14881489// Find the set of all non-abstract methods under ctxk that match m.1490// (The method m must be defined or inherited in ctxk.)1491// Include m itself in the set, unless it is abstract.1492// If this set has exactly one element, return that element.1493Method* Dependencies::find_unique_concrete_method(Klass* ctxk, Method* m) {1494ClassHierarchyWalker wf(m);1495assert(wf.check_method_context(ctxk, m), "proper context");1496wf.record_witnesses(1);1497Klass* wit = wf.find_witness_definer(ctxk);1498if (wit != NULL) return NULL; // Too many witnesses.1499Method* fm = wf.found_method(0); // Will be NULL if num_parts == 0.1500if (Dependencies::is_concrete_method(m, ctxk)) {1501if (fm == NULL) {1502// It turns out that m was always the only implementation.1503fm = m;1504} else if (fm != m) {1505// Two conflicting implementations after all.1506// (This can happen if m is inherited into ctxk and fm overrides it.)1507return NULL;1508}1509}1510#ifndef PRODUCT1511// Make sure the dependency mechanism will pass this discovery:1512if (VerifyDependencies && fm != NULL) {1513guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm),1514"verify dep.");1515}1516#endif //PRODUCT1517return fm;1518}15191520Klass* Dependencies::check_exclusive_concrete_methods(Klass* ctxk,1521Method* m1,1522Method* m2,1523KlassDepChange* changes) {1524ClassHierarchyWalker wf(m1);1525wf.add_participant(m1->method_holder());1526wf.add_participant(m2->method_holder());1527return wf.find_witness_definer(ctxk, changes);1528}15291530Klass* Dependencies::check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes) {1531Klass* search_at = ctxk;1532if (changes != NULL)1533search_at = changes->new_type(); // just look at the new bit1534return find_finalizable_subclass(search_at);1535}15361537Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {1538assert(call_site ->is_a(SystemDictionary::CallSite_klass()), "sanity");1539assert(method_handle->is_a(SystemDictionary::MethodHandle_klass()), "sanity");1540if (changes == NULL) {1541// Validate all CallSites1542if (java_lang_invoke_CallSite::target(call_site) != method_handle)1543return call_site->klass(); // assertion failed1544} else {1545// Validate the given CallSite1546if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {1547assert(method_handle != changes->method_handle(), "must be");1548return call_site->klass(); // assertion failed1549}1550}1551return NULL; // assertion still valid1552}155315541555void Dependencies::DepStream::trace_and_log_witness(Klass* witness) {1556if (witness != NULL) {1557if (TraceDependencies) {1558print_dependency(witness, /*verbose=*/ true);1559}1560// The following is a no-op unless logging is enabled:1561log_dependency(witness);1562}1563}156415651566Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {1567assert_locked_or_safepoint(Compile_lock);1568Dependencies::check_valid_dependency_type(type());15691570Klass* witness = NULL;1571switch (type()) {1572case evol_method:1573witness = check_evol_method(method_argument(0));1574break;1575case leaf_type:1576witness = check_leaf_type(context_type());1577break;1578case abstract_with_unique_concrete_subtype:1579witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes);1580break;1581case abstract_with_no_concrete_subtype:1582witness = check_abstract_with_no_concrete_subtype(context_type(), changes);1583break;1584case concrete_with_no_concrete_subtype:1585witness = check_concrete_with_no_concrete_subtype(context_type(), changes);1586break;1587case unique_concrete_method:1588witness = check_unique_concrete_method(context_type(), method_argument(1), changes);1589break;1590case abstract_with_exclusive_concrete_subtypes_2:1591witness = check_abstract_with_exclusive_concrete_subtypes(context_type(), type_argument(1), type_argument(2), changes);1592break;1593case exclusive_concrete_methods_2:1594witness = check_exclusive_concrete_methods(context_type(), method_argument(1), method_argument(2), changes);1595break;1596case no_finalizable_subclasses:1597witness = check_has_no_finalizable_subclasses(context_type(), changes);1598break;1599default:1600witness = NULL;1601break;1602}1603trace_and_log_witness(witness);1604return witness;1605}160616071608Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) {1609assert_locked_or_safepoint(Compile_lock);1610Dependencies::check_valid_dependency_type(type());16111612Klass* witness = NULL;1613switch (type()) {1614case call_site_target_value:1615witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes);1616break;1617default:1618witness = NULL;1619break;1620}1621trace_and_log_witness(witness);1622return witness;1623}162416251626Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {1627// Handle klass dependency1628if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))1629return check_klass_dependency(changes.as_klass_change());16301631// Handle CallSite dependency1632if (changes.is_call_site_change())1633return check_call_site_dependency(changes.as_call_site_change());16341635// irrelevant dependency; skip it1636return NULL;1637}163816391640void DepChange::print() {1641int nsup = 0, nint = 0;1642for (ContextStream str(*this); str.next(); ) {1643Klass* k = str.klass();1644switch (str.change_type()) {1645case Change_new_type:1646tty->print_cr(" dependee = %s", InstanceKlass::cast(k)->external_name());1647break;1648case Change_new_sub:1649if (!WizardMode) {1650++nsup;1651} else {1652tty->print_cr(" context super = %s", InstanceKlass::cast(k)->external_name());1653}1654break;1655case Change_new_impl:1656if (!WizardMode) {1657++nint;1658} else {1659tty->print_cr(" context interface = %s", InstanceKlass::cast(k)->external_name());1660}1661break;1662}1663}1664if (nsup + nint != 0) {1665tty->print_cr(" context supers = %d, interfaces = %d", nsup, nint);1666}1667}16681669void DepChange::ContextStream::start() {1670Klass* new_type = _changes.is_klass_change() ? _changes.as_klass_change()->new_type() : (Klass*) NULL;1671_change_type = (new_type == NULL ? NO_CHANGE : Start_Klass);1672_klass = new_type;1673_ti_base = NULL;1674_ti_index = 0;1675_ti_limit = 0;1676}16771678bool DepChange::ContextStream::next() {1679switch (_change_type) {1680case Start_Klass: // initial state; _klass is the new type1681_ti_base = InstanceKlass::cast(_klass)->transitive_interfaces();1682_ti_index = 0;1683_change_type = Change_new_type;1684return true;1685case Change_new_type:1686// fall through:1687_change_type = Change_new_sub;1688case Change_new_sub:1689// 6598190: brackets workaround Sun Studio C++ compiler bug 66292771690{1691_klass = InstanceKlass::cast(_klass)->super();1692if (_klass != NULL) {1693return true;1694}1695}1696// else set up _ti_limit and fall through:1697_ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length();1698_change_type = Change_new_impl;1699case Change_new_impl:1700if (_ti_index < _ti_limit) {1701_klass = _ti_base->at(_ti_index++);1702return true;1703}1704// fall through:1705_change_type = NO_CHANGE; // iterator is exhausted1706case NO_CHANGE:1707break;1708default:1709ShouldNotReachHere();1710}1711return false;1712}17131714void KlassDepChange::initialize() {1715// entire transaction must be under this lock:1716assert_lock_strong(Compile_lock);17171718// Mark all dependee and all its superclasses1719// Mark transitive interfaces1720for (ContextStream str(*this); str.next(); ) {1721Klass* d = str.klass();1722assert(!InstanceKlass::cast(d)->is_marked_dependent(), "checking");1723InstanceKlass::cast(d)->set_is_marked_dependent(true);1724}1725}17261727KlassDepChange::~KlassDepChange() {1728// Unmark all dependee and all its superclasses1729// Unmark transitive interfaces1730for (ContextStream str(*this); str.next(); ) {1731Klass* d = str.klass();1732InstanceKlass::cast(d)->set_is_marked_dependent(false);1733}1734}17351736bool KlassDepChange::involves_context(Klass* k) {1737if (k == NULL || !k->oop_is_instance()) {1738return false;1739}1740InstanceKlass* ik = InstanceKlass::cast(k);1741bool is_contained = ik->is_marked_dependent();1742assert(is_contained == new_type()->is_subtype_of(k),1743"correct marking of potential context types");1744return is_contained;1745}17461747#ifndef PRODUCT1748void Dependencies::print_statistics() {1749if (deps_find_witness_print != 0) {1750// Call one final time, to flush out the data.1751deps_find_witness_print = -1;1752count_find_witness_calls();1753}1754}1755#endif175617571758