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