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