blob: 1144d5092a16b51cdc04cdcb2999d3f9ef94b3ef [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"
19#include "dexdump/OpCodeNames.h"
20#include "interp/Jit.h"
21#include "CompilerInternals.h"
22
23/*
24 * Parse an instruction, return the length of the instruction
25 */
26static inline int parseInsn(const u2 *codePtr, DecodedInstruction *decInsn,
27 bool printMe)
28{
29 u2 instr = *codePtr;
30 OpCode opcode = instr & 0xff;
31 int insnWidth;
32
Ben Cheng8b258bf2009-06-24 17:27:07 -070033 // Don't parse instruction data
Ben Chengba4fc8b2009-06-01 13:00:29 -070034 if (opcode == OP_NOP && instr != 0) {
Ben Cheng8b258bf2009-06-24 17:27:07 -070035 return 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -070036 } else {
37 insnWidth = gDvm.instrWidth[opcode];
38 if (insnWidth < 0) {
39 insnWidth = -insnWidth;
40 }
41 }
42
43 dexDecodeInstruction(gDvm.instrFormat, codePtr, decInsn);
44 if (printMe) {
45 LOGD("%p: %#06x %s\n", codePtr, opcode, getOpcodeName(opcode));
46 }
47 return insnWidth;
48}
49
50/*
51 * Identify block-ending instructions and collect supplemental information
52 * regarding the following instructions.
53 */
54static inline bool findBlockBoundary(const Method *caller, MIR *insn,
55 unsigned int curOffset,
56 unsigned int *target, bool *isInvoke,
57 const Method **callee)
58{
59 switch (insn->dalvikInsn.opCode) {
60 /* Target is not compile-time constant */
61 case OP_RETURN_VOID:
62 case OP_RETURN:
63 case OP_RETURN_WIDE:
64 case OP_RETURN_OBJECT:
65 case OP_THROW:
66 case OP_INVOKE_VIRTUAL:
67 case OP_INVOKE_VIRTUAL_RANGE:
68 case OP_INVOKE_INTERFACE:
69 case OP_INVOKE_INTERFACE_RANGE:
70 case OP_INVOKE_VIRTUAL_QUICK:
71 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
72 *isInvoke = true;
73 break;
74 case OP_INVOKE_SUPER:
75 case OP_INVOKE_SUPER_RANGE: {
76 int mIndex = caller->clazz->pDvmDex->
77 pResMethods[insn->dalvikInsn.vB]->methodIndex;
78 const Method *calleeMethod =
79 caller->clazz->super->vtable[mIndex];
80
Ben Cheng8b258bf2009-06-24 17:27:07 -070081 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -070082 *target = (unsigned int) calleeMethod->insns;
83 }
84 *isInvoke = true;
85 *callee = calleeMethod;
86 break;
87 }
88 case OP_INVOKE_STATIC:
89 case OP_INVOKE_STATIC_RANGE: {
90 const Method *calleeMethod =
91 caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB];
92
Ben Cheng8b258bf2009-06-24 17:27:07 -070093 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -070094 *target = (unsigned int) calleeMethod->insns;
95 }
96 *isInvoke = true;
97 *callee = calleeMethod;
98 break;
99 }
100 case OP_INVOKE_SUPER_QUICK:
101 case OP_INVOKE_SUPER_QUICK_RANGE: {
102 const Method *calleeMethod =
103 caller->clazz->super->vtable[insn->dalvikInsn.vB];
104
Ben Cheng8b258bf2009-06-24 17:27:07 -0700105 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700106 *target = (unsigned int) calleeMethod->insns;
107 }
108 *isInvoke = true;
109 *callee = calleeMethod;
110 break;
111 }
112 case OP_INVOKE_DIRECT:
113 case OP_INVOKE_DIRECT_RANGE: {
114 const Method *calleeMethod =
115 caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB];
Ben Cheng8b258bf2009-06-24 17:27:07 -0700116 if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700117 *target = (unsigned int) calleeMethod->insns;
118 }
119 *isInvoke = true;
120 *callee = calleeMethod;
121 break;
122 }
123 case OP_GOTO:
124 case OP_GOTO_16:
125 case OP_GOTO_32:
126 *target = curOffset + (int) insn->dalvikInsn.vA;
127 break;
128
129 case OP_IF_EQ:
130 case OP_IF_NE:
131 case OP_IF_LT:
132 case OP_IF_GE:
133 case OP_IF_GT:
134 case OP_IF_LE:
135 *target = curOffset + (int) insn->dalvikInsn.vC;
136 break;
137
138 case OP_IF_EQZ:
139 case OP_IF_NEZ:
140 case OP_IF_LTZ:
141 case OP_IF_GEZ:
142 case OP_IF_GTZ:
143 case OP_IF_LEZ:
144 *target = curOffset + (int) insn->dalvikInsn.vB;
145 break;
146
147 default:
148 return false;
149 } return true;
150}
151
152/*
153 * Identify conditional branch instructions
154 */
155static inline bool isUnconditionalBranch(MIR *insn)
156{
157 switch (insn->dalvikInsn.opCode) {
158 case OP_RETURN_VOID:
159 case OP_RETURN:
160 case OP_RETURN_WIDE:
161 case OP_RETURN_OBJECT:
162 case OP_GOTO:
163 case OP_GOTO_16:
164 case OP_GOTO_32:
165 return true;
166 default:
167 return false;
168 }
169}
170
171/*
Ben Cheng8b258bf2009-06-24 17:27:07 -0700172 * dvmHashTableLookup() callback
173 */
174static int compareMethod(const CompilerMethodStats *m1,
175 const CompilerMethodStats *m2)
176{
177 return (int) m1->method - (int) m2->method;
178}
179
180/*
181 * Analyze each method whose traces are ever compiled. Collect a variety of
182 * statistics like the ratio of exercised vs overall code and code bloat
183 * ratios.
184 */
185static CompilerMethodStats *analyzeMethodBody(const Method *method)
186{
187 const DexCode *dexCode = dvmGetMethodCode(method);
188 const u2 *codePtr = dexCode->insns;
189 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
190 int insnSize = 0;
191 int hashValue = dvmComputeUtf8Hash(method->name);
192
193 CompilerMethodStats dummyMethodEntry; // For hash table lookup
194 CompilerMethodStats *realMethodEntry; // For hash table storage
195
196 /* For lookup only */
197 dummyMethodEntry.method = method;
198 realMethodEntry = dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
199 &dummyMethodEntry,
200 (HashCompareFunc) compareMethod,
201 false);
202
203 /* Part of this method has been compiled before - just return the entry */
204 if (realMethodEntry != NULL) {
205 return realMethodEntry;
206 }
207
208 /*
209 * First time to compile this method - set up a new entry in the hash table
210 */
211 realMethodEntry =
212 (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats));
213 realMethodEntry->method = method;
214
215 dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue,
216 realMethodEntry,
217 (HashCompareFunc) compareMethod,
218 true);
219
220 /* Count the number of instructions */
221 while (codePtr < codeEnd) {
222 DecodedInstruction dalvikInsn;
223 int width = parseInsn(codePtr, &dalvikInsn, false);
224
225 /* Terminate when the data section is seen */
226 if (width == 0)
227 break;
228
229 insnSize += width;
230 codePtr += width;
231 }
232
233 realMethodEntry->dalvikSize = insnSize * 2;
234 return realMethodEntry;
235}
236
237/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700238 * Main entry point to start trace compilation. Basic blocks are constructed
239 * first and they will be passed to the codegen routines to convert Dalvik
240 * bytecode into machine code.
241 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700242bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts,
243 JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700244{
245 const DexCode *dexCode = dvmGetMethodCode(desc->method);
246 const JitTraceRun* currRun = &desc->trace[0];
Ben Chengba4fc8b2009-06-01 13:00:29 -0700247 unsigned int curOffset = currRun->frag.startOffset;
248 unsigned int numInsts = currRun->frag.numInsts;
249 const u2 *codePtr = dexCode->insns + curOffset;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700250 int traceSize = 0; // # of half-words
Ben Chengba4fc8b2009-06-01 13:00:29 -0700251 const u2 *startCodePtr = codePtr;
252 BasicBlock *startBB, *curBB, *lastBB;
253 int numBlocks = 0;
254 static int compilationId;
255 CompilationUnit cUnit;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700256 CompilerMethodStats *methodStats;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700257
Ben Chenge9695e52009-06-16 16:11:47 -0700258 compilationId++;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700259 memset(&cUnit, 0, sizeof(CompilationUnit));
260
261 /* Locate the entry to store compilation statistics for this method */
262 methodStats = analyzeMethodBody(desc->method);
Ben Chenge9695e52009-06-16 16:11:47 -0700263
264 cUnit.registerScoreboard.nullCheckedRegs =
Ben Cheng4238ec22009-08-24 16:32:22 -0700265 dvmCompilerAllocBitVector(desc->method->registersSize, false);
Ben Chenge9695e52009-06-16 16:11:47 -0700266
Ben Chengba4fc8b2009-06-01 13:00:29 -0700267 /* Initialize the printMe flag */
268 cUnit.printMe = gDvmJit.printMe;
269
Bill Buzbee6e963e12009-06-17 16:56:19 -0700270 /* Initialize the profile flag */
271 cUnit.executionCount = gDvmJit.profile;
272
Ben Chengba4fc8b2009-06-01 13:00:29 -0700273 /* Identify traces that we don't want to compile */
274 if (gDvmJit.methodTable) {
275 int len = strlen(desc->method->clazz->descriptor) +
276 strlen(desc->method->name) + 1;
277 char *fullSignature = dvmCompilerNew(len, true);
278 strcpy(fullSignature, desc->method->clazz->descriptor);
279 strcat(fullSignature, desc->method->name);
280
281 int hashValue = dvmComputeUtf8Hash(fullSignature);
282
283 /*
284 * Doing three levels of screening to see whether we want to skip
285 * compiling this method
286 */
287
288 /* First, check the full "class;method" signature */
289 bool methodFound =
290 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
291 fullSignature, (HashCompareFunc) strcmp,
292 false) !=
293 NULL;
294
295 /* Full signature not found - check the enclosing class */
296 if (methodFound == false) {
297 int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor);
298 methodFound =
299 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
300 (char *) desc->method->clazz->descriptor,
301 (HashCompareFunc) strcmp, false) !=
302 NULL;
303 /* Enclosing class not found - check the method name */
304 if (methodFound == false) {
305 int hashValue = dvmComputeUtf8Hash(desc->method->name);
306 methodFound =
307 dvmHashTableLookup(gDvmJit.methodTable, hashValue,
308 (char *) desc->method->name,
309 (HashCompareFunc) strcmp, false) !=
310 NULL;
311 }
312 }
313
314 /*
315 * Under the following conditions, the trace will be *conservatively*
316 * compiled by only containing single-step instructions to and from the
317 * interpreter.
318 * 1) If includeSelectedMethod == false, the method matches the full or
319 * partial signature stored in the hash table.
320 *
321 * 2) If includeSelectedMethod == true, the method does not match the
322 * full and partial signature stored in the hash table.
323 */
324 if (gDvmJit.includeSelectedMethod != methodFound) {
325 cUnit.allSingleStep = true;
326 } else {
327 /* Compile the trace as normal */
328
329 /* Print the method we cherry picked */
330 if (gDvmJit.includeSelectedMethod == true) {
331 cUnit.printMe = true;
332 }
333 }
334 }
335
Ben Cheng4238ec22009-08-24 16:32:22 -0700336 /* Allocate the entry block */
337 lastBB = startBB = curBB = dvmCompilerNewBB(ENTRY_BLOCK);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700338 curBB->startOffset = curOffset;
339 curBB->id = numBlocks++;
340
Ben Cheng4238ec22009-08-24 16:32:22 -0700341 curBB = dvmCompilerNewBB(DALVIK_BYTECODE);
342 curBB->startOffset = curOffset;
343 curBB->id = numBlocks++;
344
345 /* Make the first real dalvik block the fallthrough of the entry block */
346 startBB->fallThrough = curBB;
347 lastBB->next = curBB;
348 lastBB = curBB;
349
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350 if (cUnit.printMe) {
351 LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n",
352 desc->method->name, curOffset);
353 }
354
Ben Cheng1efc9c52009-06-08 18:25:27 -0700355 /*
356 * Analyze the trace descriptor and include up to the maximal number
357 * of Dalvik instructions into the IR.
358 */
359 while (1) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700360 MIR *insn;
361 int width;
Ben Cheng4238ec22009-08-24 16:32:22 -0700362 insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700363 insn->offset = curOffset;
364 width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe);
Ben Cheng8b258bf2009-06-24 17:27:07 -0700365
366 /* The trace should never incude instruction data */
367 assert(width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700368 insn->width = width;
369 traceSize += width;
370 dvmCompilerAppendMIR(curBB, insn);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700371 cUnit.numInsts++;
372 /* Instruction limit reached - terminate the trace here */
373 if (cUnit.numInsts >= numMaxInsts) {
374 break;
375 }
376 if (--numInsts == 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700377 if (currRun->frag.runEnd) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700378 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700379 } else {
380 curBB = dvmCompilerNewBB(DALVIK_BYTECODE);
381 lastBB->next = curBB;
382 lastBB = curBB;
383 curBB->id = numBlocks++;
384 currRun++;
385 curOffset = currRun->frag.startOffset;
386 numInsts = currRun->frag.numInsts;
387 curBB->startOffset = curOffset;
388 codePtr = dexCode->insns + curOffset;
389 }
390 } else {
391 curOffset += width;
392 codePtr += width;
393 }
394 }
395
Ben Cheng8b258bf2009-06-24 17:27:07 -0700396 /* Convert # of half-word to bytes */
397 methodStats->compiledDalvikSize += traceSize * 2;
398
Ben Chengba4fc8b2009-06-01 13:00:29 -0700399 /*
400 * Now scan basic blocks containing real code to connect the
401 * taken/fallthrough links. Also create chaining cells for code not included
402 * in the trace.
403 */
404 for (curBB = startBB; curBB; curBB = curBB->next) {
405 MIR *lastInsn = curBB->lastMIRInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -0700406 /* Skip empty blocks */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700407 if (lastInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700408 continue;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700409 }
410 curOffset = lastInsn->offset;
411 unsigned int targetOffset = curOffset;
412 unsigned int fallThroughOffset = curOffset + lastInsn->width;
413 bool isInvoke = false;
414 const Method *callee = NULL;
415
416 findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset,
417 &targetOffset, &isInvoke, &callee);
418
419 /* Link the taken and fallthrough blocks */
420 BasicBlock *searchBB;
421
422 /* No backward branch in the trace - start searching the next BB */
423 for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) {
424 if (targetOffset == searchBB->startOffset) {
425 curBB->taken = searchBB;
426 }
427 if (fallThroughOffset == searchBB->startOffset) {
428 curBB->fallThrough = searchBB;
429 }
430 }
431
Ben Cheng1efc9c52009-06-08 18:25:27 -0700432 int flags = dexGetInstrFlags(gDvm.instrFlags,
433 lastInsn->dalvikInsn.opCode);
434
435 /*
436 * Some blocks are ended by non-control-flow-change instructions,
437 * currently only due to trace length constraint. In this case we need
438 * to generate an explicit branch at the end of the block to jump to
439 * the chaining cell.
Ben Cheng17f15ce2009-07-27 16:21:52 -0700440 *
441 * NOTE: INVOKE_DIRECT_EMPTY is actually not an invoke but a nop
Ben Cheng1efc9c52009-06-08 18:25:27 -0700442 */
443 curBB->needFallThroughBranch =
Ben Cheng17f15ce2009-07-27 16:21:52 -0700444 ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
445 kInstrInvoke)) == 0) ||
446 (lastInsn->dalvikInsn.opCode == OP_INVOKE_DIRECT_EMPTY);
447
Ben Cheng4238ec22009-08-24 16:32:22 -0700448 if (curBB->taken == NULL &&
449 curBB->fallThrough == NULL &&
450 flags == (kInstrCanBranch | kInstrCanContinue) &&
451 fallThroughOffset == startBB->startOffset) {
Ben Cheng0fd31e42009-09-03 14:40:16 -0700452 BasicBlock *loopBranch = curBB;
453 BasicBlock *exitBB;
454 BasicBlock *exitChainingCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700455
456 if (cUnit.printMe) {
457 LOGD("Natural loop detected!");
458 }
Ben Cheng0fd31e42009-09-03 14:40:16 -0700459 exitBB = dvmCompilerNewBB(EXIT_BLOCK);
460 lastBB->next = exitBB;
461 lastBB = exitBB;
Ben Cheng4238ec22009-08-24 16:32:22 -0700462
Ben Cheng0fd31e42009-09-03 14:40:16 -0700463 exitBB->startOffset = targetOffset;
464 exitBB->id = numBlocks++;
465 exitBB->needFallThroughBranch = true;
Ben Cheng4238ec22009-08-24 16:32:22 -0700466
Ben Cheng0fd31e42009-09-03 14:40:16 -0700467 loopBranch->taken = exitBB;
468#if !defined(WITH_SELF_VERIFICATION)
469 loopBranch->fallThrough = startBB->next;
470#else
471 BasicBlock *backwardCell =
472 dvmCompilerNewBB(CHAINING_CELL_BACKWARD_BRANCH);
473 lastBB->next = backwardCell;
474 lastBB = backwardCell;
Ben Cheng4238ec22009-08-24 16:32:22 -0700475
Ben Cheng0fd31e42009-09-03 14:40:16 -0700476 backwardCell->startOffset = startBB->startOffset;
477 backwardCell->id = numBlocks++;
478 loopBranch->fallThrough = backwardCell;
479#endif
480
481 /* Create the chaining cell as the fallthrough of the exit block */
482 exitChainingCell = dvmCompilerNewBB(CHAINING_CELL_NORMAL);
483 lastBB->next = exitChainingCell;
484 lastBB = exitChainingCell;
485
486 exitChainingCell->startOffset = targetOffset;
487 exitChainingCell->id = numBlocks++;
488
489 exitBB->fallThrough = exitChainingCell;
490
Ben Cheng4238ec22009-08-24 16:32:22 -0700491 cUnit.hasLoop = true;
492 }
493
Ben Cheng6d576092009-09-01 17:01:58 -0700494 /* Fallthrough block not included in the trace */
495 if (!isUnconditionalBranch(lastInsn) && curBB->fallThrough == NULL) {
496 /*
497 * If the chaining cell is after an invoke or
498 * instruction that cannot change the control flow, request a hot
499 * chaining cell.
500 */
501 if (isInvoke || curBB->needFallThroughBranch) {
502 lastBB->next = dvmCompilerNewBB(CHAINING_CELL_HOT);
503 } else {
504 lastBB->next = dvmCompilerNewBB(CHAINING_CELL_NORMAL);
505 }
506 lastBB = lastBB->next;
507 lastBB->id = numBlocks++;
508 lastBB->startOffset = fallThroughOffset;
509 curBB->fallThrough = lastBB;
510 }
511
Ben Chengba4fc8b2009-06-01 13:00:29 -0700512 /* Target block not included in the trace */
Ben Cheng38329f52009-07-07 14:19:20 -0700513 if (curBB->taken == NULL &&
514 (isInvoke || (targetOffset != curOffset))) {
515 BasicBlock *newBB;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700516 if (isInvoke) {
Ben Cheng38329f52009-07-07 14:19:20 -0700517 /* Monomorphic callee */
518 if (callee) {
519 newBB = dvmCompilerNewBB(CHAINING_CELL_INVOKE_SINGLETON);
520 newBB->startOffset = 0;
521 newBB->containingMethod = callee;
522 /* Will resolve at runtime */
523 } else {
524 newBB = dvmCompilerNewBB(CHAINING_CELL_INVOKE_PREDICTED);
525 newBB->startOffset = 0;
526 }
Ben Cheng1efc9c52009-06-08 18:25:27 -0700527 /* For unconditional branches, request a hot chaining cell */
528 } else {
Jeff Hao97319a82009-08-12 16:57:15 -0700529#if !defined(WITH_SELF_VERIFICATION)
Ben Cheng38329f52009-07-07 14:19:20 -0700530 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
Ben Cheng1efc9c52009-06-08 18:25:27 -0700531 CHAINING_CELL_HOT :
532 CHAINING_CELL_NORMAL);
Ben Cheng38329f52009-07-07 14:19:20 -0700533 newBB->startOffset = targetOffset;
Jeff Hao97319a82009-08-12 16:57:15 -0700534#else
535 /* Handle branches that branch back into the block */
536 if (targetOffset >= curBB->firstMIRInsn->offset &&
537 targetOffset <= curBB->lastMIRInsn->offset) {
538 newBB = dvmCompilerNewBB(CHAINING_CELL_BACKWARD_BRANCH);
539 } else {
540 newBB = dvmCompilerNewBB(flags & kInstrUnconditional ?
541 CHAINING_CELL_HOT :
542 CHAINING_CELL_NORMAL);
543 }
544 newBB->startOffset = targetOffset;
545#endif
Ben Cheng1efc9c52009-06-08 18:25:27 -0700546 }
Ben Cheng38329f52009-07-07 14:19:20 -0700547 newBB->id = numBlocks++;
548 curBB->taken = newBB;
549 lastBB->next = newBB;
550 lastBB = newBB;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700551 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700552 }
553
554 /* Now create a special block to host PC reconstruction code */
555 lastBB->next = dvmCompilerNewBB(PC_RECONSTRUCTION);
556 lastBB = lastBB->next;
557 lastBB->id = numBlocks++;
558
559 /* And one final block that publishes the PC and raise the exception */
560 lastBB->next = dvmCompilerNewBB(EXCEPTION_HANDLING);
561 lastBB = lastBB->next;
562 lastBB->id = numBlocks++;
563
564 if (cUnit.printMe) {
565 LOGD("TRACEINFO (%d): 0x%08x %s%s 0x%x %d of %d, %d blocks",
Ben Chenge9695e52009-06-16 16:11:47 -0700566 compilationId,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700567 (intptr_t) desc->method->insns,
568 desc->method->clazz->descriptor,
569 desc->method->name,
570 desc->trace[0].frag.startOffset,
571 traceSize,
572 dexCode->insnsSize,
573 numBlocks);
574 }
575
576 BasicBlock **blockList;
577
578 cUnit.method = desc->method;
579 cUnit.traceDesc = desc;
580 cUnit.numBlocks = numBlocks;
581 dvmInitGrowableList(&cUnit.pcReconstructionList, 8);
582 blockList = cUnit.blockList =
583 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
584
585 int i;
586
587 for (i = 0, curBB = startBB; i < numBlocks; i++) {
588 blockList[i] = curBB;
589 curBB = curBB->next;
590 }
591 /* Make sure all blocks are added to the cUnit */
592 assert(curBB == NULL);
593
Ben Cheng4238ec22009-08-24 16:32:22 -0700594 /* Preparation for SSA conversion */
595 dvmInitializeSSAConversion(&cUnit);
596
597 if (cUnit.hasLoop) {
598 dvmCompilerLoopOpt(&cUnit);
599 }
600 else {
601 dvmCompilerNonLoopAnalysis(&cUnit);
602 }
603
Ben Chengba4fc8b2009-06-01 13:00:29 -0700604 if (cUnit.printMe) {
605 dvmCompilerDumpCompilationUnit(&cUnit);
606 }
607
Bill Buzbee716f1202009-07-23 13:22:09 -0700608 /* Set the instruction set to use (NOTE: later components may change it) */
609 cUnit.instructionSet = dvmCompilerInstructionSet(&cUnit);
610
Ben Chengba4fc8b2009-06-01 13:00:29 -0700611 /* Convert MIR to LIR, etc. */
612 dvmCompilerMIR2LIR(&cUnit);
613
Ben Cheng1efc9c52009-06-08 18:25:27 -0700614 /* Convert LIR into machine code. */
Bill Buzbee716f1202009-07-23 13:22:09 -0700615 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700616
617 if (cUnit.printMe) {
Ben Cheng1efc9c52009-06-08 18:25:27 -0700618 if (cUnit.halveInstCount) {
619 LOGD("Assembler aborted");
620 } else {
621 dvmCompilerCodegenDump(&cUnit);
622 }
623 LOGD("End %s%s, %d Dalvik instructions",
624 desc->method->clazz->descriptor, desc->method->name,
625 cUnit.numInsts);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700626 }
627
628 /* Reset the compiler resource pool */
629 dvmCompilerArenaReset();
630
Bill Buzbee716f1202009-07-23 13:22:09 -0700631 /* Success */
Ben Cheng4238ec22009-08-24 16:32:22 -0700632 if (!cUnit.halveInstCount) {
Ben Cheng8b258bf2009-06-24 17:27:07 -0700633 methodStats->nativeSize += cUnit.totalSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700634 return info->codeAddress != NULL;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700635
636 /* Halve the instruction count and retry again */
637 } else {
Bill Buzbee716f1202009-07-23 13:22:09 -0700638 return dvmCompileTrace(desc, cUnit.numInsts / 2, info);
Ben Cheng1efc9c52009-06-08 18:25:27 -0700639 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700640}
641
642/*
643 * Similar to dvmCompileTrace, but the entity processed here is the whole
644 * method.
645 *
646 * TODO: implementation will be revisited when the trace builder can provide
647 * whole-method traces.
648 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700649bool dvmCompileMethod(const Method *method, JitTranslationInfo *info)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700650{
651 const DexCode *dexCode = dvmGetMethodCode(method);
652 const u2 *codePtr = dexCode->insns;
653 const u2 *codeEnd = dexCode->insns + dexCode->insnsSize;
654 int blockID = 0;
655 unsigned int curOffset = 0;
656
657 BasicBlock *firstBlock = dvmCompilerNewBB(DALVIK_BYTECODE);
658 firstBlock->id = blockID++;
659
660 /* Allocate the bit-vector to track the beginning of basic blocks */
Ben Cheng4238ec22009-08-24 16:32:22 -0700661 BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1,
662 false);
663 dvmCompilerSetBit(bbStartAddr, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700664
665 /*
666 * Sequentially go through every instruction first and put them in a single
667 * basic block. Identify block boundaries at the mean time.
668 */
669 while (codePtr < codeEnd) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700670 MIR *insn = dvmCompilerNew(sizeof(MIR), true);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700671 insn->offset = curOffset;
672 int width = parseInsn(codePtr, &insn->dalvikInsn, false);
673 bool isInvoke = false;
674 const Method *callee;
675 insn->width = width;
676
Ben Cheng8b258bf2009-06-24 17:27:07 -0700677 /* Terminate when the data section is seen */
678 if (width == 0)
679 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700680 dvmCompilerAppendMIR(firstBlock, insn);
681 /*
682 * Check whether this is a block ending instruction and whether it
683 * suggests the start of a new block
684 */
685 unsigned int target = curOffset;
686
687 /*
688 * If findBlockBoundary returns true, it means the current instruction
689 * is terminating the current block. If it is a branch, the target
690 * address will be recorded in target.
691 */
692 if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke,
693 &callee)) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700694 dvmCompilerSetBit(bbStartAddr, curOffset + width);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 if (target != curOffset) {
Ben Cheng4238ec22009-08-24 16:32:22 -0700696 dvmCompilerSetBit(bbStartAddr, target);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700697 }
698 }
699
700 codePtr += width;
701 /* each bit represents 16-bit quantity */
702 curOffset += width;
703 }
704
705 /*
706 * The number of blocks will be equal to the number of bits set to 1 in the
707 * bit vector minus 1, because the bit representing the location after the
708 * last instruction is set to one.
709 */
710 int numBlocks = dvmCountSetBits(bbStartAddr);
711 if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) {
712 numBlocks--;
713 }
714
715 CompilationUnit cUnit;
716 BasicBlock **blockList;
717
718 memset(&cUnit, 0, sizeof(CompilationUnit));
719 cUnit.method = method;
720 blockList = cUnit.blockList =
721 dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true);
722
723 /*
724 * Register the first block onto the list and start split it into block
725 * boundaries from there.
726 */
727 blockList[0] = firstBlock;
728 cUnit.numBlocks = 1;
729
730 int i;
731 for (i = 0; i < numBlocks; i++) {
732 MIR *insn;
733 BasicBlock *curBB = blockList[i];
734 curOffset = curBB->lastMIRInsn->offset;
735
736 for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) {
737 /* Found the beginning of a new block, see if it is created yet */
738 if (dvmIsBitSet(bbStartAddr, insn->offset)) {
739 int j;
740 for (j = 0; j < cUnit.numBlocks; j++) {
741 if (blockList[j]->firstMIRInsn->offset == insn->offset)
742 break;
743 }
744
745 /* Block not split yet - do it now */
746 if (j == cUnit.numBlocks) {
747 BasicBlock *newBB = dvmCompilerNewBB(DALVIK_BYTECODE);
748 newBB->id = blockID++;
749 newBB->firstMIRInsn = insn;
Ben Cheng8b258bf2009-06-24 17:27:07 -0700750 newBB->startOffset = insn->offset;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700751 newBB->lastMIRInsn = curBB->lastMIRInsn;
752 curBB->lastMIRInsn = insn->prev;
753 insn->prev->next = NULL;
754 insn->prev = NULL;
755
756 /*
757 * If the insn is not an unconditional branch, set up the
758 * fallthrough link.
759 */
760 if (!isUnconditionalBranch(curBB->lastMIRInsn)) {
761 curBB->fallThrough = newBB;
762 }
763
764 /* enqueue the new block */
765 blockList[cUnit.numBlocks++] = newBB;
766 break;
767 }
768 }
769 }
770 }
771
772 if (numBlocks != cUnit.numBlocks) {
773 LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit.numBlocks);
774 dvmAbort();
775 }
776
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 /* Connect the basic blocks through the taken links */
778 for (i = 0; i < numBlocks; i++) {
779 BasicBlock *curBB = blockList[i];
780 MIR *insn = curBB->lastMIRInsn;
781 unsigned int target = insn->offset;
782 bool isInvoke;
783 const Method *callee;
784
785 findBlockBoundary(method, insn, target, &target, &isInvoke, &callee);
786
787 /* Found a block ended on a branch */
788 if (target != insn->offset) {
789 int j;
790 /* Forward branch */
791 if (target > insn->offset) {
792 j = i + 1;
793 } else {
794 /* Backward branch */
795 j = 0;
796 }
797 for (; j < numBlocks; j++) {
798 if (blockList[j]->firstMIRInsn->offset == target) {
799 curBB->taken = blockList[j];
800 break;
801 }
802 }
803
Ben Cheng8b258bf2009-06-24 17:27:07 -0700804 /* Don't create dummy block for the callee yet */
805 if (j == numBlocks && !isInvoke) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700806 LOGE("Target not found for insn %x: expect target %x\n",
807 curBB->lastMIRInsn->offset, target);
808 dvmAbort();
809 }
810 }
811 }
812
Bill Buzbee716f1202009-07-23 13:22:09 -0700813 /* Set the instruction set to use (NOTE: later components may change it) */
814 cUnit.instructionSet = dvmCompilerInstructionSet(&cUnit);
815
Ben Chengba4fc8b2009-06-01 13:00:29 -0700816 dvmCompilerMIR2LIR(&cUnit);
817
Bill Buzbee716f1202009-07-23 13:22:09 -0700818 dvmCompilerAssembleLIR(&cUnit, info);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700819
820 dvmCompilerDumpCompilationUnit(&cUnit);
821
822 dvmCompilerArenaReset();
823
Bill Buzbee716f1202009-07-23 13:22:09 -0700824 return info->codeAddress != NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700825}