dependencies.hpp 25.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_CODE_DEPENDENCIES_HPP
#define SHARE_VM_CODE_DEPENDENCIES_HPP

28
#include "ci/ciCallSite.hpp"
29
#include "ci/ciKlass.hpp"
30 31
#include "ci/ciMethodHandle.hpp"
#include "classfile/systemDictionary.hpp"
32 33 34 35
#include "code/compressedStream.hpp"
#include "code/nmethod.hpp"
#include "utilities/growableArray.hpp"

D
duke 已提交
36
//** Dependencies represent assertions (approximate invariants) within
37 38 39 40 41 42
// the runtime system, e.g. class hierarchy changes.  An example is an
// assertion that a given method is not overridden; another example is
// that a type has only one concrete subtype.  Compiled code which
// relies on such assertions must be discarded if they are overturned
// by changes in the runtime system.  We can think of these assertions
// as approximate invariants, because we expect them to be overturned
D
duke 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// very infrequently.  We are willing to perform expensive recovery
// operations when they are overturned.  The benefit, of course, is
// performing optimistic optimizations (!) on the object code.
//
// Changes in the class hierarchy due to dynamic linking or
// class evolution can violate dependencies.  There is enough
// indexing between classes and nmethods to make dependency
// checking reasonably efficient.

class ciEnv;
class nmethod;
class OopRecorder;
class xmlStream;
class CompileLog;
class DepChange;
58 59
class   KlassDepChange;
class   CallSiteDepChange;
D
duke 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
class No_Safepoint_Verifier;

class Dependencies: public ResourceObj {
 public:
  // Note: In the comments on dependency types, most uses of the terms
  // subtype and supertype are used in a "non-strict" or "inclusive"
  // sense, and are starred to remind the reader of this fact.
  // Strict uses of the terms use the word "proper".
  //
  // Specifically, every class is its own subtype* and supertype*.
  // (This trick is easier than continually saying things like "Y is a
  // subtype of X or X itself".)
  //
  // Sometimes we write X > Y to mean X is a proper supertype of Y.
  // The notation X > {Y, Z} means X has proper subtypes Y, Z.
  // The notation X.m > Y means that Y inherits m from X, while
  // X.m > Y.m means Y overrides X.m.  A star denotes abstractness,
  // as *I > A, meaning (abstract) interface I is a super type of A,
  // or A.*m > B.m, meaning B.m implements abstract method A.m.
  //
  // In this module, the terms "subtype" and "supertype" refer to
  // Java-level reference type conversions, as detected by
  // "instanceof" and performed by "checkcast" operations.  The method
  // Klass::is_subtype_of tests these relations.  Note that "subtype"
  // is richer than "subclass" (as tested by Klass::is_subclass_of),
  // since it takes account of relations involving interface and array
  // types.
  //
  // To avoid needless complexity, dependencies involving array types
  // are not accepted.  If you need to make an assertion about an
  // array type, make the assertion about its corresponding element
  // types.  Any assertion that might change about an array type can
  // be converted to an assertion about its element type.
  //
  // Most dependencies are evaluated over a "context type" CX, which
  // stands for the set Subtypes(CX) of every Java type that is a subtype*
  // of CX.  When the system loads a new class or interface N, it is
  // responsible for re-evaluating changed dependencies whose context
  // type now includes N, that is, all super types of N.
  //
  enum DepType {
    end_marker = 0,

    // An 'evol' dependency simply notes that the contents of the
    // method were used.  If it evolves (is replaced), the nmethod
    // must be recompiled.  No other dependencies are implied.
    evol_method,
    FIRST_TYPE = evol_method,

    // A context type CX is a leaf it if has no proper subtype.
    leaf_type,

    // An abstract class CX has exactly one concrete subtype CC.
    abstract_with_unique_concrete_subtype,

    // The type CX is purely abstract, with no concrete subtype* at all.
    abstract_with_no_concrete_subtype,

    // The concrete CX is free of concrete proper subtypes.
    concrete_with_no_concrete_subtype,

