blob: c0f3316720f628ba1bd404466fc3d967e7427dcb [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
buzbeeeaf09bc2012-11-15 14:51:41 -080017#include "compiler.h"
buzbeeefc63692012-11-14 16:31:52 -080018#include "compiler_internals.h"
19#include "dataflow.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080020#include "ssa_transformation.h"
Ian Rogers0571d352011-11-03 19:51:38 -070021#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "runtime.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080024#include "codegen/codegen_util.h"
buzbee02031b12012-11-23 09:41:35 -080025#include "codegen/mir_to_gbc.h"
26#include "codegen/mir_to_lir.h"
buzbee67bf8852011-08-17 17:51:35 -070027
buzbee4df2bbd2012-10-11 14:46:06 -070028#include <llvm/Support/Threading.h>
29
30namespace {
Ian Rogersc928de92013-02-27 14:30:44 -080031#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070032 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
Shih-wei Liao215a9262012-10-12 10:29:46 -070033#endif
buzbee4df2bbd2012-10-11 14:46:06 -070034 void InitializeLLVMForQuick() {
35 llvm::llvm_start_multithreaded();
36 }
37}
buzbee4df2bbd2012-10-11 14:46:06 -070038
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080039namespace art {
40
buzbee4df2bbd2012-10-11 14:46:06 -070041LLVMInfo::LLVMInfo() {
Ian Rogersc928de92013-02-27 14:30:44 -080042#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070043 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
44#endif
buzbee692be802012-08-29 15:52:59 -070045 // Create context, module, intrinsic helper & ir builder
46 llvm_context_.reset(new llvm::LLVMContext());
TDYa12755e5e6c2012-09-11 15:14:42 -070047 llvm_module_ = new llvm::Module("art", *llvm_context_);
buzbee692be802012-08-29 15:52:59 -070048 llvm::StructType::create(*llvm_context_, "JavaObject");
buzbee692be802012-08-29 15:52:59 -070049 intrinsic_helper_.reset( new greenland::IntrinsicHelper(*llvm_context_, *llvm_module_));
50 ir_builder_.reset(new greenland::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
51}
52
buzbee4df2bbd2012-10-11 14:46:06 -070053LLVMInfo::~LLVMInfo() {
buzbee692be802012-08-29 15:52:59 -070054}
55
56extern "C" void ArtInitQuickCompilerContext(art::Compiler& compiler) {
57 CHECK(compiler.GetCompilerContext() == NULL);
buzbeefa57c472012-11-21 12:06:18 -080058 LLVMInfo* llvm_info = new LLVMInfo();
59 compiler.SetCompilerContext(llvm_info);
buzbee692be802012-08-29 15:52:59 -070060}
61
62extern "C" void ArtUnInitQuickCompilerContext(art::Compiler& compiler) {
buzbee4df2bbd2012-10-11 14:46:06 -070063 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
buzbee692be802012-08-29 15:52:59 -070064 compiler.SetCompilerContext(NULL);
65}
buzbee692be802012-08-29 15:52:59 -070066
buzbeece302932011-10-04 14:32:18 -070067/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070068static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee4ef3e452012-12-14 13:35:28 -080069 (1 << kLoadStoreElimination) |
Bill Buzbeea114add2012-05-03 15:00:40 -070070 //(1 << kLoadHoisting) |
71 //(1 << kSuppressLoads) |
72 //(1 << kNullCheckElimination) |
73 //(1 << kPromoteRegs) |
74 //(1 << kTrackLiveTemps) |
buzbee4ef3e452012-12-14 13:35:28 -080075 (1 << kSkipLargeMethodOptimization) |
Bill Buzbeea114add2012-05-03 15:00:40 -070076 //(1 << kSafeOptimizations) |
77 //(1 << kBBOpt) |
78 //(1 << kMatch) |
79 //(1 << kPromoteCompilerTemps) |
80 0;
buzbeece302932011-10-04 14:32:18 -070081
Elliott Hughese52e49b2012-04-02 16:05:44 -070082static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Bill Buzbeea114add2012-05-03 15:00:40 -070083 //(1 << kDebugDisplayMissingTargets) |
84 //(1 << kDebugVerbose) |
85 //(1 << kDebugDumpCFG) |
86 //(1 << kDebugSlowFieldPath) |
87 //(1 << kDebugSlowInvokePath) |
88 //(1 << kDebugSlowStringPath) |
89 //(1 << kDebugSlowestFieldPath) |
90 //(1 << kDebugSlowestStringPath) |
91 //(1 << kDebugExerciseResolveMethod) |
92 //(1 << kDebugVerifyDataflow) |
93 //(1 << kDebugShowMemoryUsage) |
94 //(1 << kDebugShowNops) |
95 //(1 << kDebugCountOpcodes) |
buzbeed1643e42012-09-05 14:06:51 -070096 //(1 << kDebugDumpCheckStats) |
buzbeead8f15e2012-06-18 14:49:45 -070097 //(1 << kDebugDumpBitcodeFile) |
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070098 //(1 << kDebugVerifyBitcode) |
Bill Buzbeea114add2012-05-03 15:00:40 -070099 0;
buzbeece302932011-10-04 14:32:18 -0700100
buzbeefa57c472012-11-21 12:06:18 -0800101static bool ContentIsInsn(const uint16_t* code_ptr) {
102 uint16_t instr = *code_ptr;
buzbeecbd6d442012-11-17 14:11:25 -0800103 Instruction::Code opcode = static_cast<Instruction::Code>(instr & 0xff);
buzbee67bf8852011-08-17 17:51:35 -0700104
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 /*
106 * Since the low 8-bit in metadata may look like NOP, we need to check
107 * both the low and whole sub-word to determine whether it is code or data.
108 */
109 return (opcode != Instruction::NOP || instr == 0);
buzbee67bf8852011-08-17 17:51:35 -0700110}
111
112/*
113 * Parse an instruction, return the length of the instruction
114 */
buzbeefa57c472012-11-21 12:06:18 -0800115static int ParseInsn(CompilationUnit* cu, const uint16_t* code_ptr,
buzbeea169e1d2012-12-05 14:26:44 -0800116 DecodedInstruction* decoded_instruction)
buzbee67bf8852011-08-17 17:51:35 -0700117{
Elliott Hughesadb8c672012-03-06 16:49:32 -0800118 // Don't parse instruction data
buzbeefa57c472012-11-21 12:06:18 -0800119 if (!ContentIsInsn(code_ptr)) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800120 return 0;
121 }
buzbee67bf8852011-08-17 17:51:35 -0700122
buzbeefa57c472012-11-21 12:06:18 -0800123 const Instruction* instruction = Instruction::At(code_ptr);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800124 *decoded_instruction = DecodedInstruction(instruction);
buzbee67bf8852011-08-17 17:51:35 -0700125
Elliott Hughesadb8c672012-03-06 16:49:32 -0800126 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -0700127}
128
129#define UNKNOWN_TARGET 0xffffffff
130
buzbee67bf8852011-08-17 17:51:35 -0700131/* Split an existing block from the specified code offset into two */
buzbeefa57c472012-11-21 12:06:18 -0800132static BasicBlock *SplitBlock(CompilationUnit* cu, unsigned int code_offset,
133 BasicBlock* orig_block, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700134{
buzbeefa57c472012-11-21 12:06:18 -0800135 MIR* insn = orig_block->first_mir_insn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 while (insn) {
buzbeefa57c472012-11-21 12:06:18 -0800137 if (insn->offset == code_offset) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700138 insn = insn->next;
139 }
140 if (insn == NULL) {
141 LOG(FATAL) << "Break split failed";
142 }
buzbeefa57c472012-11-21 12:06:18 -0800143 BasicBlock *bottom_block = NewMemBB(cu, kDalvikByteCode,
144 cu->num_blocks++);
145 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700146
buzbeefa57c472012-11-21 12:06:18 -0800147 bottom_block->start_offset = code_offset;
148 bottom_block->first_mir_insn = insn;
149 bottom_block->last_mir_insn = orig_block->last_mir_insn;
buzbee67bf8852011-08-17 17:51:35 -0700150
buzbeebbdd0532013-02-07 09:33:02 -0800151 /* If this block was terminated by a return, the flag needs to go with the bottom block */
152 bottom_block->terminated_by_return = orig_block->terminated_by_return;
153 orig_block->terminated_by_return = false;
154
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 /* Add it to the quick lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800156 cu->block_map.Put(bottom_block->start_offset, bottom_block);
buzbee5b537102012-01-17 17:33:47 -0800157
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 /* Handle the taken path */
buzbeefa57c472012-11-21 12:06:18 -0800159 bottom_block->taken = orig_block->taken;
160 if (bottom_block->taken) {
161 orig_block->taken = NULL;
162 DeleteGrowableList(bottom_block->taken->predecessors, reinterpret_cast<uintptr_t>(orig_block));
163 InsertGrowableList(cu, bottom_block->taken->predecessors,
164 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 }
166
167 /* Handle the fallthrough path */
buzbeefa57c472012-11-21 12:06:18 -0800168 bottom_block->fall_through = orig_block->fall_through;
169 orig_block->fall_through = bottom_block;
170 InsertGrowableList(cu, bottom_block->predecessors,
171 reinterpret_cast<uintptr_t>(orig_block));
172 if (bottom_block->fall_through) {
173 DeleteGrowableList(bottom_block->fall_through->predecessors,
174 reinterpret_cast<uintptr_t>(orig_block));
175 InsertGrowableList(cu, bottom_block->fall_through->predecessors,
176 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 }
178
179 /* Handle the successor list */
buzbeefa57c472012-11-21 12:06:18 -0800180 if (orig_block->successor_block_list.block_list_type != kNotUsed) {
181 bottom_block->successor_block_list = orig_block->successor_block_list;
182 orig_block->successor_block_list.block_list_type = kNotUsed;
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 GrowableListIterator iterator;
184
buzbeefa57c472012-11-21 12:06:18 -0800185 GrowableListIteratorInit(&bottom_block->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 &iterator);
187 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800188 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800189 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800190 if (successor_block_info == NULL) break;
191 BasicBlock *bb = successor_block_info->block;
192 DeleteGrowableList(bb->predecessors, reinterpret_cast<uintptr_t>(orig_block));
193 InsertGrowableList(cu, bb->predecessors, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700194 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 }
buzbee67bf8852011-08-17 17:51:35 -0700196
buzbeefa57c472012-11-21 12:06:18 -0800197 orig_block->last_mir_insn = insn->prev;
buzbee67bf8852011-08-17 17:51:35 -0700198
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 insn->prev->next = NULL;
200 insn->prev = NULL;
201 /*
202 * Update the immediate predecessor block pointer so that outgoing edges
203 * can be applied to the proper block.
204 */
buzbeefa57c472012-11-21 12:06:18 -0800205 if (immed_pred_block_p) {
206 DCHECK_EQ(*immed_pred_block_p, orig_block);
207 *immed_pred_block_p = bottom_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 }
buzbeefa57c472012-11-21 12:06:18 -0800209 return bottom_block;
buzbee67bf8852011-08-17 17:51:35 -0700210}
211
212/*
213 * Given a code offset, find out the block that starts with it. If the offset
buzbeefa57c472012-11-21 12:06:18 -0800214 * is in the middle of an existing block, split it into two. If immed_pred_block_p
215 * is not non-null and is the block being split, update *immed_pred_block_p to
buzbee9ab05de2012-01-18 15:43:48 -0800216 * point to the bottom block so that outgoing edges can be set up properly
217 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800218 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700219 */
buzbeefa57c472012-11-21 12:06:18 -0800220BasicBlock *FindBlock(CompilationUnit* cu, unsigned int code_offset,
221 bool split, bool create, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700222{
buzbeefa57c472012-11-21 12:06:18 -0800223 GrowableList* block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 BasicBlock* bb;
225 unsigned int i;
226 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700227
buzbeefa57c472012-11-21 12:06:18 -0800228 it = cu->block_map.find(code_offset);
229 if (it != cu->block_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 return it->second;
231 } else if (!create) {
232 return NULL;
233 }
234
235 if (split) {
buzbeefa57c472012-11-21 12:06:18 -0800236 for (i = 0; i < block_list->num_used; i++) {
237 bb = reinterpret_cast<BasicBlock*>(block_list->elem_list[i]);
238 if (bb->block_type != kDalvikByteCode) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 /* Check if a branch jumps into the middle of an existing block */
buzbeefa57c472012-11-21 12:06:18 -0800240 if ((code_offset > bb->start_offset) && (bb->last_mir_insn != NULL) &&
241 (code_offset <= bb->last_mir_insn->offset)) {
242 BasicBlock *new_bb = SplitBlock(cu, code_offset, bb,
243 bb == *immed_pred_block_p ?
244 immed_pred_block_p : NULL);
245 return new_bb;
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 }
buzbee5b537102012-01-17 17:33:47 -0800247 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 }
buzbee5b537102012-01-17 17:33:47 -0800249
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 /* Create a new one */
buzbeefa57c472012-11-21 12:06:18 -0800251 bb = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
252 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bb));
253 bb->start_offset = code_offset;
254 cu->block_map.Put(bb->start_offset, bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700256}
257
buzbeef58c12c2012-07-03 15:06:29 -0700258/* Find existing block */
buzbeefa57c472012-11-21 12:06:18 -0800259BasicBlock* FindBlock(CompilationUnit* cu, unsigned int code_offset)
buzbeef58c12c2012-07-03 15:06:29 -0700260{
buzbeefa57c472012-11-21 12:06:18 -0800261 return FindBlock(cu, code_offset, false, false, NULL);
buzbeef58c12c2012-07-03 15:06:29 -0700262}
263
buzbeead8f15e2012-06-18 14:49:45 -0700264/* Turn method name into a legal Linux file name */
buzbee52a77fc2012-11-20 19:50:46 -0800265void ReplaceSpecialChars(std::string& str)
buzbeead8f15e2012-06-18 14:49:45 -0700266{
267 static const struct { const char before; const char after; } match[] =
268 {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'},
269 {'(','@'}, {')','@'}, {'<','='}, {'>','='}};
270 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
271 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
272 }
273}
274
buzbee67bf8852011-08-17 17:51:35 -0700275/* Dump the CFG into a DOT graph */
buzbeed8506212012-12-20 14:15:05 -0800276void DumpCFG(CompilationUnit* cu, const char* dir_prefix, bool all_blocks)
buzbee67bf8852011-08-17 17:51:35 -0700277{
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 FILE* file;
buzbeefa57c472012-11-21 12:06:18 -0800279 std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -0800280 ReplaceSpecialChars(fname);
buzbeefa57c472012-11-21 12:06:18 -0800281 fname = StringPrintf("%s%s%x.dot", dir_prefix, fname.c_str(),
282 cu->entry_block->fall_through->start_offset);
buzbeead8f15e2012-06-18 14:49:45 -0700283 file = fopen(fname.c_str(), "w");
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 if (file == NULL) {
285 return;
286 }
287 fprintf(file, "digraph G {\n");
288
289 fprintf(file, " rankdir=TB\n");
290
buzbeed8506212012-12-20 14:15:05 -0800291 int num_blocks = all_blocks ? cu->num_blocks : cu->num_reachable_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 int idx;
buzbeefa57c472012-11-21 12:06:18 -0800293 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700294
buzbeed8506212012-12-20 14:15:05 -0800295 for (idx = 0; idx < num_blocks; idx++) {
296 int block_idx = all_blocks ? idx : cu->dfs_order.elem_list[idx];
buzbeefa57c472012-11-21 12:06:18 -0800297 BasicBlock *bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, block_idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -0800299 if (bb->block_type == kDead) continue;
300 if (bb->block_type == kEntryBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700301 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800302 } else if (bb->block_type == kExitBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700303 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800304 } else if (bb->block_type == kDalvikByteCode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700305 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800306 bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 const MIR *mir;
308 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
buzbeefa57c472012-11-21 12:06:18 -0800309 bb->first_mir_insn ? " | " : " ");
310 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
buzbeed8506212012-12-20 14:15:05 -0800311 int opcode = mir->dalvikInsn.opcode;
312 fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset,
buzbeea169e1d2012-12-05 14:26:44 -0800313 mir->ssa_rep ? GetDalvikDisassembly(cu, mir) :
buzbeed8506212012-12-20 14:15:05 -0800314 (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) :
315 extended_mir_op_names[opcode - kMirOpFirst],
316 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
317 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 mir->next ? " | " : " ");
319 }
320 fprintf(file, " }\"];\n\n");
buzbeefa57c472012-11-21 12:06:18 -0800321 } else if (bb->block_type == kExceptionHandling) {
322 char block_name[BLOCK_NAME_LEN];
Bill Buzbeea114add2012-05-03 15:00:40 -0700323
buzbeefa57c472012-11-21 12:06:18 -0800324 GetBlockName(bb, block_name);
325 fprintf(file, " %s [shape=invhouse];\n", block_name);
buzbee67bf8852011-08-17 17:51:35 -0700326 }
buzbee67bf8852011-08-17 17:51:35 -0700327
buzbeefa57c472012-11-21 12:06:18 -0800328 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
buzbee67bf8852011-08-17 17:51:35 -0700329
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 if (bb->taken) {
buzbeefa57c472012-11-21 12:06:18 -0800331 GetBlockName(bb, block_name1);
332 GetBlockName(bb->taken, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
buzbeefa57c472012-11-21 12:06:18 -0800334 block_name1, block_name2);
buzbee67bf8852011-08-17 17:51:35 -0700335 }
buzbeefa57c472012-11-21 12:06:18 -0800336 if (bb->fall_through) {
337 GetBlockName(bb, block_name1);
338 GetBlockName(bb->fall_through, block_name2);
339 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700340 }
341
buzbeefa57c472012-11-21 12:06:18 -0800342 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700343 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800344 bb->start_offset, bb->id,
345 (bb->successor_block_list.block_list_type == kCatch) ?
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 "Mrecord" : "record");
347 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800348 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 &iterator);
buzbeefa57c472012-11-21 12:06:18 -0800350 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800351 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700352
buzbeefa57c472012-11-21 12:06:18 -0800353 int succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800355 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700356
buzbeefa57c472012-11-21 12:06:18 -0800357 BasicBlock *dest_block = successor_block_info->block;
358 SuccessorBlockInfo *next_successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800359 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700360
361 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
buzbeefa57c472012-11-21 12:06:18 -0800362 succ_id++,
363 successor_block_info->key,
364 dest_block->start_offset,
365 (next_successor_block_info != NULL) ? " | " : " ");
Bill Buzbeea114add2012-05-03 15:00:40 -0700366
buzbeefa57c472012-11-21 12:06:18 -0800367 successor_block_info = next_successor_block_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 }
369 fprintf(file, " }\"];\n\n");
370
buzbeefa57c472012-11-21 12:06:18 -0800371 GetBlockName(bb, block_name1);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700372 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
buzbeefa57c472012-11-21 12:06:18 -0800373 block_name1, bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700374
buzbeefa57c472012-11-21 12:06:18 -0800375 if (bb->successor_block_list.block_list_type == kPackedSwitch ||
376 bb->successor_block_list.block_list_type == kSparseSwitch) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700377
buzbeefa57c472012-11-21 12:06:18 -0800378 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 &iterator);
380
buzbeefa57c472012-11-21 12:06:18 -0800381 succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800383 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800384 reinterpret_cast<SuccessorBlockInfo*>( GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800385 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700386
buzbeefa57c472012-11-21 12:06:18 -0800387 BasicBlock *dest_block = successor_block_info->block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700388
buzbeefa57c472012-11-21 12:06:18 -0800389 GetBlockName(dest_block, block_name2);
390 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
391 bb->id, succ_id++, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 }
393 }
394 }
395 fprintf(file, "\n");
396
buzbeed8506212012-12-20 14:15:05 -0800397 if (cu->verbose) {
398 /* Display the dominator tree */
399 GetBlockName(bb, block_name1);
400 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
401 block_name1, block_name1);
402 if (bb->i_dom) {
403 GetBlockName(bb->i_dom, block_name2);
404 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
405 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700406 }
407 }
408 fprintf(file, "}\n");
409 fclose(file);
buzbee67bf8852011-08-17 17:51:35 -0700410}
411
412/* Verify if all the successor is connected with all the claimed predecessors */
buzbeefa57c472012-11-21 12:06:18 -0800413static bool VerifyPredInfo(CompilationUnit* cu, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700414{
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700416
buzbee52a77fc2012-11-20 19:50:46 -0800417 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800419 BasicBlock *pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
420 if (!pred_bb) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800422 if (pred_bb->taken == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800424 } else if (pred_bb->fall_through == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800426 } else if (pred_bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800428 GrowableListIteratorInit(&pred_bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 &iterator);
430 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800431 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800432 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800433 if (successor_block_info == NULL) break;
434 BasicBlock *succ_bb = successor_block_info->block;
435 if (succ_bb == bb) {
buzbee67bf8852011-08-17 17:51:35 -0700436 found = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 break;
buzbee67bf8852011-08-17 17:51:35 -0700438 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 }
buzbee67bf8852011-08-17 17:51:35 -0700440 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 if (found == false) {
buzbeefa57c472012-11-21 12:06:18 -0800442 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
443 GetBlockName(bb, block_name1);
444 GetBlockName(pred_bb, block_name2);
buzbeed8506212012-12-20 14:15:05 -0800445 DumpCFG(cu, "/sdcard/cfg/", false);
buzbeefa57c472012-11-21 12:06:18 -0800446 LOG(FATAL) << "Successor " << block_name1 << "not found from "
447 << block_name2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 }
449 }
450 return true;
buzbee67bf8852011-08-17 17:51:35 -0700451}
452
453/* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800454static void ProcessTryCatchBlocks(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700455{
buzbeefa57c472012-11-21 12:06:18 -0800456 const DexFile::CodeItem* code_item = cu->code_item;
457 int tries_size = code_item->tries_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 int offset;
buzbee67bf8852011-08-17 17:51:35 -0700459
buzbeefa57c472012-11-21 12:06:18 -0800460 if (tries_size == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 return;
462 }
463
buzbeefa57c472012-11-21 12:06:18 -0800464 ArenaBitVector* try_block_addr = cu->try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700465
buzbeefa57c472012-11-21 12:06:18 -0800466 for (int i = 0; i < tries_size; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700467 const DexFile::TryItem* pTry =
468 DexFile::GetTryItems(*code_item, i);
buzbeefa57c472012-11-21 12:06:18 -0800469 int start_offset = pTry->start_addr_;
470 int end_offset = start_offset + pTry->insn_count_;
471 for (offset = start_offset; offset < end_offset; offset++) {
472 SetBit(cu, try_block_addr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700473 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 }
buzbee67bf8852011-08-17 17:51:35 -0700475
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 // Iterate over each of the handlers to enqueue the empty Catch blocks
477 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
478 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
479 for (uint32_t idx = 0; idx < handlers_size; idx++) {
480 CatchHandlerIterator iterator(handlers_ptr);
481 for (; iterator.HasNext(); iterator.Next()) {
482 uint32_t address = iterator.GetHandlerAddress();
buzbeefa57c472012-11-21 12:06:18 -0800483 FindBlock(cu, address, false /* split */, true /*create*/,
484 /* immed_pred_block_p */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700485 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 handlers_ptr = iterator.EndDataPointer();
487 }
buzbee67bf8852011-08-17 17:51:35 -0700488}
489
Elliott Hughesadb8c672012-03-06 16:49:32 -0800490/* Process instructions with the kBranch flag */
buzbeefa57c472012-11-21 12:06:18 -0800491static BasicBlock* ProcessCanBranch(CompilationUnit* cu, BasicBlock* cur_block,
492 MIR* insn, int cur_offset, int width, int flags,
493 const uint16_t* code_ptr, const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700494{
buzbeefa57c472012-11-21 12:06:18 -0800495 int target = cur_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 switch (insn->dalvikInsn.opcode) {
497 case Instruction::GOTO:
498 case Instruction::GOTO_16:
499 case Instruction::GOTO_32:
buzbeecbd6d442012-11-17 14:11:25 -0800500 target += insn->dalvikInsn.vA;
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 break;
502 case Instruction::IF_EQ:
503 case Instruction::IF_NE:
504 case Instruction::IF_LT:
505 case Instruction::IF_GE:
506 case Instruction::IF_GT:
507 case Instruction::IF_LE:
buzbeefa57c472012-11-21 12:06:18 -0800508 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800509 target += insn->dalvikInsn.vC;
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 break;
511 case Instruction::IF_EQZ:
512 case Instruction::IF_NEZ:
513 case Instruction::IF_LTZ:
514 case Instruction::IF_GEZ:
515 case Instruction::IF_GTZ:
516 case Instruction::IF_LEZ:
buzbeefa57c472012-11-21 12:06:18 -0800517 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800518 target += insn->dalvikInsn.vB;
Bill Buzbeea114add2012-05-03 15:00:40 -0700519 break;
520 default:
buzbeecbd6d442012-11-17 14:11:25 -0800521 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 }
buzbeefa57c472012-11-21 12:06:18 -0800523 BasicBlock *taken_block = FindBlock(cu, target,
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 /* split */
525 true,
526 /* create */
527 true,
buzbeefa57c472012-11-21 12:06:18 -0800528 /* immed_pred_block_p */
529 &cur_block);
530 cur_block->taken = taken_block;
531 InsertGrowableList(cu, taken_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700532
Bill Buzbeea114add2012-05-03 15:00:40 -0700533 /* Always terminate the current block for conditional branches */
534 if (flags & Instruction::kContinue) {
buzbeefa57c472012-11-21 12:06:18 -0800535 BasicBlock *fallthrough_block = FindBlock(cu,
536 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 /*
538 * If the method is processed
539 * in sequential order from the
540 * beginning, we don't need to
541 * specify split for continue
542 * blocks. However, this
543 * routine can be called by
544 * compileLoop, which starts
545 * parsing the method from an
546 * arbitrary address in the
547 * method body.
548 */
549 true,
550 /* create */
551 true,
buzbeefa57c472012-11-21 12:06:18 -0800552 /* immed_pred_block_p */
553 &cur_block);
554 cur_block->fall_through = fallthrough_block;
555 InsertGrowableList(cu, fallthrough_block->predecessors,
556 reinterpret_cast<uintptr_t>(cur_block));
557 } else if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700558 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbeefa57c472012-11-21 12:06:18 -0800559 if (ContentIsInsn(code_ptr)) {
560 FindBlock(cu, cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 /* split */
562 false,
563 /* create */
564 true,
buzbeefa57c472012-11-21 12:06:18 -0800565 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700567 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 }
buzbeefa57c472012-11-21 12:06:18 -0800569 return cur_block;
buzbee67bf8852011-08-17 17:51:35 -0700570}
571
Elliott Hughesadb8c672012-03-06 16:49:32 -0800572/* Process instructions with the kSwitch flag */
buzbeefa57c472012-11-21 12:06:18 -0800573static void ProcessCanSwitch(CompilationUnit* cu, BasicBlock* cur_block,
574 MIR* insn, int cur_offset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700575{
buzbeefa57c472012-11-21 12:06:18 -0800576 const uint16_t* switch_data =
577 reinterpret_cast<const uint16_t*>(cu->insns + cur_offset + insn->dalvikInsn.vB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 int size;
buzbeecbd6d442012-11-17 14:11:25 -0800579 const int* keyTable;
buzbeefa57c472012-11-21 12:06:18 -0800580 const int* target_table;
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 int i;
buzbeefa57c472012-11-21 12:06:18 -0800582 int first_key;
buzbee67bf8852011-08-17 17:51:35 -0700583
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 /*
585 * Packed switch data format:
586 * ushort ident = 0x0100 magic value
587 * ushort size number of entries in the table
588 * int first_key first (and lowest) switch case value
589 * int targets[size] branch targets, relative to switch opcode
590 *
591 * Total size is (4+size*2) 16-bit code units.
592 */
593 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
buzbeefa57c472012-11-21 12:06:18 -0800594 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800596 size = switch_data[1];
597 first_key = switch_data[2] | (switch_data[3] << 16);
598 target_table = reinterpret_cast<const int*>(&switch_data[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 keyTable = NULL; // Make the compiler happy
600 /*
601 * Sparse switch data format:
602 * ushort ident = 0x0200 magic value
603 * ushort size number of entries in the table; > 0
604 * int keys[size] keys, sorted low-to-high; 32-bit aligned
605 * int targets[size] branch targets, relative to switch opcode
606 *
607 * Total size is (2+size*4) 16-bit code units.
608 */
609 } else {
buzbeefa57c472012-11-21 12:06:18 -0800610 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 static_cast<int>(Instruction::kSparseSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800612 size = switch_data[1];
613 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
614 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
615 first_key = 0; // To make the compiler happy
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 }
buzbee67bf8852011-08-17 17:51:35 -0700617
buzbeefa57c472012-11-21 12:06:18 -0800618 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800620 << static_cast<int>(cur_block->successor_block_list.block_list_type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700621 }
buzbeefa57c472012-11-21 12:06:18 -0800622 cur_block->successor_block_list.block_list_type =
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
624 kPackedSwitch : kSparseSwitch;
buzbeefa57c472012-11-21 12:06:18 -0800625 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 kListSuccessorBlocks);
627
628 for (i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800629 BasicBlock *case_block = FindBlock(cu, cur_offset + target_table[i],
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 /* split */
631 true,
632 /* create */
633 true,
buzbeefa57c472012-11-21 12:06:18 -0800634 /* immed_pred_block_p */
635 &cur_block);
636 SuccessorBlockInfo *successor_block_info =
637 static_cast<SuccessorBlockInfo*>(NewMem(cu, sizeof(SuccessorBlockInfo),
buzbeecbd6d442012-11-17 14:11:25 -0800638 false, kAllocSuccessor));
buzbeefa57c472012-11-21 12:06:18 -0800639 successor_block_info->block = case_block;
640 successor_block_info->key =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800641 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
buzbeefa57c472012-11-21 12:06:18 -0800642 first_key + i : keyTable[i];
643 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
644 reinterpret_cast<uintptr_t>(successor_block_info));
645 InsertGrowableList(cu, case_block->predecessors,
646 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 }
648
649 /* Fall-through case */
buzbeefa57c472012-11-21 12:06:18 -0800650 BasicBlock* fallthrough_block = FindBlock(cu,
651 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 /* split */
653 false,
654 /* create */
655 true,
buzbeefa57c472012-11-21 12:06:18 -0800656 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 NULL);
buzbeefa57c472012-11-21 12:06:18 -0800658 cur_block->fall_through = fallthrough_block;
659 InsertGrowableList(cu, fallthrough_block->predecessors,
660 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700661}
662
Elliott Hughesadb8c672012-03-06 16:49:32 -0800663/* Process instructions with the kThrow flag */
buzbeefa57c472012-11-21 12:06:18 -0800664static BasicBlock* ProcessCanThrow(CompilationUnit* cu, BasicBlock* cur_block,
665 MIR* insn, int cur_offset, int width, int flags,
666 ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
667 const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700668{
buzbeefa57c472012-11-21 12:06:18 -0800669 const DexFile::CodeItem* code_item = cu->code_item;
670 bool in_try_block = IsBitSet(try_block_addr, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700671
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 /* In try block */
buzbeefa57c472012-11-21 12:06:18 -0800673 if (in_try_block) {
674 CatchHandlerIterator iterator(*code_item, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700675
buzbeefa57c472012-11-21 12:06:18 -0800676 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
677 LOG(INFO) << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800679 << static_cast<int>(cur_block->successor_block_list.block_list_type);
buzbee67bf8852011-08-17 17:51:35 -0700680 }
681
buzbeefa57c472012-11-21 12:06:18 -0800682 cur_block->successor_block_list.block_list_type = kCatch;
683 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, 2,
Bill Buzbeea114add2012-05-03 15:00:40 -0700684 kListSuccessorBlocks);
685
686 for (;iterator.HasNext(); iterator.Next()) {
buzbeefa57c472012-11-21 12:06:18 -0800687 BasicBlock *catch_block = FindBlock(cu, iterator.GetHandlerAddress(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700688 false /* split*/,
689 false /* creat */,
buzbeefa57c472012-11-21 12:06:18 -0800690 NULL /* immed_pred_block_p */);
691 catch_block->catch_entry = true;
692 cu->catches.insert(catch_block->start_offset);
693 SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
694 (NewMem(cu, sizeof(SuccessorBlockInfo), false, kAllocSuccessor));
695 successor_block_info->block = catch_block;
696 successor_block_info->key = iterator.GetHandlerTypeIndex();
697 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
698 reinterpret_cast<uintptr_t>(successor_block_info));
699 InsertGrowableList(cu, catch_block->predecessors,
700 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700701 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700702 } else {
buzbeefa57c472012-11-21 12:06:18 -0800703 BasicBlock *eh_block = NewMemBB(cu, kExceptionHandling,
704 cu->num_blocks++);
705 cur_block->taken = eh_block;
706 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(eh_block));
707 eh_block->start_offset = cur_offset;
708 InsertGrowableList(cu, eh_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 }
710
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700711 if (insn->dalvikInsn.opcode == Instruction::THROW){
buzbeefa57c472012-11-21 12:06:18 -0800712 cur_block->explicit_throw = true;
713 if ((code_ptr < code_end) && ContentIsInsn(code_ptr)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700714 // Force creation of new block following THROW via side-effect
buzbeefa57c472012-11-21 12:06:18 -0800715 FindBlock(cu, cur_offset + width, /* split */ false,
716 /* create */ true, /* immed_pred_block_p */ NULL);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700717 }
buzbeefa57c472012-11-21 12:06:18 -0800718 if (!in_try_block) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700719 // Don't split a THROW that can't rethrow - we're done.
buzbeefa57c472012-11-21 12:06:18 -0800720 return cur_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 }
722 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700723
724 /*
725 * Split the potentially-throwing instruction into two parts.
726 * The first half will be a pseudo-op that captures the exception
727 * edges and terminates the basic block. It always falls through.
728 * Then, create a new basic block that begins with the throwing instruction
729 * (minus exceptions). Note: this new basic block must NOT be entered into
buzbeefa57c472012-11-21 12:06:18 -0800730 * the block_map. If the potentially-throwing instruction is the target of a
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700731 * future branch, we need to find the check psuedo half. The new
732 * basic block containing the work portion of the instruction should
733 * only be entered via fallthrough from the block containing the
734 * pseudo exception edge MIR. Note also that this new block is
735 * not automatically terminated after the work portion, and may
736 * contain following instructions.
737 */
buzbeefa57c472012-11-21 12:06:18 -0800738 BasicBlock *new_block = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
739 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(new_block));
740 new_block->start_offset = insn->offset;
741 cur_block->fall_through = new_block;
742 InsertGrowableList(cu, new_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
743 MIR* new_insn = static_cast<MIR*>(NewMem(cu, sizeof(MIR), true, kAllocMIR));
744 *new_insn = *insn;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700745 insn->dalvikInsn.opcode =
746 static_cast<Instruction::Code>(kMirOpCheck);
747 // Associate the two halves
buzbeefa57c472012-11-21 12:06:18 -0800748 insn->meta.throw_insn = new_insn;
749 new_insn->meta.throw_insn = insn;
750 AppendMIR(new_block, new_insn);
751 return new_block;
buzbee67bf8852011-08-17 17:51:35 -0700752}
753
buzbeefa57c472012-11-21 12:06:18 -0800754void CompilerInit(CompilationUnit* cu, const Compiler& compiler) {
buzbee02031b12012-11-23 09:41:35 -0800755 bool success = false;
756 switch (compiler.GetInstructionSet()) {
757 case kThumb2:
758 success = InitArmCodegen(cu);
759 break;
760 case kMips:
761 success = InitMipsCodegen(cu);
762 break;
763 case kX86:
764 success = InitX86Codegen(cu);
765 break;
766 default:;
767 }
768 if (!success) {
769 LOG(FATAL) << "Failed to initialize codegen for " << compiler.GetInstructionSet();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800770 }
buzbeefa57c472012-11-21 12:06:18 -0800771 if (!HeapInit(cu)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800772 LOG(FATAL) << "Failed to initialize oat heap";
773 }
774}
775
buzbee52a77fc2012-11-20 19:50:46 -0800776static CompiledMethod* CompileMethod(Compiler& compiler,
buzbeefa57c472012-11-21 12:06:18 -0800777 const CompilerBackend compiler_backend,
buzbee52a77fc2012-11-20 19:50:46 -0800778 const DexFile::CodeItem* code_item,
779 uint32_t access_flags, InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800780 uint32_t class_def_idx, uint32_t method_idx,
781 jobject class_loader, const DexFile& dex_file,
buzbee52a77fc2012-11-20 19:50:46 -0800782 LLVMInfo* llvm_info)
buzbee67bf8852011-08-17 17:51:35 -0700783{
Bill Buzbeea114add2012-05-03 15:00:40 -0700784 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700785
buzbeefa57c472012-11-21 12:06:18 -0800786 const uint16_t* code_ptr = code_item->insns_;
787 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
788 int num_blocks = 0;
789 unsigned int cur_offset = 0;
buzbee67bf8852011-08-17 17:51:35 -0700790
Bill Buzbeea114add2012-05-03 15:00:40 -0700791 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
buzbeefa57c472012-11-21 12:06:18 -0800792 UniquePtr<CompilationUnit> cu(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800793
buzbeefa57c472012-11-21 12:06:18 -0800794 CompilerInit(cu.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800795
buzbeefa57c472012-11-21 12:06:18 -0800796 cu->compiler = &compiler;
797 cu->class_linker = class_linker;
798 cu->dex_file = &dex_file;
Ian Rogersfffdb022013-01-04 15:14:08 -0800799 cu->class_def_idx = class_def_idx;
buzbeefa57c472012-11-21 12:06:18 -0800800 cu->method_idx = method_idx;
801 cu->code_item = code_item;
802 cu->access_flags = access_flags;
803 cu->invoke_type = invoke_type;
804 cu->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
805 cu->instruction_set = compiler.GetInstructionSet();
806 cu->insns = code_item->insns_;
807 cu->insns_size = code_item->insns_size_in_code_units_;
808 cu->num_ins = code_item->ins_size_;
809 cu->num_regs = code_item->registers_size_ - cu->num_ins;
810 cu->num_outs = code_item->outs_size_;
811 DCHECK((cu->instruction_set == kThumb2) ||
812 (cu->instruction_set == kX86) ||
813 (cu->instruction_set == kMips));
814 if ((compiler_backend == kQuickGBC) || (compiler_backend == kPortable)) {
815 cu->gen_bitcode = true;
buzbee85eee022012-07-16 22:12:38 -0700816 }
buzbeefa57c472012-11-21 12:06:18 -0800817 cu->llvm_info = llvm_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700818 /* Adjust this value accordingly once inlining is performed */
buzbeefa57c472012-11-21 12:06:18 -0800819 cu->num_dalvik_registers = code_item->registers_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700820 // TODO: set this from command line
buzbeefa57c472012-11-21 12:06:18 -0800821 cu->compiler_flip_match = false;
822 bool use_match = !cu->compiler_method_match.empty();
823 bool match = use_match && (cu->compiler_flip_match ^
824 (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) !=
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 std::string::npos));
buzbeefa57c472012-11-21 12:06:18 -0800826 if (!use_match || match) {
827 cu->disable_opt = kCompilerOptimizerDisableFlags;
828 cu->enable_debug = kCompilerDebugFlags;
829 cu->verbose = VLOG_IS_ON(compiler) ||
830 (cu->enable_debug & (1 << kDebugVerbose));
Bill Buzbeea114add2012-05-03 15:00:40 -0700831 }
buzbee6459e7c2012-10-02 14:42:41 -0700832#ifndef NDEBUG
buzbeefa57c472012-11-21 12:06:18 -0800833 if (cu->gen_bitcode) {
834 cu->enable_debug |= (1 << kDebugVerifyBitcode);
buzbee6969d502012-06-15 16:40:31 -0700835 }
buzbee2cfc6392012-05-07 14:51:40 -0700836#endif
buzbee9281f002012-10-24 12:17:24 -0700837
buzbeefa57c472012-11-21 12:06:18 -0800838 if (cu->instruction_set == kMips) {
jeffhao7fbee072012-08-24 17:56:54 -0700839 // Disable some optimizations for mips for now
buzbeefa57c472012-11-21 12:06:18 -0800840 cu->disable_opt |= (
jeffhao7fbee072012-08-24 17:56:54 -0700841 (1 << kLoadStoreElimination) |
842 (1 << kLoadHoisting) |
843 (1 << kSuppressLoads) |
844 (1 << kNullCheckElimination) |
845 (1 << kPromoteRegs) |
846 (1 << kTrackLiveTemps) |
847 (1 << kSkipLargeMethodOptimization) |
848 (1 << kSafeOptimizations) |
849 (1 << kBBOpt) |
850 (1 << kMatch) |
851 (1 << kPromoteCompilerTemps));
852 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700853
854 /* Gathering opcode stats? */
855 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbeefa57c472012-11-21 12:06:18 -0800856 cu->opcode_count =
857 static_cast<int*>(NewMem(cu.get(), kNumPackedOpcodes * sizeof(int), true, kAllocMisc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 }
859
860 /* Assume non-throwing leaf */
buzbeefa57c472012-11-21 12:06:18 -0800861 cu->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
Bill Buzbeea114add2012-05-03 15:00:40 -0700862
buzbeefa57c472012-11-21 12:06:18 -0800863 /* Initialize the block list, estimate size based on insns_size */
864 CompilerInitGrowableList(cu.get(), &cu->block_list, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 kListBlockList);
866
buzbeefa57c472012-11-21 12:06:18 -0800867 /* Initialize the switch_tables list */
868 CompilerInitGrowableList(cu.get(), &cu->switch_tables, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700869 kListSwitchTables);
870
buzbeefa57c472012-11-21 12:06:18 -0800871 /* Intialize the fill_array_data list */
872 CompilerInitGrowableList(cu.get(), &cu->fill_array_data, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 kListFillArrayData);
874
buzbeefa57c472012-11-21 12:06:18 -0800875 /* Intialize the throw_launchpads list, estimate size based on insns_size */
876 CompilerInitGrowableList(cu.get(), &cu->throw_launchpads, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700877 kListThrowLaunchPads);
878
buzbeefa57c472012-11-21 12:06:18 -0800879 /* Intialize the instrinsic_launchpads list */
880 CompilerInitGrowableList(cu.get(), &cu->intrinsic_launchpads, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700881 kListMisc);
882
883
buzbeefa57c472012-11-21 12:06:18 -0800884 /* Intialize the suspend_launchpads list */
885 CompilerInitGrowableList(cu.get(), &cu->suspend_launchpads, 2048,
Bill Buzbeea114add2012-05-03 15:00:40 -0700886 kListSuspendLaunchPads);
887
888 /* Allocate the bit-vector to track the beginning of basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800889 ArenaBitVector *try_block_addr = AllocBitVector(cu.get(),
890 cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700891 true /* expandable */);
buzbeefa57c472012-11-21 12:06:18 -0800892 cu->try_block_addr = try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700893
894 /* Create the default entry and exit blocks and enter them to the list */
buzbeefa57c472012-11-21 12:06:18 -0800895 BasicBlock *entry_block = NewMemBB(cu.get(), kEntryBlock, num_blocks++);
896 BasicBlock *exit_block = NewMemBB(cu.get(), kExitBlock, num_blocks++);
Bill Buzbeea114add2012-05-03 15:00:40 -0700897
buzbeefa57c472012-11-21 12:06:18 -0800898 cu->entry_block = entry_block;
899 cu->exit_block = exit_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700900
buzbeefa57c472012-11-21 12:06:18 -0800901 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(entry_block));
902 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(exit_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700903
904 /* Current block to record parsed instructions */
buzbeefa57c472012-11-21 12:06:18 -0800905 BasicBlock *cur_block = NewMemBB(cu.get(), kDalvikByteCode, num_blocks++);
906 cur_block->start_offset = 0;
907 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 /* Add first block to the fast lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800909 cu->block_map.Put(cur_block->start_offset, cur_block);
910 entry_block->fall_through = cur_block;
911 InsertGrowableList(cu.get(), cur_block->predecessors,
912 reinterpret_cast<uintptr_t>(entry_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700913
914 /*
915 * Store back the number of blocks since new blocks may be created of
buzbeefa57c472012-11-21 12:06:18 -0800916 * accessing cu.
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 */
buzbeefa57c472012-11-21 12:06:18 -0800918 cu->num_blocks = num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700919
920 /* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800921 ProcessTryCatchBlocks(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -0700922
923 /* Set up for simple method detection */
buzbeefa57c472012-11-21 12:06:18 -0800924 int num_patterns = sizeof(special_patterns)/sizeof(special_patterns[0]);
925 bool live_pattern = (num_patterns > 0) && !(cu->disable_opt & (1 << kMatch));
926 bool* dead_pattern =
927 static_cast<bool*>(NewMem(cu.get(), sizeof(bool) * num_patterns, true, kAllocMisc));
928 SpecialCaseHandler special_case = kNoHandler;
929 int pattern_pos = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700930
931 /* Parse all instructions and put them into containing basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800932 while (code_ptr < code_end) {
933 MIR *insn = static_cast<MIR *>(NewMem(cu.get(), sizeof(MIR), true, kAllocMIR));
934 insn->offset = cur_offset;
buzbeea169e1d2012-12-05 14:26:44 -0800935 int width = ParseInsn(cu.get(), code_ptr, &insn->dalvikInsn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 insn->width = width;
937 Instruction::Code opcode = insn->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800938 if (cu->opcode_count != NULL) {
939 cu->opcode_count[static_cast<int>(opcode)]++;
buzbee44b412b2012-02-04 08:50:53 -0800940 }
941
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 /* Terminate when the data section is seen */
943 if (width == 0)
944 break;
945
946 /* Possible simple method? */
buzbeefa57c472012-11-21 12:06:18 -0800947 if (live_pattern) {
948 live_pattern = false;
949 special_case = kNoHandler;
950 for (int i = 0; i < num_patterns; i++) {
951 if (!dead_pattern[i]) {
952 if (special_patterns[i].opcodes[pattern_pos] == opcode) {
953 live_pattern = true;
954 special_case = special_patterns[i].handler_code;
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 } else {
buzbeefa57c472012-11-21 12:06:18 -0800956 dead_pattern[i] = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 }
958 }
959 }
buzbeefa57c472012-11-21 12:06:18 -0800960 pattern_pos++;
buzbeea7c12682012-03-19 13:13:53 -0700961 }
962
buzbeefa57c472012-11-21 12:06:18 -0800963 AppendMIR(cur_block, insn);
buzbeecefd1872011-09-09 09:59:52 -0700964
buzbeefa57c472012-11-21 12:06:18 -0800965 code_ptr += width;
Ian Rogersa75a0132012-09-28 11:41:42 -0700966 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700967
buzbeefa57c472012-11-21 12:06:18 -0800968 int df_flags = oat_data_flow_attributes[insn->dalvikInsn.opcode];
buzbee67bf8852011-08-17 17:51:35 -0700969
buzbeefa57c472012-11-21 12:06:18 -0800970 if (df_flags & DF_HAS_DEFS) {
971 cu->def_count += (df_flags & DF_A_WIDE) ? 2 : 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 }
buzbee67bf8852011-08-17 17:51:35 -0700973
Bill Buzbeea114add2012-05-03 15:00:40 -0700974 if (flags & Instruction::kBranch) {
buzbeefa57c472012-11-21 12:06:18 -0800975 cur_block = ProcessCanBranch(cu.get(), cur_block, insn, cur_offset,
976 width, flags, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 } else if (flags & Instruction::kReturn) {
buzbeebbdd0532013-02-07 09:33:02 -0800978 cur_block->terminated_by_return = true;
buzbeefa57c472012-11-21 12:06:18 -0800979 cur_block->fall_through = exit_block;
980 InsertGrowableList(cu.get(), exit_block->predecessors,
981 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700982 /*
983 * Terminate the current block if there are instructions
984 * afterwards.
985 */
buzbeefa57c472012-11-21 12:06:18 -0800986 if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700987 /*
988 * Create a fallthrough block for real instructions
989 * (incl. NOP).
990 */
buzbeefa57c472012-11-21 12:06:18 -0800991 if (ContentIsInsn(code_ptr)) {
992 FindBlock(cu.get(), cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700993 /* split */
994 false,
995 /* create */
996 true,
buzbeefa57c472012-11-21 12:06:18 -0800997 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 NULL);
999 }
1000 }
1001 } else if (flags & Instruction::kThrow) {
buzbeefa57c472012-11-21 12:06:18 -08001002 cur_block = ProcessCanThrow(cu.get(), cur_block, insn, cur_offset,
1003 width, flags, try_block_addr, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 } else if (flags & Instruction::kSwitch) {
buzbeefa57c472012-11-21 12:06:18 -08001005 ProcessCanSwitch(cu.get(), cur_block, insn, cur_offset, width, flags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001006 }
buzbeefa57c472012-11-21 12:06:18 -08001007 cur_offset += width;
1008 BasicBlock *next_block = FindBlock(cu.get(), cur_offset,
Bill Buzbeea114add2012-05-03 15:00:40 -07001009 /* split */
1010 false,
1011 /* create */
1012 false,
buzbeefa57c472012-11-21 12:06:18 -08001013 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 NULL);
buzbeefa57c472012-11-21 12:06:18 -08001015 if (next_block) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001016 /*
1017 * The next instruction could be the target of a previously parsed
1018 * forward branch so a block is already created. If the current
1019 * instruction is not an unconditional branch, connect them through
1020 * the fall-through link.
1021 */
buzbeefa57c472012-11-21 12:06:18 -08001022 DCHECK(cur_block->fall_through == NULL ||
1023 cur_block->fall_through == next_block ||
1024 cur_block->fall_through == exit_block);
buzbee5ade1d22011-09-09 14:44:52 -07001025
buzbeefa57c472012-11-21 12:06:18 -08001026 if ((cur_block->fall_through == NULL) && (flags & Instruction::kContinue)) {
1027 cur_block->fall_through = next_block;
1028 InsertGrowableList(cu.get(), next_block->predecessors,
1029 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 }
buzbeefa57c472012-11-21 12:06:18 -08001031 cur_block = next_block;
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 }
1033 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001034
buzbeed8506212012-12-20 14:15:05 -08001035 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1036 DumpCFG(cu.get(), "/sdcard/1_post_parse_cfg/", true);
1037 }
1038
buzbeefa57c472012-11-21 12:06:18 -08001039 if (!(cu->disable_opt & (1 << kSkipLargeMethodOptimization))) {
1040 if ((cu->num_blocks > MANY_BLOCKS) ||
1041 ((cu->num_blocks > MANY_BLOCKS_INITIALIZER) &&
Bill Buzbeea114add2012-05-03 15:00:40 -07001042 PrettyMethod(method_idx, dex_file, false).find("init>") !=
1043 std::string::npos)) {
buzbeefa57c472012-11-21 12:06:18 -08001044 cu->qd_mode = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 }
1046 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001047
buzbeefa57c472012-11-21 12:06:18 -08001048 if (cu->qd_mode) {
buzbeed1643e42012-09-05 14:06:51 -07001049 // Bitcode generation requires full dataflow analysis
buzbeefa57c472012-11-21 12:06:18 -08001050 cu->disable_dataflow = !cu->gen_bitcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001051 // Disable optimization which require dataflow/ssa
buzbeefa57c472012-11-21 12:06:18 -08001052 cu->disable_opt |= (1 << kBBOpt) | (1 << kPromoteRegs) | (1 << kNullCheckElimination);
1053 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 LOG(INFO) << "QD mode enabled: "
1055 << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001056 << " num blocks: " << cu->num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -07001057 }
1058 }
buzbeec1f45042011-09-21 16:03:19 -07001059
buzbeefa57c472012-11-21 12:06:18 -08001060 if (cu->verbose) {
1061 DumpCompilationUnit(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001062 }
buzbee67bf8852011-08-17 17:51:35 -07001063
buzbee0967a252012-09-14 10:43:54 -07001064 /* Do a code layout pass */
buzbeefa57c472012-11-21 12:06:18 -08001065 CodeLayout(cu.get());
buzbee0967a252012-09-14 10:43:54 -07001066
buzbeed8506212012-12-20 14:15:05 -08001067 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1068 DumpCFG(cu.get(), "/sdcard/2_post_layout_cfg/", true);
1069 }
1070
buzbeefa57c472012-11-21 12:06:18 -08001071 if (cu->enable_debug & (1 << kDebugVerifyDataflow)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001072 /* Verify if all blocks are connected as claimed */
buzbeefa57c472012-11-21 12:06:18 -08001073 DataFlowAnalysisDispatcher(cu.get(), VerifyPredInfo, kAllNodes,
1074 false /* is_iterative */);
Bill Buzbeea114add2012-05-03 15:00:40 -07001075 }
buzbee67bf8852011-08-17 17:51:35 -07001076
Bill Buzbeea114add2012-05-03 15:00:40 -07001077 /* Perform SSA transformation for the whole method */
buzbeefa57c472012-11-21 12:06:18 -08001078 SSATransformation(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001079
buzbeed8506212012-12-20 14:15:05 -08001080 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1081 DumpCFG(cu.get(), "/sdcard/3_post_ssa_cfg/", false);
1082 }
1083
buzbee2cfc6392012-05-07 14:51:40 -07001084 /* Do constant propagation */
buzbee4ef3e452012-12-14 13:35:28 -08001085 cu->is_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs, false /* not expandable */);
1086 cu->must_flush_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs,
1087 false /* not expandable */);
buzbeefa57c472012-11-21 12:06:18 -08001088 cu->constant_values =
1089 static_cast<int*>(NewMem(cu.get(), sizeof(int) * cu->num_ssa_regs, true, kAllocDFInfo));
1090 DataFlowAnalysisDispatcher(cu.get(), DoConstantPropogation,
buzbee2cfc6392012-05-07 14:51:40 -07001091 kAllNodes,
buzbeefa57c472012-11-21 12:06:18 -08001092 false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -07001093
Bill Buzbeea114add2012-05-03 15:00:40 -07001094 /* Detect loops */
buzbeefa57c472012-11-21 12:06:18 -08001095 LoopDetection(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001096
Bill Buzbeea114add2012-05-03 15:00:40 -07001097 /* Count uses */
buzbeefa57c472012-11-21 12:06:18 -08001098 MethodUseCount(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001099
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 /* Perform null check elimination */
buzbeefa57c472012-11-21 12:06:18 -08001101 NullCheckElimination(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001102
buzbeed8506212012-12-20 14:15:05 -08001103 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1104 DumpCFG(cu.get(), "/sdcard/4_post_nce_cfg/", false);
1105 }
1106
buzbeed1643e42012-09-05 14:06:51 -07001107 /* Combine basic blocks where possible */
buzbeefa57c472012-11-21 12:06:18 -08001108 BasicBlockCombine(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001109
buzbeed8506212012-12-20 14:15:05 -08001110 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1111 DumpCFG(cu.get(), "/sdcard/5_post_bbcombine_cfg/", false);
1112 }
1113
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 /* Do some basic block optimizations */
buzbeefa57c472012-11-21 12:06:18 -08001115 BasicBlockOptimization(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001116
buzbeed8506212012-12-20 14:15:05 -08001117 // Debugging only
1118 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1119 DumpCFG(cu.get(), "/sdcard/6_post_bbo_cfg/", false);
1120 }
1121
buzbeefa57c472012-11-21 12:06:18 -08001122 if (cu->enable_debug & (1 << kDebugDumpCheckStats)) {
1123 DumpCheckStats(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001124 }
1125
buzbee02031b12012-11-23 09:41:35 -08001126 cu.get()->cg->CompilerInitializeRegAlloc(cu.get()); // Needs to happen after SSA naming
Bill Buzbeea114add2012-05-03 15:00:40 -07001127
1128 /* Allocate Registers using simple local allocation scheme */
buzbeefa57c472012-11-21 12:06:18 -08001129 SimpleRegAlloc(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001130
buzbee2502e002012-12-31 16:05:53 -08001131 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1132 DumpCFG(cu.get(), "/sdcard/7_post_ralloc_cfg/", true);
1133 }
1134
1135
buzbee2cfc6392012-05-07 14:51:40 -07001136 /* Go the LLVM path? */
buzbeefa57c472012-11-21 12:06:18 -08001137 if (cu->gen_bitcode) {
buzbee2cfc6392012-05-07 14:51:40 -07001138 // MIR->Bitcode
buzbeefa57c472012-11-21 12:06:18 -08001139 MethodMIR2Bitcode(cu.get());
1140 if (compiler_backend == kPortable) {
buzbeeabc4c6b2012-08-23 08:17:15 -07001141 // all done
buzbeefa57c472012-11-21 12:06:18 -08001142 ArenaReset(cu.get());
buzbeeabc4c6b2012-08-23 08:17:15 -07001143 return NULL;
1144 }
buzbee2cfc6392012-05-07 14:51:40 -07001145 // Bitcode->LIR
buzbeefa57c472012-11-21 12:06:18 -08001146 MethodBitcode2LIR(cu.get());
buzbee2cfc6392012-05-07 14:51:40 -07001147 } else {
buzbeefa57c472012-11-21 12:06:18 -08001148 if (special_case != kNoHandler) {
buzbee2cfc6392012-05-07 14:51:40 -07001149 /*
1150 * Custom codegen for special cases. If for any reason the
buzbeefa57c472012-11-21 12:06:18 -08001151 * special codegen doesn't succeed, cu->first_lir_insn will
buzbee2cfc6392012-05-07 14:51:40 -07001152 * set to NULL;
1153 */
buzbeefa57c472012-11-21 12:06:18 -08001154 SpecialMIR2LIR(cu.get(), special_case);
buzbee2cfc6392012-05-07 14:51:40 -07001155 }
buzbee67bf8852011-08-17 17:51:35 -07001156
buzbee2cfc6392012-05-07 14:51:40 -07001157 /* Convert MIR to LIR, etc. */
buzbeefa57c472012-11-21 12:06:18 -08001158 if (cu->first_lir_insn == NULL) {
1159 MethodMIR2LIR(cu.get());
buzbee2cfc6392012-05-07 14:51:40 -07001160 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001161 }
buzbee67bf8852011-08-17 17:51:35 -07001162
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 /* Method is not empty */
buzbeefa57c472012-11-21 12:06:18 -08001164 if (cu->first_lir_insn) {
buzbee67bf8852011-08-17 17:51:35 -07001165
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 // mark the targets of switch statement case labels
buzbeefa57c472012-11-21 12:06:18 -08001167 ProcessSwitchTables(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001168
Bill Buzbeea114add2012-05-03 15:00:40 -07001169 /* Convert LIR into machine code. */
buzbeefa57c472012-11-21 12:06:18 -08001170 AssembleLIR(cu.get());
buzbee99ba9642012-01-25 14:23:14 -08001171
buzbeefa57c472012-11-21 12:06:18 -08001172 if (cu->verbose) {
1173 CodegenDump(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001174 }
1175
buzbeefa57c472012-11-21 12:06:18 -08001176 if (cu->opcode_count != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001177 LOG(INFO) << "Opcode Count";
1178 for (int i = 0; i < kNumPackedOpcodes; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001179 if (cu->opcode_count[i] != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001180 LOG(INFO) << "-C- "
1181 << Instruction::Name(static_cast<Instruction::Code>(i))
buzbeefa57c472012-11-21 12:06:18 -08001182 << " " << cu->opcode_count[i];
buzbee67bf8852011-08-17 17:51:35 -07001183 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001184 }
1185 }
1186 }
buzbeea7c12682012-03-19 13:13:53 -07001187
buzbeefa57c472012-11-21 12:06:18 -08001188 // Combine vmap tables - core regs, then fp regs - into vmap_table
1189 std::vector<uint16_t> vmap_table;
buzbeeca7a5e42012-08-20 11:12:18 -07001190 // Core regs may have been inserted out of order - sort first
buzbeefa57c472012-11-21 12:06:18 -08001191 std::sort(cu->core_vmap_table.begin(), cu->core_vmap_table.end());
1192 for (size_t i = 0 ; i < cu->core_vmap_table.size(); i++) {
buzbeeca7a5e42012-08-20 11:12:18 -07001193 // Copy, stripping out the phys register sort key
buzbeefa57c472012-11-21 12:06:18 -08001194 vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & cu->core_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001195 }
1196 // If we have a frame, push a marker to take place of lr
buzbeefa57c472012-11-21 12:06:18 -08001197 if (cu->frame_size > 0) {
1198 vmap_table.push_back(INVALID_VREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001199 } else {
buzbeefa57c472012-11-21 12:06:18 -08001200 DCHECK_EQ(__builtin_popcount(cu->core_spill_mask), 0);
1201 DCHECK_EQ(__builtin_popcount(cu->fp_spill_mask), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001202 }
buzbeeca7a5e42012-08-20 11:12:18 -07001203 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
buzbeefa57c472012-11-21 12:06:18 -08001204 for (uint32_t i = 0; i < cu->fp_vmap_table.size(); i++) {
1205 vmap_table.push_back(cu->fp_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001206 }
1207 CompiledMethod* result =
buzbeefa57c472012-11-21 12:06:18 -08001208 new CompiledMethod(cu->instruction_set, cu->code_buffer,
1209 cu->frame_size, cu->core_spill_mask, cu->fp_spill_mask,
1210 cu->combined_mapping_table, vmap_table, cu->native_gc_map);
buzbee67bf8852011-08-17 17:51:35 -07001211
Bill Buzbeea114add2012-05-03 15:00:40 -07001212 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001213 << " (" << (cu->code_buffer.size() * sizeof(cu->code_buffer[0]))
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 << " bytes)";
buzbee5abfa3e2012-01-31 17:01:43 -08001215
1216#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -08001217 if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) {
1218 DumpMemStats(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001219 }
buzbee5abfa3e2012-01-31 17:01:43 -08001220#endif
buzbee67bf8852011-08-17 17:51:35 -07001221
buzbeefa57c472012-11-21 12:06:18 -08001222 ArenaReset(cu.get());
buzbeeba938cb2012-02-03 14:47:55 -08001223
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 return result;
buzbee67bf8852011-08-17 17:51:35 -07001225}
1226
buzbee52a77fc2012-11-20 19:50:46 -08001227CompiledMethod* CompileOneMethod(Compiler& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001228 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -07001229 const DexFile::CodeItem* code_item,
1230 uint32_t access_flags, InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001231 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001232 const DexFile& dex_file,
buzbeefa57c472012-11-21 12:06:18 -08001233 LLVMInfo* llvm_info)
buzbeeabc4c6b2012-08-23 08:17:15 -07001234{
Ian Rogersfffdb022013-01-04 15:14:08 -08001235 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
1236 method_idx, class_loader, dex_file, llvm_info);
buzbeeabc4c6b2012-08-23 08:17:15 -07001237}
1238
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001239} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001240
Bill Buzbeea114add2012-05-03 15:00:40 -07001241extern "C" art::CompiledMethod*
buzbeec531cef2012-10-18 07:09:20 -07001242 ArtQuickCompileMethod(art::Compiler& compiler,
1243 const art::DexFile::CodeItem* code_item,
1244 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001245 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001246 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001247{
buzbeec531cef2012-10-18 07:09:20 -07001248 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
1249 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -08001250 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -08001251 class_def_idx, method_idx, class_loader, dex_file,
1252 NULL /* use thread llvm_info */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001253}