blob: 959c99ac175711a6bfaa8c020b1a5d0c63ebe266 [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
157/*
158 * Identify conditional branch instructions
159 */
160static inline bool isUnconditionalBranch(MIR *insn)
161{
162 switch (insn->dalvikInsn.opCode) {
163 case OP_RETURN_VOID:
164 case OP_RETURN:
165 case OP_RETURN_WIDE:
166 case OP_RETURN_OBJECT:
167 case OP_GOTO:
168 case OP_GOTO_16:
169 case OP_GOTO_32:
170 return true;
171 default:
172 return false;
173 }
174}
175
176/*
Ben Cheng8b258bf2009-06-24 17:27:07 -0700177 * dvmHashTableLookup() callback
178 */
179static int compareMethod(const CompilerMethodStats *m1,
180 const CompilerMethodStats *m2)
181{
182 return (int) m1->method - (int) m2->method;
183}
184
185/*
186 * Analyze each method whose traces are ever compiled. Collect a variety of
187 * statistics like the ratio of exercised vs overall code and code bloat
188 * ratios.
189 */
190static CompilerMethodStats *analyzeMethodBody(const Method *method)
191{
192 const DexCode *dexCode = dvmGetMethodCode(method);
193 const u2 *codePtr = dexCode->insns;
194 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
195 int insnSize = 0;
196 int hashValue = dvmComputeUtf8Hash(method->name);
197
198 CompilerMethodStats dummyMethodEntry; // For hash table lookup
199 CompilerMethodStats *realMethodEntry; // For hash table storage
200
201 /* For lookup only */
202 dummyMethodEntry.method = method;
203 realMethodEntry = dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
204 &dummyMethodEntry,
205 (HashCompareFunc) compareMethod,
206 false);
207
208 /* Part of this method has been compiled before - just return the entry */
209 if (realMethodEntry != NULL) {
210 return realMethodEntry;
211 }
212
213 /*
214 * First time to compile this method - set up a new entry in the hash table
215 */
216 realMethodEntry =
217 (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats));
218 realMethodEntry->method = method;
219
220 dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
221 realMethodEntry,
222 (HashCompareFunc) compareMethod,
223 true);
224
225 /* Count the number of instructions */
226 while (codePtr < codeEnd) {
227 DecodedInstruction dalvikInsn;
228 int width = parseInsn(codePtr, &dalvikInsn, false);
229
230 /* Terminate when the data section is seen */
231 if (width == 0)
232 break;
233
234 insnSize += width;
235 codePtr += width;
236 }
237
238 realMethodEntry->dalvikSize = insnSize * 2;
239 return realMethodEntry;
240}
241
242/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700243 * Main entry point to start trace compilation. Basic blocks are constructed
244 * first and they will be passed to the codegen routines to convert Dalvik
245 * bytecode into machine code.
246 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700247bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts,
248 JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700249{
250 const DexCode *dexCode = dvmGetMethodCode(desc->method);
251 const JitTraceRun* currRun = &desc->trace[0];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700252 unsigned int curOffset = currRun->frag.startOffset;
253 unsigned int numInsts = currRun->frag.numInsts;
254 const u2 *codePtr = dexCode->insns + curOffset;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700255 int traceSize = 0; // # of half-words
Ben Chengba4fc8b2009-06-01 13:00:29 -0700256 const u2 *startCodePtr = codePtr;
257 BasicBlock *startBB, *curBB, *lastBB;
258 int numBlocks = 0;
259 static int compilationId;
260 CompilationUnit cUnit;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700261 CompilerMethodStats *methodStats;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700262
Ben Chenge9695e52009-06-16 16:11:47 -0700263 compilationId++;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700264 memset(&cUnit, 0, sizeof(CompilationUnit));
265
266 /* Locate the entry to store compilation statistics for this method */
267 methodStats = analyzeMethodBody(desc->method);
Ben Chenge9695e52009-06-16 16:11:47 -0700268
269 cUnit.registerScoreboard.nullCheckedRegs =
Ben Cheng4238ec22009-08-24 16:32:22 -0700270 dvmCompilerAllocBitVector(desc->method->registersSize, false);
Ben Chenge9695e52009-06-16 16:11:47 -0700271
Ben Chengba4fc8b2009-06-01 13:00:29 -0700272 /* Initialize the printMe flag */
273 cUnit.printMe = gDvmJit.printMe;
274
Bill Buzbee6e963e12009-06-17 16:56:19 -0700275 /* Initialize the profile flag */
276 cUnit.executionCount = gDvmJit.profile;
277
Ben Chengba4fc8b2009-06-01 13:00:29 -0700278 /* Identify traces that we don't want to compile */
279 if (gDvmJit.methodTable) {
280 int len = strlen(desc->method->clazz->descriptor) +
281 strlen(desc->method->name) + 1;
282 char *fullSignature = dvmCompilerNew(len, true);
283 strcpy(fullSignature, desc->method->clazz->descriptor);
284 strcat(fullSignature, desc->method->name);
285
286 int hashValue = dvmComputeUtf8Hash(fullSignature);
287
288 /*
289 * Doing three levels of screening to see whether we want to skip
290 * compiling this method
291 */
292
293 /* First, check the full "class;method" signature */
294 bool methodFound =
295 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
296 fullSignature, (HashCompareFunc) strcmp,
297 false) !=
298 NULL;
299
300 /* Full signature not found - check the enclosing class */
301 if (methodFound == false) {
302 int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor);
303 methodFound =
304 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
305 (char *) desc->method->clazz->descriptor,
306 (HashCompareFunc) strcmp, false) !=
307 NULL;
308 /* Enclosing class not found - check the method name */
309 if (methodFound == false) {
310 int hashValue = dvmComputeUtf8Hash(desc->method->name);
311 methodFound =
312 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
313 (char *) desc->method->name,
314 (HashCompareFunc) strcmp, false) !=
315 NULL;
316 }
317 }
318
319 /*
320 * Under the following conditions, the trace will be *conservatively*
321 * compiled by only containing single-step instructions to and from the
322 * interpreter.
323 * 1) If includeSelectedMethod == false, the method matches the full or
324 * partial signature stored in the hash table.
325 *
326 * 2) If includeSelectedMethod == true, the method does not match the
327 * full and partial signature stored in the hash table.
328 */
329 if (gDvmJit.includeSelectedMethod != methodFound) {
330 cUnit.allSingleStep = true;
331 } else {
332 /* Compile the trace as normal */
333
334 /* Print the method we cherry picked */
335 if (gDvmJit.includeSelectedMethod == true) {
336 cUnit.printMe = true;
337 }
338 }
339 }
340
Ben Cheng4238ec22009-08-24 16:32:22 -0700341 /* Allocate the entry block */
342 lastBB = startBB = curBB = dvmCompilerNewBB(ENTRY_BLOCK);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343 curBB->startOffset = curOffset;
344 curBB->id = numBlocks++;
345
Ben Cheng4238ec22009-08-24 16:32:22 -0700346 curBB = dvmCompilerNewBB(DALVIK_BYTECODE);
347 curBB->startOffset = curOffset;
348 curBB->id = numBlocks++;
349
350 /* Make the first real dalvik block the fallthrough of the entry block */
351 startBB->fallThrough = curBB;
352 lastBB->next = curBB;
353 lastBB = curBB;
354
Ben Chengba4fc8b2009-06-01 13:00:29 -0700355 if (cUnit.printMe) {
356 LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n",
357 desc->method->name, curOffset);
358 }
359
Ben Cheng1efc9c52009-06-08 18:25:27 -0700360 /*
361 * Analyze the trace descriptor and include up to the maximal number
362 * of Dalvik instructions into the IR.
363 */
364 while (1) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700365 MIR *insn;
366 int width;
Ben Cheng4238ec22009-08-24 16:32:22 -0700367 insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700368 insn->offset = curOffset;
369 width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700370
371 /* The trace should never incude instruction data */
372 assert(width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700373 insn->width = width;
374 traceSize += width;
375 dvmCompilerAppendMIR(curBB, insn);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700376 cUnit.numInsts++;
377 /* Instruction limit reached - terminate the trace here */
378 if (cUnit.numInsts >= numMaxInsts) {
379 break;
380 }
381 if (--numInsts == 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700382 if (currRun->frag.runEnd) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700383 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700384 } else {
385 curBB = dvmCompilerNewBB(DALVIK_BYTECODE);
386 lastBB->next = curBB;
387 lastBB = curBB;
388 curBB->id = numBlocks++;
389 currRun++;
390 curOffset = currRun->frag.startOffset;
391 numInsts = currRun->frag.numInsts;
392 curBB->startOffset = curOffset;
393 codePtr = dexCode->insns + curOffset;
394 }
395 } else {
396 curOffset += width;
397 codePtr += width;
398 }
399 }
400
Ben Cheng8b258bf2009-06-24 17:27:07 -0700401 /* Convert # of half-word to bytes */
402 methodStats->compiledDalvikSize += traceSize * 2;
403
Ben Chengba4fc8b2009-06-01 13:00:29 -0700404 /*
405 * Now scan basic blocks containing real code to connect the
406 * taken/fallthrough links. Also create chaining cells for code not included
407 * in the trace.
408 */
409 for (curBB = startBB; curBB; curBB = curBB->next) {
410 MIR *lastInsn = curBB->lastMIRInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -0700411 /* Skip empty blocks */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700412 if (lastInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700413 continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700414 }
415 curOffset = lastInsn->offset;
416 unsigned int targetOffset = curOffset;
417 unsigned int fallThroughOffset = curOffset + lastInsn->width;
418 bool isInvoke = false;
419 const Method *callee = NULL;
420
421 findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset,
422 &targetOffset, &isInvoke, &callee);
423
424 /* Link the taken and fallthrough blocks */
425 BasicBlock *searchBB;
426
427 /* No backward branch in the trace - start searching the next BB */
428 for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) {
429 if (targetOffset == searchBB->startOffset) {
430 curBB->taken = searchBB;
431 }
432 if (fallThroughOffset == searchBB->startOffset) {
433 curBB->fallThrough = searchBB;
434 }
435 }
436
Ben Cheng1efc9c52009-06-08 18:25:27 -0700437 int flags = dexGetInstrFlags(gDvm.instrFlags,
438 lastInsn->dalvikInsn.opCode);
439
440 /*
441 * Some blocks are ended by non-control-flow-change instructions,
442 * currently only due to trace length constraint. In this case we need
443 * to generate an explicit branch at the end of the block to jump to
444 * the chaining cell.
Ben Cheng17f15ce2009-07-27 16:21:52 -0700445 *
446 * NOTE: INVOKE_DIRECT_EMPTY is actually not an invoke but a nop
Ben Cheng1efc9c52009-06-08 18:25:27 -0700447 */
448 curBB->needFallThroughBranch =
Ben Cheng17f15ce2009-07-27 16:21:52 -0700449 ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
450 kInstrInvoke)) == 0) ||
451 (lastInsn->dalvikInsn.opCode == OP_INVOKE_DIRECT_EMPTY);
452
Ben Cheng4238ec22009-08-24 16:32:22 -0700453 if (curBB->taken == NULL &&
454 curBB->fallThrough == NULL &&
455 flags == (kInstrCanBranch | kInstrCanContinue) &&
456 fallThroughOffset == startBB->startOffset) {
Ben Cheng0fd31e42009-09-03 14:40:16 -0700457 BasicBlock *loopBranch = curBB;
458 BasicBlock *exitBB;
459 BasicBlock *exitChainingCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700460
461 if (cUnit.printMe) {
462 LOGD("Natural loop detected!");
463 }
Ben Cheng0fd31e42009-09-03 14:40:16 -0700464 exitBB = dvmCompilerNewBB(EXIT_BLOCK);
465 lastBB->next = exitBB;
466 lastBB = exitBB;
Ben Cheng4238ec22009-08-24 16:32:22 -0700467
Ben Cheng0fd31e42009-09-03 14:40:16 -0700468 exitBB->startOffset = targetOffset;
469 exitBB->id = numBlocks++;
470 exitBB->needFallThroughBranch = true;
Ben Cheng4238ec22009-08-24 16:32:22 -0700471
Ben Cheng0fd31e42009-09-03 14:40:16 -0700472 loopBranch->taken = exitBB;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700473#if defined(WITH_SELF_VERIFICATION)
Ben Cheng0fd31e42009-09-03 14:40:16 -0700474 BasicBlock *backwardCell =
475 dvmCompilerNewBB(CHAINING_CELL_BACKWARD_BRANCH);
476 lastBB->next = backwardCell;
477 lastBB = backwardCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700478
Ben Cheng0fd31e42009-09-03 14:40:16 -0700479 backwardCell->startOffset = startBB->startOffset;
480 backwardCell->id = numBlocks++;
481 loopBranch->fallThrough = backwardCell;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -0700482#elif defined(WITH_JIT_TUNING)
483 if (gDvmJit.profile) {
484 BasicBlock *backwardCell =
485 dvmCompilerNewBB(CHAINING_CELL_BACKWARD_BRANCH);
486 lastBB->next = backwardCell;
487 lastBB = backwardCell;
488
489 backwardCell->startOffset = startBB->startOffset;
490 backwardCell->id = numBlocks++;
491 loopBranch->fallThrough = backwardCell;
492 } else {
493 loopBranch->fallThrough = startBB->next;
494 }
495#else
496 loopBranch->fallThrough = startBB->next;
Ben Cheng0fd31e42009-09-03 14:40:16 -0700497#endif
498
499 /* Create the chaining cell as the fallthrough of the exit block */
500 exitChainingCell = dvmCompilerNewBB(CHAINING_CELL_NORMAL);
501 lastBB->next = exitChainingCell;
502 lastBB = exitChainingCell;
503
504 exitChainingCell->startOffset = targetOffset;
505 exitChainingCell->id = numBlocks++;
506
507 exitBB->fallThrough = exitChainingCell;
508
Ben Cheng4238ec22009-08-24 16:32:22 -0700509 cUnit.hasLoop = true;
510 }
511
Ben Cheng6d576092009-09-01 17:01:58 -0700512 /* Fallthrough block not included in the trace */
513 if (!isUnconditionalBranch(lastInsn) && curBB->fallThrough == NULL) {
514 /*
515 * If the chaining cell is after an invoke or
516 * instruction that cannot change the control flow, request a hot
517 * chaining cell.
518 */
519 if (isInvoke || curBB->needFallThroughBranch) {
520 lastBB->next = dvmCompilerNewBB(CHAINING_CELL_HOT);
521 } else {
522 lastBB->next = dvmCompilerNewBB(CHAINING_CELL_NORMAL);
523 }
524 lastBB = lastBB->next;
525 lastBB->id = numBlocks++;
526 lastBB->startOffset = fallThroughOffset;
527 curBB->fallThrough = lastBB;
528 }
529
Ben Chengba4fc8b2009-06-01 13:00:29 -0700530 /* Target block not included in the trace */
Ben Cheng38329f52009-07-07 14:19:20 -0700531 if (curBB->taken == NULL &&
Ben Cheng9c147b82009-10-07 16:41:46 -0700532 (isInvoke || (targetOffset != UNKNOWN_TARGET &&
533 targetOffset != curOffset))) {
Ben Cheng38329f52009-07-07 14:19:20 -0700534 BasicBlock *newBB;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700535 if (isInvoke) {
Ben Cheng38329f52009-07-07 14:19:20 -0700536 /* Monomorphic callee */
537 if (callee) {
538 newBB = dvmCompilerNewBB(CHAINING_CELL_INVOKE_SINGLETON);
539 newBB->startOffset = 0;
540 newBB->containingMethod = callee;
541 /* Will resolve at runtime */
542 } else {
543 newBB = dvmCompilerNewBB(CHAINING_CELL_INVOKE_PREDICTED);
544 newBB->startOffset = 0;
545 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700546 /* For unconditional branches, request a hot chaining cell */
547 } else {
Jeff Hao97319a82009-08-12 16:57:15 -0700548#if !defined(WITH_SELF_VERIFICATION)
Ben Cheng38329f52009-07-07 14:19:20 -0700549 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
Ben Cheng1efc9c52009-06-08 18:25:27 -0700550 CHAINING_CELL_HOT :
551 CHAINING_CELL_NORMAL);
Ben Cheng38329f52009-07-07 14:19:20 -0700552 newBB->startOffset = targetOffset;
Jeff Hao97319a82009-08-12 16:57:15 -0700553#else
554 /* Handle branches that branch back into the block */
555 if (targetOffset >= curBB->firstMIRInsn->offset &&
556 targetOffset <= curBB->lastMIRInsn->offset) {
557 newBB = dvmCompilerNewBB(CHAINING_CELL_BACKWARD_BRANCH);
558 } else {
559 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
560 CHAINING_CELL_HOT :
561 CHAINING_CELL_NORMAL);
562 }
563 newBB->startOffset = targetOffset;
564#endif
Ben Cheng1efc9c52009-06-08 18:25:27 -0700565 }
Ben Cheng38329f52009-07-07 14:19:20 -0700566 newBB->id = numBlocks++;
567 curBB->taken = newBB;
568 lastBB->next = newBB;
569 lastBB = newBB;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700570 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700571 }
572
573 /* Now create a special block to host PC reconstruction code */
574 lastBB->next = dvmCompilerNewBB(PC_RECONSTRUCTION);
575 lastBB = lastBB->next;
576 lastBB->id = numBlocks++;
577
578 /* And one final block that publishes the PC and raise the exception */
579 lastBB->next = dvmCompilerNewBB(EXCEPTION_HANDLING);
580 lastBB = lastBB->next;
581 lastBB->id = numBlocks++;
582
583 if (cUnit.printMe) {
584 LOGD("TRACEINFO (%d): 0x%08x %s%s 0x%x %d of %d, %d blocks",
Ben Chenge9695e52009-06-16 16:11:47 -0700585 compilationId,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700586 (intptr_t) desc->method->insns,
587 desc->method->clazz->descriptor,
588 desc->method->name,
589 desc->trace[0].frag.startOffset,
590 traceSize,
591 dexCode->insnsSize,
592 numBlocks);
593 }
594
595 BasicBlock **blockList;
596
597 cUnit.method = desc->method;
598 cUnit.traceDesc = desc;
599 cUnit.numBlocks = numBlocks;
600 dvmInitGrowableList(&cUnit.pcReconstructionList, 8);
601 blockList = cUnit.blockList =
602 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
603
604 int i;
605
606 for (i = 0, curBB = startBB; i < numBlocks; i++) {
607 blockList[i] = curBB;
608 curBB = curBB->next;
609 }
610 /* Make sure all blocks are added to the cUnit */
611 assert(curBB == NULL);
612
Ben Cheng4238ec22009-08-24 16:32:22 -0700613 /* Preparation for SSA conversion */
614 dvmInitializeSSAConversion(&cUnit);
615
616 if (cUnit.hasLoop) {
617 dvmCompilerLoopOpt(&cUnit);
618 }
619 else {
620 dvmCompilerNonLoopAnalysis(&cUnit);
621 }
622
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 if (cUnit.printMe) {
624 dvmCompilerDumpCompilationUnit(&cUnit);
625 }
626
Bill Buzbee716f1202009-07-23 13:22:09 -0700627 /* Set the instruction set to use (NOTE: later components may change it) */
628 cUnit.instructionSet = dvmCompilerInstructionSet(&cUnit);
629
Ben Chengba4fc8b2009-06-01 13:00:29 -0700630 /* Convert MIR to LIR, etc. */
631 dvmCompilerMIR2LIR(&cUnit);
632
Ben Cheng1efc9c52009-06-08 18:25:27 -0700633 /* Convert LIR into machine code. */
Bill Buzbee716f1202009-07-23 13:22:09 -0700634 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635
636 if (cUnit.printMe) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700637 if (cUnit.halveInstCount) {
638 LOGD("Assembler aborted");
639 } else {
640 dvmCompilerCodegenDump(&cUnit);
641 }
642 LOGD("End %s%s, %d Dalvik instructions",
643 desc->method->clazz->descriptor, desc->method->name,
644 cUnit.numInsts);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700645 }
646
647 /* Reset the compiler resource pool */
648 dvmCompilerArenaReset();
649
Bill Buzbee716f1202009-07-23 13:22:09 -0700650 /* Success */
Ben Cheng4238ec22009-08-24 16:32:22 -0700651 if (!cUnit.halveInstCount) {
Ben Cheng8b258bf2009-06-24 17:27:07 -0700652 methodStats->nativeSize += cUnit.totalSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700653 return info->codeAddress != NULL;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700654
655 /* Halve the instruction count and retry again */
656 } else {
Bill Buzbee716f1202009-07-23 13:22:09 -0700657 return dvmCompileTrace(desc, cUnit.numInsts / 2, info);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700658 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700659}
660
661/*
662 * Similar to dvmCompileTrace, but the entity processed here is the whole
663 * method.
664 *
665 * TODO: implementation will be revisited when the trace builder can provide
666 * whole-method traces.
667 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700668bool dvmCompileMethod(const Method *method, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669{
670 const DexCode *dexCode = dvmGetMethodCode(method);
671 const u2 *codePtr = dexCode->insns;
672 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
673 int blockID = 0;
674 unsigned int curOffset = 0;
675
676 BasicBlock *firstBlock = dvmCompilerNewBB(DALVIK_BYTECODE);
677 firstBlock->id = blockID++;
678
679 /* Allocate the bit-vector to track the beginning of basic blocks */
Ben Cheng4238ec22009-08-24 16:32:22 -0700680 BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1,
681 false);
682 dvmCompilerSetBit(bbStartAddr, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700683
684 /*
685 * Sequentially go through every instruction first and put them in a single
686 * basic block. Identify block boundaries at the mean time.
687 */
688 while (codePtr < codeEnd) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700689 MIR *insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 insn->offset = curOffset;
691 int width = parseInsn(codePtr, &insn->dalvikInsn, false);
692 bool isInvoke = false;
693 const Method *callee;
694 insn->width = width;
695
Ben Cheng8b258bf2009-06-24 17:27:07 -0700696 /* Terminate when the data section is seen */
697 if (width == 0)
698 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700699 dvmCompilerAppendMIR(firstBlock, insn);
700 /*
701 * Check whether this is a block ending instruction and whether it
702 * suggests the start of a new block
703 */
704 unsigned int target = curOffset;
705
706 /*
707 * If findBlockBoundary returns true, it means the current instruction
708 * is terminating the current block. If it is a branch, the target
709 * address will be recorded in target.
710 */
711 if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke,
712 &callee)) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700713 dvmCompilerSetBit(bbStartAddr, curOffset + width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700714 if (target != curOffset) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700715 dvmCompilerSetBit(bbStartAddr, target);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700716 }
717 }
718
719 codePtr += width;
720 /* each bit represents 16-bit quantity */
721 curOffset += width;
722 }
723
724 /*
725 * The number of blocks will be equal to the number of bits set to 1 in the
726 * bit vector minus 1, because the bit representing the location after the
727 * last instruction is set to one.
728 */
729 int numBlocks = dvmCountSetBits(bbStartAddr);
730 if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) {
731 numBlocks--;
732 }
733
734 CompilationUnit cUnit;
735 BasicBlock **blockList;
736
737 memset(&cUnit, 0, sizeof(CompilationUnit));
738 cUnit.method = method;
739 blockList = cUnit.blockList =
740 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
741
742 /*
743 * Register the first block onto the list and start split it into block
744 * boundaries from there.
745 */
746 blockList[0] = firstBlock;
747 cUnit.numBlocks = 1;
748
749 int i;
750 for (i = 0; i < numBlocks; i++) {
751 MIR *insn;
752 BasicBlock *curBB = blockList[i];
753 curOffset = curBB->lastMIRInsn->offset;
754
755 for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) {
756 /* Found the beginning of a new block, see if it is created yet */
757 if (dvmIsBitSet(bbStartAddr, insn->offset)) {
758 int j;
759 for (j = 0; j < cUnit.numBlocks; j++) {
760 if (blockList[j]->firstMIRInsn->offset == insn->offset)
761 break;
762 }
763
764 /* Block not split yet - do it now */
765 if (j == cUnit.numBlocks) {
766 BasicBlock *newBB = dvmCompilerNewBB(DALVIK_BYTECODE);
767 newBB->id = blockID++;
768 newBB->firstMIRInsn = insn;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700769 newBB->startOffset = insn->offset;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700770 newBB->lastMIRInsn = curBB->lastMIRInsn;
771 curBB->lastMIRInsn = insn->prev;
772 insn->prev->next = NULL;
773 insn->prev = NULL;
774
775 /*
776 * If the insn is not an unconditional branch, set up the
777 * fallthrough link.
778 */
779 if (!isUnconditionalBranch(curBB->lastMIRInsn)) {
780 curBB->fallThrough = newBB;
781 }
782
783 /* enqueue the new block */
784 blockList[cUnit.numBlocks++] = newBB;
785 break;
786 }
787 }
788 }
789 }
790
791 if (numBlocks != cUnit.numBlocks) {
792 LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit.numBlocks);
793 dvmAbort();
794 }
795
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 /* Connect the basic blocks through the taken links */
797 for (i = 0; i < numBlocks; i++) {
798 BasicBlock *curBB = blockList[i];
799 MIR *insn = curBB->lastMIRInsn;
800 unsigned int target = insn->offset;
801 bool isInvoke;
802 const Method *callee;
803
804 findBlockBoundary(method, insn, target, &target, &isInvoke, &callee);
805
806 /* Found a block ended on a branch */
807 if (target != insn->offset) {
808 int j;
809 /* Forward branch */
810 if (target > insn->offset) {
811 j = i + 1;
812 } else {
813 /* Backward branch */
814 j = 0;
815 }
816 for (; j < numBlocks; j++) {
817 if (blockList[j]->firstMIRInsn->offset == target) {
818 curBB->taken = blockList[j];
819 break;
820 }
821 }
822
Ben Cheng8b258bf2009-06-24 17:27:07 -0700823 /* Don't create dummy block for the callee yet */
824 if (j == numBlocks && !isInvoke) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700825 LOGE("Target not found for insn %x: expect target %x\n",
826 curBB->lastMIRInsn->offset, target);
827 dvmAbort();
828 }
829 }
830 }
831
Bill Buzbee716f1202009-07-23 13:22:09 -0700832 /* Set the instruction set to use (NOTE: later components may change it) */
833 cUnit.instructionSet = dvmCompilerInstructionSet(&cUnit);
834
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 dvmCompilerMIR2LIR(&cUnit);
836
Bill Buzbee716f1202009-07-23 13:22:09 -0700837 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700838
839 dvmCompilerDumpCompilationUnit(&cUnit);
840
841 dvmCompilerArenaReset();
842
Bill Buzbee716f1202009-07-23 13:22:09 -0700843 return info->codeAddress != NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844}