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