Tue, 08 Aug 2017 15:57:29 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "code/codeCache.hpp" |
aoqi@0 | 27 | #include "code/compiledIC.hpp" |
aoqi@0 | 28 | #include "code/icBuffer.hpp" |
aoqi@0 | 29 | #include "code/nmethod.hpp" |
aoqi@0 | 30 | #include "code/scopeDesc.hpp" |
aoqi@0 | 31 | #include "gc_interface/collectedHeap.inline.hpp" |
aoqi@0 | 32 | #include "interpreter/interpreter.hpp" |
aoqi@0 | 33 | #include "interpreter/linkResolver.hpp" |
aoqi@0 | 34 | #include "memory/resourceArea.hpp" |
aoqi@0 | 35 | #include "memory/universe.inline.hpp" |
aoqi@0 | 36 | #include "oops/method.hpp" |
aoqi@0 | 37 | #include "oops/oop.inline.hpp" |
aoqi@0 | 38 | #include "oops/oop.inline2.hpp" |
aoqi@0 | 39 | #include "runtime/mutexLocker.hpp" |
aoqi@0 | 40 | #include "runtime/stubRoutines.hpp" |
aoqi@0 | 41 | |
aoqi@0 | 42 | PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
aoqi@0 | 43 | |
aoqi@0 | 44 | DEF_STUB_INTERFACE(ICStub); |
aoqi@0 | 45 | |
aoqi@0 | 46 | StubQueue* InlineCacheBuffer::_buffer = NULL; |
aoqi@0 | 47 | ICStub* InlineCacheBuffer::_next_stub = NULL; |
aoqi@0 | 48 | |
aoqi@0 | 49 | CompiledICHolder* InlineCacheBuffer::_pending_released = NULL; |
aoqi@0 | 50 | int InlineCacheBuffer::_pending_count = 0; |
aoqi@0 | 51 | |
aoqi@0 | 52 | void ICStub::finalize() { |
aoqi@0 | 53 | if (!is_empty()) { |
aoqi@0 | 54 | ResourceMark rm; |
aoqi@0 | 55 | CompiledIC *ic = CompiledIC_at(CodeCache::find_nmethod(ic_site()), ic_site()); |
aoqi@0 | 56 | assert(CodeCache::find_nmethod(ic->instruction_address()) != NULL, "inline cache in non-nmethod?"); |
aoqi@0 | 57 | |
aoqi@0 | 58 | assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer"); |
aoqi@0 | 59 | ic->set_ic_destination_and_value(destination(), cached_value()); |
aoqi@0 | 60 | } |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | |
aoqi@0 | 64 | address ICStub::destination() const { |
aoqi@0 | 65 | return InlineCacheBuffer::ic_buffer_entry_point(code_begin()); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | |
aoqi@0 | 68 | void* ICStub::cached_value() const { |
aoqi@0 | 69 | return InlineCacheBuffer::ic_buffer_cached_value(code_begin()); |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | |
aoqi@0 | 73 | void ICStub::set_stub(CompiledIC *ic, void* cached_val, address dest_addr) { |
aoqi@0 | 74 | // We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we |
aoqi@0 | 75 | // store the location of the inline cache. Then we have enough information recreate the CompiledIC |
aoqi@0 | 76 | // object when we need to remove the stub. |
aoqi@0 | 77 | _ic_site = ic->instruction_address(); |
aoqi@0 | 78 | |
aoqi@0 | 79 | // Assemble new stub |
aoqi@0 | 80 | InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_val, dest_addr); |
aoqi@0 | 81 | assert(destination() == dest_addr, "can recover destination"); |
aoqi@0 | 82 | assert(cached_value() == cached_val, "can recover destination"); |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | |
aoqi@0 | 86 | void ICStub::clear() { |
aoqi@0 | 87 | if (CompiledIC::is_icholder_entry(destination())) { |
aoqi@0 | 88 | InlineCacheBuffer::queue_for_release((CompiledICHolder*)cached_value()); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | _ic_site = NULL; |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | |
aoqi@0 | 94 | #ifndef PRODUCT |
aoqi@0 | 95 | // anybody calling to this stub will trap |
aoqi@0 | 96 | |
aoqi@0 | 97 | void ICStub::verify() { |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | void ICStub::print() { |
aoqi@0 | 101 | tty->print_cr("ICStub: site: " INTPTR_FORMAT, _ic_site); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | #endif |
aoqi@0 | 104 | |
aoqi@0 | 105 | //----------------------------------------------------------------------------------------------- |
aoqi@0 | 106 | // Implementation of InlineCacheBuffer |
aoqi@0 | 107 | |
aoqi@0 | 108 | void InlineCacheBuffer::init_next_stub() { |
aoqi@0 | 109 | ICStub* ic_stub = (ICStub*)buffer()->request_committed (ic_stub_code_size()); |
aoqi@0 | 110 | assert (ic_stub != NULL, "no room for a single stub"); |
aoqi@0 | 111 | set_next_stub(ic_stub); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | void InlineCacheBuffer::initialize() { |
aoqi@0 | 115 | if (_buffer != NULL) return; // already initialized |
aoqi@0 | 116 | _buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer"); |
aoqi@0 | 117 | assert (_buffer != NULL, "cannot allocate InlineCacheBuffer"); |
aoqi@0 | 118 | init_next_stub(); |
aoqi@0 | 119 | } |
aoqi@0 | 120 | |
aoqi@0 | 121 | |
aoqi@0 | 122 | ICStub* InlineCacheBuffer::new_ic_stub() { |
aoqi@0 | 123 | while (true) { |
aoqi@0 | 124 | ICStub* ic_stub = (ICStub*)buffer()->request_committed(ic_stub_code_size()); |
aoqi@0 | 125 | if (ic_stub != NULL) { |
aoqi@0 | 126 | return ic_stub; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | // we ran out of inline cache buffer space; must enter safepoint. |
aoqi@0 | 129 | // We do this by forcing a safepoint |
aoqi@0 | 130 | EXCEPTION_MARK; |
aoqi@0 | 131 | |
aoqi@0 | 132 | VM_ForceSafepoint vfs; |
aoqi@0 | 133 | VMThread::execute(&vfs); |
aoqi@0 | 134 | // We could potential get an async. exception at this point. |
aoqi@0 | 135 | // In that case we will rethrow it to ourselvs. |
aoqi@0 | 136 | if (HAS_PENDING_EXCEPTION) { |
aoqi@0 | 137 | oop exception = PENDING_EXCEPTION; |
aoqi@0 | 138 | CLEAR_PENDING_EXCEPTION; |
aoqi@0 | 139 | Thread::send_async_exception(JavaThread::current()->threadObj(), exception); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |
aoqi@0 | 142 | ShouldNotReachHere(); |
aoqi@0 | 143 | return NULL; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | |
aoqi@0 | 147 | void InlineCacheBuffer::update_inline_caches() { |
aoqi@0 | 148 | if (buffer()->number_of_stubs() > 1) { |
aoqi@0 | 149 | if (TraceICBuffer) { |
aoqi@0 | 150 | tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs()); |
aoqi@0 | 151 | } |
aoqi@0 | 152 | buffer()->remove_all(); |
aoqi@0 | 153 | init_next_stub(); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | release_pending_icholders(); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | |
aoqi@0 | 159 | bool InlineCacheBuffer::contains(address instruction_address) { |
aoqi@0 | 160 | return buffer()->contains(instruction_address); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | |
aoqi@0 | 164 | bool InlineCacheBuffer::is_empty() { |
aoqi@0 | 165 | return buffer()->number_of_stubs() == 1; // always has sentinel |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | |
aoqi@0 | 169 | void InlineCacheBuffer_init() { |
aoqi@0 | 170 | InlineCacheBuffer::initialize(); |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | |
aoqi@0 | 174 | void InlineCacheBuffer::create_transition_stub(CompiledIC *ic, void* cached_value, address entry) { |
aoqi@0 | 175 | assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint"); |
aoqi@0 | 176 | assert (CompiledIC_lock->is_locked(), ""); |
aoqi@0 | 177 | if (TraceICBuffer) { |
aoqi@0 | 178 | tty->print_cr(" create transition stub for " INTPTR_FORMAT " destination " INTPTR_FORMAT " cached value " INTPTR_FORMAT, |
aoqi@0 | 179 | ic->instruction_address(), entry, cached_value); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | |
aoqi@0 | 182 | // If an transition stub is already associate with the inline cache, then we remove the association. |
aoqi@0 | 183 | if (ic->is_in_transition_state()) { |
aoqi@0 | 184 | ICStub* old_stub = ICStub_from_destination_address(ic->stub_address()); |
aoqi@0 | 185 | old_stub->clear(); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | // allocate and initialize new "out-of-line" inline-cache |
aoqi@0 | 189 | ICStub* ic_stub = get_next_stub(); |
aoqi@0 | 190 | ic_stub->set_stub(ic, cached_value, entry); |
aoqi@0 | 191 | |
aoqi@0 | 192 | // Update inline cache in nmethod to point to new "out-of-line" allocated inline cache |
aoqi@0 | 193 | ic->set_ic_destination(ic_stub); |
aoqi@0 | 194 | |
aoqi@0 | 195 | set_next_stub(new_ic_stub()); // can cause safepoint synchronization |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | |
aoqi@0 | 199 | address InlineCacheBuffer::ic_destination_for(CompiledIC *ic) { |
aoqi@0 | 200 | ICStub* stub = ICStub_from_destination_address(ic->stub_address()); |
aoqi@0 | 201 | return stub->destination(); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | |
aoqi@0 | 204 | |
aoqi@0 | 205 | void* InlineCacheBuffer::cached_value_for(CompiledIC *ic) { |
aoqi@0 | 206 | ICStub* stub = ICStub_from_destination_address(ic->stub_address()); |
aoqi@0 | 207 | return stub->cached_value(); |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | |
aoqi@0 | 211 | // Free CompiledICHolder*s that are no longer in use |
aoqi@0 | 212 | void InlineCacheBuffer::release_pending_icholders() { |
aoqi@0 | 213 | assert(SafepointSynchronize::is_at_safepoint(), "should only be called during a safepoint"); |
aoqi@0 | 214 | CompiledICHolder* holder = _pending_released; |
aoqi@0 | 215 | _pending_released = NULL; |
aoqi@0 | 216 | while (holder != NULL) { |
aoqi@0 | 217 | CompiledICHolder* next = holder->next(); |
aoqi@0 | 218 | delete holder; |
aoqi@0 | 219 | holder = next; |
aoqi@0 | 220 | _pending_count--; |
aoqi@0 | 221 | } |
aoqi@0 | 222 | assert(_pending_count == 0, "wrong count"); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | // Enqueue this icholder for release during the next safepoint. It's |
aoqi@0 | 226 | // not safe to free them until them since they might be visible to |
aoqi@0 | 227 | // another thread. |
aoqi@0 | 228 | void InlineCacheBuffer::queue_for_release(CompiledICHolder* icholder) { |
aoqi@0 | 229 | MutexLockerEx mex(InlineCacheBuffer_lock); |
aoqi@0 | 230 | icholder->set_next(_pending_released); |
aoqi@0 | 231 | _pending_released = icholder; |
aoqi@0 | 232 | _pending_count++; |
aoqi@0 | 233 | if (TraceICBuffer) { |
aoqi@0 | 234 | tty->print_cr("enqueueing icholder " INTPTR_FORMAT " to be freed", icholder); |
aoqi@0 | 235 | } |
aoqi@0 | 236 | } |