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