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