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