    // Given a method M1 and a context class CX, the set MM(CX, M1) of
    // "concrete matching methods" in CX of M1 is the set of every
    // concrete M2 for which it is possible to create an invokevirtual
    // or invokeinterface call site that can reach either M1 or M2.
    // That is, M1 and M2 share a name, signature, and vtable index.
    // We wish to notice when the set MM(CX, M1) is just {M1}, or
    // perhaps a set of two {M1,M2}, and issue dependencies on this.

    // The set MM(CX, M1) can be computed by starting with any matching
    // concrete M2 that is inherited into CX, and then walking the
    // subtypes* of CX looking for concrete definitions.

    // The parameters to this dependency are the method M1 and the
    // context class CX.  M1 must be either inherited in CX or defined
    // in a subtype* of CX.  It asserts that MM(CX, M1) is no greater
    // than {M1}.
    unique_concrete_method,       // one unique concrete method under CX

    // An "exclusive" assertion concerns two methods or subtypes, and
    // declares that there are at most two (or perhaps later N>2)
    // specific items that jointly satisfy the restriction.
    // We list all items explicitly rather than just giving their
    // count, for robustness in the face of complex schema changes.

    // A context class CX (which may be either abstract or concrete)
    // has two exclusive concrete subtypes* C1, C2 if every concrete
    // subtype* of CX is either C1 or C2.  Note that if neither C1 or C2
    // are equal to CX, then CX itself must be abstract.  But it is
    // also possible (for example) that C1 is CX (a concrete class)
    // and C2 is a proper subtype of C1.
    abstract_with_exclusive_concrete_subtypes_2,

    // This dependency asserts that MM(CX, M1) is no greater than {M1,M2}.
    exclusive_concrete_methods_2,

    // This dependency asserts that no instances of class or it's
    // subclasses require finalization registration.
    no_finalizable_subclasses,

160 161 162
    // This dependency asserts when the CallSite.target value changed.
    call_site_target_value,

D
duke 已提交
163 164 165 166 167 168
    TYPE_LIMIT
  };
  enum {
    LG2_TYPE_LIMIT = 4,  // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))

    // handy categorizations of dependency types:
169 170 171 172 173 174 175 176
    all_types           = ((1 << TYPE_LIMIT) - 1) & ((-1) << FIRST_TYPE),

    non_klass_types     = (1 << call_site_target_value),
    klass_types         = all_types & ~non_klass_types,

    non_ctxk_types      = (1 << evol_method),
    implicit_ctxk_types = (1 << call_site_target_value),
    explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),
D
duke 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

    max_arg_count = 3,   // current maximum number of arguments (incl. ctxk)

    // A "context type" is a class or interface that
    // provides context for evaluating a dependency.
    // When present, it is one of the arguments (dep_context_arg).
    //
    // If a dependency does not have a context type, there is a
    // default context, depending on the type of the dependency.
    // This bit signals that a default context has been compressed away.
    default_context_type_bit = (1<<LG2_TYPE_LIMIT)
  };

  static const char* dep_name(DepType dept);
  static int         dep_args(DepType dept);
192 193 194 195 196 197 198 199 200

  static bool is_klass_type(           DepType dept) { return dept_in_mask(dept, klass_types        ); }

  static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }
  static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }

  static int           dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }
  static int  dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }

201
  static void check_valid_dependency_type(DepType dept);
D
duke 已提交
202 203 204 205

 private:
  // State for writing a new set of dependencies:
  GrowableArray<int>*       _dep_seen;  // (seen[h->ident] & (1<<dept))
206
  GrowableArray<ciBaseObject*>*  _deps[TYPE_LIMIT];
D
duke 已提交
207 208 209 210 211 212 213 214

  static const char* _dep_name[TYPE_LIMIT];
  static int         _dep_args[TYPE_LIMIT];

  static bool dept_in_mask(DepType dept, int mask) {
    return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;
  }

215
  bool note_dep_seen(int dept, ciBaseObject* x) {
D
duke 已提交
216 217 218 219 220 221 222 223 224
    assert(dept < BitsPerInt, "oob");
    int x_id = x->ident();
    assert(_dep_seen != NULL, "deps must be writable");
    int seen = _dep_seen->at_grow(x_id, 0);
    _dep_seen->at_put(x_id, seen | (1<<dept));
    // return true if we've already seen dept/x
    return (seen & (1<<dept)) != 0;
  }

