src/share/vm/code/compiledIC.cpp

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

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

merge

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

mercurial