0N/A/*
3669N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "ci/ciArrayKlass.hpp"
1879N/A#include "ci/ciEnv.hpp"
1879N/A#include "ci/ciKlass.hpp"
1879N/A#include "ci/ciMethod.hpp"
1879N/A#include "code/dependencies.hpp"
1879N/A#include "compiler/compileLog.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "utilities/copy.hpp"
0N/A
0N/A
0N/A#ifdef ASSERT
0N/Astatic bool must_be_in_vm() {
0N/A Thread* thread = Thread::current();
0N/A if (thread->is_Java_thread())
0N/A return ((JavaThread*)thread)->thread_state() == _thread_in_vm;
0N/A else
0N/A return true; //something like this: thread->is_VM_thread();
0N/A}
0N/A#endif //ASSERT
0N/A
0N/Avoid Dependencies::initialize(ciEnv* env) {
0N/A Arena* arena = env->arena();
0N/A _oop_recorder = env->oop_recorder();
0N/A _log = env->log();
0N/A _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0);
0N/A DEBUG_ONLY(_deps[end_marker] = NULL);
0N/A for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) {
0N/A _deps[i] = new(arena) GrowableArray<ciObject*>(arena, 10, 0, 0);
0N/A }
0N/A _content_bytes = NULL;
0N/A _size_in_bytes = (size_t)-1;
0N/A
0N/A assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity");
0N/A}
0N/A
0N/Avoid Dependencies::assert_evol_method(ciMethod* m) {
0N/A assert_common_1(evol_method, m);
0N/A}
0N/A
0N/Avoid Dependencies::assert_leaf_type(ciKlass* ctxk) {
0N/A if (ctxk->is_array_klass()) {
0N/A // As a special case, support this assertion on an array type,
0N/A // which reduces to an assertion on its element type.
0N/A // Note that this cannot be done with assertions that
0N/A // relate to concreteness or abstractness.
0N/A ciType* elemt = ctxk->as_array_klass()->base_element_type();
0N/A if (!elemt->is_instance_klass()) return; // Ex: int[][]
0N/A ctxk = elemt->as_instance_klass();
0N/A //if (ctxk->is_final()) return; // Ex: String[][]
0N/A }
0N/A check_ctxk(ctxk);
0N/A assert_common_1(leaf_type, ctxk);
0N/A}
0N/A
0N/Avoid Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) {
0N/A check_ctxk_abstract(ctxk);
0N/A assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck);
0N/A}
0N/A
0N/Avoid Dependencies::assert_abstract_with_no_concrete_subtype(ciKlass* ctxk) {
0N/A check_ctxk_abstract(ctxk);
0N/A assert_common_1(abstract_with_no_concrete_subtype, ctxk);
0N/A}
0N/A
0N/Avoid Dependencies::assert_concrete_with_no_concrete_subtype(ciKlass* ctxk) {
0N/A check_ctxk_concrete(ctxk);
0N/A assert_common_1(concrete_with_no_concrete_subtype, ctxk);
0N/A}
0N/A
0N/Avoid Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) {
0N/A check_ctxk(ctxk);
0N/A assert_common_2(unique_concrete_method, ctxk, uniqm);
0N/A}
0N/A
0N/Avoid Dependencies::assert_abstract_with_exclusive_concrete_subtypes(ciKlass* ctxk, ciKlass* k1, ciKlass* k2) {
0N/A check_ctxk(ctxk);
0N/A assert_common_3(abstract_with_exclusive_concrete_subtypes_2, ctxk, k1, k2);
0N/A}
0N/A
0N/Avoid Dependencies::assert_exclusive_concrete_methods(ciKlass* ctxk, ciMethod* m1, ciMethod* m2) {
0N/A check_ctxk(ctxk);
0N/A assert_common_3(exclusive_concrete_methods_2, ctxk, m1, m2);
0N/A}
0N/A
0N/Avoid Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) {
0N/A check_ctxk(ctxk);
0N/A assert_common_1(no_finalizable_subclasses, ctxk);
0N/A}
0N/A
2728N/Avoid Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) {
2728N/A check_ctxk(call_site->klass());
2728N/A assert_common_2(call_site_target_value, call_site, method_handle);
2677N/A}
2677N/A
0N/A// Helper function. If we are adding a new dep. under ctxk2,
0N/A// try to find an old dep. under a broader* ctxk1. If there is
0N/A//
0N/Abool Dependencies::maybe_merge_ctxk(GrowableArray<ciObject*>* deps,
0N/A int ctxk_i, ciKlass* ctxk2) {
0N/A ciKlass* ctxk1 = deps->at(ctxk_i)->as_klass();
0N/A if (ctxk2->is_subtype_of(ctxk1)) {
0N/A return true; // success, and no need to change
0N/A } else if (ctxk1->is_subtype_of(ctxk2)) {
0N/A // new context class fully subsumes previous one
0N/A deps->at_put(ctxk_i, ctxk2);
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A}
0N/A
2728N/Avoid Dependencies::assert_common_1(DepType dept, ciObject* x) {
0N/A assert(dep_args(dept) == 1, "sanity");
0N/A log_dependency(dept, x);
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A
0N/A // see if the same (or a similar) dep is already recorded
0N/A if (note_dep_seen(dept, x)) {
0N/A assert(deps->find(x) >= 0, "sanity");
0N/A } else {
0N/A deps->append(x);
0N/A }
0N/A}
0N/A
2728N/Avoid Dependencies::assert_common_2(DepType dept,
2728N/A ciObject* x0, ciObject* x1) {
0N/A assert(dep_args(dept) == 2, "sanity");
2728N/A log_dependency(dept, x0, x1);
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A
0N/A // see if the same (or a similar) dep is already recorded
2728N/A bool has_ctxk = has_explicit_context_arg(dept);
2728N/A if (has_ctxk) {
2728N/A assert(dep_context_arg(dept) == 0, "sanity");
2728N/A if (note_dep_seen(dept, x1)) {
2728N/A // look in this bucket for redundant assertions
2728N/A const int stride = 2;
2728N/A for (int i = deps->length(); (i -= stride) >= 0; ) {
2728N/A ciObject* y1 = deps->at(i+1);
2728N/A if (x1 == y1) { // same subject; check the context
2728N/A if (maybe_merge_ctxk(deps, i+0, x0->as_klass())) {
2728N/A return;
2728N/A }
2728N/A }
2728N/A }
2728N/A }
2728N/A } else {
2728N/A assert(dep_implicit_context_arg(dept) == 0, "sanity");
2728N/A if (note_dep_seen(dept, x0) && note_dep_seen(dept, x1)) {
2728N/A // look in this bucket for redundant assertions
2728N/A const int stride = 2;
2728N/A for (int i = deps->length(); (i -= stride) >= 0; ) {
2728N/A ciObject* y0 = deps->at(i+0);
2728N/A ciObject* y1 = deps->at(i+1);
2728N/A if (x0 == y0 && x1 == y1) {
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // append the assertion in the correct bucket:
2728N/A deps->append(x0);
2728N/A deps->append(x1);
0N/A}
0N/A
2728N/Avoid Dependencies::assert_common_3(DepType dept,
0N/A ciKlass* ctxk, ciObject* x, ciObject* x2) {
0N/A assert(dep_context_arg(dept) == 0, "sanity");
0N/A assert(dep_args(dept) == 3, "sanity");
0N/A log_dependency(dept, ctxk, x, x2);
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A
0N/A // try to normalize an unordered pair:
0N/A bool swap = false;
0N/A switch (dept) {
0N/A case abstract_with_exclusive_concrete_subtypes_2:
0N/A swap = (x->ident() > x2->ident() && x != ctxk);
0N/A break;
0N/A case exclusive_concrete_methods_2:
0N/A swap = (x->ident() > x2->ident() && x->as_method()->holder() != ctxk);
0N/A break;
0N/A }
0N/A if (swap) { ciObject* t = x; x = x2; x2 = t; }
0N/A
0N/A // see if the same (or a similar) dep is already recorded
0N/A if (note_dep_seen(dept, x) && note_dep_seen(dept, x2)) {
0N/A // look in this bucket for redundant assertions
0N/A const int stride = 3;
0N/A for (int i = deps->length(); (i -= stride) >= 0; ) {
0N/A ciObject* y = deps->at(i+1);
0N/A ciObject* y2 = deps->at(i+2);
0N/A if (x == y && x2 == y2) { // same subjects; check the context
0N/A if (maybe_merge_ctxk(deps, i+0, ctxk)) {
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A // append the assertion in the correct bucket:
0N/A deps->append(ctxk);
0N/A deps->append(x);
0N/A deps->append(x2);
0N/A}
0N/A
0N/A/// Support for encoding dependencies into an nmethod:
0N/A
0N/Avoid Dependencies::copy_to(nmethod* nm) {
0N/A address beg = nm->dependencies_begin();
0N/A address end = nm->dependencies_end();
0N/A guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing");
0N/A Copy::disjoint_words((HeapWord*) content_bytes(),
0N/A (HeapWord*) beg,
0N/A size_in_bytes() / sizeof(HeapWord));
0N/A assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words");
0N/A}
0N/A
0N/Astatic int sort_dep(ciObject** p1, ciObject** p2, int narg) {
0N/A for (int i = 0; i < narg; i++) {
0N/A int diff = p1[i]->ident() - p2[i]->ident();
0N/A if (diff != 0) return diff;
0N/A }
0N/A return 0;
0N/A}
0N/Astatic int sort_dep_arg_1(ciObject** p1, ciObject** p2)
0N/A{ return sort_dep(p1, p2, 1); }
0N/Astatic int sort_dep_arg_2(ciObject** p1, ciObject** p2)
0N/A{ return sort_dep(p1, p2, 2); }
0N/Astatic int sort_dep_arg_3(ciObject** p1, ciObject** p2)
0N/A{ return sort_dep(p1, p2, 3); }
0N/A
0N/Avoid Dependencies::sort_all_deps() {
0N/A for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
0N/A DepType dept = (DepType)deptv;
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A if (deps->length() <= 1) continue;
0N/A switch (dep_args(dept)) {
0N/A case 1: deps->sort(sort_dep_arg_1, 1); break;
0N/A case 2: deps->sort(sort_dep_arg_2, 2); break;
0N/A case 3: deps->sort(sort_dep_arg_3, 3); break;
0N/A default: ShouldNotReachHere();
0N/A }
0N/A }
0N/A}
0N/A
0N/Asize_t Dependencies::estimate_size_in_bytes() {
0N/A size_t est_size = 100;
0N/A for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
0N/A DepType dept = (DepType)deptv;
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A est_size += deps->length()*2; // tags and argument(s)
0N/A }
0N/A return est_size;
0N/A}
0N/A
0N/AciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciObject* x) {
0N/A switch (dept) {
0N/A case abstract_with_exclusive_concrete_subtypes_2:
0N/A return x->as_klass();
0N/A case unique_concrete_method:
0N/A case exclusive_concrete_methods_2:
0N/A return x->as_method()->holder();
0N/A }
0N/A return NULL; // let NULL be NULL
0N/A}
0N/A
0N/AklassOop Dependencies::ctxk_encoded_as_null(DepType dept, oop x) {
0N/A assert(must_be_in_vm(), "raw oops here");
0N/A switch (dept) {
0N/A case abstract_with_exclusive_concrete_subtypes_2:
0N/A assert(x->is_klass(), "sanity");
0N/A return (klassOop) x;
0N/A case unique_concrete_method:
0N/A case exclusive_concrete_methods_2:
0N/A assert(x->is_method(), "sanity");
0N/A return ((methodOop)x)->method_holder();
0N/A }
0N/A return NULL; // let NULL be NULL
0N/A}
0N/A
0N/Avoid Dependencies::encode_content_bytes() {
0N/A sort_all_deps();
0N/A
0N/A // cast is safe, no deps can overflow INT_MAX
0N/A CompressedWriteStream bytes((int)estimate_size_in_bytes());
0N/A
0N/A for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
0N/A DepType dept = (DepType)deptv;
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A if (deps->length() == 0) continue;
0N/A int stride = dep_args(dept);
0N/A int ctxkj = dep_context_arg(dept); // -1 if no context arg
0N/A assert(stride > 0, "sanity");
0N/A for (int i = 0; i < deps->length(); i += stride) {
0N/A jbyte code_byte = (jbyte)dept;
0N/A int skipj = -1;
0N/A if (ctxkj >= 0 && ctxkj+1 < stride) {
0N/A ciKlass* ctxk = deps->at(i+ctxkj+0)->as_klass();
0N/A ciObject* x = deps->at(i+ctxkj+1); // following argument
0N/A if (ctxk == ctxk_encoded_as_null(dept, x)) {
0N/A skipj = ctxkj; // we win: maybe one less oop to keep track of
0N/A code_byte |= default_context_type_bit;
0N/A }
0N/A }
0N/A bytes.write_byte(code_byte);
0N/A for (int j = 0; j < stride; j++) {
0N/A if (j == skipj) continue;
989N/A bytes.write_int(_oop_recorder->find_index(deps->at(i+j)->constant_encoding()));
0N/A }
0N/A }
0N/A }
0N/A
0N/A // write a sentinel byte to mark the end
0N/A bytes.write_byte(end_marker);
0N/A
0N/A // round it out to a word boundary
0N/A while (bytes.position() % sizeof(HeapWord) != 0) {
0N/A bytes.write_byte(end_marker);
0N/A }
0N/A
0N/A // check whether the dept byte encoding really works
0N/A assert((jbyte)default_context_type_bit != 0, "byte overflow");
0N/A
0N/A _content_bytes = bytes.buffer();
0N/A _size_in_bytes = bytes.position();
0N/A}
0N/A
0N/A
0N/Aconst char* Dependencies::_dep_name[TYPE_LIMIT] = {
0N/A "end_marker",
0N/A "evol_method",
0N/A "leaf_type",
0N/A "abstract_with_unique_concrete_subtype",
0N/A "abstract_with_no_concrete_subtype",
0N/A "concrete_with_no_concrete_subtype",
0N/A "unique_concrete_method",
0N/A "abstract_with_exclusive_concrete_subtypes_2",
0N/A "exclusive_concrete_methods_2",
2677N/A "no_finalizable_subclasses",
2677N/A "call_site_target_value"
0N/A};
0N/A
0N/Aint Dependencies::_dep_args[TYPE_LIMIT] = {
0N/A -1,// end_marker
0N/A 1, // evol_method m
0N/A 1, // leaf_type ctxk
0N/A 2, // abstract_with_unique_concrete_subtype ctxk, k
0N/A 1, // abstract_with_no_concrete_subtype ctxk
0N/A 1, // concrete_with_no_concrete_subtype ctxk
0N/A 2, // unique_concrete_method ctxk, m
0N/A 3, // unique_concrete_subtypes_2 ctxk, k1, k2
0N/A 3, // unique_concrete_methods_2 ctxk, m1, m2
2677N/A 1, // no_finalizable_subclasses ctxk
2728N/A 2 // call_site_target_value call_site, method_handle
0N/A};
0N/A
0N/Aconst char* Dependencies::dep_name(Dependencies::DepType dept) {
0N/A if (!dept_in_mask(dept, all_types)) return "?bad-dep?";
0N/A return _dep_name[dept];
0N/A}
0N/A
0N/Aint Dependencies::dep_args(Dependencies::DepType dept) {
0N/A if (!dept_in_mask(dept, all_types)) return -1;
0N/A return _dep_args[dept];
0N/A}
0N/A
2677N/Avoid Dependencies::check_valid_dependency_type(DepType dept) {
2728N/A guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, err_msg("invalid dependency type: %d", (int) dept));
2677N/A}
2677N/A
0N/A// for the sake of the compiler log, print out current dependencies:
0N/Avoid Dependencies::log_all_dependencies() {
0N/A if (log() == NULL) return;
0N/A ciObject* args[max_arg_count];
0N/A for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) {
0N/A DepType dept = (DepType)deptv;
0N/A GrowableArray<ciObject*>* deps = _deps[dept];
0N/A if (deps->length() == 0) continue;
0N/A int stride = dep_args(dept);
0N/A for (int i = 0; i < deps->length(); i += stride) {
0N/A for (int j = 0; j < stride; j++) {
0N/A // flush out the identities before printing
0N/A args[j] = deps->at(i+j);
0N/A }
0N/A write_dependency_to(log(), dept, stride, args);
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid Dependencies::write_dependency_to(CompileLog* log,
0N/A DepType dept,
0N/A int nargs, oop args[],
0N/A klassOop witness) {
0N/A if (log == NULL) {
0N/A return;
0N/A }
0N/A ciEnv* env = ciEnv::current();
0N/A ciObject* ciargs[max_arg_count];
0N/A assert(nargs <= max_arg_count, "oob");
0N/A for (int j = 0; j < nargs; j++) {
0N/A ciargs[j] = env->get_object(args[j]);
0N/A }
0N/A Dependencies::write_dependency_to(log, dept, nargs, ciargs, witness);
0N/A}
0N/A
0N/Avoid Dependencies::write_dependency_to(CompileLog* log,
0N/A DepType dept,
0N/A int nargs, ciObject* args[],
0N/A klassOop witness) {
0N/A if (log == NULL) return;
0N/A assert(nargs <= max_arg_count, "oob");
0N/A int argids[max_arg_count];
0N/A int ctxkj = dep_context_arg(dept); // -1 if no context arg
0N/A int j;
0N/A for (j = 0; j < nargs; j++) {
0N/A argids[j] = log->identify(args[j]);
0N/A }
0N/A if (witness != NULL) {
0N/A log->begin_elem("dependency_failed");
0N/A } else {
0N/A log->begin_elem("dependency");
0N/A }
0N/A log->print(" type='%s'", dep_name(dept));
0N/A if (ctxkj >= 0) {
0N/A log->print(" ctxk='%d'", argids[ctxkj]);
0N/A }
0N/A // write remaining arguments, if any.
0N/A for (j = 0; j < nargs; j++) {
0N/A if (j == ctxkj) continue; // already logged
0N/A if (j == 1) {
0N/A log->print( " x='%d'", argids[j]);
0N/A } else {
0N/A log->print(" x%d='%d'", j, argids[j]);
0N/A }
0N/A }
0N/A if (witness != NULL) {
0N/A log->object("witness", witness);
0N/A log->stamp();
0N/A }
0N/A log->end_elem();
0N/A}
0N/A
0N/Avoid Dependencies::write_dependency_to(xmlStream* xtty,
0N/A DepType dept,
0N/A int nargs, oop args[],
0N/A klassOop witness) {
0N/A if (xtty == NULL) return;
0N/A ttyLocker ttyl;
0N/A int ctxkj = dep_context_arg(dept); // -1 if no context arg
0N/A if (witness != NULL) {
0N/A xtty->begin_elem("dependency_failed");
0N/A } else {
0N/A xtty->begin_elem("dependency");
0N/A }
0N/A xtty->print(" type='%s'", dep_name(dept));
0N/A if (ctxkj >= 0) {
0N/A xtty->object("ctxk", args[ctxkj]);
0N/A }
0N/A // write remaining arguments, if any.
0N/A for (int j = 0; j < nargs; j++) {
0N/A if (j == ctxkj) continue; // already logged
0N/A if (j == 1) {
0N/A xtty->object("x", args[j]);
0N/A } else {
0N/A char xn[10]; sprintf(xn, "x%d", j);
0N/A xtty->object(xn, args[j]);
0N/A }
0N/A }
0N/A if (witness != NULL) {
0N/A xtty->object("witness", witness);
0N/A xtty->stamp();
0N/A }
0N/A xtty->end_elem();
0N/A}
0N/A
0N/Avoid Dependencies::print_dependency(DepType dept, int nargs, oop args[],
0N/A klassOop witness) {
0N/A ResourceMark rm;
0N/A ttyLocker ttyl; // keep the following output all in one block
0N/A tty->print_cr("%s of type %s",
0N/A (witness == NULL)? "Dependency": "Failed dependency",
0N/A dep_name(dept));
0N/A // print arguments
0N/A int ctxkj = dep_context_arg(dept); // -1 if no context arg
0N/A for (int j = 0; j < nargs; j++) {
0N/A oop arg = args[j];
0N/A bool put_star = false;
0N/A if (arg == NULL) continue;
0N/A const char* what;
0N/A if (j == ctxkj) {
0N/A what = "context";
0N/A put_star = !Dependencies::is_concrete_klass((klassOop)arg);
0N/A } else if (arg->is_method()) {
0N/A what = "method ";
0N/A put_star = !Dependencies::is_concrete_method((methodOop)arg);
0N/A } else if (arg->is_klass()) {
0N/A what = "class ";
0N/A } else {
0N/A what = "object ";
0N/A }
0N/A tty->print(" %s = %s", what, (put_star? "*": ""));
0N/A if (arg->is_klass())
0N/A tty->print("%s", Klass::cast((klassOop)arg)->external_name());
0N/A else
0N/A arg->print_value();
0N/A tty->cr();
0N/A }
0N/A if (witness != NULL) {
0N/A bool put_star = !Dependencies::is_concrete_klass(witness);
0N/A tty->print_cr(" witness = %s%s",
0N/A (put_star? "*": ""),
0N/A Klass::cast(witness)->external_name());
0N/A }
0N/A}
0N/A
0N/Avoid Dependencies::DepStream::log_dependency(klassOop witness) {
0N/A if (_deps == NULL && xtty == NULL) return; // fast cutout for runtime
0N/A int nargs = argument_count();
0N/A oop args[max_arg_count];
0N/A for (int j = 0; j < nargs; j++) {
0N/A args[j] = argument(j);
0N/A }
0N/A if (_deps != NULL && _deps->log() != NULL) {
0N/A Dependencies::write_dependency_to(_deps->log(),
0N/A type(), nargs, args, witness);
0N/A } else {
0N/A Dependencies::write_dependency_to(xtty,
0N/A type(), nargs, args, witness);
0N/A }
0N/A}
0N/A
0N/Avoid Dependencies::DepStream::print_dependency(klassOop witness, bool verbose) {
0N/A int nargs = argument_count();
0N/A oop args[max_arg_count];
0N/A for (int j = 0; j < nargs; j++) {
0N/A args[j] = argument(j);
0N/A }
0N/A Dependencies::print_dependency(type(), nargs, args, witness);
0N/A if (verbose) {
0N/A if (_code != NULL) {
0N/A tty->print(" code: ");
0N/A _code->print_value_on(tty);
0N/A tty->cr();
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/// Dependency stream support (decodes dependencies from an nmethod):
0N/A
0N/A#ifdef ASSERT
0N/Avoid Dependencies::DepStream::initial_asserts(size_t byte_limit) {
0N/A assert(must_be_in_vm(), "raw oops here");
0N/A _byte_limit = byte_limit;
0N/A _type = (DepType)(end_marker-1); // defeat "already at end" assert
0N/A assert((_code!=NULL) + (_deps!=NULL) == 1, "one or t'other");
0N/A}
0N/A#endif //ASSERT
0N/A
0N/Abool Dependencies::DepStream::next() {
0N/A assert(_type != end_marker, "already at end");
0N/A if (_bytes.position() == 0 && _code != NULL
0N/A && _code->dependencies_size() == 0) {
0N/A // Method has no dependencies at all.
0N/A return false;
0N/A }
0N/A int code_byte = (_bytes.read_byte() & 0xFF);
0N/A if (code_byte == end_marker) {
0N/A DEBUG_ONLY(_type = end_marker);
0N/A return false;
0N/A } else {
0N/A int ctxk_bit = (code_byte & Dependencies::default_context_type_bit);
0N/A code_byte -= ctxk_bit;
0N/A DepType dept = (DepType)code_byte;
0N/A _type = dept;
2728N/A Dependencies::check_valid_dependency_type(dept);
0N/A int stride = _dep_args[dept];
0N/A assert(stride == dep_args(dept), "sanity");
0N/A int skipj = -1;
0N/A if (ctxk_bit != 0) {
0N/A skipj = 0; // currently the only context argument is at zero
0N/A assert(skipj == dep_context_arg(dept), "zero arg always ctxk");
0N/A }
0N/A for (int j = 0; j < stride; j++) {
0N/A _xi[j] = (j == skipj)? 0: _bytes.read_int();
0N/A }
0N/A DEBUG_ONLY(_xi[stride] = -1); // help detect overruns
0N/A return true;
0N/A }
0N/A}
0N/A
0N/Ainline oop Dependencies::DepStream::recorded_oop_at(int i) {
0N/A return (_code != NULL)
0N/A ? _code->oop_at(i)
0N/A : JNIHandles::resolve(_deps->oop_recorder()->handle_at(i));
0N/A}
0N/A
0N/Aoop Dependencies::DepStream::argument(int i) {
4028N/A oop result = recorded_oop_at(argument_index(i));
4028N/A if (result == NULL) { // Explicit context argument can be compressed
4028N/A int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg
4028N/A if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) {
4028N/A result = ctxk_encoded_as_null(type(), argument(ctxkj+1));
4028N/A }
4028N/A }
4028N/A
4028N/A return result;
0N/A}
0N/A
0N/AklassOop Dependencies::DepStream::context_type() {
0N/A assert(must_be_in_vm(), "raw oops here");
2728N/A
2728N/A // Most dependencies have an explicit context type argument.
2728N/A {
4028N/A int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg
2728N/A if (ctxkj >= 0) {
2728N/A oop k = argument(ctxkj);
4028N/A assert(k != NULL && k->is_klass(), "type check");
4028N/A return (klassOop) k;
2728N/A }
2728N/A }
2728N/A
2728N/A // Some dependencies are using the klass of the first object
2728N/A // argument as implicit context type (e.g. call_site_target_value).
2728N/A {
4028N/A int ctxkj = dep_implicit_context_arg(type());
2728N/A if (ctxkj >= 0) {
2728N/A oop k = argument(ctxkj)->klass();
4028N/A assert(k != NULL && k->is_klass(), "type check");
0N/A return (klassOop) k;
0N/A }
0N/A }
2728N/A
2728N/A // And some dependencies don't have a context type at all,
2728N/A // e.g. evol_method.
2728N/A return NULL;
0N/A}
0N/A
0N/A/// Checking dependencies:
0N/A
0N/A// This hierarchy walker inspects subtypes of a given type,
0N/A// trying to find a "bad" class which breaks a dependency.
0N/A// Such a class is called a "witness" to the broken dependency.
0N/A// While searching around, we ignore "participants", which
0N/A// are already known to the dependency.
0N/Aclass ClassHierarchyWalker {
0N/A public:
0N/A enum { PARTICIPANT_LIMIT = 3 };
0N/A
0N/A private:
0N/A // optional method descriptor to check for:
2062N/A Symbol* _name;
2062N/A Symbol* _signature;
0N/A
0N/A // special classes which are not allowed to be witnesses:
0N/A klassOop _participants[PARTICIPANT_LIMIT+1];
0N/A int _num_participants;
0N/A
0N/A // cache of method lookups
0N/A methodOop _found_methods[PARTICIPANT_LIMIT+1];
0N/A
0N/A // if non-zero, tells how many witnesses to convert to participants
0N/A int _record_witnesses;
0N/A
0N/A void initialize(klassOop participant) {
0N/A _record_witnesses = 0;
0N/A _participants[0] = participant;
0N/A _found_methods[0] = NULL;
0N/A _num_participants = 0;
0N/A if (participant != NULL) {
0N/A // Terminating NULL.
0N/A _participants[1] = NULL;
0N/A _found_methods[1] = NULL;
0N/A _num_participants = 1;
0N/A }
0N/A }
0N/A
0N/A void initialize_from_method(methodOop m) {
0N/A assert(m != NULL && m->is_method(), "sanity");
0N/A _name = m->name();
0N/A _signature = m->signature();
0N/A }
0N/A
0N/A public:
0N/A // The walker is initialized to recognize certain methods and/or types
0N/A // as friendly participants.
0N/A ClassHierarchyWalker(klassOop participant, methodOop m) {
0N/A initialize_from_method(m);
0N/A initialize(participant);
0N/A }
0N/A ClassHierarchyWalker(methodOop m) {
0N/A initialize_from_method(m);
0N/A initialize(NULL);
0N/A }
0N/A ClassHierarchyWalker(klassOop participant = NULL) {
0N/A _name = NULL;
0N/A _signature = NULL;
0N/A initialize(participant);
0N/A }
0N/A
0N/A // This is common code for two searches: One for concrete subtypes,
0N/A // the other for concrete method implementations and overrides.
0N/A bool doing_subtype_search() {
0N/A return _name == NULL;
0N/A }
0N/A
0N/A int num_participants() { return _num_participants; }
0N/A klassOop participant(int n) {
0N/A assert((uint)n <= (uint)_num_participants, "oob");
0N/A return _participants[n];
0N/A }
0N/A
0N/A // Note: If n==num_participants, returns NULL.
0N/A methodOop found_method(int n) {
0N/A assert((uint)n <= (uint)_num_participants, "oob");
0N/A methodOop fm = _found_methods[n];
0N/A assert(n == _num_participants || fm != NULL, "proper usage");
0N/A assert(fm == NULL || fm->method_holder() == _participants[n], "sanity");
0N/A return fm;
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // Assert that m is inherited into ctxk, without intervening overrides.
0N/A // (May return true even if this is not true, in corner cases where we punt.)
0N/A bool check_method_context(klassOop ctxk, methodOop m) {
0N/A if (m->method_holder() == ctxk)
0N/A return true; // Quick win.
0N/A if (m->is_private())
0N/A return false; // Quick lose. Should not happen.
0N/A if (!(m->is_public() || m->is_protected()))
0N/A // The override story is complex when packages get involved.
0N/A return true; // Must punt the assertion to true.
0N/A Klass* k = Klass::cast(ctxk);
0N/A methodOop lm = k->lookup_method(m->name(), m->signature());
0N/A if (lm == NULL && k->oop_is_instance()) {
0N/A // It might be an abstract interface method, devoid of mirandas.
0N/A lm = ((instanceKlass*)k)->lookup_method_in_all_interfaces(m->name(),
0N/A m->signature());
0N/A }
0N/A if (lm == m)
0N/A // Method m is inherited into ctxk.
0N/A return true;
0N/A if (lm != NULL) {
2898N/A if (!(lm->is_public() || lm->is_protected())) {
0N/A // Method is [package-]private, so the override story is complex.
0N/A return true; // Must punt the assertion to true.
2898N/A }
2898N/A if (lm->is_static()) {
2898N/A // Static methods don't override non-static so punt
2898N/A return true;
2898N/A }
0N/A if ( !Dependencies::is_concrete_method(lm)
0N/A && !Dependencies::is_concrete_method(m)
0N/A && Klass::cast(lm->method_holder())->is_subtype_of(m->method_holder()))
0N/A // Method m is overridden by lm, but both are non-concrete.
0N/A return true;
0N/A }
0N/A ResourceMark rm;
0N/A tty->print_cr("Dependency method not found in the associated context:");
0N/A tty->print_cr(" context = %s", Klass::cast(ctxk)->external_name());
0N/A tty->print( " method = "); m->print_short_name(tty); tty->cr();
0N/A if (lm != NULL) {
0N/A tty->print( " found = "); lm->print_short_name(tty); tty->cr();
0N/A }
0N/A return false;
0N/A }
0N/A#endif
0N/A
0N/A void add_participant(klassOop participant) {
0N/A assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob");
0N/A int np = _num_participants++;
0N/A _participants[np] = participant;
0N/A _participants[np+1] = NULL;
0N/A _found_methods[np+1] = NULL;
0N/A }
0N/A
0N/A void record_witnesses(int add) {
0N/A if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT;
0N/A assert(_num_participants + add < PARTICIPANT_LIMIT, "oob");
0N/A _record_witnesses = add;
0N/A }
0N/A
0N/A bool is_witness(klassOop k) {
0N/A if (doing_subtype_search()) {
0N/A return Dependencies::is_concrete_klass(k);
0N/A } else {
0N/A methodOop m = instanceKlass::cast(k)->find_method(_name, _signature);
0N/A if (m == NULL || !Dependencies::is_concrete_method(m)) return false;
0N/A _found_methods[_num_participants] = m;
0N/A // Note: If add_participant(k) is called,
0N/A // the method m will already be memoized for it.
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A bool is_participant(klassOop k) {
0N/A if (k == _participants[0]) {
0N/A return true;
0N/A } else if (_num_participants <= 1) {
0N/A return false;
0N/A } else {
0N/A return in_list(k, &_participants[1]);
0N/A }
0N/A }
0N/A bool ignore_witness(klassOop witness) {
0N/A if (_record_witnesses == 0) {
0N/A return false;
0N/A } else {
0N/A --_record_witnesses;
0N/A add_participant(witness);
0N/A return true;
0N/A }
0N/A }
0N/A static bool in_list(klassOop x, klassOop* list) {
0N/A for (int i = 0; ; i++) {
0N/A klassOop y = list[i];
0N/A if (y == NULL) break;
0N/A if (y == x) return true;
0N/A }
0N/A return false; // not in list
0N/A }
0N/A
0N/A private:
0N/A // the actual search method:
0N/A klassOop find_witness_anywhere(klassOop context_type,
0N/A bool participants_hide_witnesses,
0N/A bool top_level_call = true);
0N/A // the spot-checking version:
2677N/A klassOop find_witness_in(KlassDepChange& changes,
0N/A klassOop context_type,
0N/A bool participants_hide_witnesses);
0N/A public:
2677N/A klassOop find_witness_subtype(klassOop context_type, KlassDepChange* changes = NULL) {
0N/A assert(doing_subtype_search(), "must set up a subtype search");
0N/A // When looking for unexpected concrete types,
0N/A // do not look beneath expected ones.
0N/A const bool participants_hide_witnesses = true;
0N/A // CX > CC > C' is OK, even if C' is new.
0N/A // CX > { CC, C' } is not OK if C' is new, and C' is the witness.
0N/A if (changes != NULL) {
0N/A return find_witness_in(*changes, context_type, participants_hide_witnesses);
0N/A } else {
0N/A return find_witness_anywhere(context_type, participants_hide_witnesses);
0N/A }
0N/A }
2677N/A klassOop find_witness_definer(klassOop context_type, KlassDepChange* changes = NULL) {
0N/A assert(!doing_subtype_search(), "must set up a method definer search");
0N/A // When looking for unexpected concrete methods,
0N/A // look beneath expected ones, to see if there are overrides.
0N/A const bool participants_hide_witnesses = true;
0N/A // CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness.
0N/A if (changes != NULL) {
0N/A return find_witness_in(*changes, context_type, !participants_hide_witnesses);
0N/A } else {
0N/A return find_witness_anywhere(context_type, !participants_hide_witnesses);
0N/A }
0N/A }
0N/A};
0N/A
0N/A#ifndef PRODUCT
0N/Astatic int deps_find_witness_calls = 0;
0N/Astatic int deps_find_witness_steps = 0;
0N/Astatic int deps_find_witness_recursions = 0;
0N/Astatic int deps_find_witness_singles = 0;
0N/Astatic int deps_find_witness_print = 0; // set to -1 to force a final print
0N/Astatic bool count_find_witness_calls() {
0N/A if (TraceDependencies || LogCompilation) {
0N/A int pcount = deps_find_witness_print + 1;
0N/A bool final_stats = (pcount == 0);
0N/A bool initial_call = (pcount == 1);
0N/A bool occasional_print = ((pcount & ((1<<10) - 1)) == 0);
0N/A if (pcount < 0) pcount = 1; // crude overflow protection
0N/A deps_find_witness_print = pcount;
0N/A if (VerifyDependencies && initial_call) {
0N/A tty->print_cr("Warning: TraceDependencies results may be inflated by VerifyDependencies");
0N/A }
0N/A if (occasional_print || final_stats) {
0N/A // Every now and then dump a little info about dependency searching.
0N/A if (xtty != NULL) {
1206N/A ttyLocker ttyl;
1206N/A xtty->elem("deps_find_witness calls='%d' steps='%d' recursions='%d' singles='%d'",
0N/A deps_find_witness_calls,
0N/A deps_find_witness_steps,
0N/A deps_find_witness_recursions,
0N/A deps_find_witness_singles);
0N/A }
0N/A if (final_stats || (TraceDependencies && WizardMode)) {
1206N/A ttyLocker ttyl;
0N/A tty->print_cr("Dependency check (find_witness) "
0N/A "calls=%d, steps=%d (avg=%.1f), recursions=%d, singles=%d",
0N/A deps_find_witness_calls,
0N/A deps_find_witness_steps,
0N/A (double)deps_find_witness_steps / deps_find_witness_calls,
0N/A deps_find_witness_recursions,
0N/A deps_find_witness_singles);
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A return false;
0N/A}
0N/A#else
0N/A#define count_find_witness_calls() (0)
0N/A#endif //PRODUCT
0N/A
0N/A
2677N/AklassOop ClassHierarchyWalker::find_witness_in(KlassDepChange& changes,
0N/A klassOop context_type,
0N/A bool participants_hide_witnesses) {
0N/A assert(changes.involves_context(context_type), "irrelevant dependency");
0N/A klassOop new_type = changes.new_type();
0N/A
0N/A count_find_witness_calls();
0N/A NOT_PRODUCT(deps_find_witness_singles++);
0N/A
0N/A // Current thread must be in VM (not native mode, as in CI):
0N/A assert(must_be_in_vm(), "raw oops here");
0N/A // Must not move the class hierarchy during this check:
0N/A assert_locked_or_safepoint(Compile_lock);
0N/A
30N/A int nof_impls = instanceKlass::cast(context_type)->nof_implementors();
30N/A if (nof_impls > 1) {
30N/A // Avoid this case: *I.m > { A.m, C }; B.m > C
30N/A // %%% Until this is fixed more systematically, bail out.
30N/A // See corresponding comment in find_witness_anywhere.
30N/A return context_type;
30N/A }
30N/A
0N/A assert(!is_participant(new_type), "only old classes are participants");
0N/A if (participants_hide_witnesses) {
0N/A // If the new type is a subtype of a participant, we are done.
0N/A for (int i = 0; i < num_participants(); i++) {
0N/A klassOop part = participant(i);
0N/A if (part == NULL) continue;
0N/A assert(changes.involves_context(part) == Klass::cast(new_type)->is_subtype_of(part),
0N/A "correct marking of participants, b/c new_type is unique");
0N/A if (changes.involves_context(part)) {
0N/A // new guy is protected from this check by previous participant
0N/A return NULL;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (is_witness(new_type) &&
0N/A !ignore_witness(new_type)) {
0N/A return new_type;
0N/A }
0N/A
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/A// Walk hierarchy under a context type, looking for unexpected types.
0N/A// Do not report participant types, and recursively walk beneath
0N/A// them only if participants_hide_witnesses is false.
0N/A// If top_level_call is false, skip testing the context type,
0N/A// because the caller has already considered it.
0N/AklassOop ClassHierarchyWalker::find_witness_anywhere(klassOop context_type,
0N/A bool participants_hide_witnesses,
0N/A bool top_level_call) {
0N/A // Current thread must be in VM (not native mode, as in CI):
0N/A assert(must_be_in_vm(), "raw oops here");
0N/A // Must not move the class hierarchy during this check:
0N/A assert_locked_or_safepoint(Compile_lock);
0N/A
0N/A bool do_counts = count_find_witness_calls();
0N/A
0N/A // Check the root of the sub-hierarchy first.
0N/A if (top_level_call) {
0N/A if (do_counts) {
0N/A NOT_PRODUCT(deps_find_witness_calls++);
0N/A NOT_PRODUCT(deps_find_witness_steps++);
0N/A }
0N/A if (is_participant(context_type)) {
0N/A if (participants_hide_witnesses) return NULL;
0N/A // else fall through to search loop...
0N/A } else if (is_witness(context_type) && !ignore_witness(context_type)) {
0N/A // The context is an abstract class or interface, to start with.
0N/A return context_type;
0N/A }
0N/A }
0N/A
0N/A // Now we must check each implementor and each subclass.
0N/A // Use a short worklist to avoid blowing the stack.
0N/A // Each worklist entry is a *chain* of subklass siblings to process.
0N/A const int CHAINMAX = 100; // >= 1 + instanceKlass::implementors_limit
0N/A Klass* chains[CHAINMAX];
0N/A int chaini = 0; // index into worklist
0N/A Klass* chain; // scratch variable
0N/A#define ADD_SUBCLASS_CHAIN(k) { \
0N/A assert(chaini < CHAINMAX, "oob"); \
0N/A chain = instanceKlass::cast(k)->subklass(); \
0N/A if (chain != NULL) chains[chaini++] = chain; }
0N/A
0N/A // Look for non-abstract subclasses.
0N/A // (Note: Interfaces do not have subclasses.)
0N/A ADD_SUBCLASS_CHAIN(context_type);
0N/A
0N/A // If it is an interface, search its direct implementors.
0N/A // (Their subclasses are additional indirect implementors.
0N/A // See instanceKlass::add_implementor.)
0N/A // (Note: nof_implementors is always zero for non-interfaces.)
0N/A int nof_impls = instanceKlass::cast(context_type)->nof_implementors();
0N/A if (nof_impls > 1) {
0N/A // Avoid this case: *I.m > { A.m, C }; B.m > C
0N/A // Here, I.m has 2 concrete implementations, but m appears unique
0N/A // as A.m, because the search misses B.m when checking C.
0N/A // The inherited method B.m was getting missed by the walker
0N/A // when interface 'I' was the starting point.
0N/A // %%% Until this is fixed more systematically, bail out.
0N/A // (Old CHA had the same limitation.)
0N/A return context_type;
0N/A }
3669N/A if (nof_impls > 0) {
3669N/A klassOop impl = instanceKlass::cast(context_type)->implementor();
3669N/A assert(impl != NULL, "just checking");
3669N/A // If impl is the same as the context_type, then more than one
3669N/A // implementor has seen. No exact info in this case.
3669N/A if (impl == context_type) {
0N/A return context_type; // report an inexact witness to this sad affair
0N/A }
0N/A if (do_counts)
0N/A { NOT_PRODUCT(deps_find_witness_steps++); }
0N/A if (is_participant(impl)) {
3669N/A if (!participants_hide_witnesses) {
3669N/A ADD_SUBCLASS_CHAIN(impl);
3669N/A }
0N/A } else if (is_witness(impl) && !ignore_witness(impl)) {
0N/A return impl;
3669N/A } else {
3669N/A ADD_SUBCLASS_CHAIN(impl);
0N/A }
0N/A }
0N/A
0N/A // Recursively process each non-trivial sibling chain.
0N/A while (chaini > 0) {
0N/A Klass* chain = chains[--chaini];
0N/A for (Klass* subk = chain; subk != NULL; subk = subk->next_sibling()) {
0N/A klassOop sub = subk->as_klassOop();
0N/A if (do_counts) { NOT_PRODUCT(deps_find_witness_steps++); }
0N/A if (is_participant(sub)) {
0N/A if (participants_hide_witnesses) continue;
0N/A // else fall through to process this guy's subclasses
0N/A } else if (is_witness(sub) && !ignore_witness(sub)) {
0N/A return sub;
0N/A }
0N/A if (chaini < (VerifyDependencies? 2: CHAINMAX)) {
0N/A // Fast path. (Partially disabled if VerifyDependencies.)
0N/A ADD_SUBCLASS_CHAIN(sub);
0N/A } else {
0N/A // Worklist overflow. Do a recursive call. Should be rare.
0N/A // The recursive call will have its own worklist, of course.
0N/A // (Note that sub has already been tested, so that there is
0N/A // no need for the recursive call to re-test. That's handy,
0N/A // since the recursive call sees sub as the context_type.)
0N/A if (do_counts) { NOT_PRODUCT(deps_find_witness_recursions++); }
0N/A klassOop witness = find_witness_anywhere(sub,
0N/A participants_hide_witnesses,
0N/A /*top_level_call=*/ false);
0N/A if (witness != NULL) return witness;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // No witness found. The dependency remains unbroken.
0N/A return NULL;
0N/A#undef ADD_SUBCLASS_CHAIN
0N/A}
0N/A
0N/A
0N/Abool Dependencies::is_concrete_klass(klassOop k) {
0N/A if (Klass::cast(k)->is_abstract()) return false;
0N/A // %%% We could treat classes which are concrete but
0N/A // have not yet been instantiated as virtually abstract.
0N/A // This would require a deoptimization barrier on first instantiation.
0N/A //if (k->is_not_instantiated()) return false;
0N/A return true;
0N/A}
0N/A
0N/Abool Dependencies::is_concrete_method(methodOop m) {
2898N/A // Statics are irrelevant to virtual call sites.
2898N/A if (m->is_static()) return false;
2898N/A
2898N/A // We could also return false if m does not yet appear to be
2898N/A // executed, if the VM version supports this distinction also.
0N/A return !m->is_abstract();
0N/A}
0N/A
0N/A
0N/AKlass* Dependencies::find_finalizable_subclass(Klass* k) {
0N/A if (k->is_interface()) return NULL;
0N/A if (k->has_finalizer()) return k;
0N/A k = k->subklass();
0N/A while (k != NULL) {
0N/A Klass* result = find_finalizable_subclass(k);
0N/A if (result != NULL) return result;
0N/A k = k->next_sibling();
0N/A }
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/Abool Dependencies::is_concrete_klass(ciInstanceKlass* k) {
0N/A if (k->is_abstract()) return false;
2898N/A // We could also return false if k does not yet appear to be
0N/A // instantiated, if the VM version supports this distinction also.
0N/A //if (k->is_not_instantiated()) return false;
0N/A return true;
0N/A}
0N/A
0N/Abool Dependencies::is_concrete_method(ciMethod* m) {
0N/A // Statics are irrelevant to virtual call sites.
0N/A if (m->is_static()) return false;
0N/A
2898N/A // We could also return false if m does not yet appear to be
0N/A // executed, if the VM version supports this distinction also.
0N/A return !m->is_abstract();
0N/A}
0N/A
0N/A
0N/Abool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) {
0N/A return k->has_finalizable_subclass();
0N/A}
0N/A
0N/A
0N/A// Any use of the contents (bytecodes) of a method must be
0N/A// marked by an "evol_method" dependency, if those contents
0N/A// can change. (Note: A method is always dependent on itself.)
0N/AklassOop Dependencies::check_evol_method(methodOop m) {
0N/A assert(must_be_in_vm(), "raw oops here");
0N/A // Did somebody do a JVMTI RedefineClasses while our backs were turned?
0N/A // Or is there a now a breakpoint?
0N/A // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.)
0N/A if (m->is_old()
0N/A || m->number_of_breakpoints() > 0) {
0N/A return m->method_holder();
0N/A } else {
0N/A return NULL;
0N/A }
0N/A}
0N/A
0N/A// This is a strong assertion: It is that the given type
0N/A// has no subtypes whatever. It is most useful for
0N/A// optimizing checks on reflected types or on array types.
0N/A// (Checks on types which are derived from real instances
0N/A// can be optimized more strongly than this, because we
0N/A// know that the checked type comes from a concrete type,
0N/A// and therefore we can disregard abstract types.)
0N/AklassOop Dependencies::check_leaf_type(klassOop ctxk) {
0N/A assert(must_be_in_vm(), "raw oops here");
0N/A assert_locked_or_safepoint(Compile_lock);
0N/A instanceKlass* ctx = instanceKlass::cast(ctxk);
0N/A Klass* sub = ctx->subklass();
0N/A if (sub != NULL) {
0N/A return sub->as_klassOop();
0N/A } else if (ctx->nof_implementors() != 0) {
0N/A // if it is an interface, it must be unimplemented
0N/A // (if it is not an interface, nof_implementors is always zero)
3669N/A klassOop impl = ctx->implementor();
3669N/A assert(impl != NULL, "must be set");
3669N/A return impl;
0N/A } else {
0N/A return NULL;
0N/A }
0N/A}
0N/A
0N/A// Test the assertion that conck is the only concrete subtype* of ctxk.
0N/A// The type conck itself is allowed to have have further concrete subtypes.
0N/A// This allows the compiler to narrow occurrences of ctxk by conck,
0N/A// when dealing with the types of actual instances.
0N/AklassOop Dependencies::check_abstract_with_unique_concrete_subtype(klassOop ctxk,
0N/A klassOop conck,
2677N/A KlassDepChange* changes) {
0N/A ClassHierarchyWalker wf(conck);
0N/A return wf.find_witness_subtype(ctxk, changes);
0N/A}
0N/A
0N/A// If a non-concrete class has no concrete subtypes, it is not (yet)
0N/A// instantiatable. This can allow the compiler to make some paths go
0N/A// dead, if they are gated by a test of the type.
0N/AklassOop Dependencies::check_abstract_with_no_concrete_subtype(klassOop ctxk,
2677N/A KlassDepChange* changes) {
0N/A // Find any concrete subtype, with no participants:
0N/A ClassHierarchyWalker wf;
0N/A return wf.find_witness_subtype(ctxk, changes);
0N/A}
0N/A
0N/A
0N/A// If a concrete class has no concrete subtypes, it can always be
0N/A// exactly typed. This allows the use of a cheaper type test.
0N/AklassOop Dependencies::check_concrete_with_no_concrete_subtype(klassOop ctxk,
2677N/A KlassDepChange* changes) {
0N/A // Find any concrete subtype, with only the ctxk as participant:
0N/A ClassHierarchyWalker wf(ctxk);
0N/A return wf.find_witness_subtype(ctxk, changes);
0N/A}
0N/A
0N/A
0N/A// Find the unique concrete proper subtype of ctxk, or NULL if there
0N/A// is more than one concrete proper subtype. If there are no concrete
0N/A// proper subtypes, return ctxk itself, whether it is concrete or not.
0N/A// The returned subtype is allowed to have have further concrete subtypes.
0N/A// That is, return CC1 for CX > CC1 > CC2, but NULL for CX > { CC1, CC2 }.
0N/AklassOop Dependencies::find_unique_concrete_subtype(klassOop ctxk) {
0N/A ClassHierarchyWalker wf(ctxk); // Ignore ctxk when walking.
0N/A wf.record_witnesses(1); // Record one other witness when walking.
0N/A klassOop wit = wf.find_witness_subtype(ctxk);
0N/A if (wit != NULL) return NULL; // Too many witnesses.
0N/A klassOop conck = wf.participant(0);
0N/A if (conck == NULL) {
0N/A#ifndef PRODUCT
0N/A // Make sure the dependency mechanism will pass this discovery:
0N/A if (VerifyDependencies) {
0N/A // Turn off dependency tracing while actually testing deps.
0N/A FlagSetting fs(TraceDependencies, false);
0N/A if (!Dependencies::is_concrete_klass(ctxk)) {
0N/A guarantee(NULL ==
0N/A (void *)check_abstract_with_no_concrete_subtype(ctxk),
0N/A "verify dep.");
0N/A } else {
0N/A guarantee(NULL ==
0N/A (void *)check_concrete_with_no_concrete_subtype(ctxk),
0N/A "verify dep.");
0N/A }
0N/A }
0N/A#endif //PRODUCT
0N/A return ctxk; // Return ctxk as a flag for "no subtypes".
0N/A } else {
0N/A#ifndef PRODUCT
0N/A // Make sure the dependency mechanism will pass this discovery:
0N/A if (VerifyDependencies) {
0N/A // Turn off dependency tracing while actually testing deps.
0N/A FlagSetting fs(TraceDependencies, false);
0N/A if (!Dependencies::is_concrete_klass(ctxk)) {
0N/A guarantee(NULL == (void *)
0N/A check_abstract_with_unique_concrete_subtype(ctxk, conck),
0N/A "verify dep.");
0N/A }
0N/A }
0N/A#endif //PRODUCT
0N/A return conck;
0N/A }
0N/A}
0N/A
0N/A// Test the assertion that the k[12] are the only concrete subtypes of ctxk,
0N/A// except possibly for further subtypes of k[12] themselves.
0N/A// The context type must be abstract. The types k1 and k2 are themselves
0N/A// allowed to have further concrete subtypes.
0N/AklassOop Dependencies::check_abstract_with_exclusive_concrete_subtypes(
0N/A klassOop ctxk,
0N/A klassOop k1,
0N/A klassOop k2,
2677N/A KlassDepChange* changes) {
0N/A ClassHierarchyWalker wf;
0N/A wf.add_participant(k1);
0N/A wf.add_participant(k2);
0N/A return wf.find_witness_subtype(ctxk, changes);
0N/A}
0N/A
0N/A// Search ctxk for concrete implementations. If there are klen or fewer,
0N/A// pack them into the given array and return the number.
0N/A// Otherwise, return -1, meaning the given array would overflow.
0N/A// (Note that a return of 0 means there are exactly no concrete subtypes.)
0N/A// In this search, if ctxk is concrete, it will be reported alone.
0N/A// For any type CC reported, no proper subtypes of CC will be reported.
0N/Aint Dependencies::find_exclusive_concrete_subtypes(klassOop ctxk,
0N/A int klen,
0N/A klassOop karray[]) {
0N/A ClassHierarchyWalker wf;
0N/A wf.record_witnesses(klen);
0N/A klassOop wit = wf.find_witness_subtype(ctxk);
0N/A if (wit != NULL) return -1; // Too many witnesses.
0N/A int num = wf.num_participants();
0N/A assert(num <= klen, "oob");
0N/A // Pack the result array with the good news.
0N/A for (int i = 0; i < num; i++)
0N/A karray[i] = wf.participant(i);
0N/A#ifndef PRODUCT
0N/A // Make sure the dependency mechanism will pass this discovery:
0N/A if (VerifyDependencies) {
0N/A // Turn off dependency tracing while actually testing deps.
0N/A FlagSetting fs(TraceDependencies, false);
0N/A switch (Dependencies::is_concrete_klass(ctxk)? -1: num) {
0N/A case -1: // ctxk was itself concrete
0N/A guarantee(num == 1 && karray[0] == ctxk, "verify dep.");
0N/A break;
0N/A case 0:
0N/A guarantee(NULL == (void *)check_abstract_with_no_concrete_subtype(ctxk),
0N/A "verify dep.");
0N/A break;
0N/A case 1:
0N/A guarantee(NULL == (void *)
0N/A check_abstract_with_unique_concrete_subtype(ctxk, karray[0]),
0N/A "verify dep.");
0N/A break;
0N/A case 2:
0N/A guarantee(NULL == (void *)
0N/A check_abstract_with_exclusive_concrete_subtypes(ctxk,
0N/A karray[0],
0N/A karray[1]),
0N/A "verify dep.");
0N/A break;
0N/A default:
0N/A ShouldNotReachHere(); // klen > 2 yet supported
0N/A }
0N/A }
0N/A#endif //PRODUCT
0N/A return num;
0N/A}
0N/A
0N/A// If a class (or interface) has a unique concrete method uniqm, return NULL.
0N/A// Otherwise, return a class that contains an interfering method.
0N/AklassOop Dependencies::check_unique_concrete_method(klassOop ctxk, methodOop uniqm,
2677N/A KlassDepChange* changes) {
0N/A // Here is a missing optimization: If uniqm->is_final(),
0N/A // we don't really need to search beneath it for overrides.
0N/A // This is probably not important, since we don't use dependencies
0N/A // to track final methods. (They can't be "definalized".)
0N/A ClassHierarchyWalker wf(uniqm->method_holder(), uniqm);
0N/A return wf.find_witness_definer(ctxk, changes);
0N/A}
0N/A
0N/A// Find the set of all non-abstract methods under ctxk that match m.
0N/A// (The method m must be defined or inherited in ctxk.)
0N/A// Include m itself in the set, unless it is abstract.
0N/A// If this set has exactly one element, return that element.
0N/AmethodOop Dependencies::find_unique_concrete_method(klassOop ctxk, methodOop m) {
0N/A ClassHierarchyWalker wf(m);
0N/A assert(wf.check_method_context(ctxk, m), "proper context");
0N/A wf.record_witnesses(1);
0N/A klassOop wit = wf.find_witness_definer(ctxk);
0N/A if (wit != NULL) return NULL; // Too many witnesses.
0N/A methodOop fm = wf.found_method(0); // Will be NULL if num_parts == 0.
0N/A if (Dependencies::is_concrete_method(m)) {
0N/A if (fm == NULL) {
0N/A // It turns out that m was always the only implementation.
0N/A fm = m;
0N/A } else if (fm != m) {
0N/A // Two conflicting implementations after all.
0N/A // (This can happen if m is inherited into ctxk and fm overrides it.)
0N/A return NULL;
0N/A }
0N/A }
0N/A#ifndef PRODUCT
0N/A // Make sure the dependency mechanism will pass this discovery:
0N/A if (VerifyDependencies && fm != NULL) {
0N/A guarantee(NULL == (void *)check_unique_concrete_method(ctxk, fm),
0N/A "verify dep.");
0N/A }
0N/A#endif //PRODUCT
0N/A return fm;
0N/A}
0N/A
0N/AklassOop Dependencies::check_exclusive_concrete_methods(klassOop ctxk,
0N/A methodOop m1,
0N/A methodOop m2,
2677N/A KlassDepChange* changes) {
0N/A ClassHierarchyWalker wf(m1);
0N/A wf.add_participant(m1->method_holder());
0N/A wf.add_participant(m2->method_holder());
0N/A return wf.find_witness_definer(ctxk, changes);
0N/A}
0N/A
0N/A// Find the set of all non-abstract methods under ctxk that match m[0].
0N/A// (The method m[0] must be defined or inherited in ctxk.)
0N/A// Include m itself in the set, unless it is abstract.
0N/A// Fill the given array m[0..(mlen-1)] with this set, and return the length.
0N/A// (The length may be zero if no concrete methods are found anywhere.)
0N/A// If there are too many concrete methods to fit in marray, return -1.
0N/Aint Dependencies::find_exclusive_concrete_methods(klassOop ctxk,
0N/A int mlen,
0N/A methodOop marray[]) {
0N/A methodOop m0 = marray[0];
0N/A ClassHierarchyWalker wf(m0);
0N/A assert(wf.check_method_context(ctxk, m0), "proper context");
0N/A wf.record_witnesses(mlen);
0N/A bool participants_hide_witnesses = true;
0N/A klassOop wit = wf.find_witness_definer(ctxk);
0N/A if (wit != NULL) return -1; // Too many witnesses.
0N/A int num = wf.num_participants();
0N/A assert(num <= mlen, "oob");
0N/A // Keep track of whether m is also part of the result set.
0N/A int mfill = 0;
0N/A assert(marray[mfill] == m0, "sanity");
0N/A if (Dependencies::is_concrete_method(m0))
0N/A mfill++; // keep m0 as marray[0], the first result
0N/A for (int i = 0; i < num; i++) {
0N/A methodOop fm = wf.found_method(i);
0N/A if (fm == m0) continue; // Already put this guy in the list.
0N/A if (mfill == mlen) {
0N/A return -1; // Oops. Too many methods after all!
0N/A }
0N/A marray[mfill++] = fm;
0N/A }
0N/A#ifndef PRODUCT
0N/A // Make sure the dependency mechanism will pass this discovery:
0N/A if (VerifyDependencies) {
0N/A // Turn off dependency tracing while actually testing deps.
0N/A FlagSetting fs(TraceDependencies, false);
0N/A switch (mfill) {
0N/A case 1:
0N/A guarantee(NULL == (void *)check_unique_concrete_method(ctxk, marray[0]),
0N/A "verify dep.");
0N/A break;
0N/A case 2:
0N/A guarantee(NULL == (void *)
0N/A check_exclusive_concrete_methods(ctxk, marray[0], marray[1]),
0N/A "verify dep.");
0N/A break;
0N/A default:
0N/A ShouldNotReachHere(); // mlen > 2 yet supported
0N/A }
0N/A }
0N/A#endif //PRODUCT
0N/A return mfill;
0N/A}
0N/A
0N/A
2677N/AklassOop Dependencies::check_has_no_finalizable_subclasses(klassOop ctxk, KlassDepChange* changes) {
0N/A Klass* search_at = ctxk->klass_part();
0N/A if (changes != NULL)
0N/A search_at = changes->new_type()->klass_part(); // just look at the new bit
0N/A Klass* result = find_finalizable_subclass(search_at);
0N/A if (result == NULL) {
0N/A return NULL;
0N/A }
0N/A return result->as_klassOop();
0N/A}
0N/A
0N/A
2728N/AklassOop Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) {
2677N/A assert(call_site ->is_a(SystemDictionary::CallSite_klass()), "sanity");
2677N/A assert(method_handle->is_a(SystemDictionary::MethodHandle_klass()), "sanity");
2677N/A if (changes == NULL) {
2677N/A // Validate all CallSites
2677N/A if (java_lang_invoke_CallSite::target(call_site) != method_handle)
2728N/A return call_site->klass(); // assertion failed
2677N/A } else {
2677N/A // Validate the given CallSite
2677N/A if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) {
2677N/A assert(method_handle != changes->method_handle(), "must be");
2728N/A return call_site->klass(); // assertion failed
2677N/A }
2677N/A }
2677N/A return NULL; // assertion still valid
2677N/A}
2677N/A
2677N/A
2677N/Avoid Dependencies::DepStream::trace_and_log_witness(klassOop witness) {
2677N/A if (witness != NULL) {
2677N/A if (TraceDependencies) {
2677N/A print_dependency(witness, /*verbose=*/ true);
2677N/A }
2677N/A // The following is a no-op unless logging is enabled:
2677N/A log_dependency(witness);
2677N/A }
2677N/A}
2677N/A
2677N/A
2677N/AklassOop Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) {
0N/A assert_locked_or_safepoint(Compile_lock);
2677N/A Dependencies::check_valid_dependency_type(type());
0N/A
0N/A klassOop witness = NULL;
0N/A switch (type()) {
0N/A case evol_method:
0N/A witness = check_evol_method(method_argument(0));
0N/A break;
0N/A case leaf_type:
0N/A witness = check_leaf_type(context_type());
0N/A break;
0N/A case abstract_with_unique_concrete_subtype:
2677N/A witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes);
0N/A break;
0N/A case abstract_with_no_concrete_subtype:
2677N/A witness = check_abstract_with_no_concrete_subtype(context_type(), changes);
0N/A break;
0N/A case concrete_with_no_concrete_subtype:
2677N/A witness = check_concrete_with_no_concrete_subtype(context_type(), changes);
0N/A break;
0N/A case unique_concrete_method:
2677N/A witness = check_unique_concrete_method(context_type(), method_argument(1), changes);
0N/A break;
0N/A case abstract_with_exclusive_concrete_subtypes_2:
2677N/A witness = check_abstract_with_exclusive_concrete_subtypes(context_type(), type_argument(1), type_argument(2), changes);
0N/A break;
0N/A case exclusive_concrete_methods_2:
2677N/A witness = check_exclusive_concrete_methods(context_type(), method_argument(1), method_argument(2), changes);
0N/A break;
0N/A case no_finalizable_subclasses:
2677N/A witness = check_has_no_finalizable_subclasses(context_type(), changes);
0N/A break;
2677N/A default:
0N/A witness = NULL;
0N/A break;
0N/A }
2677N/A trace_and_log_witness(witness);
2677N/A return witness;
2677N/A}
2677N/A
2677N/A
2677N/AklassOop Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) {
2677N/A assert_locked_or_safepoint(Compile_lock);
2677N/A Dependencies::check_valid_dependency_type(type());
2677N/A
2677N/A klassOop witness = NULL;
2677N/A switch (type()) {
2677N/A case call_site_target_value:
2728N/A witness = check_call_site_target_value(argument(0), argument(1), changes);
2677N/A break;
2677N/A default:
2677N/A witness = NULL;
2677N/A break;
0N/A }
2677N/A trace_and_log_witness(witness);
0N/A return witness;
0N/A}
0N/A
0N/A
0N/AklassOop Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) {
2677N/A // Handle klass dependency
2677N/A if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type()))
2677N/A return check_klass_dependency(changes.as_klass_change());
0N/A
2677N/A // Handle CallSite dependency
2677N/A if (changes.is_call_site_change())
2677N/A return check_call_site_dependency(changes.as_call_site_change());
2677N/A
2677N/A // irrelevant dependency; skip it
2677N/A return NULL;
0N/A}
0N/A
0N/A
2677N/Avoid DepChange::print() {
2677N/A int nsup = 0, nint = 0;
0N/A for (ContextStream str(*this); str.next(); ) {
2677N/A klassOop k = str.klass();
2677N/A switch (str.change_type()) {
2677N/A case Change_new_type:
2677N/A tty->print_cr(" dependee = %s", instanceKlass::cast(k)->external_name());
2677N/A break;
2677N/A case Change_new_sub:
2677N/A if (!WizardMode) {
2677N/A ++nsup;
2677N/A } else {
2677N/A tty->print_cr(" context super = %s", instanceKlass::cast(k)->external_name());
2677N/A }
2677N/A break;
2677N/A case Change_new_impl:
2677N/A if (!WizardMode) {
2677N/A ++nint;
2677N/A } else {
2677N/A tty->print_cr(" context interface = %s", instanceKlass::cast(k)->external_name());
2677N/A }
2677N/A break;
2677N/A }
2677N/A }
2677N/A if (nsup + nint != 0) {
2677N/A tty->print_cr(" context supers = %d, interfaces = %d", nsup, nint);
0N/A }
0N/A}
0N/A
2677N/Avoid DepChange::ContextStream::start() {
2677N/A klassOop new_type = _changes.is_klass_change() ? _changes.as_klass_change()->new_type() : (klassOop) NULL;
2677N/A _change_type = (new_type == NULL ? NO_CHANGE : Start_Klass);
2677N/A _klass = new_type;
2677N/A _ti_base = NULL;
2677N/A _ti_index = 0;
2677N/A _ti_limit = 0;
0N/A}
0N/A
0N/Abool DepChange::ContextStream::next() {
0N/A switch (_change_type) {
0N/A case Start_Klass: // initial state; _klass is the new type
0N/A _ti_base = instanceKlass::cast(_klass)->transitive_interfaces();
0N/A _ti_index = 0;
0N/A _change_type = Change_new_type;
0N/A return true;
0N/A case Change_new_type:
0N/A // fall through:
0N/A _change_type = Change_new_sub;
0N/A case Change_new_sub:
54N/A // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277
54N/A {
54N/A _klass = instanceKlass::cast(_klass)->super();
54N/A if (_klass != NULL) {
54N/A return true;
54N/A }
0N/A }
0N/A // else set up _ti_limit and fall through:
0N/A _ti_limit = (_ti_base == NULL) ? 0 : _ti_base->length();
0N/A _change_type = Change_new_impl;
0N/A case Change_new_impl:
0N/A if (_ti_index < _ti_limit) {
0N/A _klass = klassOop( _ti_base->obj_at(_ti_index++) );
0N/A return true;
0N/A }
0N/A // fall through:
0N/A _change_type = NO_CHANGE; // iterator is exhausted
0N/A case NO_CHANGE:
0N/A break;
0N/A default:
0N/A ShouldNotReachHere();
0N/A }
0N/A return false;
0N/A}
0N/A
2677N/Avoid KlassDepChange::initialize() {
2677N/A // entire transaction must be under this lock:
2677N/A assert_lock_strong(Compile_lock);
2677N/A
2677N/A // Mark all dependee and all its superclasses
2677N/A // Mark transitive interfaces
0N/A for (ContextStream str(*this); str.next(); ) {
2677N/A klassOop d = str.klass();
2677N/A assert(!instanceKlass::cast(d)->is_marked_dependent(), "checking");
3052N/A instanceKlass::cast(d)->set_is_marked_dependent(true);
0N/A }
2677N/A}
2677N/A
2677N/AKlassDepChange::~KlassDepChange() {
2677N/A // Unmark all dependee and all its superclasses
2677N/A // Unmark transitive interfaces
2677N/A for (ContextStream str(*this); str.next(); ) {
2677N/A klassOop d = str.klass();
3052N/A instanceKlass::cast(d)->set_is_marked_dependent(false);
0N/A }
0N/A}
0N/A
2677N/Abool KlassDepChange::involves_context(klassOop k) {
2677N/A if (k == NULL || !Klass::cast(k)->oop_is_instance()) {
2677N/A return false;
2677N/A }
2677N/A instanceKlass* ik = instanceKlass::cast(k);
2677N/A bool is_contained = ik->is_marked_dependent();
2677N/A assert(is_contained == Klass::cast(new_type())->is_subtype_of(k),
2677N/A "correct marking of potential context types");
2677N/A return is_contained;
2677N/A}
2677N/A
0N/A#ifndef PRODUCT
0N/Avoid Dependencies::print_statistics() {
0N/A if (deps_find_witness_print != 0) {
0N/A // Call one final time, to flush out the data.
0N/A deps_find_witness_print = -1;
0N/A count_find_witness_calls();
0N/A }
0N/A}
0N/A#endif