225
  bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
D
duke 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                        int ctxk_i, ciKlass* ctxk);

  void sort_all_deps();
  size_t estimate_size_in_bytes();

  // Initialize _deps, etc.
  void initialize(ciEnv* env);

  // State for making a new set of dependencies:
  OopRecorder* _oop_recorder;

  // Logging support
  CompileLog* _log;

  address  _content_bytes;  // everything but the oop references, encoded
  size_t   _size_in_bytes;

 public:
  // Make a new empty dependencies set.
  Dependencies(ciEnv* env) {
    initialize(env);
  }

 private:
  // Check for a valid context type.
  // Enforce the restriction against array types.
  static void check_ctxk(ciKlass* ctxk) {
    assert(ctxk->is_instance_klass(), "java types only");
  }
  static void check_ctxk_concrete(ciKlass* ctxk) {
    assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");
  }
  static void check_ctxk_abstract(ciKlass* ctxk) {
    check_ctxk(ctxk);
    assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");
  }

263 264 265
  void assert_common_1(DepType dept, ciBaseObject* x);
  void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);
  void assert_common_3(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2);
D
duke 已提交
266 267 268 269 270 271 272 273 274 275 276 277

 public:
  // Adding assertions to a new dependency set at compile time:
  void assert_evol_method(ciMethod* m);
  void assert_leaf_type(ciKlass* ctxk);
  void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);
  void assert_abstract_with_no_concrete_subtype(ciKlass* ctxk);
  void assert_concrete_with_no_concrete_subtype(ciKlass* ctxk);
  void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm);
  void assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2);
  void assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2);
  void assert_has_no_finalizable_subclasses(ciKlass* ctxk);
278
  void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);
D
duke 已提交
279 280 281 282 283 284 285 286 287 288

  // Define whether a given method or type is concrete.
  // These methods define the term "concrete" as used in this module.
  // For this module, an "abstract" class is one which is non-concrete.
  //
  // Future optimizations may allow some classes to remain
  // non-concrete until their first instantiation, and allow some
  // methods to remain non-concrete until their first invocation.
  // In that case, there would be a middle ground between concrete
  // and abstract (as defined by the Java language and VM).
289 290
  static bool is_concrete_klass(Klass* k);    // k is instantiable
  static bool is_concrete_method(Method* m);  // m is invocable
D
duke 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  static Klass* find_finalizable_subclass(Klass* k);

  // These versions of the concreteness queries work through the CI.
  // The CI versions are allowed to skew sometimes from the VM
  // (oop-based) versions.  The cost of such a difference is a
  // (safely) aborted compilation, or a deoptimization, or a missed
  // optimization opportunity.
  //
  // In order to prevent spurious assertions, query results must
  // remain stable within any single ciEnv instance.  (I.e., they must
  // not go back into the VM to get their value; they must cache the
  // bit in the CI, either eagerly or lazily.)
  static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable
  static bool is_concrete_method(ciMethod* m);       // m appears invocable
  static bool has_finalizable_subclass(ciInstanceKlass* k);

  // As a general rule, it is OK to compile under the assumption that
  // a given type or method is concrete, even if it at some future
  // point becomes abstract.  So dependency checking is one-sided, in
  // that it permits supposedly concrete classes or methods to turn up
  // as really abstract.  (This shouldn't happen, except during class
  // evolution, but that's the logic of the checking.)  However, if a
  // supposedly abstract class or method suddenly becomes concrete, a
  // dependency on it must fail.

  // Checking old assertions at run-time (in the VM only):
317 318 319
  static Klass* check_evol_method(Method* m);
  static Klass* check_leaf_type(Klass* ctxk);
  static Klass* check_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck,
320
                                                              KlassDepChange* changes = NULL);
321
  static Klass* check_abstract_with_no_concrete_subtype(Klass* ctxk,
322
                                                          KlassDepChange* changes = NULL);
323
  static Klass* check_concrete_with_no_concrete_subtype(Klass* ctxk,
324
                                                          KlassDepChange* changes = NULL);
325
  static Klass* check_unique_concrete_method(Klass* ctxk, Method* uniqm,
326
                                               KlassDepChange* changes = NULL);
