blob: 09bc054e5e012819b319a0acb9c04c136f1ac82c [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
17#include "Dalvik.h"
18#include "CompilerInternals.h"
19#include "Dataflow.h"
Ian Rogers0571d352011-11-03 19:51:38 -070020#include "leb128.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070021#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070022#include "runtime.h"
buzbee67bf8852011-08-17 17:51:35 -070023
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbee692be802012-08-29 15:52:59 -070026#if defined(ART_USE_QUICK_COMPILER)
TDYa12755e5e6c2012-09-11 15:14:42 -070027QuickCompiler::QuickCompiler() {
buzbee692be802012-08-29 15:52:59 -070028 // Create context, module, intrinsic helper & ir builder
29 llvm_context_.reset(new llvm::LLVMContext());
TDYa12755e5e6c2012-09-11 15:14:42 -070030 llvm_module_ = new llvm::Module("art", *llvm_context_);
buzbee692be802012-08-29 15:52:59 -070031 llvm::StructType::create(*llvm_context_, "JavaObject");
buzbee692be802012-08-29 15:52:59 -070032 intrinsic_helper_.reset( new greenland::IntrinsicHelper(*llvm_context_, *llvm_module_));
33 ir_builder_.reset(new greenland::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
34}
35
36QuickCompiler::~QuickCompiler() {
37}
38
39extern "C" void ArtInitQuickCompilerContext(art::Compiler& compiler) {
40 CHECK(compiler.GetCompilerContext() == NULL);
TDYa12755e5e6c2012-09-11 15:14:42 -070041 QuickCompiler* quickCompiler = new QuickCompiler();
buzbee692be802012-08-29 15:52:59 -070042 compiler.SetCompilerContext(quickCompiler);
43}
44
45extern "C" void ArtUnInitQuickCompilerContext(art::Compiler& compiler) {
46 delete reinterpret_cast<QuickCompiler*>(compiler.GetCompilerContext());
47 compiler.SetCompilerContext(NULL);
48}
49#endif
50
buzbeece302932011-10-04 14:32:18 -070051/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070052static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Bill Buzbeea114add2012-05-03 15:00:40 -070053 //(1 << kLoadStoreElimination) |
54 //(1 << kLoadHoisting) |
55 //(1 << kSuppressLoads) |
56 //(1 << kNullCheckElimination) |
57 //(1 << kPromoteRegs) |
58 //(1 << kTrackLiveTemps) |
59 //(1 << kSkipLargeMethodOptimization) |
60 //(1 << kSafeOptimizations) |
61 //(1 << kBBOpt) |
62 //(1 << kMatch) |
63 //(1 << kPromoteCompilerTemps) |
64 0;
buzbeece302932011-10-04 14:32:18 -070065
Elliott Hughese52e49b2012-04-02 16:05:44 -070066static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Bill Buzbeea114add2012-05-03 15:00:40 -070067 //(1 << kDebugDisplayMissingTargets) |
68 //(1 << kDebugVerbose) |
69 //(1 << kDebugDumpCFG) |
70 //(1 << kDebugSlowFieldPath) |
71 //(1 << kDebugSlowInvokePath) |
72 //(1 << kDebugSlowStringPath) |
73 //(1 << kDebugSlowestFieldPath) |
74 //(1 << kDebugSlowestStringPath) |
75 //(1 << kDebugExerciseResolveMethod) |
76 //(1 << kDebugVerifyDataflow) |
77 //(1 << kDebugShowMemoryUsage) |
78 //(1 << kDebugShowNops) |
79 //(1 << kDebugCountOpcodes) |
buzbeed1643e42012-09-05 14:06:51 -070080 //(1 << kDebugDumpCheckStats) |
buzbeead8f15e2012-06-18 14:49:45 -070081#if defined(ART_USE_QUICK_COMPILER)
82 //(1 << kDebugDumpBitcodeFile) |
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070083 //(1 << kDebugVerifyBitcode) |
buzbeead8f15e2012-06-18 14:49:45 -070084#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070085 0;
buzbeece302932011-10-04 14:32:18 -070086
buzbee31a4a6f2012-02-28 15:36:15 -080087inline bool contentIsInsn(const u2* codePtr) {
Bill Buzbeea114add2012-05-03 15:00:40 -070088 u2 instr = *codePtr;
89 Instruction::Code opcode = (Instruction::Code)(instr & 0xff);
buzbee67bf8852011-08-17 17:51:35 -070090
Bill Buzbeea114add2012-05-03 15:00:40 -070091 /*
92 * Since the low 8-bit in metadata may look like NOP, we need to check
93 * both the low and whole sub-word to determine whether it is code or data.
94 */
95 return (opcode != Instruction::NOP || instr == 0);
buzbee67bf8852011-08-17 17:51:35 -070096}
97
98/*
99 * Parse an instruction, return the length of the instruction
100 */
buzbee31a4a6f2012-02-28 15:36:15 -0800101inline int parseInsn(CompilationUnit* cUnit, const u2* codePtr,
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 DecodedInstruction* decoded_instruction, bool printMe)
buzbee67bf8852011-08-17 17:51:35 -0700103{
Elliott Hughesadb8c672012-03-06 16:49:32 -0800104 // Don't parse instruction data
105 if (!contentIsInsn(codePtr)) {
106 return 0;
107 }
buzbee67bf8852011-08-17 17:51:35 -0700108
Elliott Hughesadb8c672012-03-06 16:49:32 -0800109 const Instruction* instruction = Instruction::At(codePtr);
110 *decoded_instruction = DecodedInstruction(instruction);
buzbee67bf8852011-08-17 17:51:35 -0700111
Elliott Hughesadb8c672012-03-06 16:49:32 -0800112 if (printMe) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 char* decodedString = oatGetDalvikDisassembly(cUnit, *decoded_instruction,
114 NULL);
115 LOG(INFO) << codePtr << ": 0x"
116 << std::hex << static_cast<int>(decoded_instruction->opcode)
Elliott Hughesadb8c672012-03-06 16:49:32 -0800117 << " " << decodedString;
118 }
119 return instruction->SizeInCodeUnits();
buzbee67bf8852011-08-17 17:51:35 -0700120}
121
122#define UNKNOWN_TARGET 0xffffffff
123
Elliott Hughesadb8c672012-03-06 16:49:32 -0800124inline bool isGoto(MIR* insn) {
125 switch (insn->dalvikInsn.opcode) {
126 case Instruction::GOTO:
127 case Instruction::GOTO_16:
128 case Instruction::GOTO_32:
129 return true;
130 default:
131 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 }
buzbee67bf8852011-08-17 17:51:35 -0700133}
134
135/*
136 * Identify unconditional branch instructions
137 */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800138inline bool isUnconditionalBranch(MIR* insn) {
139 switch (insn->dalvikInsn.opcode) {
140 case Instruction::RETURN_VOID:
141 case Instruction::RETURN:
142 case Instruction::RETURN_WIDE:
143 case Instruction::RETURN_OBJECT:
144 return true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 default:
146 return isGoto(insn);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800147 }
buzbee67bf8852011-08-17 17:51:35 -0700148}
149
150/* Split an existing block from the specified code offset into two */
buzbee31a4a6f2012-02-28 15:36:15 -0800151BasicBlock *splitBlock(CompilationUnit* cUnit, unsigned int codeOffset,
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 BasicBlock* origBlock, BasicBlock** immedPredBlockP)
buzbee67bf8852011-08-17 17:51:35 -0700153{
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 MIR* insn = origBlock->firstMIRInsn;
155 while (insn) {
156 if (insn->offset == codeOffset) break;
157 insn = insn->next;
158 }
159 if (insn == NULL) {
160 LOG(FATAL) << "Break split failed";
161 }
162 BasicBlock *bottomBlock = oatNewBB(cUnit, kDalvikByteCode,
163 cUnit->numBlocks++);
164 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t) bottomBlock);
buzbee67bf8852011-08-17 17:51:35 -0700165
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 bottomBlock->startOffset = codeOffset;
167 bottomBlock->firstMIRInsn = insn;
168 bottomBlock->lastMIRInsn = origBlock->lastMIRInsn;
buzbee67bf8852011-08-17 17:51:35 -0700169
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 /* Add it to the quick lookup cache */
171 cUnit->blockMap.Put(bottomBlock->startOffset, bottomBlock);
buzbee5b537102012-01-17 17:33:47 -0800172
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 /* Handle the taken path */
174 bottomBlock->taken = origBlock->taken;
175 if (bottomBlock->taken) {
176 origBlock->taken = NULL;
177 oatDeleteGrowableList(bottomBlock->taken->predecessors,
buzbeeba938cb2012-02-03 14:47:55 -0800178 (intptr_t)origBlock);
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 oatInsertGrowableList(cUnit, bottomBlock->taken->predecessors,
180 (intptr_t)bottomBlock);
181 }
182
183 /* Handle the fallthrough path */
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 bottomBlock->fallThrough = origBlock->fallThrough;
185 origBlock->fallThrough = bottomBlock;
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 oatInsertGrowableList(cUnit, bottomBlock->predecessors,
187 (intptr_t)origBlock);
188 if (bottomBlock->fallThrough) {
189 oatDeleteGrowableList(bottomBlock->fallThrough->predecessors,
190 (intptr_t)origBlock);
191 oatInsertGrowableList(cUnit, bottomBlock->fallThrough->predecessors,
192 (intptr_t)bottomBlock);
193 }
194
195 /* Handle the successor list */
196 if (origBlock->successorBlockList.blockListType != kNotUsed) {
197 bottomBlock->successorBlockList = origBlock->successorBlockList;
198 origBlock->successorBlockList.blockListType = kNotUsed;
199 GrowableListIterator iterator;
200
201 oatGrowableListIteratorInit(&bottomBlock->successorBlockList.blocks,
202 &iterator);
203 while (true) {
204 SuccessorBlockInfo *successorBlockInfo =
205 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
206 if (successorBlockInfo == NULL) break;
207 BasicBlock *bb = successorBlockInfo->block;
208 oatDeleteGrowableList(bb->predecessors, (intptr_t)origBlock);
209 oatInsertGrowableList(cUnit, bb->predecessors, (intptr_t)bottomBlock);
buzbee67bf8852011-08-17 17:51:35 -0700210 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 }
buzbee67bf8852011-08-17 17:51:35 -0700212
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 origBlock->lastMIRInsn = insn->prev;
buzbee67bf8852011-08-17 17:51:35 -0700214
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 insn->prev->next = NULL;
216 insn->prev = NULL;
217 /*
218 * Update the immediate predecessor block pointer so that outgoing edges
219 * can be applied to the proper block.
220 */
221 if (immedPredBlockP) {
222 DCHECK_EQ(*immedPredBlockP, origBlock);
223 *immedPredBlockP = bottomBlock;
224 }
225 return bottomBlock;
buzbee67bf8852011-08-17 17:51:35 -0700226}
227
228/*
229 * Given a code offset, find out the block that starts with it. If the offset
buzbee9ab05de2012-01-18 15:43:48 -0800230 * is in the middle of an existing block, split it into two. If immedPredBlockP
231 * is not non-null and is the block being split, update *immedPredBlockP to
232 * point to the bottom block so that outgoing edges can be set up properly
233 * (by the caller)
buzbee5b537102012-01-17 17:33:47 -0800234 * Utilizes a map for fast lookup of the typical cases.
buzbee67bf8852011-08-17 17:51:35 -0700235 */
buzbee31a4a6f2012-02-28 15:36:15 -0800236BasicBlock *findBlock(CompilationUnit* cUnit, unsigned int codeOffset,
237 bool split, bool create, BasicBlock** immedPredBlockP)
buzbee67bf8852011-08-17 17:51:35 -0700238{
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 GrowableList* blockList = &cUnit->blockList;
240 BasicBlock* bb;
241 unsigned int i;
242 SafeMap<unsigned int, BasicBlock*>::iterator it;
buzbee67bf8852011-08-17 17:51:35 -0700243
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 it = cUnit->blockMap.find(codeOffset);
245 if (it != cUnit->blockMap.end()) {
246 return it->second;
247 } else if (!create) {
248 return NULL;
249 }
250
251 if (split) {
252 for (i = 0; i < blockList->numUsed; i++) {
253 bb = (BasicBlock *) blockList->elemList[i];
254 if (bb->blockType != kDalvikByteCode) continue;
255 /* Check if a branch jumps into the middle of an existing block */
256 if ((codeOffset > bb->startOffset) && (bb->lastMIRInsn != NULL) &&
257 (codeOffset <= bb->lastMIRInsn->offset)) {
258 BasicBlock *newBB = splitBlock(cUnit, codeOffset, bb,
259 bb == *immedPredBlockP ?
260 immedPredBlockP : NULL);
261 return newBB;
262 }
buzbee5b537102012-01-17 17:33:47 -0800263 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 }
buzbee5b537102012-01-17 17:33:47 -0800265
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 /* Create a new one */
267 bb = oatNewBB(cUnit, kDalvikByteCode, cUnit->numBlocks++);
268 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t) bb);
269 bb->startOffset = codeOffset;
270 cUnit->blockMap.Put(bb->startOffset, bb);
271 return bb;
buzbee67bf8852011-08-17 17:51:35 -0700272}
273
buzbeef58c12c2012-07-03 15:06:29 -0700274/* Find existing block */
275BasicBlock* oatFindBlock(CompilationUnit* cUnit, unsigned int codeOffset)
276{
277 return findBlock(cUnit, codeOffset, false, false, NULL);
278}
279
buzbeead8f15e2012-06-18 14:49:45 -0700280/* Turn method name into a legal Linux file name */
281void oatReplaceSpecialChars(std::string& str)
282{
283 static const struct { const char before; const char after; } match[] =
284 {{'/','-'}, {';','#'}, {' ','#'}, {'$','+'},
285 {'(','@'}, {')','@'}, {'<','='}, {'>','='}};
286 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
287 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
288 }
289}
290
buzbee67bf8852011-08-17 17:51:35 -0700291/* Dump the CFG into a DOT graph */
292void oatDumpCFG(CompilationUnit* cUnit, const char* dirPrefix)
293{
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 FILE* file;
buzbeead8f15e2012-06-18 14:49:45 -0700295 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
296 oatReplaceSpecialChars(fname);
297 fname = StringPrintf("%s%s%x.dot", dirPrefix, fname.c_str(),
298 cUnit->entryBlock->fallThrough->startOffset);
299 file = fopen(fname.c_str(), "w");
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 if (file == NULL) {
301 return;
302 }
303 fprintf(file, "digraph G {\n");
304
305 fprintf(file, " rankdir=TB\n");
306
307 int numReachableBlocks = cUnit->numReachableBlocks;
308 int idx;
309 const GrowableList *blockList = &cUnit->blockList;
310
311 for (idx = 0; idx < numReachableBlocks; idx++) {
312 int blockIdx = cUnit->dfsOrder.elemList[idx];
313 BasicBlock *bb = (BasicBlock *) oatGrowableListGetElement(blockList,
314 blockIdx);
315 if (bb == NULL) break;
buzbeed1643e42012-09-05 14:06:51 -0700316 if (bb->blockType == kDead) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 if (bb->blockType == kEntryBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700318 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 } else if (bb->blockType == kExitBlock) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700320 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 } else if (bb->blockType == kDalvikByteCode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700322 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
323 bb->startOffset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 const MIR *mir;
325 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
326 bb->firstMIRInsn ? " | " : " ");
327 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
328 fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset,
329 mir->ssaRep ? oatFullDisassembler(cUnit, mir) :
330 Instruction::Name(mir->dalvikInsn.opcode),
331 mir->next ? " | " : " ");
332 }
333 fprintf(file, " }\"];\n\n");
334 } else if (bb->blockType == kExceptionHandling) {
335 char blockName[BLOCK_NAME_LEN];
336
337 oatGetBlockName(bb, blockName);
338 fprintf(file, " %s [shape=invhouse];\n", blockName);
buzbee67bf8852011-08-17 17:51:35 -0700339 }
buzbee67bf8852011-08-17 17:51:35 -0700340
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
buzbee67bf8852011-08-17 17:51:35 -0700342
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 if (bb->taken) {
344 oatGetBlockName(bb, blockName1);
345 oatGetBlockName(bb->taken, blockName2);
346 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
347 blockName1, blockName2);
buzbee67bf8852011-08-17 17:51:35 -0700348 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 if (bb->fallThrough) {
350 oatGetBlockName(bb, blockName1);
351 oatGetBlockName(bb->fallThrough, blockName2);
352 fprintf(file, " %s:s -> %s:n\n", blockName1, blockName2);
353 }
354
355 if (bb->successorBlockList.blockListType != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700356 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
357 bb->startOffset, bb->id,
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 (bb->successorBlockList.blockListType == kCatch) ?
359 "Mrecord" : "record");
360 GrowableListIterator iterator;
361 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
362 &iterator);
363 SuccessorBlockInfo *successorBlockInfo =
364 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
365
366 int succId = 0;
367 while (true) {
368 if (successorBlockInfo == NULL) break;
369
370 BasicBlock *destBlock = successorBlockInfo->block;
371 SuccessorBlockInfo *nextSuccessorBlockInfo =
372 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
373
374 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
375 succId++,
376 successorBlockInfo->key,
377 destBlock->startOffset,
378 (nextSuccessorBlockInfo != NULL) ? " | " : " ");
379
380 successorBlockInfo = nextSuccessorBlockInfo;
381 }
382 fprintf(file, " }\"];\n\n");
383
384 oatGetBlockName(bb, blockName1);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700385 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
386 blockName1, bb->startOffset, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700387
388 if (bb->successorBlockList.blockListType == kPackedSwitch ||
389 bb->successorBlockList.blockListType == kSparseSwitch) {
390
391 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
392 &iterator);
393
394 succId = 0;
395 while (true) {
396 SuccessorBlockInfo *successorBlockInfo = (SuccessorBlockInfo *)
397 oatGrowableListIteratorNext(&iterator);
398 if (successorBlockInfo == NULL) break;
399
400 BasicBlock *destBlock = successorBlockInfo->block;
401
402 oatGetBlockName(destBlock, blockName2);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700403 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->startOffset,
404 bb->id, succId++, blockName2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 }
406 }
407 }
408 fprintf(file, "\n");
409
410 /* Display the dominator tree */
411 oatGetBlockName(bb, blockName1);
412 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
413 blockName1, blockName1);
414 if (bb->iDom) {
415 oatGetBlockName(bb->iDom, blockName2);
416 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", blockName2, blockName1);
417 }
418 }
419 fprintf(file, "}\n");
420 fclose(file);
buzbee67bf8852011-08-17 17:51:35 -0700421}
422
423/* Verify if all the successor is connected with all the claimed predecessors */
buzbee31a4a6f2012-02-28 15:36:15 -0800424bool verifyPredInfo(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700425{
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700427
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 oatGrowableListIteratorInit(bb->predecessors, &iter);
429 while (true) {
430 BasicBlock *predBB = (BasicBlock*)oatGrowableListIteratorNext(&iter);
431 if (!predBB) break;
432 bool found = false;
433 if (predBB->taken == bb) {
434 found = true;
435 } else if (predBB->fallThrough == bb) {
436 found = true;
437 } else if (predBB->successorBlockList.blockListType != kNotUsed) {
438 GrowableListIterator iterator;
439 oatGrowableListIteratorInit(&predBB->successorBlockList.blocks,
440 &iterator);
441 while (true) {
442 SuccessorBlockInfo *successorBlockInfo = (SuccessorBlockInfo *)
443 oatGrowableListIteratorNext(&iterator);
444 if (successorBlockInfo == NULL) break;
445 BasicBlock *succBB = successorBlockInfo->block;
446 if (succBB == bb) {
buzbee67bf8852011-08-17 17:51:35 -0700447 found = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 break;
buzbee67bf8852011-08-17 17:51:35 -0700449 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 }
buzbee67bf8852011-08-17 17:51:35 -0700451 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 if (found == false) {
453 char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN];
454 oatGetBlockName(bb, blockName1);
455 oatGetBlockName(predBB, blockName2);
456 oatDumpCFG(cUnit, "/sdcard/cfg/");
457 LOG(FATAL) << "Successor " << blockName1 << "not found from "
458 << blockName2;
459 }
460 }
461 return true;
buzbee67bf8852011-08-17 17:51:35 -0700462}
463
464/* Identify code range in try blocks and set up the empty catch blocks */
buzbee31a4a6f2012-02-28 15:36:15 -0800465void processTryCatchBlocks(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700466{
Bill Buzbeea114add2012-05-03 15:00:40 -0700467 const DexFile::CodeItem* code_item = cUnit->code_item;
468 int triesSize = code_item->tries_size_;
469 int offset;
buzbee67bf8852011-08-17 17:51:35 -0700470
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 if (triesSize == 0) {
472 return;
473 }
474
475 ArenaBitVector* tryBlockAddr = cUnit->tryBlockAddr;
476
477 for (int i = 0; i < triesSize; i++) {
478 const DexFile::TryItem* pTry =
479 DexFile::GetTryItems(*code_item, i);
480 int startOffset = pTry->start_addr_;
481 int endOffset = startOffset + pTry->insn_count_;
482 for (offset = startOffset; offset < endOffset; offset++) {
483 oatSetBit(cUnit, tryBlockAddr, offset);
buzbee67bf8852011-08-17 17:51:35 -0700484 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 }
buzbee67bf8852011-08-17 17:51:35 -0700486
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 // Iterate over each of the handlers to enqueue the empty Catch blocks
488 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
489 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
490 for (uint32_t idx = 0; idx < handlers_size; idx++) {
491 CatchHandlerIterator iterator(handlers_ptr);
492 for (; iterator.HasNext(); iterator.Next()) {
493 uint32_t address = iterator.GetHandlerAddress();
494 findBlock(cUnit, address, false /* split */, true /*create*/,
495 /* immedPredBlockP */ NULL);
buzbee67bf8852011-08-17 17:51:35 -0700496 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 handlers_ptr = iterator.EndDataPointer();
498 }
buzbee67bf8852011-08-17 17:51:35 -0700499}
500
Elliott Hughesadb8c672012-03-06 16:49:32 -0800501/* Process instructions with the kBranch flag */
buzbee31a4a6f2012-02-28 15:36:15 -0800502BasicBlock* processCanBranch(CompilationUnit* cUnit, BasicBlock* curBlock,
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 MIR* insn, int curOffset, int width, int flags,
504 const u2* codePtr, const u2* codeEnd)
buzbee67bf8852011-08-17 17:51:35 -0700505{
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 int target = curOffset;
507 switch (insn->dalvikInsn.opcode) {
508 case Instruction::GOTO:
509 case Instruction::GOTO_16:
510 case Instruction::GOTO_32:
511 target += (int) insn->dalvikInsn.vA;
512 break;
513 case Instruction::IF_EQ:
514 case Instruction::IF_NE:
515 case Instruction::IF_LT:
516 case Instruction::IF_GE:
517 case Instruction::IF_GT:
518 case Instruction::IF_LE:
buzbee0967a252012-09-14 10:43:54 -0700519 curBlock->conditionalBranch = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700520 target += (int) insn->dalvikInsn.vC;
521 break;
522 case Instruction::IF_EQZ:
523 case Instruction::IF_NEZ:
524 case Instruction::IF_LTZ:
525 case Instruction::IF_GEZ:
526 case Instruction::IF_GTZ:
527 case Instruction::IF_LEZ:
buzbee0967a252012-09-14 10:43:54 -0700528 curBlock->conditionalBranch = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 target += (int) insn->dalvikInsn.vB;
530 break;
531 default:
532 LOG(FATAL) << "Unexpected opcode(" << (int)insn->dalvikInsn.opcode
533 << ") with kBranch set";
534 }
535 BasicBlock *takenBlock = findBlock(cUnit, target,
536 /* split */
537 true,
538 /* create */
539 true,
540 /* immedPredBlockP */
541 &curBlock);
542 curBlock->taken = takenBlock;
543 oatInsertGrowableList(cUnit, takenBlock->predecessors, (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700544
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 /* Always terminate the current block for conditional branches */
546 if (flags & Instruction::kContinue) {
547 BasicBlock *fallthroughBlock = findBlock(cUnit,
548 curOffset + width,
549 /*
550 * If the method is processed
551 * in sequential order from the
552 * beginning, we don't need to
553 * specify split for continue
554 * blocks. However, this
555 * routine can be called by
556 * compileLoop, which starts
557 * parsing the method from an
558 * arbitrary address in the
559 * method body.
560 */
561 true,
562 /* create */
563 true,
564 /* immedPredBlockP */
565 &curBlock);
566 curBlock->fallThrough = fallthroughBlock;
567 oatInsertGrowableList(cUnit, fallthroughBlock->predecessors,
568 (intptr_t)curBlock);
569 } else if (codePtr < codeEnd) {
570 /* Create a fallthrough block for real instructions (incl. NOP) */
571 if (contentIsInsn(codePtr)) {
572 findBlock(cUnit, curOffset + width,
573 /* split */
574 false,
575 /* create */
576 true,
577 /* immedPredBlockP */
578 NULL);
buzbee67bf8852011-08-17 17:51:35 -0700579 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 }
581 return curBlock;
buzbee67bf8852011-08-17 17:51:35 -0700582}
583
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584/* Process instructions with the kSwitch flag */
buzbee31a4a6f2012-02-28 15:36:15 -0800585void processCanSwitch(CompilationUnit* cUnit, BasicBlock* curBlock,
586 MIR* insn, int curOffset, int width, int flags)
buzbee67bf8852011-08-17 17:51:35 -0700587{
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 u2* switchData= (u2 *) (cUnit->insns + curOffset + insn->dalvikInsn.vB);
589 int size;
590 int* keyTable;
591 int* targetTable;
592 int i;
593 int firstKey;
buzbee67bf8852011-08-17 17:51:35 -0700594
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 /*
596 * Packed switch data format:
597 * ushort ident = 0x0100 magic value
598 * ushort size number of entries in the table
599 * int first_key first (and lowest) switch case value
600 * int targets[size] branch targets, relative to switch opcode
601 *
602 * Total size is (4+size*2) 16-bit code units.
603 */
604 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
605 DCHECK_EQ(static_cast<int>(switchData[0]),
606 static_cast<int>(Instruction::kPackedSwitchSignature));
607 size = switchData[1];
608 firstKey = switchData[2] | (switchData[3] << 16);
609 targetTable = (int *) &switchData[4];
610 keyTable = NULL; // Make the compiler happy
611 /*
612 * Sparse switch data format:
613 * ushort ident = 0x0200 magic value
614 * ushort size number of entries in the table; > 0
615 * int keys[size] keys, sorted low-to-high; 32-bit aligned
616 * int targets[size] branch targets, relative to switch opcode
617 *
618 * Total size is (2+size*4) 16-bit code units.
619 */
620 } else {
621 DCHECK_EQ(static_cast<int>(switchData[0]),
622 static_cast<int>(Instruction::kSparseSwitchSignature));
623 size = switchData[1];
624 keyTable = (int *) &switchData[2];
625 targetTable = (int *) &switchData[2 + size*2];
626 firstKey = 0; // To make the compiler happy
627 }
buzbee67bf8852011-08-17 17:51:35 -0700628
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 if (curBlock->successorBlockList.blockListType != kNotUsed) {
630 LOG(FATAL) << "Successor block list already in use: "
631 << (int)curBlock->successorBlockList.blockListType;
632 }
633 curBlock->successorBlockList.blockListType =
634 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
635 kPackedSwitch : kSparseSwitch;
636 oatInitGrowableList(cUnit, &curBlock->successorBlockList.blocks, size,
637 kListSuccessorBlocks);
638
639 for (i = 0; i < size; i++) {
640 BasicBlock *caseBlock = findBlock(cUnit, curOffset + targetTable[i],
641 /* split */
642 true,
643 /* create */
644 true,
645 /* immedPredBlockP */
646 &curBlock);
647 SuccessorBlockInfo *successorBlockInfo =
648 (SuccessorBlockInfo *) oatNew(cUnit, sizeof(SuccessorBlockInfo),
649 false, kAllocSuccessor);
650 successorBlockInfo->block = caseBlock;
651 successorBlockInfo->key =
Elliott Hughesadb8c672012-03-06 16:49:32 -0800652 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 firstKey + i : keyTable[i];
654 oatInsertGrowableList(cUnit, &curBlock->successorBlockList.blocks,
655 (intptr_t) successorBlockInfo);
656 oatInsertGrowableList(cUnit, caseBlock->predecessors,
buzbeeba938cb2012-02-03 14:47:55 -0800657 (intptr_t)curBlock);
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 }
659
660 /* Fall-through case */
661 BasicBlock* fallthroughBlock = findBlock(cUnit,
662 curOffset + width,
663 /* split */
664 false,
665 /* create */
666 true,
667 /* immedPredBlockP */
668 NULL);
669 curBlock->fallThrough = fallthroughBlock;
670 oatInsertGrowableList(cUnit, fallthroughBlock->predecessors,
671 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700672}
673
Elliott Hughesadb8c672012-03-06 16:49:32 -0800674/* Process instructions with the kThrow flag */
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700675BasicBlock* processCanThrow(CompilationUnit* cUnit, BasicBlock* curBlock,
676 MIR* insn, int curOffset, int width, int flags,
677 ArenaBitVector* tryBlockAddr, const u2* codePtr,
678 const u2* codeEnd)
buzbee67bf8852011-08-17 17:51:35 -0700679{
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 const DexFile::CodeItem* code_item = cUnit->code_item;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700681 bool inTryBlock = oatIsBitSet(tryBlockAddr, curOffset);
buzbee67bf8852011-08-17 17:51:35 -0700682
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 /* In try block */
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700684 if (inTryBlock) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 CatchHandlerIterator iterator(*code_item, curOffset);
buzbee67bf8852011-08-17 17:51:35 -0700686
Bill Buzbeea114add2012-05-03 15:00:40 -0700687 if (curBlock->successorBlockList.blockListType != kNotUsed) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700688 LOG(INFO) << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700689 LOG(FATAL) << "Successor block list already in use: "
690 << (int)curBlock->successorBlockList.blockListType;
buzbee67bf8852011-08-17 17:51:35 -0700691 }
692
Bill Buzbeea114add2012-05-03 15:00:40 -0700693 curBlock->successorBlockList.blockListType = kCatch;
694 oatInitGrowableList(cUnit, &curBlock->successorBlockList.blocks, 2,
695 kListSuccessorBlocks);
696
697 for (;iterator.HasNext(); iterator.Next()) {
698 BasicBlock *catchBlock = findBlock(cUnit, iterator.GetHandlerAddress(),
699 false /* split*/,
700 false /* creat */,
701 NULL /* immedPredBlockP */);
702 catchBlock->catchEntry = true;
703 SuccessorBlockInfo *successorBlockInfo = (SuccessorBlockInfo *)
704 oatNew(cUnit, sizeof(SuccessorBlockInfo), false, kAllocSuccessor);
705 successorBlockInfo->block = catchBlock;
706 successorBlockInfo->key = iterator.GetHandlerTypeIndex();
707 oatInsertGrowableList(cUnit, &curBlock->successorBlockList.blocks,
708 (intptr_t) successorBlockInfo);
709 oatInsertGrowableList(cUnit, catchBlock->predecessors,
710 (intptr_t)curBlock);
buzbee67bf8852011-08-17 17:51:35 -0700711 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700712 } else {
713 BasicBlock *ehBlock = oatNewBB(cUnit, kExceptionHandling,
714 cUnit->numBlocks++);
715 curBlock->taken = ehBlock;
716 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t) ehBlock);
717 ehBlock->startOffset = curOffset;
718 oatInsertGrowableList(cUnit, ehBlock->predecessors, (intptr_t)curBlock);
719 }
720
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700721 if (insn->dalvikInsn.opcode == Instruction::THROW){
buzbee0967a252012-09-14 10:43:54 -0700722 curBlock->explicitThrow = true;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700723 if ((codePtr < codeEnd) && contentIsInsn(codePtr)) {
724 // Force creation of new block following THROW via side-effect
725 findBlock(cUnit, curOffset + width, /* split */ false,
726 /* create */ true, /* immedPredBlockP */ NULL);
727 }
728 if (!inTryBlock) {
729 // Don't split a THROW that can't rethrow - we're done.
730 return curBlock;
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 }
732 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700733
734 /*
735 * Split the potentially-throwing instruction into two parts.
736 * The first half will be a pseudo-op that captures the exception
737 * edges and terminates the basic block. It always falls through.
738 * Then, create a new basic block that begins with the throwing instruction
739 * (minus exceptions). Note: this new basic block must NOT be entered into
740 * the blockMap. If the potentially-throwing instruction is the target of a
741 * future branch, we need to find the check psuedo half. The new
742 * basic block containing the work portion of the instruction should
743 * only be entered via fallthrough from the block containing the
744 * pseudo exception edge MIR. Note also that this new block is
745 * not automatically terminated after the work portion, and may
746 * contain following instructions.
747 */
748 BasicBlock *newBlock = oatNewBB(cUnit, kDalvikByteCode, cUnit->numBlocks++);
749 oatInsertGrowableList(cUnit, &cUnit->blockList, (intptr_t)newBlock);
750 newBlock->startOffset = insn->offset;
751 curBlock->fallThrough = newBlock;
752 oatInsertGrowableList(cUnit, newBlock->predecessors, (intptr_t)curBlock);
753 MIR* newInsn = (MIR*)oatNew(cUnit, sizeof(MIR), true, kAllocMIR);
754 *newInsn = *insn;
755 insn->dalvikInsn.opcode =
756 static_cast<Instruction::Code>(kMirOpCheck);
757 // Associate the two halves
758 insn->meta.throwInsn = newInsn;
759 newInsn->meta.throwInsn = insn;
760 oatAppendMIR(newBlock, newInsn);
761 return newBlock;
buzbee67bf8852011-08-17 17:51:35 -0700762}
763
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800764void oatInit(CompilationUnit* cUnit, const Compiler& compiler) {
765 if (!oatArchInit()) {
766 LOG(FATAL) << "Failed to initialize oat";
767 }
768 if (!oatHeapInit(cUnit)) {
769 LOG(FATAL) << "Failed to initialize oat heap";
770 }
771}
772
buzbeeabc4c6b2012-08-23 08:17:15 -0700773CompiledMethod* compileMethod(Compiler& compiler,
774 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#if defined(ART_USE_QUICK_COMPILER)
TDYa12755e5e6c2012-09-11 15:14:42 -0700779 , QuickCompiler* quick_compiler,
buzbeeabc4c6b2012-08-23 08:17:15 -0700780 bool gbcOnly
781#endif
782 )
buzbee67bf8852011-08-17 17:51:35 -0700783{
Bill Buzbeea114add2012-05-03 15:00:40 -0700784 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700785
Bill Buzbeea114add2012-05-03 15:00:40 -0700786 const u2* codePtr = code_item->insns_;
787 const u2* codeEnd = code_item->insns_ + code_item->insns_size_in_code_units_;
788 int numBlocks = 0;
789 unsigned int curOffset = 0;
buzbee67bf8852011-08-17 17:51:35 -0700790
Bill Buzbeea114add2012-05-03 15:00:40 -0700791 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
792 UniquePtr<CompilationUnit> cUnit(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800793
Bill Buzbeea114add2012-05-03 15:00:40 -0700794 oatInit(cUnit.get(), compiler);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800795
Bill Buzbeea114add2012-05-03 15:00:40 -0700796 cUnit->compiler = &compiler;
797 cUnit->class_linker = class_linker;
798 cUnit->dex_file = &dex_file;
Bill Buzbeea114add2012-05-03 15:00:40 -0700799 cUnit->method_idx = method_idx;
800 cUnit->code_item = code_item;
801 cUnit->access_flags = access_flags;
Ian Rogers08f753d2012-08-24 14:35:25 -0700802 cUnit->invoke_type = invoke_type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700803 cUnit->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
804 cUnit->instructionSet = compiler.GetInstructionSet();
805 cUnit->insns = code_item->insns_;
806 cUnit->insnsSize = code_item->insns_size_in_code_units_;
807 cUnit->numIns = code_item->ins_size_;
808 cUnit->numRegs = code_item->registers_size_ - cUnit->numIns;
809 cUnit->numOuts = code_item->outs_size_;
buzbee2cfc6392012-05-07 14:51:40 -0700810#if defined(ART_USE_QUICK_COMPILER)
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700811 DCHECK((cUnit->instructionSet == kThumb2) ||
812 (cUnit->instructionSet == kX86) ||
813 (cUnit->instructionSet == kMips));
TDYa12755e5e6c2012-09-11 15:14:42 -0700814 if (gbcOnly) {
815 cUnit->quick_compiler = quick_compiler;
816 } else {
817 // TODO: We need one LLVMContext per thread.
818 cUnit->quick_compiler =
819 reinterpret_cast<QuickCompiler*>(compiler.GetCompilerContext());
820 }
821 DCHECK(cUnit->quick_compiler != NULL);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700822 if (cUnit->instructionSet == kThumb2) {
823 // TODO: remove this once x86 is tested
buzbee85eee022012-07-16 22:12:38 -0700824 cUnit->genBitcode = true;
825 }
buzbee2a83e8f2012-07-13 16:42:30 -0700826#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 /* Adjust this value accordingly once inlining is performed */
828 cUnit->numDalvikRegisters = code_item->registers_size_;
829 // TODO: set this from command line
830 cUnit->compilerFlipMatch = false;
831 bool useMatch = !cUnit->compilerMethodMatch.empty();
832 bool match = useMatch && (cUnit->compilerFlipMatch ^
833 (PrettyMethod(method_idx, dex_file).find(cUnit->compilerMethodMatch) !=
834 std::string::npos));
835 if (!useMatch || match) {
836 cUnit->disableOpt = kCompilerOptimizerDisableFlags;
837 cUnit->enableDebug = kCompilerDebugFlags;
838 cUnit->printMe = VLOG_IS_ON(compiler) ||
839 (cUnit->enableDebug & (1 << kDebugVerbose));
840 }
buzbee2cfc6392012-05-07 14:51:40 -0700841#if defined(ART_USE_QUICK_COMPILER)
buzbee6969d502012-06-15 16:40:31 -0700842 if (cUnit->genBitcode) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700843 //cUnit->enableDebug |= (1 << kDebugVerifyBitcode);
buzbee2a83e8f2012-07-13 16:42:30 -0700844 //cUnit->printMe = true;
845 //cUnit->enableDebug |= (1 << kDebugDumpBitcodeFile);
buzbee6969d502012-06-15 16:40:31 -0700846 }
buzbee2cfc6392012-05-07 14:51:40 -0700847#endif
jeffhao7fbee072012-08-24 17:56:54 -0700848 if (cUnit->instructionSet == kMips) {
849 // Disable some optimizations for mips for now
850 cUnit->disableOpt |= (
851 (1 << kLoadStoreElimination) |
852 (1 << kLoadHoisting) |
853 (1 << kSuppressLoads) |
854 (1 << kNullCheckElimination) |
855 (1 << kPromoteRegs) |
856 (1 << kTrackLiveTemps) |
857 (1 << kSkipLargeMethodOptimization) |
858 (1 << kSafeOptimizations) |
859 (1 << kBBOpt) |
860 (1 << kMatch) |
861 (1 << kPromoteCompilerTemps));
862 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 /* Are we generating code for the debugger? */
864 if (compiler.IsDebuggingSupported()) {
865 cUnit->genDebugger = true;
866 // Yes, disable most optimizations
867 cUnit->disableOpt |= (
868 (1 << kLoadStoreElimination) |
869 (1 << kLoadHoisting) |
870 (1 << kSuppressLoads) |
871 (1 << kPromoteRegs) |
872 (1 << kBBOpt) |
873 (1 << kMatch) |
874 (1 << kTrackLiveTemps));
875 }
876
877 /* Gathering opcode stats? */
878 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
879 cUnit->opcodeCount = (int*)oatNew(cUnit.get(),
880 kNumPackedOpcodes * sizeof(int), true, kAllocMisc);
881 }
882
883 /* Assume non-throwing leaf */
884 cUnit->attrs = (METHOD_IS_LEAF | METHOD_IS_THROW_FREE);
885
886 /* Initialize the block list, estimate size based on insnsSize */
887 oatInitGrowableList(cUnit.get(), &cUnit->blockList, cUnit->insnsSize,
888 kListBlockList);
889
890 /* Initialize the switchTables list */
891 oatInitGrowableList(cUnit.get(), &cUnit->switchTables, 4,
892 kListSwitchTables);
893
894 /* Intialize the fillArrayData list */
895 oatInitGrowableList(cUnit.get(), &cUnit->fillArrayData, 4,
896 kListFillArrayData);
897
898 /* Intialize the throwLaunchpads list, estimate size based on insnsSize */
899 oatInitGrowableList(cUnit.get(), &cUnit->throwLaunchpads, cUnit->insnsSize,
900 kListThrowLaunchPads);
901
902 /* Intialize the instrinsicLaunchpads list */
903 oatInitGrowableList(cUnit.get(), &cUnit->intrinsicLaunchpads, 4,
904 kListMisc);
905
906
907 /* Intialize the suspendLaunchpads list */
908 oatInitGrowableList(cUnit.get(), &cUnit->suspendLaunchpads, 2048,
909 kListSuspendLaunchPads);
910
911 /* Allocate the bit-vector to track the beginning of basic blocks */
912 ArenaBitVector *tryBlockAddr = oatAllocBitVector(cUnit.get(),
913 cUnit->insnsSize,
914 true /* expandable */);
915 cUnit->tryBlockAddr = tryBlockAddr;
916
917 /* Create the default entry and exit blocks and enter them to the list */
918 BasicBlock *entryBlock = oatNewBB(cUnit.get(), kEntryBlock, numBlocks++);
919 BasicBlock *exitBlock = oatNewBB(cUnit.get(), kExitBlock, numBlocks++);
920
921 cUnit->entryBlock = entryBlock;
922 cUnit->exitBlock = exitBlock;
923
924 oatInsertGrowableList(cUnit.get(), &cUnit->blockList, (intptr_t) entryBlock);
925 oatInsertGrowableList(cUnit.get(), &cUnit->blockList, (intptr_t) exitBlock);
926
927 /* Current block to record parsed instructions */
928 BasicBlock *curBlock = oatNewBB(cUnit.get(), kDalvikByteCode, numBlocks++);
929 curBlock->startOffset = 0;
930 oatInsertGrowableList(cUnit.get(), &cUnit->blockList, (intptr_t) curBlock);
931 /* Add first block to the fast lookup cache */
932 cUnit->blockMap.Put(curBlock->startOffset, curBlock);
933 entryBlock->fallThrough = curBlock;
934 oatInsertGrowableList(cUnit.get(), curBlock->predecessors,
935 (intptr_t)entryBlock);
936
937 /*
938 * Store back the number of blocks since new blocks may be created of
939 * accessing cUnit.
940 */
941 cUnit->numBlocks = numBlocks;
942
943 /* Identify code range in try blocks and set up the empty catch blocks */
944 processTryCatchBlocks(cUnit.get());
945
946 /* Set up for simple method detection */
947 int numPatterns = sizeof(specialPatterns)/sizeof(specialPatterns[0]);
948 bool livePattern = (numPatterns > 0) && !(cUnit->disableOpt & (1 << kMatch));
Elliott Hughesabe64aa2012-05-30 17:34:45 -0700949 bool* deadPattern = (bool*)oatNew(cUnit.get(), sizeof(bool) * numPatterns, true,
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 kAllocMisc);
951 SpecialCaseHandler specialCase = kNoHandler;
952 int patternPos = 0;
953
954 /* Parse all instructions and put them into containing basic blocks */
955 while (codePtr < codeEnd) {
956 MIR *insn = (MIR *) oatNew(cUnit.get(), sizeof(MIR), true, kAllocMIR);
957 insn->offset = curOffset;
958 int width = parseInsn(cUnit.get(), codePtr, &insn->dalvikInsn, false);
959 insn->width = width;
960 Instruction::Code opcode = insn->dalvikInsn.opcode;
961 if (cUnit->opcodeCount != NULL) {
962 cUnit->opcodeCount[static_cast<int>(opcode)]++;
buzbee44b412b2012-02-04 08:50:53 -0800963 }
964
Bill Buzbeea114add2012-05-03 15:00:40 -0700965 /* Terminate when the data section is seen */
966 if (width == 0)
967 break;
968
969 /* Possible simple method? */
970 if (livePattern) {
971 livePattern = false;
972 specialCase = kNoHandler;
973 for (int i = 0; i < numPatterns; i++) {
974 if (!deadPattern[i]) {
975 if (specialPatterns[i].opcodes[patternPos] == opcode) {
976 livePattern = true;
977 specialCase = specialPatterns[i].handlerCode;
978 } else {
979 deadPattern[i] = true;
980 }
981 }
982 }
983 patternPos++;
buzbeea7c12682012-03-19 13:13:53 -0700984 }
985
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 oatAppendMIR(curBlock, insn);
buzbeecefd1872011-09-09 09:59:52 -0700987
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 codePtr += width;
Ian Rogersa75a0132012-09-28 11:41:42 -0700989 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbee67bf8852011-08-17 17:51:35 -0700990
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 int dfFlags = oatDataFlowAttributes[insn->dalvikInsn.opcode];
buzbee67bf8852011-08-17 17:51:35 -0700992
Bill Buzbeea114add2012-05-03 15:00:40 -0700993 if (dfFlags & DF_HAS_DEFS) {
buzbeebff24652012-05-06 16:22:05 -0700994 cUnit->defCount += (dfFlags & DF_A_WIDE) ? 2 : 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700995 }
buzbee67bf8852011-08-17 17:51:35 -0700996
Bill Buzbeea114add2012-05-03 15:00:40 -0700997 if (flags & Instruction::kBranch) {
998 curBlock = processCanBranch(cUnit.get(), curBlock, insn, curOffset,
999 width, flags, codePtr, codeEnd);
1000 } else if (flags & Instruction::kReturn) {
1001 curBlock->fallThrough = exitBlock;
1002 oatInsertGrowableList(cUnit.get(), exitBlock->predecessors,
1003 (intptr_t)curBlock);
1004 /*
1005 * Terminate the current block if there are instructions
1006 * afterwards.
1007 */
1008 if (codePtr < codeEnd) {
1009 /*
1010 * Create a fallthrough block for real instructions
1011 * (incl. NOP).
1012 */
1013 if (contentIsInsn(codePtr)) {
1014 findBlock(cUnit.get(), curOffset + width,
1015 /* split */
1016 false,
1017 /* create */
1018 true,
1019 /* immedPredBlockP */
1020 NULL);
1021 }
1022 }
1023 } else if (flags & Instruction::kThrow) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001024 curBlock = processCanThrow(cUnit.get(), curBlock, insn, curOffset,
1025 width, flags, tryBlockAddr, codePtr, codeEnd);
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 } else if (flags & Instruction::kSwitch) {
1027 processCanSwitch(cUnit.get(), curBlock, insn, curOffset, width, flags);
1028 }
1029 curOffset += width;
1030 BasicBlock *nextBlock = findBlock(cUnit.get(), curOffset,
1031 /* split */
1032 false,
1033 /* create */
1034 false,
1035 /* immedPredBlockP */
1036 NULL);
1037 if (nextBlock) {
1038 /*
1039 * The next instruction could be the target of a previously parsed
1040 * forward branch so a block is already created. If the current
1041 * instruction is not an unconditional branch, connect them through
1042 * the fall-through link.
1043 */
1044 DCHECK(curBlock->fallThrough == NULL ||
1045 curBlock->fallThrough == nextBlock ||
1046 curBlock->fallThrough == exitBlock);
buzbee5ade1d22011-09-09 14:44:52 -07001047
Bill Buzbeea114add2012-05-03 15:00:40 -07001048 if ((curBlock->fallThrough == NULL) && (flags & Instruction::kContinue)) {
1049 curBlock->fallThrough = nextBlock;
1050 oatInsertGrowableList(cUnit.get(), nextBlock->predecessors,
1051 (intptr_t)curBlock);
1052 }
1053 curBlock = nextBlock;
1054 }
1055 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001056
Bill Buzbeea114add2012-05-03 15:00:40 -07001057 if (!(cUnit->disableOpt & (1 << kSkipLargeMethodOptimization))) {
1058 if ((cUnit->numBlocks > MANY_BLOCKS) ||
1059 ((cUnit->numBlocks > MANY_BLOCKS_INITIALIZER) &&
1060 PrettyMethod(method_idx, dex_file, false).find("init>") !=
1061 std::string::npos)) {
1062 cUnit->qdMode = true;
1063 }
1064 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001065
Bill Buzbeea114add2012-05-03 15:00:40 -07001066 if (cUnit->qdMode) {
buzbeed1643e42012-09-05 14:06:51 -07001067#if !defined(ART_USE_QUICK_COMPILER)
1068 // Bitcode generation requires full dataflow analysis
Bill Buzbeea114add2012-05-03 15:00:40 -07001069 cUnit->disableDataflow = true;
buzbeed1643e42012-09-05 14:06:51 -07001070#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001071 // Disable optimization which require dataflow/ssa
1072 cUnit->disableOpt |=
buzbeed1643e42012-09-05 14:06:51 -07001073#if !defined(ART_USE_QUICK_COMPILER)
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 (1 << kNullCheckElimination) |
buzbeed1643e42012-09-05 14:06:51 -07001075#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001076 (1 << kBBOpt) |
1077 (1 << kPromoteRegs);
1078 if (cUnit->printMe) {
1079 LOG(INFO) << "QD mode enabled: "
1080 << PrettyMethod(method_idx, dex_file)
1081 << " too big: " << cUnit->numBlocks;
1082 }
1083 }
buzbeec1f45042011-09-21 16:03:19 -07001084
Bill Buzbeea114add2012-05-03 15:00:40 -07001085 if (cUnit->printMe) {
1086 oatDumpCompilationUnit(cUnit.get());
1087 }
buzbee67bf8852011-08-17 17:51:35 -07001088
buzbee0967a252012-09-14 10:43:54 -07001089 /* Do a code layout pass */
1090 oatMethodCodeLayout(cUnit.get());
1091
Bill Buzbeea114add2012-05-03 15:00:40 -07001092 if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) {
1093 /* Verify if all blocks are connected as claimed */
1094 oatDataFlowAnalysisDispatcher(cUnit.get(), verifyPredInfo, kAllNodes,
1095 false /* isIterative */);
1096 }
buzbee67bf8852011-08-17 17:51:35 -07001097
Bill Buzbeea114add2012-05-03 15:00:40 -07001098 /* Perform SSA transformation for the whole method */
1099 oatMethodSSATransformation(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001100
buzbee2cfc6392012-05-07 14:51:40 -07001101 /* Do constant propagation */
1102 // TODO: Probably need to make these expandable to support new ssa names
1103 // introducted during MIR optimization passes
1104 cUnit->isConstantV = oatAllocBitVector(cUnit.get(), cUnit->numSSARegs,
1105 false /* not expandable */);
1106 cUnit->constantValues =
1107 (int*)oatNew(cUnit.get(), sizeof(int) * cUnit->numSSARegs, true,
1108 kAllocDFInfo);
1109 oatDataFlowAnalysisDispatcher(cUnit.get(), oatDoConstantPropagation,
1110 kAllNodes,
1111 false /* isIterative */);
1112
Bill Buzbeea114add2012-05-03 15:00:40 -07001113 /* Detect loops */
1114 oatMethodLoopDetection(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001115
Bill Buzbeea114add2012-05-03 15:00:40 -07001116 /* Count uses */
1117 oatMethodUseCount(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001118
Bill Buzbeea114add2012-05-03 15:00:40 -07001119 /* Perform null check elimination */
1120 oatMethodNullCheckElimination(cUnit.get());
1121
buzbeed1643e42012-09-05 14:06:51 -07001122 /* Combine basic blocks where possible */
1123 oatMethodBasicBlockCombine(cUnit.get());
1124
Bill Buzbeea114add2012-05-03 15:00:40 -07001125 /* Do some basic block optimizations */
1126 oatMethodBasicBlockOptimization(cUnit.get());
1127
buzbeed1643e42012-09-05 14:06:51 -07001128 if (cUnit->enableDebug & (1 << kDebugDumpCheckStats)) {
1129 oatDumpCheckStats(cUnit.get());
1130 }
1131
Bill Buzbeea114add2012-05-03 15:00:40 -07001132 oatInitializeRegAlloc(cUnit.get()); // Needs to happen after SSA naming
1133
1134 /* Allocate Registers using simple local allocation scheme */
1135 oatSimpleRegAlloc(cUnit.get());
1136
buzbee2cfc6392012-05-07 14:51:40 -07001137#if defined(ART_USE_QUICK_COMPILER)
1138 /* Go the LLVM path? */
1139 if (cUnit->genBitcode) {
1140 // MIR->Bitcode
1141 oatMethodMIR2Bitcode(cUnit.get());
TDYa12755e5e6c2012-09-11 15:14:42 -07001142 if (gbcOnly) {
buzbeeabc4c6b2012-08-23 08:17:15 -07001143 // all done
1144 oatArenaReset(cUnit.get());
1145 return NULL;
1146 }
buzbee2cfc6392012-05-07 14:51:40 -07001147 // Bitcode->LIR
1148 oatMethodBitcode2LIR(cUnit.get());
1149 } else {
1150#endif
1151 if (specialCase != kNoHandler) {
1152 /*
1153 * Custom codegen for special cases. If for any reason the
1154 * special codegen doesn't succeed, cUnit->firstLIRInsn will
1155 * set to NULL;
1156 */
1157 oatSpecialMIR2LIR(cUnit.get(), specialCase);
1158 }
buzbee67bf8852011-08-17 17:51:35 -07001159
buzbee2cfc6392012-05-07 14:51:40 -07001160 /* Convert MIR to LIR, etc. */
1161 if (cUnit->firstLIRInsn == NULL) {
1162 oatMethodMIR2LIR(cUnit.get());
1163 }
1164#if defined(ART_USE_QUICK_COMPILER)
Bill Buzbeea114add2012-05-03 15:00:40 -07001165 }
buzbee2cfc6392012-05-07 14:51:40 -07001166#endif
buzbee67bf8852011-08-17 17:51:35 -07001167
Bill Buzbeea114add2012-05-03 15:00:40 -07001168 // Debugging only
1169 if (cUnit->enableDebug & (1 << kDebugDumpCFG)) {
1170 oatDumpCFG(cUnit.get(), "/sdcard/cfg/");
1171 }
buzbee16da88c2012-03-20 10:38:17 -07001172
Bill Buzbeea114add2012-05-03 15:00:40 -07001173 /* Method is not empty */
1174 if (cUnit->firstLIRInsn) {
buzbee67bf8852011-08-17 17:51:35 -07001175
Bill Buzbeea114add2012-05-03 15:00:40 -07001176 // mark the targets of switch statement case labels
1177 oatProcessSwitchTables(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001178
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 /* Convert LIR into machine code. */
1180 oatAssembleLIR(cUnit.get());
buzbee99ba9642012-01-25 14:23:14 -08001181
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07001182 if (cUnit->printMe) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001183 oatCodegenDump(cUnit.get());
buzbee67bf8852011-08-17 17:51:35 -07001184 }
1185
Bill Buzbeea114add2012-05-03 15:00:40 -07001186 if (cUnit->opcodeCount != NULL) {
1187 LOG(INFO) << "Opcode Count";
1188 for (int i = 0; i < kNumPackedOpcodes; i++) {
1189 if (cUnit->opcodeCount[i] != 0) {
1190 LOG(INFO) << "-C- "
1191 << Instruction::Name(static_cast<Instruction::Code>(i))
1192 << " " << cUnit->opcodeCount[i];
buzbee67bf8852011-08-17 17:51:35 -07001193 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001194 }
1195 }
1196 }
buzbeea7c12682012-03-19 13:13:53 -07001197
Bill Buzbeea114add2012-05-03 15:00:40 -07001198 // Combine vmap tables - core regs, then fp regs - into vmapTable
1199 std::vector<uint16_t> vmapTable;
buzbeeca7a5e42012-08-20 11:12:18 -07001200 // Core regs may have been inserted out of order - sort first
1201 std::sort(cUnit->coreVmapTable.begin(), cUnit->coreVmapTable.end());
Bill Buzbeea114add2012-05-03 15:00:40 -07001202 for (size_t i = 0 ; i < cUnit->coreVmapTable.size(); i++) {
buzbeeca7a5e42012-08-20 11:12:18 -07001203 // Copy, stripping out the phys register sort key
1204 vmapTable.push_back(~(-1 << VREG_NUM_WIDTH) & cUnit->coreVmapTable[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001205 }
1206 // If we have a frame, push a marker to take place of lr
1207 if (cUnit->frameSize > 0) {
1208 vmapTable.push_back(INVALID_VREG);
1209 } else {
1210 DCHECK_EQ(__builtin_popcount(cUnit->coreSpillMask), 0);
1211 DCHECK_EQ(__builtin_popcount(cUnit->fpSpillMask), 0);
1212 }
buzbeeca7a5e42012-08-20 11:12:18 -07001213 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 for (uint32_t i = 0; i < cUnit->fpVmapTable.size(); i++) {
1215 vmapTable.push_back(cUnit->fpVmapTable[i]);
1216 }
1217 CompiledMethod* result =
1218 new CompiledMethod(cUnit->instructionSet, cUnit->codeBuffer,
Bill Buzbeea5b30242012-09-28 07:19:44 -07001219 cUnit->frameSize, cUnit->coreSpillMask, cUnit->fpSpillMask,
1220 cUnit->combinedMappingTable, vmapTable, cUnit->nativeGcMap);
buzbee67bf8852011-08-17 17:51:35 -07001221
Bill Buzbeea114add2012-05-03 15:00:40 -07001222 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file)
1223 << " (" << (cUnit->codeBuffer.size() * sizeof(cUnit->codeBuffer[0]))
1224 << " bytes)";
buzbee5abfa3e2012-01-31 17:01:43 -08001225
1226#ifdef WITH_MEMSTATS
Bill Buzbeea114add2012-05-03 15:00:40 -07001227 if (cUnit->enableDebug & (1 << kDebugShowMemoryUsage)) {
1228 oatDumpMemStats(cUnit.get());
1229 }
buzbee5abfa3e2012-01-31 17:01:43 -08001230#endif
buzbee67bf8852011-08-17 17:51:35 -07001231
Bill Buzbeea114add2012-05-03 15:00:40 -07001232 oatArenaReset(cUnit.get());
buzbeeba938cb2012-02-03 14:47:55 -08001233
Bill Buzbeea114add2012-05-03 15:00:40 -07001234 return result;
buzbee67bf8852011-08-17 17:51:35 -07001235}
1236
buzbeeabc4c6b2012-08-23 08:17:15 -07001237#if defined(ART_USE_QUICK_COMPILER)
1238CompiledMethod* oatCompileMethod(Compiler& compiler,
1239 const DexFile::CodeItem* code_item,
1240 uint32_t access_flags, InvokeType invoke_type,
1241 uint32_t method_idx, jobject class_loader,
1242 const DexFile& dex_file)
1243{
1244 return compileMethod(compiler, code_item, access_flags, invoke_type, method_idx, class_loader,
TDYa12755e5e6c2012-09-11 15:14:42 -07001245 dex_file, NULL, false);
buzbeeabc4c6b2012-08-23 08:17:15 -07001246}
1247
1248/*
1249 * Given existing llvm module, context, intrinsic_helper and IRBuilder,
1250 * add the bitcode for the method described by code_item to the module.
1251 */
1252void oatCompileMethodToGBC(Compiler& compiler,
1253 const DexFile::CodeItem* code_item,
1254 uint32_t access_flags, InvokeType invoke_type,
1255 uint32_t method_idx, jobject class_loader,
1256 const DexFile& dex_file,
TDYa12755e5e6c2012-09-11 15:14:42 -07001257 QuickCompiler* quick_compiler)
buzbeeabc4c6b2012-08-23 08:17:15 -07001258{
1259 compileMethod(compiler, code_item, access_flags, invoke_type, method_idx, class_loader,
TDYa12755e5e6c2012-09-11 15:14:42 -07001260 dex_file, quick_compiler, true);
buzbeeabc4c6b2012-08-23 08:17:15 -07001261}
1262#else
1263CompiledMethod* oatCompileMethod(Compiler& compiler,
1264 const DexFile::CodeItem* code_item,
1265 uint32_t access_flags, InvokeType invoke_type,
1266 uint32_t method_idx, jobject class_loader,
1267 const DexFile& dex_file)
1268{
1269 return compileMethod(compiler, code_item, access_flags, invoke_type, method_idx, class_loader,
1270 dex_file);
1271}
1272#endif
1273
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001274} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001275
Shih-wei Liao46bde5c2012-08-31 11:28:16 -07001276#if !defined(ART_USE_LLVM_COMPILER)
Bill Buzbeea114add2012-05-03 15:00:40 -07001277extern "C" art::CompiledMethod*
1278 ArtCompileMethod(art::Compiler& compiler,
1279 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -07001280 uint32_t access_flags, art::InvokeType invoke_type,
1281 uint32_t method_idx, jobject class_loader,
Bill Buzbeea114add2012-05-03 15:00:40 -07001282 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001283{
1284 CHECK_EQ(compiler.GetInstructionSet(), art::oatInstructionSet());
Ian Rogers08f753d2012-08-24 14:35:25 -07001285 return art::oatCompileMethod(compiler, code_item, access_flags, invoke_type,
1286 method_idx, class_loader, dex_file);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001287}
Shih-wei Liao46bde5c2012-08-31 11:28:16 -07001288#endif