annotate src/hotspot/share/code/compiledIC.cpp @ 49366:1956d0ec092a

8199319: Remove handles.inline.hpp include from reflectionUtils.hpp Reviewed-by: hseigel, dholmes
author stefank
date Thu, 08 Mar 2018 19:10:25 +0100
parents 4e82736053ae
children d6893a76c554
rev   line source
duke@1 1 /*
dlong@48859 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
stefank@7397 26 #include "classfile/systemDictionary.hpp"
stefank@7397 27 #include "code/codeCache.hpp"
stefank@7397 28 #include "code/compiledIC.hpp"
stefank@7397 29 #include "code/icBuffer.hpp"
stefank@7397 30 #include "code/nmethod.hpp"
stefank@7397 31 #include "code/vtableStubs.hpp"
stefank@7397 32 #include "interpreter/interpreter.hpp"
stefank@7397 33 #include "interpreter/linkResolver.hpp"
coleenp@13728 34 #include "memory/metadataFactory.hpp"
stefank@7397 35 #include "memory/oopFactory.hpp"
jprovino@37248 36 #include "memory/resourceArea.hpp"
hseigel@49345 37 #include "oops/method.inline.hpp"
stefank@7397 38 #include "oops/oop.inline.hpp"
coleenp@8076 39 #include "oops/symbol.hpp"
stefank@49366 40 #include "runtime/handles.inline.hpp"
stefank@7397 41 #include "runtime/icache.hpp"
stefank@7397 42 #include "runtime/sharedRuntime.hpp"
stefank@7397 43 #include "runtime/stubRoutines.hpp"
stefank@7397 44 #include "utilities/events.hpp"
duke@1 45
duke@1 46
duke@1 47 // Every time a compiled IC is changed or its type is being accessed,
duke@1 48 // either the CompiledIC_lock must be set or we must be at a safe point.
duke@1 49
duke@1 50 //-----------------------------------------------------------------------------
duke@1 51 // Low-level access to an inline cache. Private, since they might not be
duke@1 52 // MT-safe to use.
duke@1 53
coleenp@13728 54 void* CompiledIC::cached_value() const {
duke@1 55 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
coleenp@13728 56 assert (!is_optimized(), "an optimized virtual call does not have a cached metadata");
coleenp@13728 57
coleenp@13728 58 if (!is_in_transition_state()) {
kvn@42650 59 void* data = get_data();
coleenp@13728 60 // If we let the metadata value here be initialized to zero...
coleenp@13728 61 assert(data != NULL || Universe::non_oop_word() == NULL,
coleenp@13728 62 "no raw nulls in CompiledIC metadatas, because of patching races");
coleenp@13728 63 return (data == (void*)Universe::non_oop_word()) ? NULL : data;
coleenp@13728 64 } else {
coleenp@13728 65 return InlineCacheBuffer::cached_value_for((CompiledIC *)this);
coleenp@13728 66 }
coleenp@13728 67 }
coleenp@13728 68
coleenp@13728 69
coleenp@13728 70 void CompiledIC::internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder) {
coleenp@13728 71 assert(entry_point != NULL, "must set legal entry point");
coleenp@13728 72 assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
coleenp@13728 73 assert (!is_optimized() || cache == NULL, "an optimized virtual call does not have a cached metadata");
coleenp@13728 74 assert (cache == NULL || cache != (Metadata*)badOopVal, "invalid metadata");
coleenp@13728 75
coleenp@13728 76 assert(!is_icholder || is_icholder_entry(entry_point), "must be");
coleenp@13728 77
coleenp@13728 78 // Don't use ic_destination for this test since that forwards
coleenp@13728 79 // through ICBuffer instead of returning the actual current state of
coleenp@13728 80 // the CompiledIC.
kvn@42650 81 if (is_icholder_entry(_call->destination())) {
coleenp@13728 82 // When patching for the ICStub case the cached value isn't
coleenp@13728 83 // overwritten until the ICStub copied into the CompiledIC during
coleenp@13728 84 // the next safepoint. Make sure that the CompiledICHolder* is
coleenp@13728 85 // marked for release at this point since it won't be identifiable
coleenp@13728 86 // once the entry point is overwritten.
kvn@42650 87 InlineCacheBuffer::queue_for_release((CompiledICHolder*)get_data());
coleenp@13728 88 }
duke@1 89
duke@1 90 if (TraceCompiledIC) {
duke@1 91 tty->print(" ");
duke@1 92 print_compiled_ic();
drchase@24424 93 tty->print(" changing destination to " INTPTR_FORMAT, p2i(entry_point));
coleenp@13728 94 if (!is_optimized()) {
drchase@24424 95 tty->print(" changing cached %s to " INTPTR_FORMAT, is_icholder ? "icholder" : "metadata", p2i((address)cache));
coleenp@13728 96 }
coleenp@13728 97 if (is_icstub) {
coleenp@13728 98 tty->print(" (icstub)");
coleenp@13728 99 }
coleenp@13728 100 tty->cr();
duke@1 101 }
duke@1 102
coleenp@13728 103 {
stefank@25492 104 MutexLockerEx pl(SafepointSynchronize::is_at_safepoint() ? NULL : Patching_lock, Mutex::_no_safepoint_check_flag);
duke@1 105 #ifdef ASSERT
kvn@42650 106 CodeBlob* cb = CodeCache::find_blob_unsafe(_call->instruction_address());
rbackman@38133 107 assert(cb != NULL && cb->is_compiled(), "must be compiled");
duke@1 108 #endif
kvn@42650 109 _call->set_destination_mt_safe(entry_point);
stefank@25492 110 }
duke@1 111
coleenp@13728 112 if (is_optimized() || is_icstub) {
coleenp@13728 113 // Optimized call sites don't have a cache value and ICStub call
coleenp@13728 114 // sites only change the entry point. Changing the value in that
coleenp@13728 115 // case could lead to MT safety issues.
coleenp@13728 116 assert(cache == NULL, "must be null");
coleenp@13728 117 return;
coleenp@13728 118 }
coleenp@13728 119
coleenp@13728 120 if (cache == NULL) cache = (void*)Universe::non_oop_word();
coleenp@13728 121
kvn@42650 122 set_data((intptr_t)cache);
coleenp@13728 123 }
coleenp@13728 124
coleenp@13728 125
coleenp@13728 126 void CompiledIC::set_ic_destination(ICStub* stub) {
coleenp@13728 127 internal_set_ic_destination(stub->code_begin(), true, NULL, false);
coleenp@13728 128 }
coleenp@13728 129
coleenp@13728 130
duke@1 131
duke@1 132 address CompiledIC::ic_destination() const {
duke@1 133 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 134 if (!is_in_transition_state()) {
kvn@42650 135 return _call->destination();
duke@1 136 } else {
duke@1 137 return InlineCacheBuffer::ic_destination_for((CompiledIC *)this);
duke@1 138 }
duke@1 139 }
duke@1 140
duke@1 141
duke@1 142 bool CompiledIC::is_in_transition_state() const {
duke@1 143 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
kvn@42650 144 return InlineCacheBuffer::contains(_call->destination());;
duke@1 145 }
duke@1 146
duke@1 147
coleenp@13728 148 bool CompiledIC::is_icholder_call() const {
coleenp@13728 149 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
coleenp@13728 150 return !_is_optimized && is_icholder_entry(ic_destination());
coleenp@13728 151 }
coleenp@13728 152
duke@1 153 // Returns native address of 'call' instruction in inline-cache. Used by
duke@1 154 // the InlineCacheBuffer when it needs to find the stub.
duke@1 155 address CompiledIC::stub_address() const {
duke@1 156 assert(is_in_transition_state(), "should only be called when we are in a transition state");
kvn@42650 157 return _call->destination();
duke@1 158 }
duke@1 159
thartmann@26912 160 // Clears the IC stub if the compiled IC is in transition state
thartmann@26912 161 void CompiledIC::clear_ic_stub() {
thartmann@26912 162 if (is_in_transition_state()) {
thartmann@26912 163 ICStub* stub = ICStub_from_destination_address(stub_address());
thartmann@26912 164 stub->clear();
thartmann@26912 165 }
thartmann@26912 166 }
thartmann@26912 167
duke@1 168 //-----------------------------------------------------------------------------
duke@1 169 // High-level access to an inline cache. Guaranteed to be MT-safe.
duke@1 170
stefank@25486 171 void CompiledIC::initialize_from_iter(RelocIterator* iter) {
kvn@42650 172 assert(iter->addr() == _call->instruction_address(), "must find ic_call");
stefank@25486 173
stefank@25486 174 if (iter->type() == relocInfo::virtual_call_type) {
stefank@25486 175 virtual_call_Relocation* r = iter->virtual_call_reloc();
stefank@25486 176 _is_optimized = false;
kvn@42650 177 _value = _call->get_load_instruction(r);
stefank@25486 178 } else {
stefank@25486 179 assert(iter->type() == relocInfo::opt_virtual_call_type, "must be a virtual call");
stefank@25486 180 _is_optimized = true;
stefank@25486 181 _value = NULL;
stefank@25486 182 }
stefank@25486 183 }
stefank@25486 184
rbackman@38133 185 CompiledIC::CompiledIC(CompiledMethod* cm, NativeCall* call)
kvn@42650 186 : _method(cm)
stefank@25359 187 {
kvn@42650 188 _call = _method->call_wrapper_at((address) call);
kvn@42650 189 address ic_call = _call->instruction_address();
stefank@25359 190
stefank@25359 191 assert(ic_call != NULL, "ic_call address must be set");
rbackman@38133 192 assert(cm != NULL, "must pass compiled method");
rbackman@38133 193 assert(cm->contains(ic_call), "must be in compiled method");
stefank@25359 194
stefank@25359 195 // Search for the ic_call at the given address.
rbackman@38133 196 RelocIterator iter(cm, ic_call, ic_call+1);
stefank@25359 197 bool ret = iter.next();
stefank@25359 198 assert(ret == true, "relocInfo must exist at this address");
stefank@25359 199 assert(iter.addr() == ic_call, "must find ic_call");
stefank@25486 200
stefank@25486 201 initialize_from_iter(&iter);
stefank@25486 202 }
stefank@25486 203
stefank@25486 204 CompiledIC::CompiledIC(RelocIterator* iter)
kvn@42650 205 : _method(iter->code())
stefank@25486 206 {
kvn@42650 207 _call = _method->call_wrapper_at(iter->addr());
kvn@42650 208 address ic_call = _call->instruction_address();
stefank@25486 209
rbackman@38133 210 CompiledMethod* nm = iter->code();
stefank@25486 211 assert(ic_call != NULL, "ic_call address must be set");
rbackman@38133 212 assert(nm != NULL, "must pass compiled method");
rbackman@38133 213 assert(nm->contains(ic_call), "must be in compiled method");
stefank@25486 214
stefank@25486 215 initialize_from_iter(iter);
stefank@25359 216 }
duke@1 217
anoll@20072 218 bool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS) {
duke@1 219 assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 220 assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic");
duke@1 221 assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?");
duke@1 222
duke@1 223 address entry;
drchase@20017 224 if (call_info->call_kind() == CallInfo::itable_call) {
drchase@20017 225 assert(bytecode == Bytecodes::_invokeinterface, "");
drchase@20017 226 int itable_index = call_info->itable_index();
drchase@20017 227 entry = VtableStubs::find_itable_stub(itable_index);
gziemski@48825 228 if (entry == NULL) {
anoll@20072 229 return false;
anoll@20072 230 }
drchase@20017 231 #ifdef ASSERT
drchase@20017 232 int index = call_info->resolved_method()->itable_index();
drchase@20017 233 assert(index == itable_index, "CallInfo pre-computes this");
coleenp@14391 234 InstanceKlass* k = call_info->resolved_method()->method_holder();
drchase@20017 235 assert(k->verify_itable_index(itable_index), "sanity check");
vlivanov@48558 236 #endif //ASSERT
vlivanov@48558 237 CompiledICHolder* holder = new CompiledICHolder(call_info->resolved_method()->method_holder(),
vlivanov@48558 238 call_info->resolved_klass());
vlivanov@48558 239 holder->claim();
vlivanov@48558 240 InlineCacheBuffer::create_transition_stub(this, holder, entry);
duke@1 241 } else {
drchase@20017 242 assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable");
drchase@20017 243 // Can be different than selected_method->vtable_index(), due to package-private etc.
duke@1 244 int vtable_index = call_info->vtable_index();
drchase@20017 245 assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
drchase@20017 246 entry = VtableStubs::find_vtable_stub(vtable_index);
anoll@20072 247 if (entry == NULL) {
anoll@20072 248 return false;
anoll@20072 249 }
drchase@20017 250 InlineCacheBuffer::create_transition_stub(this, NULL, entry);
duke@1 251 }
duke@1 252
duke@1 253 if (TraceICs) {
duke@1 254 ResourceMark rm;
duke@1 255 tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
drchase@24424 256 p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
duke@1 257 }
duke@1 258
duke@1 259 // We can't check this anymore. With lazy deopt we could have already
duke@1 260 // cleaned this IC entry before we even return. This is possible if
duke@1 261 // we ran out of space in the inline cache buffer trying to do the
duke@1 262 // set_next and we safepointed to free up space. This is a benign
duke@1 263 // race because the IC entry was complete when we safepointed so
duke@1 264 // cleaning it immediately is harmless.
duke@1 265 // assert(is_megamorphic(), "sanity check");
anoll@20072 266 return true;
duke@1 267 }
duke@1 268
duke@1 269
duke@1 270 // true if destination is megamorphic stub
duke@1 271 bool CompiledIC::is_megamorphic() const {
duke@1 272 assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 273 assert(!is_optimized(), "an optimized call cannot be megamorphic");
duke@1 274
coleenp@13728 275 // Cannot rely on cached_value. It is either an interface or a method.
duke@1 276 return VtableStubs::is_entry_point(ic_destination());
duke@1 277 }
duke@1 278
duke@1 279 bool CompiledIC::is_call_to_compiled() const {
duke@1 280 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 281
duke@1 282 // Use unsafe, since an inline cache might point to a zombie method. However, the zombie
duke@1 283 // method is guaranteed to still exist, since we only remove methods after all inline caches
duke@1 284 // has been cleaned up
duke@1 285 CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
rbackman@38133 286 bool is_monomorphic = (cb != NULL && cb->is_compiled());
coleenp@13728 287 // Check that the cached_value is a klass for non-optimized monomorphic calls
duke@1 288 // This assertion is invalid for compiler1: a call that does not look optimized (no static stub) can be used
twisti@33160 289 // for calling directly to vep without using the inline cache (i.e., cached_value == NULL).
twisti@33160 290 // For JVMCI this occurs because CHA is only used to improve inlining so call sites which could be optimized
twisti@33160 291 // virtuals because there are no currently loaded subclasses of a type are left as virtual call sites.
duke@1 292 #ifdef ASSERT
duke@1 293 CodeBlob* caller = CodeCache::find_blob_unsafe(instruction_address());
twisti@33160 294 bool is_c1_or_jvmci_method = caller->is_compiled_by_c1() || caller->is_compiled_by_jvmci();
twisti@33160 295 assert( is_c1_or_jvmci_method ||
duke@1 296 !is_monomorphic ||
duke@1 297 is_optimized() ||
thartmann@32466 298 !caller->is_alive() ||
coleenp@13728 299 (cached_metadata() != NULL && cached_metadata()->is_klass()), "sanity check");
duke@1 300 #endif // ASSERT
duke@1 301 return is_monomorphic;
duke@1 302 }
duke@1 303
duke@1 304
duke@1 305 bool CompiledIC::is_call_to_interpreted() const {
duke@1 306 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 307 // Call to interpreter if destination is either calling to a stub (if it
duke@1 308 // is optimized), or calling to an I2C blob
duke@1 309 bool is_call_to_interpreted = false;
duke@1 310 if (!is_optimized()) {
duke@1 311 // must use unsafe because the destination can be a zombie (and we're cleaning)
duke@1 312 // and the print_compiled_ic code wants to know if site (in the non-zombie)
duke@1 313 // is to the interpreter.
duke@1 314 CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination());
duke@1 315 is_call_to_interpreted = (cb != NULL && cb->is_adapter_blob());
coleenp@13728 316 assert(!is_call_to_interpreted || (is_icholder_call() && cached_icholder() != NULL), "sanity check");
duke@1 317 } else {
duke@1 318 // Check if we are calling into our own codeblob (i.e., to a stub)
duke@1 319 address dest = ic_destination();
duke@1 320 #ifdef ASSERT
duke@1 321 {
kvn@42650 322 _call->verify_resolve_call(dest);
duke@1 323 }
duke@1 324 #endif /* ASSERT */
kvn@42650 325 is_call_to_interpreted = _call->is_call_to_interpreted(dest);
duke@1 326 }
duke@1 327 return is_call_to_interpreted;
duke@1 328 }
duke@1 329
thartmann@32466 330 void CompiledIC::set_to_clean(bool in_use) {
duke@1 331 assert(SafepointSynchronize::is_at_safepoint() || CompiledIC_lock->is_locked() , "MT-unsafe call");
duke@1 332 if (TraceInlineCacheClearing || TraceICs) {
drchase@24424 333 tty->print_cr("IC@" INTPTR_FORMAT ": set to clean", p2i(instruction_address()));
duke@1 334 print();
duke@1 335 }
duke@1 336
kvn@42650 337 address entry = _call->get_resolve_call_stub(is_optimized());
duke@1 338
coleenp@13728 339 // A zombie transition will always be safe, since the metadata has already been set to NULL, so
duke@1 340 // we only need to patch the destination
kvn@42650 341 bool safe_transition = _call->is_safe_for_patching() || !in_use || is_optimized() || SafepointSynchronize::is_at_safepoint();
duke@1 342
duke@1 343 if (safe_transition) {
duke@1 344 // Kill any leftover stub we might have too
thartmann@26912 345 clear_ic_stub();
coleenp@13728 346 if (is_optimized()) {
thartmann@32401 347 set_ic_destination(entry);
thartmann@32401 348 } else {
coleenp@13728 349 set_ic_destination_and_value(entry, (void*)NULL);
coleenp@13728 350 }
coleenp@13728 351 } else {
duke@1 352 // Unsafe transition - create stub.
duke@1 353 InlineCacheBuffer::create_transition_stub(this, NULL, entry);
duke@1 354 }
duke@1 355 // We can't check this anymore. With lazy deopt we could have already
duke@1 356 // cleaned this IC entry before we even return. This is possible if
duke@1 357 // we ran out of space in the inline cache buffer trying to do the
duke@1 358 // set_next and we safepointed to free up space. This is a benign
duke@1 359 // race because the IC entry was complete when we safepointed so
duke@1 360 // cleaning it immediately is harmless.
duke@1 361 // assert(is_clean(), "sanity check");
duke@1 362 }
duke@1 363
duke@1 364 bool CompiledIC::is_clean() const {
duke@1 365 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 366 bool is_clean = false;
duke@1 367 address dest = ic_destination();
kvn@42650 368 is_clean = dest == _call->get_resolve_call_stub(is_optimized());
coleenp@13728 369 assert(!is_clean || is_optimized() || cached_value() == NULL, "sanity check");
duke@1 370 return is_clean;
duke@1 371 }
duke@1 372
coleenp@13728 373 void CompiledIC::set_to_monomorphic(CompiledICInfo& info) {
duke@1 374 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "");
duke@1 375 // Updating a cache to the wrong entry can cause bugs that are very hard
duke@1 376 // to track down - if cache entry gets invalid - we just clean it. In
duke@1 377 // this way it is always the same code path that is responsible for
duke@1 378 // updating and resolving an inline cache
duke@1 379 //
duke@1 380 // The above is no longer true. SharedRuntime::fixup_callers_callsite will change optimized
duke@1 381 // callsites. In addition ic_miss code will update a site to monomorphic if it determines
duke@1 382 // that an monomorphic call to the interpreter can now be monomorphic to compiled code.
duke@1 383 //
duke@1 384 // In both of these cases the only thing being modifed is the jump/call target and these
duke@1 385 // transitions are mt_safe
duke@1 386
duke@1 387 Thread *thread = Thread::current();
kvn@42650 388 if (info.to_interpreter() || info.to_aot()) {
duke@1 389 // Call to interpreter
duke@1 390 if (info.is_optimized() && is_optimized()) {
duke@1 391 assert(is_clean(), "unsafe IC path");
duke@1 392 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
duke@1 393 // the call analysis (callee structure) specifies that the call is optimized
duke@1 394 // (either because of CHA or the static target is final)
duke@1 395 // At code generation time, this call has been emitted as static call
duke@1 396 // Call via stub
coleenp@13728 397 assert(info.cached_metadata() != NULL && info.cached_metadata()->is_method(), "sanity check");
coleenp@13728 398 methodHandle method (thread, (Method*)info.cached_metadata());
kvn@42650 399 _call->set_to_interpreted(method, info);
kvn@42650 400
duke@1 401 if (TraceICs) {
duke@1 402 ResourceMark rm(thread);
kvn@42650 403 tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to %s: %s",
drchase@24424 404 p2i(instruction_address()),
kvn@42650 405 (info.to_aot() ? "aot" : "interpreter"),
duke@1 406 method->print_value_string());
duke@1 407 }
duke@1 408 } else {
duke@1 409 // Call via method-klass-holder
coleenp@13728 410 InlineCacheBuffer::create_transition_stub(this, info.claim_cached_icholder(), info.entry());
duke@1 411 if (TraceICs) {
duke@1 412 ResourceMark rm(thread);
drchase@24424 413 tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter via icholder ", p2i(instruction_address()));
duke@1 414 }
duke@1 415 }
duke@1 416 } else {
duke@1 417 // Call to compiled code
coleenp@13728 418 bool static_bound = info.is_optimized() || (info.cached_metadata() == NULL);
duke@1 419 #ifdef ASSERT
duke@1 420 CodeBlob* cb = CodeCache::find_blob_unsafe(info.entry());
dlong@48859 421 assert (cb != NULL && cb->is_compiled(), "must be compiled!");
duke@1 422 #endif /* ASSERT */
duke@1 423
duke@1 424 // This is MT safe if we come from a clean-cache and go through a
duke@1 425 // non-verified entry point
duke@1 426 bool safe = SafepointSynchronize::is_at_safepoint() ||
duke@1 427 (!is_in_transition_state() && (info.is_optimized() || static_bound || is_clean()));
duke@1 428
duke@1 429 if (!safe) {
coleenp@13728 430 InlineCacheBuffer::create_transition_stub(this, info.cached_metadata(), info.entry());
duke@1 431 } else {
coleenp@13728 432 if (is_optimized()) {
vlivanov@35086 433 set_ic_destination(info.entry());
coleenp@13728 434 } else {
coleenp@13728 435 set_ic_destination_and_value(info.entry(), info.cached_metadata());
coleenp@13728 436 }
duke@1 437 }
duke@1 438
duke@1 439 if (TraceICs) {
duke@1 440 ResourceMark rm(thread);
coleenp@13728 441 assert(info.cached_metadata() == NULL || info.cached_metadata()->is_klass(), "must be");
duke@1 442 tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to compiled (rcvr klass) %s: %s",
drchase@24424 443 p2i(instruction_address()),
coleenp@13728 444 ((Klass*)info.cached_metadata())->print_value_string(),
duke@1 445 (safe) ? "" : "via stub");
duke@1 446 }
duke@1 447 }
duke@1 448 // We can't check this anymore. With lazy deopt we could have already
duke@1 449 // cleaned this IC entry before we even return. This is possible if
duke@1 450 // we ran out of space in the inline cache buffer trying to do the
duke@1 451 // set_next and we safepointed to free up space. This is a benign
duke@1 452 // race because the IC entry was complete when we safepointed so
duke@1 453 // cleaning it immediately is harmless.
duke@1 454 // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
duke@1 455 }
duke@1 456
duke@1 457
neliasso@42544 458 // is_optimized: Compiler has generated an optimized call (i.e. fixed, no inline cache)
neliasso@42544 459 // static_bound: The call can be static bound. If it isn't also optimized, the property
neliasso@42544 460 // wasn't provable at time of compilation. An optimized call will have any necessary
neliasso@42544 461 // null check, while a static_bound won't. A static_bound (but not optimized) must
neliasso@42544 462 // therefore use the unverified entry point.
coleenp@33593 463 void CompiledIC::compute_monomorphic_entry(const methodHandle& method,
coleenp@46329 464 Klass* receiver_klass,
duke@1 465 bool is_optimized,
duke@1 466 bool static_bound,
kvn@42650 467 bool caller_is_nmethod,
duke@1 468 CompiledICInfo& info,
duke@1 469 TRAPS) {
rbackman@38133 470 CompiledMethod* method_code = method->code();
rbackman@38133 471
duke@1 472 address entry = NULL;
kvn@22223 473 if (method_code != NULL && method_code->is_in_use()) {
rbackman@38133 474 assert(method_code->is_compiled(), "must be compiled");
duke@1 475 // Call to compiled code
neliasso@42544 476 //
neliasso@42544 477 // Note: the following problem exists with Compiler1:
neliasso@42544 478 // - at compile time we may or may not know if the destination is final
neliasso@42544 479 // - if we know that the destination is final (is_optimized), we will emit
neliasso@42544 480 // an optimized virtual call (no inline cache), and need a Method* to make
neliasso@42544 481 // a call to the interpreter
neliasso@42544 482 // - if we don't know if the destination is final, we emit a standard
neliasso@42544 483 // virtual call, and use CompiledICHolder to call interpreted code
neliasso@42544 484 // (no static call stub has been generated)
neliasso@42544 485 // - In the case that we here notice the call is static bound we
neliasso@42544 486 // convert the call into what looks to be an optimized virtual call,
neliasso@42544 487 // but we must use the unverified entry point (since there will be no
neliasso@42544 488 // null check on a call when the target isn't loaded).
neliasso@42544 489 // This causes problems when verifying the IC because
neliasso@42544 490 // it looks vanilla but is optimized. Code in is_call_to_interpreted
neliasso@42544 491 // is aware of this and weakens its asserts.
neliasso@42544 492 if (is_optimized) {
duke@1 493 entry = method_code->verified_entry_point();
duke@1 494 } else {
duke@1 495 entry = method_code->entry_point();
duke@1 496 }
duke@1 497 }
kvn@42650 498 bool far_c2a = entry != NULL && caller_is_nmethod && method_code->is_far_code();
kvn@42650 499 if (entry != NULL && !far_c2a) {
kvn@42650 500 // Call to near compiled code (nmethod or aot).
neliasso@48001 501 info.set_compiled_entry(entry, is_optimized ? NULL : receiver_klass, is_optimized);
duke@1 502 } else {
duke@1 503 if (is_optimized) {
kvn@42650 504 if (far_c2a) {
kvn@42650 505 // Call to aot code from nmethod.
kvn@42650 506 info.set_aot_entry(entry, method());
kvn@42650 507 } else {
kvn@42650 508 // Use stub entry
kvn@42650 509 info.set_interpreter_entry(method()->get_c2i_entry(), method());
kvn@42650 510 }
duke@1 511 } else {
coleenp@13728 512 // Use icholder entry
rbackman@38133 513 assert(method_code == NULL || method_code->is_compiled(), "must be compiled");
coleenp@46329 514 CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass);
coleenp@13728 515 info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder);
duke@1 516 }
duke@1 517 }
coleenp@13728 518 assert(info.is_optimized() == is_optimized, "must agree");
duke@1 519 }
duke@1 520
duke@1 521
coleenp@13728 522 bool CompiledIC::is_icholder_entry(address entry) {
coleenp@13728 523 CodeBlob* cb = CodeCache::find_blob_unsafe(entry);
vlivanov@48558 524 if (cb != NULL && cb->is_adapter_blob()) {
vlivanov@48558 525 return true;
vlivanov@48558 526 }
vlivanov@48558 527 // itable stubs also use CompiledICHolder
vlivanov@48558 528 if (VtableStubs::is_entry_point(entry) && VtableStubs::stub_containing(entry)->is_itable_stub()) {
vlivanov@48558 529 return true;
vlivanov@48558 530 }
vlivanov@48558 531 return false;
duke@1 532 }
duke@1 533
kvn@42650 534 bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm) {
kvn@42650 535 // This call site might have become stale so inspect it carefully.
kvn@42650 536 address dest = cm->call_wrapper_at(call_site->addr())->destination();
kvn@42650 537 return is_icholder_entry(dest);
kvn@42650 538 }
kvn@42650 539
kvn@38035 540 // Release the CompiledICHolder* associated with this call site is there is one.
kvn@42650 541 void CompiledIC::cleanup_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm) {
kvn@42650 542 assert(cm->is_nmethod(), "must be nmethod");
kvn@38035 543 // This call site might have become stale so inspect it carefully.
kvn@38035 544 NativeCall* call = nativeCall_at(call_site->addr());
kvn@38035 545 if (is_icholder_entry(call->destination())) {
kvn@38035 546 NativeMovConstReg* value = nativeMovConstReg_at(call_site->cached_value());
kvn@38035 547 InlineCacheBuffer::queue_for_release((CompiledICHolder*)value->data());
kvn@38035 548 }
kvn@38035 549 }
kvn@38035 550
duke@1 551 // ----------------------------------------------------------------------------
duke@1 552
duke@1 553 void CompiledStaticCall::set_to_clean() {
duke@1 554 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
duke@1 555 // Reset call site
stefank@25492 556 MutexLockerEx pl(SafepointSynchronize::is_at_safepoint() ? NULL : Patching_lock, Mutex::_no_safepoint_check_flag);
duke@1 557 #ifdef ASSERT
kvn@42650 558 CodeBlob* cb = CodeCache::find_blob_unsafe(instruction_address());
rbackman@38133 559 assert(cb != NULL && cb->is_compiled(), "must be compiled");
duke@1 560 #endif
kvn@42650 561
kvn@42650 562 set_destination_mt_safe(resolve_call_stub());
duke@1 563
duke@1 564 // Do not reset stub here: It is too expensive to call find_stub.
duke@1 565 // Instead, rely on caller (nmethod::clear_inline_caches) to clear
duke@1 566 // both the call and its stub.
duke@1 567 }
duke@1 568
duke@1 569 bool CompiledStaticCall::is_clean() const {
kvn@42650 570 return destination() == resolve_call_stub();
duke@1 571 }
duke@1 572
duke@1 573 bool CompiledStaticCall::is_call_to_compiled() const {
duke@1 574 return CodeCache::contains(destination());
duke@1 575 }
duke@1 576
kvn@42650 577 bool CompiledDirectStaticCall::is_call_to_interpreted() const {
duke@1 578 // It is a call to interpreted, if it calls to a stub. Hence, the destination
duke@1 579 // must be in the stub part of the nmethod that contains the call
rbackman@38133 580 CompiledMethod* cm = CodeCache::find_compiled(instruction_address());
rbackman@38133 581 return cm->stub_contains(destination());
duke@1 582 }
duke@1 583
kvn@42650 584 bool CompiledDirectStaticCall::is_call_to_far() const {
kvn@42650 585 // It is a call to aot method, if it calls to a stub. Hence, the destination
kvn@42650 586 // must be in the stub part of the nmethod that contains the call
kvn@42650 587 CodeBlob* desc = CodeCache::find_blob(instruction_address());
kvn@42650 588 return desc->as_compiled_method()->stub_contains(destination());
kvn@42650 589 }
kvn@42650 590
kvn@42650 591 void CompiledStaticCall::set_to_compiled(address entry) {
kvn@42650 592 if (TraceICs) {
kvn@42650 593 ResourceMark rm;
kvn@42650 594 tty->print_cr("%s@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT,
kvn@42650 595 name(),
kvn@42650 596 p2i(instruction_address()),
kvn@42650 597 p2i(entry));
kvn@42650 598 }
kvn@42650 599 // Call to compiled code
kvn@42650 600 assert(CodeCache::contains(entry), "wrong entry point");
kvn@42650 601 set_destination_mt_safe(entry);
kvn@42650 602 }
kvn@42650 603
duke@1 604 void CompiledStaticCall::set(const StaticCallInfo& info) {
duke@1 605 assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
duke@1 606 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
duke@1 607 // Updating a cache to the wrong entry can cause bugs that are very hard
duke@1 608 // to track down - if cache entry gets invalid - we just clean it. In
duke@1 609 // this way it is always the same code path that is responsible for
duke@1 610 // updating and resolving an inline cache
duke@1 611 assert(is_clean(), "do not update a call entry - use clean");
duke@1 612
duke@1 613 if (info._to_interpreter) {
duke@1 614 // Call to interpreted code
duke@1 615 set_to_interpreted(info.callee(), info.entry());
kvn@42650 616 #if INCLUDE_AOT
kvn@42650 617 } else if (info._to_aot) {
kvn@42650 618 // Call to far code
kvn@42650 619 set_to_far(info.callee(), info.entry());
kvn@42650 620 #endif
duke@1 621 } else {
kvn@42650 622 set_to_compiled(info.entry());
duke@1 623 }
duke@1 624 }
duke@1 625
duke@1 626 // Compute settings for a CompiledStaticCall. Since we might have to set
duke@1 627 // the stub when calling to the interpreter, we need to return arguments.
kvn@42650 628 void CompiledStaticCall::compute_entry(const methodHandle& m, bool caller_is_nmethod, StaticCallInfo& info) {
rbackman@38133 629 CompiledMethod* m_code = m->code();
duke@1 630 info._callee = m;
kvn@22223 631 if (m_code != NULL && m_code->is_in_use()) {
kvn@42650 632 if (caller_is_nmethod && m_code->is_far_code()) {
kvn@42650 633 // Call to far aot code from nmethod.
kvn@42650 634 info._to_aot = true;
kvn@42650 635 } else {
kvn@42650 636 info._to_aot = false;
kvn@42650 637 }
duke@1 638 info._to_interpreter = false;
duke@1 639 info._entry = m_code->verified_entry_point();
duke@1 640 } else {
duke@1 641 // Callee is interpreted code. In any case entering the interpreter
duke@1 642 // puts a converter-frame on the stack to save arguments.
iveresov@26580 643 assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics");
duke@1 644 info._to_interpreter = true;
duke@1 645 info._entry = m()->get_c2i_entry();
duke@1 646 }
duke@1 647 }
duke@1 648
kvn@42650 649 address CompiledDirectStaticCall::find_stub_for(address instruction, bool is_aot) {
duke@1 650 // Find reloc. information containing this call-site
kvn@42650 651 RelocIterator iter((nmethod*)NULL, instruction);
duke@1 652 while (iter.next()) {
kvn@42650 653 if (iter.addr() == instruction) {
duke@1 654 switch(iter.type()) {
duke@1 655 case relocInfo::static_call_type:
kvn@42650 656 return iter.static_call_reloc()->static_stub(is_aot);
duke@1 657 // We check here for opt_virtual_call_type, since we reuse the code
duke@1 658 // from the CompiledIC implementation
duke@1 659 case relocInfo::opt_virtual_call_type:
kvn@42650 660 return iter.opt_virtual_call_reloc()->static_stub(is_aot);
duke@1 661 case relocInfo::poll_type:
duke@1 662 case relocInfo::poll_return_type: // A safepoint can't overlap a call.
duke@1 663 default:
duke@1 664 ShouldNotReachHere();
duke@1 665 }
duke@1 666 }
duke@1 667 }
duke@1 668 return NULL;
duke@1 669 }
duke@1 670
kvn@42650 671 address CompiledDirectStaticCall::find_stub(bool is_aot) {
kvn@42650 672 return CompiledDirectStaticCall::find_stub_for(instruction_address(), is_aot);
kvn@42650 673 }
kvn@42650 674
kvn@42650 675 address CompiledDirectStaticCall::resolve_call_stub() const {
kvn@42650 676 return SharedRuntime::get_resolve_static_call_stub();
kvn@42650 677 }
duke@1 678
duke@1 679 //-----------------------------------------------------------------------------
duke@1 680 // Non-product mode code
duke@1 681 #ifndef PRODUCT
duke@1 682
duke@1 683 void CompiledIC::verify() {
kvn@42650 684 _call->verify();
duke@1 685 assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted()
duke@1 686 || is_optimized() || is_megamorphic(), "sanity check");
duke@1 687 }
duke@1 688
duke@1 689 void CompiledIC::print() {
duke@1 690 print_compiled_ic();
duke@1 691 tty->cr();
duke@1 692 }
duke@1 693
duke@1 694 void CompiledIC::print_compiled_ic() {
coleenp@13728 695 tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT,
drchase@24424 696 p2i(instruction_address()), is_call_to_interpreted() ? "interpreted " : "", p2i(ic_destination()), p2i(is_optimized() ? NULL : cached_value()));
duke@1 697 }
duke@1 698
kvn@42650 699 void CompiledDirectStaticCall::print() {
drchase@24424 700 tty->print("static call at " INTPTR_FORMAT " -> ", p2i(instruction_address()));
duke@1 701 if (is_clean()) {
duke@1 702 tty->print("clean");
duke@1 703 } else if (is_call_to_compiled()) {
duke@1 704 tty->print("compiled");
kvn@42650 705 } else if (is_call_to_far()) {
kvn@42650 706 tty->print("far");
duke@1 707 } else if (is_call_to_interpreted()) {
duke@1 708 tty->print("interpreted");
duke@1 709 }
duke@1 710 tty->cr();
duke@1 711 }
duke@1 712
dlong@17094 713 #endif // !PRODUCT