327
  static Klass* check_abstract_with_exclusive_concrete_subtypes(Klass* ctxk, Klass* k1, Klass* k2,
328
                                                                  KlassDepChange* changes = NULL);
329
  static Klass* check_exclusive_concrete_methods(Klass* ctxk, Method* m1, Method* m2,
330
                                                   KlassDepChange* changes = NULL);
331 332 333 334
  static Klass* check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes = NULL);
  static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL);
  // A returned Klass* is NULL if the dependency assertion is still
  // valid.  A non-NULL Klass* is a 'witness' to the assertion
D
duke 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347
  // failure, a point in the class hierarchy where the assertion has
  // been proven false.  For example, if check_leaf_type returns
  // non-NULL, the value is a subtype of the supposed leaf type.  This
  // witness value may be useful for logging the dependency failure.
  // Note that, when a dependency fails, there may be several possible
  // witnesses to the failure.  The value returned from the check_foo
  // method is chosen arbitrarily.

  // The 'changes' value, if non-null, requests a limited spot-check
  // near the indicated recent changes in the class hierarchy.
  // It is used by DepStream::spot_check_dependency_at.

  // Detecting possible new assertions:
348 349 350 351
  static Klass*    find_unique_concrete_subtype(Klass* ctxk);
  static Method*   find_unique_concrete_method(Klass* ctxk, Method* m);
  static int       find_exclusive_concrete_subtypes(Klass* ctxk, int klen, Klass* k[]);
  static int       find_exclusive_concrete_methods(Klass* ctxk, int mlen, Method* m[]);
D
duke 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

  // Create the encoding which will be stored in an nmethod.
  void encode_content_bytes();

  address content_bytes() {
    assert(_content_bytes != NULL, "encode it first");
    return _content_bytes;
  }
  size_t size_in_bytes() {
    assert(_content_bytes != NULL, "encode it first");
    return _size_in_bytes;
  }

  OopRecorder* oop_recorder() { return _oop_recorder; }
  CompileLog*  log()          { return _log; }

  void copy_to(nmethod* nm);

  void log_all_dependencies();
371 372 373 374 375 376 377

  void log_dependency(DepType dept, GrowableArray<ciBaseObject*>* args) {
    ResourceMark rm;
    int argslen = args->length();
    write_dependency_to(log(), dept, args);
    guarantee(argslen == args->length(),
              "args array cannot grow inside nested ResoureMark scope");
D
duke 已提交
378
  }
379

D
duke 已提交
380
  void log_dependency(DepType dept,
381 382 383
                      ciBaseObject* x0,
                      ciBaseObject* x1 = NULL,
                      ciBaseObject* x2 = NULL) {
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    if (log() == NULL) {
      return;
    }
    ResourceMark rm;
    GrowableArray<ciBaseObject*>* ciargs =
                new GrowableArray<ciBaseObject*>(dep_args(dept));
    assert (x0 != NULL, "no log x0");
    ciargs->push(x0);

    if (x1 != NULL) {
      ciargs->push(x1);
    }
    if (x2 != NULL) {
      ciargs->push(x2);
    }
    assert(ciargs->length() == dep_args(dept), "");
    log_dependency(dept, ciargs);
D
duke 已提交
401 402
  }

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  class DepArgument : public ResourceObj {
   private:
    bool  _is_oop;
    bool  _valid;
    void* _value;
   public:
    DepArgument() : _is_oop(false), _value(NULL), _valid(false) {}
    DepArgument(oop v): _is_oop(true), _value(v), _valid(true) {}
    DepArgument(Metadata* v): _is_oop(false), _value(v), _valid(true) {}

    bool is_null() const               { return _value == NULL; }
    bool is_oop() const                { return _is_oop; }
    bool is_metadata() const           { return !_is_oop; }
    bool is_klass() const              { return is_metadata() && metadata_value()->is_klass(); }
    bool is_method() const              { return is_metadata() && metadata_value()->is_method(); }

    oop oop_value() const              { assert(_is_oop && _valid, "must be"); return (oop) _value; }
    Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }
  };

D
duke 已提交
423
  static void print_dependency(DepType dept,
424
                               GrowableArray<DepArgument>* args,
425
                               Klass* witness = NULL);
