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