src/share/vm/runtime/deoptimization.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 1
2d8a650513c2
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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 "classfile/systemDictionary.hpp"
aoqi@0 27 #include "code/debugInfoRec.hpp"
aoqi@0 28 #include "code/nmethod.hpp"
aoqi@0 29 #include "code/pcDesc.hpp"
aoqi@0 30 #include "code/scopeDesc.hpp"
aoqi@0 31 #include "interpreter/bytecode.hpp"
aoqi@0 32 #include "interpreter/interpreter.hpp"
aoqi@0 33 #include "interpreter/oopMapCache.hpp"
aoqi@0 34 #include "memory/allocation.inline.hpp"
aoqi@0 35 #include "memory/oopFactory.hpp"
aoqi@0 36 #include "memory/resourceArea.hpp"
aoqi@0 37 #include "oops/method.hpp"
aoqi@0 38 #include "oops/oop.inline.hpp"
aoqi@0 39 #include "prims/jvmtiThreadState.hpp"
aoqi@0 40 #include "runtime/biasedLocking.hpp"
aoqi@0 41 #include "runtime/compilationPolicy.hpp"
aoqi@0 42 #include "runtime/deoptimization.hpp"
aoqi@0 43 #include "runtime/interfaceSupport.hpp"
aoqi@0 44 #include "runtime/sharedRuntime.hpp"
aoqi@0 45 #include "runtime/signature.hpp"
aoqi@0 46 #include "runtime/stubRoutines.hpp"
aoqi@0 47 #include "runtime/thread.hpp"
aoqi@0 48 #include "runtime/vframe.hpp"
aoqi@0 49 #include "runtime/vframeArray.hpp"
aoqi@0 50 #include "runtime/vframe_hp.hpp"
aoqi@0 51 #include "utilities/events.hpp"
aoqi@0 52 #include "utilities/xmlstream.hpp"
aoqi@0 53 #ifdef TARGET_ARCH_x86
aoqi@0 54 # include "vmreg_x86.inline.hpp"
aoqi@0 55 #endif
aoqi@0 56 #ifdef TARGET_ARCH_sparc
aoqi@0 57 # include "vmreg_sparc.inline.hpp"
aoqi@0 58 #endif
aoqi@0 59 #ifdef TARGET_ARCH_zero
aoqi@0 60 # include "vmreg_zero.inline.hpp"
aoqi@0 61 #endif
aoqi@0 62 #ifdef TARGET_ARCH_arm
aoqi@0 63 # include "vmreg_arm.inline.hpp"
aoqi@0 64 #endif
aoqi@0 65 #ifdef TARGET_ARCH_ppc
aoqi@0 66 # include "vmreg_ppc.inline.hpp"
aoqi@0 67 #endif
aoqi@0 68 #ifdef COMPILER2
aoqi@0 69 #ifdef TARGET_ARCH_MODEL_x86_32
aoqi@0 70 # include "adfiles/ad_x86_32.hpp"
aoqi@0 71 #endif
aoqi@0 72 #ifdef TARGET_ARCH_MODEL_x86_64
aoqi@0 73 # include "adfiles/ad_x86_64.hpp"
aoqi@0 74 #endif
aoqi@0 75 #ifdef TARGET_ARCH_MODEL_sparc
aoqi@0 76 # include "adfiles/ad_sparc.hpp"
aoqi@0 77 #endif
aoqi@0 78 #ifdef TARGET_ARCH_MODEL_zero
aoqi@0 79 # include "adfiles/ad_zero.hpp"
aoqi@0 80 #endif
aoqi@0 81 #ifdef TARGET_ARCH_MODEL_arm
aoqi@0 82 # include "adfiles/ad_arm.hpp"
aoqi@0 83 #endif
aoqi@0 84 #ifdef TARGET_ARCH_MODEL_ppc_32
aoqi@0 85 # include "adfiles/ad_ppc_32.hpp"
aoqi@0 86 #endif
aoqi@0 87 #ifdef TARGET_ARCH_MODEL_ppc_64
aoqi@0 88 # include "adfiles/ad_ppc_64.hpp"
aoqi@0 89 #endif
aoqi@0 90 #endif // COMPILER2
aoqi@0 91
aoqi@0 92 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 93
aoqi@0 94 bool DeoptimizationMarker::_is_active = false;
aoqi@0 95
aoqi@0 96 Deoptimization::UnrollBlock::UnrollBlock(int size_of_deoptimized_frame,
aoqi@0 97 int caller_adjustment,
aoqi@0 98 int caller_actual_parameters,
aoqi@0 99 int number_of_frames,
aoqi@0 100 intptr_t* frame_sizes,
aoqi@0 101 address* frame_pcs,
aoqi@0 102 BasicType return_type) {
aoqi@0 103 _size_of_deoptimized_frame = size_of_deoptimized_frame;
aoqi@0 104 _caller_adjustment = caller_adjustment;
aoqi@0 105 _caller_actual_parameters = caller_actual_parameters;
aoqi@0 106 _number_of_frames = number_of_frames;
aoqi@0 107 _frame_sizes = frame_sizes;
aoqi@0 108 _frame_pcs = frame_pcs;
aoqi@0 109 _register_block = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler);
aoqi@0 110 _return_type = return_type;
aoqi@0 111 _initial_info = 0;
aoqi@0 112 // PD (x86 only)
aoqi@0 113 _counter_temp = 0;
aoqi@0 114 _unpack_kind = 0;
aoqi@0 115 _sender_sp_temp = 0;
aoqi@0 116
aoqi@0 117 _total_frame_sizes = size_of_frames();
aoqi@0 118 }
aoqi@0 119
aoqi@0 120
aoqi@0 121 Deoptimization::UnrollBlock::~UnrollBlock() {
aoqi@0 122 FREE_C_HEAP_ARRAY(intptr_t, _frame_sizes, mtCompiler);
aoqi@0 123 FREE_C_HEAP_ARRAY(intptr_t, _frame_pcs, mtCompiler);
aoqi@0 124 FREE_C_HEAP_ARRAY(intptr_t, _register_block, mtCompiler);
aoqi@0 125 }
aoqi@0 126
aoqi@0 127
aoqi@0 128 intptr_t* Deoptimization::UnrollBlock::value_addr_at(int register_number) const {
aoqi@0 129 assert(register_number < RegisterMap::reg_count, "checking register number");
aoqi@0 130 return &_register_block[register_number * 2];
aoqi@0 131 }
aoqi@0 132
aoqi@0 133
aoqi@0 134
aoqi@0 135 int Deoptimization::UnrollBlock::size_of_frames() const {
aoqi@0 136 // Acount first for the adjustment of the initial frame
aoqi@0 137 int result = _caller_adjustment;
aoqi@0 138 for (int index = 0; index < number_of_frames(); index++) {
aoqi@0 139 result += frame_sizes()[index];
aoqi@0 140 }
aoqi@0 141 return result;
aoqi@0 142 }
aoqi@0 143
aoqi@0 144
aoqi@0 145 void Deoptimization::UnrollBlock::print() {
aoqi@0 146 ttyLocker ttyl;
aoqi@0 147 tty->print_cr("UnrollBlock");
aoqi@0 148 tty->print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
aoqi@0 149 tty->print( " frame_sizes: ");
aoqi@0 150 for (int index = 0; index < number_of_frames(); index++) {
aoqi@0 151 tty->print("%d ", frame_sizes()[index]);
aoqi@0 152 }
aoqi@0 153 tty->cr();
aoqi@0 154 }
aoqi@0 155
aoqi@0 156
aoqi@0 157 // In order to make fetch_unroll_info work properly with escape
aoqi@0 158 // analysis, The method was changed from JRT_LEAF to JRT_BLOCK_ENTRY and
aoqi@0 159 // ResetNoHandleMark and HandleMark were removed from it. The actual reallocation
aoqi@0 160 // of previously eliminated objects occurs in realloc_objects, which is
aoqi@0 161 // called from the method fetch_unroll_info_helper below.
aoqi@0 162 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* thread))
aoqi@0 163 // It is actually ok to allocate handles in a leaf method. It causes no safepoints,
aoqi@0 164 // but makes the entry a little slower. There is however a little dance we have to
aoqi@0 165 // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro
aoqi@0 166
aoqi@0 167 // fetch_unroll_info() is called at the beginning of the deoptimization
aoqi@0 168 // handler. Note this fact before we start generating temporary frames
aoqi@0 169 // that can confuse an asynchronous stack walker. This counter is
aoqi@0 170 // decremented at the end of unpack_frames().
aoqi@0 171 thread->inc_in_deopt_handler();
aoqi@0 172
aoqi@0 173 return fetch_unroll_info_helper(thread);
aoqi@0 174 JRT_END
aoqi@0 175
aoqi@0 176
aoqi@0 177 // This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap)
aoqi@0 178 Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* thread) {
aoqi@0 179
aoqi@0 180 // Note: there is a safepoint safety issue here. No matter whether we enter
aoqi@0 181 // via vanilla deopt or uncommon trap we MUST NOT stop at a safepoint once
aoqi@0 182 // the vframeArray is created.
aoqi@0 183 //
aoqi@0 184
aoqi@0 185 // Allocate our special deoptimization ResourceMark
aoqi@0 186 DeoptResourceMark* dmark = new DeoptResourceMark(thread);
aoqi@0 187 assert(thread->deopt_mark() == NULL, "Pending deopt!");
aoqi@0 188 thread->set_deopt_mark(dmark);
aoqi@0 189
aoqi@0 190 frame stub_frame = thread->last_frame(); // Makes stack walkable as side effect
aoqi@0 191 RegisterMap map(thread, true);
aoqi@0 192 RegisterMap dummy_map(thread, false);
aoqi@0 193 // Now get the deoptee with a valid map
aoqi@0 194 frame deoptee = stub_frame.sender(&map);
aoqi@0 195 // Set the deoptee nmethod
aoqi@0 196 assert(thread->deopt_nmethod() == NULL, "Pending deopt!");
aoqi@0 197 thread->set_deopt_nmethod(deoptee.cb()->as_nmethod_or_null());
aoqi@0 198
aoqi@0 199 if (VerifyStack) {
aoqi@0 200 thread->validate_frame_layout();
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 // Create a growable array of VFrames where each VFrame represents an inlined
aoqi@0 204 // Java frame. This storage is allocated with the usual system arena.
aoqi@0 205 assert(deoptee.is_compiled_frame(), "Wrong frame type");
aoqi@0 206 GrowableArray<compiledVFrame*>* chunk = new GrowableArray<compiledVFrame*>(10);
aoqi@0 207 vframe* vf = vframe::new_vframe(&deoptee, &map, thread);
aoqi@0 208 while (!vf->is_top()) {
aoqi@0 209 assert(vf->is_compiled_frame(), "Wrong frame type");
aoqi@0 210 chunk->push(compiledVFrame::cast(vf));
aoqi@0 211 vf = vf->sender();
aoqi@0 212 }
aoqi@0 213 assert(vf->is_compiled_frame(), "Wrong frame type");
aoqi@0 214 chunk->push(compiledVFrame::cast(vf));
aoqi@0 215
aoqi@0 216 #ifdef COMPILER2
aoqi@0 217 // Reallocate the non-escaping objects and restore their fields. Then
aoqi@0 218 // relock objects if synchronization on them was eliminated.
aoqi@0 219 if (DoEscapeAnalysis || EliminateNestedLocks) {
aoqi@0 220 if (EliminateAllocations) {
aoqi@0 221 assert (chunk->at(0)->scope() != NULL,"expect only compiled java frames");
aoqi@0 222 GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects();
aoqi@0 223
aoqi@0 224 // The flag return_oop() indicates call sites which return oop
aoqi@0 225 // in compiled code. Such sites include java method calls,
aoqi@0 226 // runtime calls (for example, used to allocate new objects/arrays
aoqi@0 227 // on slow code path) and any other calls generated in compiled code.
aoqi@0 228 // It is not guaranteed that we can get such information here only
aoqi@0 229 // by analyzing bytecode in deoptimized frames. This is why this flag
aoqi@0 230 // is set during method compilation (see Compile::Process_OopMap_Node()).
aoqi@0 231 bool save_oop_result = chunk->at(0)->scope()->return_oop();
aoqi@0 232 Handle return_value;
aoqi@0 233 if (save_oop_result) {
aoqi@0 234 // Reallocation may trigger GC. If deoptimization happened on return from
aoqi@0 235 // call which returns oop we need to save it since it is not in oopmap.
aoqi@0 236 oop result = deoptee.saved_oop_result(&map);
aoqi@0 237 assert(result == NULL || result->is_oop(), "must be oop");
aoqi@0 238 return_value = Handle(thread, result);
aoqi@0 239 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
aoqi@0 240 if (TraceDeoptimization) {
aoqi@0 241 ttyLocker ttyl;
aoqi@0 242 tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread);
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245 bool reallocated = false;
aoqi@0 246 if (objects != NULL) {
aoqi@0 247 JRT_BLOCK
aoqi@0 248 reallocated = realloc_objects(thread, &deoptee, objects, THREAD);
aoqi@0 249 JRT_END
aoqi@0 250 }
aoqi@0 251 if (reallocated) {
aoqi@0 252 reassign_fields(&deoptee, &map, objects);
aoqi@0 253 #ifndef PRODUCT
aoqi@0 254 if (TraceDeoptimization) {
aoqi@0 255 ttyLocker ttyl;
aoqi@0 256 tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread);
aoqi@0 257 print_objects(objects);
aoqi@0 258 }
aoqi@0 259 #endif
aoqi@0 260 }
aoqi@0 261 if (save_oop_result) {
aoqi@0 262 // Restore result.
aoqi@0 263 deoptee.set_saved_oop_result(&map, return_value());
aoqi@0 264 }
aoqi@0 265 }
aoqi@0 266 if (EliminateLocks) {
aoqi@0 267 #ifndef PRODUCT
aoqi@0 268 bool first = true;
aoqi@0 269 #endif
aoqi@0 270 for (int i = 0; i < chunk->length(); i++) {
aoqi@0 271 compiledVFrame* cvf = chunk->at(i);
aoqi@0 272 assert (cvf->scope() != NULL,"expect only compiled java frames");
aoqi@0 273 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
aoqi@0 274 if (monitors->is_nonempty()) {
aoqi@0 275 relock_objects(monitors, thread);
aoqi@0 276 #ifndef PRODUCT
aoqi@0 277 if (TraceDeoptimization) {
aoqi@0 278 ttyLocker ttyl;
aoqi@0 279 for (int j = 0; j < monitors->length(); j++) {
aoqi@0 280 MonitorInfo* mi = monitors->at(j);
aoqi@0 281 if (mi->eliminated()) {
aoqi@0 282 if (first) {
aoqi@0 283 first = false;
aoqi@0 284 tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread);
aoqi@0 285 }
aoqi@0 286 tty->print_cr(" object <" INTPTR_FORMAT "> locked", (void *)mi->owner());
aoqi@0 287 }
aoqi@0 288 }
aoqi@0 289 }
aoqi@0 290 #endif
aoqi@0 291 }
aoqi@0 292 }
aoqi@0 293 }
aoqi@0 294 }
aoqi@0 295 #endif // COMPILER2
aoqi@0 296 // Ensure that no safepoint is taken after pointers have been stored
aoqi@0 297 // in fields of rematerialized objects. If a safepoint occurs from here on
aoqi@0 298 // out the java state residing in the vframeArray will be missed.
aoqi@0 299 No_Safepoint_Verifier no_safepoint;
aoqi@0 300
aoqi@0 301 vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk);
aoqi@0 302
aoqi@0 303 assert(thread->vframe_array_head() == NULL, "Pending deopt!");;
aoqi@0 304 thread->set_vframe_array_head(array);
aoqi@0 305
aoqi@0 306 // Now that the vframeArray has been created if we have any deferred local writes
aoqi@0 307 // added by jvmti then we can free up that structure as the data is now in the
aoqi@0 308 // vframeArray
aoqi@0 309
aoqi@0 310 if (thread->deferred_locals() != NULL) {
aoqi@0 311 GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread->deferred_locals();
aoqi@0 312 int i = 0;
aoqi@0 313 do {
aoqi@0 314 // Because of inlining we could have multiple vframes for a single frame
aoqi@0 315 // and several of the vframes could have deferred writes. Find them all.
aoqi@0 316 if (list->at(i)->id() == array->original().id()) {
aoqi@0 317 jvmtiDeferredLocalVariableSet* dlv = list->at(i);
aoqi@0 318 list->remove_at(i);
aoqi@0 319 // individual jvmtiDeferredLocalVariableSet are CHeapObj's
aoqi@0 320 delete dlv;
aoqi@0 321 } else {
aoqi@0 322 i++;
aoqi@0 323 }
aoqi@0 324 } while ( i < list->length() );
aoqi@0 325 if (list->length() == 0) {
aoqi@0 326 thread->set_deferred_locals(NULL);
aoqi@0 327 // free the list and elements back to C heap.
aoqi@0 328 delete list;
aoqi@0 329 }
aoqi@0 330
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 #ifndef SHARK
aoqi@0 334 // Compute the caller frame based on the sender sp of stub_frame and stored frame sizes info.
aoqi@0 335 CodeBlob* cb = stub_frame.cb();
aoqi@0 336 // Verify we have the right vframeArray
aoqi@0 337 assert(cb->frame_size() >= 0, "Unexpected frame size");
aoqi@0 338 intptr_t* unpack_sp = stub_frame.sp() + cb->frame_size();
aoqi@0 339
aoqi@0 340 // If the deopt call site is a MethodHandle invoke call site we have
aoqi@0 341 // to adjust the unpack_sp.
aoqi@0 342 nmethod* deoptee_nm = deoptee.cb()->as_nmethod_or_null();
aoqi@0 343 if (deoptee_nm != NULL && deoptee_nm->is_method_handle_return(deoptee.pc()))
aoqi@0 344 unpack_sp = deoptee.unextended_sp();
aoqi@0 345
aoqi@0 346 #ifdef ASSERT
aoqi@0 347 assert(cb->is_deoptimization_stub() || cb->is_uncommon_trap_stub(), "just checking");
aoqi@0 348 #endif
aoqi@0 349 #else
aoqi@0 350 intptr_t* unpack_sp = stub_frame.sender(&dummy_map).unextended_sp();
aoqi@0 351 #endif // !SHARK
aoqi@0 352
aoqi@0 353 // This is a guarantee instead of an assert because if vframe doesn't match
aoqi@0 354 // we will unpack the wrong deoptimized frame and wind up in strange places
aoqi@0 355 // where it will be very difficult to figure out what went wrong. Better
aoqi@0 356 // to die an early death here than some very obscure death later when the
aoqi@0 357 // trail is cold.
aoqi@0 358 // Note: on ia64 this guarantee can be fooled by frames with no memory stack
aoqi@0 359 // in that it will fail to detect a problem when there is one. This needs
aoqi@0 360 // more work in tiger timeframe.
aoqi@0 361 guarantee(array->unextended_sp() == unpack_sp, "vframe_array_head must contain the vframeArray to unpack");
aoqi@0 362
aoqi@0 363 int number_of_frames = array->frames();
aoqi@0 364
aoqi@0 365 // Compute the vframes' sizes. Note that frame_sizes[] entries are ordered from outermost to innermost
aoqi@0 366 // virtual activation, which is the reverse of the elements in the vframes array.
aoqi@0 367 intptr_t* frame_sizes = NEW_C_HEAP_ARRAY(intptr_t, number_of_frames, mtCompiler);
aoqi@0 368 // +1 because we always have an interpreter return address for the final slot.
aoqi@0 369 address* frame_pcs = NEW_C_HEAP_ARRAY(address, number_of_frames + 1, mtCompiler);
aoqi@0 370 int popframe_extra_args = 0;
aoqi@0 371 // Create an interpreter return address for the stub to use as its return
aoqi@0 372 // address so the skeletal frames are perfectly walkable
aoqi@0 373 frame_pcs[number_of_frames] = Interpreter::deopt_entry(vtos, 0);
aoqi@0 374
aoqi@0 375 // PopFrame requires that the preserved incoming arguments from the recently-popped topmost
aoqi@0 376 // activation be put back on the expression stack of the caller for reexecution
aoqi@0 377 if (JvmtiExport::can_pop_frame() && thread->popframe_forcing_deopt_reexecution()) {
aoqi@0 378 popframe_extra_args = in_words(thread->popframe_preserved_args_size_in_words());
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 // Find the current pc for sender of the deoptee. Since the sender may have been deoptimized
aoqi@0 382 // itself since the deoptee vframeArray was created we must get a fresh value of the pc rather
aoqi@0 383 // than simply use array->sender.pc(). This requires us to walk the current set of frames
aoqi@0 384 //
aoqi@0 385 frame deopt_sender = stub_frame.sender(&dummy_map); // First is the deoptee frame
aoqi@0 386 deopt_sender = deopt_sender.sender(&dummy_map); // Now deoptee caller
aoqi@0 387
aoqi@0 388 // It's possible that the number of paramters at the call site is
aoqi@0 389 // different than number of arguments in the callee when method
aoqi@0 390 // handles are used. If the caller is interpreted get the real
aoqi@0 391 // value so that the proper amount of space can be added to it's
aoqi@0 392 // frame.
aoqi@0 393 bool caller_was_method_handle = false;
aoqi@0 394 if (deopt_sender.is_interpreted_frame()) {
aoqi@0 395 methodHandle method = deopt_sender.interpreter_frame_method();
aoqi@0 396 Bytecode_invoke cur = Bytecode_invoke_check(method, deopt_sender.interpreter_frame_bci());
aoqi@0 397 if (cur.is_invokedynamic() || cur.is_invokehandle()) {
aoqi@0 398 // Method handle invokes may involve fairly arbitrary chains of
aoqi@0 399 // calls so it's impossible to know how much actual space the
aoqi@0 400 // caller has for locals.
aoqi@0 401 caller_was_method_handle = true;
aoqi@0 402 }
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 //
aoqi@0 406 // frame_sizes/frame_pcs[0] oldest frame (int or c2i)
aoqi@0 407 // frame_sizes/frame_pcs[1] next oldest frame (int)
aoqi@0 408 // frame_sizes/frame_pcs[n] youngest frame (int)
aoqi@0 409 //
aoqi@0 410 // Now a pc in frame_pcs is actually the return address to the frame's caller (a frame
aoqi@0 411 // owns the space for the return address to it's caller). Confusing ain't it.
aoqi@0 412 //
aoqi@0 413 // The vframe array can address vframes with indices running from
aoqi@0 414 // 0.._frames-1. Index 0 is the youngest frame and _frame - 1 is the oldest (root) frame.
aoqi@0 415 // When we create the skeletal frames we need the oldest frame to be in the zero slot
aoqi@0 416 // in the frame_sizes/frame_pcs so the assembly code can do a trivial walk.
aoqi@0 417 // so things look a little strange in this loop.
aoqi@0 418 //
aoqi@0 419 int callee_parameters = 0;
aoqi@0 420 int callee_locals = 0;
aoqi@0 421 for (int index = 0; index < array->frames(); index++ ) {
aoqi@0 422 // frame[number_of_frames - 1 ] = on_stack_size(youngest)
aoqi@0 423 // frame[number_of_frames - 2 ] = on_stack_size(sender(youngest))
aoqi@0 424 // frame[number_of_frames - 3 ] = on_stack_size(sender(sender(youngest)))
aoqi@0 425 frame_sizes[number_of_frames - 1 - index] = BytesPerWord * array->element(index)->on_stack_size(callee_parameters,
aoqi@0 426 callee_locals,
aoqi@0 427 index == 0,
aoqi@0 428 popframe_extra_args);
aoqi@0 429 // This pc doesn't have to be perfect just good enough to identify the frame
aoqi@0 430 // as interpreted so the skeleton frame will be walkable
aoqi@0 431 // The correct pc will be set when the skeleton frame is completely filled out
aoqi@0 432 // The final pc we store in the loop is wrong and will be overwritten below
aoqi@0 433 frame_pcs[number_of_frames - 1 - index ] = Interpreter::deopt_entry(vtos, 0) - frame::pc_return_offset;
aoqi@0 434
aoqi@0 435 callee_parameters = array->element(index)->method()->size_of_parameters();
aoqi@0 436 callee_locals = array->element(index)->method()->max_locals();
aoqi@0 437 popframe_extra_args = 0;
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 // Compute whether the root vframe returns a float or double value.
aoqi@0 441 BasicType return_type;
aoqi@0 442 {
aoqi@0 443 HandleMark hm;
aoqi@0 444 methodHandle method(thread, array->element(0)->method());
aoqi@0 445 Bytecode_invoke invoke = Bytecode_invoke_check(method, array->element(0)->bci());
aoqi@0 446 return_type = invoke.is_valid() ? invoke.result_type() : T_ILLEGAL;
aoqi@0 447 }
aoqi@0 448
aoqi@0 449 // Compute information for handling adapters and adjusting the frame size of the caller.
aoqi@0 450 int caller_adjustment = 0;
aoqi@0 451
aoqi@0 452 // Compute the amount the oldest interpreter frame will have to adjust
aoqi@0 453 // its caller's stack by. If the caller is a compiled frame then
aoqi@0 454 // we pretend that the callee has no parameters so that the
aoqi@0 455 // extension counts for the full amount of locals and not just
aoqi@0 456 // locals-parms. This is because without a c2i adapter the parm
aoqi@0 457 // area as created by the compiled frame will not be usable by
aoqi@0 458 // the interpreter. (Depending on the calling convention there
aoqi@0 459 // may not even be enough space).
aoqi@0 460
aoqi@0 461 // QQQ I'd rather see this pushed down into last_frame_adjust
aoqi@0 462 // and have it take the sender (aka caller).
aoqi@0 463
aoqi@0 464 if (deopt_sender.is_compiled_frame() || caller_was_method_handle) {
aoqi@0 465 caller_adjustment = last_frame_adjust(0, callee_locals);
aoqi@0 466 } else if (callee_locals > callee_parameters) {
aoqi@0 467 // The caller frame may need extending to accommodate
aoqi@0 468 // non-parameter locals of the first unpacked interpreted frame.
aoqi@0 469 // Compute that adjustment.
aoqi@0 470 caller_adjustment = last_frame_adjust(callee_parameters, callee_locals);
aoqi@0 471 }
aoqi@0 472
aoqi@0 473 // If the sender is deoptimized the we must retrieve the address of the handler
aoqi@0 474 // since the frame will "magically" show the original pc before the deopt
aoqi@0 475 // and we'd undo the deopt.
aoqi@0 476
aoqi@0 477 frame_pcs[0] = deopt_sender.raw_pc();
aoqi@0 478
aoqi@0 479 #ifndef SHARK
aoqi@0 480 assert(CodeCache::find_blob_unsafe(frame_pcs[0]) != NULL, "bad pc");
aoqi@0 481 #endif // SHARK
aoqi@0 482
aoqi@0 483 UnrollBlock* info = new UnrollBlock(array->frame_size() * BytesPerWord,
aoqi@0 484 caller_adjustment * BytesPerWord,
aoqi@0 485 caller_was_method_handle ? 0 : callee_parameters,
aoqi@0 486 number_of_frames,
aoqi@0 487 frame_sizes,
aoqi@0 488 frame_pcs,
aoqi@0 489 return_type);
aoqi@0 490 // On some platforms, we need a way to pass some platform dependent
aoqi@0 491 // information to the unpacking code so the skeletal frames come out
aoqi@0 492 // correct (initial fp value, unextended sp, ...)
aoqi@0 493 info->set_initial_info((intptr_t) array->sender().initial_deoptimization_info());
aoqi@0 494
aoqi@0 495 if (array->frames() > 1) {
aoqi@0 496 if (VerifyStack && TraceDeoptimization) {
aoqi@0 497 ttyLocker ttyl;
aoqi@0 498 tty->print_cr("Deoptimizing method containing inlining");
aoqi@0 499 }
aoqi@0 500 }
aoqi@0 501
aoqi@0 502 array->set_unroll_block(info);
aoqi@0 503 return info;
aoqi@0 504 }
aoqi@0 505
aoqi@0 506 // Called to cleanup deoptimization data structures in normal case
aoqi@0 507 // after unpacking to stack and when stack overflow error occurs
aoqi@0 508 void Deoptimization::cleanup_deopt_info(JavaThread *thread,
aoqi@0 509 vframeArray *array) {
aoqi@0 510
aoqi@0 511 // Get array if coming from exception
aoqi@0 512 if (array == NULL) {
aoqi@0 513 array = thread->vframe_array_head();
aoqi@0 514 }
aoqi@0 515 thread->set_vframe_array_head(NULL);
aoqi@0 516
aoqi@0 517 // Free the previous UnrollBlock
aoqi@0 518 vframeArray* old_array = thread->vframe_array_last();
aoqi@0 519 thread->set_vframe_array_last(array);
aoqi@0 520
aoqi@0 521 if (old_array != NULL) {
aoqi@0 522 UnrollBlock* old_info = old_array->unroll_block();
aoqi@0 523 old_array->set_unroll_block(NULL);
aoqi@0 524 delete old_info;
aoqi@0 525 delete old_array;
aoqi@0 526 }
aoqi@0 527
aoqi@0 528 // Deallocate any resource creating in this routine and any ResourceObjs allocated
aoqi@0 529 // inside the vframeArray (StackValueCollections)
aoqi@0 530
aoqi@0 531 delete thread->deopt_mark();
aoqi@0 532 thread->set_deopt_mark(NULL);
aoqi@0 533 thread->set_deopt_nmethod(NULL);
aoqi@0 534
aoqi@0 535
aoqi@0 536 if (JvmtiExport::can_pop_frame()) {
aoqi@0 537 #ifndef CC_INTERP
aoqi@0 538 // Regardless of whether we entered this routine with the pending
aoqi@0 539 // popframe condition bit set, we should always clear it now
aoqi@0 540 thread->clear_popframe_condition();
aoqi@0 541 #else
aoqi@0 542 // C++ interpeter will clear has_pending_popframe when it enters
aoqi@0 543 // with method_resume. For deopt_resume2 we clear it now.
aoqi@0 544 if (thread->popframe_forcing_deopt_reexecution())
aoqi@0 545 thread->clear_popframe_condition();
aoqi@0 546 #endif /* CC_INTERP */
aoqi@0 547 }
aoqi@0 548
aoqi@0 549 // unpack_frames() is called at the end of the deoptimization handler
aoqi@0 550 // and (in C2) at the end of the uncommon trap handler. Note this fact
aoqi@0 551 // so that an asynchronous stack walker can work again. This counter is
aoqi@0 552 // incremented at the beginning of fetch_unroll_info() and (in C2) at
aoqi@0 553 // the beginning of uncommon_trap().
aoqi@0 554 thread->dec_in_deopt_handler();
aoqi@0 555 }
aoqi@0 556
aoqi@0 557
aoqi@0 558 // Return BasicType of value being returned
aoqi@0 559 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
aoqi@0 560
aoqi@0 561 // We are already active int he special DeoptResourceMark any ResourceObj's we
aoqi@0 562 // allocate will be freed at the end of the routine.
aoqi@0 563
aoqi@0 564 // It is actually ok to allocate handles in a leaf method. It causes no safepoints,
aoqi@0 565 // but makes the entry a little slower. There is however a little dance we have to
aoqi@0 566 // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro
aoqi@0 567 ResetNoHandleMark rnhm; // No-op in release/product versions
aoqi@0 568 HandleMark hm;
aoqi@0 569
aoqi@0 570 frame stub_frame = thread->last_frame();
aoqi@0 571
aoqi@0 572 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
aoqi@0 573 // must point to the vframeArray for the unpack frame.
aoqi@0 574 vframeArray* array = thread->vframe_array_head();
aoqi@0 575
aoqi@0 576 #ifndef PRODUCT
aoqi@0 577 if (TraceDeoptimization) {
aoqi@0 578 ttyLocker ttyl;
aoqi@0 579 tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d", thread, array, exec_mode);
aoqi@0 580 }
aoqi@0 581 #endif
aoqi@0 582 Events::log(thread, "DEOPT UNPACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT " mode %d",
aoqi@0 583 stub_frame.pc(), stub_frame.sp(), exec_mode);
aoqi@0 584
aoqi@0 585 UnrollBlock* info = array->unroll_block();
aoqi@0 586
aoqi@0 587 // Unpack the interpreter frames and any adapter frame (c2 only) we might create.
aoqi@0 588 array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters());
aoqi@0 589
aoqi@0 590 BasicType bt = info->return_type();
aoqi@0 591
aoqi@0 592 // If we have an exception pending, claim that the return type is an oop
aoqi@0 593 // so the deopt_blob does not overwrite the exception_oop.
aoqi@0 594
aoqi@0 595 if (exec_mode == Unpack_exception)
aoqi@0 596 bt = T_OBJECT;
aoqi@0 597
aoqi@0 598 // Cleanup thread deopt data
aoqi@0 599 cleanup_deopt_info(thread, array);
aoqi@0 600
aoqi@0 601 #ifndef PRODUCT
aoqi@0 602 if (VerifyStack) {
aoqi@0 603 ResourceMark res_mark;
aoqi@0 604
aoqi@0 605 thread->validate_frame_layout();
aoqi@0 606
aoqi@0 607 // Verify that the just-unpacked frames match the interpreter's
aoqi@0 608 // notions of expression stack and locals
aoqi@0 609 vframeArray* cur_array = thread->vframe_array_last();
aoqi@0 610 RegisterMap rm(thread, false);
aoqi@0 611 rm.set_include_argument_oops(false);
aoqi@0 612 bool is_top_frame = true;
aoqi@0 613 int callee_size_of_parameters = 0;
aoqi@0 614 int callee_max_locals = 0;
aoqi@0 615 for (int i = 0; i < cur_array->frames(); i++) {
aoqi@0 616 vframeArrayElement* el = cur_array->element(i);
aoqi@0 617 frame* iframe = el->iframe();
aoqi@0 618 guarantee(iframe->is_interpreted_frame(), "Wrong frame type");
aoqi@0 619
aoqi@0 620 // Get the oop map for this bci
aoqi@0 621 InterpreterOopMap mask;
aoqi@0 622 int cur_invoke_parameter_size = 0;
aoqi@0 623 bool try_next_mask = false;
aoqi@0 624 int next_mask_expression_stack_size = -1;
aoqi@0 625 int top_frame_expression_stack_adjustment = 0;
aoqi@0 626 methodHandle mh(thread, iframe->interpreter_frame_method());
aoqi@0 627 OopMapCache::compute_one_oop_map(mh, iframe->interpreter_frame_bci(), &mask);
aoqi@0 628 BytecodeStream str(mh);
aoqi@0 629 str.set_start(iframe->interpreter_frame_bci());
aoqi@0 630 int max_bci = mh->code_size();
aoqi@0 631 // Get to the next bytecode if possible
aoqi@0 632 assert(str.bci() < max_bci, "bci in interpreter frame out of bounds");
aoqi@0 633 // Check to see if we can grab the number of outgoing arguments
aoqi@0 634 // at an uncommon trap for an invoke (where the compiler
aoqi@0 635 // generates debug info before the invoke has executed)
aoqi@0 636 Bytecodes::Code cur_code = str.next();
aoqi@0 637 if (cur_code == Bytecodes::_invokevirtual ||
aoqi@0 638 cur_code == Bytecodes::_invokespecial ||
aoqi@0 639 cur_code == Bytecodes::_invokestatic ||
aoqi@0 640 cur_code == Bytecodes::_invokeinterface ||
aoqi@0 641 cur_code == Bytecodes::_invokedynamic) {
aoqi@0 642 Bytecode_invoke invoke(mh, iframe->interpreter_frame_bci());
aoqi@0 643 Symbol* signature = invoke.signature();
aoqi@0 644 ArgumentSizeComputer asc(signature);
aoqi@0 645 cur_invoke_parameter_size = asc.size();
aoqi@0 646 if (invoke.has_receiver()) {
aoqi@0 647 // Add in receiver
aoqi@0 648 ++cur_invoke_parameter_size;
aoqi@0 649 }
aoqi@0 650 if (i != 0 && !invoke.is_invokedynamic() && MethodHandles::has_member_arg(invoke.klass(), invoke.name())) {
aoqi@0 651 callee_size_of_parameters++;
aoqi@0 652 }
aoqi@0 653 }
aoqi@0 654 if (str.bci() < max_bci) {
aoqi@0 655 Bytecodes::Code bc = str.next();
aoqi@0 656 if (bc >= 0) {
aoqi@0 657 // The interpreter oop map generator reports results before
aoqi@0 658 // the current bytecode has executed except in the case of
aoqi@0 659 // calls. It seems to be hard to tell whether the compiler
aoqi@0 660 // has emitted debug information matching the "state before"
aoqi@0 661 // a given bytecode or the state after, so we try both
aoqi@0 662 switch (cur_code) {
aoqi@0 663 case Bytecodes::_invokevirtual:
aoqi@0 664 case Bytecodes::_invokespecial:
aoqi@0 665 case Bytecodes::_invokestatic:
aoqi@0 666 case Bytecodes::_invokeinterface:
aoqi@0 667 case Bytecodes::_invokedynamic:
aoqi@0 668 case Bytecodes::_athrow:
aoqi@0 669 break;
aoqi@0 670 default: {
aoqi@0 671 InterpreterOopMap next_mask;
aoqi@0 672 OopMapCache::compute_one_oop_map(mh, str.bci(), &next_mask);
aoqi@0 673 next_mask_expression_stack_size = next_mask.expression_stack_size();
aoqi@0 674 // Need to subtract off the size of the result type of
aoqi@0 675 // the bytecode because this is not described in the
aoqi@0 676 // debug info but returned to the interpreter in the TOS
aoqi@0 677 // caching register
aoqi@0 678 BasicType bytecode_result_type = Bytecodes::result_type(cur_code);
aoqi@0 679 if (bytecode_result_type != T_ILLEGAL) {
aoqi@0 680 top_frame_expression_stack_adjustment = type2size[bytecode_result_type];
aoqi@0 681 }
aoqi@0 682 assert(top_frame_expression_stack_adjustment >= 0, "");
aoqi@0 683 try_next_mask = true;
aoqi@0 684 break;
aoqi@0 685 }
aoqi@0 686 }
aoqi@0 687 }
aoqi@0 688 }
aoqi@0 689
aoqi@0 690 // Verify stack depth and oops in frame
aoqi@0 691 // This assertion may be dependent on the platform we're running on and may need modification (tested on x86 and sparc)
aoqi@0 692 if (!(
aoqi@0 693 /* SPARC */
aoqi@0 694 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_size_of_parameters) ||
aoqi@0 695 /* x86 */
aoqi@0 696 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) ||
aoqi@0 697 (try_next_mask &&
aoqi@0 698 (iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size -
aoqi@0 699 top_frame_expression_stack_adjustment))) ||
aoqi@0 700 (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) ||
aoqi@0 701 (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute) &&
aoqi@0 702 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size))
aoqi@0 703 )) {
aoqi@0 704 ttyLocker ttyl;
aoqi@0 705
aoqi@0 706 // Print out some information that will help us debug the problem
aoqi@0 707 tty->print_cr("Wrong number of expression stack elements during deoptimization");
aoqi@0 708 tty->print_cr(" Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1);
aoqi@0 709 tty->print_cr(" Fabricated interpreter frame had %d expression stack elements",
aoqi@0 710 iframe->interpreter_frame_expression_stack_size());
aoqi@0 711 tty->print_cr(" Interpreter oop map had %d expression stack elements", mask.expression_stack_size());
aoqi@0 712 tty->print_cr(" try_next_mask = %d", try_next_mask);
aoqi@0 713 tty->print_cr(" next_mask_expression_stack_size = %d", next_mask_expression_stack_size);
aoqi@0 714 tty->print_cr(" callee_size_of_parameters = %d", callee_size_of_parameters);
aoqi@0 715 tty->print_cr(" callee_max_locals = %d", callee_max_locals);
aoqi@0 716 tty->print_cr(" top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment);
aoqi@0 717 tty->print_cr(" exec_mode = %d", exec_mode);
aoqi@0 718 tty->print_cr(" cur_invoke_parameter_size = %d", cur_invoke_parameter_size);
aoqi@0 719 tty->print_cr(" Thread = " INTPTR_FORMAT ", thread ID = " UINTX_FORMAT, thread, thread->osthread()->thread_id());
aoqi@0 720 tty->print_cr(" Interpreted frames:");
aoqi@0 721 for (int k = 0; k < cur_array->frames(); k++) {
aoqi@0 722 vframeArrayElement* el = cur_array->element(k);
aoqi@0 723 tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
aoqi@0 724 }
aoqi@0 725 cur_array->print_on_2(tty);
aoqi@0 726 guarantee(false, "wrong number of expression stack elements during deopt");
aoqi@0 727 }
aoqi@0 728 VerifyOopClosure verify;
aoqi@0 729 iframe->oops_interpreted_do(&verify, NULL, &rm, false);
aoqi@0 730 callee_size_of_parameters = mh->size_of_parameters();
aoqi@0 731 callee_max_locals = mh->max_locals();
aoqi@0 732 is_top_frame = false;
aoqi@0 733 }
aoqi@0 734 }
aoqi@0 735 #endif /* !PRODUCT */
aoqi@0 736
aoqi@0 737
aoqi@0 738 return bt;
aoqi@0 739 JRT_END
aoqi@0 740
aoqi@0 741
aoqi@0 742 int Deoptimization::deoptimize_dependents() {
aoqi@0 743 Threads::deoptimized_wrt_marked_nmethods();
aoqi@0 744 return 0;
aoqi@0 745 }
aoqi@0 746
aoqi@0 747
aoqi@0 748 #ifdef COMPILER2
aoqi@0 749 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) {
aoqi@0 750 Handle pending_exception(thread->pending_exception());
aoqi@0 751 const char* exception_file = thread->exception_file();
aoqi@0 752 int exception_line = thread->exception_line();
aoqi@0 753 thread->clear_pending_exception();
aoqi@0 754
aoqi@0 755 for (int i = 0; i < objects->length(); i++) {
aoqi@0 756 assert(objects->at(i)->is_object(), "invalid debug information");
aoqi@0 757 ObjectValue* sv = (ObjectValue*) objects->at(i);
aoqi@0 758
aoqi@0 759 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
aoqi@0 760 oop obj = NULL;
aoqi@0 761
aoqi@0 762 if (k->oop_is_instance()) {
aoqi@0 763 InstanceKlass* ik = InstanceKlass::cast(k());
aoqi@0 764 obj = ik->allocate_instance(CHECK_(false));
aoqi@0 765 } else if (k->oop_is_typeArray()) {
aoqi@0 766 TypeArrayKlass* ak = TypeArrayKlass::cast(k());
aoqi@0 767 assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
aoqi@0 768 int len = sv->field_size() / type2size[ak->element_type()];
aoqi@0 769 obj = ak->allocate(len, CHECK_(false));
aoqi@0 770 } else if (k->oop_is_objArray()) {
aoqi@0 771 ObjArrayKlass* ak = ObjArrayKlass::cast(k());
aoqi@0 772 obj = ak->allocate(sv->field_size(), CHECK_(false));
aoqi@0 773 }
aoqi@0 774
aoqi@0 775 assert(obj != NULL, "allocation failed");
aoqi@0 776 assert(sv->value().is_null(), "redundant reallocation");
aoqi@0 777 sv->set_value(obj);
aoqi@0 778 }
aoqi@0 779
aoqi@0 780 if (pending_exception.not_null()) {
aoqi@0 781 thread->set_pending_exception(pending_exception(), exception_file, exception_line);
aoqi@0 782 }
aoqi@0 783
aoqi@0 784 return true;
aoqi@0 785 }
aoqi@0 786
aoqi@0 787 // This assumes that the fields are stored in ObjectValue in the same order
aoqi@0 788 // they are yielded by do_nonstatic_fields.
aoqi@0 789 class FieldReassigner: public FieldClosure {
aoqi@0 790 frame* _fr;
aoqi@0 791 RegisterMap* _reg_map;
aoqi@0 792 ObjectValue* _sv;
aoqi@0 793 InstanceKlass* _ik;
aoqi@0 794 oop _obj;
aoqi@0 795
aoqi@0 796 int _i;
aoqi@0 797 public:
aoqi@0 798 FieldReassigner(frame* fr, RegisterMap* reg_map, ObjectValue* sv, oop obj) :
aoqi@0 799 _fr(fr), _reg_map(reg_map), _sv(sv), _obj(obj), _i(0) {}
aoqi@0 800
aoqi@0 801 int i() const { return _i; }
aoqi@0 802
aoqi@0 803
aoqi@0 804 void do_field(fieldDescriptor* fd) {
aoqi@0 805 intptr_t val;
aoqi@0 806 StackValue* value =
aoqi@0 807 StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(i()));
aoqi@0 808 int offset = fd->offset();
aoqi@0 809 switch (fd->field_type()) {
aoqi@0 810 case T_OBJECT: case T_ARRAY:
aoqi@0 811 assert(value->type() == T_OBJECT, "Agreement.");
aoqi@0 812 _obj->obj_field_put(offset, value->get_obj()());
aoqi@0 813 break;
aoqi@0 814
aoqi@0 815 case T_LONG: case T_DOUBLE: {
aoqi@0 816 assert(value->type() == T_INT, "Agreement.");
aoqi@0 817 StackValue* low =
aoqi@0 818 StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(++_i));
aoqi@0 819 #ifdef _LP64
aoqi@0 820 jlong res = (jlong)low->get_int();
aoqi@0 821 #else
aoqi@0 822 #ifdef SPARC
aoqi@0 823 // For SPARC we have to swap high and low words.
aoqi@0 824 jlong res = jlong_from((jint)low->get_int(), (jint)value->get_int());
aoqi@0 825 #else
aoqi@0 826 jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());
aoqi@0 827 #endif //SPARC
aoqi@0 828 #endif
aoqi@0 829 _obj->long_field_put(offset, res);
aoqi@0 830 break;
aoqi@0 831 }
aoqi@0 832 // Have to cast to INT (32 bits) pointer to avoid little/big-endian problem.
aoqi@0 833 case T_INT: case T_FLOAT: // 4 bytes.
aoqi@0 834 assert(value->type() == T_INT, "Agreement.");
aoqi@0 835 val = value->get_int();
aoqi@0 836 _obj->int_field_put(offset, (jint)*((jint*)&val));
aoqi@0 837 break;
aoqi@0 838
aoqi@0 839 case T_SHORT: case T_CHAR: // 2 bytes
aoqi@0 840 assert(value->type() == T_INT, "Agreement.");
aoqi@0 841 val = value->get_int();
aoqi@0 842 _obj->short_field_put(offset, (jshort)*((jint*)&val));
aoqi@0 843 break;
aoqi@0 844
aoqi@0 845 case T_BOOLEAN: case T_BYTE: // 1 byte
aoqi@0 846 assert(value->type() == T_INT, "Agreement.");
aoqi@0 847 val = value->get_int();
aoqi@0 848 _obj->bool_field_put(offset, (jboolean)*((jint*)&val));
aoqi@0 849 break;
aoqi@0 850
aoqi@0 851 default:
aoqi@0 852 ShouldNotReachHere();
aoqi@0 853 }
aoqi@0 854 _i++;
aoqi@0 855 }
aoqi@0 856 };
aoqi@0 857
aoqi@0 858 // restore elements of an eliminated type array
aoqi@0 859 void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) {
aoqi@0 860 int index = 0;
aoqi@0 861 intptr_t val;
aoqi@0 862
aoqi@0 863 for (int i = 0; i < sv->field_size(); i++) {
aoqi@0 864 StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
aoqi@0 865 switch(type) {
aoqi@0 866 case T_LONG: case T_DOUBLE: {
aoqi@0 867 assert(value->type() == T_INT, "Agreement.");
aoqi@0 868 StackValue* low =
aoqi@0 869 StackValue::create_stack_value(fr, reg_map, sv->field_at(++i));
aoqi@0 870 #ifdef _LP64
aoqi@0 871 jlong res = (jlong)low->get_int();
aoqi@0 872 #else
aoqi@0 873 #ifdef SPARC
aoqi@0 874 // For SPARC we have to swap high and low words.
aoqi@0 875 jlong res = jlong_from((jint)low->get_int(), (jint)value->get_int());
aoqi@0 876 #else
aoqi@0 877 jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());
aoqi@0 878 #endif //SPARC
aoqi@0 879 #endif
aoqi@0 880 obj->long_at_put(index, res);
aoqi@0 881 break;
aoqi@0 882 }
aoqi@0 883
aoqi@0 884 // Have to cast to INT (32 bits) pointer to avoid little/big-endian problem.
aoqi@0 885 case T_INT: case T_FLOAT: // 4 bytes.
aoqi@0 886 assert(value->type() == T_INT, "Agreement.");
aoqi@0 887 val = value->get_int();
aoqi@0 888 obj->int_at_put(index, (jint)*((jint*)&val));
aoqi@0 889 break;
aoqi@0 890
aoqi@0 891 case T_SHORT: case T_CHAR: // 2 bytes
aoqi@0 892 assert(value->type() == T_INT, "Agreement.");
aoqi@0 893 val = value->get_int();
aoqi@0 894 obj->short_at_put(index, (jshort)*((jint*)&val));
aoqi@0 895 break;
aoqi@0 896
aoqi@0 897 case T_BOOLEAN: case T_BYTE: // 1 byte
aoqi@0 898 assert(value->type() == T_INT, "Agreement.");
aoqi@0 899 val = value->get_int();
aoqi@0 900 obj->bool_at_put(index, (jboolean)*((jint*)&val));
aoqi@0 901 break;
aoqi@0 902
aoqi@0 903 default:
aoqi@0 904 ShouldNotReachHere();
aoqi@0 905 }
aoqi@0 906 index++;
aoqi@0 907 }
aoqi@0 908 }
aoqi@0 909
aoqi@0 910
aoqi@0 911 // restore fields of an eliminated object array
aoqi@0 912 void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) {
aoqi@0 913 for (int i = 0; i < sv->field_size(); i++) {
aoqi@0 914 StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
aoqi@0 915 assert(value->type() == T_OBJECT, "object element expected");
aoqi@0 916 obj->obj_at_put(i, value->get_obj()());
aoqi@0 917 }
aoqi@0 918 }
aoqi@0 919
aoqi@0 920
aoqi@0 921 // restore fields of all eliminated objects and arrays
aoqi@0 922 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects) {
aoqi@0 923 for (int i = 0; i < objects->length(); i++) {
aoqi@0 924 ObjectValue* sv = (ObjectValue*) objects->at(i);
aoqi@0 925 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
aoqi@0 926 Handle obj = sv->value();
aoqi@0 927 assert(obj.not_null(), "reallocation was missed");
aoqi@0 928
aoqi@0 929 if (k->oop_is_instance()) {
aoqi@0 930 InstanceKlass* ik = InstanceKlass::cast(k());
aoqi@0 931 FieldReassigner reassign(fr, reg_map, sv, obj());
aoqi@0 932 ik->do_nonstatic_fields(&reassign);
aoqi@0 933 } else if (k->oop_is_typeArray()) {
aoqi@0 934 TypeArrayKlass* ak = TypeArrayKlass::cast(k());
aoqi@0 935 reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type());
aoqi@0 936 } else if (k->oop_is_objArray()) {
aoqi@0 937 reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj());
aoqi@0 938 }
aoqi@0 939 }
aoqi@0 940 }
aoqi@0 941
aoqi@0 942
aoqi@0 943 // relock objects for which synchronization was eliminated
aoqi@0 944 void Deoptimization::relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread) {
aoqi@0 945 for (int i = 0; i < monitors->length(); i++) {
aoqi@0 946 MonitorInfo* mon_info = monitors->at(i);
aoqi@0 947 if (mon_info->eliminated()) {
aoqi@0 948 assert(mon_info->owner() != NULL, "reallocation was missed");
aoqi@0 949 Handle obj = Handle(mon_info->owner());
aoqi@0 950 markOop mark = obj->mark();
aoqi@0 951 if (UseBiasedLocking && mark->has_bias_pattern()) {
aoqi@0 952 // New allocated objects may have the mark set to anonymously biased.
aoqi@0 953 // Also the deoptimized method may called methods with synchronization
aoqi@0 954 // where the thread-local object is bias locked to the current thread.
aoqi@0 955 assert(mark->is_biased_anonymously() ||
aoqi@0 956 mark->biased_locker() == thread, "should be locked to current thread");
aoqi@0 957 // Reset mark word to unbiased prototype.
aoqi@0 958 markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());
aoqi@0 959 obj->set_mark(unbiased_prototype);
aoqi@0 960 }
aoqi@0 961 BasicLock* lock = mon_info->lock();
aoqi@0 962 ObjectSynchronizer::slow_enter(obj, lock, thread);
aoqi@0 963 }
aoqi@0 964 assert(mon_info->owner()->is_locked(), "object must be locked now");
aoqi@0 965 }
aoqi@0 966 }
aoqi@0 967
aoqi@0 968
aoqi@0 969 #ifndef PRODUCT
aoqi@0 970 // print information about reallocated objects
aoqi@0 971 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects) {
aoqi@0 972 fieldDescriptor fd;
aoqi@0 973
aoqi@0 974 for (int i = 0; i < objects->length(); i++) {
aoqi@0 975 ObjectValue* sv = (ObjectValue*) objects->at(i);
aoqi@0 976 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
aoqi@0 977 Handle obj = sv->value();
aoqi@0 978
aoqi@0 979 tty->print(" object <" INTPTR_FORMAT "> of type ", (void *)sv->value()());
aoqi@0 980 k->print_value();
aoqi@0 981 tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize);
aoqi@0 982 tty->cr();
aoqi@0 983
aoqi@0 984 if (Verbose) {
aoqi@0 985 k->oop_print_on(obj(), tty);
aoqi@0 986 }
aoqi@0 987 }
aoqi@0 988 }
aoqi@0 989 #endif
aoqi@0 990 #endif // COMPILER2
aoqi@0 991
aoqi@0 992 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk) {
aoqi@0 993 Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp());
aoqi@0 994
aoqi@0 995 #ifndef PRODUCT
aoqi@0 996 if (TraceDeoptimization) {
aoqi@0 997 ttyLocker ttyl;
aoqi@0 998 tty->print("DEOPT PACKING thread " INTPTR_FORMAT " ", thread);
aoqi@0 999 fr.print_on(tty);
aoqi@0 1000 tty->print_cr(" Virtual frames (innermost first):");
aoqi@0 1001 for (int index = 0; index < chunk->length(); index++) {
aoqi@0 1002 compiledVFrame* vf = chunk->at(index);
aoqi@0 1003 tty->print(" %2d - ", index);
aoqi@0 1004 vf->print_value();
aoqi@0 1005 int bci = chunk->at(index)->raw_bci();
aoqi@0 1006 const char* code_name;
aoqi@0 1007 if (bci == SynchronizationEntryBCI) {
aoqi@0 1008 code_name = "sync entry";
aoqi@0 1009 } else {
aoqi@0 1010 Bytecodes::Code code = vf->method()->code_at(bci);
aoqi@0 1011 code_name = Bytecodes::name(code);
aoqi@0 1012 }
aoqi@0 1013 tty->print(" - %s", code_name);
aoqi@0 1014 tty->print_cr(" @ bci %d ", bci);
aoqi@0 1015 if (Verbose) {
aoqi@0 1016 vf->print();
aoqi@0 1017 tty->cr();
aoqi@0 1018 }
aoqi@0 1019 }
aoqi@0 1020 }
aoqi@0 1021 #endif
aoqi@0 1022
aoqi@0 1023 // Register map for next frame (used for stack crawl). We capture
aoqi@0 1024 // the state of the deopt'ing frame's caller. Thus if we need to
aoqi@0 1025 // stuff a C2I adapter we can properly fill in the callee-save
aoqi@0 1026 // register locations.
aoqi@0 1027 frame caller = fr.sender(reg_map);
aoqi@0 1028 int frame_size = caller.sp() - fr.sp();
aoqi@0 1029
aoqi@0 1030 frame sender = caller;
aoqi@0 1031
aoqi@0 1032 // Since the Java thread being deoptimized will eventually adjust it's own stack,
aoqi@0 1033 // the vframeArray containing the unpacking information is allocated in the C heap.
aoqi@0 1034 // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
aoqi@0 1035 vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr);
aoqi@0 1036
aoqi@0 1037 // Compare the vframeArray to the collected vframes
aoqi@0 1038 assert(array->structural_compare(thread, chunk), "just checking");
aoqi@0 1039
aoqi@0 1040 #ifndef PRODUCT
aoqi@0 1041 if (TraceDeoptimization) {
aoqi@0 1042 ttyLocker ttyl;
aoqi@0 1043 tty->print_cr(" Created vframeArray " INTPTR_FORMAT, array);
aoqi@0 1044 }
aoqi@0 1045 #endif // PRODUCT
aoqi@0 1046
aoqi@0 1047 return array;
aoqi@0 1048 }
aoqi@0 1049
aoqi@0 1050
aoqi@0 1051 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
aoqi@0 1052 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
aoqi@0 1053 for (int i = 0; i < monitors->length(); i++) {
aoqi@0 1054 MonitorInfo* mon_info = monitors->at(i);
aoqi@0 1055 if (!mon_info->eliminated() && mon_info->owner() != NULL) {
aoqi@0 1056 objects_to_revoke->append(Handle(mon_info->owner()));
aoqi@0 1057 }
aoqi@0 1058 }
aoqi@0 1059 }
aoqi@0 1060
aoqi@0 1061
aoqi@0 1062 void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) {
aoqi@0 1063 if (!UseBiasedLocking) {
aoqi@0 1064 return;
aoqi@0 1065 }
aoqi@0 1066
aoqi@0 1067 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
aoqi@0 1068
aoqi@0 1069 // Unfortunately we don't have a RegisterMap available in most of
aoqi@0 1070 // the places we want to call this routine so we need to walk the
aoqi@0 1071 // stack again to update the register map.
aoqi@0 1072 if (map == NULL || !map->update_map()) {
aoqi@0 1073 StackFrameStream sfs(thread, true);
aoqi@0 1074 bool found = false;
aoqi@0 1075 while (!found && !sfs.is_done()) {
aoqi@0 1076 frame* cur = sfs.current();
aoqi@0 1077 sfs.next();
aoqi@0 1078 found = cur->id() == fr.id();
aoqi@0 1079 }
aoqi@0 1080 assert(found, "frame to be deoptimized not found on target thread's stack");
aoqi@0 1081 map = sfs.register_map();
aoqi@0 1082 }
aoqi@0 1083
aoqi@0 1084 vframe* vf = vframe::new_vframe(&fr, map, thread);
aoqi@0 1085 compiledVFrame* cvf = compiledVFrame::cast(vf);
aoqi@0 1086 // Revoke monitors' biases in all scopes
aoqi@0 1087 while (!cvf->is_top()) {
aoqi@0 1088 collect_monitors(cvf, objects_to_revoke);
aoqi@0 1089 cvf = compiledVFrame::cast(cvf->sender());
aoqi@0 1090 }
aoqi@0 1091 collect_monitors(cvf, objects_to_revoke);
aoqi@0 1092
aoqi@0 1093 if (SafepointSynchronize::is_at_safepoint()) {
aoqi@0 1094 BiasedLocking::revoke_at_safepoint(objects_to_revoke);
aoqi@0 1095 } else {
aoqi@0 1096 BiasedLocking::revoke(objects_to_revoke);
aoqi@0 1097 }
aoqi@0 1098 }
aoqi@0 1099
aoqi@0 1100
aoqi@0 1101 void Deoptimization::revoke_biases_of_monitors(CodeBlob* cb) {
aoqi@0 1102 if (!UseBiasedLocking) {
aoqi@0 1103 return;
aoqi@0 1104 }
aoqi@0 1105
aoqi@0 1106 assert(SafepointSynchronize::is_at_safepoint(), "must only be called from safepoint");
aoqi@0 1107 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
aoqi@0 1108 for (JavaThread* jt = Threads::first(); jt != NULL ; jt = jt->next()) {
aoqi@0 1109 if (jt->has_last_Java_frame()) {
aoqi@0 1110 StackFrameStream sfs(jt, true);
aoqi@0 1111 while (!sfs.is_done()) {
aoqi@0 1112 frame* cur = sfs.current();
aoqi@0 1113 if (cb->contains(cur->pc())) {
aoqi@0 1114 vframe* vf = vframe::new_vframe(cur, sfs.register_map(), jt);
aoqi@0 1115 compiledVFrame* cvf = compiledVFrame::cast(vf);
aoqi@0 1116 // Revoke monitors' biases in all scopes
aoqi@0 1117 while (!cvf->is_top()) {
aoqi@0 1118 collect_monitors(cvf, objects_to_revoke);
aoqi@0 1119 cvf = compiledVFrame::cast(cvf->sender());
aoqi@0 1120 }
aoqi@0 1121 collect_monitors(cvf, objects_to_revoke);
aoqi@0 1122 }
aoqi@0 1123 sfs.next();
aoqi@0 1124 }
aoqi@0 1125 }
aoqi@0 1126 }
aoqi@0 1127 BiasedLocking::revoke_at_safepoint(objects_to_revoke);
aoqi@0 1128 }
aoqi@0 1129
aoqi@0 1130
aoqi@0 1131 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr) {
aoqi@0 1132 assert(fr.can_be_deoptimized(), "checking frame type");
aoqi@0 1133
aoqi@0 1134 gather_statistics(Reason_constraint, Action_none, Bytecodes::_illegal);
aoqi@0 1135
aoqi@0 1136 // Patch the nmethod so that when execution returns to it we will
aoqi@0 1137 // deopt the execution state and return to the interpreter.
aoqi@0 1138 fr.deoptimize(thread);
aoqi@0 1139 }
aoqi@0 1140
aoqi@0 1141 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) {
aoqi@0 1142 // Deoptimize only if the frame comes from compile code.
aoqi@0 1143 // Do not deoptimize the frame which is already patched
aoqi@0 1144 // during the execution of the loops below.
aoqi@0 1145 if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
aoqi@0 1146 return;
aoqi@0 1147 }
aoqi@0 1148 ResourceMark rm;
aoqi@0 1149 DeoptimizationMarker dm;
aoqi@0 1150 if (UseBiasedLocking) {
aoqi@0 1151 revoke_biases_of_monitors(thread, fr, map);
aoqi@0 1152 }
aoqi@0 1153 deoptimize_single_frame(thread, fr);
aoqi@0 1154
aoqi@0 1155 }
aoqi@0 1156
aoqi@0 1157
aoqi@0 1158 void Deoptimization::deoptimize_frame_internal(JavaThread* thread, intptr_t* id) {
aoqi@0 1159 assert(thread == Thread::current() || SafepointSynchronize::is_at_safepoint(),
aoqi@0 1160 "can only deoptimize other thread at a safepoint");
aoqi@0 1161 // Compute frame and register map based on thread and sp.
aoqi@0 1162 RegisterMap reg_map(thread, UseBiasedLocking);
aoqi@0 1163 frame fr = thread->last_frame();
aoqi@0 1164 while (fr.id() != id) {
aoqi@0 1165 fr = fr.sender(&reg_map);
aoqi@0 1166 }
aoqi@0 1167 deoptimize(thread, fr, &reg_map);
aoqi@0 1168 }
aoqi@0 1169
aoqi@0 1170
aoqi@0 1171 void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id) {
aoqi@0 1172 if (thread == Thread::current()) {
aoqi@0 1173 Deoptimization::deoptimize_frame_internal(thread, id);
aoqi@0 1174 } else {
aoqi@0 1175 VM_DeoptimizeFrame deopt(thread, id);
aoqi@0 1176 VMThread::execute(&deopt);
aoqi@0 1177 }
aoqi@0 1178 }
aoqi@0 1179
aoqi@0 1180
aoqi@0 1181 // JVMTI PopFrame support
aoqi@0 1182 JRT_LEAF(void, Deoptimization::popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address))
aoqi@0 1183 {
aoqi@0 1184 thread->popframe_preserve_args(in_ByteSize(bytes_to_save), start_address);
aoqi@0 1185 }
aoqi@0 1186 JRT_END
aoqi@0 1187
aoqi@0 1188
aoqi@0 1189 #if defined(COMPILER2) || defined(SHARK)
aoqi@0 1190 void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index, TRAPS) {
aoqi@0 1191 // in case of an unresolved klass entry, load the class.
aoqi@0 1192 if (constant_pool->tag_at(index).is_unresolved_klass()) {
aoqi@0 1193 Klass* tk = constant_pool->klass_at(index, CHECK);
aoqi@0 1194 return;
aoqi@0 1195 }
aoqi@0 1196
aoqi@0 1197 if (!constant_pool->tag_at(index).is_symbol()) return;
aoqi@0 1198
aoqi@0 1199 Handle class_loader (THREAD, constant_pool->pool_holder()->class_loader());
aoqi@0 1200 Symbol* symbol = constant_pool->symbol_at(index);
aoqi@0 1201
aoqi@0 1202 // class name?
aoqi@0 1203 if (symbol->byte_at(0) != '(') {
aoqi@0 1204 Handle protection_domain (THREAD, constant_pool->pool_holder()->protection_domain());
aoqi@0 1205 SystemDictionary::resolve_or_null(symbol, class_loader, protection_domain, CHECK);
aoqi@0 1206 return;
aoqi@0 1207 }
aoqi@0 1208
aoqi@0 1209 // then it must be a signature!
aoqi@0 1210 ResourceMark rm(THREAD);
aoqi@0 1211 for (SignatureStream ss(symbol); !ss.is_done(); ss.next()) {
aoqi@0 1212 if (ss.is_object()) {
aoqi@0 1213 Symbol* class_name = ss.as_symbol(CHECK);
aoqi@0 1214 Handle protection_domain (THREAD, constant_pool->pool_holder()->protection_domain());
aoqi@0 1215 SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK);
aoqi@0 1216 }
aoqi@0 1217 }
aoqi@0 1218 }
aoqi@0 1219
aoqi@0 1220
aoqi@0 1221 void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index) {
aoqi@0 1222 EXCEPTION_MARK;
aoqi@0 1223 load_class_by_index(constant_pool, index, THREAD);
aoqi@0 1224 if (HAS_PENDING_EXCEPTION) {
aoqi@0 1225 // Exception happened during classloading. We ignore the exception here, since it
aoqi@0 1226 // is going to be rethrown since the current activation is going to be deoptimized and
aoqi@0 1227 // the interpreter will re-execute the bytecode.
aoqi@0 1228 CLEAR_PENDING_EXCEPTION;
aoqi@0 1229 // Class loading called java code which may have caused a stack
aoqi@0 1230 // overflow. If the exception was thrown right before the return
aoqi@0 1231 // to the runtime the stack is no longer guarded. Reguard the
aoqi@0 1232 // stack otherwise if we return to the uncommon trap blob and the
aoqi@0 1233 // stack bang causes a stack overflow we crash.
aoqi@0 1234 assert(THREAD->is_Java_thread(), "only a java thread can be here");
aoqi@0 1235 JavaThread* thread = (JavaThread*)THREAD;
aoqi@0 1236 bool guard_pages_enabled = thread->stack_yellow_zone_enabled();
aoqi@0 1237 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();
aoqi@0 1238 assert(guard_pages_enabled, "stack banging in uncommon trap blob may cause crash");
aoqi@0 1239 }
aoqi@0 1240 }
aoqi@0 1241
aoqi@0 1242 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* thread, jint trap_request)) {
aoqi@0 1243 HandleMark hm;
aoqi@0 1244
aoqi@0 1245 // uncommon_trap() is called at the beginning of the uncommon trap
aoqi@0 1246 // handler. Note this fact before we start generating temporary frames
aoqi@0 1247 // that can confuse an asynchronous stack walker. This counter is
aoqi@0 1248 // decremented at the end of unpack_frames().
aoqi@0 1249 thread->inc_in_deopt_handler();
aoqi@0 1250
aoqi@0 1251 // We need to update the map if we have biased locking.
aoqi@0 1252 RegisterMap reg_map(thread, UseBiasedLocking);
aoqi@0 1253 frame stub_frame = thread->last_frame();
aoqi@0 1254 frame fr = stub_frame.sender(&reg_map);
aoqi@0 1255 // Make sure the calling nmethod is not getting deoptimized and removed
aoqi@0 1256 // before we are done with it.
aoqi@0 1257 nmethodLocker nl(fr.pc());
aoqi@0 1258
aoqi@0 1259 // Log a message
aoqi@0 1260 Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT,
aoqi@0 1261 trap_request, fr.pc());
aoqi@0 1262
aoqi@0 1263 {
aoqi@0 1264 ResourceMark rm;
aoqi@0 1265
aoqi@0 1266 // Revoke biases of any monitors in the frame to ensure we can migrate them
aoqi@0 1267 revoke_biases_of_monitors(thread, fr, &reg_map);
aoqi@0 1268
aoqi@0 1269 DeoptReason reason = trap_request_reason(trap_request);
aoqi@0 1270 DeoptAction action = trap_request_action(trap_request);
aoqi@0 1271 jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
aoqi@0 1272
aoqi@0 1273 vframe* vf = vframe::new_vframe(&fr, &reg_map, thread);
aoqi@0 1274 compiledVFrame* cvf = compiledVFrame::cast(vf);
aoqi@0 1275
aoqi@0 1276 nmethod* nm = cvf->code();
aoqi@0 1277
aoqi@0 1278 ScopeDesc* trap_scope = cvf->scope();
aoqi@0 1279 methodHandle trap_method = trap_scope->method();
aoqi@0 1280 int trap_bci = trap_scope->bci();
aoqi@0 1281 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
aoqi@0 1282
aoqi@0 1283 // Record this event in the histogram.
aoqi@0 1284 gather_statistics(reason, action, trap_bc);
aoqi@0 1285
aoqi@0 1286 // Ensure that we can record deopt. history:
aoqi@0 1287 // Need MDO to record RTM code generation state.
aoqi@0 1288 bool create_if_missing = ProfileTraps RTM_OPT_ONLY( || UseRTMLocking );
aoqi@0 1289
aoqi@0 1290 MethodData* trap_mdo =
aoqi@0 1291 get_method_data(thread, trap_method, create_if_missing);
aoqi@0 1292
aoqi@0 1293 // Log a message
aoqi@0 1294 Events::log_deopt_message(thread, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d",
aoqi@0 1295 trap_reason_name(reason), trap_action_name(action), fr.pc(),
aoqi@0 1296 trap_method->name_and_sig_as_C_string(), trap_bci);
aoqi@0 1297
aoqi@0 1298 // Print a bunch of diagnostics, if requested.
aoqi@0 1299 if (TraceDeoptimization || LogCompilation) {
aoqi@0 1300 ResourceMark rm;
aoqi@0 1301 ttyLocker ttyl;
aoqi@0 1302 char buf[100];
aoqi@0 1303 if (xtty != NULL) {
aoqi@0 1304 xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT"' %s",
aoqi@0 1305 os::current_thread_id(),
aoqi@0 1306 format_trap_request(buf, sizeof(buf), trap_request));
aoqi@0 1307 nm->log_identity(xtty);
aoqi@0 1308 }
aoqi@0 1309 Symbol* class_name = NULL;
aoqi@0 1310 bool unresolved = false;
aoqi@0 1311 if (unloaded_class_index >= 0) {
aoqi@0 1312 constantPoolHandle constants (THREAD, trap_method->constants());
aoqi@0 1313 if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {
aoqi@0 1314 class_name = constants->klass_name_at(unloaded_class_index);
aoqi@0 1315 unresolved = true;
aoqi@0 1316 if (xtty != NULL)
aoqi@0 1317 xtty->print(" unresolved='1'");
aoqi@0 1318 } else if (constants->tag_at(unloaded_class_index).is_symbol()) {
aoqi@0 1319 class_name = constants->symbol_at(unloaded_class_index);
aoqi@0 1320 }
aoqi@0 1321 if (xtty != NULL)
aoqi@0 1322 xtty->name(class_name);
aoqi@0 1323 }
aoqi@0 1324 if (xtty != NULL && trap_mdo != NULL) {
aoqi@0 1325 // Dump the relevant MDO state.
aoqi@0 1326 // This is the deopt count for the current reason, any previous
aoqi@0 1327 // reasons or recompiles seen at this point.
aoqi@0 1328 int dcnt = trap_mdo->trap_count(reason);
aoqi@0 1329 if (dcnt != 0)
aoqi@0 1330 xtty->print(" count='%d'", dcnt);
aoqi@0 1331 ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
aoqi@0 1332 int dos = (pdata == NULL)? 0: pdata->trap_state();
aoqi@0 1333 if (dos != 0) {
aoqi@0 1334 xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
aoqi@0 1335 if (trap_state_is_recompiled(dos)) {
aoqi@0 1336 int recnt2 = trap_mdo->overflow_recompile_count();
aoqi@0 1337 if (recnt2 != 0)
aoqi@0 1338 xtty->print(" recompiles2='%d'", recnt2);
aoqi@0 1339 }
aoqi@0 1340 }
aoqi@0 1341 }
aoqi@0 1342 if (xtty != NULL) {
aoqi@0 1343 xtty->stamp();
aoqi@0 1344 xtty->end_head();
aoqi@0 1345 }
aoqi@0 1346 if (TraceDeoptimization) { // make noise on the tty
aoqi@0 1347 tty->print("Uncommon trap occurred in");
aoqi@0 1348 nm->method()->print_short_name(tty);
aoqi@0 1349 tty->print(" (@" INTPTR_FORMAT ") thread=" UINTX_FORMAT " reason=%s action=%s unloaded_class_index=%d",
aoqi@0 1350 fr.pc(),
aoqi@0 1351 os::current_thread_id(),
aoqi@0 1352 trap_reason_name(reason),
aoqi@0 1353 trap_action_name(action),
aoqi@0 1354 unloaded_class_index);
aoqi@0 1355 if (class_name != NULL) {
aoqi@0 1356 tty->print(unresolved ? " unresolved class: " : " symbol: ");
aoqi@0 1357 class_name->print_symbol_on(tty);
aoqi@0 1358 }
aoqi@0 1359 tty->cr();
aoqi@0 1360 }
aoqi@0 1361 if (xtty != NULL) {
aoqi@0 1362 // Log the precise location of the trap.
aoqi@0 1363 for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) {
aoqi@0 1364 xtty->begin_elem("jvms bci='%d'", sd->bci());
aoqi@0 1365 xtty->method(sd->method());
aoqi@0 1366 xtty->end_elem();
aoqi@0 1367 if (sd->is_top()) break;
aoqi@0 1368 }
aoqi@0 1369 xtty->tail("uncommon_trap");
aoqi@0 1370 }
aoqi@0 1371 }
aoqi@0 1372 // (End diagnostic printout.)
aoqi@0 1373
aoqi@0 1374 // Load class if necessary
aoqi@0 1375 if (unloaded_class_index >= 0) {
aoqi@0 1376 constantPoolHandle constants(THREAD, trap_method->constants());
aoqi@0 1377 load_class_by_index(constants, unloaded_class_index);
aoqi@0 1378 }
aoqi@0 1379
aoqi@0 1380 // Flush the nmethod if necessary and desirable.
aoqi@0 1381 //
aoqi@0 1382 // We need to avoid situations where we are re-flushing the nmethod
aoqi@0 1383 // because of a hot deoptimization site. Repeated flushes at the same
aoqi@0 1384 // point need to be detected by the compiler and avoided. If the compiler
aoqi@0 1385 // cannot avoid them (or has a bug and "refuses" to avoid them), this
aoqi@0 1386 // module must take measures to avoid an infinite cycle of recompilation
aoqi@0 1387 // and deoptimization. There are several such measures:
aoqi@0 1388 //
aoqi@0 1389 // 1. If a recompilation is ordered a second time at some site X
aoqi@0 1390 // and for the same reason R, the action is adjusted to 'reinterpret',
aoqi@0 1391 // to give the interpreter time to exercise the method more thoroughly.
aoqi@0 1392 // If this happens, the method's overflow_recompile_count is incremented.
aoqi@0 1393 //
aoqi@0 1394 // 2. If the compiler fails to reduce the deoptimization rate, then
aoqi@0 1395 // the method's overflow_recompile_count will begin to exceed the set
aoqi@0 1396 // limit PerBytecodeRecompilationCutoff. If this happens, the action
aoqi@0 1397 // is adjusted to 'make_not_compilable', and the method is abandoned
aoqi@0 1398 // to the interpreter. This is a performance hit for hot methods,
aoqi@0 1399 // but is better than a disastrous infinite cycle of recompilations.
aoqi@0 1400 // (Actually, only the method containing the site X is abandoned.)
aoqi@0 1401 //
aoqi@0 1402 // 3. In parallel with the previous measures, if the total number of
aoqi@0 1403 // recompilations of a method exceeds the much larger set limit
aoqi@0 1404 // PerMethodRecompilationCutoff, the method is abandoned.
aoqi@0 1405 // This should only happen if the method is very large and has
aoqi@0 1406 // many "lukewarm" deoptimizations. The code which enforces this
aoqi@0 1407 // limit is elsewhere (class nmethod, class Method).
aoqi@0 1408 //
aoqi@0 1409 // Note that the per-BCI 'is_recompiled' bit gives the compiler one chance
aoqi@0 1410 // to recompile at each bytecode independently of the per-BCI cutoff.
aoqi@0 1411 //
aoqi@0 1412 // The decision to update code is up to the compiler, and is encoded
aoqi@0 1413 // in the Action_xxx code. If the compiler requests Action_none
aoqi@0 1414 // no trap state is changed, no compiled code is changed, and the
aoqi@0 1415 // computation suffers along in the interpreter.
aoqi@0 1416 //
aoqi@0 1417 // The other action codes specify various tactics for decompilation
aoqi@0 1418 // and recompilation. Action_maybe_recompile is the loosest, and
aoqi@0 1419 // allows the compiled code to stay around until enough traps are seen,
aoqi@0 1420 // and until the compiler gets around to recompiling the trapping method.
aoqi@0 1421 //
aoqi@0 1422 // The other actions cause immediate removal of the present code.
aoqi@0 1423
aoqi@0 1424 bool update_trap_state = true;
aoqi@0 1425 bool make_not_entrant = false;
aoqi@0 1426 bool make_not_compilable = false;
aoqi@0 1427 bool reprofile = false;
aoqi@0 1428 switch (action) {
aoqi@0 1429 case Action_none:
aoqi@0 1430 // Keep the old code.
aoqi@0 1431 update_trap_state = false;
aoqi@0 1432 break;
aoqi@0 1433 case Action_maybe_recompile:
aoqi@0 1434 // Do not need to invalidate the present code, but we can
aoqi@0 1435 // initiate another
aoqi@0 1436 // Start compiler without (necessarily) invalidating the nmethod.
aoqi@0 1437 // The system will tolerate the old code, but new code should be
aoqi@0 1438 // generated when possible.
aoqi@0 1439 break;
aoqi@0 1440 case Action_reinterpret:
aoqi@0 1441 // Go back into the interpreter for a while, and then consider
aoqi@0 1442 // recompiling form scratch.
aoqi@0 1443 make_not_entrant = true;
aoqi@0 1444 // Reset invocation counter for outer most method.
aoqi@0 1445 // This will allow the interpreter to exercise the bytecodes
aoqi@0 1446 // for a while before recompiling.
aoqi@0 1447 // By contrast, Action_make_not_entrant is immediate.
aoqi@0 1448 //
aoqi@0 1449 // Note that the compiler will track null_check, null_assert,
aoqi@0 1450 // range_check, and class_check events and log them as if they
aoqi@0 1451 // had been traps taken from compiled code. This will update
aoqi@0 1452 // the MDO trap history so that the next compilation will
aoqi@0 1453 // properly detect hot trap sites.
aoqi@0 1454 reprofile = true;
aoqi@0 1455 break;
aoqi@0 1456 case Action_make_not_entrant:
aoqi@0 1457 // Request immediate recompilation, and get rid of the old code.
aoqi@0 1458 // Make them not entrant, so next time they are called they get
aoqi@0 1459 // recompiled. Unloaded classes are loaded now so recompile before next
aoqi@0 1460 // time they are called. Same for uninitialized. The interpreter will
aoqi@0 1461 // link the missing class, if any.
aoqi@0 1462 make_not_entrant = true;
aoqi@0 1463 break;
aoqi@0 1464 case Action_make_not_compilable:
aoqi@0 1465 // Give up on compiling this method at all.
aoqi@0 1466 make_not_entrant = true;
aoqi@0 1467 make_not_compilable = true;
aoqi@0 1468 break;
aoqi@0 1469 default:
aoqi@0 1470 ShouldNotReachHere();
aoqi@0 1471 }
aoqi@0 1472
aoqi@0 1473 // Setting +ProfileTraps fixes the following, on all platforms:
aoqi@0 1474 // 4852688: ProfileInterpreter is off by default for ia64. The result is
aoqi@0 1475 // infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the
aoqi@0 1476 // recompile relies on a MethodData* to record heroic opt failures.
aoqi@0 1477
aoqi@0 1478 // Whether the interpreter is producing MDO data or not, we also need
aoqi@0 1479 // to use the MDO to detect hot deoptimization points and control
aoqi@0 1480 // aggressive optimization.
aoqi@0 1481 bool inc_recompile_count = false;
aoqi@0 1482 ProfileData* pdata = NULL;
aoqi@0 1483 if (ProfileTraps && update_trap_state && trap_mdo != NULL) {
aoqi@0 1484 assert(trap_mdo == get_method_data(thread, trap_method, false), "sanity");
aoqi@0 1485 uint this_trap_count = 0;
aoqi@0 1486 bool maybe_prior_trap = false;
aoqi@0 1487 bool maybe_prior_recompile = false;
aoqi@0 1488 pdata = query_update_method_data(trap_mdo, trap_bci, reason,
aoqi@0 1489 nm->method(),
aoqi@0 1490 //outputs:
aoqi@0 1491 this_trap_count,
aoqi@0 1492 maybe_prior_trap,
aoqi@0 1493 maybe_prior_recompile);
aoqi@0 1494 // Because the interpreter also counts null, div0, range, and class
aoqi@0 1495 // checks, these traps from compiled code are double-counted.
aoqi@0 1496 // This is harmless; it just means that the PerXTrapLimit values
aoqi@0 1497 // are in effect a little smaller than they look.
aoqi@0 1498
aoqi@0 1499 DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
aoqi@0 1500 if (per_bc_reason != Reason_none) {
aoqi@0 1501 // Now take action based on the partially known per-BCI history.
aoqi@0 1502 if (maybe_prior_trap
aoqi@0 1503 && this_trap_count >= (uint)PerBytecodeTrapLimit) {
aoqi@0 1504 // If there are too many traps at this BCI, force a recompile.
aoqi@0 1505 // This will allow the compiler to see the limit overflow, and
aoqi@0 1506 // take corrective action, if possible. The compiler generally
aoqi@0 1507 // does not use the exact PerBytecodeTrapLimit value, but instead
aoqi@0 1508 // changes its tactics if it sees any traps at all. This provides
aoqi@0 1509 // a little hysteresis, delaying a recompile until a trap happens
aoqi@0 1510 // several times.
aoqi@0 1511 //
aoqi@0 1512 // Actually, since there is only one bit of counter per BCI,
aoqi@0 1513 // the possible per-BCI counts are {0,1,(per-method count)}.
aoqi@0 1514 // This produces accurate results if in fact there is only
aoqi@0 1515 // one hot trap site, but begins to get fuzzy if there are
aoqi@0 1516 // many sites. For example, if there are ten sites each
aoqi@0 1517 // trapping two or more times, they each get the blame for
aoqi@0 1518 // all of their traps.
aoqi@0 1519 make_not_entrant = true;
aoqi@0 1520 }
aoqi@0 1521
aoqi@0 1522 // Detect repeated recompilation at the same BCI, and enforce a limit.
aoqi@0 1523 if (make_not_entrant && maybe_prior_recompile) {
aoqi@0 1524 // More than one recompile at this point.
aoqi@0 1525 inc_recompile_count = maybe_prior_trap;
aoqi@0 1526 }
aoqi@0 1527 } else {
aoqi@0 1528 // For reasons which are not recorded per-bytecode, we simply
aoqi@0 1529 // force recompiles unconditionally.
aoqi@0 1530 // (Note that PerMethodRecompilationCutoff is enforced elsewhere.)
aoqi@0 1531 make_not_entrant = true;
aoqi@0 1532 }
aoqi@0 1533
aoqi@0 1534 // Go back to the compiler if there are too many traps in this method.
aoqi@0 1535 if (this_trap_count >= per_method_trap_limit(reason)) {
aoqi@0 1536 // If there are too many traps in this method, force a recompile.
aoqi@0 1537 // This will allow the compiler to see the limit overflow, and
aoqi@0 1538 // take corrective action, if possible.
aoqi@0 1539 // (This condition is an unlikely backstop only, because the
aoqi@0 1540 // PerBytecodeTrapLimit is more likely to take effect first,
aoqi@0 1541 // if it is applicable.)
aoqi@0 1542 make_not_entrant = true;
aoqi@0 1543 }
aoqi@0 1544
aoqi@0 1545 // Here's more hysteresis: If there has been a recompile at
aoqi@0 1546 // this trap point already, run the method in the interpreter
aoqi@0 1547 // for a while to exercise it more thoroughly.
aoqi@0 1548 if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) {
aoqi@0 1549 reprofile = true;
aoqi@0 1550 }
aoqi@0 1551
aoqi@0 1552 }
aoqi@0 1553
aoqi@0 1554 // Take requested actions on the method:
aoqi@0 1555
aoqi@0 1556 // Recompile
aoqi@0 1557 if (make_not_entrant) {
aoqi@0 1558 if (!nm->make_not_entrant()) {
aoqi@0 1559 return; // the call did not change nmethod's state
aoqi@0 1560 }
aoqi@0 1561
aoqi@0 1562 if (pdata != NULL) {
aoqi@0 1563 // Record the recompilation event, if any.
aoqi@0 1564 int tstate0 = pdata->trap_state();
aoqi@0 1565 int tstate1 = trap_state_set_recompiled(tstate0, true);
aoqi@0 1566 if (tstate1 != tstate0)
aoqi@0 1567 pdata->set_trap_state(tstate1);
aoqi@0 1568 }
aoqi@0 1569
aoqi@0 1570 #if INCLUDE_RTM_OPT
aoqi@0 1571 // Restart collecting RTM locking abort statistic if the method
aoqi@0 1572 // is recompiled for a reason other than RTM state change.
aoqi@0 1573 // Assume that in new recompiled code the statistic could be different,
aoqi@0 1574 // for example, due to different inlining.
aoqi@0 1575 if ((reason != Reason_rtm_state_change) && (trap_mdo != NULL) &&
aoqi@0 1576 UseRTMDeopt && (nm->rtm_state() != ProfileRTM)) {
aoqi@0 1577 trap_mdo->atomic_set_rtm_state(ProfileRTM);
aoqi@0 1578 }
aoqi@0 1579 #endif
aoqi@0 1580 }
aoqi@0 1581
aoqi@0 1582 if (inc_recompile_count) {
aoqi@0 1583 trap_mdo->inc_overflow_recompile_count();
aoqi@0 1584 if ((uint)trap_mdo->overflow_recompile_count() >
aoqi@0 1585 (uint)PerBytecodeRecompilationCutoff) {
aoqi@0 1586 // Give up on the method containing the bad BCI.
aoqi@0 1587 if (trap_method() == nm->method()) {
aoqi@0 1588 make_not_compilable = true;
aoqi@0 1589 } else {
aoqi@0 1590 trap_method->set_not_compilable(CompLevel_full_optimization, true, "overflow_recompile_count > PerBytecodeRecompilationCutoff");
aoqi@0 1591 // But give grace to the enclosing nm->method().
aoqi@0 1592 }
aoqi@0 1593 }
aoqi@0 1594 }
aoqi@0 1595
aoqi@0 1596 // Reprofile
aoqi@0 1597 if (reprofile) {
aoqi@0 1598 CompilationPolicy::policy()->reprofile(trap_scope, nm->is_osr_method());
aoqi@0 1599 }
aoqi@0 1600
aoqi@0 1601 // Give up compiling
aoqi@0 1602 if (make_not_compilable && !nm->method()->is_not_compilable(CompLevel_full_optimization)) {
aoqi@0 1603 assert(make_not_entrant, "consistent");
aoqi@0 1604 nm->method()->set_not_compilable(CompLevel_full_optimization);
aoqi@0 1605 }
aoqi@0 1606
aoqi@0 1607 } // Free marked resources
aoqi@0 1608
aoqi@0 1609 }
aoqi@0 1610 JRT_END
aoqi@0 1611
aoqi@0 1612 MethodData*
aoqi@0 1613 Deoptimization::get_method_data(JavaThread* thread, methodHandle m,
aoqi@0 1614 bool create_if_missing) {
aoqi@0 1615 Thread* THREAD = thread;
aoqi@0 1616 MethodData* mdo = m()->method_data();
aoqi@0 1617 if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) {
aoqi@0 1618 // Build an MDO. Ignore errors like OutOfMemory;
aoqi@0 1619 // that simply means we won't have an MDO to update.
aoqi@0 1620 Method::build_interpreter_method_data(m, THREAD);
aoqi@0 1621 if (HAS_PENDING_EXCEPTION) {
aoqi@0 1622 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
aoqi@0 1623 CLEAR_PENDING_EXCEPTION;
aoqi@0 1624 }
aoqi@0 1625 mdo = m()->method_data();
aoqi@0 1626 }
aoqi@0 1627 return mdo;
aoqi@0 1628 }
aoqi@0 1629
aoqi@0 1630 ProfileData*
aoqi@0 1631 Deoptimization::query_update_method_data(MethodData* trap_mdo,
aoqi@0 1632 int trap_bci,
aoqi@0 1633 Deoptimization::DeoptReason reason,
aoqi@0 1634 Method* compiled_method,
aoqi@0 1635 //outputs:
aoqi@0 1636 uint& ret_this_trap_count,
aoqi@0 1637 bool& ret_maybe_prior_trap,
aoqi@0 1638 bool& ret_maybe_prior_recompile) {
aoqi@0 1639 uint prior_trap_count = trap_mdo->trap_count(reason);
aoqi@0 1640 uint this_trap_count = trap_mdo->inc_trap_count(reason);
aoqi@0 1641
aoqi@0 1642 // If the runtime cannot find a place to store trap history,
aoqi@0 1643 // it is estimated based on the general condition of the method.
aoqi@0 1644 // If the method has ever been recompiled, or has ever incurred
aoqi@0 1645 // a trap with the present reason , then this BCI is assumed
aoqi@0 1646 // (pessimistically) to be the culprit.
aoqi@0 1647 bool maybe_prior_trap = (prior_trap_count != 0);
aoqi@0 1648 bool maybe_prior_recompile = (trap_mdo->decompile_count() != 0);
aoqi@0 1649 ProfileData* pdata = NULL;
aoqi@0 1650
aoqi@0 1651
aoqi@0 1652 // For reasons which are recorded per bytecode, we check per-BCI data.
aoqi@0 1653 DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
aoqi@0 1654 if (per_bc_reason != Reason_none) {
aoqi@0 1655 // Find the profile data for this BCI. If there isn't one,
aoqi@0 1656 // try to allocate one from the MDO's set of spares.
aoqi@0 1657 // This will let us detect a repeated trap at this point.
aoqi@0 1658 pdata = trap_mdo->allocate_bci_to_data(trap_bci, reason_is_speculate(reason) ? compiled_method : NULL);
aoqi@0 1659
aoqi@0 1660 if (pdata != NULL) {
aoqi@0 1661 if (reason_is_speculate(reason) && !pdata->is_SpeculativeTrapData()) {
aoqi@0 1662 if (LogCompilation && xtty != NULL) {
aoqi@0 1663 ttyLocker ttyl;
aoqi@0 1664 // no more room for speculative traps in this MDO
aoqi@0 1665 xtty->elem("speculative_traps_oom");
aoqi@0 1666 }
aoqi@0 1667 }
aoqi@0 1668 // Query the trap state of this profile datum.
aoqi@0 1669 int tstate0 = pdata->trap_state();
aoqi@0 1670 if (!trap_state_has_reason(tstate0, per_bc_reason))
aoqi@0 1671 maybe_prior_trap = false;
aoqi@0 1672 if (!trap_state_is_recompiled(tstate0))
aoqi@0 1673 maybe_prior_recompile = false;
aoqi@0 1674
aoqi@0 1675 // Update the trap state of this profile datum.
aoqi@0 1676 int tstate1 = tstate0;
aoqi@0 1677 // Record the reason.
aoqi@0 1678 tstate1 = trap_state_add_reason(tstate1, per_bc_reason);
aoqi@0 1679 // Store the updated state on the MDO, for next time.
aoqi@0 1680 if (tstate1 != tstate0)
aoqi@0 1681 pdata->set_trap_state(tstate1);
aoqi@0 1682 } else {
aoqi@0 1683 if (LogCompilation && xtty != NULL) {
aoqi@0 1684 ttyLocker ttyl;
aoqi@0 1685 // Missing MDP? Leave a small complaint in the log.
aoqi@0 1686 xtty->elem("missing_mdp bci='%d'", trap_bci);
aoqi@0 1687 }
aoqi@0 1688 }
aoqi@0 1689 }
aoqi@0 1690
aoqi@0 1691 // Return results:
aoqi@0 1692 ret_this_trap_count = this_trap_count;
aoqi@0 1693 ret_maybe_prior_trap = maybe_prior_trap;
aoqi@0 1694 ret_maybe_prior_recompile = maybe_prior_recompile;
aoqi@0 1695 return pdata;
aoqi@0 1696 }
aoqi@0 1697
aoqi@0 1698 void
aoqi@0 1699 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
aoqi@0 1700 ResourceMark rm;
aoqi@0 1701 // Ignored outputs:
aoqi@0 1702 uint ignore_this_trap_count;
aoqi@0 1703 bool ignore_maybe_prior_trap;
aoqi@0 1704 bool ignore_maybe_prior_recompile;
aoqi@0 1705 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
aoqi@0 1706 query_update_method_data(trap_mdo, trap_bci,
aoqi@0 1707 (DeoptReason)reason,
aoqi@0 1708 NULL,
aoqi@0 1709 ignore_this_trap_count,
aoqi@0 1710 ignore_maybe_prior_trap,
aoqi@0 1711 ignore_maybe_prior_recompile);
aoqi@0 1712 }
aoqi@0 1713
aoqi@0 1714 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request) {
aoqi@0 1715
aoqi@0 1716 // Still in Java no safepoints
aoqi@0 1717 {
aoqi@0 1718 // This enters VM and may safepoint
aoqi@0 1719 uncommon_trap_inner(thread, trap_request);
aoqi@0 1720 }
aoqi@0 1721 return fetch_unroll_info_helper(thread);
aoqi@0 1722 }
aoqi@0 1723
aoqi@0 1724 // Local derived constants.
aoqi@0 1725 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
aoqi@0 1726 const int DS_REASON_MASK = DataLayout::trap_mask >> 1;
aoqi@0 1727 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
aoqi@0 1728
aoqi@0 1729 //---------------------------trap_state_reason---------------------------------
aoqi@0 1730 Deoptimization::DeoptReason
aoqi@0 1731 Deoptimization::trap_state_reason(int trap_state) {
aoqi@0 1732 // This assert provides the link between the width of DataLayout::trap_bits
aoqi@0 1733 // and the encoding of "recorded" reasons. It ensures there are enough
aoqi@0 1734 // bits to store all needed reasons in the per-BCI MDO profile.
aoqi@0 1735 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
aoqi@0 1736 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
aoqi@0 1737 trap_state -= recompile_bit;
aoqi@0 1738 if (trap_state == DS_REASON_MASK) {
aoqi@0 1739 return Reason_many;
aoqi@0 1740 } else {
aoqi@0 1741 assert((int)Reason_none == 0, "state=0 => Reason_none");
aoqi@0 1742 return (DeoptReason)trap_state;
aoqi@0 1743 }
aoqi@0 1744 }
aoqi@0 1745 //-------------------------trap_state_has_reason-------------------------------
aoqi@0 1746 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
aoqi@0 1747 assert(reason_is_recorded_per_bytecode((DeoptReason)reason), "valid reason");
aoqi@0 1748 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
aoqi@0 1749 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
aoqi@0 1750 trap_state -= recompile_bit;
aoqi@0 1751 if (trap_state == DS_REASON_MASK) {
aoqi@0 1752 return -1; // true, unspecifically (bottom of state lattice)
aoqi@0 1753 } else if (trap_state == reason) {
aoqi@0 1754 return 1; // true, definitely
aoqi@0 1755 } else if (trap_state == 0) {
aoqi@0 1756 return 0; // false, definitely (top of state lattice)
aoqi@0 1757 } else {
aoqi@0 1758 return 0; // false, definitely
aoqi@0 1759 }
aoqi@0 1760 }
aoqi@0 1761 //-------------------------trap_state_add_reason-------------------------------
aoqi@0 1762 int Deoptimization::trap_state_add_reason(int trap_state, int reason) {
aoqi@0 1763 assert(reason_is_recorded_per_bytecode((DeoptReason)reason) || reason == Reason_many, "valid reason");
aoqi@0 1764 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
aoqi@0 1765 trap_state -= recompile_bit;
aoqi@0 1766 if (trap_state == DS_REASON_MASK) {
aoqi@0 1767 return trap_state + recompile_bit; // already at state lattice bottom
aoqi@0 1768 } else if (trap_state == reason) {
aoqi@0 1769 return trap_state + recompile_bit; // the condition is already true
aoqi@0 1770 } else if (trap_state == 0) {
aoqi@0 1771 return reason + recompile_bit; // no condition has yet been true
aoqi@0 1772 } else {
aoqi@0 1773 return DS_REASON_MASK + recompile_bit; // fall to state lattice bottom
aoqi@0 1774 }
aoqi@0 1775 }
aoqi@0 1776 //-----------------------trap_state_is_recompiled------------------------------
aoqi@0 1777 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
aoqi@0 1778 return (trap_state & DS_RECOMPILE_BIT) != 0;
aoqi@0 1779 }
aoqi@0 1780 //-----------------------trap_state_set_recompiled-----------------------------
aoqi@0 1781 int Deoptimization::trap_state_set_recompiled(int trap_state, bool z) {
aoqi@0 1782 if (z) return trap_state | DS_RECOMPILE_BIT;
aoqi@0 1783 else return trap_state & ~DS_RECOMPILE_BIT;
aoqi@0 1784 }
aoqi@0 1785 //---------------------------format_trap_state---------------------------------
aoqi@0 1786 // This is used for debugging and diagnostics, including LogFile output.
aoqi@0 1787 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
aoqi@0 1788 int trap_state) {
aoqi@0 1789 DeoptReason reason = trap_state_reason(trap_state);
aoqi@0 1790 bool recomp_flag = trap_state_is_recompiled(trap_state);
aoqi@0 1791 // Re-encode the state from its decoded components.
aoqi@0 1792 int decoded_state = 0;
aoqi@0 1793 if (reason_is_recorded_per_bytecode(reason) || reason == Reason_many)
aoqi@0 1794 decoded_state = trap_state_add_reason(decoded_state, reason);
aoqi@0 1795 if (recomp_flag)
aoqi@0 1796 decoded_state = trap_state_set_recompiled(decoded_state, recomp_flag);
aoqi@0 1797 // If the state re-encodes properly, format it symbolically.
aoqi@0 1798 // Because this routine is used for debugging and diagnostics,
aoqi@0 1799 // be robust even if the state is a strange value.
aoqi@0 1800 size_t len;
aoqi@0 1801 if (decoded_state != trap_state) {
aoqi@0 1802 // Random buggy state that doesn't decode??
aoqi@0 1803 len = jio_snprintf(buf, buflen, "#%d", trap_state);
aoqi@0 1804 } else {
aoqi@0 1805 len = jio_snprintf(buf, buflen, "%s%s",
aoqi@0 1806 trap_reason_name(reason),
aoqi@0 1807 recomp_flag ? " recompiled" : "");
aoqi@0 1808 }
aoqi@0 1809 if (len >= buflen)
aoqi@0 1810 buf[buflen-1] = '\0';
aoqi@0 1811 return buf;
aoqi@0 1812 }
aoqi@0 1813
aoqi@0 1814
aoqi@0 1815 //--------------------------------statics--------------------------------------
aoqi@0 1816 Deoptimization::DeoptAction Deoptimization::_unloaded_action
aoqi@0 1817 = Deoptimization::Action_reinterpret;
aoqi@0 1818 const char* Deoptimization::_trap_reason_name[Reason_LIMIT] = {
aoqi@0 1819 // Note: Keep this in sync. with enum DeoptReason.
aoqi@0 1820 "none",
aoqi@0 1821 "null_check",
aoqi@0 1822 "null_assert",
aoqi@0 1823 "range_check",
aoqi@0 1824 "class_check",
aoqi@0 1825 "array_check",
aoqi@0 1826 "intrinsic",
aoqi@0 1827 "bimorphic",
aoqi@0 1828 "unloaded",
aoqi@0 1829 "uninitialized",
aoqi@0 1830 "unreached",
aoqi@0 1831 "unhandled",
aoqi@0 1832 "constraint",
aoqi@0 1833 "div0_check",
aoqi@0 1834 "age",
aoqi@0 1835 "predicate",
aoqi@0 1836 "loop_limit_check",
aoqi@0 1837 "speculate_class_check",
aoqi@0 1838 "rtm_state_change"
aoqi@0 1839 };
aoqi@0 1840 const char* Deoptimization::_trap_action_name[Action_LIMIT] = {
aoqi@0 1841 // Note: Keep this in sync. with enum DeoptAction.
aoqi@0 1842 "none",
aoqi@0 1843 "maybe_recompile",
aoqi@0 1844 "reinterpret",
aoqi@0 1845 "make_not_entrant",
aoqi@0 1846 "make_not_compilable"
aoqi@0 1847 };
aoqi@0 1848
aoqi@0 1849 const char* Deoptimization::trap_reason_name(int reason) {
aoqi@0 1850 if (reason == Reason_many) return "many";
aoqi@0 1851 if ((uint)reason < Reason_LIMIT)
aoqi@0 1852 return _trap_reason_name[reason];
aoqi@0 1853 static char buf[20];
aoqi@0 1854 sprintf(buf, "reason%d", reason);
aoqi@0 1855 return buf;
aoqi@0 1856 }
aoqi@0 1857 const char* Deoptimization::trap_action_name(int action) {
aoqi@0 1858 if ((uint)action < Action_LIMIT)
aoqi@0 1859 return _trap_action_name[action];
aoqi@0 1860 static char buf[20];
aoqi@0 1861 sprintf(buf, "action%d", action);
aoqi@0 1862 return buf;
aoqi@0 1863 }
aoqi@0 1864
aoqi@0 1865 // This is used for debugging and diagnostics, including LogFile output.
aoqi@0 1866 const char* Deoptimization::format_trap_request(char* buf, size_t buflen,
aoqi@0 1867 int trap_request) {
aoqi@0 1868 jint unloaded_class_index = trap_request_index(trap_request);
aoqi@0 1869 const char* reason = trap_reason_name(trap_request_reason(trap_request));
aoqi@0 1870 const char* action = trap_action_name(trap_request_action(trap_request));
aoqi@0 1871 size_t len;
aoqi@0 1872 if (unloaded_class_index < 0) {
aoqi@0 1873 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'",
aoqi@0 1874 reason, action);
aoqi@0 1875 } else {
aoqi@0 1876 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'",
aoqi@0 1877 reason, action, unloaded_class_index);
aoqi@0 1878 }
aoqi@0 1879 if (len >= buflen)
aoqi@0 1880 buf[buflen-1] = '\0';
aoqi@0 1881 return buf;
aoqi@0 1882 }
aoqi@0 1883
aoqi@0 1884 juint Deoptimization::_deoptimization_hist
aoqi@0 1885 [Deoptimization::Reason_LIMIT]
aoqi@0 1886 [1 + Deoptimization::Action_LIMIT]
aoqi@0 1887 [Deoptimization::BC_CASE_LIMIT]
aoqi@0 1888 = {0};
aoqi@0 1889
aoqi@0 1890 enum {
aoqi@0 1891 LSB_BITS = 8,
aoqi@0 1892 LSB_MASK = right_n_bits(LSB_BITS)
aoqi@0 1893 };
aoqi@0 1894
aoqi@0 1895 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
aoqi@0 1896 Bytecodes::Code bc) {
aoqi@0 1897 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
aoqi@0 1898 assert(action >= 0 && action < Action_LIMIT, "oob");
aoqi@0 1899 _deoptimization_hist[Reason_none][0][0] += 1; // total
aoqi@0 1900 _deoptimization_hist[reason][0][0] += 1; // per-reason total
aoqi@0 1901 juint* cases = _deoptimization_hist[reason][1+action];
aoqi@0 1902 juint* bc_counter_addr = NULL;
aoqi@0 1903 juint bc_counter = 0;
aoqi@0 1904 // Look for an unused counter, or an exact match to this BC.
aoqi@0 1905 if (bc != Bytecodes::_illegal) {
aoqi@0 1906 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
aoqi@0 1907 juint* counter_addr = &cases[bc_case];
aoqi@0 1908 juint counter = *counter_addr;
aoqi@0 1909 if ((counter == 0 && bc_counter_addr == NULL)
aoqi@0 1910 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
aoqi@0 1911 // this counter is either free or is already devoted to this BC
aoqi@0 1912 bc_counter_addr = counter_addr;
aoqi@0 1913 bc_counter = counter | bc;
aoqi@0 1914 }
aoqi@0 1915 }
aoqi@0 1916 }
aoqi@0 1917 if (bc_counter_addr == NULL) {
aoqi@0 1918 // Overflow, or no given bytecode.
aoqi@0 1919 bc_counter_addr = &cases[BC_CASE_LIMIT-1];
aoqi@0 1920 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
aoqi@0 1921 }
aoqi@0 1922 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
aoqi@0 1923 }
aoqi@0 1924
aoqi@0 1925 jint Deoptimization::total_deoptimization_count() {
aoqi@0 1926 return _deoptimization_hist[Reason_none][0][0];
aoqi@0 1927 }
aoqi@0 1928
aoqi@0 1929 jint Deoptimization::deoptimization_count(DeoptReason reason) {
aoqi@0 1930 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
aoqi@0 1931 return _deoptimization_hist[reason][0][0];
aoqi@0 1932 }
aoqi@0 1933
aoqi@0 1934 void Deoptimization::print_statistics() {
aoqi@0 1935 juint total = total_deoptimization_count();
aoqi@0 1936 juint account = total;
aoqi@0 1937 if (total != 0) {
aoqi@0 1938 ttyLocker ttyl;
aoqi@0 1939 if (xtty != NULL) xtty->head("statistics type='deoptimization'");
aoqi@0 1940 tty->print_cr("Deoptimization traps recorded:");
aoqi@0 1941 #define PRINT_STAT_LINE(name, r) \
aoqi@0 1942 tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
aoqi@0 1943 PRINT_STAT_LINE("total", total);
aoqi@0 1944 // For each non-zero entry in the histogram, print the reason,
aoqi@0 1945 // the action, and (if specifically known) the type of bytecode.
aoqi@0 1946 for (int reason = 0; reason < Reason_LIMIT; reason++) {
aoqi@0 1947 for (int action = 0; action < Action_LIMIT; action++) {
aoqi@0 1948 juint* cases = _deoptimization_hist[reason][1+action];
aoqi@0 1949 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
aoqi@0 1950 juint counter = cases[bc_case];
aoqi@0 1951 if (counter != 0) {
aoqi@0 1952 char name[1*K];
aoqi@0 1953 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
aoqi@0 1954 if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
aoqi@0 1955 bc = Bytecodes::_illegal;
aoqi@0 1956 sprintf(name, "%s/%s/%s",
aoqi@0 1957 trap_reason_name(reason),
aoqi@0 1958 trap_action_name(action),
aoqi@0 1959 Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
aoqi@0 1960 juint r = counter >> LSB_BITS;
aoqi@0 1961 tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
aoqi@0 1962 account -= r;
aoqi@0 1963 }
aoqi@0 1964 }
aoqi@0 1965 }
aoqi@0 1966 }
aoqi@0 1967 if (account != 0) {
aoqi@0 1968 PRINT_STAT_LINE("unaccounted", account);
aoqi@0 1969 }
aoqi@0 1970 #undef PRINT_STAT_LINE
aoqi@0 1971 if (xtty != NULL) xtty->tail("statistics");
aoqi@0 1972 }
aoqi@0 1973 }
aoqi@0 1974 #else // COMPILER2 || SHARK
aoqi@0 1975
aoqi@0 1976
aoqi@0 1977 // Stubs for C1 only system.
aoqi@0 1978 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
aoqi@0 1979 return false;
aoqi@0 1980 }
aoqi@0 1981
aoqi@0 1982 const char* Deoptimization::trap_reason_name(int reason) {
aoqi@0 1983 return "unknown";
aoqi@0 1984 }
aoqi@0 1985
aoqi@0 1986 void Deoptimization::print_statistics() {
aoqi@0 1987 // no output
aoqi@0 1988 }
aoqi@0 1989
aoqi@0 1990 void
aoqi@0 1991 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
aoqi@0 1992 // no udpate
aoqi@0 1993 }
aoqi@0 1994
aoqi@0 1995 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
aoqi@0 1996 return 0;
aoqi@0 1997 }
aoqi@0 1998
aoqi@0 1999 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
aoqi@0 2000 Bytecodes::Code bc) {
aoqi@0 2001 // no update
aoqi@0 2002 }
aoqi@0 2003
aoqi@0 2004 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
aoqi@0 2005 int trap_state) {
aoqi@0 2006 jio_snprintf(buf, buflen, "#%d", trap_state);
aoqi@0 2007 return buf;
aoqi@0 2008 }
aoqi@0 2009
aoqi@0 2010 #endif // COMPILER2 || SHARK

mercurial