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