src/share/vm/code/compiledIC.cpp

Wed, 02 Jun 2010 22:45:42 -0700

author
jrose
date
Wed, 02 Jun 2010 22:45:42 -0700
changeset 1934
e9ff18c4ace7
parent 1907
c18cbe5936b8
parent 1918
1a5913bf5e19
child 2314
f95d63e2154a
permissions
-rw-r--r--

Merge

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

mercurial