src/share/vm/code/dependencies.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/code/dependencies.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,651 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_CODE_DEPENDENCIES_HPP
    1.29 +#define SHARE_VM_CODE_DEPENDENCIES_HPP
    1.30 +
    1.31 +#include "ci/ciCallSite.hpp"
    1.32 +#include "ci/ciKlass.hpp"
    1.33 +#include "ci/ciMethodHandle.hpp"
    1.34 +#include "classfile/systemDictionary.hpp"
    1.35 +#include "code/compressedStream.hpp"
    1.36 +#include "code/nmethod.hpp"
    1.37 +#include "utilities/growableArray.hpp"
    1.38 +
    1.39 +//** Dependencies represent assertions (approximate invariants) within
    1.40 +// the runtime system, e.g. class hierarchy changes.  An example is an
    1.41 +// assertion that a given method is not overridden; another example is
    1.42 +// that a type has only one concrete subtype.  Compiled code which
    1.43 +// relies on such assertions must be discarded if they are overturned
    1.44 +// by changes in the runtime system.  We can think of these assertions
    1.45 +// as approximate invariants, because we expect them to be overturned
    1.46 +// very infrequently.  We are willing to perform expensive recovery
    1.47 +// operations when they are overturned.  The benefit, of course, is
    1.48 +// performing optimistic optimizations (!) on the object code.
    1.49 +//
    1.50 +// Changes in the class hierarchy due to dynamic linking or
    1.51 +// class evolution can violate dependencies.  There is enough
    1.52 +// indexing between classes and nmethods to make dependency
    1.53 +// checking reasonably efficient.
    1.54 +
    1.55 +class ciEnv;
    1.56 +class nmethod;
    1.57 +class OopRecorder;
    1.58 +class xmlStream;
    1.59 +class CompileLog;
    1.60 +class DepChange;
    1.61 +class   KlassDepChange;
    1.62 +class   CallSiteDepChange;
    1.63 +class No_Safepoint_Verifier;
    1.64 +
    1.65 +class Dependencies: public ResourceObj {
    1.66 + public:
    1.67 +  // Note: In the comments on dependency types, most uses of the terms
    1.68 +  // subtype and supertype are used in a "non-strict" or "inclusive"
    1.69 +  // sense, and are starred to remind the reader of this fact.
    1.70 +  // Strict uses of the terms use the word "proper".
    1.71 +  //
    1.72 +  // Specifically, every class is its own subtype* and supertype*.
    1.73 +  // (This trick is easier than continually saying things like "Y is a
    1.74 +  // subtype of X or X itself".)
    1.75 +  //
    1.76 +  // Sometimes we write X > Y to mean X is a proper supertype of Y.
    1.77 +  // The notation X > {Y, Z} means X has proper subtypes Y, Z.
    1.78 +  // The notation X.m > Y means that Y inherits m from X, while
    1.79 +  // X.m > Y.m means Y overrides X.m.  A star denotes abstractness,
    1.80 +  // as *I > A, meaning (abstract) interface I is a super type of A,
    1.81 +  // or A.*m > B.m, meaning B.m implements abstract method A.m.
    1.82 +  //
    1.83 +  // In this module, the terms "subtype" and "supertype" refer to
    1.84 +  // Java-level reference type conversions, as detected by
    1.85 +  // "instanceof" and performed by "checkcast" operations.  The method
    1.86 +  // Klass::is_subtype_of tests these relations.  Note that "subtype"
    1.87 +  // is richer than "subclass" (as tested by Klass::is_subclass_of),
    1.88 +  // since it takes account of relations involving interface and array
    1.89 +  // types.
    1.90 +  //
    1.91 +  // To avoid needless complexity, dependencies involving array types
    1.92 +  // are not accepted.  If you need to make an assertion about an
    1.93 +  // array type, make the assertion about its corresponding element
    1.94 +  // types.  Any assertion that might change about an array type can
    1.95 +  // be converted to an assertion about its element type.
    1.96 +  //
    1.97 +  // Most dependencies are evaluated over a "context type" CX, which
    1.98 +  // stands for the set Subtypes(CX) of every Java type that is a subtype*
    1.99 +  // of CX.  When the system loads a new class or interface N, it is
   1.100 +  // responsible for re-evaluating changed dependencies whose context
   1.101 +  // type now includes N, that is, all super types of N.
   1.102 +  //
   1.103 +  enum DepType {
   1.104 +    end_marker = 0,
   1.105 +
   1.106 +    // An 'evol' dependency simply notes that the contents of the
   1.107 +    // method were used.  If it evolves (is replaced), the nmethod
   1.108 +    // must be recompiled.  No other dependencies are implied.
   1.109 +    evol_method,
   1.110 +    FIRST_TYPE = evol_method,
   1.111 +
   1.112 +    // A context type CX is a leaf it if has no proper subtype.
   1.113 +    leaf_type,
   1.114 +
   1.115 +    // An abstract class CX has exactly one concrete subtype CC.
   1.116 +    abstract_with_unique_concrete_subtype,
   1.117 +
   1.118 +    // The type CX is purely abstract, with no concrete subtype* at all.
   1.119 +    abstract_with_no_concrete_subtype,
   1.120 +
   1.121 +    // The concrete CX is free of concrete proper subtypes.
   1.122 +    concrete_with_no_concrete_subtype,
   1.123 +
   1.124 +    // Given a method M1 and a context class CX, the set MM(CX, M1) of
   1.125 +    // "concrete matching methods" in CX of M1 is the set of every
   1.126 +    // concrete M2 for which it is possible to create an invokevirtual
   1.127 +    // or invokeinterface call site that can reach either M1 or M2.
   1.128 +    // That is, M1 and M2 share a name, signature, and vtable index.
   1.129 +    // We wish to notice when the set MM(CX, M1) is just {M1}, or
   1.130 +    // perhaps a set of two {M1,M2}, and issue dependencies on this.
   1.131 +
   1.132 +    // The set MM(CX, M1) can be computed by starting with any matching
   1.133 +    // concrete M2 that is inherited into CX, and then walking the
   1.134 +    // subtypes* of CX looking for concrete definitions.
   1.135 +
   1.136 +    // The parameters to this dependency are the method M1 and the
   1.137 +    // context class CX.  M1 must be either inherited in CX or defined
   1.138 +    // in a subtype* of CX.  It asserts that MM(CX, M1) is no greater
   1.139 +    // than {M1}.
   1.140 +    unique_concrete_method,       // one unique concrete method under CX
   1.141 +
   1.142 +    // An "exclusive" assertion concerns two methods or subtypes, and
   1.143 +    // declares that there are at most two (or perhaps later N>2)
   1.144 +    // specific items that jointly satisfy the restriction.
   1.145 +    // We list all items explicitly rather than just giving their
   1.146 +    // count, for robustness in the face of complex schema changes.
   1.147 +
   1.148 +    // A context class CX (which may be either abstract or concrete)
   1.149 +    // has two exclusive concrete subtypes* C1, C2 if every concrete
   1.150 +    // subtype* of CX is either C1 or C2.  Note that if neither C1 or C2
   1.151 +    // are equal to CX, then CX itself must be abstract.  But it is
   1.152 +    // also possible (for example) that C1 is CX (a concrete class)
   1.153 +    // and C2 is a proper subtype of C1.
   1.154 +    abstract_with_exclusive_concrete_subtypes_2,
   1.155 +
   1.156 +    // This dependency asserts that MM(CX, M1) is no greater than {M1,M2}.
   1.157 +    exclusive_concrete_methods_2,
   1.158 +
   1.159 +    // This dependency asserts that no instances of class or it's
   1.160 +    // subclasses require finalization registration.
   1.161 +    no_finalizable_subclasses,
   1.162 +
   1.163 +    // This dependency asserts when the CallSite.target value changed.
   1.164 +    call_site_target_value,
   1.165 +
   1.166 +    TYPE_LIMIT
   1.167 +  };
   1.168 +  enum {
   1.169 +    LG2_TYPE_LIMIT = 4,  // assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT))
   1.170 +
   1.171 +    // handy categorizations of dependency types:
   1.172 +    all_types           = ((1 << TYPE_LIMIT) - 1) & ((-1) << FIRST_TYPE),
   1.173 +
   1.174 +    non_klass_types     = (1 << call_site_target_value),
   1.175 +    klass_types         = all_types & ~non_klass_types,
   1.176 +
   1.177 +    non_ctxk_types      = (1 << evol_method),
   1.178 +    implicit_ctxk_types = (1 << call_site_target_value),
   1.179 +    explicit_ctxk_types = all_types & ~(non_ctxk_types | implicit_ctxk_types),
   1.180 +
   1.181 +    max_arg_count = 3,   // current maximum number of arguments (incl. ctxk)
   1.182 +
   1.183 +    // A "context type" is a class or interface that
   1.184 +    // provides context for evaluating a dependency.
   1.185 +    // When present, it is one of the arguments (dep_context_arg).
   1.186 +    //
   1.187 +    // If a dependency does not have a context type, there is a
   1.188 +    // default context, depending on the type of the dependency.
   1.189 +    // This bit signals that a default context has been compressed away.
   1.190 +    default_context_type_bit = (1<<LG2_TYPE_LIMIT)
   1.191 +  };
   1.192 +
   1.193 +  static const char* dep_name(DepType dept);
   1.194 +  static int         dep_args(DepType dept);
   1.195 +
   1.196 +  static bool is_klass_type(           DepType dept) { return dept_in_mask(dept, klass_types        ); }
   1.197 +
   1.198 +  static bool has_explicit_context_arg(DepType dept) { return dept_in_mask(dept, explicit_ctxk_types); }
   1.199 +  static bool has_implicit_context_arg(DepType dept) { return dept_in_mask(dept, implicit_ctxk_types); }
   1.200 +
   1.201 +  static int           dep_context_arg(DepType dept) { return has_explicit_context_arg(dept) ? 0 : -1; }
   1.202 +  static int  dep_implicit_context_arg(DepType dept) { return has_implicit_context_arg(dept) ? 0 : -1; }
   1.203 +
   1.204 +  static void check_valid_dependency_type(DepType dept);
   1.205 +
   1.206 + private:
   1.207 +  // State for writing a new set of dependencies:
   1.208 +  GrowableArray<int>*       _dep_seen;  // (seen[h->ident] & (1<<dept))
   1.209 +  GrowableArray<ciBaseObject*>*  _deps[TYPE_LIMIT];
   1.210 +
   1.211 +  static const char* _dep_name[TYPE_LIMIT];
   1.212 +  static int         _dep_args[TYPE_LIMIT];
   1.213 +
   1.214 +  static bool dept_in_mask(DepType dept, int mask) {
   1.215 +    return (int)dept >= 0 && dept < TYPE_LIMIT && ((1<<dept) & mask) != 0;
   1.216 +  }
   1.217 +
   1.218 +  bool note_dep_seen(int dept, ciBaseObject* x) {
   1.219 +    assert(dept < BitsPerInt, "oob");
   1.220 +    int x_id = x->ident();
   1.221 +    assert(_dep_seen != NULL, "deps must be writable");
   1.222 +    int seen = _dep_seen->at_grow(x_id, 0);
   1.223 +    _dep_seen->at_put(x_id, seen | (1<<dept));
   1.224 +    // return true if we've already seen dept/x
   1.225 +    return (seen & (1<<dept)) != 0;
   1.226 +  }
   1.227 +
   1.228 +  bool maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
   1.229 +                        int ctxk_i, ciKlass* ctxk);
   1.230 +
   1.231 +  void sort_all_deps();
   1.232 +  size_t estimate_size_in_bytes();
   1.233 +
   1.234 +  // Initialize _deps, etc.
   1.235 +  void initialize(ciEnv* env);
   1.236 +
   1.237 +  // State for making a new set of dependencies:
   1.238 +  OopRecorder* _oop_recorder;
   1.239 +
   1.240 +  // Logging support
   1.241 +  CompileLog* _log;
   1.242 +
   1.243 +  address  _content_bytes;  // everything but the oop references, encoded
   1.244 +  size_t   _size_in_bytes;
   1.245 +
   1.246 + public:
   1.247 +  // Make a new empty dependencies set.
   1.248 +  Dependencies(ciEnv* env) {
   1.249 +    initialize(env);
   1.250 +  }
   1.251 +
   1.252 + private:
   1.253 +  // Check for a valid context type.
   1.254 +  // Enforce the restriction against array types.
   1.255 +  static void check_ctxk(ciKlass* ctxk) {
   1.256 +    assert(ctxk->is_instance_klass(), "java types only");
   1.257 +  }
   1.258 +  static void check_ctxk_concrete(ciKlass* ctxk) {
   1.259 +    assert(is_concrete_klass(ctxk->as_instance_klass()), "must be concrete");
   1.260 +  }
   1.261 +  static void check_ctxk_abstract(ciKlass* ctxk) {
   1.262 +    check_ctxk(ctxk);
   1.263 +    assert(!is_concrete_klass(ctxk->as_instance_klass()), "must be abstract");
   1.264 +  }
   1.265 +
   1.266 +  void assert_common_1(DepType dept, ciBaseObject* x);
   1.267 +  void assert_common_2(DepType dept, ciBaseObject* x0, ciBaseObject* x1);
   1.268 +  void assert_common_3(DepType dept, ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2);
   1.269 +
   1.270 + public:
   1.271 +  // Adding assertions to a new dependency set at compile time:
   1.272 +  void assert_evol_method(ciMethod* m);
   1.273 +  void assert_leaf_type(ciKlass* ctxk);
   1.274 +  void assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck);
   1.275 +  void assert_abstract_with_no_concrete_subtype(ciKlass* ctxk);
   1.276 +  void assert_concrete_with_no_concrete_subtype(ciKlass* ctxk);
   1.277 +  void assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm);
   1.278 +  void assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2);
   1.279 +  void assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2);
   1.280 +  void assert_has_no_finalizable_subclasses(ciKlass* ctxk);
   1.281 +  void assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle);
   1.282 +
   1.283 +  // Define whether a given method or type is concrete.
   1.284 +  // These methods define the term "concrete" as used in this module.
   1.285 +  // For this module, an "abstract" class is one which is non-concrete.
   1.286 +  //
   1.287 +  // Future optimizations may allow some classes to remain
   1.288 +  // non-concrete until their first instantiation, and allow some
   1.289 +  // methods to remain non-concrete until their first invocation.
   1.290 +  // In that case, there would be a middle ground between concrete
   1.291 +  // and abstract (as defined by the Java language and VM).
   1.292 +  static bool is_concrete_klass(Klass* k);    // k is instantiable
   1.293 +  static bool is_concrete_method(Method* m);  // m is invocable
   1.294 +  static Klass* find_finalizable_subclass(Klass* k);
   1.295 +
   1.296 +  // These versions of the concreteness queries work through the CI.
   1.297 +  // The CI versions are allowed to skew sometimes from the VM
   1.298 +  // (oop-based) versions.  The cost of such a difference is a
   1.299 +  // (safely) aborted compilation, or a deoptimization, or a missed
   1.300 +  // optimization opportunity.
   1.301 +  //
   1.302 +  // In order to prevent spurious assertions, query results must
   1.303 +  // remain stable within any single ciEnv instance.  (I.e., they must
   1.304 +  // not go back into the VM to get their value; they must cache the
   1.305 +  // bit in the CI, either eagerly or lazily.)
   1.306 +  static bool is_concrete_klass(ciInstanceKlass* k); // k appears instantiable
   1.307 +  static bool is_concrete_method(ciMethod* m);       // m appears invocable
   1.308 +  static bool has_finalizable_subclass(ciInstanceKlass* k);
   1.309 +
   1.310 +  // As a general rule, it is OK to compile under the assumption that
   1.311 +  // a given type or method is concrete, even if it at some future
   1.312 +  // point becomes abstract.  So dependency checking is one-sided, in
   1.313 +  // that it permits supposedly concrete classes or methods to turn up
   1.314 +  // as really abstract.  (This shouldn't happen, except during class
   1.315 +  // evolution, but that's the logic of the checking.)  However, if a
   1.316 +  // supposedly abstract class or method suddenly becomes concrete, a
   1.317 +  // dependency on it must fail.
   1.318 +
   1.319 +  // Checking old assertions at run-time (in the VM only):
   1.320 +  static Klass* check_evol_method(Method* m);
   1.321 +  static Klass* check_leaf_type(Klass* ctxk);
   1.322 +  static Klass* check_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck,
   1.323 +                                                              KlassDepChange* changes = NULL);
   1.324 +  static Klass* check_abstract_with_no_concrete_subtype(Klass* ctxk,
   1.325 +                                                          KlassDepChange* changes = NULL);
   1.326 +  static Klass* check_concrete_with_no_concrete_subtype(Klass* ctxk,
   1.327 +                                                          KlassDepChange* changes = NULL);
   1.328 +  static Klass* check_unique_concrete_method(Klass* ctxk, Method* uniqm,
   1.329 +                                               KlassDepChange* changes = NULL);
   1.330 +  static Klass* check_abstract_with_exclusive_concrete_subtypes(Klass* ctxk, Klass* k1, Klass* k2,
   1.331 +                                                                  KlassDepChange* changes = NULL);
   1.332 +  static Klass* check_exclusive_concrete_methods(Klass* ctxk, Method* m1, Method* m2,
   1.333 +                                                   KlassDepChange* changes = NULL);
   1.334 +  static Klass* check_has_no_finalizable_subclasses(Klass* ctxk, KlassDepChange* changes = NULL);
   1.335 +  static Klass* check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes = NULL);
   1.336 +  // A returned Klass* is NULL if the dependency assertion is still
   1.337 +  // valid.  A non-NULL Klass* is a 'witness' to the assertion
   1.338 +  // failure, a point in the class hierarchy where the assertion has
   1.339 +  // been proven false.  For example, if check_leaf_type returns
   1.340 +  // non-NULL, the value is a subtype of the supposed leaf type.  This
   1.341 +  // witness value may be useful for logging the dependency failure.
   1.342 +  // Note that, when a dependency fails, there may be several possible
   1.343 +  // witnesses to the failure.  The value returned from the check_foo
   1.344 +  // method is chosen arbitrarily.
   1.345 +
   1.346 +  // The 'changes' value, if non-null, requests a limited spot-check
   1.347 +  // near the indicated recent changes in the class hierarchy.
   1.348 +  // It is used by DepStream::spot_check_dependency_at.
   1.349 +
   1.350 +  // Detecting possible new assertions:
   1.351 +  static Klass*    find_unique_concrete_subtype(Klass* ctxk);
   1.352 +  static Method*   find_unique_concrete_method(Klass* ctxk, Method* m);
   1.353 +  static int       find_exclusive_concrete_subtypes(Klass* ctxk, int klen, Klass* k[]);
   1.354 +  static int       find_exclusive_concrete_methods(Klass* ctxk, int mlen, Method* m[]);
   1.355 +
   1.356 +  // Create the encoding which will be stored in an nmethod.
   1.357 +  void encode_content_bytes();
   1.358 +
   1.359 +  address content_bytes() {
   1.360 +    assert(_content_bytes != NULL, "encode it first");
   1.361 +    return _content_bytes;
   1.362 +  }
   1.363 +  size_t size_in_bytes() {
   1.364 +    assert(_content_bytes != NULL, "encode it first");
   1.365 +    return _size_in_bytes;
   1.366 +  }
   1.367 +
   1.368 +  OopRecorder* oop_recorder() { return _oop_recorder; }
   1.369 +  CompileLog*  log()          { return _log; }
   1.370 +
   1.371 +  void copy_to(nmethod* nm);
   1.372 +
   1.373 +  void log_all_dependencies();
   1.374 +  void log_dependency(DepType dept, int nargs, ciBaseObject* args[]) {
   1.375 +    write_dependency_to(log(), dept, nargs, args);
   1.376 +  }
   1.377 +  void log_dependency(DepType dept,
   1.378 +                      ciBaseObject* x0,
   1.379 +                      ciBaseObject* x1 = NULL,
   1.380 +                      ciBaseObject* x2 = NULL) {
   1.381 +    if (log() == NULL)  return;
   1.382 +    ciBaseObject* args[max_arg_count];
   1.383 +    args[0] = x0;
   1.384 +    args[1] = x1;
   1.385 +    args[2] = x2;
   1.386 +    assert(2 < max_arg_count, "");
   1.387 +    log_dependency(dept, dep_args(dept), args);
   1.388 +  }
   1.389 +
   1.390 +  class DepArgument : public ResourceObj {
   1.391 +   private:
   1.392 +    bool  _is_oop;
   1.393 +    bool  _valid;
   1.394 +    void* _value;
   1.395 +   public:
   1.396 +    DepArgument() : _is_oop(false), _value(NULL), _valid(false) {}
   1.397 +    DepArgument(oop v): _is_oop(true), _value(v), _valid(true) {}
   1.398 +    DepArgument(Metadata* v): _is_oop(false), _value(v), _valid(true) {}
   1.399 +
   1.400 +    bool is_null() const               { return _value == NULL; }
   1.401 +    bool is_oop() const                { return _is_oop; }
   1.402 +    bool is_metadata() const           { return !_is_oop; }
   1.403 +    bool is_klass() const              { return is_metadata() && metadata_value()->is_klass(); }
   1.404 +    bool is_method() const              { return is_metadata() && metadata_value()->is_method(); }
   1.405 +
   1.406 +    oop oop_value() const              { assert(_is_oop && _valid, "must be"); return (oop) _value; }
   1.407 +    Metadata* metadata_value() const { assert(!_is_oop && _valid, "must be"); return (Metadata*) _value; }
   1.408 +  };
   1.409 +
   1.410 +  static void write_dependency_to(CompileLog* log,
   1.411 +                                  DepType dept,
   1.412 +                                  int nargs, ciBaseObject* args[],
   1.413 +                                  Klass* witness = NULL);
   1.414 +  static void write_dependency_to(CompileLog* log,
   1.415 +                                  DepType dept,
   1.416 +                                  int nargs, DepArgument args[],
   1.417 +                                  Klass* witness = NULL);
   1.418 +  static void write_dependency_to(xmlStream* xtty,
   1.419 +                                  DepType dept,
   1.420 +                                  int nargs, DepArgument args[],
   1.421 +                                  Klass* witness = NULL);
   1.422 +  static void print_dependency(DepType dept,
   1.423 +                               int nargs, DepArgument args[],
   1.424 +                               Klass* witness = NULL);
   1.425 +
   1.426 + private:
   1.427 +  // helper for encoding common context types as zero:
   1.428 +  static ciKlass* ctxk_encoded_as_null(DepType dept, ciBaseObject* x);
   1.429 +
   1.430 +  static Klass* ctxk_encoded_as_null(DepType dept, Metadata* x);
   1.431 +
   1.432 + public:
   1.433 +  // Use this to iterate over an nmethod's dependency set.
   1.434 +  // Works on new and old dependency sets.
   1.435 +  // Usage:
   1.436 +  //
   1.437 +  // ;
   1.438 +  // Dependencies::DepType dept;
   1.439 +  // for (Dependencies::DepStream deps(nm); deps.next(); ) {
   1.440 +  //   ...
   1.441 +  // }
   1.442 +  //
   1.443 +  // The caller must be in the VM, since oops are not wrapped in handles.
   1.444 +  class DepStream {
   1.445 +  private:
   1.446 +    nmethod*              _code;   // null if in a compiler thread
   1.447 +    Dependencies*         _deps;   // null if not in a compiler thread
   1.448 +    CompressedReadStream  _bytes;
   1.449 +#ifdef ASSERT
   1.450 +    size_t                _byte_limit;
   1.451 +#endif
   1.452 +
   1.453 +    // iteration variables:
   1.454 +    DepType               _type;
   1.455 +    int                   _xi[max_arg_count+1];
   1.456 +
   1.457 +    void initial_asserts(size_t byte_limit) NOT_DEBUG({});
   1.458 +
   1.459 +    inline Metadata* recorded_metadata_at(int i);
   1.460 +    inline oop recorded_oop_at(int i);
   1.461 +
   1.462 +    Klass* check_klass_dependency(KlassDepChange* changes);
   1.463 +    Klass* check_call_site_dependency(CallSiteDepChange* changes);
   1.464 +
   1.465 +    void trace_and_log_witness(Klass* witness);
   1.466 +
   1.467 +  public:
   1.468 +    DepStream(Dependencies* deps)
   1.469 +      : _deps(deps),
   1.470 +        _code(NULL),
   1.471 +        _bytes(deps->content_bytes())
   1.472 +    {
   1.473 +      initial_asserts(deps->size_in_bytes());
   1.474 +    }
   1.475 +    DepStream(nmethod* code)
   1.476 +      : _deps(NULL),
   1.477 +        _code(code),
   1.478 +        _bytes(code->dependencies_begin())
   1.479 +    {
   1.480 +      initial_asserts(code->dependencies_size());
   1.481 +    }
   1.482 +
   1.483 +    bool next();
   1.484 +
   1.485 +    DepType type()               { return _type; }
   1.486 +    int argument_count()         { return dep_args(type()); }
   1.487 +    int argument_index(int i)    { assert(0 <= i && i < argument_count(), "oob");
   1.488 +                                   return _xi[i]; }
   1.489 +    Metadata* argument(int i);     // => recorded_oop_at(argument_index(i))
   1.490 +    oop argument_oop(int i);         // => recorded_oop_at(argument_index(i))
   1.491 +    Klass* context_type();
   1.492 +
   1.493 +    bool is_klass_type()         { return Dependencies::is_klass_type(type()); }
   1.494 +
   1.495 +    Method* method_argument(int i) {
   1.496 +      Metadata* x = argument(i);
   1.497 +      assert(x->is_method(), "type");
   1.498 +      return (Method*) x;
   1.499 +    }
   1.500 +    Klass* type_argument(int i) {
   1.501 +      Metadata* x = argument(i);
   1.502 +      assert(x->is_klass(), "type");
   1.503 +      return (Klass*) x;
   1.504 +    }
   1.505 +
   1.506 +    // The point of the whole exercise:  Is this dep still OK?
   1.507 +    Klass* check_dependency() {
   1.508 +      Klass* result = check_klass_dependency(NULL);
   1.509 +      if (result != NULL)  return result;
   1.510 +      return check_call_site_dependency(NULL);
   1.511 +    }
   1.512 +
   1.513 +    // A lighter version:  Checks only around recent changes in a class
   1.514 +    // hierarchy.  (See Universe::flush_dependents_on.)
   1.515 +    Klass* spot_check_dependency_at(DepChange& changes);
   1.516 +
   1.517 +    // Log the current dependency to xtty or compilation log.
   1.518 +    void log_dependency(Klass* witness = NULL);
   1.519 +
   1.520 +    // Print the current dependency to tty.
   1.521 +    void print_dependency(Klass* witness = NULL, bool verbose = false);
   1.522 +  };
   1.523 +  friend class Dependencies::DepStream;
   1.524 +
   1.525 +  static void print_statistics() PRODUCT_RETURN;
   1.526 +};
   1.527 +
   1.528 +
   1.529 +// Every particular DepChange is a sub-class of this class.
   1.530 +class DepChange : public StackObj {
   1.531 + public:
   1.532 +  // What kind of DepChange is this?
   1.533 +  virtual bool is_klass_change()     const { return false; }
   1.534 +  virtual bool is_call_site_change() const { return false; }
   1.535 +
   1.536 +  // Subclass casting with assertions.
   1.537 +  KlassDepChange*    as_klass_change() {
   1.538 +    assert(is_klass_change(), "bad cast");
   1.539 +    return (KlassDepChange*) this;
   1.540 +  }
   1.541 +  CallSiteDepChange* as_call_site_change() {
   1.542 +    assert(is_call_site_change(), "bad cast");
   1.543 +    return (CallSiteDepChange*) this;
   1.544 +  }
   1.545 +
   1.546 +  void print();
   1.547 +
   1.548 + public:
   1.549 +  enum ChangeType {
   1.550 +    NO_CHANGE = 0,              // an uninvolved klass
   1.551 +    Change_new_type,            // a newly loaded type
   1.552 +    Change_new_sub,             // a super with a new subtype
   1.553 +    Change_new_impl,            // an interface with a new implementation
   1.554 +    CHANGE_LIMIT,
   1.555 +    Start_Klass = CHANGE_LIMIT  // internal indicator for ContextStream
   1.556 +  };
   1.557 +
   1.558 +  // Usage:
   1.559 +  // for (DepChange::ContextStream str(changes); str.next(); ) {
   1.560 +  //   Klass* k = str.klass();
   1.561 +  //   switch (str.change_type()) {
   1.562 +  //     ...
   1.563 +  //   }
   1.564 +  // }
   1.565 +  class ContextStream : public StackObj {
   1.566 +   private:
   1.567 +    DepChange&  _changes;
   1.568 +    friend class DepChange;
   1.569 +
   1.570 +    // iteration variables:
   1.571 +    ChangeType  _change_type;
   1.572 +    Klass*      _klass;
   1.573 +    Array<Klass*>* _ti_base;    // i.e., transitive_interfaces
   1.574 +    int         _ti_index;
   1.575 +    int         _ti_limit;
   1.576 +
   1.577 +    // start at the beginning:
   1.578 +    void start();
   1.579 +
   1.580 +   public:
   1.581 +    ContextStream(DepChange& changes)
   1.582 +      : _changes(changes)
   1.583 +    { start(); }
   1.584 +
   1.585 +    ContextStream(DepChange& changes, No_Safepoint_Verifier& nsv)
   1.586 +      : _changes(changes)
   1.587 +      // the nsv argument makes it safe to hold oops like _klass
   1.588 +    { start(); }
   1.589 +
   1.590 +    bool next();
   1.591 +
   1.592 +    ChangeType change_type()     { return _change_type; }
   1.593 +    Klass*     klass()           { return _klass; }
   1.594 +  };
   1.595 +  friend class DepChange::ContextStream;
   1.596 +};
   1.597 +
   1.598 +
   1.599 +// A class hierarchy change coming through the VM (under the Compile_lock).
   1.600 +// The change is structured as a single new type with any number of supers
   1.601 +// and implemented interface types.  Other than the new type, any of the
   1.602 +// super types can be context types for a relevant dependency, which the
   1.603 +// new type could invalidate.
   1.604 +class KlassDepChange : public DepChange {
   1.605 + private:
   1.606 +  // each change set is rooted in exactly one new type (at present):
   1.607 +  KlassHandle _new_type;
   1.608 +
   1.609 +  void initialize();
   1.610 +
   1.611 + public:
   1.612 +  // notes the new type, marks it and all its super-types
   1.613 +  KlassDepChange(KlassHandle new_type)
   1.614 +    : _new_type(new_type)
   1.615 +  {
   1.616 +    initialize();
   1.617 +  }
   1.618 +
   1.619 +  // cleans up the marks
   1.620 +  ~KlassDepChange();
   1.621 +
   1.622 +  // What kind of DepChange is this?
   1.623 +  virtual bool is_klass_change() const { return true; }
   1.624 +
   1.625 +  Klass* new_type() { return _new_type(); }
   1.626 +
   1.627 +  // involves_context(k) is true if k is new_type or any of the super types
   1.628 +  bool involves_context(Klass* k);
   1.629 +};
   1.630 +
   1.631 +
   1.632 +// A CallSite has changed its target.
   1.633 +class CallSiteDepChange : public DepChange {
   1.634 + private:
   1.635 +  Handle _call_site;
   1.636 +  Handle _method_handle;
   1.637 +
   1.638 + public:
   1.639 +  CallSiteDepChange(Handle call_site, Handle method_handle)
   1.640 +    : _call_site(call_site),
   1.641 +      _method_handle(method_handle)
   1.642 +  {
   1.643 +    assert(_call_site()    ->is_a(SystemDictionary::CallSite_klass()),     "must be");
   1.644 +    assert(_method_handle()->is_a(SystemDictionary::MethodHandle_klass()), "must be");
   1.645 +  }
   1.646 +
   1.647 +  // What kind of DepChange is this?
   1.648 +  virtual bool is_call_site_change() const { return true; }
   1.649 +
   1.650 +  oop call_site()     const { return _call_site();     }
   1.651 +  oop method_handle() const { return _method_handle(); }
   1.652 +};
   1.653 +
   1.654 +#endif // SHARE_VM_CODE_DEPENDENCIES_HPP

mercurial