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