src/share/vm/code/compiledIC.cpp

Wed, 20 Mar 2013 06:32:49 -0700

author
morris
date
Wed, 20 Mar 2013 06:32:49 -0700
changeset 4776
41340544e182
parent 4251
18fb7da42534
child 5000
a6e09d6dd8e5
permissions
-rw-r--r--

8009248: [parfait] Null pointer deference in hotspot/src/share/vm/code/compiledIC.cpp
Summary: add guarantee() to set_to_interpreted()
Reviewed-by: kvn

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

mercurial