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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/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" |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 25 | #include "codegen/mir_to_gbc.h" |
| 26 | #include "codegen/mir_to_lir.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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 58 | LLVMInfo* llvm_info = new LLVMInfo(); |
| 59 | compiler.SetCompilerContext(llvm_info); |
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 |
buzbee | 4ef3e45 | 2012-12-14 13:35:28 -0800 | [diff] [blame^] | 69 | (1 << kLoadStoreElimination) | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 70 | //(1 << kLoadHoisting) | |
| 71 | //(1 << kSuppressLoads) | |
| 72 | //(1 << kNullCheckElimination) | |
| 73 | //(1 << kPromoteRegs) | |
| 74 | //(1 << kTrackLiveTemps) | |
buzbee | 4ef3e45 | 2012-12-14 13:35:28 -0800 | [diff] [blame^] | 75 | (1 << kSkipLargeMethodOptimization) | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 101 | static bool ContentIsInsn(const uint16_t* code_ptr) { |
| 102 | uint16_t instr = *code_ptr; |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 115 | static int ParseInsn(CompilationUnit* cu, const uint16_t* code_ptr, |
buzbee | a169e1d | 2012-12-05 14:26:44 -0800 | [diff] [blame] | 116 | DecodedInstruction* decoded_instruction) |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 119 | if (!ContentIsInsn(code_ptr)) { |
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 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 123 | const Instruction* instruction = Instruction::At(code_ptr); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 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 | return instruction->SizeInCodeUnits(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | #define UNKNOWN_TARGET 0xffffffff |
| 130 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 131 | /* Split an existing block from the specified code offset into two */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 132 | static BasicBlock *SplitBlock(CompilationUnit* cu, unsigned int code_offset, |
| 133 | BasicBlock* orig_block, BasicBlock** immed_pred_block_p) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 134 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 135 | MIR* insn = orig_block->first_mir_insn; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 136 | while (insn) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 137 | if (insn->offset == code_offset) break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 138 | insn = insn->next; |
| 139 | } |
| 140 | if (insn == NULL) { |
| 141 | LOG(FATAL) << "Break split failed"; |
| 142 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 143 | BasicBlock *bottom_block = NewMemBB(cu, kDalvikByteCode, |
| 144 | cu->num_blocks++); |
| 145 | InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bottom_block)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 146 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 147 | bottom_block->start_offset = code_offset; |
| 148 | bottom_block->first_mir_insn = insn; |
| 149 | bottom_block->last_mir_insn = orig_block->last_mir_insn; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 150 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 151 | /* Add it to the quick lookup cache */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 152 | cu->block_map.Put(bottom_block->start_offset, bottom_block); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 153 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 154 | /* Handle the taken path */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 155 | bottom_block->taken = orig_block->taken; |
| 156 | if (bottom_block->taken) { |
| 157 | orig_block->taken = NULL; |
| 158 | DeleteGrowableList(bottom_block->taken->predecessors, reinterpret_cast<uintptr_t>(orig_block)); |
| 159 | InsertGrowableList(cu, bottom_block->taken->predecessors, |
| 160 | reinterpret_cast<uintptr_t>(bottom_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* Handle the fallthrough path */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 164 | bottom_block->fall_through = orig_block->fall_through; |
| 165 | orig_block->fall_through = bottom_block; |
| 166 | InsertGrowableList(cu, bottom_block->predecessors, |
| 167 | reinterpret_cast<uintptr_t>(orig_block)); |
| 168 | if (bottom_block->fall_through) { |
| 169 | DeleteGrowableList(bottom_block->fall_through->predecessors, |
| 170 | reinterpret_cast<uintptr_t>(orig_block)); |
| 171 | InsertGrowableList(cu, bottom_block->fall_through->predecessors, |
| 172 | reinterpret_cast<uintptr_t>(bottom_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Handle the successor list */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 176 | if (orig_block->successor_block_list.block_list_type != kNotUsed) { |
| 177 | bottom_block->successor_block_list = orig_block->successor_block_list; |
| 178 | orig_block->successor_block_list.block_list_type = kNotUsed; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 179 | GrowableListIterator iterator; |
| 180 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 181 | GrowableListIteratorInit(&bottom_block->successor_block_list.blocks, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 182 | &iterator); |
| 183 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 184 | SuccessorBlockInfo *successor_block_info = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 185 | reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 186 | if (successor_block_info == NULL) break; |
| 187 | BasicBlock *bb = successor_block_info->block; |
| 188 | DeleteGrowableList(bb->predecessors, reinterpret_cast<uintptr_t>(orig_block)); |
| 189 | InsertGrowableList(cu, bb->predecessors, reinterpret_cast<uintptr_t>(bottom_block)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 190 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 191 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 192 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 193 | orig_block->last_mir_insn = insn->prev; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 194 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 195 | insn->prev->next = NULL; |
| 196 | insn->prev = NULL; |
| 197 | /* |
| 198 | * Update the immediate predecessor block pointer so that outgoing edges |
| 199 | * can be applied to the proper block. |
| 200 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 201 | if (immed_pred_block_p) { |
| 202 | DCHECK_EQ(*immed_pred_block_p, orig_block); |
| 203 | *immed_pred_block_p = bottom_block; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 204 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 205 | return bottom_block; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Given a code offset, find out the block that starts with it. If the offset |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 210 | * is in the middle of an existing block, split it into two. If immed_pred_block_p |
| 211 | * is not non-null and is the block being split, update *immed_pred_block_p to |
buzbee | 9ab05de | 2012-01-18 15:43:48 -0800 | [diff] [blame] | 212 | * point to the bottom block so that outgoing edges can be set up properly |
| 213 | * (by the caller) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 214 | * Utilizes a map for fast lookup of the typical cases. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 215 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 216 | BasicBlock *FindBlock(CompilationUnit* cu, unsigned int code_offset, |
| 217 | bool split, bool create, BasicBlock** immed_pred_block_p) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 218 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 219 | GrowableList* block_list = &cu->block_list; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 220 | BasicBlock* bb; |
| 221 | unsigned int i; |
| 222 | SafeMap<unsigned int, BasicBlock*>::iterator it; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 223 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 224 | it = cu->block_map.find(code_offset); |
| 225 | if (it != cu->block_map.end()) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 226 | return it->second; |
| 227 | } else if (!create) { |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | if (split) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 232 | for (i = 0; i < block_list->num_used; i++) { |
| 233 | bb = reinterpret_cast<BasicBlock*>(block_list->elem_list[i]); |
| 234 | if (bb->block_type != kDalvikByteCode) continue; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 235 | /* Check if a branch jumps into the middle of an existing block */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 236 | if ((code_offset > bb->start_offset) && (bb->last_mir_insn != NULL) && |
| 237 | (code_offset <= bb->last_mir_insn->offset)) { |
| 238 | BasicBlock *new_bb = SplitBlock(cu, code_offset, bb, |
| 239 | bb == *immed_pred_block_p ? |
| 240 | immed_pred_block_p : NULL); |
| 241 | return new_bb; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 242 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 243 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 244 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 245 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 246 | /* Create a new one */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 247 | bb = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++); |
| 248 | InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bb)); |
| 249 | bb->start_offset = code_offset; |
| 250 | cu->block_map.Put(bb->start_offset, bb); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 251 | return bb; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 252 | } |
| 253 | |
buzbee | f58c12c | 2012-07-03 15:06:29 -0700 | [diff] [blame] | 254 | /* Find existing block */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 255 | BasicBlock* FindBlock(CompilationUnit* cu, unsigned int code_offset) |
buzbee | f58c12c | 2012-07-03 15:06:29 -0700 | [diff] [blame] | 256 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 257 | return FindBlock(cu, code_offset, false, false, NULL); |
buzbee | f58c12c | 2012-07-03 15:06:29 -0700 | [diff] [blame] | 258 | } |
| 259 | |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 260 | /* Turn method name into a legal Linux file name */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 261 | void ReplaceSpecialChars(std::string& str) |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 262 | { |
| 263 | static const struct { const char before; const char after; } match[] = |
| 264 | {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'}, |
| 265 | {'(','@'}, {')','@'}, {'<','='}, {'>','='}}; |
| 266 | for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) { |
| 267 | std::replace(str.begin(), str.end(), match[i].before, match[i].after); |
| 268 | } |
| 269 | } |
| 270 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 271 | /* Dump the CFG into a DOT graph */ |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 272 | void DumpCFG(CompilationUnit* cu, const char* dir_prefix, bool all_blocks) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 273 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 274 | FILE* file; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 275 | std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file)); |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 276 | ReplaceSpecialChars(fname); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 277 | fname = StringPrintf("%s%s%x.dot", dir_prefix, fname.c_str(), |
| 278 | cu->entry_block->fall_through->start_offset); |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 279 | file = fopen(fname.c_str(), "w"); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 280 | if (file == NULL) { |
| 281 | return; |
| 282 | } |
| 283 | fprintf(file, "digraph G {\n"); |
| 284 | |
| 285 | fprintf(file, " rankdir=TB\n"); |
| 286 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 287 | int num_blocks = all_blocks ? cu->num_blocks : cu->num_reachable_blocks; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 288 | int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 289 | const GrowableList *block_list = &cu->block_list; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 290 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 291 | for (idx = 0; idx < num_blocks; idx++) { |
| 292 | int block_idx = all_blocks ? idx : cu->dfs_order.elem_list[idx]; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 293 | BasicBlock *bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, block_idx)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 294 | if (bb == NULL) break; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 295 | if (bb->block_type == kDead) continue; |
| 296 | if (bb->block_type == kEntryBlock) { |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 297 | fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 298 | } else if (bb->block_type == kExitBlock) { |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 299 | fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 300 | } else if (bb->block_type == kDalvikByteCode) { |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 301 | fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 302 | bb->start_offset, bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 303 | const MIR *mir; |
| 304 | fprintf(file, " {block id %d\\l}%s\\\n", bb->id, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 305 | bb->first_mir_insn ? " | " : " "); |
| 306 | for (mir = bb->first_mir_insn; mir; mir = mir->next) { |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 307 | int opcode = mir->dalvikInsn.opcode; |
| 308 | fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset, |
buzbee | a169e1d | 2012-12-05 14:26:44 -0800 | [diff] [blame] | 309 | mir->ssa_rep ? GetDalvikDisassembly(cu, mir) : |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 310 | (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) : |
| 311 | extended_mir_op_names[opcode - kMirOpFirst], |
| 312 | (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ", |
| 313 | (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ", |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 314 | mir->next ? " | " : " "); |
| 315 | } |
| 316 | fprintf(file, " }\"];\n\n"); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 317 | } else if (bb->block_type == kExceptionHandling) { |
| 318 | char block_name[BLOCK_NAME_LEN]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 319 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 320 | GetBlockName(bb, block_name); |
| 321 | fprintf(file, " %s [shape=invhouse];\n", block_name); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 322 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 323 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 324 | char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN]; |
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 | if (bb->taken) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 327 | GetBlockName(bb, block_name1); |
| 328 | GetBlockName(bb->taken, block_name2); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 329 | fprintf(file, " %s:s -> %s:n [style=dotted]\n", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 330 | block_name1, block_name2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 331 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 332 | if (bb->fall_through) { |
| 333 | GetBlockName(bb, block_name1); |
| 334 | GetBlockName(bb->fall_through, block_name2); |
| 335 | fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 336 | } |
| 337 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 338 | if (bb->successor_block_list.block_list_type != kNotUsed) { |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 339 | fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 340 | bb->start_offset, bb->id, |
| 341 | (bb->successor_block_list.block_list_type == kCatch) ? |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 342 | "Mrecord" : "record"); |
| 343 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 344 | GrowableListIteratorInit(&bb->successor_block_list.blocks, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 345 | &iterator); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 346 | SuccessorBlockInfo *successor_block_info = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 347 | reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 348 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 349 | int succ_id = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 350 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 351 | if (successor_block_info == NULL) break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 352 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 353 | BasicBlock *dest_block = successor_block_info->block; |
| 354 | SuccessorBlockInfo *next_successor_block_info = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 355 | reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 356 | |
| 357 | fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 358 | succ_id++, |
| 359 | successor_block_info->key, |
| 360 | dest_block->start_offset, |
| 361 | (next_successor_block_info != NULL) ? " | " : " "); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 362 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 363 | successor_block_info = next_successor_block_info; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 364 | } |
| 365 | fprintf(file, " }\"];\n\n"); |
| 366 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 367 | GetBlockName(bb, block_name1); |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 368 | fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 369 | block_name1, bb->start_offset, bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 370 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 371 | if (bb->successor_block_list.block_list_type == kPackedSwitch || |
| 372 | bb->successor_block_list.block_list_type == kSparseSwitch) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 373 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 374 | GrowableListIteratorInit(&bb->successor_block_list.blocks, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 375 | &iterator); |
| 376 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 377 | succ_id = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 378 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 379 | SuccessorBlockInfo *successor_block_info = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 380 | reinterpret_cast<SuccessorBlockInfo*>( GrowableListIteratorNext(&iterator)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 381 | if (successor_block_info == NULL) break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 382 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 383 | BasicBlock *dest_block = successor_block_info->block; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 384 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 385 | GetBlockName(dest_block, block_name2); |
| 386 | fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset, |
| 387 | bb->id, succ_id++, block_name2); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
| 391 | fprintf(file, "\n"); |
| 392 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 393 | if (cu->verbose) { |
| 394 | /* Display the dominator tree */ |
| 395 | GetBlockName(bb, block_name1); |
| 396 | fprintf(file, " cfg%s [label=\"%s\", shape=none];\n", |
| 397 | block_name1, block_name1); |
| 398 | if (bb->i_dom) { |
| 399 | GetBlockName(bb->i_dom, block_name2); |
| 400 | fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1); |
| 401 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 409 | static bool VerifyPredInfo(CompilationUnit* cu, 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 415 | BasicBlock *pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter)); |
| 416 | if (!pred_bb) break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 417 | bool found = false; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 418 | if (pred_bb->taken == bb) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 419 | found = true; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 420 | } else if (pred_bb->fall_through == bb) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 421 | found = true; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 422 | } else if (pred_bb->successor_block_list.block_list_type != kNotUsed) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 423 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 424 | GrowableListIteratorInit(&pred_bb->successor_block_list.blocks, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 425 | &iterator); |
| 426 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 427 | SuccessorBlockInfo *successor_block_info = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 428 | reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 429 | if (successor_block_info == NULL) break; |
| 430 | BasicBlock *succ_bb = successor_block_info->block; |
| 431 | if (succ_bb == 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) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 438 | char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN]; |
| 439 | GetBlockName(bb, block_name1); |
| 440 | GetBlockName(pred_bb, block_name2); |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 441 | DumpCFG(cu, "/sdcard/cfg/", false); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 442 | LOG(FATAL) << "Successor " << block_name1 << "not found from " |
| 443 | << block_name2; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 450 | static void ProcessTryCatchBlocks(CompilationUnit* cu) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 451 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 452 | const DexFile::CodeItem* code_item = cu->code_item; |
| 453 | int tries_size = code_item->tries_size_; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 454 | int offset; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 455 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 456 | if (tries_size == 0) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 460 | ArenaBitVector* try_block_addr = cu->try_block_addr; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 461 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 462 | for (int i = 0; i < tries_size; i++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 463 | const DexFile::TryItem* pTry = |
| 464 | DexFile::GetTryItems(*code_item, i); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 465 | int start_offset = pTry->start_addr_; |
| 466 | int end_offset = start_offset + pTry->insn_count_; |
| 467 | for (offset = start_offset; offset < end_offset; offset++) { |
| 468 | SetBit(cu, try_block_addr, 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 479 | FindBlock(cu, address, false /* split */, true /*create*/, |
| 480 | /* immed_pred_block_p */ 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 487 | static BasicBlock* ProcessCanBranch(CompilationUnit* cu, BasicBlock* cur_block, |
| 488 | MIR* insn, int cur_offset, int width, int flags, |
| 489 | const uint16_t* code_ptr, const uint16_t* code_end) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 490 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 491 | int target = cur_offset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 504 | cur_block->conditional_branch = 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 513 | cur_block->conditional_branch = 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 519 | BasicBlock *taken_block = FindBlock(cu, target, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 520 | /* split */ |
| 521 | true, |
| 522 | /* create */ |
| 523 | true, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 524 | /* immed_pred_block_p */ |
| 525 | &cur_block); |
| 526 | cur_block->taken = taken_block; |
| 527 | InsertGrowableList(cu, taken_block->predecessors, reinterpret_cast<uintptr_t>(cur_block)); |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 531 | BasicBlock *fallthrough_block = FindBlock(cu, |
| 532 | cur_offset + width, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 548 | /* immed_pred_block_p */ |
| 549 | &cur_block); |
| 550 | cur_block->fall_through = fallthrough_block; |
| 551 | InsertGrowableList(cu, fallthrough_block->predecessors, |
| 552 | reinterpret_cast<uintptr_t>(cur_block)); |
| 553 | } else if (code_ptr < code_end) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 554 | /* Create a fallthrough block for real instructions (incl. NOP) */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 555 | if (ContentIsInsn(code_ptr)) { |
| 556 | FindBlock(cu, cur_offset + width, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 557 | /* split */ |
| 558 | false, |
| 559 | /* create */ |
| 560 | true, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 561 | /* immed_pred_block_p */ |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 565 | return cur_block; |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 569 | static void ProcessCanSwitch(CompilationUnit* cu, BasicBlock* cur_block, |
| 570 | MIR* insn, int cur_offset, int width, int flags) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 571 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 572 | const uint16_t* switch_data = |
| 573 | reinterpret_cast<const uint16_t*>(cu->insns + cur_offset + 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; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 576 | const int* target_table; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 577 | int i; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 578 | int first_key; |
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) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 590 | DCHECK_EQ(static_cast<int>(switch_data[0]), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 591 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 592 | size = switch_data[1]; |
| 593 | first_key = switch_data[2] | (switch_data[3] << 16); |
| 594 | target_table = reinterpret_cast<const int*>(&switch_data[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 { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 606 | DCHECK_EQ(static_cast<int>(switch_data[0]), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 607 | static_cast<int>(Instruction::kSparseSwitchSignature)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 608 | size = switch_data[1]; |
| 609 | keyTable = reinterpret_cast<const int*>(&switch_data[2]); |
| 610 | target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]); |
| 611 | first_key = 0; // To make the compiler happy |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 612 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 613 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 614 | if (cur_block->successor_block_list.block_list_type != kNotUsed) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 615 | LOG(FATAL) << "Successor block list already in use: " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 616 | << static_cast<int>(cur_block->successor_block_list.block_list_type); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 617 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 618 | cur_block->successor_block_list.block_list_type = |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 619 | (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? |
| 620 | kPackedSwitch : kSparseSwitch; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 621 | CompilerInitGrowableList(cu, &cur_block->successor_block_list.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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 625 | BasicBlock *case_block = FindBlock(cu, cur_offset + target_table[i], |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 626 | /* split */ |
| 627 | true, |
| 628 | /* create */ |
| 629 | true, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 630 | /* immed_pred_block_p */ |
| 631 | &cur_block); |
| 632 | SuccessorBlockInfo *successor_block_info = |
| 633 | static_cast<SuccessorBlockInfo*>(NewMem(cu, sizeof(SuccessorBlockInfo), |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 634 | false, kAllocSuccessor)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 635 | successor_block_info->block = case_block; |
| 636 | successor_block_info->key = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 637 | (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 638 | first_key + i : keyTable[i]; |
| 639 | InsertGrowableList(cu, &cur_block->successor_block_list.blocks, |
| 640 | reinterpret_cast<uintptr_t>(successor_block_info)); |
| 641 | InsertGrowableList(cu, case_block->predecessors, |
| 642 | reinterpret_cast<uintptr_t>(cur_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /* Fall-through case */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 646 | BasicBlock* fallthrough_block = FindBlock(cu, |
| 647 | cur_offset + width, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 648 | /* split */ |
| 649 | false, |
| 650 | /* create */ |
| 651 | true, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 652 | /* immed_pred_block_p */ |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 653 | NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 654 | cur_block->fall_through = fallthrough_block; |
| 655 | InsertGrowableList(cu, fallthrough_block->predecessors, |
| 656 | reinterpret_cast<uintptr_t>(cur_block)); |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 660 | static BasicBlock* ProcessCanThrow(CompilationUnit* cu, BasicBlock* cur_block, |
| 661 | MIR* insn, int cur_offset, int width, int flags, |
| 662 | ArenaBitVector* try_block_addr, const uint16_t* code_ptr, |
| 663 | const uint16_t* code_end) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 664 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 665 | const DexFile::CodeItem* code_item = cu->code_item; |
| 666 | bool in_try_block = IsBitSet(try_block_addr, cur_offset); |
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 */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 669 | if (in_try_block) { |
| 670 | CatchHandlerIterator iterator(*code_item, cur_offset); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 671 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 672 | if (cur_block->successor_block_list.block_list_type != kNotUsed) { |
| 673 | LOG(INFO) << PrettyMethod(cu->method_idx, *cu->dex_file); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 674 | LOG(FATAL) << "Successor block list already in use: " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 675 | << static_cast<int>(cur_block->successor_block_list.block_list_type); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 676 | } |
| 677 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 678 | cur_block->successor_block_list.block_list_type = kCatch; |
| 679 | CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, 2, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 680 | kListSuccessorBlocks); |
| 681 | |
| 682 | for (;iterator.HasNext(); iterator.Next()) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 683 | BasicBlock *catch_block = FindBlock(cu, iterator.GetHandlerAddress(), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 684 | false /* split*/, |
| 685 | false /* creat */, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 686 | NULL /* immed_pred_block_p */); |
| 687 | catch_block->catch_entry = true; |
| 688 | cu->catches.insert(catch_block->start_offset); |
| 689 | SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*> |
| 690 | (NewMem(cu, sizeof(SuccessorBlockInfo), false, kAllocSuccessor)); |
| 691 | successor_block_info->block = catch_block; |
| 692 | successor_block_info->key = iterator.GetHandlerTypeIndex(); |
| 693 | InsertGrowableList(cu, &cur_block->successor_block_list.blocks, |
| 694 | reinterpret_cast<uintptr_t>(successor_block_info)); |
| 695 | InsertGrowableList(cu, catch_block->predecessors, |
| 696 | reinterpret_cast<uintptr_t>(cur_block)); |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 699 | BasicBlock *eh_block = NewMemBB(cu, kExceptionHandling, |
| 700 | cu->num_blocks++); |
| 701 | cur_block->taken = eh_block; |
| 702 | InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(eh_block)); |
| 703 | eh_block->start_offset = cur_offset; |
| 704 | InsertGrowableList(cu, eh_block->predecessors, reinterpret_cast<uintptr_t>(cur_block)); |
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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 708 | cur_block->explicit_throw = true; |
| 709 | if ((code_ptr < code_end) && ContentIsInsn(code_ptr)) { |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 710 | // Force creation of new block following THROW via side-effect |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 711 | FindBlock(cu, cur_offset + width, /* split */ false, |
| 712 | /* create */ true, /* immed_pred_block_p */ NULL); |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 713 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 714 | if (!in_try_block) { |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 715 | // Don't split a THROW that can't rethrow - we're done. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 716 | return cur_block; |
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 |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 726 | * the block_map. If the potentially-throwing instruction is the target of a |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 734 | BasicBlock *new_block = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++); |
| 735 | InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(new_block)); |
| 736 | new_block->start_offset = insn->offset; |
| 737 | cur_block->fall_through = new_block; |
| 738 | InsertGrowableList(cu, new_block->predecessors, reinterpret_cast<uintptr_t>(cur_block)); |
| 739 | MIR* new_insn = static_cast<MIR*>(NewMem(cu, sizeof(MIR), true, kAllocMIR)); |
| 740 | *new_insn = *insn; |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 741 | insn->dalvikInsn.opcode = |
| 742 | static_cast<Instruction::Code>(kMirOpCheck); |
| 743 | // Associate the two halves |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 744 | insn->meta.throw_insn = new_insn; |
| 745 | new_insn->meta.throw_insn = insn; |
| 746 | AppendMIR(new_block, new_insn); |
| 747 | return new_block; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 748 | } |
| 749 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 750 | void CompilerInit(CompilationUnit* cu, const Compiler& compiler) { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 751 | bool success = false; |
| 752 | switch (compiler.GetInstructionSet()) { |
| 753 | case kThumb2: |
| 754 | success = InitArmCodegen(cu); |
| 755 | break; |
| 756 | case kMips: |
| 757 | success = InitMipsCodegen(cu); |
| 758 | break; |
| 759 | case kX86: |
| 760 | success = InitX86Codegen(cu); |
| 761 | break; |
| 762 | default:; |
| 763 | } |
| 764 | if (!success) { |
| 765 | LOG(FATAL) << "Failed to initialize codegen for " << compiler.GetInstructionSet(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 766 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 767 | if (!HeapInit(cu)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 768 | LOG(FATAL) << "Failed to initialize oat heap"; |
| 769 | } |
| 770 | } |
| 771 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 772 | static CompiledMethod* CompileMethod(Compiler& compiler, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 773 | const CompilerBackend compiler_backend, |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 774 | const DexFile::CodeItem* code_item, |
| 775 | uint32_t access_flags, InvokeType invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 776 | uint32_t class_def_idx, uint32_t method_idx, |
| 777 | jobject class_loader, const DexFile& dex_file, |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 778 | LLVMInfo* llvm_info) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 779 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 780 | VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "..."; |
Brian Carlstrom | 94496d3 | 2011-08-22 09:22:47 -0700 | [diff] [blame] | 781 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 782 | const uint16_t* code_ptr = code_item->insns_; |
| 783 | const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_; |
| 784 | int num_blocks = 0; |
| 785 | unsigned int cur_offset = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 786 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 787 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 788 | UniquePtr<CompilationUnit> cu(new CompilationUnit); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 789 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 790 | CompilerInit(cu.get(), compiler); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 791 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 792 | cu->compiler = &compiler; |
| 793 | cu->class_linker = class_linker; |
| 794 | cu->dex_file = &dex_file; |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 795 | cu->class_def_idx = class_def_idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 796 | cu->method_idx = method_idx; |
| 797 | cu->code_item = code_item; |
| 798 | cu->access_flags = access_flags; |
| 799 | cu->invoke_type = invoke_type; |
| 800 | cu->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
| 801 | cu->instruction_set = compiler.GetInstructionSet(); |
| 802 | cu->insns = code_item->insns_; |
| 803 | cu->insns_size = code_item->insns_size_in_code_units_; |
| 804 | cu->num_ins = code_item->ins_size_; |
| 805 | cu->num_regs = code_item->registers_size_ - cu->num_ins; |
| 806 | cu->num_outs = code_item->outs_size_; |
| 807 | DCHECK((cu->instruction_set == kThumb2) || |
| 808 | (cu->instruction_set == kX86) || |
| 809 | (cu->instruction_set == kMips)); |
| 810 | if ((compiler_backend == kQuickGBC) || (compiler_backend == kPortable)) { |
| 811 | cu->gen_bitcode = true; |
buzbee | 85eee02 | 2012-07-16 22:12:38 -0700 | [diff] [blame] | 812 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 813 | DCHECK_NE(compiler_backend, kIceland); // TODO: remove when Portable/Iceland merge complete |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 814 | cu->llvm_info = llvm_info; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 815 | /* Adjust this value accordingly once inlining is performed */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 816 | cu->num_dalvik_registers = code_item->registers_size_; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 817 | // TODO: set this from command line |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 818 | cu->compiler_flip_match = false; |
| 819 | bool use_match = !cu->compiler_method_match.empty(); |
| 820 | bool match = use_match && (cu->compiler_flip_match ^ |
| 821 | (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) != |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 822 | std::string::npos)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 823 | if (!use_match || match) { |
| 824 | cu->disable_opt = kCompilerOptimizerDisableFlags; |
| 825 | cu->enable_debug = kCompilerDebugFlags; |
| 826 | cu->verbose = VLOG_IS_ON(compiler) || |
| 827 | (cu->enable_debug & (1 << kDebugVerbose)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 828 | } |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 829 | #ifndef NDEBUG |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 830 | if (cu->gen_bitcode) { |
| 831 | cu->enable_debug |= (1 << kDebugVerifyBitcode); |
buzbee | 6969d50 | 2012-06-15 16:40:31 -0700 | [diff] [blame] | 832 | } |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 833 | #endif |
buzbee | 9281f00 | 2012-10-24 12:17:24 -0700 | [diff] [blame] | 834 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 835 | if (cu->instruction_set == kMips) { |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 836 | // Disable some optimizations for mips for now |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 837 | cu->disable_opt |= ( |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 838 | (1 << kLoadStoreElimination) | |
| 839 | (1 << kLoadHoisting) | |
| 840 | (1 << kSuppressLoads) | |
| 841 | (1 << kNullCheckElimination) | |
| 842 | (1 << kPromoteRegs) | |
| 843 | (1 << kTrackLiveTemps) | |
| 844 | (1 << kSkipLargeMethodOptimization) | |
| 845 | (1 << kSafeOptimizations) | |
| 846 | (1 << kBBOpt) | |
| 847 | (1 << kMatch) | |
| 848 | (1 << kPromoteCompilerTemps)); |
| 849 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 850 | |
| 851 | /* Gathering opcode stats? */ |
| 852 | if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 853 | cu->opcode_count = |
| 854 | static_cast<int*>(NewMem(cu.get(), kNumPackedOpcodes * sizeof(int), true, kAllocMisc)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* Assume non-throwing leaf */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 858 | cu->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 859 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 860 | /* Initialize the block list, estimate size based on insns_size */ |
| 861 | CompilerInitGrowableList(cu.get(), &cu->block_list, cu->insns_size, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 862 | kListBlockList); |
| 863 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 864 | /* Initialize the switch_tables list */ |
| 865 | CompilerInitGrowableList(cu.get(), &cu->switch_tables, 4, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 866 | kListSwitchTables); |
| 867 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 868 | /* Intialize the fill_array_data list */ |
| 869 | CompilerInitGrowableList(cu.get(), &cu->fill_array_data, 4, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 870 | kListFillArrayData); |
| 871 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 872 | /* Intialize the throw_launchpads list, estimate size based on insns_size */ |
| 873 | CompilerInitGrowableList(cu.get(), &cu->throw_launchpads, cu->insns_size, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 874 | kListThrowLaunchPads); |
| 875 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 876 | /* Intialize the instrinsic_launchpads list */ |
| 877 | CompilerInitGrowableList(cu.get(), &cu->intrinsic_launchpads, 4, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 878 | kListMisc); |
| 879 | |
| 880 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 881 | /* Intialize the suspend_launchpads list */ |
| 882 | CompilerInitGrowableList(cu.get(), &cu->suspend_launchpads, 2048, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 883 | kListSuspendLaunchPads); |
| 884 | |
| 885 | /* Allocate the bit-vector to track the beginning of basic blocks */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 886 | ArenaBitVector *try_block_addr = AllocBitVector(cu.get(), |
| 887 | cu->insns_size, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 888 | true /* expandable */); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 889 | cu->try_block_addr = try_block_addr; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 890 | |
| 891 | /* Create the default entry and exit blocks and enter them to the list */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 892 | BasicBlock *entry_block = NewMemBB(cu.get(), kEntryBlock, num_blocks++); |
| 893 | BasicBlock *exit_block = NewMemBB(cu.get(), kExitBlock, num_blocks++); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 894 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 895 | cu->entry_block = entry_block; |
| 896 | cu->exit_block = exit_block; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 897 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 898 | InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(entry_block)); |
| 899 | InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(exit_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 900 | |
| 901 | /* Current block to record parsed instructions */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 902 | BasicBlock *cur_block = NewMemBB(cu.get(), kDalvikByteCode, num_blocks++); |
| 903 | cur_block->start_offset = 0; |
| 904 | InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(cur_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 905 | /* Add first block to the fast lookup cache */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 906 | cu->block_map.Put(cur_block->start_offset, cur_block); |
| 907 | entry_block->fall_through = cur_block; |
| 908 | InsertGrowableList(cu.get(), cur_block->predecessors, |
| 909 | reinterpret_cast<uintptr_t>(entry_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 910 | |
| 911 | /* |
| 912 | * Store back the number of blocks since new blocks may be created of |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 913 | * accessing cu. |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 914 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 915 | cu->num_blocks = num_blocks; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 916 | |
| 917 | /* Identify code range in try blocks and set up the empty catch blocks */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 918 | ProcessTryCatchBlocks(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 919 | |
| 920 | /* Set up for simple method detection */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 921 | int num_patterns = sizeof(special_patterns)/sizeof(special_patterns[0]); |
| 922 | bool live_pattern = (num_patterns > 0) && !(cu->disable_opt & (1 << kMatch)); |
| 923 | bool* dead_pattern = |
| 924 | static_cast<bool*>(NewMem(cu.get(), sizeof(bool) * num_patterns, true, kAllocMisc)); |
| 925 | SpecialCaseHandler special_case = kNoHandler; |
| 926 | int pattern_pos = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 927 | |
| 928 | /* Parse all instructions and put them into containing basic blocks */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 929 | while (code_ptr < code_end) { |
| 930 | MIR *insn = static_cast<MIR *>(NewMem(cu.get(), sizeof(MIR), true, kAllocMIR)); |
| 931 | insn->offset = cur_offset; |
buzbee | a169e1d | 2012-12-05 14:26:44 -0800 | [diff] [blame] | 932 | int width = ParseInsn(cu.get(), code_ptr, &insn->dalvikInsn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 933 | insn->width = width; |
| 934 | Instruction::Code opcode = insn->dalvikInsn.opcode; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 935 | if (cu->opcode_count != NULL) { |
| 936 | cu->opcode_count[static_cast<int>(opcode)]++; |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 939 | /* Terminate when the data section is seen */ |
| 940 | if (width == 0) |
| 941 | break; |
| 942 | |
| 943 | /* Possible simple method? */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 944 | if (live_pattern) { |
| 945 | live_pattern = false; |
| 946 | special_case = kNoHandler; |
| 947 | for (int i = 0; i < num_patterns; i++) { |
| 948 | if (!dead_pattern[i]) { |
| 949 | if (special_patterns[i].opcodes[pattern_pos] == opcode) { |
| 950 | live_pattern = true; |
| 951 | special_case = special_patterns[i].handler_code; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 952 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 953 | dead_pattern[i] = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 957 | pattern_pos++; |
buzbee | a7c1268 | 2012-03-19 13:13:53 -0700 | [diff] [blame] | 958 | } |
| 959 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 960 | AppendMIR(cur_block, insn); |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 961 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 962 | code_ptr += width; |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 963 | int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 964 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 965 | int df_flags = oat_data_flow_attributes[insn->dalvikInsn.opcode]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 966 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 967 | if (df_flags & DF_HAS_DEFS) { |
| 968 | cu->def_count += (df_flags & DF_A_WIDE) ? 2 : 1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 969 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 970 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 971 | if (flags & Instruction::kBranch) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 972 | cur_block = ProcessCanBranch(cu.get(), cur_block, insn, cur_offset, |
| 973 | width, flags, code_ptr, code_end); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 974 | } else if (flags & Instruction::kReturn) { |
buzbee | 4ef3e45 | 2012-12-14 13:35:28 -0800 | [diff] [blame^] | 975 | cur_block->has_return = true; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 976 | cur_block->fall_through = exit_block; |
| 977 | InsertGrowableList(cu.get(), exit_block->predecessors, |
| 978 | reinterpret_cast<uintptr_t>(cur_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 979 | /* |
| 980 | * Terminate the current block if there are instructions |
| 981 | * afterwards. |
| 982 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 983 | if (code_ptr < code_end) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 984 | /* |
| 985 | * Create a fallthrough block for real instructions |
| 986 | * (incl. NOP). |
| 987 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 988 | if (ContentIsInsn(code_ptr)) { |
| 989 | FindBlock(cu.get(), cur_offset + width, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 990 | /* split */ |
| 991 | false, |
| 992 | /* create */ |
| 993 | true, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 994 | /* immed_pred_block_p */ |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 995 | NULL); |
| 996 | } |
| 997 | } |
| 998 | } else if (flags & Instruction::kThrow) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 999 | cur_block = ProcessCanThrow(cu.get(), cur_block, insn, cur_offset, |
| 1000 | width, flags, try_block_addr, code_ptr, code_end); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1001 | } else if (flags & Instruction::kSwitch) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1002 | ProcessCanSwitch(cu.get(), cur_block, insn, cur_offset, width, flags); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1003 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1004 | cur_offset += width; |
| 1005 | BasicBlock *next_block = FindBlock(cu.get(), cur_offset, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1006 | /* split */ |
| 1007 | false, |
| 1008 | /* create */ |
| 1009 | false, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1010 | /* immed_pred_block_p */ |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1011 | NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1012 | if (next_block) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * The next instruction could be the target of a previously parsed |
| 1015 | * forward branch so a block is already created. If the current |
| 1016 | * instruction is not an unconditional branch, connect them through |
| 1017 | * the fall-through link. |
| 1018 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1019 | DCHECK(cur_block->fall_through == NULL || |
| 1020 | cur_block->fall_through == next_block || |
| 1021 | cur_block->fall_through == exit_block); |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 1022 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1023 | if ((cur_block->fall_through == NULL) && (flags & Instruction::kContinue)) { |
| 1024 | cur_block->fall_through = next_block; |
| 1025 | InsertGrowableList(cu.get(), next_block->predecessors, |
| 1026 | reinterpret_cast<uintptr_t>(cur_block)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1027 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1028 | cur_block = next_block; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
buzbee | fc9e6fa | 2012-03-23 15:14:29 -0700 | [diff] [blame] | 1031 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 1032 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1033 | DumpCFG(cu.get(), "/sdcard/1_post_parse_cfg/", true); |
| 1034 | } |
| 1035 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1036 | if (!(cu->disable_opt & (1 << kSkipLargeMethodOptimization))) { |
| 1037 | if ((cu->num_blocks > MANY_BLOCKS) || |
| 1038 | ((cu->num_blocks > MANY_BLOCKS_INITIALIZER) && |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1039 | PrettyMethod(method_idx, dex_file, false).find("init>") != |
| 1040 | std::string::npos)) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1041 | cu->qd_mode = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
buzbee | fc9e6fa | 2012-03-23 15:14:29 -0700 | [diff] [blame] | 1044 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1045 | if (cu->qd_mode) { |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1046 | // Bitcode generation requires full dataflow analysis |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1047 | cu->disable_dataflow = !cu->gen_bitcode; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1048 | // Disable optimization which require dataflow/ssa |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1049 | cu->disable_opt |= (1 << kBBOpt) | (1 << kPromoteRegs) | (1 << kNullCheckElimination); |
| 1050 | if (cu->verbose) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1051 | LOG(INFO) << "QD mode enabled: " |
| 1052 | << PrettyMethod(method_idx, dex_file) |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1053 | << " num blocks: " << cu->num_blocks; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1054 | } |
| 1055 | } |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 1056 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1057 | if (cu->verbose) { |
| 1058 | DumpCompilationUnit(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1059 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1060 | |
buzbee | 0967a25 | 2012-09-14 10:43:54 -0700 | [diff] [blame] | 1061 | /* Do a code layout pass */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1062 | CodeLayout(cu.get()); |
buzbee | 0967a25 | 2012-09-14 10:43:54 -0700 | [diff] [blame] | 1063 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 1064 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1065 | DumpCFG(cu.get(), "/sdcard/2_post_layout_cfg/", true); |
| 1066 | } |
| 1067 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1068 | if (cu->enable_debug & (1 << kDebugVerifyDataflow)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1069 | /* Verify if all blocks are connected as claimed */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1070 | DataFlowAnalysisDispatcher(cu.get(), VerifyPredInfo, kAllNodes, |
| 1071 | false /* is_iterative */); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1072 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1073 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1074 | /* Perform SSA transformation for the whole method */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1075 | SSATransformation(cu.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1076 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 1077 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1078 | DumpCFG(cu.get(), "/sdcard/3_post_ssa_cfg/", false); |
| 1079 | } |
| 1080 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1081 | /* Do constant propagation */ |
buzbee | 4ef3e45 | 2012-12-14 13:35:28 -0800 | [diff] [blame^] | 1082 | cu->is_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs, false /* not expandable */); |
| 1083 | cu->must_flush_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs, |
| 1084 | false /* not expandable */); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1085 | cu->constant_values = |
| 1086 | static_cast<int*>(NewMem(cu.get(), sizeof(int) * cu->num_ssa_regs, true, kAllocDFInfo)); |
| 1087 | DataFlowAnalysisDispatcher(cu.get(), DoConstantPropogation, |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1088 | kAllNodes, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1089 | false /* is_iterative */); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1090 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1091 | /* Detect loops */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1092 | LoopDetection(cu.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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1095 | MethodUseCount(cu.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 | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1098 | NullCheckElimination(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1099 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 1100 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1101 | DumpCFG(cu.get(), "/sdcard/4_post_nce_cfg/", false); |
| 1102 | } |
| 1103 | |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1104 | /* Combine basic blocks where possible */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1105 | BasicBlockCombine(cu.get()); |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1106 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 1107 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1108 | DumpCFG(cu.get(), "/sdcard/5_post_bbcombine_cfg/", false); |
| 1109 | } |
| 1110 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1111 | /* Do some basic block optimizations */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1112 | BasicBlockOptimization(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1113 | |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 1114 | // Debugging only |
| 1115 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1116 | DumpCFG(cu.get(), "/sdcard/6_post_bbo_cfg/", false); |
| 1117 | } |
| 1118 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1119 | if (cu->enable_debug & (1 << kDebugDumpCheckStats)) { |
| 1120 | DumpCheckStats(cu.get()); |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 1123 | cu.get()->cg->CompilerInitializeRegAlloc(cu.get()); // Needs to happen after SSA naming |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1124 | |
| 1125 | /* Allocate Registers using simple local allocation scheme */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1126 | SimpleRegAlloc(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1127 | |
buzbee | 2502e00 | 2012-12-31 16:05:53 -0800 | [diff] [blame] | 1128 | if (cu->enable_debug & (1 << kDebugDumpCFG)) { |
| 1129 | DumpCFG(cu.get(), "/sdcard/7_post_ralloc_cfg/", true); |
| 1130 | } |
| 1131 | |
| 1132 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1133 | /* Go the LLVM path? */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1134 | if (cu->gen_bitcode) { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1135 | // MIR->Bitcode |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1136 | MethodMIR2Bitcode(cu.get()); |
| 1137 | if (compiler_backend == kPortable) { |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 1138 | // all done |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1139 | ArenaReset(cu.get()); |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 1140 | return NULL; |
| 1141 | } |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1142 | // Bitcode->LIR |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1143 | MethodBitcode2LIR(cu.get()); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1144 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1145 | if (special_case != kNoHandler) { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1146 | /* |
| 1147 | * Custom codegen for special cases. If for any reason the |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1148 | * special codegen doesn't succeed, cu->first_lir_insn will |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1149 | * set to NULL; |
| 1150 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1151 | SpecialMIR2LIR(cu.get(), special_case); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1152 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1153 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1154 | /* Convert MIR to LIR, etc. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1155 | if (cu->first_lir_insn == NULL) { |
| 1156 | MethodMIR2LIR(cu.get()); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 1157 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1158 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1159 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1160 | /* Method is not empty */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1161 | if (cu->first_lir_insn) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1162 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1163 | // mark the targets of switch statement case labels |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1164 | ProcessSwitchTables(cu.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1165 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1166 | /* Convert LIR into machine code. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1167 | AssembleLIR(cu.get()); |
buzbee | 99ba964 | 2012-01-25 14:23:14 -0800 | [diff] [blame] | 1168 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1169 | if (cu->verbose) { |
| 1170 | CodegenDump(cu.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1173 | if (cu->opcode_count != NULL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1174 | LOG(INFO) << "Opcode Count"; |
| 1175 | for (int i = 0; i < kNumPackedOpcodes; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1176 | if (cu->opcode_count[i] != 0) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1177 | LOG(INFO) << "-C- " |
| 1178 | << Instruction::Name(static_cast<Instruction::Code>(i)) |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1179 | << " " << cu->opcode_count[i]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1180 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | } |
buzbee | a7c1268 | 2012-03-19 13:13:53 -0700 | [diff] [blame] | 1184 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1185 | // Combine vmap tables - core regs, then fp regs - into vmap_table |
| 1186 | std::vector<uint16_t> vmap_table; |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 1187 | // Core regs may have been inserted out of order - sort first |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1188 | std::sort(cu->core_vmap_table.begin(), cu->core_vmap_table.end()); |
| 1189 | for (size_t i = 0 ; i < cu->core_vmap_table.size(); i++) { |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 1190 | // Copy, stripping out the phys register sort key |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1191 | vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & cu->core_vmap_table[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1192 | } |
| 1193 | // If we have a frame, push a marker to take place of lr |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1194 | if (cu->frame_size > 0) { |
| 1195 | vmap_table.push_back(INVALID_VREG); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1196 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1197 | DCHECK_EQ(__builtin_popcount(cu->core_spill_mask), 0); |
| 1198 | DCHECK_EQ(__builtin_popcount(cu->fp_spill_mask), 0); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1199 | } |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 1200 | // Combine vmap tables - core regs, then fp regs. fp regs already sorted |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1201 | for (uint32_t i = 0; i < cu->fp_vmap_table.size(); i++) { |
| 1202 | vmap_table.push_back(cu->fp_vmap_table[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1203 | } |
| 1204 | CompiledMethod* result = |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1205 | new CompiledMethod(cu->instruction_set, cu->code_buffer, |
| 1206 | cu->frame_size, cu->core_spill_mask, cu->fp_spill_mask, |
| 1207 | cu->combined_mapping_table, vmap_table, cu->native_gc_map); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1208 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1209 | VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file) |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1210 | << " (" << (cu->code_buffer.size() * sizeof(cu->code_buffer[0])) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1211 | << " bytes)"; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 1212 | |
| 1213 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1214 | if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) { |
| 1215 | DumpMemStats(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1216 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 1217 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1218 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1219 | ArenaReset(cu.get()); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 1220 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1221 | return result; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 1224 | CompiledMethod* CompileOneMethod(Compiler& compiler, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1225 | const CompilerBackend backend, |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 1226 | const DexFile::CodeItem* code_item, |
| 1227 | uint32_t access_flags, InvokeType invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 1228 | uint32_t class_def_idx, uint32_t method_idx, jobject class_loader, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1229 | const DexFile& dex_file, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1230 | LLVMInfo* llvm_info) |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 1231 | { |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 1232 | return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx, |
| 1233 | method_idx, class_loader, dex_file, llvm_info); |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 1236 | } // namespace art |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1237 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1238 | extern "C" art::CompiledMethod* |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1239 | ArtQuickCompileMethod(art::Compiler& compiler, |
| 1240 | const art::DexFile::CodeItem* code_item, |
| 1241 | uint32_t access_flags, art::InvokeType invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 1242 | uint32_t class_def_idx, uint32_t method_idx, jobject class_loader, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1243 | const art::DexFile& dex_file) |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1244 | { |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 1245 | // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default |
| 1246 | art::CompilerBackend backend = compiler.GetCompilerBackend(); |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 1247 | return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 1248 | class_def_idx, method_idx, class_loader, dex_file, |
| 1249 | NULL /* use thread llvm_info */); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1250 | } |