src/share/vm/code/dependencies.cpp

Thu, 20 Nov 2014 11:06:26 +0100

author
thartmann
date
Thu, 20 Nov 2014 11:06:26 +0100
changeset 7380
bee8095780db
parent 7030
3c048df3ef8b
child 7502
c4f1e23c4139
child 7717
41c3c456e326
permissions
-rw-r--r--

8050079: crash while compiling java.lang.ref.Finalizer::runFinalizer
Summary: Ignore non-instance Klasses in the subclass hierarchy.
Reviewed-by: kvn, iignatyev, jrose

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

mercurial