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