blob: d5ba07cdece068a9ded4173e4c4f20c036a82094 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifdef WITH_JIT
17
18/*
19 * Target independent portion of Android's Jit
20 */
21
22#include "Dalvik.h"
23#include "Jit.h"
24
25
Dan Bornsteindf4daaf2010-12-01 14:23:44 -080026#include "libdex/DexOpcodes.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070027#include <unistd.h>
28#include <pthread.h>
29#include <sys/time.h>
30#include <signal.h>
31#include "compiler/Compiler.h"
Bill Buzbee6e963e12009-06-17 16:56:19 -070032#include "compiler/CompilerUtility.h"
33#include "compiler/CompilerIR.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070034#include <errno.h>
35
Jeff Hao97319a82009-08-12 16:57:15 -070036#if defined(WITH_SELF_VERIFICATION)
37/* Allocate space for per-thread ShadowSpace data structures */
38void* dvmSelfVerificationShadowSpaceAlloc(Thread* self)
39{
40 self->shadowSpace = (ShadowSpace*) calloc(1, sizeof(ShadowSpace));
41 if (self->shadowSpace == NULL)
42 return NULL;
43
44 self->shadowSpace->registerSpaceSize = REG_SPACE;
45 self->shadowSpace->registerSpace =
46 (int*) calloc(self->shadowSpace->registerSpaceSize, sizeof(int));
47
48 return self->shadowSpace->registerSpace;
49}
50
51/* Free per-thread ShadowSpace data structures */
52void dvmSelfVerificationShadowSpaceFree(Thread* self)
53{
54 free(self->shadowSpace->registerSpace);
55 free(self->shadowSpace);
56}
57
58/*
buzbee9f601a92011-02-11 17:48:20 -080059 * Save out PC, FP, thread state, and registers to shadow space.
Jeff Hao97319a82009-08-12 16:57:15 -070060 * Return a pointer to the shadow space for JIT to use.
buzbee9f601a92011-02-11 17:48:20 -080061 *
62 * The set of saved state from the Thread structure is:
63 * pc (Dalvik PC)
64 * fp (Dalvik FP)
65 * retval
66 * method
67 * methodClassDex
68 * interpStackEnd
Jeff Hao97319a82009-08-12 16:57:15 -070069 */
buzbee9f601a92011-02-11 17:48:20 -080070void* dvmSelfVerificationSaveState(const u2* pc, u4* fp,
71 Thread* self, int targetTrace)
Jeff Hao97319a82009-08-12 16:57:15 -070072{
Jeff Hao97319a82009-08-12 16:57:15 -070073 ShadowSpace *shadowSpace = self->shadowSpace;
buzbee9f601a92011-02-11 17:48:20 -080074 unsigned preBytes = self->interpSave.method->outsSize*4 +
75 sizeof(StackSaveArea);
76 unsigned postBytes = self->interpSave.method->registersSize*4;
Jeff Hao97319a82009-08-12 16:57:15 -070077
78 //LOGD("### selfVerificationSaveState(%d) pc: 0x%x fp: 0x%x",
79 // self->threadId, (int)pc, (int)fp);
80
81 if (shadowSpace->selfVerificationState != kSVSIdle) {
82 LOGD("~~~ Save: INCORRECT PREVIOUS STATE(%d): %d",
83 self->threadId, shadowSpace->selfVerificationState);
84 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -070085 LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp);
Jeff Hao97319a82009-08-12 16:57:15 -070086 }
87 shadowSpace->selfVerificationState = kSVSStart;
88
buzbee9f601a92011-02-11 17:48:20 -080089 if (self->entryPoint == kInterpEntryResume) {
90 self->entryPoint = kInterpEntryInstr;
Ben Chengd5adae12010-03-26 17:45:28 -070091#if 0
92 /* Tracking the success rate of resume after single-stepping */
buzbee9f601a92011-02-11 17:48:20 -080093 if (self->jitResumeDPC == pc) {
Ben Chengd5adae12010-03-26 17:45:28 -070094 LOGD("SV single step resumed at %p", pc);
95 }
96 else {
buzbee9f601a92011-02-11 17:48:20 -080097 LOGD("real %p DPC %p NPC %p", pc, self->jitResumeDPC,
98 self->jitResumeNPC);
Ben Chengd5adae12010-03-26 17:45:28 -070099 }
100#endif
101 }
102
Jeff Hao97319a82009-08-12 16:57:15 -0700103 // Dynamically grow shadow register space if necessary
Ben Cheng11d8f142010-03-24 15:24:19 -0700104 if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) {
Jeff Hao97319a82009-08-12 16:57:15 -0700105 free(shadowSpace->registerSpace);
Ben Cheng11d8f142010-03-24 15:24:19 -0700106 shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4);
Jeff Hao97319a82009-08-12 16:57:15 -0700107 shadowSpace->registerSpace =
Ben Cheng11d8f142010-03-24 15:24:19 -0700108 (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4));
Jeff Hao97319a82009-08-12 16:57:15 -0700109 }
110
111 // Remember original state
112 shadowSpace->startPC = pc;
113 shadowSpace->fp = fp;
buzbee9f601a92011-02-11 17:48:20 -0800114 shadowSpace->retval = self->retval;
115 shadowSpace->interpStackEnd = self->interpStackEnd;
116
Ben Chengccd6c012009-10-15 14:52:45 -0700117 /*
118 * Store the original method here in case the trace ends with a
119 * return/invoke, the last method.
120 */
buzbee9f601a92011-02-11 17:48:20 -0800121 shadowSpace->method = self->interpSave.method;
122 shadowSpace->methodClassDex = self->interpSave.methodClassDex;
123
Jeff Hao97319a82009-08-12 16:57:15 -0700124 shadowSpace->shadowFP = shadowSpace->registerSpace +
125 shadowSpace->registerSpaceSize - postBytes/4;
126
buzbee9f601a92011-02-11 17:48:20 -0800127 self->interpSave.fp = (u4*)shadowSpace->shadowFP;
128 self->interpStackEnd = (u1*)shadowSpace->registerSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700129
130 // Create a copy of the stack
131 memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes,
132 preBytes+postBytes);
133
134 // Setup the shadowed heap space
135 shadowSpace->heapSpaceTail = shadowSpace->heapSpace;
136
137 // Reset trace length
138 shadowSpace->traceLength = 0;
139
140 return shadowSpace;
141}
142
143/*
144 * Save ending PC, FP and compiled code exit point to shadow space.
145 * Return a pointer to the shadow space for JIT to restore state.
146 */
buzbee9f601a92011-02-11 17:48:20 -0800147void* dvmSelfVerificationRestoreState(const u2* pc, u4* fp,
148 SelfVerificationState exitState,
149 Thread* self)
Jeff Hao97319a82009-08-12 16:57:15 -0700150{
Jeff Hao97319a82009-08-12 16:57:15 -0700151 ShadowSpace *shadowSpace = self->shadowSpace;
152 shadowSpace->endPC = pc;
153 shadowSpace->endShadowFP = fp;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700154 shadowSpace->jitExitState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700155
156 //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x",
157 // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp,
158 // (int)pc);
159
160 if (shadowSpace->selfVerificationState != kSVSStart) {
161 LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d",
162 self->threadId, shadowSpace->selfVerificationState);
163 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700164 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700165 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700166 LOGD("Interp FP: 0x%x", (int)shadowSpace->fp);
167 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700168 (int)shadowSpace->endShadowFP);
169 }
170
171 // Special case when punting after a single instruction
Ben Cheng7a2697d2010-06-07 13:44:23 -0700172 if (exitState == kSVSPunt && pc == shadowSpace->startPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700173 shadowSpace->selfVerificationState = kSVSIdle;
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700174 } else if (exitState == kSVSBackwardBranch && pc < shadowSpace->startPC) {
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700175 /*
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700176 * Consider a trace with a backward branch:
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700177 * 1: ..
178 * 2: ..
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700179 * 3: ..
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700180 * 4: ..
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700181 * 5: Goto {1 or 2 or 3 or 4}
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700182 *
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700183 * If there instruction 5 goes to 1 and there is no single-step
184 * instruction in the loop, pc is equal to shadowSpace->startPC and
185 * we will honor the backward branch condition.
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700186 *
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700187 * If the single-step instruction is outside the loop, then after
188 * resuming in the trace the startPC will be less than pc so we will
189 * also honor the backward branch condition.
190 *
191 * If the single-step is inside the loop, we won't hit the same endPC
192 * twice when the interpreter is re-executing the trace so we want to
193 * cancel the backward branch condition. In this case it can be
194 * detected as the endPC (ie pc) will be less than startPC.
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700195 */
196 shadowSpace->selfVerificationState = kSVSNormal;
Jeff Hao97319a82009-08-12 16:57:15 -0700197 } else {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700198 shadowSpace->selfVerificationState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700199 }
200
buzbee9f601a92011-02-11 17:48:20 -0800201 /* Restore state before returning */
202 self->interpSave.pc = shadowSpace->startPC;
203 self->interpSave.fp = shadowSpace->fp;
204 self->interpSave.method = shadowSpace->method;
205 self->interpSave.methodClassDex = shadowSpace->methodClassDex;
206 self->retval = shadowSpace->retval;
207 self->interpStackEnd = shadowSpace->interpStackEnd;
208
Jeff Hao97319a82009-08-12 16:57:15 -0700209 return shadowSpace;
210}
211
212/* Print contents of virtual registers */
Ben Chengccd6c012009-10-15 14:52:45 -0700213static void selfVerificationPrintRegisters(int* addr, int* addrRef,
214 int numWords)
Jeff Hao97319a82009-08-12 16:57:15 -0700215{
216 int i;
217 for (i = 0; i < numWords; i++) {
Ben Chengccd6c012009-10-15 14:52:45 -0700218 LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : "");
Jeff Hao97319a82009-08-12 16:57:15 -0700219 }
220}
221
222/* Print values maintained in shadowSpace */
223static void selfVerificationDumpState(const u2* pc, Thread* self)
224{
225 ShadowSpace* shadowSpace = self->shadowSpace;
226 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
227 int frameBytes = (int) shadowSpace->registerSpace +
228 shadowSpace->registerSpaceSize*4 -
229 (int) shadowSpace->shadowFP;
230 int localRegs = 0;
231 int frameBytes2 = 0;
buzbee9f601a92011-02-11 17:48:20 -0800232 if ((uintptr_t)self->curFrame < (uintptr_t)shadowSpace->fp) {
Jeff Hao97319a82009-08-12 16:57:15 -0700233 localRegs = (stackSave->method->registersSize -
234 stackSave->method->insSize)*4;
235 frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs;
236 }
237 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700238 LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
Jeff Hao97319a82009-08-12 16:57:15 -0700239 (int)(pc - stackSave->method->insns));
Ben Chengccd6c012009-10-15 14:52:45 -0700240 LOGD("Class: %s", shadowSpace->method->clazz->descriptor);
241 LOGD("Method: %s", shadowSpace->method->name);
242 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700243 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700244 LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
Jeff Hao97319a82009-08-12 16:57:15 -0700245 (int)self->curFrame);
Ben Chengccd6c012009-10-15 14:52:45 -0700246 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700247 (int)shadowSpace->endShadowFP);
Ben Chengccd6c012009-10-15 14:52:45 -0700248 LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
Jeff Hao97319a82009-08-12 16:57:15 -0700249 localRegs, frameBytes2);
Ben Chengccd6c012009-10-15 14:52:45 -0700250 LOGD("Trace length: %d State: %d", shadowSpace->traceLength,
Jeff Hao97319a82009-08-12 16:57:15 -0700251 shadowSpace->selfVerificationState);
252}
253
254/* Print decoded instructions in the current trace */
255static void selfVerificationDumpTrace(const u2* pc, Thread* self)
256{
257 ShadowSpace* shadowSpace = self->shadowSpace;
258 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700259 int i, addr, offset;
260 DecodedInstruction *decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700261
262 LOGD("********** SHADOW TRACE DUMP **********");
263 for (i = 0; i < shadowSpace->traceLength; i++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700264 addr = shadowSpace->trace[i].addr;
265 offset = (int)((u2*)addr - stackSave->method->insns);
266 decInsn = &(shadowSpace->trace[i].decInsn);
267 /* Not properly decoding instruction, some registers may be garbage */
Andy McFaddenc6b25c72010-06-22 11:01:20 -0700268 LOGD("0x%x: (0x%04x) %s",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800269 addr, offset, dexGetOpcodeName(decInsn->opcode));
Jeff Hao97319a82009-08-12 16:57:15 -0700270 }
271}
272
Ben Chengbcdc1de2009-08-21 16:18:46 -0700273/* Code is forced into this spin loop when a divergence is detected */
Ben Chengccd6c012009-10-15 14:52:45 -0700274static void selfVerificationSpinLoop(ShadowSpace *shadowSpace)
Ben Chengbcdc1de2009-08-21 16:18:46 -0700275{
Ben Chengccd6c012009-10-15 14:52:45 -0700276 const u2 *startPC = shadowSpace->startPC;
Ben Cheng88a0f972010-02-24 15:00:40 -0800277 JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL);
Ben Chengccd6c012009-10-15 14:52:45 -0700278 if (desc) {
279 dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc);
Ben Cheng1357e942010-02-10 17:21:39 -0800280 /*
281 * This function effectively terminates the VM right here, so not
282 * freeing the desc pointer when the enqueuing fails is acceptable.
283 */
Ben Chengccd6c012009-10-15 14:52:45 -0700284 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700285 gDvmJit.selfVerificationSpin = true;
286 while(gDvmJit.selfVerificationSpin) sleep(10);
287}
288
Jeff Hao97319a82009-08-12 16:57:15 -0700289/* Manage self verification while in the debug interpreter */
buzbee9f601a92011-02-11 17:48:20 -0800290static bool selfVerificationDebugInterp(const u2* pc, Thread* self)
Jeff Hao97319a82009-08-12 16:57:15 -0700291{
292 ShadowSpace *shadowSpace = self->shadowSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700293 SelfVerificationState state = shadowSpace->selfVerificationState;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700294
295 DecodedInstruction decInsn;
Dan Bornstein54322392010-11-17 14:16:56 -0800296 dexDecodeInstruction(pc, &decInsn);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700297
Jeff Hao97319a82009-08-12 16:57:15 -0700298 //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
299 // self->threadId, (int)pc, (int)shadowSpace->endPC, state,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800300 // shadowSpace->traceLength, dexGetOpcodeName(decInsn.opcode));
Jeff Hao97319a82009-08-12 16:57:15 -0700301
302 if (state == kSVSIdle || state == kSVSStart) {
303 LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d",
304 self->threadId, state);
305 selfVerificationDumpState(pc, self);
306 selfVerificationDumpTrace(pc, self);
307 }
308
Ben Chengd5adae12010-03-26 17:45:28 -0700309 /*
310 * Skip endPC once when trace has a backward branch. If the SV state is
311 * single step, keep it that way.
312 */
Jeff Hao97319a82009-08-12 16:57:15 -0700313 if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
Ben Chengd5adae12010-03-26 17:45:28 -0700314 (state != kSVSBackwardBranch && state != kSVSSingleStep)) {
Jeff Hao97319a82009-08-12 16:57:15 -0700315 shadowSpace->selfVerificationState = kSVSDebugInterp;
316 }
317
318 /* Check that the current pc is the end of the trace */
Ben Chengd5adae12010-03-26 17:45:28 -0700319 if ((state == kSVSDebugInterp || state == kSVSSingleStep) &&
320 pc == shadowSpace->endPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700321
322 shadowSpace->selfVerificationState = kSVSIdle;
323
324 /* Check register space */
325 int frameBytes = (int) shadowSpace->registerSpace +
326 shadowSpace->registerSpaceSize*4 -
327 (int) shadowSpace->shadowFP;
328 if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700329 LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId);
Jeff Hao97319a82009-08-12 16:57:15 -0700330 selfVerificationDumpState(pc, self);
331 selfVerificationDumpTrace(pc, self);
332 LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
333 (int)shadowSpace->fp, frameBytes);
Ben Chengccd6c012009-10-15 14:52:45 -0700334 selfVerificationPrintRegisters((int*)shadowSpace->fp,
335 (int*)shadowSpace->shadowFP,
336 frameBytes/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700337 LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
338 (int)shadowSpace->shadowFP, frameBytes);
339 selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700340 (int*)shadowSpace->fp,
341 frameBytes/4);
342 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700343 }
344 /* Check new frame if it exists (invokes only) */
buzbee9f601a92011-02-11 17:48:20 -0800345 if ((uintptr_t)self->curFrame < (uintptr_t)shadowSpace->fp) {
Jeff Hao97319a82009-08-12 16:57:15 -0700346 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
347 int localRegs = (stackSave->method->registersSize -
348 stackSave->method->insSize)*4;
349 int frameBytes2 = (int) shadowSpace->fp -
350 (int) self->curFrame - localRegs;
351 if (memcmp(((char*)self->curFrame)+localRegs,
352 ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700353 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!",
Jeff Hao97319a82009-08-12 16:57:15 -0700354 self->threadId);
355 selfVerificationDumpState(pc, self);
356 selfVerificationDumpTrace(pc, self);
357 LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d",
358 (int)self->curFrame, localRegs, frameBytes2);
359 selfVerificationPrintRegisters((int*)self->curFrame,
Ben Chengccd6c012009-10-15 14:52:45 -0700360 (int*)shadowSpace->endShadowFP,
361 (frameBytes2+localRegs)/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700362 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
363 (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
364 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700365 (int*)self->curFrame,
366 (frameBytes2+localRegs)/4);
367 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700368 }
369 }
370
371 /* Check memory space */
Ben Chengbcdc1de2009-08-21 16:18:46 -0700372 bool memDiff = false;
Jeff Hao97319a82009-08-12 16:57:15 -0700373 ShadowHeap* heapSpacePtr;
374 for (heapSpacePtr = shadowSpace->heapSpace;
375 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700376 int memData = *((unsigned int*) heapSpacePtr->addr);
377 if (heapSpacePtr->data != memData) {
Ben Chengccd6c012009-10-15 14:52:45 -0700378 LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId);
379 LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
Ben Chengbcdc1de2009-08-21 16:18:46 -0700380 heapSpacePtr->addr, memData, heapSpacePtr->data);
Jeff Hao97319a82009-08-12 16:57:15 -0700381 selfVerificationDumpState(pc, self);
382 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700383 memDiff = true;
Jeff Hao97319a82009-08-12 16:57:15 -0700384 }
385 }
Ben Chengccd6c012009-10-15 14:52:45 -0700386 if (memDiff) selfVerificationSpinLoop(shadowSpace);
Ben Chengd5adae12010-03-26 17:45:28 -0700387
388 /*
389 * Switch to JIT single step mode to stay in the debug interpreter for
390 * one more instruction
391 */
392 if (state == kSVSSingleStep) {
buzbee9f601a92011-02-11 17:48:20 -0800393 self->jitState = kJitSingleStepEnd;
Ben Chengd5adae12010-03-26 17:45:28 -0700394 }
Jeff Hao97319a82009-08-12 16:57:15 -0700395 return true;
396
397 /* If end not been reached, make sure max length not exceeded */
398 } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) {
399 LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId);
Ben Chengccd6c012009-10-15 14:52:45 -0700400 LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700401 (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
402 selfVerificationDumpState(pc, self);
403 selfVerificationDumpTrace(pc, self);
Ben Chengccd6c012009-10-15 14:52:45 -0700404 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700405
406 return true;
407 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700408 /* Log the instruction address and decoded instruction for debug */
Jeff Hao97319a82009-08-12 16:57:15 -0700409 shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700410 shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700411 shadowSpace->traceLength++;
412
413 return false;
414}
415#endif
416
Ben Chengba4fc8b2009-06-01 13:00:29 -0700417/*
418 * If one of our fixed tables or the translation buffer fills up,
419 * call this routine to avoid wasting cycles on future translation requests.
420 */
421void dvmJitStopTranslationRequests()
422{
423 /*
424 * Note 1: This won't necessarily stop all translation requests, and
425 * operates on a delayed mechanism. Running threads look to the copy
buzbee9f601a92011-02-11 17:48:20 -0800426 * of this value in their private thread structures and won't see
Ben Chengba4fc8b2009-06-01 13:00:29 -0700427 * this change until it is refreshed (which happens on interpreter
428 * entry).
429 * Note 2: This is a one-shot memory leak on this table. Because this is a
430 * permanent off switch for Jit profiling, it is a one-time leak of 1K
431 * bytes, and no further attempt will be made to re-allocate it. Can't
432 * free it because some thread may be holding a reference.
433 */
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800434 gDvmJit.pProfTable = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700435}
436
Ben Cheng978738d2010-05-13 13:45:57 -0700437#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700438/* Convenience function to increment counter from assembly code */
Ben Cheng6c10a972009-10-29 14:39:18 -0700439void dvmBumpNoChain(int from)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700440{
Ben Cheng6c10a972009-10-29 14:39:18 -0700441 gDvmJit.noChainExit[from]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700442}
443
444/* Convenience function to increment counter from assembly code */
445void dvmBumpNormal()
446{
Ben Cheng6c10a972009-10-29 14:39:18 -0700447 gDvmJit.normalExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700448}
449
450/* Convenience function to increment counter from assembly code */
451void dvmBumpPunt(int from)
452{
Ben Cheng6c10a972009-10-29 14:39:18 -0700453 gDvmJit.puntExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454}
455#endif
456
457/* Dumps debugging & tuning stats to the log */
458void dvmJitStats()
459{
460 int i;
461 int hit;
462 int not_hit;
463 int chains;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800464 int stubs;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700465 if (gDvmJit.pJitEntryTable) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800466 for (i=0, stubs=chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700467 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700468 i++) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800469 if (gDvmJit.pJitEntryTable[i].dPC != 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 hit++;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800471 if (gDvmJit.pJitEntryTable[i].codeAddress ==
Bill Buzbeebd047242010-05-13 13:02:53 -0700472 dvmCompilerGetInterpretTemplate())
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800473 stubs++;
474 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -0700475 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700476 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700477 chains++;
478 }
Ben Cheng72621c92010-03-10 13:12:55 -0800479 LOGD("JIT: table size is %d, entries used is %d",
Ben Cheng86717f72010-03-05 15:27:21 -0800480 gDvmJit.jitTableSize, gDvmJit.jitTableEntriesUsed);
Ben Cheng72621c92010-03-10 13:12:55 -0800481 LOGD("JIT: %d traces, %d slots, %d chains, %d thresh, %s",
482 hit, not_hit + hit, chains, gDvmJit.threshold,
483 gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
Ben Cheng86717f72010-03-05 15:27:21 -0800484
Ben Cheng978738d2010-05-13 13:45:57 -0700485#if defined(WITH_JIT_TUNING)
486 LOGD("JIT: Code cache patches: %d", gDvmJit.codeCachePatches);
487
Ben Cheng72621c92010-03-10 13:12:55 -0800488 LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
489 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
490 gDvmJit.normalExit, gDvmJit.puntExit);
Ben Cheng452efba2010-04-30 15:14:00 -0700491
Ben Cheng978738d2010-05-13 13:45:57 -0700492 LOGD("JIT: ICHits: %d", gDvmICHitCount);
493
Ben Cheng72621c92010-03-10 13:12:55 -0800494 LOGD("JIT: noChainExit: %d IC miss, %d interp callsite, "
495 "%d switch overflow",
496 gDvmJit.noChainExit[kInlineCacheMiss],
497 gDvmJit.noChainExit[kCallsiteInterpreted],
498 gDvmJit.noChainExit[kSwitchOverflow]);
Ben Cheng86717f72010-03-05 15:27:21 -0800499
Ben Chengb88ec3c2010-05-17 12:50:33 -0700500 LOGD("JIT: ICPatch: %d init, %d rejected, %d lock-free, %d queued, "
501 "%d dropped",
502 gDvmJit.icPatchInit, gDvmJit.icPatchRejected,
503 gDvmJit.icPatchLockFree, gDvmJit.icPatchQueued,
Ben Cheng452efba2010-04-30 15:14:00 -0700504 gDvmJit.icPatchDropped);
505
Ben Cheng86717f72010-03-05 15:27:21 -0800506 LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return",
507 gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic,
508 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700509 LOGD("JIT: Inline: %d mgetter, %d msetter, %d pgetter, %d psetter",
510 gDvmJit.invokeMonoGetterInlined, gDvmJit.invokeMonoSetterInlined,
511 gDvmJit.invokePolyGetterInlined, gDvmJit.invokePolySetterInlined);
Ben Cheng86717f72010-03-05 15:27:21 -0800512 LOGD("JIT: Total compilation time: %llu ms", gDvmJit.jitTime / 1000);
513 LOGD("JIT: Avg unit compilation time: %llu us",
514 gDvmJit.jitTime / gDvmJit.numCompilations);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700515#endif
Ben Cheng86717f72010-03-05 15:27:21 -0800516
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800517 LOGD("JIT: %d Translation chains, %d interp stubs",
518 gDvmJit.translationChains, stubs);
buzbee2e152ba2010-12-15 16:32:35 -0800519 if (gDvmJit.profileMode == kTraceProfilingContinuous) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700520 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700521 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700522 }
523}
524
Bill Buzbee716f1202009-07-23 13:22:09 -0700525
Bill Buzbee1b3da592011-02-03 07:38:22 -0800526/* End current trace after last successful instruction */
buzbee9f601a92011-02-11 17:48:20 -0800527void dvmJitEndTraceSelect(Thread* self)
Bill Buzbeed7269912009-11-10 14:31:32 -0800528{
buzbee9f601a92011-02-11 17:48:20 -0800529 if (self->jitState == kJitTSelect)
530 self->jitState = kJitTSelectEnd;
Bill Buzbeed7269912009-11-10 14:31:32 -0800531}
532
Ben Chengba4fc8b2009-06-01 13:00:29 -0700533/*
Bill Buzbee964a7b02010-01-28 12:54:19 -0800534 * Find an entry in the JitTable, creating if necessary.
535 * Returns null if table is full.
536 */
Ben Chengcfdeca32011-01-14 11:36:46 -0800537static JitEntry *lookupAndAdd(const u2* dPC, bool callerLocked,
538 bool isMethodEntry)
Bill Buzbee964a7b02010-01-28 12:54:19 -0800539{
540 u4 chainEndMarker = gDvmJit.jitTableSize;
541 u4 idx = dvmJitHash(dPC);
542
Ben Chengcfdeca32011-01-14 11:36:46 -0800543 /*
544 * Walk the bucket chain to find an exact match for our PC and trace/method
545 * type
546 */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800547 while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
Ben Chengcfdeca32011-01-14 11:36:46 -0800548 ((gDvmJit.pJitEntryTable[idx].dPC != dPC) ||
549 (gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry !=
550 isMethodEntry))) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800551 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
552 }
553
Ben Chengcfdeca32011-01-14 11:36:46 -0800554 if (gDvmJit.pJitEntryTable[idx].dPC != dPC ||
555 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry != isMethodEntry) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800556 /*
557 * No match. Aquire jitTableLock and find the last
558 * slot in the chain. Possibly continue the chain walk in case
559 * some other thread allocated the slot we were looking
560 * at previuosly (perhaps even the dPC we're trying to enter).
561 */
562 if (!callerLocked)
563 dvmLockMutex(&gDvmJit.tableLock);
564 /*
565 * At this point, if .dPC is NULL, then the slot we're
566 * looking at is the target slot from the primary hash
567 * (the simple, and common case). Otherwise we're going
568 * to have to find a free slot and chain it.
569 */
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700570 ANDROID_MEMBAR_FULL(); /* Make sure we reload [].dPC after lock */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800571 if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
572 u4 prev;
573 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
Ben Chengcfdeca32011-01-14 11:36:46 -0800574 if (gDvmJit.pJitEntryTable[idx].dPC == dPC &&
575 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry ==
576 isMethodEntry) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800577 /* Another thread got there first for this dPC */
578 if (!callerLocked)
579 dvmUnlockMutex(&gDvmJit.tableLock);
580 return &gDvmJit.pJitEntryTable[idx];
581 }
582 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
583 }
584 /* Here, idx should be pointing to the last cell of an
585 * active chain whose last member contains a valid dPC */
586 assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
587 /* Linear walk to find a free cell and add it to the end */
588 prev = idx;
589 while (true) {
590 idx++;
591 if (idx == chainEndMarker)
592 idx = 0; /* Wraparound */
593 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
594 (idx == prev))
595 break;
596 }
597 if (idx != prev) {
598 JitEntryInfoUnion oldValue;
599 JitEntryInfoUnion newValue;
600 /*
601 * Although we hold the lock so that noone else will
602 * be trying to update a chain field, the other fields
603 * packed into the word may be in use by other threads.
604 */
605 do {
606 oldValue = gDvmJit.pJitEntryTable[prev].u;
607 newValue = oldValue;
608 newValue.info.chain = idx;
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700609 } while (android_atomic_release_cas(oldValue.infoWord,
610 newValue.infoWord,
611 &gDvmJit.pJitEntryTable[prev].u.infoWord) != 0);
Bill Buzbee964a7b02010-01-28 12:54:19 -0800612 }
613 }
614 if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
Ben Chengcfdeca32011-01-14 11:36:46 -0800615 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry = isMethodEntry;
Bill Buzbee964a7b02010-01-28 12:54:19 -0800616 /*
617 * Initialize codeAddress and allocate the slot. Must
618 * happen in this order (since dPC is set, the entry is live.
619 */
Ben Chengcfdeca32011-01-14 11:36:46 -0800620 android_atomic_release_store((int32_t)dPC,
621 (volatile int32_t *)(void *)&gDvmJit.pJitEntryTable[idx].dPC);
Bill Buzbee964a7b02010-01-28 12:54:19 -0800622 gDvmJit.pJitEntryTable[idx].dPC = dPC;
623 gDvmJit.jitTableEntriesUsed++;
624 } else {
625 /* Table is full */
626 idx = chainEndMarker;
627 }
628 if (!callerLocked)
629 dvmUnlockMutex(&gDvmJit.tableLock);
630 }
631 return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
632}
Ben Chenga4973592010-03-31 11:59:18 -0700633
Bill Buzbee964a7b02010-01-28 12:54:19 -0800634/*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700635 * Append the class ptr of "this" and the current method ptr to the current
636 * trace. That is, the trace runs will contain the following components:
637 * + trace run that ends with an invoke (existing entry)
638 * + thisClass (new)
639 * + calleeMethod (new)
640 */
buzbee9f601a92011-02-11 17:48:20 -0800641static void insertClassMethodInfo(Thread* self,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700642 const ClassObject* thisClass,
643 const Method* calleeMethod,
644 const DecodedInstruction* insn)
645{
buzbee9f601a92011-02-11 17:48:20 -0800646 int currTraceRun = ++self->currTraceRun;
647 self->trace[currTraceRun].meta = (void *) thisClass;
648 currTraceRun = ++self->currTraceRun;
649 self->trace[currTraceRun].meta = (void *) calleeMethod;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700650}
651
652/*
Ben Chengd44faf52010-06-02 15:33:51 -0700653 * Check if the next instruction following the invoke is a move-result and if
Ben Cheng7a2697d2010-06-07 13:44:23 -0700654 * so add it to the trace. That is, this will add the trace run that includes
655 * the move-result to the trace list.
656 *
657 * + trace run that ends with an invoke (existing entry)
658 * + thisClass (existing entry)
659 * + calleeMethod (existing entry)
660 * + move result (new)
Ben Chengd44faf52010-06-02 15:33:51 -0700661 *
662 * lastPC, len, offset are all from the preceding invoke instruction
663 */
664static void insertMoveResult(const u2 *lastPC, int len, int offset,
buzbee9f601a92011-02-11 17:48:20 -0800665 Thread *self)
Ben Chengd44faf52010-06-02 15:33:51 -0700666{
667 DecodedInstruction nextDecInsn;
668 const u2 *moveResultPC = lastPC + len;
669
Dan Bornstein54322392010-11-17 14:16:56 -0800670 dexDecodeInstruction(moveResultPC, &nextDecInsn);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800671 if ((nextDecInsn.opcode != OP_MOVE_RESULT) &&
672 (nextDecInsn.opcode != OP_MOVE_RESULT_WIDE) &&
673 (nextDecInsn.opcode != OP_MOVE_RESULT_OBJECT))
Ben Chengd44faf52010-06-02 15:33:51 -0700674 return;
675
676 /* We need to start a new trace run */
buzbee9f601a92011-02-11 17:48:20 -0800677 int currTraceRun = ++self->currTraceRun;
678 self->currRunHead = moveResultPC;
679 self->trace[currTraceRun].frag.startOffset = offset + len;
680 self->trace[currTraceRun].frag.numInsts = 1;
681 self->trace[currTraceRun].frag.runEnd = false;
682 self->trace[currTraceRun].frag.hint = kJitHintNone;
683 self->trace[currTraceRun].frag.isCode = true;
684 self->totalTraceLen++;
Ben Chengd44faf52010-06-02 15:33:51 -0700685
buzbee9f601a92011-02-11 17:48:20 -0800686 self->currRunLen = dexGetWidthFromInstruction(moveResultPC);
Ben Chengd44faf52010-06-02 15:33:51 -0700687}
688
689/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 * Adds to the current trace request one instruction at a time, just
691 * before that instruction is interpreted. This is the primary trace
692 * selection function. NOTE: return instruction are handled a little
693 * differently. In general, instructions are "proposed" to be added
694 * to the current trace prior to interpretation. If the interpreter
695 * then successfully completes the instruction, is will be considered
696 * part of the request. This allows us to examine machine state prior
697 * to interpretation, and also abort the trace request if the instruction
698 * throws or does something unexpected. However, return instructions
699 * will cause an immediate end to the translation request - which will
700 * be passed to the compiler before the return completes. This is done
701 * in response to special handling of returns by the interpreter (and
702 * because returns cannot throw in a way that causes problems for the
703 * translated code.
704 */
buzbee9f601a92011-02-11 17:48:20 -0800705int dvmCheckJit(const u2* pc, Thread* self, const ClassObject* thisClass,
706 const Method* curMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700707{
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700708 int flags, len;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 int switchInterp = false;
Ben Chenga4973592010-03-31 11:59:18 -0700710 bool debugOrProfile = dvmDebuggerOrProfilerActive();
Ben Cheng7a2697d2010-06-07 13:44:23 -0700711 /* Stay in the dbg interpreter for the next instruction */
712 bool stayOneMoreInst = false;
Bill Buzbeed7269912009-11-10 14:31:32 -0800713
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700714 /*
715 * Bug 2710533 - dalvik crash when disconnecting debugger
716 *
717 * Reset the entry point to the default value. If needed it will be set to a
718 * specific value in the corresponding case statement (eg kJitSingleStepEnd)
719 */
buzbee9f601a92011-02-11 17:48:20 -0800720 self->entryPoint = kInterpEntryInstr;
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700721
Ben Cheng79d173c2009-09-29 16:12:51 -0700722 /* Prepare to handle last PC and stage the current PC */
buzbee9f601a92011-02-11 17:48:20 -0800723 const u2 *lastPC = self->lastPC;
724 self->lastPC = pc;
Ben Cheng79d173c2009-09-29 16:12:51 -0700725
buzbee9f601a92011-02-11 17:48:20 -0800726 switch (self->jitState) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700727 int offset;
728 DecodedInstruction decInsn;
729 case kJitTSelect:
Ben Chengdc84bb22009-10-02 12:58:52 -0700730 /* First instruction - just remember the PC and exit */
731 if (lastPC == NULL) break;
Ben Cheng79d173c2009-09-29 16:12:51 -0700732 /* Grow the trace around the last PC if jitState is kJitTSelect */
Dan Bornstein54322392010-11-17 14:16:56 -0800733 dexDecodeInstruction(lastPC, &decInsn);
Ben Cheng6c10a972009-10-29 14:39:18 -0700734
735 /*
736 * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due
737 * to the amount of space it takes to generate the chaining
738 * cells.
739 */
buzbee9f601a92011-02-11 17:48:20 -0800740 if (self->totalTraceLen != 0 &&
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800741 (decInsn.opcode == OP_PACKED_SWITCH ||
742 decInsn.opcode == OP_SPARSE_SWITCH)) {
buzbee9f601a92011-02-11 17:48:20 -0800743 self->jitState = kJitTSelectEnd;
Ben Cheng6c10a972009-10-29 14:39:18 -0700744 break;
745 }
746
Bill Buzbeef9f33282009-11-22 12:45:30 -0800747
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748#if defined(SHOW_TRACE)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800749 LOGD("TraceGen: adding %s", dexGetOpcodeName(decInsn.opcode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750#endif
Dan Bornsteine4852762010-12-02 12:45:00 -0800751 flags = dexGetFlagsFromOpcode(decInsn.opcode);
752 len = dexGetWidthFromInstruction(lastPC);
buzbee9f601a92011-02-11 17:48:20 -0800753 offset = lastPC - self->interpSave.method->insns;
Ben Cheng79d173c2009-09-29 16:12:51 -0700754 assert((unsigned) offset <
buzbee9f601a92011-02-11 17:48:20 -0800755 dvmGetMethodInsnsSize(self->interpSave.method));
756 if (lastPC != self->currRunHead + self->currRunLen) {
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700757 int currTraceRun;
758 /* We need to start a new trace run */
buzbee9f601a92011-02-11 17:48:20 -0800759 currTraceRun = ++self->currTraceRun;
760 self->currRunLen = 0;
761 self->currRunHead = (u2*)lastPC;
762 self->trace[currTraceRun].frag.startOffset = offset;
763 self->trace[currTraceRun].frag.numInsts = 0;
764 self->trace[currTraceRun].frag.runEnd = false;
765 self->trace[currTraceRun].frag.hint = kJitHintNone;
766 self->trace[currTraceRun].frag.isCode = true;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700767 }
buzbee9f601a92011-02-11 17:48:20 -0800768 self->trace[self->currTraceRun].frag.numInsts++;
769 self->totalTraceLen++;
770 self->currRunLen += len;
Ben Cheng79d173c2009-09-29 16:12:51 -0700771
Ben Chengd44faf52010-06-02 15:33:51 -0700772 /*
773 * If the last instruction is an invoke, we will try to sneak in
774 * the move-result* (if existent) into a separate trace run.
775 */
776 int needReservedRun = (flags & kInstrInvoke) ? 1 : 0;
777
Ben Cheng79d173c2009-09-29 16:12:51 -0700778 /* Will probably never hit this with the current trace buildier */
buzbee9f601a92011-02-11 17:48:20 -0800779 if (self->currTraceRun ==
Ben Chengd44faf52010-06-02 15:33:51 -0700780 (MAX_JIT_RUN_LEN - 1 - needReservedRun)) {
buzbee9f601a92011-02-11 17:48:20 -0800781 self->jitState = kJitTSelectEnd;
Ben Cheng79d173c2009-09-29 16:12:51 -0700782 }
783
Dan Bornsteinc2b486f2010-11-12 16:07:16 -0800784 if (!dexIsGoto(flags) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700785 ((flags & (kInstrCanBranch |
786 kInstrCanSwitch |
787 kInstrCanReturn |
788 kInstrInvoke)) != 0)) {
buzbee9f601a92011-02-11 17:48:20 -0800789 self->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790#if defined(SHOW_TRACE)
Ben Chengd44faf52010-06-02 15:33:51 -0700791 LOGD("TraceGen: ending on %s, basic block end",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800792 dexGetOpcodeName(decInsn.opcode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793#endif
Ben Chengd44faf52010-06-02 15:33:51 -0700794
795 /*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700796 * If the current invoke is a {virtual,interface}, get the
797 * current class/method pair into the trace as well.
Ben Chengd44faf52010-06-02 15:33:51 -0700798 * If the next instruction is a variant of move-result, insert
Ben Cheng7a2697d2010-06-07 13:44:23 -0700799 * it to the trace too.
Ben Chengd44faf52010-06-02 15:33:51 -0700800 */
801 if (flags & kInstrInvoke) {
buzbee9f601a92011-02-11 17:48:20 -0800802 insertClassMethodInfo(self, thisClass, curMethod,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700803 &decInsn);
buzbee9f601a92011-02-11 17:48:20 -0800804 insertMoveResult(lastPC, len, offset, self);
Ben Chengd44faf52010-06-02 15:33:51 -0700805 }
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700806 }
Bill Buzbee2ce8a6c2009-12-03 15:09:32 -0800807 /* Break on throw or self-loop */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800808 if ((decInsn.opcode == OP_THROW) || (lastPC == pc)){
buzbee9f601a92011-02-11 17:48:20 -0800809 self->jitState = kJitTSelectEnd;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700810 }
buzbee9f601a92011-02-11 17:48:20 -0800811 if (self->totalTraceLen >= JIT_MAX_TRACE_LEN) {
812 self->jitState = kJitTSelectEnd;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700813 }
Ben Chenga4973592010-03-31 11:59:18 -0700814 /* Abandon the trace request if debugger/profiler is attached */
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700815 if (debugOrProfile) {
buzbee9f601a92011-02-11 17:48:20 -0800816 self->jitState = kJitDone;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700817 break;
818 }
819 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
820 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700821 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700822 else {
823 /*
824 * Last instruction is a return - stay in the dbg interpreter
825 * for one more instruction if it is a non-void return, since
826 * we don't want to start a trace with move-result as the first
827 * instruction (which is already included in the trace
828 * containing the invoke.
829 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800830 if (decInsn.opcode != OP_RETURN_VOID) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700831 stayOneMoreInst = true;
832 }
833 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700834 /* NOTE: intentional fallthrough for returns */
835 case kJitTSelectEnd:
836 {
Bill Buzbee1b3da592011-02-03 07:38:22 -0800837 /* Empty trace - set to bail to interpreter */
buzbee9f601a92011-02-11 17:48:20 -0800838 if (self->totalTraceLen == 0) {
839 dvmJitSetCodeAddr(self->currTraceHead,
Bill Buzbee1b3da592011-02-03 07:38:22 -0800840 dvmCompilerGetInterpretTemplate(),
841 dvmCompilerGetInterpretTemplateSet(),
842 false /* Not method entry */, 0);
buzbee9f601a92011-02-11 17:48:20 -0800843 self->jitState = kJitDone;
Ben Chenga4973592010-03-31 11:59:18 -0700844 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700845 break;
846 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700847
buzbee9f601a92011-02-11 17:48:20 -0800848 int lastTraceDesc = self->currTraceRun;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700849
850 /* Extend a new empty desc if the last slot is meta info */
buzbee9f601a92011-02-11 17:48:20 -0800851 if (!self->trace[lastTraceDesc].frag.isCode) {
852 lastTraceDesc = ++self->currTraceRun;
853 self->trace[lastTraceDesc].frag.startOffset = 0;
854 self->trace[lastTraceDesc].frag.numInsts = 0;
855 self->trace[lastTraceDesc].frag.hint = kJitHintNone;
856 self->trace[lastTraceDesc].frag.isCode = true;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700857 }
858
859 /* Mark the end of the trace runs */
buzbee9f601a92011-02-11 17:48:20 -0800860 self->trace[lastTraceDesc].frag.runEnd = true;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700861
Ben Chengba4fc8b2009-06-01 13:00:29 -0700862 JitTraceDescription* desc =
863 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
buzbee9f601a92011-02-11 17:48:20 -0800864 sizeof(JitTraceRun) * (self->currTraceRun+1));
Ben Cheng7a2697d2010-06-07 13:44:23 -0700865
Ben Chengba4fc8b2009-06-01 13:00:29 -0700866 if (desc == NULL) {
867 LOGE("Out of memory in trace selection");
868 dvmJitStopTranslationRequests();
buzbee9f601a92011-02-11 17:48:20 -0800869 self->jitState = kJitDone;
Ben Chenga4973592010-03-31 11:59:18 -0700870 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700871 break;
872 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700873
buzbee9f601a92011-02-11 17:48:20 -0800874 desc->method = self->interpSave.method;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700875 memcpy((char*)&(desc->trace[0]),
buzbee9f601a92011-02-11 17:48:20 -0800876 (char*)&(self->trace[0]),
877 sizeof(JitTraceRun) * (self->currTraceRun+1));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700878#if defined(SHOW_TRACE)
879 LOGD("TraceGen: trace done, adding to queue");
880#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800881 if (dvmCompilerWorkEnqueue(
buzbee9f601a92011-02-11 17:48:20 -0800882 self->currTraceHead,kWorkOrderTrace,desc)) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800883 /* Work order successfully enqueued */
884 if (gDvmJit.blockingMode) {
885 dvmCompilerDrainQueue();
886 }
Ben Cheng1357e942010-02-10 17:21:39 -0800887 } else {
888 /*
889 * Make sure the descriptor for the abandoned work order is
890 * freed.
891 */
892 free(desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700893 }
buzbee9f601a92011-02-11 17:48:20 -0800894 self->jitState = kJitDone;
Ben Chenga4973592010-03-31 11:59:18 -0700895 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700896 }
897 break;
898 case kJitSingleStep:
buzbee9f601a92011-02-11 17:48:20 -0800899 self->jitState = kJitSingleStepEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700900 break;
901 case kJitSingleStepEnd:
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700902 /*
903 * Clear the inJitCodeCache flag and abandon the resume attempt if
904 * we cannot switch back to the translation due to corner-case
905 * conditions. If the flag is not cleared and the code cache is full
906 * we will be stuck in the debug interpreter as the code cache
907 * cannot be reset.
908 */
909 if (dvmJitStayInPortableInterpreter()) {
buzbee9f601a92011-02-11 17:48:20 -0800910 self->entryPoint = kInterpEntryInstr;
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700911 self->inJitCodeCache = 0;
912 } else {
buzbee9f601a92011-02-11 17:48:20 -0800913 self->entryPoint = kInterpEntryResume;
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700914 }
buzbee9f601a92011-02-11 17:48:20 -0800915 self->jitState = kJitDone;
Ben Chenga4973592010-03-31 11:59:18 -0700916 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700917 break;
Ben Chenga4973592010-03-31 11:59:18 -0700918 case kJitDone:
919 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700920 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700921#if defined(WITH_SELF_VERIFICATION)
922 case kJitSelfVerification:
buzbee9f601a92011-02-11 17:48:20 -0800923 if (selfVerificationDebugInterp(pc, self)) {
Ben Chengd5adae12010-03-26 17:45:28 -0700924 /*
925 * If the next state is not single-step end, we can switch
926 * interpreter now.
927 */
buzbee9f601a92011-02-11 17:48:20 -0800928 if (self->jitState != kJitSingleStepEnd) {
929 self->jitState = kJitDone;
Ben Chenga4973592010-03-31 11:59:18 -0700930 switchInterp = true;
Ben Chengd5adae12010-03-26 17:45:28 -0700931 }
Jeff Hao97319a82009-08-12 16:57:15 -0700932 }
933 break;
934#endif
Ben Chenga4973592010-03-31 11:59:18 -0700935 case kJitNot:
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700936 switchInterp = !debugOrProfile;
Ben Chenged79ff02009-10-13 13:26:40 -0700937 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700938 default:
Ben Chenga4973592010-03-31 11:59:18 -0700939 LOGE("Unexpected JIT state: %d entry point: %d",
buzbee9f601a92011-02-11 17:48:20 -0800940 self->jitState, self->entryPoint);
Ben Chenga4973592010-03-31 11:59:18 -0700941 dvmAbort();
Ben Cheng9c147b82009-10-07 16:41:46 -0700942 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700943 }
Ben Chenga4973592010-03-31 11:59:18 -0700944 /*
945 * Final check to see if we can really switch the interpreter. Make sure
946 * the jitState is kJitDone or kJitNot when switchInterp is set to true.
947 */
buzbee9f601a92011-02-11 17:48:20 -0800948 assert(switchInterp == false || self->jitState == kJitDone ||
949 self->jitState == kJitNot);
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700950 return switchInterp && !debugOrProfile && !stayOneMoreInst &&
951 !dvmJitStayInPortableInterpreter();
Ben Chengba4fc8b2009-06-01 13:00:29 -0700952}
953
Bill Buzbee1b3da592011-02-03 07:38:22 -0800954JitEntry *dvmJitFindEntry(const u2* pc, bool isMethodEntry)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955{
956 int idx = dvmJitHash(pc);
957
958 /* Expect a high hit rate on 1st shot */
Bill Buzbee1b3da592011-02-03 07:38:22 -0800959 if ((gDvmJit.pJitEntryTable[idx].dPC == pc) &&
960 (gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry == isMethodEntry))
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 return &gDvmJit.pJitEntryTable[idx];
962 else {
Bill Buzbee27176222009-06-09 09:20:16 -0700963 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700964 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
965 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Bill Buzbee1b3da592011-02-03 07:38:22 -0800966 if ((gDvmJit.pJitEntryTable[idx].dPC == pc) &&
967 (gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry ==
968 isMethodEntry))
Ben Chengba4fc8b2009-06-01 13:00:29 -0700969 return &gDvmJit.pJitEntryTable[idx];
970 }
971 }
972 return NULL;
973}
974
Bill Buzbee27176222009-06-09 09:20:16 -0700975/*
Ben Chengcfdeca32011-01-14 11:36:46 -0800976 * Walk through the JIT profile table and find the corresponding JIT code, in
977 * the specified format (ie trace vs method). This routine needs to be fast.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700978 */
Ben Chengcfdeca32011-01-14 11:36:46 -0800979void* getCodeAddrCommon(const u2* dPC, bool methodEntry)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700980{
981 int idx = dvmJitHash(dPC);
Ben Chengcfdeca32011-01-14 11:36:46 -0800982 const u2* pc = gDvmJit.pJitEntryTable[idx].dPC;
983 if (pc != NULL) {
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700984 bool hideTranslation = dvmJitHideTranslation();
Ben Chengcfdeca32011-01-14 11:36:46 -0800985 if (pc == dPC &&
986 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry == methodEntry) {
buzbee2e152ba2010-12-15 16:32:35 -0800987 int offset = (gDvmJit.profileMode >= kTraceProfilingContinuous) ?
988 0 : gDvmJit.pJitEntryTable[idx].u.info.profileOffset;
989 intptr_t codeAddress =
990 (intptr_t)gDvmJit.pJitEntryTable[idx].codeAddress;
Ben Cheng978738d2010-05-13 13:45:57 -0700991#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -0800992 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993#endif
buzbee99ddb1e2011-01-28 10:44:30 -0800994 return hideTranslation || !codeAddress ? NULL :
995 (void *)(codeAddress + offset);
Bill Buzbee9797a232010-01-12 12:20:13 -0800996 } else {
997 int chainEndMarker = gDvmJit.jitTableSize;
998 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
999 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengcfdeca32011-01-14 11:36:46 -08001000 if (gDvmJit.pJitEntryTable[idx].dPC == dPC &&
1001 gDvmJit.pJitEntryTable[idx].u.info.isMethodEntry ==
1002 methodEntry) {
buzbee2e152ba2010-12-15 16:32:35 -08001003 int offset = (gDvmJit.profileMode >=
1004 kTraceProfilingContinuous) ? 0 :
1005 gDvmJit.pJitEntryTable[idx].u.info.profileOffset;
1006 intptr_t codeAddress =
1007 (intptr_t)gDvmJit.pJitEntryTable[idx].codeAddress;
Ben Cheng978738d2010-05-13 13:45:57 -07001008#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -08001009 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010#endif
buzbee99ddb1e2011-01-28 10:44:30 -08001011 return hideTranslation || !codeAddress ? NULL :
buzbee2e152ba2010-12-15 16:32:35 -08001012 (void *)(codeAddress + offset);
Bill Buzbee9797a232010-01-12 12:20:13 -08001013 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001014 }
1015 }
1016 }
Ben Cheng978738d2010-05-13 13:45:57 -07001017#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001018 gDvmJit.addrLookupsNotFound++;
1019#endif
1020 return NULL;
1021}
1022
1023/*
Ben Chengcfdeca32011-01-14 11:36:46 -08001024 * If a translated code address, in trace format, exists for the davik byte code
1025 * pointer return it.
1026 */
1027void* dvmJitGetTraceAddr(const u2* dPC)
1028{
1029 return getCodeAddrCommon(dPC, false /* method entry */);
1030}
1031
1032/*
1033 * If a translated code address, in whole-method format, exists for the davik
1034 * byte code pointer return it.
1035 */
1036void* dvmJitGetMethodAddr(const u2* dPC)
1037{
1038 return getCodeAddrCommon(dPC, true /* method entry */);
1039}
1040
1041/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 * Register the translated code pointer into the JitTable.
Bill Buzbee9a8c75a2009-11-08 14:31:20 -08001043 * NOTE: Once a codeAddress field transitions from initial state to
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -07001045 * threads. This routine should only be called by the compiler
buzbee2e152ba2010-12-15 16:32:35 -08001046 * thread. We defer the setting of the profile prefix size until
1047 * after the new code address is set to ensure that the prefix offset
1048 * is never applied to the initial interpret-only translation. All
1049 * translations with non-zero profile prefixes will still be correct
1050 * if entered as if the profile offset is 0, but the interpret-only
1051 * template cannot handle a non-zero prefix.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001052 */
buzbee2e152ba2010-12-15 16:32:35 -08001053void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set,
Ben Chengcfdeca32011-01-14 11:36:46 -08001054 bool isMethodEntry, int profilePrefixSize)
buzbee2e152ba2010-12-15 16:32:35 -08001055{
Bill Buzbee716f1202009-07-23 13:22:09 -07001056 JitEntryInfoUnion oldValue;
1057 JitEntryInfoUnion newValue;
Bill Buzbee1b3da592011-02-03 07:38:22 -08001058 JitEntry *jitEntry = dvmJitFindEntry(dPC, isMethodEntry);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001059 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -07001060 /* Note: order of update is important */
1061 do {
1062 oldValue = jitEntry->u;
1063 newValue = oldValue;
Ben Chengcfdeca32011-01-14 11:36:46 -08001064 newValue.info.isMethodEntry = isMethodEntry;
Bill Buzbee716f1202009-07-23 13:22:09 -07001065 newValue.info.instructionSet = set;
buzbee99ddb1e2011-01-28 10:44:30 -08001066 newValue.info.profileOffset = profilePrefixSize;
Andy McFadden6e10b9a2010-06-14 15:24:39 -07001067 } while (android_atomic_release_cas(
1068 oldValue.infoWord, newValue.infoWord,
1069 &jitEntry->u.infoWord) != 0);
Bill Buzbee716f1202009-07-23 13:22:09 -07001070 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001071}
1072
1073/*
1074 * Determine if valid trace-bulding request is active. Return true
1075 * if we need to abort and switch back to the fast interpreter, false
Ben Chenga4973592010-03-31 11:59:18 -07001076 * otherwise.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001077 */
buzbee9f601a92011-02-11 17:48:20 -08001078bool dvmJitCheckTraceRequest(Thread* self)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079{
Ben Chenga4973592010-03-31 11:59:18 -07001080 bool switchInterp = false; /* Assume success */
Bill Buzbee48f18242009-06-19 16:02:27 -07001081 int i;
buzbee852aacd2010-06-08 16:24:46 -07001082 /*
1083 * A note on trace "hotness" filtering:
1084 *
1085 * Our first level trigger is intentionally loose - we need it to
1086 * fire easily not just to identify potential traces to compile, but
1087 * also to allow re-entry into the code cache.
1088 *
1089 * The 2nd level filter (done here) exists to be selective about
1090 * what we actually compile. It works by requiring the same
1091 * trace head "key" (defined as filterKey below) to appear twice in
1092 * a relatively short period of time. The difficulty is defining the
1093 * shape of the filterKey. Unfortunately, there is no "one size fits
1094 * all" approach.
1095 *
1096 * For spiky execution profiles dominated by a smallish
1097 * number of very hot loops, we would want the second-level filter
1098 * to be very selective. A good selective filter is requiring an
1099 * exact match of the Dalvik PC. In other words, defining filterKey as:
buzbee9f601a92011-02-11 17:48:20 -08001100 * intptr_t filterKey = (intptr_t)self->interpSave.pc
buzbee852aacd2010-06-08 16:24:46 -07001101 *
1102 * However, for flat execution profiles we do best when aggressively
1103 * translating. A heuristically decent proxy for this is to use
1104 * the value of the method pointer containing the trace as the filterKey.
1105 * Intuitively, this is saying that once any trace in a method appears hot,
1106 * immediately translate any other trace from that same method that
1107 * survives the first-level filter. Here, filterKey would be defined as:
buzbee9f601a92011-02-11 17:48:20 -08001108 * intptr_t filterKey = (intptr_t)self->interpSave.method
buzbee852aacd2010-06-08 16:24:46 -07001109 *
1110 * The problem is that we can't easily detect whether we're dealing
1111 * with a spiky or flat profile. If we go with the "pc" match approach,
1112 * flat profiles perform poorly. If we go with the loose "method" match,
1113 * we end up generating a lot of useless translations. Probably the
1114 * best approach in the future will be to retain profile information
1115 * across runs of each application in order to determine it's profile,
1116 * and then choose once we have enough history.
1117 *
1118 * However, for now we've decided to chose a compromise filter scheme that
1119 * includes elements of both. The high order bits of the filter key
1120 * are drawn from the enclosing method, and are combined with a slice
1121 * of the low-order bits of the Dalvik pc of the trace head. The
1122 * looseness of the filter can be adjusted by changing with width of
1123 * the Dalvik pc slice (JIT_TRACE_THRESH_FILTER_PC_BITS). The wider
1124 * the slice, the tighter the filter.
1125 *
1126 * Note: the fixed shifts in the function below reflect assumed word
1127 * alignment for method pointers, and half-word alignment of the Dalvik pc.
1128 * for method pointers and half-word alignment for dalvik pc.
1129 */
buzbee9f601a92011-02-11 17:48:20 -08001130 u4 methodKey = (u4)self->interpSave.method <<
buzbeec35294d2010-06-09 14:22:50 -07001131 (JIT_TRACE_THRESH_FILTER_PC_BITS - 2);
buzbee9f601a92011-02-11 17:48:20 -08001132 u4 pcKey = ((u4)self->interpSave.pc >> 1) &
buzbeec35294d2010-06-09 14:22:50 -07001133 ((1 << JIT_TRACE_THRESH_FILTER_PC_BITS) - 1);
1134 intptr_t filterKey = (intptr_t)(methodKey | pcKey);
Ben Chenga4973592010-03-31 11:59:18 -07001135 bool debugOrProfile = dvmDebuggerOrProfilerActive();
Ben Cheng40094c12010-02-24 20:58:44 -08001136
Ben Chenga4973592010-03-31 11:59:18 -07001137 /* Check if the JIT request can be handled now */
1138 if (gDvmJit.pJitEntryTable != NULL && debugOrProfile == false) {
1139 /* Bypass the filter for hot trace requests or during stress mode */
buzbee9f601a92011-02-11 17:48:20 -08001140 if (self->jitState == kJitTSelectRequest &&
Ben Chenga4973592010-03-31 11:59:18 -07001141 gDvmJit.threshold > 6) {
Ben Cheng40094c12010-02-24 20:58:44 -08001142 /* Two-level filtering scheme */
1143 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
buzbee9f601a92011-02-11 17:48:20 -08001144 if (filterKey == self->threshFilter[i]) {
1145 self->threshFilter[i] = 0; // Reset filter entry
Ben Cheng40094c12010-02-24 20:58:44 -08001146 break;
1147 }
Bill Buzbee48f18242009-06-19 16:02:27 -07001148 }
Ben Cheng40094c12010-02-24 20:58:44 -08001149 if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
1150 /*
1151 * Use random replacement policy - otherwise we could miss a
1152 * large loop that contains more traces than the size of our
1153 * filter array.
1154 */
1155 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
buzbee9f601a92011-02-11 17:48:20 -08001156 self->threshFilter[i] = filterKey;
1157 self->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001158 }
Ben Chenga4973592010-03-31 11:59:18 -07001159 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001160
Ben Chenga4973592010-03-31 11:59:18 -07001161 /* If the compiler is backlogged, cancel any JIT actions */
1162 if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) {
buzbee9f601a92011-02-11 17:48:20 -08001163 self->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001164 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001165
Ben Chengba4fc8b2009-06-01 13:00:29 -07001166 /*
Ben Chenga4973592010-03-31 11:59:18 -07001167 * Check for additional reasons that might force the trace select
1168 * request to be dropped
Ben Chengba4fc8b2009-06-01 13:00:29 -07001169 */
buzbee9f601a92011-02-11 17:48:20 -08001170 if (self->jitState == kJitTSelectRequest ||
1171 self->jitState == kJitTSelectRequestHot) {
1172 if (dvmJitFindEntry(self->interpSave.pc, false)) {
Bill Buzbee1b3da592011-02-03 07:38:22 -08001173 /* In progress - nothing do do */
buzbee9f601a92011-02-11 17:48:20 -08001174 self->jitState = kJitDone;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001175 } else {
buzbee9f601a92011-02-11 17:48:20 -08001176 JitEntry *slot = lookupAndAdd(self->interpSave.pc,
Bill Buzbee1b3da592011-02-03 07:38:22 -08001177 false /* lock */,
1178 false /* method entry */);
1179 if (slot == NULL) {
1180 /*
1181 * Table is full. This should have been
1182 * detected by the compiler thread and the table
1183 * resized before we run into it here. Assume bad things
1184 * are afoot and disable profiling.
1185 */
buzbee9f601a92011-02-11 17:48:20 -08001186 self->jitState = kJitDone;
Bill Buzbee1b3da592011-02-03 07:38:22 -08001187 LOGD("JIT: JitTable full, disabling profiling");
1188 dvmJitStopTranslationRequests();
1189 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001190 }
1191 }
Ben Chenga4973592010-03-31 11:59:18 -07001192
buzbee9f601a92011-02-11 17:48:20 -08001193 switch (self->jitState) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001194 case kJitTSelectRequest:
Ben Cheng40094c12010-02-24 20:58:44 -08001195 case kJitTSelectRequestHot:
buzbee9f601a92011-02-11 17:48:20 -08001196 self->jitState = kJitTSelect;
1197 self->currTraceHead = self->interpSave.pc;
1198 self->currTraceRun = 0;
1199 self->totalTraceLen = 0;
1200 self->currRunHead = self->interpSave.pc;
1201 self->currRunLen = 0;
1202 self->trace[0].frag.startOffset =
1203 self->interpSave.pc - self->interpSave.method->insns;
1204 self->trace[0].frag.numInsts = 0;
1205 self->trace[0].frag.runEnd = false;
1206 self->trace[0].frag.hint = kJitHintNone;
1207 self->trace[0].frag.isCode = true;
1208 self->lastPC = 0;
Ben Chenga4973592010-03-31 11:59:18 -07001209 break;
1210 /*
1211 * For JIT's perspective there is no need to stay in the debug
1212 * interpreter unless debugger/profiler is attached.
1213 */
1214 case kJitDone:
1215 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001216 break;
1217 default:
Ben Chenga4973592010-03-31 11:59:18 -07001218 LOGE("Unexpected JIT state: %d entry point: %d",
buzbee9f601a92011-02-11 17:48:20 -08001219 self->jitState, self->entryPoint);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001220 dvmAbort();
1221 }
Ben Chenga4973592010-03-31 11:59:18 -07001222 } else {
1223 /*
1224 * Cannot build trace this time - ready to leave the dbg interpreter
1225 */
buzbee9f601a92011-02-11 17:48:20 -08001226 self->jitState = kJitDone;
Ben Chenga4973592010-03-31 11:59:18 -07001227 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001228 }
Ben Chenga4973592010-03-31 11:59:18 -07001229
1230 /*
1231 * Final check to see if we can really switch the interpreter. Make sure
1232 * the jitState is kJitDone when switchInterp is set to true.
1233 */
buzbee9f601a92011-02-11 17:48:20 -08001234 assert(switchInterp == false || self->jitState == kJitDone);
Ben Cheng1a7b9d72010-09-20 22:20:31 -07001235 return switchInterp && !debugOrProfile &&
1236 !dvmJitStayInPortableInterpreter();
Ben Chengba4fc8b2009-06-01 13:00:29 -07001237}
1238
Bill Buzbee27176222009-06-09 09:20:16 -07001239/*
1240 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
Bill Buzbee964a7b02010-01-28 12:54:19 -08001241 * Stops all threads, and thus is a heavyweight operation. May only be called
1242 * by the compiler thread.
Bill Buzbee27176222009-06-09 09:20:16 -07001243 */
1244bool dvmJitResizeJitTable( unsigned int size )
1245{
Bill Buzbee716f1202009-07-23 13:22:09 -07001246 JitEntry *pNewTable;
1247 JitEntry *pOldTable;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001248 JitEntry tempEntry;
Bill Buzbee27176222009-06-09 09:20:16 -07001249 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -07001250 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001251 unsigned int i;
1252
Ben Cheng3f02aa42009-08-14 13:52:09 -07001253 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -07001254 assert(size && !(size & (size - 1))); /* Is power of 2? */
1255
Ben Chenga4973592010-03-31 11:59:18 -07001256 LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
Bill Buzbee27176222009-06-09 09:20:16 -07001257
1258 newMask = size - 1;
1259
1260 if (size <= gDvmJit.jitTableSize) {
1261 return true;
1262 }
1263
Bill Buzbee964a7b02010-01-28 12:54:19 -08001264 /* Make sure requested size is compatible with chain field width */
1265 tempEntry.u.info.chain = size;
1266 if (tempEntry.u.info.chain != size) {
1267 LOGD("Jit: JitTable request of %d too big", size);
1268 return true;
1269 }
1270
Bill Buzbee716f1202009-07-23 13:22:09 -07001271 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -07001272 if (pNewTable == NULL) {
1273 return true;
1274 }
1275 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -07001276 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -07001277 }
1278
1279 /* Stop all other interpreting/jit'ng threads */
Ben Chenga8e64a72009-10-20 13:01:36 -07001280 dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001281
Bill Buzbee716f1202009-07-23 13:22:09 -07001282 pOldTable = gDvmJit.pJitEntryTable;
1283 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001284
1285 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -07001286 gDvmJit.pJitEntryTable = pNewTable;
1287 gDvmJit.jitTableSize = size;
1288 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -07001289 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -07001290
Bill Buzbee716f1202009-07-23 13:22:09 -07001291 for (i=0; i < oldSize; i++) {
1292 if (pOldTable[i].dPC) {
1293 JitEntry *p;
1294 u2 chain;
Ben Chengcfdeca32011-01-14 11:36:46 -08001295 p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/,
1296 pOldTable[i].u.info.isMethodEntry);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001297 p->codeAddress = pOldTable[i].codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -07001298 /* We need to preserve the new chain field, but copy the rest */
Bill Buzbee716f1202009-07-23 13:22:09 -07001299 chain = p->u.info.chain;
1300 p->u = pOldTable[i].u;
1301 p->u.info.chain = chain;
Bill Buzbee716f1202009-07-23 13:22:09 -07001302 }
1303 }
buzbee2e152ba2010-12-15 16:32:35 -08001304
Bill Buzbee964a7b02010-01-28 12:54:19 -08001305 dvmUnlockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -07001306
1307 free(pOldTable);
1308
Bill Buzbee27176222009-06-09 09:20:16 -07001309 /* Restart the world */
Ben Chenga8e64a72009-10-20 13:01:36 -07001310 dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001311
1312 return false;
1313}
1314
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001315/*
Ben Cheng60c24f42010-01-04 12:29:56 -08001316 * Reset the JitTable to the initial clean state.
1317 */
1318void dvmJitResetTable(void)
1319{
1320 JitEntry *jitEntry = gDvmJit.pJitEntryTable;
1321 unsigned int size = gDvmJit.jitTableSize;
1322 unsigned int i;
1323
1324 dvmLockMutex(&gDvmJit.tableLock);
buzbee2e152ba2010-12-15 16:32:35 -08001325
1326 /* Note: If need to preserve any existing counts. Do so here. */
buzbee38c41342011-01-11 15:45:49 -08001327 if (gDvmJit.pJitTraceProfCounters) {
1328 for (i=0; i < JIT_PROF_BLOCK_BUCKETS; i++) {
1329 if (gDvmJit.pJitTraceProfCounters->buckets[i])
1330 memset((void *) gDvmJit.pJitTraceProfCounters->buckets[i],
1331 0, sizeof(JitTraceCounter_t) * JIT_PROF_BLOCK_ENTRIES);
1332 }
1333 gDvmJit.pJitTraceProfCounters->next = 0;
buzbee2e152ba2010-12-15 16:32:35 -08001334 }
buzbee2e152ba2010-12-15 16:32:35 -08001335
Ben Cheng60c24f42010-01-04 12:29:56 -08001336 memset((void *) jitEntry, 0, sizeof(JitEntry) * size);
1337 for (i=0; i< size; i++) {
1338 jitEntry[i].u.info.chain = size; /* Initialize chain termination */
1339 }
1340 gDvmJit.jitTableEntriesUsed = 0;
1341 dvmUnlockMutex(&gDvmJit.tableLock);
1342}
1343
1344/*
buzbee2e152ba2010-12-15 16:32:35 -08001345 * Return the address of the next trace profile counter. This address
1346 * will be embedded in the generated code for the trace, and thus cannot
1347 * change while the trace exists.
1348 */
1349JitTraceCounter_t *dvmJitNextTraceCounter()
1350{
1351 int idx = gDvmJit.pJitTraceProfCounters->next / JIT_PROF_BLOCK_ENTRIES;
1352 int elem = gDvmJit.pJitTraceProfCounters->next % JIT_PROF_BLOCK_ENTRIES;
1353 JitTraceCounter_t *res;
1354 /* Lazily allocate blocks of counters */
1355 if (!gDvmJit.pJitTraceProfCounters->buckets[idx]) {
1356 JitTraceCounter_t *p =
1357 (JitTraceCounter_t*) calloc(JIT_PROF_BLOCK_ENTRIES, sizeof(*p));
1358 if (!p) {
1359 LOGE("Failed to allocate block of trace profile counters");
1360 dvmAbort();
1361 }
1362 gDvmJit.pJitTraceProfCounters->buckets[idx] = p;
1363 }
1364 res = &gDvmJit.pJitTraceProfCounters->buckets[idx][elem];
1365 gDvmJit.pJitTraceProfCounters->next++;
1366 return res;
1367}
1368
1369/*
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001370 * Float/double conversion requires clamping to min and max of integer form. If
1371 * target doesn't support this normally, use these.
1372 */
1373s8 dvmJitd2l(double d)
1374{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001375 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1376 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001377 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001378 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001379 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001380 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001381 else if (d != d) // NaN case
1382 return 0;
1383 else
1384 return (s8)d;
1385}
1386
1387s8 dvmJitf2l(float f)
1388{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001389 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1390 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001391 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001392 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001393 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001394 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001395 else if (f != f) // NaN case
1396 return 0;
1397 else
1398 return (s8)f;
1399}
1400
buzbee2e152ba2010-12-15 16:32:35 -08001401/* Should only be called by the compiler thread */
1402void dvmJitChangeProfileMode(TraceProfilingModes newState)
1403{
1404 if (gDvmJit.profileMode != newState) {
1405 gDvmJit.profileMode = newState;
1406 dvmJitUnchainAll();
1407 }
1408}
1409
1410void dvmJitTraceProfilingOn()
1411{
1412 if (gDvmJit.profileMode == kTraceProfilingPeriodicOff)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001413 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1414 (void*) kTraceProfilingPeriodicOn);
buzbee2e152ba2010-12-15 16:32:35 -08001415 else if (gDvmJit.profileMode == kTraceProfilingDisabled)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001416 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1417 (void*) kTraceProfilingContinuous);
buzbee2e152ba2010-12-15 16:32:35 -08001418}
1419
1420void dvmJitTraceProfilingOff()
1421{
1422 if (gDvmJit.profileMode == kTraceProfilingPeriodicOn)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001423 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1424 (void*) kTraceProfilingPeriodicOff);
buzbee2e152ba2010-12-15 16:32:35 -08001425 else if (gDvmJit.profileMode == kTraceProfilingContinuous)
Bill Buzbee1b3da592011-02-03 07:38:22 -08001426 dvmCompilerForceWorkEnqueue(NULL, kWorkOrderProfileMode,
1427 (void*) kTraceProfilingDisabled);
buzbee2e152ba2010-12-15 16:32:35 -08001428}
1429
Ben Chengba4fc8b2009-06-01 13:00:29 -07001430#endif /* WITH_JIT */