buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 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" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 20 | #include "constants.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 21 | #include "leb128.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 22 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 23 | #include "runtime.h" |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 24 | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 25 | /* Default optimizer/debug setting for the compiler. */ |
| 26 | uint32_t compilerOptimizerDisableFlags = 0 | // Disable specific optimizations |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 27 | //(1 << kLoadStoreElimination) | |
| 28 | //(1 << kLoadHoisting) | |
| 29 | //(1 << kSuppressLoads) | |
| 30 | //(1 << kNullCheckElimination) | |
buzbee | 769fde1 | 2012-01-05 17:35:23 -0800 | [diff] [blame] | 31 | //(1 << kPromoteRegs) | |
| 32 | //(1 << kTrackLiveTemps) | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 33 | 0; |
| 34 | |
| 35 | uint32_t compilerDebugFlags = 0 | // Enable debug/testing modes |
buzbee | e3de749 | 2011-10-05 13:37:17 -0700 | [diff] [blame] | 36 | //(1 << kDebugDisplayMissingTargets) | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 37 | //(1 << kDebugVerbose) | |
| 38 | //(1 << kDebugDumpCFG) | |
| 39 | //(1 << kDebugSlowFieldPath) | |
| 40 | //(1 << kDebugSlowInvokePath) | |
| 41 | //(1 << kDebugSlowStringPath) | |
| 42 | //(1 << kDebugSlowestFieldPath) | |
| 43 | //(1 << kDebugSlowestStringPath) | |
buzbee | 34c77ad | 2012-01-11 13:01:32 -0800 | [diff] [blame] | 44 | //(1 << kDebugExerciseResolveMethod) | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 45 | 0; |
| 46 | |
| 47 | std::string compilerMethodMatch; // Method name match to apply above flags |
| 48 | |
| 49 | bool compilerFlipMatch = false; // Reverses sense of method name match |
| 50 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 51 | STATIC inline bool contentIsInsn(const u2* codePtr) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 52 | 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 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 65 | STATIC inline int parseInsn(const u2* codePtr, DecodedInstruction* decInsn, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 66 | 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) { |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 78 | char* decodedString = oatGetDalvikDisassembly(decInsn, NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 79 | LOG(INFO) << codePtr << ": 0x" << std::hex << (int)opcode << |
| 80 | " " << decodedString; |
| 81 | } |
| 82 | return dexGetWidthFromOpcode(opcode); |
| 83 | } |
| 84 | |
| 85 | #define UNKNOWN_TARGET 0xffffffff |
| 86 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 87 | STATIC inline bool isGoto(MIR* insn) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 88 | { |
| 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 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 102 | STATIC inline bool isUnconditionalBranch(MIR* insn) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 103 | { |
| 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 */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 116 | STATIC BasicBlock *splitBlock(CompilationUnit* cUnit, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 117 | unsigned int codeOffset, |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 118 | BasicBlock* origBlock, |
| 119 | BasicBlock** immedPredBlockP) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 120 | { |
| 121 | MIR* insn = origBlock->firstMIRInsn; |
| 122 | while (insn) { |
| 123 | if (insn->offset == codeOffset) break; |
| 124 | insn = insn->next; |
| 125 | } |
| 126 | if (insn == NULL) { |
| 127 | LOG(FATAL) << "Break split failed"; |
| 128 | } |
| 129 | BasicBlock *bottomBlock = oatNewBB(kDalvikByteCode, |
| 130 | cUnit->numBlocks++); |
| 131 | oatInsertGrowableList(&cUnit->blockList, (intptr_t) bottomBlock); |
| 132 | |
| 133 | bottomBlock->startOffset = codeOffset; |
| 134 | bottomBlock->firstMIRInsn = insn; |
| 135 | bottomBlock->lastMIRInsn = origBlock->lastMIRInsn; |
| 136 | |
| 137 | /* Handle the taken path */ |
| 138 | bottomBlock->taken = origBlock->taken; |
| 139 | if (bottomBlock->taken) { |
| 140 | origBlock->taken = NULL; |
| 141 | oatClearBit(bottomBlock->taken->predecessors, origBlock->id); |
| 142 | oatSetBit(bottomBlock->taken->predecessors, bottomBlock->id); |
| 143 | } |
| 144 | |
| 145 | /* Handle the fallthrough path */ |
| 146 | bottomBlock->needFallThroughBranch = origBlock->needFallThroughBranch; |
| 147 | bottomBlock->fallThrough = origBlock->fallThrough; |
| 148 | origBlock->fallThrough = bottomBlock; |
| 149 | origBlock->needFallThroughBranch = true; |
| 150 | oatSetBit(bottomBlock->predecessors, origBlock->id); |
| 151 | if (bottomBlock->fallThrough) { |
| 152 | oatClearBit(bottomBlock->fallThrough->predecessors, |
| 153 | origBlock->id); |
| 154 | oatSetBit(bottomBlock->fallThrough->predecessors, |
| 155 | bottomBlock->id); |
| 156 | } |
| 157 | |
| 158 | /* Handle the successor list */ |
| 159 | if (origBlock->successorBlockList.blockListType != kNotUsed) { |
| 160 | bottomBlock->successorBlockList = origBlock->successorBlockList; |
| 161 | origBlock->successorBlockList.blockListType = kNotUsed; |
| 162 | GrowableListIterator iterator; |
| 163 | |
| 164 | oatGrowableListIteratorInit(&bottomBlock->successorBlockList.blocks, |
| 165 | &iterator); |
| 166 | while (true) { |
| 167 | SuccessorBlockInfo *successorBlockInfo = |
| 168 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 169 | if (successorBlockInfo == NULL) break; |
| 170 | BasicBlock *bb = successorBlockInfo->block; |
| 171 | oatClearBit(bb->predecessors, origBlock->id); |
| 172 | oatSetBit(bb->predecessors, bottomBlock->id); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | origBlock->lastMIRInsn = insn->prev; |
| 177 | |
| 178 | insn->prev->next = NULL; |
| 179 | insn->prev = NULL; |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 180 | /* |
| 181 | * Update the immediate predecessor block pointer so that outgoing edges |
| 182 | * can be applied to the proper block. |
| 183 | */ |
| 184 | if (immedPredBlockP) { |
| 185 | DCHECK_EQ(*immedPredBlockP, origBlock); |
| 186 | *immedPredBlockP = bottomBlock; |
| 187 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 188 | return bottomBlock; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Given a code offset, find out the block that starts with it. If the offset |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 193 | * is in the middle of an existing block, split it into two. If immedPredBlockP |
| 194 | * is not non-null and is the block being split, update *immedPredBlockP to |
| 195 | * point to the bottom block so that outgoing edges can be set up properly |
| 196 | * (by the caller) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 197 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 198 | STATIC BasicBlock *findBlock(CompilationUnit* cUnit, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 199 | unsigned int codeOffset, |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 200 | bool split, bool create, |
| 201 | BasicBlock** immedPredBlockP) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 202 | { |
| 203 | GrowableList* blockList = &cUnit->blockList; |
| 204 | BasicBlock* bb; |
| 205 | unsigned int i; |
| 206 | |
| 207 | for (i = 0; i < blockList->numUsed; i++) { |
| 208 | bb = (BasicBlock *) blockList->elemList[i]; |
| 209 | if (bb->blockType != kDalvikByteCode) continue; |
| 210 | if (bb->startOffset == codeOffset) return bb; |
| 211 | /* Check if a branch jumps into the middle of an existing block */ |
| 212 | if ((split == true) && (codeOffset > bb->startOffset) && |
| 213 | (bb->lastMIRInsn != NULL) && |
| 214 | (codeOffset <= bb->lastMIRInsn->offset)) { |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 215 | BasicBlock *newBB = splitBlock(cUnit, codeOffset, bb, |
| 216 | bb == *immedPredBlockP ? |
| 217 | immedPredBlockP : NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 218 | return newBB; |
| 219 | } |
| 220 | } |
| 221 | if (create) { |
| 222 | bb = oatNewBB(kDalvikByteCode, cUnit->numBlocks++); |
| 223 | oatInsertGrowableList(&cUnit->blockList, (intptr_t) bb); |
| 224 | bb->startOffset = codeOffset; |
| 225 | return bb; |
| 226 | } |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | /* Dump the CFG into a DOT graph */ |
| 231 | void oatDumpCFG(CompilationUnit* cUnit, const char* dirPrefix) |
| 232 | { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 233 | FILE* file; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 234 | std::string name(art::PrettyMethod(cUnit->method_idx, *cUnit->dex_file)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 235 | char startOffset[80]; |
| 236 | sprintf(startOffset, "_%x", cUnit->entryBlock->fallThrough->startOffset); |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 237 | char* fileName = (char*) oatNew( |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 238 | strlen(dirPrefix) + |
| 239 | name.length() + |
| 240 | strlen(".dot") + 1, true); |
| 241 | sprintf(fileName, "%s%s%s.dot", dirPrefix, name.c_str(), startOffset); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * Convert the special characters into a filesystem- and shell-friendly |
| 245 | * format. |
| 246 | */ |
| 247 | int i; |
| 248 | for (i = strlen(dirPrefix); fileName[i]; i++) { |
| 249 | if (fileName[i] == '/') { |
| 250 | fileName[i] = '_'; |
| 251 | } else if (fileName[i] == ';') { |
| 252 | fileName[i] = '#'; |
| 253 | } else if (fileName[i] == '$') { |
| 254 | fileName[i] = '+'; |
| 255 | } else if (fileName[i] == '(' || fileName[i] == ')') { |
| 256 | fileName[i] = '@'; |
| 257 | } else if (fileName[i] == '<' || fileName[i] == '>') { |
| 258 | fileName[i] = '='; |
| 259 | } |
| 260 | } |
| 261 | file = fopen(fileName, "w"); |
| 262 | if (file == NULL) { |
| 263 | return; |
| 264 | } |
| 265 | fprintf(file, "digraph G {\n"); |
| 266 | |
| 267 | fprintf(file, " rankdir=TB\n"); |
| 268 | |
| 269 | int numReachableBlocks = cUnit->numReachableBlocks; |
| 270 | int idx; |
| 271 | const GrowableList *blockList = &cUnit->blockList; |
| 272 | |
| 273 | for (idx = 0; idx < numReachableBlocks; idx++) { |
| 274 | int blockIdx = cUnit->dfsOrder.elemList[idx]; |
| 275 | BasicBlock *bb = (BasicBlock *) oatGrowableListGetElement(blockList, |
| 276 | blockIdx); |
| 277 | if (bb == NULL) break; |
| 278 | if (bb->blockType == kEntryBlock) { |
| 279 | fprintf(file, " entry [shape=Mdiamond];\n"); |
| 280 | } else if (bb->blockType == kExitBlock) { |
| 281 | fprintf(file, " exit [shape=Mdiamond];\n"); |
| 282 | } else if (bb->blockType == kDalvikByteCode) { |
| 283 | fprintf(file, " block%04x [shape=record,label = \"{ \\\n", |
| 284 | bb->startOffset); |
| 285 | const MIR *mir; |
| 286 | fprintf(file, " {block id %d\\l}%s\\\n", bb->id, |
| 287 | bb->firstMIRInsn ? " | " : " "); |
| 288 | for (mir = bb->firstMIRInsn; mir; mir = mir->next) { |
| 289 | fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset, |
| 290 | mir->ssaRep ? |
| 291 | oatFullDisassembler(cUnit, mir) : |
| 292 | dexGetOpcodeName(mir->dalvikInsn.opcode), |
| 293 | mir->next ? " | " : " "); |
| 294 | } |
| 295 | fprintf(file, " }\"];\n\n"); |
| 296 | } else if (bb->blockType == kExceptionHandling) { |
| 297 | char blockName[BLOCK_NAME_LEN]; |
| 298 | |
| 299 | oatGetBlockName(bb, blockName); |
| 300 | fprintf(file, " %s [shape=invhouse];\n", blockName); |
| 301 | } |
| 302 | |
| 303 | char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN]; |
| 304 | |
| 305 | if (bb->taken) { |
| 306 | oatGetBlockName(bb, blockName1); |
| 307 | oatGetBlockName(bb->taken, blockName2); |
| 308 | fprintf(file, " %s:s -> %s:n [style=dotted]\n", |
| 309 | blockName1, blockName2); |
| 310 | } |
| 311 | if (bb->fallThrough) { |
| 312 | oatGetBlockName(bb, blockName1); |
| 313 | oatGetBlockName(bb->fallThrough, blockName2); |
| 314 | fprintf(file, " %s:s -> %s:n\n", blockName1, blockName2); |
| 315 | } |
| 316 | |
| 317 | if (bb->successorBlockList.blockListType != kNotUsed) { |
| 318 | fprintf(file, " succ%04x [shape=%s,label = \"{ \\\n", |
| 319 | bb->startOffset, |
| 320 | (bb->successorBlockList.blockListType == kCatch) ? |
| 321 | "Mrecord" : "record"); |
| 322 | GrowableListIterator iterator; |
| 323 | oatGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 324 | &iterator); |
| 325 | SuccessorBlockInfo *successorBlockInfo = |
| 326 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 327 | |
| 328 | int succId = 0; |
| 329 | while (true) { |
| 330 | if (successorBlockInfo == NULL) break; |
| 331 | |
| 332 | BasicBlock *destBlock = successorBlockInfo->block; |
| 333 | SuccessorBlockInfo *nextSuccessorBlockInfo = |
| 334 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 335 | |
| 336 | fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n", |
| 337 | succId++, |
| 338 | successorBlockInfo->key, |
| 339 | destBlock->startOffset, |
| 340 | (nextSuccessorBlockInfo != NULL) ? " | " : " "); |
| 341 | |
| 342 | successorBlockInfo = nextSuccessorBlockInfo; |
| 343 | } |
| 344 | fprintf(file, " }\"];\n\n"); |
| 345 | |
| 346 | oatGetBlockName(bb, blockName1); |
| 347 | fprintf(file, " %s:s -> succ%04x:n [style=dashed]\n", |
| 348 | blockName1, bb->startOffset); |
| 349 | |
| 350 | if (bb->successorBlockList.blockListType == kPackedSwitch || |
| 351 | bb->successorBlockList.blockListType == kSparseSwitch) { |
| 352 | |
| 353 | oatGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 354 | &iterator); |
| 355 | |
| 356 | succId = 0; |
| 357 | while (true) { |
| 358 | SuccessorBlockInfo *successorBlockInfo = |
| 359 | (SuccessorBlockInfo *) |
| 360 | oatGrowableListIteratorNext(&iterator); |
| 361 | if (successorBlockInfo == NULL) break; |
| 362 | |
| 363 | BasicBlock *destBlock = successorBlockInfo->block; |
| 364 | |
| 365 | oatGetBlockName(destBlock, blockName2); |
| 366 | fprintf(file, " succ%04x:f%d:e -> %s:n\n", |
| 367 | bb->startOffset, succId++, |
| 368 | blockName2); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | fprintf(file, "\n"); |
| 373 | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 374 | /* Display the dominator tree */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 375 | oatGetBlockName(bb, blockName1); |
| 376 | fprintf(file, " cfg%s [label=\"%s\", shape=none];\n", |
| 377 | blockName1, blockName1); |
| 378 | if (bb->iDom) { |
| 379 | oatGetBlockName(bb->iDom, blockName2); |
| 380 | fprintf(file, " cfg%s:s -> cfg%s:n\n\n", |
| 381 | blockName2, blockName1); |
| 382 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 383 | } |
| 384 | fprintf(file, "}\n"); |
| 385 | fclose(file); |
| 386 | } |
| 387 | |
| 388 | /* Verify if all the successor is connected with all the claimed predecessors */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 389 | STATIC bool verifyPredInfo(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 390 | { |
| 391 | ArenaBitVectorIterator bvIterator; |
| 392 | |
| 393 | oatBitVectorIteratorInit(bb->predecessors, &bvIterator); |
| 394 | while (true) { |
| 395 | int blockIdx = oatBitVectorIteratorNext(&bvIterator); |
| 396 | if (blockIdx == -1) break; |
| 397 | BasicBlock *predBB = (BasicBlock *) |
| 398 | oatGrowableListGetElement(&cUnit->blockList, blockIdx); |
| 399 | bool found = false; |
| 400 | if (predBB->taken == bb) { |
| 401 | found = true; |
| 402 | } else if (predBB->fallThrough == bb) { |
| 403 | found = true; |
| 404 | } else if (predBB->successorBlockList.blockListType != kNotUsed) { |
| 405 | GrowableListIterator iterator; |
| 406 | oatGrowableListIteratorInit(&predBB->successorBlockList.blocks, |
| 407 | &iterator); |
| 408 | while (true) { |
| 409 | SuccessorBlockInfo *successorBlockInfo = |
| 410 | (SuccessorBlockInfo *) |
| 411 | oatGrowableListIteratorNext(&iterator); |
| 412 | if (successorBlockInfo == NULL) break; |
| 413 | BasicBlock *succBB = successorBlockInfo->block; |
| 414 | if (succBB == bb) { |
| 415 | found = true; |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | if (found == false) { |
| 421 | char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN]; |
| 422 | oatGetBlockName(bb, blockName1); |
| 423 | oatGetBlockName(predBB, blockName2); |
| 424 | oatDumpCFG(cUnit, "/sdcard/cfg/"); |
| 425 | LOG(FATAL) << "Successor " << blockName1 << "not found from " |
| 426 | << blockName2; |
| 427 | } |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | /* Identify code range in try blocks and set up the empty catch blocks */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 433 | STATIC void processTryCatchBlocks(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 434 | { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 435 | const art::DexFile::CodeItem* code_item = cUnit->code_item; |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 436 | int triesSize = code_item->tries_size_; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 437 | int offset; |
| 438 | |
| 439 | if (triesSize == 0) { |
| 440 | return; |
| 441 | } |
| 442 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 443 | ArenaBitVector* tryBlockAddr = cUnit->tryBlockAddr; |
| 444 | |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 445 | for (int i = 0; i < triesSize; i++) { |
| 446 | const art::DexFile::TryItem* pTry = |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 447 | art::DexFile::GetTryItems(*code_item, i); |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 448 | int startOffset = pTry->start_addr_; |
| 449 | int endOffset = startOffset + pTry->insn_count_; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 450 | for (offset = startOffset; offset < endOffset; offset++) { |
| 451 | oatSetBit(tryBlockAddr, offset); |
| 452 | } |
| 453 | } |
| 454 | |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 455 | // Iterate over each of the handlers to enqueue the empty Catch blocks |
| 456 | const art::byte* handlers_ptr = |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 457 | art::DexFile::GetCatchHandlerData(*code_item, 0); |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 458 | uint32_t handlers_size = art::DecodeUnsignedLeb128(&handlers_ptr); |
| 459 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 460 | art::CatchHandlerIterator iterator(handlers_ptr); |
| 461 | for (; iterator.HasNext(); iterator.Next()) { |
| 462 | uint32_t address = iterator.GetHandlerAddress(); |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 463 | findBlock(cUnit, address, false /* split */, true /*create*/, |
| 464 | /* immedPredBlockP */ NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 465 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 466 | handlers_ptr = iterator.EndDataPointer(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Process instructions with the kInstrCanBranch flag */ |
buzbee | e941e2c | 2011-12-05 12:38:17 -0800 | [diff] [blame] | 471 | STATIC BasicBlock* processCanBranch(CompilationUnit* cUnit, |
| 472 | BasicBlock* curBlock, MIR* insn, |
| 473 | int curOffset, int width, int flags, |
| 474 | const u2* codePtr, const u2* codeEnd) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 475 | { |
| 476 | int target = curOffset; |
| 477 | switch (insn->dalvikInsn.opcode) { |
| 478 | case OP_GOTO: |
| 479 | case OP_GOTO_16: |
| 480 | case OP_GOTO_32: |
| 481 | target += (int) insn->dalvikInsn.vA; |
| 482 | break; |
| 483 | case OP_IF_EQ: |
| 484 | case OP_IF_NE: |
| 485 | case OP_IF_LT: |
| 486 | case OP_IF_GE: |
| 487 | case OP_IF_GT: |
| 488 | case OP_IF_LE: |
| 489 | target += (int) insn->dalvikInsn.vC; |
| 490 | break; |
| 491 | case OP_IF_EQZ: |
| 492 | case OP_IF_NEZ: |
| 493 | case OP_IF_LTZ: |
| 494 | case OP_IF_GEZ: |
| 495 | case OP_IF_GTZ: |
| 496 | case OP_IF_LEZ: |
| 497 | target += (int) insn->dalvikInsn.vB; |
| 498 | break; |
| 499 | default: |
| 500 | LOG(FATAL) << "Unexpected opcode(" << (int)insn->dalvikInsn.opcode |
| 501 | << ") with kInstrCanBranch set"; |
| 502 | } |
| 503 | BasicBlock *takenBlock = findBlock(cUnit, target, |
| 504 | /* split */ |
| 505 | true, |
| 506 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 507 | true, |
| 508 | /* immedPredBlockP */ |
| 509 | &curBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 510 | curBlock->taken = takenBlock; |
| 511 | oatSetBit(takenBlock->predecessors, curBlock->id); |
| 512 | |
| 513 | /* Always terminate the current block for conditional branches */ |
| 514 | if (flags & kInstrCanContinue) { |
| 515 | BasicBlock *fallthroughBlock = findBlock(cUnit, |
| 516 | curOffset + width, |
| 517 | /* |
| 518 | * If the method is processed |
| 519 | * in sequential order from the |
| 520 | * beginning, we don't need to |
| 521 | * specify split for continue |
| 522 | * blocks. However, this |
| 523 | * routine can be called by |
| 524 | * compileLoop, which starts |
| 525 | * parsing the method from an |
| 526 | * arbitrary address in the |
| 527 | * method body. |
| 528 | */ |
| 529 | true, |
| 530 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 531 | true, |
| 532 | /* immedPredBlockP */ |
| 533 | &curBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 534 | curBlock->fallThrough = fallthroughBlock; |
| 535 | oatSetBit(fallthroughBlock->predecessors, curBlock->id); |
| 536 | } else if (codePtr < codeEnd) { |
| 537 | /* Create a fallthrough block for real instructions (incl. OP_NOP) */ |
| 538 | if (contentIsInsn(codePtr)) { |
| 539 | findBlock(cUnit, curOffset + width, |
| 540 | /* split */ |
| 541 | false, |
| 542 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 543 | true, |
| 544 | /* immedPredBlockP */ |
| 545 | NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
buzbee | e941e2c | 2011-12-05 12:38:17 -0800 | [diff] [blame] | 548 | return curBlock; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* Process instructions with the kInstrCanSwitch flag */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 552 | STATIC void processCanSwitch(CompilationUnit* cUnit, BasicBlock* curBlock, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 553 | MIR* insn, int curOffset, int width, int flags) |
| 554 | { |
| 555 | u2* switchData= (u2 *) (cUnit->insns + curOffset + |
| 556 | insn->dalvikInsn.vB); |
| 557 | int size; |
| 558 | int* keyTable; |
| 559 | int* targetTable; |
| 560 | int i; |
| 561 | int firstKey; |
| 562 | |
| 563 | /* |
| 564 | * Packed switch data format: |
| 565 | * ushort ident = 0x0100 magic value |
| 566 | * ushort size number of entries in the table |
| 567 | * int first_key first (and lowest) switch case value |
| 568 | * int targets[size] branch targets, relative to switch opcode |
| 569 | * |
| 570 | * Total size is (4+size*2) 16-bit code units. |
| 571 | */ |
| 572 | if (insn->dalvikInsn.opcode == OP_PACKED_SWITCH) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 573 | DCHECK_EQ(switchData[0], kPackedSwitchSignature); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 574 | size = switchData[1]; |
| 575 | firstKey = switchData[2] | (switchData[3] << 16); |
| 576 | targetTable = (int *) &switchData[4]; |
| 577 | keyTable = NULL; // Make the compiler happy |
| 578 | /* |
| 579 | * Sparse switch data format: |
| 580 | * ushort ident = 0x0200 magic value |
| 581 | * ushort size number of entries in the table; > 0 |
| 582 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 583 | * int targets[size] branch targets, relative to switch opcode |
| 584 | * |
| 585 | * Total size is (2+size*4) 16-bit code units. |
| 586 | */ |
| 587 | } else { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 588 | DCHECK_EQ(switchData[0], kSparseSwitchSignature); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 589 | size = switchData[1]; |
| 590 | keyTable = (int *) &switchData[2]; |
| 591 | targetTable = (int *) &switchData[2 + size*2]; |
| 592 | firstKey = 0; // To make the compiler happy |
| 593 | } |
| 594 | |
| 595 | if (curBlock->successorBlockList.blockListType != kNotUsed) { |
| 596 | LOG(FATAL) << "Successor block list already in use: " << |
| 597 | (int)curBlock->successorBlockList.blockListType; |
| 598 | } |
| 599 | curBlock->successorBlockList.blockListType = |
| 600 | (insn->dalvikInsn.opcode == OP_PACKED_SWITCH) ? |
| 601 | kPackedSwitch : kSparseSwitch; |
| 602 | oatInitGrowableList(&curBlock->successorBlockList.blocks, size); |
| 603 | |
| 604 | for (i = 0; i < size; i++) { |
| 605 | BasicBlock *caseBlock = findBlock(cUnit, curOffset + targetTable[i], |
| 606 | /* split */ |
| 607 | true, |
| 608 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 609 | true, |
| 610 | /* immedPredBlockP */ |
| 611 | &curBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 612 | SuccessorBlockInfo *successorBlockInfo = |
| 613 | (SuccessorBlockInfo *) oatNew(sizeof(SuccessorBlockInfo), |
| 614 | false); |
| 615 | successorBlockInfo->block = caseBlock; |
| 616 | successorBlockInfo->key = (insn->dalvikInsn.opcode == OP_PACKED_SWITCH)? |
| 617 | firstKey + i : keyTable[i]; |
| 618 | oatInsertGrowableList(&curBlock->successorBlockList.blocks, |
| 619 | (intptr_t) successorBlockInfo); |
| 620 | oatSetBit(caseBlock->predecessors, curBlock->id); |
| 621 | } |
| 622 | |
| 623 | /* Fall-through case */ |
| 624 | BasicBlock* fallthroughBlock = findBlock(cUnit, |
| 625 | curOffset + width, |
| 626 | /* split */ |
| 627 | false, |
| 628 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 629 | true, |
| 630 | /* immedPredBlockP */ |
| 631 | NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 632 | curBlock->fallThrough = fallthroughBlock; |
| 633 | oatSetBit(fallthroughBlock->predecessors, curBlock->id); |
| 634 | } |
| 635 | |
| 636 | /* Process instructions with the kInstrCanThrow flag */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 637 | STATIC void processCanThrow(CompilationUnit* cUnit, BasicBlock* curBlock, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 638 | MIR* insn, int curOffset, int width, int flags, |
| 639 | ArenaBitVector* tryBlockAddr, const u2* codePtr, |
| 640 | const u2* codeEnd) |
| 641 | { |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 642 | const art::DexFile::CodeItem* code_item = cUnit->code_item; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 643 | |
| 644 | /* In try block */ |
| 645 | if (oatIsBitSet(tryBlockAddr, curOffset)) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 646 | art::CatchHandlerIterator iterator(*code_item, curOffset); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 647 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 648 | if (curBlock->successorBlockList.blockListType != kNotUsed) { |
| 649 | LOG(FATAL) << "Successor block list already in use: " << |
| 650 | (int)curBlock->successorBlockList.blockListType; |
| 651 | } |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 652 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 653 | curBlock->successorBlockList.blockListType = kCatch; |
| 654 | oatInitGrowableList(&curBlock->successorBlockList.blocks, 2); |
| 655 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 656 | for (;iterator.HasNext(); iterator.Next()) { |
| 657 | BasicBlock *catchBlock = findBlock(cUnit, iterator.GetHandlerAddress(), |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 658 | false /* split*/, |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 659 | false /* creat */, |
| 660 | NULL /* immedPredBlockP */); |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 661 | catchBlock->catchEntry = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 662 | SuccessorBlockInfo *successorBlockInfo = |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 663 | (SuccessorBlockInfo *) oatNew(sizeof(SuccessorBlockInfo), |
| 664 | false); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 665 | successorBlockInfo->block = catchBlock; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 666 | successorBlockInfo->key = iterator.GetHandlerTypeIndex(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 667 | oatInsertGrowableList(&curBlock->successorBlockList.blocks, |
| 668 | (intptr_t) successorBlockInfo); |
| 669 | oatSetBit(catchBlock->predecessors, curBlock->id); |
| 670 | } |
| 671 | } else { |
| 672 | BasicBlock *ehBlock = oatNewBB(kExceptionHandling, |
| 673 | cUnit->numBlocks++); |
| 674 | curBlock->taken = ehBlock; |
| 675 | oatInsertGrowableList(&cUnit->blockList, (intptr_t) ehBlock); |
| 676 | ehBlock->startOffset = curOffset; |
| 677 | oatSetBit(ehBlock->predecessors, curBlock->id); |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Force the current block to terminate. |
| 682 | * |
| 683 | * Data may be present before codeEnd, so we need to parse it to know |
| 684 | * whether it is code or data. |
| 685 | */ |
| 686 | if (codePtr < codeEnd) { |
| 687 | /* Create a fallthrough block for real instructions (incl. OP_NOP) */ |
| 688 | if (contentIsInsn(codePtr)) { |
| 689 | BasicBlock *fallthroughBlock = findBlock(cUnit, |
| 690 | curOffset + width, |
| 691 | /* split */ |
| 692 | false, |
| 693 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 694 | true, |
| 695 | /* immedPredBlockP */ |
| 696 | NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 697 | /* |
buzbee | 510c605 | 2011-10-27 10:47:20 -0700 | [diff] [blame] | 698 | * OP_THROW is an unconditional branch. NOTE: |
| 699 | * OP_THROW_VERIFICATION_ERROR is also an unconditional |
| 700 | * branch, but we shouldn't treat it as such until we have |
| 701 | * a dead code elimination pass (which won't be important |
| 702 | * until inlining w/ constant propogation is implemented. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 703 | */ |
buzbee | 510c605 | 2011-10-27 10:47:20 -0700 | [diff] [blame] | 704 | if (insn->dalvikInsn.opcode != OP_THROW) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 705 | curBlock->fallThrough = fallthroughBlock; |
| 706 | oatSetBit(fallthroughBlock->predecessors, curBlock->id); |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Compile a method. |
| 714 | */ |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 715 | CompiledMethod* oatCompileMethod(const Compiler& compiler, const art::DexFile::CodeItem* code_item, |
| 716 | uint32_t access_flags, uint32_t method_idx, |
| 717 | const art::ClassLoader* class_loader, |
| 718 | const art::DexFile& dex_file, art::InstructionSet insnSet) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 719 | { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 720 | VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "..."; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 721 | oatArenaReset(); |
Brian Carlstrom | 94496d3 | 2011-08-22 09:22:47 -0700 | [diff] [blame] | 722 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 723 | const u2* codePtr = code_item->insns_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 724 | const u2* codeEnd = code_item->insns_ + code_item->insns_size_in_code_units_; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 725 | int numBlocks = 0; |
| 726 | unsigned int curOffset = 0; |
| 727 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 728 | oatInit(compiler); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 729 | |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 730 | art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 731 | UniquePtr<CompilationUnit> cUnit(new CompilationUnit); |
| 732 | memset(cUnit.get(), 0, sizeof(*cUnit)); |
| 733 | cUnit->compiler = &compiler; |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 734 | cUnit->class_linker = class_linker; |
| 735 | cUnit->dex_file = &dex_file; |
| 736 | cUnit->dex_cache = class_linker->FindDexCache(dex_file); |
| 737 | cUnit->method_idx = method_idx; |
| 738 | cUnit->code_item = code_item; |
| 739 | cUnit->access_flags = access_flags; |
| 740 | cUnit->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 741 | cUnit->instructionSet = (OatInstructionSetType)insnSet; |
| 742 | cUnit->insns = code_item->insns_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 743 | cUnit->insnsSize = code_item->insns_size_in_code_units_; |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 744 | cUnit->numIns = code_item->ins_size_; |
| 745 | cUnit->numRegs = code_item->registers_size_ - cUnit->numIns; |
| 746 | cUnit->numOuts = code_item->outs_size_; |
| 747 | /* Adjust this value accordingly once inlining is performed */ |
| 748 | cUnit->numDalvikRegisters = code_item->registers_size_; |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 749 | bool useMatch = compilerMethodMatch.length() != 0; |
| 750 | bool match = useMatch && (compilerFlipMatch ^ |
Ian Rogers | a3760aa | 2011-11-14 14:32:37 -0800 | [diff] [blame] | 751 | (PrettyMethod(method_idx, dex_file).find(compilerMethodMatch) != std::string::npos)); |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 752 | if (!useMatch || match) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 753 | cUnit->disableOpt = compilerOptimizerDisableFlags; |
| 754 | cUnit->enableDebug = compilerDebugFlags; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 755 | cUnit->printMe = VLOG_IS_ON(compiler) || (cUnit->enableDebug & (1 << kDebugVerbose)); |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 756 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 757 | |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 758 | /* Assume non-throwing leaf */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 759 | cUnit->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE); |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 760 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 761 | /* Initialize the block list */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 762 | oatInitGrowableList(&cUnit->blockList, 40); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 763 | |
| 764 | /* Initialize the switchTables list */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 765 | oatInitGrowableList(&cUnit->switchTables, 4); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 766 | |
| 767 | /* Intialize the fillArrayData list */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 768 | oatInitGrowableList(&cUnit->fillArrayData, 4); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 769 | |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 770 | /* Intialize the throwLaunchpads list */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 771 | oatInitGrowableList(&cUnit->throwLaunchpads, 4); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 772 | |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 773 | /* Intialize the suspendLaunchpads list */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 774 | oatInitGrowableList(&cUnit->suspendLaunchpads, 4); |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 775 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 776 | /* Allocate the bit-vector to track the beginning of basic blocks */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 777 | ArenaBitVector *tryBlockAddr = oatAllocBitVector(cUnit->insnsSize, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 778 | true /* expandable */); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 779 | cUnit->tryBlockAddr = tryBlockAddr; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 780 | |
| 781 | /* Create the default entry and exit blocks and enter them to the list */ |
| 782 | BasicBlock *entryBlock = oatNewBB(kEntryBlock, numBlocks++); |
| 783 | BasicBlock *exitBlock = oatNewBB(kExitBlock, numBlocks++); |
| 784 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 785 | cUnit->entryBlock = entryBlock; |
| 786 | cUnit->exitBlock = exitBlock; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 787 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 788 | oatInsertGrowableList(&cUnit->blockList, (intptr_t) entryBlock); |
| 789 | oatInsertGrowableList(&cUnit->blockList, (intptr_t) exitBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 790 | |
| 791 | /* Current block to record parsed instructions */ |
| 792 | BasicBlock *curBlock = oatNewBB(kDalvikByteCode, numBlocks++); |
| 793 | curBlock->startOffset = 0; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 794 | oatInsertGrowableList(&cUnit->blockList, (intptr_t) curBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 795 | entryBlock->fallThrough = curBlock; |
| 796 | oatSetBit(curBlock->predecessors, entryBlock->id); |
| 797 | |
| 798 | /* |
| 799 | * Store back the number of blocks since new blocks may be created of |
| 800 | * accessing cUnit. |
| 801 | */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 802 | cUnit->numBlocks = numBlocks; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 803 | |
| 804 | /* Identify code range in try blocks and set up the empty catch blocks */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 805 | processTryCatchBlocks(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 806 | |
| 807 | /* Parse all instructions and put them into containing basic blocks */ |
| 808 | while (codePtr < codeEnd) { |
| 809 | MIR *insn = (MIR *) oatNew(sizeof(MIR), true); |
| 810 | insn->offset = curOffset; |
| 811 | int width = parseInsn(codePtr, &insn->dalvikInsn, false); |
| 812 | insn->width = width; |
| 813 | |
| 814 | /* Terminate when the data section is seen */ |
| 815 | if (width == 0) |
| 816 | break; |
| 817 | |
| 818 | oatAppendMIR(curBlock, insn); |
| 819 | |
| 820 | codePtr += width; |
| 821 | int flags = dexGetFlagsFromOpcode(insn->dalvikInsn.opcode); |
| 822 | |
| 823 | if (flags & kInstrCanBranch) { |
buzbee | e941e2c | 2011-12-05 12:38:17 -0800 | [diff] [blame] | 824 | curBlock = processCanBranch(cUnit.get(), curBlock, insn, curOffset, |
| 825 | width, flags, codePtr, codeEnd); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 826 | } else if (flags & kInstrCanReturn) { |
| 827 | curBlock->fallThrough = exitBlock; |
| 828 | oatSetBit(exitBlock->predecessors, curBlock->id); |
| 829 | /* |
| 830 | * Terminate the current block if there are instructions |
| 831 | * afterwards. |
| 832 | */ |
| 833 | if (codePtr < codeEnd) { |
| 834 | /* |
| 835 | * Create a fallthrough block for real instructions |
| 836 | * (incl. OP_NOP). |
| 837 | */ |
| 838 | if (contentIsInsn(codePtr)) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 839 | findBlock(cUnit.get(), curOffset + width, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 840 | /* split */ |
| 841 | false, |
| 842 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 843 | true, |
| 844 | /* immedPredBlockP */ |
| 845 | NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } else if (flags & kInstrCanThrow) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 849 | processCanThrow(cUnit.get(), curBlock, insn, curOffset, width, flags, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 850 | tryBlockAddr, codePtr, codeEnd); |
| 851 | } else if (flags & kInstrCanSwitch) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 852 | processCanSwitch(cUnit.get(), curBlock, insn, curOffset, width, flags); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 853 | } |
| 854 | curOffset += width; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 855 | BasicBlock *nextBlock = findBlock(cUnit.get(), curOffset, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 856 | /* split */ |
| 857 | false, |
| 858 | /* create */ |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 859 | false, |
| 860 | /* immedPredBlockP */ |
| 861 | NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 862 | if (nextBlock) { |
| 863 | /* |
| 864 | * The next instruction could be the target of a previously parsed |
| 865 | * forward branch so a block is already created. If the current |
| 866 | * instruction is not an unconditional branch, connect them through |
| 867 | * the fall-through link. |
| 868 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 869 | DCHECK(curBlock->fallThrough == NULL || |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 870 | curBlock->fallThrough == nextBlock || |
| 871 | curBlock->fallThrough == exitBlock); |
| 872 | |
| 873 | if ((curBlock->fallThrough == NULL) && |
| 874 | (flags & kInstrCanContinue)) { |
| 875 | curBlock->fallThrough = nextBlock; |
| 876 | oatSetBit(nextBlock->predecessors, curBlock->id); |
| 877 | } |
| 878 | curBlock = nextBlock; |
| 879 | } |
| 880 | } |
| 881 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 882 | if (cUnit->printMe) { |
| 883 | oatDumpCompilationUnit(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 884 | } |
| 885 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 886 | /* Verify if all blocks are connected as claimed */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 887 | oatDataFlowAnalysisDispatcher(cUnit.get(), verifyPredInfo, kAllNodes, false /* isIterative */); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 888 | |
| 889 | /* Perform SSA transformation for the whole method */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 890 | oatMethodSSATransformation(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 891 | |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 892 | /* Perform null check elimination */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 893 | oatMethodNullCheckElimination(cUnit.get()); |
buzbee | 43a3642 | 2011-09-14 14:00:13 -0700 | [diff] [blame] | 894 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 895 | oatInitializeRegAlloc(cUnit.get()); // Needs to happen after SSA naming |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 896 | |
| 897 | /* Allocate Registers using simple local allocation scheme */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 898 | oatSimpleRegAlloc(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 899 | |
| 900 | /* Convert MIR to LIR, etc. */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 901 | oatMethodMIR2LIR(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 902 | |
| 903 | // Debugging only |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 904 | if (cUnit->enableDebug & (1 << kDebugDumpCFG)) { |
| 905 | oatDumpCFG(cUnit.get(), "/sdcard/cfg/"); |
buzbee | ec5adf3 | 2011-09-11 15:25:43 -0700 | [diff] [blame] | 906 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 907 | |
| 908 | /* Method is not empty */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 909 | if (cUnit->firstLIRInsn) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 910 | |
| 911 | // mark the targets of switch statement case labels |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 912 | oatProcessSwitchTables(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 913 | |
| 914 | /* Convert LIR into machine code. */ |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 915 | oatAssembleLIR(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 916 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 917 | if (cUnit->printMe) { |
| 918 | oatCodegenDump(cUnit.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 922 | // Combine vmap tables - core regs, then fp regs - into vmapTable |
| 923 | std::vector<uint16_t> vmapTable; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 924 | for (size_t i = 0 ; i < cUnit->coreVmapTable.size(); i++) { |
| 925 | vmapTable.push_back(cUnit->coreVmapTable[i]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 926 | } |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 927 | // Add a marker to take place of lr |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 928 | vmapTable.push_back(INVALID_VREG); |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 929 | // Combine vmap tables - core regs, then fp regs |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 930 | for (uint32_t i = 0; i < cUnit->fpVmapTable.size(); i++) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 931 | vmapTable.push_back(cUnit->fpVmapTable[i]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 932 | } |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 933 | DCHECK_EQ(vmapTable.size(), |
| 934 | static_cast<uint32_t>(__builtin_popcount(cUnit->coreSpillMask) |
| 935 | + __builtin_popcount(cUnit->fpSpillMask))); |
| 936 | DCHECK_GE(vmapTable.size(), 1U); // should always at least one INVALID_VREG for lr |
| 937 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 938 | CompiledMethod* result = new CompiledMethod(art::kThumb2, cUnit->codeBuffer, |
| 939 | cUnit->frameSize, cUnit->coreSpillMask, |
| 940 | cUnit->fpSpillMask, cUnit->mappingTable, |
| 941 | vmapTable); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 942 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 943 | VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file) |
| 944 | << " (" << (cUnit->codeBuffer.size() * sizeof(cUnit->codeBuffer[0])) << " bytes)"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 945 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 946 | return result; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 949 | void oatInit(const Compiler& compiler) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 950 | { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 951 | static bool initialized = false; |
| 952 | if (initialized) |
| 953 | return; |
| 954 | initialized = true; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 955 | VLOG(compiler) << "Initializing compiler"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 956 | if (!oatArchInit()) { |
| 957 | LOG(FATAL) << "Failed to initialize oat"; |
| 958 | } |
| 959 | if (!oatHeapInit()) { |
| 960 | LOG(FATAL) << "Failed to initialize oat heap"; |
| 961 | } |
| 962 | } |