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