1 /*
   2  * Copyright (c) 2005, 2015, 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_VM_CODE_DEPENDENCIES_HPP
  26 #define SHARE_VM_CODE_DEPENDENCIES_HPP
  27 
  28 #include "ci/ciCallSite.hpp"
  29 #include "ci/ciKlass.hpp"
  30 #include "ci/ciMethodHandle.hpp"
  31 #include "classfile/systemDictionary.hpp"
  32 #include "code/compressedStream.hpp"
  33 #include "code/nmethod.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "utilities/growableArray.hpp"
  36 #include "utilities/hashtable.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 DepChange;
  60 class   KlassDepChange;
  61 class   CallSiteDepChange;

  62 class No_Safepoint_Verifier;
  63 
  64 class Dependencies: public ResourceObj {
  65  public:
  66   // Note: In the comments on dependency types, most uses of the terms
  67   // subtype and supertype are used in a "non-strict" or "inclusive"
  68   // sense, and are starred to remind the reader of this fact.
  69   // Strict uses of the terms use the word "proper".
  70   //
  71   // Specifically, every class is its own subtype* and supertype*.
  72   // (This trick is easier than continually saying things like "Y is a
  73   // subtype of X or X itself".)
  74   //
  75   // Sometimes we write X > Y to mean X is a proper supertype of Y.
  76   // The notation X > {Y, Z} means X has proper subtypes Y, Z.
  77   // The notation X.m > Y means that Y inherits m from X, while
  78   // X.m > Y.m means Y overrides X.m.  A star denotes abstractness,
  79   // as *I > A, meaning (abstract) interface I is a super type of A,
  80   // or A.*m > B.m, meaning B.m implements abstract method A.m.
  81   //
  82   // In this module, the terms "subtype" and "supertype" refer to
  83   // Java-level reference type conversions, as detected by
  84   // "instanceof" and performed by "checkcast" operations.  The method
  85   // Klass::is_subtype_of tests these relations.  Note that "subtype"
  86   // is richer than "subclass" (as tested by Klass::is_subclass_of),
  87   // since it takes account of relations involving interface and array
  88   // types.
  89   //
  90   // To avoid needless complexity, dependencies involving array types
  91   // are not accepted.  If you need to make an assertion about an
  92   // array type, make the assertion about its corresponding element
  93   // types.  Any assertion that might change about an array type can
  94   // be converted to an assertion about its element type.
  95   //
  96   // Most dependencies are evaluated over a "context type" CX, which
  97   // stands for the set Subtypes(CX) of every Java type that is a subtype*
  98   // of CX.  When the system loads a new class or interface N, it is
  99   // responsible for re-evaluating changed dependencies whose context
 100   // type now includes N, that is, all super types of N.
 101   //
 102   enum DepType {
 103     end_marker = 0,
 104 
 105     // An 'evol' dependency simply notes that the contents of the
 106     // method were used.  If it evolves (is replaced), the nmethod
 107     // must be recompiled.  No other dependencies are implied.
 108     evol_method,
 109     FIRST_TYPE = evol_method,
 110 
 111     // A context type CX is a leaf it if has no proper subtype.
 112     leaf_type,
 113 
 114     // An abstract class CX has exactly one concrete subtype CC.
 115     abstract_with_unique_concrete_subtype,
 116 
 117     // The type CX is purely abstract, with no concrete subtype* at all.
 118     abstract_with_no_concrete_subtype,
 119 
 120     // The concrete CX is free of concrete proper subtypes.
 121     concrete_with_no_concrete_subtype,
 122 
 123     // Given a method M1 and a context class CX, the set MM(CX, M1) 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) is just {M1}, or
 129     // perhaps a set of two {M1,M2}, and issue dependencies on this.
 130 
 131     // The set MM(CX, M1) 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 method M1 and the
 136     // context class CX.  M1 must be either inherited in CX or defined
 137     // in a subtype* of CX.  It asserts that MM(CX, M1) is no greater
 138     // than {M1}.
 139     unique_concrete_method,       // one unique concrete method under CX
 140 
 141     // An "exclusive" assertion concerns two methods or subtypes, and
 142     // declares that there are at most two (or perhaps later N>2)
 143     // specific items that jointly satisfy the restriction.
 144     // We list all items explicitly rather than just giving their
 145     // count, for robustness in the face of complex schema changes.
 146 
 147     // A context class CX (which may be either abstract or concrete)
 148     // has two exclusive concrete subtypes* C1, C2 if every concrete
 149     // subtype* of CX is either C1 or C2.  Note that if neither C1 or C2
 150     // are equal to CX, then CX itself must be abstract.  But it is
 151     // also possible (for example) that C1 is CX (a concrete class)
 152     // and C2 is a proper subtype of C1.
 153     abstract_with_exclusive_concrete_subtypes_2,
 154 
 155     // This dependency asserts that MM(CX, M1) is no greater than {M1,M2}.
 156     exclusive_concrete_methods_2,
 157 
 158     // This dependency asserts that no instances of class or it's
 159     // subclasses require finalization registration.
 160     no_finalizable_subclasses,
 161 
 162     // This dependency asserts when the CallSite.target value changed.
 163     call_site_target_value,
 164 



 165     TYPE_LIMIT
 166   };
 167   enum {
 168     LG2_TYPE_LIMIT = 4,  // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))
 169 
 170     // handy categorizations of dependency types:
 171     all_types           = ((1 << TYPE_LIMIT) - 1) & ((-1) << FIRST_TYPE),
 172 
 173     non_klass_types     = (1 << call_site_target_value),
 174     klass_types         = all_types & ~non_klass_types,
 175 
 176     non_ctxk_types      = (1 << evol_method) | (1 << call_site_target_value),
 177     implicit_ctxk_types = 0,
 178     explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),
 179 
 180     max_arg_count = 3,   // current maximum number of arguments (incl. ctxk)
 181 
 182     // A "context type" is a class or interface that
 183     // provides context for evaluating a dependency.
 184     // When present, it is one of the arguments (dep_context_arg).
 185     //
 186     // If a dependency does not have a context type, there is a
 187     // default context, depending on the type of the dependency.
 188     // This bit signals that a default context has been compressed away.
 189     default_context_type_bit = (1<<LG2_TYPE_LIMIT)
 190   };
 191 
 192   static const char* dep_name(DepType dept);
 193   static int         dep_args(DepType dept);
 194 
 195   static bool is_klass_type(           DepType dept) { return dept_in_mask(dept, klass_types        ); }
 196 
 197   static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }
 198   static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }
 199 
 200   static int           dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }
 201   static int  dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }
 202 
 203   static void check_valid_dependency_type(DepType dept);
 204 
 205  private:
 206   // State for writing a new set of dependencies:
 207   GrowableArray<int>*       _dep_seen;  // (seen[h->ident] & (1<<dept))
 208   GrowableArray<ciBaseObject*>*  _deps[TYPE_LIMIT];
 209 
 210   static const char* _dep_name[TYPE_LIMIT];
 211   static int         _dep_args[TYPE_LIMIT];
 212 
 213   static bool dept_in_mask(DepType dept, int mask) {
 214     return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;
 215   }
 216 
 217   bool note_dep_seen(int dept, ciBaseObject* x) {
 218     assert(dept < BitsPerInt, "oob");
 219     int x_id = x->ident();
 220     assert(_dep_seen != NULL, "deps must be writable");
 221     int seen = _dep_seen->at_grow(x_id, 0);
 222     _dep_seen->at_put(x_id, seen | (1<<dept));
 223     // return true if we've already seen dept/x
 224     return (seen & (1<<dept)) != 0;
 225   }
 226 
 227   bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
 228                         int ctxk_i, ciKlass* ctxk);
 229 
 230   void sort_all_deps();
 231   size_t estimate_size_in_bytes();
 232 
 233   // Initialize _deps, etc.
 234   void initialize(ciEnv* env);
 235 
 236   // State for making a new set of dependencies:
 237   OopRecorder* _oop_recorder;
 238 
 239   // Logging support
 240   CompileLog* _log;
 241 
 242   address  _content_bytes;  // everything but the oop references, encoded
 243   size_t   _size_in_bytes;
 244 
 245  public:
 246   // Make a new empty dependencies set.
 247   Dependencies(ciEnv* env) {
 248     initialize(env);
 249   }
 250 
 251  private:
 252   // Check for a valid context type.
 253   // Enforce the restriction against array types.
 254   static void check_ctxk(ciKlass* ctxk) {
 255     assert(ctxk->is_instance_klass(), "java types only");
 256   }
 257   static void check_ctxk_concrete(ciKlass* ctxk) {
 258     assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");
 259   }
 260   static void check_ctxk_abstract(ciKlass* ctxk) {
 261     check_ctxk(ctxk);
 262     assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");
 263   }
 264 
 265   void assert_common_1(DepType dept, ciBaseObject* x);
 266   void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);
 267   void assert_common_3(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2);
 268 
 269  public:
 270   // Adding assertions to a new dependency set at compile time:
 271   void assert_evol_method(ciMethod* m);
 272   void assert_leaf_type(ciKlass* ctxk);
 273   void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);
 274   void assert_abstract_with_no_concrete_subtype(ciKlass* ctxk);
 275   void assert_concrete_with_no_concrete_subtype(ciKlass* ctxk);
 276   void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm);
 277   void assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2);
 278   void assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2);
 279   void assert_has_no_finalizable_subclasses(ciKlass* ctxk);
 280   void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);
 281 



 282   // Define whether a given method or type is concrete.
 283   // These methods define the term "concrete" as used in this module.
 284   // For this module, an "abstract" class is one which is non-concrete.
 285   //
 286   // Future optimizations may allow some classes to remain
 287   // non-concrete until their first instantiation, and allow some
 288   // methods to remain non-concrete until their first invocation.
 289   // In that case, there would be a middle ground between concrete
 290   // and abstract (as defined by the Java language and VM).
 291   static bool is_concrete_klass(Klass* k);    // k is instantiable
 292   static bool is_concrete_method(Method* m, Klass* k);  // m is invocable
 293   static Klass* find_finalizable_subclass(Klass* k);
 294 
 295   // These versions of the concreteness queries work through the CI.
 296   // The CI versions are allowed to skew sometimes from the VM
 297   // (oop-based) versions.  The cost of such a difference is a
 298   // (safely) aborted compilation, or a deoptimization, or a missed
 299   // optimization opportunity.
 300   //
 301   // In order to prevent spurious assertions, query results must
 302   // remain stable within any single ciEnv instance.  (I.e., they must
 303   // not go back into the VM to get their value; they must cache the
 304   // bit in the CI, either eagerly or lazily.)
 305   static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable
 306   static bool has_finalizable_subclass(ciInstanceKlass* k);
 307 
 308   // As a general rule, it is OK to compile under the assumption that
 309   // a given type or method is concrete, even if it at some future
 310   // point becomes abstract.  So dependency checking is one-sided, in
 311   // that it permits supposedly concrete classes or methods to turn up
 312   // as really abstract.  (This shouldn't happen, except during class
 313   // evolution, but that's the logic of the checking.)  However, if a
 314   // supposedly abstract class or method suddenly becomes concrete, a
 315   // dependency on it must fail.
 316 
 317   // Checking old assertions at run-time (in the VM only):
 318   static Klass* check_evol_method(Method* m);
 319   static Klass* check_leaf_type(Klass* ctxk);
 320   static Klass* check_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck,
 321                                                               KlassDepChange* changes = NULL);
 322   static Klass* check_abstract_with_no_concrete_subtype(Klass* ctxk,
 323                                                           KlassDepChange* changes = NULL);
 324   static Klass* check_concrete_with_no_concrete_subtype(Klass* ctxk,
 325                                                           KlassDepChange* changes = NULL);
 326   static Klass* check_unique_concrete_method(Klass* ctxk, Method* uniqm,
 327                                                KlassDepChange* changes = NULL);
 328   static Klass* check_abstract_with_exclusive_concrete_subtypes(Klass* ctxk, Klass* k1, Klass* k2,
 329                                                                   KlassDepChange* changes = NULL);
 330   static Klass* check_exclusive_concrete_methods(Klass* ctxk, Method* m1, Method* m2,
 331                                                    KlassDepChange* changes = NULL);
 332   static Klass* check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes = NULL);
 333   static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL);
 334   // A returned Klass* is NULL if the dependency assertion is still
 335   // valid.  A non-NULL Klass* is a 'witness' to the assertion
 336   // failure, a point in the class hierarchy where the assertion has
 337   // been proven false.  For example, if check_leaf_type returns
 338   // non-NULL, the value is a subtype of the supposed leaf type.  This
 339   // witness value may be useful for logging the dependency failure.
 340   // Note that, when a dependency fails, there may be several possible
 341   // witnesses to the failure.  The value returned from the check_foo
 342   // method is chosen arbitrarily.
 343 
 344   // The 'changes' value, if non-null, requests a limited spot-check
 345   // near the indicated recent changes in the class hierarchy.
 346   // It is used by DepStream::spot_check_dependency_at.
 347 
 348   // Detecting possible new assertions:
 349   static Klass*    find_unique_concrete_subtype(Klass* ctxk);
 350   static Method*   find_unique_concrete_method(Klass* ctxk, Method* m);
 351   static int       find_exclusive_concrete_subtypes(Klass* ctxk, int klen, Klass* k[]);
 352 


 353   // Create the encoding which will be stored in an nmethod.
 354   void encode_content_bytes();
 355 
 356   address content_bytes() {
 357     assert(_content_bytes != NULL, "encode it first");
 358     return _content_bytes;
 359   }
 360   size_t size_in_bytes() {
 361     assert(_content_bytes != NULL, "encode it first");
 362     return _size_in_bytes;
 363   }
 364 
 365   OopRecorder* oop_recorder() { return _oop_recorder; }
 366   CompileLog*  log()          { return _log; }
 367 
 368   void copy_to(nmethod* nm);
 369 
 370   void log_all_dependencies();
 371 
 372   void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) {
 373     ResourceMark rm;
 374     int argslen = args->length();
 375     write_dependency_to(log(), dept, args);
 376     guarantee(argslen == args->length(),
 377               "args array cannot grow inside nested ResoureMark scope");
 378   }
 379 
 380   void log_dependency(DepType dept,
 381                       ciBaseObject* x0,
 382                       ciBaseObject* x1 = NULL,
 383                       ciBaseObject* x2 = NULL) {
 384     if (log() == NULL) {
 385       return;
 386     }
 387     ResourceMark rm;
 388     GrowableArray<ciBaseObject*>* ciargs =
 389                 new GrowableArray<ciBaseObject*>(dep_args(dept));
 390     assert (x0 != NULL, "no log x0");
 391     ciargs->push(x0);
 392 
 393     if (x1 != NULL) {
 394       ciargs->push(x1);
 395     }
 396     if (x2 != NULL) {
 397       ciargs->push(x2);
 398     }
 399     assert(ciargs->length() == dep_args(dept), "");
 400     log_dependency(dept, ciargs);
 401   }
 402 






 403   class DepArgument : public ResourceObj {
 404    private:
 405     bool  _is_oop;
 406     bool  _valid;
 407     void* _value;
 408    public:
 409     DepArgument() : _is_oop(false), _value(NULL), _valid(false) {}
 410     DepArgument(oop v): _is_oop(true), _value(v), _valid(true) {}
 411     DepArgument(Metadata* v): _is_oop(false), _value(v), _valid(true) {}
 412 
 413     bool is_null() const               { return _value == NULL; }
 414     bool is_oop() const                { return _is_oop; }
 415     bool is_metadata() const           { return !_is_oop; }
 416     bool is_klass() const              { return is_metadata() && metadata_value()->is_klass(); }
 417     bool is_method() const              { return is_metadata() && metadata_value()->is_method(); }
 418 
 419     oop oop_value() const              { assert(_is_oop && _valid, "must be"); return (oop) _value; }
 420     Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }
 421   };
 422 
 423   static void print_dependency(DepType dept,
 424                                GrowableArray<DepArgument>* args,
 425                                Klass* witness = NULL);
 426 
 427  private:
 428   // helper for encoding common context types as zero:
 429   static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);
 430 
 431   static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);
 432 
 433   static void write_dependency_to(CompileLog* log,
 434                                   DepType dept,
 435                                   GrowableArray<ciBaseObject*>* args,
 436                                   Klass* witness = NULL);
 437   static void write_dependency_to(CompileLog* log,
 438                                   DepType dept,
 439                                   GrowableArray<DepArgument>* args,
 440                                   Klass* witness = NULL);
 441   static void write_dependency_to(xmlStream* xtty,
 442                                   DepType dept,
 443                                   GrowableArray<DepArgument>* args,
 444                                   Klass* witness = NULL);
 445  public:
 446   // Use this to iterate over an nmethod's dependency set.
 447   // Works on new and old dependency sets.
 448   // Usage:
 449   //
 450   // ;
 451   // Dependencies::DepType dept;
 452   // for (Dependencies::DepStream deps(nm); deps.next(); ) {
 453   //   ...
 454   // }
 455   //
 456   // The caller must be in the VM, since oops are not wrapped in handles.
 457   class DepStream {
 458   private:
 459     nmethod*              _code;   // null if in a compiler thread
 460     Dependencies*         _deps;   // null if not in a compiler thread
 461     CompressedReadStream  _bytes;
 462 #ifdef ASSERT
 463     size_t                _byte_limit;
 464 #endif
 465 
 466     // iteration variables:
 467     DepType               _type;
 468     int                   _xi[max_arg_count+1];
 469 
 470     void initial_asserts(size_t byte_limit) NOT_DEBUG({});
 471 
 472     inline Metadata* recorded_metadata_at(int i);
 473     inline oop recorded_oop_at(int i);
 474 
 475     Klass* check_klass_dependency(KlassDepChange* changes);
 476     Klass* check_call_site_dependency(CallSiteDepChange* changes);
 477 
 478     void trace_and_log_witness(Klass* witness);
 479 
 480   public:
 481     DepStream(Dependencies* deps)
 482       : _deps(deps),
 483         _code(NULL),
 484         _bytes(deps->content_bytes())
 485     {
 486       initial_asserts(deps->size_in_bytes());
 487     }
 488     DepStream(nmethod* code)
 489       : _deps(NULL),
 490         _code(code),
 491         _bytes(code->dependencies_begin())
 492     {
 493       initial_asserts(code->dependencies_size());
 494     }
 495 
 496     bool next();
 497 
 498     DepType type()               { return _type; }
 499     bool is_oop_argument(int i)  { return type() == call_site_target_value; }















 500     uintptr_t get_identifier(int i);
 501 
 502     int argument_count()         { return dep_args(type()); }
 503     int argument_index(int i)    { assert(0 <= i && i < argument_count(), "oob");
 504                                    return _xi[i]; }
 505     Metadata* argument(int i);     // => recorded_oop_at(argument_index(i))
 506     oop argument_oop(int i);         // => recorded_oop_at(argument_index(i))

 507     Klass* context_type();
 508 
 509     bool is_klass_type()         { return Dependencies::is_klass_type(type()); }
 510 
 511     Method* method_argument(int i) {
 512       Metadata* x = argument(i);
 513       assert(x->is_method(), "type");
 514       return (Method*) x;
 515     }
 516     Klass* type_argument(int i) {
 517       Metadata* x = argument(i);
 518       assert(x->is_klass(), "type");
 519       return (Klass*) x;
 520     }
 521 
 522     // The point of the whole exercise:  Is this dep still OK?
 523     Klass* check_dependency() {
 524       Klass* result = check_klass_dependency(NULL);
 525       if (result != NULL)  return result;
 526       return check_call_site_dependency(NULL);
 527     }
 528 
 529     // A lighter version:  Checks only around recent changes in a class
 530     // hierarchy.  (See Universe::flush_dependents_on.)
 531     Klass* spot_check_dependency_at(DepChange& changes);
 532 
 533     // Log the current dependency to xtty or compilation log.
 534     void log_dependency(Klass* witness = NULL);
 535 
 536     // Print the current dependency to tty.
 537     void print_dependency(Klass* witness = NULL, bool verbose = false);
 538   };
 539   friend class Dependencies::DepStream;
 540 
 541   static void print_statistics() PRODUCT_RETURN;
 542 };
 543 
 544 
 545 class DependencySignature : public ResourceObj {
 546  private:
 547   int                   _args_count;
 548   uintptr_t             _argument_hash[Dependencies::max_arg_count];
 549   Dependencies::DepType _type;
 550 
 551  public:
 552   DependencySignature(Dependencies::DepStream& dep) {
 553     _args_count = dep.argument_count();
 554     _type = dep.type();
 555     for (int i = 0; i < _args_count; i++) {
 556       _argument_hash[i] = dep.get_identifier(i);
 557     }
 558   }
 559 
 560   static bool     equals(DependencySignature const& s1, DependencySignature const& s2);
 561   static unsigned hash  (DependencySignature const& s1) { return s1.arg(0) >> 2; }
 562 
 563   int args_count()             const { return _args_count; }
 564   uintptr_t arg(int idx)       const { return _argument_hash[idx]; }
 565   Dependencies::DepType type() const { return _type; }
 566 
 567 };
 568 
 569 
 570 // Every particular DepChange is a sub-class of this class.
 571 class DepChange : public StackObj {
 572  public:
 573   // What kind of DepChange is this?
 574   virtual bool is_klass_change()     const { return false; }
 575   virtual bool is_call_site_change() const { return false; }

 576 
 577   // Subclass casting with assertions.
 578   KlassDepChange*    as_klass_change() {
 579     assert(is_klass_change(), "bad cast");
 580     return (KlassDepChange*) this;
 581   }
 582   CallSiteDepChange* as_call_site_change() {
 583     assert(is_call_site_change(), "bad cast");
 584     return (CallSiteDepChange*) this;
 585   }




 586 
 587   void print();
 588 
 589  public:
 590   enum ChangeType {
 591     NO_CHANGE = 0,              // an uninvolved klass
 592     Change_new_type,            // a newly loaded type
 593     Change_new_sub,             // a super with a new subtype
 594     Change_new_impl,            // an interface with a new implementation
 595     CHANGE_LIMIT,
 596     Start_Klass = CHANGE_LIMIT  // internal indicator for ContextStream
 597   };
 598 
 599   // Usage:
 600   // for (DepChange::ContextStream str(changes); str.next(); ) {
 601   //   Klass* k = str.klass();
 602   //   switch (str.change_type()) {
 603   //     ...
 604   //   }
 605   // }
 606   class ContextStream : public StackObj {
 607    private:
 608     DepChange&  _changes;
 609     friend class DepChange;
 610 
 611     // iteration variables:
 612     ChangeType  _change_type;
 613     Klass*      _klass;
 614     Array<Klass*>* _ti_base;    // i.e., transitive_interfaces
 615     int         _ti_index;
 616     int         _ti_limit;
 617 
 618     // start at the beginning:
 619     void start();
 620 
 621    public:
 622     ContextStream(DepChange& changes)
 623       : _changes(changes)
 624     { start(); }
 625 
 626     ContextStream(DepChange& changes, No_Safepoint_Verifier& nsv)
 627       : _changes(changes)
 628       // the nsv argument makes it safe to hold oops like _klass
 629     { start(); }
 630 
 631     bool next();
 632 
 633     ChangeType change_type()     { return _change_type; }
 634     Klass*     klass()           { return _klass; }
 635   };
 636   friend class DepChange::ContextStream;
 637 };
 638 
 639 
 640 // A class hierarchy change coming through the VM (under the Compile_lock).
 641 // The change is structured as a single new type with any number of supers
 642 // and implemented interface types.  Other than the new type, any of the
 643 // super types can be context types for a relevant dependency, which the
 644 // new type could invalidate.
 645 class KlassDepChange : public DepChange {
 646  private:
 647   // each change set is rooted in exactly one new type (at present):
 648   KlassHandle _new_type;
 649 
 650   void initialize();
 651 
 652  public:
 653   // notes the new type, marks it and all its super-types
 654   KlassDepChange(KlassHandle new_type)
 655     : _new_type(new_type)
 656   {
 657     initialize();
 658   }
 659 
 660   // cleans up the marks
 661   ~KlassDepChange();
 662 
 663   // What kind of DepChange is this?
 664   virtual bool is_klass_change() const { return true; }
 665 
 666   Klass* new_type() { return _new_type(); }
 667 
 668   // involves_context(k) is true if k is new_type or any of the super types
 669   bool involves_context(Klass* k);
 670 };
 671 
 672 
 673 // A CallSite has changed its target.
 674 class CallSiteDepChange : public DepChange {
 675  private:
 676   Handle _call_site;
 677   Handle _method_handle;
 678 
 679  public:
 680   CallSiteDepChange(Handle call_site, Handle method_handle)
 681     : _call_site(call_site),
 682       _method_handle(method_handle)
 683   {
 684     assert(_call_site()    ->is_a(SystemDictionary::CallSite_klass()),     "must be");
 685     assert(_method_handle.is_null() || _method_handle()->is_a(SystemDictionary::MethodHandle_klass()), "must be");
 686   }
 687 
 688   // What kind of DepChange is this?
 689   virtual bool is_call_site_change() const { return true; }
 690 
 691   oop call_site()     const { return _call_site();     }
 692   oop method_handle() const { return _method_handle(); }
 693 };
 694 
















 695 #endif // SHARE_VM_CODE_DEPENDENCIES_HPP
--- EOF ---