dependencies.cpp revision 8484:54b7b0210ad6
1/* 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#include "precompiled.hpp" 26#include "ci/ciArrayKlass.hpp" 27#include "ci/ciEnv.hpp" 28#include "ci/ciKlass.hpp" 29#include "ci/ciMethod.hpp" 30#include "classfile/javaClasses.inline.hpp" 31#include "code/dependencies.hpp" 32#include "compiler/compileLog.hpp" 33#include "oops/oop.inline.hpp" 34#include "runtime/handles.hpp" 35#include "runtime/handles.inline.hpp" 36#include "runtime/thread.inline.hpp" 37#include "utilities/copy.hpp" 38 39 40#ifdef ASSERT 41static bool must_be_in_vm() { 42 Thread* thread = Thread::current(); 43 if (thread->is_Java_thread()) 44 return ((JavaThread*)thread)->thread_state() == _thread_in_vm; 45 else 46 return true; //something like this: thread->is_VM_thread(); 47} 48#endif //ASSERT 49 50void Dependencies::initialize(ciEnv* env) { 51 Arena* arena = env->arena(); 52 _oop_recorder = env->oop_recorder(); 53 _log = env->log(); 54 _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0); 55 DEBUG_ONLY(_deps[end_marker] = NULL); 56 for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) { 57 _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0); 58 } 59 _content_bytes = NULL; 60 _size_in_bytes = (size_t)-1; 61 62 assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity"); 63} 64 65void Dependencies::assert_evol_method(ciMethod* m) { 66 assert_common_1(evol_method, m); 67} 68 69void Dependencies::assert_leaf_type(ciKlass* ctxk) { 70 if (ctxk->is_array_klass()) { 71 // As a special case, support this assertion on an array type, 72 // which reduces to an assertion on its element type. 73 // Note that this cannot be done with assertions that 74 // relate to concreteness or abstractness. 75 ciType* elemt = ctxk->as_array_klass()->base_element_type(); 76 if (!elemt->is_instance_klass()) return; // Ex: int[][] 77 ctxk = elemt->as_instance_klass(); 78 //if (ctxk->is_final()) return; // Ex: String[][] 79 } 80 check_ctxk(ctxk); 81 assert_common_1(leaf_type, ctxk); 82} 83 84void Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) { 85 check_ctxk_abstract(ctxk); 86 assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck); 87} 88 89void Dependencies::assert_abstract_with_no_concrete_subtype(ciKlass* ctxk) { 90 check_ctxk_abstract(ctxk); 91 assert_common_1(abstract_with_no_concrete_subtype, ctxk); 92} 93 94void Dependencies::assert_concrete_with_no_concrete_subtype(ciKlass* ctxk) { 95 check_ctxk_concrete(ctxk); 96 assert_common_1(concrete_with_no_concrete_subtype, ctxk); 97} 98 99void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) { 100 check_ctxk(ctxk); 101 assert_common_2(unique_concrete_method, ctxk, uniqm); 102} 103 104void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) { 105 check_ctxk(ctxk); 106 assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2); 107} 108 109void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) { 110 check_ctxk(ctxk); 111 assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2); 112} 113 114void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) { 115 check_ctxk(ctxk); 116 assert_common_1(no_finalizable_subclasses, ctxk); 117} 118 119void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) { 120 assert_common_2(call_site_target_value, call_site, method_handle); 121} 122 123// Helper function. If we are adding a new dep. under ctxk2, 124// try to find an old dep. under a broader* ctxk1. If there is 125// 126bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps, 127 int ctxk_i, ciKlass* ctxk2) { 128 ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass(); 129 if (ctxk2->is_subtype_of(ctxk1)) { 130 return true; // success, and no need to change 131 } else if (ctxk1->is_subtype_of(ctxk2)) { 132 // new context class fully subsumes previous one 133 deps->at_put(ctxk_i, ctxk2); 134 return true; 135 } else { 136 return false; 137 } 138} 139 140void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) { 141 assert(dep_args(dept) == 1, "sanity"); 142 log_dependency(dept, x); 143 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 144 145 // see if the same (or a similar) dep is already recorded 146 if (note_dep_seen(dept, x)) { 147 assert(deps->find(x) >= 0, "sanity"); 148 } else { 149 deps->append(x); 150 } 151} 152 153void Dependencies::assert_common_2(DepType dept, 154 ciBaseObject* x0, ciBaseObject* x1) { 155 assert(dep_args(dept) == 2, "sanity"); 156 log_dependency(dept, x0, x1); 157 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 158 159 // see if the same (or a similar) dep is already recorded 160 bool has_ctxk = has_explicit_context_arg(dept); 161 if (has_ctxk) { 162 assert(dep_context_arg(dept) == 0, "sanity"); 163 if (note_dep_seen(dept, x1)) { 164 // look in this bucket for redundant assertions 165 const int stride = 2; 166 for (int i = deps->length(); (i -= stride) >= 0; ) { 167 ciBaseObject* y1 = deps->at(i+1); 168 if (x1 == y1) { // same subject; check the context 169 if (maybe_merge_ctxk(deps, i+0, x0->as_metadata()->as_klass())) { 170 return; 171 } 172 } 173 } 174 } 175 } else { 176 if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) { 177 // look in this bucket for redundant assertions 178 const int stride = 2; 179 for (int i = deps->length(); (i -= stride) >= 0; ) { 180 ciBaseObject* y0 = deps->at(i+0); 181 ciBaseObject* y1 = deps->at(i+1); 182 if (x0 == y0 && x1 == y1) { 183 return; 184 } 185 } 186 } 187 } 188 189 // append the assertion in the correct bucket: 190 deps->append(x0); 191 deps->append(x1); 192} 193 194void Dependencies::assert_common_3(DepType dept, 195 ciKlass* ctxk, ciBaseObject* x, ciBaseObject* x2) { 196 assert(dep_context_arg(dept) == 0, "sanity"); 197 assert(dep_args(dept) == 3, "sanity"); 198 log_dependency(dept, ctxk, x, x2); 199 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 200 201 // try to normalize an unordered pair: 202 bool swap = false; 203 switch (dept) { 204 case abstract_with_exclusive_concrete_subtypes_2: 205 swap = (x->ident() > x2->ident() && x->as_metadata()->as_klass() != ctxk); 206 break; 207 case exclusive_concrete_methods_2: 208 swap = (x->ident() > x2->ident() && x->as_metadata()->as_method()->holder() != ctxk); 209 break; 210 } 211 if (swap) { ciBaseObject* t = x; x = x2; x2 = t; } 212 213 // see if the same (or a similar) dep is already recorded 214 if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) { 215 // look in this bucket for redundant assertions 216 const int stride = 3; 217 for (int i = deps->length(); (i -= stride) >= 0; ) { 218 ciBaseObject* y = deps->at(i+1); 219 ciBaseObject* y2 = deps->at(i+2); 220 if (x == y && x2 == y2) { // same subjects; check the context 221 if (maybe_merge_ctxk(deps, i+0, ctxk)) { 222 return; 223 } 224 } 225 } 226 } 227 // append the assertion in the correct bucket: 228 deps->append(ctxk); 229 deps->append(x); 230 deps->append(x2); 231} 232 233/// Support for encoding dependencies into an nmethod: 234 235void Dependencies::copy_to(nmethod* nm) { 236 address beg = nm->dependencies_begin(); 237 address end = nm->dependencies_end(); 238 guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing"); 239 Copy::disjoint_words((HeapWord*) content_bytes(), 240 (HeapWord*) beg, 241 size_in_bytes() / sizeof(HeapWord)); 242 assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words"); 243} 244 245static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) { 246 for (int i = 0; i < narg; i++) { 247 int diff = p1[i]->ident() - p2[i]->ident(); 248 if (diff != 0) return diff; 249 } 250 return 0; 251} 252static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2) 253{ return sort_dep(p1, p2, 1); } 254static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2) 255{ return sort_dep(p1, p2, 2); } 256static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2) 257{ return sort_dep(p1, p2, 3); } 258 259void Dependencies::sort_all_deps() { 260 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 261 DepType dept = (DepType)deptv; 262 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 263 if (deps->length() <= 1) continue; 264 switch (dep_args(dept)) { 265 case 1: deps->sort(sort_dep_arg_1, 1); break; 266 case 2: deps->sort(sort_dep_arg_2, 2); break; 267 case 3: deps->sort(sort_dep_arg_3, 3); break; 268 default: ShouldNotReachHere(); 269 } 270 } 271} 272 273size_t Dependencies::estimate_size_in_bytes() { 274 size_t est_size = 100; 275 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 276 DepType dept = (DepType)deptv; 277 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 278 est_size += deps->length()*2; // tags and argument(s) 279 } 280 return est_size; 281} 282 283ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) { 284 switch (dept) { 285 case abstract_with_exclusive_concrete_subtypes_2: 286 return x->as_metadata()->as_klass(); 287 case unique_concrete_method: 288 case exclusive_concrete_methods_2: 289 return x->as_metadata()->as_method()->holder(); 290 } 291 return NULL; // let NULL be NULL 292} 293 294Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) { 295 assert(must_be_in_vm(), "raw oops here"); 296 switch (dept) { 297 case abstract_with_exclusive_concrete_subtypes_2: 298 assert(x->is_klass(), "sanity"); 299 return (Klass*) x; 300 case unique_concrete_method: 301 case exclusive_concrete_methods_2: 302 assert(x->is_method(), "sanity"); 303 return ((Method*)x)->method_holder(); 304 } 305 return NULL; // let NULL be NULL 306} 307 308void Dependencies::encode_content_bytes() { 309 sort_all_deps(); 310 311 // cast is safe, no deps can overflow INT_MAX 312 CompressedWriteStream bytes((int)estimate_size_in_bytes()); 313 314 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 315 DepType dept = (DepType)deptv; 316 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 317 if (deps->length() == 0) continue; 318 int stride = dep_args(dept); 319 int ctxkj = dep_context_arg(dept); // -1 if no context arg 320 assert(stride > 0, "sanity"); 321 for (int i = 0; i < deps->length(); i += stride) { 322 jbyte code_byte = (jbyte)dept; 323 int skipj = -1; 324 if (ctxkj >= 0 && ctxkj+1 < stride) { 325 ciKlass* ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass(); 326 ciBaseObject* x = deps->at(i+ctxkj+1); // following argument 327 if (ctxk == ctxk_encoded_as_null(dept, x)) { 328 skipj = ctxkj; // we win: maybe one less oop to keep track of 329 code_byte |= default_context_type_bit; 330 } 331 } 332 bytes.write_byte(code_byte); 333 for (int j = 0; j < stride; j++) { 334 if (j == skipj) continue; 335 ciBaseObject* v = deps->at(i+j); 336 int idx; 337 if (v->is_object()) { 338 idx = _oop_recorder->find_index(v->as_object()->constant_encoding()); 339 } else { 340 ciMetadata* meta = v->as_metadata(); 341 idx = _oop_recorder->find_index(meta->constant_encoding()); 342 } 343 bytes.write_int(idx); 344 } 345 } 346 } 347 348 // write a sentinel byte to mark the end 349 bytes.write_byte(end_marker); 350 351 // round it out to a word boundary 352 while (bytes.position() % sizeof(HeapWord) != 0) { 353 bytes.write_byte(end_marker); 354 } 355 356 // check whether the dept byte encoding really works 357 assert((jbyte)default_context_type_bit != 0, "byte overflow"); 358 359 _content_bytes = bytes.buffer(); 360 _size_in_bytes = bytes.position(); 361} 362 363 364const char* Dependencies::_dep_name[TYPE_LIMIT] = { 365 "end_marker", 366 "evol_method", 367 "leaf_type", 368 "abstract_with_unique_concrete_subtype", 369 "abstract_with_no_concrete_subtype", 370 "concrete_with_no_concrete_subtype", 371 "unique_concrete_method", 372 "abstract_with_exclusive_concrete_subtypes_2", 373 "exclusive_concrete_methods_2", 374 "no_finalizable_subclasses", 375 "call_site_target_value" 376}; 377 378int Dependencies::_dep_args[TYPE_LIMIT] = { 379 -1,// end_marker 380 1, // evol_method m 381 1, // leaf_type ctxk 382 2, // abstract_with_unique_concrete_subtype ctxk, k 383 1, // abstract_with_no_concrete_subtype ctxk 384 1, // concrete_with_no_concrete_subtype ctxk 385 2, // unique_concrete_method ctxk, m 386 3, // unique_concrete_subtypes_2 ctxk, k1, k2 387 3, // unique_concrete_methods_2 ctxk, m1, m2 388 1, // no_finalizable_subclasses ctxk 389 2 // call_site_target_value call_site, method_handle 390}; 391 392const char* Dependencies::dep_name(Dependencies::DepType dept) { 393 if (!dept_in_mask(dept, all_types)) return "?bad-dep?"; 394 return _dep_name[dept]; 395} 396 397int Dependencies::dep_args(Dependencies::DepType dept) { 398 if (!dept_in_mask(dept, all_types)) return -1; 399 return _dep_args[dept]; 400} 401 402void Dependencies::check_valid_dependency_type(DepType dept) { 403 guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, err_msg("invalid dependency type: %d", (int) dept)); 404} 405 406// for the sake of the compiler log, print out current dependencies: 407void Dependencies::log_all_dependencies() { 408 if (log() == NULL) return; 409 ResourceMark rm; 410 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 411 DepType dept = (DepType)deptv; 412 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 413 int deplen = deps->length(); 414 if (deplen == 0) { 415 continue; 416 } 417 int stride = dep_args(dept); 418 GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(stride); 419 for (int i = 0; i < deps->length(); i += stride) { 420 for (int j = 0; j < stride; j++) { 421 // flush out the identities before printing 422 ciargs->push(deps->at(i+j)); 423 } 424 write_dependency_to(log(), dept, ciargs); 425 ciargs->clear(); 426 } 427 guarantee(deplen == deps->length(), "deps array cannot grow inside nested ResoureMark scope"); 428 } 429} 430 431void Dependencies::write_dependency_to(CompileLog* log, 432 DepType dept, 433 GrowableArray<DepArgument>* args, 434 Klass* witness) { 435 if (log == NULL) { 436 return; 437 } 438 ResourceMark rm; 439 ciEnv* env = ciEnv::current(); 440 GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(args->length()); 441 for (GrowableArrayIterator<DepArgument> it = args->begin(); it != args->end(); ++it) { 442 DepArgument arg = *it; 443 if (arg.is_oop()) { 444 ciargs->push(env->get_object(arg.oop_value())); 445 } else { 446 ciargs->push(env->get_metadata(arg.metadata_value())); 447 } 448 } 449 int argslen = ciargs->length(); 450 Dependencies::write_dependency_to(log, dept, ciargs, witness); 451 guarantee(argslen == ciargs->length(), "ciargs array cannot grow inside nested ResoureMark scope"); 452} 453 454void Dependencies::write_dependency_to(CompileLog* log, 455 DepType dept, 456 GrowableArray<ciBaseObject*>* args, 457 Klass* witness) { 458 if (log == NULL) { 459 return; 460 } 461 ResourceMark rm; 462 GrowableArray<int>* argids = new GrowableArray<int>(args->length()); 463 for (GrowableArrayIterator<ciBaseObject*> it = args->begin(); it != args->end(); ++it) { 464 ciBaseObject* obj = *it; 465 if (obj->is_object()) { 466 argids->push(log->identify(obj->as_object())); 467 } else { 468 argids->push(log->identify(obj->as_metadata())); 469 } 470 } 471 if (witness != NULL) { 472 log->begin_elem("dependency_failed"); 473 } else { 474 log->begin_elem("dependency"); 475 } 476 log->print(" type='%s'", dep_name(dept)); 477 const int ctxkj = dep_context_arg(dept); // -1 if no context arg 478 if (ctxkj >= 0 && ctxkj < argids->length()) { 479 log->print(" ctxk='%d'", argids->at(ctxkj)); 480 } 481 // write remaining arguments, if any. 482 for (int j = 0; j < argids->length(); j++) { 483 if (j == ctxkj) continue; // already logged 484 if (j == 1) { 485 log->print( " x='%d'", argids->at(j)); 486 } else { 487 log->print(" x%d='%d'", j, argids->at(j)); 488 } 489 } 490 if (witness != NULL) { 491 log->object("witness", witness); 492 log->stamp(); 493 } 494 log->end_elem(); 495} 496 497void Dependencies::write_dependency_to(xmlStream* xtty, 498 DepType dept, 499 GrowableArray<DepArgument>* args, 500 Klass* witness) { 501 if (xtty == NULL) { 502 return; 503 } 504 ResourceMark rm; 505 ttyLocker ttyl; 506 int ctxkj = dep_context_arg(dept); // -1 if no context arg 507 if (witness != NULL) { 508 xtty->begin_elem("dependency_failed"); 509 } else { 510 xtty->begin_elem("dependency"); 511 } 512 xtty->print(" type='%s'", dep_name(dept)); 513 if (ctxkj >= 0) { 514 xtty->object("ctxk", args->at(ctxkj).metadata_value()); 515 } 516 // write remaining arguments, if any. 517 for (int j = 0; j < args->length(); j++) { 518 if (j == ctxkj) continue; // already logged 519 DepArgument arg = args->at(j); 520 if (j == 1) { 521 if (arg.is_oop()) { 522 xtty->object("x", arg.oop_value()); 523 } else { 524 xtty->object("x", arg.metadata_value()); 525 } 526 } else { 527 char xn[10]; sprintf(xn, "x%d", j); 528 if (arg.is_oop()) { 529 xtty->object(xn, arg.oop_value()); 530 } else { 531 xtty->object(xn, arg.metadata_value()); 532 } 533 } 534 } 535 if (witness != NULL) { 536 xtty->object("witness", witness); 537 xtty->stamp(); 538 } 539 xtty->end_elem(); 540} 541 542void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args, 543 Klass* witness) { 544 ResourceMark rm; 545 ttyLocker ttyl; // keep the following output all in one block 546 tty->print_cr("%s of type %s", 547 (witness == NULL)? "Dependency": "Failed dependency", 548 dep_name(dept)); 549 // print arguments 550 int ctxkj = dep_context_arg(dept); // -1 if no context arg 551 for (int j = 0; j < args->length(); j++) { 552 DepArgument arg = args->at(j); 553 bool put_star = false; 554 if (arg.is_null()) continue; 555 const char* what; 556 if (j == ctxkj) { 557 assert(arg.is_metadata(), "must be"); 558 what = "context"; 559 put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value()); 560 } else if (arg.is_method()) { 561 what = "method "; 562 put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), NULL); 563 } else if (arg.is_klass()) { 564 what = "class "; 565 } else { 566 what = "object "; 567 } 568 tty->print(" %s = %s", what, (put_star? "*": "")); 569 if (arg.is_klass()) { 570 tty->print("%s", ((Klass*)arg.metadata_value())->external_name()); 571 } else if (arg.is_method()) { 572 ((Method*)arg.metadata_value())->print_value(); 573 } else if (arg.is_oop()) { 574 arg.oop_value()->print_value_on(tty); 575 } else { 576 ShouldNotReachHere(); // Provide impl for this type. 577 } 578 579 tty->cr(); 580 } 581 if (witness != NULL) { 582 bool put_star = !Dependencies::is_concrete_klass(witness); 583 tty->print_cr(" witness = %s%s", 584 (put_star? "*": ""), 585 witness->external_name()); 586 } 587} 588 589void Dependencies::DepStream::log_dependency(Klass* witness) { 590 if (_deps == NULL && xtty == NULL) return; // fast cutout for runtime 591 ResourceMark rm; 592 const int nargs = argument_count(); 593 GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs); 594 for (int j = 0; j < nargs; j++) { 595 if (is_oop_argument(j)) { 596 args->push(argument_oop(j)); 597 } else { 598 args->push(argument(j)); 599 } 600 } 601 int argslen = args->length(); 602 if (_deps != NULL && _deps->log() != NULL) { 603 Dependencies::write_dependency_to(_deps->log(), type(), args, witness); 604 } else { 605 Dependencies::write_dependency_to(xtty, type(), args, witness); 606 } 607 guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope"); 608} 609 610void Dependencies::DepStream::print_dependency(Klass* witness, bool verbose) { 611 ResourceMark rm; 612 int nargs = argument_count(); 613 GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs); 614 for (int j = 0; j < nargs; j++) { 615 if (is_oop_argument(j)) { 616 args->push(argument_oop(j)); 617 } else { 618 args->push(argument(j)); 619 } 620 } 621 int argslen = args->length(); 622 Dependencies::print_dependency(type(), args, witness); 623 if (verbose) { 624 if (_code != NULL) { 625 tty->print(" code: "); 626 _code->print_value_on(tty); 627 tty->cr(); 628 } 629 } 630 guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope"); 631} 632 633 634/// Dependency stream support (decodes dependencies from an nmethod): 635 636#ifdef ASSERT 637void Dependencies::DepStream::initial_asserts(size_t byte_limit) { 638 assert(must_be_in_vm(), "raw oops here"); 639 _byte_limit = byte_limit; 640 _type = (DepType)(end_marker-1); // defeat "already at end" assert 641 assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other"); 642} 643#endif //ASSERT 644 645bool Dependencies::DepStream::next() { 646 assert(_type != end_marker, "already at end"); 647 if (_bytes.position() == 0 && _code != NULL 648 && _code->dependencies_size() == 0) { 649 // Method has no dependencies at all. 650 return false; 651 } 652 int code_byte = (_bytes.read_byte() & 0xFF); 653 if (code_byte == end_marker) { 654 DEBUG_ONLY(_type = end_marker); 655 return false; 656 } else { 657 int ctxk_bit = (code_byte & Dependencies::default_context_type_bit); 658 code_byte -= ctxk_bit; 659 DepType dept = (DepType)code_byte; 660 _type = dept; 661 Dependencies::check_valid_dependency_type(dept); 662 int stride = _dep_args[dept]; 663 assert(stride == dep_args(dept), "sanity"); 664 int skipj = -1; 665 if (ctxk_bit != 0) { 666 skipj = 0; // currently the only context argument is at zero 667 assert(skipj == dep_context_arg(dept), "zero arg always ctxk"); 668 } 669 for (int j = 0; j < stride; j++) { 670 _xi[j] = (j == skipj)? 0: _bytes.read_int(); 671 } 672 DEBUG_ONLY(_xi[stride] = -1); // help detect overruns 673 return true; 674 } 675} 676 677inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) { 678 Metadata* o = NULL; 679 if (_code != NULL) { 680 o = _code->metadata_at(i); 681 } else { 682 o = _deps->oop_recorder()->metadata_at(i); 683 } 684 return o; 685} 686 687inline oop Dependencies::DepStream::recorded_oop_at(int i) { 688 return (_code != NULL) 689 ? _code->oop_at(i) 690 : JNIHandles::resolve(_deps->oop_recorder()->oop_at(i)); 691} 692 693Metadata* Dependencies::DepStream::argument(int i) { 694 Metadata* result = recorded_metadata_at(argument_index(i)); 695 696 if (result == NULL) { // Explicit context argument can be compressed 697 int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg 698 if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) { 699 result = ctxk_encoded_as_null(type(), argument(ctxkj+1)); 700 } 701 } 702 703 assert(result == NULL || result->is_klass() || result->is_method(), "must be"); 704 return result; 705} 706 707/** 708 * Returns a unique identifier for each dependency argument. 709 */ 710uintptr_t Dependencies::DepStream::get_identifier(int i) { 711 if (is_oop_argument(i)) { 712 return (uintptr_t)(oopDesc*)argument_oop(i); 713 } else { 714 return (uintptr_t)argument(i); 715 } 716} 717 718oop Dependencies::DepStream::argument_oop(int i) { 719 oop result = recorded_oop_at(argument_index(i)); 720 assert(result == NULL || result->is_oop(), "must be"); 721 return result; 722} 723 724Klass* Dependencies::DepStream::context_type() { 725 assert(must_be_in_vm(), "raw oops here"); 726 727 // Most dependencies have an explicit context type argument. 728 { 729 int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg 730 if (ctxkj >= 0) { 731 Metadata* k = argument(ctxkj); 732 assert(k != NULL && k->is_klass(), "type check"); 733 return (Klass*)k; 734 } 735 } 736 737 // Some dependencies are using the klass of the first object 738 // argument as implicit context type. 739 { 740 int ctxkj = dep_implicit_context_arg(type()); 741 if (ctxkj >= 0) { 742 Klass* k = argument_oop(ctxkj)->klass(); 743 assert(k != NULL && k->is_klass(), "type check"); 744 return (Klass*) k; 745 } 746 } 747 748 // And some dependencies don't have a context type at all, 749 // e.g. evol_method. 750 return NULL; 751} 752 753// ----------------- DependencySignature -------------------------------------- 754bool DependencySignature::equals(DependencySignature const& s1, DependencySignature const& s2) { 755 if ((s1.type() != s2.type()) || (s1.args_count() != s2.args_count())) { 756 return false; 757 } 758 759 for (int i = 0; i < s1.args_count(); i++) { 760 if (s1.arg(i) != s2.arg(i)) { 761 return false; 762 } 763 } 764 return true; 765} 766 767/// Checking dependencies: 768 769// This hierarchy walker inspects subtypes of a given type, 770// trying to find a "bad" class which breaks a dependency. 771// Such a class is called a "witness" to the broken dependency. 772// While searching around, we ignore "participants", which 773// are already known to the dependency. 774class ClassHierarchyWalker { 775 public: 776 enum { PARTICIPANT_LIMIT = 3 }; 777 778 private: 779 // optional method descriptor to check for: 780 Symbol* _name; 781 Symbol* _signature; 782 783 // special classes which are not allowed to be witnesses: 784 Klass* _participants[PARTICIPANT_LIMIT+1]; 785 int _num_participants; 786 787 // cache of method lookups 788 Method* _found_methods[PARTICIPANT_LIMIT+1]; 789 790 // if non-zero, tells how many witnesses to convert to participants 791 int _record_witnesses; 792 793 void initialize(Klass* participant) { 794 _record_witnesses = 0; 795 _participants[0] = participant; 796 _found_methods[0] = NULL; 797 _num_participants = 0; 798 if (participant != NULL) { 799 // Terminating NULL. 800 _participants[1] = NULL; 801 _found_methods[1] = NULL; 802 _num_participants = 1; 803 } 804 } 805 806 void initialize_from_method(Method* m) { 807 assert(m != NULL && m->is_method(), "sanity"); 808 _name = m->name(); 809 _signature = m->signature(); 810 } 811 812 public: 813 // The walker is initialized to recognize certain methods and/or types 814 // as friendly participants. 815 ClassHierarchyWalker(Klass* participant, Method* m) { 816 initialize_from_method(m); 817 initialize(participant); 818 } 819 ClassHierarchyWalker(Method* m) { 820 initialize_from_method(m); 821 initialize(NULL); 822 } 823 ClassHierarchyWalker(Klass* participant = NULL) { 824 _name = NULL; 825 _signature = NULL; 826 initialize(participant); 827 } 828 829 // This is common code for two searches: One for concrete subtypes, 830 // the other for concrete method implementations and overrides. 831 bool doing_subtype_search() { 832 return _name == NULL; 833 } 834 835 int num_participants() { return _num_participants; } 836 Klass* participant(int n) { 837 assert((uint)n <= (uint)_num_participants, "oob"); 838 return _participants[n]; 839 } 840 841 // Note: If n==num_participants, returns NULL. 842 Method* found_method(int n) { 843 assert((uint)n <= (uint)_num_participants, "oob"); 844 Method* fm = _found_methods[n]; 845 assert(n == _num_participants || fm != NULL, "proper usage"); 846 if (fm != NULL && fm->method_holder() != _participants[n]) { 847 // Default methods from interfaces can be added to classes. In 848 // that case the holder of the method is not the class but the 849 // interface where it's defined. 850 assert(fm->is_default_method(), "sanity"); 851 return NULL; 852 } 853 return fm; 854 } 855 856#ifdef ASSERT 857 // Assert that m is inherited into ctxk, without intervening overrides. 858 // (May return true even if this is not true, in corner cases where we punt.) 859 bool check_method_context(Klass* ctxk, Method* m) { 860 if (m->method_holder() == ctxk) 861 return true; // Quick win. 862 if (m->is_private()) 863 return false; // Quick lose. Should not happen. 864 if (!(m->is_public() || m->is_protected())) 865 // The override story is complex when packages get involved. 866 return true; // Must punt the assertion to true. 867 Klass* k = ctxk; 868 Method* lm = k->lookup_method(m->name(), m->signature()); 869 if (lm == NULL && k->oop_is_instance()) { 870 // It might be an interface method 871 lm = ((InstanceKlass*)k)->lookup_method_in_ordered_interfaces(m->name(), 872 m->signature()); 873 } 874 if (lm == m) 875 // Method m is inherited into ctxk. 876 return true; 877 if (lm != NULL) { 878 if (!(lm->is_public() || lm->is_protected())) { 879 // Method is [package-]private, so the override story is complex. 880 return true; // Must punt the assertion to true. 881 } 882 if (lm->is_static()) { 883 // Static methods don't override non-static so punt 884 return true; 885 } 886 if ( !Dependencies::is_concrete_method(lm, k) 887 && !Dependencies::is_concrete_method(m, ctxk) 888 && lm->method_holder()->is_subtype_of(m->method_holder())) 889 // Method m is overridden by lm, but both are non-concrete. 890 return true; 891 } 892 ResourceMark rm; 893 tty->print_cr("Dependency method not found in the associated context:"); 894 tty->print_cr(" context = %s", ctxk->external_name()); 895 tty->print( " method = "); m->print_short_name(tty); tty->cr(); 896 if (lm != NULL) { 897 tty->print( " found = "); lm->print_short_name(tty); tty->cr(); 898 } 899 return false; 900 } 901#endif 902 903 void add_participant(Klass* participant) { 904 assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob"); 905 int np = _num_participants++; 906 _participants[np] = participant; 907 _participants[np+1] = NULL; 908 _found_methods[np+1] = NULL; 909 } 910 911 void record_witnesses(int add) { 912 if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT; 913 assert(_num_participants + add < PARTICIPANT_LIMIT, "oob"); 914 _record_witnesses = add; 915 } 916 917 bool is_witness(Klass* k) { 918 if (doing_subtype_search()) { 919 return Dependencies::is_concrete_klass(k); 920 } else if (!k->oop_is_instance()) { 921 return false; // no methods to find in an array type 922 } else { 923 // Search class hierarchy first. 924 Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature); 925 if (!Dependencies::is_concrete_method(m, k)) { 926 // Check interface defaults also, if any exist. 927 Array<Method*>* default_methods = InstanceKlass::cast(k)->default_methods(); 928 if (default_methods == NULL) 929 return false; 930 m = InstanceKlass::cast(k)->find_method(default_methods, _name, _signature); 931 if (!Dependencies::is_concrete_method(m, NULL)) 932 return false; 933 } 934 _found_methods[_num_participants] = m; 935 // Note: If add_participant(k) is called, 936 // the method m will already be memoized for it. 937 return true; 938 } 939 } 940 941 bool is_participant(Klass* k) { 942 if (k == _participants[0]) { 943 return true; 944 } else if (_num_participants <= 1) { 945 return false; 946 } else { 947 return in_list(k, &_participants[1]); 948 } 949 } 950 bool ignore_witness(Klass* witness) { 951 if (_record_witnesses == 0) { 952 return false; 953 } else { 954 --_record_witnesses; 955 add_participant(witness); 956 return true; 957 } 958 } 959 static bool in_list(Klass* x, Klass** list) { 960 for (int i = 0; ; i++) { 961 Klass* y = list[i]; 962 if (y == NULL) break; 963 if (y == x) return true; 964 } 965 return false; // not in list 966 } 967 968 private: 969 // the actual search method: 970 Klass* find_witness_anywhere(Klass* context_type, 971 bool participants_hide_witnesses, 972 bool top_level_call = true); 973 // the spot-checking version: 974 Klass* find_witness_in(KlassDepChange& changes, 975 Klass* context_type, 976 bool participants_hide_witnesses); 977 public: 978 Klass* find_witness_subtype(Klass* context_type, KlassDepChange* changes = NULL) { 979 assert(doing_subtype_search(), "must set up a subtype search"); 980 // When looking for unexpected concrete types, 981 // do not look beneath expected ones. 982 const bool participants_hide_witnesses = true; 983 // CX > CC > C' is OK, even if C' is new. 984 // CX > { CC, C' } is not OK if C' is new, and C' is the witness. 985 if (changes != NULL) { 986 return find_witness_in(*changes, context_type, participants_hide_witnesses); 987 } else { 988 return find_witness_anywhere(context_type, participants_hide_witnesses); 989 } 990 } 991 Klass* find_witness_definer(Klass* context_type, KlassDepChange* changes = NULL) { 992 assert(!doing_subtype_search(), "must set up a method definer search"); 993 // When looking for unexpected concrete methods, 994 // look beneath expected ones, to see if there are overrides. 995 const bool participants_hide_witnesses = true; 996 // CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness. 997 if (changes != NULL) { 998 return find_witness_in(*changes, context_type, !participants_hide_witnesses); 999 } else { 1000 return find_witness_anywhere(context_type, !participants_hide_witnesses); 1001 } 1002 } 1003}; 1004 1005#ifndef PRODUCT 1006static int deps_find_witness_calls = 0; 1007static int deps_find_witness_steps = 0; 1008static int deps_find_witness_recursions = 0; 1009static int deps_find_witness_singles = 0; 1010static int deps_find_witness_print = 0; // set to -1 to force a final print 1011static bool count_find_witness_calls() { 1012 if (TraceDependencies || LogCompilation) { 1013 int pcount = deps_find_witness_print + 1; 1014 bool final_stats = (pcount == 0); 1015 bool initial_call = (pcount == 1); 1016 bool occasional_print = ((pcount & ((1<<10) - 1)) == 0); 1017 if (pcount < 0) pcount = 1; // crude overflow protection 1018 deps_find_witness_print = pcount; 1019 if (VerifyDependencies && initial_call) { 1020 tty->print_cr("Warning: TraceDependencies results may be inflated by VerifyDependencies"); 1021 } 1022 if (occasional_print || final_stats) { 1023 // Every now and then dump a little info about dependency searching. 1024 if (xtty != NULL) { 1025 ttyLocker ttyl; 1026 xtty->elem("deps_find_witness calls='%d' steps='%d' recursions='%d' singles='%d'", 1027 deps_find_witness_calls, 1028 deps_find_witness_steps, 1029 deps_find_witness_recursions, 1030 deps_find_witness_singles); 1031 } 1032 if (final_stats || (TraceDependencies && WizardMode)) { 1033 ttyLocker ttyl; 1034 tty->print_cr("Dependency check (find_witness) " 1035 "calls=%d, steps=%d (avg=%.1f), recursions=%d, singles=%d", 1036 deps_find_witness_calls, 1037 deps_find_witness_steps, 1038 (double)deps_find_witness_steps / deps_find_witness_calls, 1039 deps_find_witness_recursions, 1040 deps_find_witness_singles); 1041 } 1042 } 1043 return true; 1044 } 1045 return false; 1046} 1047#else 1048#define count_find_witness_calls() (0) 1049#endif //PRODUCT 1050 1051 1052Klass* ClassHierarchyWalker::find_witness_in(KlassDepChange& changes, 1053 Klass* context_type, 1054 bool participants_hide_witnesses) { 1055 assert(changes.involves_context(context_type), "irrelevant dependency"); 1056 Klass* new_type = changes.new_type(); 1057 1058 (void)count_find_witness_calls(); 1059 NOT_PRODUCT(deps_find_witness_singles++); 1060 1061 // Current thread must be in VM (not native mode, as in CI): 1062 assert(must_be_in_vm(), "raw oops here"); 1063 // Must not move the class hierarchy during this check: 1064 assert_locked_or_safepoint(Compile_lock); 1065 1066 int nof_impls = InstanceKlass::cast(context_type)->nof_implementors(); 1067 if (nof_impls > 1) { 1068 // Avoid this case: *I.m > { A.m, C }; B.m > C 1069 // %%% Until this is fixed more systematically, bail out. 1070 // See corresponding comment in find_witness_anywhere. 1071 return context_type; 1072 } 1073 1074 assert(!is_participant(new_type), "only old classes are participants"); 1075 if (participants_hide_witnesses) { 1076 // If the new type is a subtype of a participant, we are done. 1077 for (int i = 0; i < num_participants(); i++) { 1078 Klass* part = participant(i); 1079 if (part == NULL) continue; 1080 assert(changes.involves_context(part) == new_type->is_subtype_of(part), 1081 "correct marking of participants, b/c new_type is unique"); 1082 if (changes.involves_context(part)) { 1083 // new guy is protected from this check by previous participant 1084 return NULL; 1085 } 1086 } 1087 } 1088 1089 if (is_witness(new_type) && 1090 !ignore_witness(new_type)) { 1091 return new_type; 1092 } 1093 1094 return NULL; 1095} 1096 1097 1098// Walk hierarchy under a context type, looking for unexpected types. 1099// Do not report participant types, and recursively walk beneath 1100// 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, 1104 bool participants_hide_witnesses, 1105 bool top_level_call) { 1106 // Current thread must be in VM (not native mode, as in CI): 1107 assert(must_be_in_vm(), "raw oops here"); 1108 // Must not move the class hierarchy during this check: 1109 assert_locked_or_safepoint(Compile_lock); 1110 1111 bool do_counts = count_find_witness_calls(); 1112 1113 // Check the root of the sub-hierarchy first. 1114 if (top_level_call) { 1115 if (do_counts) { 1116 NOT_PRODUCT(deps_find_witness_calls++); 1117 NOT_PRODUCT(deps_find_witness_steps++); 1118 } 1119 if (is_participant(context_type)) { 1120 if (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. 1124 return context_type; 1125 } 1126 } 1127 1128 // 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. 1131 const int CHAINMAX = 100; // >= 1 + InstanceKlass::implementors_limit 1132 Klass* chains[CHAINMAX]; 1133 int chaini = 0; // index into worklist 1134 Klass* chain; // scratch variable 1135#define ADD_SUBCLASS_CHAIN(k) { \ 1136 assert(chaini < CHAINMAX, "oob"); \ 1137 chain = k->subklass(); \ 1138 if (chain != NULL) chains[chaini++] = chain; } 1139 1140 // Look for non-abstract subclasses. 1141 // (Note: Interfaces do not have subclasses.) 1142 ADD_SUBCLASS_CHAIN(context_type); 1143 1144 // 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.) 1148 if (top_level_call) { 1149 int nof_impls = InstanceKlass::cast(context_type)->nof_implementors(); 1150 if (nof_impls > 1) { 1151 // Avoid this case: *I.m > { A.m, C }; B.m > C 1152 // Here, I.m has 2 concrete implementations, but m appears unique 1153 // as A.m, because the search misses B.m when checking C. 1154 // The inherited method B.m was getting missed by the walker 1155 // when interface 'I' was the starting point. 1156 // %%% Until this is fixed more systematically, bail out. 1157 // (Old CHA had the same limitation.) 1158 return context_type; 1159 } 1160 if (nof_impls > 0) { 1161 Klass* impl = InstanceKlass::cast(context_type)->implementor(); 1162 assert(impl != NULL, "just checking"); 1163 // If impl is the same as the context_type, then more than one 1164 // implementor has seen. No exact info in this case. 1165 if (impl == context_type) { 1166 return context_type; // report an inexact witness to this sad affair 1167 } 1168 if (do_counts) 1169 { NOT_PRODUCT(deps_find_witness_steps++); } 1170 if (is_participant(impl)) { 1171 if (!participants_hide_witnesses) { 1172 ADD_SUBCLASS_CHAIN(impl); 1173 } 1174 } else if (is_witness(impl) && !ignore_witness(impl)) { 1175 return impl; 1176 } else { 1177 ADD_SUBCLASS_CHAIN(impl); 1178 } 1179 } 1180 } 1181 1182 // Recursively process each non-trivial sibling chain. 1183 while (chaini > 0) { 1184 Klass* chain = chains[--chaini]; 1185 for (Klass* sub = chain; sub != NULL; sub = sub->next_sibling()) { 1186 if (do_counts) { NOT_PRODUCT(deps_find_witness_steps++); } 1187 if (is_participant(sub)) { 1188 if (participants_hide_witnesses) continue; 1189 // else fall through to process this guy's subclasses 1190 } else if (is_witness(sub) && !ignore_witness(sub)) { 1191 return sub; 1192 } 1193 if (chaini < (VerifyDependencies? 2: CHAINMAX)) { 1194 // Fast path. (Partially disabled if VerifyDependencies.) 1195 ADD_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 is 1200 // no need for the recursive call to re-test. That's handy, 1201 // since the recursive call sees sub as the context_type.) 1202 if (do_counts) { NOT_PRODUCT(deps_find_witness_recursions++); } 1203 Klass* witness = find_witness_anywhere(sub, 1204 participants_hide_witnesses, 1205 /*top_level_call=*/ false); 1206 if (witness != NULL) return witness; 1207 } 1208 } 1209 } 1210 1211 // No witness found. The dependency remains unbroken. 1212 return NULL; 1213#undef ADD_SUBCLASS_CHAIN 1214} 1215 1216 1217bool Dependencies::is_concrete_klass(Klass* k) { 1218 if (k->is_abstract()) return false; 1219 // %%% We could treat classes which are concrete but 1220 // have not yet been instantiated as virtually abstract. 1221 // This would require a deoptimization barrier on first instantiation. 1222 //if (k->is_not_instantiated()) return false; 1223 return true; 1224} 1225 1226bool Dependencies::is_concrete_method(Method* m, Klass * k) { 1227 // NULL is not a concrete method, 1228 // statics are irrelevant to virtual call sites, 1229 // abstract methods are not concrete, 1230 // overpass (error) methods are not concrete if k is abstract 1231 // 1232 // note "true" is conservative answer -- 1233 // overpass clause is false if k == NULL, implies return true if 1234 // answer depends on overpass clause. 1235 return ! ( m == NULL || m -> is_static() || m -> is_abstract() || 1236 m->is_overpass() && k != NULL && k -> is_abstract() ); 1237} 1238 1239 1240Klass* Dependencies::find_finalizable_subclass(Klass* k) { 1241 if (k->is_interface()) return NULL; 1242 if (k->has_finalizer()) return k; 1243 k = k->subklass(); 1244 while (k != NULL) { 1245 Klass* result = find_finalizable_subclass(k); 1246 if (result != NULL) return result; 1247 k = k->next_sibling(); 1248 } 1249 return NULL; 1250} 1251 1252 1253bool Dependencies::is_concrete_klass(ciInstanceKlass* k) { 1254 if (k->is_abstract()) return false; 1255 // We could also return false if k does not yet appear to be 1256 // instantiated, if the VM version supports this distinction also. 1257 //if (k->is_not_instantiated()) return false; 1258 return true; 1259} 1260 1261bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) { 1262 return k->has_finalizable_subclass(); 1263} 1264 1265 1266// Any use of the contents (bytecodes) of a method must be 1267// marked by an "evol_method" dependency, if those contents 1268// can change. (Note: A method is always dependent on itself.) 1269Klass* Dependencies::check_evol_method(Method* m) { 1270 assert(must_be_in_vm(), "raw oops here"); 1271 // Did somebody do a JVMTI RedefineClasses while our backs were turned? 1272 // Or is there a now a breakpoint? 1273 // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.) 1274 if (m->is_old() 1275 || m->number_of_breakpoints() > 0) { 1276 return m->method_holder(); 1277 } else { 1278 return NULL; 1279 } 1280} 1281 1282// This is a strong assertion: It is that the given type 1283// has no subtypes whatever. It is most useful for 1284// optimizing checks on reflected types or on array types. 1285// (Checks on types which are derived from real instances 1286// can be optimized more strongly than this, because we 1287// know that the checked type comes from a concrete type, 1288// and therefore we can disregard abstract types.) 1289Klass* Dependencies::check_leaf_type(Klass* ctxk) { 1290 assert(must_be_in_vm(), "raw oops here"); 1291 assert_locked_or_safepoint(Compile_lock); 1292 InstanceKlass* ctx = InstanceKlass::cast(ctxk); 1293 Klass* sub = ctx->subklass(); 1294 if (sub != NULL) { 1295 return sub; 1296 } else if (ctx->nof_implementors() != 0) { 1297 // if it is an interface, it must be unimplemented 1298 // (if it is not an interface, nof_implementors is always zero) 1299 Klass* impl = ctx->implementor(); 1300 assert(impl != NULL, "must be set"); 1301 return impl; 1302 } else { 1303 return NULL; 1304 } 1305} 1306 1307// Test the assertion that conck is the only concrete subtype* of ctxk. 1308// The type conck itself is allowed to have have further concrete subtypes. 1309// This allows the compiler to narrow occurrences of ctxk by conck, 1310// when dealing with the types of actual instances. 1311Klass* Dependencies::check_abstract_with_unique_concrete_subtype(Klass* ctxk, 1312 Klass* conck, 1313 KlassDepChange* changes) { 1314 ClassHierarchyWalker wf(conck); 1315 return wf.find_witness_subtype(ctxk, changes); 1316} 1317 1318// If a non-concrete class has no concrete subtypes, it is not (yet) 1319// instantiatable. This can allow the compiler to make some paths go 1320// dead, if they are gated by a test of the type. 1321Klass* Dependencies::check_abstract_with_no_concrete_subtype(Klass* ctxk, 1322 KlassDepChange* changes) { 1323 // Find any concrete subtype, with no participants: 1324 ClassHierarchyWalker wf; 1325 return wf.find_witness_subtype(ctxk, changes); 1326} 1327 1328 1329// If a concrete class has no concrete subtypes, it can always be 1330// exactly typed. This allows the use of a cheaper type test. 1331Klass* Dependencies::check_concrete_with_no_concrete_subtype(Klass* ctxk, 1332 KlassDepChange* changes) { 1333 // Find any concrete subtype, with only the ctxk as participant: 1334 ClassHierarchyWalker wf(ctxk); 1335 return wf.find_witness_subtype(ctxk, changes); 1336} 1337 1338 1339// Find the unique concrete proper subtype of ctxk, or NULL if there 1340// is more than one concrete proper subtype. If there are no concrete 1341// proper subtypes, return ctxk itself, whether it is concrete or not. 1342// The returned subtype is allowed to have have further concrete subtypes. 1343// That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }. 1344Klass* Dependencies::find_unique_concrete_subtype(Klass* ctxk) { 1345 ClassHierarchyWalker wf(ctxk); // Ignore ctxk when walking. 1346 wf.record_witnesses(1); // Record one other witness when walking. 1347 Klass* wit = wf.find_witness_subtype(ctxk); 1348 if (wit != NULL) return NULL; // Too many witnesses. 1349 Klass* conck = wf.participant(0); 1350 if (conck == NULL) { 1351#ifndef PRODUCT 1352 // Make sure the dependency mechanism will pass this discovery: 1353 if (VerifyDependencies) { 1354 // Turn off dependency tracing while actually testing deps. 1355 FlagSetting fs(TraceDependencies, false); 1356 if (!Dependencies::is_concrete_klass(ctxk)) { 1357 guarantee(NULL == 1358 (void *)check_abstract_with_no_concrete_subtype(ctxk), 1359 "verify dep."); 1360 } else { 1361 guarantee(NULL == 1362 (void *)check_concrete_with_no_concrete_subtype(ctxk), 1363 "verify dep."); 1364 } 1365 } 1366#endif //PRODUCT 1367 return ctxk; // Return ctxk as a flag for "no subtypes". 1368 } else { 1369#ifndef PRODUCT 1370 // Make sure the dependency mechanism will pass this discovery: 1371 if (VerifyDependencies) { 1372 // Turn off dependency tracing while actually testing deps. 1373 FlagSetting fs(TraceDependencies, false); 1374 if (!Dependencies::is_concrete_klass(ctxk)) { 1375 guarantee(NULL == (void *) 1376 check_abstract_with_unique_concrete_subtype(ctxk, conck), 1377 "verify dep."); 1378 } 1379 } 1380#endif //PRODUCT 1381 return conck; 1382 } 1383} 1384 1385// Test the assertion that the k[12] are the only concrete subtypes of ctxk, 1386// except possibly for further subtypes of k[12] themselves. 1387// The context type must be abstract. The types k1 and k2 are themselves 1388// allowed to have further concrete subtypes. 1389Klass* Dependencies::check_abstract_with_exclusive_concrete_subtypes( 1390 Klass* ctxk, 1391 Klass* k1, 1392 Klass* k2, 1393 KlassDepChange* changes) { 1394 ClassHierarchyWalker wf; 1395 wf.add_participant(k1); 1396 wf.add_participant(k2); 1397 return wf.find_witness_subtype(ctxk, changes); 1398} 1399 1400// Search ctxk for concrete implementations. If there are klen or fewer, 1401// pack them into the given array and return the number. 1402// Otherwise, return -1, meaning the given array would overflow. 1403// (Note that a return of 0 means there are exactly no concrete subtypes.) 1404// In this search, if ctxk is concrete, it will be reported alone. 1405// For any type CC reported, no proper subtypes of CC will be reported. 1406int Dependencies::find_exclusive_concrete_subtypes(Klass* ctxk, 1407 int klen, 1408 Klass* karray[]) { 1409 ClassHierarchyWalker wf; 1410 wf.record_witnesses(klen); 1411 Klass* wit = wf.find_witness_subtype(ctxk); 1412 if (wit != NULL) return -1; // Too many witnesses. 1413 int num = wf.num_participants(); 1414 assert(num <= klen, "oob"); 1415 // Pack the result array with the good news. 1416 for (int i = 0; i < num; i++) 1417 karray[i] = wf.participant(i); 1418#ifndef PRODUCT 1419 // Make sure the dependency mechanism will pass this discovery: 1420 if (VerifyDependencies) { 1421 // Turn off dependency tracing while actually testing deps. 1422 FlagSetting fs(TraceDependencies, false); 1423 switch (Dependencies::is_concrete_klass(ctxk)? -1: num) { 1424 case -1: // ctxk was itself concrete 1425 guarantee(num == 1 && karray[0] == ctxk, "verify dep."); 1426 break; 1427 case 0: 1428 guarantee(NULL == (void *)check_abstract_with_no_concrete_subtype(ctxk), 1429 "verify dep."); 1430 break; 1431 case 1: 1432 guarantee(NULL == (void *) 1433 check_abstract_with_unique_concrete_subtype(ctxk, karray[0]), 1434 "verify dep."); 1435 break; 1436 case 2: 1437 guarantee(NULL == (void *) 1438 check_abstract_with_exclusive_concrete_subtypes(ctxk, 1439 karray[0], 1440 karray[1]), 1441 "verify dep."); 1442 break; 1443 default: 1444 ShouldNotReachHere(); // klen > 2 yet supported 1445 } 1446 } 1447#endif //PRODUCT 1448 return num; 1449} 1450 1451// If a class (or interface) has a unique concrete method uniqm, return NULL. 1452// Otherwise, return a class that contains an interfering method. 1453Klass* Dependencies::check_unique_concrete_method(Klass* ctxk, Method* uniqm, 1454 KlassDepChange* changes) { 1455 // Here is a missing optimization: If uniqm->is_final(), 1456 // we don't really need to search beneath it for overrides. 1457 // This is probably not important, since we don't use dependencies 1458 // to track final methods. (They can't be "definalized".) 1459 ClassHierarchyWalker wf(uniqm->method_holder(), uniqm); 1460 return wf.find_witness_definer(ctxk, changes); 1461} 1462 1463// Find the set of all non-abstract methods under ctxk that match m. 1464// (The method m must be defined or inherited in ctxk.) 1465// Include m itself in the set, unless it is abstract. 1466// If this set has exactly one element, return that element. 1467Method* Dependencies::find_unique_concrete_method(Klass* ctxk, Method* m) { 1468 // Return NULL if m is marked old; must have been a redefined method. 1469 if (m->is_old()) { 1470 return NULL; 1471 } 1472 ClassHierarchyWalker wf(m); 1473 assert(wf.check_method_context(ctxk, m), "proper context"); 1474 wf.record_witnesses(1); 1475 Klass* wit = wf.find_witness_definer(ctxk); 1476 if (wit != NULL) return NULL; // Too many witnesses. 1477 Method* fm = wf.found_method(0); // Will be NULL if num_parts == 0. 1478 if (Dependencies::is_concrete_method(m, ctxk)) { 1479 if (fm == NULL) { 1480 // It turns out that m was always the only implementation. 1481 fm = m; 1482 } else if (fm != m) { 1483 // Two conflicting implementations after all. 1484 // (This can happen if m is inherited into ctxk and fm overrides it.) 1485 return NULL; 1486 } 1487 } 1488#ifndef PRODUCT 1489 // Make sure the dependency mechanism will pass this discovery: 1490 if (VerifyDependencies && fm != NULL) { 1491 guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm), 1492 "verify dep."); 1493 } 1494#endif //PRODUCT 1495 return fm; 1496} 1497 1498Klass* Dependencies::check_exclusive_concrete_methods(Klass* ctxk, 1499 Method* m1, 1500 Method* m2, 1501 KlassDepChange* changes) { 1502 ClassHierarchyWalker wf(m1); 1503 wf.add_participant(m1->method_holder()); 1504 wf.add_participant(m2->method_holder()); 1505 return wf.find_witness_definer(ctxk, changes); 1506} 1507 1508Klass* Dependencies::check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes) { 1509 Klass* search_at = ctxk; 1510 if (changes != NULL) 1511 search_at = changes->new_type(); // just look at the new bit 1512 return find_finalizable_subclass(search_at); 1513} 1514 1515Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) { 1516 assert(!oopDesc::is_null(call_site), "sanity"); 1517 assert(!oopDesc::is_null(method_handle), "sanity"); 1518 assert(call_site->is_a(SystemDictionary::CallSite_klass()), "sanity"); 1519 1520 if (changes == NULL) { 1521 // Validate all CallSites 1522 if (java_lang_invoke_CallSite::target(call_site) != method_handle) 1523 return call_site->klass(); // assertion failed 1524 } else { 1525 // Validate the given CallSite 1526 if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) { 1527 assert(method_handle != changes->method_handle(), "must be"); 1528 return call_site->klass(); // assertion failed 1529 } 1530 } 1531 return NULL; // assertion still valid 1532} 1533 1534void Dependencies::DepStream::trace_and_log_witness(Klass* witness) { 1535 if (witness != NULL) { 1536 if (TraceDependencies) { 1537 print_dependency(witness, /*verbose=*/ true); 1538 } 1539 // The following is a no-op unless logging is enabled: 1540 log_dependency(witness); 1541 } 1542} 1543 1544 1545Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) { 1546 assert_locked_or_safepoint(Compile_lock); 1547 Dependencies::check_valid_dependency_type(type()); 1548 1549 Klass* witness = NULL; 1550 switch (type()) { 1551 case evol_method: 1552 witness = check_evol_method(method_argument(0)); 1553 break; 1554 case leaf_type: 1555 witness = check_leaf_type(context_type()); 1556 break; 1557 case abstract_with_unique_concrete_subtype: 1558 witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes); 1559 break; 1560 case abstract_with_no_concrete_subtype: 1561 witness = check_abstract_with_no_concrete_subtype(context_type(), changes); 1562 break; 1563 case concrete_with_no_concrete_subtype: 1564 witness = check_concrete_with_no_concrete_subtype(context_type(), changes); 1565 break; 1566 case unique_concrete_method: 1567 witness = check_unique_concrete_method(context_type(), method_argument(1), changes); 1568 break; 1569 case abstract_with_exclusive_concrete_subtypes_2: 1570 witness = check_abstract_with_exclusive_concrete_subtypes(context_type(), type_argument(1), type_argument(2), changes); 1571 break; 1572 case exclusive_concrete_methods_2: 1573 witness = check_exclusive_concrete_methods(context_type(), method_argument(1), method_argument(2), changes); 1574 break; 1575 case no_finalizable_subclasses: 1576 witness = check_has_no_finalizable_subclasses(context_type(), changes); 1577 break; 1578 default: 1579 witness = NULL; 1580 break; 1581 } 1582 trace_and_log_witness(witness); 1583 return witness; 1584} 1585 1586 1587Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) { 1588 assert_locked_or_safepoint(Compile_lock); 1589 Dependencies::check_valid_dependency_type(type()); 1590 1591 Klass* witness = NULL; 1592 switch (type()) { 1593 case call_site_target_value: 1594 witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes); 1595 break; 1596 default: 1597 witness = NULL; 1598 break; 1599 } 1600 trace_and_log_witness(witness); 1601 return witness; 1602} 1603 1604 1605Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) { 1606 // Handle klass dependency 1607 if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type())) 1608 return check_klass_dependency(changes.as_klass_change()); 1609 1610 // Handle CallSite dependency 1611 if (changes.is_call_site_change()) 1612 return check_call_site_dependency(changes.as_call_site_change()); 1613 1614 // irrelevant dependency; skip it 1615 return NULL; 1616} 1617 1618 1619void DepChange::print() { 1620 int nsup = 0, nint = 0; 1621 for (ContextStream str(*this); str.next(); ) { 1622 Klass* k = str.klass(); 1623 switch (str.change_type()) { 1624 case Change_new_type: 1625 tty->print_cr(" dependee = %s", InstanceKlass::cast(k)->external_name()); 1626 break; 1627 case Change_new_sub: 1628 if (!WizardMode) { 1629 ++nsup; 1630 } else { 1631 tty->print_cr(" context super = %s", InstanceKlass::cast(k)->external_name()); 1632 } 1633 break; 1634 case Change_new_impl: 1635 if (!WizardMode) { 1636 ++nint; 1637 } else { 1638 tty->print_cr(" context interface = %s", InstanceKlass::cast(k)->external_name()); 1639 } 1640 break; 1641 } 1642 } 1643 if (nsup + nint != 0) { 1644 tty->print_cr(" context supers = %d, interfaces = %d", nsup, nint); 1645 } 1646} 1647 1648void DepChange::ContextStream::start() { 1649 Klass* new_type = _changes.is_klass_change() ? _changes.as_klass_change()->new_type() : (Klass*) NULL; 1650 _change_type = (new_type == NULL ? NO_CHANGE : Start_Klass); 1651 _klass = new_type; 1652 _ti_base = NULL; 1653 _ti_index = 0; 1654 _ti_limit = 0; 1655} 1656 1657bool DepChange::ContextStream::next() { 1658 switch (_change_type) { 1659 case Start_Klass: // initial state; _klass is the new type 1660 _ti_base = InstanceKlass::cast(_klass)->transitive_interfaces(); 1661 _ti_index = 0; 1662 _change_type = Change_new_type; 1663 return true; 1664 case Change_new_type: 1665 // fall through: 1666 _change_type = Change_new_sub; 1667 case Change_new_sub: 1668 // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277 1669 { 1670 _klass = InstanceKlass::cast(_klass)->super(); 1671 if (_klass != NULL) { 1672 return true; 1673 } 1674 } 1675 // else set up _ti_limit and fall through: 1676 _ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length(); 1677 _change_type = Change_new_impl; 1678 case Change_new_impl: 1679 if (_ti_index < _ti_limit) { 1680 _klass = _ti_base->at(_ti_index++); 1681 return true; 1682 } 1683 // fall through: 1684 _change_type = NO_CHANGE; // iterator is exhausted 1685 case NO_CHANGE: 1686 break; 1687 default: 1688 ShouldNotReachHere(); 1689 } 1690 return false; 1691} 1692 1693void KlassDepChange::initialize() { 1694 // entire transaction must be under this lock: 1695 assert_lock_strong(Compile_lock); 1696 1697 // Mark all dependee and all its superclasses 1698 // Mark transitive interfaces 1699 for (ContextStream str(*this); str.next(); ) { 1700 Klass* d = str.klass(); 1701 assert(!InstanceKlass::cast(d)->is_marked_dependent(), "checking"); 1702 InstanceKlass::cast(d)->set_is_marked_dependent(true); 1703 } 1704} 1705 1706KlassDepChange::~KlassDepChange() { 1707 // Unmark all dependee and all its superclasses 1708 // Unmark transitive interfaces 1709 for (ContextStream str(*this); str.next(); ) { 1710 Klass* d = str.klass(); 1711 InstanceKlass::cast(d)->set_is_marked_dependent(false); 1712 } 1713} 1714 1715bool KlassDepChange::involves_context(Klass* k) { 1716 if (k == NULL || !k->oop_is_instance()) { 1717 return false; 1718 } 1719 InstanceKlass* ik = InstanceKlass::cast(k); 1720 bool is_contained = ik->is_marked_dependent(); 1721 assert(is_contained == new_type()->is_subtype_of(k), 1722 "correct marking of potential context types"); 1723 return is_contained; 1724} 1725 1726#ifndef PRODUCT 1727void Dependencies::print_statistics() { 1728 if (deps_find_witness_print != 0) { 1729 // Call one final time, to flush out the data. 1730 deps_find_witness_print = -1; 1731 count_find_witness_calls(); 1732 } 1733} 1734#endif 1735