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