blob: b8c9b8e1dd7817f3a85429ca4418f12de1ef8bde [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"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070022#include "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 {
Shih-wei Liao215a9262012-10-12 10:29:46 -070031#if !defined(ART_USE_LLVM_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() {
42#if !defined(ART_USE_LLVM_COMPILER)
43 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
Bill Buzbeea114add2012-05-03 15:00:40 -070069 //(1 << kLoadStoreElimination) |
70 //(1 << kLoadHoisting) |
71 //(1 << kSuppressLoads) |
72 //(1 << kNullCheckElimination) |
73 //(1 << kPromoteRegs) |
74 //(1 << kTrackLiveTemps) |
75 //(1 << kSkipLargeMethodOptimization) |
76 //(1 << kSafeOptimizations) |
77 //(1 << kBBOpt) |
78 //(1 << kMatch) |
79 //(1 << kPromoteCompilerTemps) |
80 0;
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,
116 DecodedInstruction* decoded_instruction, bool verbose)
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
buzbeefa57c472012-11-21 12:06:18 -0800126 if (verbose) {
127 char* decoded_string = GetDalvikDisassembly(cu, *decoded_instruction,
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 NULL);
buzbeefa57c472012-11-21 12:06:18 -0800129 LOG(INFO) << code_ptr << ": 0x" << std::hex << static_cast<int>(decoded_instruction->opcode)
130 << " " << decoded_string;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800131 }
132 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -0700133}
134
135#define UNKNOWN_TARGET 0xffffffff
136
buzbee67bf8852011-08-17 17:51:35 -0700137/* Split an existing block from the specified code offset into two */
buzbeefa57c472012-11-21 12:06:18 -0800138static BasicBlock *SplitBlock(CompilationUnit* cu, unsigned int code_offset,
139 BasicBlock* orig_block, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700140{
buzbeefa57c472012-11-21 12:06:18 -0800141 MIR* insn = orig_block->first_mir_insn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 while (insn) {
buzbeefa57c472012-11-21 12:06:18 -0800143 if (insn->offset == code_offset) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 insn = insn->next;
145 }
146 if (insn == NULL) {
147 LOG(FATAL) << "Break split failed";
148 }
buzbeefa57c472012-11-21 12:06:18 -0800149 BasicBlock *bottom_block = NewMemBB(cu, kDalvikByteCode,
150 cu->num_blocks++);
151 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700152
buzbeefa57c472012-11-21 12:06:18 -0800153 bottom_block->start_offset = code_offset;
154 bottom_block->first_mir_insn = insn;
155 bottom_block->last_mir_insn = orig_block->last_mir_insn;
buzbee67bf8852011-08-17 17:51:35 -0700156
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 /* Add it to the quick lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800158 cu->block_map.Put(bottom_block->start_offset, bottom_block);
buzbee5b537102012-01-17 17:33:47 -0800159
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 /* Handle the taken path */
buzbeefa57c472012-11-21 12:06:18 -0800161 bottom_block->taken = orig_block->taken;
162 if (bottom_block->taken) {
163 orig_block->taken = NULL;
164 DeleteGrowableList(bottom_block->taken->predecessors, reinterpret_cast<uintptr_t>(orig_block));
165 InsertGrowableList(cu, bottom_block->taken->predecessors,
166 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 }
168
169 /* Handle the fallthrough path */
buzbeefa57c472012-11-21 12:06:18 -0800170 bottom_block->fall_through = orig_block->fall_through;
171 orig_block->fall_through = bottom_block;
172 InsertGrowableList(cu, bottom_block->predecessors,
173 reinterpret_cast<uintptr_t>(orig_block));
174 if (bottom_block->fall_through) {
175 DeleteGrowableList(bottom_block->fall_through->predecessors,
176 reinterpret_cast<uintptr_t>(orig_block));
177 InsertGrowableList(cu, bottom_block->fall_through->predecessors,
178 reinterpret_cast<uintptr_t>(bottom_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 }
180
181 /* Handle the successor list */
buzbeefa57c472012-11-21 12:06:18 -0800182 if (orig_block->successor_block_list.block_list_type != kNotUsed) {
183 bottom_block->successor_block_list = orig_block->successor_block_list;
184 orig_block->successor_block_list.block_list_type = kNotUsed;
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 GrowableListIterator iterator;
186
buzbeefa57c472012-11-21 12:06:18 -0800187 GrowableListIteratorInit(&bottom_block->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 &iterator);
189 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800190 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800191 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800192 if (successor_block_info == NULL) break;
193 BasicBlock *bb = successor_block_info->block;
194 DeleteGrowableList(bb->predecessors, reinterpret_cast<uintptr_t>(orig_block));
195 InsertGrowableList(cu, bb->predecessors, reinterpret_cast<uintptr_t>(bottom_block));
buzbee67bf8852011-08-17 17:51:35 -0700196 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 }
buzbee67bf8852011-08-17 17:51:35 -0700198
buzbeefa57c472012-11-21 12:06:18 -0800199 orig_block->last_mir_insn = insn->prev;
buzbee67bf8852011-08-17 17:51:35 -0700200
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 insn->prev->next = NULL;
202 insn->prev = NULL;
203 /*
204 * Update the immediate predecessor block pointer so that outgoing edges
205 * can be applied to the proper block.
206 */
buzbeefa57c472012-11-21 12:06:18 -0800207 if (immed_pred_block_p) {
208 DCHECK_EQ(*immed_pred_block_p, orig_block);
209 *immed_pred_block_p = bottom_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 }
buzbeefa57c472012-11-21 12:06:18 -0800211 return bottom_block;
buzbee67bf8852011-08-17 17:51:35 -0700212}
213
214/*
215 * Given a code offset, find out the block that starts with it. If the offset
buzbeefa57c472012-11-21 12:06:18 -0800216 * is in the middle of an existing block, split it into two. If immed_pred_block_p
217 * is not non-null and is the block being split, update *immed_pred_block_p to
buzbee9ab05de2012-01-18 15:43:48 -0800218 * point to the bottom block so that outgoing edges can be set up properly
219 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800220 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700221 */
buzbeefa57c472012-11-21 12:06:18 -0800222BasicBlock *FindBlock(CompilationUnit* cu, unsigned int code_offset,
223 bool split, bool create, BasicBlock** immed_pred_block_p)
buzbee67bf8852011-08-17 17:51:35 -0700224{
buzbeefa57c472012-11-21 12:06:18 -0800225 GrowableList* block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 BasicBlock* bb;
227 unsigned int i;
228 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700229
buzbeefa57c472012-11-21 12:06:18 -0800230 it = cu->block_map.find(code_offset);
231 if (it != cu->block_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 return it->second;
233 } else if (!create) {
234 return NULL;
235 }
236
237 if (split) {
buzbeefa57c472012-11-21 12:06:18 -0800238 for (i = 0; i < block_list->num_used; i++) {
239 bb = reinterpret_cast<BasicBlock*>(block_list->elem_list[i]);
240 if (bb->block_type != kDalvikByteCode) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 /* Check if a branch jumps into the middle of an existing block */
buzbeefa57c472012-11-21 12:06:18 -0800242 if ((code_offset > bb->start_offset) && (bb->last_mir_insn != NULL) &&
243 (code_offset <= bb->last_mir_insn->offset)) {
244 BasicBlock *new_bb = SplitBlock(cu, code_offset, bb,
245 bb == *immed_pred_block_p ?
246 immed_pred_block_p : NULL);
247 return new_bb;
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 }
buzbee5b537102012-01-17 17:33:47 -0800249 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 }
buzbee5b537102012-01-17 17:33:47 -0800251
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 /* Create a new one */
buzbeefa57c472012-11-21 12:06:18 -0800253 bb = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
254 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(bb));
255 bb->start_offset = code_offset;
256 cu->block_map.Put(bb->start_offset, bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700258}
259
buzbeef58c12c2012-07-03 15:06:29 -0700260/* Find existing block */
buzbeefa57c472012-11-21 12:06:18 -0800261BasicBlock* FindBlock(CompilationUnit* cu, unsigned int code_offset)
buzbeef58c12c2012-07-03 15:06:29 -0700262{
buzbeefa57c472012-11-21 12:06:18 -0800263 return FindBlock(cu, code_offset, false, false, NULL);
buzbeef58c12c2012-07-03 15:06:29 -0700264}
265
buzbeead8f15e2012-06-18 14:49:45 -0700266/* Turn method name into a legal Linux file name */
buzbee52a77fc2012-11-20 19:50:46 -0800267void ReplaceSpecialChars(std::string& str)
buzbeead8f15e2012-06-18 14:49:45 -0700268{
269 static const struct { const char before; const char after; } match[] =
270 {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'},
271 {'(','@'}, {')','@'}, {'<','='}, {'>','='}};
272 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
273 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
274 }
275}
276
buzbee67bf8852011-08-17 17:51:35 -0700277/* Dump the CFG into a DOT graph */
buzbeefa57c472012-11-21 12:06:18 -0800278void DumpCFG(CompilationUnit* cu, const char* dir_prefix)
buzbee67bf8852011-08-17 17:51:35 -0700279{
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 FILE* file;
buzbeefa57c472012-11-21 12:06:18 -0800281 std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -0800282 ReplaceSpecialChars(fname);
buzbeefa57c472012-11-21 12:06:18 -0800283 fname = StringPrintf("%s%s%x.dot", dir_prefix, fname.c_str(),
284 cu->entry_block->fall_through->start_offset);
buzbeead8f15e2012-06-18 14:49:45 -0700285 file = fopen(fname.c_str(), "w");
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 if (file == NULL) {
287 return;
288 }
289 fprintf(file, "digraph G {\n");
290
291 fprintf(file, " rankdir=TB\n");
292
buzbeefa57c472012-11-21 12:06:18 -0800293 int num_reachable_blocks = cu->num_reachable_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 int idx;
buzbeefa57c472012-11-21 12:06:18 -0800295 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700296
buzbeefa57c472012-11-21 12:06:18 -0800297 for (idx = 0; idx < num_reachable_blocks; idx++) {
298 int block_idx = cu->dfs_order.elem_list[idx];
299 BasicBlock *bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, block_idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -0800301 if (bb->block_type == kDead) continue;
302 if (bb->block_type == kEntryBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700303 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800304 } else if (bb->block_type == kExitBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700305 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
buzbeefa57c472012-11-21 12:06:18 -0800306 } else if (bb->block_type == kDalvikByteCode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700307 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800308 bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 const MIR *mir;
310 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
buzbeefa57c472012-11-21 12:06:18 -0800311 bb->first_mir_insn ? " | " : " ");
312 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700313 fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset,
buzbeefa57c472012-11-21 12:06:18 -0800314 mir->ssa_rep ? FullDisassembler(cu, mir) :
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 Instruction::Name(mir->dalvikInsn.opcode),
316 mir->next ? " | " : " ");
317 }
318 fprintf(file, " }\"];\n\n");
buzbeefa57c472012-11-21 12:06:18 -0800319 } else if (bb->block_type == kExceptionHandling) {
320 char block_name[BLOCK_NAME_LEN];
Bill Buzbeea114add2012-05-03 15:00:40 -0700321
buzbeefa57c472012-11-21 12:06:18 -0800322 GetBlockName(bb, block_name);
323 fprintf(file, " %s [shape=invhouse];\n", block_name);
buzbee67bf8852011-08-17 17:51:35 -0700324 }
buzbee67bf8852011-08-17 17:51:35 -0700325
buzbeefa57c472012-11-21 12:06:18 -0800326 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
buzbee67bf8852011-08-17 17:51:35 -0700327
Bill Buzbeea114add2012-05-03 15:00:40 -0700328 if (bb->taken) {
buzbeefa57c472012-11-21 12:06:18 -0800329 GetBlockName(bb, block_name1);
330 GetBlockName(bb->taken, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
buzbeefa57c472012-11-21 12:06:18 -0800332 block_name1, block_name2);
buzbee67bf8852011-08-17 17:51:35 -0700333 }
buzbeefa57c472012-11-21 12:06:18 -0800334 if (bb->fall_through) {
335 GetBlockName(bb, block_name1);
336 GetBlockName(bb->fall_through, block_name2);
337 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 }
339
buzbeefa57c472012-11-21 12:06:18 -0800340 if (bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700341 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
buzbeefa57c472012-11-21 12:06:18 -0800342 bb->start_offset, bb->id,
343 (bb->successor_block_list.block_list_type == kCatch) ?
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 "Mrecord" : "record");
345 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800346 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700347 &iterator);
buzbeefa57c472012-11-21 12:06:18 -0800348 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800349 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700350
buzbeefa57c472012-11-21 12:06:18 -0800351 int succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800353 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700354
buzbeefa57c472012-11-21 12:06:18 -0800355 BasicBlock *dest_block = successor_block_info->block;
356 SuccessorBlockInfo *next_successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800357 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700358
359 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
buzbeefa57c472012-11-21 12:06:18 -0800360 succ_id++,
361 successor_block_info->key,
362 dest_block->start_offset,
363 (next_successor_block_info != NULL) ? " | " : " ");
Bill Buzbeea114add2012-05-03 15:00:40 -0700364
buzbeefa57c472012-11-21 12:06:18 -0800365 successor_block_info = next_successor_block_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 }
367 fprintf(file, " }\"];\n\n");
368
buzbeefa57c472012-11-21 12:06:18 -0800369 GetBlockName(bb, block_name1);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700370 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
buzbeefa57c472012-11-21 12:06:18 -0800371 block_name1, bb->start_offset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700372
buzbeefa57c472012-11-21 12:06:18 -0800373 if (bb->successor_block_list.block_list_type == kPackedSwitch ||
374 bb->successor_block_list.block_list_type == kSparseSwitch) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700375
buzbeefa57c472012-11-21 12:06:18 -0800376 GrowableListIteratorInit(&bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 &iterator);
378
buzbeefa57c472012-11-21 12:06:18 -0800379 succ_id = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800381 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800382 reinterpret_cast<SuccessorBlockInfo*>( GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800383 if (successor_block_info == NULL) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700384
buzbeefa57c472012-11-21 12:06:18 -0800385 BasicBlock *dest_block = successor_block_info->block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700386
buzbeefa57c472012-11-21 12:06:18 -0800387 GetBlockName(dest_block, block_name2);
388 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
389 bb->id, succ_id++, block_name2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 }
391 }
392 }
393 fprintf(file, "\n");
394
395 /* Display the dominator tree */
buzbeefa57c472012-11-21 12:06:18 -0800396 GetBlockName(bb, block_name1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
buzbeefa57c472012-11-21 12:06:18 -0800398 block_name1, block_name1);
399 if (bb->i_dom) {
400 GetBlockName(bb->i_dom, block_name2);
401 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 }
403 }
404 fprintf(file, "}\n");
405 fclose(file);
buzbee67bf8852011-08-17 17:51:35 -0700406}
407
408/* Verify if all the successor is connected with all the claimed predecessors */
buzbeefa57c472012-11-21 12:06:18 -0800409static bool VerifyPredInfo(CompilationUnit* cu, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700410{
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700412
buzbee52a77fc2012-11-20 19:50:46 -0800413 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800415 BasicBlock *pred_bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
416 if (!pred_bb) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800418 if (pred_bb->taken == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800420 } else if (pred_bb->fall_through == bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 found = true;
buzbeefa57c472012-11-21 12:06:18 -0800422 } else if (pred_bb->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800424 GrowableListIteratorInit(&pred_bb->successor_block_list.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 &iterator);
426 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800427 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -0800428 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800429 if (successor_block_info == NULL) break;
430 BasicBlock *succ_bb = successor_block_info->block;
431 if (succ_bb == bb) {
buzbee67bf8852011-08-17 17:51:35 -0700432 found = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 break;
buzbee67bf8852011-08-17 17:51:35 -0700434 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 }
buzbee67bf8852011-08-17 17:51:35 -0700436 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 if (found == false) {
buzbeefa57c472012-11-21 12:06:18 -0800438 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
439 GetBlockName(bb, block_name1);
440 GetBlockName(pred_bb, block_name2);
441 DumpCFG(cu, "/sdcard/cfg/");
442 LOG(FATAL) << "Successor " << block_name1 << "not found from "
443 << block_name2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 }
445 }
446 return true;
buzbee67bf8852011-08-17 17:51:35 -0700447}
448
449/* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800450static void ProcessTryCatchBlocks(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700451{
buzbeefa57c472012-11-21 12:06:18 -0800452 const DexFile::CodeItem* code_item = cu->code_item;
453 int tries_size = code_item->tries_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 int offset;
buzbee67bf8852011-08-17 17:51:35 -0700455
buzbeefa57c472012-11-21 12:06:18 -0800456 if (tries_size == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 return;
458 }
459
buzbeefa57c472012-11-21 12:06:18 -0800460 ArenaBitVector* try_block_addr = cu->try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700461
buzbeefa57c472012-11-21 12:06:18 -0800462 for (int i = 0; i < tries_size; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 const DexFile::TryItem* pTry =
464 DexFile::GetTryItems(*code_item, i);
buzbeefa57c472012-11-21 12:06:18 -0800465 int start_offset = pTry->start_addr_;
466 int end_offset = start_offset + pTry->insn_count_;
467 for (offset = start_offset; offset < end_offset; offset++) {
468 SetBit(cu, try_block_addr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700469 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700470 }
buzbee67bf8852011-08-17 17:51:35 -0700471
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 // Iterate over each of the handlers to enqueue the empty Catch blocks
473 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
474 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
475 for (uint32_t idx = 0; idx < handlers_size; idx++) {
476 CatchHandlerIterator iterator(handlers_ptr);
477 for (; iterator.HasNext(); iterator.Next()) {
478 uint32_t address = iterator.GetHandlerAddress();
buzbeefa57c472012-11-21 12:06:18 -0800479 FindBlock(cu, address, false /* split */, true /*create*/,
480 /* immed_pred_block_p */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700481 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 handlers_ptr = iterator.EndDataPointer();
483 }
buzbee67bf8852011-08-17 17:51:35 -0700484}
485
Elliott Hughesadb8c672012-03-06 16:49:32 -0800486/* Process instructions with the kBranch flag */
buzbeefa57c472012-11-21 12:06:18 -0800487static BasicBlock* ProcessCanBranch(CompilationUnit* cu, BasicBlock* cur_block,
488 MIR* insn, int cur_offset, int width, int flags,
489 const uint16_t* code_ptr, const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700490{
buzbeefa57c472012-11-21 12:06:18 -0800491 int target = cur_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700492 switch (insn->dalvikInsn.opcode) {
493 case Instruction::GOTO:
494 case Instruction::GOTO_16:
495 case Instruction::GOTO_32:
buzbeecbd6d442012-11-17 14:11:25 -0800496 target += insn->dalvikInsn.vA;
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 break;
498 case Instruction::IF_EQ:
499 case Instruction::IF_NE:
500 case Instruction::IF_LT:
501 case Instruction::IF_GE:
502 case Instruction::IF_GT:
503 case Instruction::IF_LE:
buzbeefa57c472012-11-21 12:06:18 -0800504 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800505 target += insn->dalvikInsn.vC;
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 break;
507 case Instruction::IF_EQZ:
508 case Instruction::IF_NEZ:
509 case Instruction::IF_LTZ:
510 case Instruction::IF_GEZ:
511 case Instruction::IF_GTZ:
512 case Instruction::IF_LEZ:
buzbeefa57c472012-11-21 12:06:18 -0800513 cur_block->conditional_branch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800514 target += insn->dalvikInsn.vB;
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 break;
516 default:
buzbeecbd6d442012-11-17 14:11:25 -0800517 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
Bill Buzbeea114add2012-05-03 15:00:40 -0700518 }
buzbeefa57c472012-11-21 12:06:18 -0800519 BasicBlock *taken_block = FindBlock(cu, target,
Bill Buzbeea114add2012-05-03 15:00:40 -0700520 /* split */
521 true,
522 /* create */
523 true,
buzbeefa57c472012-11-21 12:06:18 -0800524 /* immed_pred_block_p */
525 &cur_block);
526 cur_block->taken = taken_block;
527 InsertGrowableList(cu, taken_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700528
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 /* Always terminate the current block for conditional branches */
530 if (flags & Instruction::kContinue) {
buzbeefa57c472012-11-21 12:06:18 -0800531 BasicBlock *fallthrough_block = FindBlock(cu,
532 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700533 /*
534 * If the method is processed
535 * in sequential order from the
536 * beginning, we don't need to
537 * specify split for continue
538 * blocks. However, this
539 * routine can be called by
540 * compileLoop, which starts
541 * parsing the method from an
542 * arbitrary address in the
543 * method body.
544 */
545 true,
546 /* create */
547 true,
buzbeefa57c472012-11-21 12:06:18 -0800548 /* immed_pred_block_p */
549 &cur_block);
550 cur_block->fall_through = fallthrough_block;
551 InsertGrowableList(cu, fallthrough_block->predecessors,
552 reinterpret_cast<uintptr_t>(cur_block));
553 } else if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700554 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbeefa57c472012-11-21 12:06:18 -0800555 if (ContentIsInsn(code_ptr)) {
556 FindBlock(cu, cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 /* split */
558 false,
559 /* create */
560 true,
buzbeefa57c472012-11-21 12:06:18 -0800561 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700563 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700564 }
buzbeefa57c472012-11-21 12:06:18 -0800565 return cur_block;
buzbee67bf8852011-08-17 17:51:35 -0700566}
567
Elliott Hughesadb8c672012-03-06 16:49:32 -0800568/* Process instructions with the kSwitch flag */
buzbeefa57c472012-11-21 12:06:18 -0800569static void ProcessCanSwitch(CompilationUnit* cu, BasicBlock* cur_block,
570 MIR* insn, int cur_offset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700571{
buzbeefa57c472012-11-21 12:06:18 -0800572 const uint16_t* switch_data =
573 reinterpret_cast<const uint16_t*>(cu->insns + cur_offset + insn->dalvikInsn.vB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 int size;
buzbeecbd6d442012-11-17 14:11:25 -0800575 const int* keyTable;
buzbeefa57c472012-11-21 12:06:18 -0800576 const int* target_table;
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 int i;
buzbeefa57c472012-11-21 12:06:18 -0800578 int first_key;
buzbee67bf8852011-08-17 17:51:35 -0700579
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 /*
581 * Packed switch data format:
582 * ushort ident = 0x0100 magic value
583 * ushort size number of entries in the table
584 * int first_key first (and lowest) switch case value
585 * int targets[size] branch targets, relative to switch opcode
586 *
587 * Total size is (4+size*2) 16-bit code units.
588 */
589 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
buzbeefa57c472012-11-21 12:06:18 -0800590 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800592 size = switch_data[1];
593 first_key = switch_data[2] | (switch_data[3] << 16);
594 target_table = reinterpret_cast<const int*>(&switch_data[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 keyTable = NULL; // Make the compiler happy
596 /*
597 * Sparse switch data format:
598 * ushort ident = 0x0200 magic value
599 * ushort size number of entries in the table; > 0
600 * int keys[size] keys, sorted low-to-high; 32-bit aligned
601 * int targets[size] branch targets, relative to switch opcode
602 *
603 * Total size is (2+size*4) 16-bit code units.
604 */
605 } else {
buzbeefa57c472012-11-21 12:06:18 -0800606 DCHECK_EQ(static_cast<int>(switch_data[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 static_cast<int>(Instruction::kSparseSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800608 size = switch_data[1];
609 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
610 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
611 first_key = 0; // To make the compiler happy
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 }
buzbee67bf8852011-08-17 17:51:35 -0700613
buzbeefa57c472012-11-21 12:06:18 -0800614 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800616 << static_cast<int>(cur_block->successor_block_list.block_list_type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 }
buzbeefa57c472012-11-21 12:06:18 -0800618 cur_block->successor_block_list.block_list_type =
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
620 kPackedSwitch : kSparseSwitch;
buzbeefa57c472012-11-21 12:06:18 -0800621 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 kListSuccessorBlocks);
623
624 for (i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800625 BasicBlock *case_block = FindBlock(cu, cur_offset + target_table[i],
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 /* split */
627 true,
628 /* create */
629 true,
buzbeefa57c472012-11-21 12:06:18 -0800630 /* immed_pred_block_p */
631 &cur_block);
632 SuccessorBlockInfo *successor_block_info =
633 static_cast<SuccessorBlockInfo*>(NewMem(cu, sizeof(SuccessorBlockInfo),
buzbeecbd6d442012-11-17 14:11:25 -0800634 false, kAllocSuccessor));
buzbeefa57c472012-11-21 12:06:18 -0800635 successor_block_info->block = case_block;
636 successor_block_info->key =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
buzbeefa57c472012-11-21 12:06:18 -0800638 first_key + i : keyTable[i];
639 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
640 reinterpret_cast<uintptr_t>(successor_block_info));
641 InsertGrowableList(cu, case_block->predecessors,
642 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700643 }
644
645 /* Fall-through case */
buzbeefa57c472012-11-21 12:06:18 -0800646 BasicBlock* fallthrough_block = FindBlock(cu,
647 cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 /* split */
649 false,
650 /* create */
651 true,
buzbeefa57c472012-11-21 12:06:18 -0800652 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 NULL);
buzbeefa57c472012-11-21 12:06:18 -0800654 cur_block->fall_through = fallthrough_block;
655 InsertGrowableList(cu, fallthrough_block->predecessors,
656 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700657}
658
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659/* Process instructions with the kThrow flag */
buzbeefa57c472012-11-21 12:06:18 -0800660static BasicBlock* ProcessCanThrow(CompilationUnit* cu, BasicBlock* cur_block,
661 MIR* insn, int cur_offset, int width, int flags,
662 ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
663 const uint16_t* code_end)
buzbee67bf8852011-08-17 17:51:35 -0700664{
buzbeefa57c472012-11-21 12:06:18 -0800665 const DexFile::CodeItem* code_item = cu->code_item;
666 bool in_try_block = IsBitSet(try_block_addr, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700667
Bill Buzbeea114add2012-05-03 15:00:40 -0700668 /* In try block */
buzbeefa57c472012-11-21 12:06:18 -0800669 if (in_try_block) {
670 CatchHandlerIterator iterator(*code_item, cur_offset);
buzbee67bf8852011-08-17 17:51:35 -0700671
buzbeefa57c472012-11-21 12:06:18 -0800672 if (cur_block->successor_block_list.block_list_type != kNotUsed) {
673 LOG(INFO) << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 LOG(FATAL) << "Successor block list already in use: "
buzbeefa57c472012-11-21 12:06:18 -0800675 << static_cast<int>(cur_block->successor_block_list.block_list_type);
buzbee67bf8852011-08-17 17:51:35 -0700676 }
677
buzbeefa57c472012-11-21 12:06:18 -0800678 cur_block->successor_block_list.block_list_type = kCatch;
679 CompilerInitGrowableList(cu, &cur_block->successor_block_list.blocks, 2,
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 kListSuccessorBlocks);
681
682 for (;iterator.HasNext(); iterator.Next()) {
buzbeefa57c472012-11-21 12:06:18 -0800683 BasicBlock *catch_block = FindBlock(cu, iterator.GetHandlerAddress(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700684 false /* split*/,
685 false /* creat */,
buzbeefa57c472012-11-21 12:06:18 -0800686 NULL /* immed_pred_block_p */);
687 catch_block->catch_entry = true;
688 cu->catches.insert(catch_block->start_offset);
689 SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
690 (NewMem(cu, sizeof(SuccessorBlockInfo), false, kAllocSuccessor));
691 successor_block_info->block = catch_block;
692 successor_block_info->key = iterator.GetHandlerTypeIndex();
693 InsertGrowableList(cu, &cur_block->successor_block_list.blocks,
694 reinterpret_cast<uintptr_t>(successor_block_info));
695 InsertGrowableList(cu, catch_block->predecessors,
696 reinterpret_cast<uintptr_t>(cur_block));
buzbee67bf8852011-08-17 17:51:35 -0700697 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 } else {
buzbeefa57c472012-11-21 12:06:18 -0800699 BasicBlock *eh_block = NewMemBB(cu, kExceptionHandling,
700 cu->num_blocks++);
701 cur_block->taken = eh_block;
702 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(eh_block));
703 eh_block->start_offset = cur_offset;
704 InsertGrowableList(cu, eh_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 }
706
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700707 if (insn->dalvikInsn.opcode == Instruction::THROW){
buzbeefa57c472012-11-21 12:06:18 -0800708 cur_block->explicit_throw = true;
709 if ((code_ptr < code_end) && ContentIsInsn(code_ptr)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700710 // Force creation of new block following THROW via side-effect
buzbeefa57c472012-11-21 12:06:18 -0800711 FindBlock(cu, cur_offset + width, /* split */ false,
712 /* create */ true, /* immed_pred_block_p */ NULL);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700713 }
buzbeefa57c472012-11-21 12:06:18 -0800714 if (!in_try_block) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700715 // Don't split a THROW that can't rethrow - we're done.
buzbeefa57c472012-11-21 12:06:18 -0800716 return cur_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700717 }
718 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700719
720 /*
721 * Split the potentially-throwing instruction into two parts.
722 * The first half will be a pseudo-op that captures the exception
723 * edges and terminates the basic block. It always falls through.
724 * Then, create a new basic block that begins with the throwing instruction
725 * (minus exceptions). Note: this new basic block must NOT be entered into
buzbeefa57c472012-11-21 12:06:18 -0800726 * the block_map. If the potentially-throwing instruction is the target of a
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700727 * future branch, we need to find the check psuedo half. The new
728 * basic block containing the work portion of the instruction should
729 * only be entered via fallthrough from the block containing the
730 * pseudo exception edge MIR. Note also that this new block is
731 * not automatically terminated after the work portion, and may
732 * contain following instructions.
733 */
buzbeefa57c472012-11-21 12:06:18 -0800734 BasicBlock *new_block = NewMemBB(cu, kDalvikByteCode, cu->num_blocks++);
735 InsertGrowableList(cu, &cu->block_list, reinterpret_cast<uintptr_t>(new_block));
736 new_block->start_offset = insn->offset;
737 cur_block->fall_through = new_block;
738 InsertGrowableList(cu, new_block->predecessors, reinterpret_cast<uintptr_t>(cur_block));
739 MIR* new_insn = static_cast<MIR*>(NewMem(cu, sizeof(MIR), true, kAllocMIR));
740 *new_insn = *insn;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700741 insn->dalvikInsn.opcode =
742 static_cast<Instruction::Code>(kMirOpCheck);
743 // Associate the two halves
buzbeefa57c472012-11-21 12:06:18 -0800744 insn->meta.throw_insn = new_insn;
745 new_insn->meta.throw_insn = insn;
746 AppendMIR(new_block, new_insn);
747 return new_block;
buzbee67bf8852011-08-17 17:51:35 -0700748}
749
buzbeefa57c472012-11-21 12:06:18 -0800750void CompilerInit(CompilationUnit* cu, const Compiler& compiler) {
buzbee02031b12012-11-23 09:41:35 -0800751 bool success = false;
752 switch (compiler.GetInstructionSet()) {
753 case kThumb2:
754 success = InitArmCodegen(cu);
755 break;
756 case kMips:
757 success = InitMipsCodegen(cu);
758 break;
759 case kX86:
760 success = InitX86Codegen(cu);
761 break;
762 default:;
763 }
764 if (!success) {
765 LOG(FATAL) << "Failed to initialize codegen for " << compiler.GetInstructionSet();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800766 }
buzbeefa57c472012-11-21 12:06:18 -0800767 if (!HeapInit(cu)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800768 LOG(FATAL) << "Failed to initialize oat heap";
769 }
770}
771
buzbee52a77fc2012-11-20 19:50:46 -0800772static CompiledMethod* CompileMethod(Compiler& compiler,
buzbeefa57c472012-11-21 12:06:18 -0800773 const CompilerBackend compiler_backend,
buzbee52a77fc2012-11-20 19:50:46 -0800774 const DexFile::CodeItem* code_item,
775 uint32_t access_flags, InvokeType invoke_type,
776 uint32_t method_idx, jobject class_loader,
777 const DexFile& dex_file,
778 LLVMInfo* llvm_info)
buzbee67bf8852011-08-17 17:51:35 -0700779{
Bill Buzbeea114add2012-05-03 15:00:40 -0700780 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700781
buzbeefa57c472012-11-21 12:06:18 -0800782 const uint16_t* code_ptr = code_item->insns_;
783 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
784 int num_blocks = 0;
785 unsigned int cur_offset = 0;
buzbee67bf8852011-08-17 17:51:35 -0700786
Bill Buzbeea114add2012-05-03 15:00:40 -0700787 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
buzbeefa57c472012-11-21 12:06:18 -0800788 UniquePtr<CompilationUnit> cu(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800789
buzbeefa57c472012-11-21 12:06:18 -0800790 CompilerInit(cu.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800791
buzbeefa57c472012-11-21 12:06:18 -0800792 cu->compiler = &compiler;
793 cu->class_linker = class_linker;
794 cu->dex_file = &dex_file;
795 cu->method_idx = method_idx;
796 cu->code_item = code_item;
797 cu->access_flags = access_flags;
798 cu->invoke_type = invoke_type;
799 cu->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
800 cu->instruction_set = compiler.GetInstructionSet();
801 cu->insns = code_item->insns_;
802 cu->insns_size = code_item->insns_size_in_code_units_;
803 cu->num_ins = code_item->ins_size_;
804 cu->num_regs = code_item->registers_size_ - cu->num_ins;
805 cu->num_outs = code_item->outs_size_;
806 DCHECK((cu->instruction_set == kThumb2) ||
807 (cu->instruction_set == kX86) ||
808 (cu->instruction_set == kMips));
809 if ((compiler_backend == kQuickGBC) || (compiler_backend == kPortable)) {
810 cu->gen_bitcode = true;
buzbee85eee022012-07-16 22:12:38 -0700811 }
buzbeefa57c472012-11-21 12:06:18 -0800812 DCHECK_NE(compiler_backend, kIceland); // TODO: remove when Portable/Iceland merge complete
buzbeec531cef2012-10-18 07:09:20 -0700813 // TODO: remove this once x86 is tested
buzbeefa57c472012-11-21 12:06:18 -0800814 if (cu->gen_bitcode && (cu->instruction_set != kThumb2)) {
buzbeec531cef2012-10-18 07:09:20 -0700815 UNIMPLEMENTED(WARNING) << "GBC generation untested for non-Thumb targets";
816 }
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
838#if 1
839// *** Temporary ****
840// For use in debugging issue 7250540. Disable optimization in problem method
841// to see if monkey results change. Should be removed after monkey runs
842// complete.
843if (PrettyMethod(method_idx, dex_file).find("void com.android.inputmethod.keyboard.Key.<init>(android.content.res.Resources, com.android.inputmethod.keyboard.Keyboard$Params, com.android.inputmethod.keyboard.Keyboard$Builder$Row, org.xmlpull.v1.XmlPullParser)") != std::string::npos) {
buzbeefa57c472012-11-21 12:06:18 -0800844 cu->disable_opt |= (
buzbee9281f002012-10-24 12:17:24 -0700845 (1 << kLoadStoreElimination) |
846 (1 << kLoadHoisting) |
847 (1 << kSuppressLoads) |
buzbee663c09f2012-10-31 05:21:00 -0700848 //(1 << kNullCheckElimination) |
849 //(1 << kPromoteRegs) |
buzbee9281f002012-10-24 12:17:24 -0700850 (1 << kTrackLiveTemps) |
buzbee663c09f2012-10-31 05:21:00 -0700851 //(1 << kSkipLargeMethodOptimization) |
852 //(1 << kSafeOptimizations) |
buzbee9281f002012-10-24 12:17:24 -0700853 (1 << kBBOpt) |
854 (1 << kMatch) |
buzbee663c09f2012-10-31 05:21:00 -0700855 //(1 << kPromoteCompilerTemps) |
856 0);
buzbee9281f002012-10-24 12:17:24 -0700857}
858#endif
859
buzbeefa57c472012-11-21 12:06:18 -0800860 if (cu->instruction_set == kMips) {
jeffhao7fbee072012-08-24 17:56:54 -0700861 // Disable some optimizations for mips for now
buzbeefa57c472012-11-21 12:06:18 -0800862 cu->disable_opt |= (
jeffhao7fbee072012-08-24 17:56:54 -0700863 (1 << kLoadStoreElimination) |
864 (1 << kLoadHoisting) |
865 (1 << kSuppressLoads) |
866 (1 << kNullCheckElimination) |
867 (1 << kPromoteRegs) |
868 (1 << kTrackLiveTemps) |
869 (1 << kSkipLargeMethodOptimization) |
870 (1 << kSafeOptimizations) |
871 (1 << kBBOpt) |
872 (1 << kMatch) |
873 (1 << kPromoteCompilerTemps));
874 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700875
876 /* Gathering opcode stats? */
877 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbeefa57c472012-11-21 12:06:18 -0800878 cu->opcode_count =
879 static_cast<int*>(NewMem(cu.get(), kNumPackedOpcodes * sizeof(int), true, kAllocMisc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 }
881
882 /* Assume non-throwing leaf */
buzbeefa57c472012-11-21 12:06:18 -0800883 cu->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
Bill Buzbeea114add2012-05-03 15:00:40 -0700884
buzbeefa57c472012-11-21 12:06:18 -0800885 /* Initialize the block list, estimate size based on insns_size */
886 CompilerInitGrowableList(cu.get(), &cu->block_list, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700887 kListBlockList);
888
buzbeefa57c472012-11-21 12:06:18 -0800889 /* Initialize the switch_tables list */
890 CompilerInitGrowableList(cu.get(), &cu->switch_tables, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700891 kListSwitchTables);
892
buzbeefa57c472012-11-21 12:06:18 -0800893 /* Intialize the fill_array_data list */
894 CompilerInitGrowableList(cu.get(), &cu->fill_array_data, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 kListFillArrayData);
896
buzbeefa57c472012-11-21 12:06:18 -0800897 /* Intialize the throw_launchpads list, estimate size based on insns_size */
898 CompilerInitGrowableList(cu.get(), &cu->throw_launchpads, cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700899 kListThrowLaunchPads);
900
buzbeefa57c472012-11-21 12:06:18 -0800901 /* Intialize the instrinsic_launchpads list */
902 CompilerInitGrowableList(cu.get(), &cu->intrinsic_launchpads, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700903 kListMisc);
904
905
buzbeefa57c472012-11-21 12:06:18 -0800906 /* Intialize the suspend_launchpads list */
907 CompilerInitGrowableList(cu.get(), &cu->suspend_launchpads, 2048,
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 kListSuspendLaunchPads);
909
910 /* Allocate the bit-vector to track the beginning of basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800911 ArenaBitVector *try_block_addr = AllocBitVector(cu.get(),
912 cu->insns_size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 true /* expandable */);
buzbeefa57c472012-11-21 12:06:18 -0800914 cu->try_block_addr = try_block_addr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700915
916 /* Create the default entry and exit blocks and enter them to the list */
buzbeefa57c472012-11-21 12:06:18 -0800917 BasicBlock *entry_block = NewMemBB(cu.get(), kEntryBlock, num_blocks++);
918 BasicBlock *exit_block = NewMemBB(cu.get(), kExitBlock, num_blocks++);
Bill Buzbeea114add2012-05-03 15:00:40 -0700919
buzbeefa57c472012-11-21 12:06:18 -0800920 cu->entry_block = entry_block;
921 cu->exit_block = exit_block;
Bill Buzbeea114add2012-05-03 15:00:40 -0700922
buzbeefa57c472012-11-21 12:06:18 -0800923 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(entry_block));
924 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(exit_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700925
926 /* Current block to record parsed instructions */
buzbeefa57c472012-11-21 12:06:18 -0800927 BasicBlock *cur_block = NewMemBB(cu.get(), kDalvikByteCode, num_blocks++);
928 cur_block->start_offset = 0;
929 InsertGrowableList(cu.get(), &cu->block_list, reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 /* Add first block to the fast lookup cache */
buzbeefa57c472012-11-21 12:06:18 -0800931 cu->block_map.Put(cur_block->start_offset, cur_block);
932 entry_block->fall_through = cur_block;
933 InsertGrowableList(cu.get(), cur_block->predecessors,
934 reinterpret_cast<uintptr_t>(entry_block));
Bill Buzbeea114add2012-05-03 15:00:40 -0700935
936 /*
937 * Store back the number of blocks since new blocks may be created of
buzbeefa57c472012-11-21 12:06:18 -0800938 * accessing cu.
Bill Buzbeea114add2012-05-03 15:00:40 -0700939 */
buzbeefa57c472012-11-21 12:06:18 -0800940 cu->num_blocks = num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -0700941
942 /* Identify code range in try blocks and set up the empty catch blocks */
buzbeefa57c472012-11-21 12:06:18 -0800943 ProcessTryCatchBlocks(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -0700944
945 /* Set up for simple method detection */
buzbeefa57c472012-11-21 12:06:18 -0800946 int num_patterns = sizeof(special_patterns)/sizeof(special_patterns[0]);
947 bool live_pattern = (num_patterns > 0) && !(cu->disable_opt & (1 << kMatch));
948 bool* dead_pattern =
949 static_cast<bool*>(NewMem(cu.get(), sizeof(bool) * num_patterns, true, kAllocMisc));
950 SpecialCaseHandler special_case = kNoHandler;
951 int pattern_pos = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700952
953 /* Parse all instructions and put them into containing basic blocks */
buzbeefa57c472012-11-21 12:06:18 -0800954 while (code_ptr < code_end) {
955 MIR *insn = static_cast<MIR *>(NewMem(cu.get(), sizeof(MIR), true, kAllocMIR));
956 insn->offset = cur_offset;
957 int width = ParseInsn(cu.get(), code_ptr, &insn->dalvikInsn, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700958 insn->width = width;
959 Instruction::Code opcode = insn->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800960 if (cu->opcode_count != NULL) {
961 cu->opcode_count[static_cast<int>(opcode)]++;
buzbee44b412b2012-02-04 08:50:53 -0800962 }
963
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 /* Terminate when the data section is seen */
965 if (width == 0)
966 break;
967
968 /* Possible simple method? */
buzbeefa57c472012-11-21 12:06:18 -0800969 if (live_pattern) {
970 live_pattern = false;
971 special_case = kNoHandler;
972 for (int i = 0; i < num_patterns; i++) {
973 if (!dead_pattern[i]) {
974 if (special_patterns[i].opcodes[pattern_pos] == opcode) {
975 live_pattern = true;
976 special_case = special_patterns[i].handler_code;
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 } else {
buzbeefa57c472012-11-21 12:06:18 -0800978 dead_pattern[i] = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700979 }
980 }
981 }
buzbeefa57c472012-11-21 12:06:18 -0800982 pattern_pos++;
buzbeea7c12682012-03-19 13:13:53 -0700983 }
984
buzbeefa57c472012-11-21 12:06:18 -0800985 AppendMIR(cur_block, insn);
buzbeecefd1872011-09-09 09:59:52 -0700986
buzbeefa57c472012-11-21 12:06:18 -0800987 code_ptr += width;
Ian Rogersa75a0132012-09-28 11:41:42 -0700988 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700989
buzbeefa57c472012-11-21 12:06:18 -0800990 int df_flags = oat_data_flow_attributes[insn->dalvikInsn.opcode];
buzbee67bf8852011-08-17 17:51:35 -0700991
buzbeefa57c472012-11-21 12:06:18 -0800992 if (df_flags & DF_HAS_DEFS) {
993 cu->def_count += (df_flags & DF_A_WIDE) ? 2 : 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700994 }
buzbee67bf8852011-08-17 17:51:35 -0700995
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 if (flags & Instruction::kBranch) {
buzbeefa57c472012-11-21 12:06:18 -0800997 cur_block = ProcessCanBranch(cu.get(), cur_block, insn, cur_offset,
998 width, flags, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -0700999 } else if (flags & Instruction::kReturn) {
buzbeefa57c472012-11-21 12:06:18 -08001000 cur_block->fall_through = exit_block;
1001 InsertGrowableList(cu.get(), exit_block->predecessors,
1002 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -07001003 /*
1004 * Terminate the current block if there are instructions
1005 * afterwards.
1006 */
buzbeefa57c472012-11-21 12:06:18 -08001007 if (code_ptr < code_end) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 /*
1009 * Create a fallthrough block for real instructions
1010 * (incl. NOP).
1011 */
buzbeefa57c472012-11-21 12:06:18 -08001012 if (ContentIsInsn(code_ptr)) {
1013 FindBlock(cu.get(), cur_offset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 /* split */
1015 false,
1016 /* create */
1017 true,
buzbeefa57c472012-11-21 12:06:18 -08001018 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 NULL);
1020 }
1021 }
1022 } else if (flags & Instruction::kThrow) {
buzbeefa57c472012-11-21 12:06:18 -08001023 cur_block = ProcessCanThrow(cu.get(), cur_block, insn, cur_offset,
1024 width, flags, try_block_addr, code_ptr, code_end);
Bill Buzbeea114add2012-05-03 15:00:40 -07001025 } else if (flags & Instruction::kSwitch) {
buzbeefa57c472012-11-21 12:06:18 -08001026 ProcessCanSwitch(cu.get(), cur_block, insn, cur_offset, width, flags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 }
buzbeefa57c472012-11-21 12:06:18 -08001028 cur_offset += width;
1029 BasicBlock *next_block = FindBlock(cu.get(), cur_offset,
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 /* split */
1031 false,
1032 /* create */
1033 false,
buzbeefa57c472012-11-21 12:06:18 -08001034 /* immed_pred_block_p */
Bill Buzbeea114add2012-05-03 15:00:40 -07001035 NULL);
buzbeefa57c472012-11-21 12:06:18 -08001036 if (next_block) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001037 /*
1038 * The next instruction could be the target of a previously parsed
1039 * forward branch so a block is already created. If the current
1040 * instruction is not an unconditional branch, connect them through
1041 * the fall-through link.
1042 */
buzbeefa57c472012-11-21 12:06:18 -08001043 DCHECK(cur_block->fall_through == NULL ||
1044 cur_block->fall_through == next_block ||
1045 cur_block->fall_through == exit_block);
buzbee5ade1d22011-09-09 14:44:52 -07001046
buzbeefa57c472012-11-21 12:06:18 -08001047 if ((cur_block->fall_through == NULL) && (flags & Instruction::kContinue)) {
1048 cur_block->fall_through = next_block;
1049 InsertGrowableList(cu.get(), next_block->predecessors,
1050 reinterpret_cast<uintptr_t>(cur_block));
Bill Buzbeea114add2012-05-03 15:00:40 -07001051 }
buzbeefa57c472012-11-21 12:06:18 -08001052 cur_block = next_block;
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 }
1054 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001055
buzbeefa57c472012-11-21 12:06:18 -08001056 if (!(cu->disable_opt & (1 << kSkipLargeMethodOptimization))) {
1057 if ((cu->num_blocks > MANY_BLOCKS) ||
1058 ((cu->num_blocks > MANY_BLOCKS_INITIALIZER) &&
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 PrettyMethod(method_idx, dex_file, false).find("init>") !=
1060 std::string::npos)) {
buzbeefa57c472012-11-21 12:06:18 -08001061 cu->qd_mode = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001062 }
1063 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001064
buzbeefa57c472012-11-21 12:06:18 -08001065 if (cu->qd_mode) {
buzbeed1643e42012-09-05 14:06:51 -07001066 // Bitcode generation requires full dataflow analysis
buzbeefa57c472012-11-21 12:06:18 -08001067 cu->disable_dataflow = !cu->gen_bitcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001068 // Disable optimization which require dataflow/ssa
buzbeefa57c472012-11-21 12:06:18 -08001069 cu->disable_opt |= (1 << kBBOpt) | (1 << kPromoteRegs) | (1 << kNullCheckElimination);
1070 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001071 LOG(INFO) << "QD mode enabled: "
1072 << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001073 << " num blocks: " << cu->num_blocks;
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 }
1075 }
buzbeec1f45042011-09-21 16:03:19 -07001076
buzbeefa57c472012-11-21 12:06:18 -08001077 if (cu->verbose) {
1078 DumpCompilationUnit(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001079 }
buzbee67bf8852011-08-17 17:51:35 -07001080
buzbee0967a252012-09-14 10:43:54 -07001081 /* Do a code layout pass */
buzbeefa57c472012-11-21 12:06:18 -08001082 CodeLayout(cu.get());
buzbee0967a252012-09-14 10:43:54 -07001083
buzbeefa57c472012-11-21 12:06:18 -08001084 if (cu->enable_debug & (1 << kDebugVerifyDataflow)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001085 /* Verify if all blocks are connected as claimed */
buzbeefa57c472012-11-21 12:06:18 -08001086 DataFlowAnalysisDispatcher(cu.get(), VerifyPredInfo, kAllNodes,
1087 false /* is_iterative */);
Bill Buzbeea114add2012-05-03 15:00:40 -07001088 }
buzbee67bf8852011-08-17 17:51:35 -07001089
Bill Buzbeea114add2012-05-03 15:00:40 -07001090 /* Perform SSA transformation for the whole method */
buzbeefa57c472012-11-21 12:06:18 -08001091 SSATransformation(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001092
buzbee2cfc6392012-05-07 14:51:40 -07001093 /* Do constant propagation */
1094 // TODO: Probably need to make these expandable to support new ssa names
1095 // introducted during MIR optimization passes
buzbeefa57c472012-11-21 12:06:18 -08001096 cu->is_constant_v = AllocBitVector(cu.get(), cu->num_ssa_regs,
buzbee2cfc6392012-05-07 14:51:40 -07001097 false /* not expandable */);
buzbeefa57c472012-11-21 12:06:18 -08001098 cu->constant_values =
1099 static_cast<int*>(NewMem(cu.get(), sizeof(int) * cu->num_ssa_regs, true, kAllocDFInfo));
1100 DataFlowAnalysisDispatcher(cu.get(), DoConstantPropogation,
buzbee2cfc6392012-05-07 14:51:40 -07001101 kAllNodes,
buzbeefa57c472012-11-21 12:06:18 -08001102 false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -07001103
Bill Buzbeea114add2012-05-03 15:00:40 -07001104 /* Detect loops */
buzbeefa57c472012-11-21 12:06:18 -08001105 LoopDetection(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001106
Bill Buzbeea114add2012-05-03 15:00:40 -07001107 /* Count uses */
buzbeefa57c472012-11-21 12:06:18 -08001108 MethodUseCount(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001109
Bill Buzbeea114add2012-05-03 15:00:40 -07001110 /* Perform null check elimination */
buzbeefa57c472012-11-21 12:06:18 -08001111 NullCheckElimination(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001112
buzbeed1643e42012-09-05 14:06:51 -07001113 /* Combine basic blocks where possible */
buzbeefa57c472012-11-21 12:06:18 -08001114 BasicBlockCombine(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001115
Bill Buzbeea114add2012-05-03 15:00:40 -07001116 /* Do some basic block optimizations */
buzbeefa57c472012-11-21 12:06:18 -08001117 BasicBlockOptimization(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001118
buzbeefa57c472012-11-21 12:06:18 -08001119 if (cu->enable_debug & (1 << kDebugDumpCheckStats)) {
1120 DumpCheckStats(cu.get());
buzbeed1643e42012-09-05 14:06:51 -07001121 }
1122
buzbee02031b12012-11-23 09:41:35 -08001123 cu.get()->cg->CompilerInitializeRegAlloc(cu.get()); // Needs to happen after SSA naming
Bill Buzbeea114add2012-05-03 15:00:40 -07001124
1125 /* Allocate Registers using simple local allocation scheme */
buzbeefa57c472012-11-21 12:06:18 -08001126 SimpleRegAlloc(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001127
buzbee2cfc6392012-05-07 14:51:40 -07001128 /* Go the LLVM path? */
buzbeefa57c472012-11-21 12:06:18 -08001129 if (cu->gen_bitcode) {
buzbee2cfc6392012-05-07 14:51:40 -07001130 // MIR->Bitcode
buzbeefa57c472012-11-21 12:06:18 -08001131 MethodMIR2Bitcode(cu.get());
1132 if (compiler_backend == kPortable) {
buzbeeabc4c6b2012-08-23 08:17:15 -07001133 // all done
buzbeefa57c472012-11-21 12:06:18 -08001134 ArenaReset(cu.get());
buzbeeabc4c6b2012-08-23 08:17:15 -07001135 return NULL;
1136 }
buzbee2cfc6392012-05-07 14:51:40 -07001137 // Bitcode->LIR
buzbeefa57c472012-11-21 12:06:18 -08001138 MethodBitcode2LIR(cu.get());
buzbee2cfc6392012-05-07 14:51:40 -07001139 } else {
buzbeefa57c472012-11-21 12:06:18 -08001140 if (special_case != kNoHandler) {
buzbee2cfc6392012-05-07 14:51:40 -07001141 /*
1142 * Custom codegen for special cases. If for any reason the
buzbeefa57c472012-11-21 12:06:18 -08001143 * special codegen doesn't succeed, cu->first_lir_insn will
buzbee2cfc6392012-05-07 14:51:40 -07001144 * set to NULL;
1145 */
buzbeefa57c472012-11-21 12:06:18 -08001146 SpecialMIR2LIR(cu.get(), special_case);
buzbee2cfc6392012-05-07 14:51:40 -07001147 }
buzbee67bf8852011-08-17 17:51:35 -07001148
buzbee2cfc6392012-05-07 14:51:40 -07001149 /* Convert MIR to LIR, etc. */
buzbeefa57c472012-11-21 12:06:18 -08001150 if (cu->first_lir_insn == NULL) {
1151 MethodMIR2LIR(cu.get());
buzbee2cfc6392012-05-07 14:51:40 -07001152 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 }
buzbee67bf8852011-08-17 17:51:35 -07001154
Bill Buzbeea114add2012-05-03 15:00:40 -07001155 // Debugging only
buzbeefa57c472012-11-21 12:06:18 -08001156 if (cu->enable_debug & (1 << kDebugDumpCFG)) {
1157 DumpCFG(cu.get(), "/sdcard/cfg/");
Bill Buzbeea114add2012-05-03 15:00:40 -07001158 }
buzbee16da88c2012-03-20 10:38:17 -07001159
Bill Buzbeea114add2012-05-03 15:00:40 -07001160 /* Method is not empty */
buzbeefa57c472012-11-21 12:06:18 -08001161 if (cu->first_lir_insn) {
buzbee67bf8852011-08-17 17:51:35 -07001162
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 // mark the targets of switch statement case labels
buzbeefa57c472012-11-21 12:06:18 -08001164 ProcessSwitchTables(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001165
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 /* Convert LIR into machine code. */
buzbeefa57c472012-11-21 12:06:18 -08001167 AssembleLIR(cu.get());
buzbee99ba9642012-01-25 14:23:14 -08001168
buzbeefa57c472012-11-21 12:06:18 -08001169 if (cu->verbose) {
1170 CodegenDump(cu.get());
buzbee67bf8852011-08-17 17:51:35 -07001171 }
1172
buzbeefa57c472012-11-21 12:06:18 -08001173 if (cu->opcode_count != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001174 LOG(INFO) << "Opcode Count";
1175 for (int i = 0; i < kNumPackedOpcodes; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001176 if (cu->opcode_count[i] != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001177 LOG(INFO) << "-C- "
1178 << Instruction::Name(static_cast<Instruction::Code>(i))
buzbeefa57c472012-11-21 12:06:18 -08001179 << " " << cu->opcode_count[i];
buzbee67bf8852011-08-17 17:51:35 -07001180 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001181 }
1182 }
1183 }
buzbeea7c12682012-03-19 13:13:53 -07001184
buzbeefa57c472012-11-21 12:06:18 -08001185 // Combine vmap tables - core regs, then fp regs - into vmap_table
1186 std::vector<uint16_t> vmap_table;
buzbeeca7a5e42012-08-20 11:12:18 -07001187 // Core regs may have been inserted out of order - sort first
buzbeefa57c472012-11-21 12:06:18 -08001188 std::sort(cu->core_vmap_table.begin(), cu->core_vmap_table.end());
1189 for (size_t i = 0 ; i < cu->core_vmap_table.size(); i++) {
buzbeeca7a5e42012-08-20 11:12:18 -07001190 // Copy, stripping out the phys register sort key
buzbeefa57c472012-11-21 12:06:18 -08001191 vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & cu->core_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001192 }
1193 // If we have a frame, push a marker to take place of lr
buzbeefa57c472012-11-21 12:06:18 -08001194 if (cu->frame_size > 0) {
1195 vmap_table.push_back(INVALID_VREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001196 } else {
buzbeefa57c472012-11-21 12:06:18 -08001197 DCHECK_EQ(__builtin_popcount(cu->core_spill_mask), 0);
1198 DCHECK_EQ(__builtin_popcount(cu->fp_spill_mask), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001199 }
buzbeeca7a5e42012-08-20 11:12:18 -07001200 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
buzbeefa57c472012-11-21 12:06:18 -08001201 for (uint32_t i = 0; i < cu->fp_vmap_table.size(); i++) {
1202 vmap_table.push_back(cu->fp_vmap_table[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001203 }
1204 CompiledMethod* result =
buzbeefa57c472012-11-21 12:06:18 -08001205 new CompiledMethod(cu->instruction_set, cu->code_buffer,
1206 cu->frame_size, cu->core_spill_mask, cu->fp_spill_mask,
1207 cu->combined_mapping_table, vmap_table, cu->native_gc_map);
buzbee67bf8852011-08-17 17:51:35 -07001208
Bill Buzbeea114add2012-05-03 15:00:40 -07001209 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
buzbeefa57c472012-11-21 12:06:18 -08001210 << " (" << (cu->code_buffer.size() * sizeof(cu->code_buffer[0]))
Bill Buzbeea114add2012-05-03 15:00:40 -07001211 << " bytes)";
buzbee5abfa3e2012-01-31 17:01:43 -08001212
1213#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -08001214 if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) {
1215 DumpMemStats(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001216 }
buzbee5abfa3e2012-01-31 17:01:43 -08001217#endif
buzbee67bf8852011-08-17 17:51:35 -07001218
buzbeefa57c472012-11-21 12:06:18 -08001219 ArenaReset(cu.get());
buzbeeba938cb2012-02-03 14:47:55 -08001220
Bill Buzbeea114add2012-05-03 15:00:40 -07001221 return result;
buzbee67bf8852011-08-17 17:51:35 -07001222}
1223
buzbee52a77fc2012-11-20 19:50:46 -08001224CompiledMethod* CompileOneMethod(Compiler& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001225 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -07001226 const DexFile::CodeItem* code_item,
1227 uint32_t access_flags, InvokeType invoke_type,
1228 uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001229 const DexFile& dex_file,
buzbeefa57c472012-11-21 12:06:18 -08001230 LLVMInfo* llvm_info)
buzbeeabc4c6b2012-08-23 08:17:15 -07001231{
buzbee52a77fc2012-11-20 19:50:46 -08001232 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, method_idx, class_loader,
buzbeefa57c472012-11-21 12:06:18 -08001233 dex_file, llvm_info);
buzbeeabc4c6b2012-08-23 08:17:15 -07001234}
1235
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001236} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001237
Bill Buzbeea114add2012-05-03 15:00:40 -07001238extern "C" art::CompiledMethod*
buzbeec531cef2012-10-18 07:09:20 -07001239 ArtQuickCompileMethod(art::Compiler& compiler,
1240 const art::DexFile::CodeItem* code_item,
1241 uint32_t access_flags, art::InvokeType invoke_type,
1242 uint32_t method_idx, jobject class_loader,
1243 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001244{
buzbeec531cef2012-10-18 07:09:20 -07001245 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
1246 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -08001247 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
buzbeefa57c472012-11-21 12:06:18 -08001248 method_idx, class_loader, dex_file, NULL /* use thread llvm_info */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001249}