blob: 0c8a8df0e7f5d5079f9f0aff45386b268fc1143e [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
Ben Cheng1357e942010-02-10 17:21:39 -0800194#if defined(WITH_JIT_TUNING)
Ben Cheng8b258bf2009-06-24 17:27:07 -0700195/*
196 * Analyze each method whose traces are ever compiled. Collect a variety of
197 * statistics like the ratio of exercised vs overall code and code bloat
198 * ratios.
199 */
200static CompilerMethodStats *analyzeMethodBody(const Method *method)
201{
202 const DexCode *dexCode = dvmGetMethodCode(method);
203 const u2 *codePtr = dexCode->insns;
204 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
205 int insnSize = 0;
206 int hashValue = dvmComputeUtf8Hash(method->name);
207
208 CompilerMethodStats dummyMethodEntry; // For hash table lookup
209 CompilerMethodStats *realMethodEntry; // For hash table storage
210
211 /* For lookup only */
212 dummyMethodEntry.method = method;
213 realMethodEntry = dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
214 &dummyMethodEntry,
215 (HashCompareFunc) compareMethod,
216 false);
217
218 /* Part of this method has been compiled before - just return the entry */
219 if (realMethodEntry != NULL) {
220 return realMethodEntry;
221 }
222
223 /*
224 * First time to compile this method - set up a new entry in the hash table
225 */
226 realMethodEntry =
227 (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats));
228 realMethodEntry->method = method;
229
230 dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
231 realMethodEntry,
232 (HashCompareFunc) compareMethod,
233 true);
234
235 /* Count the number of instructions */
236 while (codePtr < codeEnd) {
237 DecodedInstruction dalvikInsn;
238 int width = parseInsn(codePtr, &dalvikInsn, false);
239
240 /* Terminate when the data section is seen */
241 if (width == 0)
242 break;
243
244 insnSize += width;
245 codePtr += width;
246 }
247
248 realMethodEntry->dalvikSize = insnSize * 2;
249 return realMethodEntry;
250}
Ben Cheng1357e942010-02-10 17:21:39 -0800251#endif
Ben Cheng8b258bf2009-06-24 17:27:07 -0700252
253/*
Ben Cheng33672452010-01-12 14:59:30 -0800254 * Crawl the stack of the thread that requesed compilation to see if any of the
255 * ancestors are on the blacklist.
256 */
257bool filterMethodByCallGraph(Thread *thread, const char *curMethodName)
258{
259 /* Crawl the Dalvik stack frames and compare the method name*/
260 StackSaveArea *ssaPtr = ((StackSaveArea *) thread->curFrame) - 1;
261 while (ssaPtr != ((StackSaveArea *) NULL) - 1) {
262 const Method *method = ssaPtr->method;
263 if (method) {
264 int hashValue = dvmComputeUtf8Hash(method->name);
265 bool found =
266 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
267 (char *) method->name,
268 (HashCompareFunc) strcmp, false) !=
269 NULL;
270 if (found) {
271 LOGD("Method %s (--> %s) found on the JIT %s list",
272 method->name, curMethodName,
273 gDvmJit.includeSelectedMethod ? "white" : "black");
274 return true;
275 }
276
277 }
278 ssaPtr = ((StackSaveArea *) ssaPtr->prevFrame) - 1;
279 };
280 return false;
281}
282
283/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700284 * Main entry point to start trace compilation. Basic blocks are constructed
285 * first and they will be passed to the codegen routines to convert Dalvik
286 * bytecode into machine code.
287 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700288bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts,
289 JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700290{
291 const DexCode *dexCode = dvmGetMethodCode(desc->method);
292 const JitTraceRun* currRun = &desc->trace[0];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700293 unsigned int curOffset = currRun->frag.startOffset;
294 unsigned int numInsts = currRun->frag.numInsts;
295 const u2 *codePtr = dexCode->insns + curOffset;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700296 int traceSize = 0; // # of half-words
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297 const u2 *startCodePtr = codePtr;
298 BasicBlock *startBB, *curBB, *lastBB;
299 int numBlocks = 0;
300 static int compilationId;
301 CompilationUnit cUnit;
Ben Cheng1357e942010-02-10 17:21:39 -0800302#if defined(WITH_JIT_TUNING)
Ben Cheng8b258bf2009-06-24 17:27:07 -0700303 CompilerMethodStats *methodStats;
Ben Cheng1357e942010-02-10 17:21:39 -0800304#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700305
Bill Buzbee964a7b02010-01-28 12:54:19 -0800306 /* If we've already compiled this trace, just return success */
jeffhao9e45c0b2010-02-03 10:24:05 -0800307 if (dvmJitGetCodeAddr(startCodePtr) && !info->discardResult) {
Bill Buzbee964a7b02010-01-28 12:54:19 -0800308 return true;
309 }
310
Ben Chenge9695e52009-06-16 16:11:47 -0700311 compilationId++;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700312 memset(&cUnit, 0, sizeof(CompilationUnit));
313
Ben Cheng1357e942010-02-10 17:21:39 -0800314#if defined(WITH_JIT_TUNING)
Ben Cheng8b258bf2009-06-24 17:27:07 -0700315 /* Locate the entry to store compilation statistics for this method */
316 methodStats = analyzeMethodBody(desc->method);
Ben Cheng1357e942010-02-10 17:21:39 -0800317#endif
Ben Chenge9695e52009-06-16 16:11:47 -0700318
Ben Chengba4fc8b2009-06-01 13:00:29 -0700319 /* Initialize the printMe flag */
320 cUnit.printMe = gDvmJit.printMe;
321
Bill Buzbee6e963e12009-06-17 16:56:19 -0700322 /* Initialize the profile flag */
323 cUnit.executionCount = gDvmJit.profile;
324
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325 /* Identify traces that we don't want to compile */
326 if (gDvmJit.methodTable) {
327 int len = strlen(desc->method->clazz->descriptor) +
328 strlen(desc->method->name) + 1;
329 char *fullSignature = dvmCompilerNew(len, true);
330 strcpy(fullSignature, desc->method->clazz->descriptor);
331 strcat(fullSignature, desc->method->name);
332
333 int hashValue = dvmComputeUtf8Hash(fullSignature);
334
335 /*
336 * Doing three levels of screening to see whether we want to skip
337 * compiling this method
338 */
339
340 /* First, check the full "class;method" signature */
341 bool methodFound =
342 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
343 fullSignature, (HashCompareFunc) strcmp,
344 false) !=
345 NULL;
346
347 /* Full signature not found - check the enclosing class */
348 if (methodFound == false) {
349 int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor);
350 methodFound =
351 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
352 (char *) desc->method->clazz->descriptor,
353 (HashCompareFunc) strcmp, false) !=
354 NULL;
355 /* Enclosing class not found - check the method name */
356 if (methodFound == false) {
357 int hashValue = dvmComputeUtf8Hash(desc->method->name);
358 methodFound =
359 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
360 (char *) desc->method->name,
361 (HashCompareFunc) strcmp, false) !=
362 NULL;
Ben Cheng33672452010-01-12 14:59:30 -0800363
364 /*
365 * Debug by call-graph is enabled. Check if the debug list
366 * covers any methods on the VM stack.
367 */
368 if (methodFound == false && gDvmJit.checkCallGraph == true) {
369 methodFound =
370 filterMethodByCallGraph(info->requestingThread,
371 desc->method->name);
372 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700373 }
374 }
375
376 /*
377 * Under the following conditions, the trace will be *conservatively*
378 * compiled by only containing single-step instructions to and from the
379 * interpreter.
380 * 1) If includeSelectedMethod == false, the method matches the full or
381 * partial signature stored in the hash table.
382 *
383 * 2) If includeSelectedMethod == true, the method does not match the
384 * full and partial signature stored in the hash table.
385 */
386 if (gDvmJit.includeSelectedMethod != methodFound) {
387 cUnit.allSingleStep = true;
388 } else {
389 /* Compile the trace as normal */
390
391 /* Print the method we cherry picked */
392 if (gDvmJit.includeSelectedMethod == true) {
393 cUnit.printMe = true;
394 }
395 }
396 }
397
Ben Cheng4238ec22009-08-24 16:32:22 -0700398 /* Allocate the entry block */
Bill Buzbee1465db52009-09-23 17:17:35 -0700399 lastBB = startBB = curBB = dvmCompilerNewBB(kEntryBlock);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400 curBB->startOffset = curOffset;
401 curBB->id = numBlocks++;
402
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 curBB = dvmCompilerNewBB(kDalvikByteCode);
Ben Cheng4238ec22009-08-24 16:32:22 -0700404 curBB->startOffset = curOffset;
405 curBB->id = numBlocks++;
406
407 /* Make the first real dalvik block the fallthrough of the entry block */
408 startBB->fallThrough = curBB;
409 lastBB->next = curBB;
410 lastBB = curBB;
411
Ben Chengba4fc8b2009-06-01 13:00:29 -0700412 if (cUnit.printMe) {
413 LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n",
414 desc->method->name, curOffset);
415 }
416
Ben Cheng1efc9c52009-06-08 18:25:27 -0700417 /*
418 * Analyze the trace descriptor and include up to the maximal number
419 * of Dalvik instructions into the IR.
420 */
421 while (1) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700422 MIR *insn;
423 int width;
Ben Cheng4238ec22009-08-24 16:32:22 -0700424 insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700425 insn->offset = curOffset;
426 width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700427
428 /* The trace should never incude instruction data */
429 assert(width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700430 insn->width = width;
431 traceSize += width;
432 dvmCompilerAppendMIR(curBB, insn);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700433 cUnit.numInsts++;
434 /* Instruction limit reached - terminate the trace here */
435 if (cUnit.numInsts >= numMaxInsts) {
436 break;
437 }
438 if (--numInsts == 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700439 if (currRun->frag.runEnd) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700440 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700441 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700442 curBB = dvmCompilerNewBB(kDalvikByteCode);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700443 lastBB->next = curBB;
444 lastBB = curBB;
445 curBB->id = numBlocks++;
446 currRun++;
447 curOffset = currRun->frag.startOffset;
448 numInsts = currRun->frag.numInsts;
449 curBB->startOffset = curOffset;
450 codePtr = dexCode->insns + curOffset;
451 }
452 } else {
453 curOffset += width;
454 codePtr += width;
455 }
456 }
457
Ben Cheng1357e942010-02-10 17:21:39 -0800458#if defined(WITH_JIT_TUNING)
Ben Cheng8b258bf2009-06-24 17:27:07 -0700459 /* Convert # of half-word to bytes */
460 methodStats->compiledDalvikSize += traceSize * 2;
Ben Cheng1357e942010-02-10 17:21:39 -0800461#endif
Ben Cheng8b258bf2009-06-24 17:27:07 -0700462
Ben Chengba4fc8b2009-06-01 13:00:29 -0700463 /*
464 * Now scan basic blocks containing real code to connect the
465 * taken/fallthrough links. Also create chaining cells for code not included
466 * in the trace.
467 */
468 for (curBB = startBB; curBB; curBB = curBB->next) {
469 MIR *lastInsn = curBB->lastMIRInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -0700470 /* Skip empty blocks */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700471 if (lastInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700472 continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700473 }
474 curOffset = lastInsn->offset;
475 unsigned int targetOffset = curOffset;
476 unsigned int fallThroughOffset = curOffset + lastInsn->width;
477 bool isInvoke = false;
478 const Method *callee = NULL;
479
480 findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset,
481 &targetOffset, &isInvoke, &callee);
482
483 /* Link the taken and fallthrough blocks */
484 BasicBlock *searchBB;
485
486 /* No backward branch in the trace - start searching the next BB */
487 for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) {
488 if (targetOffset == searchBB->startOffset) {
489 curBB->taken = searchBB;
490 }
491 if (fallThroughOffset == searchBB->startOffset) {
492 curBB->fallThrough = searchBB;
493 }
494 }
495
Ben Cheng1efc9c52009-06-08 18:25:27 -0700496 int flags = dexGetInstrFlags(gDvm.instrFlags,
497 lastInsn->dalvikInsn.opCode);
498
499 /*
500 * Some blocks are ended by non-control-flow-change instructions,
501 * currently only due to trace length constraint. In this case we need
502 * to generate an explicit branch at the end of the block to jump to
503 * the chaining cell.
Ben Cheng17f15ce2009-07-27 16:21:52 -0700504 *
505 * NOTE: INVOKE_DIRECT_EMPTY is actually not an invoke but a nop
Ben Cheng1efc9c52009-06-08 18:25:27 -0700506 */
507 curBB->needFallThroughBranch =
Ben Cheng17f15ce2009-07-27 16:21:52 -0700508 ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
509 kInstrInvoke)) == 0) ||
510 (lastInsn->dalvikInsn.opCode == OP_INVOKE_DIRECT_EMPTY);
511
Ben Cheng4238ec22009-08-24 16:32:22 -0700512 if (curBB->taken == NULL &&
513 curBB->fallThrough == NULL &&
514 flags == (kInstrCanBranch | kInstrCanContinue) &&
515 fallThroughOffset == startBB->startOffset) {
Ben Cheng0fd31e42009-09-03 14:40:16 -0700516 BasicBlock *loopBranch = curBB;
517 BasicBlock *exitBB;
518 BasicBlock *exitChainingCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700519
520 if (cUnit.printMe) {
521 LOGD("Natural loop detected!");
522 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700523 exitBB = dvmCompilerNewBB(kExitBlock);
Ben Cheng0fd31e42009-09-03 14:40:16 -0700524 lastBB->next = exitBB;
525 lastBB = exitBB;
Ben Cheng4238ec22009-08-24 16:32:22 -0700526
Ben Cheng0fd31e42009-09-03 14:40:16 -0700527 exitBB->startOffset = targetOffset;
528 exitBB->id = numBlocks++;
529 exitBB->needFallThroughBranch = true;
Ben Cheng4238ec22009-08-24 16:32:22 -0700530
Ben Cheng0fd31e42009-09-03 14:40:16 -0700531 loopBranch->taken = exitBB;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700532#if defined(WITH_SELF_VERIFICATION)
Ben Cheng0fd31e42009-09-03 14:40:16 -0700533 BasicBlock *backwardCell =
Bill Buzbee1465db52009-09-23 17:17:35 -0700534 dvmCompilerNewBB(kChainingCellBackwardBranch);
Ben Cheng0fd31e42009-09-03 14:40:16 -0700535 lastBB->next = backwardCell;
536 lastBB = backwardCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700537
Ben Cheng0fd31e42009-09-03 14:40:16 -0700538 backwardCell->startOffset = startBB->startOffset;
539 backwardCell->id = numBlocks++;
540 loopBranch->fallThrough = backwardCell;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700541#elif defined(WITH_JIT_TUNING)
542 if (gDvmJit.profile) {
543 BasicBlock *backwardCell =
Bill Buzbee1465db52009-09-23 17:17:35 -0700544 dvmCompilerNewBB(kChainingCellBackwardBranch);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700545 lastBB->next = backwardCell;
546 lastBB = backwardCell;
547
548 backwardCell->startOffset = startBB->startOffset;
549 backwardCell->id = numBlocks++;
550 loopBranch->fallThrough = backwardCell;
551 } else {
552 loopBranch->fallThrough = startBB->next;
553 }
554#else
555 loopBranch->fallThrough = startBB->next;
Ben Cheng0fd31e42009-09-03 14:40:16 -0700556#endif
557
558 /* Create the chaining cell as the fallthrough of the exit block */
Bill Buzbee1465db52009-09-23 17:17:35 -0700559 exitChainingCell = dvmCompilerNewBB(kChainingCellNormal);
Ben Cheng0fd31e42009-09-03 14:40:16 -0700560 lastBB->next = exitChainingCell;
561 lastBB = exitChainingCell;
562
563 exitChainingCell->startOffset = targetOffset;
564 exitChainingCell->id = numBlocks++;
565
566 exitBB->fallThrough = exitChainingCell;
567
Ben Cheng4238ec22009-08-24 16:32:22 -0700568 cUnit.hasLoop = true;
569 }
570
Ben Cheng6c10a972009-10-29 14:39:18 -0700571 if (lastInsn->dalvikInsn.opCode == OP_PACKED_SWITCH ||
572 lastInsn->dalvikInsn.opCode == OP_SPARSE_SWITCH) {
573 int i;
574 const u2 *switchData = desc->method->insns + lastInsn->offset +
575 lastInsn->dalvikInsn.vB;
576 int size = switchData[1];
577 int maxChains = MIN(size, MAX_CHAINED_SWITCH_CASES);
578
579 /*
580 * Generate the landing pad for cases whose ranks are higher than
581 * MAX_CHAINED_SWITCH_CASES. The code will re-enter the interpreter
582 * through the NoChain point.
583 */
584 if (maxChains != size) {
585 cUnit.switchOverflowPad =
586 desc->method->insns + lastInsn->offset;
587 }
588
589 s4 *targets = (s4 *) (switchData + 2 +
590 (lastInsn->dalvikInsn.opCode == OP_PACKED_SWITCH ?
591 2 : size * 2));
592
593 /* One chaining cell for the first MAX_CHAINED_SWITCH_CASES cases */
594 for (i = 0; i < maxChains; i++) {
595 BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal);
596 lastBB->next = caseChain;
597 lastBB = caseChain;
598
599 caseChain->startOffset = lastInsn->offset + targets[i];
600 caseChain->id = numBlocks++;
601 }
602
603 /* One more chaining cell for the default case */
604 BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal);
605 lastBB->next = caseChain;
606 lastBB = caseChain;
607
608 caseChain->startOffset = lastInsn->offset + lastInsn->width;
609 caseChain->id = numBlocks++;
Ben Cheng6d576092009-09-01 17:01:58 -0700610 /* Fallthrough block not included in the trace */
Ben Cheng6c10a972009-10-29 14:39:18 -0700611 } else if (!isUnconditionalBranch(lastInsn) &&
612 curBB->fallThrough == NULL) {
Ben Cheng6d576092009-09-01 17:01:58 -0700613 /*
614 * If the chaining cell is after an invoke or
615 * instruction that cannot change the control flow, request a hot
616 * chaining cell.
617 */
618 if (isInvoke || curBB->needFallThroughBranch) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700619 lastBB->next = dvmCompilerNewBB(kChainingCellHot);
Ben Cheng6d576092009-09-01 17:01:58 -0700620 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700621 lastBB->next = dvmCompilerNewBB(kChainingCellNormal);
Ben Cheng6d576092009-09-01 17:01:58 -0700622 }
623 lastBB = lastBB->next;
624 lastBB->id = numBlocks++;
625 lastBB->startOffset = fallThroughOffset;
626 curBB->fallThrough = lastBB;
627 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700628 /* Target block not included in the trace */
Ben Cheng38329f52009-07-07 14:19:20 -0700629 if (curBB->taken == NULL &&
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800630 (isGoto(lastInsn) || isInvoke ||
631 (targetOffset != UNKNOWN_TARGET && targetOffset != curOffset))) {
Ben Cheng38329f52009-07-07 14:19:20 -0700632 BasicBlock *newBB;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700633 if (isInvoke) {
Ben Cheng38329f52009-07-07 14:19:20 -0700634 /* Monomorphic callee */
635 if (callee) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton);
Ben Cheng38329f52009-07-07 14:19:20 -0700637 newBB->startOffset = 0;
638 newBB->containingMethod = callee;
639 /* Will resolve at runtime */
640 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700641 newBB = dvmCompilerNewBB(kChainingCellInvokePredicted);
Ben Cheng38329f52009-07-07 14:19:20 -0700642 newBB->startOffset = 0;
643 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700644 /* For unconditional branches, request a hot chaining cell */
645 } else {
Jeff Hao97319a82009-08-12 16:57:15 -0700646#if !defined(WITH_SELF_VERIFICATION)
Ben Cheng38329f52009-07-07 14:19:20 -0700647 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
Bill Buzbee1465db52009-09-23 17:17:35 -0700648 kChainingCellHot :
649 kChainingCellNormal);
Ben Cheng38329f52009-07-07 14:19:20 -0700650 newBB->startOffset = targetOffset;
Jeff Hao97319a82009-08-12 16:57:15 -0700651#else
652 /* Handle branches that branch back into the block */
653 if (targetOffset >= curBB->firstMIRInsn->offset &&
654 targetOffset <= curBB->lastMIRInsn->offset) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700655 newBB = dvmCompilerNewBB(kChainingCellBackwardBranch);
Jeff Hao97319a82009-08-12 16:57:15 -0700656 } else {
657 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
Bill Buzbee1465db52009-09-23 17:17:35 -0700658 kChainingCellHot :
659 kChainingCellNormal);
Jeff Hao97319a82009-08-12 16:57:15 -0700660 }
661 newBB->startOffset = targetOffset;
662#endif
Ben Cheng1efc9c52009-06-08 18:25:27 -0700663 }
Ben Cheng38329f52009-07-07 14:19:20 -0700664 newBB->id = numBlocks++;
665 curBB->taken = newBB;
666 lastBB->next = newBB;
667 lastBB = newBB;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700668 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669 }
670
671 /* Now create a special block to host PC reconstruction code */
Bill Buzbee1465db52009-09-23 17:17:35 -0700672 lastBB->next = dvmCompilerNewBB(kPCReconstruction);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700673 lastBB = lastBB->next;
674 lastBB->id = numBlocks++;
675
676 /* And one final block that publishes the PC and raise the exception */
Bill Buzbee1465db52009-09-23 17:17:35 -0700677 lastBB->next = dvmCompilerNewBB(kExceptionHandling);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 lastBB = lastBB->next;
679 lastBB->id = numBlocks++;
680
681 if (cUnit.printMe) {
682 LOGD("TRACEINFO (%d): 0x%08x %s%s 0x%x %d of %d, %d blocks",
Ben Chenge9695e52009-06-16 16:11:47 -0700683 compilationId,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700684 (intptr_t) desc->method->insns,
685 desc->method->clazz->descriptor,
686 desc->method->name,
687 desc->trace[0].frag.startOffset,
688 traceSize,
689 dexCode->insnsSize,
690 numBlocks);
691 }
692
693 BasicBlock **blockList;
694
695 cUnit.method = desc->method;
696 cUnit.traceDesc = desc;
697 cUnit.numBlocks = numBlocks;
698 dvmInitGrowableList(&cUnit.pcReconstructionList, 8);
699 blockList = cUnit.blockList =
700 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
701
702 int i;
703
704 for (i = 0, curBB = startBB; i < numBlocks; i++) {
705 blockList[i] = curBB;
706 curBB = curBB->next;
707 }
708 /* Make sure all blocks are added to the cUnit */
709 assert(curBB == NULL);
710
Ben Cheng4238ec22009-08-24 16:32:22 -0700711 /* Preparation for SSA conversion */
712 dvmInitializeSSAConversion(&cUnit);
713
Bill Buzbee1465db52009-09-23 17:17:35 -0700714
Ben Cheng4238ec22009-08-24 16:32:22 -0700715 if (cUnit.hasLoop) {
716 dvmCompilerLoopOpt(&cUnit);
717 }
718 else {
719 dvmCompilerNonLoopAnalysis(&cUnit);
720 }
721
Bill Buzbee1465db52009-09-23 17:17:35 -0700722 dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming
723
Ben Chengba4fc8b2009-06-01 13:00:29 -0700724 if (cUnit.printMe) {
725 dvmCompilerDumpCompilationUnit(&cUnit);
726 }
727
Bill Buzbee716f1202009-07-23 13:22:09 -0700728 /* Set the instruction set to use (NOTE: later components may change it) */
Ben Cheng5d90c202009-11-22 23:31:11 -0800729 cUnit.instructionSet = dvmCompilerInstructionSet();
Bill Buzbee716f1202009-07-23 13:22:09 -0700730
Bill Buzbee1465db52009-09-23 17:17:35 -0700731 /* Allocate Registers */
732 dvmCompilerRegAlloc(&cUnit);
733
Ben Chengba4fc8b2009-06-01 13:00:29 -0700734 /* Convert MIR to LIR, etc. */
735 dvmCompilerMIR2LIR(&cUnit);
736
Ben Cheng1efc9c52009-06-08 18:25:27 -0700737 /* Convert LIR into machine code. */
Bill Buzbee716f1202009-07-23 13:22:09 -0700738 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700739
740 if (cUnit.printMe) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700741 if (cUnit.halveInstCount) {
742 LOGD("Assembler aborted");
743 } else {
744 dvmCompilerCodegenDump(&cUnit);
745 }
746 LOGD("End %s%s, %d Dalvik instructions",
747 desc->method->clazz->descriptor, desc->method->name,
748 cUnit.numInsts);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700749 }
750
751 /* Reset the compiler resource pool */
752 dvmCompilerArenaReset();
753
Bill Buzbee716f1202009-07-23 13:22:09 -0700754 /* Success */
Ben Cheng4238ec22009-08-24 16:32:22 -0700755 if (!cUnit.halveInstCount) {
Ben Cheng1357e942010-02-10 17:21:39 -0800756#if defined(WITH_JIT_TUNING)
Ben Cheng8b258bf2009-06-24 17:27:07 -0700757 methodStats->nativeSize += cUnit.totalSize;
Ben Cheng1357e942010-02-10 17:21:39 -0800758#endif
Bill Buzbee716f1202009-07-23 13:22:09 -0700759 return info->codeAddress != NULL;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700760
761 /* Halve the instruction count and retry again */
762 } else {
Bill Buzbee716f1202009-07-23 13:22:09 -0700763 return dvmCompileTrace(desc, cUnit.numInsts / 2, info);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700764 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700765}
766
767/*
768 * Similar to dvmCompileTrace, but the entity processed here is the whole
769 * method.
770 *
771 * TODO: implementation will be revisited when the trace builder can provide
772 * whole-method traces.
773 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700774bool dvmCompileMethod(const Method *method, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775{
776 const DexCode *dexCode = dvmGetMethodCode(method);
777 const u2 *codePtr = dexCode->insns;
778 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
779 int blockID = 0;
780 unsigned int curOffset = 0;
781
Bill Buzbee1465db52009-09-23 17:17:35 -0700782 BasicBlock *firstBlock = dvmCompilerNewBB(kDalvikByteCode);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700783 firstBlock->id = blockID++;
784
785 /* Allocate the bit-vector to track the beginning of basic blocks */
Ben Cheng4238ec22009-08-24 16:32:22 -0700786 BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1,
787 false);
788 dvmCompilerSetBit(bbStartAddr, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700789
790 /*
791 * Sequentially go through every instruction first and put them in a single
792 * basic block. Identify block boundaries at the mean time.
793 */
794 while (codePtr < codeEnd) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700795 MIR *insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 insn->offset = curOffset;
797 int width = parseInsn(codePtr, &insn->dalvikInsn, false);
798 bool isInvoke = false;
799 const Method *callee;
800 insn->width = width;
801
Ben Cheng8b258bf2009-06-24 17:27:07 -0700802 /* Terminate when the data section is seen */
803 if (width == 0)
804 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700805 dvmCompilerAppendMIR(firstBlock, insn);
806 /*
807 * Check whether this is a block ending instruction and whether it
808 * suggests the start of a new block
809 */
810 unsigned int target = curOffset;
811
812 /*
813 * If findBlockBoundary returns true, it means the current instruction
814 * is terminating the current block. If it is a branch, the target
815 * address will be recorded in target.
816 */
817 if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke,
818 &callee)) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700819 dvmCompilerSetBit(bbStartAddr, curOffset + width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700820 if (target != curOffset) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700821 dvmCompilerSetBit(bbStartAddr, target);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700822 }
823 }
824
825 codePtr += width;
826 /* each bit represents 16-bit quantity */
827 curOffset += width;
828 }
829
830 /*
831 * The number of blocks will be equal to the number of bits set to 1 in the
832 * bit vector minus 1, because the bit representing the location after the
833 * last instruction is set to one.
834 */
835 int numBlocks = dvmCountSetBits(bbStartAddr);
836 if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) {
837 numBlocks--;
838 }
839
840 CompilationUnit cUnit;
841 BasicBlock **blockList;
842
843 memset(&cUnit, 0, sizeof(CompilationUnit));
844 cUnit.method = method;
845 blockList = cUnit.blockList =
846 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
847
848 /*
849 * Register the first block onto the list and start split it into block
850 * boundaries from there.
851 */
852 blockList[0] = firstBlock;
853 cUnit.numBlocks = 1;
854
855 int i;
856 for (i = 0; i < numBlocks; i++) {
857 MIR *insn;
858 BasicBlock *curBB = blockList[i];
859 curOffset = curBB->lastMIRInsn->offset;
860
861 for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) {
862 /* Found the beginning of a new block, see if it is created yet */
863 if (dvmIsBitSet(bbStartAddr, insn->offset)) {
864 int j;
865 for (j = 0; j < cUnit.numBlocks; j++) {
866 if (blockList[j]->firstMIRInsn->offset == insn->offset)
867 break;
868 }
869
870 /* Block not split yet - do it now */
871 if (j == cUnit.numBlocks) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700872 BasicBlock *newBB = dvmCompilerNewBB(kDalvikByteCode);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700873 newBB->id = blockID++;
874 newBB->firstMIRInsn = insn;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700875 newBB->startOffset = insn->offset;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700876 newBB->lastMIRInsn = curBB->lastMIRInsn;
877 curBB->lastMIRInsn = insn->prev;
878 insn->prev->next = NULL;
879 insn->prev = NULL;
880
881 /*
882 * If the insn is not an unconditional branch, set up the
883 * fallthrough link.
884 */
885 if (!isUnconditionalBranch(curBB->lastMIRInsn)) {
886 curBB->fallThrough = newBB;
887 }
888
889 /* enqueue the new block */
890 blockList[cUnit.numBlocks++] = newBB;
891 break;
892 }
893 }
894 }
895 }
896
897 if (numBlocks != cUnit.numBlocks) {
898 LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit.numBlocks);
899 dvmAbort();
900 }
901
Ben Chengba4fc8b2009-06-01 13:00:29 -0700902 /* Connect the basic blocks through the taken links */
903 for (i = 0; i < numBlocks; i++) {
904 BasicBlock *curBB = blockList[i];
905 MIR *insn = curBB->lastMIRInsn;
906 unsigned int target = insn->offset;
907 bool isInvoke;
908 const Method *callee;
909
910 findBlockBoundary(method, insn, target, &target, &isInvoke, &callee);
911
912 /* Found a block ended on a branch */
913 if (target != insn->offset) {
914 int j;
915 /* Forward branch */
916 if (target > insn->offset) {
917 j = i + 1;
918 } else {
919 /* Backward branch */
920 j = 0;
921 }
922 for (; j < numBlocks; j++) {
923 if (blockList[j]->firstMIRInsn->offset == target) {
924 curBB->taken = blockList[j];
925 break;
926 }
927 }
928
Ben Cheng8b258bf2009-06-24 17:27:07 -0700929 /* Don't create dummy block for the callee yet */
930 if (j == numBlocks && !isInvoke) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931 LOGE("Target not found for insn %x: expect target %x\n",
932 curBB->lastMIRInsn->offset, target);
933 dvmAbort();
934 }
935 }
936 }
937
Bill Buzbee716f1202009-07-23 13:22:09 -0700938 /* Set the instruction set to use (NOTE: later components may change it) */
Ben Cheng5d90c202009-11-22 23:31:11 -0800939 cUnit.instructionSet = dvmCompilerInstructionSet();
Bill Buzbee716f1202009-07-23 13:22:09 -0700940
Ben Chengba4fc8b2009-06-01 13:00:29 -0700941 dvmCompilerMIR2LIR(&cUnit);
942
Bill Buzbee716f1202009-07-23 13:22:09 -0700943 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700944
945 dvmCompilerDumpCompilationUnit(&cUnit);
946
947 dvmCompilerArenaReset();
948
Bill Buzbee716f1202009-07-23 13:22:09 -0700949 return info->codeAddress != NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700950}