blob: b266b24a3e5d9f44b95e0dcfea007c2d747d15c5 [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/*
59 * Save out PC, FP, InterpState, and registers to shadow space.
60 * Return a pointer to the shadow space for JIT to use.
61 */
62void* dvmSelfVerificationSaveState(const u2* pc, const void* fp,
Bill Buzbee9a8c75a2009-11-08 14:31:20 -080063 InterpState* interpState, int targetTrace)
Jeff Hao97319a82009-08-12 16:57:15 -070064{
65 Thread *self = dvmThreadSelf();
66 ShadowSpace *shadowSpace = self->shadowSpace;
Ben Cheng11d8f142010-03-24 15:24:19 -070067 unsigned preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea);
68 unsigned postBytes = interpState->method->registersSize*4;
Jeff Hao97319a82009-08-12 16:57:15 -070069
70 //LOGD("### selfVerificationSaveState(%d) pc: 0x%x fp: 0x%x",
71 // self->threadId, (int)pc, (int)fp);
72
73 if (shadowSpace->selfVerificationState != kSVSIdle) {
74 LOGD("~~~ Save: INCORRECT PREVIOUS STATE(%d): %d",
75 self->threadId, shadowSpace->selfVerificationState);
76 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -070077 LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp);
Jeff Hao97319a82009-08-12 16:57:15 -070078 }
79 shadowSpace->selfVerificationState = kSVSStart;
80
Ben Chengd5adae12010-03-26 17:45:28 -070081 if (interpState->entryPoint == kInterpEntryResume) {
82 interpState->entryPoint = kInterpEntryInstr;
83#if 0
84 /* Tracking the success rate of resume after single-stepping */
85 if (interpState->jitResumeDPC == pc) {
86 LOGD("SV single step resumed at %p", pc);
87 }
88 else {
89 LOGD("real %p DPC %p NPC %p", pc, interpState->jitResumeDPC,
90 interpState->jitResumeNPC);
91 }
92#endif
93 }
94
Jeff Hao97319a82009-08-12 16:57:15 -070095 // Dynamically grow shadow register space if necessary
Ben Cheng11d8f142010-03-24 15:24:19 -070096 if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) {
Jeff Hao97319a82009-08-12 16:57:15 -070097 free(shadowSpace->registerSpace);
Ben Cheng11d8f142010-03-24 15:24:19 -070098 shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4);
Jeff Hao97319a82009-08-12 16:57:15 -070099 shadowSpace->registerSpace =
Ben Cheng11d8f142010-03-24 15:24:19 -0700100 (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4));
Jeff Hao97319a82009-08-12 16:57:15 -0700101 }
102
103 // Remember original state
104 shadowSpace->startPC = pc;
105 shadowSpace->fp = fp;
Ben Chengccd6c012009-10-15 14:52:45 -0700106 shadowSpace->glue = interpState;
107 /*
108 * Store the original method here in case the trace ends with a
109 * return/invoke, the last method.
110 */
111 shadowSpace->method = interpState->method;
Jeff Hao97319a82009-08-12 16:57:15 -0700112 shadowSpace->shadowFP = shadowSpace->registerSpace +
113 shadowSpace->registerSpaceSize - postBytes/4;
114
115 // Create a copy of the InterpState
Ben Chengccd6c012009-10-15 14:52:45 -0700116 memcpy(&(shadowSpace->interpState), interpState, sizeof(InterpState));
Jeff Hao97319a82009-08-12 16:57:15 -0700117 shadowSpace->interpState.fp = shadowSpace->shadowFP;
118 shadowSpace->interpState.interpStackEnd = (u1*)shadowSpace->registerSpace;
119
120 // Create a copy of the stack
121 memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes,
122 preBytes+postBytes);
123
124 // Setup the shadowed heap space
125 shadowSpace->heapSpaceTail = shadowSpace->heapSpace;
126
127 // Reset trace length
128 shadowSpace->traceLength = 0;
129
130 return shadowSpace;
131}
132
133/*
134 * Save ending PC, FP and compiled code exit point to shadow space.
135 * Return a pointer to the shadow space for JIT to restore state.
136 */
137void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700138 SelfVerificationState exitState)
Jeff Hao97319a82009-08-12 16:57:15 -0700139{
140 Thread *self = dvmThreadSelf();
141 ShadowSpace *shadowSpace = self->shadowSpace;
Ben Chengd5adae12010-03-26 17:45:28 -0700142 // Official InterpState structure
143 InterpState *realGlue = shadowSpace->glue;
Jeff Hao97319a82009-08-12 16:57:15 -0700144 shadowSpace->endPC = pc;
145 shadowSpace->endShadowFP = fp;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700146 shadowSpace->jitExitState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700147
148 //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x",
149 // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp,
150 // (int)pc);
151
152 if (shadowSpace->selfVerificationState != kSVSStart) {
153 LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d",
154 self->threadId, shadowSpace->selfVerificationState);
155 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700156 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700157 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700158 LOGD("Interp FP: 0x%x", (int)shadowSpace->fp);
159 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700160 (int)shadowSpace->endShadowFP);
161 }
162
Ben Chengd5adae12010-03-26 17:45:28 -0700163 // Move the resume [ND]PC from the shadow space to the real space so that
164 // the debug interpreter can return to the translation
Ben Cheng7a2697d2010-06-07 13:44:23 -0700165 if (exitState == kSVSSingleStep) {
Ben Chengd5adae12010-03-26 17:45:28 -0700166 realGlue->jitResumeNPC = shadowSpace->interpState.jitResumeNPC;
167 realGlue->jitResumeDPC = shadowSpace->interpState.jitResumeDPC;
168 } else {
169 realGlue->jitResumeNPC = NULL;
170 realGlue->jitResumeDPC = NULL;
171 }
172
Jeff Hao97319a82009-08-12 16:57:15 -0700173 // Special case when punting after a single instruction
Ben Cheng7a2697d2010-06-07 13:44:23 -0700174 if (exitState == kSVSPunt && pc == shadowSpace->startPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700175 shadowSpace->selfVerificationState = kSVSIdle;
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700176 } else if (exitState == kSVSBackwardBranch && pc < shadowSpace->startPC) {
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700177 /*
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700178 * Consider a trace with a backward branch:
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700179 * 1: ..
180 * 2: ..
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700181 * 3: ..
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700182 * 4: ..
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700183 * 5: Goto {1 or 2 or 3 or 4}
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700184 *
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700185 * If there instruction 5 goes to 1 and there is no single-step
186 * instruction in the loop, pc is equal to shadowSpace->startPC and
187 * we will honor the backward branch condition.
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700188 *
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700189 * If the single-step instruction is outside the loop, then after
190 * resuming in the trace the startPC will be less than pc so we will
191 * also honor the backward branch condition.
192 *
193 * If the single-step is inside the loop, we won't hit the same endPC
194 * twice when the interpreter is re-executing the trace so we want to
195 * cancel the backward branch condition. In this case it can be
196 * detected as the endPC (ie pc) will be less than startPC.
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700197 */
198 shadowSpace->selfVerificationState = kSVSNormal;
Jeff Hao97319a82009-08-12 16:57:15 -0700199 } else {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700200 shadowSpace->selfVerificationState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700201 }
202
203 return shadowSpace;
204}
205
206/* Print contents of virtual registers */
Ben Chengccd6c012009-10-15 14:52:45 -0700207static void selfVerificationPrintRegisters(int* addr, int* addrRef,
208 int numWords)
Jeff Hao97319a82009-08-12 16:57:15 -0700209{
210 int i;
211 for (i = 0; i < numWords; i++) {
Ben Chengccd6c012009-10-15 14:52:45 -0700212 LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : "");
Jeff Hao97319a82009-08-12 16:57:15 -0700213 }
214}
215
216/* Print values maintained in shadowSpace */
217static void selfVerificationDumpState(const u2* pc, Thread* self)
218{
219 ShadowSpace* shadowSpace = self->shadowSpace;
220 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
221 int frameBytes = (int) shadowSpace->registerSpace +
222 shadowSpace->registerSpaceSize*4 -
223 (int) shadowSpace->shadowFP;
224 int localRegs = 0;
225 int frameBytes2 = 0;
226 if (self->curFrame < shadowSpace->fp) {
227 localRegs = (stackSave->method->registersSize -
228 stackSave->method->insSize)*4;
229 frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs;
230 }
231 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700232 LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
Jeff Hao97319a82009-08-12 16:57:15 -0700233 (int)(pc - stackSave->method->insns));
Ben Chengccd6c012009-10-15 14:52:45 -0700234 LOGD("Class: %s", shadowSpace->method->clazz->descriptor);
235 LOGD("Method: %s", shadowSpace->method->name);
236 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700237 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700238 LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
Jeff Hao97319a82009-08-12 16:57:15 -0700239 (int)self->curFrame);
Ben Chengccd6c012009-10-15 14:52:45 -0700240 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700241 (int)shadowSpace->endShadowFP);
Ben Chengccd6c012009-10-15 14:52:45 -0700242 LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
Jeff Hao97319a82009-08-12 16:57:15 -0700243 localRegs, frameBytes2);
Ben Chengccd6c012009-10-15 14:52:45 -0700244 LOGD("Trace length: %d State: %d", shadowSpace->traceLength,
Jeff Hao97319a82009-08-12 16:57:15 -0700245 shadowSpace->selfVerificationState);
246}
247
248/* Print decoded instructions in the current trace */
249static void selfVerificationDumpTrace(const u2* pc, Thread* self)
250{
251 ShadowSpace* shadowSpace = self->shadowSpace;
252 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700253 int i, addr, offset;
254 DecodedInstruction *decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700255
256 LOGD("********** SHADOW TRACE DUMP **********");
257 for (i = 0; i < shadowSpace->traceLength; i++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700258 addr = shadowSpace->trace[i].addr;
259 offset = (int)((u2*)addr - stackSave->method->insns);
260 decInsn = &(shadowSpace->trace[i].decInsn);
261 /* Not properly decoding instruction, some registers may be garbage */
Andy McFaddenc6b25c72010-06-22 11:01:20 -0700262 LOGD("0x%x: (0x%04x) %s",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800263 addr, offset, dexGetOpcodeName(decInsn->opcode));
Jeff Hao97319a82009-08-12 16:57:15 -0700264 }
265}
266
Ben Chengbcdc1de2009-08-21 16:18:46 -0700267/* Code is forced into this spin loop when a divergence is detected */
Ben Chengccd6c012009-10-15 14:52:45 -0700268static void selfVerificationSpinLoop(ShadowSpace *shadowSpace)
Ben Chengbcdc1de2009-08-21 16:18:46 -0700269{
Ben Chengccd6c012009-10-15 14:52:45 -0700270 const u2 *startPC = shadowSpace->startPC;
Ben Cheng88a0f972010-02-24 15:00:40 -0800271 JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL);
Ben Chengccd6c012009-10-15 14:52:45 -0700272 if (desc) {
273 dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc);
Ben Cheng1357e942010-02-10 17:21:39 -0800274 /*
275 * This function effectively terminates the VM right here, so not
276 * freeing the desc pointer when the enqueuing fails is acceptable.
277 */
Ben Chengccd6c012009-10-15 14:52:45 -0700278 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700279 gDvmJit.selfVerificationSpin = true;
280 while(gDvmJit.selfVerificationSpin) sleep(10);
281}
282
Jeff Hao97319a82009-08-12 16:57:15 -0700283/* Manage self verification while in the debug interpreter */
Ben Chengd5adae12010-03-26 17:45:28 -0700284static bool selfVerificationDebugInterp(const u2* pc, Thread* self,
285 InterpState *interpState)
Jeff Hao97319a82009-08-12 16:57:15 -0700286{
287 ShadowSpace *shadowSpace = self->shadowSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700288 SelfVerificationState state = shadowSpace->selfVerificationState;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700289
290 DecodedInstruction decInsn;
Dan Bornstein54322392010-11-17 14:16:56 -0800291 dexDecodeInstruction(pc, &decInsn);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700292
Jeff Hao97319a82009-08-12 16:57:15 -0700293 //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
294 // self->threadId, (int)pc, (int)shadowSpace->endPC, state,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800295 // shadowSpace->traceLength, dexGetOpcodeName(decInsn.opcode));
Jeff Hao97319a82009-08-12 16:57:15 -0700296
297 if (state == kSVSIdle || state == kSVSStart) {
298 LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d",
299 self->threadId, state);
300 selfVerificationDumpState(pc, self);
301 selfVerificationDumpTrace(pc, self);
302 }
303
Ben Chengd5adae12010-03-26 17:45:28 -0700304 /*
305 * Skip endPC once when trace has a backward branch. If the SV state is
306 * single step, keep it that way.
307 */
Jeff Hao97319a82009-08-12 16:57:15 -0700308 if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
Ben Chengd5adae12010-03-26 17:45:28 -0700309 (state != kSVSBackwardBranch && state != kSVSSingleStep)) {
Jeff Hao97319a82009-08-12 16:57:15 -0700310 shadowSpace->selfVerificationState = kSVSDebugInterp;
311 }
312
313 /* Check that the current pc is the end of the trace */
Ben Chengd5adae12010-03-26 17:45:28 -0700314 if ((state == kSVSDebugInterp || state == kSVSSingleStep) &&
315 pc == shadowSpace->endPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700316
317 shadowSpace->selfVerificationState = kSVSIdle;
318
319 /* Check register space */
320 int frameBytes = (int) shadowSpace->registerSpace +
321 shadowSpace->registerSpaceSize*4 -
322 (int) shadowSpace->shadowFP;
323 if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700324 LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId);
Jeff Hao97319a82009-08-12 16:57:15 -0700325 selfVerificationDumpState(pc, self);
326 selfVerificationDumpTrace(pc, self);
327 LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
328 (int)shadowSpace->fp, frameBytes);
Ben Chengccd6c012009-10-15 14:52:45 -0700329 selfVerificationPrintRegisters((int*)shadowSpace->fp,
330 (int*)shadowSpace->shadowFP,
331 frameBytes/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700332 LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
333 (int)shadowSpace->shadowFP, frameBytes);
334 selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700335 (int*)shadowSpace->fp,
336 frameBytes/4);
337 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700338 }
339 /* Check new frame if it exists (invokes only) */
340 if (self->curFrame < shadowSpace->fp) {
341 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
342 int localRegs = (stackSave->method->registersSize -
343 stackSave->method->insSize)*4;
344 int frameBytes2 = (int) shadowSpace->fp -
345 (int) self->curFrame - localRegs;
346 if (memcmp(((char*)self->curFrame)+localRegs,
347 ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700348 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!",
Jeff Hao97319a82009-08-12 16:57:15 -0700349 self->threadId);
350 selfVerificationDumpState(pc, self);
351 selfVerificationDumpTrace(pc, self);
352 LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d",
353 (int)self->curFrame, localRegs, frameBytes2);
354 selfVerificationPrintRegisters((int*)self->curFrame,
Ben Chengccd6c012009-10-15 14:52:45 -0700355 (int*)shadowSpace->endShadowFP,
356 (frameBytes2+localRegs)/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700357 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
358 (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
359 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700360 (int*)self->curFrame,
361 (frameBytes2+localRegs)/4);
362 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700363 }
364 }
365
366 /* Check memory space */
Ben Chengbcdc1de2009-08-21 16:18:46 -0700367 bool memDiff = false;
Jeff Hao97319a82009-08-12 16:57:15 -0700368 ShadowHeap* heapSpacePtr;
369 for (heapSpacePtr = shadowSpace->heapSpace;
370 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700371 int memData = *((unsigned int*) heapSpacePtr->addr);
372 if (heapSpacePtr->data != memData) {
Ben Chengccd6c012009-10-15 14:52:45 -0700373 LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId);
374 LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
Ben Chengbcdc1de2009-08-21 16:18:46 -0700375 heapSpacePtr->addr, memData, heapSpacePtr->data);
Jeff Hao97319a82009-08-12 16:57:15 -0700376 selfVerificationDumpState(pc, self);
377 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700378 memDiff = true;
Jeff Hao97319a82009-08-12 16:57:15 -0700379 }
380 }
Ben Chengccd6c012009-10-15 14:52:45 -0700381 if (memDiff) selfVerificationSpinLoop(shadowSpace);
Ben Chengd5adae12010-03-26 17:45:28 -0700382
383 /*
384 * Switch to JIT single step mode to stay in the debug interpreter for
385 * one more instruction
386 */
387 if (state == kSVSSingleStep) {
388 interpState->jitState = kJitSingleStepEnd;
389 }
Jeff Hao97319a82009-08-12 16:57:15 -0700390 return true;
391
392 /* If end not been reached, make sure max length not exceeded */
393 } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) {
394 LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId);
Ben Chengccd6c012009-10-15 14:52:45 -0700395 LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700396 (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
397 selfVerificationDumpState(pc, self);
398 selfVerificationDumpTrace(pc, self);
Ben Chengccd6c012009-10-15 14:52:45 -0700399 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700400
401 return true;
402 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700403 /* Log the instruction address and decoded instruction for debug */
Jeff Hao97319a82009-08-12 16:57:15 -0700404 shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700405 shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700406 shadowSpace->traceLength++;
407
408 return false;
409}
410#endif
411
Ben Chengba4fc8b2009-06-01 13:00:29 -0700412/*
413 * If one of our fixed tables or the translation buffer fills up,
414 * call this routine to avoid wasting cycles on future translation requests.
415 */
416void dvmJitStopTranslationRequests()
417{
418 /*
419 * Note 1: This won't necessarily stop all translation requests, and
420 * operates on a delayed mechanism. Running threads look to the copy
421 * of this value in their private InterpState structures and won't see
422 * this change until it is refreshed (which happens on interpreter
423 * entry).
424 * Note 2: This is a one-shot memory leak on this table. Because this is a
425 * permanent off switch for Jit profiling, it is a one-time leak of 1K
426 * bytes, and no further attempt will be made to re-allocate it. Can't
427 * free it because some thread may be holding a reference.
428 */
Bill Buzbeeb1d80442009-12-17 14:55:21 -0800429 gDvmJit.pProfTable = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700430}
431
Ben Cheng978738d2010-05-13 13:45:57 -0700432#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700433/* Convenience function to increment counter from assembly code */
Ben Cheng6c10a972009-10-29 14:39:18 -0700434void dvmBumpNoChain(int from)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700435{
Ben Cheng6c10a972009-10-29 14:39:18 -0700436 gDvmJit.noChainExit[from]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700437}
438
439/* Convenience function to increment counter from assembly code */
440void dvmBumpNormal()
441{
Ben Cheng6c10a972009-10-29 14:39:18 -0700442 gDvmJit.normalExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700443}
444
445/* Convenience function to increment counter from assembly code */
446void dvmBumpPunt(int from)
447{
Ben Cheng6c10a972009-10-29 14:39:18 -0700448 gDvmJit.puntExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700449}
450#endif
451
452/* Dumps debugging & tuning stats to the log */
453void dvmJitStats()
454{
455 int i;
456 int hit;
457 int not_hit;
458 int chains;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800459 int stubs;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700460 if (gDvmJit.pJitEntryTable) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800461 for (i=0, stubs=chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700462 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700463 i++) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800464 if (gDvmJit.pJitEntryTable[i].dPC != 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700465 hit++;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800466 if (gDvmJit.pJitEntryTable[i].codeAddress ==
Bill Buzbeebd047242010-05-13 13:02:53 -0700467 dvmCompilerGetInterpretTemplate())
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800468 stubs++;
469 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -0700470 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700471 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700472 chains++;
473 }
Ben Cheng72621c92010-03-10 13:12:55 -0800474 LOGD("JIT: table size is %d, entries used is %d",
Ben Cheng86717f72010-03-05 15:27:21 -0800475 gDvmJit.jitTableSize, gDvmJit.jitTableEntriesUsed);
Ben Cheng72621c92010-03-10 13:12:55 -0800476 LOGD("JIT: %d traces, %d slots, %d chains, %d thresh, %s",
477 hit, not_hit + hit, chains, gDvmJit.threshold,
478 gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
Ben Cheng86717f72010-03-05 15:27:21 -0800479
Ben Cheng978738d2010-05-13 13:45:57 -0700480#if defined(WITH_JIT_TUNING)
481 LOGD("JIT: Code cache patches: %d", gDvmJit.codeCachePatches);
482
Ben Cheng72621c92010-03-10 13:12:55 -0800483 LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
484 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
485 gDvmJit.normalExit, gDvmJit.puntExit);
Ben Cheng452efba2010-04-30 15:14:00 -0700486
Ben Cheng978738d2010-05-13 13:45:57 -0700487 LOGD("JIT: ICHits: %d", gDvmICHitCount);
488
Ben Cheng72621c92010-03-10 13:12:55 -0800489 LOGD("JIT: noChainExit: %d IC miss, %d interp callsite, "
490 "%d switch overflow",
491 gDvmJit.noChainExit[kInlineCacheMiss],
492 gDvmJit.noChainExit[kCallsiteInterpreted],
493 gDvmJit.noChainExit[kSwitchOverflow]);
Ben Cheng86717f72010-03-05 15:27:21 -0800494
Ben Chengb88ec3c2010-05-17 12:50:33 -0700495 LOGD("JIT: ICPatch: %d init, %d rejected, %d lock-free, %d queued, "
496 "%d dropped",
497 gDvmJit.icPatchInit, gDvmJit.icPatchRejected,
498 gDvmJit.icPatchLockFree, gDvmJit.icPatchQueued,
Ben Cheng452efba2010-04-30 15:14:00 -0700499 gDvmJit.icPatchDropped);
500
Ben Cheng86717f72010-03-05 15:27:21 -0800501 LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return",
502 gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic,
503 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700504 LOGD("JIT: Inline: %d mgetter, %d msetter, %d pgetter, %d psetter",
505 gDvmJit.invokeMonoGetterInlined, gDvmJit.invokeMonoSetterInlined,
506 gDvmJit.invokePolyGetterInlined, gDvmJit.invokePolySetterInlined);
Ben Cheng86717f72010-03-05 15:27:21 -0800507 LOGD("JIT: Total compilation time: %llu ms", gDvmJit.jitTime / 1000);
508 LOGD("JIT: Avg unit compilation time: %llu us",
509 gDvmJit.jitTime / gDvmJit.numCompilations);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700510#endif
Ben Cheng86717f72010-03-05 15:27:21 -0800511
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800512 LOGD("JIT: %d Translation chains, %d interp stubs",
513 gDvmJit.translationChains, stubs);
Ben Chenge80cd942009-07-17 15:54:23 -0700514 if (gDvmJit.profile) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700515 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700516 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700517 }
518}
519
Bill Buzbee716f1202009-07-23 13:22:09 -0700520
Andy McFadden953a0ed2010-09-17 15:48:38 -0700521static void setTraceConstruction(JitEntry *slot, bool value)
Bill Buzbeed7269912009-11-10 14:31:32 -0800522{
523
524 JitEntryInfoUnion oldValue;
525 JitEntryInfoUnion newValue;
526 do {
527 oldValue = slot->u;
528 newValue = oldValue;
529 newValue.info.traceConstruction = value;
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700530 } while (android_atomic_release_cas(oldValue.infoWord, newValue.infoWord,
531 &slot->u.infoWord) != 0);
Bill Buzbeed7269912009-11-10 14:31:32 -0800532}
533
Andy McFadden953a0ed2010-09-17 15:48:38 -0700534static void resetTracehead(InterpState* interpState, JitEntry *slot)
Bill Buzbeed7269912009-11-10 14:31:32 -0800535{
Bill Buzbeebd047242010-05-13 13:02:53 -0700536 slot->codeAddress = dvmCompilerGetInterpretTemplate();
Bill Buzbeed7269912009-11-10 14:31:32 -0800537 setTraceConstruction(slot, false);
538}
539
540/* Clean up any pending trace builds */
541void dvmJitAbortTraceSelect(InterpState* interpState)
542{
543 if (interpState->jitState == kJitTSelect)
Ben Chenga4973592010-03-31 11:59:18 -0700544 interpState->jitState = kJitDone;
Bill Buzbeed7269912009-11-10 14:31:32 -0800545}
546
Ben Chengba4fc8b2009-06-01 13:00:29 -0700547/*
Bill Buzbee964a7b02010-01-28 12:54:19 -0800548 * Find an entry in the JitTable, creating if necessary.
549 * Returns null if table is full.
550 */
551static JitEntry *lookupAndAdd(const u2* dPC, bool callerLocked)
552{
553 u4 chainEndMarker = gDvmJit.jitTableSize;
554 u4 idx = dvmJitHash(dPC);
555
556 /* Walk the bucket chain to find an exact match for our PC */
557 while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
558 (gDvmJit.pJitEntryTable[idx].dPC != dPC)) {
559 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
560 }
561
562 if (gDvmJit.pJitEntryTable[idx].dPC != dPC) {
563 /*
564 * No match. Aquire jitTableLock and find the last
565 * slot in the chain. Possibly continue the chain walk in case
566 * some other thread allocated the slot we were looking
567 * at previuosly (perhaps even the dPC we're trying to enter).
568 */
569 if (!callerLocked)
570 dvmLockMutex(&gDvmJit.tableLock);
571 /*
572 * At this point, if .dPC is NULL, then the slot we're
573 * looking at is the target slot from the primary hash
574 * (the simple, and common case). Otherwise we're going
575 * to have to find a free slot and chain it.
576 */
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700577 ANDROID_MEMBAR_FULL(); /* Make sure we reload [].dPC after lock */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800578 if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
579 u4 prev;
580 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
581 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
582 /* Another thread got there first for this dPC */
583 if (!callerLocked)
584 dvmUnlockMutex(&gDvmJit.tableLock);
585 return &gDvmJit.pJitEntryTable[idx];
586 }
587 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
588 }
589 /* Here, idx should be pointing to the last cell of an
590 * active chain whose last member contains a valid dPC */
591 assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
592 /* Linear walk to find a free cell and add it to the end */
593 prev = idx;
594 while (true) {
595 idx++;
596 if (idx == chainEndMarker)
597 idx = 0; /* Wraparound */
598 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
599 (idx == prev))
600 break;
601 }
602 if (idx != prev) {
603 JitEntryInfoUnion oldValue;
604 JitEntryInfoUnion newValue;
605 /*
606 * Although we hold the lock so that noone else will
607 * be trying to update a chain field, the other fields
608 * packed into the word may be in use by other threads.
609 */
610 do {
611 oldValue = gDvmJit.pJitEntryTable[prev].u;
612 newValue = oldValue;
613 newValue.info.chain = idx;
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700614 } while (android_atomic_release_cas(oldValue.infoWord,
615 newValue.infoWord,
616 &gDvmJit.pJitEntryTable[prev].u.infoWord) != 0);
Bill Buzbee964a7b02010-01-28 12:54:19 -0800617 }
618 }
619 if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
620 /*
621 * Initialize codeAddress and allocate the slot. Must
622 * happen in this order (since dPC is set, the entry is live.
623 */
624 gDvmJit.pJitEntryTable[idx].dPC = dPC;
625 gDvmJit.jitTableEntriesUsed++;
626 } else {
627 /* Table is full */
628 idx = chainEndMarker;
629 }
630 if (!callerLocked)
631 dvmUnlockMutex(&gDvmJit.tableLock);
632 }
633 return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
634}
Ben Chenga4973592010-03-31 11:59:18 -0700635
Bill Buzbee964a7b02010-01-28 12:54:19 -0800636/*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700637 * Append the class ptr of "this" and the current method ptr to the current
638 * trace. That is, the trace runs will contain the following components:
639 * + trace run that ends with an invoke (existing entry)
640 * + thisClass (new)
641 * + calleeMethod (new)
642 */
643static void insertClassMethodInfo(InterpState* interpState,
644 const ClassObject* thisClass,
645 const Method* calleeMethod,
646 const DecodedInstruction* insn)
647{
648 int currTraceRun = ++interpState->currTraceRun;
649 interpState->trace[currTraceRun].meta = (void *) thisClass;
650 currTraceRun = ++interpState->currTraceRun;
651 interpState->trace[currTraceRun].meta = (void *) calleeMethod;
652}
653
654/*
Ben Chengd44faf52010-06-02 15:33:51 -0700655 * Check if the next instruction following the invoke is a move-result and if
Ben Cheng7a2697d2010-06-07 13:44:23 -0700656 * so add it to the trace. That is, this will add the trace run that includes
657 * the move-result to the trace list.
658 *
659 * + trace run that ends with an invoke (existing entry)
660 * + thisClass (existing entry)
661 * + calleeMethod (existing entry)
662 * + move result (new)
Ben Chengd44faf52010-06-02 15:33:51 -0700663 *
664 * lastPC, len, offset are all from the preceding invoke instruction
665 */
666static void insertMoveResult(const u2 *lastPC, int len, int offset,
667 InterpState *interpState)
668{
669 DecodedInstruction nextDecInsn;
670 const u2 *moveResultPC = lastPC + len;
671
Dan Bornstein54322392010-11-17 14:16:56 -0800672 dexDecodeInstruction(moveResultPC, &nextDecInsn);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800673 if ((nextDecInsn.opcode != OP_MOVE_RESULT) &&
674 (nextDecInsn.opcode != OP_MOVE_RESULT_WIDE) &&
675 (nextDecInsn.opcode != OP_MOVE_RESULT_OBJECT))
Ben Chengd44faf52010-06-02 15:33:51 -0700676 return;
677
678 /* We need to start a new trace run */
679 int currTraceRun = ++interpState->currTraceRun;
680 interpState->currRunHead = moveResultPC;
681 interpState->trace[currTraceRun].frag.startOffset = offset + len;
682 interpState->trace[currTraceRun].frag.numInsts = 1;
683 interpState->trace[currTraceRun].frag.runEnd = false;
684 interpState->trace[currTraceRun].frag.hint = kJitHintNone;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700685 interpState->trace[currTraceRun].frag.isCode = true;
Ben Chengd44faf52010-06-02 15:33:51 -0700686 interpState->totalTraceLen++;
687
Dan Bornstein54322392010-11-17 14:16:56 -0800688 interpState->currRunLen = dexGetInstrOrTableWidth(moveResultPC);
Ben Chengd44faf52010-06-02 15:33:51 -0700689}
690
691/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 * Adds to the current trace request one instruction at a time, just
693 * before that instruction is interpreted. This is the primary trace
694 * selection function. NOTE: return instruction are handled a little
695 * differently. In general, instructions are "proposed" to be added
696 * to the current trace prior to interpretation. If the interpreter
697 * then successfully completes the instruction, is will be considered
698 * part of the request. This allows us to examine machine state prior
699 * to interpretation, and also abort the trace request if the instruction
700 * throws or does something unexpected. However, return instructions
701 * will cause an immediate end to the translation request - which will
702 * be passed to the compiler before the return completes. This is done
703 * in response to special handling of returns by the interpreter (and
704 * because returns cannot throw in a way that causes problems for the
705 * translated code.
706 */
Ben Cheng7a2697d2010-06-07 13:44:23 -0700707int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState,
708 const ClassObject* thisClass, const Method* curMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709{
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700710 int flags, len;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 int switchInterp = false;
Ben Chenga4973592010-03-31 11:59:18 -0700712 bool debugOrProfile = dvmDebuggerOrProfilerActive();
Ben Cheng7a2697d2010-06-07 13:44:23 -0700713 /* Stay in the dbg interpreter for the next instruction */
714 bool stayOneMoreInst = false;
Bill Buzbeed7269912009-11-10 14:31:32 -0800715
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700716 /*
717 * Bug 2710533 - dalvik crash when disconnecting debugger
718 *
719 * Reset the entry point to the default value. If needed it will be set to a
720 * specific value in the corresponding case statement (eg kJitSingleStepEnd)
721 */
722 interpState->entryPoint = kInterpEntryInstr;
723
Ben Cheng79d173c2009-09-29 16:12:51 -0700724 /* Prepare to handle last PC and stage the current PC */
725 const u2 *lastPC = interpState->lastPC;
726 interpState->lastPC = pc;
727
Ben Chengba4fc8b2009-06-01 13:00:29 -0700728 switch (interpState->jitState) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729 int offset;
730 DecodedInstruction decInsn;
731 case kJitTSelect:
Ben Chengdc84bb22009-10-02 12:58:52 -0700732 /* First instruction - just remember the PC and exit */
733 if (lastPC == NULL) break;
Ben Cheng79d173c2009-09-29 16:12:51 -0700734 /* Grow the trace around the last PC if jitState is kJitTSelect */
Dan Bornstein54322392010-11-17 14:16:56 -0800735 dexDecodeInstruction(lastPC, &decInsn);
Ben Cheng6c10a972009-10-29 14:39:18 -0700736
737 /*
738 * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due
739 * to the amount of space it takes to generate the chaining
740 * cells.
741 */
742 if (interpState->totalTraceLen != 0 &&
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800743 (decInsn.opcode == OP_PACKED_SWITCH ||
744 decInsn.opcode == OP_SPARSE_SWITCH)) {
Ben Cheng6c10a972009-10-29 14:39:18 -0700745 interpState->jitState = kJitTSelectEnd;
746 break;
747 }
748
Bill Buzbeef9f33282009-11-22 12:45:30 -0800749
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750#if defined(SHOW_TRACE)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800751 LOGD("TraceGen: adding %s", dexGetOpcodeName(decInsn.opcode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700752#endif
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800753 flags = dexGetInstrFlags(decInsn.opcode);
Dan Bornstein54322392010-11-17 14:16:56 -0800754 len = dexGetInstrOrTableWidth(lastPC);
Ben Cheng79d173c2009-09-29 16:12:51 -0700755 offset = lastPC - interpState->method->insns;
756 assert((unsigned) offset <
757 dvmGetMethodInsnsSize(interpState->method));
758 if (lastPC != interpState->currRunHead + interpState->currRunLen) {
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700759 int currTraceRun;
760 /* We need to start a new trace run */
761 currTraceRun = ++interpState->currTraceRun;
762 interpState->currRunLen = 0;
Ben Cheng79d173c2009-09-29 16:12:51 -0700763 interpState->currRunHead = (u2*)lastPC;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700764 interpState->trace[currTraceRun].frag.startOffset = offset;
765 interpState->trace[currTraceRun].frag.numInsts = 0;
766 interpState->trace[currTraceRun].frag.runEnd = false;
767 interpState->trace[currTraceRun].frag.hint = kJitHintNone;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700768 interpState->trace[currTraceRun].frag.isCode = true;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700769 }
770 interpState->trace[interpState->currTraceRun].frag.numInsts++;
771 interpState->totalTraceLen++;
772 interpState->currRunLen += len;
Ben Cheng79d173c2009-09-29 16:12:51 -0700773
Ben Chengd44faf52010-06-02 15:33:51 -0700774 /*
775 * If the last instruction is an invoke, we will try to sneak in
776 * the move-result* (if existent) into a separate trace run.
777 */
778 int needReservedRun = (flags & kInstrInvoke) ? 1 : 0;
779
Ben Cheng79d173c2009-09-29 16:12:51 -0700780 /* Will probably never hit this with the current trace buildier */
Ben Chengd44faf52010-06-02 15:33:51 -0700781 if (interpState->currTraceRun ==
782 (MAX_JIT_RUN_LEN - 1 - needReservedRun)) {
Ben Cheng79d173c2009-09-29 16:12:51 -0700783 interpState->jitState = kJitTSelectEnd;
784 }
785
Dan Bornsteinc2b486f2010-11-12 16:07:16 -0800786 if (!dexIsGoto(flags) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700787 ((flags & (kInstrCanBranch |
788 kInstrCanSwitch |
789 kInstrCanReturn |
790 kInstrInvoke)) != 0)) {
791 interpState->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792#if defined(SHOW_TRACE)
Ben Chengd44faf52010-06-02 15:33:51 -0700793 LOGD("TraceGen: ending on %s, basic block end",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800794 dexGetOpcodeName(decInsn.opcode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700795#endif
Ben Chengd44faf52010-06-02 15:33:51 -0700796
797 /*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700798 * If the current invoke is a {virtual,interface}, get the
799 * current class/method pair into the trace as well.
Ben Chengd44faf52010-06-02 15:33:51 -0700800 * If the next instruction is a variant of move-result, insert
Ben Cheng7a2697d2010-06-07 13:44:23 -0700801 * it to the trace too.
Ben Chengd44faf52010-06-02 15:33:51 -0700802 */
803 if (flags & kInstrInvoke) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700804 insertClassMethodInfo(interpState, thisClass, curMethod,
805 &decInsn);
Ben Chengd44faf52010-06-02 15:33:51 -0700806 insertMoveResult(lastPC, len, offset, interpState);
807 }
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700808 }
Bill Buzbee2ce8a6c2009-12-03 15:09:32 -0800809 /* Break on throw or self-loop */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800810 if ((decInsn.opcode == OP_THROW) || (lastPC == pc)){
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700811 interpState->jitState = kJitTSelectEnd;
812 }
813 if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) {
814 interpState->jitState = kJitTSelectEnd;
815 }
Ben Chenga4973592010-03-31 11:59:18 -0700816 /* Abandon the trace request if debugger/profiler is attached */
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700817 if (debugOrProfile) {
Ben Chenga4973592010-03-31 11:59:18 -0700818 interpState->jitState = kJitDone;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700819 break;
820 }
821 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
822 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700823 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700824 else {
825 /*
826 * Last instruction is a return - stay in the dbg interpreter
827 * for one more instruction if it is a non-void return, since
828 * we don't want to start a trace with move-result as the first
829 * instruction (which is already included in the trace
830 * containing the invoke.
831 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800832 if (decInsn.opcode != OP_RETURN_VOID) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700833 stayOneMoreInst = true;
834 }
835 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700836 /* NOTE: intentional fallthrough for returns */
837 case kJitTSelectEnd:
838 {
Ben Chenga4973592010-03-31 11:59:18 -0700839 /* Bad trace */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700840 if (interpState->totalTraceLen == 0) {
Bill Buzbeed7269912009-11-10 14:31:32 -0800841 /* Bad trace - mark as untranslatable */
Ben Chenga4973592010-03-31 11:59:18 -0700842 interpState->jitState = kJitDone;
843 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844 break;
845 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700846
847 int lastTraceDesc = interpState->currTraceRun;
848
849 /* Extend a new empty desc if the last slot is meta info */
850 if (!interpState->trace[lastTraceDesc].frag.isCode) {
851 lastTraceDesc = ++interpState->currTraceRun;
852 interpState->trace[lastTraceDesc].frag.startOffset = 0;
853 interpState->trace[lastTraceDesc].frag.numInsts = 0;
854 interpState->trace[lastTraceDesc].frag.hint = kJitHintNone;
855 interpState->trace[lastTraceDesc].frag.isCode = true;
856 }
857
858 /* Mark the end of the trace runs */
859 interpState->trace[lastTraceDesc].frag.runEnd = true;
860
Ben Chengba4fc8b2009-06-01 13:00:29 -0700861 JitTraceDescription* desc =
862 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
863 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
Ben Cheng7a2697d2010-06-07 13:44:23 -0700864
Ben Chengba4fc8b2009-06-01 13:00:29 -0700865 if (desc == NULL) {
866 LOGE("Out of memory in trace selection");
867 dvmJitStopTranslationRequests();
Ben Chenga4973592010-03-31 11:59:18 -0700868 interpState->jitState = kJitDone;
869 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700870 break;
871 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700872
Ben Chengba4fc8b2009-06-01 13:00:29 -0700873 desc->method = interpState->method;
874 memcpy((char*)&(desc->trace[0]),
875 (char*)&(interpState->trace[0]),
876 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
877#if defined(SHOW_TRACE)
878 LOGD("TraceGen: trace done, adding to queue");
879#endif
Bill Buzbee964a7b02010-01-28 12:54:19 -0800880 if (dvmCompilerWorkEnqueue(
881 interpState->currTraceHead,kWorkOrderTrace,desc)) {
882 /* Work order successfully enqueued */
883 if (gDvmJit.blockingMode) {
884 dvmCompilerDrainQueue();
885 }
Ben Cheng1357e942010-02-10 17:21:39 -0800886 } else {
887 /*
888 * Make sure the descriptor for the abandoned work order is
889 * freed.
890 */
891 free(desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892 }
Bill Buzbee964a7b02010-01-28 12:54:19 -0800893 /*
894 * Reset "trace in progress" flag whether or not we
895 * successfully entered a work order.
896 */
Ben Cheng6999d842010-01-26 16:46:15 -0800897 JitEntry *jitEntry =
898 lookupAndAdd(interpState->currTraceHead, false);
899 if (jitEntry) {
900 setTraceConstruction(jitEntry, false);
901 }
Ben Chenga4973592010-03-31 11:59:18 -0700902 interpState->jitState = kJitDone;
903 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700904 }
905 break;
906 case kJitSingleStep:
907 interpState->jitState = kJitSingleStepEnd;
908 break;
909 case kJitSingleStepEnd:
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700910 /*
911 * Clear the inJitCodeCache flag and abandon the resume attempt if
912 * we cannot switch back to the translation due to corner-case
913 * conditions. If the flag is not cleared and the code cache is full
914 * we will be stuck in the debug interpreter as the code cache
915 * cannot be reset.
916 */
917 if (dvmJitStayInPortableInterpreter()) {
918 interpState->entryPoint = kInterpEntryInstr;
919 self->inJitCodeCache = 0;
920 } else {
921 interpState->entryPoint = kInterpEntryResume;
922 }
Ben Chenga4973592010-03-31 11:59:18 -0700923 interpState->jitState = kJitDone;
924 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 break;
Ben Chenga4973592010-03-31 11:59:18 -0700926 case kJitDone:
927 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700929#if defined(WITH_SELF_VERIFICATION)
930 case kJitSelfVerification:
Ben Chengd5adae12010-03-26 17:45:28 -0700931 if (selfVerificationDebugInterp(pc, self, interpState)) {
932 /*
933 * If the next state is not single-step end, we can switch
934 * interpreter now.
935 */
936 if (interpState->jitState != kJitSingleStepEnd) {
Ben Chenga4973592010-03-31 11:59:18 -0700937 interpState->jitState = kJitDone;
938 switchInterp = true;
Ben Chengd5adae12010-03-26 17:45:28 -0700939 }
Jeff Hao97319a82009-08-12 16:57:15 -0700940 }
941 break;
942#endif
Ben Chenga4973592010-03-31 11:59:18 -0700943 case kJitNot:
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700944 switchInterp = !debugOrProfile;
Ben Chenged79ff02009-10-13 13:26:40 -0700945 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 default:
Ben Chenga4973592010-03-31 11:59:18 -0700947 LOGE("Unexpected JIT state: %d entry point: %d",
948 interpState->jitState, interpState->entryPoint);
949 dvmAbort();
Ben Cheng9c147b82009-10-07 16:41:46 -0700950 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951 }
Ben Chenga4973592010-03-31 11:59:18 -0700952 /*
953 * Final check to see if we can really switch the interpreter. Make sure
954 * the jitState is kJitDone or kJitNot when switchInterp is set to true.
955 */
956 assert(switchInterp == false || interpState->jitState == kJitDone ||
957 interpState->jitState == kJitNot);
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700958 return switchInterp && !debugOrProfile && !stayOneMoreInst &&
959 !dvmJitStayInPortableInterpreter();
Ben Chengba4fc8b2009-06-01 13:00:29 -0700960}
961
Ben Chengccd6c012009-10-15 14:52:45 -0700962JitEntry *dvmFindJitEntry(const u2* pc)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700963{
964 int idx = dvmJitHash(pc);
965
966 /* Expect a high hit rate on 1st shot */
967 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
968 return &gDvmJit.pJitEntryTable[idx];
969 else {
Bill Buzbee27176222009-06-09 09:20:16 -0700970 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700971 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
972 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700973 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
974 return &gDvmJit.pJitEntryTable[idx];
975 }
976 }
977 return NULL;
978}
979
Bill Buzbee27176222009-06-09 09:20:16 -0700980/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700981 * If a translated code address exists for the davik byte code
982 * pointer return it. This routine needs to be fast.
983 */
984void* dvmJitGetCodeAddr(const u2* dPC)
985{
986 int idx = dvmJitHash(dPC);
Ben Cheng6999d842010-01-26 16:46:15 -0800987 const u2* npc = gDvmJit.pJitEntryTable[idx].dPC;
Bill Buzbee9797a232010-01-12 12:20:13 -0800988 if (npc != NULL) {
Ben Cheng1a7b9d72010-09-20 22:20:31 -0700989 bool hideTranslation = dvmJitHideTranslation();
Ben Cheng6999d842010-01-26 16:46:15 -0800990
Bill Buzbee9797a232010-01-12 12:20:13 -0800991 if (npc == dPC) {
Ben Cheng978738d2010-05-13 13:45:57 -0700992#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -0800993 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700994#endif
Ben Cheng6999d842010-01-26 16:46:15 -0800995 return hideTranslation ?
996 NULL : gDvmJit.pJitEntryTable[idx].codeAddress;
Bill Buzbee9797a232010-01-12 12:20:13 -0800997 } else {
998 int chainEndMarker = gDvmJit.jitTableSize;
999 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
1000 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
1001 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
Ben Cheng978738d2010-05-13 13:45:57 -07001002#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -08001003 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004#endif
Ben Cheng6999d842010-01-26 16:46:15 -08001005 return hideTranslation ?
1006 NULL : gDvmJit.pJitEntryTable[idx].codeAddress;
Bill Buzbee9797a232010-01-12 12:20:13 -08001007 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001008 }
1009 }
1010 }
Ben Cheng978738d2010-05-13 13:45:57 -07001011#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001012 gDvmJit.addrLookupsNotFound++;
1013#endif
1014 return NULL;
1015}
1016
1017/*
1018 * Register the translated code pointer into the JitTable.
Bill Buzbee9a8c75a2009-11-08 14:31:20 -08001019 * NOTE: Once a codeAddress field transitions from initial state to
Ben Chengba4fc8b2009-06-01 13:00:29 -07001020 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -07001021 * threads. This routine should only be called by the compiler
1022 * thread.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001023 */
Bill Buzbee716f1202009-07-23 13:22:09 -07001024void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) {
1025 JitEntryInfoUnion oldValue;
1026 JitEntryInfoUnion newValue;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001027 JitEntry *jitEntry = lookupAndAdd(dPC, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001028 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -07001029 /* Note: order of update is important */
1030 do {
1031 oldValue = jitEntry->u;
1032 newValue = oldValue;
1033 newValue.info.instructionSet = set;
Andy McFadden6e10b9a2010-06-14 15:24:39 -07001034 } while (android_atomic_release_cas(
1035 oldValue.infoWord, newValue.infoWord,
1036 &jitEntry->u.infoWord) != 0);
Bill Buzbee716f1202009-07-23 13:22:09 -07001037 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001038}
1039
1040/*
1041 * Determine if valid trace-bulding request is active. Return true
1042 * if we need to abort and switch back to the fast interpreter, false
Ben Chenga4973592010-03-31 11:59:18 -07001043 * otherwise.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001045bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState)
1046{
Ben Chenga4973592010-03-31 11:59:18 -07001047 bool switchInterp = false; /* Assume success */
Bill Buzbee48f18242009-06-19 16:02:27 -07001048 int i;
buzbee852aacd2010-06-08 16:24:46 -07001049 /*
1050 * A note on trace "hotness" filtering:
1051 *
1052 * Our first level trigger is intentionally loose - we need it to
1053 * fire easily not just to identify potential traces to compile, but
1054 * also to allow re-entry into the code cache.
1055 *
1056 * The 2nd level filter (done here) exists to be selective about
1057 * what we actually compile. It works by requiring the same
1058 * trace head "key" (defined as filterKey below) to appear twice in
1059 * a relatively short period of time. The difficulty is defining the
1060 * shape of the filterKey. Unfortunately, there is no "one size fits
1061 * all" approach.
1062 *
1063 * For spiky execution profiles dominated by a smallish
1064 * number of very hot loops, we would want the second-level filter
1065 * to be very selective. A good selective filter is requiring an
1066 * exact match of the Dalvik PC. In other words, defining filterKey as:
1067 * intptr_t filterKey = (intptr_t)interpState->pc
1068 *
1069 * However, for flat execution profiles we do best when aggressively
1070 * translating. A heuristically decent proxy for this is to use
1071 * the value of the method pointer containing the trace as the filterKey.
1072 * Intuitively, this is saying that once any trace in a method appears hot,
1073 * immediately translate any other trace from that same method that
1074 * survives the first-level filter. Here, filterKey would be defined as:
1075 * intptr_t filterKey = (intptr_t)interpState->method
1076 *
1077 * The problem is that we can't easily detect whether we're dealing
1078 * with a spiky or flat profile. If we go with the "pc" match approach,
1079 * flat profiles perform poorly. If we go with the loose "method" match,
1080 * we end up generating a lot of useless translations. Probably the
1081 * best approach in the future will be to retain profile information
1082 * across runs of each application in order to determine it's profile,
1083 * and then choose once we have enough history.
1084 *
1085 * However, for now we've decided to chose a compromise filter scheme that
1086 * includes elements of both. The high order bits of the filter key
1087 * are drawn from the enclosing method, and are combined with a slice
1088 * of the low-order bits of the Dalvik pc of the trace head. The
1089 * looseness of the filter can be adjusted by changing with width of
1090 * the Dalvik pc slice (JIT_TRACE_THRESH_FILTER_PC_BITS). The wider
1091 * the slice, the tighter the filter.
1092 *
1093 * Note: the fixed shifts in the function below reflect assumed word
1094 * alignment for method pointers, and half-word alignment of the Dalvik pc.
1095 * for method pointers and half-word alignment for dalvik pc.
1096 */
buzbeec35294d2010-06-09 14:22:50 -07001097 u4 methodKey = (u4)interpState->method <<
1098 (JIT_TRACE_THRESH_FILTER_PC_BITS - 2);
1099 u4 pcKey = ((u4)interpState->pc >> 1) &
1100 ((1 << JIT_TRACE_THRESH_FILTER_PC_BITS) - 1);
1101 intptr_t filterKey = (intptr_t)(methodKey | pcKey);
Ben Chenga4973592010-03-31 11:59:18 -07001102 bool debugOrProfile = dvmDebuggerOrProfilerActive();
Ben Cheng40094c12010-02-24 20:58:44 -08001103
Ben Chenga4973592010-03-31 11:59:18 -07001104 /* Check if the JIT request can be handled now */
1105 if (gDvmJit.pJitEntryTable != NULL && debugOrProfile == false) {
1106 /* Bypass the filter for hot trace requests or during stress mode */
1107 if (interpState->jitState == kJitTSelectRequest &&
1108 gDvmJit.threshold > 6) {
Ben Cheng40094c12010-02-24 20:58:44 -08001109 /* Two-level filtering scheme */
1110 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
1111 if (filterKey == interpState->threshFilter[i]) {
buzbee852aacd2010-06-08 16:24:46 -07001112 interpState->threshFilter[i] = 0; // Reset filter entry
Ben Cheng40094c12010-02-24 20:58:44 -08001113 break;
1114 }
Bill Buzbee48f18242009-06-19 16:02:27 -07001115 }
Ben Cheng40094c12010-02-24 20:58:44 -08001116 if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
1117 /*
1118 * Use random replacement policy - otherwise we could miss a
1119 * large loop that contains more traces than the size of our
1120 * filter array.
1121 */
1122 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
1123 interpState->threshFilter[i] = filterKey;
Ben Chenga4973592010-03-31 11:59:18 -07001124 interpState->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001125 }
Ben Chenga4973592010-03-31 11:59:18 -07001126 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001127
Ben Chenga4973592010-03-31 11:59:18 -07001128 /* If the compiler is backlogged, cancel any JIT actions */
1129 if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) {
1130 interpState->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001131 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001132
Ben Chengba4fc8b2009-06-01 13:00:29 -07001133 /*
Ben Chenga4973592010-03-31 11:59:18 -07001134 * Check for additional reasons that might force the trace select
1135 * request to be dropped
Ben Chengba4fc8b2009-06-01 13:00:29 -07001136 */
Ben Chenga4973592010-03-31 11:59:18 -07001137 if (interpState->jitState == kJitTSelectRequest ||
1138 interpState->jitState == kJitTSelectRequestHot) {
Bill Buzbee964a7b02010-01-28 12:54:19 -08001139 JitEntry *slot = lookupAndAdd(interpState->pc, false);
Bill Buzbee716f1202009-07-23 13:22:09 -07001140 if (slot == NULL) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001141 /*
Bill Buzbee716f1202009-07-23 13:22:09 -07001142 * Table is full. This should have been
1143 * detected by the compiler thread and the table
1144 * resized before we run into it here. Assume bad things
1145 * are afoot and disable profiling.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001146 */
Ben Chenga4973592010-03-31 11:59:18 -07001147 interpState->jitState = kJitDone;
Bill Buzbee716f1202009-07-23 13:22:09 -07001148 LOGD("JIT: JitTable full, disabling profiling");
1149 dvmJitStopTranslationRequests();
Bill Buzbeed7269912009-11-10 14:31:32 -08001150 } else if (slot->u.info.traceConstruction) {
1151 /*
Ben Cheng60c24f42010-01-04 12:29:56 -08001152 * Trace request already in progress, but most likely it
Bill Buzbeed7269912009-11-10 14:31:32 -08001153 * aborted without cleaning up. Assume the worst and
1154 * mark trace head as untranslatable. If we're wrong,
1155 * the compiler thread will correct the entry when the
1156 * translation is completed. The downside here is that
1157 * some existing translation may chain to the interpret-only
1158 * template instead of the real translation during this
1159 * window. Performance, but not correctness, issue.
1160 */
Ben Chenga4973592010-03-31 11:59:18 -07001161 interpState->jitState = kJitDone;
Bill Buzbeed7269912009-11-10 14:31:32 -08001162 resetTracehead(interpState, slot);
1163 } else if (slot->codeAddress) {
1164 /* Nothing to do here - just return */
Ben Chenga4973592010-03-31 11:59:18 -07001165 interpState->jitState = kJitDone;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001166 } else {
Bill Buzbeed7269912009-11-10 14:31:32 -08001167 /*
1168 * Mark request. Note, we are not guaranteed exclusivity
1169 * here. A window exists for another thread to be
1170 * attempting to build this same trace. Rather than
1171 * bear the cost of locking, we'll just allow that to
1172 * happen. The compiler thread, if it chooses, can
1173 * discard redundant requests.
1174 */
1175 setTraceConstruction(slot, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001176 }
1177 }
Ben Chenga4973592010-03-31 11:59:18 -07001178
Ben Chengba4fc8b2009-06-01 13:00:29 -07001179 switch (interpState->jitState) {
1180 case kJitTSelectRequest:
Ben Cheng40094c12010-02-24 20:58:44 -08001181 case kJitTSelectRequestHot:
Ben Chenga4973592010-03-31 11:59:18 -07001182 interpState->jitState = kJitTSelect;
1183 interpState->currTraceHead = interpState->pc;
1184 interpState->currTraceRun = 0;
1185 interpState->totalTraceLen = 0;
1186 interpState->currRunHead = interpState->pc;
1187 interpState->currRunLen = 0;
1188 interpState->trace[0].frag.startOffset =
1189 interpState->pc - interpState->method->insns;
1190 interpState->trace[0].frag.numInsts = 0;
1191 interpState->trace[0].frag.runEnd = false;
1192 interpState->trace[0].frag.hint = kJitHintNone;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001193 interpState->trace[0].frag.isCode = true;
Ben Chenga4973592010-03-31 11:59:18 -07001194 interpState->lastPC = 0;
1195 break;
1196 /*
1197 * For JIT's perspective there is no need to stay in the debug
1198 * interpreter unless debugger/profiler is attached.
1199 */
1200 case kJitDone:
1201 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001202 break;
1203 default:
Ben Chenga4973592010-03-31 11:59:18 -07001204 LOGE("Unexpected JIT state: %d entry point: %d",
1205 interpState->jitState, interpState->entryPoint);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001206 dvmAbort();
1207 }
Ben Chenga4973592010-03-31 11:59:18 -07001208 } else {
1209 /*
1210 * Cannot build trace this time - ready to leave the dbg interpreter
1211 */
1212 interpState->jitState = kJitDone;
1213 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001214 }
Ben Chenga4973592010-03-31 11:59:18 -07001215
1216 /*
1217 * Final check to see if we can really switch the interpreter. Make sure
1218 * the jitState is kJitDone when switchInterp is set to true.
1219 */
1220 assert(switchInterp == false || interpState->jitState == kJitDone);
Ben Cheng1a7b9d72010-09-20 22:20:31 -07001221 return switchInterp && !debugOrProfile &&
1222 !dvmJitStayInPortableInterpreter();
Ben Chengba4fc8b2009-06-01 13:00:29 -07001223}
1224
Bill Buzbee27176222009-06-09 09:20:16 -07001225/*
1226 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
Bill Buzbee964a7b02010-01-28 12:54:19 -08001227 * Stops all threads, and thus is a heavyweight operation. May only be called
1228 * by the compiler thread.
Bill Buzbee27176222009-06-09 09:20:16 -07001229 */
1230bool dvmJitResizeJitTable( unsigned int size )
1231{
Bill Buzbee716f1202009-07-23 13:22:09 -07001232 JitEntry *pNewTable;
1233 JitEntry *pOldTable;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001234 JitEntry tempEntry;
Bill Buzbee27176222009-06-09 09:20:16 -07001235 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -07001236 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001237 unsigned int i;
1238
Ben Cheng3f02aa42009-08-14 13:52:09 -07001239 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -07001240 assert(size && !(size & (size - 1))); /* Is power of 2? */
1241
Ben Chenga4973592010-03-31 11:59:18 -07001242 LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
Bill Buzbee27176222009-06-09 09:20:16 -07001243
1244 newMask = size - 1;
1245
1246 if (size <= gDvmJit.jitTableSize) {
1247 return true;
1248 }
1249
Bill Buzbee964a7b02010-01-28 12:54:19 -08001250 /* Make sure requested size is compatible with chain field width */
1251 tempEntry.u.info.chain = size;
1252 if (tempEntry.u.info.chain != size) {
1253 LOGD("Jit: JitTable request of %d too big", size);
1254 return true;
1255 }
1256
Bill Buzbee716f1202009-07-23 13:22:09 -07001257 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -07001258 if (pNewTable == NULL) {
1259 return true;
1260 }
1261 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -07001262 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -07001263 }
1264
1265 /* Stop all other interpreting/jit'ng threads */
Ben Chenga8e64a72009-10-20 13:01:36 -07001266 dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001267
Bill Buzbee716f1202009-07-23 13:22:09 -07001268 pOldTable = gDvmJit.pJitEntryTable;
1269 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001270
1271 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -07001272 gDvmJit.pJitEntryTable = pNewTable;
1273 gDvmJit.jitTableSize = size;
1274 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -07001275 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -07001276
Bill Buzbee716f1202009-07-23 13:22:09 -07001277 for (i=0; i < oldSize; i++) {
1278 if (pOldTable[i].dPC) {
1279 JitEntry *p;
1280 u2 chain;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001281 p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/ );
1282 p->codeAddress = pOldTable[i].codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -07001283 /* We need to preserve the new chain field, but copy the rest */
Bill Buzbee716f1202009-07-23 13:22:09 -07001284 chain = p->u.info.chain;
1285 p->u = pOldTable[i].u;
1286 p->u.info.chain = chain;
Bill Buzbee716f1202009-07-23 13:22:09 -07001287 }
1288 }
Bill Buzbee964a7b02010-01-28 12:54:19 -08001289 dvmUnlockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -07001290
1291 free(pOldTable);
1292
Bill Buzbee27176222009-06-09 09:20:16 -07001293 /* Restart the world */
Ben Chenga8e64a72009-10-20 13:01:36 -07001294 dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001295
1296 return false;
1297}
1298
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001299/*
Ben Cheng60c24f42010-01-04 12:29:56 -08001300 * Reset the JitTable to the initial clean state.
1301 */
1302void dvmJitResetTable(void)
1303{
1304 JitEntry *jitEntry = gDvmJit.pJitEntryTable;
1305 unsigned int size = gDvmJit.jitTableSize;
1306 unsigned int i;
1307
1308 dvmLockMutex(&gDvmJit.tableLock);
1309 memset((void *) jitEntry, 0, sizeof(JitEntry) * size);
1310 for (i=0; i< size; i++) {
1311 jitEntry[i].u.info.chain = size; /* Initialize chain termination */
1312 }
1313 gDvmJit.jitTableEntriesUsed = 0;
1314 dvmUnlockMutex(&gDvmJit.tableLock);
1315}
1316
1317/*
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001318 * Float/double conversion requires clamping to min and max of integer form. If
1319 * target doesn't support this normally, use these.
1320 */
1321s8 dvmJitd2l(double d)
1322{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001323 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1324 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001325 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001326 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001327 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001328 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001329 else if (d != d) // NaN case
1330 return 0;
1331 else
1332 return (s8)d;
1333}
1334
1335s8 dvmJitf2l(float f)
1336{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001337 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1338 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001339 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001340 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001341 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001342 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001343 else if (f != f) // NaN case
1344 return 0;
1345 else
1346 return (s8)f;
1347}
1348
Ben Chengba4fc8b2009-06-01 13:00:29 -07001349#endif /* WITH_JIT */