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