blob: ebbd72f79cef10ffbf222fbbf0d6e12b2d00653a [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"
buzbeec143c552011-08-20 17:38:58 -070020#include "constants.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
buzbeece302932011-10-04 14:32:18 -070024/* Default optimizer/debug setting for the compiler. */
25uint32_t compilerOptimizerDisableFlags = 0 | // Disable specific optimizations
26 // TODO: enable all of these by default in ToT
27 (1 << kLoadStoreElimination) |
28 (1 << kLoadHoisting) |
29 (1 << kSuppressLoads) |
30 (1 << kNullCheckElimination) |
31 (1 << kPromoteRegs) |
32 (1 << kTrackLiveTemps) |
33 0;
34
35uint32_t compilerDebugFlags = 0 | // Enable debug/testing modes
36 // TODO: disable all of these by default in ToT
37 (1 << kDebugDisplayMissingTargets) |
38 //(1 << kDebugVerbose) |
39 //(1 << kDebugDumpCFG) |
40 //(1 << kDebugSlowFieldPath) |
41 //(1 << kDebugSlowInvokePath) |
42 //(1 << kDebugSlowStringPath) |
43 //(1 << kDebugSlowestFieldPath) |
44 //(1 << kDebugSlowestStringPath) |
45 0;
46
47std::string compilerMethodMatch; // Method name match to apply above flags
48
49bool compilerFlipMatch = false; // Reverses sense of method name match
50
buzbeeed3e9302011-09-23 17:34:19 -070051STATIC inline bool contentIsInsn(const u2* codePtr) {
buzbee67bf8852011-08-17 17:51:35 -070052 u2 instr = *codePtr;
53 Opcode opcode = (Opcode)(instr & 0xff);
54
55 /*
56 * Since the low 8-bit in metadata may look like OP_NOP, we need to check
57 * both the low and whole sub-word to determine whether it is code or data.
58 */
59 return (opcode != OP_NOP || instr == 0);
60}
61
62/*
63 * Parse an instruction, return the length of the instruction
64 */
buzbeeed3e9302011-09-23 17:34:19 -070065STATIC inline int parseInsn(const u2* codePtr, DecodedInstruction* decInsn,
buzbee67bf8852011-08-17 17:51:35 -070066 bool printMe)
67{
68 // Don't parse instruction data
69 if (!contentIsInsn(codePtr)) {
70 return 0;
71 }
72
73 u2 instr = *codePtr;
74 Opcode opcode = dexOpcodeFromCodeUnit(instr);
75
76 dexDecodeInstruction(codePtr, decInsn);
77 if (printMe) {
78 char *decodedString = oatGetDalvikDisassembly(decInsn, NULL);
79 LOG(INFO) << codePtr << ": 0x" << std::hex << (int)opcode <<
80 " " << decodedString;
81 }
82 return dexGetWidthFromOpcode(opcode);
83}
84
85#define UNKNOWN_TARGET 0xffffffff
86
buzbeeed3e9302011-09-23 17:34:19 -070087STATIC inline bool isGoto(MIR* insn)
buzbee67bf8852011-08-17 17:51:35 -070088{
89 switch (insn->dalvikInsn.opcode) {
90 case OP_GOTO:
91 case OP_GOTO_16:
92 case OP_GOTO_32:
93 return true;
94 default:
95 return false;
96 }
97}
98
99/*
100 * Identify unconditional branch instructions
101 */
buzbeeed3e9302011-09-23 17:34:19 -0700102STATIC inline bool isUnconditionalBranch(MIR* insn)
buzbee67bf8852011-08-17 17:51:35 -0700103{
104 switch (insn->dalvikInsn.opcode) {
105 case OP_RETURN_VOID:
106 case OP_RETURN:
107 case OP_RETURN_WIDE:
108 case OP_RETURN_OBJECT:
109 return true;
110 default:
111 return isGoto(insn);
112 }
113}
114
115/* Split an existing block from the specified code offset into two */
buzbeeed3e9302011-09-23 17:34:19 -0700116STATIC BasicBlock *splitBlock(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -0700117 unsigned int codeOffset,
118 BasicBlock* origBlock)
119{
120 MIR* insn = origBlock->firstMIRInsn;
121 while (insn) {
122 if (insn->offset == codeOffset) break;
123 insn = insn->next;
124 }
125 if (insn == NULL) {
126 LOG(FATAL) << "Break split failed";
127 }
128 BasicBlock *bottomBlock = oatNewBB(kDalvikByteCode,
129 cUnit->numBlocks++);
130 oatInsertGrowableList(&cUnit->blockList, (intptr_t) bottomBlock);
131
132 bottomBlock->startOffset = codeOffset;
133 bottomBlock->firstMIRInsn = insn;
134 bottomBlock->lastMIRInsn = origBlock->lastMIRInsn;
135
136 /* Handle the taken path */
137 bottomBlock->taken = origBlock->taken;
138 if (bottomBlock->taken) {
139 origBlock->taken = NULL;
140 oatClearBit(bottomBlock->taken->predecessors, origBlock->id);
141 oatSetBit(bottomBlock->taken->predecessors, bottomBlock->id);
142 }
143
144 /* Handle the fallthrough path */
145 bottomBlock->needFallThroughBranch = origBlock->needFallThroughBranch;
146 bottomBlock->fallThrough = origBlock->fallThrough;
147 origBlock->fallThrough = bottomBlock;
148 origBlock->needFallThroughBranch = true;
149 oatSetBit(bottomBlock->predecessors, origBlock->id);
150 if (bottomBlock->fallThrough) {
151 oatClearBit(bottomBlock->fallThrough->predecessors,
152 origBlock->id);
153 oatSetBit(bottomBlock->fallThrough->predecessors,
154 bottomBlock->id);
155 }
156
157 /* Handle the successor list */
158 if (origBlock->successorBlockList.blockListType != kNotUsed) {
159 bottomBlock->successorBlockList = origBlock->successorBlockList;
160 origBlock->successorBlockList.blockListType = kNotUsed;
161 GrowableListIterator iterator;
162
163 oatGrowableListIteratorInit(&bottomBlock->successorBlockList.blocks,
164 &iterator);
165 while (true) {
166 SuccessorBlockInfo *successorBlockInfo =
167 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
168 if (successorBlockInfo == NULL) break;
169 BasicBlock *bb = successorBlockInfo->block;
170 oatClearBit(bb->predecessors, origBlock->id);
171 oatSetBit(bb->predecessors, bottomBlock->id);
172 }
173 }
174
175 origBlock->lastMIRInsn = insn->prev;
176
177 insn->prev->next = NULL;
178 insn->prev = NULL;
179 return bottomBlock;
180}
181
182/*
183 * Given a code offset, find out the block that starts with it. If the offset
184 * is in the middle of an existing block, split it into two.
185 */
buzbeeed3e9302011-09-23 17:34:19 -0700186STATIC BasicBlock *findBlock(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -0700187 unsigned int codeOffset,
188 bool split, bool create)
189{
190 GrowableList* blockList = &cUnit->blockList;
191 BasicBlock* bb;
192 unsigned int i;
193
194 for (i = 0; i < blockList->numUsed; i++) {
195 bb = (BasicBlock *) blockList->elemList[i];
196 if (bb->blockType != kDalvikByteCode) continue;
197 if (bb->startOffset == codeOffset) return bb;
198 /* Check if a branch jumps into the middle of an existing block */
199 if ((split == true) && (codeOffset > bb->startOffset) &&
200 (bb->lastMIRInsn != NULL) &&
201 (codeOffset <= bb->lastMIRInsn->offset)) {
202 BasicBlock *newBB = splitBlock(cUnit, codeOffset, bb);
203 return newBB;
204 }
205 }
206 if (create) {
207 bb = oatNewBB(kDalvikByteCode, cUnit->numBlocks++);
208 oatInsertGrowableList(&cUnit->blockList, (intptr_t) bb);
209 bb->startOffset = codeOffset;
210 return bb;
211 }
212 return NULL;
213}
214
215/* Dump the CFG into a DOT graph */
216void oatDumpCFG(CompilationUnit* cUnit, const char* dirPrefix)
217{
buzbee67bf8852011-08-17 17:51:35 -0700218 FILE* file;
buzbeedfd3d702011-08-28 12:56:51 -0700219 std::string name = art::PrettyMethod(cUnit->method);
buzbee67bf8852011-08-17 17:51:35 -0700220 char startOffset[80];
221 sprintf(startOffset, "_%x", cUnit->entryBlock->fallThrough->startOffset);
222 char* fileName = (char *) oatNew(
buzbeec143c552011-08-20 17:38:58 -0700223 strlen(dirPrefix) +
224 name.length() +
225 strlen(".dot") + 1, true);
226 sprintf(fileName, "%s%s%s.dot", dirPrefix, name.c_str(), startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700227
228 /*
229 * Convert the special characters into a filesystem- and shell-friendly
230 * format.
231 */
232 int i;
233 for (i = strlen(dirPrefix); fileName[i]; i++) {
234 if (fileName[i] == '/') {
235 fileName[i] = '_';
236 } else if (fileName[i] == ';') {
237 fileName[i] = '#';
238 } else if (fileName[i] == '$') {
239 fileName[i] = '+';
240 } else if (fileName[i] == '(' || fileName[i] == ')') {
241 fileName[i] = '@';
242 } else if (fileName[i] == '<' || fileName[i] == '>') {
243 fileName[i] = '=';
244 }
245 }
246 file = fopen(fileName, "w");
247 if (file == NULL) {
248 return;
249 }
250 fprintf(file, "digraph G {\n");
251
252 fprintf(file, " rankdir=TB\n");
253
254 int numReachableBlocks = cUnit->numReachableBlocks;
255 int idx;
256 const GrowableList *blockList = &cUnit->blockList;
257
258 for (idx = 0; idx < numReachableBlocks; idx++) {
259 int blockIdx = cUnit->dfsOrder.elemList[idx];
260 BasicBlock *bb = (BasicBlock *) oatGrowableListGetElement(blockList,
261 blockIdx);
262 if (bb == NULL) break;
263 if (bb->blockType == kEntryBlock) {
264 fprintf(file, " entry [shape=Mdiamond];\n");
265 } else if (bb->blockType == kExitBlock) {
266 fprintf(file, " exit [shape=Mdiamond];\n");
267 } else if (bb->blockType == kDalvikByteCode) {
268 fprintf(file, " block%04x [shape=record,label = \"{ \\\n",
269 bb->startOffset);
270 const MIR *mir;
271 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
272 bb->firstMIRInsn ? " | " : " ");
273 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
274 fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset,
275 mir->ssaRep ?
276 oatFullDisassembler(cUnit, mir) :
277 dexGetOpcodeName(mir->dalvikInsn.opcode),
278 mir->next ? " | " : " ");
279 }
280 fprintf(file, " }\"];\n\n");
281 } else if (bb->blockType == kExceptionHandling) {
282 char blockName[BLOCK_NAME_LEN];
283
284 oatGetBlockName(bb, blockName);
285 fprintf(file, " %s [shape=invhouse];\n", blockName);
286 }
287
288 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
289
290 if (bb->taken) {
291 oatGetBlockName(bb, blockName1);
292 oatGetBlockName(bb->taken, blockName2);
293 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
294 blockName1, blockName2);
295 }
296 if (bb->fallThrough) {
297 oatGetBlockName(bb, blockName1);
298 oatGetBlockName(bb->fallThrough, blockName2);
299 fprintf(file, " %s:s -> %s:n\n", blockName1, blockName2);
300 }
301
302 if (bb->successorBlockList.blockListType != kNotUsed) {
303 fprintf(file, " succ%04x [shape=%s,label = \"{ \\\n",
304 bb->startOffset,
305 (bb->successorBlockList.blockListType == kCatch) ?
306 "Mrecord" : "record");
307 GrowableListIterator iterator;
308 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
309 &iterator);
310 SuccessorBlockInfo *successorBlockInfo =
311 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
312
313 int succId = 0;
314 while (true) {
315 if (successorBlockInfo == NULL) break;
316
317 BasicBlock *destBlock = successorBlockInfo->block;
318 SuccessorBlockInfo *nextSuccessorBlockInfo =
319 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
320
321 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
322 succId++,
323 successorBlockInfo->key,
324 destBlock->startOffset,
325 (nextSuccessorBlockInfo != NULL) ? " | " : " ");
326
327 successorBlockInfo = nextSuccessorBlockInfo;
328 }
329 fprintf(file, " }\"];\n\n");
330
331 oatGetBlockName(bb, blockName1);
332 fprintf(file, " %s:s -> succ%04x:n [style=dashed]\n",
333 blockName1, bb->startOffset);
334
335 if (bb->successorBlockList.blockListType == kPackedSwitch ||
336 bb->successorBlockList.blockListType == kSparseSwitch) {
337
338 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
339 &iterator);
340
341 succId = 0;
342 while (true) {
343 SuccessorBlockInfo *successorBlockInfo =
344 (SuccessorBlockInfo *)
345 oatGrowableListIteratorNext(&iterator);
346 if (successorBlockInfo == NULL) break;
347
348 BasicBlock *destBlock = successorBlockInfo->block;
349
350 oatGetBlockName(destBlock, blockName2);
351 fprintf(file, " succ%04x:f%d:e -> %s:n\n",
352 bb->startOffset, succId++,
353 blockName2);
354 }
355 }
356 }
357 fprintf(file, "\n");
358
buzbeece302932011-10-04 14:32:18 -0700359 /* Display the dominator tree */
buzbee67bf8852011-08-17 17:51:35 -0700360 oatGetBlockName(bb, blockName1);
361 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
362 blockName1, blockName1);
363 if (bb->iDom) {
364 oatGetBlockName(bb->iDom, blockName2);
365 fprintf(file, " cfg%s:s -> cfg%s:n\n\n",
366 blockName2, blockName1);
367 }
buzbee67bf8852011-08-17 17:51:35 -0700368 }
369 fprintf(file, "}\n");
370 fclose(file);
371}
372
373/* Verify if all the successor is connected with all the claimed predecessors */
buzbeeed3e9302011-09-23 17:34:19 -0700374STATIC bool verifyPredInfo(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700375{
376 ArenaBitVectorIterator bvIterator;
377
378 oatBitVectorIteratorInit(bb->predecessors, &bvIterator);
379 while (true) {
380 int blockIdx = oatBitVectorIteratorNext(&bvIterator);
381 if (blockIdx == -1) break;
382 BasicBlock *predBB = (BasicBlock *)
383 oatGrowableListGetElement(&cUnit->blockList, blockIdx);
384 bool found = false;
385 if (predBB->taken == bb) {
386 found = true;
387 } else if (predBB->fallThrough == bb) {
388 found = true;
389 } else if (predBB->successorBlockList.blockListType != kNotUsed) {
390 GrowableListIterator iterator;
391 oatGrowableListIteratorInit(&predBB->successorBlockList.blocks,
392 &iterator);
393 while (true) {
394 SuccessorBlockInfo *successorBlockInfo =
395 (SuccessorBlockInfo *)
396 oatGrowableListIteratorNext(&iterator);
397 if (successorBlockInfo == NULL) break;
398 BasicBlock *succBB = successorBlockInfo->block;
399 if (succBB == bb) {
400 found = true;
401 break;
402 }
403 }
404 }
405 if (found == false) {
406 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
407 oatGetBlockName(bb, blockName1);
408 oatGetBlockName(predBB, blockName2);
409 oatDumpCFG(cUnit, "/sdcard/cfg/");
410 LOG(FATAL) << "Successor " << blockName1 << "not found from "
411 << blockName2;
412 }
413 }
414 return true;
415}
416
417/* Identify code range in try blocks and set up the empty catch blocks */
buzbeeed3e9302011-09-23 17:34:19 -0700418STATIC void processTryCatchBlocks(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700419{
buzbeee9a72f62011-09-04 17:59:07 -0700420 const Method* method = cUnit->method;
421 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
422 const art::DexFile& dex_file = class_linker->FindDexFile(
423 method->GetDeclaringClass()->GetDexCache());
424 const art::DexFile::CodeItem* code_item =
425 dex_file.GetCodeItem(method->GetCodeItemOffset());
426 int triesSize = code_item->tries_size_;
buzbee67bf8852011-08-17 17:51:35 -0700427 int offset;
428
429 if (triesSize == 0) {
430 return;
431 }
432
buzbee67bf8852011-08-17 17:51:35 -0700433 ArenaBitVector* tryBlockAddr = cUnit->tryBlockAddr;
434
buzbeee9a72f62011-09-04 17:59:07 -0700435 for (int i = 0; i < triesSize; i++) {
436 const art::DexFile::TryItem* pTry =
437 art::DexFile::dexGetTryItems(*code_item, i);
438 int startOffset = pTry->start_addr_;
439 int endOffset = startOffset + pTry->insn_count_;
buzbee67bf8852011-08-17 17:51:35 -0700440 for (offset = startOffset; offset < endOffset; offset++) {
441 oatSetBit(tryBlockAddr, offset);
442 }
443 }
444
buzbeee9a72f62011-09-04 17:59:07 -0700445 // Iterate over each of the handlers to enqueue the empty Catch blocks
446 const art::byte* handlers_ptr =
447 art::DexFile::dexGetCatchHandlerData(*code_item, 0);
448 uint32_t handlers_size = art::DecodeUnsignedLeb128(&handlers_ptr);
449 for (uint32_t idx = 0; idx < handlers_size; idx++) {
450 art::DexFile::CatchHandlerIterator iterator(handlers_ptr);
buzbee67bf8852011-08-17 17:51:35 -0700451
buzbeee9a72f62011-09-04 17:59:07 -0700452 for (; !iterator.HasNext(); iterator.Next()) {
453 uint32_t address = iterator.Get().address_;
454 findBlock(cUnit, address, false /* split */, true /*create*/);
buzbee67bf8852011-08-17 17:51:35 -0700455 }
buzbeee9a72f62011-09-04 17:59:07 -0700456 handlers_ptr = iterator.GetData();
buzbee67bf8852011-08-17 17:51:35 -0700457 }
458}
459
460/* Process instructions with the kInstrCanBranch flag */
buzbeeed3e9302011-09-23 17:34:19 -0700461STATIC void processCanBranch(CompilationUnit* cUnit, BasicBlock* curBlock,
buzbee67bf8852011-08-17 17:51:35 -0700462 MIR* insn, int curOffset, int width, int flags,
463 const u2* codePtr, const u2* codeEnd)
464{
465 int target = curOffset;
466 switch (insn->dalvikInsn.opcode) {
467 case OP_GOTO:
468 case OP_GOTO_16:
469 case OP_GOTO_32:
470 target += (int) insn->dalvikInsn.vA;
471 break;
472 case OP_IF_EQ:
473 case OP_IF_NE:
474 case OP_IF_LT:
475 case OP_IF_GE:
476 case OP_IF_GT:
477 case OP_IF_LE:
478 target += (int) insn->dalvikInsn.vC;
479 break;
480 case OP_IF_EQZ:
481 case OP_IF_NEZ:
482 case OP_IF_LTZ:
483 case OP_IF_GEZ:
484 case OP_IF_GTZ:
485 case OP_IF_LEZ:
486 target += (int) insn->dalvikInsn.vB;
487 break;
488 default:
489 LOG(FATAL) << "Unexpected opcode(" << (int)insn->dalvikInsn.opcode
490 << ") with kInstrCanBranch set";
491 }
492 BasicBlock *takenBlock = findBlock(cUnit, target,
493 /* split */
494 true,
495 /* create */
496 true);
497 curBlock->taken = takenBlock;
498 oatSetBit(takenBlock->predecessors, curBlock->id);
499
500 /* Always terminate the current block for conditional branches */
501 if (flags & kInstrCanContinue) {
502 BasicBlock *fallthroughBlock = findBlock(cUnit,
503 curOffset + width,
504 /*
505 * If the method is processed
506 * in sequential order from the
507 * beginning, we don't need to
508 * specify split for continue
509 * blocks. However, this
510 * routine can be called by
511 * compileLoop, which starts
512 * parsing the method from an
513 * arbitrary address in the
514 * method body.
515 */
516 true,
517 /* create */
518 true);
519 curBlock->fallThrough = fallthroughBlock;
520 oatSetBit(fallthroughBlock->predecessors, curBlock->id);
521 } else if (codePtr < codeEnd) {
522 /* Create a fallthrough block for real instructions (incl. OP_NOP) */
523 if (contentIsInsn(codePtr)) {
524 findBlock(cUnit, curOffset + width,
525 /* split */
526 false,
527 /* create */
528 true);
529 }
530 }
531}
532
533/* Process instructions with the kInstrCanSwitch flag */
buzbeeed3e9302011-09-23 17:34:19 -0700534STATIC void processCanSwitch(CompilationUnit* cUnit, BasicBlock* curBlock,
buzbee67bf8852011-08-17 17:51:35 -0700535 MIR* insn, int curOffset, int width, int flags)
536{
537 u2* switchData= (u2 *) (cUnit->insns + curOffset +
538 insn->dalvikInsn.vB);
539 int size;
540 int* keyTable;
541 int* targetTable;
542 int i;
543 int firstKey;
544
545 /*
546 * Packed switch data format:
547 * ushort ident = 0x0100 magic value
548 * ushort size number of entries in the table
549 * int first_key first (and lowest) switch case value
550 * int targets[size] branch targets, relative to switch opcode
551 *
552 * Total size is (4+size*2) 16-bit code units.
553 */
554 if (insn->dalvikInsn.opcode == OP_PACKED_SWITCH) {
buzbeeed3e9302011-09-23 17:34:19 -0700555 DCHECK_EQ(switchData[0], kPackedSwitchSignature);
buzbee67bf8852011-08-17 17:51:35 -0700556 size = switchData[1];
557 firstKey = switchData[2] | (switchData[3] << 16);
558 targetTable = (int *) &switchData[4];
559 keyTable = NULL; // Make the compiler happy
560 /*
561 * Sparse switch data format:
562 * ushort ident = 0x0200 magic value
563 * ushort size number of entries in the table; > 0
564 * int keys[size] keys, sorted low-to-high; 32-bit aligned
565 * int targets[size] branch targets, relative to switch opcode
566 *
567 * Total size is (2+size*4) 16-bit code units.
568 */
569 } else {
buzbeeed3e9302011-09-23 17:34:19 -0700570 DCHECK_EQ(switchData[0], kSparseSwitchSignature);
buzbee67bf8852011-08-17 17:51:35 -0700571 size = switchData[1];
572 keyTable = (int *) &switchData[2];
573 targetTable = (int *) &switchData[2 + size*2];
574 firstKey = 0; // To make the compiler happy
575 }
576
577 if (curBlock->successorBlockList.blockListType != kNotUsed) {
578 LOG(FATAL) << "Successor block list already in use: " <<
579 (int)curBlock->successorBlockList.blockListType;
580 }
581 curBlock->successorBlockList.blockListType =
582 (insn->dalvikInsn.opcode == OP_PACKED_SWITCH) ?
583 kPackedSwitch : kSparseSwitch;
584 oatInitGrowableList(&curBlock->successorBlockList.blocks, size);
585
586 for (i = 0; i < size; i++) {
587 BasicBlock *caseBlock = findBlock(cUnit, curOffset + targetTable[i],
588 /* split */
589 true,
590 /* create */
591 true);
592 SuccessorBlockInfo *successorBlockInfo =
593 (SuccessorBlockInfo *) oatNew(sizeof(SuccessorBlockInfo),
594 false);
595 successorBlockInfo->block = caseBlock;
596 successorBlockInfo->key = (insn->dalvikInsn.opcode == OP_PACKED_SWITCH)?
597 firstKey + i : keyTable[i];
598 oatInsertGrowableList(&curBlock->successorBlockList.blocks,
599 (intptr_t) successorBlockInfo);
600 oatSetBit(caseBlock->predecessors, curBlock->id);
601 }
602
603 /* Fall-through case */
604 BasicBlock* fallthroughBlock = findBlock(cUnit,
605 curOffset + width,
606 /* split */
607 false,
608 /* create */
609 true);
610 curBlock->fallThrough = fallthroughBlock;
611 oatSetBit(fallthroughBlock->predecessors, curBlock->id);
612}
613
614/* Process instructions with the kInstrCanThrow flag */
buzbeeed3e9302011-09-23 17:34:19 -0700615STATIC void processCanThrow(CompilationUnit* cUnit, BasicBlock* curBlock,
buzbee67bf8852011-08-17 17:51:35 -0700616 MIR* insn, int curOffset, int width, int flags,
617 ArenaBitVector* tryBlockAddr, const u2* codePtr,
618 const u2* codeEnd)
619{
buzbeee9a72f62011-09-04 17:59:07 -0700620
buzbee67bf8852011-08-17 17:51:35 -0700621 const Method* method = cUnit->method;
buzbeee9a72f62011-09-04 17:59:07 -0700622 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
623 const art::DexFile& dex_file = class_linker->FindDexFile(
624 method->GetDeclaringClass()->GetDexCache());
625 const art::DexFile::CodeItem* code_item =
626 dex_file.GetCodeItem(method->GetCodeItemOffset());
buzbee67bf8852011-08-17 17:51:35 -0700627
628 /* In try block */
629 if (oatIsBitSet(tryBlockAddr, curOffset)) {
buzbeee9a72f62011-09-04 17:59:07 -0700630 art::DexFile::CatchHandlerIterator iterator =
631 art::DexFile::dexFindCatchHandler(*code_item, curOffset);
buzbee67bf8852011-08-17 17:51:35 -0700632
buzbee67bf8852011-08-17 17:51:35 -0700633 if (curBlock->successorBlockList.blockListType != kNotUsed) {
634 LOG(FATAL) << "Successor block list already in use: " <<
635 (int)curBlock->successorBlockList.blockListType;
636 }
buzbeee9a72f62011-09-04 17:59:07 -0700637
buzbee67bf8852011-08-17 17:51:35 -0700638 curBlock->successorBlockList.blockListType = kCatch;
639 oatInitGrowableList(&curBlock->successorBlockList.blocks, 2);
640
buzbeee9a72f62011-09-04 17:59:07 -0700641 for (;!iterator.HasNext(); iterator.Next()) {
642 BasicBlock *catchBlock = findBlock(cUnit, iterator.Get().address_,
643 false /* split*/,
644 false /* creat */);
buzbee43a36422011-09-14 14:00:13 -0700645 catchBlock->catchEntry = true;
buzbee67bf8852011-08-17 17:51:35 -0700646 SuccessorBlockInfo *successorBlockInfo =
buzbeee9a72f62011-09-04 17:59:07 -0700647 (SuccessorBlockInfo *) oatNew(sizeof(SuccessorBlockInfo),
648 false);
buzbee67bf8852011-08-17 17:51:35 -0700649 successorBlockInfo->block = catchBlock;
buzbeee9a72f62011-09-04 17:59:07 -0700650 successorBlockInfo->key = iterator.Get().type_idx_;
buzbee67bf8852011-08-17 17:51:35 -0700651 oatInsertGrowableList(&curBlock->successorBlockList.blocks,
652 (intptr_t) successorBlockInfo);
653 oatSetBit(catchBlock->predecessors, curBlock->id);
654 }
655 } else {
656 BasicBlock *ehBlock = oatNewBB(kExceptionHandling,
657 cUnit->numBlocks++);
658 curBlock->taken = ehBlock;
659 oatInsertGrowableList(&cUnit->blockList, (intptr_t) ehBlock);
660 ehBlock->startOffset = curOffset;
661 oatSetBit(ehBlock->predecessors, curBlock->id);
662 }
663
664 /*
665 * Force the current block to terminate.
666 *
667 * Data may be present before codeEnd, so we need to parse it to know
668 * whether it is code or data.
669 */
670 if (codePtr < codeEnd) {
671 /* Create a fallthrough block for real instructions (incl. OP_NOP) */
672 if (contentIsInsn(codePtr)) {
673 BasicBlock *fallthroughBlock = findBlock(cUnit,
674 curOffset + width,
675 /* split */
676 false,
677 /* create */
678 true);
679 /*
680 * OP_THROW and OP_THROW_VERIFICATION_ERROR are unconditional
681 * branches.
682 */
683 if (insn->dalvikInsn.opcode != OP_THROW_VERIFICATION_ERROR &&
684 insn->dalvikInsn.opcode != OP_THROW) {
685 curBlock->fallThrough = fallthroughBlock;
686 oatSetBit(fallthroughBlock->predecessors, curBlock->id);
687 }
688 }
689 }
690}
691
692/*
693 * Compile a method.
694 */
Brian Carlstrom16192862011-09-12 17:50:06 -0700695bool oatCompileMethod(const Compiler& compiler, Method* method, art::InstructionSet insnSet)
buzbee67bf8852011-08-17 17:51:35 -0700696{
Brian Carlstrom16192862011-09-12 17:50:06 -0700697 if (compiler.IsVerbose()) {
698 LOG(INFO) << "Compiling " << PrettyMethod(method) << "...";
699 }
buzbee2a475e72011-09-07 17:19:17 -0700700 oatArenaReset();
Brian Carlstrom94496d32011-08-22 09:22:47 -0700701
buzbee67bf8852011-08-17 17:51:35 -0700702 CompilationUnit cUnit;
buzbeec143c552011-08-20 17:38:58 -0700703 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
704 const art::DexFile& dex_file = class_linker->FindDexFile(
705 method->GetDeclaringClass()->GetDexCache());
706 const art::DexFile::CodeItem* code_item =
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700707 dex_file.GetCodeItem(method->GetCodeItemOffset());
buzbeec143c552011-08-20 17:38:58 -0700708 const u2* codePtr = code_item->insns_;
709 const u2* codeEnd = code_item->insns_ + code_item->insns_size_;
buzbee67bf8852011-08-17 17:51:35 -0700710 int numBlocks = 0;
711 unsigned int curOffset = 0;
712
Brian Carlstrom16192862011-09-12 17:50:06 -0700713 oatInit(compiler);
buzbee67bf8852011-08-17 17:51:35 -0700714
715 memset(&cUnit, 0, sizeof(cUnit));
716 cUnit.method = method;
buzbeec143c552011-08-20 17:38:58 -0700717 cUnit.instructionSet = (OatInstructionSetType)insnSet;
718 cUnit.insns = code_item->insns_;
719 cUnit.insnsSize = code_item->insns_size_;
buzbeece302932011-10-04 14:32:18 -0700720 bool useMatch = compilerMethodMatch.length() != 0;
721 bool match = useMatch && (compilerFlipMatch ^
722 (PrettyMethod(method).find(compilerMethodMatch) != std::string::npos));
723 if (!useMatch || match) {
724 cUnit.disableOpt = compilerOptimizerDisableFlags;
725 cUnit.enableDebug = compilerDebugFlags;
726 cUnit.printMe = compiler.IsVerbose() ||
727 (cUnit.enableDebug & (1 << kDebugVerbose));
728 }
buzbee67bf8852011-08-17 17:51:35 -0700729
buzbeecefd1872011-09-09 09:59:52 -0700730 /* Assume non-throwing leaf */
731 cUnit.attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
732
buzbee67bf8852011-08-17 17:51:35 -0700733 /* Initialize the block list */
734 oatInitGrowableList(&cUnit.blockList, 40);
735
736 /* Initialize the switchTables list */
737 oatInitGrowableList(&cUnit.switchTables, 4);
738
739 /* Intialize the fillArrayData list */
740 oatInitGrowableList(&cUnit.fillArrayData, 4);
741
buzbee5ade1d22011-09-09 14:44:52 -0700742 /* Intialize the throwLaunchpads list */
743 oatInitGrowableList(&cUnit.throwLaunchpads, 4);
744
buzbeec1f45042011-09-21 16:03:19 -0700745 /* Intialize the suspendLaunchpads list */
746 oatInitGrowableList(&cUnit.suspendLaunchpads, 4);
747
buzbee67bf8852011-08-17 17:51:35 -0700748 /* Allocate the bit-vector to track the beginning of basic blocks */
749 ArenaBitVector *tryBlockAddr = oatAllocBitVector(cUnit.insnsSize,
750 true /* expandable */);
751 cUnit.tryBlockAddr = tryBlockAddr;
752
753 /* Create the default entry and exit blocks and enter them to the list */
754 BasicBlock *entryBlock = oatNewBB(kEntryBlock, numBlocks++);
755 BasicBlock *exitBlock = oatNewBB(kExitBlock, numBlocks++);
756
757 cUnit.entryBlock = entryBlock;
758 cUnit.exitBlock = exitBlock;
759
760 oatInsertGrowableList(&cUnit.blockList, (intptr_t) entryBlock);
761 oatInsertGrowableList(&cUnit.blockList, (intptr_t) exitBlock);
762
763 /* Current block to record parsed instructions */
764 BasicBlock *curBlock = oatNewBB(kDalvikByteCode, numBlocks++);
765 curBlock->startOffset = 0;
766 oatInsertGrowableList(&cUnit.blockList, (intptr_t) curBlock);
767 entryBlock->fallThrough = curBlock;
768 oatSetBit(curBlock->predecessors, entryBlock->id);
769
770 /*
771 * Store back the number of blocks since new blocks may be created of
772 * accessing cUnit.
773 */
774 cUnit.numBlocks = numBlocks;
775
776 /* Identify code range in try blocks and set up the empty catch blocks */
777 processTryCatchBlocks(&cUnit);
778
779 /* Parse all instructions and put them into containing basic blocks */
780 while (codePtr < codeEnd) {
781 MIR *insn = (MIR *) oatNew(sizeof(MIR), true);
782 insn->offset = curOffset;
783 int width = parseInsn(codePtr, &insn->dalvikInsn, false);
784 insn->width = width;
785
786 /* Terminate when the data section is seen */
787 if (width == 0)
788 break;
789
790 oatAppendMIR(curBlock, insn);
791
792 codePtr += width;
793 int flags = dexGetFlagsFromOpcode(insn->dalvikInsn.opcode);
794
795 if (flags & kInstrCanBranch) {
796 processCanBranch(&cUnit, curBlock, insn, curOffset, width, flags,
797 codePtr, codeEnd);
798 } else if (flags & kInstrCanReturn) {
799 curBlock->fallThrough = exitBlock;
800 oatSetBit(exitBlock->predecessors, curBlock->id);
801 /*
802 * Terminate the current block if there are instructions
803 * afterwards.
804 */
805 if (codePtr < codeEnd) {
806 /*
807 * Create a fallthrough block for real instructions
808 * (incl. OP_NOP).
809 */
810 if (contentIsInsn(codePtr)) {
811 findBlock(&cUnit, curOffset + width,
812 /* split */
813 false,
814 /* create */
815 true);
816 }
817 }
818 } else if (flags & kInstrCanThrow) {
819 processCanThrow(&cUnit, curBlock, insn, curOffset, width, flags,
820 tryBlockAddr, codePtr, codeEnd);
821 } else if (flags & kInstrCanSwitch) {
822 processCanSwitch(&cUnit, curBlock, insn, curOffset, width, flags);
823 }
824 curOffset += width;
825 BasicBlock *nextBlock = findBlock(&cUnit, curOffset,
826 /* split */
827 false,
828 /* create */
829 false);
830 if (nextBlock) {
831 /*
832 * The next instruction could be the target of a previously parsed
833 * forward branch so a block is already created. If the current
834 * instruction is not an unconditional branch, connect them through
835 * the fall-through link.
836 */
buzbeeed3e9302011-09-23 17:34:19 -0700837 DCHECK(curBlock->fallThrough == NULL ||
buzbee67bf8852011-08-17 17:51:35 -0700838 curBlock->fallThrough == nextBlock ||
839 curBlock->fallThrough == exitBlock);
840
841 if ((curBlock->fallThrough == NULL) &&
842 (flags & kInstrCanContinue)) {
843 curBlock->fallThrough = nextBlock;
844 oatSetBit(nextBlock->predecessors, curBlock->id);
845 }
846 curBlock = nextBlock;
847 }
848 }
849
850 if (cUnit.printMe) {
851 oatDumpCompilationUnit(&cUnit);
852 }
853
854 /* Adjust this value accordingly once inlining is performed */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700855 cUnit.numDalvikRegisters = cUnit.method->NumRegisters();
buzbee67bf8852011-08-17 17:51:35 -0700856
857
858 /* Verify if all blocks are connected as claimed */
859 oatDataFlowAnalysisDispatcher(&cUnit, verifyPredInfo,
860 kAllNodes,
861 false /* isIterative */);
862
863
864
865 /* Perform SSA transformation for the whole method */
866 oatMethodSSATransformation(&cUnit);
867
buzbee43a36422011-09-14 14:00:13 -0700868 /* Perform null check elimination */
869 oatMethodNullCheckElimination(&cUnit);
870
buzbee67bf8852011-08-17 17:51:35 -0700871 oatInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming
872
873 /* Allocate Registers using simple local allocation scheme */
874 oatSimpleRegAlloc(&cUnit);
875
876 /* Convert MIR to LIR, etc. */
877 oatMethodMIR2LIR(&cUnit);
878
879 // Debugging only
buzbeece302932011-10-04 14:32:18 -0700880 if (cUnit.enableDebug & (1 << kDebugDumpCFG)) {
buzbeeec5adf32011-09-11 15:25:43 -0700881 oatDumpCFG(&cUnit, "/sdcard/cfg/");
882 }
buzbee67bf8852011-08-17 17:51:35 -0700883
884 /* Method is not empty */
885 if (cUnit.firstLIRInsn) {
886
887 // mark the targets of switch statement case labels
888 oatProcessSwitchTables(&cUnit);
889
890 /* Convert LIR into machine code. */
891 oatAssembleLIR(&cUnit);
892
893 if (cUnit.printMe) {
894 oatCodegenDump(&cUnit);
895 }
896 }
897
buzbee4ef76522011-09-08 10:00:32 -0700898 art::ByteArray* managed_code =
Ian Rogersbdb03912011-09-14 00:55:44 -0700899 art::ByteArray::Alloc(cUnit.codeBuffer.size() * sizeof(cUnit.codeBuffer[0]));
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700900 memcpy(managed_code->GetData(),
901 reinterpret_cast<const int8_t*>(&cUnit.codeBuffer[0]),
902 managed_code->GetLength());
Ian Rogersbdb03912011-09-14 00:55:44 -0700903 art::IntArray* mapping_table =
904 art::IntArray::Alloc(cUnit.mappingTable.size());
905 DCHECK_EQ(mapping_table->GetClass()->GetComponentSize(), sizeof(cUnit.mappingTable[0]));
buzbee4ef76522011-09-08 10:00:32 -0700906 memcpy(mapping_table->GetData(),
Ian Rogersbdb03912011-09-14 00:55:44 -0700907 reinterpret_cast<const int32_t*>(&cUnit.mappingTable[0]),
908 mapping_table->GetLength() * sizeof(cUnit.mappingTable[0]));
buzbeec41e5b52011-09-23 12:46:19 -0700909 // Add a marker to take place of lr
910 cUnit.coreVmapTable.push_back(-1);
911 // Combine vmap tables - core regs, then fp regs
912 for (uint32_t i = 0; i < cUnit.fpVmapTable.size(); i++) {
913 cUnit.coreVmapTable.push_back(cUnit.fpVmapTable[i]);
914 }
915 DCHECK(cUnit.coreVmapTable.size() == (uint32_t)
916 (__builtin_popcount(cUnit.coreSpillMask) +
917 __builtin_popcount(cUnit.fpSpillMask)));
918 art::ShortArray* vmap_table =
919 art::ShortArray::Alloc(cUnit.coreVmapTable.size());
920 memcpy(vmap_table->GetData(),
921 reinterpret_cast<const int16_t*>(&cUnit.coreVmapTable[0]),
922 vmap_table->GetLength() * sizeof(cUnit.coreVmapTable[0]));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700923 method->SetCodeArray(managed_code, art::kThumb2);
924 method->SetMappingTable(mapping_table);
925 method->SetVMapTable(vmap_table);
Shih-wei Liaod11af152011-08-23 16:02:11 -0700926 method->SetFrameSizeInBytes(cUnit.frameSize);
Ian Rogersbdb03912011-09-14 00:55:44 -0700927 method->SetReturnPcOffsetInBytes(cUnit.frameSize - sizeof(intptr_t));
buzbeec143c552011-08-20 17:38:58 -0700928 method->SetCoreSpillMask(cUnit.coreSpillMask);
929 method->SetFpSpillMask(cUnit.fpSpillMask);
Brian Carlstrom16192862011-09-12 17:50:06 -0700930 if (compiler.IsVerbose()) {
931 LOG(INFO) << "Compiled " << PrettyMethod(method)
932 << " code at " << reinterpret_cast<void*>(managed_code->GetData())
933 << " (" << managed_code->GetLength() << " bytes)";
934 }
buzbee67bf8852011-08-17 17:51:35 -0700935
Brian Carlstrombffb1552011-08-25 12:23:53 -0700936 return true;
buzbee67bf8852011-08-17 17:51:35 -0700937}
938
Brian Carlstrom16192862011-09-12 17:50:06 -0700939void oatInit(const Compiler& compiler)
buzbee67bf8852011-08-17 17:51:35 -0700940{
buzbee67bf8852011-08-17 17:51:35 -0700941 static bool initialized = false;
942 if (initialized)
943 return;
944 initialized = true;
Brian Carlstrom16192862011-09-12 17:50:06 -0700945 if (compiler.IsVerbose()) {
946 LOG(INFO) << "Initializing compiler";
947 }
buzbee67bf8852011-08-17 17:51:35 -0700948 if (!oatArchInit()) {
949 LOG(FATAL) << "Failed to initialize oat";
950 }
951 if (!oatHeapInit()) {
952 LOG(FATAL) << "Failed to initialize oat heap";
953 }
954}