blob: 7fb8011f7ac24150331e473d97d8ed9c236ca800 [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"
25#include "codegen/method_bitcode.h"
26#include "codegen/method_codegen_driver.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);
buzbee4df2bbd2012-10-11 14:46:06 -070058 LLVMInfo* llvmInfo = new LLVMInfo();
59 compiler.SetCompilerContext(llvmInfo);
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
buzbee52a77fc2012-11-20 19:50:46 -0800101static inline bool ContentIsInsn(const uint16_t* codePtr) {
buzbeeeaf09bc2012-11-15 14:51:41 -0800102 uint16_t instr = *codePtr;
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800115static inline int ParseInsn(CompilationUnit* cUnit, const uint16_t* codePtr,
116 DecodedInstruction* decoded_instruction, bool printMe)
buzbee67bf8852011-08-17 17:51:35 -0700117{
Elliott Hughesadb8c672012-03-06 16:49:32 -0800118 // Don't parse instruction data
buzbee52a77fc2012-11-20 19:50:46 -0800119 if (!ContentIsInsn(codePtr)) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800120 return 0;
121 }
buzbee67bf8852011-08-17 17:51:35 -0700122
Elliott Hughesadb8c672012-03-06 16:49:32 -0800123 const Instruction* instruction = Instruction::At(codePtr);
124 *decoded_instruction = DecodedInstruction(instruction);
buzbee67bf8852011-08-17 17:51:35 -0700125
Elliott Hughesadb8c672012-03-06 16:49:32 -0800126 if (printMe) {
buzbee52a77fc2012-11-20 19:50:46 -0800127 char* decodedString = GetDalvikDisassembly(cUnit, *decoded_instruction,
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 NULL);
buzbeecbd6d442012-11-17 14:11:25 -0800129 LOG(INFO) << codePtr << ": 0x" << std::hex << static_cast<int>(decoded_instruction->opcode)
Elliott Hughesadb8c672012-03-06 16:49:32 -0800130 << " " << decodedString;
131 }
132 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -0700133}
134
135#define UNKNOWN_TARGET 0xffffffff
136
buzbee52a77fc2012-11-20 19:50:46 -0800137static inline bool IsGoto(MIR* insn) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800138 switch (insn->dalvikInsn.opcode) {
139 case Instruction::GOTO:
140 case Instruction::GOTO_16:
141 case Instruction::GOTO_32:
142 return true;
143 default:
144 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 }
buzbee67bf8852011-08-17 17:51:35 -0700146}
147
buzbee67bf8852011-08-17 17:51:35 -0700148/* Split an existing block from the specified code offset into two */
buzbee52a77fc2012-11-20 19:50:46 -0800149static BasicBlock *SplitBlock(CompilationUnit* cUnit, unsigned int codeOffset,
150 BasicBlock* origBlock, BasicBlock** immedPredBlockP)
buzbee67bf8852011-08-17 17:51:35 -0700151{
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 MIR* insn = origBlock->firstMIRInsn;
153 while (insn) {
154 if (insn->offset == codeOffset) break;
155 insn = insn->next;
156 }
157 if (insn == NULL) {
158 LOG(FATAL) << "Break split failed";
159 }
buzbee52a77fc2012-11-20 19:50:46 -0800160 BasicBlock *bottomBlock = NewMemBB(cUnit, kDalvikByteCode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 cUnit->numBlocks++);
buzbee52a77fc2012-11-20 19:50:46 -0800162 InsertGrowableList(cUnit, &cUnit->blockList, reinterpret_cast<uintptr_t>(bottomBlock));
buzbee67bf8852011-08-17 17:51:35 -0700163
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 bottomBlock->startOffset = codeOffset;
165 bottomBlock->firstMIRInsn = insn;
166 bottomBlock->lastMIRInsn = origBlock->lastMIRInsn;
buzbee67bf8852011-08-17 17:51:35 -0700167
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 /* Add it to the quick lookup cache */
169 cUnit->blockMap.Put(bottomBlock->startOffset, bottomBlock);
buzbee5b537102012-01-17 17:33:47 -0800170
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 /* Handle the taken path */
172 bottomBlock->taken = origBlock->taken;
173 if (bottomBlock->taken) {
174 origBlock->taken = NULL;
buzbee52a77fc2012-11-20 19:50:46 -0800175 DeleteGrowableList(bottomBlock->taken->predecessors, reinterpret_cast<uintptr_t>(origBlock));
176 InsertGrowableList(cUnit, bottomBlock->taken->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800177 reinterpret_cast<uintptr_t>(bottomBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 }
179
180 /* Handle the fallthrough path */
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 bottomBlock->fallThrough = origBlock->fallThrough;
182 origBlock->fallThrough = bottomBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800183 InsertGrowableList(cUnit, bottomBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800184 reinterpret_cast<uintptr_t>(origBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 if (bottomBlock->fallThrough) {
buzbee52a77fc2012-11-20 19:50:46 -0800186 DeleteGrowableList(bottomBlock->fallThrough->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800187 reinterpret_cast<uintptr_t>(origBlock));
buzbee52a77fc2012-11-20 19:50:46 -0800188 InsertGrowableList(cUnit, bottomBlock->fallThrough->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800189 reinterpret_cast<uintptr_t>(bottomBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 }
191
192 /* Handle the successor list */
193 if (origBlock->successorBlockList.blockListType != kNotUsed) {
194 bottomBlock->successorBlockList = origBlock->successorBlockList;
195 origBlock->successorBlockList.blockListType = kNotUsed;
196 GrowableListIterator iterator;
197
buzbee52a77fc2012-11-20 19:50:46 -0800198 GrowableListIteratorInit(&bottomBlock->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 &iterator);
200 while (true) {
201 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800202 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 if (successorBlockInfo == NULL) break;
204 BasicBlock *bb = successorBlockInfo->block;
buzbee52a77fc2012-11-20 19:50:46 -0800205 DeleteGrowableList(bb->predecessors, reinterpret_cast<uintptr_t>(origBlock));
206 InsertGrowableList(cUnit, bb->predecessors, reinterpret_cast<uintptr_t>(bottomBlock));
buzbee67bf8852011-08-17 17:51:35 -0700207 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 }
buzbee67bf8852011-08-17 17:51:35 -0700209
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 origBlock->lastMIRInsn = insn->prev;
buzbee67bf8852011-08-17 17:51:35 -0700211
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 insn->prev->next = NULL;
213 insn->prev = NULL;
214 /*
215 * Update the immediate predecessor block pointer so that outgoing edges
216 * can be applied to the proper block.
217 */
218 if (immedPredBlockP) {
219 DCHECK_EQ(*immedPredBlockP, origBlock);
220 *immedPredBlockP = bottomBlock;
221 }
222 return bottomBlock;
buzbee67bf8852011-08-17 17:51:35 -0700223}
224
225/*
226 * Given a code offset, find out the block that starts with it. If the offset
buzbee9ab05de2012-01-18 15:43:48 -0800227 * is in the middle of an existing block, split it into two. If immedPredBlockP
228 * is not non-null and is the block being split, update *immedPredBlockP to
229 * point to the bottom block so that outgoing edges can be set up properly
230 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800231 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700232 */
buzbee52a77fc2012-11-20 19:50:46 -0800233BasicBlock *FindBlock(CompilationUnit* cUnit, unsigned int codeOffset,
buzbee31a4a6f2012-02-28 15:36:15 -0800234 bool split, bool create, BasicBlock** immedPredBlockP)
buzbee67bf8852011-08-17 17:51:35 -0700235{
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 GrowableList* blockList = &cUnit->blockList;
237 BasicBlock* bb;
238 unsigned int i;
239 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700240
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 it = cUnit->blockMap.find(codeOffset);
242 if (it != cUnit->blockMap.end()) {
243 return it->second;
244 } else if (!create) {
245 return NULL;
246 }
247
248 if (split) {
249 for (i = 0; i < blockList->numUsed; i++) {
buzbeecbd6d442012-11-17 14:11:25 -0800250 bb = reinterpret_cast<BasicBlock*>(blockList->elemList[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 if (bb->blockType != kDalvikByteCode) continue;
252 /* Check if a branch jumps into the middle of an existing block */
253 if ((codeOffset > bb->startOffset) && (bb->lastMIRInsn != NULL) &&
254 (codeOffset <= bb->lastMIRInsn->offset)) {
buzbee52a77fc2012-11-20 19:50:46 -0800255 BasicBlock *newBB = SplitBlock(cUnit, codeOffset, bb,
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 bb == *immedPredBlockP ?
257 immedPredBlockP : NULL);
258 return newBB;
259 }
buzbee5b537102012-01-17 17:33:47 -0800260 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 }
buzbee5b537102012-01-17 17:33:47 -0800262
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 /* Create a new one */
buzbee52a77fc2012-11-20 19:50:46 -0800264 bb = NewMemBB(cUnit, kDalvikByteCode, cUnit->numBlocks++);
265 InsertGrowableList(cUnit, &cUnit->blockList, reinterpret_cast<uintptr_t>(bb));
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 bb->startOffset = codeOffset;
267 cUnit->blockMap.Put(bb->startOffset, bb);
268 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700269}
270
buzbeef58c12c2012-07-03 15:06:29 -0700271/* Find existing block */
buzbee52a77fc2012-11-20 19:50:46 -0800272BasicBlock* FindBlock(CompilationUnit* cUnit, unsigned int codeOffset)
buzbeef58c12c2012-07-03 15:06:29 -0700273{
buzbee52a77fc2012-11-20 19:50:46 -0800274 return FindBlock(cUnit, codeOffset, false, false, NULL);
buzbeef58c12c2012-07-03 15:06:29 -0700275}
276
buzbeead8f15e2012-06-18 14:49:45 -0700277/* Turn method name into a legal Linux file name */
buzbee52a77fc2012-11-20 19:50:46 -0800278void ReplaceSpecialChars(std::string& str)
buzbeead8f15e2012-06-18 14:49:45 -0700279{
280 static const struct { const char before; const char after; } match[] =
281 {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'},
282 {'(','@'}, {')','@'}, {'<','='}, {'>','='}};
283 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
284 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
285 }
286}
287
buzbee67bf8852011-08-17 17:51:35 -0700288/* Dump the CFG into a DOT graph */
buzbee52a77fc2012-11-20 19:50:46 -0800289void DumpCFG(CompilationUnit* cUnit, const char* dirPrefix)
buzbee67bf8852011-08-17 17:51:35 -0700290{
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 FILE* file;
buzbeead8f15e2012-06-18 14:49:45 -0700292 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -0800293 ReplaceSpecialChars(fname);
buzbeead8f15e2012-06-18 14:49:45 -0700294 fname = StringPrintf("%s%s%x.dot", dirPrefix, fname.c_str(),
295 cUnit->entryBlock->fallThrough->startOffset);
296 file = fopen(fname.c_str(), "w");
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 if (file == NULL) {
298 return;
299 }
300 fprintf(file, "digraph G {\n");
301
302 fprintf(file, " rankdir=TB\n");
303
304 int numReachableBlocks = cUnit->numReachableBlocks;
305 int idx;
306 const GrowableList *blockList = &cUnit->blockList;
307
308 for (idx = 0; idx < numReachableBlocks; idx++) {
309 int blockIdx = cUnit->dfsOrder.elemList[idx];
buzbee52a77fc2012-11-20 19:50:46 -0800310 BasicBlock *bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(blockList, blockIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 if (bb == NULL) break;
buzbeed1643e42012-09-05 14:06:51 -0700312 if (bb->blockType == kDead) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700313 if (bb->blockType == kEntryBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700314 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 } else if (bb->blockType == kExitBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700316 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 } else if (bb->blockType == kDalvikByteCode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700318 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
319 bb->startOffset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 const MIR *mir;
321 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
322 bb->firstMIRInsn ? " | " : " ");
323 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
324 fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset,
buzbee52a77fc2012-11-20 19:50:46 -0800325 mir->ssaRep ? FullDisassembler(cUnit, mir) :
Bill Buzbeea114add2012-05-03 15:00:40 -0700326 Instruction::Name(mir->dalvikInsn.opcode),
327 mir->next ? " | " : " ");
328 }
329 fprintf(file, " }\"];\n\n");
330 } else if (bb->blockType == kExceptionHandling) {
331 char blockName[BLOCK_NAME_LEN];
332
buzbee52a77fc2012-11-20 19:50:46 -0800333 GetBlockName(bb, blockName);
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 fprintf(file, " %s [shape=invhouse];\n", blockName);
buzbee67bf8852011-08-17 17:51:35 -0700335 }
buzbee67bf8852011-08-17 17:51:35 -0700336
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
buzbee67bf8852011-08-17 17:51:35 -0700338
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 if (bb->taken) {
buzbee52a77fc2012-11-20 19:50:46 -0800340 GetBlockName(bb, blockName1);
341 GetBlockName(bb->taken, blockName2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
343 blockName1, blockName2);
buzbee67bf8852011-08-17 17:51:35 -0700344 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 if (bb->fallThrough) {
buzbee52a77fc2012-11-20 19:50:46 -0800346 GetBlockName(bb, blockName1);
347 GetBlockName(bb->fallThrough, blockName2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 fprintf(file, " %s:s -> %s:n\n", blockName1, blockName2);
349 }
350
351 if (bb->successorBlockList.blockListType != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700352 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
353 bb->startOffset, bb->id,
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 (bb->successorBlockList.blockListType == kCatch) ?
355 "Mrecord" : "record");
356 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800357 GrowableListIteratorInit(&bb->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 &iterator);
359 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800360 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700361
362 int succId = 0;
363 while (true) {
364 if (successorBlockInfo == NULL) break;
365
366 BasicBlock *destBlock = successorBlockInfo->block;
367 SuccessorBlockInfo *nextSuccessorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800368 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700369
370 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
371 succId++,
372 successorBlockInfo->key,
373 destBlock->startOffset,
374 (nextSuccessorBlockInfo != NULL) ? " | " : " ");
375
376 successorBlockInfo = nextSuccessorBlockInfo;
377 }
378 fprintf(file, " }\"];\n\n");
379
buzbee52a77fc2012-11-20 19:50:46 -0800380 GetBlockName(bb, blockName1);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700381 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
382 blockName1, bb->startOffset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700383
384 if (bb->successorBlockList.blockListType == kPackedSwitch ||
385 bb->successorBlockList.blockListType == kSparseSwitch) {
386
buzbee52a77fc2012-11-20 19:50:46 -0800387 GrowableListIteratorInit(&bb->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 &iterator);
389
390 succId = 0;
391 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800392 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800393 reinterpret_cast<SuccessorBlockInfo*>( GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 if (successorBlockInfo == NULL) break;
395
396 BasicBlock *destBlock = successorBlockInfo->block;
397
buzbee52a77fc2012-11-20 19:50:46 -0800398 GetBlockName(destBlock, blockName2);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700399 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->startOffset,
400 bb->id, succId++, blockName2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700401 }
402 }
403 }
404 fprintf(file, "\n");
405
406 /* Display the dominator tree */
buzbee52a77fc2012-11-20 19:50:46 -0800407 GetBlockName(bb, blockName1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
409 blockName1, blockName1);
410 if (bb->iDom) {
buzbee52a77fc2012-11-20 19:50:46 -0800411 GetBlockName(bb->iDom, blockName2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", blockName2, blockName1);
413 }
414 }
415 fprintf(file, "}\n");
416 fclose(file);
buzbee67bf8852011-08-17 17:51:35 -0700417}
418
419/* Verify if all the successor is connected with all the claimed predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800420static bool VerifyPredInfo(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700421{
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700423
buzbee52a77fc2012-11-20 19:50:46 -0800424 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800426 BasicBlock *predBB = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 if (!predBB) break;
428 bool found = false;
429 if (predBB->taken == bb) {
430 found = true;
431 } else if (predBB->fallThrough == bb) {
432 found = true;
433 } else if (predBB->successorBlockList.blockListType != kNotUsed) {
434 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800435 GrowableListIteratorInit(&predBB->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 &iterator);
437 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800438 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800439 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 if (successorBlockInfo == NULL) break;
441 BasicBlock *succBB = successorBlockInfo->block;
442 if (succBB == bb) {
buzbee67bf8852011-08-17 17:51:35 -0700443 found = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 break;
buzbee67bf8852011-08-17 17:51:35 -0700445 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700446 }
buzbee67bf8852011-08-17 17:51:35 -0700447 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 if (found == false) {
449 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
buzbee52a77fc2012-11-20 19:50:46 -0800450 GetBlockName(bb, blockName1);
451 GetBlockName(predBB, blockName2);
452 DumpCFG(cUnit, "/sdcard/cfg/");
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 LOG(FATAL) << "Successor " << blockName1 << "not found from "
454 << blockName2;
455 }
456 }
457 return true;
buzbee67bf8852011-08-17 17:51:35 -0700458}
459
460/* Identify code range in try blocks and set up the empty catch blocks */
buzbee52a77fc2012-11-20 19:50:46 -0800461static void ProcessTryCatchBlocks(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700462{
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 const DexFile::CodeItem* code_item = cUnit->code_item;
464 int triesSize = code_item->tries_size_;
465 int offset;
buzbee67bf8852011-08-17 17:51:35 -0700466
Bill Buzbeea114add2012-05-03 15:00:40 -0700467 if (triesSize == 0) {
468 return;
469 }
470
471 ArenaBitVector* tryBlockAddr = cUnit->tryBlockAddr;
472
473 for (int i = 0; i < triesSize; i++) {
474 const DexFile::TryItem* pTry =
475 DexFile::GetTryItems(*code_item, i);
476 int startOffset = pTry->start_addr_;
477 int endOffset = startOffset + pTry->insn_count_;
478 for (offset = startOffset; offset < endOffset; offset++) {
buzbee52a77fc2012-11-20 19:50:46 -0800479 SetBit(cUnit, tryBlockAddr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700480 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 }
buzbee67bf8852011-08-17 17:51:35 -0700482
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 // Iterate over each of the handlers to enqueue the empty Catch blocks
484 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
485 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
486 for (uint32_t idx = 0; idx < handlers_size; idx++) {
487 CatchHandlerIterator iterator(handlers_ptr);
488 for (; iterator.HasNext(); iterator.Next()) {
489 uint32_t address = iterator.GetHandlerAddress();
buzbee52a77fc2012-11-20 19:50:46 -0800490 FindBlock(cUnit, address, false /* split */, true /*create*/,
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 /* immedPredBlockP */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700492 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 handlers_ptr = iterator.EndDataPointer();
494 }
buzbee67bf8852011-08-17 17:51:35 -0700495}
496
Elliott Hughesadb8c672012-03-06 16:49:32 -0800497/* Process instructions with the kBranch flag */
buzbee52a77fc2012-11-20 19:50:46 -0800498static BasicBlock* ProcessCanBranch(CompilationUnit* cUnit, BasicBlock* curBlock,
499 MIR* insn, int curOffset, int width, int flags,
500 const uint16_t* codePtr, const uint16_t* codeEnd)
buzbee67bf8852011-08-17 17:51:35 -0700501{
Bill Buzbeea114add2012-05-03 15:00:40 -0700502 int target = curOffset;
503 switch (insn->dalvikInsn.opcode) {
504 case Instruction::GOTO:
505 case Instruction::GOTO_16:
506 case Instruction::GOTO_32:
buzbeecbd6d442012-11-17 14:11:25 -0800507 target += insn->dalvikInsn.vA;
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 break;
509 case Instruction::IF_EQ:
510 case Instruction::IF_NE:
511 case Instruction::IF_LT:
512 case Instruction::IF_GE:
513 case Instruction::IF_GT:
514 case Instruction::IF_LE:
buzbee0967a252012-09-14 10:43:54 -0700515 curBlock->conditionalBranch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800516 target += insn->dalvikInsn.vC;
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 break;
518 case Instruction::IF_EQZ:
519 case Instruction::IF_NEZ:
520 case Instruction::IF_LTZ:
521 case Instruction::IF_GEZ:
522 case Instruction::IF_GTZ:
523 case Instruction::IF_LEZ:
buzbee0967a252012-09-14 10:43:54 -0700524 curBlock->conditionalBranch = true;
buzbeecbd6d442012-11-17 14:11:25 -0800525 target += insn->dalvikInsn.vB;
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 break;
527 default:
buzbeecbd6d442012-11-17 14:11:25 -0800528 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 }
buzbee52a77fc2012-11-20 19:50:46 -0800530 BasicBlock *takenBlock = FindBlock(cUnit, target,
Bill Buzbeea114add2012-05-03 15:00:40 -0700531 /* split */
532 true,
533 /* create */
534 true,
535 /* immedPredBlockP */
536 &curBlock);
537 curBlock->taken = takenBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800538 InsertGrowableList(cUnit, takenBlock->predecessors, reinterpret_cast<uintptr_t>(curBlock));
buzbee67bf8852011-08-17 17:51:35 -0700539
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 /* Always terminate the current block for conditional branches */
541 if (flags & Instruction::kContinue) {
buzbee52a77fc2012-11-20 19:50:46 -0800542 BasicBlock *fallthroughBlock = FindBlock(cUnit,
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 curOffset + width,
544 /*
545 * If the method is processed
546 * in sequential order from the
547 * beginning, we don't need to
548 * specify split for continue
549 * blocks. However, this
550 * routine can be called by
551 * compileLoop, which starts
552 * parsing the method from an
553 * arbitrary address in the
554 * method body.
555 */
556 true,
557 /* create */
558 true,
559 /* immedPredBlockP */
560 &curBlock);
561 curBlock->fallThrough = fallthroughBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800562 InsertGrowableList(cUnit, fallthroughBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800563 reinterpret_cast<uintptr_t>(curBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700564 } else if (codePtr < codeEnd) {
565 /* Create a fallthrough block for real instructions (incl. NOP) */
buzbee52a77fc2012-11-20 19:50:46 -0800566 if (ContentIsInsn(codePtr)) {
567 FindBlock(cUnit, curOffset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 /* split */
569 false,
570 /* create */
571 true,
572 /* immedPredBlockP */
573 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700574 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 }
576 return curBlock;
buzbee67bf8852011-08-17 17:51:35 -0700577}
578
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579/* Process instructions with the kSwitch flag */
buzbee52a77fc2012-11-20 19:50:46 -0800580static void ProcessCanSwitch(CompilationUnit* cUnit, BasicBlock* curBlock,
581 MIR* insn, int curOffset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700582{
buzbeecbd6d442012-11-17 14:11:25 -0800583 const uint16_t* switchData =
584 reinterpret_cast<const uint16_t*>(cUnit->insns + curOffset + insn->dalvikInsn.vB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700585 int size;
buzbeecbd6d442012-11-17 14:11:25 -0800586 const int* keyTable;
587 const int* targetTable;
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 int i;
589 int firstKey;
buzbee67bf8852011-08-17 17:51:35 -0700590
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 /*
592 * Packed switch data format:
593 * ushort ident = 0x0100 magic value
594 * ushort size number of entries in the table
595 * int first_key first (and lowest) switch case value
596 * int targets[size] branch targets, relative to switch opcode
597 *
598 * Total size is (4+size*2) 16-bit code units.
599 */
600 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
601 DCHECK_EQ(static_cast<int>(switchData[0]),
602 static_cast<int>(Instruction::kPackedSwitchSignature));
603 size = switchData[1];
604 firstKey = switchData[2] | (switchData[3] << 16);
buzbeecbd6d442012-11-17 14:11:25 -0800605 targetTable = reinterpret_cast<const int*>(&switchData[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 keyTable = NULL; // Make the compiler happy
607 /*
608 * Sparse switch data format:
609 * ushort ident = 0x0200 magic value
610 * ushort size number of entries in the table; > 0
611 * int keys[size] keys, sorted low-to-high; 32-bit aligned
612 * int targets[size] branch targets, relative to switch opcode
613 *
614 * Total size is (2+size*4) 16-bit code units.
615 */
616 } else {
617 DCHECK_EQ(static_cast<int>(switchData[0]),
618 static_cast<int>(Instruction::kSparseSwitchSignature));
619 size = switchData[1];
buzbeecbd6d442012-11-17 14:11:25 -0800620 keyTable = reinterpret_cast<const int*>(&switchData[2]);
621 targetTable = reinterpret_cast<const int*>(&switchData[2 + size*2]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 firstKey = 0; // To make the compiler happy
623 }
buzbee67bf8852011-08-17 17:51:35 -0700624
Bill Buzbeea114add2012-05-03 15:00:40 -0700625 if (curBlock->successorBlockList.blockListType != kNotUsed) {
626 LOG(FATAL) << "Successor block list already in use: "
buzbeecbd6d442012-11-17 14:11:25 -0800627 << static_cast<int>(curBlock->successorBlockList.blockListType);
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 }
629 curBlock->successorBlockList.blockListType =
630 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
631 kPackedSwitch : kSparseSwitch;
buzbee52a77fc2012-11-20 19:50:46 -0800632 CompilerInitGrowableList(cUnit, &curBlock->successorBlockList.blocks, size,
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 kListSuccessorBlocks);
634
635 for (i = 0; i < size; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800636 BasicBlock *caseBlock = FindBlock(cUnit, curOffset + targetTable[i],
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 /* split */
638 true,
639 /* create */
640 true,
641 /* immedPredBlockP */
642 &curBlock);
643 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800644 static_cast<SuccessorBlockInfo*>(NewMem(cUnit, sizeof(SuccessorBlockInfo),
buzbeecbd6d442012-11-17 14:11:25 -0800645 false, kAllocSuccessor));
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 successorBlockInfo->block = caseBlock;
647 successorBlockInfo->key =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
Bill Buzbeea114add2012-05-03 15:00:40 -0700649 firstKey + i : keyTable[i];
buzbee52a77fc2012-11-20 19:50:46 -0800650 InsertGrowableList(cUnit, &curBlock->successorBlockList.blocks,
buzbeecbd6d442012-11-17 14:11:25 -0800651 reinterpret_cast<uintptr_t>(successorBlockInfo));
buzbee52a77fc2012-11-20 19:50:46 -0800652 InsertGrowableList(cUnit, caseBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800653 reinterpret_cast<uintptr_t>(curBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 }
655
656 /* Fall-through case */
buzbee52a77fc2012-11-20 19:50:46 -0800657 BasicBlock* fallthroughBlock = FindBlock(cUnit,
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 curOffset + width,
659 /* split */
660 false,
661 /* create */
662 true,
663 /* immedPredBlockP */
664 NULL);
665 curBlock->fallThrough = fallthroughBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800666 InsertGrowableList(cUnit, fallthroughBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800667 reinterpret_cast<uintptr_t>(curBlock));
buzbee67bf8852011-08-17 17:51:35 -0700668}
669
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670/* Process instructions with the kThrow flag */
buzbee52a77fc2012-11-20 19:50:46 -0800671static BasicBlock* ProcessCanThrow(CompilationUnit* cUnit, BasicBlock* curBlock,
672 MIR* insn, int curOffset, int width, int flags,
673 ArenaBitVector* tryBlockAddr, const uint16_t* codePtr,
674 const uint16_t* codeEnd)
buzbee67bf8852011-08-17 17:51:35 -0700675{
Bill Buzbeea114add2012-05-03 15:00:40 -0700676 const DexFile::CodeItem* code_item = cUnit->code_item;
buzbee52a77fc2012-11-20 19:50:46 -0800677 bool inTryBlock = IsBitSet(tryBlockAddr, curOffset);
buzbee67bf8852011-08-17 17:51:35 -0700678
Bill Buzbeea114add2012-05-03 15:00:40 -0700679 /* In try block */
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700680 if (inTryBlock) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 CatchHandlerIterator iterator(*code_item, curOffset);
buzbee67bf8852011-08-17 17:51:35 -0700682
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 if (curBlock->successorBlockList.blockListType != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700684 LOG(INFO) << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 LOG(FATAL) << "Successor block list already in use: "
buzbeecbd6d442012-11-17 14:11:25 -0800686 << static_cast<int>(curBlock->successorBlockList.blockListType);
buzbee67bf8852011-08-17 17:51:35 -0700687 }
688
Bill Buzbeea114add2012-05-03 15:00:40 -0700689 curBlock->successorBlockList.blockListType = kCatch;
buzbee52a77fc2012-11-20 19:50:46 -0800690 CompilerInitGrowableList(cUnit, &curBlock->successorBlockList.blocks, 2,
Bill Buzbeea114add2012-05-03 15:00:40 -0700691 kListSuccessorBlocks);
692
693 for (;iterator.HasNext(); iterator.Next()) {
buzbee52a77fc2012-11-20 19:50:46 -0800694 BasicBlock *catchBlock = FindBlock(cUnit, iterator.GetHandlerAddress(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 false /* split*/,
696 false /* creat */,
697 NULL /* immedPredBlockP */);
698 catchBlock->catchEntry = true;
buzbee6459e7c2012-10-02 14:42:41 -0700699 cUnit->catches.insert(catchBlock->startOffset);
buzbeecbd6d442012-11-17 14:11:25 -0800700 SuccessorBlockInfo *successorBlockInfo = reinterpret_cast<SuccessorBlockInfo*>
buzbee52a77fc2012-11-20 19:50:46 -0800701 (NewMem(cUnit, sizeof(SuccessorBlockInfo), false, kAllocSuccessor));
Bill Buzbeea114add2012-05-03 15:00:40 -0700702 successorBlockInfo->block = catchBlock;
703 successorBlockInfo->key = iterator.GetHandlerTypeIndex();
buzbee52a77fc2012-11-20 19:50:46 -0800704 InsertGrowableList(cUnit, &curBlock->successorBlockList.blocks,
buzbeecbd6d442012-11-17 14:11:25 -0800705 reinterpret_cast<uintptr_t>(successorBlockInfo));
buzbee52a77fc2012-11-20 19:50:46 -0800706 InsertGrowableList(cUnit, catchBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800707 reinterpret_cast<uintptr_t>(curBlock));
buzbee67bf8852011-08-17 17:51:35 -0700708 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800710 BasicBlock *ehBlock = NewMemBB(cUnit, kExceptionHandling,
Bill Buzbeea114add2012-05-03 15:00:40 -0700711 cUnit->numBlocks++);
712 curBlock->taken = ehBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800713 InsertGrowableList(cUnit, &cUnit->blockList, reinterpret_cast<uintptr_t>(ehBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 ehBlock->startOffset = curOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800715 InsertGrowableList(cUnit, ehBlock->predecessors, reinterpret_cast<uintptr_t>(curBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700716 }
717
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700718 if (insn->dalvikInsn.opcode == Instruction::THROW){
buzbee0967a252012-09-14 10:43:54 -0700719 curBlock->explicitThrow = true;
buzbee52a77fc2012-11-20 19:50:46 -0800720 if ((codePtr < codeEnd) && ContentIsInsn(codePtr)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700721 // Force creation of new block following THROW via side-effect
buzbee52a77fc2012-11-20 19:50:46 -0800722 FindBlock(cUnit, curOffset + width, /* split */ false,
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700723 /* create */ true, /* immedPredBlockP */ NULL);
724 }
725 if (!inTryBlock) {
726 // Don't split a THROW that can't rethrow - we're done.
727 return curBlock;
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 }
729 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700730
731 /*
732 * Split the potentially-throwing instruction into two parts.
733 * The first half will be a pseudo-op that captures the exception
734 * edges and terminates the basic block. It always falls through.
735 * Then, create a new basic block that begins with the throwing instruction
736 * (minus exceptions). Note: this new basic block must NOT be entered into
737 * the blockMap. If the potentially-throwing instruction is the target of a
738 * future branch, we need to find the check psuedo half. The new
739 * basic block containing the work portion of the instruction should
740 * only be entered via fallthrough from the block containing the
741 * pseudo exception edge MIR. Note also that this new block is
742 * not automatically terminated after the work portion, and may
743 * contain following instructions.
744 */
buzbee52a77fc2012-11-20 19:50:46 -0800745 BasicBlock *newBlock = NewMemBB(cUnit, kDalvikByteCode, cUnit->numBlocks++);
746 InsertGrowableList(cUnit, &cUnit->blockList, reinterpret_cast<uintptr_t>(newBlock));
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700747 newBlock->startOffset = insn->offset;
748 curBlock->fallThrough = newBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800749 InsertGrowableList(cUnit, newBlock->predecessors, reinterpret_cast<uintptr_t>(curBlock));
750 MIR* newInsn = static_cast<MIR*>(NewMem(cUnit, sizeof(MIR), true, kAllocMIR));
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700751 *newInsn = *insn;
752 insn->dalvikInsn.opcode =
753 static_cast<Instruction::Code>(kMirOpCheck);
754 // Associate the two halves
755 insn->meta.throwInsn = newInsn;
756 newInsn->meta.throwInsn = insn;
buzbee52a77fc2012-11-20 19:50:46 -0800757 AppendMIR(newBlock, newInsn);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700758 return newBlock;
buzbee67bf8852011-08-17 17:51:35 -0700759}
760
buzbee52a77fc2012-11-20 19:50:46 -0800761void CompilerInit(CompilationUnit* cUnit, const Compiler& compiler) {
762 if (!ArchInit()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800763 LOG(FATAL) << "Failed to initialize oat";
764 }
buzbee52a77fc2012-11-20 19:50:46 -0800765 if (!HeapInit(cUnit)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800766 LOG(FATAL) << "Failed to initialize oat heap";
767 }
768}
769
buzbee52a77fc2012-11-20 19:50:46 -0800770static CompiledMethod* CompileMethod(Compiler& compiler,
771 const CompilerBackend compilerBackend,
772 const DexFile::CodeItem* code_item,
773 uint32_t access_flags, InvokeType invoke_type,
774 uint32_t method_idx, jobject class_loader,
775 const DexFile& dex_file,
776 LLVMInfo* llvm_info)
buzbee67bf8852011-08-17 17:51:35 -0700777{
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700779
buzbeeeaf09bc2012-11-15 14:51:41 -0800780 const uint16_t* codePtr = code_item->insns_;
781 const uint16_t* codeEnd = code_item->insns_ + code_item->insns_size_in_code_units_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700782 int numBlocks = 0;
783 unsigned int curOffset = 0;
buzbee67bf8852011-08-17 17:51:35 -0700784
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
786 UniquePtr<CompilationUnit> cUnit(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800787
buzbee52a77fc2012-11-20 19:50:46 -0800788 CompilerInit(cUnit.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800789
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 cUnit->compiler = &compiler;
791 cUnit->class_linker = class_linker;
792 cUnit->dex_file = &dex_file;
Bill Buzbeea114add2012-05-03 15:00:40 -0700793 cUnit->method_idx = method_idx;
794 cUnit->code_item = code_item;
795 cUnit->access_flags = access_flags;
Ian Rogers08f753d2012-08-24 14:35:25 -0700796 cUnit->invoke_type = invoke_type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700797 cUnit->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
798 cUnit->instructionSet = compiler.GetInstructionSet();
799 cUnit->insns = code_item->insns_;
800 cUnit->insnsSize = code_item->insns_size_in_code_units_;
801 cUnit->numIns = code_item->ins_size_;
802 cUnit->numRegs = code_item->registers_size_ - cUnit->numIns;
803 cUnit->numOuts = code_item->outs_size_;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700804 DCHECK((cUnit->instructionSet == kThumb2) ||
805 (cUnit->instructionSet == kX86) ||
806 (cUnit->instructionSet == kMips));
buzbeec531cef2012-10-18 07:09:20 -0700807 if ((compilerBackend == kQuickGBC) || (compilerBackend == kPortable)) {
buzbee85eee022012-07-16 22:12:38 -0700808 cUnit->genBitcode = true;
809 }
buzbeec531cef2012-10-18 07:09:20 -0700810 DCHECK_NE(compilerBackend, kIceland); // TODO: remove when Portable/Iceland merge complete
811 // TODO: remove this once x86 is tested
812 if (cUnit->genBitcode && (cUnit->instructionSet != kThumb2)) {
813 UNIMPLEMENTED(WARNING) << "GBC generation untested for non-Thumb targets";
814 }
815 cUnit->llvm_info = llvm_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 /* Adjust this value accordingly once inlining is performed */
817 cUnit->numDalvikRegisters = code_item->registers_size_;
818 // TODO: set this from command line
819 cUnit->compilerFlipMatch = false;
820 bool useMatch = !cUnit->compilerMethodMatch.empty();
821 bool match = useMatch && (cUnit->compilerFlipMatch ^
822 (PrettyMethod(method_idx, dex_file).find(cUnit->compilerMethodMatch) !=
823 std::string::npos));
824 if (!useMatch || match) {
825 cUnit->disableOpt = kCompilerOptimizerDisableFlags;
826 cUnit->enableDebug = kCompilerDebugFlags;
827 cUnit->printMe = VLOG_IS_ON(compiler) ||
828 (cUnit->enableDebug & (1 << kDebugVerbose));
829 }
buzbee6459e7c2012-10-02 14:42:41 -0700830#ifndef NDEBUG
buzbeec531cef2012-10-18 07:09:20 -0700831 if (cUnit->genBitcode) {
buzbee6459e7c2012-10-02 14:42:41 -0700832 cUnit->enableDebug |= (1 << kDebugVerifyBitcode);
buzbee6969d502012-06-15 16:40:31 -0700833 }
buzbee2cfc6392012-05-07 14:51:40 -0700834#endif
buzbee9281f002012-10-24 12:17:24 -0700835
836#if 1
837// *** Temporary ****
838// For use in debugging issue 7250540. Disable optimization in problem method
839// to see if monkey results change. Should be removed after monkey runs
840// complete.
841if (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) {
842 cUnit->disableOpt |= (
843 (1 << kLoadStoreElimination) |
844 (1 << kLoadHoisting) |
845 (1 << kSuppressLoads) |
buzbee663c09f2012-10-31 05:21:00 -0700846 //(1 << kNullCheckElimination) |
847 //(1 << kPromoteRegs) |
buzbee9281f002012-10-24 12:17:24 -0700848 (1 << kTrackLiveTemps) |
buzbee663c09f2012-10-31 05:21:00 -0700849 //(1 << kSkipLargeMethodOptimization) |
850 //(1 << kSafeOptimizations) |
buzbee9281f002012-10-24 12:17:24 -0700851 (1 << kBBOpt) |
852 (1 << kMatch) |
buzbee663c09f2012-10-31 05:21:00 -0700853 //(1 << kPromoteCompilerTemps) |
854 0);
buzbee9281f002012-10-24 12:17:24 -0700855}
856#endif
857
jeffhao7fbee072012-08-24 17:56:54 -0700858 if (cUnit->instructionSet == kMips) {
859 // Disable some optimizations for mips for now
860 cUnit->disableOpt |= (
861 (1 << kLoadStoreElimination) |
862 (1 << kLoadHoisting) |
863 (1 << kSuppressLoads) |
864 (1 << kNullCheckElimination) |
865 (1 << kPromoteRegs) |
866 (1 << kTrackLiveTemps) |
867 (1 << kSkipLargeMethodOptimization) |
868 (1 << kSafeOptimizations) |
869 (1 << kBBOpt) |
870 (1 << kMatch) |
871 (1 << kPromoteCompilerTemps));
872 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700873
874 /* Gathering opcode stats? */
875 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbeecbd6d442012-11-17 14:11:25 -0800876 cUnit->opcodeCount =
buzbee52a77fc2012-11-20 19:50:46 -0800877 static_cast<int*>(NewMem(cUnit.get(), kNumPackedOpcodes * sizeof(int), true, kAllocMisc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700878 }
879
880 /* Assume non-throwing leaf */
881 cUnit->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
882
883 /* Initialize the block list, estimate size based on insnsSize */
buzbee52a77fc2012-11-20 19:50:46 -0800884 CompilerInitGrowableList(cUnit.get(), &cUnit->blockList, cUnit->insnsSize,
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 kListBlockList);
886
887 /* Initialize the switchTables list */
buzbee52a77fc2012-11-20 19:50:46 -0800888 CompilerInitGrowableList(cUnit.get(), &cUnit->switchTables, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700889 kListSwitchTables);
890
891 /* Intialize the fillArrayData list */
buzbee52a77fc2012-11-20 19:50:46 -0800892 CompilerInitGrowableList(cUnit.get(), &cUnit->fillArrayData, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700893 kListFillArrayData);
894
895 /* Intialize the throwLaunchpads list, estimate size based on insnsSize */
buzbee52a77fc2012-11-20 19:50:46 -0800896 CompilerInitGrowableList(cUnit.get(), &cUnit->throwLaunchpads, cUnit->insnsSize,
Bill Buzbeea114add2012-05-03 15:00:40 -0700897 kListThrowLaunchPads);
898
899 /* Intialize the instrinsicLaunchpads list */
buzbee52a77fc2012-11-20 19:50:46 -0800900 CompilerInitGrowableList(cUnit.get(), &cUnit->intrinsicLaunchpads, 4,
Bill Buzbeea114add2012-05-03 15:00:40 -0700901 kListMisc);
902
903
904 /* Intialize the suspendLaunchpads list */
buzbee52a77fc2012-11-20 19:50:46 -0800905 CompilerInitGrowableList(cUnit.get(), &cUnit->suspendLaunchpads, 2048,
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 kListSuspendLaunchPads);
907
908 /* Allocate the bit-vector to track the beginning of basic blocks */
buzbee52a77fc2012-11-20 19:50:46 -0800909 ArenaBitVector *tryBlockAddr = AllocBitVector(cUnit.get(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700910 cUnit->insnsSize,
911 true /* expandable */);
912 cUnit->tryBlockAddr = tryBlockAddr;
913
914 /* Create the default entry and exit blocks and enter them to the list */
buzbee52a77fc2012-11-20 19:50:46 -0800915 BasicBlock *entryBlock = NewMemBB(cUnit.get(), kEntryBlock, numBlocks++);
916 BasicBlock *exitBlock = NewMemBB(cUnit.get(), kExitBlock, numBlocks++);
Bill Buzbeea114add2012-05-03 15:00:40 -0700917
918 cUnit->entryBlock = entryBlock;
919 cUnit->exitBlock = exitBlock;
920
buzbee52a77fc2012-11-20 19:50:46 -0800921 InsertGrowableList(cUnit.get(), &cUnit->blockList, reinterpret_cast<uintptr_t>(entryBlock));
922 InsertGrowableList(cUnit.get(), &cUnit->blockList, reinterpret_cast<uintptr_t>(exitBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700923
924 /* Current block to record parsed instructions */
buzbee52a77fc2012-11-20 19:50:46 -0800925 BasicBlock *curBlock = NewMemBB(cUnit.get(), kDalvikByteCode, numBlocks++);
Bill Buzbeea114add2012-05-03 15:00:40 -0700926 curBlock->startOffset = 0;
buzbee52a77fc2012-11-20 19:50:46 -0800927 InsertGrowableList(cUnit.get(), &cUnit->blockList, reinterpret_cast<uintptr_t>(curBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700928 /* Add first block to the fast lookup cache */
929 cUnit->blockMap.Put(curBlock->startOffset, curBlock);
930 entryBlock->fallThrough = curBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800931 InsertGrowableList(cUnit.get(), curBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -0800932 reinterpret_cast<uintptr_t>(entryBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -0700933
934 /*
935 * Store back the number of blocks since new blocks may be created of
936 * accessing cUnit.
937 */
938 cUnit->numBlocks = numBlocks;
939
940 /* Identify code range in try blocks and set up the empty catch blocks */
buzbee52a77fc2012-11-20 19:50:46 -0800941 ProcessTryCatchBlocks(cUnit.get());
Bill Buzbeea114add2012-05-03 15:00:40 -0700942
943 /* Set up for simple method detection */
944 int numPatterns = sizeof(specialPatterns)/sizeof(specialPatterns[0]);
945 bool livePattern = (numPatterns > 0) && !(cUnit->disableOpt & (1 << kMatch));
buzbeecbd6d442012-11-17 14:11:25 -0800946 bool* deadPattern =
buzbee52a77fc2012-11-20 19:50:46 -0800947 static_cast<bool*>(NewMem(cUnit.get(), sizeof(bool) * numPatterns, true, kAllocMisc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 SpecialCaseHandler specialCase = kNoHandler;
949 int patternPos = 0;
950
951 /* Parse all instructions and put them into containing basic blocks */
952 while (codePtr < codeEnd) {
buzbee52a77fc2012-11-20 19:50:46 -0800953 MIR *insn = static_cast<MIR *>(NewMem(cUnit.get(), sizeof(MIR), true, kAllocMIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 insn->offset = curOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800955 int width = ParseInsn(cUnit.get(), codePtr, &insn->dalvikInsn, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 insn->width = width;
957 Instruction::Code opcode = insn->dalvikInsn.opcode;
958 if (cUnit->opcodeCount != NULL) {
959 cUnit->opcodeCount[static_cast<int>(opcode)]++;
buzbee44b412b2012-02-04 08:50:53 -0800960 }
961
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 /* Terminate when the data section is seen */
963 if (width == 0)
964 break;
965
966 /* Possible simple method? */
967 if (livePattern) {
968 livePattern = false;
969 specialCase = kNoHandler;
970 for (int i = 0; i < numPatterns; i++) {
971 if (!deadPattern[i]) {
972 if (specialPatterns[i].opcodes[patternPos] == opcode) {
973 livePattern = true;
974 specialCase = specialPatterns[i].handlerCode;
975 } else {
976 deadPattern[i] = true;
977 }
978 }
979 }
980 patternPos++;
buzbeea7c12682012-03-19 13:13:53 -0700981 }
982
buzbee52a77fc2012-11-20 19:50:46 -0800983 AppendMIR(curBlock, insn);
buzbeecefd1872011-09-09 09:59:52 -0700984
Bill Buzbeea114add2012-05-03 15:00:40 -0700985 codePtr += width;
Ian Rogersa75a0132012-09-28 11:41:42 -0700986 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700987
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 int dfFlags = oatDataFlowAttributes[insn->dalvikInsn.opcode];
buzbee67bf8852011-08-17 17:51:35 -0700989
Bill Buzbeea114add2012-05-03 15:00:40 -0700990 if (dfFlags & DF_HAS_DEFS) {
buzbeebff24652012-05-06 16:22:05 -0700991 cUnit->defCount += (dfFlags & DF_A_WIDE) ? 2 : 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700992 }
buzbee67bf8852011-08-17 17:51:35 -0700993
Bill Buzbeea114add2012-05-03 15:00:40 -0700994 if (flags & Instruction::kBranch) {
buzbee52a77fc2012-11-20 19:50:46 -0800995 curBlock = ProcessCanBranch(cUnit.get(), curBlock, insn, curOffset,
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 width, flags, codePtr, codeEnd);
997 } else if (flags & Instruction::kReturn) {
998 curBlock->fallThrough = exitBlock;
buzbee52a77fc2012-11-20 19:50:46 -0800999 InsertGrowableList(cUnit.get(), exitBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -08001000 reinterpret_cast<uintptr_t>(curBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -07001001 /*
1002 * Terminate the current block if there are instructions
1003 * afterwards.
1004 */
1005 if (codePtr < codeEnd) {
1006 /*
1007 * Create a fallthrough block for real instructions
1008 * (incl. NOP).
1009 */
buzbee52a77fc2012-11-20 19:50:46 -08001010 if (ContentIsInsn(codePtr)) {
1011 FindBlock(cUnit.get(), curOffset + width,
Bill Buzbeea114add2012-05-03 15:00:40 -07001012 /* split */
1013 false,
1014 /* create */
1015 true,
1016 /* immedPredBlockP */
1017 NULL);
1018 }
1019 }
1020 } else if (flags & Instruction::kThrow) {
buzbee52a77fc2012-11-20 19:50:46 -08001021 curBlock = ProcessCanThrow(cUnit.get(), curBlock, insn, curOffset,
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001022 width, flags, tryBlockAddr, codePtr, codeEnd);
Bill Buzbeea114add2012-05-03 15:00:40 -07001023 } else if (flags & Instruction::kSwitch) {
buzbee52a77fc2012-11-20 19:50:46 -08001024 ProcessCanSwitch(cUnit.get(), curBlock, insn, curOffset, width, flags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001025 }
1026 curOffset += width;
buzbee52a77fc2012-11-20 19:50:46 -08001027 BasicBlock *nextBlock = FindBlock(cUnit.get(), curOffset,
Bill Buzbeea114add2012-05-03 15:00:40 -07001028 /* split */
1029 false,
1030 /* create */
1031 false,
1032 /* immedPredBlockP */
1033 NULL);
1034 if (nextBlock) {
1035 /*
1036 * The next instruction could be the target of a previously parsed
1037 * forward branch so a block is already created. If the current
1038 * instruction is not an unconditional branch, connect them through
1039 * the fall-through link.
1040 */
1041 DCHECK(curBlock->fallThrough == NULL ||
1042 curBlock->fallThrough == nextBlock ||
1043 curBlock->fallThrough == exitBlock);
buzbee5ade1d22011-09-09 14:44:52 -07001044
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 if ((curBlock->fallThrough == NULL) && (flags & Instruction::kContinue)) {
1046 curBlock->fallThrough = nextBlock;
buzbee52a77fc2012-11-20 19:50:46 -08001047 InsertGrowableList(cUnit.get(), nextBlock->predecessors,
buzbeecbd6d442012-11-17 14:11:25 -08001048 reinterpret_cast<uintptr_t>(curBlock));
Bill Buzbeea114add2012-05-03 15:00:40 -07001049 }
1050 curBlock = nextBlock;
1051 }
1052 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001053
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 if (!(cUnit->disableOpt & (1 << kSkipLargeMethodOptimization))) {
1055 if ((cUnit->numBlocks > MANY_BLOCKS) ||
1056 ((cUnit->numBlocks > MANY_BLOCKS_INITIALIZER) &&
1057 PrettyMethod(method_idx, dex_file, false).find("init>") !=
1058 std::string::npos)) {
1059 cUnit->qdMode = true;
1060 }
1061 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001062
Bill Buzbeea114add2012-05-03 15:00:40 -07001063 if (cUnit->qdMode) {
buzbeed1643e42012-09-05 14:06:51 -07001064 // Bitcode generation requires full dataflow analysis
buzbeec531cef2012-10-18 07:09:20 -07001065 cUnit->disableDataflow = !cUnit->genBitcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001066 // Disable optimization which require dataflow/ssa
buzbeec531cef2012-10-18 07:09:20 -07001067 cUnit->disableOpt |= (1 << kBBOpt) | (1 << kPromoteRegs) | (1 << kNullCheckElimination);
Bill Buzbeea114add2012-05-03 15:00:40 -07001068 if (cUnit->printMe) {
1069 LOG(INFO) << "QD mode enabled: "
1070 << PrettyMethod(method_idx, dex_file)
buzbeec531cef2012-10-18 07:09:20 -07001071 << " num blocks: " << cUnit->numBlocks;
Bill Buzbeea114add2012-05-03 15:00:40 -07001072 }
1073 }
buzbeec1f45042011-09-21 16:03:19 -07001074
Bill Buzbeea114add2012-05-03 15:00:40 -07001075 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -08001076 DumpCompilationUnit(cUnit.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001077 }
buzbee67bf8852011-08-17 17:51:35 -07001078
buzbee0967a252012-09-14 10:43:54 -07001079 /* Do a code layout pass */
buzbee52a77fc2012-11-20 19:50:46 -08001080 CodeLayout(cUnit.get());
buzbee0967a252012-09-14 10:43:54 -07001081
Bill Buzbeea114add2012-05-03 15:00:40 -07001082 if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) {
1083 /* Verify if all blocks are connected as claimed */
buzbee52a77fc2012-11-20 19:50:46 -08001084 DataFlowAnalysisDispatcher(cUnit.get(), VerifyPredInfo, kAllNodes,
Bill Buzbeea114add2012-05-03 15:00:40 -07001085 false /* isIterative */);
1086 }
buzbee67bf8852011-08-17 17:51:35 -07001087
Bill Buzbeea114add2012-05-03 15:00:40 -07001088 /* Perform SSA transformation for the whole method */
buzbee52a77fc2012-11-20 19:50:46 -08001089 SSATransformation(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001090
buzbee2cfc6392012-05-07 14:51:40 -07001091 /* Do constant propagation */
1092 // TODO: Probably need to make these expandable to support new ssa names
1093 // introducted during MIR optimization passes
buzbee52a77fc2012-11-20 19:50:46 -08001094 cUnit->isConstantV = AllocBitVector(cUnit.get(), cUnit->numSSARegs,
buzbee2cfc6392012-05-07 14:51:40 -07001095 false /* not expandable */);
1096 cUnit->constantValues =
buzbee52a77fc2012-11-20 19:50:46 -08001097 static_cast<int*>(NewMem(cUnit.get(), sizeof(int) * cUnit->numSSARegs, true, kAllocDFInfo));
1098 DataFlowAnalysisDispatcher(cUnit.get(), DoConstantPropogation,
buzbee2cfc6392012-05-07 14:51:40 -07001099 kAllNodes,
1100 false /* isIterative */);
1101
Bill Buzbeea114add2012-05-03 15:00:40 -07001102 /* Detect loops */
buzbee52a77fc2012-11-20 19:50:46 -08001103 LoopDetection(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001104
Bill Buzbeea114add2012-05-03 15:00:40 -07001105 /* Count uses */
buzbee52a77fc2012-11-20 19:50:46 -08001106 MethodUseCount(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001107
Bill Buzbeea114add2012-05-03 15:00:40 -07001108 /* Perform null check elimination */
buzbee52a77fc2012-11-20 19:50:46 -08001109 NullCheckElimination(cUnit.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001110
buzbeed1643e42012-09-05 14:06:51 -07001111 /* Combine basic blocks where possible */
buzbee52a77fc2012-11-20 19:50:46 -08001112 BasicBlockCombine(cUnit.get());
buzbeed1643e42012-09-05 14:06:51 -07001113
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 /* Do some basic block optimizations */
buzbee52a77fc2012-11-20 19:50:46 -08001115 BasicBlockOptimization(cUnit.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001116
buzbeed1643e42012-09-05 14:06:51 -07001117 if (cUnit->enableDebug & (1 << kDebugDumpCheckStats)) {
buzbee52a77fc2012-11-20 19:50:46 -08001118 DumpCheckStats(cUnit.get());
buzbeed1643e42012-09-05 14:06:51 -07001119 }
1120
buzbee52a77fc2012-11-20 19:50:46 -08001121 CompilerInitializeRegAlloc(cUnit.get()); // Needs to happen after SSA naming
Bill Buzbeea114add2012-05-03 15:00:40 -07001122
1123 /* Allocate Registers using simple local allocation scheme */
buzbee52a77fc2012-11-20 19:50:46 -08001124 SimpleRegAlloc(cUnit.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001125
buzbee2cfc6392012-05-07 14:51:40 -07001126 /* Go the LLVM path? */
1127 if (cUnit->genBitcode) {
1128 // MIR->Bitcode
buzbee52a77fc2012-11-20 19:50:46 -08001129 MethodMIR2Bitcode(cUnit.get());
buzbeec531cef2012-10-18 07:09:20 -07001130 if (compilerBackend == kPortable) {
buzbeeabc4c6b2012-08-23 08:17:15 -07001131 // all done
buzbee52a77fc2012-11-20 19:50:46 -08001132 ArenaReset(cUnit.get());
buzbeeabc4c6b2012-08-23 08:17:15 -07001133 return NULL;
1134 }
buzbee2cfc6392012-05-07 14:51:40 -07001135 // Bitcode->LIR
buzbee52a77fc2012-11-20 19:50:46 -08001136 MethodBitcode2LIR(cUnit.get());
buzbee2cfc6392012-05-07 14:51:40 -07001137 } else {
buzbee2cfc6392012-05-07 14:51:40 -07001138 if (specialCase != kNoHandler) {
1139 /*
1140 * Custom codegen for special cases. If for any reason the
1141 * special codegen doesn't succeed, cUnit->firstLIRInsn will
1142 * set to NULL;
1143 */
buzbee52a77fc2012-11-20 19:50:46 -08001144 SpecialMIR2LIR(cUnit.get(), specialCase);
buzbee2cfc6392012-05-07 14:51:40 -07001145 }
buzbee67bf8852011-08-17 17:51:35 -07001146
buzbee2cfc6392012-05-07 14:51:40 -07001147 /* Convert MIR to LIR, etc. */
1148 if (cUnit->firstLIRInsn == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -08001149 MethodMIR2LIR(cUnit.get());
buzbee2cfc6392012-05-07 14:51:40 -07001150 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001151 }
buzbee67bf8852011-08-17 17:51:35 -07001152
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 // Debugging only
1154 if (cUnit->enableDebug & (1 << kDebugDumpCFG)) {
buzbee52a77fc2012-11-20 19:50:46 -08001155 DumpCFG(cUnit.get(), "/sdcard/cfg/");
Bill Buzbeea114add2012-05-03 15:00:40 -07001156 }
buzbee16da88c2012-03-20 10:38:17 -07001157
Bill Buzbeea114add2012-05-03 15:00:40 -07001158 /* Method is not empty */
1159 if (cUnit->firstLIRInsn) {
buzbee67bf8852011-08-17 17:51:35 -07001160
Bill Buzbeea114add2012-05-03 15:00:40 -07001161 // mark the targets of switch statement case labels
buzbee52a77fc2012-11-20 19:50:46 -08001162 ProcessSwitchTables(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001163
Bill Buzbeea114add2012-05-03 15:00:40 -07001164 /* Convert LIR into machine code. */
buzbee52a77fc2012-11-20 19:50:46 -08001165 AssembleLIR(cUnit.get());
buzbee99ba9642012-01-25 14:23:14 -08001166
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001167 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -08001168 CodegenDump(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001169 }
1170
Bill Buzbeea114add2012-05-03 15:00:40 -07001171 if (cUnit->opcodeCount != NULL) {
1172 LOG(INFO) << "Opcode Count";
1173 for (int i = 0; i < kNumPackedOpcodes; i++) {
1174 if (cUnit->opcodeCount[i] != 0) {
1175 LOG(INFO) << "-C- "
1176 << Instruction::Name(static_cast<Instruction::Code>(i))
1177 << " " << cUnit->opcodeCount[i];
buzbee67bf8852011-08-17 17:51:35 -07001178 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 }
1180 }
1181 }
buzbeea7c12682012-03-19 13:13:53 -07001182
Bill Buzbeea114add2012-05-03 15:00:40 -07001183 // Combine vmap tables - core regs, then fp regs - into vmapTable
1184 std::vector<uint16_t> vmapTable;
buzbeeca7a5e42012-08-20 11:12:18 -07001185 // Core regs may have been inserted out of order - sort first
1186 std::sort(cUnit->coreVmapTable.begin(), cUnit->coreVmapTable.end());
Bill Buzbeea114add2012-05-03 15:00:40 -07001187 for (size_t i = 0 ; i < cUnit->coreVmapTable.size(); i++) {
buzbeeca7a5e42012-08-20 11:12:18 -07001188 // Copy, stripping out the phys register sort key
1189 vmapTable.push_back(~(-1 << VREG_NUM_WIDTH) & cUnit->coreVmapTable[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001190 }
1191 // If we have a frame, push a marker to take place of lr
1192 if (cUnit->frameSize > 0) {
1193 vmapTable.push_back(INVALID_VREG);
1194 } else {
1195 DCHECK_EQ(__builtin_popcount(cUnit->coreSpillMask), 0);
1196 DCHECK_EQ(__builtin_popcount(cUnit->fpSpillMask), 0);
1197 }
buzbeeca7a5e42012-08-20 11:12:18 -07001198 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
Bill Buzbeea114add2012-05-03 15:00:40 -07001199 for (uint32_t i = 0; i < cUnit->fpVmapTable.size(); i++) {
1200 vmapTable.push_back(cUnit->fpVmapTable[i]);
1201 }
1202 CompiledMethod* result =
1203 new CompiledMethod(cUnit->instructionSet, cUnit->codeBuffer,
Bill Buzbeea5b30242012-09-28 07:19:44 -07001204 cUnit->frameSize, cUnit->coreSpillMask, cUnit->fpSpillMask,
1205 cUnit->combinedMappingTable, vmapTable, cUnit->nativeGcMap);
buzbee67bf8852011-08-17 17:51:35 -07001206
Bill Buzbeea114add2012-05-03 15:00:40 -07001207 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
1208 << " (" << (cUnit->codeBuffer.size() * sizeof(cUnit->codeBuffer[0]))
1209 << " bytes)";
buzbee5abfa3e2012-01-31 17:01:43 -08001210
1211#ifdef WITH_MEMSTATS
Bill Buzbeea114add2012-05-03 15:00:40 -07001212 if (cUnit->enableDebug & (1 << kDebugShowMemoryUsage)) {
buzbee52a77fc2012-11-20 19:50:46 -08001213 DumpMemStats(cUnit.get());
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 }
buzbee5abfa3e2012-01-31 17:01:43 -08001215#endif
buzbee67bf8852011-08-17 17:51:35 -07001216
buzbee52a77fc2012-11-20 19:50:46 -08001217 ArenaReset(cUnit.get());
buzbeeba938cb2012-02-03 14:47:55 -08001218
Bill Buzbeea114add2012-05-03 15:00:40 -07001219 return result;
buzbee67bf8852011-08-17 17:51:35 -07001220}
1221
buzbee52a77fc2012-11-20 19:50:46 -08001222CompiledMethod* CompileOneMethod(Compiler& compiler,
buzbeec531cef2012-10-18 07:09:20 -07001223 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -07001224 const DexFile::CodeItem* code_item,
1225 uint32_t access_flags, InvokeType invoke_type,
1226 uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001227 const DexFile& dex_file,
1228 LLVMInfo* llvmInfo)
buzbeeabc4c6b2012-08-23 08:17:15 -07001229{
buzbee52a77fc2012-11-20 19:50:46 -08001230 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, method_idx, class_loader,
buzbeec531cef2012-10-18 07:09:20 -07001231 dex_file, llvmInfo);
buzbeeabc4c6b2012-08-23 08:17:15 -07001232}
1233
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001234} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001235
Bill Buzbeea114add2012-05-03 15:00:40 -07001236extern "C" art::CompiledMethod*
buzbeec531cef2012-10-18 07:09:20 -07001237 ArtQuickCompileMethod(art::Compiler& compiler,
1238 const art::DexFile::CodeItem* code_item,
1239 uint32_t access_flags, art::InvokeType invoke_type,
1240 uint32_t method_idx, jobject class_loader,
1241 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001242{
buzbeec531cef2012-10-18 07:09:20 -07001243 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
1244 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -08001245 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
buzbeec531cef2012-10-18 07:09:20 -07001246 method_idx, class_loader, dex_file, NULL /* use thread llvmInfo */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001247}