blob: 5b8faf455170626f1c5704b9e7173a70c4c7b310 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
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
Brian Carlstrom265091e2013-01-30 14:08:26 -080017#include <llvm/Support/Threading.h>
18
Ian Rogers1212a022013-03-04 10:48:41 -080019#include "compiler/driver/compiler_driver.h"
buzbeeefc63692012-11-14 16:31:52 -080020#include "compiler_internals.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080021#if defined(ART_USE_PORTABLE_COMPILER)
22#include "compiler/llvm/llvm_compilation_unit.h"
23#endif
buzbeeefc63692012-11-14 16:31:52 -080024#include "dataflow.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080025#include "ssa_transformation.h"
Ian Rogers0571d352011-11-03 19:51:38 -070026#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "runtime.h"
buzbee395116c2013-02-27 14:30:25 -080029#include "quick/codegen_util.h"
30#include "portable/mir_to_gbc.h"
31#include "quick/mir_to_lir.h"
buzbee67bf8852011-08-17 17:51:35 -070032
buzbee4df2bbd2012-10-11 14:46:06 -070033namespace {
Ian Rogersc928de92013-02-27 14:30:44 -080034#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070035 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
Shih-wei Liao215a9262012-10-12 10:29:46 -070036#endif
buzbee4df2bbd2012-10-11 14:46:06 -070037 void InitializeLLVMForQuick() {
Ian Rogers4c1c2832013-03-04 18:30:13 -080038 ::llvm::llvm_start_multithreaded();
buzbee4df2bbd2012-10-11 14:46:06 -070039 }
40}
buzbee4df2bbd2012-10-11 14:46:06 -070041
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080042namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080043namespace llvm {
44::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Ian Rogers76ae4fe2013-02-27 16:03:41 -080045}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080046
buzbee4df2bbd2012-10-11 14:46:06 -070047LLVMInfo::LLVMInfo() {
Ian Rogersc928de92013-02-27 14:30:44 -080048#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070049 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
50#endif
buzbee692be802012-08-29 15:52:59 -070051 // Create context, module, intrinsic helper & ir builder
Ian Rogers4c1c2832013-03-04 18:30:13 -080052 llvm_context_.reset(new ::llvm::LLVMContext());
53 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
54 ::llvm::StructType::create(*llvm_context_, "JavaObject");
55 art::llvm::makeLLVMModuleContents(llvm_module_);
56 intrinsic_helper_.reset( new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
57 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
buzbee692be802012-08-29 15:52:59 -070058}
59
buzbee4df2bbd2012-10-11 14:46:06 -070060LLVMInfo::~LLVMInfo() {
buzbee692be802012-08-29 15:52:59 -070061}
62
Ian Rogers1212a022013-03-04 10:48:41 -080063extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee692be802012-08-29 15:52:59 -070064 CHECK(compiler.GetCompilerContext() == NULL);
buzbeefa57c472012-11-21 12:06:18 -080065 LLVMInfo* llvm_info = new LLVMInfo();
66 compiler.SetCompilerContext(llvm_info);
buzbee692be802012-08-29 15:52:59 -070067}
68
Ian Rogers1212a022013-03-04 10:48:41 -080069extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee4df2bbd2012-10-11 14:46:06 -070070 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
buzbee692be802012-08-29 15:52:59 -070071 compiler.SetCompilerContext(NULL);
72}
buzbee692be802012-08-29 15:52:59 -070073
buzbeece302932011-10-04 14:32:18 -070074/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070075static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee4ef3e452012-12-14 13:35:28 -080076 (1 << kLoadStoreElimination) |
Bill Buzbeea114add2012-05-03 15:00:40 -070077 //(1 << kLoadHoisting) |
78 //(1 << kSuppressLoads) |
79 //(1 << kNullCheckElimination) |
80 //(1 << kPromoteRegs) |
81 //(1 << kTrackLiveTemps) |
buzbee4ef3e452012-12-14 13:35:28 -080082 (1 << kSkipLargeMethodOptimization) |
Bill Buzbeea114add2012-05-03 15:00:40 -070083 //(1 << kSafeOptimizations) |
84 //(1 << kBBOpt) |
85 //(1 << kMatch) |
86 //(1 << kPromoteCompilerTemps) |
87 0;
buzbeece302932011-10-04 14:32:18 -070088
Elliott Hughese52e49b2012-04-02 16:05:44 -070089static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Bill Buzbeea114add2012-05-03 15:00:40 -070090 //(1 << kDebugDisplayMissingTargets) |
91 //(1 << kDebugVerbose) |
92 //(1 << kDebugDumpCFG) |
93 //(1 << kDebugSlowFieldPath) |
94 //(1 << kDebugSlowInvokePath) |
95 //(1 << kDebugSlowStringPath) |
96 //(1 << kDebugSlowestFieldPath) |
97 //(1 << kDebugSlowestStringPath) |
98 //(1 << kDebugExerciseResolveMethod) |
99 //(1 << kDebugVerifyDataflow) |
100 //(1 << kDebugShowMemoryUsage) |
101 //(1 << kDebugShowNops) |
102 //(1 << kDebugCountOpcodes) |
buzbeed1643e42012-09-05 14:06:51 -0700103 //(1 << kDebugDumpCheckStats) |
buzbeead8f15e2012-06-18 14:49:45 -0700104 //(1 << kDebugDumpBitcodeFile) |
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700105 //(1 << kDebugVerifyBitcode) |
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 0;
buzbeece302932011-10-04 14:32:18 -0700107
buzbeefa57c472012-11-21 12:06:18 -0800108static bool ContentIsInsn(const uint16_t* code_ptr) {
109 uint16_t instr = *code_ptr;
buzbeecbd6d442012-11-17 14:11:25 -0800110 Instruction::Code opcode = static_cast<Instruction::Code>(instr & 0xff);
buzbee67bf8852011-08-17 17:51:35 -0700111
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 /*
113 * Since the low 8-bit in metadata may look like NOP, we need to check
114 * both the low and whole sub-word to determine whether it is code or data.
115 */
116 return (opcode != Instruction::NOP || instr == 0);
buzbee67bf8852011-08-17 17:51:35 -0700117}
118
119/*
120 * Parse an instruction, return the length of the instruction
121 */
buzbeefa57c472012-11-21 12:06:18 -0800122static int ParseInsn(CompilationUnit* cu, const uint16_t* code_ptr,
buzbeea169e1d2012-12-05 14:26:44 -0800123 DecodedInstruction* decoded_instruction)
buzbee67bf8852011-08-17 17:51:35 -0700124{
Elliott Hughesadb8c672012-03-06 16:49:32 -0800125 // Don't parse instruction data
buzbeefa57c472012-11-21 12:06:18 -0800126 if (!ContentIsInsn(code_ptr)) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800127 return 0;
128 }
buzbee67bf8852011-08-17 17:51:35 -0700129
buzbeefa57c472012-11-21 12:06:18 -0800130 const Instruction* instruction = Instruction::At(code_ptr);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800131 *decoded_instruction = DecodedInstruction(instruction);
buzbee67bf8852011-08-17 17:51:35 -0700132
Elliott Hughesadb8c672012-03-06 16:49:32 -0800133 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -0700134}
135
136#define UNKNOWN_TARGET 0xffffffff
137
buzbee67bf8852011-08-17 17:51:35 -0700138/* Split an existing block from the specified code offset into two */
buzbeefa57c472012-11-21 12:06:18 -0800139static BasicBlock *SplitBlock(CompilationUnit* cu, unsigned int code_offset,
140 BasicBlock* orig_block, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700141{
buzbeefa57c472012-11-21 12:06:18 -0800142 MIR* insn = orig_block->first_mir_insn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 while (insn) {
buzbeefa57c472012-11-21 12:06:18 -0800144 if (insn->offset == code_offset) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 insn = insn->next;
146 }
147 if (insn == NULL) {
148 LOG(FATAL) << "Break split failed";
149 }
buzbeefa57c472012-11-21 12:06:18 -0800150 BasicBlock *bottom_block = NewMemBB(cu, kDalvikByteCode,
151 cu->num_blocks++);
152 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700153
buzbeefa57c472012-11-21 12:06:18 -0800154 bottom_block->start_offset = code_offset;
155 bottom_block->first_mir_insn = insn;
156 bottom_block->last_mir_insn = orig_block->last_mir_insn;
buzbee67bf8852011-08-17 17:51:35 -0700157
buzbeebbdd0532013-02-07 09:33:02 -0800158 /* If this block was terminated by a return, the flag needs to go with the bottom block */
159 bottom_block->terminated_by_return = orig_block->terminated_by_return;
160 orig_block->terminated_by_return = false;
161
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 /* Add it to the quick lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800163 cu->block_map.Put(bottom_block->start_offset, bottom_block);
buzbee5b537102012-01-17 17:33:47 -0800164
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 /* Handle the taken path */
buzbeefa57c472012-11-21 12:06:18 -0800166 bottom_block->taken = orig_block->taken;
167 if (bottom_block->taken) {
168 orig_block->taken = NULL;
169 DeleteGrowableList(bottom_block->taken->predecessors, reinterpret_cast<uintptr_t>(orig_block));
170 InsertGrowableList(cu, bottom_block->taken->predecessors,
171 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 }
173
174 /* Handle the fallthrough path */
buzbeefa57c472012-11-21 12:06:18 -0800175 bottom_block->fall_through = orig_block->fall_through;
176 orig_block->fall_through = bottom_block;
177 InsertGrowableList(cu, bottom_block->predecessors,
178 reinterpret_cast<uintptr_t>(orig_block));
179 if (bottom_block->fall_through) {
180 DeleteGrowableList(bottom_block->fall_through->predecessors,
181 reinterpret_cast<uintptr_t>(orig_block));
182 InsertGrowableList(cu, bottom_block->fall_through->predecessors,
183 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 }
185
186 /* Handle the successor list */
buzbeefa57c472012-11-21 12:06:18 -0800187 if (orig_block->successor_block_list.block_list_type != kNotUsed) {
188 bottom_block->successor_block_list = orig_block->successor_block_list;
189 orig_block->successor_block_list.block_list_type = kNotUsed;
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 GrowableListIterator iterator;
191
buzbeefa57c472012-11-21 12:06:18 -0800192 GrowableListIteratorInit(&bottom_block->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 &iterator);
194 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800195 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800196 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800197 if (successor_block_info == NULL) break;
198 BasicBlock *bb = successor_block_info->block;
199 DeleteGrowableList(bb->predecessors, reinterpret_cast<uintptr_t>(orig_block));
200 InsertGrowableList(cu, bb->predecessors, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700201 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 }
buzbee67bf8852011-08-17 17:51:35 -0700203
buzbeefa57c472012-11-21 12:06:18 -0800204 orig_block->last_mir_insn = insn->prev;
buzbee67bf8852011-08-17 17:51:35 -0700205
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 insn->prev->next = NULL;
207 insn->prev = NULL;
208 /*
209 * Update the immediate predecessor block pointer so that outgoing edges
210 * can be applied to the proper block.
211 */
buzbeefa57c472012-11-21 12:06:18 -0800212 if (immed_pred_block_p) {
213 DCHECK_EQ(*immed_pred_block_p, orig_block);
214 *immed_pred_block_p = bottom_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 }
buzbeefa57c472012-11-21 12:06:18 -0800216 return bottom_block;
buzbee67bf8852011-08-17 17:51:35 -0700217}
218
219/*
220 * Given a code offset, find out the block that starts with it. If the offset
buzbeefa57c472012-11-21 12:06:18 -0800221 * is in the middle of an existing block, split it into two. If immed_pred_block_p
222 * is not non-null and is the block being split, update *immed_pred_block_p to
buzbee9ab05de2012-01-18 15:43:48 -0800223 * point to the bottom block so that outgoing edges can be set up properly
224 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800225 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700226 */
buzbeefa57c472012-11-21 12:06:18 -0800227BasicBlock *FindBlock(CompilationUnit* cu, unsigned int code_offset,
228 bool split, bool create, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700229{
buzbeefa57c472012-11-21 12:06:18 -0800230 GrowableList* block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 BasicBlock* bb;
232 unsigned int i;
233 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700234
buzbeefa57c472012-11-21 12:06:18 -0800235 it = cu->block_map.find(code_offset);
236 if (it != cu->block_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 return it->second;
238 } else if (!create) {
239 return NULL;
240 }
241
242 if (split) {
buzbeefa57c472012-11-21 12:06:18 -0800243 for (i = 0; i < block_list->num_used; i++) {
244 bb = reinterpret_cast<BasicBlock*>(block_list->elem_list[i]);
245 if (bb->block_type != kDalvikByteCode) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 /* Check if a branch jumps into the middle of an existing block */
buzbeefa57c472012-11-21 12:06:18 -0800247 if ((code_offset > bb->start_offset) && (bb->last_mir_insn != NULL) &&
248 (code_offset <= bb->last_mir_insn->offset)) {
249 BasicBlock *new_bb = SplitBlock(cu, code_offset, bb,
250 bb == *immed_pred_block_p ?
251 immed_pred_block_p : NULL);
252 return new_bb;
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 }
buzbee5b537102012-01-17 17:33:47 -0800254 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 }
buzbee5b537102012-01-17 17:33:47 -0800256
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 /* Create a new one */
buzbeefa57c472012-11-21 12:06:18 -0800258 bb = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
259 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bb));
260 bb->start_offset = code_offset;
261 cu->block_map.Put(bb->start_offset, bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700263}
264
buzbeef58c12c2012-07-03 15:06:29 -0700265/* Find existing block */
buzbeefa57c472012-11-21 12:06:18 -0800266BasicBlock* FindBlock(CompilationUnit* cu, unsigned int code_offset)
buzbeef58c12c2012-07-03 15:06:29 -0700267{
buzbeefa57c472012-11-21 12:06:18 -0800268 return FindBlock(cu, code_offset, false, false, NULL);
buzbeef58c12c2012-07-03 15:06:29 -0700269}
270
buzbeead8f15e2012-06-18 14:49:45 -0700271/* Turn method name into a legal Linux file name */
buzbee52a77fc2012-11-20 19:50:46 -0800272void ReplaceSpecialChars(std::string& str)
buzbeead8f15e2012-06-18 14:49:45 -0700273{
274 static const struct { const char before; const char after; } match[] =
275 {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'},
276 {'(','@'}, {')','@'}, {'<','='}, {'>','='}};
277 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
278 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
279 }
280}
281
buzbee67bf8852011-08-17 17:51:35 -0700282/* Dump the CFG into a DOT graph */
buzbeed8506212012-12-20 14:15:05 -0800283void DumpCFG(CompilationUnit* cu, const char* dir_prefix, bool all_blocks)
buzbee67bf8852011-08-17 17:51:35 -0700284{
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 FILE* file;
buzbeefa57c472012-11-21 12:06:18 -0800286 std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -0800287 ReplaceSpecialChars(fname);
buzbeefa57c472012-11-21 12:06:18 -0800288 fname = StringPrintf("%s%s%x.dot", dir_prefix, fname.c_str(),
289 cu->entry_block->fall_through->start_offset);
buzbeead8f15e2012-06-18 14:49:45 -0700290 file = fopen(fname.c_str(), "w");
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 if (file == NULL) {
292 return;
293 }
294 fprintf(file, "digraph G {\n");
295
296 fprintf(file, " rankdir=TB\n");
297
buzbeed8506212012-12-20 14:15:05 -0800298 int num_blocks = all_blocks ? cu->num_blocks : cu->num_reachable_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700299 int idx;
buzbeefa57c472012-11-21 12:06:18 -0800300 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700301
buzbeed8506212012-12-20 14:15:05 -0800302 for (idx = 0; idx < num_blocks; idx++) {
303 int block_idx = all_blocks ? idx : cu->dfs_order.elem_list[idx];
buzbeefa57c472012-11-21 12:06:18 -0800304 BasicBlock *bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, block_idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -0800306 if (bb->block_type == kDead) continue;
307 if (bb->block_type == kEntryBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700308 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800309 } else if (bb->block_type == kExitBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700310 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800311 } else if (bb->block_type == kDalvikByteCode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700312 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800313 bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 const MIR *mir;
315 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
buzbeefa57c472012-11-21 12:06:18 -0800316 bb->first_mir_insn ? " | " : " ");
317 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
buzbeed8506212012-12-20 14:15:05 -0800318 int opcode = mir->dalvikInsn.opcode;
319 fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset,
buzbeea169e1d2012-12-05 14:26:44 -0800320 mir->ssa_rep ? GetDalvikDisassembly(cu, mir) :
buzbeed8506212012-12-20 14:15:05 -0800321 (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) :
322 extended_mir_op_names[opcode - kMirOpFirst],
323 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
324 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 mir->next ? " | " : " ");
326 }
327 fprintf(file, " }\"];\n\n");
buzbeefa57c472012-11-21 12:06:18 -0800328 } else if (bb->block_type == kExceptionHandling) {
329 char block_name[BLOCK_NAME_LEN];
Bill Buzbeea114add2012-05-03 15:00:40 -0700330
buzbeefa57c472012-11-21 12:06:18 -0800331 GetBlockName(bb, block_name);
332 fprintf(file, " %s [shape=invhouse];\n", block_name);
buzbee67bf8852011-08-17 17:51:35 -0700333 }
buzbee67bf8852011-08-17 17:51:35 -0700334
buzbeefa57c472012-11-21 12:06:18 -0800335 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
buzbee67bf8852011-08-17 17:51:35 -0700336
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 if (bb->taken) {
buzbeefa57c472012-11-21 12:06:18 -0800338 GetBlockName(bb, block_name1);
339 GetBlockName(bb->taken, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700340 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
buzbeefa57c472012-11-21 12:06:18 -0800341 block_name1, block_name2);
buzbee67bf8852011-08-17 17:51:35 -0700342 }
buzbeefa57c472012-11-21 12:06:18 -0800343 if (bb->fall_through) {
344 GetBlockName(bb, block_name1);
345 GetBlockName(bb->fall_through, block_name2);
346 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700347 }
348
buzbeefa57c472012-11-21 12:06:18 -0800349 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700350 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800351 bb->start_offset, bb->id,
352 (bb->successor_block_list.block_list_type == kCatch) ?
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 "Mrecord" : "record");
354 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800355 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700356 &iterator);
buzbeefa57c472012-11-21 12:06:18 -0800357 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800358 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700359
buzbeefa57c472012-11-21 12:06:18 -0800360 int succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800362 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700363
buzbeefa57c472012-11-21 12:06:18 -0800364 BasicBlock *dest_block = successor_block_info->block;
365 SuccessorBlockInfo *next_successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800366 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700367
368 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
buzbeefa57c472012-11-21 12:06:18 -0800369 succ_id++,
370 successor_block_info->key,
371 dest_block->start_offset,
372 (next_successor_block_info != NULL) ? " | " : " ");
Bill Buzbeea114add2012-05-03 15:00:40 -0700373
buzbeefa57c472012-11-21 12:06:18 -0800374 successor_block_info = next_successor_block_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 }
376 fprintf(file, " }\"];\n\n");
377
buzbeefa57c472012-11-21 12:06:18 -0800378 GetBlockName(bb, block_name1);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700379 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
buzbeefa57c472012-11-21 12:06:18 -0800380 block_name1, bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700381
buzbeefa57c472012-11-21 12:06:18 -0800382 if (bb->successor_block_list.block_list_type == kPackedSwitch ||
383 bb->successor_block_list.block_list_type == kSparseSwitch) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700384
buzbeefa57c472012-11-21 12:06:18 -0800385 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 &iterator);
387
buzbeefa57c472012-11-21 12:06:18 -0800388 succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800390 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800391 reinterpret_cast<SuccessorBlockInfo*>( GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800392 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700393
buzbeefa57c472012-11-21 12:06:18 -0800394 BasicBlock *dest_block = successor_block_info->block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700395
buzbeefa57c472012-11-21 12:06:18 -0800396 GetBlockName(dest_block, block_name2);
397 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
398 bb->id, succ_id++, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700399 }
400 }
401 }
402 fprintf(file, "\n");
403
buzbeed8506212012-12-20 14:15:05 -0800404 if (cu->verbose) {
405 /* Display the dominator tree */
406 GetBlockName(bb, block_name1);
407 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
408 block_name1, block_name1);
409 if (bb->i_dom) {
410 GetBlockName(bb->i_dom, block_name2);
411 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
412 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 }
414 }
415 fprintf(file, "}\n");
416 fclose(file);
buzbee67bf8852011-08-17 17:51:35 -0700417}
418
419/* Verify if all the successor is connected with all the claimed predecessors */
buzbeefa57c472012-11-21 12:06:18 -0800420static bool VerifyPredInfo(CompilationUnit* cu, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700421{
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700423
buzbee52a77fc2012-11-20 19:50:46 -0800424 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800426 BasicBlock *pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
427 if (!pred_bb) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800429 if (pred_bb->taken == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800431 } else if (pred_bb->fall_through == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800433 } else if (pred_bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800435 GrowableListIteratorInit(&pred_bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 &iterator);
437 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800438 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800439 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800440 if (successor_block_info == NULL) break;
441 BasicBlock *succ_bb = successor_block_info->block;
442 if (succ_bb == bb) {
buzbee67bf8852011-08-17 17:51:35 -0700443 found = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 break;
buzbee67bf8852011-08-17 17:51:35 -0700445 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700446 }
buzbee67bf8852011-08-17 17:51:35 -0700447 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 if (found == false) {
buzbeefa57c472012-11-21 12:06:18 -0800449 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
450 GetBlockName(bb, block_name1);
451 GetBlockName(pred_bb, block_name2);
buzbeed8506212012-12-20 14:15:05 -0800452 DumpCFG(cu, "/sdcard/cfg/", false);
buzbeefa57c472012-11-21 12:06:18 -0800453 LOG(FATAL) << "Successor " << block_name1 << "not found from "
454 << block_name2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 }
456 }
457 return true;
buzbee67bf8852011-08-17 17:51:35 -0700458}
459
460/* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800461static void ProcessTryCatchBlocks(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700462{
buzbeefa57c472012-11-21 12:06:18 -0800463 const DexFile::CodeItem* code_item = cu->code_item;
464 int tries_size = code_item->tries_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700465 int offset;
buzbee67bf8852011-08-17 17:51:35 -0700466
buzbeefa57c472012-11-21 12:06:18 -0800467 if (tries_size == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 return;
469 }
470
buzbeefa57c472012-11-21 12:06:18 -0800471 ArenaBitVector* try_block_addr = cu->try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700472
buzbeefa57c472012-11-21 12:06:18 -0800473 for (int i = 0; i < tries_size; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 const DexFile::TryItem* pTry =
475 DexFile::GetTryItems(*code_item, i);
buzbeefa57c472012-11-21 12:06:18 -0800476 int start_offset = pTry->start_addr_;
477 int end_offset = start_offset + pTry->insn_count_;
478 for (offset = start_offset; offset < end_offset; offset++) {
479 SetBit(cu, try_block_addr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700480 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 }
buzbee67bf8852011-08-17 17:51:35 -0700482
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 // Iterate over each of the handlers to enqueue the empty Catch blocks
484 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
485 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
486 for (uint32_t idx = 0; idx < handlers_size; idx++) {
487 CatchHandlerIterator iterator(handlers_ptr);
488 for (; iterator.HasNext(); iterator.Next()) {
489 uint32_t address = iterator.GetHandlerAddress();
buzbeefa57c472012-11-21 12:06:18 -0800490 FindBlock(cu, address, false /* split */, true /*create*/,
491 /* immed_pred_block_p */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700492 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 handlers_ptr = iterator.EndDataPointer();
494 }
buzbee67bf8852011-08-17 17:51:35 -0700495}
496
Elliott Hughesadb8c672012-03-06 16:49:32 -0800497/* Process instructions with the kBranch flag */
buzbeefa57c472012-11-21 12:06:18 -0800498static BasicBlock* ProcessCanBranch(CompilationUnit* cu, BasicBlock* cur_block,
499 MIR* insn, int cur_offset, int width, int flags,
500 const uint16_t* code_ptr, const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700501{
buzbeefa57c472012-11-21 12:06:18 -0800502 int target = cur_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 switch (insn->dalvikInsn.opcode) {
504 case Instruction::GOTO:
505 case Instruction::GOTO_16:
506 case Instruction::GOTO_32:
buzbeecbd6d442012-11-17 14:11:25 -0800507 target += insn->dalvikInsn.vA;
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 break;
509 case Instruction::IF_EQ:
510 case Instruction::IF_NE:
511 case Instruction::IF_LT:
512 case Instruction::IF_GE:
513 case Instruction::IF_GT:
514 case Instruction::IF_LE:
buzbeefa57c472012-11-21 12:06:18 -0800515 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800516 target += insn->dalvikInsn.vC;
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 break;
518 case Instruction::IF_EQZ:
519 case Instruction::IF_NEZ:
520 case Instruction::IF_LTZ:
521 case Instruction::IF_GEZ:
522 case Instruction::IF_GTZ:
523 case Instruction::IF_LEZ:
buzbeefa57c472012-11-21 12:06:18 -0800524 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800525 target += insn->dalvikInsn.vB;
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 break;
527 default:
buzbeecbd6d442012-11-17 14:11:25 -0800528 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 }
buzbeefa57c472012-11-21 12:06:18 -0800530 BasicBlock *taken_block = FindBlock(cu, target,
Bill Buzbeea114add2012-05-03 15:00:40 -0700531 /* split */
532 true,
533 /* create */
534 true,
buzbeefa57c472012-11-21 12:06:18 -0800535 /* immed_pred_block_p */
536 &cur_block);
537 cur_block->taken = taken_block;
538 InsertGrowableList(cu, taken_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700539
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 /* Always terminate the current block for conditional branches */
541 if (flags & Instruction::kContinue) {
buzbeefa57c472012-11-21 12:06:18 -0800542 BasicBlock *fallthrough_block = FindBlock(cu,
543 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 /*
545 * If the method is processed
546 * in sequential order from the
547 * beginning, we don't need to
548 * specify split for continue
549 * blocks. However, this
550 * routine can be called by
551 * compileLoop, which starts
552 * parsing the method from an
553 * arbitrary address in the
554 * method body.
555 */
556 true,
557 /* create */
558 true,
buzbeefa57c472012-11-21 12:06:18 -0800559 /* immed_pred_block_p */
560 &cur_block);
561 cur_block->fall_through = fallthrough_block;
562 InsertGrowableList(cu, fallthrough_block->predecessors,
563 reinterpret_cast<uintptr_t>(cur_block));
564 } else if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbeefa57c472012-11-21 12:06:18 -0800566 if (ContentIsInsn(code_ptr)) {
567 FindBlock(cu, cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 /* split */
569 false,
570 /* create */
571 true,
buzbeefa57c472012-11-21 12:06:18 -0800572 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700574 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 }
buzbeefa57c472012-11-21 12:06:18 -0800576 return cur_block;
buzbee67bf8852011-08-17 17:51:35 -0700577}
578
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579/* Process instructions with the kSwitch flag */
buzbeefa57c472012-11-21 12:06:18 -0800580static void ProcessCanSwitch(CompilationUnit* cu, BasicBlock* cur_block,
581 MIR* insn, int cur_offset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700582{
buzbeefa57c472012-11-21 12:06:18 -0800583 const uint16_t* switch_data =
584 reinterpret_cast<const uint16_t*>(cu->insns + cur_offset + insn->dalvikInsn.vB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700585 int size;
buzbeecbd6d442012-11-17 14:11:25 -0800586 const int* keyTable;
buzbeefa57c472012-11-21 12:06:18 -0800587 const int* target_table;
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 int i;
buzbeefa57c472012-11-21 12:06:18 -0800589 int first_key;
buzbee67bf8852011-08-17 17:51:35 -0700590
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 /*
592 * Packed switch data format:
593 * ushort ident = 0x0100 magic value
594 * ushort size number of entries in the table
595 * int first_key first (and lowest) switch case value
596 * int targets[size] branch targets, relative to switch opcode
597 *
598 * Total size is (4+size*2) 16-bit code units.
599 */
600 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
buzbeefa57c472012-11-21 12:06:18 -0800601 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700602 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800603 size = switch_data[1];
604 first_key = switch_data[2] | (switch_data[3] << 16);
605 target_table = reinterpret_cast<const int*>(&switch_data[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 keyTable = NULL; // Make the compiler happy
607 /*
608 * Sparse switch data format:
609 * ushort ident = 0x0200 magic value
610 * ushort size number of entries in the table; > 0
611 * int keys[size] keys, sorted low-to-high; 32-bit aligned
612 * int targets[size] branch targets, relative to switch opcode
613 *
614 * Total size is (2+size*4) 16-bit code units.
615 */
616 } else {
buzbeefa57c472012-11-21 12:06:18 -0800617 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 static_cast<int>(Instruction::kSparseSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800619 size = switch_data[1];
620 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
621 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
622 first_key = 0; // To make the compiler happy
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 }
buzbee67bf8852011-08-17 17:51:35 -0700624
buzbeefa57c472012-11-21 12:06:18 -0800625 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800627 << static_cast<int>(cur_block->successor_block_list.block_list_type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 }
buzbeefa57c472012-11-21 12:06:18 -0800629 cur_block->successor_block_list.block_list_type =
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
631 kPackedSwitch : kSparseSwitch;
buzbeefa57c472012-11-21 12:06:18 -0800632 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 kListSuccessorBlocks);
634
635 for (i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800636 BasicBlock *case_block = FindBlock(cu, cur_offset + target_table[i],
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 /* split */
638 true,
639 /* create */
640 true,
buzbeefa57c472012-11-21 12:06:18 -0800641 /* immed_pred_block_p */
642 &cur_block);
643 SuccessorBlockInfo *successor_block_info =
644 static_cast<SuccessorBlockInfo*>(NewMem(cu, sizeof(SuccessorBlockInfo),
buzbeecbd6d442012-11-17 14:11:25 -0800645 false, kAllocSuccessor));
buzbeefa57c472012-11-21 12:06:18 -0800646 successor_block_info->block = case_block;
647 successor_block_info->key =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
buzbeefa57c472012-11-21 12:06:18 -0800649 first_key + i : keyTable[i];
650 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
651 reinterpret_cast<uintptr_t>(successor_block_info));
652 InsertGrowableList(cu, case_block->predecessors,
653 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 }
655
656 /* Fall-through case */
buzbeefa57c472012-11-21 12:06:18 -0800657 BasicBlock* fallthrough_block = FindBlock(cu,
658 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700659 /* split */
660 false,
661 /* create */
662 true,
buzbeefa57c472012-11-21 12:06:18 -0800663 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 NULL);
buzbeefa57c472012-11-21 12:06:18 -0800665 cur_block->fall_through = fallthrough_block;
666 InsertGrowableList(cu, fallthrough_block->predecessors,
667 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700668}
669
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670/* Process instructions with the kThrow flag */
buzbeefa57c472012-11-21 12:06:18 -0800671static BasicBlock* ProcessCanThrow(CompilationUnit* cu, BasicBlock* cur_block,
672 MIR* insn, int cur_offset, int width, int flags,
673 ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
674 const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700675{
buzbeefa57c472012-11-21 12:06:18 -0800676 const DexFile::CodeItem* code_item = cu->code_item;
677 bool in_try_block = IsBitSet(try_block_addr, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700678
Bill Buzbeea114add2012-05-03 15:00:40 -0700679 /* In try block */
buzbeefa57c472012-11-21 12:06:18 -0800680 if (in_try_block) {
681 CatchHandlerIterator iterator(*code_item, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700682
buzbeefa57c472012-11-21 12:06:18 -0800683 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
684 LOG(INFO) << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800686 << static_cast<int>(cur_block->successor_block_list.block_list_type);
buzbee67bf8852011-08-17 17:51:35 -0700687 }
688
buzbeefa57c472012-11-21 12:06:18 -0800689 cur_block->successor_block_list.block_list_type = kCatch;
690 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, 2,
Bill Buzbeea114add2012-05-03 15:00:40 -0700691 kListSuccessorBlocks);
692
693 for (;iterator.HasNext(); iterator.Next()) {
buzbeefa57c472012-11-21 12:06:18 -0800694 BasicBlock *catch_block = FindBlock(cu, iterator.GetHandlerAddress(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 false /* split*/,
696 false /* creat */,
buzbeefa57c472012-11-21 12:06:18 -0800697 NULL /* immed_pred_block_p */);
698 catch_block->catch_entry = true;
699 cu->catches.insert(catch_block->start_offset);
700 SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
701 (NewMem(cu, sizeof(SuccessorBlockInfo), false, kAllocSuccessor));
702 successor_block_info->block = catch_block;
703 successor_block_info->key = iterator.GetHandlerTypeIndex();
704 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
705 reinterpret_cast<uintptr_t>(successor_block_info));
706 InsertGrowableList(cu, catch_block->predecessors,
707 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700708 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 } else {
buzbeefa57c472012-11-21 12:06:18 -0800710 BasicBlock *eh_block = NewMemBB(cu, kExceptionHandling,
711 cu->num_blocks++);
712 cur_block->taken = eh_block;
713 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(eh_block));
714 eh_block->start_offset = cur_offset;
715 InsertGrowableList(cu, eh_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700716 }
717
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700718 if (insn->dalvikInsn.opcode == Instruction::THROW){
buzbeefa57c472012-11-21 12:06:18 -0800719 cur_block->explicit_throw = true;
720 if ((code_ptr < code_end) && ContentIsInsn(code_ptr)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700721 // Force creation of new block following THROW via side-effect
buzbeefa57c472012-11-21 12:06:18 -0800722 FindBlock(cu, cur_offset + width, /* split */ false,
723 /* create */ true, /* immed_pred_block_p */ NULL);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700724 }
buzbeefa57c472012-11-21 12:06:18 -0800725 if (!in_try_block) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700726 // Don't split a THROW that can't rethrow - we're done.
buzbeefa57c472012-11-21 12:06:18 -0800727 return cur_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 }
729 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700730
731 /*
732 * Split the potentially-throwing instruction into two parts.
733 * The first half will be a pseudo-op that captures the exception
734 * edges and terminates the basic block. It always falls through.
735 * Then, create a new basic block that begins with the throwing instruction
736 * (minus exceptions). Note: this new basic block must NOT be entered into
buzbeefa57c472012-11-21 12:06:18 -0800737 * the block_map. If the potentially-throwing instruction is the target of a
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700738 * future branch, we need to find the check psuedo half. The new
739 * basic block containing the work portion of the instruction should
740 * only be entered via fallthrough from the block containing the
741 * pseudo exception edge MIR. Note also that this new block is
742 * not automatically terminated after the work portion, and may
743 * contain following instructions.
744 */
buzbeefa57c472012-11-21 12:06:18 -0800745 BasicBlock *new_block = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
746 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(new_block));
747 new_block->start_offset = insn->offset;
748 cur_block->fall_through = new_block;
749 InsertGrowableList(cu, new_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
750 MIR* new_insn = static_cast<MIR*>(NewMem(cu, sizeof(MIR), true, kAllocMIR));
751 *new_insn = *insn;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700752 insn->dalvikInsn.opcode =
753 static_cast<Instruction::Code>(kMirOpCheck);
754 // Associate the two halves
buzbeefa57c472012-11-21 12:06:18 -0800755 insn->meta.throw_insn = new_insn;
756 new_insn->meta.throw_insn = insn;
757 AppendMIR(new_block, new_insn);
758 return new_block;
buzbee67bf8852011-08-17 17:51:35 -0700759}
760
Ian Rogers1212a022013-03-04 10:48:41 -0800761void CompilerInit(CompilationUnit* cu, const CompilerDriver& compiler) {
buzbee02031b12012-11-23 09:41:35 -0800762 bool success = false;
763 switch (compiler.GetInstructionSet()) {
764 case kThumb2:
765 success = InitArmCodegen(cu);
766 break;
767 case kMips:
768 success = InitMipsCodegen(cu);
769 break;
770 case kX86:
771 success = InitX86Codegen(cu);
772 break;
773 default:;
774 }
775 if (!success) {
776 LOG(FATAL) << "Failed to initialize codegen for " << compiler.GetInstructionSet();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800777 }
buzbeefa57c472012-11-21 12:06:18 -0800778 if (!HeapInit(cu)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800779 LOG(FATAL) << "Failed to initialize oat heap";
780 }
781}
782
Ian Rogers1212a022013-03-04 10:48:41 -0800783static CompiledMethod* CompileMethod(CompilerDriver& compiler,
buzbeefa57c472012-11-21 12:06:18 -0800784 const CompilerBackend compiler_backend,
buzbee52a77fc2012-11-20 19:50:46 -0800785 const DexFile::CodeItem* code_item,
786 uint32_t access_flags, InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800787 uint32_t class_def_idx, uint32_t method_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800788 jobject class_loader, const DexFile& dex_file
789#if defined(ART_USE_PORTABLE_COMPILER)
790 , llvm::LlvmCompilationUnit* llvm_compilation_unit
791#endif
792)
buzbee67bf8852011-08-17 17:51:35 -0700793{
Bill Buzbeea114add2012-05-03 15:00:40 -0700794 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700795
buzbeefa57c472012-11-21 12:06:18 -0800796 const uint16_t* code_ptr = code_item->insns_;
797 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
798 int num_blocks = 0;
799 unsigned int cur_offset = 0;
buzbee67bf8852011-08-17 17:51:35 -0700800
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
buzbeefa57c472012-11-21 12:06:18 -0800802 UniquePtr<CompilationUnit> cu(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800803
buzbeefa57c472012-11-21 12:06:18 -0800804 CompilerInit(cu.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800805
Ian Rogers1212a022013-03-04 10:48:41 -0800806 cu->compiler_driver = &compiler;
buzbeefa57c472012-11-21 12:06:18 -0800807 cu->class_linker = class_linker;
808 cu->dex_file = &dex_file;
Ian Rogersfffdb022013-01-04 15:14:08 -0800809 cu->class_def_idx = class_def_idx;
buzbeefa57c472012-11-21 12:06:18 -0800810 cu->method_idx = method_idx;
811 cu->code_item = code_item;
812 cu->access_flags = access_flags;
813 cu->invoke_type = invoke_type;
814 cu->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
815 cu->instruction_set = compiler.GetInstructionSet();
816 cu->insns = code_item->insns_;
817 cu->insns_size = code_item->insns_size_in_code_units_;
818 cu->num_ins = code_item->ins_size_;
819 cu->num_regs = code_item->registers_size_ - cu->num_ins;
820 cu->num_outs = code_item->outs_size_;
821 DCHECK((cu->instruction_set == kThumb2) ||
822 (cu->instruction_set == kX86) ||
823 (cu->instruction_set == kMips));
824 if ((compiler_backend == kQuickGBC) || (compiler_backend == kPortable)) {
825 cu->gen_bitcode = true;
buzbee85eee022012-07-16 22:12:38 -0700826 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800827#if defined(ART_USE_PORTABLE_COMPILER)
828 cu->llvm_compilation_unit = llvm_compilation_unit;
829 cu->llvm_info = llvm_compilation_unit->GetQuickContext();
830 cu->symbol = llvm_compilation_unit->GetDexCompilationUnit()->GetSymbol();
831#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700832 /* Adjust this value accordingly once inlining is performed */
buzbeefa57c472012-11-21 12:06:18 -0800833 cu->num_dalvik_registers = code_item->registers_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 // TODO: set this from command line
buzbeefa57c472012-11-21 12:06:18 -0800835 cu->compiler_flip_match = false;
836 bool use_match = !cu->compiler_method_match.empty();
837 bool match = use_match && (cu->compiler_flip_match ^
838 (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) !=
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 std::string::npos));
buzbeefa57c472012-11-21 12:06:18 -0800840 if (!use_match || match) {
841 cu->disable_opt = kCompilerOptimizerDisableFlags;
842 cu->enable_debug = kCompilerDebugFlags;
843 cu->verbose = VLOG_IS_ON(compiler) ||
844 (cu->enable_debug & (1 << kDebugVerbose));
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 }
buzbee6459e7c2012-10-02 14:42:41 -0700846#ifndef NDEBUG
buzbeefa57c472012-11-21 12:06:18 -0800847 if (cu->gen_bitcode) {
848 cu->enable_debug |= (1 << kDebugVerifyBitcode);
buzbee6969d502012-06-15 16:40:31 -0700849 }
buzbee2cfc6392012-05-07 14:51:40 -0700850#endif
buzbee9281f002012-10-24 12:17:24 -0700851
buzbeefa57c472012-11-21 12:06:18 -0800852 if (cu->instruction_set == kMips) {
jeffhao7fbee072012-08-24 17:56:54 -0700853 // Disable some optimizations for mips for now
buzbeefa57c472012-11-21 12:06:18 -0800854 cu->disable_opt |= (
jeffhao7fbee072012-08-24 17:56:54 -0700855 (1 << kLoadStoreElimination) |
856 (1 << kLoadHoisting) |
857 (1 << kSuppressLoads) |
858 (1 << kNullCheckElimination) |
859 (1 << kPromoteRegs) |
860 (1 << kTrackLiveTemps) |
861 (1 << kSkipLargeMethodOptimization) |
862 (1 << kSafeOptimizations) |
863 (1 << kBBOpt) |
864 (1 << kMatch) |
865 (1 << kPromoteCompilerTemps));
866 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700867
868 /* Gathering opcode stats? */
869 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbeefa57c472012-11-21 12:06:18 -0800870 cu->opcode_count =
871 static_cast<int*>(NewMem(cu.get(), kNumPackedOpcodes * sizeof(int), true, kAllocMisc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700872 }
873
874 /* Assume non-throwing leaf */
buzbeefa57c472012-11-21 12:06:18 -0800875 cu->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
Bill Buzbeea114add2012-05-03 15:00:40 -0700876
buzbeefa57c472012-11-21 12:06:18 -0800877 /* Initialize the block list, estimate size based on insns_size */
878 CompilerInitGrowableList(cu.get(), &cu->block_list, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700879 kListBlockList);
880
buzbeefa57c472012-11-21 12:06:18 -0800881 /* Initialize the switch_tables list */
882 CompilerInitGrowableList(cu.get(), &cu->switch_tables, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700883 kListSwitchTables);
884
buzbeefa57c472012-11-21 12:06:18 -0800885 /* Intialize the fill_array_data list */
886 CompilerInitGrowableList(cu.get(), &cu->fill_array_data, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700887 kListFillArrayData);
888
buzbeefa57c472012-11-21 12:06:18 -0800889 /* Intialize the throw_launchpads list, estimate size based on insns_size */
890 CompilerInitGrowableList(cu.get(), &cu->throw_launchpads, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700891 kListThrowLaunchPads);
892
buzbeefa57c472012-11-21 12:06:18 -0800893 /* Intialize the instrinsic_launchpads list */
894 CompilerInitGrowableList(cu.get(), &cu->intrinsic_launchpads, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 kListMisc);
896
897
buzbeefa57c472012-11-21 12:06:18 -0800898 /* Intialize the suspend_launchpads list */
899 CompilerInitGrowableList(cu.get(), &cu->suspend_launchpads, 2048,
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 kListSuspendLaunchPads);
901
902 /* Allocate the bit-vector to track the beginning of basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800903 ArenaBitVector *try_block_addr = AllocBitVector(cu.get(),
904 cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700905 true /* expandable */);
buzbeefa57c472012-11-21 12:06:18 -0800906 cu->try_block_addr = try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700907
908 /* Create the default entry and exit blocks and enter them to the list */
buzbeefa57c472012-11-21 12:06:18 -0800909 BasicBlock *entry_block = NewMemBB(cu.get(), kEntryBlock, num_blocks++);
910 BasicBlock *exit_block = NewMemBB(cu.get(), kExitBlock, num_blocks++);
Bill Buzbeea114add2012-05-03 15:00:40 -0700911
buzbeefa57c472012-11-21 12:06:18 -0800912 cu->entry_block = entry_block;
913 cu->exit_block = exit_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700914
buzbeefa57c472012-11-21 12:06:18 -0800915 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(entry_block));
916 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(exit_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700917
918 /* Current block to record parsed instructions */
buzbeefa57c472012-11-21 12:06:18 -0800919 BasicBlock *cur_block = NewMemBB(cu.get(), kDalvikByteCode, num_blocks++);
920 cur_block->start_offset = 0;
921 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 /* Add first block to the fast lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800923 cu->block_map.Put(cur_block->start_offset, cur_block);
924 entry_block->fall_through = cur_block;
925 InsertGrowableList(cu.get(), cur_block->predecessors,
926 reinterpret_cast<uintptr_t>(entry_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700927
928 /*
929 * Store back the number of blocks since new blocks may be created of
buzbeefa57c472012-11-21 12:06:18 -0800930 * accessing cu.
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 */
buzbeefa57c472012-11-21 12:06:18 -0800932 cu->num_blocks = num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700933
934 /* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800935 ProcessTryCatchBlocks(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -0700936
937 /* Set up for simple method detection */
buzbeefa57c472012-11-21 12:06:18 -0800938 int num_patterns = sizeof(special_patterns)/sizeof(special_patterns[0]);
939 bool live_pattern = (num_patterns > 0) && !(cu->disable_opt & (1 << kMatch));
940 bool* dead_pattern =
941 static_cast<bool*>(NewMem(cu.get(), sizeof(bool) * num_patterns, true, kAllocMisc));
942 SpecialCaseHandler special_case = kNoHandler;
943 int pattern_pos = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700944
945 /* Parse all instructions and put them into containing basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800946 while (code_ptr < code_end) {
947 MIR *insn = static_cast<MIR *>(NewMem(cu.get(), sizeof(MIR), true, kAllocMIR));
948 insn->offset = cur_offset;
buzbeea169e1d2012-12-05 14:26:44 -0800949 int width = ParseInsn(cu.get(), code_ptr, &insn->dalvikInsn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 insn->width = width;
951 Instruction::Code opcode = insn->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800952 if (cu->opcode_count != NULL) {
953 cu->opcode_count[static_cast<int>(opcode)]++;
buzbee44b412b2012-02-04 08:50:53 -0800954 }
955
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 /* Terminate when the data section is seen */
957 if (width == 0)
958 break;
959
960 /* Possible simple method? */
buzbeefa57c472012-11-21 12:06:18 -0800961 if (live_pattern) {
962 live_pattern = false;
963 special_case = kNoHandler;
964 for (int i = 0; i < num_patterns; i++) {
965 if (!dead_pattern[i]) {
966 if (special_patterns[i].opcodes[pattern_pos] == opcode) {
967 live_pattern = true;
968 special_case = special_patterns[i].handler_code;
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 } else {
buzbeefa57c472012-11-21 12:06:18 -0800970 dead_pattern[i] = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 }
972 }
973 }
buzbeefa57c472012-11-21 12:06:18 -0800974 pattern_pos++;
buzbeea7c12682012-03-19 13:13:53 -0700975 }
976
buzbeefa57c472012-11-21 12:06:18 -0800977 AppendMIR(cur_block, insn);
buzbeecefd1872011-09-09 09:59:52 -0700978
buzbeefa57c472012-11-21 12:06:18 -0800979 code_ptr += width;
Ian Rogersa75a0132012-09-28 11:41:42 -0700980 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700981
buzbeefa57c472012-11-21 12:06:18 -0800982 int df_flags = oat_data_flow_attributes[insn->dalvikInsn.opcode];
buzbee67bf8852011-08-17 17:51:35 -0700983
buzbeefa57c472012-11-21 12:06:18 -0800984 if (df_flags & DF_HAS_DEFS) {
985 cu->def_count += (df_flags & DF_A_WIDE) ? 2 : 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 }
buzbee67bf8852011-08-17 17:51:35 -0700987
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 if (flags & Instruction::kBranch) {
buzbeefa57c472012-11-21 12:06:18 -0800989 cur_block = ProcessCanBranch(cu.get(), cur_block, insn, cur_offset,
990 width, flags, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 } else if (flags & Instruction::kReturn) {
buzbeebbdd0532013-02-07 09:33:02 -0800992 cur_block->terminated_by_return = true;
buzbeefa57c472012-11-21 12:06:18 -0800993 cur_block->fall_through = exit_block;
994 InsertGrowableList(cu.get(), exit_block->predecessors,
995 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 /*
997 * Terminate the current block if there are instructions
998 * afterwards.
999 */
buzbeefa57c472012-11-21 12:06:18 -08001000 if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001001 /*
1002 * Create a fallthrough block for real instructions
1003 * (incl. NOP).
1004 */
buzbeefa57c472012-11-21 12:06:18 -08001005 if (ContentIsInsn(code_ptr)) {
1006 FindBlock(cu.get(), cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -07001007 /* split */
1008 false,
1009 /* create */
1010 true,
buzbeefa57c472012-11-21 12:06:18 -08001011 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001012 NULL);
1013 }
1014 }
1015 } else if (flags & Instruction::kThrow) {
buzbeefa57c472012-11-21 12:06:18 -08001016 cur_block = ProcessCanThrow(cu.get(), cur_block, insn, cur_offset,
1017 width, flags, try_block_addr, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 } else if (flags & Instruction::kSwitch) {
buzbeefa57c472012-11-21 12:06:18 -08001019 ProcessCanSwitch(cu.get(), cur_block, insn, cur_offset, width, flags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001020 }
buzbeefa57c472012-11-21 12:06:18 -08001021 cur_offset += width;
1022 BasicBlock *next_block = FindBlock(cu.get(), cur_offset,
Bill Buzbeea114add2012-05-03 15:00:40 -07001023 /* split */
1024 false,
1025 /* create */
1026 false,
buzbeefa57c472012-11-21 12:06:18 -08001027 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001028 NULL);
buzbeefa57c472012-11-21 12:06:18 -08001029 if (next_block) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 /*
1031 * The next instruction could be the target of a previously parsed
1032 * forward branch so a block is already created. If the current
1033 * instruction is not an unconditional branch, connect them through
1034 * the fall-through link.
1035 */
buzbeefa57c472012-11-21 12:06:18 -08001036 DCHECK(cur_block->fall_through == NULL ||
1037 cur_block->fall_through == next_block ||
1038 cur_block->fall_through == exit_block);
buzbee5ade1d22011-09-09 14:44:52 -07001039
buzbeefa57c472012-11-21 12:06:18 -08001040 if ((cur_block->fall_through == NULL) && (flags & Instruction::kContinue)) {
1041 cur_block->fall_through = next_block;
1042 InsertGrowableList(cu.get(), next_block->predecessors,
1043 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 }
buzbeefa57c472012-11-21 12:06:18 -08001045 cur_block = next_block;
Bill Buzbeea114add2012-05-03 15:00:40 -07001046 }
1047 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001048
buzbeed8506212012-12-20 14:15:05 -08001049 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1050 DumpCFG(cu.get(), "/sdcard/1_post_parse_cfg/", true);
1051 }
1052
buzbeefa57c472012-11-21 12:06:18 -08001053 if (!(cu->disable_opt & (1 << kSkipLargeMethodOptimization))) {
1054 if ((cu->num_blocks > MANY_BLOCKS) ||
1055 ((cu->num_blocks > MANY_BLOCKS_INITIALIZER) &&
Bill Buzbeea114add2012-05-03 15:00:40 -07001056 PrettyMethod(method_idx, dex_file, false).find("init>") !=
1057 std::string::npos)) {
buzbeefa57c472012-11-21 12:06:18 -08001058 cu->qd_mode = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 }
1060 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001061
buzbeefa57c472012-11-21 12:06:18 -08001062 if (cu->qd_mode) {
buzbeed1643e42012-09-05 14:06:51 -07001063 // Bitcode generation requires full dataflow analysis
buzbeefa57c472012-11-21 12:06:18 -08001064 cu->disable_dataflow = !cu->gen_bitcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001065 // Disable optimization which require dataflow/ssa
buzbeefa57c472012-11-21 12:06:18 -08001066 cu->disable_opt |= (1 << kBBOpt) | (1 << kPromoteRegs) | (1 << kNullCheckElimination);
1067 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001068 LOG(INFO) << "QD mode enabled: "
1069 << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001070 << " num blocks: " << cu->num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -07001071 }
1072 }
buzbeec1f45042011-09-21 16:03:19 -07001073
buzbeefa57c472012-11-21 12:06:18 -08001074 if (cu->verbose) {
1075 DumpCompilationUnit(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001076 }
buzbee67bf8852011-08-17 17:51:35 -07001077
buzbee0967a252012-09-14 10:43:54 -07001078 /* Do a code layout pass */
buzbeefa57c472012-11-21 12:06:18 -08001079 CodeLayout(cu.get());
buzbee0967a252012-09-14 10:43:54 -07001080
buzbeed8506212012-12-20 14:15:05 -08001081 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1082 DumpCFG(cu.get(), "/sdcard/2_post_layout_cfg/", true);
1083 }
1084
buzbeefa57c472012-11-21 12:06:18 -08001085 if (cu->enable_debug & (1 << kDebugVerifyDataflow)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 /* Verify if all blocks are connected as claimed */
buzbeefa57c472012-11-21 12:06:18 -08001087 DataFlowAnalysisDispatcher(cu.get(), VerifyPredInfo, kAllNodes,
1088 false /* is_iterative */);
Bill Buzbeea114add2012-05-03 15:00:40 -07001089 }
buzbee67bf8852011-08-17 17:51:35 -07001090
Bill Buzbeea114add2012-05-03 15:00:40 -07001091 /* Perform SSA transformation for the whole method */
buzbeefa57c472012-11-21 12:06:18 -08001092 SSATransformation(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001093
buzbeed8506212012-12-20 14:15:05 -08001094 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1095 DumpCFG(cu.get(), "/sdcard/3_post_ssa_cfg/", false);
1096 }
1097
buzbee2cfc6392012-05-07 14:51:40 -07001098 /* Do constant propagation */
buzbee4ef3e452012-12-14 13:35:28 -08001099 cu->is_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs, false /* not expandable */);
1100 cu->must_flush_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs,
1101 false /* not expandable */);
buzbeefa57c472012-11-21 12:06:18 -08001102 cu->constant_values =
1103 static_cast<int*>(NewMem(cu.get(), sizeof(int) * cu->num_ssa_regs, true, kAllocDFInfo));
1104 DataFlowAnalysisDispatcher(cu.get(), DoConstantPropogation,
buzbee2cfc6392012-05-07 14:51:40 -07001105 kAllNodes,
buzbeefa57c472012-11-21 12:06:18 -08001106 false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -07001107
Bill Buzbeea114add2012-05-03 15:00:40 -07001108 /* Detect loops */
buzbeefa57c472012-11-21 12:06:18 -08001109 LoopDetection(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001110
Bill Buzbeea114add2012-05-03 15:00:40 -07001111 /* Count uses */
buzbeefa57c472012-11-21 12:06:18 -08001112 MethodUseCount(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001113
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 /* Perform null check elimination */
buzbeefa57c472012-11-21 12:06:18 -08001115 NullCheckElimination(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001116
buzbeed8506212012-12-20 14:15:05 -08001117 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1118 DumpCFG(cu.get(), "/sdcard/4_post_nce_cfg/", false);
1119 }
1120
buzbeed1643e42012-09-05 14:06:51 -07001121 /* Combine basic blocks where possible */
buzbeefa57c472012-11-21 12:06:18 -08001122 BasicBlockCombine(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001123
buzbeed8506212012-12-20 14:15:05 -08001124 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1125 DumpCFG(cu.get(), "/sdcard/5_post_bbcombine_cfg/", false);
1126 }
1127
Bill Buzbeea114add2012-05-03 15:00:40 -07001128 /* Do some basic block optimizations */
buzbeefa57c472012-11-21 12:06:18 -08001129 BasicBlockOptimization(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001130
buzbeed8506212012-12-20 14:15:05 -08001131 // Debugging only
1132 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1133 DumpCFG(cu.get(), "/sdcard/6_post_bbo_cfg/", false);
1134 }
1135
buzbeefa57c472012-11-21 12:06:18 -08001136 if (cu->enable_debug & (1 << kDebugDumpCheckStats)) {
1137 DumpCheckStats(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001138 }
1139
buzbee02031b12012-11-23 09:41:35 -08001140 cu.get()->cg->CompilerInitializeRegAlloc(cu.get()); // Needs to happen after SSA naming
Bill Buzbeea114add2012-05-03 15:00:40 -07001141
1142 /* Allocate Registers using simple local allocation scheme */
buzbeefa57c472012-11-21 12:06:18 -08001143 SimpleRegAlloc(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001144
buzbee2502e002012-12-31 16:05:53 -08001145 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1146 DumpCFG(cu.get(), "/sdcard/7_post_ralloc_cfg/", true);
1147 }
1148
1149
Brian Carlstrom265091e2013-01-30 14:08:26 -08001150#if defined(ART_USE_PORTABLE_COMPILER)
buzbee2cfc6392012-05-07 14:51:40 -07001151 /* Go the LLVM path? */
buzbeefa57c472012-11-21 12:06:18 -08001152 if (cu->gen_bitcode) {
buzbee2cfc6392012-05-07 14:51:40 -07001153 // MIR->Bitcode
buzbeefa57c472012-11-21 12:06:18 -08001154 MethodMIR2Bitcode(cu.get());
1155 if (compiler_backend == kPortable) {
buzbeeabc4c6b2012-08-23 08:17:15 -07001156 // all done
buzbeefa57c472012-11-21 12:06:18 -08001157 ArenaReset(cu.get());
buzbeeabc4c6b2012-08-23 08:17:15 -07001158 return NULL;
1159 }
buzbee2cfc6392012-05-07 14:51:40 -07001160 // Bitcode->LIR
buzbeefa57c472012-11-21 12:06:18 -08001161 MethodBitcode2LIR(cu.get());
Brian Carlstrom265091e2013-01-30 14:08:26 -08001162 } else
1163#endif
1164 {
buzbeefa57c472012-11-21 12:06:18 -08001165 if (special_case != kNoHandler) {
buzbee2cfc6392012-05-07 14:51:40 -07001166 /*
1167 * Custom codegen for special cases. If for any reason the
buzbeefa57c472012-11-21 12:06:18 -08001168 * special codegen doesn't succeed, cu->first_lir_insn will
buzbee2cfc6392012-05-07 14:51:40 -07001169 * set to NULL;
1170 */
buzbeefa57c472012-11-21 12:06:18 -08001171 SpecialMIR2LIR(cu.get(), special_case);
buzbee2cfc6392012-05-07 14:51:40 -07001172 }
buzbee67bf8852011-08-17 17:51:35 -07001173
buzbee2cfc6392012-05-07 14:51:40 -07001174 /* Convert MIR to LIR, etc. */
buzbeefa57c472012-11-21 12:06:18 -08001175 if (cu->first_lir_insn == NULL) {
1176 MethodMIR2LIR(cu.get());
buzbee2cfc6392012-05-07 14:51:40 -07001177 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001178 }
buzbee67bf8852011-08-17 17:51:35 -07001179
Bill Buzbeea114add2012-05-03 15:00:40 -07001180 /* Method is not empty */
buzbeefa57c472012-11-21 12:06:18 -08001181 if (cu->first_lir_insn) {
buzbee67bf8852011-08-17 17:51:35 -07001182
Bill Buzbeea114add2012-05-03 15:00:40 -07001183 // mark the targets of switch statement case labels
buzbeefa57c472012-11-21 12:06:18 -08001184 ProcessSwitchTables(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001185
Bill Buzbeea114add2012-05-03 15:00:40 -07001186 /* Convert LIR into machine code. */
buzbeefa57c472012-11-21 12:06:18 -08001187 AssembleLIR(cu.get());
buzbee99ba9642012-01-25 14:23:14 -08001188
buzbeefa57c472012-11-21 12:06:18 -08001189 if (cu->verbose) {
1190 CodegenDump(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001191 }
1192
buzbeefa57c472012-11-21 12:06:18 -08001193 if (cu->opcode_count != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001194 LOG(INFO) << "Opcode Count";
1195 for (int i = 0; i < kNumPackedOpcodes; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001196 if (cu->opcode_count[i] != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001197 LOG(INFO) << "-C- "
1198 << Instruction::Name(static_cast<Instruction::Code>(i))
buzbeefa57c472012-11-21 12:06:18 -08001199 << " " << cu->opcode_count[i];
buzbee67bf8852011-08-17 17:51:35 -07001200 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001201 }
1202 }
1203 }
buzbeea7c12682012-03-19 13:13:53 -07001204
buzbeefa57c472012-11-21 12:06:18 -08001205 // Combine vmap tables - core regs, then fp regs - into vmap_table
1206 std::vector<uint16_t> vmap_table;
buzbeeca7a5e42012-08-20 11:12:18 -07001207 // Core regs may have been inserted out of order - sort first
buzbeefa57c472012-11-21 12:06:18 -08001208 std::sort(cu->core_vmap_table.begin(), cu->core_vmap_table.end());
1209 for (size_t i = 0 ; i < cu->core_vmap_table.size(); i++) {
buzbeeca7a5e42012-08-20 11:12:18 -07001210 // Copy, stripping out the phys register sort key
buzbeefa57c472012-11-21 12:06:18 -08001211 vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & cu->core_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001212 }
1213 // If we have a frame, push a marker to take place of lr
buzbeefa57c472012-11-21 12:06:18 -08001214 if (cu->frame_size > 0) {
1215 vmap_table.push_back(INVALID_VREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001216 } else {
buzbeefa57c472012-11-21 12:06:18 -08001217 DCHECK_EQ(__builtin_popcount(cu->core_spill_mask), 0);
1218 DCHECK_EQ(__builtin_popcount(cu->fp_spill_mask), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001219 }
buzbeeca7a5e42012-08-20 11:12:18 -07001220 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
buzbeefa57c472012-11-21 12:06:18 -08001221 for (uint32_t i = 0; i < cu->fp_vmap_table.size(); i++) {
1222 vmap_table.push_back(cu->fp_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001223 }
1224 CompiledMethod* result =
buzbeefa57c472012-11-21 12:06:18 -08001225 new CompiledMethod(cu->instruction_set, cu->code_buffer,
1226 cu->frame_size, cu->core_spill_mask, cu->fp_spill_mask,
1227 cu->combined_mapping_table, vmap_table, cu->native_gc_map);
buzbee67bf8852011-08-17 17:51:35 -07001228
Bill Buzbeea114add2012-05-03 15:00:40 -07001229 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001230 << " (" << (cu->code_buffer.size() * sizeof(cu->code_buffer[0]))
Bill Buzbeea114add2012-05-03 15:00:40 -07001231 << " bytes)";
buzbee5abfa3e2012-01-31 17:01:43 -08001232
1233#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -08001234 if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) {
1235 DumpMemStats(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001236 }
buzbee5abfa3e2012-01-31 17:01:43 -08001237#endif
buzbee67bf8852011-08-17 17:51:35 -07001238
buzbeefa57c472012-11-21 12:06:18 -08001239 ArenaReset(cu.get());
buzbeeba938cb2012-02-03 14:47:55 -08001240
Bill Buzbeea114add2012-05-03 15:00:40 -07001241 return result;
buzbee67bf8852011-08-17 17:51:35 -07001242}
1243
Ian Rogers1212a022013-03-04 10:48:41 -08001244CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001245 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -07001246 const DexFile::CodeItem* code_item,
Brian Carlstrom265091e2013-01-30 14:08:26 -08001247 uint32_t access_flags,
1248 InvokeType invoke_type,
1249 uint32_t class_def_idx,
1250 uint32_t method_idx,
1251 jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001252 const DexFile& dex_file,
Brian Carlstrom265091e2013-01-30 14:08:26 -08001253 llvm::LlvmCompilationUnit* llvm_compilation_unit)
buzbeeabc4c6b2012-08-23 08:17:15 -07001254{
Ian Rogersfffdb022013-01-04 15:14:08 -08001255 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -08001256 method_idx, class_loader, dex_file
1257#if defined(ART_USE_PORTABLE_COMPILER)
1258 , llvm_compilation_unit
1259#endif
1260 );
buzbeeabc4c6b2012-08-23 08:17:15 -07001261}
1262
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001263} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001264
Bill Buzbeea114add2012-05-03 15:00:40 -07001265extern "C" art::CompiledMethod*
Ian Rogers1212a022013-03-04 10:48:41 -08001266 ArtQuickCompileMethod(art::CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001267 const art::DexFile::CodeItem* code_item,
1268 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001269 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001270 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001271{
buzbeec531cef2012-10-18 07:09:20 -07001272 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
1273 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -08001274 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001275 class_def_idx, method_idx, class_loader, dex_file,
1276 NULL /* use thread llvm_info */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001277}