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