blob: a4a82c9fff9286bae7a26a442a4fd7757143cb68 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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
17#include "Dalvik.h"
18#include "libdex/OpCode.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070019#include "interp/Jit.h"
20#include "CompilerInternals.h"
21
22/*
23 * Parse an instruction, return the length of the instruction
24 */
25static inline int parseInsn(const u2 *codePtr, DecodedInstruction *decInsn,
26 bool printMe)
27{
28 u2 instr = *codePtr;
29 OpCode opcode = instr & 0xff;
30 int insnWidth;
31
Ben Cheng8b258bf2009-06-24 17:27:07 -070032 // Don't parse instruction data
Ben Chengba4fc8b2009-06-01 13:00:29 -070033 if (opcode == OP_NOP && instr != 0) {
Ben Cheng8b258bf2009-06-24 17:27:07 -070034 return 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -070035 } else {
36 insnWidth = gDvm.instrWidth[opcode];
37 if (insnWidth < 0) {
38 insnWidth = -insnWidth;
39 }
40 }
41
42 dexDecodeInstruction(gDvm.instrFormat, codePtr, decInsn);
43 if (printMe) {
Ben Chengccd6c012009-10-15 14:52:45 -070044 char *decodedString = dvmCompilerGetDalvikDisassembly(decInsn);
45 LOGD("%p: %#06x %s\n", codePtr, opcode, decodedString);
Ben Chengba4fc8b2009-06-01 13:00:29 -070046 }
47 return insnWidth;
48}
49
Ben Cheng9c147b82009-10-07 16:41:46 -070050#define UNKNOWN_TARGET 0xffffffff
51
Ben Chengba4fc8b2009-06-01 13:00:29 -070052/*
53 * Identify block-ending instructions and collect supplemental information
54 * regarding the following instructions.
55 */
56static inline bool findBlockBoundary(const Method *caller, MIR *insn,
57 unsigned int curOffset,
58 unsigned int *target, bool *isInvoke,
59 const Method **callee)
60{
61 switch (insn->dalvikInsn.opCode) {
62 /* Target is not compile-time constant */
63 case OP_RETURN_VOID:
64 case OP_RETURN:
65 case OP_RETURN_WIDE:
66 case OP_RETURN_OBJECT:
67 case OP_THROW:
Ben Cheng9c147b82009-10-07 16:41:46 -070068 *target = UNKNOWN_TARGET;
69 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -070070 case OP_INVOKE_VIRTUAL:
71 case OP_INVOKE_VIRTUAL_RANGE:
72 case OP_INVOKE_INTERFACE:
73 case OP_INVOKE_INTERFACE_RANGE:
74 case OP_INVOKE_VIRTUAL_QUICK:
75 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
76 *isInvoke = true;
77 break;
78 case OP_INVOKE_SUPER:
79 case OP_INVOKE_SUPER_RANGE: {
80 int mIndex = caller->clazz->pDvmDex->
81 pResMethods[insn->dalvikInsn.vB]->methodIndex;
82 const Method *calleeMethod =
83 caller->clazz->super->vtable[mIndex];
84
Ben Cheng8b258bf2009-06-24 17:27:07 -070085 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -070086 *target = (unsigned int) calleeMethod->insns;
87 }
88 *isInvoke = true;
89 *callee = calleeMethod;
90 break;
91 }
92 case OP_INVOKE_STATIC:
93 case OP_INVOKE_STATIC_RANGE: {
94 const Method *calleeMethod =
95 caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB];
96
Ben Cheng8b258bf2009-06-24 17:27:07 -070097 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -070098 *target = (unsigned int) calleeMethod->insns;
99 }
100 *isInvoke = true;
101 *callee = calleeMethod;
102 break;
103 }
104 case OP_INVOKE_SUPER_QUICK:
105 case OP_INVOKE_SUPER_QUICK_RANGE: {
106 const Method *calleeMethod =
107 caller->clazz->super->vtable[insn->dalvikInsn.vB];
108
Ben Cheng8b258bf2009-06-24 17:27:07 -0700109 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700110 *target = (unsigned int) calleeMethod->insns;
111 }
112 *isInvoke = true;
113 *callee = calleeMethod;
114 break;
115 }
116 case OP_INVOKE_DIRECT:
117 case OP_INVOKE_DIRECT_RANGE: {
118 const Method *calleeMethod =
119 caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB];
Ben Cheng8b258bf2009-06-24 17:27:07 -0700120 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700121 *target = (unsigned int) calleeMethod->insns;
122 }
123 *isInvoke = true;
124 *callee = calleeMethod;
125 break;
126 }
127 case OP_GOTO:
128 case OP_GOTO_16:
129 case OP_GOTO_32:
130 *target = curOffset + (int) insn->dalvikInsn.vA;
131 break;
132
133 case OP_IF_EQ:
134 case OP_IF_NE:
135 case OP_IF_LT:
136 case OP_IF_GE:
137 case OP_IF_GT:
138 case OP_IF_LE:
139 *target = curOffset + (int) insn->dalvikInsn.vC;
140 break;
141
142 case OP_IF_EQZ:
143 case OP_IF_NEZ:
144 case OP_IF_LTZ:
145 case OP_IF_GEZ:
146 case OP_IF_GTZ:
147 case OP_IF_LEZ:
148 *target = curOffset + (int) insn->dalvikInsn.vB;
149 break;
150
151 default:
152 return false;
Ben Cheng9c147b82009-10-07 16:41:46 -0700153 }
154 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700155}
156
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800157static inline bool isGoto(MIR *insn)
158{
159 switch (insn->dalvikInsn.opCode) {
160 case OP_GOTO:
161 case OP_GOTO_16:
162 case OP_GOTO_32:
163 return true;
164 default:
165 return false;
166 }
167}
168
Ben Chengba4fc8b2009-06-01 13:00:29 -0700169/*
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800170 * Identify unconditional branch instructions
Ben Chengba4fc8b2009-06-01 13:00:29 -0700171 */
172static inline bool isUnconditionalBranch(MIR *insn)
173{
174 switch (insn->dalvikInsn.opCode) {
175 case OP_RETURN_VOID:
176 case OP_RETURN:
177 case OP_RETURN_WIDE:
178 case OP_RETURN_OBJECT:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700179 return true;
180 default:
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800181 return isGoto(insn);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700182 }
183}
184
185/*
Ben Cheng8b258bf2009-06-24 17:27:07 -0700186 * dvmHashTableLookup() callback
187 */
188static int compareMethod(const CompilerMethodStats *m1,
189 const CompilerMethodStats *m2)
190{
191 return (int) m1->method - (int) m2->method;
192}
193
194/*
195 * Analyze each method whose traces are ever compiled. Collect a variety of
196 * statistics like the ratio of exercised vs overall code and code bloat
197 * ratios.
198 */
199static CompilerMethodStats *analyzeMethodBody(const Method *method)
200{
201 const DexCode *dexCode = dvmGetMethodCode(method);
202 const u2 *codePtr = dexCode->insns;
203 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
204 int insnSize = 0;
205 int hashValue = dvmComputeUtf8Hash(method->name);
206
207 CompilerMethodStats dummyMethodEntry; // For hash table lookup
208 CompilerMethodStats *realMethodEntry; // For hash table storage
209
210 /* For lookup only */
211 dummyMethodEntry.method = method;
212 realMethodEntry = dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
213 &dummyMethodEntry,
214 (HashCompareFunc) compareMethod,
215 false);
216
217 /* Part of this method has been compiled before - just return the entry */
218 if (realMethodEntry != NULL) {
219 return realMethodEntry;
220 }
221
222 /*
223 * First time to compile this method - set up a new entry in the hash table
224 */
225 realMethodEntry =
226 (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats));
227 realMethodEntry->method = method;
228
229 dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
230 realMethodEntry,
231 (HashCompareFunc) compareMethod,
232 true);
233
234 /* Count the number of instructions */
235 while (codePtr < codeEnd) {
236 DecodedInstruction dalvikInsn;
237 int width = parseInsn(codePtr, &dalvikInsn, false);
238
239 /* Terminate when the data section is seen */
240 if (width == 0)
241 break;
242
243 insnSize += width;
244 codePtr += width;
245 }
246
247 realMethodEntry->dalvikSize = insnSize * 2;
248 return realMethodEntry;
249}
250
251/*
Ben Cheng33672452010-01-12 14:59:30 -0800252 * Crawl the stack of the thread that requesed compilation to see if any of the
253 * ancestors are on the blacklist.
254 */
255bool filterMethodByCallGraph(Thread *thread, const char *curMethodName)
256{
257 /* Crawl the Dalvik stack frames and compare the method name*/
258 StackSaveArea *ssaPtr = ((StackSaveArea *) thread->curFrame) - 1;
259 while (ssaPtr != ((StackSaveArea *) NULL) - 1) {
260 const Method *method = ssaPtr->method;
261 if (method) {
262 int hashValue = dvmComputeUtf8Hash(method->name);
263 bool found =
264 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
265 (char *) method->name,
266 (HashCompareFunc) strcmp, false) !=
267 NULL;
268 if (found) {
269 LOGD("Method %s (--> %s) found on the JIT %s list",
270 method->name, curMethodName,
271 gDvmJit.includeSelectedMethod ? "white" : "black");
272 return true;
273 }
274
275 }
276 ssaPtr = ((StackSaveArea *) ssaPtr->prevFrame) - 1;
277 };
278 return false;
279}
280
281/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700282 * Main entry point to start trace compilation. Basic blocks are constructed
283 * first and they will be passed to the codegen routines to convert Dalvik
284 * bytecode into machine code.
285 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700286bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts,
287 JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288{
289 const DexCode *dexCode = dvmGetMethodCode(desc->method);
290 const JitTraceRun* currRun = &desc->trace[0];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700291 unsigned int curOffset = currRun->frag.startOffset;
292 unsigned int numInsts = currRun->frag.numInsts;
293 const u2 *codePtr = dexCode->insns + curOffset;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700294 int traceSize = 0; // # of half-words
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295 const u2 *startCodePtr = codePtr;
296 BasicBlock *startBB, *curBB, *lastBB;
297 int numBlocks = 0;
298 static int compilationId;
299 CompilationUnit cUnit;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700300 CompilerMethodStats *methodStats;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700301
Ben Chenge9695e52009-06-16 16:11:47 -0700302 compilationId++;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700303 memset(&cUnit, 0, sizeof(CompilationUnit));
304
305 /* Locate the entry to store compilation statistics for this method */
306 methodStats = analyzeMethodBody(desc->method);
Ben Chenge9695e52009-06-16 16:11:47 -0700307
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308 /* Initialize the printMe flag */
309 cUnit.printMe = gDvmJit.printMe;
310
Bill Buzbee6e963e12009-06-17 16:56:19 -0700311 /* Initialize the profile flag */
312 cUnit.executionCount = gDvmJit.profile;
313
Ben Chengba4fc8b2009-06-01 13:00:29 -0700314 /* Identify traces that we don't want to compile */
315 if (gDvmJit.methodTable) {
316 int len = strlen(desc->method->clazz->descriptor) +
317 strlen(desc->method->name) + 1;
318 char *fullSignature = dvmCompilerNew(len, true);
319 strcpy(fullSignature, desc->method->clazz->descriptor);
320 strcat(fullSignature, desc->method->name);
321
322 int hashValue = dvmComputeUtf8Hash(fullSignature);
323
324 /*
325 * Doing three levels of screening to see whether we want to skip
326 * compiling this method
327 */
328
329 /* First, check the full "class;method" signature */
330 bool methodFound =
331 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
332 fullSignature, (HashCompareFunc) strcmp,
333 false) !=
334 NULL;
335
336 /* Full signature not found - check the enclosing class */
337 if (methodFound == false) {
338 int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor);
339 methodFound =
340 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
341 (char *) desc->method->clazz->descriptor,
342 (HashCompareFunc) strcmp, false) !=
343 NULL;
344 /* Enclosing class not found - check the method name */
345 if (methodFound == false) {
346 int hashValue = dvmComputeUtf8Hash(desc->method->name);
347 methodFound =
348 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
349 (char *) desc->method->name,
350 (HashCompareFunc) strcmp, false) !=
351 NULL;
Ben Cheng33672452010-01-12 14:59:30 -0800352
353 /*
354 * Debug by call-graph is enabled. Check if the debug list
355 * covers any methods on the VM stack.
356 */
357 if (methodFound == false && gDvmJit.checkCallGraph == true) {
358 methodFound =
359 filterMethodByCallGraph(info->requestingThread,
360 desc->method->name);
361 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700362 }
363 }
364
365 /*
366 * Under the following conditions, the trace will be *conservatively*
367 * compiled by only containing single-step instructions to and from the
368 * interpreter.
369 * 1) If includeSelectedMethod == false, the method matches the full or
370 * partial signature stored in the hash table.
371 *
372 * 2) If includeSelectedMethod == true, the method does not match the
373 * full and partial signature stored in the hash table.
374 */
375 if (gDvmJit.includeSelectedMethod != methodFound) {
376 cUnit.allSingleStep = true;
377 } else {
378 /* Compile the trace as normal */
379
380 /* Print the method we cherry picked */
381 if (gDvmJit.includeSelectedMethod == true) {
382 cUnit.printMe = true;
383 }
384 }
385 }
386
Ben Cheng4238ec22009-08-24 16:32:22 -0700387 /* Allocate the entry block */
Bill Buzbee1465db52009-09-23 17:17:35 -0700388 lastBB = startBB = curBB = dvmCompilerNewBB(kEntryBlock);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700389 curBB->startOffset = curOffset;
390 curBB->id = numBlocks++;
391
Bill Buzbee1465db52009-09-23 17:17:35 -0700392 curBB = dvmCompilerNewBB(kDalvikByteCode);
Ben Cheng4238ec22009-08-24 16:32:22 -0700393 curBB->startOffset = curOffset;
394 curBB->id = numBlocks++;
395
396 /* Make the first real dalvik block the fallthrough of the entry block */
397 startBB->fallThrough = curBB;
398 lastBB->next = curBB;
399 lastBB = curBB;
400
Ben Chengba4fc8b2009-06-01 13:00:29 -0700401 if (cUnit.printMe) {
402 LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n",
403 desc->method->name, curOffset);
404 }
405
Ben Cheng1efc9c52009-06-08 18:25:27 -0700406 /*
407 * Analyze the trace descriptor and include up to the maximal number
408 * of Dalvik instructions into the IR.
409 */
410 while (1) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700411 MIR *insn;
412 int width;
Ben Cheng4238ec22009-08-24 16:32:22 -0700413 insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700414 insn->offset = curOffset;
415 width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700416
417 /* The trace should never incude instruction data */
418 assert(width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700419 insn->width = width;
420 traceSize += width;
421 dvmCompilerAppendMIR(curBB, insn);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700422 cUnit.numInsts++;
423 /* Instruction limit reached - terminate the trace here */
424 if (cUnit.numInsts >= numMaxInsts) {
425 break;
426 }
427 if (--numInsts == 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700428 if (currRun->frag.runEnd) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700429 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700430 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700431 curBB = dvmCompilerNewBB(kDalvikByteCode);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700432 lastBB->next = curBB;
433 lastBB = curBB;
434 curBB->id = numBlocks++;
435 currRun++;
436 curOffset = currRun->frag.startOffset;
437 numInsts = currRun->frag.numInsts;
438 curBB->startOffset = curOffset;
439 codePtr = dexCode->insns + curOffset;
440 }
441 } else {
442 curOffset += width;
443 codePtr += width;
444 }
445 }
446
Ben Cheng8b258bf2009-06-24 17:27:07 -0700447 /* Convert # of half-word to bytes */
448 methodStats->compiledDalvikSize += traceSize * 2;
449
Ben Chengba4fc8b2009-06-01 13:00:29 -0700450 /*
451 * Now scan basic blocks containing real code to connect the
452 * taken/fallthrough links. Also create chaining cells for code not included
453 * in the trace.
454 */
455 for (curBB = startBB; curBB; curBB = curBB->next) {
456 MIR *lastInsn = curBB->lastMIRInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -0700457 /* Skip empty blocks */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700458 if (lastInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700459 continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700460 }
461 curOffset = lastInsn->offset;
462 unsigned int targetOffset = curOffset;
463 unsigned int fallThroughOffset = curOffset + lastInsn->width;
464 bool isInvoke = false;
465 const Method *callee = NULL;
466
467 findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset,
468 &targetOffset, &isInvoke, &callee);
469
470 /* Link the taken and fallthrough blocks */
471 BasicBlock *searchBB;
472
473 /* No backward branch in the trace - start searching the next BB */
474 for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) {
475 if (targetOffset == searchBB->startOffset) {
476 curBB->taken = searchBB;
477 }
478 if (fallThroughOffset == searchBB->startOffset) {
479 curBB->fallThrough = searchBB;
480 }
481 }
482
Ben Cheng1efc9c52009-06-08 18:25:27 -0700483 int flags = dexGetInstrFlags(gDvm.instrFlags,
484 lastInsn->dalvikInsn.opCode);
485
486 /*
487 * Some blocks are ended by non-control-flow-change instructions,
488 * currently only due to trace length constraint. In this case we need
489 * to generate an explicit branch at the end of the block to jump to
490 * the chaining cell.
Ben Cheng17f15ce2009-07-27 16:21:52 -0700491 *
492 * NOTE: INVOKE_DIRECT_EMPTY is actually not an invoke but a nop
Ben Cheng1efc9c52009-06-08 18:25:27 -0700493 */
494 curBB->needFallThroughBranch =
Ben Cheng17f15ce2009-07-27 16:21:52 -0700495 ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
496 kInstrInvoke)) == 0) ||
497 (lastInsn->dalvikInsn.opCode == OP_INVOKE_DIRECT_EMPTY);
498
Ben Cheng4238ec22009-08-24 16:32:22 -0700499 if (curBB->taken == NULL &&
500 curBB->fallThrough == NULL &&
501 flags == (kInstrCanBranch | kInstrCanContinue) &&
502 fallThroughOffset == startBB->startOffset) {
Ben Cheng0fd31e42009-09-03 14:40:16 -0700503 BasicBlock *loopBranch = curBB;
504 BasicBlock *exitBB;
505 BasicBlock *exitChainingCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700506
507 if (cUnit.printMe) {
508 LOGD("Natural loop detected!");
509 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700510 exitBB = dvmCompilerNewBB(kExitBlock);
Ben Cheng0fd31e42009-09-03 14:40:16 -0700511 lastBB->next = exitBB;
512 lastBB = exitBB;
Ben Cheng4238ec22009-08-24 16:32:22 -0700513
Ben Cheng0fd31e42009-09-03 14:40:16 -0700514 exitBB->startOffset = targetOffset;
515 exitBB->id = numBlocks++;
516 exitBB->needFallThroughBranch = true;
Ben Cheng4238ec22009-08-24 16:32:22 -0700517
Ben Cheng0fd31e42009-09-03 14:40:16 -0700518 loopBranch->taken = exitBB;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700519#if defined(WITH_SELF_VERIFICATION)
Ben Cheng0fd31e42009-09-03 14:40:16 -0700520 BasicBlock *backwardCell =
Bill Buzbee1465db52009-09-23 17:17:35 -0700521 dvmCompilerNewBB(kChainingCellBackwardBranch);
Ben Cheng0fd31e42009-09-03 14:40:16 -0700522 lastBB->next = backwardCell;
523 lastBB = backwardCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700524
Ben Cheng0fd31e42009-09-03 14:40:16 -0700525 backwardCell->startOffset = startBB->startOffset;
526 backwardCell->id = numBlocks++;
527 loopBranch->fallThrough = backwardCell;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700528#elif defined(WITH_JIT_TUNING)
529 if (gDvmJit.profile) {
530 BasicBlock *backwardCell =
Bill Buzbee1465db52009-09-23 17:17:35 -0700531 dvmCompilerNewBB(kChainingCellBackwardBranch);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700532 lastBB->next = backwardCell;
533 lastBB = backwardCell;
534
535 backwardCell->startOffset = startBB->startOffset;
536 backwardCell->id = numBlocks++;
537 loopBranch->fallThrough = backwardCell;
538 } else {
539 loopBranch->fallThrough = startBB->next;
540 }
541#else
542 loopBranch->fallThrough = startBB->next;
Ben Cheng0fd31e42009-09-03 14:40:16 -0700543#endif
544
545 /* Create the chaining cell as the fallthrough of the exit block */
Bill Buzbee1465db52009-09-23 17:17:35 -0700546 exitChainingCell = dvmCompilerNewBB(kChainingCellNormal);
Ben Cheng0fd31e42009-09-03 14:40:16 -0700547 lastBB->next = exitChainingCell;
548 lastBB = exitChainingCell;
549
550 exitChainingCell->startOffset = targetOffset;
551 exitChainingCell->id = numBlocks++;
552
553 exitBB->fallThrough = exitChainingCell;
554
Ben Cheng4238ec22009-08-24 16:32:22 -0700555 cUnit.hasLoop = true;
556 }
557
Ben Cheng6c10a972009-10-29 14:39:18 -0700558 if (lastInsn->dalvikInsn.opCode == OP_PACKED_SWITCH ||
559 lastInsn->dalvikInsn.opCode == OP_SPARSE_SWITCH) {
560 int i;
561 const u2 *switchData = desc->method->insns + lastInsn->offset +
562 lastInsn->dalvikInsn.vB;
563 int size = switchData[1];
564 int maxChains = MIN(size, MAX_CHAINED_SWITCH_CASES);
565
566 /*
567 * Generate the landing pad for cases whose ranks are higher than
568 * MAX_CHAINED_SWITCH_CASES. The code will re-enter the interpreter
569 * through the NoChain point.
570 */
571 if (maxChains != size) {
572 cUnit.switchOverflowPad =
573 desc->method->insns + lastInsn->offset;
574 }
575
576 s4 *targets = (s4 *) (switchData + 2 +
577 (lastInsn->dalvikInsn.opCode == OP_PACKED_SWITCH ?
578 2 : size * 2));
579
580 /* One chaining cell for the first MAX_CHAINED_SWITCH_CASES cases */
581 for (i = 0; i < maxChains; i++) {
582 BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal);
583 lastBB->next = caseChain;
584 lastBB = caseChain;
585
586 caseChain->startOffset = lastInsn->offset + targets[i];
587 caseChain->id = numBlocks++;
588 }
589
590 /* One more chaining cell for the default case */
591 BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal);
592 lastBB->next = caseChain;
593 lastBB = caseChain;
594
595 caseChain->startOffset = lastInsn->offset + lastInsn->width;
596 caseChain->id = numBlocks++;
Ben Cheng6d576092009-09-01 17:01:58 -0700597 /* Fallthrough block not included in the trace */
Ben Cheng6c10a972009-10-29 14:39:18 -0700598 } else if (!isUnconditionalBranch(lastInsn) &&
599 curBB->fallThrough == NULL) {
Ben Cheng6d576092009-09-01 17:01:58 -0700600 /*
601 * If the chaining cell is after an invoke or
602 * instruction that cannot change the control flow, request a hot
603 * chaining cell.
604 */
605 if (isInvoke || curBB->needFallThroughBranch) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700606 lastBB->next = dvmCompilerNewBB(kChainingCellHot);
Ben Cheng6d576092009-09-01 17:01:58 -0700607 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700608 lastBB->next = dvmCompilerNewBB(kChainingCellNormal);
Ben Cheng6d576092009-09-01 17:01:58 -0700609 }
610 lastBB = lastBB->next;
611 lastBB->id = numBlocks++;
612 lastBB->startOffset = fallThroughOffset;
613 curBB->fallThrough = lastBB;
614 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700615 /* Target block not included in the trace */
Ben Cheng38329f52009-07-07 14:19:20 -0700616 if (curBB->taken == NULL &&
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800617 (isGoto(lastInsn) || isInvoke ||
618 (targetOffset != UNKNOWN_TARGET && targetOffset != curOffset))) {
Ben Cheng38329f52009-07-07 14:19:20 -0700619 BasicBlock *newBB;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700620 if (isInvoke) {
Ben Cheng38329f52009-07-07 14:19:20 -0700621 /* Monomorphic callee */
622 if (callee) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700623 newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton);
Ben Cheng38329f52009-07-07 14:19:20 -0700624 newBB->startOffset = 0;
625 newBB->containingMethod = callee;
626 /* Will resolve at runtime */
627 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700628 newBB = dvmCompilerNewBB(kChainingCellInvokePredicted);
Ben Cheng38329f52009-07-07 14:19:20 -0700629 newBB->startOffset = 0;
630 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700631 /* For unconditional branches, request a hot chaining cell */
632 } else {
Jeff Hao97319a82009-08-12 16:57:15 -0700633#if !defined(WITH_SELF_VERIFICATION)
Ben Cheng38329f52009-07-07 14:19:20 -0700634 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
Bill Buzbee1465db52009-09-23 17:17:35 -0700635 kChainingCellHot :
636 kChainingCellNormal);
Ben Cheng38329f52009-07-07 14:19:20 -0700637 newBB->startOffset = targetOffset;
Jeff Hao97319a82009-08-12 16:57:15 -0700638#else
639 /* Handle branches that branch back into the block */
640 if (targetOffset >= curBB->firstMIRInsn->offset &&
641 targetOffset <= curBB->lastMIRInsn->offset) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700642 newBB = dvmCompilerNewBB(kChainingCellBackwardBranch);
Jeff Hao97319a82009-08-12 16:57:15 -0700643 } else {
644 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 kChainingCellHot :
646 kChainingCellNormal);
Jeff Hao97319a82009-08-12 16:57:15 -0700647 }
648 newBB->startOffset = targetOffset;
649#endif
Ben Cheng1efc9c52009-06-08 18:25:27 -0700650 }
Ben Cheng38329f52009-07-07 14:19:20 -0700651 newBB->id = numBlocks++;
652 curBB->taken = newBB;
653 lastBB->next = newBB;
654 lastBB = newBB;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700655 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700656 }
657
658 /* Now create a special block to host PC reconstruction code */
Bill Buzbee1465db52009-09-23 17:17:35 -0700659 lastBB->next = dvmCompilerNewBB(kPCReconstruction);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700660 lastBB = lastBB->next;
661 lastBB->id = numBlocks++;
662
663 /* And one final block that publishes the PC and raise the exception */
Bill Buzbee1465db52009-09-23 17:17:35 -0700664 lastBB->next = dvmCompilerNewBB(kExceptionHandling);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700665 lastBB = lastBB->next;
666 lastBB->id = numBlocks++;
667
668 if (cUnit.printMe) {
669 LOGD("TRACEINFO (%d): 0x%08x %s%s 0x%x %d of %d, %d blocks",
Ben Chenge9695e52009-06-16 16:11:47 -0700670 compilationId,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700671 (intptr_t) desc->method->insns,
672 desc->method->clazz->descriptor,
673 desc->method->name,
674 desc->trace[0].frag.startOffset,
675 traceSize,
676 dexCode->insnsSize,
677 numBlocks);
678 }
679
680 BasicBlock **blockList;
681
682 cUnit.method = desc->method;
683 cUnit.traceDesc = desc;
684 cUnit.numBlocks = numBlocks;
685 dvmInitGrowableList(&cUnit.pcReconstructionList, 8);
686 blockList = cUnit.blockList =
687 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
688
689 int i;
690
691 for (i = 0, curBB = startBB; i < numBlocks; i++) {
692 blockList[i] = curBB;
693 curBB = curBB->next;
694 }
695 /* Make sure all blocks are added to the cUnit */
696 assert(curBB == NULL);
697
Ben Cheng4238ec22009-08-24 16:32:22 -0700698 /* Preparation for SSA conversion */
699 dvmInitializeSSAConversion(&cUnit);
700
Bill Buzbee1465db52009-09-23 17:17:35 -0700701
Ben Cheng4238ec22009-08-24 16:32:22 -0700702 if (cUnit.hasLoop) {
703 dvmCompilerLoopOpt(&cUnit);
704 }
705 else {
706 dvmCompilerNonLoopAnalysis(&cUnit);
707 }
708
Bill Buzbee1465db52009-09-23 17:17:35 -0700709 dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming
710
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 if (cUnit.printMe) {
712 dvmCompilerDumpCompilationUnit(&cUnit);
713 }
714
Bill Buzbee716f1202009-07-23 13:22:09 -0700715 /* Set the instruction set to use (NOTE: later components may change it) */
Ben Cheng5d90c202009-11-22 23:31:11 -0800716 cUnit.instructionSet = dvmCompilerInstructionSet();
Bill Buzbee716f1202009-07-23 13:22:09 -0700717
Bill Buzbee1465db52009-09-23 17:17:35 -0700718 /* Allocate Registers */
719 dvmCompilerRegAlloc(&cUnit);
720
Ben Chengba4fc8b2009-06-01 13:00:29 -0700721 /* Convert MIR to LIR, etc. */
722 dvmCompilerMIR2LIR(&cUnit);
723
Ben Cheng1efc9c52009-06-08 18:25:27 -0700724 /* Convert LIR into machine code. */
Bill Buzbee716f1202009-07-23 13:22:09 -0700725 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700726
727 if (cUnit.printMe) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700728 if (cUnit.halveInstCount) {
729 LOGD("Assembler aborted");
730 } else {
731 dvmCompilerCodegenDump(&cUnit);
732 }
733 LOGD("End %s%s, %d Dalvik instructions",
734 desc->method->clazz->descriptor, desc->method->name,
735 cUnit.numInsts);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700736 }
737
738 /* Reset the compiler resource pool */
739 dvmCompilerArenaReset();
740
Bill Buzbee716f1202009-07-23 13:22:09 -0700741 /* Success */
Ben Cheng4238ec22009-08-24 16:32:22 -0700742 if (!cUnit.halveInstCount) {
Ben Cheng8b258bf2009-06-24 17:27:07 -0700743 methodStats->nativeSize += cUnit.totalSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700744 return info->codeAddress != NULL;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700745
746 /* Halve the instruction count and retry again */
747 } else {
Bill Buzbee716f1202009-07-23 13:22:09 -0700748 return dvmCompileTrace(desc, cUnit.numInsts / 2, info);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700749 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750}
751
752/*
753 * Similar to dvmCompileTrace, but the entity processed here is the whole
754 * method.
755 *
756 * TODO: implementation will be revisited when the trace builder can provide
757 * whole-method traces.
758 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700759bool dvmCompileMethod(const Method *method, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700760{
761 const DexCode *dexCode = dvmGetMethodCode(method);
762 const u2 *codePtr = dexCode->insns;
763 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
764 int blockID = 0;
765 unsigned int curOffset = 0;
766
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 BasicBlock *firstBlock = dvmCompilerNewBB(kDalvikByteCode);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700768 firstBlock->id = blockID++;
769
770 /* Allocate the bit-vector to track the beginning of basic blocks */
Ben Cheng4238ec22009-08-24 16:32:22 -0700771 BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1,
772 false);
773 dvmCompilerSetBit(bbStartAddr, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700774
775 /*
776 * Sequentially go through every instruction first and put them in a single
777 * basic block. Identify block boundaries at the mean time.
778 */
779 while (codePtr < codeEnd) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700780 MIR *insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700781 insn->offset = curOffset;
782 int width = parseInsn(codePtr, &insn->dalvikInsn, false);
783 bool isInvoke = false;
784 const Method *callee;
785 insn->width = width;
786
Ben Cheng8b258bf2009-06-24 17:27:07 -0700787 /* Terminate when the data section is seen */
788 if (width == 0)
789 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790 dvmCompilerAppendMIR(firstBlock, insn);
791 /*
792 * Check whether this is a block ending instruction and whether it
793 * suggests the start of a new block
794 */
795 unsigned int target = curOffset;
796
797 /*
798 * If findBlockBoundary returns true, it means the current instruction
799 * is terminating the current block. If it is a branch, the target
800 * address will be recorded in target.
801 */
802 if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke,
803 &callee)) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700804 dvmCompilerSetBit(bbStartAddr, curOffset + width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700805 if (target != curOffset) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700806 dvmCompilerSetBit(bbStartAddr, target);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700807 }
808 }
809
810 codePtr += width;
811 /* each bit represents 16-bit quantity */
812 curOffset += width;
813 }
814
815 /*
816 * The number of blocks will be equal to the number of bits set to 1 in the
817 * bit vector minus 1, because the bit representing the location after the
818 * last instruction is set to one.
819 */
820 int numBlocks = dvmCountSetBits(bbStartAddr);
821 if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) {
822 numBlocks--;
823 }
824
825 CompilationUnit cUnit;
826 BasicBlock **blockList;
827
828 memset(&cUnit, 0, sizeof(CompilationUnit));
829 cUnit.method = method;
830 blockList = cUnit.blockList =
831 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
832
833 /*
834 * Register the first block onto the list and start split it into block
835 * boundaries from there.
836 */
837 blockList[0] = firstBlock;
838 cUnit.numBlocks = 1;
839
840 int i;
841 for (i = 0; i < numBlocks; i++) {
842 MIR *insn;
843 BasicBlock *curBB = blockList[i];
844 curOffset = curBB->lastMIRInsn->offset;
845
846 for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) {
847 /* Found the beginning of a new block, see if it is created yet */
848 if (dvmIsBitSet(bbStartAddr, insn->offset)) {
849 int j;
850 for (j = 0; j < cUnit.numBlocks; j++) {
851 if (blockList[j]->firstMIRInsn->offset == insn->offset)
852 break;
853 }
854
855 /* Block not split yet - do it now */
856 if (j == cUnit.numBlocks) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700857 BasicBlock *newBB = dvmCompilerNewBB(kDalvikByteCode);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700858 newBB->id = blockID++;
859 newBB->firstMIRInsn = insn;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700860 newBB->startOffset = insn->offset;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700861 newBB->lastMIRInsn = curBB->lastMIRInsn;
862 curBB->lastMIRInsn = insn->prev;
863 insn->prev->next = NULL;
864 insn->prev = NULL;
865
866 /*
867 * If the insn is not an unconditional branch, set up the
868 * fallthrough link.
869 */
870 if (!isUnconditionalBranch(curBB->lastMIRInsn)) {
871 curBB->fallThrough = newBB;
872 }
873
874 /* enqueue the new block */
875 blockList[cUnit.numBlocks++] = newBB;
876 break;
877 }
878 }
879 }
880 }
881
882 if (numBlocks != cUnit.numBlocks) {
883 LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit.numBlocks);
884 dvmAbort();
885 }
886
Ben Chengba4fc8b2009-06-01 13:00:29 -0700887 /* Connect the basic blocks through the taken links */
888 for (i = 0; i < numBlocks; i++) {
889 BasicBlock *curBB = blockList[i];
890 MIR *insn = curBB->lastMIRInsn;
891 unsigned int target = insn->offset;
892 bool isInvoke;
893 const Method *callee;
894
895 findBlockBoundary(method, insn, target, &target, &isInvoke, &callee);
896
897 /* Found a block ended on a branch */
898 if (target != insn->offset) {
899 int j;
900 /* Forward branch */
901 if (target > insn->offset) {
902 j = i + 1;
903 } else {
904 /* Backward branch */
905 j = 0;
906 }
907 for (; j < numBlocks; j++) {
908 if (blockList[j]->firstMIRInsn->offset == target) {
909 curBB->taken = blockList[j];
910 break;
911 }
912 }
913
Ben Cheng8b258bf2009-06-24 17:27:07 -0700914 /* Don't create dummy block for the callee yet */
915 if (j == numBlocks && !isInvoke) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 LOGE("Target not found for insn %x: expect target %x\n",
917 curBB->lastMIRInsn->offset, target);
918 dvmAbort();
919 }
920 }
921 }
922
Bill Buzbee716f1202009-07-23 13:22:09 -0700923 /* Set the instruction set to use (NOTE: later components may change it) */
Ben Cheng5d90c202009-11-22 23:31:11 -0800924 cUnit.instructionSet = dvmCompilerInstructionSet();
Bill Buzbee716f1202009-07-23 13:22:09 -0700925
Ben Chengba4fc8b2009-06-01 13:00:29 -0700926 dvmCompilerMIR2LIR(&cUnit);
927
Bill Buzbee716f1202009-07-23 13:22:09 -0700928 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700929
930 dvmCompilerDumpCompilationUnit(&cUnit);
931
932 dvmCompilerArenaReset();
933
Bill Buzbee716f1202009-07-23 13:22:09 -0700934 return info->codeAddress != NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700935}