D
duke 已提交
426 427 428

 private:
  // helper for encoding common context types as zero:
429
  static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);
D
duke 已提交
430

431
  static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);
D
duke 已提交
432

433 434 435 436 437 438 439 440 441 442 443 444
  static void write_dependency_to(CompileLog* log,
                                  DepType dept,
                                  GrowableArray<ciBaseObject*>* args,
                                  Klass* witness = NULL);
  static void write_dependency_to(CompileLog* log,
                                  DepType dept,
                                  GrowableArray<DepArgument>* args,
                                  Klass* witness = NULL);
  static void write_dependency_to(xmlStream* xtty,
                                  DepType dept,
                                  GrowableArray<DepArgument>* args,
                                  Klass* witness = NULL);
D
duke 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
 public:
  // Use this to iterate over an nmethod's dependency set.
  // Works on new and old dependency sets.
  // Usage:
  //
  // ;
  // Dependencies::DepType dept;
  // for (Dependencies::DepStream deps(nm); deps.next(); ) {
  //   ...
  // }
  //
  // The caller must be in the VM, since oops are not wrapped in handles.
  class DepStream {
  private:
    nmethod*              _code;   // null if in a compiler thread
    Dependencies*         _deps;   // null if not in a compiler thread
    CompressedReadStream  _bytes;
#ifdef ASSERT
    size_t                _byte_limit;
#endif

    // iteration variables:
    DepType               _type;
    int                   _xi[max_arg_count+1];

    void initial_asserts(size_t byte_limit) NOT_DEBUG({});

472
    inline Metadata* recorded_metadata_at(int i);
D
duke 已提交
473 474
    inline oop recorded_oop_at(int i);

475 476
    Klass* check_klass_dependency(KlassDepChange* changes);
    Klass* check_call_site_dependency(CallSiteDepChange* changes);
477

478
    void trace_and_log_witness(Klass* witness);
D
duke 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

  public:
    DepStream(Dependencies* deps)
      : _deps(deps),
        _code(NULL),
        _bytes(deps->content_bytes())
    {
      initial_asserts(deps->size_in_bytes());
    }
    DepStream(nmethod* code)
      : _deps(NULL),
        _code(code),
        _bytes(code->dependencies_begin())
    {
      initial_asserts(code->dependencies_size());
    }

    bool next();

    DepType type()               { return _type; }
    int argument_count()         { return dep_args(type()); }
    int argument_index(int i)    { assert(0 <= i && i < argument_count(), "oob");
                                   return _xi[i]; }
502 503 504
    Metadata* argument(int i);     // => recorded_oop_at(argument_index(i))
    oop argument_oop(int i);         // => recorded_oop_at(argument_index(i))
    Klass* context_type();
D
duke 已提交
505

506 507
    bool is_klass_type()         { return Dependencies::is_klass_type(type()); }

508 509
    Method* method_argument(int i) {
      Metadata* x = argument(i);
D
duke 已提交
510
      assert(x->is_method(), "type");
511
      return (Method*) x;
D
duke 已提交
512
    }
513 514
    Klass* type_argument(int i) {
      Metadata* x = argument(i);
D
duke 已提交
515
      assert(x->is_klass(), "type");
516
      return (Klass*) x;
D
duke 已提交
517 518
    }

519
    // The point of the whole exercise:  Is this dep still OK?
520 521
    Klass* check_dependency() {
      Klass* result = check_klass_dependency(NULL);
522 523
      if (result != NULL)  return result;
      return check_call_site_dependency(NULL);
D
duke 已提交
524
    }
525

D
duke 已提交
526 527
    // A lighter version:  Checks only around recent changes in a class
    // hierarchy.  (See Universe::flush_dependents_on.)
528
    Klass* spot_check_dependency_at(DepChange& changes);
D
duke 已提交
529 530

    // Log the current dependency to xtty or compilation log.
531
    void log_dependency(Klass* witness = NULL);
D
duke 已提交
532 533

    // Print the current dependency to tty.
534
    void print_dependency(Klass* witness = NULL, bool verbose = false);
D
duke 已提交
535 536 537 538 539 540
  };
  friend class Dependencies::DepStream;

  static void print_statistics() PRODUCT_RETURN;
};

