src/share/vm/code/dependencies.cpp

Tue, 16 Aug 2011 04:14:05 -0700

author
twisti
date
Tue, 16 Aug 2011 04:14:05 -0700
changeset 3050
fdb992d83a87
parent 2708
1d1603768966
child 3094
b27c72d69fd1
permissions
-rw-r--r--

7071653: JSR 292: call site change notification should be pushed not pulled
Reviewed-by: kvn, never, bdelsart

     1 /*
     2  * Copyright (c) 2005, 2011, 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  */
    25 #include "precompiled.hpp"
    26 #include "ci/ciArrayKlass.hpp"
    27 #include "ci/ciEnv.hpp"
    28 #include "ci/ciKlass.hpp"
    29 #include "ci/ciMethod.hpp"
    30 #include "code/dependencies.hpp"
    31 #include "compiler/compileLog.hpp"
    32 #include "oops/oop.inline.hpp"
    33 #include "runtime/handles.inline.hpp"
    34 #include "utilities/copy.hpp"
    37 #ifdef ASSERT
    38 static bool must_be_in_vm() {
    39   Thread* thread = Thread::current();
    40   if (thread->is_Java_thread())
    41     return ((JavaThread*)thread)->thread_state() == _thread_in_vm;
    42   else
    43     return true;  //something like this: thread->is_VM_thread();
    44 }
    45 #endif //ASSERT
    47 void Dependencies::initialize(ciEnv* env) {
    48   Arena* arena = env->arena();
    49   _oop_recorder = env->oop_recorder();
    50   _log = env->log();
    51   _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);
    52   DEBUG_ONLY(_deps[end_marker] = NULL);
    53   for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
    54     _deps[i] = new(arena) GrowableArray<ciObject*>(arena, 10, 0, 0);
    55   }
    56   _content_bytes = NULL;
    57   _size_in_bytes = (size_t)-1;
    59   assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
    60 }
    62 void Dependencies::assert_evol_method(ciMethod* m) {
    63   assert_common_1(evol_method, m);
    64 }
    66 void Dependencies::assert_leaf_type(ciKlass* ctxk) {
    67   if (ctxk->is_array_klass()) {
    68     // As a special case, support this assertion on an array type,
    69     // which reduces to an assertion on its element type.
    70     // Note that this cannot be done with assertions that
    71     // relate to concreteness or abstractness.
    72     ciType* elemt = ctxk->as_array_klass()->base_element_type();
    73     if (!elemt->is_instance_klass())  return;   // Ex:  int[][]
    74     ctxk = elemt->as_instance_klass();
    75     //if (ctxk->is_final())  return;            // Ex:  String[][]
    76   }
    77   check_ctxk(ctxk);
    78   assert_common_1(leaf_type, ctxk);
    79 }
    81 void Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) {
    82   check_ctxk_abstract(ctxk);
    83   assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck);
    84 }
    86 void Dependencies::assert_abstract_with_no_concrete_subtype(ciKlass* ctxk) {
    87   check_ctxk_abstract(ctxk);
    88   assert_common_1(abstract_with_no_concrete_subtype, ctxk);
    89 }
    91 void Dependencies::assert_concrete_with_no_concrete_subtype(ciKlass* ctxk) {
    92   check_ctxk_concrete(ctxk);
    93   assert_common_1(concrete_with_no_concrete_subtype, ctxk);
    94 }
    96 void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) {
    97   check_ctxk(ctxk);
    98   assert_common_2(unique_concrete_method, ctxk, uniqm);
    99 }
   101 void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {
   102   check_ctxk(ctxk);
   103   assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);
   104 }
   106 void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {
   107   check_ctxk(ctxk);
   108   assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);
   109 }
   111 void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {
   112   check_ctxk(ctxk);
   113   assert_common_1(no_finalizable_subclasses, ctxk);
   114 }
   116 void Dependencies::assert_call_site_target_value(ciKlass* ctxk, ciCallSite* call_site, ciMethodHandle* method_handle) {
   117   check_ctxk(ctxk);
   118   assert_common_3(call_site_target_value, ctxk, call_site, method_handle);
   119 }
   121 // Helper function.  If we are adding a new dep. under ctxk2,
   122 // try to find an old dep. under a broader* ctxk1.  If there is
   123 //
   124 bool Dependencies::maybe_merge_ctxk(GrowableArray<ciObject*>* deps,
   125                                     int ctxk_i, ciKlass* ctxk2) {
   126   ciKlass* ctxk1 = deps->at(ctxk_i)->as_klass();
   127   if (ctxk2->is_subtype_of(ctxk1)) {
   128     return true;  // success, and no need to change
   129   } else if (ctxk1->is_subtype_of(ctxk2)) {
   130     // new context class fully subsumes previous one
   131     deps->at_put(ctxk_i, ctxk2);
   132     return true;
   133   } else {
   134     return false;
   135   }
   136 }
   138 void Dependencies::assert_common_1(Dependencies::DepType dept, ciObject* x) {
   139   assert(dep_args(dept) == 1, "sanity");
   140   log_dependency(dept, x);
   141   GrowableArray<ciObject*>* deps = _deps[dept];
   143   // see if the same (or a similar) dep is already recorded
   144   if (note_dep_seen(dept, x)) {
   145     assert(deps->find(x) >= 0, "sanity");
   146   } else {
   147     deps->append(x);
   148   }
   149 }
   151 void Dependencies::assert_common_2(Dependencies::DepType dept,
   152                                    ciKlass* ctxk, ciObject* x) {
   153   assert(dep_context_arg(dept) == 0, "sanity");
   154   assert(dep_args(dept) == 2, "sanity");
   155   log_dependency(dept, ctxk, x);
   156   GrowableArray<ciObject*>* deps = _deps[dept];
   158   // see if the same (or a similar) dep is already recorded
   159   if (note_dep_seen(dept, x)) {
   160     // look in this bucket for redundant assertions
   161     const int stride = 2;
   162     for (int i = deps->length(); (i -= stride) >= 0; ) {
   163       ciObject* x1 = deps->at(i+1);
   164       if (x == x1) {  // same subject; check the context
   165         if (maybe_merge_ctxk(deps, i+0, ctxk)) {
   166           return;
   167         }
   168       }
   169     }
   170   }
   172   // append the assertion in the correct bucket:
   173   deps->append(ctxk);
   174   deps->append(x);
   175 }
   177 void Dependencies::assert_common_3(Dependencies::DepType dept,
   178                                    ciKlass* ctxk, ciObject* x, ciObject* x2) {
   179   assert(dep_context_arg(dept) == 0, "sanity");
   180   assert(dep_args(dept) == 3, "sanity");
   181   log_dependency(dept, ctxk, x, x2);
   182   GrowableArray<ciObject*>* deps = _deps[dept];
   184   // try to normalize an unordered pair:
   185   bool swap = false;
   186   switch (dept) {
   187   case abstract_with_exclusive_concrete_subtypes_2:
   188     swap = (x->ident() > x2->ident() && x != ctxk);
   189     break;
   190   case exclusive_concrete_methods_2:
   191     swap = (x->ident() > x2->ident() && x->as_method()->holder() != ctxk);
   192     break;
   193   }
   194   if (swap) { ciObject* t = x; x = x2; x2 = t; }
   196   // see if the same (or a similar) dep is already recorded
   197   if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) {
   198     // look in this bucket for redundant assertions
   199     const int stride = 3;
   200     for (int i = deps->length(); (i -= stride) >= 0; ) {
   201       ciObject* y  = deps->at(i+1);
   202       ciObject* y2 = deps->at(i+2);
   203       if (x == y && x2 == y2) {  // same subjects; check the context
   204         if (maybe_merge_ctxk(deps, i+0, ctxk)) {
   205           return;
   206         }
   207       }
   208     }
   209   }
   210   // append the assertion in the correct bucket:
   211   deps->append(ctxk);
   212   deps->append(x);
   213   deps->append(x2);
   214 }
   216 /// Support for encoding dependencies into an nmethod:
   218 void Dependencies::copy_to(nmethod* nm) {
   219   address beg = nm->dependencies_begin();
   220   address end = nm->dependencies_end();
   221   guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");
   222   Copy::disjoint_words((HeapWord*) content_bytes(),
   223                        (HeapWord*) beg,
   224                        size_in_bytes() / sizeof(HeapWord));
   225   assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");
   226 }
   228 static int sort_dep(ciObject** p1, ciObject** p2, int narg) {
   229   for (int i = 0; i < narg; i++) {
   230     int diff = p1[i]->ident() - p2[i]->ident();
   231     if (diff != 0)  return diff;
   232   }
   233   return 0;
   234 }
   235 static int sort_dep_arg_1(ciObject** p1, ciObject** p2)
   236 { return sort_dep(p1, p2, 1); }
   237 static int sort_dep_arg_2(ciObject** p1, ciObject** p2)
   238 { return sort_dep(p1, p2, 2); }
   239 static int sort_dep_arg_3(ciObject** p1, ciObject** p2)
   240 { return sort_dep(p1, p2, 3); }
   242 void Dependencies::sort_all_deps() {
   243   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
   244     DepType dept = (DepType)deptv;
   245     GrowableArray<ciObject*>* deps = _deps[dept];
   246     if (deps->length() <= 1)  continue;
   247     switch (dep_args(dept)) {
   248     case 1: deps->sort(sort_dep_arg_1, 1); break;
   249     case 2: deps->sort(sort_dep_arg_2, 2); break;
   250     case 3: deps->sort(sort_dep_arg_3, 3); break;
   251     default: ShouldNotReachHere();
   252     }
   253   }
   254 }
   256 size_t Dependencies::estimate_size_in_bytes() {
   257   size_t est_size = 100;
   258   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
   259     DepType dept = (DepType)deptv;
   260     GrowableArray<ciObject*>* deps = _deps[dept];
   261     est_size += deps->length()*2;  // tags and argument(s)
   262   }
   263   return est_size;
   264 }
   266 ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciObject* x) {
   267   switch (dept) {
   268   case abstract_with_exclusive_concrete_subtypes_2:
   269     return x->as_klass();
   270   case unique_concrete_method:
   271   case exclusive_concrete_methods_2:
   272     return x->as_method()->holder();
   273   }
   274   return NULL;  // let NULL be NULL
   275 }
   277 klassOop Dependencies::ctxk_encoded_as_null(DepType dept, oop x) {
   278   assert(must_be_in_vm(), "raw oops here");
   279   switch (dept) {
   280   case abstract_with_exclusive_concrete_subtypes_2:
   281     assert(x->is_klass(), "sanity");
   282     return (klassOop) x;
   283   case unique_concrete_method:
   284   case exclusive_concrete_methods_2:
   285     assert(x->is_method(), "sanity");
   286     return ((methodOop)x)->method_holder();
   287   }
   288   return NULL;  // let NULL be NULL
   289 }
   291 void Dependencies::encode_content_bytes() {
   292   sort_all_deps();
   294   // cast is safe, no deps can overflow INT_MAX
   295   CompressedWriteStream bytes((int)estimate_size_in_bytes());
   297   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
   298     DepType dept = (DepType)deptv;
   299     GrowableArray<ciObject*>* deps = _deps[dept];
   300     if (deps->length() == 0)  continue;
   301     int stride = dep_args(dept);
   302     int ctxkj  = dep_context_arg(dept);  // -1 if no context arg
   303     assert(stride > 0, "sanity");
   304     for (int i = 0; i < deps->length(); i += stride) {
   305       jbyte code_byte = (jbyte)dept;
   306       int skipj = -1;
   307       if (ctxkj >= 0 && ctxkj+1 < stride) {
   308         ciKlass*  ctxk = deps->at(i+ctxkj+0)->as_klass();
   309         ciObject* x    = deps->at(i+ctxkj+1);  // following argument
   310         if (ctxk == ctxk_encoded_as_null(dept, x)) {
   311           skipj = ctxkj;  // we win:  maybe one less oop to keep track of
   312           code_byte |= default_context_type_bit;
   313         }
   314       }
   315       bytes.write_byte(code_byte);
   316       for (int j = 0; j < stride; j++) {
   317         if (j == skipj)  continue;
   318         bytes.write_int(_oop_recorder->find_index(deps->at(i+j)->constant_encoding()));
   319       }
   320     }
   321   }
   323   // write a sentinel byte to mark the end
   324   bytes.write_byte(end_marker);
   326   // round it out to a word boundary
   327   while (bytes.position() % sizeof(HeapWord) != 0) {
   328     bytes.write_byte(end_marker);
   329   }
   331   // check whether the dept byte encoding really works
   332   assert((jbyte)default_context_type_bit != 0, "byte overflow");
   334   _content_bytes = bytes.buffer();
   335   _size_in_bytes = bytes.position();
   336 }
   339 const char* Dependencies::_dep_name[TYPE_LIMIT] = {
   340   "end_marker",
   341   "evol_method",
   342   "leaf_type",
   343   "abstract_with_unique_concrete_subtype",
   344   "abstract_with_no_concrete_subtype",
   345   "concrete_with_no_concrete_subtype",
   346   "unique_concrete_method",
   347   "abstract_with_exclusive_concrete_subtypes_2",
   348   "exclusive_concrete_methods_2",
   349   "no_finalizable_subclasses",
   350   "call_site_target_value"
   351 };
   353 int Dependencies::_dep_args[TYPE_LIMIT] = {
   354   -1,// end_marker
   355   1, // evol_method m
   356   1, // leaf_type ctxk
   357   2, // abstract_with_unique_concrete_subtype ctxk, k
   358   1, // abstract_with_no_concrete_subtype ctxk
   359   1, // concrete_with_no_concrete_subtype ctxk
   360   2, // unique_concrete_method ctxk, m
   361   3, // unique_concrete_subtypes_2 ctxk, k1, k2
   362   3, // unique_concrete_methods_2 ctxk, m1, m2
   363   1, // no_finalizable_subclasses ctxk
   364   3  // call_site_target_value ctxk, call_site, method_handle
   365 };
   367 const char* Dependencies::dep_name(Dependencies::DepType dept) {
   368   if (!dept_in_mask(dept, all_types))  return "?bad-dep?";
   369   return _dep_name[dept];
   370 }
   372 int Dependencies::dep_args(Dependencies::DepType dept) {
   373   if (!dept_in_mask(dept, all_types))  return -1;
   374   return _dep_args[dept];
   375 }
   377 void Dependencies::check_valid_dependency_type(DepType dept) {
   378   for (int deptv = (int) FIRST_TYPE; deptv < (int) TYPE_LIMIT; deptv++) {
   379     if (dept == ((DepType) deptv))  return;
   380   }
   381   ShouldNotReachHere();
   382 }
   384 // for the sake of the compiler log, print out current dependencies:
   385 void Dependencies::log_all_dependencies() {
   386   if (log() == NULL)  return;
   387   ciObject* args[max_arg_count];
   388   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
   389     DepType dept = (DepType)deptv;
   390     GrowableArray<ciObject*>* deps = _deps[dept];
   391     if (deps->length() == 0)  continue;
   392     int stride = dep_args(dept);
   393     for (int i = 0; i < deps->length(); i += stride) {
   394       for (int j = 0; j < stride; j++) {
   395         // flush out the identities before printing
   396         args[j] = deps->at(i+j);
   397       }
   398       write_dependency_to(log(), dept, stride, args);
   399     }
   400   }
   401 }
   403 void Dependencies::write_dependency_to(CompileLog* log,
   404                                        DepType dept,
   405                                        int nargs, oop args[],
   406                                        klassOop witness) {
   407   if (log == NULL) {
   408     return;
   409   }
   410   ciEnv* env = ciEnv::current();
   411   ciObject* ciargs[max_arg_count];
   412   assert(nargs <= max_arg_count, "oob");
   413   for (int j = 0; j < nargs; j++) {
   414     ciargs[j] = env->get_object(args[j]);
   415   }
   416   Dependencies::write_dependency_to(log, dept, nargs, ciargs, witness);
   417 }
   419 void Dependencies::write_dependency_to(CompileLog* log,
   420                                        DepType dept,
   421                                        int nargs, ciObject* args[],
   422                                        klassOop witness) {
   423   if (log == NULL)  return;
   424   assert(nargs <= max_arg_count, "oob");
   425   int argids[max_arg_count];
   426   int ctxkj = dep_context_arg(dept);  // -1 if no context arg
   427   int j;
   428   for (j = 0; j < nargs; j++) {
   429     argids[j] = log->identify(args[j]);
   430   }
   431   if (witness != NULL) {
   432     log->begin_elem("dependency_failed");
   433   } else {
   434     log->begin_elem("dependency");
   435   }
   436   log->print(" type='%s'", dep_name(dept));
   437   if (ctxkj >= 0) {
   438     log->print(" ctxk='%d'", argids[ctxkj]);
   439   }
   440   // write remaining arguments, if any.
   441   for (j = 0; j < nargs; j++) {
   442     if (j == ctxkj)  continue;  // already logged
   443     if (j == 1) {
   444       log->print(  " x='%d'",    argids[j]);
   445     } else {
   446       log->print(" x%d='%d'", j, argids[j]);
   447     }
   448   }
   449   if (witness != NULL) {
   450     log->object("witness", witness);
   451     log->stamp();
   452   }
   453   log->end_elem();
   454 }
   456 void Dependencies::write_dependency_to(xmlStream* xtty,
   457                                        DepType dept,
   458                                        int nargs, oop args[],
   459                                        klassOop witness) {
   460   if (xtty == NULL)  return;
   461   ttyLocker ttyl;
   462   int ctxkj = dep_context_arg(dept);  // -1 if no context arg
   463   if (witness != NULL) {
   464     xtty->begin_elem("dependency_failed");
   465   } else {
   466     xtty->begin_elem("dependency");
   467   }
   468   xtty->print(" type='%s'", dep_name(dept));
   469   if (ctxkj >= 0) {
   470     xtty->object("ctxk", args[ctxkj]);
   471   }
   472   // write remaining arguments, if any.
   473   for (int j = 0; j < nargs; j++) {
   474     if (j == ctxkj)  continue;  // already logged
   475     if (j == 1) {
   476       xtty->object("x", args[j]);
   477     } else {
   478       char xn[10]; sprintf(xn, "x%d", j);
   479       xtty->object(xn, args[j]);
   480     }
   481   }
   482   if (witness != NULL) {
   483     xtty->object("witness", witness);
   484     xtty->stamp();
   485   }
   486   xtty->end_elem();
   487 }
   489 void Dependencies::print_dependency(DepType dept, int nargs, oop args[],
   490                                     klassOop witness) {
   491   ResourceMark rm;
   492   ttyLocker ttyl;   // keep the following output all in one block
   493   tty->print_cr("%s of type %s",
   494                 (witness == NULL)? "Dependency": "Failed dependency",
   495                 dep_name(dept));
   496   // print arguments
   497   int ctxkj = dep_context_arg(dept);  // -1 if no context arg
   498   for (int j = 0; j < nargs; j++) {
   499     oop arg = args[j];
   500     bool put_star = false;
   501     if (arg == NULL)  continue;
   502     const char* what;
   503     if (j == ctxkj) {
   504       what = "context";
   505       put_star = !Dependencies::is_concrete_klass((klassOop)arg);
   506     } else if (arg->is_method()) {
   507       what = "method ";
   508       put_star = !Dependencies::is_concrete_method((methodOop)arg);
   509     } else if (arg->is_klass()) {
   510       what = "class  ";
   511     } else {
   512       what = "object ";
   513     }
   514     tty->print("  %s = %s", what, (put_star? "*": ""));
   515     if (arg->is_klass())
   516       tty->print("%s", Klass::cast((klassOop)arg)->external_name());
   517     else
   518       arg->print_value();
   519     tty->cr();
   520   }
   521   if (witness != NULL) {
   522     bool put_star = !Dependencies::is_concrete_klass(witness);
   523     tty->print_cr("  witness = %s%s",
   524                   (put_star? "*": ""),
   525                   Klass::cast(witness)->external_name());
   526   }
   527 }
   529 void Dependencies::DepStream::log_dependency(klassOop witness) {
   530   if (_deps == NULL && xtty == NULL)  return;  // fast cutout for runtime
   531   int nargs = argument_count();
   532   oop args[max_arg_count];
   533   for (int j = 0; j < nargs; j++) {
   534     args[j] = argument(j);
   535   }
   536   if (_deps != NULL && _deps->log() != NULL) {
   537     Dependencies::write_dependency_to(_deps->log(),
   538                                       type(), nargs, args, witness);
   539   } else {
   540     Dependencies::write_dependency_to(xtty,
   541                                       type(), nargs, args, witness);
   542   }
   543 }
   545 void Dependencies::DepStream::print_dependency(klassOop witness, bool verbose) {
   546   int nargs = argument_count();
   547   oop args[max_arg_count];
   548   for (int j = 0; j < nargs; j++) {
   549     args[j] = argument(j);
   550   }
   551   Dependencies::print_dependency(type(), nargs, args, witness);
   552   if (verbose) {
   553     if (_code != NULL) {
   554       tty->print("  code: ");
   555       _code->print_value_on(tty);
   556       tty->cr();
   557     }
   558   }
   559 }
   562 /// Dependency stream support (decodes dependencies from an nmethod):
   564 #ifdef ASSERT
   565 void Dependencies::DepStream::initial_asserts(size_t byte_limit) {
   566   assert(must_be_in_vm(), "raw oops here");
   567   _byte_limit = byte_limit;
   568   _type       = (DepType)(end_marker-1);  // defeat "already at end" assert
   569   assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");
   570 }
   571 #endif //ASSERT
   573 bool Dependencies::DepStream::next() {
   574   assert(_type != end_marker, "already at end");
   575   if (_bytes.position() == 0 && _code != NULL
   576       && _code->dependencies_size() == 0) {
   577     // Method has no dependencies at all.
   578     return false;
   579   }
   580   int code_byte = (_bytes.read_byte() & 0xFF);
   581   if (code_byte == end_marker) {
   582     DEBUG_ONLY(_type = end_marker);
   583     return false;
   584   } else {
   585     int ctxk_bit = (code_byte & Dependencies::default_context_type_bit);
   586     code_byte -= ctxk_bit;
   587     DepType dept = (DepType)code_byte;
   588     _type = dept;
   589     guarantee((dept - FIRST_TYPE) < (TYPE_LIMIT - FIRST_TYPE),
   590               "bad dependency type tag");
   591     int stride = _dep_args[dept];
   592     assert(stride == dep_args(dept), "sanity");
   593     int skipj = -1;
   594     if (ctxk_bit != 0) {
   595       skipj = 0;  // currently the only context argument is at zero
   596       assert(skipj == dep_context_arg(dept), "zero arg always ctxk");
   597     }
   598     for (int j = 0; j < stride; j++) {
   599       _xi[j] = (j == skipj)? 0: _bytes.read_int();
   600     }
   601     DEBUG_ONLY(_xi[stride] = -1);   // help detect overruns
   602     return true;
   603   }
   604 }
   606 inline oop Dependencies::DepStream::recorded_oop_at(int i) {
   607   return (_code != NULL)
   608          ? _code->oop_at(i)
   609          : JNIHandles::resolve(_deps->oop_recorder()->handle_at(i));
   610 }
   612 oop Dependencies::DepStream::argument(int i) {
   613   return recorded_oop_at(argument_index(i));
   614 }
   616 klassOop Dependencies::DepStream::context_type() {
   617   assert(must_be_in_vm(), "raw oops here");
   618   int ctxkj = dep_context_arg(_type);  // -1 if no context arg
   619   if (ctxkj < 0) {
   620     return NULL;           // for example, evol_method
   621   } else {
   622     oop k = recorded_oop_at(_xi[ctxkj]);
   623     if (k != NULL) {       // context type was not compressed away
   624       assert(k->is_klass(), "type check");
   625       return (klassOop) k;
   626     } else {               // recompute "default" context type
   627       return ctxk_encoded_as_null(_type, recorded_oop_at(_xi[ctxkj+1]));
   628     }
   629   }
   630 }
   632 /// Checking dependencies:
   634 // This hierarchy walker inspects subtypes of a given type,
   635 // trying to find a "bad" class which breaks a dependency.
   636 // Such a class is called a "witness" to the broken dependency.
   637 // While searching around, we ignore "participants", which
   638 // are already known to the dependency.
   639 class ClassHierarchyWalker {
   640  public:
   641   enum { PARTICIPANT_LIMIT = 3 };
   643  private:
   644   // optional method descriptor to check for:
   645   Symbol* _name;
   646   Symbol* _signature;
   648   // special classes which are not allowed to be witnesses:
   649   klassOop  _participants[PARTICIPANT_LIMIT+1];
   650   int       _num_participants;
   652   // cache of method lookups
   653   methodOop _found_methods[PARTICIPANT_LIMIT+1];
   655   // if non-zero, tells how many witnesses to convert to participants
   656   int       _record_witnesses;
   658   void initialize(klassOop participant) {
   659     _record_witnesses = 0;
   660     _participants[0]  = participant;
   661     _found_methods[0] = NULL;
   662     _num_participants = 0;
   663     if (participant != NULL) {
   664       // Terminating NULL.
   665       _participants[1] = NULL;
   666       _found_methods[1] = NULL;
   667       _num_participants = 1;
   668     }
   669   }
   671   void initialize_from_method(methodOop m) {
   672     assert(m != NULL && m->is_method(), "sanity");
   673     _name      = m->name();
   674     _signature = m->signature();
   675   }
   677  public:
   678   // The walker is initialized to recognize certain methods and/or types
   679   // as friendly participants.
   680   ClassHierarchyWalker(klassOop participant, methodOop m) {
   681     initialize_from_method(m);
   682     initialize(participant);
   683   }
   684   ClassHierarchyWalker(methodOop m) {
   685     initialize_from_method(m);
   686     initialize(NULL);
   687   }
   688   ClassHierarchyWalker(klassOop participant = NULL) {
   689     _name      = NULL;
   690     _signature = NULL;
   691     initialize(participant);
   692   }
   694   // This is common code for two searches:  One for concrete subtypes,
   695   // the other for concrete method implementations and overrides.
   696   bool doing_subtype_search() {
   697     return _name == NULL;
   698   }
   700   int num_participants() { return _num_participants; }
   701   klassOop participant(int n) {
   702     assert((uint)n <= (uint)_num_participants, "oob");
   703     return _participants[n];
   704   }
   706   // Note:  If n==num_participants, returns NULL.
   707   methodOop found_method(int n) {
   708     assert((uint)n <= (uint)_num_participants, "oob");
   709     methodOop fm = _found_methods[n];
   710     assert(n == _num_participants || fm != NULL, "proper usage");
   711     assert(fm == NULL || fm->method_holder() == _participants[n], "sanity");
   712     return fm;
   713   }
   715 #ifdef ASSERT
   716   // Assert that m is inherited into ctxk, without intervening overrides.
   717   // (May return true even if this is not true, in corner cases where we punt.)
   718   bool check_method_context(klassOop ctxk, methodOop m) {
   719     if (m->method_holder() == ctxk)
   720       return true;  // Quick win.
   721     if (m->is_private())
   722       return false; // Quick lose.  Should not happen.
   723     if (!(m->is_public() || m->is_protected()))
   724       // The override story is complex when packages get involved.
   725       return true;  // Must punt the assertion to true.
   726     Klass* k = Klass::cast(ctxk);
   727     methodOop lm = k->lookup_method(m->name(), m->signature());
   728     if (lm == NULL && k->oop_is_instance()) {
   729       // It might be an abstract interface method, devoid of mirandas.
   730       lm = ((instanceKlass*)k)->lookup_method_in_all_interfaces(m->name(),
   731                                                                 m->signature());
   732     }
   733     if (lm == m)
   734       // Method m is inherited into ctxk.
   735       return true;
   736     if (lm != NULL) {
   737       if (!(lm->is_public() || lm->is_protected()))
   738         // Method is [package-]private, so the override story is complex.
   739         return true;  // Must punt the assertion to true.
   740       if (   !Dependencies::is_concrete_method(lm)
   741           && !Dependencies::is_concrete_method(m)
   742           && Klass::cast(lm->method_holder())->is_subtype_of(m->method_holder()))
   743         // Method m is overridden by lm, but both are non-concrete.
   744         return true;
   745     }
   746     ResourceMark rm;
   747     tty->print_cr("Dependency method not found in the associated context:");
   748     tty->print_cr("  context = %s", Klass::cast(ctxk)->external_name());
   749     tty->print(   "  method = "); m->print_short_name(tty); tty->cr();
   750     if (lm != NULL) {
   751       tty->print( "  found = "); lm->print_short_name(tty); tty->cr();
   752     }
   753     return false;
   754   }
   755 #endif
   757   void add_participant(klassOop participant) {
   758     assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob");
   759     int np = _num_participants++;
   760     _participants[np] = participant;
   761     _participants[np+1] = NULL;
   762     _found_methods[np+1] = NULL;
   763   }
   765   void record_witnesses(int add) {
   766     if (add > PARTICIPANT_LIMIT)  add = PARTICIPANT_LIMIT;
   767     assert(_num_participants + add < PARTICIPANT_LIMIT, "oob");
   768     _record_witnesses = add;
   769   }
   771   bool is_witness(klassOop k) {
   772     if (doing_subtype_search()) {
   773       return Dependencies::is_concrete_klass(k);
   774     } else {
   775       methodOop m = instanceKlass::cast(k)->find_method(_name, _signature);
   776       if (m == NULL || !Dependencies::is_concrete_method(m))  return false;
   777       _found_methods[_num_participants] = m;
   778       // Note:  If add_participant(k) is called,
   779       // the method m will already be memoized for it.
   780       return true;
   781     }
   782   }
   784   bool is_participant(klassOop k) {
   785     if (k == _participants[0]) {
   786       return true;
   787     } else if (_num_participants <= 1) {
   788       return false;
   789     } else {
   790       return in_list(k, &_participants[1]);
   791     }
   792   }
   793   bool ignore_witness(klassOop witness) {
   794     if (_record_witnesses == 0) {
   795       return false;
   796     } else {
   797       --_record_witnesses;
   798       add_participant(witness);
   799       return true;
   800     }
   801   }
   802   static bool in_list(klassOop x, klassOop* list) {
   803     for (int i = 0; ; i++) {
   804       klassOop y = list[i];
   805       if (y == NULL)  break;
   806       if (y == x)  return true;
   807     }
   808     return false;  // not in list
   809   }
   811  private:
   812   // the actual search method:
   813   klassOop find_witness_anywhere(klassOop context_type,
   814                                  bool participants_hide_witnesses,
   815                                  bool top_level_call = true);
   816   // the spot-checking version:
   817   klassOop find_witness_in(KlassDepChange& changes,
   818                            klassOop context_type,
   819                            bool participants_hide_witnesses);
   820  public:
   821   klassOop find_witness_subtype(klassOop context_type, KlassDepChange* changes = NULL) {
   822     assert(doing_subtype_search(), "must set up a subtype search");
   823     // When looking for unexpected concrete types,
   824     // do not look beneath expected ones.
   825     const bool participants_hide_witnesses = true;
   826     // CX > CC > C' is OK, even if C' is new.
   827     // CX > { CC,  C' } is not OK if C' is new, and C' is the witness.
   828     if (changes != NULL) {
   829       return find_witness_in(*changes, context_type, participants_hide_witnesses);
   830     } else {
   831       return find_witness_anywhere(context_type, participants_hide_witnesses);
   832     }
   833   }
   834   klassOop find_witness_definer(klassOop context_type, KlassDepChange* changes = NULL) {
   835     assert(!doing_subtype_search(), "must set up a method definer search");
   836     // When looking for unexpected concrete methods,
   837     // look beneath expected ones, to see if there are overrides.
   838     const bool participants_hide_witnesses = true;
   839     // CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness.
   840     if (changes != NULL) {
   841       return find_witness_in(*changes, context_type, !participants_hide_witnesses);
   842     } else {
   843       return find_witness_anywhere(context_type, !participants_hide_witnesses);
   844     }
   845   }
   846 };
   848 #ifndef PRODUCT
   849 static int deps_find_witness_calls = 0;
   850 static int deps_find_witness_steps = 0;
   851 static int deps_find_witness_recursions = 0;
   852 static int deps_find_witness_singles = 0;
   853 static int deps_find_witness_print = 0; // set to -1 to force a final print
   854 static bool count_find_witness_calls() {
   855   if (TraceDependencies || LogCompilation) {
   856     int pcount = deps_find_witness_print + 1;
   857     bool final_stats      = (pcount == 0);
   858     bool initial_call     = (pcount == 1);
   859     bool occasional_print = ((pcount & ((1<<10) - 1)) == 0);
   860     if (pcount < 0)  pcount = 1; // crude overflow protection
   861     deps_find_witness_print = pcount;
   862     if (VerifyDependencies && initial_call) {
   863       tty->print_cr("Warning:  TraceDependencies results may be inflated by VerifyDependencies");
   864     }
   865     if (occasional_print || final_stats) {
   866       // Every now and then dump a little info about dependency searching.
   867       if (xtty != NULL) {
   868        ttyLocker ttyl;
   869        xtty->elem("deps_find_witness calls='%d' steps='%d' recursions='%d' singles='%d'",
   870                    deps_find_witness_calls,
   871                    deps_find_witness_steps,
   872                    deps_find_witness_recursions,
   873                    deps_find_witness_singles);
   874       }
   875       if (final_stats || (TraceDependencies && WizardMode)) {
   876         ttyLocker ttyl;
   877         tty->print_cr("Dependency check (find_witness) "
   878                       "calls=%d, steps=%d (avg=%.1f), recursions=%d, singles=%d",
   879                       deps_find_witness_calls,
   880                       deps_find_witness_steps,
   881                       (double)deps_find_witness_steps / deps_find_witness_calls,
   882                       deps_find_witness_recursions,
   883                       deps_find_witness_singles);
   884       }
   885     }
   886     return true;
   887   }
   888   return false;
   889 }
   890 #else
   891 #define count_find_witness_calls() (0)
   892 #endif //PRODUCT
   895 klassOop ClassHierarchyWalker::find_witness_in(KlassDepChange& changes,
   896                                                klassOop context_type,
   897                                                bool participants_hide_witnesses) {
   898   assert(changes.involves_context(context_type), "irrelevant dependency");
   899   klassOop new_type = changes.new_type();
   901   count_find_witness_calls();
   902   NOT_PRODUCT(deps_find_witness_singles++);
   904   // Current thread must be in VM (not native mode, as in CI):
   905   assert(must_be_in_vm(), "raw oops here");
   906   // Must not move the class hierarchy during this check:
   907   assert_locked_or_safepoint(Compile_lock);
   909   int nof_impls = instanceKlass::cast(context_type)->nof_implementors();
   910   if (nof_impls > 1) {
   911     // Avoid this case: *I.m > { A.m, C }; B.m > C
   912     // %%% Until this is fixed more systematically, bail out.
   913     // See corresponding comment in find_witness_anywhere.
   914     return context_type;
   915   }
   917   assert(!is_participant(new_type), "only old classes are participants");
   918   if (participants_hide_witnesses) {
   919     // If the new type is a subtype of a participant, we are done.
   920     for (int i = 0; i < num_participants(); i++) {
   921       klassOop part = participant(i);
   922       if (part == NULL)  continue;
   923       assert(changes.involves_context(part) == Klass::cast(new_type)->is_subtype_of(part),
   924              "correct marking of participants, b/c new_type is unique");
   925       if (changes.involves_context(part)) {
   926         // new guy is protected from this check by previous participant
   927         return NULL;
   928       }
   929     }
   930   }
   932   if (is_witness(new_type) &&
   933       !ignore_witness(new_type)) {
   934     return new_type;
   935   }
   937   return NULL;
   938 }
   941 // Walk hierarchy under a context type, looking for unexpected types.
   942 // Do not report participant types, and recursively walk beneath
   943 // them only if participants_hide_witnesses is false.
   944 // If top_level_call is false, skip testing the context type,
   945 // because the caller has already considered it.
   946 klassOop ClassHierarchyWalker::find_witness_anywhere(klassOop context_type,
   947                                                      bool participants_hide_witnesses,
   948                                                      bool top_level_call) {
   949   // Current thread must be in VM (not native mode, as in CI):
   950   assert(must_be_in_vm(), "raw oops here");
   951   // Must not move the class hierarchy during this check:
   952   assert_locked_or_safepoint(Compile_lock);
   954   bool do_counts = count_find_witness_calls();
   956   // Check the root of the sub-hierarchy first.
   957   if (top_level_call) {
   958     if (do_counts) {
   959       NOT_PRODUCT(deps_find_witness_calls++);
   960       NOT_PRODUCT(deps_find_witness_steps++);
   961     }
   962     if (is_participant(context_type)) {
   963       if (participants_hide_witnesses)  return NULL;
   964       // else fall through to search loop...
   965     } else if (is_witness(context_type) && !ignore_witness(context_type)) {
   966       // The context is an abstract class or interface, to start with.
   967       return context_type;
   968     }
   969   }
   971   // Now we must check each implementor and each subclass.
   972   // Use a short worklist to avoid blowing the stack.
   973   // Each worklist entry is a *chain* of subklass siblings to process.
   974   const int CHAINMAX = 100;  // >= 1 + instanceKlass::implementors_limit
   975   Klass* chains[CHAINMAX];
   976   int    chaini = 0;  // index into worklist
   977   Klass* chain;       // scratch variable
   978 #define ADD_SUBCLASS_CHAIN(k)                     {  \
   979     assert(chaini < CHAINMAX, "oob");                \
   980     chain = instanceKlass::cast(k)->subklass();      \
   981     if (chain != NULL)  chains[chaini++] = chain;    }
   983   // Look for non-abstract subclasses.
   984   // (Note:  Interfaces do not have subclasses.)
   985   ADD_SUBCLASS_CHAIN(context_type);
   987   // If it is an interface, search its direct implementors.
   988   // (Their subclasses are additional indirect implementors.
   989   // See instanceKlass::add_implementor.)
   990   // (Note:  nof_implementors is always zero for non-interfaces.)
   991   int nof_impls = instanceKlass::cast(context_type)->nof_implementors();
   992   if (nof_impls > 1) {
   993     // Avoid this case: *I.m > { A.m, C }; B.m > C
   994     // Here, I.m has 2 concrete implementations, but m appears unique
   995     // as A.m, because the search misses B.m when checking C.
   996     // The inherited method B.m was getting missed by the walker
   997     // when interface 'I' was the starting point.
   998     // %%% Until this is fixed more systematically, bail out.
   999     // (Old CHA had the same limitation.)
  1000     return context_type;
  1002   for (int i = 0; i < nof_impls; i++) {
  1003     klassOop impl = instanceKlass::cast(context_type)->implementor(i);
  1004     if (impl == NULL) {
  1005       // implementors array overflowed => no exact info.
  1006       return context_type;  // report an inexact witness to this sad affair
  1008     if (do_counts)
  1009       { NOT_PRODUCT(deps_find_witness_steps++); }
  1010     if (is_participant(impl)) {
  1011       if (participants_hide_witnesses)  continue;
  1012       // else fall through to process this guy's subclasses
  1013     } else if (is_witness(impl) && !ignore_witness(impl)) {
  1014       return impl;
  1016     ADD_SUBCLASS_CHAIN(impl);
  1019   // Recursively process each non-trivial sibling chain.
  1020   while (chaini > 0) {
  1021     Klass* chain = chains[--chaini];
  1022     for (Klass* subk = chain; subk != NULL; subk = subk->next_sibling()) {
  1023       klassOop sub = subk->as_klassOop();
  1024       if (do_counts) { NOT_PRODUCT(deps_find_witness_steps++); }
  1025       if (is_participant(sub)) {
  1026         if (participants_hide_witnesses)  continue;
  1027         // else fall through to process this guy's subclasses
  1028       } else if (is_witness(sub) && !ignore_witness(sub)) {
  1029         return sub;
  1031       if (chaini < (VerifyDependencies? 2: CHAINMAX)) {
  1032         // Fast path.  (Partially disabled if VerifyDependencies.)
  1033         ADD_SUBCLASS_CHAIN(sub);
  1034       } else {
  1035         // Worklist overflow.  Do a recursive call.  Should be rare.
  1036         // The recursive call will have its own worklist, of course.
  1037         // (Note that sub has already been tested, so that there is
  1038         // no need for the recursive call to re-test.  That's handy,
  1039         // since the recursive call sees sub as the context_type.)
  1040         if (do_counts) { NOT_PRODUCT(deps_find_witness_recursions++); }
  1041         klassOop witness = find_witness_anywhere(sub,
  1042                                                  participants_hide_witnesses,
  1043                                                  /*top_level_call=*/ false);
  1044         if (witness != NULL)  return witness;
  1049   // No witness found.  The dependency remains unbroken.
  1050   return NULL;
  1051 #undef ADD_SUBCLASS_CHAIN
  1055 bool Dependencies::is_concrete_klass(klassOop k) {
  1056   if (Klass::cast(k)->is_abstract())  return false;
  1057   // %%% We could treat classes which are concrete but
  1058   // have not yet been instantiated as virtually abstract.
  1059   // This would require a deoptimization barrier on first instantiation.
  1060   //if (k->is_not_instantiated())  return false;
  1061   return true;
  1064 bool Dependencies::is_concrete_method(methodOop m) {
  1065   if (m->is_abstract())  return false;
  1066   // %%% We could treat unexecuted methods as virtually abstract also.
  1067   // This would require a deoptimization barrier on first execution.
  1068   return !m->is_abstract();
  1072 Klass* Dependencies::find_finalizable_subclass(Klass* k) {
  1073   if (k->is_interface())  return NULL;
  1074   if (k->has_finalizer()) return k;
  1075   k = k->subklass();
  1076   while (k != NULL) {
  1077     Klass* result = find_finalizable_subclass(k);
  1078     if (result != NULL) return result;
  1079     k = k->next_sibling();
  1081   return NULL;
  1085 bool Dependencies::is_concrete_klass(ciInstanceKlass* k) {
  1086   if (k->is_abstract())  return false;
  1087   // We could return also false if k does not yet appear to be
  1088   // instantiated, if the VM version supports this distinction also.
  1089   //if (k->is_not_instantiated())  return false;
  1090   return true;
  1093 bool Dependencies::is_concrete_method(ciMethod* m) {
  1094   // Statics are irrelevant to virtual call sites.
  1095   if (m->is_static())  return false;
  1097   // We could return also false if m does not yet appear to be
  1098   // executed, if the VM version supports this distinction also.
  1099   return !m->is_abstract();
  1103 bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) {
  1104   return k->has_finalizable_subclass();
  1108 // Any use of the contents (bytecodes) of a method must be
  1109 // marked by an "evol_method" dependency, if those contents
  1110 // can change.  (Note: A method is always dependent on itself.)
  1111 klassOop Dependencies::check_evol_method(methodOop m) {
  1112   assert(must_be_in_vm(), "raw oops here");
  1113   // Did somebody do a JVMTI RedefineClasses while our backs were turned?
  1114   // Or is there a now a breakpoint?
  1115   // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.)
  1116   if (m->is_old()
  1117       || m->number_of_breakpoints() > 0) {
  1118     return m->method_holder();
  1119   } else {
  1120     return NULL;
  1124 // This is a strong assertion:  It is that the given type
  1125 // has no subtypes whatever.  It is most useful for
  1126 // optimizing checks on reflected types or on array types.
  1127 // (Checks on types which are derived from real instances
  1128 // can be optimized more strongly than this, because we
  1129 // know that the checked type comes from a concrete type,
  1130 // and therefore we can disregard abstract types.)
  1131 klassOop Dependencies::check_leaf_type(klassOop ctxk) {
  1132   assert(must_be_in_vm(), "raw oops here");
  1133   assert_locked_or_safepoint(Compile_lock);
  1134   instanceKlass* ctx = instanceKlass::cast(ctxk);
  1135   Klass* sub = ctx->subklass();
  1136   if (sub != NULL) {
  1137     return sub->as_klassOop();
  1138   } else if (ctx->nof_implementors() != 0) {
  1139     // if it is an interface, it must be unimplemented
  1140     // (if it is not an interface, nof_implementors is always zero)
  1141     klassOop impl = ctx->implementor(0);
  1142     return (impl != NULL)? impl: ctxk;
  1143   } else {
  1144     return NULL;
  1148 // Test the assertion that conck is the only concrete subtype* of ctxk.
  1149 // The type conck itself is allowed to have have further concrete subtypes.
  1150 // This allows the compiler to narrow occurrences of ctxk by conck,
  1151 // when dealing with the types of actual instances.
  1152 klassOop Dependencies::check_abstract_with_unique_concrete_subtype(klassOop ctxk,
  1153                                                                    klassOop conck,
  1154                                                                    KlassDepChange* changes) {
  1155   ClassHierarchyWalker wf(conck);
  1156   return wf.find_witness_subtype(ctxk, changes);
  1159 // If a non-concrete class has no concrete subtypes, it is not (yet)
  1160 // instantiatable.  This can allow the compiler to make some paths go
  1161 // dead, if they are gated by a test of the type.
  1162 klassOop Dependencies::check_abstract_with_no_concrete_subtype(klassOop ctxk,
  1163                                                                KlassDepChange* changes) {
  1164   // Find any concrete subtype, with no participants:
  1165   ClassHierarchyWalker wf;
  1166   return wf.find_witness_subtype(ctxk, changes);
  1170 // If a concrete class has no concrete subtypes, it can always be
  1171 // exactly typed.  This allows the use of a cheaper type test.
  1172 klassOop Dependencies::check_concrete_with_no_concrete_subtype(klassOop ctxk,
  1173                                                                KlassDepChange* changes) {
  1174   // Find any concrete subtype, with only the ctxk as participant:
  1175   ClassHierarchyWalker wf(ctxk);
  1176   return wf.find_witness_subtype(ctxk, changes);
  1180 // Find the unique concrete proper subtype of ctxk, or NULL if there
  1181 // is more than one concrete proper subtype.  If there are no concrete
  1182 // proper subtypes, return ctxk itself, whether it is concrete or not.
  1183 // The returned subtype is allowed to have have further concrete subtypes.
  1184 // That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }.
  1185 klassOop Dependencies::find_unique_concrete_subtype(klassOop ctxk) {
  1186   ClassHierarchyWalker wf(ctxk);   // Ignore ctxk when walking.
  1187   wf.record_witnesses(1);          // Record one other witness when walking.
  1188   klassOop wit = wf.find_witness_subtype(ctxk);
  1189   if (wit != NULL)  return NULL;   // Too many witnesses.
  1190   klassOop conck = wf.participant(0);
  1191   if (conck == NULL) {
  1192 #ifndef PRODUCT
  1193     // Make sure the dependency mechanism will pass this discovery:
  1194     if (VerifyDependencies) {
  1195       // Turn off dependency tracing while actually testing deps.
  1196       FlagSetting fs(TraceDependencies, false);
  1197       if (!Dependencies::is_concrete_klass(ctxk)) {
  1198         guarantee(NULL ==
  1199                   (void *)check_abstract_with_no_concrete_subtype(ctxk),
  1200                   "verify dep.");
  1201       } else {
  1202         guarantee(NULL ==
  1203                   (void *)check_concrete_with_no_concrete_subtype(ctxk),
  1204                   "verify dep.");
  1207 #endif //PRODUCT
  1208     return ctxk;                   // Return ctxk as a flag for "no subtypes".
  1209   } else {
  1210 #ifndef PRODUCT
  1211     // Make sure the dependency mechanism will pass this discovery:
  1212     if (VerifyDependencies) {
  1213       // Turn off dependency tracing while actually testing deps.
  1214       FlagSetting fs(TraceDependencies, false);
  1215       if (!Dependencies::is_concrete_klass(ctxk)) {
  1216         guarantee(NULL == (void *)
  1217                   check_abstract_with_unique_concrete_subtype(ctxk, conck),
  1218                   "verify dep.");
  1221 #endif //PRODUCT
  1222     return conck;
  1226 // Test the assertion that the k[12] are the only concrete subtypes of ctxk,
  1227 // except possibly for further subtypes of k[12] themselves.
  1228 // The context type must be abstract.  The types k1 and k2 are themselves
  1229 // allowed to have further concrete subtypes.
  1230 klassOop Dependencies::check_abstract_with_exclusive_concrete_subtypes(
  1231                                                 klassOop ctxk,
  1232                                                 klassOop k1,
  1233                                                 klassOop k2,
  1234                                                 KlassDepChange* changes) {
  1235   ClassHierarchyWalker wf;
  1236   wf.add_participant(k1);
  1237   wf.add_participant(k2);
  1238   return wf.find_witness_subtype(ctxk, changes);
  1241 // Search ctxk for concrete implementations.  If there are klen or fewer,
  1242 // pack them into the given array and return the number.
  1243 // Otherwise, return -1, meaning the given array would overflow.
  1244 // (Note that a return of 0 means there are exactly no concrete subtypes.)
  1245 // In this search, if ctxk is concrete, it will be reported alone.
  1246 // For any type CC reported, no proper subtypes of CC will be reported.
  1247 int Dependencies::find_exclusive_concrete_subtypes(klassOop ctxk,
  1248                                                    int klen,
  1249                                                    klassOop karray[]) {
  1250   ClassHierarchyWalker wf;
  1251   wf.record_witnesses(klen);
  1252   klassOop wit = wf.find_witness_subtype(ctxk);
  1253   if (wit != NULL)  return -1;  // Too many witnesses.
  1254   int num = wf.num_participants();
  1255   assert(num <= klen, "oob");
  1256   // Pack the result array with the good news.
  1257   for (int i = 0; i < num; i++)
  1258     karray[i] = wf.participant(i);
  1259 #ifndef PRODUCT
  1260   // Make sure the dependency mechanism will pass this discovery:
  1261   if (VerifyDependencies) {
  1262     // Turn off dependency tracing while actually testing deps.
  1263     FlagSetting fs(TraceDependencies, false);
  1264     switch (Dependencies::is_concrete_klass(ctxk)? -1: num) {
  1265     case -1: // ctxk was itself concrete
  1266       guarantee(num == 1 && karray[0] == ctxk, "verify dep.");
  1267       break;
  1268     case 0:
  1269       guarantee(NULL == (void *)check_abstract_with_no_concrete_subtype(ctxk),
  1270                 "verify dep.");
  1271       break;
  1272     case 1:
  1273       guarantee(NULL == (void *)
  1274                 check_abstract_with_unique_concrete_subtype(ctxk, karray[0]),
  1275                 "verify dep.");
  1276       break;
  1277     case 2:
  1278       guarantee(NULL == (void *)
  1279                 check_abstract_with_exclusive_concrete_subtypes(ctxk,
  1280                                                                 karray[0],
  1281                                                                 karray[1]),
  1282                 "verify dep.");
  1283       break;
  1284     default:
  1285       ShouldNotReachHere();  // klen > 2 yet supported
  1288 #endif //PRODUCT
  1289   return num;
  1292 // If a class (or interface) has a unique concrete method uniqm, return NULL.
  1293 // Otherwise, return a class that contains an interfering method.
  1294 klassOop Dependencies::check_unique_concrete_method(klassOop ctxk, methodOop uniqm,
  1295                                                     KlassDepChange* changes) {
  1296   // Here is a missing optimization:  If uniqm->is_final(),
  1297   // we don't really need to search beneath it for overrides.
  1298   // This is probably not important, since we don't use dependencies
  1299   // to track final methods.  (They can't be "definalized".)
  1300   ClassHierarchyWalker wf(uniqm->method_holder(), uniqm);
  1301   return wf.find_witness_definer(ctxk, changes);
  1304 // Find the set of all non-abstract methods under ctxk that match m.
  1305 // (The method m must be defined or inherited in ctxk.)
  1306 // Include m itself in the set, unless it is abstract.
  1307 // If this set has exactly one element, return that element.
  1308 methodOop Dependencies::find_unique_concrete_method(klassOop ctxk, methodOop m) {
  1309   ClassHierarchyWalker wf(m);
  1310   assert(wf.check_method_context(ctxk, m), "proper context");
  1311   wf.record_witnesses(1);
  1312   klassOop wit = wf.find_witness_definer(ctxk);
  1313   if (wit != NULL)  return NULL;  // Too many witnesses.
  1314   methodOop fm = wf.found_method(0);  // Will be NULL if num_parts == 0.
  1315   if (Dependencies::is_concrete_method(m)) {
  1316     if (fm == NULL) {
  1317       // It turns out that m was always the only implementation.
  1318       fm = m;
  1319     } else if (fm != m) {
  1320       // Two conflicting implementations after all.
  1321       // (This can happen if m is inherited into ctxk and fm overrides it.)
  1322       return NULL;
  1325 #ifndef PRODUCT
  1326   // Make sure the dependency mechanism will pass this discovery:
  1327   if (VerifyDependencies && fm != NULL) {
  1328     guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm),
  1329               "verify dep.");
  1331 #endif //PRODUCT
  1332   return fm;
  1335 klassOop Dependencies::check_exclusive_concrete_methods(klassOop ctxk,
  1336                                                         methodOop m1,
  1337                                                         methodOop m2,
  1338                                                         KlassDepChange* changes) {
  1339   ClassHierarchyWalker wf(m1);
  1340   wf.add_participant(m1->method_holder());
  1341   wf.add_participant(m2->method_holder());
  1342   return wf.find_witness_definer(ctxk, changes);
  1345 // Find the set of all non-abstract methods under ctxk that match m[0].
  1346 // (The method m[0] must be defined or inherited in ctxk.)
  1347 // Include m itself in the set, unless it is abstract.
  1348 // Fill the given array m[0..(mlen-1)] with this set, and return the length.
  1349 // (The length may be zero if no concrete methods are found anywhere.)
  1350 // If there are too many concrete methods to fit in marray, return -1.
  1351 int Dependencies::find_exclusive_concrete_methods(klassOop ctxk,
  1352                                                   int mlen,
  1353                                                   methodOop marray[]) {
  1354   methodOop m0 = marray[0];
  1355   ClassHierarchyWalker wf(m0);
  1356   assert(wf.check_method_context(ctxk, m0), "proper context");
  1357   wf.record_witnesses(mlen);
  1358   bool participants_hide_witnesses = true;
  1359   klassOop wit = wf.find_witness_definer(ctxk);
  1360   if (wit != NULL)  return -1;  // Too many witnesses.
  1361   int num = wf.num_participants();
  1362   assert(num <= mlen, "oob");
  1363   // Keep track of whether m is also part of the result set.
  1364   int mfill = 0;
  1365   assert(marray[mfill] == m0, "sanity");
  1366   if (Dependencies::is_concrete_method(m0))
  1367     mfill++;  // keep m0 as marray[0], the first result
  1368   for (int i = 0; i < num; i++) {
  1369     methodOop fm = wf.found_method(i);
  1370     if (fm == m0)  continue;  // Already put this guy in the list.
  1371     if (mfill == mlen) {
  1372       return -1;              // Oops.  Too many methods after all!
  1374     marray[mfill++] = fm;
  1376 #ifndef PRODUCT
  1377   // Make sure the dependency mechanism will pass this discovery:
  1378   if (VerifyDependencies) {
  1379     // Turn off dependency tracing while actually testing deps.
  1380     FlagSetting fs(TraceDependencies, false);
  1381     switch (mfill) {
  1382     case 1:
  1383       guarantee(NULL == (void *)check_unique_concrete_method(ctxk, marray[0]),
  1384                 "verify dep.");
  1385       break;
  1386     case 2:
  1387       guarantee(NULL == (void *)
  1388                 check_exclusive_concrete_methods(ctxk, marray[0], marray[1]),
  1389                 "verify dep.");
  1390       break;
  1391     default:
  1392       ShouldNotReachHere();  // mlen > 2 yet supported
  1395 #endif //PRODUCT
  1396   return mfill;
  1400 klassOop Dependencies::check_has_no_finalizable_subclasses(klassOop ctxk, KlassDepChange* changes) {
  1401   Klass* search_at = ctxk->klass_part();
  1402   if (changes != NULL)
  1403     search_at = changes->new_type()->klass_part(); // just look at the new bit
  1404   Klass* result = find_finalizable_subclass(search_at);
  1405   if (result == NULL) {
  1406     return NULL;
  1408   return result->as_klassOop();
  1412 klassOop Dependencies::check_call_site_target_value(klassOop ctxk, oop call_site, oop method_handle, CallSiteDepChange* changes) {
  1413   assert(call_site    ->is_a(SystemDictionary::CallSite_klass()),     "sanity");
  1414   assert(method_handle->is_a(SystemDictionary::MethodHandle_klass()), "sanity");
  1415   if (changes == NULL) {
  1416     // Validate all CallSites
  1417     if (java_lang_invoke_CallSite::target(call_site) != method_handle)
  1418       return ctxk;  // assertion failed
  1419   } else {
  1420     // Validate the given CallSite
  1421     if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {
  1422       assert(method_handle != changes->method_handle(), "must be");
  1423       return ctxk;  // assertion failed
  1426   assert(java_lang_invoke_CallSite::target(call_site) == method_handle, "should still be valid");
  1427   return NULL;  // assertion still valid
  1431 void Dependencies::DepStream::trace_and_log_witness(klassOop witness) {
  1432   if (witness != NULL) {
  1433     if (TraceDependencies) {
  1434       print_dependency(witness, /*verbose=*/ true);
  1436     // The following is a no-op unless logging is enabled:
  1437     log_dependency(witness);
  1442 klassOop Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {
  1443   assert_locked_or_safepoint(Compile_lock);
  1444   Dependencies::check_valid_dependency_type(type());
  1446   klassOop witness = NULL;
  1447   switch (type()) {
  1448   case evol_method:
  1449     witness = check_evol_method(method_argument(0));
  1450     break;
  1451   case leaf_type:
  1452     witness = check_leaf_type(context_type());
  1453     break;
  1454   case abstract_with_unique_concrete_subtype:
  1455     witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes);
  1456     break;
  1457   case abstract_with_no_concrete_subtype:
  1458     witness = check_abstract_with_no_concrete_subtype(context_type(), changes);
  1459     break;
  1460   case concrete_with_no_concrete_subtype:
  1461     witness = check_concrete_with_no_concrete_subtype(context_type(), changes);
  1462     break;
  1463   case unique_concrete_method:
  1464     witness = check_unique_concrete_method(context_type(), method_argument(1), changes);
  1465     break;
  1466   case abstract_with_exclusive_concrete_subtypes_2:
  1467     witness = check_abstract_with_exclusive_concrete_subtypes(context_type(), type_argument(1), type_argument(2), changes);
  1468     break;
  1469   case exclusive_concrete_methods_2:
  1470     witness = check_exclusive_concrete_methods(context_type(), method_argument(1), method_argument(2), changes);
  1471     break;
  1472   case no_finalizable_subclasses:
  1473     witness = check_has_no_finalizable_subclasses(context_type(), changes);
  1474     break;
  1475   default:
  1476     witness = NULL;
  1477     break;
  1479   trace_and_log_witness(witness);
  1480   return witness;
  1484 klassOop Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) {
  1485   assert_locked_or_safepoint(Compile_lock);
  1486   Dependencies::check_valid_dependency_type(type());
  1488   klassOop witness = NULL;
  1489   switch (type()) {
  1490   case call_site_target_value:
  1491     witness = check_call_site_target_value(context_type(), argument(1), argument(2), changes);
  1492     break;
  1493   default:
  1494     witness = NULL;
  1495     break;
  1497   trace_and_log_witness(witness);
  1498   return witness;
  1502 klassOop Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {
  1503   // Handle klass dependency
  1504   if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))
  1505     return check_klass_dependency(changes.as_klass_change());
  1507   // Handle CallSite dependency
  1508   if (changes.is_call_site_change())
  1509     return check_call_site_dependency(changes.as_call_site_change());
  1511   // irrelevant dependency; skip it
  1512   return NULL;
  1516 void DepChange::print() {
  1517   int nsup = 0, nint = 0;
  1518   for (ContextStream str(*this); str.next(); ) {
  1519     klassOop k = str.klass();
  1520     switch (str.change_type()) {
  1521     case Change_new_type:
  1522       tty->print_cr("  dependee = %s", instanceKlass::cast(k)->external_name());
  1523       break;
  1524     case Change_new_sub:
  1525       if (!WizardMode) {
  1526         ++nsup;
  1527       } else {
  1528         tty->print_cr("  context super = %s", instanceKlass::cast(k)->external_name());
  1530       break;
  1531     case Change_new_impl:
  1532       if (!WizardMode) {
  1533         ++nint;
  1534       } else {
  1535         tty->print_cr("  context interface = %s", instanceKlass::cast(k)->external_name());
  1537       break;
  1540   if (nsup + nint != 0) {
  1541     tty->print_cr("  context supers = %d, interfaces = %d", nsup, nint);
  1545 void DepChange::ContextStream::start() {
  1546   klassOop new_type = _changes.is_klass_change() ? _changes.as_klass_change()->new_type() : (klassOop) NULL;
  1547   _change_type = (new_type == NULL ? NO_CHANGE : Start_Klass);
  1548   _klass = new_type;
  1549   _ti_base = NULL;
  1550   _ti_index = 0;
  1551   _ti_limit = 0;
  1554 bool DepChange::ContextStream::next() {
  1555   switch (_change_type) {
  1556   case Start_Klass:             // initial state; _klass is the new type
  1557     _ti_base = instanceKlass::cast(_klass)->transitive_interfaces();
  1558     _ti_index = 0;
  1559     _change_type = Change_new_type;
  1560     return true;
  1561   case Change_new_type:
  1562     // fall through:
  1563     _change_type = Change_new_sub;
  1564   case Change_new_sub:
  1565     // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277
  1567       _klass = instanceKlass::cast(_klass)->super();
  1568       if (_klass != NULL) {
  1569         return true;
  1572     // else set up _ti_limit and fall through:
  1573     _ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length();
  1574     _change_type = Change_new_impl;
  1575   case Change_new_impl:
  1576     if (_ti_index < _ti_limit) {
  1577       _klass = klassOop( _ti_base->obj_at(_ti_index++) );
  1578       return true;
  1580     // fall through:
  1581     _change_type = NO_CHANGE;  // iterator is exhausted
  1582   case NO_CHANGE:
  1583     break;
  1584   default:
  1585     ShouldNotReachHere();
  1587   return false;
  1590 void KlassDepChange::initialize() {
  1591   // entire transaction must be under this lock:
  1592   assert_lock_strong(Compile_lock);
  1594   // Mark all dependee and all its superclasses
  1595   // Mark transitive interfaces
  1596   for (ContextStream str(*this); str.next(); ) {
  1597     klassOop d = str.klass();
  1598     assert(!instanceKlass::cast(d)->is_marked_dependent(), "checking");
  1599     instanceKlass::cast(d)->set_is_marked_dependent(true);
  1603 KlassDepChange::~KlassDepChange() {
  1604   // Unmark all dependee and all its superclasses
  1605   // Unmark transitive interfaces
  1606   for (ContextStream str(*this); str.next(); ) {
  1607     klassOop d = str.klass();
  1608     instanceKlass::cast(d)->set_is_marked_dependent(false);
  1612 bool KlassDepChange::involves_context(klassOop k) {
  1613   if (k == NULL || !Klass::cast(k)->oop_is_instance()) {
  1614     return false;
  1616   instanceKlass* ik = instanceKlass::cast(k);
  1617   bool is_contained = ik->is_marked_dependent();
  1618   assert(is_contained == Klass::cast(new_type())->is_subtype_of(k),
  1619          "correct marking of potential context types");
  1620   return is_contained;
  1623 #ifndef PRODUCT
  1624 void Dependencies::print_statistics() {
  1625   if (deps_find_witness_print != 0) {
  1626     // Call one final time, to flush out the data.
  1627     deps_find_witness_print = -1;
  1628     count_find_witness_calls();
  1631 #endif

mercurial