blob: dbaf323a2d469f81eccb94b44fe7ba52336bf289 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 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 "CompilerInternals.h"
19#include "Dataflow.h"
Ian Rogers0571d352011-11-03 19:51:38 -070020#include "leb128.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070021#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070022#include "runtime.h"
buzbee67bf8852011-08-17 17:51:35 -070023
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbeece302932011-10-04 14:32:18 -070026/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070027static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee67bc2362011-10-11 18:08:40 -070028 //(1 << kLoadStoreElimination) |
29 //(1 << kLoadHoisting) |
30 //(1 << kSuppressLoads) |
31 //(1 << kNullCheckElimination) |
buzbee769fde12012-01-05 17:35:23 -080032 //(1 << kPromoteRegs) |
33 //(1 << kTrackLiveTemps) |
buzbee99ba9642012-01-25 14:23:14 -080034 //(1 << kSkipLargeMethodOptimization) |
buzbee86a4bce2012-03-06 18:15:00 -080035 //(1 << kSafeOptimizations) |
buzbee239c4e72012-03-16 08:42:29 -070036 //(1 << kBBOpt) |
buzbee16da88c2012-03-20 10:38:17 -070037 //(1 << kMatch) |
buzbee9c044ce2012-03-18 13:24:07 -070038 //(1 << kPromoteCompilerTemps) |
buzbeece302932011-10-04 14:32:18 -070039 0;
40
Elliott Hughese52e49b2012-04-02 16:05:44 -070041static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
buzbeee3de7492011-10-05 13:37:17 -070042 //(1 << kDebugDisplayMissingTargets) |
buzbeece302932011-10-04 14:32:18 -070043 //(1 << kDebugVerbose) |
44 //(1 << kDebugDumpCFG) |
45 //(1 << kDebugSlowFieldPath) |
46 //(1 << kDebugSlowInvokePath) |
47 //(1 << kDebugSlowStringPath) |
48 //(1 << kDebugSlowestFieldPath) |
49 //(1 << kDebugSlowestStringPath) |
buzbee34c77ad2012-01-11 13:01:32 -080050 //(1 << kDebugExerciseResolveMethod) |
buzbee5b537102012-01-17 17:33:47 -080051 //(1 << kDebugVerifyDataflow) |
buzbee5abfa3e2012-01-31 17:01:43 -080052 //(1 << kDebugShowMemoryUsage) |
buzbee86a4bce2012-03-06 18:15:00 -080053 //(1 << kDebugShowNops) |
buzbeea7c12682012-03-19 13:13:53 -070054 //(1 << kDebugCountOpcodes) |
buzbeece302932011-10-04 14:32:18 -070055 0;
56
buzbee31a4a6f2012-02-28 15:36:15 -080057inline bool contentIsInsn(const u2* codePtr) {
buzbee67bf8852011-08-17 17:51:35 -070058 u2 instr = *codePtr;
Elliott Hughesadb8c672012-03-06 16:49:32 -080059 Instruction::Code opcode = (Instruction::Code)(instr & 0xff);
buzbee67bf8852011-08-17 17:51:35 -070060
61 /*
Elliott Hughesadb8c672012-03-06 16:49:32 -080062 * Since the low 8-bit in metadata may look like NOP, we need to check
buzbee67bf8852011-08-17 17:51:35 -070063 * both the low and whole sub-word to determine whether it is code or data.
64 */
Elliott Hughesadb8c672012-03-06 16:49:32 -080065 return (opcode != Instruction::NOP || instr == 0);
buzbee67bf8852011-08-17 17:51:35 -070066}
67
68/*
69 * Parse an instruction, return the length of the instruction
70 */
buzbee31a4a6f2012-02-28 15:36:15 -080071inline int parseInsn(CompilationUnit* cUnit, const u2* codePtr,
Elliott Hughesadb8c672012-03-06 16:49:32 -080072 DecodedInstruction* decoded_instruction, bool printMe)
buzbee67bf8852011-08-17 17:51:35 -070073{
Elliott Hughesadb8c672012-03-06 16:49:32 -080074 // Don't parse instruction data
75 if (!contentIsInsn(codePtr)) {
76 return 0;
77 }
buzbee67bf8852011-08-17 17:51:35 -070078
Elliott Hughesadb8c672012-03-06 16:49:32 -080079 const Instruction* instruction = Instruction::At(codePtr);
80 *decoded_instruction = DecodedInstruction(instruction);
buzbee67bf8852011-08-17 17:51:35 -070081
Elliott Hughesadb8c672012-03-06 16:49:32 -080082 if (printMe) {
83 char* decodedString = oatGetDalvikDisassembly(cUnit, *decoded_instruction, NULL);
84 LOG(INFO) << codePtr << ": 0x" << std::hex << static_cast<int>(decoded_instruction->opcode)
85 << " " << decodedString;
86 }
87 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -070088}
89
90#define UNKNOWN_TARGET 0xffffffff
91
Elliott Hughesadb8c672012-03-06 16:49:32 -080092inline bool isGoto(MIR* insn) {
93 switch (insn->dalvikInsn.opcode) {
94 case Instruction::GOTO:
95 case Instruction::GOTO_16:
96 case Instruction::GOTO_32:
97 return true;
98 default:
99 return false;
buzbee67bf8852011-08-17 17:51:35 -0700100 }
101}
102
103/*
104 * Identify unconditional branch instructions
105 */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800106inline bool isUnconditionalBranch(MIR* insn) {
107 switch (insn->dalvikInsn.opcode) {
108 case Instruction::RETURN_VOID:
109 case Instruction::RETURN:
110 case Instruction::RETURN_WIDE:
111 case Instruction::RETURN_OBJECT:
112 return true;
113 default:
114 return isGoto(insn);
115 }
buzbee67bf8852011-08-17 17:51:35 -0700116}
117
118/* Split an existing block from the specified code offset into two */
buzbee31a4a6f2012-02-28 15:36:15 -0800119BasicBlock *splitBlock(CompilationUnit* cUnit, unsigned int codeOffset,
120 BasicBlock* origBlock, BasicBlock** immedPredBlockP)
buzbee67bf8852011-08-17 17:51:35 -0700121{
122 MIR* insn = origBlock->firstMIRInsn;
123 while (insn) {
124 if (insn->offset == codeOffset) break;
125 insn = insn->next;
126 }
127 if (insn == NULL) {
128 LOG(FATAL) << "Break split failed";
129 }
buzbee5abfa3e2012-01-31 17:01:43 -0800130 BasicBlock *bottomBlock = oatNewBB(cUnit, kDalvikByteCode,
131 cUnit->numBlocks++);
buzbeeba938cb2012-02-03 14:47:55 -0800132 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t) bottomBlock);
buzbee67bf8852011-08-17 17:51:35 -0700133
134 bottomBlock->startOffset = codeOffset;
135 bottomBlock->firstMIRInsn = insn;
136 bottomBlock->lastMIRInsn = origBlock->lastMIRInsn;
137
buzbee5b537102012-01-17 17:33:47 -0800138 /* Add it to the quick lookup cache */
Elliott Hughesa0e18062012-04-13 15:59:59 -0700139 cUnit->blockMap.Put(bottomBlock->startOffset, bottomBlock);
buzbee5b537102012-01-17 17:33:47 -0800140
buzbee67bf8852011-08-17 17:51:35 -0700141 /* Handle the taken path */
142 bottomBlock->taken = origBlock->taken;
143 if (bottomBlock->taken) {
144 origBlock->taken = NULL;
buzbee5abfa3e2012-01-31 17:01:43 -0800145 oatDeleteGrowableList(bottomBlock->taken->predecessors,
146 (intptr_t)origBlock);
buzbeeba938cb2012-02-03 14:47:55 -0800147 oatInsertGrowableList(cUnit, bottomBlock->taken->predecessors,
buzbee5abfa3e2012-01-31 17:01:43 -0800148 (intptr_t)bottomBlock);
buzbee67bf8852011-08-17 17:51:35 -0700149 }
150
151 /* Handle the fallthrough path */
152 bottomBlock->needFallThroughBranch = origBlock->needFallThroughBranch;
153 bottomBlock->fallThrough = origBlock->fallThrough;
154 origBlock->fallThrough = bottomBlock;
155 origBlock->needFallThroughBranch = true;
buzbeeba938cb2012-02-03 14:47:55 -0800156 oatInsertGrowableList(cUnit, bottomBlock->predecessors,
157 (intptr_t)origBlock);
buzbee67bf8852011-08-17 17:51:35 -0700158 if (bottomBlock->fallThrough) {
buzbee5abfa3e2012-01-31 17:01:43 -0800159 oatDeleteGrowableList(bottomBlock->fallThrough->predecessors,
160 (intptr_t)origBlock);
buzbeeba938cb2012-02-03 14:47:55 -0800161 oatInsertGrowableList(cUnit, bottomBlock->fallThrough->predecessors,
buzbee5abfa3e2012-01-31 17:01:43 -0800162 (intptr_t)bottomBlock);
buzbee67bf8852011-08-17 17:51:35 -0700163 }
164
165 /* Handle the successor list */
166 if (origBlock->successorBlockList.blockListType != kNotUsed) {
167 bottomBlock->successorBlockList = origBlock->successorBlockList;
168 origBlock->successorBlockList.blockListType = kNotUsed;
169 GrowableListIterator iterator;
170
171 oatGrowableListIteratorInit(&bottomBlock->successorBlockList.blocks,
172 &iterator);
173 while (true) {
174 SuccessorBlockInfo *successorBlockInfo =
175 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
176 if (successorBlockInfo == NULL) break;
177 BasicBlock *bb = successorBlockInfo->block;
buzbee5abfa3e2012-01-31 17:01:43 -0800178 oatDeleteGrowableList(bb->predecessors, (intptr_t)origBlock);
buzbeeba938cb2012-02-03 14:47:55 -0800179 oatInsertGrowableList(cUnit, bb->predecessors,
180 (intptr_t)bottomBlock);
buzbee67bf8852011-08-17 17:51:35 -0700181 }
182 }
183
184 origBlock->lastMIRInsn = insn->prev;
185
186 insn->prev->next = NULL;
187 insn->prev = NULL;
buzbee9ab05de2012-01-18 15:43:48 -0800188 /*
189 * Update the immediate predecessor block pointer so that outgoing edges
190 * can be applied to the proper block.
191 */
192 if (immedPredBlockP) {
193 DCHECK_EQ(*immedPredBlockP, origBlock);
194 *immedPredBlockP = bottomBlock;
195 }
buzbee67bf8852011-08-17 17:51:35 -0700196 return bottomBlock;
197}
198
199/*
200 * Given a code offset, find out the block that starts with it. If the offset
buzbee9ab05de2012-01-18 15:43:48 -0800201 * is in the middle of an existing block, split it into two. If immedPredBlockP
202 * is not non-null and is the block being split, update *immedPredBlockP to
203 * point to the bottom block so that outgoing edges can be set up properly
204 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800205 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700206 */
buzbee31a4a6f2012-02-28 15:36:15 -0800207BasicBlock *findBlock(CompilationUnit* cUnit, unsigned int codeOffset,
208 bool split, bool create, BasicBlock** immedPredBlockP)
buzbee67bf8852011-08-17 17:51:35 -0700209{
210 GrowableList* blockList = &cUnit->blockList;
211 BasicBlock* bb;
212 unsigned int i;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700213 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700214
buzbee5b537102012-01-17 17:33:47 -0800215 it = cUnit->blockMap.find(codeOffset);
216 if (it != cUnit->blockMap.end()) {
217 return it->second;
218 } else if (!create) {
219 return NULL;
220 }
221
222 if (split) {
223 for (i = 0; i < blockList->numUsed; i++) {
224 bb = (BasicBlock *) blockList->elemList[i];
225 if (bb->blockType != kDalvikByteCode) continue;
226 /* Check if a branch jumps into the middle of an existing block */
227 if ((codeOffset > bb->startOffset) && (bb->lastMIRInsn != NULL) &&
228 (codeOffset <= bb->lastMIRInsn->offset)) {
229 BasicBlock *newBB = splitBlock(cUnit, codeOffset, bb,
230 bb == *immedPredBlockP ?
231 immedPredBlockP : NULL);
232 return newBB;
233 }
buzbee67bf8852011-08-17 17:51:35 -0700234 }
235 }
buzbee5b537102012-01-17 17:33:47 -0800236
237 /* Create a new one */
buzbee5abfa3e2012-01-31 17:01:43 -0800238 bb = oatNewBB(cUnit, kDalvikByteCode, cUnit->numBlocks++);
buzbeeba938cb2012-02-03 14:47:55 -0800239 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t) bb);
buzbee5b537102012-01-17 17:33:47 -0800240 bb->startOffset = codeOffset;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700241 cUnit->blockMap.Put(bb->startOffset, bb);
buzbee5b537102012-01-17 17:33:47 -0800242 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700243}
244
245/* Dump the CFG into a DOT graph */
246void oatDumpCFG(CompilationUnit* cUnit, const char* dirPrefix)
247{
buzbee67bf8852011-08-17 17:51:35 -0700248 FILE* file;
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800249 std::string name(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
buzbee67bf8852011-08-17 17:51:35 -0700250 char startOffset[80];
251 sprintf(startOffset, "_%x", cUnit->entryBlock->fallThrough->startOffset);
buzbeeba938cb2012-02-03 14:47:55 -0800252 char* fileName = (char*) oatNew(cUnit,
buzbeec143c552011-08-20 17:38:58 -0700253 strlen(dirPrefix) +
254 name.length() +
buzbee5abfa3e2012-01-31 17:01:43 -0800255 strlen(".dot") + 1, true, kAllocDebugInfo);
buzbeec143c552011-08-20 17:38:58 -0700256 sprintf(fileName, "%s%s%s.dot", dirPrefix, name.c_str(), startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700257
258 /*
259 * Convert the special characters into a filesystem- and shell-friendly
260 * format.
261 */
262 int i;
263 for (i = strlen(dirPrefix); fileName[i]; i++) {
264 if (fileName[i] == '/') {
265 fileName[i] = '_';
266 } else if (fileName[i] == ';') {
267 fileName[i] = '#';
268 } else if (fileName[i] == '$') {
269 fileName[i] = '+';
270 } else if (fileName[i] == '(' || fileName[i] == ')') {
271 fileName[i] = '@';
272 } else if (fileName[i] == '<' || fileName[i] == '>') {
273 fileName[i] = '=';
274 }
275 }
276 file = fopen(fileName, "w");
277 if (file == NULL) {
278 return;
279 }
280 fprintf(file, "digraph G {\n");
281
282 fprintf(file, " rankdir=TB\n");
283
284 int numReachableBlocks = cUnit->numReachableBlocks;
285 int idx;
286 const GrowableList *blockList = &cUnit->blockList;
287
288 for (idx = 0; idx < numReachableBlocks; idx++) {
289 int blockIdx = cUnit->dfsOrder.elemList[idx];
290 BasicBlock *bb = (BasicBlock *) oatGrowableListGetElement(blockList,
291 blockIdx);
292 if (bb == NULL) break;
293 if (bb->blockType == kEntryBlock) {
294 fprintf(file, " entry [shape=Mdiamond];\n");
295 } else if (bb->blockType == kExitBlock) {
296 fprintf(file, " exit [shape=Mdiamond];\n");
297 } else if (bb->blockType == kDalvikByteCode) {
298 fprintf(file, " block%04x [shape=record,label = \"{ \\\n",
299 bb->startOffset);
300 const MIR *mir;
301 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
302 bb->firstMIRInsn ? " | " : " ");
303 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
304 fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset,
Elliott Hughesadb8c672012-03-06 16:49:32 -0800305 mir->ssaRep ? oatFullDisassembler(cUnit, mir) : Instruction::Name(mir->dalvikInsn.opcode),
buzbee67bf8852011-08-17 17:51:35 -0700306 mir->next ? " | " : " ");
307 }
308 fprintf(file, " }\"];\n\n");
309 } else if (bb->blockType == kExceptionHandling) {
310 char blockName[BLOCK_NAME_LEN];
311
312 oatGetBlockName(bb, blockName);
313 fprintf(file, " %s [shape=invhouse];\n", blockName);
314 }
315
316 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
317
318 if (bb->taken) {
319 oatGetBlockName(bb, blockName1);
320 oatGetBlockName(bb->taken, blockName2);
321 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
322 blockName1, blockName2);
323 }
324 if (bb->fallThrough) {
325 oatGetBlockName(bb, blockName1);
326 oatGetBlockName(bb->fallThrough, blockName2);
327 fprintf(file, " %s:s -> %s:n\n", blockName1, blockName2);
328 }
329
330 if (bb->successorBlockList.blockListType != kNotUsed) {
331 fprintf(file, " succ%04x [shape=%s,label = \"{ \\\n",
332 bb->startOffset,
333 (bb->successorBlockList.blockListType == kCatch) ?
334 "Mrecord" : "record");
335 GrowableListIterator iterator;
336 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
337 &iterator);
338 SuccessorBlockInfo *successorBlockInfo =
339 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
340
341 int succId = 0;
342 while (true) {
343 if (successorBlockInfo == NULL) break;
344
345 BasicBlock *destBlock = successorBlockInfo->block;
346 SuccessorBlockInfo *nextSuccessorBlockInfo =
347 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
348
349 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
350 succId++,
351 successorBlockInfo->key,
352 destBlock->startOffset,
353 (nextSuccessorBlockInfo != NULL) ? " | " : " ");
354
355 successorBlockInfo = nextSuccessorBlockInfo;
356 }
357 fprintf(file, " }\"];\n\n");
358
359 oatGetBlockName(bb, blockName1);
360 fprintf(file, " %s:s -> succ%04x:n [style=dashed]\n",
361 blockName1, bb->startOffset);
362
363 if (bb->successorBlockList.blockListType == kPackedSwitch ||
364 bb->successorBlockList.blockListType == kSparseSwitch) {
365
366 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
367 &iterator);
368
369 succId = 0;
370 while (true) {
371 SuccessorBlockInfo *successorBlockInfo =
372 (SuccessorBlockInfo *)
373 oatGrowableListIteratorNext(&iterator);
374 if (successorBlockInfo == NULL) break;
375
376 BasicBlock *destBlock = successorBlockInfo->block;
377
378 oatGetBlockName(destBlock, blockName2);
379 fprintf(file, " succ%04x:f%d:e -> %s:n\n",
380 bb->startOffset, succId++,
381 blockName2);
382 }
383 }
384 }
385 fprintf(file, "\n");
386
buzbeece302932011-10-04 14:32:18 -0700387 /* Display the dominator tree */
buzbee67bf8852011-08-17 17:51:35 -0700388 oatGetBlockName(bb, blockName1);
389 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
390 blockName1, blockName1);
391 if (bb->iDom) {
392 oatGetBlockName(bb->iDom, blockName2);
393 fprintf(file, " cfg%s:s -> cfg%s:n\n\n",
394 blockName2, blockName1);
395 }
buzbee67bf8852011-08-17 17:51:35 -0700396 }
397 fprintf(file, "}\n");
398 fclose(file);
399}
400
401/* Verify if all the successor is connected with all the claimed predecessors */
buzbee31a4a6f2012-02-28 15:36:15 -0800402bool verifyPredInfo(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700403{
buzbee5abfa3e2012-01-31 17:01:43 -0800404 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700405
buzbee5abfa3e2012-01-31 17:01:43 -0800406 oatGrowableListIteratorInit(bb->predecessors, &iter);
buzbee67bf8852011-08-17 17:51:35 -0700407 while (true) {
buzbee5abfa3e2012-01-31 17:01:43 -0800408 BasicBlock *predBB = (BasicBlock*)oatGrowableListIteratorNext(&iter);
409 if (!predBB) break;
buzbee67bf8852011-08-17 17:51:35 -0700410 bool found = false;
411 if (predBB->taken == bb) {
412 found = true;
413 } else if (predBB->fallThrough == bb) {
414 found = true;
415 } else if (predBB->successorBlockList.blockListType != kNotUsed) {
416 GrowableListIterator iterator;
417 oatGrowableListIteratorInit(&predBB->successorBlockList.blocks,
418 &iterator);
419 while (true) {
420 SuccessorBlockInfo *successorBlockInfo =
421 (SuccessorBlockInfo *)
422 oatGrowableListIteratorNext(&iterator);
423 if (successorBlockInfo == NULL) break;
424 BasicBlock *succBB = successorBlockInfo->block;
425 if (succBB == bb) {
426 found = true;
427 break;
428 }
429 }
430 }
431 if (found == false) {
432 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
433 oatGetBlockName(bb, blockName1);
434 oatGetBlockName(predBB, blockName2);
435 oatDumpCFG(cUnit, "/sdcard/cfg/");
436 LOG(FATAL) << "Successor " << blockName1 << "not found from "
437 << blockName2;
438 }
439 }
440 return true;
441}
442
443/* Identify code range in try blocks and set up the empty catch blocks */
buzbee31a4a6f2012-02-28 15:36:15 -0800444void processTryCatchBlocks(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700445{
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800446 const DexFile::CodeItem* code_item = cUnit->code_item;
buzbeee9a72f62011-09-04 17:59:07 -0700447 int triesSize = code_item->tries_size_;
buzbee67bf8852011-08-17 17:51:35 -0700448 int offset;
449
450 if (triesSize == 0) {
451 return;
452 }
453
buzbee67bf8852011-08-17 17:51:35 -0700454 ArenaBitVector* tryBlockAddr = cUnit->tryBlockAddr;
455
buzbeee9a72f62011-09-04 17:59:07 -0700456 for (int i = 0; i < triesSize; i++) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800457 const DexFile::TryItem* pTry =
458 DexFile::GetTryItems(*code_item, i);
buzbeee9a72f62011-09-04 17:59:07 -0700459 int startOffset = pTry->start_addr_;
460 int endOffset = startOffset + pTry->insn_count_;
buzbee67bf8852011-08-17 17:51:35 -0700461 for (offset = startOffset; offset < endOffset; offset++) {
buzbeeba938cb2012-02-03 14:47:55 -0800462 oatSetBit(cUnit, tryBlockAddr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700463 }
464 }
465
buzbeee9a72f62011-09-04 17:59:07 -0700466 // Iterate over each of the handlers to enqueue the empty Catch blocks
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800467 const byte* handlers_ptr =
468 DexFile::GetCatchHandlerData(*code_item, 0);
469 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
buzbeee9a72f62011-09-04 17:59:07 -0700470 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800471 CatchHandlerIterator iterator(handlers_ptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700472 for (; iterator.HasNext(); iterator.Next()) {
473 uint32_t address = iterator.GetHandlerAddress();
buzbee9ab05de2012-01-18 15:43:48 -0800474 findBlock(cUnit, address, false /* split */, true /*create*/,
475 /* immedPredBlockP */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700476 }
Ian Rogers0571d352011-11-03 19:51:38 -0700477 handlers_ptr = iterator.EndDataPointer();
buzbee67bf8852011-08-17 17:51:35 -0700478 }
479}
480
Elliott Hughesadb8c672012-03-06 16:49:32 -0800481/* Process instructions with the kBranch flag */
buzbee31a4a6f2012-02-28 15:36:15 -0800482BasicBlock* processCanBranch(CompilationUnit* cUnit, BasicBlock* curBlock,
483 MIR* insn, int curOffset, int width, int flags,
484 const u2* codePtr, const u2* codeEnd)
buzbee67bf8852011-08-17 17:51:35 -0700485{
486 int target = curOffset;
487 switch (insn->dalvikInsn.opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800488 case Instruction::GOTO:
489 case Instruction::GOTO_16:
490 case Instruction::GOTO_32:
buzbee67bf8852011-08-17 17:51:35 -0700491 target += (int) insn->dalvikInsn.vA;
492 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800493 case Instruction::IF_EQ:
494 case Instruction::IF_NE:
495 case Instruction::IF_LT:
496 case Instruction::IF_GE:
497 case Instruction::IF_GT:
498 case Instruction::IF_LE:
buzbee67bf8852011-08-17 17:51:35 -0700499 target += (int) insn->dalvikInsn.vC;
500 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800501 case Instruction::IF_EQZ:
502 case Instruction::IF_NEZ:
503 case Instruction::IF_LTZ:
504 case Instruction::IF_GEZ:
505 case Instruction::IF_GTZ:
506 case Instruction::IF_LEZ:
buzbee67bf8852011-08-17 17:51:35 -0700507 target += (int) insn->dalvikInsn.vB;
508 break;
509 default:
510 LOG(FATAL) << "Unexpected opcode(" << (int)insn->dalvikInsn.opcode
Elliott Hughesadb8c672012-03-06 16:49:32 -0800511 << ") with kBranch set";
buzbee67bf8852011-08-17 17:51:35 -0700512 }
513 BasicBlock *takenBlock = findBlock(cUnit, target,
514 /* split */
515 true,
516 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800517 true,
518 /* immedPredBlockP */
519 &curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700520 curBlock->taken = takenBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800521 oatInsertGrowableList(cUnit, takenBlock->predecessors, (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700522
523 /* Always terminate the current block for conditional branches */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800524 if (flags & Instruction::kContinue) {
buzbee67bf8852011-08-17 17:51:35 -0700525 BasicBlock *fallthroughBlock = findBlock(cUnit,
526 curOffset + width,
527 /*
528 * If the method is processed
529 * in sequential order from the
530 * beginning, we don't need to
531 * specify split for continue
532 * blocks. However, this
533 * routine can be called by
534 * compileLoop, which starts
535 * parsing the method from an
536 * arbitrary address in the
537 * method body.
538 */
539 true,
540 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800541 true,
542 /* immedPredBlockP */
543 &curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700544 curBlock->fallThrough = fallthroughBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800545 oatInsertGrowableList(cUnit, fallthroughBlock->predecessors,
buzbee5abfa3e2012-01-31 17:01:43 -0800546 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700547 } else if (codePtr < codeEnd) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800548 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbee67bf8852011-08-17 17:51:35 -0700549 if (contentIsInsn(codePtr)) {
550 findBlock(cUnit, curOffset + width,
551 /* split */
552 false,
553 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800554 true,
555 /* immedPredBlockP */
556 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700557 }
558 }
buzbeee941e2c2011-12-05 12:38:17 -0800559 return curBlock;
buzbee67bf8852011-08-17 17:51:35 -0700560}
561
Elliott Hughesadb8c672012-03-06 16:49:32 -0800562/* Process instructions with the kSwitch flag */
buzbee31a4a6f2012-02-28 15:36:15 -0800563void processCanSwitch(CompilationUnit* cUnit, BasicBlock* curBlock,
564 MIR* insn, int curOffset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700565{
566 u2* switchData= (u2 *) (cUnit->insns + curOffset +
567 insn->dalvikInsn.vB);
568 int size;
569 int* keyTable;
570 int* targetTable;
571 int i;
572 int firstKey;
573
574 /*
575 * Packed switch data format:
576 * ushort ident = 0x0100 magic value
577 * ushort size number of entries in the table
578 * int first_key first (and lowest) switch case value
579 * int targets[size] branch targets, relative to switch opcode
580 *
581 * Total size is (4+size*2) 16-bit code units.
582 */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800583 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
584 DCHECK_EQ(static_cast<int>(switchData[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbee67bf8852011-08-17 17:51:35 -0700585 size = switchData[1];
586 firstKey = switchData[2] | (switchData[3] << 16);
587 targetTable = (int *) &switchData[4];
588 keyTable = NULL; // Make the compiler happy
589 /*
590 * Sparse switch data format:
591 * ushort ident = 0x0200 magic value
592 * ushort size number of entries in the table; > 0
593 * int keys[size] keys, sorted low-to-high; 32-bit aligned
594 * int targets[size] branch targets, relative to switch opcode
595 *
596 * Total size is (2+size*4) 16-bit code units.
597 */
598 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800599 DCHECK_EQ(static_cast<int>(switchData[0]), static_cast<int>(Instruction::kSparseSwitchSignature));
buzbee67bf8852011-08-17 17:51:35 -0700600 size = switchData[1];
601 keyTable = (int *) &switchData[2];
602 targetTable = (int *) &switchData[2 + size*2];
603 firstKey = 0; // To make the compiler happy
604 }
605
606 if (curBlock->successorBlockList.blockListType != kNotUsed) {
607 LOG(FATAL) << "Successor block list already in use: " <<
608 (int)curBlock->successorBlockList.blockListType;
609 }
610 curBlock->successorBlockList.blockListType =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
buzbee67bf8852011-08-17 17:51:35 -0700612 kPackedSwitch : kSparseSwitch;
buzbeeba938cb2012-02-03 14:47:55 -0800613 oatInitGrowableList(cUnit, &curBlock->successorBlockList.blocks, size,
buzbee5abfa3e2012-01-31 17:01:43 -0800614 kListSuccessorBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700615
616 for (i = 0; i < size; i++) {
617 BasicBlock *caseBlock = findBlock(cUnit, curOffset + targetTable[i],
618 /* split */
619 true,
620 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800621 true,
622 /* immedPredBlockP */
623 &curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700624 SuccessorBlockInfo *successorBlockInfo =
buzbeeba938cb2012-02-03 14:47:55 -0800625 (SuccessorBlockInfo *) oatNew(cUnit, sizeof(SuccessorBlockInfo),
626 false, kAllocSuccessor);
buzbee67bf8852011-08-17 17:51:35 -0700627 successorBlockInfo->block = caseBlock;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 successorBlockInfo->key = (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH)?
buzbee67bf8852011-08-17 17:51:35 -0700629 firstKey + i : keyTable[i];
buzbeeba938cb2012-02-03 14:47:55 -0800630 oatInsertGrowableList(cUnit, &curBlock->successorBlockList.blocks,
buzbee67bf8852011-08-17 17:51:35 -0700631 (intptr_t) successorBlockInfo);
buzbeeba938cb2012-02-03 14:47:55 -0800632 oatInsertGrowableList(cUnit, caseBlock->predecessors,
633 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700634 }
635
636 /* Fall-through case */
637 BasicBlock* fallthroughBlock = findBlock(cUnit,
638 curOffset + width,
639 /* split */
640 false,
641 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800642 true,
643 /* immedPredBlockP */
644 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700645 curBlock->fallThrough = fallthroughBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800646 oatInsertGrowableList(cUnit, fallthroughBlock->predecessors,
647 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700648}
649
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650/* Process instructions with the kThrow flag */
buzbee31a4a6f2012-02-28 15:36:15 -0800651void processCanThrow(CompilationUnit* cUnit, BasicBlock* curBlock, MIR* insn,
652 int curOffset, int width, int flags,
653 ArenaBitVector* tryBlockAddr, const u2* codePtr,
654 const u2* codeEnd)
buzbee67bf8852011-08-17 17:51:35 -0700655{
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800656 const DexFile::CodeItem* code_item = cUnit->code_item;
buzbee67bf8852011-08-17 17:51:35 -0700657
658 /* In try block */
659 if (oatIsBitSet(tryBlockAddr, curOffset)) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800660 CatchHandlerIterator iterator(*code_item, curOffset);
buzbee67bf8852011-08-17 17:51:35 -0700661
buzbee67bf8852011-08-17 17:51:35 -0700662 if (curBlock->successorBlockList.blockListType != kNotUsed) {
663 LOG(FATAL) << "Successor block list already in use: " <<
664 (int)curBlock->successorBlockList.blockListType;
665 }
buzbeee9a72f62011-09-04 17:59:07 -0700666
buzbee67bf8852011-08-17 17:51:35 -0700667 curBlock->successorBlockList.blockListType = kCatch;
buzbeeba938cb2012-02-03 14:47:55 -0800668 oatInitGrowableList(cUnit, &curBlock->successorBlockList.blocks, 2,
buzbee5abfa3e2012-01-31 17:01:43 -0800669 kListSuccessorBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700670
Ian Rogers0571d352011-11-03 19:51:38 -0700671 for (;iterator.HasNext(); iterator.Next()) {
672 BasicBlock *catchBlock = findBlock(cUnit, iterator.GetHandlerAddress(),
buzbeee9a72f62011-09-04 17:59:07 -0700673 false /* split*/,
buzbee9ab05de2012-01-18 15:43:48 -0800674 false /* creat */,
675 NULL /* immedPredBlockP */);
buzbee43a36422011-09-14 14:00:13 -0700676 catchBlock->catchEntry = true;
buzbeeba938cb2012-02-03 14:47:55 -0800677 SuccessorBlockInfo *successorBlockInfo = (SuccessorBlockInfo *)
678 oatNew(cUnit, sizeof(SuccessorBlockInfo), false,
679 kAllocSuccessor);
buzbee67bf8852011-08-17 17:51:35 -0700680 successorBlockInfo->block = catchBlock;
Ian Rogers0571d352011-11-03 19:51:38 -0700681 successorBlockInfo->key = iterator.GetHandlerTypeIndex();
buzbeeba938cb2012-02-03 14:47:55 -0800682 oatInsertGrowableList(cUnit, &curBlock->successorBlockList.blocks,
buzbee67bf8852011-08-17 17:51:35 -0700683 (intptr_t) successorBlockInfo);
buzbeeba938cb2012-02-03 14:47:55 -0800684 oatInsertGrowableList(cUnit, catchBlock->predecessors,
685 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700686 }
687 } else {
buzbee5abfa3e2012-01-31 17:01:43 -0800688 BasicBlock *ehBlock = oatNewBB(cUnit, kExceptionHandling,
689 cUnit->numBlocks++);
buzbee67bf8852011-08-17 17:51:35 -0700690 curBlock->taken = ehBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800691 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t) ehBlock);
buzbee67bf8852011-08-17 17:51:35 -0700692 ehBlock->startOffset = curOffset;
buzbeeba938cb2012-02-03 14:47:55 -0800693 oatInsertGrowableList(cUnit, ehBlock->predecessors, (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700694 }
695
696 /*
697 * Force the current block to terminate.
698 *
699 * Data may be present before codeEnd, so we need to parse it to know
700 * whether it is code or data.
701 */
702 if (codePtr < codeEnd) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800703 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbee67bf8852011-08-17 17:51:35 -0700704 if (contentIsInsn(codePtr)) {
705 BasicBlock *fallthroughBlock = findBlock(cUnit,
706 curOffset + width,
707 /* split */
708 false,
709 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800710 true,
711 /* immedPredBlockP */
712 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700713 /*
Elliott Hughesadb8c672012-03-06 16:49:32 -0800714 * THROW is an unconditional branch. NOTE:
715 * THROW_VERIFICATION_ERROR is also an unconditional
buzbee510c6052011-10-27 10:47:20 -0700716 * branch, but we shouldn't treat it as such until we have
717 * a dead code elimination pass (which won't be important
718 * until inlining w/ constant propogation is implemented.
buzbee67bf8852011-08-17 17:51:35 -0700719 */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800720 if (insn->dalvikInsn.opcode != Instruction::THROW) {
buzbee67bf8852011-08-17 17:51:35 -0700721 curBlock->fallThrough = fallthroughBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800722 oatInsertGrowableList(cUnit, fallthroughBlock->predecessors,
buzbee5abfa3e2012-01-31 17:01:43 -0800723 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700724 }
725 }
726 }
727}
728
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800729void oatInit(CompilationUnit* cUnit, const Compiler& compiler) {
730 if (!oatArchInit()) {
731 LOG(FATAL) << "Failed to initialize oat";
732 }
733 if (!oatHeapInit(cUnit)) {
734 LOG(FATAL) << "Failed to initialize oat heap";
735 }
736}
737
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700738CompiledMethod* oatCompileMethod(Compiler& compiler,
739 const DexFile::CodeItem* code_item,
740 uint32_t access_flags, uint32_t method_idx,
741 const ClassLoader* class_loader,
742 const DexFile& dex_file)
buzbee67bf8852011-08-17 17:51:35 -0700743{
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800744 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700745
buzbeec143c552011-08-20 17:38:58 -0700746 const u2* codePtr = code_item->insns_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 const u2* codeEnd = code_item->insns_ + code_item->insns_size_in_code_units_;
buzbee67bf8852011-08-17 17:51:35 -0700748 int numBlocks = 0;
749 unsigned int curOffset = 0;
750
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800751 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700752 UniquePtr<CompilationUnit> cUnit(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800753
754 oatInit(cUnit.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800755
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700756 cUnit->compiler = &compiler;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800757 cUnit->class_linker = class_linker;
758 cUnit->dex_file = &dex_file;
759 cUnit->dex_cache = class_linker->FindDexCache(dex_file);
760 cUnit->method_idx = method_idx;
761 cUnit->code_item = code_item;
762 cUnit->access_flags = access_flags;
763 cUnit->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800764 cUnit->instructionSet = compiler.GetInstructionSet();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700765 cUnit->insns = code_item->insns_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 cUnit->insnsSize = code_item->insns_size_in_code_units_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800767 cUnit->numIns = code_item->ins_size_;
768 cUnit->numRegs = code_item->registers_size_ - cUnit->numIns;
769 cUnit->numOuts = code_item->outs_size_;
770 /* Adjust this value accordingly once inlining is performed */
771 cUnit->numDalvikRegisters = code_item->registers_size_;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700772 // TODO: set this from command line
buzbeeba938cb2012-02-03 14:47:55 -0800773 cUnit->compilerFlipMatch = false;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700774 bool useMatch = !cUnit->compilerMethodMatch.empty();
buzbeeba938cb2012-02-03 14:47:55 -0800775 bool match = useMatch && (cUnit->compilerFlipMatch ^
Elliott Hughese52e49b2012-04-02 16:05:44 -0700776 (PrettyMethod(method_idx, dex_file).find(cUnit->compilerMethodMatch) != std::string::npos));
buzbeece302932011-10-04 14:32:18 -0700777 if (!useMatch || match) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700778 cUnit->disableOpt = kCompilerOptimizerDisableFlags;
779 cUnit->enableDebug = kCompilerDebugFlags;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800780 cUnit->printMe = VLOG_IS_ON(compiler) || (cUnit->enableDebug & (1 << kDebugVerbose));
buzbeece302932011-10-04 14:32:18 -0700781 }
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700782 if (cUnit->instructionSet == kX86) {
783 // Disable optimizations on X86 for now
784 cUnit->disableOpt = -1;
785 }
buzbee44b412b2012-02-04 08:50:53 -0800786 /* Are we generating code for the debugger? */
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800787 if (compiler.IsDebuggingSupported()) {
buzbee44b412b2012-02-04 08:50:53 -0800788 cUnit->genDebugger = true;
789 // Yes, disable most optimizations
790 cUnit->disableOpt |= (
791 (1 << kLoadStoreElimination) |
792 (1 << kLoadHoisting) |
793 (1 << kSuppressLoads) |
794 (1 << kPromoteRegs) |
buzbeeb37c9992012-03-18 21:28:43 -0700795 (1 << kBBOpt) |
buzbee16da88c2012-03-20 10:38:17 -0700796 (1 << kMatch) |
buzbee44b412b2012-02-04 08:50:53 -0800797 (1 << kTrackLiveTemps));
798 }
799
buzbeea7c12682012-03-19 13:13:53 -0700800 /* Gathering opcode stats? */
Elliott Hughese52e49b2012-04-02 16:05:44 -0700801 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbeea7c12682012-03-19 13:13:53 -0700802 cUnit->opcodeCount = (int*)oatNew(cUnit.get(),
803 kNumPackedOpcodes * sizeof(int), true, kAllocMisc);
804 }
805
buzbeecefd1872011-09-09 09:59:52 -0700806 /* Assume non-throwing leaf */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700807 cUnit->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
buzbeecefd1872011-09-09 09:59:52 -0700808
buzbee5abfa3e2012-01-31 17:01:43 -0800809 /* Initialize the block list, estimate size based on insnsSize */
buzbeeba938cb2012-02-03 14:47:55 -0800810 oatInitGrowableList(cUnit.get(), &cUnit->blockList, cUnit->insnsSize,
811 kListBlockList);
buzbee67bf8852011-08-17 17:51:35 -0700812
813 /* Initialize the switchTables list */
buzbeeba938cb2012-02-03 14:47:55 -0800814 oatInitGrowableList(cUnit.get(), &cUnit->switchTables, 4,
815 kListSwitchTables);
buzbee67bf8852011-08-17 17:51:35 -0700816
817 /* Intialize the fillArrayData list */
buzbeeba938cb2012-02-03 14:47:55 -0800818 oatInitGrowableList(cUnit.get(), &cUnit->fillArrayData, 4,
819 kListFillArrayData);
buzbee67bf8852011-08-17 17:51:35 -0700820
buzbee5abfa3e2012-01-31 17:01:43 -0800821 /* Intialize the throwLaunchpads list, estimate size based on insnsSize */
buzbeeba938cb2012-02-03 14:47:55 -0800822 oatInitGrowableList(cUnit.get(), &cUnit->throwLaunchpads, cUnit->insnsSize,
buzbee5abfa3e2012-01-31 17:01:43 -0800823 kListThrowLaunchPads);
buzbee5ade1d22011-09-09 14:44:52 -0700824
buzbeefc9e6fa2012-03-23 15:14:29 -0700825 /* Intialize the instrinsicLaunchpads list */
826 oatInitGrowableList(cUnit.get(), &cUnit->intrinsicLaunchpads, 4,
827 kListMisc);
828
829
buzbeec1f45042011-09-21 16:03:19 -0700830 /* Intialize the suspendLaunchpads list */
buzbeeba938cb2012-02-03 14:47:55 -0800831 oatInitGrowableList(cUnit.get(), &cUnit->suspendLaunchpads, 2048,
buzbee5abfa3e2012-01-31 17:01:43 -0800832 kListSuspendLaunchPads);
buzbeec1f45042011-09-21 16:03:19 -0700833
buzbee67bf8852011-08-17 17:51:35 -0700834 /* Allocate the bit-vector to track the beginning of basic blocks */
buzbeeba938cb2012-02-03 14:47:55 -0800835 ArenaBitVector *tryBlockAddr = oatAllocBitVector(cUnit.get(),
836 cUnit->insnsSize,
buzbee67bf8852011-08-17 17:51:35 -0700837 true /* expandable */);
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700838 cUnit->tryBlockAddr = tryBlockAddr;
buzbee67bf8852011-08-17 17:51:35 -0700839
840 /* Create the default entry and exit blocks and enter them to the list */
buzbee5abfa3e2012-01-31 17:01:43 -0800841 BasicBlock *entryBlock = oatNewBB(cUnit.get(), kEntryBlock, numBlocks++);
842 BasicBlock *exitBlock = oatNewBB(cUnit.get(), kExitBlock, numBlocks++);
buzbee67bf8852011-08-17 17:51:35 -0700843
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700844 cUnit->entryBlock = entryBlock;
845 cUnit->exitBlock = exitBlock;
buzbee67bf8852011-08-17 17:51:35 -0700846
buzbeeba938cb2012-02-03 14:47:55 -0800847 oatInsertGrowableList(cUnit.get(), &cUnit->blockList, (intptr_t) entryBlock);
848 oatInsertGrowableList(cUnit.get(), &cUnit->blockList, (intptr_t) exitBlock);
buzbee67bf8852011-08-17 17:51:35 -0700849
850 /* Current block to record parsed instructions */
buzbee5abfa3e2012-01-31 17:01:43 -0800851 BasicBlock *curBlock = oatNewBB(cUnit.get(), kDalvikByteCode, numBlocks++);
buzbee67bf8852011-08-17 17:51:35 -0700852 curBlock->startOffset = 0;
buzbeeba938cb2012-02-03 14:47:55 -0800853 oatInsertGrowableList(cUnit.get(), &cUnit->blockList, (intptr_t) curBlock);
buzbee5b537102012-01-17 17:33:47 -0800854 /* Add first block to the fast lookup cache */
Elliott Hughesa0e18062012-04-13 15:59:59 -0700855 cUnit->blockMap.Put(curBlock->startOffset, curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700856 entryBlock->fallThrough = curBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800857 oatInsertGrowableList(cUnit.get(), curBlock->predecessors,
858 (intptr_t)entryBlock);
buzbee67bf8852011-08-17 17:51:35 -0700859
860 /*
861 * Store back the number of blocks since new blocks may be created of
862 * accessing cUnit.
863 */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700864 cUnit->numBlocks = numBlocks;
buzbee67bf8852011-08-17 17:51:35 -0700865
866 /* Identify code range in try blocks and set up the empty catch blocks */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700867 processTryCatchBlocks(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -0700868
buzbee16da88c2012-03-20 10:38:17 -0700869 /* Set up for simple method detection */
870 int numPatterns = sizeof(specialPatterns)/sizeof(specialPatterns[0]);
871 bool livePattern = (numPatterns > 0) && !(cUnit->disableOpt & (1 << kMatch));
872 bool* deadPattern = (bool*)oatNew(cUnit.get(), sizeof(bool) * numPatterns,
873 kAllocMisc);
874 SpecialCaseHandler specialCase = kNoHandler;
875 int patternPos = 0;
876
buzbee67bf8852011-08-17 17:51:35 -0700877 /* Parse all instructions and put them into containing basic blocks */
878 while (codePtr < codeEnd) {
buzbeeba938cb2012-02-03 14:47:55 -0800879 MIR *insn = (MIR *) oatNew(cUnit.get(), sizeof(MIR), true, kAllocMIR);
buzbee67bf8852011-08-17 17:51:35 -0700880 insn->offset = curOffset;
buzbeeba938cb2012-02-03 14:47:55 -0800881 int width = parseInsn(cUnit.get(), codePtr, &insn->dalvikInsn, false);
buzbee67bf8852011-08-17 17:51:35 -0700882 insn->width = width;
buzbee16da88c2012-03-20 10:38:17 -0700883 Instruction::Code opcode = insn->dalvikInsn.opcode;
buzbeea7c12682012-03-19 13:13:53 -0700884 if (cUnit->opcodeCount != NULL) {
buzbee16da88c2012-03-20 10:38:17 -0700885 cUnit->opcodeCount[static_cast<int>(opcode)]++;
buzbeea7c12682012-03-19 13:13:53 -0700886 }
buzbee67bf8852011-08-17 17:51:35 -0700887
888 /* Terminate when the data section is seen */
889 if (width == 0)
890 break;
891
buzbee16da88c2012-03-20 10:38:17 -0700892 /* Possible simple method? */
893 if (livePattern) {
894 livePattern = false;
895 specialCase = kNoHandler;
896 for (int i = 0; i < numPatterns; i++) {
897 if (!deadPattern[i]) {
898 if (specialPatterns[i].opcodes[patternPos] == opcode) {
899 livePattern = true;
900 specialCase = specialPatterns[i].handlerCode;
901 } else {
902 deadPattern[i] = true;
903 }
904 }
905 }
906 patternPos++;
907 }
908
buzbee67bf8852011-08-17 17:51:35 -0700909 oatAppendMIR(curBlock, insn);
910
911 codePtr += width;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800912 int flags = Instruction::Flags(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700913
buzbee5abfa3e2012-01-31 17:01:43 -0800914 int dfFlags = oatDataFlowAttributes[insn->dalvikInsn.opcode];
915
916 if (dfFlags & DF_HAS_DEFS) {
917 cUnit->defCount += (dfFlags & DF_DA_WIDE) ? 2 : 1;
918 }
buzbee99ba9642012-01-25 14:23:14 -0800919
Elliott Hughesadb8c672012-03-06 16:49:32 -0800920 if (flags & Instruction::kBranch) {
buzbeee941e2c2011-12-05 12:38:17 -0800921 curBlock = processCanBranch(cUnit.get(), curBlock, insn, curOffset,
922 width, flags, codePtr, codeEnd);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800923 } else if (flags & Instruction::kReturn) {
buzbee67bf8852011-08-17 17:51:35 -0700924 curBlock->fallThrough = exitBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800925 oatInsertGrowableList(cUnit.get(), exitBlock->predecessors,
926 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700927 /*
928 * Terminate the current block if there are instructions
929 * afterwards.
930 */
931 if (codePtr < codeEnd) {
932 /*
933 * Create a fallthrough block for real instructions
Elliott Hughesadb8c672012-03-06 16:49:32 -0800934 * (incl. NOP).
buzbee67bf8852011-08-17 17:51:35 -0700935 */
936 if (contentIsInsn(codePtr)) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700937 findBlock(cUnit.get(), curOffset + width,
buzbee67bf8852011-08-17 17:51:35 -0700938 /* split */
939 false,
940 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800941 true,
942 /* immedPredBlockP */
943 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700944 }
945 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800946 } else if (flags & Instruction::kThrow) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700947 processCanThrow(cUnit.get(), curBlock, insn, curOffset, width, flags,
buzbee67bf8852011-08-17 17:51:35 -0700948 tryBlockAddr, codePtr, codeEnd);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800949 } else if (flags & Instruction::kSwitch) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700950 processCanSwitch(cUnit.get(), curBlock, insn, curOffset, width, flags);
buzbee67bf8852011-08-17 17:51:35 -0700951 }
952 curOffset += width;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700953 BasicBlock *nextBlock = findBlock(cUnit.get(), curOffset,
buzbee67bf8852011-08-17 17:51:35 -0700954 /* split */
955 false,
956 /* create */
buzbee9ab05de2012-01-18 15:43:48 -0800957 false,
958 /* immedPredBlockP */
959 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700960 if (nextBlock) {
961 /*
962 * The next instruction could be the target of a previously parsed
963 * forward branch so a block is already created. If the current
964 * instruction is not an unconditional branch, connect them through
965 * the fall-through link.
966 */
buzbeeed3e9302011-09-23 17:34:19 -0700967 DCHECK(curBlock->fallThrough == NULL ||
buzbee67bf8852011-08-17 17:51:35 -0700968 curBlock->fallThrough == nextBlock ||
969 curBlock->fallThrough == exitBlock);
970
Elliott Hughesadb8c672012-03-06 16:49:32 -0800971 if ((curBlock->fallThrough == NULL) && (flags & Instruction::kContinue)) {
buzbee67bf8852011-08-17 17:51:35 -0700972 curBlock->fallThrough = nextBlock;
buzbeeba938cb2012-02-03 14:47:55 -0800973 oatInsertGrowableList(cUnit.get(), nextBlock->predecessors,
buzbee5abfa3e2012-01-31 17:01:43 -0800974 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700975 }
976 curBlock = nextBlock;
977 }
978 }
979
buzbee5abfa3e2012-01-31 17:01:43 -0800980 if (!(cUnit->disableOpt & (1 << kSkipLargeMethodOptimization))) {
buzbee99ba9642012-01-25 14:23:14 -0800981 if ((cUnit->numBlocks > MANY_BLOCKS) ||
982 ((cUnit->numBlocks > MANY_BLOCKS_INITIALIZER) &&
buzbeefc9e6fa2012-03-23 15:14:29 -0700983 PrettyMethod(method_idx, dex_file, false).find("init>") !=
buzbee99ba9642012-01-25 14:23:14 -0800984 std::string::npos)) {
buzbeea7c12682012-03-19 13:13:53 -0700985 cUnit->qdMode = true;
986 }
987 }
988
989 if (cUnit->qdMode) {
990 cUnit->disableDataflow = true;
991 // Disable optimization which require dataflow/ssa
992 cUnit->disableOpt |=
993 (1 << kNullCheckElimination) |
994 (1 << kBBOpt) |
995 (1 << kPromoteRegs);
996 if (cUnit->printMe) {
997 LOG(INFO) << "QD mode enabled: "
998 << PrettyMethod(method_idx, dex_file)
999 << " too big: " << cUnit->numBlocks;
buzbee99ba9642012-01-25 14:23:14 -08001000 }
1001 }
1002
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001003 if (cUnit->printMe) {
1004 oatDumpCompilationUnit(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001005 }
1006
buzbee5b537102012-01-17 17:33:47 -08001007 if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) {
1008 /* Verify if all blocks are connected as claimed */
buzbee5abfa3e2012-01-31 17:01:43 -08001009 oatDataFlowAnalysisDispatcher(cUnit.get(), verifyPredInfo, kAllNodes,
1010 false /* isIterative */);
buzbee5b537102012-01-17 17:33:47 -08001011 }
buzbee67bf8852011-08-17 17:51:35 -07001012
1013 /* Perform SSA transformation for the whole method */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001014 oatMethodSSATransformation(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001015
buzbee239c4e72012-03-16 08:42:29 -07001016 /* Detect loops */
1017 oatMethodLoopDetection(cUnit.get());
1018
1019 /* Count uses */
1020 oatMethodUseCount(cUnit.get());
1021
buzbee43a36422011-09-14 14:00:13 -07001022 /* Perform null check elimination */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001023 oatMethodNullCheckElimination(cUnit.get());
buzbee43a36422011-09-14 14:00:13 -07001024
buzbeee1965672012-03-11 18:39:19 -07001025 /* Do some basic block optimizations */
1026 oatMethodBasicBlockOptimization(cUnit.get());
buzbeee1965672012-03-11 18:39:19 -07001027
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001028 oatInitializeRegAlloc(cUnit.get()); // Needs to happen after SSA naming
buzbee67bf8852011-08-17 17:51:35 -07001029
1030 /* Allocate Registers using simple local allocation scheme */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001031 oatSimpleRegAlloc(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001032
buzbee16da88c2012-03-20 10:38:17 -07001033 if (specialCase != kNoHandler) {
1034 /*
1035 * Custom codegen for special cases. If for any reason the
1036 * special codegen doesn't success, cUnit->firstLIRInsn will
1037 * set to NULL;
1038 */
1039 oatSpecialMIR2LIR(cUnit.get(), specialCase);
1040 }
1041
buzbee67bf8852011-08-17 17:51:35 -07001042 /* Convert MIR to LIR, etc. */
buzbee16da88c2012-03-20 10:38:17 -07001043 if (cUnit->firstLIRInsn == NULL) {
1044 oatMethodMIR2LIR(cUnit.get());
1045 }
buzbee67bf8852011-08-17 17:51:35 -07001046
1047 // Debugging only
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001048 if (cUnit->enableDebug & (1 << kDebugDumpCFG)) {
1049 oatDumpCFG(cUnit.get(), "/sdcard/cfg/");
buzbeeec5adf32011-09-11 15:25:43 -07001050 }
buzbee67bf8852011-08-17 17:51:35 -07001051
1052 /* Method is not empty */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001053 if (cUnit->firstLIRInsn) {
buzbee67bf8852011-08-17 17:51:35 -07001054
1055 // mark the targets of switch statement case labels
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001056 oatProcessSwitchTables(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001057
1058 /* Convert LIR into machine code. */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001059 oatAssembleLIR(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001060
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001061 if (cUnit->printMe) {
1062 oatCodegenDump(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001063 }
buzbeea7c12682012-03-19 13:13:53 -07001064
1065 if (cUnit->opcodeCount != NULL) {
1066 LOG(INFO) << "Opcode Count";
1067 for (int i = 0; i < kNumPackedOpcodes; i++) {
1068 if (cUnit->opcodeCount[i] != 0) {
1069 LOG(INFO) << "-C- "
1070 <<Instruction::Name(static_cast<Instruction::Code>(i))
1071 << " " << cUnit->opcodeCount[i];
1072 }
1073 }
1074 }
buzbee67bf8852011-08-17 17:51:35 -07001075 }
1076
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001077 // Combine vmap tables - core regs, then fp regs - into vmapTable
1078 std::vector<uint16_t> vmapTable;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001079 for (size_t i = 0 ; i < cUnit->coreVmapTable.size(); i++) {
1080 vmapTable.push_back(cUnit->coreVmapTable[i]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001081 }
buzbee16da88c2012-03-20 10:38:17 -07001082 // If we have a frame, push a marker to take place of lr
1083 if (cUnit->frameSize > 0) {
1084 vmapTable.push_back(INVALID_VREG);
1085 } else {
1086 DCHECK_EQ(__builtin_popcount(cUnit->coreSpillMask), 0);
1087 DCHECK_EQ(__builtin_popcount(cUnit->fpSpillMask), 0);
1088 }
buzbeec41e5b52011-09-23 12:46:19 -07001089 // Combine vmap tables - core regs, then fp regs
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001090 for (uint32_t i = 0; i < cUnit->fpVmapTable.size(); i++) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001091 vmapTable.push_back(cUnit->fpVmapTable[i]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001092 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001093 CompiledMethod* result = new CompiledMethod(cUnit->instructionSet, cUnit->codeBuffer,
Ian Rogers0571d352011-11-03 19:51:38 -07001094 cUnit->frameSize, cUnit->coreSpillMask,
1095 cUnit->fpSpillMask, cUnit->mappingTable,
1096 vmapTable);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001097
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001098 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
buzbee5abfa3e2012-01-31 17:01:43 -08001099 << " (" << (cUnit->codeBuffer.size() * sizeof(cUnit->codeBuffer[0]))
1100 << " bytes)";
1101
1102#ifdef WITH_MEMSTATS
1103 if (cUnit->enableDebug & (1 << kDebugShowMemoryUsage)) {
1104 oatDumpMemStats(cUnit.get());
1105 }
1106#endif
buzbee67bf8852011-08-17 17:51:35 -07001107
buzbeeba938cb2012-02-03 14:47:55 -08001108 oatArenaReset(cUnit.get());
1109
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001110 return result;
buzbee67bf8852011-08-17 17:51:35 -07001111}
1112
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001113} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001114
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -07001115extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001116 const art::DexFile::CodeItem* code_item,
1117 uint32_t access_flags, uint32_t method_idx,
1118 const art::ClassLoader* class_loader,
1119 const art::DexFile& dex_file)
1120{
1121 CHECK_EQ(compiler.GetInstructionSet(), art::oatInstructionSet());
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -07001122 return art::oatCompileMethod(compiler, code_item, access_flags, method_idx, class_loader, dex_file);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001123}