1 /*
  2  * Copyright (c) 2005, 2026, 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 #ifndef SHARE_CODE_DEPENDENCIES_HPP
 26 #define SHARE_CODE_DEPENDENCIES_HPP
 27 
 28 #include "ci/ciCallSite.hpp"
 29 #include "ci/ciKlass.hpp"
 30 #include "ci/ciMethod.hpp"
 31 #include "ci/ciMethodHandle.hpp"
 32 #include "code/compressedStream.hpp"
 33 #include "code/nmethod.hpp"
 34 #include "memory/resourceArea.hpp"
 35 #include "runtime/safepointVerifiers.hpp"
 36 #include "utilities/growableArray.hpp"
 37 
 38 //** Dependencies represent assertions (approximate invariants) within
 39 // the runtime system, e.g. class hierarchy changes.  An example is an
 40 // assertion that a given method is not overridden; another example is
 41 // that a type has only one concrete subtype.  Compiled code which
 42 // relies on such assertions must be discarded if they are overturned
 43 // by changes in the runtime system.  We can think of these assertions
 44 // as approximate invariants, because we expect them to be overturned
 45 // very infrequently.  We are willing to perform expensive recovery
 46 // operations when they are overturned.  The benefit, of course, is
 47 // performing optimistic optimizations (!) on the object code.
 48 //
 49 // Changes in the class hierarchy due to dynamic linking or
 50 // class evolution can violate dependencies.  There is enough
 51 // indexing between classes and nmethods to make dependency
 52 // checking reasonably efficient.
 53 
 54 class ciEnv;
 55 class nmethod;
 56 class OopRecorder;
 57 class xmlStream;
 58 class CompileLog;
 59 class CompileTask;
 60 class DepChange;
 61 class   KlassDepChange;
 62 class     NewKlassDepChange;
 63 class     KlassInitDepChange;
 64 class   CallSiteDepChange;
 65 class NoSafepointVerifier;
 66 
 67 class Dependencies: public ResourceObj {
 68  public:
 69   // Note: In the comments on dependency types, most uses of the terms
 70   // subtype and supertype are used in a "non-strict" or "inclusive"
 71   // sense, and are starred to remind the reader of this fact.
 72   // Strict uses of the terms use the word "proper".
 73   //
 74   // Specifically, every class is its own subtype* and supertype*.
 75   // (This trick is easier than continually saying things like "Y is a
 76   // subtype of X or X itself".)
 77   //
 78   // Sometimes we write X > Y to mean X is a proper supertype of Y.
 79   // The notation X > {Y, Z} means X has proper subtypes Y, Z.
 80   // The notation X.m > Y means that Y inherits m from X, while
 81   // X.m > Y.m means Y overrides X.m.  A star denotes abstractness,
 82   // as *I > A, meaning (abstract) interface I is a super type of A,
 83   // or A.*m > B.m, meaning B.m implements abstract method A.m.
 84   //
 85   // In this module, the terms "subtype" and "supertype" refer to
 86   // Java-level reference type conversions, as detected by
 87   // "instanceof" and performed by "checkcast" operations.  The method
 88   // Klass::is_subtype_of tests these relations.  Note that "subtype"
 89   // is richer than "subclass" (as tested by Klass::is_subclass_of),
 90   // since it takes account of relations involving interface and array
 91   // types.
 92   //
 93   // To avoid needless complexity, dependencies involving array types
 94   // are not accepted.  If you need to make an assertion about an
 95   // array type, make the assertion about its corresponding element
 96   // types.  Any assertion that might change about an array type can
 97   // be converted to an assertion about its element type.
 98   //
 99   // Most dependencies are evaluated over a "context type" CX, which
100   // stands for the set Subtypes(CX) of every Java type that is a subtype*
101   // of CX.  When the system loads a new class or interface N, it is
102   // responsible for re-evaluating changed dependencies whose context
103   // type now includes N, that is, all super types of N.
104   //
105   enum DepType {
106     // _type is initially set to -1, to prevent "already at end" assert
107     undefined_dependency = -1,
108 
109     end_marker = 0,
110 
111     // An 'evol' dependency simply notes that the contents of the
112     // method were used.  If it evolves (is replaced), the nmethod
113     // must be recompiled.  No other dependencies are implied.
114     evol_method,
115     FIRST_TYPE = evol_method,
116 
117     // A context type CX is a leaf it if has no proper subtype.
118     leaf_type,
119 
120     // An abstract class CX has exactly one concrete subtype CC.
121     abstract_with_unique_concrete_subtype,
122 
123     // Given a method M1 and a context class CX, the set MM(CX, M1, RC1, RM1) of
124     // "concrete matching methods" in CX of M1 is the set of every
125     // concrete M2 for which it is possible to create an invokevirtual
126     // or invokeinterface call site that can reach either M1 or M2.
127     // That is, M1 and M2 share a name, signature, and vtable index.
128     // We wish to notice when the set MM(CX, M1, RC1, RM1) is just {M1}, or
129     // perhaps a set of two {M1,M2}, and issue dependencies on this.
130 
131     // The set MM(CX, M1, RC1, RM1) can be computed by starting with any matching
132     // concrete M2 that is inherited into CX, and then walking the
133     // subtypes* of CX looking for concrete definitions.
134 
135     // The parameters to this dependency are the context class CX, the method M1,
136     // the resolved class RC1, and the resolved method RM1. M1 must be either inherited in CX
137     // or defined in a subtype* of CX. It asserts that MM(CX, M1, RC1, RM1) is
138     // no greater than {M1}. RC1 and RM1 are used to improve the precision of the analysis.
139     unique_concrete_method, // one unique concrete method under CX
140 
141     // This dependency asserts that interface CX has a unique implementor class.
142     unique_implementor, // one unique implementor under CX
143 
144     // This dependency asserts that no instances of class or it's
145     // subclasses require finalization registration.
146     no_finalizable_subclasses,
147 
148     // This dependency asserts when the CallSite.target value changed.
149     call_site_target_value,
150 
151     TYPE_LIMIT
152   };
153   enum {
154     LG2_TYPE_LIMIT = 4,  // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))
155 
156     // handy categorizations of dependency types:
157     all_types           = ((1 << TYPE_LIMIT) - 1) & ((~0u) << FIRST_TYPE),
158 
159     non_klass_types     = (1 << call_site_target_value),
160     klass_types         = all_types & ~non_klass_types,
161 
162     non_ctxk_types      = (1 << evol_method) | (1 << call_site_target_value),
163     implicit_ctxk_types = 0,
164     explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),
165 
166     max_arg_count = 4,   // current maximum number of arguments (incl. ctxk)
167 
168     // A "context type" is a class or interface that
169     // provides context for evaluating a dependency.
170     // When present, it is one of the arguments (dep_context_arg).
171     //
172     // If a dependency does not have a context type, there is a
173     // default context, depending on the type of the dependency.
174     // This bit signals that a default context has been compressed away.
175     default_context_type_bit = (1<<LG2_TYPE_LIMIT)
176   };
177 
178   static const char* dep_name(DepType dept);
179   static int         dep_args(DepType dept);
180 
181   static bool is_klass_type(           DepType dept) { return dept_in_mask(dept, klass_types        ); }
182 
183   static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }
184   static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }
185 
186   static int           dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }
187   static int  dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }
188 
189   static void check_valid_dependency_type(DepType dept);
190 
191  private:
192   // State for writing a new set of dependencies:
193   GrowableArray<int>*       _dep_seen;  // (seen[h->ident] & (1<<dept))
194   GrowableArray<ciBaseObject*>*  _deps[TYPE_LIMIT];
195 
196   static const char* _dep_name[TYPE_LIMIT];
197   static int         _dep_args[TYPE_LIMIT];
198 
199   static bool dept_in_mask(DepType dept, int mask) {
200     return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;
201   }
202 
203   bool note_dep_seen(int dept, ciBaseObject* x) {
204     assert(dept < BitsPerInt, "oob");
205     int x_id = x->ident();
206     assert(_dep_seen != nullptr, "deps must be writable");
207     int seen = _dep_seen->at_grow(x_id, 0);
208     _dep_seen->at_put(x_id, seen | (1<<dept));
209     // return true if we've already seen dept/x
210     return (seen & (1<<dept)) != 0;
211   }
212 
213   bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
214                         int ctxk_i, ciKlass* ctxk);
215 
216   void sort_all_deps();
217   size_t estimate_size_in_bytes();
218 
219   // Initialize _deps, etc.
220   void initialize(ciEnv* env);
221 
222   // State for making a new set of dependencies:
223   OopRecorder* _oop_recorder;
224 
225   // Logging support
226   CompileLog* _log;
227 
228   address  _content_bytes;  // everything but the oop references, encoded
229   size_t   _size_in_bytes;
230 
231  public:
232   // Make a new empty dependencies set.
233   Dependencies(ciEnv* env) {
234     initialize(env);
235   }
236 
237  private:
238   // Check for a valid context type.
239   // Enforce the restriction against array types.
240   static void check_ctxk(ciKlass* ctxk) {
241     assert(ctxk->is_instance_klass(), "java types only");
242   }
243   static void check_ctxk_concrete(ciKlass* ctxk) {
244     assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");
245   }
246   static void check_ctxk_abstract(ciKlass* ctxk) {
247     check_ctxk(ctxk);
248     assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");
249   }
250   static void check_unique_method(ciKlass* ctxk, ciMethod* m) {
251     assert(!m->can_be_statically_bound(ctxk->as_instance_klass()) || ctxk->is_interface(), "redundant");
252   }
253   static void check_unique_implementor(ciInstanceKlass* ctxk, ciInstanceKlass* uniqk) {
254     assert(ctxk->implementor() == uniqk, "not a unique implementor");
255   }
256 
257   void assert_common_1(DepType dept, ciBaseObject* x);
258   void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);
259   void assert_common_4(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2, ciBaseObject* x3);
260 
261  public:
262   // Adding assertions to a new dependency set at compile time:
263   void assert_evol_method(ciMethod* m);
264   void assert_leaf_type(ciKlass* ctxk);
265   void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);
266   void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm, ciKlass* resolved_klass, ciMethod* resolved_method);
267   void assert_unique_implementor(ciInstanceKlass* ctxk, ciInstanceKlass* uniqk);
268   void assert_has_no_finalizable_subclasses(ciKlass* ctxk);
269   void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);
270 
271   // Define whether a given method or type is concrete.
272   // These methods define the term "concrete" as used in this module.
273   // For this module, an "abstract" class is one which is non-concrete.
274   //
275   // Future optimizations may allow some classes to remain
276   // non-concrete until their first instantiation, and allow some
277   // methods to remain non-concrete until their first invocation.
278   // In that case, there would be a middle ground between concrete
279   // and abstract (as defined by the Java language and VM).
280   static bool is_concrete_klass(Klass* k);    // k is instantiable
281   static bool is_concrete_method(Method* m, Klass* k);  // m is invocable
282   static Klass* find_finalizable_subclass(InstanceKlass* ik);
283 
284   // These versions of the concreteness queries work through the CI.
285   // The CI versions are allowed to skew sometimes from the VM
286   // (oop-based) versions.  The cost of such a difference is a
287   // (safely) aborted compilation, or a deoptimization, or a missed
288   // optimization opportunity.
289   //
290   // In order to prevent spurious assertions, query results must
291   // remain stable within any single ciEnv instance.  (I.e., they must
292   // not go back into the VM to get their value; they must cache the
293   // bit in the CI, either eagerly or lazily.)
294   static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable
295   static bool has_finalizable_subclass(ciInstanceKlass* k);
296 
297   // As a general rule, it is OK to compile under the assumption that
298   // a given type or method is concrete, even if it at some future
299   // point becomes abstract.  So dependency checking is one-sided, in
300   // that it permits supposedly concrete classes or methods to turn up
301   // as really abstract.  (This shouldn't happen, except during class
302   // evolution, but that's the logic of the checking.)  However, if a
303   // supposedly abstract class or method suddenly becomes concrete, a
304   // dependency on it must fail.
305 
306   // Checking old assertions at run-time (in the VM only):
307   static Klass* check_evol_method(Method* m);
308   static Klass* check_leaf_type(InstanceKlass* ctxk);
309   static Klass* check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk, Klass* conck, NewKlassDepChange* changes = nullptr);
310   static Klass* check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes = nullptr);
311   static Klass* check_unique_concrete_method(InstanceKlass* ctxk, Method* uniqm, Klass* resolved_klass, Method* resolved_method, KlassDepChange* changes = nullptr);
312   static Klass* check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes = nullptr);
313   static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = nullptr);
314   // A returned Klass* is nullptr if the dependency assertion is still
315   // valid.  A non-nullptr Klass* is a 'witness' to the assertion
316   // failure, a point in the class hierarchy where the assertion has
317   // been proven false.  For example, if check_leaf_type returns
318   // non-nullptr, the value is a subtype of the supposed leaf type.  This
319   // witness value may be useful for logging the dependency failure.
320   // Note that, when a dependency fails, there may be several possible
321   // witnesses to the failure.  The value returned from the check_foo
322   // method is chosen arbitrarily.
323 
324   // The 'changes' value, if non-null, requests a limited spot-check
325   // near the indicated recent changes in the class hierarchy.
326   // It is used by DepStream::spot_check_dependency_at.
327 
328   // Detecting possible new assertions:
329   static Klass*  find_unique_concrete_subtype(InstanceKlass* ctxk);
330 
331   static Method* find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method);
332 
333 #ifdef ASSERT
334   static bool verify_method_context(InstanceKlass* ctxk, Method* m);
335 #endif // ASSERT
336 
337   // Create the encoding which will be stored in an nmethod.
338   void encode_content_bytes();
339 
340   address content_bytes() {
341     assert(_content_bytes != nullptr, "encode it first");
342     return _content_bytes;
343   }
344   size_t size_in_bytes() {
345     assert(_content_bytes != nullptr, "encode it first");
346     return _size_in_bytes;
347   }
348 
349   OopRecorder* oop_recorder() { return _oop_recorder; }
350   CompileLog*  log()          { return _log; }
351 
352   void copy_to(nmethod* nm);
353 
354   static bool _verify_in_progress;  // turn off logging dependencies
355 
356   DepType validate_dependencies(CompileTask* task, char** failure_detail = nullptr);
357 
358   void log_all_dependencies();
359 
360   void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) {
361     ResourceMark rm;
362     int argslen = args->length();
363     write_dependency_to(log(), dept, args);
364     guarantee(argslen == args->length(),
365               "args array cannot grow inside nested ResoureMark scope");
366   }
367 
368   void log_dependency(DepType dept,
369                       ciBaseObject* x0,
370                       ciBaseObject* x1 = nullptr,
371                       ciBaseObject* x2 = nullptr,
372                       ciBaseObject* x3 = nullptr) {
373     if (log() == nullptr) {
374       return;
375     }
376     ResourceMark rm;
377     GrowableArray<ciBaseObject*>* ciargs =
378                 new GrowableArray<ciBaseObject*>(dep_args(dept));
379     assert (x0 != nullptr, "no log x0");
380     ciargs->push(x0);
381 
382     if (x1 != nullptr) {
383       ciargs->push(x1);
384     }
385     if (x2 != nullptr) {
386       ciargs->push(x2);
387     }
388     if (x3 != nullptr) {
389       ciargs->push(x3);
390     }
391     assert(ciargs->length() == dep_args(dept), "");
392     log_dependency(dept, ciargs);
393   }
394 
395   class DepArgument : public ResourceObj {
396    private:
397     bool  _is_oop;
398     bool  _valid;
399     void* _value;
400    public:
401     DepArgument() : _is_oop(false), _valid(false), _value(nullptr) {}
402     DepArgument(oop v): _is_oop(true), _valid(true), _value(v) {}
403     DepArgument(Metadata* v): _is_oop(false), _valid(true), _value(v) {}
404 
405     bool is_null() const               { return _value == nullptr; }
406     bool is_oop() const                { return _is_oop; }
407     bool is_metadata() const           { return !_is_oop; }
408     bool is_klass() const              { return is_metadata() && metadata_value()->is_klass(); }
409     bool is_method() const             { return is_metadata() && metadata_value()->is_method(); }
410 
411     oop oop_value() const              { assert(_is_oop && _valid, "must be"); return cast_to_oop(_value); }
412     Metadata* metadata_value() const   { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }
413   };
414 
415   static void print_dependency(DepType dept,
416                                GrowableArray<DepArgument>* args,
417                                Klass* witness = nullptr, outputStream* st = tty);
418 
419  private:
420   // helper for encoding common context types as zero:
421   static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);
422 
423   static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);
424 
425   static void write_dependency_to(CompileLog* log,
426                                   DepType dept,
427                                   GrowableArray<ciBaseObject*>* args,
428                                   Klass* witness = nullptr);
429   static void write_dependency_to(CompileLog* log,
430                                   DepType dept,
431                                   GrowableArray<DepArgument>* args,
432                                   Klass* witness = nullptr);
433   static void write_dependency_to(xmlStream* xtty,
434                                   DepType dept,
435                                   GrowableArray<DepArgument>* args,
436                                   Klass* witness = nullptr);
437  public:
438   // Use this to iterate over an nmethod's dependency set.
439   // Works on new and old dependency sets.
440   // Usage:
441   //
442   // ;
443   // Dependencies::DepType dept;
444   // for (Dependencies::DepStream deps(nm); deps.next(); ) {
445   //   ...
446   // }
447   //
448   // The caller must be in the VM, since oops are not wrapped in handles.
449   class DepStream {
450   private:
451     nmethod*              _code;   // null if in a compiler thread
452     Dependencies*         _deps;   // null if not in a compiler thread
453     CompressedReadStream  _bytes;
454 #ifdef ASSERT
455     size_t                _byte_limit;
456 #endif
457 
458     // iteration variables:
459     DepType               _type;
460     int                   _xi[max_arg_count+1];
461 
462     void initial_asserts(size_t byte_limit) NOT_DEBUG({});
463 
464     inline Metadata* recorded_metadata_at(int i);
465     inline oop recorded_oop_at(int i);
466 
467     Klass* check_klass_dependency(KlassDepChange* changes);
468     Klass* check_new_klass_dependency(NewKlassDepChange* changes);
469     Klass* check_klass_init_dependency(KlassInitDepChange* changes);
470     Klass* check_call_site_dependency(CallSiteDepChange* changes);
471 
472     void trace_and_log_witness(Klass* witness);
473 
474   public:
475     DepStream(Dependencies* deps)
476       : _code(nullptr),
477         _deps(deps),
478         _bytes(deps->content_bytes())
479     {
480       initial_asserts(deps->size_in_bytes());
481     }
482     DepStream(nmethod* code)
483       : _code(code),
484         _deps(nullptr),
485         _bytes(code->dependencies_begin())
486     {
487       initial_asserts(code->dependencies_size());
488     }
489 
490     bool next();
491 
492     DepType type()               { return _type; }
493     bool is_oop_argument(int i)  { return type() == call_site_target_value; }
494     uintptr_t get_identifier(int i);
495 
496     int argument_count()         { return dep_args(type()); }
497     int argument_index(int i)    { assert(0 <= i && i < argument_count(), "oob");
498                                    return _xi[i]; }
499     Metadata* argument(int i);     // => recorded_oop_at(argument_index(i))
500     oop argument_oop(int i);         // => recorded_oop_at(argument_index(i))
501     InstanceKlass* context_type();
502 
503     bool is_klass_type()         { return Dependencies::is_klass_type(type()); }
504 
505     Method* method_argument(int i) {
506       Metadata* x = argument(i);
507       assert(x->is_method(), "type");
508       return (Method*) x;
509     }
510     Klass* type_argument(int i) {
511       Metadata* x = argument(i);
512       assert(x->is_klass(), "type");
513       return (Klass*) x;
514     }
515 
516     // The point of the whole exercise:  Is this dep still OK?
517     Klass* check_dependency() {
518       Klass* result = check_klass_dependency(nullptr);
519       if (result != nullptr)  return result;
520       return check_call_site_dependency(nullptr);
521     }
522 
523     // A lighter version:  Checks only around recent changes in a class
524     // hierarchy.  (See Universe::flush_dependents_on.)
525     Klass* spot_check_dependency_at(DepChange& changes);
526 
527     // Log the current dependency to xtty or compilation log.
528     void log_dependency(Klass* witness = nullptr);
529 
530     // Print the current dependency to tty.
531     void print_dependency(outputStream* st, Klass* witness = nullptr, bool verbose = false);
532   };
533   friend class Dependencies::DepStream;
534 
535   NOT_PRODUCT(static void print_statistics();)
536 };
537 
538 
539 class DependencySignature : public ResourceObj {
540  private:
541   int                   _args_count;
542   uintptr_t             _argument_hash[Dependencies::max_arg_count];
543   Dependencies::DepType _type;
544 
545  public:
546   DependencySignature(Dependencies::DepStream& dep) {
547     _args_count = dep.argument_count();
548     _type = dep.type();
549     for (int i = 0; i < _args_count; i++) {
550       _argument_hash[i] = dep.get_identifier(i);
551     }
552   }
553 
554   static bool     equals(DependencySignature const& s1, DependencySignature const& s2);
555   static unsigned hash  (DependencySignature const& s1) { return (unsigned)(s1.arg(0) >> 2); }
556 
557   int args_count()             const { return _args_count; }
558   uintptr_t arg(int idx)       const { return _argument_hash[idx]; }
559   Dependencies::DepType type() const { return _type; }
560 
561 };
562 
563 
564 // Every particular DepChange is a sub-class of this class.
565 class DepChange : public StackObj {
566  public:
567   // What kind of DepChange is this?
568   virtual bool is_klass_change()      const { return false; }
569   virtual bool is_new_klass_change()  const { return false; }
570   virtual bool is_klass_init_change() const { return false; }
571   virtual bool is_call_site_change()  const { return false; }
572 
573   // Subclass casting with assertions.
574   KlassDepChange*    as_klass_change() {
575     assert(is_klass_change(), "bad cast");
576     return (KlassDepChange*) this;
577   }
578   NewKlassDepChange* as_new_klass_change() {
579     assert(is_new_klass_change(), "bad cast");
580     return (NewKlassDepChange*) this;
581   }
582   KlassInitDepChange* as_klass_init_change() {
583     assert(is_klass_init_change(), "bad cast");
584     return (KlassInitDepChange*) this;
585   }
586   CallSiteDepChange* as_call_site_change() {
587     assert(is_call_site_change(), "bad cast");
588     return (CallSiteDepChange*) this;
589   }
590 
591   void print();
592   void print_on(outputStream* st);
593 
594  public:
595   enum ChangeType {
596     NO_CHANGE = 0,              // an uninvolved klass
597     Change_new_type,            // a newly loaded type
598     Change_new_sub,             // a super with a new subtype
599     Change_new_impl,            // an interface with a new implementation
600     CHANGE_LIMIT,
601     Start_Klass = CHANGE_LIMIT  // internal indicator for ContextStream
602   };
603 
604   // Usage:
605   // for (DepChange::ContextStream str(changes); str.next(); ) {
606   //   InstanceKlass* k = str.klass();
607   //   switch (str.change_type()) {
608   //     ...
609   //   }
610   // }
611   class ContextStream : public StackObj {
612    private:
613     DepChange&  _changes;
614     friend class DepChange;
615 
616     // iteration variables:
617     ChangeType     _change_type;
618     InstanceKlass* _klass;
619     Array<InstanceKlass*>* _ti_base;    // i.e., transitive_interfaces
620     int         _ti_index;
621     int         _ti_limit;
622 
623     // start at the beginning:
624     void start();
625 
626    public:
627     ContextStream(DepChange& changes)
628       : _changes(changes)
629     { start(); }
630 
631     ContextStream(DepChange& changes, NoSafepointVerifier& nsv)
632       : _changes(changes)
633       // the nsv argument makes it safe to hold oops like _klass
634     { start(); }
635 
636     bool next();
637 
638     ChangeType change_type()     { return _change_type; }
639     InstanceKlass* klass()       { return _klass; }
640   };
641   friend class DepChange::ContextStream;
642 };
643 
644 
645 // A class hierarchy change coming through the VM (under the Compile_lock).
646 // The change is structured as a single type with any number of supers
647 // and implemented interface types.  Other than the type, any of the
648 // super types can be context types for a relevant dependency, which the
649 // type could invalidate.
650 class KlassDepChange : public DepChange {
651  private:
652   // each change set is rooted in exactly one type (at present):
653   InstanceKlass* _type;
654 
655   void initialize();
656 
657  protected:
658   // notes the type, marks it and all its super-types
659   KlassDepChange(InstanceKlass* type) : _type(type) {
660     initialize();
661   }
662 
663   // cleans up the marks
664   ~KlassDepChange();
665 
666  public:
667   // What kind of DepChange is this?
668   virtual bool is_klass_change() const { return true; }
669 
670   InstanceKlass* type() { return _type; }
671 
672   // involves_context(k) is true if k == _type or any of its super types
673   bool involves_context(Klass* k);
674 };
675 
676 // A class hierarchy change: new type is loaded.
677 class NewKlassDepChange : public KlassDepChange {
678  public:
679   NewKlassDepChange(InstanceKlass* new_type) : KlassDepChange(new_type) {}
680 
681   // What kind of DepChange is this?
682   virtual bool is_new_klass_change() const { return true; }
683 
684   InstanceKlass* new_type() { return type(); }
685 };
686 
687 // Change in initialization state of a loaded class.
688 class KlassInitDepChange : public KlassDepChange {
689  public:
690   KlassInitDepChange(InstanceKlass* type) : KlassDepChange(type) {}
691 
692   // What kind of DepChange is this?
693   virtual bool is_klass_init_change() const { return true; }
694 };
695 
696 // A CallSite has changed its target.
697 class CallSiteDepChange : public DepChange {
698  private:
699   Handle _call_site;
700   Handle _method_handle;
701 
702  public:
703   CallSiteDepChange(Handle call_site, Handle method_handle);
704 
705   // What kind of DepChange is this?
706   virtual bool is_call_site_change() const { return true; }
707 
708   oop call_site()     const { return _call_site();     }
709   oop method_handle() const { return _method_handle(); }
710 };
711 
712 #endif // SHARE_CODE_DEPENDENCIES_HPP