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