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