blob: 0d3cb2e4c69b46eb5eeceab62adaf1689832219e [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"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080026#include "ssa_transformation.h"
Ian Rogers0571d352011-11-03 19:51:38 -070027#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070029#include "runtime.h"
buzbee395116c2013-02-27 14:30:25 -080030#include "quick/codegen_util.h"
31#include "portable/mir_to_gbc.h"
32#include "quick/mir_to_lir.h"
buzbee67bf8852011-08-17 17:51:35 -070033
buzbee4df2bbd2012-10-11 14:46:06 -070034namespace {
Ian Rogersc928de92013-02-27 14:30:44 -080035#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070036 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
Shih-wei Liao215a9262012-10-12 10:29:46 -070037#endif
buzbee4df2bbd2012-10-11 14:46:06 -070038 void InitializeLLVMForQuick() {
Ian Rogers4c1c2832013-03-04 18:30:13 -080039 ::llvm::llvm_start_multithreaded();
buzbee4df2bbd2012-10-11 14:46:06 -070040 }
41}
buzbee4df2bbd2012-10-11 14:46:06 -070042
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080043namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080044namespace llvm {
45::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Ian Rogers76ae4fe2013-02-27 16:03:41 -080046}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080047
buzbee4df2bbd2012-10-11 14:46:06 -070048LLVMInfo::LLVMInfo() {
Ian Rogersc928de92013-02-27 14:30:44 -080049#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070050 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
51#endif
buzbee692be802012-08-29 15:52:59 -070052 // Create context, module, intrinsic helper & ir builder
Ian Rogers4c1c2832013-03-04 18:30:13 -080053 llvm_context_.reset(new ::llvm::LLVMContext());
54 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
55 ::llvm::StructType::create(*llvm_context_, "JavaObject");
56 art::llvm::makeLLVMModuleContents(llvm_module_);
57 intrinsic_helper_.reset( new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
58 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
buzbee692be802012-08-29 15:52:59 -070059}
60
buzbee4df2bbd2012-10-11 14:46:06 -070061LLVMInfo::~LLVMInfo() {
buzbee692be802012-08-29 15:52:59 -070062}
63
Ian Rogers1212a022013-03-04 10:48:41 -080064extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee692be802012-08-29 15:52:59 -070065 CHECK(compiler.GetCompilerContext() == NULL);
buzbeefa57c472012-11-21 12:06:18 -080066 LLVMInfo* llvm_info = new LLVMInfo();
67 compiler.SetCompilerContext(llvm_info);
buzbee692be802012-08-29 15:52:59 -070068}
69
Ian Rogers1212a022013-03-04 10:48:41 -080070extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee4df2bbd2012-10-11 14:46:06 -070071 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
buzbee692be802012-08-29 15:52:59 -070072 compiler.SetCompilerContext(NULL);
73}
buzbee692be802012-08-29 15:52:59 -070074
buzbeece302932011-10-04 14:32:18 -070075/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070076static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee4ef3e452012-12-14 13:35:28 -080077 (1 << kLoadStoreElimination) |
Bill Buzbeea114add2012-05-03 15:00:40 -070078 //(1 << kLoadHoisting) |
79 //(1 << kSuppressLoads) |
80 //(1 << kNullCheckElimination) |
81 //(1 << kPromoteRegs) |
82 //(1 << kTrackLiveTemps) |
buzbee4ef3e452012-12-14 13:35:28 -080083 (1 << kSkipLargeMethodOptimization) |
Bill Buzbeea114add2012-05-03 15:00:40 -070084 //(1 << kSafeOptimizations) |
85 //(1 << kBBOpt) |
86 //(1 << kMatch) |
87 //(1 << kPromoteCompilerTemps) |
88 0;
buzbeece302932011-10-04 14:32:18 -070089
Elliott Hughese52e49b2012-04-02 16:05:44 -070090static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Bill Buzbeea114add2012-05-03 15:00:40 -070091 //(1 << kDebugDisplayMissingTargets) |
92 //(1 << kDebugVerbose) |
93 //(1 << kDebugDumpCFG) |
94 //(1 << kDebugSlowFieldPath) |
95 //(1 << kDebugSlowInvokePath) |
96 //(1 << kDebugSlowStringPath) |
97 //(1 << kDebugSlowestFieldPath) |
98 //(1 << kDebugSlowestStringPath) |
99 //(1 << kDebugExerciseResolveMethod) |
100 //(1 << kDebugVerifyDataflow) |
101 //(1 << kDebugShowMemoryUsage) |
102 //(1 << kDebugShowNops) |
103 //(1 << kDebugCountOpcodes) |
buzbeed1643e42012-09-05 14:06:51 -0700104 //(1 << kDebugDumpCheckStats) |
buzbeead8f15e2012-06-18 14:49:45 -0700105 //(1 << kDebugDumpBitcodeFile) |
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700106 //(1 << kDebugVerifyBitcode) |
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 0;
buzbeece302932011-10-04 14:32:18 -0700108
buzbeefa57c472012-11-21 12:06:18 -0800109static bool ContentIsInsn(const uint16_t* code_ptr) {
110 uint16_t instr = *code_ptr;
buzbeecbd6d442012-11-17 14:11:25 -0800111 Instruction::Code opcode = static_cast<Instruction::Code>(instr & 0xff);
buzbee67bf8852011-08-17 17:51:35 -0700112
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 /*
114 * Since the low 8-bit in metadata may look like NOP, we need to check
115 * both the low and whole sub-word to determine whether it is code or data.
116 */
117 return (opcode != Instruction::NOP || instr == 0);
buzbee67bf8852011-08-17 17:51:35 -0700118}
119
120/*
121 * Parse an instruction, return the length of the instruction
122 */
buzbeefa57c472012-11-21 12:06:18 -0800123static int ParseInsn(CompilationUnit* cu, const uint16_t* code_ptr,
buzbeea169e1d2012-12-05 14:26:44 -0800124 DecodedInstruction* decoded_instruction)
buzbee67bf8852011-08-17 17:51:35 -0700125{
Elliott Hughesadb8c672012-03-06 16:49:32 -0800126 // Don't parse instruction data
buzbeefa57c472012-11-21 12:06:18 -0800127 if (!ContentIsInsn(code_ptr)) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800128 return 0;
129 }
buzbee67bf8852011-08-17 17:51:35 -0700130
buzbeefa57c472012-11-21 12:06:18 -0800131 const Instruction* instruction = Instruction::At(code_ptr);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800132 *decoded_instruction = DecodedInstruction(instruction);
buzbee67bf8852011-08-17 17:51:35 -0700133
Elliott Hughesadb8c672012-03-06 16:49:32 -0800134 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -0700135}
136
137#define UNKNOWN_TARGET 0xffffffff
138
buzbee67bf8852011-08-17 17:51:35 -0700139/* Split an existing block from the specified code offset into two */
buzbeefa57c472012-11-21 12:06:18 -0800140static BasicBlock *SplitBlock(CompilationUnit* cu, unsigned int code_offset,
141 BasicBlock* orig_block, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700142{
buzbeefa57c472012-11-21 12:06:18 -0800143 MIR* insn = orig_block->first_mir_insn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 while (insn) {
buzbeefa57c472012-11-21 12:06:18 -0800145 if (insn->offset == code_offset) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700146 insn = insn->next;
147 }
148 if (insn == NULL) {
149 LOG(FATAL) << "Break split failed";
150 }
buzbeefa57c472012-11-21 12:06:18 -0800151 BasicBlock *bottom_block = NewMemBB(cu, kDalvikByteCode,
152 cu->num_blocks++);
153 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700154
buzbeefa57c472012-11-21 12:06:18 -0800155 bottom_block->start_offset = code_offset;
156 bottom_block->first_mir_insn = insn;
157 bottom_block->last_mir_insn = orig_block->last_mir_insn;
buzbee67bf8852011-08-17 17:51:35 -0700158
buzbeebbdd0532013-02-07 09:33:02 -0800159 /* If this block was terminated by a return, the flag needs to go with the bottom block */
160 bottom_block->terminated_by_return = orig_block->terminated_by_return;
161 orig_block->terminated_by_return = false;
162
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 /* Add it to the quick lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800164 cu->block_map.Put(bottom_block->start_offset, bottom_block);
buzbee5b537102012-01-17 17:33:47 -0800165
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 /* Handle the taken path */
buzbeefa57c472012-11-21 12:06:18 -0800167 bottom_block->taken = orig_block->taken;
168 if (bottom_block->taken) {
169 orig_block->taken = NULL;
170 DeleteGrowableList(bottom_block->taken->predecessors, reinterpret_cast<uintptr_t>(orig_block));
171 InsertGrowableList(cu, bottom_block->taken->predecessors,
172 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 }
174
175 /* Handle the fallthrough path */
buzbeefa57c472012-11-21 12:06:18 -0800176 bottom_block->fall_through = orig_block->fall_through;
177 orig_block->fall_through = bottom_block;
178 InsertGrowableList(cu, bottom_block->predecessors,
179 reinterpret_cast<uintptr_t>(orig_block));
180 if (bottom_block->fall_through) {
181 DeleteGrowableList(bottom_block->fall_through->predecessors,
182 reinterpret_cast<uintptr_t>(orig_block));
183 InsertGrowableList(cu, bottom_block->fall_through->predecessors,
184 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 }
186
187 /* Handle the successor list */
buzbeefa57c472012-11-21 12:06:18 -0800188 if (orig_block->successor_block_list.block_list_type != kNotUsed) {
189 bottom_block->successor_block_list = orig_block->successor_block_list;
190 orig_block->successor_block_list.block_list_type = kNotUsed;
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 GrowableListIterator iterator;
192
buzbeefa57c472012-11-21 12:06:18 -0800193 GrowableListIteratorInit(&bottom_block->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 &iterator);
195 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800196 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800197 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800198 if (successor_block_info == NULL) break;
199 BasicBlock *bb = successor_block_info->block;
200 DeleteGrowableList(bb->predecessors, reinterpret_cast<uintptr_t>(orig_block));
201 InsertGrowableList(cu, bb->predecessors, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700202 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 }
buzbee67bf8852011-08-17 17:51:35 -0700204
buzbeefa57c472012-11-21 12:06:18 -0800205 orig_block->last_mir_insn = insn->prev;
buzbee67bf8852011-08-17 17:51:35 -0700206
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 insn->prev->next = NULL;
208 insn->prev = NULL;
209 /*
210 * Update the immediate predecessor block pointer so that outgoing edges
211 * can be applied to the proper block.
212 */
buzbeefa57c472012-11-21 12:06:18 -0800213 if (immed_pred_block_p) {
214 DCHECK_EQ(*immed_pred_block_p, orig_block);
215 *immed_pred_block_p = bottom_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 }
buzbeefa57c472012-11-21 12:06:18 -0800217 return bottom_block;
buzbee67bf8852011-08-17 17:51:35 -0700218}
219
220/*
221 * Given a code offset, find out the block that starts with it. If the offset
buzbeefa57c472012-11-21 12:06:18 -0800222 * is in the middle of an existing block, split it into two. If immed_pred_block_p
223 * is not non-null and is the block being split, update *immed_pred_block_p to
buzbee9ab05de2012-01-18 15:43:48 -0800224 * point to the bottom block so that outgoing edges can be set up properly
225 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800226 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700227 */
buzbeefa57c472012-11-21 12:06:18 -0800228BasicBlock *FindBlock(CompilationUnit* cu, unsigned int code_offset,
229 bool split, bool create, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700230{
buzbeefa57c472012-11-21 12:06:18 -0800231 GrowableList* block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 BasicBlock* bb;
233 unsigned int i;
234 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700235
buzbeefa57c472012-11-21 12:06:18 -0800236 it = cu->block_map.find(code_offset);
237 if (it != cu->block_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700238 return it->second;
239 } else if (!create) {
240 return NULL;
241 }
242
243 if (split) {
buzbeefa57c472012-11-21 12:06:18 -0800244 for (i = 0; i < block_list->num_used; i++) {
245 bb = reinterpret_cast<BasicBlock*>(block_list->elem_list[i]);
246 if (bb->block_type != kDalvikByteCode) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 /* Check if a branch jumps into the middle of an existing block */
buzbeefa57c472012-11-21 12:06:18 -0800248 if ((code_offset > bb->start_offset) && (bb->last_mir_insn != NULL) &&
249 (code_offset <= bb->last_mir_insn->offset)) {
250 BasicBlock *new_bb = SplitBlock(cu, code_offset, bb,
251 bb == *immed_pred_block_p ?
252 immed_pred_block_p : NULL);
253 return new_bb;
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 }
buzbee5b537102012-01-17 17:33:47 -0800255 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 }
buzbee5b537102012-01-17 17:33:47 -0800257
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 /* Create a new one */
buzbeefa57c472012-11-21 12:06:18 -0800259 bb = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
260 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bb));
261 bb->start_offset = code_offset;
262 cu->block_map.Put(bb->start_offset, bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700264}
265
buzbeef58c12c2012-07-03 15:06:29 -0700266/* Find existing block */
buzbeefa57c472012-11-21 12:06:18 -0800267BasicBlock* FindBlock(CompilationUnit* cu, unsigned int code_offset)
buzbeef58c12c2012-07-03 15:06:29 -0700268{
buzbeefa57c472012-11-21 12:06:18 -0800269 return FindBlock(cu, code_offset, false, false, NULL);
buzbeef58c12c2012-07-03 15:06:29 -0700270}
271
buzbeead8f15e2012-06-18 14:49:45 -0700272/* Turn method name into a legal Linux file name */
buzbee52a77fc2012-11-20 19:50:46 -0800273void ReplaceSpecialChars(std::string& str)
buzbeead8f15e2012-06-18 14:49:45 -0700274{
275 static const struct { const char before; const char after; } match[] =
276 {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'},
277 {'(','@'}, {')','@'}, {'<','='}, {'>','='}};
278 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
279 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
280 }
281}
282
buzbee67bf8852011-08-17 17:51:35 -0700283/* Dump the CFG into a DOT graph */
buzbeed8506212012-12-20 14:15:05 -0800284void DumpCFG(CompilationUnit* cu, const char* dir_prefix, bool all_blocks)
buzbee67bf8852011-08-17 17:51:35 -0700285{
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 FILE* file;
buzbeefa57c472012-11-21 12:06:18 -0800287 std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -0800288 ReplaceSpecialChars(fname);
buzbeefa57c472012-11-21 12:06:18 -0800289 fname = StringPrintf("%s%s%x.dot", dir_prefix, fname.c_str(),
290 cu->entry_block->fall_through->start_offset);
buzbeead8f15e2012-06-18 14:49:45 -0700291 file = fopen(fname.c_str(), "w");
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 if (file == NULL) {
293 return;
294 }
295 fprintf(file, "digraph G {\n");
296
297 fprintf(file, " rankdir=TB\n");
298
buzbeed8506212012-12-20 14:15:05 -0800299 int num_blocks = all_blocks ? cu->num_blocks : cu->num_reachable_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 int idx;
buzbeefa57c472012-11-21 12:06:18 -0800301 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700302
buzbeed8506212012-12-20 14:15:05 -0800303 for (idx = 0; idx < num_blocks; idx++) {
304 int block_idx = all_blocks ? idx : cu->dfs_order.elem_list[idx];
buzbeefa57c472012-11-21 12:06:18 -0800305 BasicBlock *bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, block_idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -0800307 if (bb->block_type == kDead) continue;
308 if (bb->block_type == kEntryBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700309 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800310 } else if (bb->block_type == kExitBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700311 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800312 } else if (bb->block_type == kDalvikByteCode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700313 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800314 bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 const MIR *mir;
316 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
buzbeefa57c472012-11-21 12:06:18 -0800317 bb->first_mir_insn ? " | " : " ");
318 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
buzbeed8506212012-12-20 14:15:05 -0800319 int opcode = mir->dalvikInsn.opcode;
320 fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset,
buzbeea169e1d2012-12-05 14:26:44 -0800321 mir->ssa_rep ? GetDalvikDisassembly(cu, mir) :
buzbeed8506212012-12-20 14:15:05 -0800322 (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) :
323 extended_mir_op_names[opcode - kMirOpFirst],
324 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
325 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
Bill Buzbeea114add2012-05-03 15:00:40 -0700326 mir->next ? " | " : " ");
327 }
328 fprintf(file, " }\"];\n\n");
buzbeefa57c472012-11-21 12:06:18 -0800329 } else if (bb->block_type == kExceptionHandling) {
330 char block_name[BLOCK_NAME_LEN];
Bill Buzbeea114add2012-05-03 15:00:40 -0700331
buzbeefa57c472012-11-21 12:06:18 -0800332 GetBlockName(bb, block_name);
333 fprintf(file, " %s [shape=invhouse];\n", block_name);
buzbee67bf8852011-08-17 17:51:35 -0700334 }
buzbee67bf8852011-08-17 17:51:35 -0700335
buzbeefa57c472012-11-21 12:06:18 -0800336 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
buzbee67bf8852011-08-17 17:51:35 -0700337
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 if (bb->taken) {
buzbeefa57c472012-11-21 12:06:18 -0800339 GetBlockName(bb, block_name1);
340 GetBlockName(bb->taken, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
buzbeefa57c472012-11-21 12:06:18 -0800342 block_name1, block_name2);
buzbee67bf8852011-08-17 17:51:35 -0700343 }
buzbeefa57c472012-11-21 12:06:18 -0800344 if (bb->fall_through) {
345 GetBlockName(bb, block_name1);
346 GetBlockName(bb->fall_through, block_name2);
347 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 }
349
buzbeefa57c472012-11-21 12:06:18 -0800350 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700351 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800352 bb->start_offset, bb->id,
353 (bb->successor_block_list.block_list_type == kCatch) ?
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 "Mrecord" : "record");
355 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800356 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700357 &iterator);
buzbeefa57c472012-11-21 12:06:18 -0800358 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800359 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700360
buzbeefa57c472012-11-21 12:06:18 -0800361 int succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800363 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700364
buzbeefa57c472012-11-21 12:06:18 -0800365 BasicBlock *dest_block = successor_block_info->block;
366 SuccessorBlockInfo *next_successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800367 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700368
369 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
buzbeefa57c472012-11-21 12:06:18 -0800370 succ_id++,
371 successor_block_info->key,
372 dest_block->start_offset,
373 (next_successor_block_info != NULL) ? " | " : " ");
Bill Buzbeea114add2012-05-03 15:00:40 -0700374
buzbeefa57c472012-11-21 12:06:18 -0800375 successor_block_info = next_successor_block_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 }
377 fprintf(file, " }\"];\n\n");
378
buzbeefa57c472012-11-21 12:06:18 -0800379 GetBlockName(bb, block_name1);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700380 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
buzbeefa57c472012-11-21 12:06:18 -0800381 block_name1, bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700382
buzbeefa57c472012-11-21 12:06:18 -0800383 if (bb->successor_block_list.block_list_type == kPackedSwitch ||
384 bb->successor_block_list.block_list_type == kSparseSwitch) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700385
buzbeefa57c472012-11-21 12:06:18 -0800386 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 &iterator);
388
buzbeefa57c472012-11-21 12:06:18 -0800389 succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800391 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800392 reinterpret_cast<SuccessorBlockInfo*>( GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800393 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700394
buzbeefa57c472012-11-21 12:06:18 -0800395 BasicBlock *dest_block = successor_block_info->block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700396
buzbeefa57c472012-11-21 12:06:18 -0800397 GetBlockName(dest_block, block_name2);
398 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
399 bb->id, succ_id++, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 }
401 }
402 }
403 fprintf(file, "\n");
404
buzbeed8506212012-12-20 14:15:05 -0800405 if (cu->verbose) {
406 /* Display the dominator tree */
407 GetBlockName(bb, block_name1);
408 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
409 block_name1, block_name1);
410 if (bb->i_dom) {
411 GetBlockName(bb->i_dom, block_name2);
412 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
413 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 }
415 }
416 fprintf(file, "}\n");
417 fclose(file);
buzbee67bf8852011-08-17 17:51:35 -0700418}
419
420/* Verify if all the successor is connected with all the claimed predecessors */
buzbeefa57c472012-11-21 12:06:18 -0800421static bool VerifyPredInfo(CompilationUnit* cu, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700422{
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700424
buzbee52a77fc2012-11-20 19:50:46 -0800425 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800427 BasicBlock *pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
428 if (!pred_bb) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800430 if (pred_bb->taken == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800432 } else if (pred_bb->fall_through == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800434 } else if (pred_bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800436 GrowableListIteratorInit(&pred_bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 &iterator);
438 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800439 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800440 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800441 if (successor_block_info == NULL) break;
442 BasicBlock *succ_bb = successor_block_info->block;
443 if (succ_bb == bb) {
buzbee67bf8852011-08-17 17:51:35 -0700444 found = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 break;
buzbee67bf8852011-08-17 17:51:35 -0700446 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 }
buzbee67bf8852011-08-17 17:51:35 -0700448 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 if (found == false) {
buzbeefa57c472012-11-21 12:06:18 -0800450 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
451 GetBlockName(bb, block_name1);
452 GetBlockName(pred_bb, block_name2);
buzbeed8506212012-12-20 14:15:05 -0800453 DumpCFG(cu, "/sdcard/cfg/", false);
buzbeefa57c472012-11-21 12:06:18 -0800454 LOG(FATAL) << "Successor " << block_name1 << "not found from "
455 << block_name2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 }
457 }
458 return true;
buzbee67bf8852011-08-17 17:51:35 -0700459}
460
461/* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800462static void ProcessTryCatchBlocks(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700463{
buzbeefa57c472012-11-21 12:06:18 -0800464 const DexFile::CodeItem* code_item = cu->code_item;
465 int tries_size = code_item->tries_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700466 int offset;
buzbee67bf8852011-08-17 17:51:35 -0700467
buzbeefa57c472012-11-21 12:06:18 -0800468 if (tries_size == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 return;
470 }
471
buzbeefa57c472012-11-21 12:06:18 -0800472 ArenaBitVector* try_block_addr = cu->try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700473
buzbeefa57c472012-11-21 12:06:18 -0800474 for (int i = 0; i < tries_size; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 const DexFile::TryItem* pTry =
476 DexFile::GetTryItems(*code_item, i);
buzbeefa57c472012-11-21 12:06:18 -0800477 int start_offset = pTry->start_addr_;
478 int end_offset = start_offset + pTry->insn_count_;
479 for (offset = start_offset; offset < end_offset; offset++) {
480 SetBit(cu, try_block_addr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700481 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 }
buzbee67bf8852011-08-17 17:51:35 -0700483
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 // Iterate over each of the handlers to enqueue the empty Catch blocks
485 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
486 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
487 for (uint32_t idx = 0; idx < handlers_size; idx++) {
488 CatchHandlerIterator iterator(handlers_ptr);
489 for (; iterator.HasNext(); iterator.Next()) {
490 uint32_t address = iterator.GetHandlerAddress();
buzbeefa57c472012-11-21 12:06:18 -0800491 FindBlock(cu, address, false /* split */, true /*create*/,
492 /* immed_pred_block_p */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700493 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700494 handlers_ptr = iterator.EndDataPointer();
495 }
buzbee67bf8852011-08-17 17:51:35 -0700496}
497
Elliott Hughesadb8c672012-03-06 16:49:32 -0800498/* Process instructions with the kBranch flag */
buzbeefa57c472012-11-21 12:06:18 -0800499static BasicBlock* ProcessCanBranch(CompilationUnit* cu, BasicBlock* cur_block,
500 MIR* insn, int cur_offset, int width, int flags,
501 const uint16_t* code_ptr, const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700502{
buzbeefa57c472012-11-21 12:06:18 -0800503 int target = cur_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 switch (insn->dalvikInsn.opcode) {
505 case Instruction::GOTO:
506 case Instruction::GOTO_16:
507 case Instruction::GOTO_32:
buzbeecbd6d442012-11-17 14:11:25 -0800508 target += insn->dalvikInsn.vA;
Bill Buzbeea114add2012-05-03 15:00:40 -0700509 break;
510 case Instruction::IF_EQ:
511 case Instruction::IF_NE:
512 case Instruction::IF_LT:
513 case Instruction::IF_GE:
514 case Instruction::IF_GT:
515 case Instruction::IF_LE:
buzbeefa57c472012-11-21 12:06:18 -0800516 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800517 target += insn->dalvikInsn.vC;
Bill Buzbeea114add2012-05-03 15:00:40 -0700518 break;
519 case Instruction::IF_EQZ:
520 case Instruction::IF_NEZ:
521 case Instruction::IF_LTZ:
522 case Instruction::IF_GEZ:
523 case Instruction::IF_GTZ:
524 case Instruction::IF_LEZ:
buzbeefa57c472012-11-21 12:06:18 -0800525 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800526 target += insn->dalvikInsn.vB;
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 break;
528 default:
buzbeecbd6d442012-11-17 14:11:25 -0800529 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 }
buzbeefa57c472012-11-21 12:06:18 -0800531 BasicBlock *taken_block = FindBlock(cu, target,
Bill Buzbeea114add2012-05-03 15:00:40 -0700532 /* split */
533 true,
534 /* create */
535 true,
buzbeefa57c472012-11-21 12:06:18 -0800536 /* immed_pred_block_p */
537 &cur_block);
538 cur_block->taken = taken_block;
539 InsertGrowableList(cu, taken_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700540
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 /* Always terminate the current block for conditional branches */
542 if (flags & Instruction::kContinue) {
buzbeefa57c472012-11-21 12:06:18 -0800543 BasicBlock *fallthrough_block = FindBlock(cu,
544 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 /*
546 * If the method is processed
547 * in sequential order from the
548 * beginning, we don't need to
549 * specify split for continue
550 * blocks. However, this
551 * routine can be called by
552 * compileLoop, which starts
553 * parsing the method from an
554 * arbitrary address in the
555 * method body.
556 */
557 true,
558 /* create */
559 true,
buzbeefa57c472012-11-21 12:06:18 -0800560 /* immed_pred_block_p */
561 &cur_block);
562 cur_block->fall_through = fallthrough_block;
563 InsertGrowableList(cu, fallthrough_block->predecessors,
564 reinterpret_cast<uintptr_t>(cur_block));
565 } else if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbeefa57c472012-11-21 12:06:18 -0800567 if (ContentIsInsn(code_ptr)) {
568 FindBlock(cu, cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700569 /* split */
570 false,
571 /* create */
572 true,
buzbeefa57c472012-11-21 12:06:18 -0800573 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700575 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 }
buzbeefa57c472012-11-21 12:06:18 -0800577 return cur_block;
buzbee67bf8852011-08-17 17:51:35 -0700578}
579
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580/* Process instructions with the kSwitch flag */
buzbeefa57c472012-11-21 12:06:18 -0800581static void ProcessCanSwitch(CompilationUnit* cu, BasicBlock* cur_block,
582 MIR* insn, int cur_offset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700583{
buzbeefa57c472012-11-21 12:06:18 -0800584 const uint16_t* switch_data =
585 reinterpret_cast<const uint16_t*>(cu->insns + cur_offset + insn->dalvikInsn.vB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700586 int size;
buzbeecbd6d442012-11-17 14:11:25 -0800587 const int* keyTable;
buzbeefa57c472012-11-21 12:06:18 -0800588 const int* target_table;
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 int i;
buzbeefa57c472012-11-21 12:06:18 -0800590 int first_key;
buzbee67bf8852011-08-17 17:51:35 -0700591
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 /*
593 * Packed switch data format:
594 * ushort ident = 0x0100 magic value
595 * ushort size number of entries in the table
596 * int first_key first (and lowest) switch case value
597 * int targets[size] branch targets, relative to switch opcode
598 *
599 * Total size is (4+size*2) 16-bit code units.
600 */
601 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
buzbeefa57c472012-11-21 12:06:18 -0800602 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800604 size = switch_data[1];
605 first_key = switch_data[2] | (switch_data[3] << 16);
606 target_table = reinterpret_cast<const int*>(&switch_data[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 keyTable = NULL; // Make the compiler happy
608 /*
609 * Sparse switch data format:
610 * ushort ident = 0x0200 magic value
611 * ushort size number of entries in the table; > 0
612 * int keys[size] keys, sorted low-to-high; 32-bit aligned
613 * int targets[size] branch targets, relative to switch opcode
614 *
615 * Total size is (2+size*4) 16-bit code units.
616 */
617 } else {
buzbeefa57c472012-11-21 12:06:18 -0800618 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 static_cast<int>(Instruction::kSparseSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800620 size = switch_data[1];
621 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
622 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
623 first_key = 0; // To make the compiler happy
Bill Buzbeea114add2012-05-03 15:00:40 -0700624 }
buzbee67bf8852011-08-17 17:51:35 -0700625
buzbeefa57c472012-11-21 12:06:18 -0800626 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700627 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800628 << static_cast<int>(cur_block->successor_block_list.block_list_type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 }
buzbeefa57c472012-11-21 12:06:18 -0800630 cur_block->successor_block_list.block_list_type =
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
632 kPackedSwitch : kSparseSwitch;
buzbeefa57c472012-11-21 12:06:18 -0800633 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700634 kListSuccessorBlocks);
635
636 for (i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800637 BasicBlock *case_block = FindBlock(cu, cur_offset + target_table[i],
Bill Buzbeea114add2012-05-03 15:00:40 -0700638 /* split */
639 true,
640 /* create */
641 true,
buzbeefa57c472012-11-21 12:06:18 -0800642 /* immed_pred_block_p */
643 &cur_block);
644 SuccessorBlockInfo *successor_block_info =
645 static_cast<SuccessorBlockInfo*>(NewMem(cu, sizeof(SuccessorBlockInfo),
buzbeecbd6d442012-11-17 14:11:25 -0800646 false, kAllocSuccessor));
buzbeefa57c472012-11-21 12:06:18 -0800647 successor_block_info->block = case_block;
648 successor_block_info->key =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800649 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
buzbeefa57c472012-11-21 12:06:18 -0800650 first_key + i : keyTable[i];
651 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
652 reinterpret_cast<uintptr_t>(successor_block_info));
653 InsertGrowableList(cu, case_block->predecessors,
654 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 }
656
657 /* Fall-through case */
buzbeefa57c472012-11-21 12:06:18 -0800658 BasicBlock* fallthrough_block = FindBlock(cu,
659 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 /* split */
661 false,
662 /* create */
663 true,
buzbeefa57c472012-11-21 12:06:18 -0800664 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 NULL);
buzbeefa57c472012-11-21 12:06:18 -0800666 cur_block->fall_through = fallthrough_block;
667 InsertGrowableList(cu, fallthrough_block->predecessors,
668 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700669}
670
Elliott Hughesadb8c672012-03-06 16:49:32 -0800671/* Process instructions with the kThrow flag */
buzbeefa57c472012-11-21 12:06:18 -0800672static BasicBlock* ProcessCanThrow(CompilationUnit* cu, BasicBlock* cur_block,
673 MIR* insn, int cur_offset, int width, int flags,
674 ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
675 const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700676{
buzbeefa57c472012-11-21 12:06:18 -0800677 const DexFile::CodeItem* code_item = cu->code_item;
678 bool in_try_block = IsBitSet(try_block_addr, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700679
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 /* In try block */
buzbeefa57c472012-11-21 12:06:18 -0800681 if (in_try_block) {
682 CatchHandlerIterator iterator(*code_item, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700683
buzbeefa57c472012-11-21 12:06:18 -0800684 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
685 LOG(INFO) << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700686 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800687 << static_cast<int>(cur_block->successor_block_list.block_list_type);
buzbee67bf8852011-08-17 17:51:35 -0700688 }
689
buzbeefa57c472012-11-21 12:06:18 -0800690 cur_block->successor_block_list.block_list_type = kCatch;
691 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, 2,
Bill Buzbeea114add2012-05-03 15:00:40 -0700692 kListSuccessorBlocks);
693
694 for (;iterator.HasNext(); iterator.Next()) {
buzbeefa57c472012-11-21 12:06:18 -0800695 BasicBlock *catch_block = FindBlock(cu, iterator.GetHandlerAddress(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700696 false /* split*/,
697 false /* creat */,
buzbeefa57c472012-11-21 12:06:18 -0800698 NULL /* immed_pred_block_p */);
699 catch_block->catch_entry = true;
700 cu->catches.insert(catch_block->start_offset);
701 SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
702 (NewMem(cu, sizeof(SuccessorBlockInfo), false, kAllocSuccessor));
703 successor_block_info->block = catch_block;
704 successor_block_info->key = iterator.GetHandlerTypeIndex();
705 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
706 reinterpret_cast<uintptr_t>(successor_block_info));
707 InsertGrowableList(cu, catch_block->predecessors,
708 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700709 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700710 } else {
buzbeefa57c472012-11-21 12:06:18 -0800711 BasicBlock *eh_block = NewMemBB(cu, kExceptionHandling,
712 cu->num_blocks++);
713 cur_block->taken = eh_block;
714 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(eh_block));
715 eh_block->start_offset = cur_offset;
716 InsertGrowableList(cu, eh_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700717 }
718
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700719 if (insn->dalvikInsn.opcode == Instruction::THROW){
buzbeefa57c472012-11-21 12:06:18 -0800720 cur_block->explicit_throw = true;
721 if ((code_ptr < code_end) && ContentIsInsn(code_ptr)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700722 // Force creation of new block following THROW via side-effect
buzbeefa57c472012-11-21 12:06:18 -0800723 FindBlock(cu, cur_offset + width, /* split */ false,
724 /* create */ true, /* immed_pred_block_p */ NULL);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700725 }
buzbeefa57c472012-11-21 12:06:18 -0800726 if (!in_try_block) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700727 // Don't split a THROW that can't rethrow - we're done.
buzbeefa57c472012-11-21 12:06:18 -0800728 return cur_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700729 }
730 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700731
732 /*
733 * Split the potentially-throwing instruction into two parts.
734 * The first half will be a pseudo-op that captures the exception
735 * edges and terminates the basic block. It always falls through.
736 * Then, create a new basic block that begins with the throwing instruction
737 * (minus exceptions). Note: this new basic block must NOT be entered into
buzbeefa57c472012-11-21 12:06:18 -0800738 * the block_map. If the potentially-throwing instruction is the target of a
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700739 * future branch, we need to find the check psuedo half. The new
740 * basic block containing the work portion of the instruction should
741 * only be entered via fallthrough from the block containing the
742 * pseudo exception edge MIR. Note also that this new block is
743 * not automatically terminated after the work portion, and may
744 * contain following instructions.
745 */
buzbeefa57c472012-11-21 12:06:18 -0800746 BasicBlock *new_block = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
747 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(new_block));
748 new_block->start_offset = insn->offset;
749 cur_block->fall_through = new_block;
750 InsertGrowableList(cu, new_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
751 MIR* new_insn = static_cast<MIR*>(NewMem(cu, sizeof(MIR), true, kAllocMIR));
752 *new_insn = *insn;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700753 insn->dalvikInsn.opcode =
754 static_cast<Instruction::Code>(kMirOpCheck);
755 // Associate the two halves
buzbeefa57c472012-11-21 12:06:18 -0800756 insn->meta.throw_insn = new_insn;
757 new_insn->meta.throw_insn = insn;
758 AppendMIR(new_block, new_insn);
759 return new_block;
buzbee67bf8852011-08-17 17:51:35 -0700760}
761
Ian Rogers1212a022013-03-04 10:48:41 -0800762void CompilerInit(CompilationUnit* cu, const CompilerDriver& compiler) {
buzbee02031b12012-11-23 09:41:35 -0800763 bool success = false;
764 switch (compiler.GetInstructionSet()) {
765 case kThumb2:
766 success = InitArmCodegen(cu);
767 break;
768 case kMips:
769 success = InitMipsCodegen(cu);
770 break;
771 case kX86:
772 success = InitX86Codegen(cu);
773 break;
774 default:;
775 }
776 if (!success) {
777 LOG(FATAL) << "Failed to initialize codegen for " << compiler.GetInstructionSet();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800778 }
buzbeefa57c472012-11-21 12:06:18 -0800779 if (!HeapInit(cu)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800780 LOG(FATAL) << "Failed to initialize oat heap";
781 }
782}
783
Ian Rogers1212a022013-03-04 10:48:41 -0800784static CompiledMethod* CompileMethod(CompilerDriver& compiler,
buzbeefa57c472012-11-21 12:06:18 -0800785 const CompilerBackend compiler_backend,
buzbee52a77fc2012-11-20 19:50:46 -0800786 const DexFile::CodeItem* code_item,
787 uint32_t access_flags, InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800788 uint32_t class_def_idx, uint32_t method_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800789 jobject class_loader, const DexFile& dex_file
790#if defined(ART_USE_PORTABLE_COMPILER)
791 , llvm::LlvmCompilationUnit* llvm_compilation_unit
792#endif
793)
buzbee67bf8852011-08-17 17:51:35 -0700794{
Bill Buzbeea114add2012-05-03 15:00:40 -0700795 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700796
buzbeefa57c472012-11-21 12:06:18 -0800797 const uint16_t* code_ptr = code_item->insns_;
798 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
799 int num_blocks = 0;
800 unsigned int cur_offset = 0;
buzbee67bf8852011-08-17 17:51:35 -0700801
Bill Buzbeea114add2012-05-03 15:00:40 -0700802 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
buzbeefa57c472012-11-21 12:06:18 -0800803 UniquePtr<CompilationUnit> cu(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800804
buzbeefa57c472012-11-21 12:06:18 -0800805 CompilerInit(cu.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800806
Ian Rogers1212a022013-03-04 10:48:41 -0800807 cu->compiler_driver = &compiler;
buzbeefa57c472012-11-21 12:06:18 -0800808 cu->class_linker = class_linker;
809 cu->dex_file = &dex_file;
Ian Rogersfffdb022013-01-04 15:14:08 -0800810 cu->class_def_idx = class_def_idx;
buzbeefa57c472012-11-21 12:06:18 -0800811 cu->method_idx = method_idx;
812 cu->code_item = code_item;
813 cu->access_flags = access_flags;
814 cu->invoke_type = invoke_type;
815 cu->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
816 cu->instruction_set = compiler.GetInstructionSet();
817 cu->insns = code_item->insns_;
818 cu->insns_size = code_item->insns_size_in_code_units_;
819 cu->num_ins = code_item->ins_size_;
820 cu->num_regs = code_item->registers_size_ - cu->num_ins;
821 cu->num_outs = code_item->outs_size_;
822 DCHECK((cu->instruction_set == kThumb2) ||
823 (cu->instruction_set == kX86) ||
824 (cu->instruction_set == kMips));
825 if ((compiler_backend == kQuickGBC) || (compiler_backend == kPortable)) {
826 cu->gen_bitcode = true;
buzbee85eee022012-07-16 22:12:38 -0700827 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800828#if defined(ART_USE_PORTABLE_COMPILER)
829 cu->llvm_compilation_unit = llvm_compilation_unit;
830 cu->llvm_info = llvm_compilation_unit->GetQuickContext();
831 cu->symbol = llvm_compilation_unit->GetDexCompilationUnit()->GetSymbol();
832#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700833 /* Adjust this value accordingly once inlining is performed */
buzbeefa57c472012-11-21 12:06:18 -0800834 cu->num_dalvik_registers = code_item->registers_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 // TODO: set this from command line
buzbeefa57c472012-11-21 12:06:18 -0800836 cu->compiler_flip_match = false;
837 bool use_match = !cu->compiler_method_match.empty();
838 bool match = use_match && (cu->compiler_flip_match ^
839 (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) !=
Bill Buzbeea114add2012-05-03 15:00:40 -0700840 std::string::npos));
buzbeefa57c472012-11-21 12:06:18 -0800841 if (!use_match || match) {
842 cu->disable_opt = kCompilerOptimizerDisableFlags;
843 cu->enable_debug = kCompilerDebugFlags;
844 cu->verbose = VLOG_IS_ON(compiler) ||
845 (cu->enable_debug & (1 << kDebugVerbose));
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 }
buzbee6459e7c2012-10-02 14:42:41 -0700847#ifndef NDEBUG
buzbeefa57c472012-11-21 12:06:18 -0800848 if (cu->gen_bitcode) {
849 cu->enable_debug |= (1 << kDebugVerifyBitcode);
buzbee6969d502012-06-15 16:40:31 -0700850 }
buzbee2cfc6392012-05-07 14:51:40 -0700851#endif
buzbee9281f002012-10-24 12:17:24 -0700852
buzbeefa57c472012-11-21 12:06:18 -0800853 if (cu->instruction_set == kMips) {
jeffhao7fbee072012-08-24 17:56:54 -0700854 // Disable some optimizations for mips for now
buzbeefa57c472012-11-21 12:06:18 -0800855 cu->disable_opt |= (
jeffhao7fbee072012-08-24 17:56:54 -0700856 (1 << kLoadStoreElimination) |
857 (1 << kLoadHoisting) |
858 (1 << kSuppressLoads) |
859 (1 << kNullCheckElimination) |
860 (1 << kPromoteRegs) |
861 (1 << kTrackLiveTemps) |
862 (1 << kSkipLargeMethodOptimization) |
863 (1 << kSafeOptimizations) |
864 (1 << kBBOpt) |
865 (1 << kMatch) |
866 (1 << kPromoteCompilerTemps));
867 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700868
869 /* Gathering opcode stats? */
870 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbeefa57c472012-11-21 12:06:18 -0800871 cu->opcode_count =
872 static_cast<int*>(NewMem(cu.get(), kNumPackedOpcodes * sizeof(int), true, kAllocMisc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 }
874
875 /* Assume non-throwing leaf */
buzbeefa57c472012-11-21 12:06:18 -0800876 cu->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
Bill Buzbeea114add2012-05-03 15:00:40 -0700877
buzbeefa57c472012-11-21 12:06:18 -0800878 /* Initialize the block list, estimate size based on insns_size */
879 CompilerInitGrowableList(cu.get(), &cu->block_list, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 kListBlockList);
881
buzbeefa57c472012-11-21 12:06:18 -0800882 /* Initialize the switch_tables list */
883 CompilerInitGrowableList(cu.get(), &cu->switch_tables, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700884 kListSwitchTables);
885
buzbeefa57c472012-11-21 12:06:18 -0800886 /* Intialize the fill_array_data list */
887 CompilerInitGrowableList(cu.get(), &cu->fill_array_data, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700888 kListFillArrayData);
889
buzbeefa57c472012-11-21 12:06:18 -0800890 /* Intialize the throw_launchpads list, estimate size based on insns_size */
891 CompilerInitGrowableList(cu.get(), &cu->throw_launchpads, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700892 kListThrowLaunchPads);
893
buzbeefa57c472012-11-21 12:06:18 -0800894 /* Intialize the instrinsic_launchpads list */
895 CompilerInitGrowableList(cu.get(), &cu->intrinsic_launchpads, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 kListMisc);
897
898
buzbeefa57c472012-11-21 12:06:18 -0800899 /* Intialize the suspend_launchpads list */
900 CompilerInitGrowableList(cu.get(), &cu->suspend_launchpads, 2048,
Bill Buzbeea114add2012-05-03 15:00:40 -0700901 kListSuspendLaunchPads);
902
903 /* Allocate the bit-vector to track the beginning of basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800904 ArenaBitVector *try_block_addr = AllocBitVector(cu.get(),
905 cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 true /* expandable */);
buzbeefa57c472012-11-21 12:06:18 -0800907 cu->try_block_addr = try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700908
909 /* Create the default entry and exit blocks and enter them to the list */
buzbeefa57c472012-11-21 12:06:18 -0800910 BasicBlock *entry_block = NewMemBB(cu.get(), kEntryBlock, num_blocks++);
911 BasicBlock *exit_block = NewMemBB(cu.get(), kExitBlock, num_blocks++);
Bill Buzbeea114add2012-05-03 15:00:40 -0700912
buzbeefa57c472012-11-21 12:06:18 -0800913 cu->entry_block = entry_block;
914 cu->exit_block = exit_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700915
buzbeefa57c472012-11-21 12:06:18 -0800916 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(entry_block));
917 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(exit_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700918
919 /* Current block to record parsed instructions */
buzbeefa57c472012-11-21 12:06:18 -0800920 BasicBlock *cur_block = NewMemBB(cu.get(), kDalvikByteCode, num_blocks++);
921 cur_block->start_offset = 0;
922 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700923 /* Add first block to the fast lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800924 cu->block_map.Put(cur_block->start_offset, cur_block);
925 entry_block->fall_through = cur_block;
926 InsertGrowableList(cu.get(), cur_block->predecessors,
927 reinterpret_cast<uintptr_t>(entry_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700928
929 /*
930 * Store back the number of blocks since new blocks may be created of
buzbeefa57c472012-11-21 12:06:18 -0800931 * accessing cu.
Bill Buzbeea114add2012-05-03 15:00:40 -0700932 */
buzbeefa57c472012-11-21 12:06:18 -0800933 cu->num_blocks = num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700934
935 /* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800936 ProcessTryCatchBlocks(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -0700937
938 /* Set up for simple method detection */
buzbeefa57c472012-11-21 12:06:18 -0800939 int num_patterns = sizeof(special_patterns)/sizeof(special_patterns[0]);
940 bool live_pattern = (num_patterns > 0) && !(cu->disable_opt & (1 << kMatch));
941 bool* dead_pattern =
942 static_cast<bool*>(NewMem(cu.get(), sizeof(bool) * num_patterns, true, kAllocMisc));
943 SpecialCaseHandler special_case = kNoHandler;
944 int pattern_pos = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700945
946 /* Parse all instructions and put them into containing basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800947 while (code_ptr < code_end) {
948 MIR *insn = static_cast<MIR *>(NewMem(cu.get(), sizeof(MIR), true, kAllocMIR));
949 insn->offset = cur_offset;
buzbeea169e1d2012-12-05 14:26:44 -0800950 int width = ParseInsn(cu.get(), code_ptr, &insn->dalvikInsn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 insn->width = width;
952 Instruction::Code opcode = insn->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800953 if (cu->opcode_count != NULL) {
954 cu->opcode_count[static_cast<int>(opcode)]++;
buzbee44b412b2012-02-04 08:50:53 -0800955 }
956
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 /* Terminate when the data section is seen */
958 if (width == 0)
959 break;
960
961 /* Possible simple method? */
buzbeefa57c472012-11-21 12:06:18 -0800962 if (live_pattern) {
963 live_pattern = false;
964 special_case = kNoHandler;
965 for (int i = 0; i < num_patterns; i++) {
966 if (!dead_pattern[i]) {
967 if (special_patterns[i].opcodes[pattern_pos] == opcode) {
968 live_pattern = true;
969 special_case = special_patterns[i].handler_code;
Bill Buzbeea114add2012-05-03 15:00:40 -0700970 } else {
buzbeefa57c472012-11-21 12:06:18 -0800971 dead_pattern[i] = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 }
973 }
974 }
buzbeefa57c472012-11-21 12:06:18 -0800975 pattern_pos++;
buzbeea7c12682012-03-19 13:13:53 -0700976 }
977
buzbeefa57c472012-11-21 12:06:18 -0800978 AppendMIR(cur_block, insn);
buzbeecefd1872011-09-09 09:59:52 -0700979
buzbeefa57c472012-11-21 12:06:18 -0800980 code_ptr += width;
Ian Rogersa75a0132012-09-28 11:41:42 -0700981 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700982
buzbeefa57c472012-11-21 12:06:18 -0800983 int df_flags = oat_data_flow_attributes[insn->dalvikInsn.opcode];
buzbee67bf8852011-08-17 17:51:35 -0700984
buzbeefa57c472012-11-21 12:06:18 -0800985 if (df_flags & DF_HAS_DEFS) {
986 cu->def_count += (df_flags & DF_A_WIDE) ? 2 : 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700987 }
buzbee67bf8852011-08-17 17:51:35 -0700988
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 if (flags & Instruction::kBranch) {
buzbeefa57c472012-11-21 12:06:18 -0800990 cur_block = ProcessCanBranch(cu.get(), cur_block, insn, cur_offset,
991 width, flags, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -0700992 } else if (flags & Instruction::kReturn) {
buzbeebbdd0532013-02-07 09:33:02 -0800993 cur_block->terminated_by_return = true;
buzbeefa57c472012-11-21 12:06:18 -0800994 cur_block->fall_through = exit_block;
995 InsertGrowableList(cu.get(), exit_block->predecessors,
996 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700997 /*
998 * Terminate the current block if there are instructions
999 * afterwards.
1000 */
buzbeefa57c472012-11-21 12:06:18 -08001001 if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001002 /*
1003 * Create a fallthrough block for real instructions
1004 * (incl. NOP).
1005 */
buzbeefa57c472012-11-21 12:06:18 -08001006 if (ContentIsInsn(code_ptr)) {
1007 FindBlock(cu.get(), cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 /* split */
1009 false,
1010 /* create */
1011 true,
buzbeefa57c472012-11-21 12:06:18 -08001012 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001013 NULL);
1014 }
1015 }
1016 } else if (flags & Instruction::kThrow) {
buzbeefa57c472012-11-21 12:06:18 -08001017 cur_block = ProcessCanThrow(cu.get(), cur_block, insn, cur_offset,
1018 width, flags, try_block_addr, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 } else if (flags & Instruction::kSwitch) {
buzbeefa57c472012-11-21 12:06:18 -08001020 ProcessCanSwitch(cu.get(), cur_block, insn, cur_offset, width, flags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001021 }
buzbeefa57c472012-11-21 12:06:18 -08001022 cur_offset += width;
1023 BasicBlock *next_block = FindBlock(cu.get(), cur_offset,
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 /* split */
1025 false,
1026 /* create */
1027 false,
buzbeefa57c472012-11-21 12:06:18 -08001028 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001029 NULL);
buzbeefa57c472012-11-21 12:06:18 -08001030 if (next_block) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001031 /*
1032 * The next instruction could be the target of a previously parsed
1033 * forward branch so a block is already created. If the current
1034 * instruction is not an unconditional branch, connect them through
1035 * the fall-through link.
1036 */
buzbeefa57c472012-11-21 12:06:18 -08001037 DCHECK(cur_block->fall_through == NULL ||
1038 cur_block->fall_through == next_block ||
1039 cur_block->fall_through == exit_block);
buzbee5ade1d22011-09-09 14:44:52 -07001040
buzbeefa57c472012-11-21 12:06:18 -08001041 if ((cur_block->fall_through == NULL) && (flags & Instruction::kContinue)) {
1042 cur_block->fall_through = next_block;
1043 InsertGrowableList(cu.get(), next_block->predecessors,
1044 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 }
buzbeefa57c472012-11-21 12:06:18 -08001046 cur_block = next_block;
Bill Buzbeea114add2012-05-03 15:00:40 -07001047 }
1048 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001049
buzbeed8506212012-12-20 14:15:05 -08001050 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1051 DumpCFG(cu.get(), "/sdcard/1_post_parse_cfg/", true);
1052 }
1053
buzbeefa57c472012-11-21 12:06:18 -08001054 if (!(cu->disable_opt & (1 << kSkipLargeMethodOptimization))) {
1055 if ((cu->num_blocks > MANY_BLOCKS) ||
1056 ((cu->num_blocks > MANY_BLOCKS_INITIALIZER) &&
Bill Buzbeea114add2012-05-03 15:00:40 -07001057 PrettyMethod(method_idx, dex_file, false).find("init>") !=
1058 std::string::npos)) {
buzbeefa57c472012-11-21 12:06:18 -08001059 cu->qd_mode = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001060 }
1061 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001062
buzbeefa57c472012-11-21 12:06:18 -08001063 if (cu->qd_mode) {
buzbeed1643e42012-09-05 14:06:51 -07001064 // Bitcode generation requires full dataflow analysis
buzbeefa57c472012-11-21 12:06:18 -08001065 cu->disable_dataflow = !cu->gen_bitcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001066 // Disable optimization which require dataflow/ssa
buzbeefa57c472012-11-21 12:06:18 -08001067 cu->disable_opt |= (1 << kBBOpt) | (1 << kPromoteRegs) | (1 << kNullCheckElimination);
1068 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001069 LOG(INFO) << "QD mode enabled: "
1070 << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001071 << " num blocks: " << cu->num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -07001072 }
1073 }
buzbeec1f45042011-09-21 16:03:19 -07001074
buzbeefa57c472012-11-21 12:06:18 -08001075 if (cu->verbose) {
1076 DumpCompilationUnit(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001077 }
buzbee67bf8852011-08-17 17:51:35 -07001078
buzbee0967a252012-09-14 10:43:54 -07001079 /* Do a code layout pass */
buzbeefa57c472012-11-21 12:06:18 -08001080 CodeLayout(cu.get());
buzbee0967a252012-09-14 10:43:54 -07001081
buzbeed8506212012-12-20 14:15:05 -08001082 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1083 DumpCFG(cu.get(), "/sdcard/2_post_layout_cfg/", true);
1084 }
1085
buzbeefa57c472012-11-21 12:06:18 -08001086 if (cu->enable_debug & (1 << kDebugVerifyDataflow)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001087 /* Verify if all blocks are connected as claimed */
buzbeefa57c472012-11-21 12:06:18 -08001088 DataFlowAnalysisDispatcher(cu.get(), VerifyPredInfo, kAllNodes,
1089 false /* is_iterative */);
Bill Buzbeea114add2012-05-03 15:00:40 -07001090 }
buzbee67bf8852011-08-17 17:51:35 -07001091
Bill Buzbeea114add2012-05-03 15:00:40 -07001092 /* Perform SSA transformation for the whole method */
buzbeefa57c472012-11-21 12:06:18 -08001093 SSATransformation(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001094
buzbeed8506212012-12-20 14:15:05 -08001095 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1096 DumpCFG(cu.get(), "/sdcard/3_post_ssa_cfg/", false);
1097 }
1098
buzbee2cfc6392012-05-07 14:51:40 -07001099 /* Do constant propagation */
buzbee4ef3e452012-12-14 13:35:28 -08001100 cu->is_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs, false /* not expandable */);
1101 cu->must_flush_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs,
1102 false /* not expandable */);
buzbeefa57c472012-11-21 12:06:18 -08001103 cu->constant_values =
1104 static_cast<int*>(NewMem(cu.get(), sizeof(int) * cu->num_ssa_regs, true, kAllocDFInfo));
1105 DataFlowAnalysisDispatcher(cu.get(), DoConstantPropogation,
buzbee2cfc6392012-05-07 14:51:40 -07001106 kAllNodes,
buzbeefa57c472012-11-21 12:06:18 -08001107 false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -07001108
Bill Buzbeea114add2012-05-03 15:00:40 -07001109 /* Detect loops */
buzbeefa57c472012-11-21 12:06:18 -08001110 LoopDetection(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001111
Bill Buzbeea114add2012-05-03 15:00:40 -07001112 /* Count uses */
buzbeefa57c472012-11-21 12:06:18 -08001113 MethodUseCount(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001114
Bill Buzbeea114add2012-05-03 15:00:40 -07001115 /* Perform null check elimination */
buzbeefa57c472012-11-21 12:06:18 -08001116 NullCheckElimination(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001117
buzbeed8506212012-12-20 14:15:05 -08001118 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1119 DumpCFG(cu.get(), "/sdcard/4_post_nce_cfg/", false);
1120 }
1121
buzbeed1643e42012-09-05 14:06:51 -07001122 /* Combine basic blocks where possible */
buzbeefa57c472012-11-21 12:06:18 -08001123 BasicBlockCombine(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001124
buzbeed8506212012-12-20 14:15:05 -08001125 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1126 DumpCFG(cu.get(), "/sdcard/5_post_bbcombine_cfg/", false);
1127 }
1128
Bill Buzbeea114add2012-05-03 15:00:40 -07001129 /* Do some basic block optimizations */
buzbeefa57c472012-11-21 12:06:18 -08001130 BasicBlockOptimization(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001131
buzbeed8506212012-12-20 14:15:05 -08001132 // Debugging only
1133 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1134 DumpCFG(cu.get(), "/sdcard/6_post_bbo_cfg/", false);
1135 }
1136
buzbeefa57c472012-11-21 12:06:18 -08001137 if (cu->enable_debug & (1 << kDebugDumpCheckStats)) {
1138 DumpCheckStats(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001139 }
1140
buzbee02031b12012-11-23 09:41:35 -08001141 cu.get()->cg->CompilerInitializeRegAlloc(cu.get()); // Needs to happen after SSA naming
Bill Buzbeea114add2012-05-03 15:00:40 -07001142
1143 /* Allocate Registers using simple local allocation scheme */
buzbeefa57c472012-11-21 12:06:18 -08001144 SimpleRegAlloc(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001145
buzbee2502e002012-12-31 16:05:53 -08001146 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1147 DumpCFG(cu.get(), "/sdcard/7_post_ralloc_cfg/", true);
1148 }
1149
1150
Brian Carlstrom265091e2013-01-30 14:08:26 -08001151#if defined(ART_USE_PORTABLE_COMPILER)
buzbee2cfc6392012-05-07 14:51:40 -07001152 /* Go the LLVM path? */
buzbeefa57c472012-11-21 12:06:18 -08001153 if (cu->gen_bitcode) {
buzbee2cfc6392012-05-07 14:51:40 -07001154 // MIR->Bitcode
buzbeefa57c472012-11-21 12:06:18 -08001155 MethodMIR2Bitcode(cu.get());
1156 if (compiler_backend == kPortable) {
buzbeeabc4c6b2012-08-23 08:17:15 -07001157 // all done
buzbeefa57c472012-11-21 12:06:18 -08001158 ArenaReset(cu.get());
buzbeeabc4c6b2012-08-23 08:17:15 -07001159 return NULL;
1160 }
buzbee2cfc6392012-05-07 14:51:40 -07001161 // Bitcode->LIR
buzbeefa57c472012-11-21 12:06:18 -08001162 MethodBitcode2LIR(cu.get());
Brian Carlstrom265091e2013-01-30 14:08:26 -08001163 } else
1164#endif
1165 {
buzbeefa57c472012-11-21 12:06:18 -08001166 if (special_case != kNoHandler) {
buzbee2cfc6392012-05-07 14:51:40 -07001167 /*
1168 * Custom codegen for special cases. If for any reason the
buzbeefa57c472012-11-21 12:06:18 -08001169 * special codegen doesn't succeed, cu->first_lir_insn will
buzbee2cfc6392012-05-07 14:51:40 -07001170 * set to NULL;
1171 */
buzbeefa57c472012-11-21 12:06:18 -08001172 SpecialMIR2LIR(cu.get(), special_case);
buzbee2cfc6392012-05-07 14:51:40 -07001173 }
buzbee67bf8852011-08-17 17:51:35 -07001174
buzbee2cfc6392012-05-07 14:51:40 -07001175 /* Convert MIR to LIR, etc. */
buzbeefa57c472012-11-21 12:06:18 -08001176 if (cu->first_lir_insn == NULL) {
1177 MethodMIR2LIR(cu.get());
buzbee2cfc6392012-05-07 14:51:40 -07001178 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 }
buzbee67bf8852011-08-17 17:51:35 -07001180
Bill Buzbeea114add2012-05-03 15:00:40 -07001181 /* Method is not empty */
buzbeefa57c472012-11-21 12:06:18 -08001182 if (cu->first_lir_insn) {
buzbee67bf8852011-08-17 17:51:35 -07001183
Bill Buzbeea114add2012-05-03 15:00:40 -07001184 // mark the targets of switch statement case labels
buzbeefa57c472012-11-21 12:06:18 -08001185 ProcessSwitchTables(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001186
Bill Buzbeea114add2012-05-03 15:00:40 -07001187 /* Convert LIR into machine code. */
buzbeefa57c472012-11-21 12:06:18 -08001188 AssembleLIR(cu.get());
buzbee99ba9642012-01-25 14:23:14 -08001189
buzbeefa57c472012-11-21 12:06:18 -08001190 if (cu->verbose) {
1191 CodegenDump(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001192 }
1193
buzbeefa57c472012-11-21 12:06:18 -08001194 if (cu->opcode_count != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001195 LOG(INFO) << "Opcode Count";
1196 for (int i = 0; i < kNumPackedOpcodes; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001197 if (cu->opcode_count[i] != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001198 LOG(INFO) << "-C- "
1199 << Instruction::Name(static_cast<Instruction::Code>(i))
buzbeefa57c472012-11-21 12:06:18 -08001200 << " " << cu->opcode_count[i];
buzbee67bf8852011-08-17 17:51:35 -07001201 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001202 }
1203 }
1204 }
buzbeea7c12682012-03-19 13:13:53 -07001205
buzbeefa57c472012-11-21 12:06:18 -08001206 // Combine vmap tables - core regs, then fp regs - into vmap_table
1207 std::vector<uint16_t> vmap_table;
buzbeeca7a5e42012-08-20 11:12:18 -07001208 // Core regs may have been inserted out of order - sort first
buzbeefa57c472012-11-21 12:06:18 -08001209 std::sort(cu->core_vmap_table.begin(), cu->core_vmap_table.end());
1210 for (size_t i = 0 ; i < cu->core_vmap_table.size(); i++) {
buzbeeca7a5e42012-08-20 11:12:18 -07001211 // Copy, stripping out the phys register sort key
buzbeefa57c472012-11-21 12:06:18 -08001212 vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & cu->core_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001213 }
1214 // If we have a frame, push a marker to take place of lr
buzbeefa57c472012-11-21 12:06:18 -08001215 if (cu->frame_size > 0) {
1216 vmap_table.push_back(INVALID_VREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001217 } else {
buzbeefa57c472012-11-21 12:06:18 -08001218 DCHECK_EQ(__builtin_popcount(cu->core_spill_mask), 0);
1219 DCHECK_EQ(__builtin_popcount(cu->fp_spill_mask), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001220 }
buzbeeca7a5e42012-08-20 11:12:18 -07001221 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
buzbeefa57c472012-11-21 12:06:18 -08001222 for (uint32_t i = 0; i < cu->fp_vmap_table.size(); i++) {
1223 vmap_table.push_back(cu->fp_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 }
1225 CompiledMethod* result =
buzbeefa57c472012-11-21 12:06:18 -08001226 new CompiledMethod(cu->instruction_set, cu->code_buffer,
1227 cu->frame_size, cu->core_spill_mask, cu->fp_spill_mask,
1228 cu->combined_mapping_table, vmap_table, cu->native_gc_map);
buzbee67bf8852011-08-17 17:51:35 -07001229
Bill Buzbeea114add2012-05-03 15:00:40 -07001230 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001231 << " (" << (cu->code_buffer.size() * sizeof(cu->code_buffer[0]))
Bill Buzbeea114add2012-05-03 15:00:40 -07001232 << " bytes)";
buzbee5abfa3e2012-01-31 17:01:43 -08001233
1234#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -08001235 if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) {
1236 DumpMemStats(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001237 }
buzbee5abfa3e2012-01-31 17:01:43 -08001238#endif
buzbee67bf8852011-08-17 17:51:35 -07001239
buzbeefa57c472012-11-21 12:06:18 -08001240 ArenaReset(cu.get());
buzbeeba938cb2012-02-03 14:47:55 -08001241
Bill Buzbeea114add2012-05-03 15:00:40 -07001242 return result;
buzbee67bf8852011-08-17 17:51:35 -07001243}
1244
Ian Rogers1212a022013-03-04 10:48:41 -08001245CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001246 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -07001247 const DexFile::CodeItem* code_item,
Brian Carlstrom265091e2013-01-30 14:08:26 -08001248 uint32_t access_flags,
1249 InvokeType invoke_type,
1250 uint32_t class_def_idx,
1251 uint32_t method_idx,
1252 jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001253 const DexFile& dex_file,
Brian Carlstrom265091e2013-01-30 14:08:26 -08001254 llvm::LlvmCompilationUnit* llvm_compilation_unit)
buzbeeabc4c6b2012-08-23 08:17:15 -07001255{
Ian Rogersfffdb022013-01-04 15:14:08 -08001256 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -08001257 method_idx, class_loader, dex_file
1258#if defined(ART_USE_PORTABLE_COMPILER)
1259 , llvm_compilation_unit
1260#endif
1261 );
buzbeeabc4c6b2012-08-23 08:17:15 -07001262}
1263
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001264} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001265
Bill Buzbeea114add2012-05-03 15:00:40 -07001266extern "C" art::CompiledMethod*
Ian Rogers1212a022013-03-04 10:48:41 -08001267 ArtQuickCompileMethod(art::CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001268 const art::DexFile::CodeItem* code_item,
1269 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001270 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001271 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001272{
buzbeec531cef2012-10-18 07:09:20 -07001273 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
1274 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -08001275 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001276 class_def_idx, method_idx, class_loader, dex_file,
1277 NULL /* use thread llvm_info */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001278}