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_cr("  witness = %s%s",
 841                   (put_star? "*": ""),
 842                   witness->external_name());
 843   }
 844 }
 845 
 846 void Dependencies::DepStream::log_dependency(Klass* witness) {
 847   if (_deps == nullptr && xtty == nullptr)  return;  // fast cutout for runtime
 848   ResourceMark rm;
 849   const int nargs = argument_count();
 850   GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);
 851   for (int j = 0; j < nargs; j++) {
 852     if (is_oop_argument(j)) {
 853       args->push(argument_oop(j));
 854     } else {
 855       args->push(argument(j));
 856     }
 857   }
 858   int argslen = args->length();
 859   if (_deps != nullptr && _deps->log() != nullptr) {
 860     if (ciEnv::current() != nullptr) {
 861       Dependencies::write_dependency_to(_deps->log(), type(), args, witness);
 862     } else {
 863       // Treat the CompileLog as an xmlstream instead
 864       Dependencies::write_dependency_to((xmlStream*)_deps->log(), type(), args, witness);
 865     }
 866   } else {
 867     Dependencies::write_dependency_to(xtty, type(), args, witness);
 868   }
 869   guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");
 870 }
 871 
 872 void Dependencies::DepStream::print_dependency(outputStream* st, Klass* witness, bool verbose) {
 873   ResourceMark rm;
 874   int nargs = argument_count();
 875   GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);
 876   for (int j = 0; j < nargs; j++) {
 877     if (is_oop_argument(j)) {
 878       args->push(argument_oop(j));
 879     } else {
 880       args->push(argument(j));
 881     }
 882   }
 883   int argslen = args->length();
 884   Dependencies::print_dependency(type(), args, witness, st);
 885   if (verbose) {
 886     if (_code != nullptr) {
 887       st->print("  code: ");
 888       _code->print_value_on(st);
 889       st->cr();
 890     }
 891   }
 892   guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");
 893 }
 894 
 895 
 896 /// Dependency stream support (decodes dependencies from an nmethod):
 897 
 898 #ifdef ASSERT
 899 void Dependencies::DepStream::initial_asserts(size_t byte_limit) {
 900   assert(must_be_in_vm(), "raw oops here");
 901   _byte_limit = byte_limit;
 902   _type       = (DepType)(end_marker-1);  // defeat "already at end" assert
 903   assert((_code!=nullptr) + (_deps!=nullptr) == 1, "one or t'other");
 904 }
 905 #endif //ASSERT
 906 
 907 bool Dependencies::DepStream::next() {
 908   assert(_type != end_marker, "already at end");
 909   if (_bytes.position() == 0 && _code != nullptr
 910       && _code->dependencies_size() == 0) {
 911     // Method has no dependencies at all.
 912     return false;
 913   }
 914   int code_byte = (_bytes.read_byte() & 0xFF);
 915   if (code_byte == end_marker) {
 916     DEBUG_ONLY(_type = end_marker);
 917     return false;
 918   } else {
 919     int ctxk_bit = (code_byte & Dependencies::default_context_type_bit);
 920     code_byte -= ctxk_bit;
 921     DepType dept = (DepType)code_byte;
 922     _type = dept;
 923     Dependencies::check_valid_dependency_type(dept);
 924     int stride = _dep_args[dept];
 925     assert(stride == dep_args(dept), "sanity");
 926     int skipj = -1;
 927     if (ctxk_bit != 0) {
 928       skipj = 0;  // currently the only context argument is at zero
 929       assert(skipj == dep_context_arg(dept), "zero arg always ctxk");
 930     }
 931     for (int j = 0; j < stride; j++) {
 932       _xi[j] = (j == skipj)? 0: _bytes.read_int();
 933     }
 934     DEBUG_ONLY(_xi[stride] = -1);   // help detect overruns
 935     return true;
 936   }
 937 }
 938 
 939 inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) {
 940   Metadata* o = nullptr;
 941   if (_code != nullptr) {
 942     o = _code->metadata_at(i);
 943   } else {
 944     o = _deps->oop_recorder()->metadata_at(i);
 945   }
 946   return o;
 947 }
 948 
 949 inline oop Dependencies::DepStream::recorded_oop_at(int i) {
 950   return (_code != nullptr)
 951          ? _code->oop_at(i)
 952     : JNIHandles::resolve(_deps->oop_recorder()->oop_at(i));
 953 }
 954 
 955 Metadata* Dependencies::DepStream::argument(int i) {
 956   Metadata* result = recorded_metadata_at(argument_index(i));
 957 
 958   if (result == nullptr) { // Explicit context argument can be compressed
 959     int ctxkj = dep_context_arg(type());  // -1 if no explicit context arg
 960     if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) {
 961       result = ctxk_encoded_as_null(type(), argument(ctxkj+1));
 962     }
 963   }
 964 
 965   assert(result == nullptr || result->is_klass() || result->is_method(), "must be");
 966   return result;
 967 }
 968 
 969 /**
 970  * Returns a unique identifier for each dependency argument.
 971  */
 972 uintptr_t Dependencies::DepStream::get_identifier(int i) {
 973   if (is_oop_argument(i)) {
 974     return (uintptr_t)(oopDesc*)argument_oop(i);
 975   } else {
 976     return (uintptr_t)argument(i);
 977   }
 978 }
 979 
 980 oop Dependencies::DepStream::argument_oop(int i) {
 981   oop result = recorded_oop_at(argument_index(i));
 982   assert(oopDesc::is_oop_or_null(result), "must be");
 983   return result;
 984 }
 985 
 986 InstanceKlass* Dependencies::DepStream::context_type() {
 987   assert(must_be_in_vm(), "raw oops here");
 988 
 989   // Most dependencies have an explicit context type argument.
 990   {
 991     int ctxkj = dep_context_arg(type());  // -1 if no explicit context arg
 992     if (ctxkj >= 0) {
 993       Metadata* k = argument(ctxkj);
 994       assert(k != nullptr && k->is_klass(), "type check");
 995       return InstanceKlass::cast((Klass*)k);
 996     }
 997   }
 998 
 999   // Some dependencies are using the klass of the first object
1000   // argument as implicit context type.
1001   {
1002     int ctxkj = dep_implicit_context_arg(type());
1003     if (ctxkj >= 0) {
1004       Klass* k = argument_oop(ctxkj)->klass();
1005       assert(k != nullptr, "type check");
1006       return InstanceKlass::cast(k);
1007     }
1008   }
1009 
1010   // And some dependencies don't have a context type at all,
1011   // e.g. evol_method.
1012   return nullptr;
1013 }
1014 
1015 // ----------------- DependencySignature --------------------------------------
1016 bool DependencySignature::equals(DependencySignature const& s1, DependencySignature const& s2) {
1017   if ((s1.type() != s2.type()) || (s1.args_count() != s2.args_count())) {
1018     return false;
1019   }
1020 
1021   for (int i = 0; i < s1.args_count(); i++) {
1022     if (s1.arg(i) != s2.arg(i)) {
1023       return false;
1024     }
1025   }
1026   return true;
1027 }
1028 
1029 /// Checking dependencies
1030 
1031 // This hierarchy walker inspects subtypes of a given type, trying to find a "bad" class which breaks a dependency.
1032 // Such a class is called a "witness" to the broken dependency.
1033 // While searching around, we ignore "participants", which are already known to the dependency.
1034 class AbstractClassHierarchyWalker {
1035  public:
1036   enum { PARTICIPANT_LIMIT = 3 };
1037 
1038  private:
1039   // if non-zero, tells how many witnesses to convert to participants
1040   uint _record_witnesses;
1041 
1042   // special classes which are not allowed to be witnesses:
1043   Klass* _participants[PARTICIPANT_LIMIT+1];
1044   uint   _num_participants;
1045 
1046 #ifdef ASSERT
1047   uint _nof_requests; // one-shot walker
1048 #endif // ASSERT
1049 
1050   static PerfCounter* _perf_find_witness_anywhere_calls_count;
1051   static PerfCounter* _perf_find_witness_anywhere_steps_count;
1052   static PerfCounter* _perf_find_witness_in_calls_count;
1053 
1054  protected:
1055   virtual Klass* find_witness_in(KlassDepChange& changes) = 0;
1056   virtual Klass* find_witness_anywhere(InstanceKlass* context_type) = 0;
1057 
1058   AbstractClassHierarchyWalker(Klass* participant) : _record_witnesses(0), _num_participants(0)
1059 #ifdef ASSERT
1060   , _nof_requests(0)
1061 #endif // ASSERT
1062   {
1063     for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) {
1064       _participants[i] = nullptr;
1065     }
1066     if (participant != nullptr) {
1067       add_participant(participant);
1068     }
1069   }
1070 
1071   bool is_participant(Klass* k) {
1072     for (uint i = 0; i < _num_participants; i++) {
1073       if (_participants[i] == k) {
1074         return true;
1075       }
1076     }
1077     return false;
1078   }
1079 
1080   bool record_witness(Klass* witness) {
1081     if (_record_witnesses > 0) {
1082       --_record_witnesses;
1083       add_participant(witness);
1084       return false; // not a witness
1085     } else {
1086       return true; // is a witness
1087     }
1088   }
1089 
1090   class CountingClassHierarchyIterator : public ClassHierarchyIterator {
1091    private:
1092     jlong _nof_steps;
1093    public:
1094     CountingClassHierarchyIterator(InstanceKlass* root) : ClassHierarchyIterator(root), _nof_steps(0) {}
1095 
1096     void next() {
1097       _nof_steps++;
1098       ClassHierarchyIterator::next();
1099     }
1100 
1101     ~CountingClassHierarchyIterator() {
1102       if (UsePerfData) {
1103         _perf_find_witness_anywhere_steps_count->inc(_nof_steps);
1104       }
1105     }
1106   };
1107 
1108  public:
1109   uint num_participants() { return _num_participants; }
1110   Klass* participant(uint n) {
1111     assert(n <= _num_participants, "oob");
1112     if (n < _num_participants) {
1113       return _participants[n];
1114     } else {
1115       return nullptr;
1116     }
1117   }
1118 
1119   void add_participant(Klass* participant) {
1120     assert(!is_participant(participant), "sanity");
1121     assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob");
1122     uint np = _num_participants++;
1123     _participants[np] = participant;
1124   }
1125 
1126   void record_witnesses(uint add) {
1127     if (add > PARTICIPANT_LIMIT)  add = PARTICIPANT_LIMIT;
1128     assert(_num_participants + add < PARTICIPANT_LIMIT, "oob");
1129     _record_witnesses = add;
1130   }
1131 
1132   Klass* find_witness(InstanceKlass* context_type, KlassDepChange* changes = nullptr);
1133 
1134   static void init();
1135   static void print_statistics();
1136 };
1137 
1138 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_calls_count = nullptr;
1139 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_steps_count = nullptr;
1140 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_in_calls_count       = nullptr;
1141 
1142 void AbstractClassHierarchyWalker::init() {
1143   if (UsePerfData) {
1144     EXCEPTION_MARK;
1145     _perf_find_witness_anywhere_calls_count =
1146         PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhere", PerfData::U_Events, CHECK);
1147     _perf_find_witness_anywhere_steps_count =
1148         PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhereSteps", PerfData::U_Events, CHECK);
1149     _perf_find_witness_in_calls_count =
1150         PerfDataManager::create_counter(SUN_CI, "findWitnessIn", PerfData::U_Events, CHECK);
1151   }
1152 }
1153 
1154 Klass* AbstractClassHierarchyWalker::find_witness(InstanceKlass* context_type, KlassDepChange* changes) {
1155   // Current thread must be in VM (not native mode, as in CI):
1156   assert(must_be_in_vm(), "raw oops here");
1157   // Must not move the class hierarchy during this check:
1158   assert_locked_or_safepoint(Compile_lock);
1159   assert(_nof_requests++ == 0, "repeated requests are not supported");
1160 
1161   assert(changes == nullptr || changes->involves_context(context_type), "irrelevant dependency");
1162 
1163   // (Note: Interfaces do not have subclasses.)
1164   // If it is an interface, search its direct implementors.
1165   // (Their subclasses are additional indirect implementors. See InstanceKlass::add_implementor().)
1166   if (context_type->is_interface()) {
1167     int nof_impls = context_type->nof_implementors();
1168     if (nof_impls == 0) {
1169       return nullptr; // no implementors
1170     } else if (nof_impls == 1) { // unique implementor
1171       assert(context_type != context_type->implementor(), "not unique");
1172       context_type = context_type->implementor();
1173     } else { // nof_impls >= 2
1174       // Avoid this case: *I.m > { A.m, C }; B.m > C
1175       // Here, I.m has 2 concrete implementations, but m appears unique
1176       // as A.m, because the search misses B.m when checking C.
1177       // The inherited method B.m was getting missed by the walker
1178       // when interface 'I' was the starting point.
1179       // %%% Until this is fixed more systematically, bail out.
1180       return context_type;
1181     }
1182   }
1183   assert(!context_type->is_interface(), "no interfaces allowed");
1184 
1185   if (changes != nullptr) {
1186     if (UsePerfData) {
1187       _perf_find_witness_in_calls_count->inc();
1188     }
1189     return find_witness_in(*changes);
1190   } else {
1191     if (UsePerfData) {
1192       _perf_find_witness_anywhere_calls_count->inc();
1193     }
1194     return find_witness_anywhere(context_type);
1195   }
1196 }
1197 
1198 class ConcreteSubtypeFinder : public AbstractClassHierarchyWalker {
1199  private:
1200   bool is_witness(Klass* k);
1201 
1202  protected:
1203   virtual Klass* find_witness_in(KlassDepChange& changes);
1204   virtual Klass* find_witness_anywhere(InstanceKlass* context_type);
1205 
1206  public:
1207   ConcreteSubtypeFinder(Klass* participant = nullptr) : AbstractClassHierarchyWalker(participant) {}
1208 };
1209 
1210 bool ConcreteSubtypeFinder::is_witness(Klass* k) {
1211   if (Dependencies::is_concrete_klass(k)) {
1212     return record_witness(k); // concrete subtype
1213   } else {
1214     return false; // not a concrete class
1215   }
1216 }
1217 
1218 Klass* ConcreteSubtypeFinder::find_witness_in(KlassDepChange& changes) {
1219   // When looking for unexpected concrete types, do not look beneath expected ones:
1220   //  * CX > CC > C' is OK, even if C' is new.
1221   //  * CX > { CC,  C' } is not OK if C' is new, and C' is the witness.
1222   Klass* new_type = changes.as_new_klass_change()->new_type();
1223   assert(!is_participant(new_type), "only old classes are participants");
1224   // If the new type is a subtype of a participant, we are done.
1225   for (uint i = 0; i < num_participants(); i++) {
1226     if (changes.involves_context(participant(i))) {
1227       // new guy is protected from this check by previous participant
1228       return nullptr;
1229     }
1230   }
1231   if (is_witness(new_type)) {
1232     return new_type;
1233   }
1234   // No witness found.  The dependency remains unbroken.
1235   return nullptr;
1236 }
1237 
1238 Klass* ConcreteSubtypeFinder::find_witness_anywhere(InstanceKlass* context_type) {
1239   for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {
1240     Klass* sub = iter.klass();
1241     // Do not report participant types.
1242     if (is_participant(sub)) {
1243       // Don't walk beneath a participant since it hides witnesses.
1244       iter.skip_subclasses();
1245     } else if (is_witness(sub)) {
1246       return sub; // found a witness
1247     }
1248   }
1249   // No witness found.  The dependency remains unbroken.
1250   return nullptr;
1251 }
1252 
1253 class ConcreteMethodFinder : public AbstractClassHierarchyWalker {
1254  private:
1255   Symbol* _name;
1256   Symbol* _signature;
1257 
1258   // cache of method lookups
1259   Method* _found_methods[PARTICIPANT_LIMIT+1];
1260 
1261   bool is_witness(Klass* k);
1262 
1263  protected:
1264   virtual Klass* find_witness_in(KlassDepChange& changes);
1265   virtual Klass* find_witness_anywhere(InstanceKlass* context_type);
1266 
1267  public:
1268   bool witnessed_reabstraction_in_supers(Klass* k);
1269 
1270   ConcreteMethodFinder(Method* m, Klass* participant = nullptr) : AbstractClassHierarchyWalker(participant) {
1271     assert(m != nullptr && m->is_method(), "sanity");
1272     _name      = m->name();
1273     _signature = m->signature();
1274 
1275     for (int i = 0; i < PARTICIPANT_LIMIT+1; i++) {
1276       _found_methods[i] = nullptr;
1277     }
1278   }
1279 
1280   // Note:  If n==num_participants, returns nullptr.
1281   Method* found_method(uint n) {
1282     assert(n <= num_participants(), "oob");
1283     Method* fm = _found_methods[n];
1284     assert(n == num_participants() || fm != nullptr, "proper usage");
1285     if (fm != nullptr && fm->method_holder() != participant(n)) {
1286       // Default methods from interfaces can be added to classes. In
1287       // that case the holder of the method is not the class but the
1288       // interface where it's defined.
1289       assert(fm->is_default_method(), "sanity");
1290       return nullptr;
1291     }
1292     return fm;
1293   }
1294 
1295   void add_participant(Klass* participant) {
1296     AbstractClassHierarchyWalker::add_participant(participant);
1297     _found_methods[num_participants()] = nullptr;
1298   }
1299 
1300   bool record_witness(Klass* witness, Method* m) {
1301     _found_methods[num_participants()] = m;
1302     return AbstractClassHierarchyWalker::record_witness(witness);
1303   }
1304 
1305  private:
1306   static PerfCounter* _perf_find_witness_anywhere_calls_count;
1307   static PerfCounter* _perf_find_witness_anywhere_steps_count;
1308   static PerfCounter* _perf_find_witness_in_calls_count;
1309 
1310  public:
1311   static void init();
1312   static void print_statistics();
1313 };
1314 
1315 bool ConcreteMethodFinder::is_witness(Klass* k) {
1316   if (is_participant(k)) {
1317     return false; // do not report participant types
1318   }
1319   if (k->is_instance_klass()) {
1320     InstanceKlass* ik = InstanceKlass::cast(k);
1321     // Search class hierarchy first, skipping private implementations
1322     // as they never override any inherited methods
1323     Method* m = ik->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);
1324     if (Dependencies::is_concrete_method(m, ik)) {
1325       return record_witness(k, m); // concrete method found
1326     } else {
1327       // Check for re-abstraction of method
1328       if (!ik->is_interface() && m != nullptr && m->is_abstract()) {
1329         // Found a matching abstract method 'm' in the class hierarchy.
1330         // This is fine iff 'k' is an abstract class and all concrete subtypes
1331         // of 'k' override 'm' and are participates of the current search.
1332         ConcreteSubtypeFinder wf;
1333         for (uint i = 0; i < num_participants(); i++) {
1334           Klass* p = participant(i);
1335           wf.add_participant(p);
1336         }
1337         Klass* w = wf.find_witness(ik);
1338         if (w != nullptr) {
1339           Method* wm = InstanceKlass::cast(w)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);
1340           if (!Dependencies::is_concrete_method(wm, w)) {
1341             // Found a concrete subtype 'w' which does not override abstract method 'm'.
1342             // Bail out because 'm' could be called with 'w' as receiver (leading to an
1343             // AbstractMethodError) and thus the method we are looking for is not unique.
1344             return record_witness(k, m);
1345           }
1346         }
1347       }
1348       // Check interface defaults also, if any exist.
1349       Array<Method*>* default_methods = ik->default_methods();
1350       if (default_methods != nullptr) {
1351         Method* dm = ik->find_method(default_methods, _name, _signature);
1352         if (Dependencies::is_concrete_method(dm, nullptr)) {
1353           return record_witness(k, dm); // default method found
1354         }
1355       }
1356       return false; // no concrete method found
1357     }
1358   } else {
1359     return false; // no methods to find in an array type
1360   }
1361 }
1362 
1363 Klass* ConcreteMethodFinder::find_witness_in(KlassDepChange& changes) {
1364   // When looking for unexpected concrete methods, look beneath expected ones, to see if there are overrides.
1365   //  * CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness.
1366   Klass* new_type = changes.as_new_klass_change()->new_type();
1367   assert(!is_participant(new_type), "only old classes are participants");
1368   if (is_witness(new_type)) {
1369     return new_type;
1370   } else {
1371     // No witness found, but is_witness() doesn't detect method re-abstraction in case of spot-checking.
1372     if (witnessed_reabstraction_in_supers(new_type)) {
1373       return new_type;
1374     }
1375   }
1376   // No witness found.  The dependency remains unbroken.
1377   return nullptr;
1378 }
1379 
1380 bool ConcreteMethodFinder::witnessed_reabstraction_in_supers(Klass* k) {
1381   if (!k->is_instance_klass()) {
1382     return false; // no methods to find in an array type
1383   } else {
1384     // Looking for a case when an abstract method is inherited into a concrete class.
1385     if (Dependencies::is_concrete_klass(k) && !k->is_interface()) {
1386       Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);
1387       if (m != nullptr) {
1388         return false; // no reabstraction possible: local method found
1389       }
1390       for (InstanceKlass* super = k->java_super(); super != nullptr; super = super->java_super()) {
1391         m = super->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip);
1392         if (m != nullptr) { // inherited method found
1393           if (m->is_abstract() || m->is_overpass()) {
1394             return record_witness(super, m); // abstract method found
1395           }
1396           return false;
1397         }
1398       }
1399       // Miranda.
1400       return true;
1401     }
1402     return false;
1403   }
1404 }
1405 
1406 
1407 Klass* ConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) {
1408   // Walk hierarchy under a context type, looking for unexpected types.
1409   for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {
1410     Klass* sub = iter.klass();
1411     if (is_witness(sub)) {
1412       return sub; // found a witness
1413     }
1414   }
1415   // No witness found.  The dependency remains unbroken.
1416   return nullptr;
1417 }
1418 
1419 // For some method m and some class ctxk (subclass of method holder),
1420 // enumerate all distinct overrides of m in concrete subclasses of ctxk.
1421 // It relies on vtable/itable information to perform method selection on each linked subclass
1422 // and ignores all non yet linked ones (speculatively treat them as "effectively abstract").
1423 class LinkedConcreteMethodFinder : public AbstractClassHierarchyWalker {
1424  private:
1425   InstanceKlass* _resolved_klass;   // resolved class (JVMS-5.4.3.1)
1426   InstanceKlass* _declaring_klass;  // the holder of resolved method (JVMS-5.4.3.3)
1427   int            _vtable_index;     // vtable/itable index of the resolved method
1428   bool           _do_itable_lookup; // choose between itable and vtable lookup logic
1429 
1430   // cache of method lookups
1431   Method* _found_methods[PARTICIPANT_LIMIT+1];
1432 
1433   bool is_witness(Klass* k);
1434   Method* select_method(InstanceKlass* recv_klass);
1435   static int compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, bool& is_itable_index);
1436   static bool is_concrete_klass(InstanceKlass* ik);
1437 
1438   void add_participant(Method* m, Klass* participant) {
1439     uint np = num_participants();
1440     AbstractClassHierarchyWalker::add_participant(participant);
1441     assert(np + 1 == num_participants(), "sanity");
1442     _found_methods[np] = m; // record the method for the participant
1443   }
1444 
1445   bool record_witness(Klass* witness, Method* m) {
1446     for (uint i = 0; i < num_participants(); i++) {
1447       if (found_method(i) == m) {
1448         return false; // already recorded
1449       }
1450     }
1451     // Record not yet seen method.
1452     _found_methods[num_participants()] = m;
1453     return AbstractClassHierarchyWalker::record_witness(witness);
1454   }
1455 
1456   void initialize(Method* participant) {
1457     for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) {
1458       _found_methods[i] = nullptr;
1459     }
1460     if (participant != nullptr) {
1461       add_participant(participant, participant->method_holder());
1462     }
1463   }
1464 
1465  protected:
1466   virtual Klass* find_witness_in(KlassDepChange& changes);
1467   virtual Klass* find_witness_anywhere(InstanceKlass* context_type);
1468 
1469  public:
1470   // In order to perform method selection, the following info is needed:
1471   //  (1) interface or virtual call;
1472   //  (2) vtable/itable index;
1473   //  (3) declaring class (in case of interface call).
1474   //
1475   // It is prepared based on the results of method resolution: resolved class and resolved method (as specified in JVMS-5.4.3.3).
1476   // Optionally, a method which was previously determined as a unique target (uniqm) is added as a participant
1477   // to enable dependency spot-checking and speed up the search.
1478   LinkedConcreteMethodFinder(InstanceKlass* resolved_klass, Method* resolved_method, Method* uniqm = nullptr) : AbstractClassHierarchyWalker(nullptr) {
1479     assert(UseVtableBasedCHA, "required");
1480     assert(resolved_klass->is_linked(), "required");
1481     assert(resolved_method->method_holder()->is_linked(), "required");
1482     assert(!resolved_method->can_be_statically_bound(), "no vtable index available");
1483 
1484     _resolved_klass  = resolved_klass;
1485     _declaring_klass = resolved_method->method_holder();
1486     _vtable_index    = compute_vtable_index(resolved_klass, resolved_method,
1487                                             _do_itable_lookup); // out parameter
1488     assert(_vtable_index >= 0, "invalid vtable index");
1489 
1490     initialize(uniqm);
1491   }
1492 
1493   // Note:  If n==num_participants, returns nullptr.
1494   Method* found_method(uint n) {
1495     assert(n <= num_participants(), "oob");
1496     assert(participant(n) != nullptr || n == num_participants(), "proper usage");
1497     return _found_methods[n];
1498   }
1499 };
1500 
1501 Klass* LinkedConcreteMethodFinder::find_witness_in(KlassDepChange& changes) {
1502   Klass* type = changes.type();
1503 
1504   assert(!is_participant(type), "only old classes are participants");
1505 
1506   if (is_witness(type)) {
1507     return type;
1508   }
1509   return nullptr; // No witness found.  The dependency remains unbroken.
1510 }
1511 
1512 Klass* LinkedConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) {
1513   for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) {
1514     Klass* sub = iter.klass();
1515     if (is_witness(sub)) {
1516       return sub;
1517     }
1518     if (sub->is_instance_klass() && !InstanceKlass::cast(sub)->is_linked()) {
1519       iter.skip_subclasses(); // ignore not yet linked classes
1520     }
1521   }
1522   return nullptr; // No witness found. The dependency remains unbroken.
1523 }
1524 
1525 bool LinkedConcreteMethodFinder::is_witness(Klass* k) {
1526   if (is_participant(k)) {
1527     return false; // do not report participant types
1528   } else if (k->is_instance_klass()) {
1529     InstanceKlass* ik = InstanceKlass::cast(k);
1530     if (is_concrete_klass(ik)) {
1531       Method* m = select_method(ik);
1532       return record_witness(ik, m);
1533     } else {
1534       return false; // ignore non-concrete holder class
1535     }
1536   } else {
1537     return false; // no methods to find in an array type
1538   }
1539 }
1540 
1541 Method* LinkedConcreteMethodFinder::select_method(InstanceKlass* recv_klass) {
1542   Method* selected_method = nullptr;
1543   if (_do_itable_lookup) {
1544     assert(_declaring_klass->is_interface(), "sanity");
1545     bool implements_interface; // initialized by method_at_itable_or_null()
1546     selected_method = recv_klass->method_at_itable_or_null(_declaring_klass, _vtable_index,
1547                                                            implements_interface); // out parameter
1548     assert(implements_interface, "not implemented");
1549   } else {
1550     selected_method = recv_klass->method_at_vtable(_vtable_index);
1551   }
1552   return selected_method; // nullptr when corresponding slot is empty (AbstractMethodError case)
1553 }
1554 
1555 int LinkedConcreteMethodFinder::compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method,
1556                                                      // out parameter
1557                                                      bool& is_itable_index) {
1558   if (resolved_klass->is_interface() && resolved_method->has_itable_index()) {
1559     is_itable_index = true;
1560     return resolved_method->itable_index();
1561   }
1562   // Check for default or miranda method first.
1563   InstanceKlass* declaring_klass = resolved_method->method_holder();
1564   if (!resolved_klass->is_interface() && declaring_klass->is_interface()) {
1565     is_itable_index = false;
1566     return resolved_klass->vtable_index_of_interface_method(resolved_method);
1567   }
1568   // At this point we are sure that resolved_method is virtual and not
1569   // a default or miranda method; therefore, it must have a valid vtable index.
1570   assert(resolved_method->has_vtable_index(), "");
1571   is_itable_index = false;
1572   return resolved_method->vtable_index();
1573 }
1574 
1575 bool LinkedConcreteMethodFinder::is_concrete_klass(InstanceKlass* ik) {
1576   if (!Dependencies::is_concrete_klass(ik)) {
1577     return false; // not concrete
1578   }
1579   if (ik->is_interface()) {
1580     return false; // interfaces aren't concrete
1581   }
1582   if (!ik->is_linked()) {
1583     return false; // not yet linked classes don't have instances
1584   }
1585   return true;
1586 }
1587 
1588 #ifdef ASSERT
1589 // Assert that m is inherited into ctxk, without intervening overrides.
1590 // (May return true even if this is not true, in corner cases where we punt.)
1591 bool Dependencies::verify_method_context(InstanceKlass* ctxk, Method* m) {
1592   if (m->is_private()) {
1593     return false; // Quick lose.  Should not happen.
1594   }
1595   if (m->method_holder() == ctxk) {
1596     return true;  // Quick win.
1597   }
1598   if (!(m->is_public() || m->is_protected())) {
1599     // The override story is complex when packages get involved.
1600     return true;  // Must punt the assertion to true.
1601   }
1602   Method* lm = ctxk->lookup_method(m->name(), m->signature());
1603   if (lm == nullptr) {
1604     // It might be an interface method
1605     lm = ctxk->lookup_method_in_ordered_interfaces(m->name(), m->signature());
1606   }
1607   if (lm == m) {
1608     // Method m is inherited into ctxk.
1609     return true;
1610   }
1611   if (lm != nullptr) {
1612     if (!(lm->is_public() || lm->is_protected())) {
1613       // Method is [package-]private, so the override story is complex.
1614       return true;  // Must punt the assertion to true.
1615     }
1616     if (lm->is_static()) {
1617       // Static methods don't override non-static so punt
1618       return true;
1619     }
1620     if (!Dependencies::is_concrete_method(lm, ctxk) &&
1621         !Dependencies::is_concrete_method(m, ctxk)) {
1622       // They are both non-concrete
1623       if (lm->method_holder()->is_subtype_of(m->method_holder())) {
1624         // Method m is overridden by lm, but both are non-concrete.
1625         return true;
1626       }
1627       if (lm->method_holder()->is_interface() && m->method_holder()->is_interface() &&
1628           ctxk->is_subtype_of(m->method_holder()) && ctxk->is_subtype_of(lm->method_holder())) {
1629         // Interface method defined in multiple super interfaces
1630         return true;
1631       }
1632     }
1633   }
1634   ResourceMark rm;
1635   tty->print_cr("Dependency method not found in the associated context:");
1636   tty->print_cr("  context = %s", ctxk->external_name());
1637   tty->print(   "  method = "); m->print_short_name(tty); tty->cr();
1638   if (lm != nullptr) {
1639     tty->print( "  found = "); lm->print_short_name(tty); tty->cr();
1640   }
1641   return false;
1642 }
1643 #endif // ASSERT
1644 
1645 bool Dependencies::is_concrete_klass(Klass* k) {
1646   if (k->is_abstract())  return false;
1647   // %%% We could treat classes which are concrete but
1648   // have not yet been instantiated as virtually abstract.
1649   // This would require a deoptimization barrier on first instantiation.
1650   //if (k->is_not_instantiated())  return false;
1651   return true;
1652 }
1653 
1654 bool Dependencies::is_concrete_method(Method* m, Klass* k) {
1655   // nullptr is not a concrete method.
1656   if (m == nullptr) {
1657     return false;
1658   }
1659   // Statics are irrelevant to virtual call sites.
1660   if (m->is_static()) {
1661     return false;
1662   }
1663   // Abstract methods are not concrete.
1664   if (m->is_abstract()) {
1665     return false;
1666   }
1667   // Overpass (error) methods are not concrete if k is abstract.
1668   if (m->is_overpass() && k != nullptr) {
1669      return !k->is_abstract();
1670   }
1671   // Note "true" is conservative answer: overpass clause is false if k == nullptr,
1672   // implies return true if answer depends on overpass clause.
1673   return true;
1674  }
1675 
1676 Klass* Dependencies::find_finalizable_subclass(InstanceKlass* ik) {
1677   for (ClassHierarchyIterator iter(ik); !iter.done(); iter.next()) {
1678     Klass* sub = iter.klass();
1679     if (sub->has_finalizer() && !sub->is_interface()) {
1680       return sub;
1681     }
1682   }
1683   return nullptr; // not found
1684 }
1685 
1686 bool Dependencies::is_concrete_klass(ciInstanceKlass* k) {
1687   if (k->is_abstract())  return false;
1688   // We could also return false if k does not yet appear to be
1689   // instantiated, if the VM version supports this distinction also.
1690   //if (k->is_not_instantiated())  return false;
1691   return true;
1692 }
1693 
1694 bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) {
1695   return k->has_finalizable_subclass();
1696 }
1697 
1698 // Any use of the contents (bytecodes) of a method must be
1699 // marked by an "evol_method" dependency, if those contents
1700 // can change.  (Note: A method is always dependent on itself.)
1701 Klass* Dependencies::check_evol_method(Method* m) {
1702   assert(must_be_in_vm(), "raw oops here");
1703   // Did somebody do a JVMTI RedefineClasses while our backs were turned?
1704   // Or is there a now a breakpoint?
1705   // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.)
1706   if (m->is_old()
1707       || m->number_of_breakpoints() > 0
1708       || m->mismatch()) {
1709     return m->method_holder();
1710   } else {
1711     return nullptr;
1712   }
1713 }
1714 
1715 // This is a strong assertion:  It is that the given type
1716 // has no subtypes whatever.  It is most useful for
1717 // optimizing checks on reflected types or on array types.
1718 // (Checks on types which are derived from real instances
1719 // can be optimized more strongly than this, because we
1720 // know that the checked type comes from a concrete type,
1721 // and therefore we can disregard abstract types.)
1722 Klass* Dependencies::check_leaf_type(InstanceKlass* ctxk) {
1723   assert(must_be_in_vm(), "raw oops here");
1724   assert_locked_or_safepoint(Compile_lock);
1725   Klass* sub = ctxk->subklass();
1726   if (sub != nullptr) {
1727     return sub;
1728   } else if (ctxk->nof_implementors() != 0) {
1729     // if it is an interface, it must be unimplemented
1730     // (if it is not an interface, nof_implementors is always zero)
1731     InstanceKlass* impl = ctxk->implementor();
1732     assert(impl != nullptr, "must be set");
1733     return impl;
1734   } else {
1735     return nullptr;
1736   }
1737 }
1738 
1739 // Test the assertion that conck is the only concrete subtype* of ctxk.
1740 // The type conck itself is allowed to have have further concrete subtypes.
1741 // This allows the compiler to narrow occurrences of ctxk by conck,
1742 // when dealing with the types of actual instances.
1743 Klass* Dependencies::check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk,
1744                                                                  Klass* conck,
1745                                                                  NewKlassDepChange* changes) {
1746   ConcreteSubtypeFinder wf(conck);
1747   Klass* k = wf.find_witness(ctxk, changes);
1748   return k;
1749 }
1750 
1751 
1752 // Find the unique concrete proper subtype of ctxk, or nullptr if there
1753 // is more than one concrete proper subtype.  If there are no concrete
1754 // proper subtypes, return ctxk itself, whether it is concrete or not.
1755 // The returned subtype is allowed to have have further concrete subtypes.
1756 // That is, return CC1 for CX > CC1 > CC2, but nullptr for CX > { CC1, CC2 }.
1757 Klass* Dependencies::find_unique_concrete_subtype(InstanceKlass* ctxk) {
1758   ConcreteSubtypeFinder wf(ctxk);  // Ignore ctxk when walking.
1759   wf.record_witnesses(1);          // Record one other witness when walking.
1760   Klass* wit = wf.find_witness(ctxk);
1761   if (wit != nullptr)  return nullptr;   // Too many witnesses.
1762   Klass* conck = wf.participant(0);
1763   if (conck == nullptr) {
1764     return ctxk;                   // Return ctxk as a flag for "no subtypes".
1765   } else {
1766 #ifndef PRODUCT
1767     // Make sure the dependency mechanism will pass this discovery:
1768     if (VerifyDependencies) {
1769       // Turn off dependency tracing while actually testing deps.
1770       FlagSetting fs(_verify_in_progress, true);
1771       if (!Dependencies::is_concrete_klass(ctxk)) {
1772         guarantee(nullptr == (void *)
1773                   check_abstract_with_unique_concrete_subtype(ctxk, conck),
1774                   "verify dep.");
1775       }
1776     }
1777 #endif //PRODUCT
1778     return conck;
1779   }
1780 }
1781 
1782 // Try to determine whether root method in some context is concrete or not based on the information about the unique method
1783 // in that context. It exploits the fact that concrete root method is always inherited into the context when there's a unique method.
1784 // Hence, unique method holder is always a supertype of the context class when root method is concrete.
1785 // Examples for concrete_root_method
1786 //      C (C.m uniqm)
1787 //      |
1788 //      CX (ctxk) uniqm is inherited into context.
1789 //
1790 //      CX (ctxk) (CX.m uniqm) here uniqm is defined in ctxk.
1791 // Examples for !concrete_root_method
1792 //      CX (ctxk)
1793 //      |
1794 //      C (C.m uniqm) uniqm is in subtype of ctxk.
1795 bool Dependencies::is_concrete_root_method(Method* uniqm, InstanceKlass* ctxk) {
1796   if (uniqm == nullptr) {
1797     return false; // match Dependencies::is_concrete_method() behavior
1798   }
1799   // Theoretically, the "direction" of subtype check matters here.
1800   // On one hand, in case of interface context with a single implementor, uniqm can be in a superclass of the implementor which
1801   // is not related to context class.
1802   // On another hand, uniqm could come from an interface unrelated to the context class, but right now it is not possible:
1803   // it is required that uniqm->method_holder() is the participant (uniqm->method_holder() <: ctxk), hence a default method
1804   // can't be used as unique.
1805   if (ctxk->is_interface()) {
1806     InstanceKlass* implementor = ctxk->implementor();
1807     assert(implementor != ctxk, "single implementor only"); // should have been invalidated earlier
1808     ctxk = implementor;
1809   }
1810   InstanceKlass* holder = uniqm->method_holder();
1811   assert(!holder->is_interface(), "no default methods allowed");
1812   assert(ctxk->is_subclass_of(holder) || holder->is_subclass_of(ctxk), "not related");
1813   return ctxk->is_subclass_of(holder);
1814 }
1815 
1816 // If a class (or interface) has a unique concrete method uniqm, return nullptr.
1817 // Otherwise, return a class that contains an interfering method.
1818 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk,
1819                                                   Method* uniqm,
1820                                                   NewKlassDepChange* changes) {
1821   ConcreteMethodFinder wf(uniqm, uniqm->method_holder());
1822   Klass* k = wf.find_witness(ctxk, changes);
1823   if (k != nullptr) {
1824     return k;
1825   }
1826   if (!Dependencies::is_concrete_root_method(uniqm, ctxk) || changes != nullptr) {
1827     Klass* conck = find_witness_AME(ctxk, uniqm, changes);
1828     if (conck != nullptr) {
1829       // Found a concrete subtype 'conck' which does not override abstract root method.
1830       return conck;
1831     }
1832   }
1833   return nullptr;
1834 }
1835 
1836 Klass* Dependencies::check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes) {
1837   assert(ctxk->is_interface(), "sanity");
1838   assert(ctxk->nof_implementors() > 0, "no implementors");
1839   if (ctxk->nof_implementors() == 1) {
1840     assert(ctxk->implementor() == uniqk, "sanity");
1841     return nullptr;
1842   }
1843   return ctxk; // no unique implementor
1844 }
1845 
1846 // Search for AME.
1847 // There are two version of checks.
1848 //   1) Spot checking version(Classload time). Newly added class is checked for AME.
1849 //      Checks whether abstract/overpass method is inherited into/declared in newly added concrete class.
1850 //   2) Compile time analysis for abstract/overpass(abstract klass) root_m. The non uniqm subtrees are checked for concrete classes.
1851 Klass* Dependencies::find_witness_AME(InstanceKlass* ctxk, Method* m, KlassDepChange* changes) {
1852   if (m != nullptr) {
1853     if (changes != nullptr) {
1854       // Spot checking version.
1855       ConcreteMethodFinder wf(m);
1856       Klass* new_type = changes->as_new_klass_change()->new_type();
1857       if (wf.witnessed_reabstraction_in_supers(new_type)) {
1858         return new_type;
1859       }
1860     } else {
1861       // Note: It is required that uniqm->method_holder() is the participant (see ClassHierarchyWalker::found_method()).
1862       ConcreteSubtypeFinder wf(m->method_holder());
1863       Klass* conck = wf.find_witness(ctxk);
1864       if (conck != nullptr) {
1865         Method* cm = InstanceKlass::cast(conck)->find_instance_method(m->name(), m->signature(), Klass::PrivateLookupMode::skip);
1866         if (!Dependencies::is_concrete_method(cm, conck)) {
1867           return conck;
1868         }
1869       }
1870     }
1871   }
1872   return nullptr;
1873 }
1874 
1875 // This function is used by find_unique_concrete_method(non vtable based)
1876 // to check whether subtype method overrides the base method.
1877 static bool overrides(Method* sub_m, Method* base_m) {
1878   assert(base_m != nullptr, "base method should be non null");
1879   if (sub_m == nullptr) {
1880     return false;
1881   }
1882   /**
1883    *  If base_m is public or protected then sub_m always overrides.
1884    *  If base_m is !public, !protected and !private (i.e. base_m is package private)
1885    *  then sub_m should be in the same package as that of base_m.
1886    *  For package private base_m this is conservative approach as it allows only subset of all allowed cases in
1887    *  the jvm specification.
1888    **/
1889   if (base_m->is_public() || base_m->is_protected() ||
1890       base_m->method_holder()->is_same_class_package(sub_m->method_holder())) {
1891     return true;
1892   }
1893   return false;
1894 }
1895 
1896 // Find the set of all non-abstract methods under ctxk that match m.
1897 // (The method m must be defined or inherited in ctxk.)
1898 // Include m itself in the set, unless it is abstract.
1899 // If this set has exactly one element, return that element.
1900 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass** participant) {
1901   // Return nullptr if m is marked old; must have been a redefined method.
1902   if (m->is_old()) {
1903     return nullptr;
1904   }
1905   if (m->is_default_method()) {
1906     return nullptr; // not supported
1907   }
1908   assert(verify_method_context(ctxk, m), "proper context");
1909   ConcreteMethodFinder wf(m);
1910   wf.record_witnesses(1);
1911   Klass* wit = wf.find_witness(ctxk);
1912   if (wit != nullptr)  return nullptr;  // Too many witnesses.
1913   Method* fm = wf.found_method(0);  // Will be nullptr if num_parts == 0.
1914   if (participant != nullptr) {
1915     (*participant) = wf.participant(0);
1916   }
1917   if (!Dependencies::is_concrete_method(fm, nullptr)) {
1918     fm = nullptr; // ignore abstract methods
1919   }
1920   if (Dependencies::is_concrete_method(m, ctxk)) {
1921     if (fm == nullptr) {
1922       // It turns out that m was always the only implementation.
1923       fm = m;
1924     } else if (fm != m) {
1925       // Two conflicting implementations after all.
1926       // (This can happen if m is inherited into ctxk and fm overrides it.)
1927       return nullptr;
1928     }
1929   } else if (Dependencies::find_witness_AME(ctxk, fm) != nullptr) {
1930     // Found a concrete subtype which does not override abstract root method.
1931     return nullptr;
1932   } else if (!overrides(fm, m)) {
1933     // Found method doesn't override abstract root method.
1934     return nullptr;
1935   }
1936   assert(Dependencies::is_concrete_root_method(fm, ctxk) == Dependencies::is_concrete_method(m, ctxk), "mismatch");
1937 #ifndef PRODUCT
1938   // Make sure the dependency mechanism will pass this discovery:
1939   if (VerifyDependencies && fm != nullptr) {
1940     guarantee(nullptr == (void *)check_unique_concrete_method(ctxk, fm),
1941               "verify dep.");
1942   }
1943 #endif //PRODUCT
1944   return fm;
1945 }
1946 
1947 // If a class (or interface) has a unique concrete method uniqm, return nullptr.
1948 // Otherwise, return a class that contains an interfering method.
1949 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk,
1950                                                   Method* uniqm,
1951                                                   Klass* resolved_klass,
1952                                                   Method* resolved_method,
1953                                                   KlassDepChange* changes) {
1954   assert(UseVtableBasedCHA, "required");
1955   assert(!ctxk->is_interface() || ctxk == resolved_klass, "sanity");
1956   assert(!resolved_method->can_be_statically_bound() || resolved_method == uniqm, "sanity");
1957   assert(resolved_klass->is_subtype_of(resolved_method->method_holder()), "sanity");
1958 
1959   if (!InstanceKlass::cast(resolved_klass)->is_linked() ||
1960       !resolved_method->method_holder()->is_linked() ||
1961       resolved_method->can_be_statically_bound()) {
1962     // Dependency is redundant, but benign. Just keep it to avoid unnecessary recompilation.
1963     return nullptr; // no vtable index available
1964   }
1965 
1966   LinkedConcreteMethodFinder mf(InstanceKlass::cast(resolved_klass), resolved_method, uniqm);
1967   return mf.find_witness(ctxk, changes);
1968 }
1969 
1970 // Find the set of all non-abstract methods under ctxk that match m.
1971 // (The method m must be defined or inherited in ctxk.)
1972 // Include m itself in the set, unless it is abstract.
1973 // If this set has exactly one element, return that element.
1974 // Not yet linked subclasses of ctxk are ignored since they don't have any instances yet.
1975 // Additionally, resolved_klass and resolved_method complete the description of the call site being analyzed.
1976 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method) {
1977   // Return nullptr if m is marked old; must have been a redefined method.
1978   if (m->is_old()) {
1979     return nullptr;
1980   }
1981   if (!InstanceKlass::cast(resolved_klass)->is_linked() ||
1982       !resolved_method->method_holder()->is_linked() ||
1983       resolved_method->can_be_statically_bound()) {
1984     return m; // nothing to do: no witness under ctxk
1985   }
1986   LinkedConcreteMethodFinder wf(InstanceKlass::cast(resolved_klass), resolved_method);
1987   assert(Dependencies::verify_method_context(ctxk, m), "proper context");
1988   wf.record_witnesses(1);
1989   Klass* wit = wf.find_witness(ctxk);
1990   if (wit != nullptr) {
1991     return nullptr;  // Too many witnesses.
1992   }
1993   // p == nullptr when no participants are found (wf.num_participants() == 0).
1994   // fm == nullptr case has 2 meanings:
1995   //  * when p == nullptr: no method found;
1996   //  * when p != nullptr: AbstractMethodError-throwing method found.
1997   // Also, found method should always be accompanied by a participant class.
1998   Klass*   p = wf.participant(0);
1999   Method* fm = wf.found_method(0);
2000   assert(fm == nullptr || p != nullptr, "no participant");
2001   // Normalize all error-throwing cases to nullptr.
2002   if (fm == Universe::throw_illegal_access_error() ||
2003       fm == Universe::throw_no_such_method_error() ||
2004       !Dependencies::is_concrete_method(fm, p)) {
2005     fm = nullptr; // error-throwing method
2006   }
2007   if (Dependencies::is_concrete_method(m, ctxk)) {
2008     if (p == nullptr) {
2009       // It turns out that m was always the only implementation.
2010       assert(fm == nullptr, "sanity");
2011       fm = m;
2012     }
2013   }
2014 #ifndef PRODUCT
2015   // Make sure the dependency mechanism will pass this discovery:
2016   if (VerifyDependencies && fm != nullptr) {
2017     guarantee(nullptr == check_unique_concrete_method(ctxk, fm, resolved_klass, resolved_method),
2018               "verify dep.");
2019   }
2020 #endif // PRODUCT
2021   assert(fm == nullptr || !fm->is_abstract(), "sanity");
2022   // Old CHA conservatively reports concrete methods in abstract classes
2023   // irrespective of whether they have concrete subclasses or not.
2024   // Also, abstract root method case is not fully supported.
2025 #ifdef ASSERT
2026   Klass*  uniqp = nullptr;
2027   Method* uniqm = Dependencies::find_unique_concrete_method(ctxk, m, &uniqp);
2028   assert(uniqm == nullptr || uniqm == fm ||
2029          m->is_abstract() ||
2030          uniqm->method_holder()->is_abstract() ||
2031          (fm == nullptr && uniqm != nullptr && uniqp != nullptr && !InstanceKlass::cast(uniqp)->is_linked()),
2032          "sanity");
2033 #endif // ASSERT
2034   return fm;
2035 }
2036 
2037 Klass* Dependencies::check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes) {
2038   InstanceKlass* search_at = ctxk;
2039   if (changes != nullptr) {
2040     search_at = changes->new_type(); // just look at the new bit
2041   }
2042   return find_finalizable_subclass(search_at);
2043 }
2044 
2045 Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {
2046   assert(call_site != nullptr, "sanity");
2047   assert(method_handle != nullptr, "sanity");
2048   assert(call_site->is_a(vmClasses::CallSite_klass()),     "sanity");
2049 
2050   if (changes == nullptr) {
2051     // Validate all CallSites
2052     if (java_lang_invoke_CallSite::target(call_site) != method_handle)
2053       return call_site->klass();  // assertion failed
2054   } else {
2055     // Validate the given CallSite
2056     if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {
2057       assert(method_handle != changes->method_handle(), "must be");
2058       return call_site->klass();  // assertion failed
2059     }
2060   }
2061   return nullptr;  // assertion still valid
2062 }
2063 
2064 void Dependencies::DepStream::trace_and_log_witness(Klass* witness) {
2065   if (_verify_in_progress) return;  // don't log
2066   if (witness != nullptr) {
2067     LogTarget(Debug, dependencies) lt;
2068     if (lt.is_enabled()) {
2069       LogStream ls(&lt);
2070       print_dependency(&ls, witness, /*verbose=*/ true);
2071     }
2072     // The following is a no-op unless logging is enabled:
2073     log_dependency(witness);
2074   }
2075 }
2076 
2077 Klass* Dependencies::DepStream::check_new_klass_dependency(NewKlassDepChange* changes) {
2078   assert_locked_or_safepoint(Compile_lock);
2079   Dependencies::check_valid_dependency_type(type());
2080 
2081   Klass* witness = nullptr;
2082   switch (type()) {
2083   case evol_method:
2084     witness = check_evol_method(method_argument(0));
2085     break;
2086   case leaf_type:
2087     witness = check_leaf_type(context_type());
2088     break;
2089   case abstract_with_unique_concrete_subtype:
2090     witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes);
2091     break;
2092   case unique_concrete_method_2:
2093     witness = check_unique_concrete_method(context_type(), method_argument(1), changes);
2094     break;
2095   case unique_concrete_method_4:
2096     witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes);
2097     break;
2098   case unique_implementor:
2099     witness = check_unique_implementor(context_type(), type_argument(1), changes);
2100     break;
2101   case no_finalizable_subclasses:
2102     witness = check_has_no_finalizable_subclasses(context_type(), changes);
2103     break;
2104   default:
2105     witness = nullptr;
2106     break;
2107   }
2108   trace_and_log_witness(witness);
2109   return witness;
2110 }
2111 
2112 Klass* Dependencies::DepStream::check_klass_init_dependency(KlassInitDepChange* changes) {
2113   assert_locked_or_safepoint(Compile_lock);
2114   Dependencies::check_valid_dependency_type(type());
2115 
2116   // No new types added. Only unique_concrete_method_4 is sensitive to class initialization changes.
2117   Klass* witness = nullptr;
2118   switch (type()) {
2119   case unique_concrete_method_4:
2120     witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes);
2121     break;
2122   default:
2123     witness = nullptr;
2124     break;
2125   }
2126   trace_and_log_witness(witness);
2127   return witness;
2128 }
2129 
2130 Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {
2131   assert_locked_or_safepoint(Compile_lock);
2132   Dependencies::check_valid_dependency_type(type());
2133 
2134   if (changes != nullptr) {
2135     if (UseVtableBasedCHA && changes->is_klass_init_change()) {
2136       return check_klass_init_dependency(changes->as_klass_init_change());
2137     } else {
2138       return check_new_klass_dependency(changes->as_new_klass_change());
2139     }
2140   } else {
2141     Klass* witness = check_new_klass_dependency(nullptr);
2142     // check_klass_init_dependency duplicates check_new_klass_dependency checks when class hierarchy change info is absent.
2143     assert(witness != nullptr || check_klass_init_dependency(nullptr) == nullptr, "missed dependency");
2144     return witness;
2145   }
2146 }
2147 
2148 Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) {
2149   assert_locked_or_safepoint(Compile_lock);
2150   Dependencies::check_valid_dependency_type(type());
2151 
2152   Klass* witness = nullptr;
2153   switch (type()) {
2154   case call_site_target_value:
2155     witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes);
2156     break;
2157   default:
2158     witness = nullptr;
2159     break;
2160   }
2161   trace_and_log_witness(witness);
2162   return witness;
2163 }
2164 
2165 
2166 Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {
2167   // Handle klass dependency
2168   if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))
2169     return check_klass_dependency(changes.as_klass_change());
2170 
2171   // Handle CallSite dependency
2172   if (changes.is_call_site_change())
2173     return check_call_site_dependency(changes.as_call_site_change());
2174 
2175   // irrelevant dependency; skip it
2176   return nullptr;
2177 }
2178 
2179 
2180 void DepChange::print() { print_on(tty); }
2181 
2182 void DepChange::print_on(outputStream* st) {
2183   int nsup = 0, nint = 0;
2184   for (ContextStream str(*this); str.next(); ) {
2185     InstanceKlass* k = str.klass();
2186     switch (str.change_type()) {
2187     case Change_new_type:
2188       st->print_cr("  dependee = %s", k->external_name());
2189       break;
2190     case Change_new_sub:
2191       if (!WizardMode) {
2192         ++nsup;
2193       } else {
2194         st->print_cr("  context super = %s", k->external_name());
2195       }
2196       break;
2197     case Change_new_impl:
2198       if (!WizardMode) {
2199         ++nint;
2200       } else {
2201         st->print_cr("  context interface = %s", k->external_name());
2202       }
2203       break;
2204     default:
2205       break;
2206     }
2207   }
2208   if (nsup + nint != 0) {
2209     st->print_cr("  context supers = %d, interfaces = %d", nsup, nint);
2210   }
2211 }
2212 
2213 void DepChange::ContextStream::start() {
2214   InstanceKlass* type = (_changes.is_klass_change() ? _changes.as_klass_change()->type() : (InstanceKlass*) nullptr);
2215   _change_type = (type == nullptr ? NO_CHANGE : Start_Klass);
2216   _klass = type;
2217   _ti_base = nullptr;
2218   _ti_index = 0;
2219   _ti_limit = 0;
2220 }
2221 
2222 bool DepChange::ContextStream::next() {
2223   switch (_change_type) {
2224   case Start_Klass:             // initial state; _klass is the new type
2225     _ti_base = _klass->transitive_interfaces();
2226     _ti_index = 0;
2227     _change_type = Change_new_type;
2228     return true;
2229   case Change_new_type:
2230     // fall through:
2231     _change_type = Change_new_sub;
2232   case Change_new_sub:
2233     // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277
2234     {
2235       _klass = _klass->java_super();
2236       if (_klass != nullptr) {
2237         return true;
2238       }
2239     }
2240     // else set up _ti_limit and fall through:
2241     _ti_limit = (_ti_base == nullptr) ? 0 : _ti_base->length();
2242     _change_type = Change_new_impl;
2243   case Change_new_impl:
2244     if (_ti_index < _ti_limit) {
2245       _klass = _ti_base->at(_ti_index++);
2246       return true;
2247     }
2248     // fall through:
2249     _change_type = NO_CHANGE;  // iterator is exhausted
2250   case NO_CHANGE:
2251     break;
2252   default:
2253     ShouldNotReachHere();
2254   }
2255   return false;
2256 }
2257 
2258 void KlassDepChange::initialize() {
2259   // entire transaction must be under this lock:
2260   assert_lock_strong(Compile_lock);
2261 
2262   // Mark all dependee and all its superclasses
2263   // Mark transitive interfaces
2264   for (ContextStream str(*this); str.next(); ) {
2265     InstanceKlass* d = str.klass();
2266     assert(!d->is_marked_dependent(), "checking");
2267     d->set_is_marked_dependent(true);
2268   }
2269 }
2270 
2271 KlassDepChange::~KlassDepChange() {
2272   // Unmark all dependee and all its superclasses
2273   // Unmark transitive interfaces
2274   for (ContextStream str(*this); str.next(); ) {
2275     InstanceKlass* d = str.klass();
2276     d->set_is_marked_dependent(false);
2277   }
2278 }
2279 
2280 bool KlassDepChange::involves_context(Klass* k) {
2281   if (k == nullptr || !k->is_instance_klass()) {
2282     return false;
2283   }
2284   InstanceKlass* ik = InstanceKlass::cast(k);
2285   bool is_contained = ik->is_marked_dependent();
2286   assert(is_contained == type()->is_subtype_of(k),
2287          "correct marking of potential context types");
2288   return is_contained;
2289 }
2290 
2291 void Dependencies::print_statistics() {
2292   AbstractClassHierarchyWalker::print_statistics();
2293 }
2294 
2295 void AbstractClassHierarchyWalker::print_statistics() {
2296   if (UsePerfData) {
2297     jlong deps_find_witness_calls   = _perf_find_witness_anywhere_calls_count->get_value();
2298     jlong deps_find_witness_steps   = _perf_find_witness_anywhere_steps_count->get_value();
2299     jlong deps_find_witness_singles = _perf_find_witness_in_calls_count->get_value();
2300 
2301     ttyLocker ttyl;
2302     tty->print_cr("Dependency check (find_witness) "
2303                   "calls=" JLONG_FORMAT ", steps=" JLONG_FORMAT " (avg=%.1f), singles=" JLONG_FORMAT,
2304                   deps_find_witness_calls,
2305                   deps_find_witness_steps,
2306                   (double)deps_find_witness_steps / deps_find_witness_calls,
2307                   deps_find_witness_singles);
2308     if (xtty != nullptr) {
2309       xtty->elem("deps_find_witness calls='" JLONG_FORMAT "' steps='" JLONG_FORMAT "' singles='" JLONG_FORMAT "'",
2310                  deps_find_witness_calls,
2311                  deps_find_witness_steps,
2312                  deps_find_witness_singles);
2313     }
2314   }
2315 }
2316 
2317 CallSiteDepChange::CallSiteDepChange(Handle call_site, Handle method_handle) :
2318   _call_site(call_site),
2319   _method_handle(method_handle) {
2320   assert(_call_site()->is_a(vmClasses::CallSite_klass()), "must be");
2321   assert(_method_handle.is_null() || _method_handle()->is_a(vmClasses::MethodHandle_klass()), "must be");
2322 }
2323 
2324 void dependencies_init() {
2325   AbstractClassHierarchyWalker::init();
2326 }