dependencies.cpp revision 3602:da91efe96a93
1223013Sdim/* 2223013Sdim * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3223013Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4223013Sdim * 5223013Sdim * This code is free software; you can redistribute it and/or modify it 6223013Sdim * under the terms of the GNU General Public License version 2 only, as 7223013Sdim * published by the Free Software Foundation. 8223013Sdim * 9223013Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10223013Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11223013Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12223013Sdim * version 2 for more details (a copy is included in the LICENSE file that 13223013Sdim * accompanied this code). 14223013Sdim * 15263508Sdim * You should have received a copy of the GNU General Public License version 16263508Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17223013Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18223013Sdim * 19234353Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20249423Sdim * or visit www.oracle.com if you need additional information or have any 21223013Sdim * questions. 22223013Sdim * 23234982Sdim */ 24263508Sdim 25249423Sdim#include "precompiled.hpp" 26223013Sdim#include "ci/ciArrayKlass.hpp" 27223013Sdim#include "ci/ciEnv.hpp" 28223013Sdim#include "ci/ciKlass.hpp" 29223013Sdim#include "ci/ciMethod.hpp" 30234353Sdim#include "code/dependencies.hpp" 31234353Sdim#include "compiler/compileLog.hpp" 32234353Sdim#include "oops/oop.inline.hpp" 33234353Sdim#include "runtime/handles.hpp" 34263508Sdim#include "runtime/handles.inline.hpp" 35239462Sdim#include "utilities/copy.hpp" 36239462Sdim 37239462Sdim 38263508Sdim#ifdef ASSERT 39263508Sdimstatic bool must_be_in_vm() { 40234353Sdim Thread* thread = Thread::current(); 41234353Sdim if (thread->is_Java_thread()) 42239462Sdim return ((JavaThread*)thread)->thread_state() == _thread_in_vm; 43239462Sdim else 44263508Sdim return true; //something like this: thread->is_VM_thread(); 45263508Sdim} 46234353Sdim#endif //ASSERT 47234353Sdim 48234353Sdimvoid Dependencies::initialize(ciEnv* env) { 49234353Sdim Arena* arena = env->arena(); 50234353Sdim _oop_recorder = env->oop_recorder(); 51234353Sdim _log = env->log(); 52234353Sdim _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0); 53234353Sdim DEBUG_ONLY(_deps[end_marker] = NULL); 54234353Sdim for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) { 55234353Sdim _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0); 56234353Sdim } 57239462Sdim _content_bytes = NULL; 58239462Sdim _size_in_bytes = (size_t)-1; 59239462Sdim 60234353Sdim assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity"); 61239462Sdim} 62239462Sdim 63243830Sdimvoid Dependencies::assert_evol_method(ciMethod* m) { 64243830Sdim assert_common_1(evol_method, m); 65239462Sdim} 66239462Sdim 67239462Sdimvoid Dependencies::assert_leaf_type(ciKlass* ctxk) { 68239462Sdim if (ctxk->is_array_klass()) { 69243830Sdim // As a special case, support this assertion on an array type, 70239462Sdim // which reduces to an assertion on its element type. 71239462Sdim // Note that this cannot be done with assertions that 72239462Sdim // relate to concreteness or abstractness. 73239462Sdim ciType* elemt = ctxk->as_array_klass()->base_element_type(); 74239462Sdim if (!elemt->is_instance_klass()) return; // Ex: int[][] 75239462Sdim ctxk = elemt->as_instance_klass(); 76243830Sdim //if (ctxk->is_final()) return; // Ex: String[][] 77263508Sdim } 78239462Sdim check_ctxk(ctxk); 79239462Sdim assert_common_1(leaf_type, ctxk); 80239462Sdim} 81239462Sdim 82239462Sdimvoid Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) { 83234353Sdim check_ctxk_abstract(ctxk); 84234353Sdim assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck); 85243830Sdim} 86243830Sdim 87243830Sdimvoid Dependencies::assert_abstract_with_no_concrete_subtype(ciKlass* ctxk) { 88243830Sdim check_ctxk_abstract(ctxk); 89243830Sdim assert_common_1(abstract_with_no_concrete_subtype, ctxk); 90243830Sdim} 91243830Sdim 92243830Sdimvoid Dependencies::assert_concrete_with_no_concrete_subtype(ciKlass* ctxk) { 93243830Sdim check_ctxk_concrete(ctxk); 94243830Sdim assert_common_1(concrete_with_no_concrete_subtype, ctxk); 95243830Sdim} 96243830Sdim 97243830Sdimvoid Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) { 98243830Sdim check_ctxk(ctxk); 99243830Sdim assert_common_2(unique_concrete_method, ctxk, uniqm); 100234353Sdim} 101234353Sdim 102234353Sdimvoid Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) { 103223013Sdim check_ctxk(ctxk); 104223013Sdim assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2); 105223013Sdim} 106223013Sdim 107223013Sdimvoid Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) { 108223013Sdim check_ctxk(ctxk); 109223013Sdim assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2); 110234353Sdim} 111239462Sdim 112239462Sdimvoid Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) { 113239462Sdim check_ctxk(ctxk); 114239462Sdim assert_common_1(no_finalizable_subclasses, ctxk); 115223013Sdim} 116223013Sdim 117239462Sdimvoid Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) { 118239462Sdim check_ctxk(call_site->klass()); 119239462Sdim assert_common_2(call_site_target_value, call_site, method_handle); 120239462Sdim} 121239462Sdim 122243830Sdim// Helper function. If we are adding a new dep. under ctxk2, 123243830Sdim// try to find an old dep. under a broader* ctxk1. If there is 124239462Sdim// 125239462Sdimbool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps, 126239462Sdim int ctxk_i, ciKlass* ctxk2) { 127239462Sdim ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass(); 128239462Sdim if (ctxk2->is_subtype_of(ctxk1)) { 129239462Sdim return true; // success, and no need to change 130239462Sdim } else if (ctxk1->is_subtype_of(ctxk2)) { 131239462Sdim // new context class fully subsumes previous one 132239462Sdim deps->at_put(ctxk_i, ctxk2); 133239462Sdim return true; 134239462Sdim } else { 135239462Sdim return false; 136239462Sdim } 137239462Sdim} 138239462Sdim 139239462Sdimvoid Dependencies::assert_common_1(DepType dept, ciBaseObject* x) { 140239462Sdim assert(dep_args(dept) == 1, "sanity"); 141239462Sdim log_dependency(dept, x); 142239462Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 143239462Sdim 144239462Sdim // see if the same (or a similar) dep is already recorded 145239462Sdim if (note_dep_seen(dept, x)) { 146239462Sdim assert(deps->find(x) >= 0, "sanity"); 147239462Sdim } else { 148223013Sdim deps->append(x); 149223013Sdim } 150223013Sdim} 151223013Sdim 152223013Sdimvoid Dependencies::assert_common_2(DepType dept, 153234353Sdim ciBaseObject* x0, ciBaseObject* x1) { 154234353Sdim assert(dep_args(dept) == 2, "sanity"); 155234353Sdim log_dependency(dept, x0, x1); 156234353Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 157234353Sdim 158234353Sdim // see if the same (or a similar) dep is already recorded 159234353Sdim bool has_ctxk = has_explicit_context_arg(dept); 160234353Sdim if (has_ctxk) { 161234353Sdim assert(dep_context_arg(dept) == 0, "sanity"); 162234353Sdim if (note_dep_seen(dept, x1)) { 163234353Sdim // look in this bucket for redundant assertions 164234353Sdim const int stride = 2; 165234353Sdim for (int i = deps->length(); (i -= stride) >= 0; ) { 166234353Sdim ciBaseObject* y1 = deps->at(i+1); 167234353Sdim if (x1 == y1) { // same subject; check the context 168234353Sdim if (maybe_merge_ctxk(deps, i+0, x0->as_metadata()->as_klass())) { 169234353Sdim return; 170234353Sdim } 171239462Sdim } 172234353Sdim } 173234353Sdim } 174234353Sdim } else { 175234353Sdim assert(dep_implicit_context_arg(dept) == 0, "sanity"); 176234353Sdim if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) { 177234353Sdim // look in this bucket for redundant assertions 178234353Sdim const int stride = 2; 179234353Sdim for (int i = deps->length(); (i -= stride) >= 0; ) { 180234353Sdim ciBaseObject* y0 = deps->at(i+0); 181234353Sdim ciBaseObject* y1 = deps->at(i+1); 182234353Sdim if (x0 == y0 && x1 == y1) { 183234353Sdim return; 184234353Sdim } 185234353Sdim } 186234353Sdim } 187234353Sdim } 188234353Sdim 189234353Sdim // append the assertion in the correct bucket: 190234353Sdim deps->append(x0); 191234353Sdim deps->append(x1); 192234353Sdim} 193234353Sdim 194234353Sdimvoid Dependencies::assert_common_3(DepType dept, 195234353Sdim ciKlass* ctxk, ciBaseObject* x, ciBaseObject* x2) { 196234353Sdim assert(dep_context_arg(dept) == 0, "sanity"); 197234353Sdim assert(dep_args(dept) == 3, "sanity"); 198234353Sdim log_dependency(dept, ctxk, x, x2); 199234353Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 200234353Sdim 201234353Sdim // try to normalize an unordered pair: 202223013Sdim bool swap = false; 203223013Sdim switch (dept) { 204234353Sdim case abstract_with_exclusive_concrete_subtypes_2: 205234353Sdim swap = (x->ident() > x2->ident() && x->as_metadata()->as_klass() != ctxk); 206234353Sdim break; 207234353Sdim case exclusive_concrete_methods_2: 208234353Sdim swap = (x->ident() > x2->ident() && x->as_metadata()->as_method()->holder() != ctxk); 209234353Sdim break; 210234353Sdim } 211234353Sdim if (swap) { ciBaseObject* t = x; x = x2; x2 = t; } 212234353Sdim 213234353Sdim // see if the same (or a similar) dep is already recorded 214234353Sdim if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) { 215234353Sdim // look in this bucket for redundant assertions 216234353Sdim const int stride = 3; 217234353Sdim for (int i = deps->length(); (i -= stride) >= 0; ) { 218234353Sdim ciBaseObject* y = deps->at(i+1); 219234353Sdim ciBaseObject* y2 = deps->at(i+2); 220234353Sdim if (x == y && x2 == y2) { // same subjects; check the context 221234353Sdim if (maybe_merge_ctxk(deps, i+0, ctxk)) { 222223013Sdim return; 223239462Sdim } 224223013Sdim } 225223013Sdim } 226223013Sdim } 227223013Sdim // append the assertion in the correct bucket: 228223013Sdim deps->append(ctxk); 229239462Sdim deps->append(x); 230239462Sdim deps->append(x2); 231239462Sdim} 232239462Sdim 233234353Sdim/// Support for encoding dependencies into an nmethod: 234243830Sdim 235243830Sdimvoid Dependencies::copy_to(nmethod* nm) { 236239462Sdim address beg = nm->dependencies_begin(); 237239462Sdim address end = nm->dependencies_end(); 238239462Sdim guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing"); 239223013Sdim Copy::disjoint_words((HeapWord*) content_bytes(), 240223013Sdim (HeapWord*) beg, 241223013Sdim size_in_bytes() / sizeof(HeapWord)); 242234353Sdim assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words"); 243223013Sdim} 244234353Sdim 245223013Sdimstatic int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) { 246239462Sdim for (int i = 0; i < narg; i++) { 247239462Sdim int diff = p1[i]->ident() - p2[i]->ident(); 248239462Sdim if (diff != 0) return diff; 249224145Sdim } 250223013Sdim return 0; 251224145Sdim} 252223013Sdimstatic int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2) 253234353Sdim{ return sort_dep(p1, p2, 1); } 254224145Sdimstatic int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2) 255223013Sdim{ return sort_dep(p1, p2, 2); } 256223013Sdimstatic int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2) 257234353Sdim{ return sort_dep(p1, p2, 3); } 258234353Sdim 259234353Sdimvoid Dependencies::sort_all_deps() { 260234353Sdim for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 261239462Sdim DepType dept = (DepType)deptv; 262234353Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 263234353Sdim if (deps->length() <= 1) continue; 264234353Sdim switch (dep_args(dept)) { 265234353Sdim case 1: deps->sort(sort_dep_arg_1, 1); break; 266239462Sdim case 2: deps->sort(sort_dep_arg_2, 2); break; 267234353Sdim case 3: deps->sort(sort_dep_arg_3, 3); break; 268234353Sdim default: ShouldNotReachHere(); 269234353Sdim } 270234353Sdim } 271234353Sdim} 272234353Sdim 273234353Sdimsize_t Dependencies::estimate_size_in_bytes() { 274234353Sdim size_t est_size = 100; 275234353Sdim for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 276234353Sdim DepType dept = (DepType)deptv; 277234353Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 278234353Sdim est_size += deps->length()*2; // tags and argument(s) 279234353Sdim } 280234353Sdim return est_size; 281234353Sdim} 282234353Sdim 283234353SdimciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) { 284234353Sdim switch (dept) { 285223013Sdim case abstract_with_exclusive_concrete_subtypes_2: 286223013Sdim return x->as_metadata()->as_klass(); 287234353Sdim case unique_concrete_method: 288234353Sdim case exclusive_concrete_methods_2: 289234353Sdim return x->as_metadata()->as_method()->holder(); 290234353Sdim } 291234353Sdim return NULL; // let NULL be NULL 292234353Sdim} 293234353Sdim 294234353SdimKlass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) { 295234353Sdim assert(must_be_in_vm(), "raw oops here"); 296234353Sdim switch (dept) { 297234353Sdim case abstract_with_exclusive_concrete_subtypes_2: 298234353Sdim assert(x->is_klass(), "sanity"); 299239462Sdim return (Klass*) x; 300234353Sdim case unique_concrete_method: 301234353Sdim case exclusive_concrete_methods_2: 302234353Sdim assert(x->is_method(), "sanity"); 303234353Sdim return ((Method*)x)->method_holder(); 304239462Sdim } 305234353Sdim return NULL; // let NULL be NULL 306234353Sdim} 307234353Sdim 308234353Sdimvoid Dependencies::encode_content_bytes() { 309223013Sdim sort_all_deps(); 310234353Sdim 311239462Sdim // cast is safe, no deps can overflow INT_MAX 312239462Sdim CompressedWriteStream bytes((int)estimate_size_in_bytes()); 313239462Sdim 314239462Sdim for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 315243830Sdim DepType dept = (DepType)deptv; 316239462Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 317239462Sdim if (deps->length() == 0) continue; 318243830Sdim int stride = dep_args(dept); 319243830Sdim int ctxkj = dep_context_arg(dept); // -1 if no context arg 320239462Sdim assert(stride > 0, "sanity"); 321263508Sdim for (int i = 0; i < deps->length(); i += stride) { 322263508Sdim jbyte code_byte = (jbyte)dept; 323263508Sdim int skipj = -1; 324263508Sdim if (ctxkj >= 0 && ctxkj+1 < stride) { 325263508Sdim ciKlass* ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass(); 326239462Sdim ciBaseObject* x = deps->at(i+ctxkj+1); // following argument 327239462Sdim if (ctxk == ctxk_encoded_as_null(dept, x)) { 328239462Sdim skipj = ctxkj; // we win: maybe one less oop to keep track of 329239462Sdim code_byte |= default_context_type_bit; 330239462Sdim } 331239462Sdim } 332243830Sdim bytes.write_byte(code_byte); 333239462Sdim for (int j = 0; j < stride; j++) { 334239462Sdim if (j == skipj) continue; 335243830Sdim ciBaseObject* v = deps->at(i+j); 336239462Sdim if (v->is_object()) { 337239462Sdim bytes.write_int(_oop_recorder->find_index(v->as_object()->constant_encoding())); 338239462Sdim } else { 339239462Sdim ciMetadata* meta = v->as_metadata(); 340239462Sdim bytes.write_int(_oop_recorder->find_index(meta->constant_encoding())); 341239462Sdim } 342239462Sdim } 343239462Sdim } 344239462Sdim } 345239462Sdim 346239462Sdim // write a sentinel byte to mark the end 347239462Sdim bytes.write_byte(end_marker); 348239462Sdim 349239462Sdim // round it out to a word boundary 350239462Sdim while (bytes.position() % sizeof(HeapWord) != 0) { 351239462Sdim bytes.write_byte(end_marker); 352239462Sdim } 353239462Sdim 354239462Sdim // check whether the dept byte encoding really works 355239462Sdim assert((jbyte)default_context_type_bit != 0, "byte overflow"); 356239462Sdim 357239462Sdim _content_bytes = bytes.buffer(); 358239462Sdim _size_in_bytes = bytes.position(); 359239462Sdim} 360234353Sdim 361239462Sdim 362239462Sdimconst char* Dependencies::_dep_name[TYPE_LIMIT] = { 363239462Sdim "end_marker", 364239462Sdim "evol_method", 365239462Sdim "leaf_type", 366239462Sdim "abstract_with_unique_concrete_subtype", 367239462Sdim "abstract_with_no_concrete_subtype", 368239462Sdim "concrete_with_no_concrete_subtype", 369239462Sdim "unique_concrete_method", 370239462Sdim "abstract_with_exclusive_concrete_subtypes_2", 371239462Sdim "exclusive_concrete_methods_2", 372239462Sdim "no_finalizable_subclasses", 373239462Sdim "call_site_target_value" 374239462Sdim}; 375239462Sdim 376239462Sdimint Dependencies::_dep_args[TYPE_LIMIT] = { 377239462Sdim -1,// end_marker 378239462Sdim 1, // evol_method m 379239462Sdim 1, // leaf_type ctxk 380239462Sdim 2, // abstract_with_unique_concrete_subtype ctxk, k 381239462Sdim 1, // abstract_with_no_concrete_subtype ctxk 382239462Sdim 1, // concrete_with_no_concrete_subtype ctxk 383239462Sdim 2, // unique_concrete_method ctxk, m 384239462Sdim 3, // unique_concrete_subtypes_2 ctxk, k1, k2 385239462Sdim 3, // unique_concrete_methods_2 ctxk, m1, m2 386239462Sdim 1, // no_finalizable_subclasses ctxk 387239462Sdim 2 // call_site_target_value call_site, method_handle 388239462Sdim}; 389239462Sdim 390239462Sdimconst char* Dependencies::dep_name(Dependencies::DepType dept) { 391239462Sdim if (!dept_in_mask(dept, all_types)) return "?bad-dep?"; 392239462Sdim return _dep_name[dept]; 393239462Sdim} 394239462Sdim 395239462Sdimint Dependencies::dep_args(Dependencies::DepType dept) { 396239462Sdim if (!dept_in_mask(dept, all_types)) return -1; 397239462Sdim return _dep_args[dept]; 398239462Sdim} 399239462Sdim 400239462Sdimvoid Dependencies::check_valid_dependency_type(DepType dept) { 401239462Sdim guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, err_msg("invalid dependency type: %d", (int) dept)); 402239462Sdim} 403239462Sdim 404239462Sdim// for the sake of the compiler log, print out current dependencies: 405223013Sdimvoid Dependencies::log_all_dependencies() { 406223013Sdim if (log() == NULL) return; 407223013Sdim ciBaseObject* args[max_arg_count]; 408239462Sdim for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 409239462Sdim DepType dept = (DepType)deptv; 410239462Sdim GrowableArray<ciBaseObject*>* deps = _deps[dept]; 411239462Sdim if (deps->length() == 0) continue; 412239462Sdim int stride = dep_args(dept); 413239462Sdim for (int i = 0; i < deps->length(); i += stride) { 414239462Sdim for (int j = 0; j < stride; j++) { 415239462Sdim // flush out the identities before printing 416239462Sdim args[j] = deps->at(i+j); 417239462Sdim } 418239462Sdim write_dependency_to(log(), dept, stride, args); 419239462Sdim } 420239462Sdim } 421239462Sdim} 422239462Sdim 423239462Sdimvoid Dependencies::write_dependency_to(CompileLog* log, 424239462Sdim DepType dept, 425239462Sdim int nargs, DepArgument args[], 426239462Sdim Klass* witness) { 427239462Sdim if (log == NULL) { 428239462Sdim return; 429239462Sdim } 430239462Sdim ciEnv* env = ciEnv::current(); 431239462Sdim ciBaseObject* ciargs[max_arg_count]; 432239462Sdim assert(nargs <= max_arg_count, "oob"); 433239462Sdim for (int j = 0; j < nargs; j++) { 434239462Sdim if (args[j].is_oop()) { 435239462Sdim ciargs[j] = env->get_object(args[j].oop_value()); 436239462Sdim } else { 437239462Sdim ciargs[j] = env->get_metadata(args[j].metadata_value()); 438239462Sdim } 439239462Sdim } 440239462Sdim Dependencies::write_dependency_to(log, dept, nargs, ciargs, witness); 441239462Sdim} 442239462Sdim 443239462Sdimvoid Dependencies::write_dependency_to(CompileLog* log, 444239462Sdim DepType dept, 445239462Sdim int nargs, ciBaseObject* args[], 446239462Sdim Klass* witness) { 447239462Sdim if (log == NULL) return; 448239462Sdim assert(nargs <= max_arg_count, "oob"); 449239462Sdim int argids[max_arg_count]; 450239462Sdim int ctxkj = dep_context_arg(dept); // -1 if no context arg 451239462Sdim int j; 452239462Sdim for (j = 0; j < nargs; j++) { 453239462Sdim if (args[j]->is_object()) { 454239462Sdim argids[j] = log->identify(args[j]->as_object()); 455239462Sdim } else { 456239462Sdim argids[j] = log->identify(args[j]->as_metadata()); 457239462Sdim } 458239462Sdim } 459239462Sdim if (witness != NULL) { 460239462Sdim log->begin_elem("dependency_failed"); 461239462Sdim } else { 462239462Sdim log->begin_elem("dependency"); 463239462Sdim } 464239462Sdim log->print(" type='%s'", dep_name(dept)); 465239462Sdim if (ctxkj >= 0) { 466239462Sdim log->print(" ctxk='%d'", argids[ctxkj]); 467239462Sdim } 468239462Sdim // write remaining arguments, if any. 469239462Sdim for (j = 0; j < nargs; j++) { 470239462Sdim if (j == ctxkj) continue; // already logged 471239462Sdim if (j == 1) { 472239462Sdim log->print( " x='%d'", argids[j]); 473239462Sdim } else { 474239462Sdim log->print(" x%d='%d'", j, argids[j]); 475239462Sdim } 476239462Sdim } 477239462Sdim if (witness != NULL) { 478239462Sdim log->object("witness", witness); 479239462Sdim log->stamp(); 480239462Sdim } 481239462Sdim log->end_elem(); 482243830Sdim} 483243830Sdim 484239462Sdimvoid Dependencies::write_dependency_to(xmlStream* xtty, 485239462Sdim DepType dept, 486239462Sdim int nargs, DepArgument args[], 487239462Sdim Klass* witness) { 488239462Sdim if (xtty == NULL) return; 489239462Sdim ttyLocker ttyl; 490239462Sdim int ctxkj = dep_context_arg(dept); // -1 if no context arg 491239462Sdim if (witness != NULL) { 492239462Sdim xtty->begin_elem("dependency_failed"); 493239462Sdim } else { 494239462Sdim xtty->begin_elem("dependency"); 495239462Sdim } 496239462Sdim xtty->print(" type='%s'", dep_name(dept)); 497239462Sdim if (ctxkj >= 0) { 498239462Sdim xtty->object("ctxk", args[ctxkj].metadata_value()); 499239462Sdim } 500239462Sdim // write remaining arguments, if any. 501239462Sdim for (int j = 0; j < nargs; j++) { 502239462Sdim if (j == ctxkj) continue; // already logged 503239462Sdim if (j == 1) { 504239462Sdim if (args[j].is_oop()) { 505239462Sdim xtty->object("x", args[j].oop_value()); 506239462Sdim } else { 507239462Sdim xtty->object("x", args[j].metadata_value()); 508239462Sdim } 509239462Sdim } else { 510239462Sdim char xn[10]; sprintf(xn, "x%d", j); 511239462Sdim if (args[j].is_oop()) { 512239462Sdim xtty->object(xn, args[j].oop_value()); 513239462Sdim } else { 514239462Sdim xtty->object(xn, args[j].metadata_value()); 515239462Sdim } 516239462Sdim } 517239462Sdim } 518239462Sdim if (witness != NULL) { 519224145Sdim xtty->object("witness", witness); 520234353Sdim xtty->stamp(); 521234353Sdim } 522224145Sdim xtty->end_elem(); 523239462Sdim} 524239462Sdim 525224145Sdimvoid Dependencies::print_dependency(DepType dept, int nargs, DepArgument args[], 526234353Sdim Klass* witness) { 527224145Sdim ResourceMark rm; 528239462Sdim ttyLocker ttyl; // keep the following output all in one block 529239462Sdim tty->print_cr("%s of type %s", 530239462Sdim (witness == NULL)? "Dependency": "Failed dependency", 531239462Sdim dep_name(dept)); 532224145Sdim // print arguments 533224145Sdim int ctxkj = dep_context_arg(dept); // -1 if no context arg 534234353Sdim for (int j = 0; j < nargs; j++) { 535234353Sdim DepArgument arg = args[j]; 536234353Sdim bool put_star = false; 537234353Sdim if (arg.is_null()) continue; 538234353Sdim const char* what; 539239462Sdim if (j == ctxkj) { 540234353Sdim assert(arg.is_metadata(), "must be"); 541234353Sdim what = "context"; 542234353Sdim put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value()); 543234353Sdim } else if (arg.is_method()) { 544223013Sdim what = "method "; 545224145Sdim put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value()); 546224145Sdim } else if (arg.is_klass()) { 547224145Sdim what = "class "; 548224145Sdim } else { 549224145Sdim what = "object "; 550224145Sdim } 551224145Sdim tty->print(" %s = %s", what, (put_star? "*": "")); 552224145Sdim if (arg.is_klass()) 553224145Sdim tty->print("%s", Klass::cast((Klass*)arg.metadata_value())->external_name()); 554224145Sdim else if (arg.is_method()) 555224145Sdim ((Method*)arg.metadata_value())->print_value(); 556224145Sdim else 557224145Sdim ShouldNotReachHere(); // Provide impl for this type. 558243830Sdim tty->cr(); 559224145Sdim } 560243830Sdim if (witness != NULL) { 561243830Sdim bool put_star = !Dependencies::is_concrete_klass(witness); 562224145Sdim tty->print_cr(" witness = %s%s", 563224145Sdim (put_star? "*": ""), 564224145Sdim Klass::cast(witness)->external_name()); 565224145Sdim } 566224145Sdim} 567243830Sdim 568224145Sdimvoid Dependencies::DepStream::log_dependency(Klass* witness) { 569224145Sdim if (_deps == NULL && xtty == NULL) return; // fast cutout for runtime 570224145Sdim if (type() == call_site_target_value) { 571224145Sdim os::breakpoint(); 572224145Sdim } 573224145Sdim int nargs = argument_count(); 574224145Sdim DepArgument args[max_arg_count]; 575224145Sdim for (int j = 0; j < nargs; j++) { 576226633Sdim if (type() == call_site_target_value) { 577226633Sdim args[j] = argument_oop(j); 578224145Sdim } else { 579224145Sdim args[j] = argument(j); 580224145Sdim } 581224145Sdim } 582224145Sdim if (_deps != NULL && _deps->log() != NULL) { 583224145Sdim Dependencies::write_dependency_to(_deps->log(), 584224145Sdim type(), nargs, args, witness); 585224145Sdim } else { 586224145Sdim Dependencies::write_dependency_to(xtty, 587224145Sdim type(), nargs, args, witness); 588224145Sdim } 589226633Sdim} 590224145Sdim 591224145Sdimvoid Dependencies::DepStream::print_dependency(Klass* witness, bool verbose) { 592224145Sdim int nargs = argument_count(); 593224145Sdim DepArgument args[max_arg_count]; 594224145Sdim for (int j = 0; j < nargs; j++) { 595224145Sdim args[j] = argument(j); 596224145Sdim } 597224145Sdim Dependencies::print_dependency(type(), nargs, args, witness); 598224145Sdim if (verbose) { 599224145Sdim if (_code != NULL) { 600224145Sdim tty->print(" code: "); 601249423Sdim _code->print_value_on(tty); 602249423Sdim tty->cr(); 603249423Sdim } 604249423Sdim } 605224145Sdim} 606224145Sdim 607224145Sdim 608224145Sdim/// Dependency stream support (decodes dependencies from an nmethod): 609224145Sdim 610234353Sdim#ifdef ASSERT 611234353Sdimvoid Dependencies::DepStream::initial_asserts(size_t byte_limit) { 612234353Sdim assert(must_be_in_vm(), "raw oops here"); 613234353Sdim _byte_limit = byte_limit; 614234353Sdim _type = (DepType)(end_marker-1); // defeat "already at end" assert 615234353Sdim assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other"); 616224145Sdim} 617234353Sdim#endif //ASSERT 618226633Sdim 619224145Sdimbool Dependencies::DepStream::next() { 620224145Sdim assert(_type != end_marker, "already at end"); 621234353Sdim if (_bytes.position() == 0 && _code != NULL 622224145Sdim && _code->dependencies_size() == 0) { 623224145Sdim // Method has no dependencies at all. 624224145Sdim return false; 625234353Sdim } 626226633Sdim int code_byte = (_bytes.read_byte() & 0xFF); 627224145Sdim if (code_byte == end_marker) { 628234353Sdim DEBUG_ONLY(_type = end_marker); 629234353Sdim return false; 630234353Sdim } else { 631234353Sdim int ctxk_bit = (code_byte & Dependencies::default_context_type_bit); 632224145Sdim code_byte -= ctxk_bit; 633234353Sdim DepType dept = (DepType)code_byte; 634234353Sdim _type = dept; 635234353Sdim Dependencies::check_valid_dependency_type(dept); 636224145Sdim int stride = _dep_args[dept]; 637224145Sdim assert(stride == dep_args(dept), "sanity"); 638224145Sdim int skipj = -1; 639224145Sdim if (ctxk_bit != 0) { 640234353Sdim skipj = 0; // currently the only context argument is at zero 641234353Sdim assert(skipj == dep_context_arg(dept), "zero arg always ctxk"); 642234353Sdim } 643234353Sdim for (int j = 0; j < stride; j++) { 644224145Sdim _xi[j] = (j == skipj)? 0: _bytes.read_int(); 645224145Sdim } 646224145Sdim DEBUG_ONLY(_xi[stride] = -1); // help detect overruns 647224145Sdim return true; 648224145Sdim } 649224145Sdim} 650224145Sdim 651224145Sdiminline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) { 652224145Sdim Metadata* o = NULL; 653224145Sdim if (_code != NULL) { 654224145Sdim o = _code->metadata_at(i); 655224145Sdim } else { 656224145Sdim o = _deps->oop_recorder()->metadata_at(i); 657223013Sdim } 658223013Sdim assert(o == NULL || o->is_metadata(), 659223013Sdim err_msg("Should be perm " PTR_FORMAT, o)); 660224145Sdim return o; 661239462Sdim} 662239462Sdim 663239462Sdiminline oop Dependencies::DepStream::recorded_oop_at(int i) { 664239462Sdim return (_code != NULL) 665223013Sdim ? _code->oop_at(i) 666223013Sdim : JNIHandles::resolve(_deps->oop_recorder()->oop_at(i)); 667223013Sdim} 668249423Sdim 669249423SdimMetadata* Dependencies::DepStream::argument(int i) { 670249423Sdim Metadata* result = recorded_metadata_at(argument_index(i)); 671223013Sdim assert(result == NULL || result->is_klass() || result->is_method(), "must be"); 672223013Sdim return result; 673223013Sdim} 674223013Sdim 675223013Sdimoop Dependencies::DepStream::argument_oop(int i) { 676223013Sdim oop result = recorded_oop_at(argument_index(i)); 677243830Sdim assert(result == NULL || result->is_oop(), "must be"); 678243830Sdim return result; 679223013Sdim} 680223013Sdim 681223013SdimKlass* Dependencies::DepStream::context_type() { 682223013Sdim assert(must_be_in_vm(), "raw oops here"); 683226633Sdim 684226633Sdim // Most dependencies have an explicit context type argument. 685226633Sdim { 686226633Sdim int ctxkj = dep_context_arg(_type); // -1 if no explicit context arg 687226633Sdim if (ctxkj >= 0) { 688224145Sdim Metadata* k = argument(ctxkj); 689226633Sdim if (k != NULL) { // context type was not compressed away 690226633Sdim assert(k->is_klass(), "type check"); 691239462Sdim return (Klass*) k; 692239462Sdim } 693239462Sdim // recompute "default" context type 694226633Sdim return ctxk_encoded_as_null(_type, argument(ctxkj+1)); 695224145Sdim } 696224145Sdim } 697224145Sdim 698226633Sdim // Some dependencies are using the klass of the first object 699243830Sdim // argument as implicit context type (e.g. call_site_target_value). 700226633Sdim { 701224145Sdim int ctxkj = dep_implicit_context_arg(_type); 702224145Sdim if (ctxkj >= 0) { 703224145Sdim Klass* k = argument_oop(ctxkj)->klass(); 704224145Sdim assert(k->is_klass(), "type check"); 705224145Sdim return (Klass*) k; 706243830Sdim } 707224145Sdim } 708224145Sdim 709223013Sdim // And some dependencies don't have a context type at all, 710223013Sdim // e.g. evol_method. 711223013Sdim return NULL; 712223013Sdim} 713223013Sdim 714223013Sdim/// Checking dependencies: 715223013Sdim 716223013Sdim// This hierarchy walker inspects subtypes of a given type, 717223013Sdim// trying to find a "bad" class which breaks a dependency. 718223013Sdim// Such a class is called a "witness" to the broken dependency. 719234353Sdim// While searching around, we ignore "participants", which 720223013Sdim// are already known to the dependency. 721223013Sdimclass ClassHierarchyWalker { 722226633Sdim public: 723226633Sdim enum { PARTICIPANT_LIMIT = 3 }; 724226633Sdim 725239462Sdim private: 726239462Sdim // optional method descriptor to check for: 727226633Sdim Symbol* _name; 728226633Sdim Symbol* _signature; 729226633Sdim 730239462Sdim // special classes which are not allowed to be witnesses: 731226633Sdim Klass* _participants[PARTICIPANT_LIMIT+1]; 732226633Sdim int _num_participants; 733226633Sdim 734226633Sdim // cache of method lookups 735226633Sdim Method* _found_methods[PARTICIPANT_LIMIT+1]; 736239462Sdim 737239462Sdim // if non-zero, tells how many witnesses to convert to participants 738239462Sdim int _record_witnesses; 739226633Sdim 740226633Sdim void initialize(Klass* participant) { 741226633Sdim _record_witnesses = 0; 742226633Sdim _participants[0] = participant; 743226633Sdim _found_methods[0] = NULL; 744226633Sdim _num_participants = 0; 745226633Sdim if (participant != NULL) { 746226633Sdim // Terminating NULL. 747226633Sdim _participants[1] = NULL; 748226633Sdim _found_methods[1] = NULL; 749226633Sdim _num_participants = 1; 750226633Sdim } 751226633Sdim } 752226633Sdim 753226633Sdim void initialize_from_method(Method* m) { 754226633Sdim assert(m != NULL && m->is_method(), "sanity"); 755226633Sdim _name = m->name(); 756226633Sdim _signature = m->signature(); 757226633Sdim } 758226633Sdim 759226633Sdim public: 760226633Sdim // The walker is initialized to recognize certain methods and/or types 761226633Sdim // as friendly participants. 762226633Sdim ClassHierarchyWalker(Klass* participant, Method* m) { 763226633Sdim initialize_from_method(m); 764226633Sdim initialize(participant); 765226633Sdim } 766224145Sdim ClassHierarchyWalker(Method* m) { 767224145Sdim initialize_from_method(m); 768224145Sdim initialize(NULL); 769224145Sdim } 770226633Sdim ClassHierarchyWalker(Klass* participant = NULL) { 771226633Sdim _name = NULL; 772226633Sdim _signature = NULL; 773226633Sdim initialize(participant); 774226633Sdim } 775226633Sdim 776226633Sdim // This is common code for two searches: One for concrete subtypes, 777226633Sdim // the other for concrete method implementations and overrides. 778226633Sdim bool doing_subtype_search() { 779226633Sdim return _name == NULL; 780226633Sdim } 781226633Sdim 782226633Sdim int num_participants() { return _num_participants; } 783226633Sdim Klass* participant(int n) { 784226633Sdim assert((uint)n <= (uint)_num_participants, "oob"); 785226633Sdim return _participants[n]; 786226633Sdim } 787226633Sdim 788226633Sdim // Note: If n==num_participants, returns NULL. 789226633Sdim Method* found_method(int n) { 790226633Sdim assert((uint)n <= (uint)_num_participants, "oob"); 791226633Sdim Method* fm = _found_methods[n]; 792224145Sdim assert(n == _num_participants || fm != NULL, "proper usage"); 793224145Sdim assert(fm == NULL || fm->method_holder() == _participants[n], "sanity"); 794224145Sdim return fm; 795224145Sdim } 796224145Sdim 797224145Sdim#ifdef ASSERT 798224145Sdim // Assert that m is inherited into ctxk, without intervening overrides. 799224145Sdim // (May return true even if this is not true, in corner cases where we punt.) 800224145Sdim bool check_method_context(Klass* ctxk, Method* m) { 801226633Sdim if (m->method_holder() == ctxk) 802226633Sdim return true; // Quick win. 803226633Sdim if (m->is_private()) 804226633Sdim return false; // Quick lose. Should not happen. 805226633Sdim if (!(m->is_public() || m->is_protected())) 806226633Sdim // The override story is complex when packages get involved. 807224145Sdim return true; // Must punt the assertion to true. 808224145Sdim Klass* k = Klass::cast(ctxk); 809224145Sdim Method* lm = k->lookup_method(m->name(), m->signature()); 810226633Sdim if (lm == NULL && k->oop_is_instance()) { 811226633Sdim // It might be an abstract interface method, devoid of mirandas. 812226633Sdim lm = ((InstanceKlass*)k)->lookup_method_in_all_interfaces(m->name(), 813226633Sdim m->signature()); 814226633Sdim } 815226633Sdim if (lm == m) 816263508Sdim // Method m is inherited into ctxk. 817263508Sdim return true; 818263508Sdim if (lm != NULL) { 819263508Sdim if (!(lm->is_public() || lm->is_protected())) { 820226633Sdim // Method is [package-]private, so the override story is complex. 821226633Sdim return true; // Must punt the assertion to true. 822226633Sdim } 823226633Sdim if (lm->is_static()) { 824226633Sdim // Static methods don't override non-static so punt 825226633Sdim return true; 826226633Sdim } 827226633Sdim if ( !Dependencies::is_concrete_method(lm) 828226633Sdim && !Dependencies::is_concrete_method(m) 829226633Sdim && Klass::cast(lm->method_holder())->is_subtype_of(m->method_holder())) 830226633Sdim // Method m is overridden by lm, but both are non-concrete. 831226633Sdim return true; 832226633Sdim } 833226633Sdim ResourceMark rm; 834226633Sdim tty->print_cr("Dependency method not found in the associated context:"); 835239462Sdim tty->print_cr(" context = %s", Klass::cast(ctxk)->external_name()); 836239462Sdim tty->print( " method = "); m->print_short_name(tty); tty->cr(); 837239462Sdim if (lm != NULL) { 838239462Sdim tty->print( " found = "); lm->print_short_name(tty); tty->cr(); 839239462Sdim } 840239462Sdim return false; 841239462Sdim } 842226633Sdim#endif 843234353Sdim 844223013Sdim void add_participant(Klass* participant) { 845223013Sdim assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob"); 846226633Sdim int np = _num_participants++; 847226633Sdim _participants[np] = participant; 848226633Sdim _participants[np+1] = NULL; 849226633Sdim _found_methods[np+1] = NULL; 850226633Sdim } 851226633Sdim 852226633Sdim void record_witnesses(int add) { 853226633Sdim if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT; 854226633Sdim assert(_num_participants + add < PARTICIPANT_LIMIT, "oob"); 855226633Sdim _record_witnesses = add; 856226633Sdim } 857226633Sdim 858226633Sdim bool is_witness(Klass* k) { 859226633Sdim if (doing_subtype_search()) { 860226633Sdim return Dependencies::is_concrete_klass(k); 861226633Sdim } else { 862226633Sdim Method* m = InstanceKlass::cast(k)->find_method(_name, _signature); 863226633Sdim if (m == NULL || !Dependencies::is_concrete_method(m)) return false; 864226633Sdim _found_methods[_num_participants] = m; 865226633Sdim // Note: If add_participant(k) is called, 866226633Sdim // the method m will already be memoized for it. 867226633Sdim return true; 868226633Sdim } 869226633Sdim } 870226633Sdim 871226633Sdim bool is_participant(Klass* k) { 872226633Sdim if (k == _participants[0]) { 873226633Sdim return true; 874226633Sdim } else if (_num_participants <= 1) { 875226633Sdim return false; 876239462Sdim } else { 877226633Sdim return in_list(k, &_participants[1]); 878226633Sdim } 879226633Sdim } 880226633Sdim bool ignore_witness(Klass* witness) { 881226633Sdim if (_record_witnesses == 0) { 882226633Sdim return false; 883226633Sdim } else { 884226633Sdim --_record_witnesses; 885226633Sdim add_participant(witness); 886226633Sdim return true; 887226633Sdim } 888226633Sdim } 889226633Sdim static bool in_list(Klass* x, Klass** list) { 890226633Sdim for (int i = 0; ; i++) { 891226633Sdim Klass* y = list[i]; 892226633Sdim if (y == NULL) break; 893226633Sdim if (y == x) return true; 894226633Sdim } 895226633Sdim return false; // not in list 896226633Sdim } 897226633Sdim 898226633Sdim private: 899234353Sdim // the actual search method: 900234353Sdim Klass* find_witness_anywhere(Klass* context_type, 901234353Sdim bool participants_hide_witnesses, 902234353Sdim bool top_level_call = true); 903234353Sdim // the spot-checking version: 904234353Sdim Klass* find_witness_in(KlassDepChange& changes, 905234353Sdim Klass* context_type, 906234353Sdim bool participants_hide_witnesses); 907234353Sdim public: 908234353Sdim Klass* find_witness_subtype(Klass* context_type, KlassDepChange* changes = NULL) { 909234353Sdim assert(doing_subtype_search(), "must set up a subtype search"); 910234353Sdim // When looking for unexpected concrete types, 911234353Sdim // do not look beneath expected ones. 912234353Sdim const bool participants_hide_witnesses = true; 913234353Sdim // CX > CC > C' is OK, even if C' is new. 914234353Sdim // CX > { CC, C' } is not OK if C' is new, and C' is the witness. 915234353Sdim if (changes != NULL) { 916234353Sdim return find_witness_in(*changes, context_type, participants_hide_witnesses); 917234353Sdim } else { 918234353Sdim return find_witness_anywhere(context_type, participants_hide_witnesses); 919234353Sdim } 920234353Sdim } 921234353Sdim Klass* find_witness_definer(Klass* context_type, KlassDepChange* changes = NULL) { 922234353Sdim assert(!doing_subtype_search(), "must set up a method definer search"); 923223013Sdim // When looking for unexpected concrete methods, 924223013Sdim // look beneath expected ones, to see if there are overrides. 925223013Sdim const bool participants_hide_witnesses = true; 926223013Sdim // CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness. 927239462Sdim if (changes != NULL) { 928224145Sdim return find_witness_in(*changes, context_type, !participants_hide_witnesses); 929224145Sdim } else { 930234353Sdim return find_witness_anywhere(context_type, !participants_hide_witnesses); 931224145Sdim } 932224145Sdim } 933223013Sdim}; 934223013Sdim 935234353Sdim#ifndef PRODUCT 936234353Sdimstatic int deps_find_witness_calls = 0; 937234353Sdimstatic int deps_find_witness_steps = 0; 938234353Sdimstatic int deps_find_witness_recursions = 0; 939234353Sdimstatic int deps_find_witness_singles = 0; 940234353Sdimstatic int deps_find_witness_print = 0; // set to -1 to force a final print 941234353Sdimstatic bool count_find_witness_calls() { 942223013Sdim if (TraceDependencies || LogCompilation) { 943223013Sdim int pcount = deps_find_witness_print + 1; 944223013Sdim bool final_stats = (pcount == 0); 945263508Sdim bool initial_call = (pcount == 1); 946223013Sdim bool occasional_print = ((pcount & ((1<<10) - 1)) == 0); 947223013Sdim if (pcount < 0) pcount = 1; // crude overflow protection 948223013Sdim deps_find_witness_print = pcount; 949224145Sdim if (VerifyDependencies && initial_call) { 950224145Sdim tty->print_cr("Warning: TraceDependencies results may be inflated by VerifyDependencies"); 951224145Sdim } 952224145Sdim if (occasional_print || final_stats) { 953224145Sdim // Every now and then dump a little info about dependency searching. 954263508Sdim if (xtty != NULL) { 955263508Sdim ttyLocker ttyl; 956224145Sdim xtty->elem("deps_find_witness calls='%d' steps='%d' recursions='%d' singles='%d'", 957224145Sdim deps_find_witness_calls, 958263508Sdim deps_find_witness_steps, 959263508Sdim deps_find_witness_recursions, 960263508Sdim deps_find_witness_singles); 961263508Sdim } 962263508Sdim if (final_stats || (TraceDependencies && WizardMode)) { 963263508Sdim ttyLocker ttyl; 964224145Sdim tty->print_cr("Dependency check (find_witness) " 965224145Sdim "calls=%d, steps=%d (avg=%.1f), recursions=%d, singles=%d", 966239462Sdim deps_find_witness_calls, 967239462Sdim deps_find_witness_steps, 968239462Sdim (double)deps_find_witness_steps / deps_find_witness_calls, 969239462Sdim deps_find_witness_recursions, 970239462Sdim deps_find_witness_singles); 971243830Sdim } 972243830Sdim } 973243830Sdim return true; 974243830Sdim } 975243830Sdim return false; 976243830Sdim} 977239462Sdim#else 978226633Sdim#define count_find_witness_calls() (0) 979226633Sdim#endif //PRODUCT 980239462Sdim 981226633Sdim 982239462SdimKlass* ClassHierarchyWalker::find_witness_in(KlassDepChange& changes, 983239462Sdim Klass* context_type, 984239462Sdim bool participants_hide_witnesses) { 985239462Sdim assert(changes.involves_context(context_type), "irrelevant dependency"); 986239462Sdim Klass* new_type = changes.new_type(); 987239462Sdim 988239462Sdim count_find_witness_calls(); 989239462Sdim NOT_PRODUCT(deps_find_witness_singles++); 990239462Sdim 991239462Sdim // Current thread must be in VM (not native mode, as in CI): 992234353Sdim assert(must_be_in_vm(), "raw oops here"); 993234353Sdim // Must not move the class hierarchy during this check: 994239462Sdim assert_locked_or_safepoint(Compile_lock); 995234353Sdim 996224145Sdim int nof_impls = InstanceKlass::cast(context_type)->nof_implementors(); 997224145Sdim if (nof_impls > 1) { 998224145Sdim // Avoid this case: *I.m > { A.m, C }; B.m > C 999243830Sdim // %%% Until this is fixed more systematically, bail out. 1000224145Sdim // See corresponding comment in find_witness_anywhere. 1001226633Sdim return context_type; 1002224145Sdim } 1003224145Sdim 1004226633Sdim assert(!is_participant(new_type), "only old classes are participants"); 1005226633Sdim if (participants_hide_witnesses) { 1006226633Sdim // If the new type is a subtype of a participant, we are done. 1007226633Sdim for (int i = 0; i < num_participants(); i++) { 1008226633Sdim Klass* part = participant(i); 1009226633Sdim if (part == NULL) continue; 1010226633Sdim assert(changes.involves_context(part) == Klass::cast(new_type)->is_subtype_of(part), 1011226633Sdim "correct marking of participants, b/c new_type is unique"); 1012226633Sdim if (changes.involves_context(part)) { 1013226633Sdim // new guy is protected from this check by previous participant 1014223013Sdim return NULL; 1015223013Sdim } 1016239462Sdim } 1017239462Sdim } 1018239462Sdim 1019239462Sdim if (is_witness(new_type) && 1020239462Sdim !ignore_witness(new_type)) { 1021239462Sdim return new_type; 1022239462Sdim } 1023239462Sdim 1024239462Sdim return NULL; 1025234353Sdim} 1026234353Sdim 1027234353Sdim 1028234353Sdim// Walk hierarchy under a context type, looking for unexpected types. 1029234353Sdim// Do not report participant types, and recursively walk beneath 1030234353Sdim// them only if participants_hide_witnesses is false. 1031234353Sdim// If top_level_call is false, skip testing the context type, 1032234353Sdim// because the caller has already considered it. 1033234353SdimKlass* ClassHierarchyWalker::find_witness_anywhere(Klass* context_type, 1034223013Sdim bool participants_hide_witnesses, 1035224145Sdim bool top_level_call) { 1036224145Sdim // Current thread must be in VM (not native mode, as in CI): 1037223013Sdim assert(must_be_in_vm(), "raw oops here"); 1038224145Sdim // Must not move the class hierarchy during this check: 1039224145Sdim assert_locked_or_safepoint(Compile_lock); 1040224145Sdim 1041224145Sdim bool do_counts = count_find_witness_calls(); 1042223013Sdim 1043226633Sdim // Check the root of the sub-hierarchy first. 1044226633Sdim if (top_level_call) { 1045226633Sdim if (do_counts) { 1046226633Sdim NOT_PRODUCT(deps_find_witness_calls++); 1047226633Sdim NOT_PRODUCT(deps_find_witness_steps++); 1048226633Sdim } 1049226633Sdim if (is_participant(context_type)) { 1050226633Sdim if (participants_hide_witnesses) return NULL; 1051226633Sdim // else fall through to search loop... 1052226633Sdim } else if (is_witness(context_type) && !ignore_witness(context_type)) { 1053226633Sdim // The context is an abstract class or interface, to start with. 1054226633Sdim return context_type; 1055234353Sdim } 1056234353Sdim } 1057234353Sdim 1058234353Sdim // Now we must check each implementor and each subclass. 1059234353Sdim // Use a short worklist to avoid blowing the stack. 1060234353Sdim // Each worklist entry is a *chain* of subklass siblings to process. 1061234353Sdim const int CHAINMAX = 100; // >= 1 + InstanceKlass::implementors_limit 1062234353Sdim Klass* chains[CHAINMAX]; 1063234353Sdim int chaini = 0; // index into worklist 1064234353Sdim Klass* chain; // scratch variable 1065234353Sdim#define ADD_SUBCLASS_CHAIN(k) { \ 1066234353Sdim assert(chaini < CHAINMAX, "oob"); \ 1067239462Sdim chain = InstanceKlass::cast(k)->subklass(); \ 1068234353Sdim if (chain != NULL) chains[chaini++] = chain; } 1069234353Sdim 1070234353Sdim // Look for non-abstract subclasses. 1071234353Sdim // (Note: Interfaces do not have subclasses.) 1072224145Sdim ADD_SUBCLASS_CHAIN(context_type); 1073224145Sdim 1074224145Sdim // If it is an interface, search its direct implementors. 1075224145Sdim // (Their subclasses are additional indirect implementors. 1076243830Sdim // See InstanceKlass::add_implementor.) 1077223013Sdim // (Note: nof_implementors is always zero for non-interfaces.) 1078223013Sdim int nof_impls = InstanceKlass::cast(context_type)->nof_implementors(); 1079234353Sdim if (nof_impls > 1) { 1080234353Sdim // Avoid this case: *I.m > { A.m, C }; B.m > C 1081234353Sdim // Here, I.m has 2 concrete implementations, but m appears unique 1082223013Sdim // as A.m, because the search misses B.m when checking C. 1083234353Sdim // The inherited method B.m was getting missed by the walker 1084234353Sdim // when interface 'I' was the starting point. 1085223013Sdim // %%% Until this is fixed more systematically, bail out. 1086223013Sdim // (Old CHA had the same limitation.) 1087223013Sdim return context_type; 1088223013Sdim } 1089239462Sdim if (nof_impls > 0) { 1090234353Sdim Klass* impl = InstanceKlass::cast(context_type)->implementor(); 1091223013Sdim assert(impl != NULL, "just checking"); 1092223013Sdim // If impl is the same as the context_type, then more than one 1093223013Sdim // implementor has seen. No exact info in this case. 1094239462Sdim if (impl == context_type) { 1095263508Sdim return context_type; // report an inexact witness to this sad affair 1096239462Sdim } 1097239462Sdim if (do_counts) 1098239462Sdim { NOT_PRODUCT(deps_find_witness_steps++); } 1099239462Sdim if (is_participant(impl)) { 1100239462Sdim if (!participants_hide_witnesses) { 1101239462Sdim ADD_SUBCLASS_CHAIN(impl); 1102239462Sdim } 1103239462Sdim } else if (is_witness(impl) && !ignore_witness(impl)) { 1104239462Sdim return impl; 1105263508Sdim } else { 1106263508Sdim ADD_SUBCLASS_CHAIN(impl); 1107263508Sdim } 1108263508Sdim } 1109263508Sdim 1110239462Sdim // Recursively process each non-trivial sibling chain. 1111239462Sdim while (chaini > 0) { 1112239462Sdim Klass* chain = chains[--chaini]; 1113263508Sdim for (Klass* sub = chain; sub != NULL; sub = sub->next_sibling()) { 1114263508Sdim if (do_counts) { NOT_PRODUCT(deps_find_witness_steps++); } 1115263508Sdim if (is_participant(sub)) { 1116263508Sdim if (participants_hide_witnesses) continue; 1117263508Sdim // else fall through to process this guy's subclasses 1118239462Sdim } else if (is_witness(sub) && !ignore_witness(sub)) { 1119263508Sdim return sub; 1120263508Sdim } 1121263508Sdim if (chaini < (VerifyDependencies? 2: CHAINMAX)) { 1122263508Sdim // Fast path. (Partially disabled if VerifyDependencies.) 1123239462Sdim ADD_SUBCLASS_CHAIN(sub); 1124239462Sdim } else { 1125223013Sdim // Worklist overflow. Do a recursive call. Should be rare. 1126239462Sdim // The recursive call will have its own worklist, of course. 1127239462Sdim // (Note that sub has already been tested, so that there is 1128239462Sdim // no need for the recursive call to re-test. That's handy, 1129239462Sdim // since the recursive call sees sub as the context_type.) 1130223013Sdim if (do_counts) { NOT_PRODUCT(deps_find_witness_recursions++); } 1131224145Sdim Klass* witness = find_witness_anywhere(sub, 1132239462Sdim participants_hide_witnesses, 1133239462Sdim /*top_level_call=*/ false); 1134239462Sdim if (witness != NULL) return witness; 1135239462Sdim } 1136239462Sdim } 1137239462Sdim } 1138224145Sdim 1139223013Sdim // No witness found. The dependency remains unbroken. 1140223013Sdim return NULL; 1141234353Sdim#undef ADD_SUBCLASS_CHAIN 1142223013Sdim} 1143223013Sdim 1144223013Sdim 1145223013Sdimbool Dependencies::is_concrete_klass(Klass* k) { 1146224145Sdim if (Klass::cast(k)->is_abstract()) return false; 1147223013Sdim // %%% We could treat classes which are concrete but 1148223013Sdim // have not yet been instantiated as virtually abstract. 1149223013Sdim // This would require a deoptimization barrier on first instantiation. 1150239462Sdim //if (k->is_not_instantiated()) return false; 1151223013Sdim return true; 1152223013Sdim} 1153223013Sdim 1154223013Sdimbool Dependencies::is_concrete_method(Method* m) { 1155223013Sdim // Statics are irrelevant to virtual call sites. 1156239462Sdim if (m->is_static()) return false; 1157239462Sdim 1158239462Sdim // We could also return false if m does not yet appear to be 1159239462Sdim // executed, if the VM version supports this distinction also. 1160239462Sdim return !m->is_abstract(); 1161239462Sdim} 1162239462Sdim 1163239462Sdim 1164239462SdimKlass* Dependencies::find_finalizable_subclass(Klass* k) { 1165223013Sdim if (k->is_interface()) return NULL; 1166223013Sdim if (k->has_finalizer()) return k; 1167223013Sdim k = k->subklass(); 1168243830Sdim while (k != NULL) { 1169223013Sdim Klass* result = find_finalizable_subclass(k); 1170243830Sdim if (result != NULL) return result; 1171243830Sdim k = k->next_sibling(); 1172243830Sdim } 1173243830Sdim return NULL; 1174243830Sdim} 1175243830Sdim 1176243830Sdim 1177243830Sdimbool Dependencies::is_concrete_klass(ciInstanceKlass* k) { 1178243830Sdim if (k->is_abstract()) return false; 1179243830Sdim // We could also return false if k does not yet appear to be 1180263508Sdim // instantiated, if the VM version supports this distinction also. 1181263508Sdim //if (k->is_not_instantiated()) return false; 1182243830Sdim return true; 1183243830Sdim} 1184243830Sdim 1185243830Sdimbool Dependencies::is_concrete_method(ciMethod* m) { 1186243830Sdim // Statics are irrelevant to virtual call sites. 1187249423Sdim if (m->is_static()) return false; 1188249423Sdim 1189249423Sdim // We could also return false if m does not yet appear to be 1190249423Sdim // executed, if the VM version supports this distinction also. 1191249423Sdim return !m->is_abstract(); 1192249423Sdim} 1193263508Sdim 1194263508Sdim 1195263508Sdimbool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) { 1196263508Sdim return k->has_finalizable_subclass(); 1197263508Sdim} 1198263508Sdim 1199243830Sdim 1200243830Sdim// Any use of the contents (bytecodes) of a method must be 1201243830Sdim// marked by an "evol_method" dependency, if those contents 1202243830Sdim// can change. (Note: A method is always dependent on itself.) 1203243830SdimKlass* Dependencies::check_evol_method(Method* m) { 1204243830Sdim assert(must_be_in_vm(), "raw oops here"); 1205243830Sdim // Did somebody do a JVMTI RedefineClasses while our backs were turned? 1206243830Sdim // Or is there a now a breakpoint? 1207243830Sdim // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.) 1208263508Sdim if (m->is_old() 1209263508Sdim || m->number_of_breakpoints() > 0) { 1210263508Sdim return m->method_holder(); 1211263508Sdim } else { 1212263508Sdim return NULL; 1213263508Sdim } 1214263508Sdim} 1215234353Sdim 1216234353Sdim// This is a strong assertion: It is that the given type 1217234353Sdim// has no subtypes whatever. It is most useful for 1218234353Sdim// optimizing checks on reflected types or on array types. 1219234353Sdim// (Checks on types which are derived from real instances 1220234353Sdim// can be optimized more strongly than this, because we 1221234353Sdim// know that the checked type comes from a concrete type, 1222234353Sdim// and therefore we can disregard abstract types.) 1223234353SdimKlass* Dependencies::check_leaf_type(Klass* ctxk) { 1224234353Sdim assert(must_be_in_vm(), "raw oops here"); 1225234353Sdim assert_locked_or_safepoint(Compile_lock); 1226234353Sdim InstanceKlass* ctx = InstanceKlass::cast(ctxk); 1227234353Sdim Klass* sub = ctx->subklass(); 1228234353Sdim if (sub != NULL) { 1229234353Sdim return sub; 1230234353Sdim } else if (ctx->nof_implementors() != 0) { 1231234353Sdim // if it is an interface, it must be unimplemented 1232234353Sdim // (if it is not an interface, nof_implementors is always zero) 1233234353Sdim Klass* impl = ctx->implementor(); 1234234353Sdim assert(impl != NULL, "must be set"); 1235234353Sdim return impl; 1236234353Sdim } else { 1237234353Sdim return NULL; 1238234353Sdim } 1239234353Sdim} 1240234353Sdim 1241234353Sdim// Test the assertion that conck is the only concrete subtype* of ctxk. 1242234353Sdim// The type conck itself is allowed to have have further concrete subtypes. 1243234353Sdim// This allows the compiler to narrow occurrences of ctxk by conck, 1244234353Sdim// when dealing with the types of actual instances. 1245234353SdimKlass* Dependencies::check_abstract_with_unique_concrete_subtype(Klass* ctxk, 1246234353Sdim Klass* conck, 1247234353Sdim KlassDepChange* changes) { 1248234353Sdim ClassHierarchyWalker wf(conck); 1249234353Sdim return wf.find_witness_subtype(ctxk, changes); 1250234353Sdim} 1251234353Sdim 1252234353Sdim// If a non-concrete class has no concrete subtypes, it is not (yet) 1253234353Sdim// instantiatable. This can allow the compiler to make some paths go 1254234353Sdim// dead, if they are gated by a test of the type. 1255234353SdimKlass* Dependencies::check_abstract_with_no_concrete_subtype(Klass* ctxk, 1256234353Sdim KlassDepChange* changes) { 1257234353Sdim // Find any concrete subtype, with no participants: 1258234353Sdim ClassHierarchyWalker wf; 1259234353Sdim return wf.find_witness_subtype(ctxk, changes); 1260234353Sdim} 1261234353Sdim 1262234353Sdim 1263234353Sdim// If a concrete class has no concrete subtypes, it can always be 1264234353Sdim// exactly typed. This allows the use of a cheaper type test. 1265234353SdimKlass* Dependencies::check_concrete_with_no_concrete_subtype(Klass* ctxk, 1266234353Sdim KlassDepChange* changes) { 1267234353Sdim // Find any concrete subtype, with only the ctxk as participant: 1268234353Sdim ClassHierarchyWalker wf(ctxk); 1269234353Sdim return wf.find_witness_subtype(ctxk, changes); 1270234353Sdim} 1271234353Sdim 1272234353Sdim 1273234353Sdim// Find the unique concrete proper subtype of ctxk, or NULL if there 1274234353Sdim// is more than one concrete proper subtype. If there are no concrete 1275234353Sdim// proper subtypes, return ctxk itself, whether it is concrete or not. 1276234353Sdim// The returned subtype is allowed to have have further concrete subtypes. 1277234353Sdim// That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }. 1278234353SdimKlass* Dependencies::find_unique_concrete_subtype(Klass* ctxk) { 1279223013Sdim ClassHierarchyWalker wf(ctxk); // Ignore ctxk when walking. 1280234353Sdim wf.record_witnesses(1); // Record one other witness when walking. 1281234353Sdim Klass* wit = wf.find_witness_subtype(ctxk); 1282234353Sdim if (wit != NULL) return NULL; // Too many witnesses. 1283234353Sdim Klass* conck = wf.participant(0); 1284234353Sdim if (conck == NULL) { 1285234353Sdim#ifndef PRODUCT 1286234353Sdim // Make sure the dependency mechanism will pass this discovery: 1287234353Sdim if (VerifyDependencies) { 1288234353Sdim // Turn off dependency tracing while actually testing deps. 1289234353Sdim FlagSetting fs(TraceDependencies, false); 1290234353Sdim if (!Dependencies::is_concrete_klass(ctxk)) { 1291234353Sdim guarantee(NULL == 1292234353Sdim (void *)check_abstract_with_no_concrete_subtype(ctxk), 1293234353Sdim "verify dep."); 1294234353Sdim } else { 1295234353Sdim guarantee(NULL == 1296234353Sdim (void *)check_concrete_with_no_concrete_subtype(ctxk), 1297234353Sdim "verify dep."); 1298234353Sdim } 1299234353Sdim } 1300234353Sdim#endif //PRODUCT 1301234353Sdim return ctxk; // Return ctxk as a flag for "no subtypes". 1302234353Sdim } else { 1303234353Sdim#ifndef PRODUCT 1304234353Sdim // Make sure the dependency mechanism will pass this discovery: 1305234353Sdim if (VerifyDependencies) { 1306234353Sdim // Turn off dependency tracing while actually testing deps. 1307234353Sdim FlagSetting fs(TraceDependencies, false); 1308223013Sdim if (!Dependencies::is_concrete_klass(ctxk)) { 1309223013Sdim guarantee(NULL == (void *) 1310234353Sdim check_abstract_with_unique_concrete_subtype(ctxk, conck), 1311234353Sdim "verify dep."); 1312234353Sdim } 1313234353Sdim } 1314234353Sdim#endif //PRODUCT 1315234353Sdim return conck; 1316234353Sdim } 1317234353Sdim} 1318234353Sdim 1319234353Sdim// Test the assertion that the k[12] are the only concrete subtypes of ctxk, 1320234353Sdim// except possibly for further subtypes of k[12] themselves. 1321234353Sdim// The context type must be abstract. The types k1 and k2 are themselves 1322234353Sdim// allowed to have further concrete subtypes. 1323234353SdimKlass* Dependencies::check_abstract_with_exclusive_concrete_subtypes( 1324234353Sdim Klass* ctxk, 1325234353Sdim Klass* k1, 1326234353Sdim Klass* k2, 1327239462Sdim KlassDepChange* changes) { 1328234353Sdim ClassHierarchyWalker wf; 1329234353Sdim wf.add_participant(k1); 1330234353Sdim wf.add_participant(k2); 1331234353Sdim return wf.find_witness_subtype(ctxk, changes); 1332234353Sdim} 1333234353Sdim 1334234353Sdim// Search ctxk for concrete implementations. If there are klen or fewer, 1335234353Sdim// pack them into the given array and return the number. 1336263508Sdim// Otherwise, return -1, meaning the given array would overflow. 1337263508Sdim// (Note that a return of 0 means there are exactly no concrete subtypes.) 1338263508Sdim// In this search, if ctxk is concrete, it will be reported alone. 1339263508Sdim// For any type CC reported, no proper subtypes of CC will be reported. 1340263508Sdimint Dependencies::find_exclusive_concrete_subtypes(Klass* ctxk, 1341263508Sdim int klen, 1342263508Sdim Klass* karray[]) { 1343263508Sdim ClassHierarchyWalker wf; 1344263508Sdim wf.record_witnesses(klen); 1345263508Sdim Klass* wit = wf.find_witness_subtype(ctxk); 1346263508Sdim if (wit != NULL) return -1; // Too many witnesses. 1347263508Sdim int num = wf.num_participants(); 1348234353Sdim assert(num <= klen, "oob"); 1349234353Sdim // Pack the result array with the good news. 1350234353Sdim for (int i = 0; i < num; i++) 1351234353Sdim karray[i] = wf.participant(i); 1352234353Sdim#ifndef PRODUCT 1353234353Sdim // Make sure the dependency mechanism will pass this discovery: 1354234353Sdim if (VerifyDependencies) { 1355234353Sdim // Turn off dependency tracing while actually testing deps. 1356234353Sdim FlagSetting fs(TraceDependencies, false); 1357234353Sdim switch (Dependencies::is_concrete_klass(ctxk)? -1: num) { 1358234353Sdim case -1: // ctxk was itself concrete 1359234353Sdim guarantee(num == 1 && karray[0] == ctxk, "verify dep."); 1360234353Sdim break; 1361234353Sdim case 0: 1362234353Sdim guarantee(NULL == (void *)check_abstract_with_no_concrete_subtype(ctxk), 1363234353Sdim "verify dep."); 1364234353Sdim break; 1365234353Sdim case 1: 1366234353Sdim guarantee(NULL == (void *) 1367234353Sdim check_abstract_with_unique_concrete_subtype(ctxk, karray[0]), 1368234353Sdim "verify dep."); 1369234353Sdim break; 1370234353Sdim case 2: 1371234353Sdim guarantee(NULL == (void *) 1372239462Sdim check_abstract_with_exclusive_concrete_subtypes(ctxk, 1373234353Sdim karray[0], 1374234353Sdim karray[1]), 1375234353Sdim "verify dep."); 1376239462Sdim break; 1377239462Sdim default: 1378239462Sdim ShouldNotReachHere(); // klen > 2 yet supported 1379234353Sdim } 1380234353Sdim } 1381234353Sdim#endif //PRODUCT 1382234353Sdim return num; 1383234353Sdim} 1384234353Sdim 1385239462Sdim// If a class (or interface) has a unique concrete method uniqm, return NULL. 1386239462Sdim// Otherwise, return a class that contains an interfering method. 1387234353SdimKlass* Dependencies::check_unique_concrete_method(Klass* ctxk, Method* uniqm, 1388234353Sdim KlassDepChange* changes) { 1389234353Sdim // Here is a missing optimization: If uniqm->is_final(), 1390234353Sdim // we don't really need to search beneath it for overrides. 1391234353Sdim // This is probably not important, since we don't use dependencies 1392234353Sdim // to track final methods. (They can't be "definalized".) 1393234353Sdim ClassHierarchyWalker wf(uniqm->method_holder(), uniqm); 1394234353Sdim return wf.find_witness_definer(ctxk, changes); 1395234353Sdim} 1396234353Sdim 1397234353Sdim// Find the set of all non-abstract methods under ctxk that match m. 1398234353Sdim// (The method m must be defined or inherited in ctxk.) 1399234353Sdim// Include m itself in the set, unless it is abstract. 1400234353Sdim// If this set has exactly one element, return that element. 1401234353SdimMethod* Dependencies::find_unique_concrete_method(Klass* ctxk, Method* m) { 1402234353Sdim ClassHierarchyWalker wf(m); 1403234353Sdim assert(wf.check_method_context(ctxk, m), "proper context"); 1404234353Sdim wf.record_witnesses(1); 1405234353Sdim Klass* wit = wf.find_witness_definer(ctxk); 1406234353Sdim if (wit != NULL) return NULL; // Too many witnesses. 1407234353Sdim Method* fm = wf.found_method(0); // Will be NULL if num_parts == 0. 1408234353Sdim if (Dependencies::is_concrete_method(m)) { 1409234353Sdim if (fm == NULL) { 1410234353Sdim // It turns out that m was always the only implementation. 1411234353Sdim fm = m; 1412234353Sdim } else if (fm != m) { 1413234353Sdim // Two conflicting implementations after all. 1414234353Sdim // (This can happen if m is inherited into ctxk and fm overrides it.) 1415234353Sdim return NULL; 1416234353Sdim } 1417234353Sdim } 1418234353Sdim#ifndef PRODUCT 1419234353Sdim // Make sure the dependency mechanism will pass this discovery: 1420234353Sdim if (VerifyDependencies && fm != NULL) { 1421234353Sdim guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm), 1422234353Sdim "verify dep."); 1423234353Sdim } 1424234353Sdim#endif //PRODUCT 1425234353Sdim return fm; 1426234353Sdim} 1427234353Sdim 1428234353SdimKlass* Dependencies::check_exclusive_concrete_methods(Klass* ctxk, 1429234353Sdim Method* m1, 1430234353Sdim Method* m2, 1431234353Sdim KlassDepChange* changes) { 1432234353Sdim ClassHierarchyWalker wf(m1); 1433234353Sdim wf.add_participant(m1->method_holder()); 1434234353Sdim wf.add_participant(m2->method_holder()); 1435234353Sdim return wf.find_witness_definer(ctxk, changes); 1436234353Sdim} 1437234353Sdim 1438234353Sdim// Find the set of all non-abstract methods under ctxk that match m[0]. 1439234353Sdim// (The method m[0] must be defined or inherited in ctxk.) 1440234353Sdim// Include m itself in the set, unless it is abstract. 1441234353Sdim// Fill the given array m[0..(mlen-1)] with this set, and return the length. 1442234353Sdim// (The length may be zero if no concrete methods are found anywhere.) 1443234353Sdim// If there are too many concrete methods to fit in marray, return -1. 1444234353Sdimint Dependencies::find_exclusive_concrete_methods(Klass* ctxk, 1445234353Sdim int mlen, 1446239462Sdim Method* marray[]) { 1447239462Sdim Method* m0 = marray[0]; 1448239462Sdim ClassHierarchyWalker wf(m0); 1449234353Sdim assert(wf.check_method_context(ctxk, m0), "proper context"); 1450234353Sdim wf.record_witnesses(mlen); 1451234353Sdim bool participants_hide_witnesses = true; 1452234353Sdim Klass* wit = wf.find_witness_definer(ctxk); 1453234353Sdim if (wit != NULL) return -1; // Too many witnesses. 1454234353Sdim int num = wf.num_participants(); 1455234353Sdim assert(num <= mlen, "oob"); 1456234353Sdim // Keep track of whether m is also part of the result set. 1457234353Sdim int mfill = 0; 1458234353Sdim assert(marray[mfill] == m0, "sanity"); 1459234353Sdim if (Dependencies::is_concrete_method(m0)) 1460234353Sdim mfill++; // keep m0 as marray[0], the first result 1461234353Sdim for (int i = 0; i < num; i++) { 1462234353Sdim Method* fm = wf.found_method(i); 1463234353Sdim if (fm == m0) continue; // Already put this guy in the list. 1464234353Sdim if (mfill == mlen) { 1465234353Sdim return -1; // Oops. Too many methods after all! 1466234353Sdim } 1467234353Sdim marray[mfill++] = fm; 1468234353Sdim } 1469234353Sdim#ifndef PRODUCT 1470234353Sdim // Make sure the dependency mechanism will pass this discovery: 1471234353Sdim if (VerifyDependencies) { 1472234353Sdim // Turn off dependency tracing while actually testing deps. 1473234353Sdim FlagSetting fs(TraceDependencies, false); 1474263508Sdim switch (mfill) { 1475263508Sdim case 1: 1476263508Sdim guarantee(NULL == (void *)check_unique_concrete_method(ctxk, marray[0]), 1477263508Sdim "verify dep."); 1478263508Sdim break; 1479263508Sdim case 2: 1480263508Sdim guarantee(NULL == (void *) 1481263508Sdim check_exclusive_concrete_methods(ctxk, marray[0], marray[1]), 1482263508Sdim "verify dep."); 1483263508Sdim break; 1484263508Sdim default: 1485263508Sdim ShouldNotReachHere(); // mlen > 2 yet supported 1486263508Sdim } 1487263508Sdim } 1488263508Sdim#endif //PRODUCT 1489263508Sdim return mfill; 1490263508Sdim} 1491234353Sdim 1492234353Sdim 1493234353SdimKlass* Dependencies::check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes) { 1494234353Sdim Klass* search_at = ctxk; 1495234353Sdim if (changes != NULL) 1496234353Sdim search_at = changes->new_type(); // just look at the new bit 1497234353Sdim return find_finalizable_subclass(search_at); 1498234353Sdim} 1499234353Sdim 1500234353Sdim 1501234353SdimKlass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) { 1502234353Sdim assert(call_site ->is_a(SystemDictionary::CallSite_klass()), "sanity"); 1503234353Sdim assert(method_handle->is_a(SystemDictionary::MethodHandle_klass()), "sanity"); 1504263508Sdim if (changes == NULL) { 1505234353Sdim // Validate all CallSites 1506234353Sdim if (java_lang_invoke_CallSite::target(call_site) != method_handle) 1507263508Sdim return call_site->klass(); // assertion failed 1508263508Sdim } else { 1509263508Sdim // Validate the given CallSite 1510263508Sdim if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) { 1511263508Sdim assert(method_handle != changes->method_handle(), "must be"); 1512234353Sdim return call_site->klass(); // assertion failed 1513234353Sdim } 1514234353Sdim } 1515234353Sdim return NULL; // assertion still valid 1516234353Sdim} 1517234353Sdim 1518234353Sdim 1519234353Sdimvoid Dependencies::DepStream::trace_and_log_witness(Klass* witness) { 1520234353Sdim if (witness != NULL) { 1521234353Sdim if (TraceDependencies) { 1522234353Sdim print_dependency(witness, /*verbose=*/ true); 1523234353Sdim } 1524234353Sdim // The following is a no-op unless logging is enabled: 1525234353Sdim log_dependency(witness); 1526234353Sdim } 1527234353Sdim} 1528234353Sdim 1529234353Sdim 1530234353SdimKlass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) { 1531234353Sdim assert_locked_or_safepoint(Compile_lock); 1532234353Sdim Dependencies::check_valid_dependency_type(type()); 1533234353Sdim 1534234353Sdim Klass* witness = NULL; 1535234353Sdim switch (type()) { 1536263508Sdim case evol_method: 1537234353Sdim witness = check_evol_method(method_argument(0)); 1538234353Sdim break; 1539234353Sdim case leaf_type: 1540234353Sdim witness = check_leaf_type(context_type()); 1541234353Sdim break; 1542234353Sdim case abstract_with_unique_concrete_subtype: 1543234353Sdim witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes); 1544234353Sdim break; 1545234353Sdim case abstract_with_no_concrete_subtype: 1546234353Sdim witness = check_abstract_with_no_concrete_subtype(context_type(), changes); 1547234353Sdim break; 1548234353Sdim case concrete_with_no_concrete_subtype: 1549234353Sdim witness = check_concrete_with_no_concrete_subtype(context_type(), changes); 1550234353Sdim break; 1551234353Sdim case unique_concrete_method: 1552234353Sdim witness = check_unique_concrete_method(context_type(), method_argument(1), changes); 1553234353Sdim break; 1554234353Sdim case abstract_with_exclusive_concrete_subtypes_2: 1555234353Sdim witness = check_abstract_with_exclusive_concrete_subtypes(context_type(), type_argument(1), type_argument(2), changes); 1556234353Sdim break; 1557234353Sdim case exclusive_concrete_methods_2: 1558234353Sdim witness = check_exclusive_concrete_methods(context_type(), method_argument(1), method_argument(2), changes); 1559263508Sdim break; 1560263508Sdim case no_finalizable_subclasses: 1561263508Sdim witness = check_has_no_finalizable_subclasses(context_type(), changes); 1562263508Sdim break; 1563263508Sdim default: 1564263508Sdim witness = NULL; 1565263508Sdim break; 1566263508Sdim } 1567263508Sdim trace_and_log_witness(witness); 1568263508Sdim return witness; 1569263508Sdim} 1570234353Sdim 1571234353Sdim 1572234353SdimKlass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) { 1573263508Sdim assert_locked_or_safepoint(Compile_lock); 1574263508Sdim Dependencies::check_valid_dependency_type(type()); 1575263508Sdim 1576263508Sdim Klass* witness = NULL; 1577263508Sdim switch (type()) { 1578263508Sdim case call_site_target_value: 1579263508Sdim witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes); 1580263508Sdim break; 1581263508Sdim default: 1582263508Sdim witness = NULL; 1583263508Sdim break; 1584263508Sdim } 1585234353Sdim trace_and_log_witness(witness); 1586234353Sdim return witness; 1587234353Sdim} 1588234353Sdim 1589234353Sdim 1590234353SdimKlass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) { 1591234353Sdim // Handle klass dependency 1592234353Sdim if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type())) 1593234353Sdim return check_klass_dependency(changes.as_klass_change()); 1594234353Sdim 1595234353Sdim // Handle CallSite dependency 1596234353Sdim if (changes.is_call_site_change()) 1597234353Sdim return check_call_site_dependency(changes.as_call_site_change()); 1598234353Sdim 1599234353Sdim // irrelevant dependency; skip it 1600234353Sdim return NULL; 1601234353Sdim} 1602234353Sdim 1603234353Sdim 1604234353Sdimvoid DepChange::print() { 1605234353Sdim int nsup = 0, nint = 0; 1606234353Sdim for (ContextStream str(*this); str.next(); ) { 1607234353Sdim Klass* k = str.klass(); 1608234353Sdim switch (str.change_type()) { 1609234353Sdim case Change_new_type: 1610234353Sdim tty->print_cr(" dependee = %s", InstanceKlass::cast(k)->external_name()); 1611234353Sdim break; 1612234353Sdim case Change_new_sub: 1613234353Sdim if (!WizardMode) { 1614234353Sdim ++nsup; 1615234353Sdim } else { 1616234353Sdim tty->print_cr(" context super = %s", InstanceKlass::cast(k)->external_name()); 1617234353Sdim } 1618234353Sdim break; 1619234353Sdim case Change_new_impl: 1620234353Sdim if (!WizardMode) { 1621234353Sdim ++nint; 1622263508Sdim } else { 1623263508Sdim tty->print_cr(" context interface = %s", InstanceKlass::cast(k)->external_name()); 1624263508Sdim } 1625263508Sdim break; 1626263508Sdim } 1627263508Sdim } 1628263508Sdim if (nsup + nint != 0) { 1629263508Sdim tty->print_cr(" context supers = %d, interfaces = %d", nsup, nint); 1630234353Sdim } 1631234353Sdim} 1632234353Sdim 1633234353Sdimvoid DepChange::ContextStream::start() { 1634234353Sdim Klass* new_type = _changes.is_klass_change() ? _changes.as_klass_change()->new_type() : (Klass*) NULL; 1635234353Sdim _change_type = (new_type == NULL ? NO_CHANGE : Start_Klass); 1636263508Sdim _klass = new_type; 1637263508Sdim _ti_base = NULL; 1638263508Sdim _ti_index = 0; 1639263508Sdim _ti_limit = 0; 1640263508Sdim} 1641263508Sdim 1642263508Sdimbool DepChange::ContextStream::next() { 1643263508Sdim switch (_change_type) { 1644263508Sdim case Start_Klass: // initial state; _klass is the new type 1645263508Sdim _ti_base = InstanceKlass::cast(_klass)->transitive_interfaces(); 1646263508Sdim _ti_index = 0; 1647234353Sdim _change_type = Change_new_type; 1648234353Sdim return true; 1649234353Sdim case Change_new_type: 1650234353Sdim // fall through: 1651234353Sdim _change_type = Change_new_sub; 1652234353Sdim case Change_new_sub: 1653234353Sdim // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277 1654263508Sdim { 1655263508Sdim _klass = InstanceKlass::cast(_klass)->super(); 1656234353Sdim if (_klass != NULL) { 1657234353Sdim return true; 1658263508Sdim } 1659234353Sdim } 1660234353Sdim // else set up _ti_limit and fall through: 1661263508Sdim _ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length(); 1662263508Sdim _change_type = Change_new_impl; 1663263508Sdim case Change_new_impl: 1664263508Sdim if (_ti_index < _ti_limit) { 1665263508Sdim _klass = _ti_base->at(_ti_index++); 1666234353Sdim return true; 1667234353Sdim } 1668234353Sdim // fall through: 1669263508Sdim _change_type = NO_CHANGE; // iterator is exhausted 1670263508Sdim case NO_CHANGE: 1671234353Sdim break; 1672263508Sdim default: 1673234353Sdim ShouldNotReachHere(); 1674263508Sdim } 1675234353Sdim return false; 1676234353Sdim} 1677249423Sdim 1678249423Sdimvoid KlassDepChange::initialize() { 1679249423Sdim // entire transaction must be under this lock: 1680249423Sdim assert_lock_strong(Compile_lock); 1681249423Sdim 1682249423Sdim // Mark all dependee and all its superclasses 1683249423Sdim // Mark transitive interfaces 1684249423Sdim for (ContextStream str(*this); str.next(); ) { 1685249423Sdim Klass* d = str.klass(); 1686249423Sdim assert(!InstanceKlass::cast(d)->is_marked_dependent(), "checking"); 1687249423Sdim InstanceKlass::cast(d)->set_is_marked_dependent(true); 1688249423Sdim } 1689249423Sdim} 1690249423Sdim 1691249423SdimKlassDepChange::~KlassDepChange() { 1692249423Sdim // Unmark all dependee and all its superclasses 1693249423Sdim // Unmark transitive interfaces 1694249423Sdim for (ContextStream str(*this); str.next(); ) { 1695249423Sdim Klass* d = str.klass(); 1696249423Sdim InstanceKlass::cast(d)->set_is_marked_dependent(false); 1697249423Sdim } 1698249423Sdim} 1699249423Sdim 1700249423Sdimbool KlassDepChange::involves_context(Klass* k) { 1701249423Sdim if (k == NULL || !Klass::cast(k)->oop_is_instance()) { 1702249423Sdim return false; 1703249423Sdim } 1704249423Sdim InstanceKlass* ik = InstanceKlass::cast(k); 1705249423Sdim bool is_contained = ik->is_marked_dependent(); 1706234353Sdim assert(is_contained == Klass::cast(new_type())->is_subtype_of(k), 1707234353Sdim "correct marking of potential context types"); 1708263508Sdim return is_contained; 1709263508Sdim} 1710263508Sdim 1711263508Sdim#ifndef PRODUCT 1712263508Sdimvoid Dependencies::print_statistics() { 1713263508Sdim if (deps_find_witness_print != 0) { 1714263508Sdim // Call one final time, to flush out the data. 1715263508Sdim deps_find_witness_print = -1; 1716263508Sdim count_find_witness_calls(); 1717263508Sdim } 1718223013Sdim} 1719223013Sdim#endif 1720243830Sdim