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