< prev index next >

src/share/vm/code/dependencies.cpp

Print this page




  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #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 "classfile/javaClasses.inline.hpp"
  31 #include "code/dependencies.hpp"
  32 #include "compiler/compileLog.hpp"
  33 #include "oops/oop.inline.hpp"

  34 #include "runtime/handles.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/thread.inline.hpp"
  37 #include "utilities/copy.hpp"
  38 
  39 
  40 #ifdef ASSERT
  41 static bool must_be_in_vm() {
  42   Thread* thread = Thread::current();
  43   if (thread->is_Java_thread())
  44     return ((JavaThread*)thread)->thread_state() == _thread_in_vm;
  45   else
  46     return true;  //something like this: thread->is_VM_thread();
  47 }
  48 #endif //ASSERT
  49 
  50 void Dependencies::initialize(ciEnv* env) {
  51   Arena* arena = env->arena();
  52   _oop_recorder = env->oop_recorder();
  53   _log = env->log();
  54   _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);



  55   DEBUG_ONLY(_deps[end_marker] = NULL);
  56   for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
  57     _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0);
  58   }
  59   _content_bytes = NULL;
  60   _size_in_bytes = (size_t)-1;
  61 
  62   assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
  63 }
  64 
  65 void Dependencies::assert_evol_method(ciMethod* m) {
  66   assert_common_1(evol_method, m);
  67 }
  68 
  69 void Dependencies::assert_leaf_type(ciKlass* ctxk) {
  70   if (ctxk->is_array_klass()) {
  71     // As a special case, support this assertion on an array type,
  72     // which reduces to an assertion on its element type.
  73     // Note that this cannot be done with assertions that
  74     // relate to concreteness or abstractness.


 103 
 104 void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {
 105   check_ctxk(ctxk);
 106   assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);
 107 }
 108 
 109 void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {
 110   check_ctxk(ctxk);
 111   assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);
 112 }
 113 
 114 void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {
 115   check_ctxk(ctxk);
 116   assert_common_1(no_finalizable_subclasses, ctxk);
 117 }
 118 
 119 void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {
 120   assert_common_2(call_site_target_value, call_site, method_handle);
 121 }
 122 




























































 123 // Helper function.  If we are adding a new dep. under ctxk2,
 124 // try to find an old dep. under a broader* ctxk1.  If there is
 125 //
 126 bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
 127                                     int ctxk_i, ciKlass* ctxk2) {
 128   ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();
 129   if (ctxk2->is_subtype_of(ctxk1)) {
 130     return true;  // success, and no need to change
 131   } else if (ctxk1->is_subtype_of(ctxk2)) {
 132     // new context class fully subsumes previous one
 133     deps->at_put(ctxk_i, ctxk2);
 134     return true;
 135   } else {
 136     return false;
 137   }
 138 }
 139 
 140 void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {
 141   assert(dep_args(dept) == 1, "sanity");
 142   log_dependency(dept, x);


 213   // see if the same (or a similar) dep is already recorded
 214   if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) {
 215     // look in this bucket for redundant assertions
 216     const int stride = 3;
 217     for (int i = deps->length(); (i -= stride) >= 0; ) {
 218       ciBaseObject* y  = deps->at(i+1);
 219       ciBaseObject* y2 = deps->at(i+2);
 220       if (x == y && x2 == y2) {  // same subjects; check the context
 221         if (maybe_merge_ctxk(deps, i+0, ctxk)) {
 222           return;
 223         }
 224       }
 225     }
 226   }
 227   // append the assertion in the correct bucket:
 228   deps->append(ctxk);
 229   deps->append(x);
 230   deps->append(x2);
 231 }
 232 








































































 233 /// Support for encoding dependencies into an nmethod:
 234 
 235 void Dependencies::copy_to(nmethod* nm) {
 236   address beg = nm->dependencies_begin();
 237   address end = nm->dependencies_end();
 238   guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");
 239   Copy::disjoint_words((HeapWord*) content_bytes(),
 240                        (HeapWord*) beg,
 241                        size_in_bytes() / sizeof(HeapWord));
 242   assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");
 243 }
 244 
 245 static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) {
 246   for (int i = 0; i < narg; i++) {
 247     int diff = p1[i]->ident() - p2[i]->ident();
 248     if (diff != 0)  return diff;
 249   }
 250   return 0;
 251 }
 252 static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2)
 253 { return sort_dep(p1, p2, 1); }
 254 static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2)
 255 { return sort_dep(p1, p2, 2); }
 256 static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2)
 257 { return sort_dep(p1, p2, 3); }
 258 

















 259 void Dependencies::sort_all_deps() {
















 260   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 261     DepType dept = (DepType)deptv;
 262     GrowableArray<ciBaseObject*>* deps = _deps[dept];
 263     if (deps->length() <= 1)  continue;
 264     switch (dep_args(dept)) {
 265     case 1: deps->sort(sort_dep_arg_1, 1); break;
 266     case 2: deps->sort(sort_dep_arg_2, 2); break;
 267     case 3: deps->sort(sort_dep_arg_3, 3); break;
 268     default: ShouldNotReachHere();
 269     }
 270   }
 271 }
 272 
 273 size_t Dependencies::estimate_size_in_bytes() {
 274   size_t est_size = 100;










 275   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 276     DepType dept = (DepType)deptv;
 277     GrowableArray<ciBaseObject*>* deps = _deps[dept];
 278     est_size += deps->length()*2;  // tags and argument(s)
 279   }
 280   return est_size;
 281 }
 282 
 283 ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) {
 284   switch (dept) {
 285   case abstract_with_exclusive_concrete_subtypes_2:
 286     return x->as_metadata()->as_klass();
 287   case unique_concrete_method:
 288   case exclusive_concrete_methods_2:
 289     return x->as_metadata()->as_method()->holder();
 290   }
 291   return NULL;  // let NULL be NULL
 292 }
 293 
 294 Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) {
 295   assert(must_be_in_vm(), "raw oops here");
 296   switch (dept) {
 297   case abstract_with_exclusive_concrete_subtypes_2:
 298     assert(x->is_klass(), "sanity");
 299     return (Klass*) x;
 300   case unique_concrete_method:
 301   case exclusive_concrete_methods_2:
 302     assert(x->is_method(), "sanity");
 303     return ((Method*)x)->method_holder();
 304   }
 305   return NULL;  // let NULL be NULL
 306 }
 307 
 308 void Dependencies::encode_content_bytes() {
 309   sort_all_deps();
 310 
 311   // cast is safe, no deps can overflow INT_MAX
 312   CompressedWriteStream bytes((int)estimate_size_in_bytes());
 313 































 314   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 315     DepType dept = (DepType)deptv;
 316     GrowableArray<ciBaseObject*>* deps = _deps[dept];
 317     if (deps->length() == 0)  continue;
 318     int stride = dep_args(dept);
 319     int ctxkj  = dep_context_arg(dept);  // -1 if no context arg
 320     assert(stride > 0, "sanity");
 321     for (int i = 0; i < deps->length(); i += stride) {
 322       jbyte code_byte = (jbyte)dept;
 323       int skipj = -1;
 324       if (ctxkj >= 0 && ctxkj+1 < stride) {
 325         ciKlass*  ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass();
 326         ciBaseObject* x     = deps->at(i+ctxkj+1);  // following argument
 327         if (ctxk == ctxk_encoded_as_null(dept, x)) {
 328           skipj = ctxkj;  // we win:  maybe one less oop to keep track of
 329           code_byte |= default_context_type_bit;
 330         }
 331       }
 332       bytes.write_byte(code_byte);
 333       for (int j = 0; j < stride; j++) {
 334         if (j == skipj)  continue;
 335         ciBaseObject* v = deps->at(i+j);
 336         int idx;
 337         if (v->is_object()) {
 338           idx = _oop_recorder->find_index(v->as_object()->constant_encoding());
 339         } else {
 340           ciMetadata* meta = v->as_metadata();
 341           idx = _oop_recorder->find_index(meta->constant_encoding());
 342         }
 343         bytes.write_int(idx);
 344       }
 345     }
 346   }



 347 
 348   // write a sentinel byte to mark the end
 349   bytes.write_byte(end_marker);
 350 
 351   // round it out to a word boundary
 352   while (bytes.position() % sizeof(HeapWord) != 0) {
 353     bytes.write_byte(end_marker);
 354   }
 355 
 356   // check whether the dept byte encoding really works
 357   assert((jbyte)default_context_type_bit != 0, "byte overflow");
 358 
 359   _content_bytes = bytes.buffer();
 360   _size_in_bytes = bytes.position();
 361 }
 362 
 363 
 364 const char* Dependencies::_dep_name[TYPE_LIMIT] = {
 365   "end_marker",
 366   "evol_method",


 523       } else {
 524         xtty->object("x", arg.metadata_value());
 525       }
 526     } else {
 527       char xn[10]; sprintf(xn, "x%d", j);
 528       if (arg.is_oop()) {
 529         xtty->object(xn, arg.oop_value());
 530       } else {
 531         xtty->object(xn, arg.metadata_value());
 532       }
 533     }
 534   }
 535   if (witness != NULL) {
 536     xtty->object("witness", witness);
 537     xtty->stamp();
 538   }
 539   xtty->end_elem();
 540 }
 541 
 542 void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args,
 543                                     Klass* witness) {
 544   ResourceMark rm;
 545   ttyLocker ttyl;   // keep the following output all in one block
 546   tty->print_cr("%s of type %s",
 547                 (witness == NULL)? "Dependency": "Failed dependency",
 548                 dep_name(dept));
 549   // print arguments
 550   int ctxkj = dep_context_arg(dept);  // -1 if no context arg
 551   for (int j = 0; j < args->length(); j++) {
 552     DepArgument arg = args->at(j);
 553     bool put_star = false;
 554     if (arg.is_null())  continue;
 555     const char* what;
 556     if (j == ctxkj) {
 557       assert(arg.is_metadata(), "must be");
 558       what = "context";
 559       put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value());
 560     } else if (arg.is_method()) {
 561       what = "method ";
 562       put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), NULL);
 563     } else if (arg.is_klass()) {
 564       what = "class  ";
 565     } else {
 566       what = "object ";
 567     }
 568     tty->print("  %s = %s", what, (put_star? "*": ""));
 569     if (arg.is_klass()) {
 570       tty->print("%s", ((Klass*)arg.metadata_value())->external_name());
 571     } else if (arg.is_method()) {
 572       ((Method*)arg.metadata_value())->print_value();
 573     } else if (arg.is_oop()) {
 574       arg.oop_value()->print_value_on(tty);
 575     } else {
 576       ShouldNotReachHere(); // Provide impl for this type.
 577     }
 578 
 579     tty->cr();
 580   }
 581   if (witness != NULL) {
 582     bool put_star = !Dependencies::is_concrete_klass(witness);
 583     tty->print_cr("  witness = %s%s",
 584                   (put_star? "*": ""),
 585                   witness->external_name());
 586   }
 587 }
 588 
 589 void Dependencies::DepStream::log_dependency(Klass* witness) {
 590   if (_deps == NULL && xtty == NULL)  return;  // fast cutout for runtime
 591   ResourceMark rm;
 592   const int nargs = argument_count();
 593   GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);
 594   for (int j = 0; j < nargs; j++) {
 595     if (is_oop_argument(j)) {
 596       args->push(argument_oop(j));
 597     } else {
 598       args->push(argument(j));
 599     }
 600   }
 601   int argslen = args->length();
 602   if (_deps != NULL && _deps->log() != NULL) {

 603     Dependencies::write_dependency_to(_deps->log(), type(), args, witness);
 604   } else {




 605     Dependencies::write_dependency_to(xtty, type(), args, witness);
 606   }
 607   guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");
 608 }
 609 
 610 void Dependencies::DepStream::print_dependency(Klass* witness, bool verbose) {
 611   ResourceMark rm;
 612   int nargs = argument_count();
 613   GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);
 614   for (int j = 0; j < nargs; j++) {
 615     if (is_oop_argument(j)) {
 616       args->push(argument_oop(j));
 617     } else {
 618       args->push(argument(j));
 619     }
 620   }
 621   int argslen = args->length();
 622   Dependencies::print_dependency(type(), args, witness);
 623   if (verbose) {
 624     if (_code != NULL) {
 625       tty->print("  code: ");
 626       _code->print_value_on(tty);
 627       tty->cr();
 628     }
 629   }
 630   guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");
 631 }
 632 
 633 
 634 /// Dependency stream support (decodes dependencies from an nmethod):
 635 
 636 #ifdef ASSERT
 637 void Dependencies::DepStream::initial_asserts(size_t byte_limit) {
 638   assert(must_be_in_vm(), "raw oops here");
 639   _byte_limit = byte_limit;
 640   _type       = (DepType)(end_marker-1);  // defeat "already at end" assert
 641   assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");
 642 }
 643 #endif //ASSERT
 644 
 645 bool Dependencies::DepStream::next() {
 646   assert(_type != end_marker, "already at end");
 647   if (_bytes.position() == 0 && _code != NULL




  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #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 "classfile/javaClasses.inline.hpp"
  31 #include "code/dependencies.hpp"
  32 #include "compiler/compileLog.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "oops/objArrayKlass.hpp"
  35 #include "runtime/handles.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/thread.inline.hpp"
  38 #include "utilities/copy.hpp"
  39 
  40 
  41 #ifdef ASSERT
  42 static bool must_be_in_vm() {
  43   Thread* thread = Thread::current();
  44   if (thread->is_Java_thread())
  45     return ((JavaThread*)thread)->thread_state() == _thread_in_vm;
  46   else
  47     return true;  //something like this: thread->is_VM_thread();
  48 }
  49 #endif //ASSERT
  50 
  51 void Dependencies::initialize(ciEnv* env) {
  52   Arena* arena = env->arena();
  53   _oop_recorder = env->oop_recorder();
  54   _log = env->log();
  55   _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);
  56 #if INCLUDE_JVMCI
  57   _using_dep_values = false;
  58 #endif
  59   DEBUG_ONLY(_deps[end_marker] = NULL);
  60   for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
  61     _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0);
  62   }
  63   _content_bytes = NULL;
  64   _size_in_bytes = (size_t)-1;
  65 
  66   assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
  67 }
  68 
  69 void Dependencies::assert_evol_method(ciMethod* m) {
  70   assert_common_1(evol_method, m);
  71 }
  72 
  73 void Dependencies::assert_leaf_type(ciKlass* ctxk) {
  74   if (ctxk->is_array_klass()) {
  75     // As a special case, support this assertion on an array type,
  76     // which reduces to an assertion on its element type.
  77     // Note that this cannot be done with assertions that
  78     // relate to concreteness or abstractness.


 107 
 108 void Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {
 109   check_ctxk(ctxk);
 110   assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);
 111 }
 112 
 113 void Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {
 114   check_ctxk(ctxk);
 115   assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);
 116 }
 117 
 118 void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {
 119   check_ctxk(ctxk);
 120   assert_common_1(no_finalizable_subclasses, ctxk);
 121 }
 122 
 123 void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {
 124   assert_common_2(call_site_target_value, call_site, method_handle);
 125 }
 126 
 127 #if INCLUDE_JVMCI
 128 
 129 Dependencies::Dependencies(Arena* arena, OopRecorder* oop_recorder, CompileLog* log) {
 130   _oop_recorder = oop_recorder;
 131   _log = log;
 132   _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);
 133   _using_dep_values = true;
 134   DEBUG_ONLY(_dep_values[end_marker] = NULL);
 135   for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
 136     _dep_values[i] = new(arena) GrowableArray<DepValue>(arena, 10, 0, DepValue());
 137   }
 138   _content_bytes = NULL;
 139   _size_in_bytes = (size_t)-1;
 140 
 141   assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
 142 }
 143 
 144 void Dependencies::assert_evol_method(Method* m) {
 145   assert_common_1(evol_method, DepValue(_oop_recorder, m));
 146 }
 147 
 148 void Dependencies::assert_has_no_finalizable_subclasses(Klass* ctxk) {
 149   check_ctxk(ctxk);
 150   assert_common_1(no_finalizable_subclasses, DepValue(_oop_recorder, ctxk));
 151 }
 152 
 153 void Dependencies::assert_leaf_type(Klass* ctxk) {
 154   if (ctxk->oop_is_array()) {
 155     // As a special case, support this assertion on an array type,
 156     // which reduces to an assertion on its element type.
 157     // Note that this cannot be done with assertions that
 158     // relate to concreteness or abstractness.
 159     BasicType elemt = ArrayKlass::cast(ctxk)->element_type();
 160     if (is_java_primitive(elemt))  return;   // Ex:  int[][]
 161     ctxk = ObjArrayKlass::cast(ctxk)->bottom_klass();
 162     //if (ctxk->is_final())  return;            // Ex:  String[][]
 163   }
 164   check_ctxk(ctxk);
 165   assert_common_1(leaf_type, DepValue(_oop_recorder, ctxk));
 166 }
 167 
 168 void Dependencies::assert_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck) {
 169   check_ctxk_abstract(ctxk);
 170   DepValue ctxk_dv(_oop_recorder, ctxk);
 171   DepValue conck_dv(_oop_recorder, conck, &ctxk_dv);
 172   assert_common_2(abstract_with_unique_concrete_subtype, ctxk_dv, conck_dv);
 173 }
 174 
 175 void Dependencies::assert_unique_concrete_method(Klass* ctxk, Method* uniqm) {
 176   check_ctxk(ctxk);
 177   assert_common_2(unique_concrete_method, DepValue(_oop_recorder, ctxk), DepValue(_oop_recorder, uniqm));
 178 }
 179 
 180 void Dependencies::assert_call_site_target_value(oop call_site, oop method_handle) {
 181   assert_common_2(call_site_target_value, DepValue(_oop_recorder, JNIHandles::make_local(call_site)), DepValue(_oop_recorder, JNIHandles::make_local(method_handle)));
 182 }
 183 
 184 #endif // INCLUDE_JVMCI
 185 
 186 
 187 // Helper function.  If we are adding a new dep. under ctxk2,
 188 // try to find an old dep. under a broader* ctxk1.  If there is
 189 //
 190 bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps,
 191                                     int ctxk_i, ciKlass* ctxk2) {
 192   ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass();
 193   if (ctxk2->is_subtype_of(ctxk1)) {
 194     return true;  // success, and no need to change
 195   } else if (ctxk1->is_subtype_of(ctxk2)) {
 196     // new context class fully subsumes previous one
 197     deps->at_put(ctxk_i, ctxk2);
 198     return true;
 199   } else {
 200     return false;
 201   }
 202 }
 203 
 204 void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) {
 205   assert(dep_args(dept) == 1, "sanity");
 206   log_dependency(dept, x);


 277   // see if the same (or a similar) dep is already recorded
 278   if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) {
 279     // look in this bucket for redundant assertions
 280     const int stride = 3;
 281     for (int i = deps->length(); (i -= stride) >= 0; ) {
 282       ciBaseObject* y  = deps->at(i+1);
 283       ciBaseObject* y2 = deps->at(i+2);
 284       if (x == y && x2 == y2) {  // same subjects; check the context
 285         if (maybe_merge_ctxk(deps, i+0, ctxk)) {
 286           return;
 287         }
 288       }
 289     }
 290   }
 291   // append the assertion in the correct bucket:
 292   deps->append(ctxk);
 293   deps->append(x);
 294   deps->append(x2);
 295 }
 296 
 297 #if INCLUDE_JVMCI
 298 bool Dependencies::maybe_merge_ctxk(GrowableArray<DepValue>* deps,
 299                                     int ctxk_i, DepValue ctxk2_dv) {
 300   Klass* ctxk1 = deps->at(ctxk_i).as_klass(_oop_recorder);
 301   Klass* ctxk2 = ctxk2_dv.as_klass(_oop_recorder);
 302   if (ctxk2->is_subtype_of(ctxk1)) {
 303     return true;  // success, and no need to change
 304   } else if (ctxk1->is_subtype_of(ctxk2)) {
 305     // new context class fully subsumes previous one
 306     deps->at_put(ctxk_i, ctxk2_dv);
 307     return true;
 308   } else {
 309     return false;
 310   }
 311 }
 312 
 313 void Dependencies::assert_common_1(DepType dept, DepValue x) {
 314   assert(dep_args(dept) == 1, "sanity");
 315   //log_dependency(dept, x);
 316   GrowableArray<DepValue>* deps = _dep_values[dept];
 317 
 318   // see if the same (or a similar) dep is already recorded
 319   if (note_dep_seen(dept, x)) {
 320     assert(deps->find(x) >= 0, "sanity");
 321   } else {
 322     deps->append(x);
 323   }
 324 }
 325 
 326 void Dependencies::assert_common_2(DepType dept,
 327                                    DepValue x0, DepValue x1) {
 328   assert(dep_args(dept) == 2, "sanity");
 329   //log_dependency(dept, x0, x1);
 330   GrowableArray<DepValue>* deps = _dep_values[dept];
 331 
 332   // see if the same (or a similar) dep is already recorded
 333   bool has_ctxk = has_explicit_context_arg(dept);
 334   if (has_ctxk) {
 335     assert(dep_context_arg(dept) == 0, "sanity");
 336     if (note_dep_seen(dept, x1)) {
 337       // look in this bucket for redundant assertions
 338       const int stride = 2;
 339       for (int i = deps->length(); (i -= stride) >= 0; ) {
 340         DepValue y1 = deps->at(i+1);
 341         if (x1 == y1) {  // same subject; check the context
 342           if (maybe_merge_ctxk(deps, i+0, x0)) {
 343             return;
 344           }
 345         }
 346       }
 347     }
 348   } else {
 349     assert(dep_implicit_context_arg(dept) == 0, "sanity");
 350     if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {
 351       // look in this bucket for redundant assertions
 352       const int stride = 2;
 353       for (int i = deps->length(); (i -= stride) >= 0; ) {
 354         DepValue y0 = deps->at(i+0);
 355         DepValue y1 = deps->at(i+1);
 356         if (x0 == y0 && x1 == y1) {
 357           return;
 358         }
 359       }
 360     }
 361   }
 362 
 363   // append the assertion in the correct bucket:
 364   deps->append(x0);
 365   deps->append(x1);
 366 }
 367 #endif // INCLUDE_JVMCI
 368 
 369 /// Support for encoding dependencies into an nmethod:
 370 
 371 void Dependencies::copy_to(nmethod* nm) {
 372   address beg = nm->dependencies_begin();
 373   address end = nm->dependencies_end();
 374   guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");
 375   Copy::disjoint_words((HeapWord*) content_bytes(),
 376                        (HeapWord*) beg,
 377                        size_in_bytes() / sizeof(HeapWord));
 378   assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");
 379 }
 380 
 381 static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) {
 382   for (int i = 0; i < narg; i++) {
 383     int diff = p1[i]->ident() - p2[i]->ident();
 384     if (diff != 0)  return diff;
 385   }
 386   return 0;
 387 }
 388 static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2)
 389 { return sort_dep(p1, p2, 1); }
 390 static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2)
 391 { return sort_dep(p1, p2, 2); }
 392 static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2)
 393 { return sort_dep(p1, p2, 3); }
 394 
 395 #if INCLUDE_JVMCI
 396 // metadata deps are sorted before object deps
 397 static int sort_dep_value(Dependencies::DepValue* p1, Dependencies::DepValue* p2, int narg) {
 398   for (int i = 0; i < narg; i++) {
 399     int diff = p1[i].sort_key() - p2[i].sort_key();
 400     if (diff != 0)  return diff;
 401   }
 402   return 0;
 403 }
 404 static int sort_dep_value_arg_1(Dependencies::DepValue* p1, Dependencies::DepValue* p2)
 405 { return sort_dep_value(p1, p2, 1); }
 406 static int sort_dep_value_arg_2(Dependencies::DepValue* p1, Dependencies::DepValue* p2)
 407 { return sort_dep_value(p1, p2, 2); }
 408 static int sort_dep_value_arg_3(Dependencies::DepValue* p1, Dependencies::DepValue* p2)
 409 { return sort_dep_value(p1, p2, 3); }
 410 #endif // INCLUDE_JVMCI
 411 
 412 void Dependencies::sort_all_deps() {
 413 #if INCLUDE_JVMCI
 414   if (_using_dep_values) {
 415     for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 416       DepType dept = (DepType)deptv;
 417       GrowableArray<DepValue>* deps = _dep_values[dept];
 418       if (deps->length() <= 1)  continue;
 419       switch (dep_args(dept)) {
 420       case 1: deps->sort(sort_dep_value_arg_1, 1); break;
 421       case 2: deps->sort(sort_dep_value_arg_2, 2); break;
 422       case 3: deps->sort(sort_dep_value_arg_3, 3); break;
 423       default: ShouldNotReachHere();
 424       }
 425     }
 426     return;
 427   }
 428 #endif // INCLUDE_JVMCI
 429   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 430     DepType dept = (DepType)deptv;
 431     GrowableArray<ciBaseObject*>* deps = _deps[dept];
 432     if (deps->length() <= 1)  continue;
 433     switch (dep_args(dept)) {
 434     case 1: deps->sort(sort_dep_arg_1, 1); break;
 435     case 2: deps->sort(sort_dep_arg_2, 2); break;
 436     case 3: deps->sort(sort_dep_arg_3, 3); break;
 437     default: ShouldNotReachHere();
 438     }
 439   }
 440 }
 441 
 442 size_t Dependencies::estimate_size_in_bytes() {
 443   size_t est_size = 100;
 444 #if INCLUDE_JVMCI
 445   if (_using_dep_values) {
 446     for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 447       DepType dept = (DepType)deptv;
 448       GrowableArray<DepValue>* deps = _dep_values[dept];
 449       est_size += deps->length() * 2;  // tags and argument(s)
 450     }
 451     return est_size;
 452   }
 453 #endif // INCLUDE_JVMCI
 454   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 455     DepType dept = (DepType)deptv;
 456     GrowableArray<ciBaseObject*>* deps = _deps[dept];
 457     est_size += deps->length()*2;  // tags and argument(s)
 458   }
 459   return est_size;
 460 }
 461 
 462 ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) {
 463   switch (dept) {
 464   case abstract_with_exclusive_concrete_subtypes_2:
 465     return x->as_metadata()->as_klass();
 466   case unique_concrete_method:
 467   case exclusive_concrete_methods_2:
 468     return x->as_metadata()->as_method()->holder();
 469   }
 470   return NULL;  // let NULL be NULL
 471 }
 472 
 473 Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) {
 474   assert(must_be_in_vm(), "raw oops here");
 475   switch (dept) {
 476   case abstract_with_exclusive_concrete_subtypes_2:
 477     assert(x->is_klass(), "sanity");
 478     return (Klass*) x;
 479   case unique_concrete_method:
 480   case exclusive_concrete_methods_2:
 481     assert(x->is_method(), "sanity");
 482     return ((Method*)x)->method_holder();
 483   }
 484   return NULL;  // let NULL be NULL
 485 }
 486 
 487 void Dependencies::encode_content_bytes() {
 488   sort_all_deps();
 489 
 490   // cast is safe, no deps can overflow INT_MAX
 491   CompressedWriteStream bytes((int)estimate_size_in_bytes());
 492 
 493 #if INCLUDE_JVMCI
 494   if (_using_dep_values) {
 495     for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 496       DepType dept = (DepType)deptv;
 497       GrowableArray<DepValue>* deps = _dep_values[dept];
 498       if (deps->length() == 0)  continue;
 499       int stride = dep_args(dept);
 500       int ctxkj  = dep_context_arg(dept);  // -1 if no context arg
 501       assert(stride > 0, "sanity");
 502       for (int i = 0; i < deps->length(); i += stride) {
 503         jbyte code_byte = (jbyte)dept;
 504         int skipj = -1;
 505         if (ctxkj >= 0 && ctxkj+1 < stride) {
 506           Klass*  ctxk = deps->at(i+ctxkj+0).as_klass(_oop_recorder);
 507           DepValue x = deps->at(i+ctxkj+1);  // following argument
 508           if (ctxk == ctxk_encoded_as_null(dept, x.as_metadata(_oop_recorder))) {
 509             skipj = ctxkj;  // we win:  maybe one less oop to keep track of
 510             code_byte |= default_context_type_bit;
 511           }
 512         }
 513         bytes.write_byte(code_byte);
 514         for (int j = 0; j < stride; j++) {
 515           if (j == skipj)  continue;
 516           DepValue v = deps->at(i+j);
 517           int idx = v.index();
 518           bytes.write_int(idx);
 519         }
 520       }
 521     }
 522   } else {
 523 #endif // INCLUDE_JVMCI
 524   for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
 525     DepType dept = (DepType)deptv;
 526     GrowableArray<ciBaseObject*>* deps = _deps[dept];
 527     if (deps->length() == 0)  continue;
 528     int stride = dep_args(dept);
 529     int ctxkj  = dep_context_arg(dept);  // -1 if no context arg
 530     assert(stride > 0, "sanity");
 531     for (int i = 0; i < deps->length(); i += stride) {
 532       jbyte code_byte = (jbyte)dept;
 533       int skipj = -1;
 534       if (ctxkj >= 0 && ctxkj+1 < stride) {
 535         ciKlass*  ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass();
 536         ciBaseObject* x     = deps->at(i+ctxkj+1);  // following argument
 537         if (ctxk == ctxk_encoded_as_null(dept, x)) {
 538           skipj = ctxkj;  // we win:  maybe one less oop to keep track of
 539           code_byte |= default_context_type_bit;
 540         }
 541       }
 542       bytes.write_byte(code_byte);
 543       for (int j = 0; j < stride; j++) {
 544         if (j == skipj)  continue;
 545         ciBaseObject* v = deps->at(i+j);
 546         int idx;
 547         if (v->is_object()) {
 548           idx = _oop_recorder->find_index(v->as_object()->constant_encoding());
 549         } else {
 550           ciMetadata* meta = v->as_metadata();
 551           idx = _oop_recorder->find_index(meta->constant_encoding());
 552         }
 553         bytes.write_int(idx);
 554       }
 555     }
 556   }
 557 #if INCLUDE_JVMCI
 558   }
 559 #endif
 560 
 561   // write a sentinel byte to mark the end
 562   bytes.write_byte(end_marker);
 563 
 564   // round it out to a word boundary
 565   while (bytes.position() % sizeof(HeapWord) != 0) {
 566     bytes.write_byte(end_marker);
 567   }
 568 
 569   // check whether the dept byte encoding really works
 570   assert((jbyte)default_context_type_bit != 0, "byte overflow");
 571 
 572   _content_bytes = bytes.buffer();
 573   _size_in_bytes = bytes.position();
 574 }
 575 
 576 
 577 const char* Dependencies::_dep_name[TYPE_LIMIT] = {
 578   "end_marker",
 579   "evol_method",


 736       } else {
 737         xtty->object("x", arg.metadata_value());
 738       }
 739     } else {
 740       char xn[10]; sprintf(xn, "x%d", j);
 741       if (arg.is_oop()) {
 742         xtty->object(xn, arg.oop_value());
 743       } else {
 744         xtty->object(xn, arg.metadata_value());
 745       }
 746     }
 747   }
 748   if (witness != NULL) {
 749     xtty->object("witness", witness);
 750     xtty->stamp();
 751   }
 752   xtty->end_elem();
 753 }
 754 
 755 void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args,
 756                                     Klass* witness, outputStream* st) {
 757   ResourceMark rm;
 758   ttyLocker ttyl;   // keep the following output all in one block
 759   st->print_cr("%s of type %s",
 760                 (witness == NULL)? "Dependency": "Failed dependency",
 761                 dep_name(dept));
 762   // print arguments
 763   int ctxkj = dep_context_arg(dept);  // -1 if no context arg
 764   for (int j = 0; j < args->length(); j++) {
 765     DepArgument arg = args->at(j);
 766     bool put_star = false;
 767     if (arg.is_null())  continue;
 768     const char* what;
 769     if (j == ctxkj) {
 770       assert(arg.is_metadata(), "must be");
 771       what = "context";
 772       put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value());
 773     } else if (arg.is_method()) {
 774       what = "method ";
 775       put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), NULL);
 776     } else if (arg.is_klass()) {
 777       what = "class  ";
 778     } else {
 779       what = "object ";
 780     }
 781     st->print("  %s = %s", what, (put_star? "*": ""));
 782     if (arg.is_klass()) {
 783       st->print("%s", ((Klass*)arg.metadata_value())->external_name());
 784     } else if (arg.is_method()) {
 785       ((Method*)arg.metadata_value())->print_value_on(st);
 786     } else if (arg.is_oop()) {
 787       arg.oop_value()->print_value_on(st);
 788     } else {
 789       ShouldNotReachHere(); // Provide impl for this type.
 790     }
 791 
 792     st->cr();
 793   }
 794   if (witness != NULL) {
 795     bool put_star = !Dependencies::is_concrete_klass(witness);
 796     st->print_cr("  witness = %s%s",
 797                   (put_star? "*": ""),
 798                   witness->external_name());
 799   }
 800 }
 801 
 802 void Dependencies::DepStream::log_dependency(Klass* witness) {
 803   if (_deps == NULL && xtty == NULL)  return;  // fast cutout for runtime
 804   ResourceMark rm;
 805   const int nargs = argument_count();
 806   GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);
 807   for (int j = 0; j < nargs; j++) {
 808     if (is_oop_argument(j)) {
 809       args->push(argument_oop(j));
 810     } else {
 811       args->push(argument(j));
 812     }
 813   }
 814   int argslen = args->length();
 815   if (_deps != NULL && _deps->log() != NULL) {
 816     if (ciEnv::current() != NULL) {
 817       Dependencies::write_dependency_to(_deps->log(), type(), args, witness);
 818     } else {
 819       // Treat the CompileLog as an xmlstream instead
 820       Dependencies::write_dependency_to((xmlStream*)_deps->log(), type(), args, witness);
 821     }
 822   } else {
 823     Dependencies::write_dependency_to(xtty, type(), args, witness);
 824   }
 825   guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");
 826 }
 827 
 828 void Dependencies::DepStream::print_dependency(Klass* witness, bool verbose, outputStream* st) {
 829   ResourceMark rm;
 830   int nargs = argument_count();
 831   GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs);
 832   for (int j = 0; j < nargs; j++) {
 833     if (is_oop_argument(j)) {
 834       args->push(argument_oop(j));
 835     } else {
 836       args->push(argument(j));
 837     }
 838   }
 839   int argslen = args->length();
 840   Dependencies::print_dependency(type(), args, witness, st);
 841   if (verbose) {
 842     if (_code != NULL) {
 843       st->print("  code: ");
 844       _code->print_value_on(st);
 845       st->cr();
 846     }
 847   }
 848   guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope");
 849 }
 850 
 851 
 852 /// Dependency stream support (decodes dependencies from an nmethod):
 853 
 854 #ifdef ASSERT
 855 void Dependencies::DepStream::initial_asserts(size_t byte_limit) {
 856   assert(must_be_in_vm(), "raw oops here");
 857   _byte_limit = byte_limit;
 858   _type       = (DepType)(end_marker-1);  // defeat "already at end" assert
 859   assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");
 860 }
 861 #endif //ASSERT
 862 
 863 bool Dependencies::DepStream::next() {
 864   assert(_type != end_marker, "already at end");
 865   if (_bytes.position() == 0 && _code != NULL


< prev index next >