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