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