src/share/vm/code/dependencies.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7761
d8f133adf05d
parent 7535
7ae4e26cb1e0
child 9122
024be04bb151
permissions
-rw-r--r--

merge

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

mercurial