541 542

// Every particular DepChange is a sub-class of this class.
D
duke 已提交
543
class DepChange : public StackObj {
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
 public:
  // What kind of DepChange is this?
  virtual bool is_klass_change()     const { return false; }
  virtual bool is_call_site_change() const { return false; }

  // Subclass casting with assertions.
  KlassDepChange*    as_klass_change() {
    assert(is_klass_change(), "bad cast");
    return (KlassDepChange*) this;
  }
  CallSiteDepChange* as_call_site_change() {
    assert(is_call_site_change(), "bad cast");
    return (CallSiteDepChange*) this;
  }

  void print();

561
 public:
D
duke 已提交
562 563 564 565 566 567 568 569 570 571 572
  enum ChangeType {
    NO_CHANGE = 0,              // an uninvolved klass
    Change_new_type,            // a newly loaded type
    Change_new_sub,             // a super with a new subtype
    Change_new_impl,            // an interface with a new implementation
    CHANGE_LIMIT,
    Start_Klass = CHANGE_LIMIT  // internal indicator for ContextStream
  };

  // Usage:
  // for (DepChange::ContextStream str(changes); str.next(); ) {
573
  //   Klass* k = str.klass();
D
duke 已提交
574 575 576 577 578 579
  //   switch (str.change_type()) {
  //     ...
  //   }
  // }
  class ContextStream : public StackObj {
   private:
580
    DepChange&  _changes;
D
duke 已提交
581 582 583
    friend class DepChange;

    // iteration variables:
584
    ChangeType  _change_type;
585 586
    Klass*      _klass;
    Array<Klass*>* _ti_base;    // i.e., transitive_interfaces
587 588
    int         _ti_index;
    int         _ti_limit;
D
duke 已提交
589 590

    // start at the beginning:
591
    void start();
D
duke 已提交
592

593
   public:
D
duke 已提交
594 595 596 597 598 599 600 601 602 603 604
    ContextStream(DepChange& changes)
      : _changes(changes)
    { start(); }

    ContextStream(DepChange& changes, No_Safepoint_Verifier& nsv)
      : _changes(changes)
      // the nsv argument makes it safe to hold oops like _klass
    { start(); }

    bool next();

605
    ChangeType change_type()     { return _change_type; }
606
    Klass*     klass()           { return _klass; }
D
duke 已提交
607 608
  };
  friend class DepChange::ContextStream;
609
};
D
duke 已提交
610

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

// A class hierarchy change coming through the VM (under the Compile_lock).
// The change is structured as a single new type with any number of supers
// and implemented interface types.  Other than the new type, any of the
// super types can be context types for a relevant dependency, which the
// new type could invalidate.
class KlassDepChange : public DepChange {
 private:
  // each change set is rooted in exactly one new type (at present):
  KlassHandle _new_type;

  void initialize();

 public:
  // notes the new type, marks it and all its super-types
  KlassDepChange(KlassHandle new_type)
    : _new_type(new_type)
  {
    initialize();
  }

  // cleans up the marks
  ~KlassDepChange();

  // What kind of DepChange is this?
  virtual bool is_klass_change() const { return true; }

638
  Klass* new_type() { return _new_type(); }
639 640

  // involves_context(k) is true if k is new_type or any of the super types
641
  bool involves_context(Klass* k);
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
};


// A CallSite has changed its target.
class CallSiteDepChange : public DepChange {
 private:
  Handle _call_site;
  Handle _method_handle;

 public:
  CallSiteDepChange(Handle call_site, Handle method_handle)
    : _call_site(call_site),
      _method_handle(method_handle)
  {
    assert(_call_site()    ->is_a(SystemDictionary::CallSite_klass()),     "must be");
    assert(_method_handle()->is_a(SystemDictionary::MethodHandle_klass()), "must be");
  }

  // What kind of DepChange is this?
  virtual bool is_call_site_change() const { return true; }

  oop call_site()     const { return _call_site();     }
  oop method_handle() const { return _method_handle(); }
D
duke 已提交
665
};
666 667

#endif // SHARE_VM_CODE_DEPENDENCIES_HPP