Thu, 26 Jul 2018 16:04:06 +0800
Merge
aoqi@0 | 1 | /* |
poonam@9185 | 2 | * Copyright (c) 1997, 2018, 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 | { |
stefank@6992 | 102 | MutexLockerEx pl(SafepointSynchronize::is_at_safepoint() ? NULL : Patching_lock, Mutex::_no_safepoint_check_flag); |
aoqi@0 | 103 | #ifdef ASSERT |
stefank@6992 | 104 | CodeBlob* cb = CodeCache::find_blob_unsafe(_ic_call); |
stefank@6992 | 105 | assert(cb != NULL && cb->is_nmethod(), "must be nmethod"); |
aoqi@0 | 106 | #endif |
stefank@6992 | 107 | _ic_call->set_destination_mt_safe(entry_point); |
stefank@6992 | 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 | |
thartmann@8073 | 158 | // Clears the IC stub if the compiled IC is in transition state |
thartmann@8073 | 159 | void CompiledIC::clear_ic_stub() { |
thartmann@8073 | 160 | if (is_in_transition_state()) { |
thartmann@8073 | 161 | ICStub* stub = ICStub_from_destination_address(stub_address()); |
thartmann@8073 | 162 | stub->clear(); |
thartmann@8073 | 163 | } |
thartmann@8073 | 164 | } |
thartmann@8073 | 165 | |
aoqi@0 | 166 | |
aoqi@0 | 167 | //----------------------------------------------------------------------------- |
aoqi@0 | 168 | // High-level access to an inline cache. Guaranteed to be MT-safe. |
aoqi@0 | 169 | |
stefank@6991 | 170 | void CompiledIC::initialize_from_iter(RelocIterator* iter) { |
stefank@6991 | 171 | assert(iter->addr() == _ic_call->instruction_address(), "must find ic_call"); |
stefank@6991 | 172 | |
stefank@6991 | 173 | if (iter->type() == relocInfo::virtual_call_type) { |
stefank@6991 | 174 | virtual_call_Relocation* r = iter->virtual_call_reloc(); |
stefank@6991 | 175 | _is_optimized = false; |
stefank@6991 | 176 | _value = nativeMovConstReg_at(r->cached_value()); |
stefank@6991 | 177 | } else { |
stefank@6991 | 178 | assert(iter->type() == relocInfo::opt_virtual_call_type, "must be a virtual call"); |
stefank@6991 | 179 | _is_optimized = true; |
stefank@6991 | 180 | _value = NULL; |
stefank@6991 | 181 | } |
stefank@6991 | 182 | } |
stefank@6991 | 183 | |
stefank@6985 | 184 | CompiledIC::CompiledIC(nmethod* nm, NativeCall* call) |
stefank@6985 | 185 | : _ic_call(call) |
stefank@6985 | 186 | { |
stefank@6991 | 187 | address ic_call = _ic_call->instruction_address(); |
stefank@6985 | 188 | |
stefank@6985 | 189 | assert(ic_call != NULL, "ic_call address must be set"); |
stefank@6985 | 190 | assert(nm != NULL, "must pass nmethod"); |
stefank@6985 | 191 | assert(nm->contains(ic_call), "must be in nmethod"); |
stefank@6985 | 192 | |
stefank@6985 | 193 | // Search for the ic_call at the given address. |
stefank@6985 | 194 | RelocIterator iter(nm, ic_call, ic_call+1); |
stefank@6985 | 195 | bool ret = iter.next(); |
stefank@6985 | 196 | assert(ret == true, "relocInfo must exist at this address"); |
stefank@6985 | 197 | assert(iter.addr() == ic_call, "must find ic_call"); |
stefank@6991 | 198 | |
stefank@6991 | 199 | initialize_from_iter(&iter); |
stefank@6991 | 200 | } |
stefank@6991 | 201 | |
stefank@6991 | 202 | CompiledIC::CompiledIC(RelocIterator* iter) |
stefank@6991 | 203 | : _ic_call(nativeCall_at(iter->addr())) |
stefank@6991 | 204 | { |
stefank@6991 | 205 | address ic_call = _ic_call->instruction_address(); |
stefank@6991 | 206 | |
stefank@6991 | 207 | nmethod* nm = iter->code(); |
stefank@6991 | 208 | assert(ic_call != NULL, "ic_call address must be set"); |
stefank@6991 | 209 | assert(nm != NULL, "must pass nmethod"); |
stefank@6991 | 210 | assert(nm->contains(ic_call), "must be in nmethod"); |
stefank@6991 | 211 | |
stefank@6991 | 212 | initialize_from_iter(iter); |
stefank@6985 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | bool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS) { |
aoqi@0 | 216 | assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), ""); |
aoqi@0 | 217 | assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic"); |
aoqi@0 | 218 | assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?"); |
aoqi@0 | 219 | |
aoqi@0 | 220 | address entry; |
aoqi@0 | 221 | if (call_info->call_kind() == CallInfo::itable_call) { |
aoqi@0 | 222 | assert(bytecode == Bytecodes::_invokeinterface, ""); |
aoqi@0 | 223 | int itable_index = call_info->itable_index(); |
aoqi@0 | 224 | entry = VtableStubs::find_itable_stub(itable_index); |
aoqi@0 | 225 | if (entry == false) { |
aoqi@0 | 226 | return false; |
aoqi@0 | 227 | } |
aoqi@0 | 228 | #ifdef ASSERT |
aoqi@0 | 229 | int index = call_info->resolved_method()->itable_index(); |
aoqi@0 | 230 | assert(index == itable_index, "CallInfo pre-computes this"); |
aoqi@0 | 231 | InstanceKlass* k = call_info->resolved_method()->method_holder(); |
aoqi@0 | 232 | assert(k->verify_itable_index(itable_index), "sanity check"); |
dbuck@8997 | 233 | #endif //ASSERT |
dbuck@8997 | 234 | CompiledICHolder* holder = new CompiledICHolder(call_info->resolved_method()->method_holder(), |
poonam@9185 | 235 | call_info->resolved_klass()(), false); |
dbuck@8997 | 236 | holder->claim(); |
dbuck@8997 | 237 | InlineCacheBuffer::create_transition_stub(this, holder, entry); |
aoqi@0 | 238 | } else { |
aoqi@0 | 239 | assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable"); |
aoqi@0 | 240 | // Can be different than selected_method->vtable_index(), due to package-private etc. |
aoqi@0 | 241 | int vtable_index = call_info->vtable_index(); |
aoqi@0 | 242 | assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check"); |
aoqi@0 | 243 | entry = VtableStubs::find_vtable_stub(vtable_index); |
aoqi@0 | 244 | if (entry == NULL) { |
aoqi@0 | 245 | return false; |
aoqi@0 | 246 | } |
aoqi@0 | 247 | InlineCacheBuffer::create_transition_stub(this, NULL, entry); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | if (TraceICs) { |
aoqi@0 | 251 | ResourceMark rm; |
aoqi@0 | 252 | tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT, |
aoqi@0 | 253 | p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry)); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | // We can't check this anymore. With lazy deopt we could have already |
aoqi@0 | 257 | // cleaned this IC entry before we even return. This is possible if |
aoqi@0 | 258 | // we ran out of space in the inline cache buffer trying to do the |
aoqi@0 | 259 | // set_next and we safepointed to free up space. This is a benign |
aoqi@0 | 260 | // race because the IC entry was complete when we safepointed so |
aoqi@0 | 261 | // cleaning it immediately is harmless. |
aoqi@0 | 262 | // assert(is_megamorphic(), "sanity check"); |
aoqi@0 | 263 | return true; |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | |
aoqi@0 | 267 | // true if destination is megamorphic stub |
aoqi@0 | 268 | bool CompiledIC::is_megamorphic() const { |
aoqi@0 | 269 | assert(CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), ""); |
aoqi@0 | 270 | assert(!is_optimized(), "an optimized call cannot be megamorphic"); |
aoqi@0 | 271 | |
aoqi@0 | 272 | // Cannot rely on cached_value. It is either an interface or a method. |
poonam@9185 | 273 | return VtableStubs::entry_point(ic_destination()) != NULL; |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | bool CompiledIC::is_call_to_compiled() const { |
aoqi@0 | 277 | assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), ""); |
aoqi@0 | 278 | |
aoqi@0 | 279 | // Use unsafe, since an inline cache might point to a zombie method. However, the zombie |
aoqi@0 | 280 | // method is guaranteed to still exist, since we only remove methods after all inline caches |
aoqi@0 | 281 | // has been cleaned up |
aoqi@0 | 282 | CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination()); |
aoqi@0 | 283 | bool is_monomorphic = (cb != NULL && cb->is_nmethod()); |
aoqi@0 | 284 | // Check that the cached_value is a klass for non-optimized monomorphic calls |
aoqi@0 | 285 | // This assertion is invalid for compiler1: a call that does not look optimized (no static stub) can be used |
aoqi@0 | 286 | // for calling directly to vep without using the inline cache (i.e., cached_value == NULL) |
aoqi@0 | 287 | #ifdef ASSERT |
aoqi@0 | 288 | CodeBlob* caller = CodeCache::find_blob_unsafe(instruction_address()); |
aoqi@0 | 289 | bool is_c1_method = caller->is_compiled_by_c1(); |
aoqi@0 | 290 | assert( is_c1_method || |
aoqi@0 | 291 | !is_monomorphic || |
aoqi@0 | 292 | is_optimized() || |
thartmann@8075 | 293 | !caller->is_alive() || |
aoqi@0 | 294 | (cached_metadata() != NULL && cached_metadata()->is_klass()), "sanity check"); |
aoqi@0 | 295 | #endif // ASSERT |
aoqi@0 | 296 | return is_monomorphic; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | |
aoqi@0 | 299 | |
aoqi@0 | 300 | bool CompiledIC::is_call_to_interpreted() const { |
aoqi@0 | 301 | assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), ""); |
aoqi@0 | 302 | // Call to interpreter if destination is either calling to a stub (if it |
aoqi@0 | 303 | // is optimized), or calling to an I2C blob |
aoqi@0 | 304 | bool is_call_to_interpreted = false; |
aoqi@0 | 305 | if (!is_optimized()) { |
aoqi@0 | 306 | // must use unsafe because the destination can be a zombie (and we're cleaning) |
aoqi@0 | 307 | // and the print_compiled_ic code wants to know if site (in the non-zombie) |
aoqi@0 | 308 | // is to the interpreter. |
aoqi@0 | 309 | CodeBlob* cb = CodeCache::find_blob_unsafe(ic_destination()); |
aoqi@0 | 310 | is_call_to_interpreted = (cb != NULL && cb->is_adapter_blob()); |
aoqi@0 | 311 | assert(!is_call_to_interpreted || (is_icholder_call() && cached_icholder() != NULL), "sanity check"); |
aoqi@0 | 312 | } else { |
aoqi@0 | 313 | // Check if we are calling into our own codeblob (i.e., to a stub) |
aoqi@0 | 314 | CodeBlob* cb = CodeCache::find_blob(_ic_call->instruction_address()); |
aoqi@0 | 315 | address dest = ic_destination(); |
aoqi@0 | 316 | #ifdef ASSERT |
aoqi@0 | 317 | { |
aoqi@0 | 318 | CodeBlob* db = CodeCache::find_blob_unsafe(dest); |
aoqi@0 | 319 | assert(!db->is_adapter_blob(), "must use stub!"); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | #endif /* ASSERT */ |
aoqi@0 | 322 | is_call_to_interpreted = cb->contains(dest); |
aoqi@0 | 323 | } |
aoqi@0 | 324 | return is_call_to_interpreted; |
aoqi@0 | 325 | } |
aoqi@0 | 326 | |
aoqi@0 | 327 | |
thartmann@8075 | 328 | void CompiledIC::set_to_clean(bool in_use) { |
aoqi@0 | 329 | assert(SafepointSynchronize::is_at_safepoint() || CompiledIC_lock->is_locked() , "MT-unsafe call"); |
aoqi@0 | 330 | if (TraceInlineCacheClearing || TraceICs) { |
aoqi@0 | 331 | tty->print_cr("IC@" INTPTR_FORMAT ": set to clean", p2i(instruction_address())); |
aoqi@0 | 332 | print(); |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | address entry; |
aoqi@0 | 336 | if (is_optimized()) { |
aoqi@0 | 337 | entry = SharedRuntime::get_resolve_opt_virtual_call_stub(); |
aoqi@0 | 338 | } else { |
aoqi@0 | 339 | entry = SharedRuntime::get_resolve_virtual_call_stub(); |
aoqi@0 | 340 | } |
aoqi@0 | 341 | |
aoqi@0 | 342 | // A zombie transition will always be safe, since the metadata has already been set to NULL, so |
aoqi@0 | 343 | // we only need to patch the destination |
thartmann@8075 | 344 | bool safe_transition = !in_use || is_optimized() || SafepointSynchronize::is_at_safepoint(); |
aoqi@0 | 345 | |
aoqi@0 | 346 | if (safe_transition) { |
aoqi@0 | 347 | // Kill any leftover stub we might have too |
thartmann@8073 | 348 | clear_ic_stub(); |
aoqi@0 | 349 | if (is_optimized()) { |
thartmann@8074 | 350 | set_ic_destination(entry); |
thartmann@8074 | 351 | } else { |
aoqi@0 | 352 | set_ic_destination_and_value(entry, (void*)NULL); |
aoqi@0 | 353 | } |
aoqi@0 | 354 | } else { |
aoqi@0 | 355 | // Unsafe transition - create stub. |
aoqi@0 | 356 | InlineCacheBuffer::create_transition_stub(this, NULL, entry); |
aoqi@0 | 357 | } |
aoqi@0 | 358 | // We can't check this anymore. With lazy deopt we could have already |
aoqi@0 | 359 | // cleaned this IC entry before we even return. This is possible if |
aoqi@0 | 360 | // we ran out of space in the inline cache buffer trying to do the |
aoqi@0 | 361 | // set_next and we safepointed to free up space. This is a benign |
aoqi@0 | 362 | // race because the IC entry was complete when we safepointed so |
aoqi@0 | 363 | // cleaning it immediately is harmless. |
aoqi@0 | 364 | // assert(is_clean(), "sanity check"); |
aoqi@0 | 365 | } |
aoqi@0 | 366 | |
aoqi@0 | 367 | |
aoqi@0 | 368 | bool CompiledIC::is_clean() const { |
aoqi@0 | 369 | assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), ""); |
aoqi@0 | 370 | bool is_clean = false; |
aoqi@0 | 371 | address dest = ic_destination(); |
aoqi@0 | 372 | is_clean = dest == SharedRuntime::get_resolve_opt_virtual_call_stub() || |
aoqi@0 | 373 | dest == SharedRuntime::get_resolve_virtual_call_stub(); |
aoqi@0 | 374 | assert(!is_clean || is_optimized() || cached_value() == NULL, "sanity check"); |
aoqi@0 | 375 | return is_clean; |
aoqi@0 | 376 | } |
aoqi@0 | 377 | |
aoqi@0 | 378 | |
aoqi@0 | 379 | void CompiledIC::set_to_monomorphic(CompiledICInfo& info) { |
aoqi@0 | 380 | assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), ""); |
aoqi@0 | 381 | // Updating a cache to the wrong entry can cause bugs that are very hard |
aoqi@0 | 382 | // to track down - if cache entry gets invalid - we just clean it. In |
aoqi@0 | 383 | // this way it is always the same code path that is responsible for |
aoqi@0 | 384 | // updating and resolving an inline cache |
aoqi@0 | 385 | // |
aoqi@0 | 386 | // The above is no longer true. SharedRuntime::fixup_callers_callsite will change optimized |
aoqi@0 | 387 | // callsites. In addition ic_miss code will update a site to monomorphic if it determines |
aoqi@0 | 388 | // that an monomorphic call to the interpreter can now be monomorphic to compiled code. |
aoqi@0 | 389 | // |
aoqi@0 | 390 | // In both of these cases the only thing being modifed is the jump/call target and these |
aoqi@0 | 391 | // transitions are mt_safe |
aoqi@0 | 392 | |
aoqi@0 | 393 | Thread *thread = Thread::current(); |
aoqi@0 | 394 | if (info.to_interpreter()) { |
aoqi@0 | 395 | // Call to interpreter |
aoqi@0 | 396 | if (info.is_optimized() && is_optimized()) { |
aoqi@0 | 397 | assert(is_clean(), "unsafe IC path"); |
aoqi@0 | 398 | MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); |
aoqi@0 | 399 | // the call analysis (callee structure) specifies that the call is optimized |
aoqi@0 | 400 | // (either because of CHA or the static target is final) |
aoqi@0 | 401 | // At code generation time, this call has been emitted as static call |
aoqi@0 | 402 | // Call via stub |
aoqi@0 | 403 | assert(info.cached_metadata() != NULL && info.cached_metadata()->is_method(), "sanity check"); |
aoqi@0 | 404 | CompiledStaticCall* csc = compiledStaticCall_at(instruction_address()); |
aoqi@0 | 405 | methodHandle method (thread, (Method*)info.cached_metadata()); |
aoqi@0 | 406 | csc->set_to_interpreted(method, info.entry()); |
aoqi@0 | 407 | if (TraceICs) { |
aoqi@0 | 408 | ResourceMark rm(thread); |
aoqi@0 | 409 | tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter: %s", |
aoqi@0 | 410 | p2i(instruction_address()), |
aoqi@0 | 411 | method->print_value_string()); |
aoqi@0 | 412 | } |
aoqi@0 | 413 | } else { |
aoqi@0 | 414 | // Call via method-klass-holder |
aoqi@0 | 415 | InlineCacheBuffer::create_transition_stub(this, info.claim_cached_icholder(), info.entry()); |
aoqi@0 | 416 | if (TraceICs) { |
aoqi@0 | 417 | ResourceMark rm(thread); |
aoqi@0 | 418 | tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter via icholder ", p2i(instruction_address())); |
aoqi@0 | 419 | } |
aoqi@0 | 420 | } |
aoqi@0 | 421 | } else { |
aoqi@0 | 422 | // Call to compiled code |
aoqi@0 | 423 | bool static_bound = info.is_optimized() || (info.cached_metadata() == NULL); |
aoqi@0 | 424 | #ifdef ASSERT |
aoqi@0 | 425 | CodeBlob* cb = CodeCache::find_blob_unsafe(info.entry()); |
aoqi@0 | 426 | assert (cb->is_nmethod(), "must be compiled!"); |
aoqi@0 | 427 | #endif /* ASSERT */ |
aoqi@0 | 428 | |
aoqi@0 | 429 | // This is MT safe if we come from a clean-cache and go through a |
aoqi@0 | 430 | // non-verified entry point |
aoqi@0 | 431 | bool safe = SafepointSynchronize::is_at_safepoint() || |
aoqi@0 | 432 | (!is_in_transition_state() && (info.is_optimized() || static_bound || is_clean())); |
aoqi@0 | 433 | |
aoqi@0 | 434 | if (!safe) { |
aoqi@0 | 435 | InlineCacheBuffer::create_transition_stub(this, info.cached_metadata(), info.entry()); |
aoqi@0 | 436 | } else { |
aoqi@0 | 437 | if (is_optimized()) { |
aoqi@0 | 438 | set_ic_destination(info.entry()); |
aoqi@0 | 439 | } else { |
aoqi@0 | 440 | set_ic_destination_and_value(info.entry(), info.cached_metadata()); |
aoqi@0 | 441 | } |
aoqi@0 | 442 | } |
aoqi@0 | 443 | |
aoqi@0 | 444 | if (TraceICs) { |
aoqi@0 | 445 | ResourceMark rm(thread); |
aoqi@0 | 446 | assert(info.cached_metadata() == NULL || info.cached_metadata()->is_klass(), "must be"); |
aoqi@0 | 447 | tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to compiled (rcvr klass) %s: %s", |
aoqi@0 | 448 | p2i(instruction_address()), |
aoqi@0 | 449 | ((Klass*)info.cached_metadata())->print_value_string(), |
aoqi@0 | 450 | (safe) ? "" : "via stub"); |
aoqi@0 | 451 | } |
aoqi@0 | 452 | } |
aoqi@0 | 453 | // We can't check this anymore. With lazy deopt we could have already |
aoqi@0 | 454 | // cleaned this IC entry before we even return. This is possible if |
aoqi@0 | 455 | // we ran out of space in the inline cache buffer trying to do the |
aoqi@0 | 456 | // set_next and we safepointed to free up space. This is a benign |
aoqi@0 | 457 | // race because the IC entry was complete when we safepointed so |
aoqi@0 | 458 | // cleaning it immediately is harmless. |
aoqi@0 | 459 | // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check"); |
aoqi@0 | 460 | } |
aoqi@0 | 461 | |
aoqi@0 | 462 | |
aoqi@0 | 463 | // is_optimized: Compiler has generated an optimized call (i.e., no inline |
aoqi@0 | 464 | // cache) static_bound: The call can be static bound (i.e, no need to use |
aoqi@0 | 465 | // inline cache) |
aoqi@0 | 466 | void CompiledIC::compute_monomorphic_entry(methodHandle method, |
aoqi@0 | 467 | KlassHandle receiver_klass, |
aoqi@0 | 468 | bool is_optimized, |
aoqi@0 | 469 | bool static_bound, |
aoqi@0 | 470 | CompiledICInfo& info, |
aoqi@0 | 471 | TRAPS) { |
aoqi@0 | 472 | nmethod* method_code = method->code(); |
aoqi@0 | 473 | address entry = NULL; |
aoqi@0 | 474 | if (method_code != NULL && method_code->is_in_use()) { |
aoqi@0 | 475 | // Call to compiled code |
aoqi@0 | 476 | if (static_bound || is_optimized) { |
aoqi@0 | 477 | entry = method_code->verified_entry_point(); |
aoqi@0 | 478 | } else { |
aoqi@0 | 479 | entry = method_code->entry_point(); |
aoqi@0 | 480 | } |
aoqi@0 | 481 | } |
aoqi@0 | 482 | if (entry != NULL) { |
aoqi@0 | 483 | // Call to compiled code |
aoqi@0 | 484 | info.set_compiled_entry(entry, (static_bound || is_optimized) ? NULL : receiver_klass(), is_optimized); |
aoqi@0 | 485 | } else { |
aoqi@0 | 486 | // Note: the following problem exists with Compiler1: |
aoqi@0 | 487 | // - at compile time we may or may not know if the destination is final |
aoqi@0 | 488 | // - if we know that the destination is final, we will emit an optimized |
aoqi@0 | 489 | // virtual call (no inline cache), and need a Method* to make a call |
aoqi@0 | 490 | // to the interpreter |
aoqi@0 | 491 | // - if we do not know if the destination is final, we emit a standard |
aoqi@0 | 492 | // virtual call, and use CompiledICHolder to call interpreted code |
aoqi@0 | 493 | // (no static call stub has been generated) |
aoqi@0 | 494 | // However in that case we will now notice it is static_bound |
aoqi@0 | 495 | // and convert the call into what looks to be an optimized |
aoqi@0 | 496 | // virtual call. This causes problems in verifying the IC because |
aoqi@0 | 497 | // it look vanilla but is optimized. Code in is_call_to_interpreted |
aoqi@0 | 498 | // is aware of this and weakens its asserts. |
aoqi@0 | 499 | |
aoqi@0 | 500 | // static_bound should imply is_optimized -- otherwise we have a |
aoqi@0 | 501 | // performance bug (statically-bindable method is called via |
aoqi@0 | 502 | // dynamically-dispatched call note: the reverse implication isn't |
aoqi@0 | 503 | // necessarily true -- the call may have been optimized based on compiler |
aoqi@0 | 504 | // analysis (static_bound is only based on "final" etc.) |
aoqi@0 | 505 | #ifdef COMPILER2 |
aoqi@0 | 506 | #ifdef TIERED |
aoqi@0 | 507 | #if defined(ASSERT) |
aoqi@0 | 508 | // can't check the assert because we don't have the CompiledIC with which to |
aoqi@0 | 509 | // find the address if the call instruction. |
aoqi@0 | 510 | // |
aoqi@0 | 511 | // CodeBlob* cb = find_blob_unsafe(instruction_address()); |
aoqi@0 | 512 | // assert(cb->is_compiled_by_c1() || !static_bound || is_optimized, "static_bound should imply is_optimized"); |
aoqi@0 | 513 | #endif // ASSERT |
aoqi@0 | 514 | #else |
aoqi@0 | 515 | assert(!static_bound || is_optimized, "static_bound should imply is_optimized"); |
aoqi@0 | 516 | #endif // TIERED |
aoqi@0 | 517 | #endif // COMPILER2 |
aoqi@0 | 518 | if (is_optimized) { |
aoqi@0 | 519 | // Use stub entry |
aoqi@0 | 520 | info.set_interpreter_entry(method()->get_c2i_entry(), method()); |
aoqi@0 | 521 | } else { |
aoqi@0 | 522 | // Use icholder entry |
aoqi@0 | 523 | CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass()); |
aoqi@0 | 524 | info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder); |
aoqi@0 | 525 | } |
aoqi@0 | 526 | } |
aoqi@0 | 527 | assert(info.is_optimized() == is_optimized, "must agree"); |
aoqi@0 | 528 | } |
aoqi@0 | 529 | |
aoqi@0 | 530 | |
aoqi@0 | 531 | bool CompiledIC::is_icholder_entry(address entry) { |
aoqi@0 | 532 | CodeBlob* cb = CodeCache::find_blob_unsafe(entry); |
dbuck@8997 | 533 | if (cb != NULL && cb->is_adapter_blob()) { |
dbuck@8997 | 534 | return true; |
dbuck@8997 | 535 | } |
dbuck@8997 | 536 | // itable stubs also use CompiledICHolder |
poonam@9185 | 537 | if (cb != NULL && cb->is_vtable_blob()) { |
poonam@9185 | 538 | VtableStub* s = VtableStubs::entry_point(entry); |
poonam@9185 | 539 | return (s != NULL) && s->is_itable_stub(); |
dbuck@8997 | 540 | } |
poonam@9185 | 541 | |
dbuck@8997 | 542 | return false; |
aoqi@0 | 543 | } |
aoqi@0 | 544 | |
aoqi@0 | 545 | // ---------------------------------------------------------------------------- |
aoqi@0 | 546 | |
aoqi@0 | 547 | void CompiledStaticCall::set_to_clean() { |
aoqi@0 | 548 | assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call"); |
aoqi@0 | 549 | // Reset call site |
stefank@6992 | 550 | MutexLockerEx pl(SafepointSynchronize::is_at_safepoint() ? NULL : Patching_lock, Mutex::_no_safepoint_check_flag); |
aoqi@0 | 551 | #ifdef ASSERT |
aoqi@0 | 552 | CodeBlob* cb = CodeCache::find_blob_unsafe(this); |
aoqi@0 | 553 | assert(cb != NULL && cb->is_nmethod(), "must be nmethod"); |
aoqi@0 | 554 | #endif |
aoqi@0 | 555 | set_destination_mt_safe(SharedRuntime::get_resolve_static_call_stub()); |
aoqi@0 | 556 | |
aoqi@0 | 557 | // Do not reset stub here: It is too expensive to call find_stub. |
aoqi@0 | 558 | // Instead, rely on caller (nmethod::clear_inline_caches) to clear |
aoqi@0 | 559 | // both the call and its stub. |
aoqi@0 | 560 | } |
aoqi@0 | 561 | |
aoqi@0 | 562 | |
aoqi@0 | 563 | bool CompiledStaticCall::is_clean() const { |
aoqi@0 | 564 | return destination() == SharedRuntime::get_resolve_static_call_stub(); |
aoqi@0 | 565 | } |
aoqi@0 | 566 | |
aoqi@0 | 567 | bool CompiledStaticCall::is_call_to_compiled() const { |
aoqi@0 | 568 | return CodeCache::contains(destination()); |
aoqi@0 | 569 | } |
aoqi@0 | 570 | |
aoqi@0 | 571 | |
aoqi@0 | 572 | bool CompiledStaticCall::is_call_to_interpreted() const { |
aoqi@0 | 573 | // It is a call to interpreted, if it calls to a stub. Hence, the destination |
aoqi@0 | 574 | // must be in the stub part of the nmethod that contains the call |
aoqi@0 | 575 | nmethod* nm = CodeCache::find_nmethod(instruction_address()); |
aoqi@0 | 576 | return nm->stub_contains(destination()); |
aoqi@0 | 577 | } |
aoqi@0 | 578 | |
aoqi@0 | 579 | void CompiledStaticCall::set(const StaticCallInfo& info) { |
aoqi@0 | 580 | assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call"); |
aoqi@0 | 581 | MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag); |
aoqi@0 | 582 | // Updating a cache to the wrong entry can cause bugs that are very hard |
aoqi@0 | 583 | // to track down - if cache entry gets invalid - we just clean it. In |
aoqi@0 | 584 | // this way it is always the same code path that is responsible for |
aoqi@0 | 585 | // updating and resolving an inline cache |
aoqi@0 | 586 | assert(is_clean(), "do not update a call entry - use clean"); |
aoqi@0 | 587 | |
aoqi@0 | 588 | if (info._to_interpreter) { |
aoqi@0 | 589 | // Call to interpreted code |
aoqi@0 | 590 | set_to_interpreted(info.callee(), info.entry()); |
aoqi@0 | 591 | } else { |
aoqi@0 | 592 | if (TraceICs) { |
aoqi@0 | 593 | ResourceMark rm; |
aoqi@0 | 594 | tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT, |
aoqi@0 | 595 | p2i(instruction_address()), |
aoqi@0 | 596 | p2i(info.entry())); |
aoqi@0 | 597 | } |
aoqi@0 | 598 | // Call to compiled code |
aoqi@0 | 599 | assert (CodeCache::contains(info.entry()), "wrong entry point"); |
aoqi@0 | 600 | set_destination_mt_safe(info.entry()); |
aoqi@0 | 601 | } |
aoqi@0 | 602 | } |
aoqi@0 | 603 | |
aoqi@0 | 604 | |
aoqi@0 | 605 | // Compute settings for a CompiledStaticCall. Since we might have to set |
aoqi@0 | 606 | // the stub when calling to the interpreter, we need to return arguments. |
aoqi@0 | 607 | void CompiledStaticCall::compute_entry(methodHandle m, StaticCallInfo& info) { |
aoqi@0 | 608 | nmethod* m_code = m->code(); |
aoqi@0 | 609 | info._callee = m; |
aoqi@0 | 610 | if (m_code != NULL && m_code->is_in_use()) { |
aoqi@0 | 611 | info._to_interpreter = false; |
aoqi@0 | 612 | info._entry = m_code->verified_entry_point(); |
aoqi@0 | 613 | } else { |
aoqi@0 | 614 | // Callee is interpreted code. In any case entering the interpreter |
aoqi@0 | 615 | // puts a converter-frame on the stack to save arguments. |
iveresov@7146 | 616 | assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics"); |
aoqi@0 | 617 | info._to_interpreter = true; |
aoqi@0 | 618 | info._entry = m()->get_c2i_entry(); |
aoqi@0 | 619 | } |
aoqi@0 | 620 | } |
aoqi@0 | 621 | |
aoqi@0 | 622 | address CompiledStaticCall::find_stub() { |
aoqi@0 | 623 | // Find reloc. information containing this call-site |
aoqi@0 | 624 | RelocIterator iter((nmethod*)NULL, instruction_address()); |
aoqi@0 | 625 | while (iter.next()) { |
aoqi@0 | 626 | if (iter.addr() == instruction_address()) { |
aoqi@0 | 627 | switch(iter.type()) { |
aoqi@0 | 628 | case relocInfo::static_call_type: |
aoqi@0 | 629 | return iter.static_call_reloc()->static_stub(); |
aoqi@0 | 630 | // We check here for opt_virtual_call_type, since we reuse the code |
aoqi@0 | 631 | // from the CompiledIC implementation |
aoqi@0 | 632 | case relocInfo::opt_virtual_call_type: |
aoqi@0 | 633 | return iter.opt_virtual_call_reloc()->static_stub(); |
aoqi@0 | 634 | case relocInfo::poll_type: |
aoqi@0 | 635 | case relocInfo::poll_return_type: // A safepoint can't overlap a call. |
aoqi@0 | 636 | default: |
aoqi@0 | 637 | ShouldNotReachHere(); |
aoqi@0 | 638 | } |
aoqi@0 | 639 | } |
aoqi@0 | 640 | } |
aoqi@0 | 641 | return NULL; |
aoqi@0 | 642 | } |
aoqi@0 | 643 | |
aoqi@0 | 644 | |
aoqi@0 | 645 | //----------------------------------------------------------------------------- |
aoqi@0 | 646 | // Non-product mode code |
aoqi@0 | 647 | #ifndef PRODUCT |
aoqi@0 | 648 | |
aoqi@0 | 649 | void CompiledIC::verify() { |
aoqi@0 | 650 | // make sure code pattern is actually a call imm32 instruction |
aoqi@0 | 651 | _ic_call->verify(); |
aoqi@0 | 652 | if (os::is_MP()) { |
aoqi@0 | 653 | _ic_call->verify_alignment(); |
aoqi@0 | 654 | } |
aoqi@0 | 655 | assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted() |
aoqi@0 | 656 | || is_optimized() || is_megamorphic(), "sanity check"); |
aoqi@0 | 657 | } |
aoqi@0 | 658 | |
aoqi@0 | 659 | void CompiledIC::print() { |
aoqi@0 | 660 | print_compiled_ic(); |
aoqi@0 | 661 | tty->cr(); |
aoqi@0 | 662 | } |
aoqi@0 | 663 | |
aoqi@0 | 664 | void CompiledIC::print_compiled_ic() { |
aoqi@0 | 665 | tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT, |
aoqi@0 | 666 | p2i(instruction_address()), is_call_to_interpreted() ? "interpreted " : "", p2i(ic_destination()), p2i(is_optimized() ? NULL : cached_value())); |
aoqi@0 | 667 | } |
aoqi@0 | 668 | |
aoqi@0 | 669 | void CompiledStaticCall::print() { |
aoqi@0 | 670 | tty->print("static call at " INTPTR_FORMAT " -> ", p2i(instruction_address())); |
aoqi@0 | 671 | if (is_clean()) { |
aoqi@0 | 672 | tty->print("clean"); |
aoqi@0 | 673 | } else if (is_call_to_compiled()) { |
aoqi@0 | 674 | tty->print("compiled"); |
aoqi@0 | 675 | } else if (is_call_to_interpreted()) { |
aoqi@0 | 676 | tty->print("interpreted"); |
aoqi@0 | 677 | } |
aoqi@0 | 678 | tty->cr(); |
aoqi@0 | 679 | } |
aoqi@0 | 680 | |
aoqi@0 | 681 | #endif // !PRODUCT |