src/share/vm/code/dependencies.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6319
51e1bb81df86
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial