blob: 79208833f160777f6e47bc49102a33a82b510274 [file] [log] [blame]
buzbee2cfc6392012-05-07 14:51:40 -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
buzbee2cfc6392012-05-07 14:51:40 -070017#include "object_utils.h"
18
19#include <llvm/Support/ToolOutputFile.h>
20#include <llvm/Bitcode/ReaderWriter.h>
21#include <llvm/Analysis/Verifier.h>
22#include <llvm/Metadata.h>
23#include <llvm/ADT/DepthFirstIterator.h>
24#include <llvm/Instruction.h>
25#include <llvm/Type.h>
26#include <llvm/Instructions.h>
27#include <llvm/Support/Casting.h>
buzbeead8f15e2012-06-18 14:49:45 -070028#include <llvm/Support/InstIterator.h>
buzbee2cfc6392012-05-07 14:51:40 -070029
buzbee8320f382012-09-11 16:29:42 -070030static const char* kLabelFormat = "%c0x%x_%d";
buzbee951c0a12012-10-03 16:31:39 -070031static const char kInvalidBlock = 0xff;
buzbee8320f382012-09-11 16:29:42 -070032static const char kNormalBlock = 'L';
33static const char kCatchBlock = 'C';
buzbee2cfc6392012-05-07 14:51:40 -070034
35namespace art {
36extern const RegLocation badLoc;
buzbeeb03f4872012-06-11 15:22:11 -070037RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val);
buzbee2cfc6392012-05-07 14:51:40 -070038
39llvm::BasicBlock* getLLVMBlock(CompilationUnit* cUnit, int id)
40{
41 return cUnit->idToBlockMap.Get(id);
42}
43
44llvm::Value* getLLVMValue(CompilationUnit* cUnit, int sReg)
45{
46 return (llvm::Value*)oatGrowableListGetElement(&cUnit->llvmValues, sReg);
47}
48
49// Replace the placeholder value with the real definition
50void defineValue(CompilationUnit* cUnit, llvm::Value* val, int sReg)
51{
52 llvm::Value* placeholder = getLLVMValue(cUnit, sReg);
buzbee9a2487f2012-07-26 14:01:13 -070053 if (placeholder == NULL) {
54 // This can happen on instruction rewrite on verification failure
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070055 LOG(WARNING) << "Null placeholder";
buzbee9a2487f2012-07-26 14:01:13 -070056 return;
57 }
buzbee2cfc6392012-05-07 14:51:40 -070058 placeholder->replaceAllUsesWith(val);
59 val->takeName(placeholder);
60 cUnit->llvmValues.elemList[sReg] = (intptr_t)val;
buzbee4be777b2012-07-12 14:38:18 -070061 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(placeholder);
62 DCHECK(inst != NULL);
63 inst->eraseFromParent();
buzbee2cfc6392012-05-07 14:51:40 -070064}
65
66llvm::Type* llvmTypeFromLocRec(CompilationUnit* cUnit, RegLocation loc)
67{
68 llvm::Type* res = NULL;
69 if (loc.wide) {
70 if (loc.fp)
buzbee4f1181f2012-06-22 13:52:12 -070071 res = cUnit->irb->getDoubleTy();
buzbee2cfc6392012-05-07 14:51:40 -070072 else
buzbee4f1181f2012-06-22 13:52:12 -070073 res = cUnit->irb->getInt64Ty();
buzbee2cfc6392012-05-07 14:51:40 -070074 } else {
75 if (loc.fp) {
buzbee4f1181f2012-06-22 13:52:12 -070076 res = cUnit->irb->getFloatTy();
buzbee2cfc6392012-05-07 14:51:40 -070077 } else {
78 if (loc.ref)
79 res = cUnit->irb->GetJObjectTy();
80 else
buzbee4f1181f2012-06-22 13:52:12 -070081 res = cUnit->irb->getInt32Ty();
buzbee2cfc6392012-05-07 14:51:40 -070082 }
83 }
84 return res;
85}
86
buzbeead8f15e2012-06-18 14:49:45 -070087/* Create an in-memory RegLocation from an llvm Value. */
88void createLocFromValue(CompilationUnit* cUnit, llvm::Value* val)
89{
90 // NOTE: llvm takes shortcuts with c_str() - get to std::string firstt
91 std::string s(val->getName().str());
92 const char* valName = s.c_str();
buzbeead8f15e2012-06-18 14:49:45 -070093 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
94 DCHECK(it == cUnit->locMap.end()) << " - already defined: " << valName;
95 int baseSReg = INVALID_SREG;
96 int subscript = -1;
97 sscanf(valName, "v%d_%d", &baseSReg, &subscript);
98 if ((baseSReg == INVALID_SREG) && (!strcmp(valName, "method"))) {
99 baseSReg = SSA_METHOD_BASEREG;
100 subscript = 0;
101 }
buzbeead8f15e2012-06-18 14:49:45 -0700102 DCHECK_NE(baseSReg, INVALID_SREG);
103 DCHECK_NE(subscript, -1);
104 // TODO: redo during C++'ification
105 RegLocation loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, INVALID_REG,
106 INVALID_REG, INVALID_SREG, INVALID_SREG};
107 llvm::Type* ty = val->getType();
108 loc.wide = ((ty == cUnit->irb->getInt64Ty()) ||
109 (ty == cUnit->irb->getDoubleTy()));
110 loc.defined = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700111 loc.home = false; // May change during promotion
buzbeead8f15e2012-06-18 14:49:45 -0700112 loc.sRegLow = baseSReg;
113 loc.origSReg = cUnit->locMap.size();
buzbeeca7a5e42012-08-20 11:12:18 -0700114 PromotionMap pMap = cUnit->promotionMap[baseSReg];
115 if (ty == cUnit->irb->getFloatTy()) {
116 loc.fp = true;
117 if (pMap.fpLocation == kLocPhysReg) {
118 loc.lowReg = pMap.fpReg;
119 loc.location = kLocPhysReg;
120 loc.home = true;
121 }
122 } else if (ty == cUnit->irb->getDoubleTy()) {
123 loc.fp = true;
124 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
125 if ((pMap.fpLocation == kLocPhysReg) &&
126 (pMapHigh.fpLocation == kLocPhysReg) &&
127 ((pMap.fpReg & 0x1) == 0) &&
128 (pMap.fpReg + 1 == pMapHigh.fpReg)) {
129 loc.lowReg = pMap.fpReg;
130 loc.highReg = pMapHigh.fpReg;
131 loc.location = kLocPhysReg;
132 loc.home = true;
133 }
134 } else if (ty == cUnit->irb->GetJObjectTy()) {
135 loc.ref = true;
136 if (pMap.coreLocation == kLocPhysReg) {
137 loc.lowReg = pMap.coreReg;
138 loc.location = kLocPhysReg;
139 loc.home = true;
140 }
141 } else if (ty == cUnit->irb->getInt64Ty()) {
142 loc.core = true;
143 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
144 if ((pMap.coreLocation == kLocPhysReg) &&
145 (pMapHigh.coreLocation == kLocPhysReg)) {
146 loc.lowReg = pMap.coreReg;
147 loc.highReg = pMapHigh.coreReg;
148 loc.location = kLocPhysReg;
149 loc.home = true;
150 }
151 } else {
152 loc.core = true;
153 if (pMap.coreLocation == kLocPhysReg) {
154 loc.lowReg = pMap.coreReg;
155 loc.location = kLocPhysReg;
156 loc.home = true;
157 }
158 }
159
160 if (cUnit->printMe && loc.home) {
161 if (loc.wide) {
buzbee0967a252012-09-14 10:43:54 -0700162 LOG(INFO) << "Promoted wide " << s << " to regs " << static_cast<int>(loc.lowReg)
buzbeeca7a5e42012-08-20 11:12:18 -0700163 << "/" << loc.highReg;
164 } else {
buzbee0967a252012-09-14 10:43:54 -0700165 LOG(INFO) << "Promoted " << s << " to reg " << static_cast<int>(loc.lowReg);
buzbeeca7a5e42012-08-20 11:12:18 -0700166 }
167 }
buzbeead8f15e2012-06-18 14:49:45 -0700168 cUnit->locMap.Put(val, loc);
169}
buzbee2cfc6392012-05-07 14:51:40 -0700170void initIR(CompilationUnit* cUnit)
171{
buzbee4df2bbd2012-10-11 14:46:06 -0700172 LLVMInfo* llvmInfo = cUnit->llvm_info;
173 if (llvmInfo == NULL) {
174 CompilerTls* tls = cUnit->compiler->GetTls();
175 CHECK(tls != NULL);
176 llvmInfo = static_cast<LLVMInfo*>(tls->GetLLVMInfo());
177 if (llvmInfo == NULL) {
178 llvmInfo = new LLVMInfo();
179 tls->SetLLVMInfo(llvmInfo);
180 }
181 }
182 cUnit->context = llvmInfo->GetLLVMContext();
183 cUnit->module = llvmInfo->GetLLVMModule();
184 cUnit->intrinsic_helper = llvmInfo->GetIntrinsicHelper();
185 cUnit->irb = llvmInfo->GetIRBuilder();
buzbee2cfc6392012-05-07 14:51:40 -0700186}
187
188const char* llvmSSAName(CompilationUnit* cUnit, int ssaReg) {
189 return GET_ELEM_N(cUnit->ssaStrings, char*, ssaReg);
190}
191
buzbeef58c12c2012-07-03 15:06:29 -0700192llvm::BasicBlock* findCaseTarget(CompilationUnit* cUnit, uint32_t vaddr)
193{
194 BasicBlock* bb = oatFindBlock(cUnit, vaddr);
195 DCHECK(bb != NULL);
196 return getLLVMBlock(cUnit, bb->id);
197}
198
199void convertPackedSwitch(CompilationUnit* cUnit, BasicBlock* bb,
200 int32_t tableOffset, RegLocation rlSrc)
201{
202 const Instruction::PackedSwitchPayload* payload =
203 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
204 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
205
206 llvm::Value* value = getLLVMValue(cUnit, rlSrc.origSReg);
207
208 llvm::SwitchInst* sw =
209 cUnit->irb->CreateSwitch(value, getLLVMBlock(cUnit, bb->fallThrough->id),
210 payload->case_count);
211
212 for (uint16_t i = 0; i < payload->case_count; ++i) {
213 llvm::BasicBlock* llvmBB =
214 findCaseTarget(cUnit, cUnit->currentDalvikOffset + payload->targets[i]);
215 sw->addCase(cUnit->irb->getInt32(payload->first_key + i), llvmBB);
216 }
217 llvm::MDNode* switchNode =
218 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
219 sw->setMetadata("SwitchTable", switchNode);
220 bb->taken = NULL;
221 bb->fallThrough = NULL;
222}
223
buzbeea1da8a52012-07-09 14:00:21 -0700224void convertSparseSwitch(CompilationUnit* cUnit, BasicBlock* bb,
225 int32_t tableOffset, RegLocation rlSrc)
226{
227 const Instruction::SparseSwitchPayload* payload =
228 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
229 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
230
231 const int32_t* keys = payload->GetKeys();
232 const int32_t* targets = payload->GetTargets();
233
234 llvm::Value* value = getLLVMValue(cUnit, rlSrc.origSReg);
235
236 llvm::SwitchInst* sw =
237 cUnit->irb->CreateSwitch(value, getLLVMBlock(cUnit, bb->fallThrough->id),
238 payload->case_count);
239
240 for (size_t i = 0; i < payload->case_count; ++i) {
241 llvm::BasicBlock* llvmBB =
242 findCaseTarget(cUnit, cUnit->currentDalvikOffset + targets[i]);
243 sw->addCase(cUnit->irb->getInt32(keys[i]), llvmBB);
244 }
245 llvm::MDNode* switchNode =
246 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
247 sw->setMetadata("SwitchTable", switchNode);
248 bb->taken = NULL;
249 bb->fallThrough = NULL;
250}
251
buzbee8fa0fda2012-06-27 15:44:52 -0700252void convertSget(CompilationUnit* cUnit, int32_t fieldIndex,
253 greenland::IntrinsicHelper::IntrinsicId id,
254 RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700255{
buzbee8fa0fda2012-06-27 15:44:52 -0700256 llvm::Constant* fieldIdx = cUnit->irb->getInt32(fieldIndex);
buzbee4f1181f2012-06-22 13:52:12 -0700257 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700258 llvm::Value* res = cUnit->irb->CreateCall(intr, fieldIdx);
259 defineValue(cUnit, res, rlDest.origSReg);
260}
261
262void convertSput(CompilationUnit* cUnit, int32_t fieldIndex,
263 greenland::IntrinsicHelper::IntrinsicId id,
264 RegLocation rlSrc)
265{
266 llvm::SmallVector<llvm::Value*, 2> args;
267 args.push_back(cUnit->irb->getInt32(fieldIndex));
268 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
269 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
270 cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700271}
272
buzbee101305f2012-06-28 18:00:56 -0700273void convertFillArrayData(CompilationUnit* cUnit, int32_t offset,
274 RegLocation rlArray)
275{
276 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700277 id = greenland::IntrinsicHelper::HLFillArrayData;
buzbee101305f2012-06-28 18:00:56 -0700278 llvm::SmallVector<llvm::Value*, 2> args;
279 args.push_back(cUnit->irb->getInt32(offset));
280 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
281 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
282 cUnit->irb->CreateCall(intr, args);
283}
284
buzbee2cfc6392012-05-07 14:51:40 -0700285llvm::Value* emitConst(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
286 RegLocation loc)
287{
288 greenland::IntrinsicHelper::IntrinsicId id;
289 if (loc.wide) {
290 if (loc.fp) {
291 id = greenland::IntrinsicHelper::ConstDouble;
292 } else {
293 id = greenland::IntrinsicHelper::ConstLong;
294 }
295 } else {
296 if (loc.fp) {
297 id = greenland::IntrinsicHelper::ConstFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700298 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700299 id = greenland::IntrinsicHelper::ConstObj;
300 } else {
301 id = greenland::IntrinsicHelper::ConstInt;
302 }
303 }
304 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
305 return cUnit->irb->CreateCall(intr, src);
306}
buzbeeb03f4872012-06-11 15:22:11 -0700307
308void emitPopShadowFrame(CompilationUnit* cUnit)
309{
310 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
311 greenland::IntrinsicHelper::PopShadowFrame);
312 cUnit->irb->CreateCall(intr);
313}
314
buzbee2cfc6392012-05-07 14:51:40 -0700315llvm::Value* emitCopy(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
316 RegLocation loc)
317{
318 greenland::IntrinsicHelper::IntrinsicId id;
319 if (loc.wide) {
320 if (loc.fp) {
321 id = greenland::IntrinsicHelper::CopyDouble;
322 } else {
323 id = greenland::IntrinsicHelper::CopyLong;
324 }
325 } else {
326 if (loc.fp) {
327 id = greenland::IntrinsicHelper::CopyFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700328 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700329 id = greenland::IntrinsicHelper::CopyObj;
330 } else {
331 id = greenland::IntrinsicHelper::CopyInt;
332 }
333 }
334 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
335 return cUnit->irb->CreateCall(intr, src);
336}
337
buzbee32412962012-06-26 16:27:56 -0700338void convertMoveException(CompilationUnit* cUnit, RegLocation rlDest)
339{
340 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
341 greenland::IntrinsicHelper::GetException);
342 llvm::Value* res = cUnit->irb->CreateCall(func);
343 defineValue(cUnit, res, rlDest.origSReg);
344}
345
346void convertThrow(CompilationUnit* cUnit, RegLocation rlSrc)
347{
348 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
349 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
TDYa127f71bf5a2012-07-29 20:09:52 -0700350 greenland::IntrinsicHelper::HLThrowException);
buzbee32412962012-06-26 16:27:56 -0700351 cUnit->irb->CreateCall(func, src);
buzbee32412962012-06-26 16:27:56 -0700352}
353
buzbee8fa0fda2012-06-27 15:44:52 -0700354void convertMonitorEnterExit(CompilationUnit* cUnit, int optFlags,
355 greenland::IntrinsicHelper::IntrinsicId id,
356 RegLocation rlSrc)
357{
358 llvm::SmallVector<llvm::Value*, 2> args;
359 args.push_back(cUnit->irb->getInt32(optFlags));
360 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
361 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
362 cUnit->irb->CreateCall(func, args);
363}
364
buzbee76592632012-06-29 15:18:35 -0700365void convertArrayLength(CompilationUnit* cUnit, int optFlags,
366 RegLocation rlDest, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700367{
368 llvm::SmallVector<llvm::Value*, 2> args;
369 args.push_back(cUnit->irb->getInt32(optFlags));
370 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
371 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700372 greenland::IntrinsicHelper::OptArrayLength);
buzbee76592632012-06-29 15:18:35 -0700373 llvm::Value* res = cUnit->irb->CreateCall(func, args);
374 defineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700375}
376
buzbee2cfc6392012-05-07 14:51:40 -0700377void emitSuspendCheck(CompilationUnit* cUnit)
378{
379 greenland::IntrinsicHelper::IntrinsicId id =
380 greenland::IntrinsicHelper::CheckSuspend;
381 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
382 cUnit->irb->CreateCall(intr);
383}
384
385llvm::Value* convertCompare(CompilationUnit* cUnit, ConditionCode cc,
386 llvm::Value* src1, llvm::Value* src2)
387{
388 llvm::Value* res = NULL;
buzbee76592632012-06-29 15:18:35 -0700389 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700390 switch(cc) {
391 case kCondEq: res = cUnit->irb->CreateICmpEQ(src1, src2); break;
392 case kCondNe: res = cUnit->irb->CreateICmpNE(src1, src2); break;
393 case kCondLt: res = cUnit->irb->CreateICmpSLT(src1, src2); break;
394 case kCondGe: res = cUnit->irb->CreateICmpSGE(src1, src2); break;
395 case kCondGt: res = cUnit->irb->CreateICmpSGT(src1, src2); break;
396 case kCondLe: res = cUnit->irb->CreateICmpSLE(src1, src2); break;
397 default: LOG(FATAL) << "Unexpected cc value " << cc;
398 }
399 return res;
400}
401
402void convertCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
403 ConditionCode cc, RegLocation rlSrc1,
404 RegLocation rlSrc2)
405{
406 if (bb->taken->startOffset <= mir->offset) {
407 emitSuspendCheck(cUnit);
408 }
409 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
410 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
411 llvm::Value* condValue = convertCompare(cUnit, cc, src1, src2);
412 condValue->setName(StringPrintf("t%d", cUnit->tempName++));
413 cUnit->irb->CreateCondBr(condValue, getLLVMBlock(cUnit, bb->taken->id),
414 getLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700415 // Don't redo the fallthrough branch in the BB driver
416 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700417}
418
419void convertCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb,
420 MIR* mir, ConditionCode cc, RegLocation rlSrc1)
421{
422 if (bb->taken->startOffset <= mir->offset) {
423 emitSuspendCheck(cUnit);
424 }
425 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
426 llvm::Value* src2;
427 if (rlSrc1.ref) {
428 src2 = cUnit->irb->GetJNull();
429 } else {
430 src2 = cUnit->irb->getInt32(0);
431 }
432 llvm::Value* condValue = convertCompare(cUnit, cc, src1, src2);
buzbee2cfc6392012-05-07 14:51:40 -0700433 cUnit->irb->CreateCondBr(condValue, getLLVMBlock(cUnit, bb->taken->id),
434 getLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700435 // Don't redo the fallthrough branch in the BB driver
436 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700437}
438
439llvm::Value* genDivModOp(CompilationUnit* cUnit, bool isDiv, bool isLong,
440 llvm::Value* src1, llvm::Value* src2)
441{
442 greenland::IntrinsicHelper::IntrinsicId id;
443 if (isLong) {
444 if (isDiv) {
445 id = greenland::IntrinsicHelper::DivLong;
446 } else {
447 id = greenland::IntrinsicHelper::RemLong;
448 }
Logan Chien554e6072012-07-23 20:00:01 -0700449 } else {
450 if (isDiv) {
buzbee2cfc6392012-05-07 14:51:40 -0700451 id = greenland::IntrinsicHelper::DivInt;
452 } else {
453 id = greenland::IntrinsicHelper::RemInt;
Logan Chien554e6072012-07-23 20:00:01 -0700454 }
buzbee2cfc6392012-05-07 14:51:40 -0700455 }
456 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
457 llvm::SmallVector<llvm::Value*, 2>args;
458 args.push_back(src1);
459 args.push_back(src2);
460 return cUnit->irb->CreateCall(intr, args);
461}
462
463llvm::Value* genArithOp(CompilationUnit* cUnit, OpKind op, bool isLong,
464 llvm::Value* src1, llvm::Value* src2)
465{
466 llvm::Value* res = NULL;
467 switch(op) {
468 case kOpAdd: res = cUnit->irb->CreateAdd(src1, src2); break;
469 case kOpSub: res = cUnit->irb->CreateSub(src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700470 case kOpRsub: res = cUnit->irb->CreateSub(src2, src1); break;
buzbee2cfc6392012-05-07 14:51:40 -0700471 case kOpMul: res = cUnit->irb->CreateMul(src1, src2); break;
472 case kOpOr: res = cUnit->irb->CreateOr(src1, src2); break;
473 case kOpAnd: res = cUnit->irb->CreateAnd(src1, src2); break;
474 case kOpXor: res = cUnit->irb->CreateXor(src1, src2); break;
475 case kOpDiv: res = genDivModOp(cUnit, true, isLong, src1, src2); break;
476 case kOpRem: res = genDivModOp(cUnit, false, isLong, src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700477 case kOpLsl: res = cUnit->irb->CreateShl(src1, src2); break;
478 case kOpLsr: res = cUnit->irb->CreateLShr(src1, src2); break;
479 case kOpAsr: res = cUnit->irb->CreateAShr(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700480 default:
481 LOG(FATAL) << "Invalid op " << op;
482 }
483 return res;
484}
485
486void convertFPArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
487 RegLocation rlSrc1, RegLocation rlSrc2)
488{
489 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
490 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
491 llvm::Value* res = NULL;
492 switch(op) {
493 case kOpAdd: res = cUnit->irb->CreateFAdd(src1, src2); break;
494 case kOpSub: res = cUnit->irb->CreateFSub(src1, src2); break;
495 case kOpMul: res = cUnit->irb->CreateFMul(src1, src2); break;
496 case kOpDiv: res = cUnit->irb->CreateFDiv(src1, src2); break;
497 case kOpRem: res = cUnit->irb->CreateFRem(src1, src2); break;
498 default:
499 LOG(FATAL) << "Invalid op " << op;
500 }
501 defineValue(cUnit, res, rlDest.origSReg);
502}
503
buzbee2a83e8f2012-07-13 16:42:30 -0700504void convertShift(CompilationUnit* cUnit,
505 greenland::IntrinsicHelper::IntrinsicId id,
506 RegLocation rlDest, RegLocation rlSrc1, RegLocation rlSrc2)
buzbee4f1181f2012-06-22 13:52:12 -0700507{
buzbee2a83e8f2012-07-13 16:42:30 -0700508 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
509 llvm::SmallVector<llvm::Value*, 2>args;
510 args.push_back(getLLVMValue(cUnit, rlSrc1.origSReg));
511 args.push_back(getLLVMValue(cUnit, rlSrc2.origSReg));
512 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
513 defineValue(cUnit, res, rlDest.origSReg);
514}
515
516void convertShiftLit(CompilationUnit* cUnit,
517 greenland::IntrinsicHelper::IntrinsicId id,
518 RegLocation rlDest, RegLocation rlSrc, int shiftAmount)
519{
520 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
521 llvm::SmallVector<llvm::Value*, 2>args;
522 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
523 args.push_back(cUnit->irb->getInt32(shiftAmount));
524 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700525 defineValue(cUnit, res, rlDest.origSReg);
526}
527
buzbee2cfc6392012-05-07 14:51:40 -0700528void convertArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
529 RegLocation rlSrc1, RegLocation rlSrc2)
530{
531 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
532 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
buzbee4f4dfc72012-07-02 14:54:44 -0700533 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700534 llvm::Value* res = genArithOp(cUnit, op, rlDest.wide, src1, src2);
535 defineValue(cUnit, res, rlDest.origSReg);
536}
537
buzbeeb03f4872012-06-11 15:22:11 -0700538void setShadowFrameEntry(CompilationUnit* cUnit, llvm::Value* newVal)
539{
540 int index = -1;
541 DCHECK(newVal != NULL);
542 int vReg = SRegToVReg(cUnit, getLoc(cUnit, newVal).origSReg);
543 for (int i = 0; i < cUnit->numShadowFrameEntries; i++) {
544 if (cUnit->shadowMap[i] == vReg) {
545 index = i;
546 break;
547 }
548 }
TDYa127347166a2012-08-23 12:23:44 -0700549 if (index == -1) {
550 return;
551 }
buzbee6459e7c2012-10-02 14:42:41 -0700552 llvm::Type* ty = newVal->getType();
buzbeeb03f4872012-06-11 15:22:11 -0700553 greenland::IntrinsicHelper::IntrinsicId id =
554 greenland::IntrinsicHelper::SetShadowFrameEntry;
555 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
556 llvm::Value* tableSlot = cUnit->irb->getInt32(index);
buzbee6459e7c2012-10-02 14:42:41 -0700557 // If newVal is a Null pointer, we'll see it here as a const int. Replace
558 if (!ty->isPointerTy()) {
559 // TODO: assert newVal created w/ dex_lang_const_int(0) or dex_lang_const_float(0)
560 newVal = cUnit->irb->GetJNull();
561 }
buzbeeb03f4872012-06-11 15:22:11 -0700562 llvm::Value* args[] = { newVal, tableSlot };
563 cUnit->irb->CreateCall(func, args);
564}
565
buzbee2cfc6392012-05-07 14:51:40 -0700566void convertArithOpLit(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
567 RegLocation rlSrc1, int32_t imm)
568{
569 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
570 llvm::Value* src2 = cUnit->irb->getInt32(imm);
571 llvm::Value* res = genArithOp(cUnit, op, rlDest.wide, src1, src2);
572 defineValue(cUnit, res, rlDest.origSReg);
573}
574
buzbee101305f2012-06-28 18:00:56 -0700575/*
576 * Process arguments for invoke. Note: this code is also used to
577 * collect and process arguments for NEW_FILLED_ARRAY and NEW_FILLED_ARRAY_RANGE.
578 * The requirements are similar.
579 */
buzbee6969d502012-06-15 16:40:31 -0700580void convertInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
buzbee76592632012-06-29 15:18:35 -0700581 InvokeType invokeType, bool isRange, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -0700582{
583 CallInfo* info = oatNewCallInfo(cUnit, bb, mir, invokeType, isRange);
584 llvm::SmallVector<llvm::Value*, 10> args;
585 // Insert the invokeType
586 args.push_back(cUnit->irb->getInt32(static_cast<int>(invokeType)));
587 // Insert the method_idx
588 args.push_back(cUnit->irb->getInt32(info->index));
589 // Insert the optimization flags
590 args.push_back(cUnit->irb->getInt32(info->optFlags));
591 // Now, insert the actual arguments
buzbee6969d502012-06-15 16:40:31 -0700592 for (int i = 0; i < info->numArgWords;) {
buzbee6969d502012-06-15 16:40:31 -0700593 llvm::Value* val = getLLVMValue(cUnit, info->args[i].origSReg);
594 args.push_back(val);
595 i += info->args[i].wide ? 2 : 1;
596 }
597 /*
598 * Choose the invoke return type based on actual usage. Note: may
599 * be different than shorty. For example, if a function return value
600 * is not used, we'll treat this as a void invoke.
601 */
602 greenland::IntrinsicHelper::IntrinsicId id;
buzbee76592632012-06-29 15:18:35 -0700603 if (isFilledNewArray) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700604 id = greenland::IntrinsicHelper::HLFilledNewArray;
buzbee101305f2012-06-28 18:00:56 -0700605 } else if (info->result.location == kLocInvalid) {
buzbee6969d502012-06-15 16:40:31 -0700606 id = greenland::IntrinsicHelper::HLInvokeVoid;
607 } else {
608 if (info->result.wide) {
609 if (info->result.fp) {
610 id = greenland::IntrinsicHelper::HLInvokeDouble;
611 } else {
buzbee8fa0fda2012-06-27 15:44:52 -0700612 id = greenland::IntrinsicHelper::HLInvokeLong;
buzbee6969d502012-06-15 16:40:31 -0700613 }
614 } else if (info->result.ref) {
615 id = greenland::IntrinsicHelper::HLInvokeObj;
616 } else if (info->result.fp) {
617 id = greenland::IntrinsicHelper::HLInvokeFloat;
618 } else {
619 id = greenland::IntrinsicHelper::HLInvokeInt;
620 }
621 }
622 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
623 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
624 if (info->result.location != kLocInvalid) {
625 defineValue(cUnit, res, info->result.origSReg);
TDYa127890ea892012-08-22 10:49:42 -0700626 if (info->result.ref) {
627 setShadowFrameEntry(cUnit, (llvm::Value*)
628 cUnit->llvmValues.elemList[info->result.origSReg]);
629 }
buzbee6969d502012-06-15 16:40:31 -0700630 }
631}
632
buzbee101305f2012-06-28 18:00:56 -0700633void convertConstObject(CompilationUnit* cUnit, uint32_t idx,
634 greenland::IntrinsicHelper::IntrinsicId id,
635 RegLocation rlDest)
buzbee6969d502012-06-15 16:40:31 -0700636{
buzbee6969d502012-06-15 16:40:31 -0700637 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee101305f2012-06-28 18:00:56 -0700638 llvm::Value* index = cUnit->irb->getInt32(idx);
buzbee6969d502012-06-15 16:40:31 -0700639 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
640 defineValue(cUnit, res, rlDest.origSReg);
641}
642
buzbee101305f2012-06-28 18:00:56 -0700643void convertCheckCast(CompilationUnit* cUnit, uint32_t type_idx,
644 RegLocation rlSrc)
645{
646 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700647 id = greenland::IntrinsicHelper::HLCheckCast;
buzbee101305f2012-06-28 18:00:56 -0700648 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
649 llvm::SmallVector<llvm::Value*, 2> args;
650 args.push_back(cUnit->irb->getInt32(type_idx));
651 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
652 cUnit->irb->CreateCall(intr, args);
653}
654
buzbee8fa0fda2012-06-27 15:44:52 -0700655void convertNewInstance(CompilationUnit* cUnit, uint32_t type_idx,
656 RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700657{
658 greenland::IntrinsicHelper::IntrinsicId id;
659 id = greenland::IntrinsicHelper::NewInstance;
660 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
661 llvm::Value* index = cUnit->irb->getInt32(type_idx);
662 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
663 defineValue(cUnit, res, rlDest.origSReg);
664}
665
buzbee8fa0fda2012-06-27 15:44:52 -0700666void convertNewArray(CompilationUnit* cUnit, uint32_t type_idx,
667 RegLocation rlDest, RegLocation rlSrc)
668{
669 greenland::IntrinsicHelper::IntrinsicId id;
670 id = greenland::IntrinsicHelper::NewArray;
671 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
672 llvm::SmallVector<llvm::Value*, 2> args;
673 args.push_back(cUnit->irb->getInt32(type_idx));
674 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
675 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
676 defineValue(cUnit, res, rlDest.origSReg);
677}
678
679void convertAget(CompilationUnit* cUnit, int optFlags,
680 greenland::IntrinsicHelper::IntrinsicId id,
681 RegLocation rlDest, RegLocation rlArray, RegLocation rlIndex)
682{
683 llvm::SmallVector<llvm::Value*, 3> args;
684 args.push_back(cUnit->irb->getInt32(optFlags));
685 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
686 args.push_back(getLLVMValue(cUnit, rlIndex.origSReg));
687 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
688 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
689 defineValue(cUnit, res, rlDest.origSReg);
690}
691
692void convertAput(CompilationUnit* cUnit, int optFlags,
693 greenland::IntrinsicHelper::IntrinsicId id,
694 RegLocation rlSrc, RegLocation rlArray, RegLocation rlIndex)
695{
696 llvm::SmallVector<llvm::Value*, 4> args;
697 args.push_back(cUnit->irb->getInt32(optFlags));
698 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
699 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
700 args.push_back(getLLVMValue(cUnit, rlIndex.origSReg));
701 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
702 cUnit->irb->CreateCall(intr, args);
703}
704
buzbee101305f2012-06-28 18:00:56 -0700705void convertIget(CompilationUnit* cUnit, int optFlags,
706 greenland::IntrinsicHelper::IntrinsicId id,
707 RegLocation rlDest, RegLocation rlObj, int fieldIndex)
708{
709 llvm::SmallVector<llvm::Value*, 3> args;
710 args.push_back(cUnit->irb->getInt32(optFlags));
711 args.push_back(getLLVMValue(cUnit, rlObj.origSReg));
712 args.push_back(cUnit->irb->getInt32(fieldIndex));
713 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
714 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
715 defineValue(cUnit, res, rlDest.origSReg);
716}
717
718void convertIput(CompilationUnit* cUnit, int optFlags,
719 greenland::IntrinsicHelper::IntrinsicId id,
720 RegLocation rlSrc, RegLocation rlObj, int fieldIndex)
721{
722 llvm::SmallVector<llvm::Value*, 4> args;
723 args.push_back(cUnit->irb->getInt32(optFlags));
724 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
725 args.push_back(getLLVMValue(cUnit, rlObj.origSReg));
726 args.push_back(cUnit->irb->getInt32(fieldIndex));
727 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
728 cUnit->irb->CreateCall(intr, args);
729}
730
buzbee8fa0fda2012-06-27 15:44:52 -0700731void convertInstanceOf(CompilationUnit* cUnit, uint32_t type_idx,
732 RegLocation rlDest, RegLocation rlSrc)
733{
734 greenland::IntrinsicHelper::IntrinsicId id;
735 id = greenland::IntrinsicHelper::InstanceOf;
736 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
737 llvm::SmallVector<llvm::Value*, 2> args;
738 args.push_back(cUnit->irb->getInt32(type_idx));
739 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
740 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
741 defineValue(cUnit, res, rlDest.origSReg);
742}
743
buzbee101305f2012-06-28 18:00:56 -0700744void convertIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
745 RegLocation rlSrc)
746{
747 llvm::Value* res = cUnit->irb->CreateSExt(getLLVMValue(cUnit, rlSrc.origSReg),
748 cUnit->irb->getInt64Ty());
749 defineValue(cUnit, res, rlDest.origSReg);
750}
751
buzbee76592632012-06-29 15:18:35 -0700752void convertLongToInt(CompilationUnit* cUnit, RegLocation rlDest,
753 RegLocation rlSrc)
754{
755 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
756 llvm::Value* res = cUnit->irb->CreateTrunc(src, cUnit->irb->getInt32Ty());
757 defineValue(cUnit, res, rlDest.origSReg);
758}
759
760void convertFloatToDouble(CompilationUnit* cUnit, RegLocation rlDest,
761 RegLocation rlSrc)
762{
763 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
764 llvm::Value* res = cUnit->irb->CreateFPExt(src, cUnit->irb->getDoubleTy());
765 defineValue(cUnit, res, rlDest.origSReg);
766}
767
768void convertDoubleToFloat(CompilationUnit* cUnit, RegLocation rlDest,
769 RegLocation rlSrc)
770{
771 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
772 llvm::Value* res = cUnit->irb->CreateFPTrunc(src, cUnit->irb->getFloatTy());
773 defineValue(cUnit, res, rlDest.origSReg);
774}
775
776void convertWideComparison(CompilationUnit* cUnit,
777 greenland::IntrinsicHelper::IntrinsicId id,
778 RegLocation rlDest, RegLocation rlSrc1,
779 RegLocation rlSrc2)
780{
781 DCHECK_EQ(rlSrc1.fp, rlSrc2.fp);
782 DCHECK_EQ(rlSrc1.wide, rlSrc2.wide);
783 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
784 llvm::SmallVector<llvm::Value*, 2> args;
785 args.push_back(getLLVMValue(cUnit, rlSrc1.origSReg));
786 args.push_back(getLLVMValue(cUnit, rlSrc2.origSReg));
787 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
788 defineValue(cUnit, res, rlDest.origSReg);
789}
790
buzbee101305f2012-06-28 18:00:56 -0700791void convertIntNarrowing(CompilationUnit* cUnit, RegLocation rlDest,
792 RegLocation rlSrc,
793 greenland::IntrinsicHelper::IntrinsicId id)
794{
795 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700796 llvm::Value* res =
797 cUnit->irb->CreateCall(intr, getLLVMValue(cUnit, rlSrc.origSReg));
798 defineValue(cUnit, res, rlDest.origSReg);
799}
800
801void convertNeg(CompilationUnit* cUnit, RegLocation rlDest,
802 RegLocation rlSrc)
803{
804 llvm::Value* res = cUnit->irb->CreateNeg(getLLVMValue(cUnit, rlSrc.origSReg));
805 defineValue(cUnit, res, rlDest.origSReg);
806}
807
808void convertIntToFP(CompilationUnit* cUnit, llvm::Type* ty, RegLocation rlDest,
809 RegLocation rlSrc)
810{
811 llvm::Value* res =
812 cUnit->irb->CreateSIToFP(getLLVMValue(cUnit, rlSrc.origSReg), ty);
813 defineValue(cUnit, res, rlDest.origSReg);
814}
815
TDYa1274ec8ccd2012-08-11 07:04:57 -0700816void convertFPToInt(CompilationUnit* cUnit,
817 greenland::IntrinsicHelper::IntrinsicId id,
818 RegLocation rlDest,
buzbee76592632012-06-29 15:18:35 -0700819 RegLocation rlSrc)
820{
TDYa1274ec8ccd2012-08-11 07:04:57 -0700821 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
822 llvm::Value* res = cUnit->irb->CreateCall(intr, getLLVMValue(cUnit, rlSrc.origSReg));
buzbee76592632012-06-29 15:18:35 -0700823 defineValue(cUnit, res, rlDest.origSReg);
824}
825
826
827void convertNegFP(CompilationUnit* cUnit, RegLocation rlDest,
828 RegLocation rlSrc)
829{
830 llvm::Value* res =
831 cUnit->irb->CreateFNeg(getLLVMValue(cUnit, rlSrc.origSReg));
832 defineValue(cUnit, res, rlDest.origSReg);
833}
834
835void convertNot(CompilationUnit* cUnit, RegLocation rlDest,
836 RegLocation rlSrc)
837{
838 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
839 llvm::Value* res = cUnit->irb->CreateXor(src, static_cast<uint64_t>(-1));
buzbee101305f2012-06-28 18:00:56 -0700840 defineValue(cUnit, res, rlDest.origSReg);
841}
842
buzbee2cfc6392012-05-07 14:51:40 -0700843/*
844 * Target-independent code generation. Use only high-level
845 * load/store utilities here, or target-dependent genXX() handlers
846 * when necessary.
847 */
848bool convertMIRNode(CompilationUnit* cUnit, MIR* mir, BasicBlock* bb,
849 llvm::BasicBlock* llvmBB, LIR* labelList)
850{
851 bool res = false; // Assume success
852 RegLocation rlSrc[3];
853 RegLocation rlDest = badLoc;
buzbee2cfc6392012-05-07 14:51:40 -0700854 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee6969d502012-06-15 16:40:31 -0700855 uint32_t vB = mir->dalvikInsn.vB;
856 uint32_t vC = mir->dalvikInsn.vC;
buzbee8fa0fda2012-06-27 15:44:52 -0700857 int optFlags = mir->optimizationFlags;
buzbee6969d502012-06-15 16:40:31 -0700858
buzbeeb03f4872012-06-11 15:22:11 -0700859 bool objectDefinition = false;
buzbee2cfc6392012-05-07 14:51:40 -0700860
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700861 if (cUnit->printMe) {
862 if ((int)opcode < kMirOpFirst) {
863 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x"
864 << std::hex << (int)opcode;
865 } else {
866 LOG(INFO) << ".. opcode 0x" << std::hex << (int)opcode;
867 }
868 }
869
buzbee2cfc6392012-05-07 14:51:40 -0700870 /* Prep Src and Dest locations */
871 int nextSreg = 0;
872 int nextLoc = 0;
873 int attrs = oatDataFlowAttributes[opcode];
874 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
875 if (attrs & DF_UA) {
876 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700877 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700878 nextSreg+= 2;
879 } else {
880 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
881 nextSreg++;
882 }
883 }
884 if (attrs & DF_UB) {
885 if (attrs & DF_B_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700886 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700887 nextSreg+= 2;
888 } else {
889 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
890 nextSreg++;
891 }
892 }
893 if (attrs & DF_UC) {
894 if (attrs & DF_C_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700895 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700896 } else {
897 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
898 }
899 }
900 if (attrs & DF_DA) {
901 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700902 rlDest = oatGetDestWide(cUnit, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700903 } else {
buzbee15bf9802012-06-12 17:49:27 -0700904 rlDest = oatGetDest(cUnit, mir);
buzbeeb03f4872012-06-11 15:22:11 -0700905 if (rlDest.ref) {
906 objectDefinition = true;
907 }
buzbee2cfc6392012-05-07 14:51:40 -0700908 }
909 }
910
911 switch (opcode) {
912 case Instruction::NOP:
913 break;
914
915 case Instruction::MOVE:
916 case Instruction::MOVE_OBJECT:
917 case Instruction::MOVE_16:
918 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700919 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700920 case Instruction::MOVE_FROM16:
921 case Instruction::MOVE_WIDE:
922 case Instruction::MOVE_WIDE_16:
923 case Instruction::MOVE_WIDE_FROM16: {
924 /*
925 * Moves/copies are meaningless in pure SSA register form,
926 * but we need to preserve them for the conversion back into
927 * MIR (at least until we stop using the Dalvik register maps).
928 * Insert a dummy intrinsic copy call, which will be recognized
929 * by the quick path and removed by the portable path.
930 */
931 llvm::Value* src = getLLVMValue(cUnit, rlSrc[0].origSReg);
932 llvm::Value* res = emitCopy(cUnit, src, rlDest);
933 defineValue(cUnit, res, rlDest.origSReg);
934 }
935 break;
936
937 case Instruction::CONST:
938 case Instruction::CONST_4:
939 case Instruction::CONST_16: {
TDYa127347166a2012-08-23 12:23:44 -0700940 if (vB == 0) {
941 objectDefinition = true;
942 }
buzbee6969d502012-06-15 16:40:31 -0700943 llvm::Constant* immValue = cUnit->irb->GetJInt(vB);
buzbee2cfc6392012-05-07 14:51:40 -0700944 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
945 defineValue(cUnit, res, rlDest.origSReg);
946 }
947 break;
948
949 case Instruction::CONST_WIDE_16:
950 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700951 // Sign extend to 64 bits
952 int64_t imm = static_cast<int32_t>(vB);
953 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
buzbee2cfc6392012-05-07 14:51:40 -0700954 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
955 defineValue(cUnit, res, rlDest.origSReg);
956 }
957 break;
958
959 case Instruction::CONST_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700960 llvm::Constant* immValue = cUnit->irb->GetJInt(vB << 16);
buzbee2cfc6392012-05-07 14:51:40 -0700961 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
962 defineValue(cUnit, res, rlDest.origSReg);
963 }
964 break;
965
966 case Instruction::CONST_WIDE: {
967 llvm::Constant* immValue =
968 cUnit->irb->GetJLong(mir->dalvikInsn.vB_wide);
969 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
970 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700971 }
972 break;
buzbee2cfc6392012-05-07 14:51:40 -0700973 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700974 int64_t imm = static_cast<int64_t>(vB) << 48;
buzbee2cfc6392012-05-07 14:51:40 -0700975 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
976 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
977 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700978 }
979 break;
980
buzbee8fa0fda2012-06-27 15:44:52 -0700981 case Instruction::SPUT_OBJECT:
buzbee76592632012-06-29 15:18:35 -0700982 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputObject,
buzbee8fa0fda2012-06-27 15:44:52 -0700983 rlSrc[0]);
984 break;
985 case Instruction::SPUT:
986 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -0700987 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputFloat,
buzbee8fa0fda2012-06-27 15:44:52 -0700988 rlSrc[0]);
989 } else {
buzbee76592632012-06-29 15:18:35 -0700990 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSput, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700991 }
992 break;
993 case Instruction::SPUT_BOOLEAN:
buzbee76592632012-06-29 15:18:35 -0700994 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputBoolean,
buzbee8fa0fda2012-06-27 15:44:52 -0700995 rlSrc[0]);
996 break;
997 case Instruction::SPUT_BYTE:
buzbee76592632012-06-29 15:18:35 -0700998 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputByte, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700999 break;
1000 case Instruction::SPUT_CHAR:
buzbee76592632012-06-29 15:18:35 -07001001 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputChar, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001002 break;
1003 case Instruction::SPUT_SHORT:
buzbee76592632012-06-29 15:18:35 -07001004 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputShort, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001005 break;
1006 case Instruction::SPUT_WIDE:
1007 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -07001008 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputDouble,
buzbee8fa0fda2012-06-27 15:44:52 -07001009 rlSrc[0]);
1010 } else {
buzbee76592632012-06-29 15:18:35 -07001011 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputWide,
buzbee8fa0fda2012-06-27 15:44:52 -07001012 rlSrc[0]);
1013 }
1014 break;
1015
1016 case Instruction::SGET_OBJECT:
1017 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetObject, rlDest);
1018 break;
1019 case Instruction::SGET:
1020 if (rlDest.fp) {
1021 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetFloat, rlDest);
1022 } else {
1023 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSget, rlDest);
1024 }
1025 break;
1026 case Instruction::SGET_BOOLEAN:
1027 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetBoolean, rlDest);
1028 break;
1029 case Instruction::SGET_BYTE:
1030 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetByte, rlDest);
1031 break;
1032 case Instruction::SGET_CHAR:
1033 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetChar, rlDest);
1034 break;
1035 case Instruction::SGET_SHORT:
1036 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetShort, rlDest);
1037 break;
1038 case Instruction::SGET_WIDE:
1039 if (rlDest.fp) {
1040 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetDouble,
1041 rlDest);
1042 } else {
1043 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetWide, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001044 }
1045 break;
buzbee2cfc6392012-05-07 14:51:40 -07001046
1047 case Instruction::RETURN_WIDE:
1048 case Instruction::RETURN:
1049 case Instruction::RETURN_OBJECT: {
TDYa1274f2935e2012-06-22 06:25:03 -07001050 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001051 emitSuspendCheck(cUnit);
1052 }
buzbeeb03f4872012-06-11 15:22:11 -07001053 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001054 cUnit->irb->CreateRet(getLLVMValue(cUnit, rlSrc[0].origSReg));
1055 bb->hasReturn = true;
1056 }
1057 break;
1058
1059 case Instruction::RETURN_VOID: {
TDYa1274f2935e2012-06-22 06:25:03 -07001060 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001061 emitSuspendCheck(cUnit);
1062 }
buzbeeb03f4872012-06-11 15:22:11 -07001063 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001064 cUnit->irb->CreateRetVoid();
1065 bb->hasReturn = true;
1066 }
1067 break;
1068
1069 case Instruction::IF_EQ:
1070 convertCompareAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0], rlSrc[1]);
1071 break;
1072 case Instruction::IF_NE:
1073 convertCompareAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0], rlSrc[1]);
1074 break;
1075 case Instruction::IF_LT:
1076 convertCompareAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0], rlSrc[1]);
1077 break;
1078 case Instruction::IF_GE:
1079 convertCompareAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0], rlSrc[1]);
1080 break;
1081 case Instruction::IF_GT:
1082 convertCompareAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0], rlSrc[1]);
1083 break;
1084 case Instruction::IF_LE:
1085 convertCompareAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0], rlSrc[1]);
1086 break;
1087 case Instruction::IF_EQZ:
1088 convertCompareZeroAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0]);
1089 break;
1090 case Instruction::IF_NEZ:
1091 convertCompareZeroAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0]);
1092 break;
1093 case Instruction::IF_LTZ:
1094 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0]);
1095 break;
1096 case Instruction::IF_GEZ:
1097 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0]);
1098 break;
1099 case Instruction::IF_GTZ:
1100 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0]);
1101 break;
1102 case Instruction::IF_LEZ:
1103 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0]);
1104 break;
1105
1106 case Instruction::GOTO:
1107 case Instruction::GOTO_16:
1108 case Instruction::GOTO_32: {
1109 if (bb->taken->startOffset <= bb->startOffset) {
1110 emitSuspendCheck(cUnit);
1111 }
1112 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->taken->id));
1113 }
1114 break;
1115
1116 case Instruction::ADD_LONG:
1117 case Instruction::ADD_LONG_2ADDR:
1118 case Instruction::ADD_INT:
1119 case Instruction::ADD_INT_2ADDR:
1120 convertArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1121 break;
1122 case Instruction::SUB_LONG:
1123 case Instruction::SUB_LONG_2ADDR:
1124 case Instruction::SUB_INT:
1125 case Instruction::SUB_INT_2ADDR:
1126 convertArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1127 break;
1128 case Instruction::MUL_LONG:
1129 case Instruction::MUL_LONG_2ADDR:
1130 case Instruction::MUL_INT:
1131 case Instruction::MUL_INT_2ADDR:
1132 convertArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1133 break;
1134 case Instruction::DIV_LONG:
1135 case Instruction::DIV_LONG_2ADDR:
1136 case Instruction::DIV_INT:
1137 case Instruction::DIV_INT_2ADDR:
1138 convertArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1139 break;
1140 case Instruction::REM_LONG:
1141 case Instruction::REM_LONG_2ADDR:
1142 case Instruction::REM_INT:
1143 case Instruction::REM_INT_2ADDR:
1144 convertArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1145 break;
1146 case Instruction::AND_LONG:
1147 case Instruction::AND_LONG_2ADDR:
1148 case Instruction::AND_INT:
1149 case Instruction::AND_INT_2ADDR:
1150 convertArithOp(cUnit, kOpAnd, rlDest, rlSrc[0], rlSrc[1]);
1151 break;
1152 case Instruction::OR_LONG:
1153 case Instruction::OR_LONG_2ADDR:
1154 case Instruction::OR_INT:
1155 case Instruction::OR_INT_2ADDR:
1156 convertArithOp(cUnit, kOpOr, rlDest, rlSrc[0], rlSrc[1]);
1157 break;
1158 case Instruction::XOR_LONG:
1159 case Instruction::XOR_LONG_2ADDR:
1160 case Instruction::XOR_INT:
1161 case Instruction::XOR_INT_2ADDR:
1162 convertArithOp(cUnit, kOpXor, rlDest, rlSrc[0], rlSrc[1]);
1163 break;
1164 case Instruction::SHL_LONG:
1165 case Instruction::SHL_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001166 convertShift(cUnit, greenland::IntrinsicHelper::SHLLong,
1167 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001168 break;
buzbee2cfc6392012-05-07 14:51:40 -07001169 case Instruction::SHL_INT:
1170 case Instruction::SHL_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001171 convertShift(cUnit, greenland::IntrinsicHelper::SHLInt,
1172 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001173 break;
1174 case Instruction::SHR_LONG:
1175 case Instruction::SHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001176 convertShift(cUnit, greenland::IntrinsicHelper::SHRLong,
1177 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001178 break;
buzbee2cfc6392012-05-07 14:51:40 -07001179 case Instruction::SHR_INT:
1180 case Instruction::SHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001181 convertShift(cUnit, greenland::IntrinsicHelper::SHRInt,
1182 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001183 break;
1184 case Instruction::USHR_LONG:
1185 case Instruction::USHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001186 convertShift(cUnit, greenland::IntrinsicHelper::USHRLong,
1187 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001188 break;
buzbee2cfc6392012-05-07 14:51:40 -07001189 case Instruction::USHR_INT:
1190 case Instruction::USHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001191 convertShift(cUnit, greenland::IntrinsicHelper::USHRInt,
1192 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001193 break;
1194
1195 case Instruction::ADD_INT_LIT16:
1196 case Instruction::ADD_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001197 convertArithOpLit(cUnit, kOpAdd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001198 break;
1199 case Instruction::RSUB_INT:
1200 case Instruction::RSUB_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001201 convertArithOpLit(cUnit, kOpRsub, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001202 break;
1203 case Instruction::MUL_INT_LIT16:
1204 case Instruction::MUL_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001205 convertArithOpLit(cUnit, kOpMul, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001206 break;
1207 case Instruction::DIV_INT_LIT16:
1208 case Instruction::DIV_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001209 convertArithOpLit(cUnit, kOpDiv, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001210 break;
1211 case Instruction::REM_INT_LIT16:
1212 case Instruction::REM_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001213 convertArithOpLit(cUnit, kOpRem, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001214 break;
1215 case Instruction::AND_INT_LIT16:
1216 case Instruction::AND_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001217 convertArithOpLit(cUnit, kOpAnd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001218 break;
1219 case Instruction::OR_INT_LIT16:
1220 case Instruction::OR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001221 convertArithOpLit(cUnit, kOpOr, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001222 break;
1223 case Instruction::XOR_INT_LIT16:
1224 case Instruction::XOR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001225 convertArithOpLit(cUnit, kOpXor, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001226 break;
1227 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001228 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHLInt,
1229 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001230 break;
1231 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001232 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHRInt,
1233 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001234 break;
1235 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001236 convertShiftLit(cUnit, greenland::IntrinsicHelper::USHRInt,
1237 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001238 break;
1239
1240 case Instruction::ADD_FLOAT:
1241 case Instruction::ADD_FLOAT_2ADDR:
1242 case Instruction::ADD_DOUBLE:
1243 case Instruction::ADD_DOUBLE_2ADDR:
1244 convertFPArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1245 break;
1246
1247 case Instruction::SUB_FLOAT:
1248 case Instruction::SUB_FLOAT_2ADDR:
1249 case Instruction::SUB_DOUBLE:
1250 case Instruction::SUB_DOUBLE_2ADDR:
1251 convertFPArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1252 break;
1253
1254 case Instruction::MUL_FLOAT:
1255 case Instruction::MUL_FLOAT_2ADDR:
1256 case Instruction::MUL_DOUBLE:
1257 case Instruction::MUL_DOUBLE_2ADDR:
1258 convertFPArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1259 break;
1260
1261 case Instruction::DIV_FLOAT:
1262 case Instruction::DIV_FLOAT_2ADDR:
1263 case Instruction::DIV_DOUBLE:
1264 case Instruction::DIV_DOUBLE_2ADDR:
1265 convertFPArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1266 break;
1267
1268 case Instruction::REM_FLOAT:
1269 case Instruction::REM_FLOAT_2ADDR:
1270 case Instruction::REM_DOUBLE:
1271 case Instruction::REM_DOUBLE_2ADDR:
1272 convertFPArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1273 break;
1274
buzbee6969d502012-06-15 16:40:31 -07001275 case Instruction::INVOKE_STATIC:
buzbee101305f2012-06-28 18:00:56 -07001276 convertInvoke(cUnit, bb, mir, kStatic, false /*range*/,
1277 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001278 break;
1279 case Instruction::INVOKE_STATIC_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001280 convertInvoke(cUnit, bb, mir, kStatic, true /*range*/,
1281 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001282 break;
1283
1284 case Instruction::INVOKE_DIRECT:
buzbee101305f2012-06-28 18:00:56 -07001285 convertInvoke(cUnit, bb, mir, kDirect, false /*range*/,
1286 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001287 break;
1288 case Instruction::INVOKE_DIRECT_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001289 convertInvoke(cUnit, bb, mir, kDirect, true /*range*/,
1290 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001291 break;
1292
1293 case Instruction::INVOKE_VIRTUAL:
buzbee101305f2012-06-28 18:00:56 -07001294 convertInvoke(cUnit, bb, mir, kVirtual, false /*range*/,
1295 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001296 break;
1297 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001298 convertInvoke(cUnit, bb, mir, kVirtual, true /*range*/,
1299 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001300 break;
1301
1302 case Instruction::INVOKE_SUPER:
buzbee101305f2012-06-28 18:00:56 -07001303 convertInvoke(cUnit, bb, mir, kSuper, false /*range*/,
1304 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001305 break;
1306 case Instruction::INVOKE_SUPER_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001307 convertInvoke(cUnit, bb, mir, kSuper, true /*range*/,
1308 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001309 break;
1310
1311 case Instruction::INVOKE_INTERFACE:
buzbee101305f2012-06-28 18:00:56 -07001312 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1313 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001314 break;
1315 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001316 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1317 false /* NewFilledArray */);
1318 break;
1319 case Instruction::FILLED_NEW_ARRAY:
1320 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1321 true /* NewFilledArray */);
1322 break;
1323 case Instruction::FILLED_NEW_ARRAY_RANGE:
1324 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1325 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001326 break;
1327
1328 case Instruction::CONST_STRING:
1329 case Instruction::CONST_STRING_JUMBO:
buzbee101305f2012-06-28 18:00:56 -07001330 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstString,
1331 rlDest);
1332 break;
1333
1334 case Instruction::CONST_CLASS:
1335 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstClass,
1336 rlDest);
1337 break;
1338
1339 case Instruction::CHECK_CAST:
1340 convertCheckCast(cUnit, vB, rlSrc[0]);
buzbee6969d502012-06-15 16:40:31 -07001341 break;
1342
buzbee4f1181f2012-06-22 13:52:12 -07001343 case Instruction::NEW_INSTANCE:
buzbee8fa0fda2012-06-27 15:44:52 -07001344 convertNewInstance(cUnit, vB, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001345 break;
1346
buzbee32412962012-06-26 16:27:56 -07001347 case Instruction::MOVE_EXCEPTION:
1348 convertMoveException(cUnit, rlDest);
1349 break;
1350
1351 case Instruction::THROW:
1352 convertThrow(cUnit, rlSrc[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001353 /*
1354 * If this throw is standalone, terminate.
1355 * If it might rethrow, force termination
1356 * of the following block.
1357 */
1358 if (bb->fallThrough == NULL) {
1359 cUnit->irb->CreateUnreachable();
1360 } else {
1361 bb->fallThrough->fallThrough = NULL;
1362 bb->fallThrough->taken = NULL;
1363 }
buzbee32412962012-06-26 16:27:56 -07001364 break;
1365
buzbee2cfc6392012-05-07 14:51:40 -07001366 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001367 case Instruction::MOVE_RESULT:
1368 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001369 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001370 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001371 */
jeffhao9a4f0032012-08-30 16:17:40 -07001372 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001373 break;
1374
1375 case Instruction::MONITOR_ENTER:
buzbee8fa0fda2012-06-27 15:44:52 -07001376 convertMonitorEnterExit(cUnit, optFlags,
1377 greenland::IntrinsicHelper::MonitorEnter,
1378 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001379 break;
1380
1381 case Instruction::MONITOR_EXIT:
buzbee8fa0fda2012-06-27 15:44:52 -07001382 convertMonitorEnterExit(cUnit, optFlags,
1383 greenland::IntrinsicHelper::MonitorExit,
1384 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001385 break;
1386
1387 case Instruction::ARRAY_LENGTH:
buzbee76592632012-06-29 15:18:35 -07001388 convertArrayLength(cUnit, optFlags, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001389 break;
1390
1391 case Instruction::NEW_ARRAY:
1392 convertNewArray(cUnit, vC, rlDest, rlSrc[0]);
1393 break;
1394
1395 case Instruction::INSTANCE_OF:
1396 convertInstanceOf(cUnit, vC, rlDest, rlSrc[0]);
1397 break;
1398
1399 case Instruction::AGET:
1400 if (rlDest.fp) {
1401 convertAget(cUnit, optFlags,
1402 greenland::IntrinsicHelper::HLArrayGetFloat,
1403 rlDest, rlSrc[0], rlSrc[1]);
1404 } else {
1405 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGet,
1406 rlDest, rlSrc[0], rlSrc[1]);
1407 }
1408 break;
1409 case Instruction::AGET_OBJECT:
1410 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetObject,
1411 rlDest, rlSrc[0], rlSrc[1]);
1412 break;
1413 case Instruction::AGET_BOOLEAN:
1414 convertAget(cUnit, optFlags,
1415 greenland::IntrinsicHelper::HLArrayGetBoolean,
1416 rlDest, rlSrc[0], rlSrc[1]);
1417 break;
1418 case Instruction::AGET_BYTE:
1419 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetByte,
1420 rlDest, rlSrc[0], rlSrc[1]);
1421 break;
1422 case Instruction::AGET_CHAR:
1423 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetChar,
1424 rlDest, rlSrc[0], rlSrc[1]);
1425 break;
1426 case Instruction::AGET_SHORT:
1427 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetShort,
1428 rlDest, rlSrc[0], rlSrc[1]);
1429 break;
1430 case Instruction::AGET_WIDE:
1431 if (rlDest.fp) {
1432 convertAget(cUnit, optFlags,
1433 greenland::IntrinsicHelper::HLArrayGetDouble,
1434 rlDest, rlSrc[0], rlSrc[1]);
1435 } else {
1436 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetWide,
1437 rlDest, rlSrc[0], rlSrc[1]);
1438 }
1439 break;
1440
1441 case Instruction::APUT:
1442 if (rlSrc[0].fp) {
1443 convertAput(cUnit, optFlags,
1444 greenland::IntrinsicHelper::HLArrayPutFloat,
1445 rlSrc[0], rlSrc[1], rlSrc[2]);
1446 } else {
1447 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPut,
1448 rlSrc[0], rlSrc[1], rlSrc[2]);
1449 }
1450 break;
1451 case Instruction::APUT_OBJECT:
1452 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutObject,
1453 rlSrc[0], rlSrc[1], rlSrc[2]);
1454 break;
1455 case Instruction::APUT_BOOLEAN:
1456 convertAput(cUnit, optFlags,
1457 greenland::IntrinsicHelper::HLArrayPutBoolean,
1458 rlSrc[0], rlSrc[1], rlSrc[2]);
1459 break;
1460 case Instruction::APUT_BYTE:
1461 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutByte,
1462 rlSrc[0], rlSrc[1], rlSrc[2]);
1463 break;
1464 case Instruction::APUT_CHAR:
1465 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutChar,
1466 rlSrc[0], rlSrc[1], rlSrc[2]);
1467 break;
1468 case Instruction::APUT_SHORT:
1469 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutShort,
1470 rlSrc[0], rlSrc[1], rlSrc[2]);
1471 break;
1472 case Instruction::APUT_WIDE:
1473 if (rlSrc[0].fp) {
1474 convertAput(cUnit, optFlags,
1475 greenland::IntrinsicHelper::HLArrayPutDouble,
1476 rlSrc[0], rlSrc[1], rlSrc[2]);
1477 } else {
1478 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutWide,
1479 rlSrc[0], rlSrc[1], rlSrc[2]);
1480 }
1481 break;
1482
buzbee101305f2012-06-28 18:00:56 -07001483 case Instruction::IGET:
1484 if (rlDest.fp) {
1485 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetFloat,
buzbee4f4dfc72012-07-02 14:54:44 -07001486 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001487 } else {
1488 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGet,
buzbee4f4dfc72012-07-02 14:54:44 -07001489 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001490 }
buzbee2cfc6392012-05-07 14:51:40 -07001491 break;
buzbee101305f2012-06-28 18:00:56 -07001492 case Instruction::IGET_OBJECT:
1493 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetObject,
buzbee4f4dfc72012-07-02 14:54:44 -07001494 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001495 break;
1496 case Instruction::IGET_BOOLEAN:
1497 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetBoolean,
buzbee4f4dfc72012-07-02 14:54:44 -07001498 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001499 break;
1500 case Instruction::IGET_BYTE:
1501 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetByte,
buzbee4f4dfc72012-07-02 14:54:44 -07001502 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001503 break;
1504 case Instruction::IGET_CHAR:
1505 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetChar,
buzbee4f4dfc72012-07-02 14:54:44 -07001506 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001507 break;
1508 case Instruction::IGET_SHORT:
1509 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetShort,
buzbee4f4dfc72012-07-02 14:54:44 -07001510 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001511 break;
1512 case Instruction::IGET_WIDE:
1513 if (rlDest.fp) {
1514 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetDouble,
buzbee4f4dfc72012-07-02 14:54:44 -07001515 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001516 } else {
1517 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetWide,
buzbee4f4dfc72012-07-02 14:54:44 -07001518 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001519 }
1520 break;
1521 case Instruction::IPUT:
buzbee85eee022012-07-16 22:12:38 -07001522 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001523 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutFloat,
1524 rlSrc[0], rlSrc[1], vC);
1525 } else {
1526 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPut,
1527 rlSrc[0], rlSrc[1], vC);
1528 }
1529 break;
1530 case Instruction::IPUT_OBJECT:
1531 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutObject,
1532 rlSrc[0], rlSrc[1], vC);
1533 break;
1534 case Instruction::IPUT_BOOLEAN:
1535 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutBoolean,
1536 rlSrc[0], rlSrc[1], vC);
1537 break;
1538 case Instruction::IPUT_BYTE:
1539 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutByte,
1540 rlSrc[0], rlSrc[1], vC);
1541 break;
1542 case Instruction::IPUT_CHAR:
1543 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutChar,
1544 rlSrc[0], rlSrc[1], vC);
1545 break;
1546 case Instruction::IPUT_SHORT:
1547 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutShort,
1548 rlSrc[0], rlSrc[1], vC);
1549 break;
1550 case Instruction::IPUT_WIDE:
buzbee85eee022012-07-16 22:12:38 -07001551 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001552 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutDouble,
1553 rlSrc[0], rlSrc[1], vC);
1554 } else {
1555 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutWide,
1556 rlSrc[0], rlSrc[1], vC);
1557 }
buzbee2cfc6392012-05-07 14:51:40 -07001558 break;
1559
1560 case Instruction::FILL_ARRAY_DATA:
buzbee101305f2012-06-28 18:00:56 -07001561 convertFillArrayData(cUnit, vB, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001562 break;
1563
buzbee76592632012-06-29 15:18:35 -07001564 case Instruction::LONG_TO_INT:
1565 convertLongToInt(cUnit, rlDest, rlSrc[0]);
1566 break;
1567
buzbee101305f2012-06-28 18:00:56 -07001568 case Instruction::INT_TO_LONG:
1569 convertIntToLong(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001570 break;
1571
buzbee101305f2012-06-28 18:00:56 -07001572 case Instruction::INT_TO_CHAR:
1573 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1574 greenland::IntrinsicHelper::IntToChar);
1575 break;
1576 case Instruction::INT_TO_BYTE:
1577 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1578 greenland::IntrinsicHelper::IntToByte);
1579 break;
1580 case Instruction::INT_TO_SHORT:
1581 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1582 greenland::IntrinsicHelper::IntToShort);
1583 break;
1584
buzbee76592632012-06-29 15:18:35 -07001585 case Instruction::INT_TO_FLOAT:
1586 case Instruction::LONG_TO_FLOAT:
1587 convertIntToFP(cUnit, cUnit->irb->getFloatTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001588 break;
1589
buzbee76592632012-06-29 15:18:35 -07001590 case Instruction::INT_TO_DOUBLE:
1591 case Instruction::LONG_TO_DOUBLE:
1592 convertIntToFP(cUnit, cUnit->irb->getDoubleTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001593 break;
1594
buzbee76592632012-06-29 15:18:35 -07001595 case Instruction::FLOAT_TO_DOUBLE:
1596 convertFloatToDouble(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001597 break;
1598
buzbee76592632012-06-29 15:18:35 -07001599 case Instruction::DOUBLE_TO_FLOAT:
1600 convertDoubleToFloat(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001601 break;
1602
1603 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001604 case Instruction::NEG_INT:
1605 convertNeg(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001606 break;
1607
1608 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001609 case Instruction::NEG_DOUBLE:
buzbee76592632012-06-29 15:18:35 -07001610 convertNegFP(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001611 break;
1612
buzbee76592632012-06-29 15:18:35 -07001613 case Instruction::NOT_LONG:
1614 case Instruction::NOT_INT:
1615 convertNot(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001616 break;
1617
buzbee2cfc6392012-05-07 14:51:40 -07001618 case Instruction::FLOAT_TO_INT:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001619 convertFPToInt(cUnit, greenland::IntrinsicHelper::F2I, rlDest, rlSrc[0]);
1620 break;
1621
buzbee2cfc6392012-05-07 14:51:40 -07001622 case Instruction::DOUBLE_TO_INT:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001623 convertFPToInt(cUnit, greenland::IntrinsicHelper::D2I, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001624 break;
1625
buzbee76592632012-06-29 15:18:35 -07001626 case Instruction::FLOAT_TO_LONG:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001627 convertFPToInt(cUnit, greenland::IntrinsicHelper::F2L, rlDest, rlSrc[0]);
1628 break;
1629
buzbee76592632012-06-29 15:18:35 -07001630 case Instruction::DOUBLE_TO_LONG:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001631 convertFPToInt(cUnit, greenland::IntrinsicHelper::D2L, rlDest, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001632 break;
1633
1634 case Instruction::CMPL_FLOAT:
1635 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplFloat,
1636 rlDest, rlSrc[0], rlSrc[1]);
1637 break;
1638 case Instruction::CMPG_FLOAT:
1639 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgFloat,
1640 rlDest, rlSrc[0], rlSrc[1]);
1641 break;
1642 case Instruction::CMPL_DOUBLE:
1643 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplDouble,
1644 rlDest, rlSrc[0], rlSrc[1]);
1645 break;
1646 case Instruction::CMPG_DOUBLE:
1647 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgDouble,
1648 rlDest, rlSrc[0], rlSrc[1]);
1649 break;
1650 case Instruction::CMP_LONG:
1651 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpLong,
1652 rlDest, rlSrc[0], rlSrc[1]);
1653 break;
1654
buzbee76592632012-06-29 15:18:35 -07001655 case Instruction::PACKED_SWITCH:
buzbeef58c12c2012-07-03 15:06:29 -07001656 convertPackedSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001657 break;
1658
1659 case Instruction::SPARSE_SWITCH:
buzbeea1da8a52012-07-09 14:00:21 -07001660 convertSparseSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001661 break;
buzbee2cfc6392012-05-07 14:51:40 -07001662
1663 default:
buzbee32412962012-06-26 16:27:56 -07001664 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001665 res = true;
1666 }
buzbeeb03f4872012-06-11 15:22:11 -07001667 if (objectDefinition) {
1668 setShadowFrameEntry(cUnit, (llvm::Value*)
1669 cUnit->llvmValues.elemList[rlDest.origSReg]);
1670 }
buzbee2cfc6392012-05-07 14:51:40 -07001671 return res;
1672}
1673
1674/* Extended MIR instructions like PHI */
1675void convertExtendedMIR(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
1676 llvm::BasicBlock* llvmBB)
1677{
1678
1679 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1680 case kMirOpPhi: {
buzbee2cfc6392012-05-07 14:51:40 -07001681 RegLocation rlDest = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee2a83e8f2012-07-13 16:42:30 -07001682 /*
1683 * The Art compiler's Phi nodes only handle 32-bit operands,
1684 * representing wide values using a matched set of Phi nodes
1685 * for the lower and upper halves. In the llvm world, we only
1686 * want a single Phi for wides. Here we will simply discard
1687 * the Phi node representing the high word.
1688 */
1689 if (rlDest.highWord) {
1690 return; // No Phi node - handled via low word
1691 }
1692 int* incoming = (int*)mir->dalvikInsn.vB;
buzbee2cfc6392012-05-07 14:51:40 -07001693 llvm::Type* phiType =
1694 llvmTypeFromLocRec(cUnit, rlDest);
1695 llvm::PHINode* phi = cUnit->irb->CreatePHI(phiType, mir->ssaRep->numUses);
1696 for (int i = 0; i < mir->ssaRep->numUses; i++) {
1697 RegLocation loc;
buzbee2a83e8f2012-07-13 16:42:30 -07001698 // Don't check width here.
1699 loc = oatGetRawSrc(cUnit, mir, i);
1700 DCHECK_EQ(rlDest.wide, loc.wide);
1701 DCHECK_EQ(rlDest.wide & rlDest.highWord, loc.wide & loc.highWord);
1702 DCHECK_EQ(rlDest.fp, loc.fp);
1703 DCHECK_EQ(rlDest.core, loc.core);
1704 DCHECK_EQ(rlDest.ref, loc.ref);
buzbeed1643e42012-09-05 14:06:51 -07001705 SafeMap<unsigned int, unsigned int>::iterator it;
1706 it = cUnit->blockIdMap.find(incoming[i]);
1707 DCHECK(it != cUnit->blockIdMap.end());
buzbee2cfc6392012-05-07 14:51:40 -07001708 phi->addIncoming(getLLVMValue(cUnit, loc.origSReg),
buzbeed1643e42012-09-05 14:06:51 -07001709 getLLVMBlock(cUnit, it->second));
buzbee2cfc6392012-05-07 14:51:40 -07001710 }
1711 defineValue(cUnit, phi, rlDest.origSReg);
1712 break;
1713 }
1714 case kMirOpCopy: {
1715 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1716 break;
1717 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001718 case kMirOpNop:
1719 if ((mir == bb->lastMIRInsn) && (bb->taken == NULL) &&
1720 (bb->fallThrough == NULL)) {
1721 cUnit->irb->CreateUnreachable();
1722 }
1723 break;
1724
buzbee2cfc6392012-05-07 14:51:40 -07001725#if defined(TARGET_ARM)
1726 case kMirOpFusedCmplFloat:
1727 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmpFloat";
1728 break;
1729 case kMirOpFusedCmpgFloat:
1730 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmgFloat";
1731 break;
1732 case kMirOpFusedCmplDouble:
1733 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmplDouble";
1734 break;
1735 case kMirOpFusedCmpgDouble:
1736 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmpgDouble";
1737 break;
1738 case kMirOpFusedCmpLong:
1739 UNIMPLEMENTED(WARNING) << "unimp kMirOpLongCmpBranch";
1740 break;
1741#endif
1742 default:
1743 break;
1744 }
1745}
1746
1747void setDexOffset(CompilationUnit* cUnit, int32_t offset)
1748{
1749 cUnit->currentDalvikOffset = offset;
buzbee76592632012-06-29 15:18:35 -07001750 llvm::SmallVector<llvm::Value*, 1> arrayRef;
buzbee2cfc6392012-05-07 14:51:40 -07001751 arrayRef.push_back(cUnit->irb->getInt32(offset));
1752 llvm::MDNode* node = llvm::MDNode::get(*cUnit->context, arrayRef);
1753 cUnit->irb->SetDexOffset(node);
1754}
1755
1756// Attach method info as metadata to special intrinsic
1757void setMethodInfo(CompilationUnit* cUnit)
1758{
1759 // We don't want dex offset on this
1760 cUnit->irb->SetDexOffset(NULL);
1761 greenland::IntrinsicHelper::IntrinsicId id;
1762 id = greenland::IntrinsicHelper::MethodInfo;
1763 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1764 llvm::Instruction* inst = cUnit->irb->CreateCall(intr);
1765 llvm::SmallVector<llvm::Value*, 2> regInfo;
1766 regInfo.push_back(cUnit->irb->getInt32(cUnit->numIns));
1767 regInfo.push_back(cUnit->irb->getInt32(cUnit->numRegs));
1768 regInfo.push_back(cUnit->irb->getInt32(cUnit->numOuts));
1769 regInfo.push_back(cUnit->irb->getInt32(cUnit->numCompilerTemps));
1770 regInfo.push_back(cUnit->irb->getInt32(cUnit->numSSARegs));
1771 llvm::MDNode* regInfoNode = llvm::MDNode::get(*cUnit->context, regInfo);
1772 inst->setMetadata("RegInfo", regInfoNode);
1773 int promoSize = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
1774 llvm::SmallVector<llvm::Value*, 50> pmap;
1775 for (int i = 0; i < promoSize; i++) {
1776 PromotionMap* p = &cUnit->promotionMap[i];
1777 int32_t mapData = ((p->firstInPair & 0xff) << 24) |
1778 ((p->fpReg & 0xff) << 16) |
1779 ((p->coreReg & 0xff) << 8) |
1780 ((p->fpLocation & 0xf) << 4) |
1781 (p->coreLocation & 0xf);
1782 pmap.push_back(cUnit->irb->getInt32(mapData));
1783 }
1784 llvm::MDNode* mapNode = llvm::MDNode::get(*cUnit->context, pmap);
1785 inst->setMetadata("PromotionMap", mapNode);
1786 setDexOffset(cUnit, cUnit->currentDalvikOffset);
1787}
1788
1789/* Handle the content in each basic block */
1790bool methodBlockBitcodeConversion(CompilationUnit* cUnit, BasicBlock* bb)
1791{
buzbeed1643e42012-09-05 14:06:51 -07001792 if (bb->blockType == kDead) return false;
buzbee2cfc6392012-05-07 14:51:40 -07001793 llvm::BasicBlock* llvmBB = getLLVMBlock(cUnit, bb->id);
buzbeef5f5a122012-09-21 13:57:36 -07001794 if (llvmBB == NULL) {
1795 CHECK(bb->blockType == kExitBlock);
1796 } else {
1797 cUnit->irb->SetInsertPoint(llvmBB);
1798 setDexOffset(cUnit, bb->startOffset);
1799 }
buzbee2cfc6392012-05-07 14:51:40 -07001800
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001801 if (cUnit->printMe) {
1802 LOG(INFO) << "................................";
1803 LOG(INFO) << "Block id " << bb->id;
1804 if (llvmBB != NULL) {
1805 LOG(INFO) << "label " << llvmBB->getName().str().c_str();
1806 } else {
1807 LOG(INFO) << "llvmBB is NULL";
1808 }
1809 }
1810
buzbee2cfc6392012-05-07 14:51:40 -07001811 if (bb->blockType == kEntryBlock) {
1812 setMethodInfo(cUnit);
buzbeeb03f4872012-06-11 15:22:11 -07001813 bool *canBeRef = (bool*) oatNew(cUnit, sizeof(bool) *
1814 cUnit->numDalvikRegisters, true,
1815 kAllocMisc);
1816 for (int i = 0; i < cUnit->numSSARegs; i++) {
buzbee6ec5e232012-09-20 15:50:03 -07001817 int vReg = SRegToVReg(cUnit, i);
1818 if (vReg > SSA_METHOD_BASEREG) {
1819 canBeRef[SRegToVReg(cUnit, i)] |= cUnit->regLocation[i].ref;
1820 }
buzbeeb03f4872012-06-11 15:22:11 -07001821 }
1822 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
1823 if (canBeRef[i]) {
1824 cUnit->numShadowFrameEntries++;
1825 }
1826 }
1827 if (cUnit->numShadowFrameEntries > 0) {
1828 cUnit->shadowMap = (int*) oatNew(cUnit, sizeof(int) *
1829 cUnit->numShadowFrameEntries, true,
1830 kAllocMisc);
1831 for (int i = 0, j = 0; i < cUnit->numDalvikRegisters; i++) {
1832 if (canBeRef[i]) {
1833 cUnit->shadowMap[j++] = i;
1834 }
1835 }
buzbeeb03f4872012-06-11 15:22:11 -07001836 }
Shih-wei Liao569daf12012-08-10 23:22:33 -07001837 greenland::IntrinsicHelper::IntrinsicId id =
1838 greenland::IntrinsicHelper::AllocaShadowFrame;
1839 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1840 llvm::Value* entries = cUnit->irb->getInt32(cUnit->numShadowFrameEntries);
1841 cUnit->irb->CreateCall(func, entries);
buzbee2cfc6392012-05-07 14:51:40 -07001842 } else if (bb->blockType == kExitBlock) {
1843 /*
1844 * Because of the differences between how MIR/LIR and llvm handle exit
1845 * blocks, we won't explicitly covert them. On the llvm-to-lir
1846 * path, it will need to be regenereated.
1847 */
1848 return false;
buzbee6969d502012-06-15 16:40:31 -07001849 } else if (bb->blockType == kExceptionHandling) {
1850 /*
1851 * Because we're deferring null checking, delete the associated empty
1852 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001853 */
1854 llvmBB->eraseFromParent();
1855 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001856 }
1857
1858 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
1859
1860 setDexOffset(cUnit, mir->offset);
1861
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001862 int opcode = mir->dalvikInsn.opcode;
1863 Instruction::Format dalvikFormat =
1864 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001865
1866 /* If we're compiling for the debugger, generate an update callout */
1867 if (cUnit->genDebugger) {
1868 UNIMPLEMENTED(FATAL) << "Need debug codegen";
1869 //genDebuggerUpdate(cUnit, mir->offset);
1870 }
1871
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001872 if (opcode == kMirOpCheck) {
1873 // Combine check and work halves of throwing instruction.
1874 MIR* workHalf = mir->meta.throwInsn;
1875 mir->dalvikInsn.opcode = workHalf->dalvikInsn.opcode;
1876 opcode = mir->dalvikInsn.opcode;
1877 SSARepresentation* ssaRep = workHalf->ssaRep;
1878 workHalf->ssaRep = mir->ssaRep;
1879 mir->ssaRep = ssaRep;
1880 workHalf->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1881 if (bb->successorBlockList.blockListType == kCatch) {
1882 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
1883 greenland::IntrinsicHelper::CatchTargets);
1884 llvm::Value* switchKey =
1885 cUnit->irb->CreateCall(intr, cUnit->irb->getInt32(mir->offset));
1886 GrowableListIterator iter;
1887 oatGrowableListIteratorInit(&bb->successorBlockList.blocks, &iter);
1888 // New basic block to use for work half
1889 llvm::BasicBlock* workBB =
1890 llvm::BasicBlock::Create(*cUnit->context, "", cUnit->func);
1891 llvm::SwitchInst* sw =
1892 cUnit->irb->CreateSwitch(switchKey, workBB,
1893 bb->successorBlockList.blocks.numUsed);
1894 while (true) {
1895 SuccessorBlockInfo *successorBlockInfo =
1896 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iter);
1897 if (successorBlockInfo == NULL) break;
1898 llvm::BasicBlock *target =
1899 getLLVMBlock(cUnit, successorBlockInfo->block->id);
1900 int typeIndex = successorBlockInfo->key;
1901 sw->addCase(cUnit->irb->getInt32(typeIndex), target);
1902 }
1903 llvmBB = workBB;
1904 cUnit->irb->SetInsertPoint(llvmBB);
1905 }
1906 }
1907
1908 if (opcode >= kMirOpFirst) {
buzbee2cfc6392012-05-07 14:51:40 -07001909 convertExtendedMIR(cUnit, bb, mir, llvmBB);
1910 continue;
1911 }
1912
1913 bool notHandled = convertMIRNode(cUnit, mir, bb, llvmBB,
1914 NULL /* labelList */);
1915 if (notHandled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001916 Instruction::Code dalvikOpcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001917 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001918 mir->offset, opcode,
buzbee2cfc6392012-05-07 14:51:40 -07001919 Instruction::Name(dalvikOpcode),
1920 dalvikFormat);
1921 }
1922 }
1923
buzbee4be777b2012-07-12 14:38:18 -07001924 if (bb->blockType == kEntryBlock) {
1925 cUnit->entryTargetBB = getLLVMBlock(cUnit, bb->fallThrough->id);
1926 } else if ((bb->fallThrough != NULL) && !bb->hasReturn) {
buzbee2cfc6392012-05-07 14:51:40 -07001927 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->fallThrough->id));
1928 }
1929
1930 return false;
1931}
1932
buzbee4f4dfc72012-07-02 14:54:44 -07001933char remapShorty(char shortyType) {
1934 /*
1935 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1936 * and longs/doubles are represented as a pair of registers. When sub-word
1937 * arguments (and method results) are passed, they are extended to Dalvik
1938 * virtual register containers. Because llvm is picky about type consistency,
1939 * we must either cast the "real" type to 32-bit container multiple Dalvik
1940 * register types, or always use the expanded values.
1941 * Here, we're doing the latter. We map the shorty signature to container
1942 * types (which is valid so long as we always do a real expansion of passed
1943 * arguments and field loads).
1944 */
1945 switch(shortyType) {
1946 case 'Z' : shortyType = 'I'; break;
1947 case 'B' : shortyType = 'I'; break;
1948 case 'S' : shortyType = 'I'; break;
1949 case 'C' : shortyType = 'I'; break;
1950 default: break;
1951 }
1952 return shortyType;
1953}
1954
buzbee2cfc6392012-05-07 14:51:40 -07001955llvm::FunctionType* getFunctionType(CompilationUnit* cUnit) {
1956
1957 // Get return type
buzbee4f4dfc72012-07-02 14:54:44 -07001958 llvm::Type* ret_type = cUnit->irb->GetJType(remapShorty(cUnit->shorty[0]),
buzbee2cfc6392012-05-07 14:51:40 -07001959 greenland::kAccurate);
1960
1961 // Get argument type
1962 std::vector<llvm::Type*> args_type;
1963
1964 // method object
1965 args_type.push_back(cUnit->irb->GetJMethodTy());
1966
1967 // Do we have a "this"?
1968 if ((cUnit->access_flags & kAccStatic) == 0) {
1969 args_type.push_back(cUnit->irb->GetJObjectTy());
1970 }
1971
1972 for (uint32_t i = 1; i < strlen(cUnit->shorty); ++i) {
buzbee4f4dfc72012-07-02 14:54:44 -07001973 args_type.push_back(cUnit->irb->GetJType(remapShorty(cUnit->shorty[i]),
buzbee2cfc6392012-05-07 14:51:40 -07001974 greenland::kAccurate));
1975 }
1976
1977 return llvm::FunctionType::get(ret_type, args_type, false);
1978}
1979
1980bool createFunction(CompilationUnit* cUnit) {
1981 std::string func_name(PrettyMethod(cUnit->method_idx, *cUnit->dex_file,
1982 /* with_signature */ false));
1983 llvm::FunctionType* func_type = getFunctionType(cUnit);
1984
1985 if (func_type == NULL) {
1986 return false;
1987 }
1988
1989 cUnit->func = llvm::Function::Create(func_type,
1990 llvm::Function::ExternalLinkage,
1991 func_name, cUnit->module);
1992
1993 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
1994 llvm::Function::arg_iterator arg_end(cUnit->func->arg_end());
1995
1996 arg_iter->setName("method");
1997 ++arg_iter;
1998
1999 int startSReg = cUnit->numRegs;
2000
2001 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
2002 arg_iter->setName(StringPrintf("v%i_0", startSReg));
2003 startSReg += cUnit->regLocation[startSReg].wide ? 2 : 1;
2004 }
2005
2006 return true;
2007}
2008
2009bool createLLVMBasicBlock(CompilationUnit* cUnit, BasicBlock* bb)
2010{
2011 // Skip the exit block
buzbeed1643e42012-09-05 14:06:51 -07002012 if ((bb->blockType == kDead) ||(bb->blockType == kExitBlock)) {
buzbee2cfc6392012-05-07 14:51:40 -07002013 cUnit->idToBlockMap.Put(bb->id, NULL);
2014 } else {
2015 int offset = bb->startOffset;
2016 bool entryBlock = (bb->blockType == kEntryBlock);
2017 llvm::BasicBlock* llvmBB =
2018 llvm::BasicBlock::Create(*cUnit->context, entryBlock ? "entry" :
buzbee8320f382012-09-11 16:29:42 -07002019 StringPrintf(kLabelFormat, bb->catchEntry ? kCatchBlock :
2020 kNormalBlock, offset, bb->id), cUnit->func);
buzbee2cfc6392012-05-07 14:51:40 -07002021 if (entryBlock) {
2022 cUnit->entryBB = llvmBB;
2023 cUnit->placeholderBB =
2024 llvm::BasicBlock::Create(*cUnit->context, "placeholder",
2025 cUnit->func);
2026 }
2027 cUnit->idToBlockMap.Put(bb->id, llvmBB);
2028 }
2029 return false;
2030}
2031
2032
2033/*
2034 * Convert MIR to LLVM_IR
2035 * o For each ssa name, create LLVM named value. Type these
2036 * appropriately, and ignore high half of wide and double operands.
2037 * o For each MIR basic block, create an LLVM basic block.
2038 * o Iterate through the MIR a basic block at a time, setting arguments
2039 * to recovered ssa name.
2040 */
2041void oatMethodMIR2Bitcode(CompilationUnit* cUnit)
2042{
2043 initIR(cUnit);
2044 oatInitGrowableList(cUnit, &cUnit->llvmValues, cUnit->numSSARegs);
2045
2046 // Create the function
2047 createFunction(cUnit);
2048
2049 // Create an LLVM basic block for each MIR block in dfs preorder
2050 oatDataFlowAnalysisDispatcher(cUnit, createLLVMBasicBlock,
2051 kPreOrderDFSTraversal, false /* isIterative */);
2052 /*
2053 * Create an llvm named value for each MIR SSA name. Note: we'll use
2054 * placeholders for all non-argument values (because we haven't seen
2055 * the definition yet).
2056 */
2057 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2058 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
2059 arg_iter++; /* Skip path method */
2060 for (int i = 0; i < cUnit->numSSARegs; i++) {
2061 llvm::Value* val;
buzbee85eee022012-07-16 22:12:38 -07002062 RegLocation rlTemp = cUnit->regLocation[i];
2063 if ((SRegToVReg(cUnit, i) < 0) || rlTemp.highWord) {
buzbee2a83e8f2012-07-13 16:42:30 -07002064 oatInsertGrowableList(cUnit, &cUnit->llvmValues, 0);
2065 } else if ((i < cUnit->numRegs) ||
2066 (i >= (cUnit->numRegs + cUnit->numIns))) {
buzbee85eee022012-07-16 22:12:38 -07002067 llvm::Constant* immValue = cUnit->regLocation[i].wide ?
2068 cUnit->irb->GetJLong(0) : cUnit->irb->GetJInt(0);
buzbee2a83e8f2012-07-13 16:42:30 -07002069 val = emitConst(cUnit, immValue, cUnit->regLocation[i]);
2070 val->setName(llvmSSAName(cUnit, i));
buzbee2cfc6392012-05-07 14:51:40 -07002071 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)val);
buzbee2cfc6392012-05-07 14:51:40 -07002072 } else {
2073 // Recover previously-created argument values
2074 llvm::Value* argVal = arg_iter++;
2075 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)argVal);
2076 }
2077 }
buzbee2cfc6392012-05-07 14:51:40 -07002078
2079 oatDataFlowAnalysisDispatcher(cUnit, methodBlockBitcodeConversion,
2080 kPreOrderDFSTraversal, false /* Iterative */);
2081
buzbee4be777b2012-07-12 14:38:18 -07002082 /*
2083 * In a few rare cases of verification failure, the verifier will
2084 * replace one or more Dalvik opcodes with the special
2085 * throw-verification-failure opcode. This can leave the SSA graph
2086 * in an invalid state, as definitions may be lost, while uses retained.
2087 * To work around this problem, we insert placeholder definitions for
2088 * all Dalvik SSA regs in the "placeholder" block. Here, after
2089 * bitcode conversion is complete, we examine those placeholder definitions
2090 * and delete any with no references (which normally is all of them).
2091 *
2092 * If any definitions remain, we link the placeholder block into the
2093 * CFG. Otherwise, it is deleted.
2094 */
2095 for (llvm::BasicBlock::iterator it = cUnit->placeholderBB->begin(),
2096 itEnd = cUnit->placeholderBB->end(); it != itEnd;) {
2097 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2098 DCHECK(inst != NULL);
2099 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2100 DCHECK(val != NULL);
2101 if (val->getNumUses() == 0) {
2102 inst->eraseFromParent();
2103 }
2104 }
2105 setDexOffset(cUnit, 0);
2106 if (cUnit->placeholderBB->empty()) {
2107 cUnit->placeholderBB->eraseFromParent();
2108 } else {
2109 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2110 cUnit->irb->CreateBr(cUnit->entryTargetBB);
2111 cUnit->entryTargetBB = cUnit->placeholderBB;
2112 }
2113 cUnit->irb->SetInsertPoint(cUnit->entryBB);
2114 cUnit->irb->CreateBr(cUnit->entryTargetBB);
buzbee2cfc6392012-05-07 14:51:40 -07002115
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002116 if (cUnit->enableDebug & (1 << kDebugVerifyBitcode)) {
2117 if (llvm::verifyFunction(*cUnit->func, llvm::PrintMessageAction)) {
2118 LOG(INFO) << "Bitcode verification FAILED for "
2119 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
2120 << " of size " << cUnit->insnsSize;
2121 cUnit->enableDebug |= (1 << kDebugDumpBitcodeFile);
2122 }
2123 }
buzbee2cfc6392012-05-07 14:51:40 -07002124
buzbeead8f15e2012-06-18 14:49:45 -07002125 if (cUnit->enableDebug & (1 << kDebugDumpBitcodeFile)) {
2126 // Write bitcode to file
2127 std::string errmsg;
2128 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
2129 oatReplaceSpecialChars(fname);
buzbee6459e7c2012-10-02 14:42:41 -07002130 // TODO: make configurable change naming mechanism to avoid fname length issues.
buzbee4f1181f2012-06-22 13:52:12 -07002131 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002132
buzbee6459e7c2012-10-02 14:42:41 -07002133 if (fname.size() > 240) {
2134 LOG(INFO) << "Warning: bitcode filename too long. Truncated.";
2135 fname.resize(240);
2136 }
2137
buzbeead8f15e2012-06-18 14:49:45 -07002138 llvm::OwningPtr<llvm::tool_output_file> out_file(
2139 new llvm::tool_output_file(fname.c_str(), errmsg,
2140 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002141
buzbeead8f15e2012-06-18 14:49:45 -07002142 if (!errmsg.empty()) {
2143 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2144 }
2145
2146 llvm::WriteBitcodeToFile(cUnit->module, out_file->os());
2147 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002148 }
buzbee2cfc6392012-05-07 14:51:40 -07002149}
2150
2151RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val) {
2152 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002153 DCHECK(val != NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002154 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2155 if (it == cUnit->locMap.end()) {
buzbee4f1181f2012-06-22 13:52:12 -07002156 std::string valName = val->getName().str();
buzbee32412962012-06-26 16:27:56 -07002157 if (valName.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002158 // FIXME: need to be more robust, handle FP and be in a position to
2159 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002160 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2161 memset(&res, 0, sizeof(res));
2162 res.location = kLocPhysReg;
2163 res.lowReg = oatAllocTemp(cUnit);
2164 res.home = true;
2165 res.sRegLow = INVALID_SREG;
2166 res.origSReg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002167 llvm::Type* ty = val->getType();
2168 res.wide = ((ty == cUnit->irb->getInt64Ty()) ||
2169 (ty == cUnit->irb->getDoubleTy()));
2170 if (res.wide) {
2171 res.highReg = oatAllocTemp(cUnit);
2172 }
buzbee4f1181f2012-06-22 13:52:12 -07002173 cUnit->locMap.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002174 } else {
2175 DCHECK_EQ(valName[0], 'v');
2176 int baseSReg = INVALID_SREG;
2177 sscanf(valName.c_str(), "v%d_", &baseSReg);
2178 res = cUnit->regLocation[baseSReg];
2179 cUnit->locMap.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002180 }
2181 } else {
2182 res = it->second;
2183 }
2184 return res;
2185}
2186
2187Instruction::Code getDalvikOpcode(OpKind op, bool isConst, bool isWide)
2188{
2189 Instruction::Code res = Instruction::NOP;
2190 if (isWide) {
2191 switch(op) {
2192 case kOpAdd: res = Instruction::ADD_LONG; break;
2193 case kOpSub: res = Instruction::SUB_LONG; break;
2194 case kOpMul: res = Instruction::MUL_LONG; break;
2195 case kOpDiv: res = Instruction::DIV_LONG; break;
2196 case kOpRem: res = Instruction::REM_LONG; break;
2197 case kOpAnd: res = Instruction::AND_LONG; break;
2198 case kOpOr: res = Instruction::OR_LONG; break;
2199 case kOpXor: res = Instruction::XOR_LONG; break;
2200 case kOpLsl: res = Instruction::SHL_LONG; break;
2201 case kOpLsr: res = Instruction::USHR_LONG; break;
2202 case kOpAsr: res = Instruction::SHR_LONG; break;
2203 default: LOG(FATAL) << "Unexpected OpKind " << op;
2204 }
2205 } else if (isConst){
2206 switch(op) {
2207 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2208 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2209 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2210 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2211 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2212 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2213 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2214 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2215 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2216 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2217 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2218 default: LOG(FATAL) << "Unexpected OpKind " << op;
2219 }
2220 } else {
2221 switch(op) {
2222 case kOpAdd: res = Instruction::ADD_INT; break;
2223 case kOpSub: res = Instruction::SUB_INT; break;
2224 case kOpMul: res = Instruction::MUL_INT; break;
2225 case kOpDiv: res = Instruction::DIV_INT; break;
2226 case kOpRem: res = Instruction::REM_INT; break;
2227 case kOpAnd: res = Instruction::AND_INT; break;
2228 case kOpOr: res = Instruction::OR_INT; break;
2229 case kOpXor: res = Instruction::XOR_INT; break;
2230 case kOpLsl: res = Instruction::SHL_INT; break;
2231 case kOpLsr: res = Instruction::USHR_INT; break;
2232 case kOpAsr: res = Instruction::SHR_INT; break;
2233 default: LOG(FATAL) << "Unexpected OpKind " << op;
2234 }
2235 }
2236 return res;
2237}
2238
buzbee4f1181f2012-06-22 13:52:12 -07002239Instruction::Code getDalvikFPOpcode(OpKind op, bool isConst, bool isWide)
2240{
2241 Instruction::Code res = Instruction::NOP;
2242 if (isWide) {
2243 switch(op) {
2244 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2245 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2246 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2247 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2248 case kOpRem: res = Instruction::REM_DOUBLE; break;
2249 default: LOG(FATAL) << "Unexpected OpKind " << op;
2250 }
2251 } else {
2252 switch(op) {
2253 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2254 case kOpSub: res = Instruction::SUB_FLOAT; break;
2255 case kOpMul: res = Instruction::MUL_FLOAT; break;
2256 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2257 case kOpRem: res = Instruction::REM_FLOAT; break;
2258 default: LOG(FATAL) << "Unexpected OpKind " << op;
2259 }
2260 }
2261 return res;
2262}
2263
2264void cvtBinFPOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2265{
2266 RegLocation rlDest = getLoc(cUnit, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002267 /*
2268 * Normally, we won't ever generate an FP operation with an immediate
2269 * operand (not supported in Dex instruction set). However, the IR builder
2270 * may insert them - in particular for createNegFP. Recognize this case
2271 * and deal with it.
2272 */
2273 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2274 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2275 DCHECK(op2C == NULL);
2276 if ((op1C != NULL) && (op == kOpSub)) {
2277 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(1));
2278 if (rlDest.wide) {
2279 genArithOpDouble(cUnit, Instruction::NEG_DOUBLE, rlDest, rlSrc, rlSrc);
2280 } else {
2281 genArithOpFloat(cUnit, Instruction::NEG_FLOAT, rlDest, rlSrc, rlSrc);
2282 }
buzbee4f1181f2012-06-22 13:52:12 -07002283 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002284 DCHECK(op1C == NULL);
2285 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2286 RegLocation rlSrc2 = getLoc(cUnit, inst->getOperand(1));
2287 Instruction::Code dalvikOp = getDalvikFPOpcode(op, false, rlDest.wide);
2288 if (rlDest.wide) {
2289 genArithOpDouble(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2290 } else {
2291 genArithOpFloat(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2292 }
buzbee4f1181f2012-06-22 13:52:12 -07002293 }
2294}
2295
buzbee101305f2012-06-28 18:00:56 -07002296void cvtIntNarrowing(CompilationUnit* cUnit, llvm::Instruction* inst,
2297 Instruction::Code opcode)
2298{
2299 RegLocation rlDest = getLoc(cUnit, inst);
2300 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2301 genIntNarrowing(cUnit, opcode, rlDest, rlSrc);
2302}
2303
buzbee76592632012-06-29 15:18:35 -07002304void cvtIntToFP(CompilationUnit* cUnit, llvm::Instruction* inst)
2305{
2306 RegLocation rlDest = getLoc(cUnit, inst);
2307 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2308 Instruction::Code opcode;
2309 if (rlDest.wide) {
2310 if (rlSrc.wide) {
2311 opcode = Instruction::LONG_TO_DOUBLE;
2312 } else {
2313 opcode = Instruction::INT_TO_DOUBLE;
2314 }
2315 } else {
2316 if (rlSrc.wide) {
2317 opcode = Instruction::LONG_TO_FLOAT;
2318 } else {
2319 opcode = Instruction::INT_TO_FLOAT;
2320 }
2321 }
2322 genConversion(cUnit, opcode, rlDest, rlSrc);
2323}
2324
TDYa1274ec8ccd2012-08-11 07:04:57 -07002325void cvtFPToInt(CompilationUnit* cUnit, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002326{
TDYa1274ec8ccd2012-08-11 07:04:57 -07002327 RegLocation rlDest = getLoc(cUnit, call_inst);
2328 RegLocation rlSrc = getLoc(cUnit, call_inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002329 Instruction::Code opcode;
2330 if (rlDest.wide) {
2331 if (rlSrc.wide) {
2332 opcode = Instruction::DOUBLE_TO_LONG;
2333 } else {
2334 opcode = Instruction::FLOAT_TO_LONG;
2335 }
2336 } else {
2337 if (rlSrc.wide) {
2338 opcode = Instruction::DOUBLE_TO_INT;
2339 } else {
2340 opcode = Instruction::FLOAT_TO_INT;
2341 }
2342 }
2343 genConversion(cUnit, opcode, rlDest, rlSrc);
2344}
2345
2346void cvtFloatToDouble(CompilationUnit* cUnit, llvm::Instruction* inst)
2347{
2348 RegLocation rlDest = getLoc(cUnit, inst);
2349 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2350 genConversion(cUnit, Instruction::FLOAT_TO_DOUBLE, rlDest, rlSrc);
2351}
2352
2353void cvtTrunc(CompilationUnit* cUnit, llvm::Instruction* inst)
2354{
2355 RegLocation rlDest = getLoc(cUnit, inst);
2356 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2357 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
2358 rlSrc = oatWideToNarrow(cUnit, rlSrc);
2359 storeValue(cUnit, rlDest, rlSrc);
2360}
2361
2362void cvtDoubleToFloat(CompilationUnit* cUnit, llvm::Instruction* inst)
2363{
2364 RegLocation rlDest = getLoc(cUnit, inst);
2365 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2366 genConversion(cUnit, Instruction::DOUBLE_TO_FLOAT, rlDest, rlSrc);
2367}
2368
2369
buzbee101305f2012-06-28 18:00:56 -07002370void cvtIntExt(CompilationUnit* cUnit, llvm::Instruction* inst, bool isSigned)
2371{
2372 // TODO: evaluate src/tgt types and add general support for more than int to long
2373 RegLocation rlDest = getLoc(cUnit, inst);
2374 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2375 DCHECK(rlDest.wide);
2376 DCHECK(!rlSrc.wide);
2377 DCHECK(!rlDest.fp);
2378 DCHECK(!rlSrc.fp);
2379 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2380 if (rlSrc.location == kLocPhysReg) {
2381 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2382 } else {
2383 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2384 }
2385 if (isSigned) {
2386 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2387 } else {
2388 loadConstant(cUnit, rlResult.highReg, 0);
2389 }
2390 storeValueWide(cUnit, rlDest, rlResult);
2391}
2392
buzbee2cfc6392012-05-07 14:51:40 -07002393void cvtBinOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2394{
2395 RegLocation rlDest = getLoc(cUnit, inst);
2396 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002397 // Special-case RSUB/NEG
buzbee4f1181f2012-06-22 13:52:12 -07002398 llvm::ConstantInt* lhsImm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2399 if ((op == kOpSub) && (lhsImm != NULL)) {
2400 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(1));
buzbeef58c12c2012-07-03 15:06:29 -07002401 if (rlSrc1.wide) {
2402 DCHECK_EQ(lhsImm->getSExtValue(), 0);
2403 genArithOpLong(cUnit, Instruction::NEG_LONG, rlDest, rlSrc1, rlSrc1);
2404 } else {
2405 genArithOpIntLit(cUnit, Instruction::RSUB_INT, rlDest, rlSrc1,
2406 lhsImm->getSExtValue());
2407 }
buzbee4f1181f2012-06-22 13:52:12 -07002408 return;
2409 }
2410 DCHECK(lhsImm == NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002411 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2412 llvm::Value* rhs = inst->getOperand(1);
buzbee9a2487f2012-07-26 14:01:13 -07002413 llvm::ConstantInt* constRhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2414 if (!rlDest.wide && (constRhs != NULL)) {
buzbee2cfc6392012-05-07 14:51:40 -07002415 Instruction::Code dalvikOp = getDalvikOpcode(op, true, false);
buzbee9a2487f2012-07-26 14:01:13 -07002416 genArithOpIntLit(cUnit, dalvikOp, rlDest, rlSrc1, constRhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002417 } else {
2418 Instruction::Code dalvikOp = getDalvikOpcode(op, false, rlDest.wide);
buzbee9a2487f2012-07-26 14:01:13 -07002419 RegLocation rlSrc2;
2420 if (constRhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002421 // ir_builder converts NOT_LONG to xor src, -1. Restore
2422 DCHECK_EQ(dalvikOp, Instruction::XOR_LONG);
2423 DCHECK_EQ(-1L, constRhs->getSExtValue());
2424 dalvikOp = Instruction::NOT_LONG;
buzbee9a2487f2012-07-26 14:01:13 -07002425 rlSrc2 = rlSrc1;
2426 } else {
2427 rlSrc2 = getLoc(cUnit, rhs);
2428 }
buzbee2cfc6392012-05-07 14:51:40 -07002429 if (rlDest.wide) {
2430 genArithOpLong(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2431 } else {
2432 genArithOpInt(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2433 }
2434 }
2435}
2436
buzbee2a83e8f2012-07-13 16:42:30 -07002437void cvtShiftOp(CompilationUnit* cUnit, Instruction::Code opcode,
2438 llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002439{
buzbee2a83e8f2012-07-13 16:42:30 -07002440 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2441 RegLocation rlDest = getLoc(cUnit, callInst);
2442 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2443 llvm::Value* rhs = callInst->getArgOperand(1);
2444 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2445 DCHECK(!rlDest.wide);
2446 genArithOpIntLit(cUnit, opcode, rlDest, rlSrc, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002447 } else {
buzbee2a83e8f2012-07-13 16:42:30 -07002448 RegLocation rlShift = getLoc(cUnit, rhs);
2449 if (callInst->getType() == cUnit->irb->getInt64Ty()) {
2450 genShiftOpLong(cUnit, opcode, rlDest, rlSrc, rlShift);
2451 } else {
2452 genArithOpInt(cUnit, opcode, rlDest, rlSrc, rlShift);
2453 }
buzbee101305f2012-06-28 18:00:56 -07002454 }
2455}
2456
buzbee2cfc6392012-05-07 14:51:40 -07002457void cvtBr(CompilationUnit* cUnit, llvm::Instruction* inst)
2458{
2459 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(inst);
2460 DCHECK(brInst != NULL);
2461 DCHECK(brInst->isUnconditional()); // May change - but this is all we use now
2462 llvm::BasicBlock* targetBB = brInst->getSuccessor(0);
2463 opUnconditionalBranch(cUnit, cUnit->blockToLabelMap.Get(targetBB));
2464}
2465
2466void cvtPhi(CompilationUnit* cUnit, llvm::Instruction* inst)
2467{
2468 // Nop - these have already been processed
2469}
2470
2471void cvtRet(CompilationUnit* cUnit, llvm::Instruction* inst)
2472{
2473 llvm::ReturnInst* retInst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2474 llvm::Value* retVal = retInst->getReturnValue();
2475 if (retVal != NULL) {
2476 RegLocation rlSrc = getLoc(cUnit, retVal);
2477 if (rlSrc.wide) {
2478 storeValueWide(cUnit, oatGetReturnWide(cUnit, rlSrc.fp), rlSrc);
2479 } else {
2480 storeValue(cUnit, oatGetReturn(cUnit, rlSrc.fp), rlSrc);
2481 }
2482 }
2483 genExitSequence(cUnit);
2484}
2485
2486ConditionCode getCond(llvm::ICmpInst::Predicate llvmCond)
2487{
2488 ConditionCode res = kCondAl;
2489 switch(llvmCond) {
buzbee6969d502012-06-15 16:40:31 -07002490 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002491 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2492 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2493 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002494 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002495 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002496 default: LOG(FATAL) << "Unexpected llvm condition";
2497 }
2498 return res;
2499}
2500
2501void cvtICmp(CompilationUnit* cUnit, llvm::Instruction* inst)
2502{
2503 // genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2)
2504 UNIMPLEMENTED(FATAL);
2505}
2506
2507void cvtICmpBr(CompilationUnit* cUnit, llvm::Instruction* inst,
2508 llvm::BranchInst* brInst)
2509{
2510 // Get targets
2511 llvm::BasicBlock* takenBB = brInst->getSuccessor(0);
2512 LIR* taken = cUnit->blockToLabelMap.Get(takenBB);
2513 llvm::BasicBlock* fallThroughBB = brInst->getSuccessor(1);
2514 LIR* fallThrough = cUnit->blockToLabelMap.Get(fallThroughBB);
2515 // Get comparison operands
2516 llvm::ICmpInst* iCmpInst = llvm::dyn_cast<llvm::ICmpInst>(inst);
2517 ConditionCode cond = getCond(iCmpInst->getPredicate());
2518 llvm::Value* lhs = iCmpInst->getOperand(0);
2519 // Not expecting a constant as 1st operand
2520 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
2521 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2522 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2523 llvm::Value* rhs = inst->getOperand(1);
2524#if defined(TARGET_MIPS)
2525 // Compare and branch in one shot
2526 (void)taken;
2527 (void)cond;
2528 (void)rhs;
2529 UNIMPLEMENTED(FATAL);
2530#else
2531 //Compare, then branch
2532 // TODO: handle fused CMP_LONG/IF_xxZ case
2533 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2534 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002535 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
2536 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002537 } else {
2538 RegLocation rlSrc2 = getLoc(cUnit, rhs);
2539 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2540 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
2541 }
2542 opCondBranch(cUnit, cond, taken);
2543#endif
2544 // Fallthrough
2545 opUnconditionalBranch(cUnit, fallThrough);
2546}
2547
2548void cvtCall(CompilationUnit* cUnit, llvm::CallInst* callInst,
2549 llvm::Function* callee)
2550{
2551 UNIMPLEMENTED(FATAL);
2552}
2553
buzbee2cfc6392012-05-07 14:51:40 -07002554void cvtCopy(CompilationUnit* cUnit, llvm::CallInst* callInst)
2555{
buzbee4f1181f2012-06-22 13:52:12 -07002556 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002557 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2558 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee76592632012-06-29 15:18:35 -07002559 DCHECK_EQ(rlSrc.wide, rlDest.wide);
2560 DCHECK_EQ(rlSrc.fp, rlDest.fp);
buzbee2cfc6392012-05-07 14:51:40 -07002561 if (rlSrc.wide) {
2562 storeValueWide(cUnit, rlDest, rlSrc);
2563 } else {
2564 storeValue(cUnit, rlDest, rlSrc);
2565 }
2566}
2567
2568// Note: Immediate arg is a ConstantInt regardless of result type
2569void cvtConst(CompilationUnit* cUnit, llvm::CallInst* callInst)
2570{
buzbee4f1181f2012-06-22 13:52:12 -07002571 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002572 llvm::ConstantInt* src =
2573 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2574 uint64_t immval = src->getZExtValue();
2575 RegLocation rlDest = getLoc(cUnit, callInst);
2576 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
2577 if (rlDest.wide) {
2578 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
2579 (immval) & 0xffffffff, (immval >> 32) & 0xffffffff);
2580 storeValueWide(cUnit, rlDest, rlResult);
2581 } else {
2582 loadConstantNoClobber(cUnit, rlResult.lowReg, immval & 0xffffffff);
2583 storeValue(cUnit, rlDest, rlResult);
2584 }
2585}
2586
buzbee101305f2012-06-28 18:00:56 -07002587void cvtConstObject(CompilationUnit* cUnit, llvm::CallInst* callInst,
2588 bool isString)
buzbee6969d502012-06-15 16:40:31 -07002589{
buzbee4f1181f2012-06-22 13:52:12 -07002590 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee101305f2012-06-28 18:00:56 -07002591 llvm::ConstantInt* idxVal =
buzbee6969d502012-06-15 16:40:31 -07002592 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee101305f2012-06-28 18:00:56 -07002593 uint32_t index = idxVal->getZExtValue();
buzbee6969d502012-06-15 16:40:31 -07002594 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee101305f2012-06-28 18:00:56 -07002595 if (isString) {
2596 genConstString(cUnit, index, rlDest);
2597 } else {
2598 genConstClass(cUnit, index, rlDest);
2599 }
2600}
2601
2602void cvtFillArrayData(CompilationUnit* cUnit, llvm::CallInst* callInst)
2603{
2604 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2605 llvm::ConstantInt* offsetVal =
2606 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2607 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2608 genFillArrayData(cUnit, offsetVal->getSExtValue(), rlSrc);
buzbee6969d502012-06-15 16:40:31 -07002609}
2610
buzbee4f1181f2012-06-22 13:52:12 -07002611void cvtNewInstance(CompilationUnit* cUnit, llvm::CallInst* callInst)
2612{
buzbee32412962012-06-26 16:27:56 -07002613 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002614 llvm::ConstantInt* typeIdxVal =
2615 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2616 uint32_t typeIdx = typeIdxVal->getZExtValue();
2617 RegLocation rlDest = getLoc(cUnit, callInst);
2618 genNewInstance(cUnit, typeIdx, rlDest);
2619}
2620
buzbee8fa0fda2012-06-27 15:44:52 -07002621void cvtNewArray(CompilationUnit* cUnit, llvm::CallInst* callInst)
2622{
2623 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2624 llvm::ConstantInt* typeIdxVal =
2625 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2626 uint32_t typeIdx = typeIdxVal->getZExtValue();
2627 llvm::Value* len = callInst->getArgOperand(1);
2628 RegLocation rlLen = getLoc(cUnit, len);
2629 RegLocation rlDest = getLoc(cUnit, callInst);
2630 genNewArray(cUnit, typeIdx, rlDest, rlLen);
2631}
2632
2633void cvtInstanceOf(CompilationUnit* cUnit, llvm::CallInst* callInst)
2634{
2635 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2636 llvm::ConstantInt* typeIdxVal =
2637 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2638 uint32_t typeIdx = typeIdxVal->getZExtValue();
2639 llvm::Value* src = callInst->getArgOperand(1);
2640 RegLocation rlSrc = getLoc(cUnit, src);
2641 RegLocation rlDest = getLoc(cUnit, callInst);
2642 genInstanceof(cUnit, typeIdx, rlDest, rlSrc);
2643}
2644
buzbee32412962012-06-26 16:27:56 -07002645void cvtThrow(CompilationUnit* cUnit, llvm::CallInst* callInst)
2646{
2647 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
2648 llvm::Value* src = callInst->getArgOperand(0);
2649 RegLocation rlSrc = getLoc(cUnit, src);
2650 genThrow(cUnit, rlSrc);
2651}
2652
buzbee8fa0fda2012-06-27 15:44:52 -07002653void cvtMonitorEnterExit(CompilationUnit* cUnit, bool isEnter,
2654 llvm::CallInst* callInst)
2655{
2656 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2657 llvm::ConstantInt* optFlags =
2658 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2659 llvm::Value* src = callInst->getArgOperand(1);
2660 RegLocation rlSrc = getLoc(cUnit, src);
2661 if (isEnter) {
2662 genMonitorEnter(cUnit, optFlags->getZExtValue(), rlSrc);
2663 } else {
2664 genMonitorExit(cUnit, optFlags->getZExtValue(), rlSrc);
2665 }
2666}
2667
buzbee76592632012-06-29 15:18:35 -07002668void cvtArrayLength(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002669{
2670 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2671 llvm::ConstantInt* optFlags =
2672 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2673 llvm::Value* src = callInst->getArgOperand(1);
2674 RegLocation rlSrc = getLoc(cUnit, src);
2675 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2676 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg, optFlags->getZExtValue());
2677 RegLocation rlDest = getLoc(cUnit, callInst);
2678 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2679 int lenOffset = Array::LengthOffset().Int32Value();
2680 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset, rlResult.lowReg);
2681 storeValue(cUnit, rlDest, rlResult);
2682}
2683
buzbee32412962012-06-26 16:27:56 -07002684void cvtMoveException(CompilationUnit* cUnit, llvm::CallInst* callInst)
2685{
buzbee32412962012-06-26 16:27:56 -07002686 RegLocation rlDest = getLoc(cUnit, callInst);
Ian Rogers474b6da2012-09-25 00:20:38 -07002687 genMoveException(cUnit, rlDest);
buzbee32412962012-06-26 16:27:56 -07002688}
2689
buzbee4f1181f2012-06-22 13:52:12 -07002690void cvtSget(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2691 bool isObject)
2692{
buzbee32412962012-06-26 16:27:56 -07002693 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002694 llvm::ConstantInt* typeIdxVal =
2695 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2696 uint32_t typeIdx = typeIdxVal->getZExtValue();
2697 RegLocation rlDest = getLoc(cUnit, callInst);
2698 genSget(cUnit, typeIdx, rlDest, isWide, isObject);
2699}
2700
buzbee8fa0fda2012-06-27 15:44:52 -07002701void cvtSput(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2702 bool isObject)
2703{
2704 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2705 llvm::ConstantInt* typeIdxVal =
2706 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2707 uint32_t typeIdx = typeIdxVal->getZExtValue();
2708 llvm::Value* src = callInst->getArgOperand(1);
2709 RegLocation rlSrc = getLoc(cUnit, src);
2710 genSput(cUnit, typeIdx, rlSrc, isWide, isObject);
2711}
2712
2713void cvtAget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2714 int scale)
2715{
2716 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2717 llvm::ConstantInt* optFlags =
2718 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2719 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(1));
2720 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(2));
2721 RegLocation rlDest = getLoc(cUnit, callInst);
2722 genArrayGet(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2723 rlDest, scale);
2724}
2725
2726void cvtAput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
buzbeef1f86362012-07-10 15:18:31 -07002727 int scale, bool isObject)
buzbee8fa0fda2012-06-27 15:44:52 -07002728{
2729 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2730 llvm::ConstantInt* optFlags =
2731 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2732 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2733 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(2));
2734 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(3));
buzbeef1f86362012-07-10 15:18:31 -07002735 if (isObject) {
2736 genArrayObjPut(cUnit, optFlags->getZExtValue(), rlArray, rlIndex,
2737 rlSrc, scale);
2738 } else {
2739 genArrayPut(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2740 rlSrc, scale);
2741 }
2742}
2743
2744void cvtAputObj(CompilationUnit* cUnit, llvm::CallInst* callInst)
2745{
2746 cvtAput(cUnit, callInst, kWord, 2, true /* isObject */);
2747}
2748
2749void cvtAputPrimitive(CompilationUnit* cUnit, llvm::CallInst* callInst,
2750 OpSize size, int scale)
2751{
2752 cvtAput(cUnit, callInst, size, scale, false /* isObject */);
buzbee8fa0fda2012-06-27 15:44:52 -07002753}
2754
buzbee101305f2012-06-28 18:00:56 -07002755void cvtIget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2756 bool isWide, bool isObj)
2757{
2758 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2759 llvm::ConstantInt* optFlags =
2760 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2761 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(1));
2762 llvm::ConstantInt* fieldIdx =
2763 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2764 RegLocation rlDest = getLoc(cUnit, callInst);
2765 genIGet(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2766 size, rlDest, rlObj, isWide, isObj);
2767}
2768
2769void cvtIput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2770 bool isWide, bool isObj)
2771{
2772 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2773 llvm::ConstantInt* optFlags =
2774 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2775 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2776 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(2));
2777 llvm::ConstantInt* fieldIdx =
buzbee4f4dfc72012-07-02 14:54:44 -07002778 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(3));
buzbee101305f2012-06-28 18:00:56 -07002779 genIPut(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2780 size, rlSrc, rlObj, isWide, isObj);
2781}
2782
2783void cvtCheckCast(CompilationUnit* cUnit, llvm::CallInst* callInst)
2784{
2785 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2786 llvm::ConstantInt* typeIdx =
2787 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2788 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2789 genCheckCast(cUnit, typeIdx->getZExtValue(), rlSrc);
2790}
2791
buzbee76592632012-06-29 15:18:35 -07002792void cvtFPCompare(CompilationUnit* cUnit, llvm::CallInst* callInst,
2793 Instruction::Code opcode)
2794{
2795 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2796 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2797 RegLocation rlDest = getLoc(cUnit, callInst);
2798 genCmpFP(cUnit, opcode, rlDest, rlSrc1, rlSrc2);
2799}
2800
2801void cvtLongCompare(CompilationUnit* cUnit, llvm::CallInst* callInst)
2802{
2803 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2804 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2805 RegLocation rlDest = getLoc(cUnit, callInst);
2806 genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2);
2807}
2808
buzbeef58c12c2012-07-03 15:06:29 -07002809void cvtSwitch(CompilationUnit* cUnit, llvm::Instruction* inst)
2810{
2811 llvm::SwitchInst* swInst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2812 DCHECK(swInst != NULL);
2813 llvm::Value* testVal = swInst->getCondition();
2814 llvm::MDNode* tableOffsetNode = swInst->getMetadata("SwitchTable");
2815 DCHECK(tableOffsetNode != NULL);
2816 llvm::ConstantInt* tableOffsetValue =
2817 static_cast<llvm::ConstantInt*>(tableOffsetNode->getOperand(0));
2818 int32_t tableOffset = tableOffsetValue->getSExtValue();
2819 RegLocation rlSrc = getLoc(cUnit, testVal);
buzbeea1da8a52012-07-09 14:00:21 -07002820 const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
2821 u2 tableMagic = *table;
2822 if (tableMagic == 0x100) {
2823 genPackedSwitch(cUnit, tableOffset, rlSrc);
2824 } else {
2825 DCHECK_EQ(tableMagic, 0x200);
2826 genSparseSwitch(cUnit, tableOffset, rlSrc);
2827 }
buzbeef58c12c2012-07-03 15:06:29 -07002828}
2829
buzbee6969d502012-06-15 16:40:31 -07002830void cvtInvoke(CompilationUnit* cUnit, llvm::CallInst* callInst,
buzbee76592632012-06-29 15:18:35 -07002831 bool isVoid, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -07002832{
2833 CallInfo* info = (CallInfo*)oatNew(cUnit, sizeof(CallInfo), true,
2834 kAllocMisc);
buzbee8fa0fda2012-06-27 15:44:52 -07002835 if (isVoid) {
buzbee6969d502012-06-15 16:40:31 -07002836 info->result.location = kLocInvalid;
2837 } else {
2838 info->result = getLoc(cUnit, callInst);
2839 }
2840 llvm::ConstantInt* invokeTypeVal =
2841 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2842 llvm::ConstantInt* methodIndexVal =
2843 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(1));
2844 llvm::ConstantInt* optFlagsVal =
2845 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2846 info->type = static_cast<InvokeType>(invokeTypeVal->getZExtValue());
2847 info->index = methodIndexVal->getZExtValue();
2848 info->optFlags = optFlagsVal->getZExtValue();
2849 info->offset = cUnit->currentDalvikOffset;
2850
buzbee6969d502012-06-15 16:40:31 -07002851 // Count the argument words, and then build argument array.
2852 info->numArgWords = 0;
2853 for (unsigned int i = 3; i < callInst->getNumArgOperands(); i++) {
2854 RegLocation tLoc = getLoc(cUnit, callInst->getArgOperand(i));
2855 info->numArgWords += tLoc.wide ? 2 : 1;
2856 }
2857 info->args = (info->numArgWords == 0) ? NULL : (RegLocation*)
2858 oatNew(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc);
2859 // Now, fill in the location records, synthesizing high loc of wide vals
2860 for (int i = 3, next = 0; next < info->numArgWords;) {
buzbee4f1181f2012-06-22 13:52:12 -07002861 info->args[next] = getLoc(cUnit, callInst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002862 if (info->args[next].wide) {
2863 next++;
2864 // TODO: Might make sense to mark this as an invalid loc
2865 info->args[next].origSReg = info->args[next-1].origSReg+1;
2866 info->args[next].sRegLow = info->args[next-1].sRegLow+1;
2867 }
2868 next++;
2869 }
buzbee4f4dfc72012-07-02 14:54:44 -07002870 // TODO - rework such that we no longer need isRange
2871 info->isRange = (info->numArgWords > 5);
2872
buzbee76592632012-06-29 15:18:35 -07002873 if (isFilledNewArray) {
buzbee101305f2012-06-28 18:00:56 -07002874 genFilledNewArray(cUnit, info);
2875 } else {
2876 genInvoke(cUnit, info);
2877 }
buzbee6969d502012-06-15 16:40:31 -07002878}
2879
buzbeead8f15e2012-06-18 14:49:45 -07002880/* Look up the RegLocation associated with a Value. Must already be defined */
2881RegLocation valToLoc(CompilationUnit* cUnit, llvm::Value* val)
2882{
2883 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2884 DCHECK(it != cUnit->locMap.end()) << "Missing definition";
2885 return it->second;
2886}
2887
buzbee2cfc6392012-05-07 14:51:40 -07002888bool methodBitcodeBlockCodeGen(CompilationUnit* cUnit, llvm::BasicBlock* bb)
2889{
buzbee0967a252012-09-14 10:43:54 -07002890 while (cUnit->llvmBlocks.find(bb) == cUnit->llvmBlocks.end()) {
2891 llvm::BasicBlock* nextBB = NULL;
2892 cUnit->llvmBlocks.insert(bb);
2893 bool isEntry = (bb == &cUnit->func->getEntryBlock());
2894 // Define the starting label
2895 LIR* blockLabel = cUnit->blockToLabelMap.Get(bb);
2896 // Extract the type and starting offset from the block's name
buzbee951c0a12012-10-03 16:31:39 -07002897 char blockType = kInvalidBlock;
2898 if (isEntry) {
2899 blockType = kNormalBlock;
2900 blockLabel->operands[0] = 0;
2901 } else if (!bb->hasName()) {
2902 blockType = kNormalBlock;
2903 blockLabel->operands[0] = DexFile::kDexNoIndex;
buzbee0967a252012-09-14 10:43:54 -07002904 } else {
buzbee951c0a12012-10-03 16:31:39 -07002905 std::string blockName = bb->getName().str();
2906 int dummy;
2907 sscanf(blockName.c_str(), kLabelFormat, &blockType, &blockLabel->operands[0], &dummy);
2908 cUnit->currentDalvikOffset = blockLabel->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002909 }
buzbee951c0a12012-10-03 16:31:39 -07002910 DCHECK((blockType == kNormalBlock) || (blockType == kCatchBlock));
2911 cUnit->currentDalvikOffset = blockLabel->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002912 // Set the label kind
2913 blockLabel->opcode = kPseudoNormalBlockLabel;
2914 // Insert the label
2915 oatAppendLIR(cUnit, blockLabel);
buzbee2cfc6392012-05-07 14:51:40 -07002916
buzbee0967a252012-09-14 10:43:54 -07002917 LIR* headLIR = NULL;
buzbee8320f382012-09-11 16:29:42 -07002918
buzbee0967a252012-09-14 10:43:54 -07002919 if (blockType == kCatchBlock) {
Bill Buzbeea5b30242012-09-28 07:19:44 -07002920 headLIR = newLIR0(cUnit, kPseudoExportedPC);
buzbee0967a252012-09-14 10:43:54 -07002921 }
buzbee8320f382012-09-11 16:29:42 -07002922
buzbee0967a252012-09-14 10:43:54 -07002923 // Free temp registers and reset redundant store tracking */
2924 oatResetRegPool(cUnit);
2925 oatResetDefTracking(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002926
buzbee0967a252012-09-14 10:43:54 -07002927 //TODO: restore oat incoming liveness optimization
2928 oatClobberAllRegs(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002929
buzbee0967a252012-09-14 10:43:54 -07002930 if (isEntry) {
2931 RegLocation* argLocs = (RegLocation*)
2932 oatNew(cUnit, sizeof(RegLocation) * cUnit->numIns, true, kAllocMisc);
2933 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
2934 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
2935 // Skip past Method*
2936 it++;
2937 for (unsigned i = 0; it != it_end; ++it) {
2938 llvm::Value* val = it;
2939 argLocs[i++] = valToLoc(cUnit, val);
2940 llvm::Type* ty = val->getType();
2941 if ((ty == cUnit->irb->getInt64Ty()) || (ty == cUnit->irb->getDoubleTy())) {
2942 argLocs[i] = argLocs[i-1];
2943 argLocs[i].lowReg = argLocs[i].highReg;
2944 argLocs[i].origSReg++;
2945 argLocs[i].sRegLow = INVALID_SREG;
2946 argLocs[i].highWord = true;
2947 i++;
2948 }
2949 }
2950 genEntrySequence(cUnit, argLocs, cUnit->methodLoc);
2951 }
2952
2953 // Visit all of the instructions in the block
2954 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2955 llvm::Instruction* inst = it;
2956 llvm::BasicBlock::iterator nextIt = ++it;
2957 // Extract the Dalvik offset from the instruction
2958 uint32_t opcode = inst->getOpcode();
2959 llvm::MDNode* dexOffsetNode = inst->getMetadata("DexOff");
2960 if (dexOffsetNode != NULL) {
2961 llvm::ConstantInt* dexOffsetValue =
2962 static_cast<llvm::ConstantInt*>(dexOffsetNode->getOperand(0));
2963 cUnit->currentDalvikOffset = dexOffsetValue->getZExtValue();
2964 }
2965
2966 oatResetRegPool(cUnit);
2967 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
2968 oatClobberAllRegs(cUnit);
2969 }
2970
2971 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
2972 oatResetDefTracking(cUnit);
2973 }
2974
2975 #ifndef NDEBUG
2976 /* Reset temp tracking sanity check */
2977 cUnit->liveSReg = INVALID_SREG;
2978 #endif
2979
2980 // TODO: use llvm opcode name here instead of "boundary" if verbose
2981 LIR* boundaryLIR = markBoundary(cUnit, cUnit->currentDalvikOffset, "boundary");
2982
2983 /* Remember the first LIR for thisl block*/
2984 if (headLIR == NULL) {
2985 headLIR = boundaryLIR;
2986 headLIR->defMask = ENCODE_ALL;
2987 }
2988
2989 switch(opcode) {
2990
2991 case llvm::Instruction::ICmp: {
2992 llvm::Instruction* nextInst = nextIt;
2993 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(nextInst);
2994 if (brInst != NULL /* and... */) {
2995 cvtICmpBr(cUnit, inst, brInst);
2996 ++it;
2997 } else {
2998 cvtICmp(cUnit, inst);
2999 }
3000 }
3001 break;
3002
3003 case llvm::Instruction::Call: {
3004 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(inst);
3005 llvm::Function* callee = callInst->getCalledFunction();
3006 greenland::IntrinsicHelper::IntrinsicId id =
3007 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3008 switch (id) {
3009 case greenland::IntrinsicHelper::AllocaShadowFrame:
3010 case greenland::IntrinsicHelper::SetShadowFrameEntry:
3011 case greenland::IntrinsicHelper::PopShadowFrame:
3012 // Ignore shadow frame stuff for quick compiler
3013 break;
3014 case greenland::IntrinsicHelper::CopyInt:
3015 case greenland::IntrinsicHelper::CopyObj:
3016 case greenland::IntrinsicHelper::CopyFloat:
3017 case greenland::IntrinsicHelper::CopyLong:
3018 case greenland::IntrinsicHelper::CopyDouble:
3019 cvtCopy(cUnit, callInst);
3020 break;
3021 case greenland::IntrinsicHelper::ConstInt:
3022 case greenland::IntrinsicHelper::ConstObj:
3023 case greenland::IntrinsicHelper::ConstLong:
3024 case greenland::IntrinsicHelper::ConstFloat:
3025 case greenland::IntrinsicHelper::ConstDouble:
3026 cvtConst(cUnit, callInst);
3027 break;
3028 case greenland::IntrinsicHelper::DivInt:
3029 case greenland::IntrinsicHelper::DivLong:
3030 cvtBinOp(cUnit, kOpDiv, inst);
3031 break;
3032 case greenland::IntrinsicHelper::RemInt:
3033 case greenland::IntrinsicHelper::RemLong:
3034 cvtBinOp(cUnit, kOpRem, inst);
3035 break;
3036 case greenland::IntrinsicHelper::MethodInfo:
3037 // Already dealt with - just ignore it here.
3038 break;
3039 case greenland::IntrinsicHelper::CheckSuspend:
3040 genSuspendTest(cUnit, 0 /* optFlags already applied */);
3041 break;
3042 case greenland::IntrinsicHelper::HLInvokeObj:
3043 case greenland::IntrinsicHelper::HLInvokeFloat:
3044 case greenland::IntrinsicHelper::HLInvokeDouble:
3045 case greenland::IntrinsicHelper::HLInvokeLong:
3046 case greenland::IntrinsicHelper::HLInvokeInt:
3047 cvtInvoke(cUnit, callInst, false /* isVoid */, false /* newArray */);
3048 break;
3049 case greenland::IntrinsicHelper::HLInvokeVoid:
3050 cvtInvoke(cUnit, callInst, true /* isVoid */, false /* newArray */);
3051 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003052 case greenland::IntrinsicHelper::HLFilledNewArray:
buzbee0967a252012-09-14 10:43:54 -07003053 cvtInvoke(cUnit, callInst, false /* isVoid */, true /* newArray */);
3054 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003055 case greenland::IntrinsicHelper::HLFillArrayData:
buzbee0967a252012-09-14 10:43:54 -07003056 cvtFillArrayData(cUnit, callInst);
3057 break;
3058 case greenland::IntrinsicHelper::ConstString:
3059 cvtConstObject(cUnit, callInst, true /* isString */);
3060 break;
3061 case greenland::IntrinsicHelper::ConstClass:
3062 cvtConstObject(cUnit, callInst, false /* isString */);
3063 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003064 case greenland::IntrinsicHelper::HLCheckCast:
buzbee0967a252012-09-14 10:43:54 -07003065 cvtCheckCast(cUnit, callInst);
3066 break;
3067 case greenland::IntrinsicHelper::NewInstance:
3068 cvtNewInstance(cUnit, callInst);
3069 break;
3070 case greenland::IntrinsicHelper::HLSgetObject:
3071 cvtSget(cUnit, callInst, false /* wide */, true /* Object */);
3072 break;
3073 case greenland::IntrinsicHelper::HLSget:
3074 case greenland::IntrinsicHelper::HLSgetFloat:
3075 case greenland::IntrinsicHelper::HLSgetBoolean:
3076 case greenland::IntrinsicHelper::HLSgetByte:
3077 case greenland::IntrinsicHelper::HLSgetChar:
3078 case greenland::IntrinsicHelper::HLSgetShort:
3079 cvtSget(cUnit, callInst, false /* wide */, false /* Object */);
3080 break;
3081 case greenland::IntrinsicHelper::HLSgetWide:
3082 case greenland::IntrinsicHelper::HLSgetDouble:
3083 cvtSget(cUnit, callInst, true /* wide */, false /* Object */);
3084 break;
3085 case greenland::IntrinsicHelper::HLSput:
3086 case greenland::IntrinsicHelper::HLSputFloat:
3087 case greenland::IntrinsicHelper::HLSputBoolean:
3088 case greenland::IntrinsicHelper::HLSputByte:
3089 case greenland::IntrinsicHelper::HLSputChar:
3090 case greenland::IntrinsicHelper::HLSputShort:
3091 cvtSput(cUnit, callInst, false /* wide */, false /* Object */);
3092 break;
3093 case greenland::IntrinsicHelper::HLSputWide:
3094 case greenland::IntrinsicHelper::HLSputDouble:
3095 cvtSput(cUnit, callInst, true /* wide */, false /* Object */);
3096 break;
3097 case greenland::IntrinsicHelper::HLSputObject:
3098 cvtSput(cUnit, callInst, false /* wide */, true /* Object */);
3099 break;
3100 case greenland::IntrinsicHelper::GetException:
3101 cvtMoveException(cUnit, callInst);
3102 break;
TDYa127f71bf5a2012-07-29 20:09:52 -07003103 case greenland::IntrinsicHelper::HLThrowException:
buzbee0967a252012-09-14 10:43:54 -07003104 cvtThrow(cUnit, callInst);
3105 break;
3106 case greenland::IntrinsicHelper::MonitorEnter:
3107 cvtMonitorEnterExit(cUnit, true /* isEnter */, callInst);
3108 break;
3109 case greenland::IntrinsicHelper::MonitorExit:
3110 cvtMonitorEnterExit(cUnit, false /* isEnter */, callInst);
3111 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003112 case greenland::IntrinsicHelper::OptArrayLength:
buzbee0967a252012-09-14 10:43:54 -07003113 cvtArrayLength(cUnit, callInst);
3114 break;
3115 case greenland::IntrinsicHelper::NewArray:
3116 cvtNewArray(cUnit, callInst);
3117 break;
3118 case greenland::IntrinsicHelper::InstanceOf:
3119 cvtInstanceOf(cUnit, callInst);
3120 break;
3121
3122 case greenland::IntrinsicHelper::HLArrayGet:
3123 case greenland::IntrinsicHelper::HLArrayGetObject:
3124 case greenland::IntrinsicHelper::HLArrayGetFloat:
3125 cvtAget(cUnit, callInst, kWord, 2);
3126 break;
3127 case greenland::IntrinsicHelper::HLArrayGetWide:
3128 case greenland::IntrinsicHelper::HLArrayGetDouble:
3129 cvtAget(cUnit, callInst, kLong, 3);
3130 break;
3131 case greenland::IntrinsicHelper::HLArrayGetBoolean:
3132 cvtAget(cUnit, callInst, kUnsignedByte, 0);
3133 break;
3134 case greenland::IntrinsicHelper::HLArrayGetByte:
3135 cvtAget(cUnit, callInst, kSignedByte, 0);
3136 break;
3137 case greenland::IntrinsicHelper::HLArrayGetChar:
3138 cvtAget(cUnit, callInst, kUnsignedHalf, 1);
3139 break;
3140 case greenland::IntrinsicHelper::HLArrayGetShort:
3141 cvtAget(cUnit, callInst, kSignedHalf, 1);
3142 break;
3143
3144 case greenland::IntrinsicHelper::HLArrayPut:
3145 case greenland::IntrinsicHelper::HLArrayPutFloat:
3146 cvtAputPrimitive(cUnit, callInst, kWord, 2);
3147 break;
3148 case greenland::IntrinsicHelper::HLArrayPutObject:
3149 cvtAputObj(cUnit, callInst);
3150 break;
3151 case greenland::IntrinsicHelper::HLArrayPutWide:
3152 case greenland::IntrinsicHelper::HLArrayPutDouble:
3153 cvtAputPrimitive(cUnit, callInst, kLong, 3);
3154 break;
3155 case greenland::IntrinsicHelper::HLArrayPutBoolean:
3156 cvtAputPrimitive(cUnit, callInst, kUnsignedByte, 0);
3157 break;
3158 case greenland::IntrinsicHelper::HLArrayPutByte:
3159 cvtAputPrimitive(cUnit, callInst, kSignedByte, 0);
3160 break;
3161 case greenland::IntrinsicHelper::HLArrayPutChar:
3162 cvtAputPrimitive(cUnit, callInst, kUnsignedHalf, 1);
3163 break;
3164 case greenland::IntrinsicHelper::HLArrayPutShort:
3165 cvtAputPrimitive(cUnit, callInst, kSignedHalf, 1);
3166 break;
3167
3168 case greenland::IntrinsicHelper::HLIGet:
3169 case greenland::IntrinsicHelper::HLIGetFloat:
3170 cvtIget(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3171 break;
3172 case greenland::IntrinsicHelper::HLIGetObject:
3173 cvtIget(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3174 break;
3175 case greenland::IntrinsicHelper::HLIGetWide:
3176 case greenland::IntrinsicHelper::HLIGetDouble:
3177 cvtIget(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3178 break;
3179 case greenland::IntrinsicHelper::HLIGetBoolean:
3180 cvtIget(cUnit, callInst, kUnsignedByte, false /* isWide */,
3181 false /* obj */);
3182 break;
3183 case greenland::IntrinsicHelper::HLIGetByte:
3184 cvtIget(cUnit, callInst, kSignedByte, false /* isWide */,
3185 false /* obj */);
3186 break;
3187 case greenland::IntrinsicHelper::HLIGetChar:
3188 cvtIget(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3189 false /* obj */);
3190 break;
3191 case greenland::IntrinsicHelper::HLIGetShort:
3192 cvtIget(cUnit, callInst, kSignedHalf, false /* isWide */,
3193 false /* obj */);
3194 break;
3195
3196 case greenland::IntrinsicHelper::HLIPut:
3197 case greenland::IntrinsicHelper::HLIPutFloat:
3198 cvtIput(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3199 break;
3200 case greenland::IntrinsicHelper::HLIPutObject:
3201 cvtIput(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3202 break;
3203 case greenland::IntrinsicHelper::HLIPutWide:
3204 case greenland::IntrinsicHelper::HLIPutDouble:
3205 cvtIput(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3206 break;
3207 case greenland::IntrinsicHelper::HLIPutBoolean:
3208 cvtIput(cUnit, callInst, kUnsignedByte, false /* isWide */,
3209 false /* obj */);
3210 break;
3211 case greenland::IntrinsicHelper::HLIPutByte:
3212 cvtIput(cUnit, callInst, kSignedByte, false /* isWide */,
3213 false /* obj */);
3214 break;
3215 case greenland::IntrinsicHelper::HLIPutChar:
3216 cvtIput(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3217 false /* obj */);
3218 break;
3219 case greenland::IntrinsicHelper::HLIPutShort:
3220 cvtIput(cUnit, callInst, kSignedHalf, false /* isWide */,
3221 false /* obj */);
3222 break;
3223
3224 case greenland::IntrinsicHelper::IntToChar:
3225 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_CHAR);
3226 break;
3227 case greenland::IntrinsicHelper::IntToShort:
3228 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_SHORT);
3229 break;
3230 case greenland::IntrinsicHelper::IntToByte:
3231 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_BYTE);
3232 break;
3233
TDYa1274ec8ccd2012-08-11 07:04:57 -07003234 case greenland::IntrinsicHelper::F2I:
3235 case greenland::IntrinsicHelper::D2I:
3236 case greenland::IntrinsicHelper::F2L:
3237 case greenland::IntrinsicHelper::D2L:
3238 cvtFPToInt(cUnit, callInst);
3239 break;
3240
buzbee0967a252012-09-14 10:43:54 -07003241 case greenland::IntrinsicHelper::CmplFloat:
3242 cvtFPCompare(cUnit, callInst, Instruction::CMPL_FLOAT);
3243 break;
3244 case greenland::IntrinsicHelper::CmpgFloat:
3245 cvtFPCompare(cUnit, callInst, Instruction::CMPG_FLOAT);
3246 break;
3247 case greenland::IntrinsicHelper::CmplDouble:
3248 cvtFPCompare(cUnit, callInst, Instruction::CMPL_DOUBLE);
3249 break;
3250 case greenland::IntrinsicHelper::CmpgDouble:
3251 cvtFPCompare(cUnit, callInst, Instruction::CMPG_DOUBLE);
3252 break;
3253
3254 case greenland::IntrinsicHelper::CmpLong:
3255 cvtLongCompare(cUnit, callInst);
3256 break;
3257
3258 case greenland::IntrinsicHelper::SHLLong:
3259 cvtShiftOp(cUnit, Instruction::SHL_LONG, callInst);
3260 break;
3261 case greenland::IntrinsicHelper::SHRLong:
3262 cvtShiftOp(cUnit, Instruction::SHR_LONG, callInst);
3263 break;
3264 case greenland::IntrinsicHelper::USHRLong:
3265 cvtShiftOp(cUnit, Instruction::USHR_LONG, callInst);
3266 break;
3267 case greenland::IntrinsicHelper::SHLInt:
3268 cvtShiftOp(cUnit, Instruction::SHL_INT, callInst);
3269 break;
3270 case greenland::IntrinsicHelper::SHRInt:
3271 cvtShiftOp(cUnit, Instruction::SHR_INT, callInst);
3272 break;
3273 case greenland::IntrinsicHelper::USHRInt:
3274 cvtShiftOp(cUnit, Instruction::USHR_INT, callInst);
3275 break;
3276
3277 case greenland::IntrinsicHelper::CatchTargets: {
3278 llvm::SwitchInst* swInst =
3279 llvm::dyn_cast<llvm::SwitchInst>(nextIt);
3280 DCHECK(swInst != NULL);
3281 /*
3282 * Discard the edges and the following conditional branch.
3283 * Do a direct branch to the default target (which is the
3284 * "work" portion of the pair.
3285 * TODO: awful code layout - rework
3286 */
3287 llvm::BasicBlock* targetBB = swInst->getDefaultDest();
3288 DCHECK(targetBB != NULL);
3289 opUnconditionalBranch(cUnit,
3290 cUnit->blockToLabelMap.Get(targetBB));
3291 ++it;
3292 // Set next bb to default target - improves code layout
3293 nextBB = targetBB;
3294 }
3295 break;
3296
3297 default:
3298 LOG(FATAL) << "Unexpected intrinsic " << (int)id << ", "
3299 << cUnit->intrinsic_helper->GetName(id);
3300 }
3301 }
3302 break;
3303
3304 case llvm::Instruction::Br: cvtBr(cUnit, inst); break;
3305 case llvm::Instruction::Add: cvtBinOp(cUnit, kOpAdd, inst); break;
3306 case llvm::Instruction::Sub: cvtBinOp(cUnit, kOpSub, inst); break;
3307 case llvm::Instruction::Mul: cvtBinOp(cUnit, kOpMul, inst); break;
3308 case llvm::Instruction::SDiv: cvtBinOp(cUnit, kOpDiv, inst); break;
3309 case llvm::Instruction::SRem: cvtBinOp(cUnit, kOpRem, inst); break;
3310 case llvm::Instruction::And: cvtBinOp(cUnit, kOpAnd, inst); break;
3311 case llvm::Instruction::Or: cvtBinOp(cUnit, kOpOr, inst); break;
3312 case llvm::Instruction::Xor: cvtBinOp(cUnit, kOpXor, inst); break;
3313 case llvm::Instruction::PHI: cvtPhi(cUnit, inst); break;
3314 case llvm::Instruction::Ret: cvtRet(cUnit, inst); break;
3315 case llvm::Instruction::FAdd: cvtBinFPOp(cUnit, kOpAdd, inst); break;
3316 case llvm::Instruction::FSub: cvtBinFPOp(cUnit, kOpSub, inst); break;
3317 case llvm::Instruction::FMul: cvtBinFPOp(cUnit, kOpMul, inst); break;
3318 case llvm::Instruction::FDiv: cvtBinFPOp(cUnit, kOpDiv, inst); break;
3319 case llvm::Instruction::FRem: cvtBinFPOp(cUnit, kOpRem, inst); break;
3320 case llvm::Instruction::SIToFP: cvtIntToFP(cUnit, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003321 case llvm::Instruction::FPTrunc: cvtDoubleToFloat(cUnit, inst); break;
3322 case llvm::Instruction::FPExt: cvtFloatToDouble(cUnit, inst); break;
3323 case llvm::Instruction::Trunc: cvtTrunc(cUnit, inst); break;
3324
3325 case llvm::Instruction::ZExt: cvtIntExt(cUnit, inst, false /* signed */);
3326 break;
3327 case llvm::Instruction::SExt: cvtIntExt(cUnit, inst, true /* signed */);
3328 break;
3329
3330 case llvm::Instruction::Switch: cvtSwitch(cUnit, inst); break;
3331
3332 case llvm::Instruction::Unreachable:
3333 break; // FIXME: can we really ignore these?
3334
3335 case llvm::Instruction::Shl:
3336 case llvm::Instruction::LShr:
3337 case llvm::Instruction::AShr:
3338 case llvm::Instruction::Invoke:
3339 case llvm::Instruction::FPToUI:
TDYa1274ec8ccd2012-08-11 07:04:57 -07003340 case llvm::Instruction::FPToSI:
buzbee0967a252012-09-14 10:43:54 -07003341 case llvm::Instruction::UIToFP:
3342 case llvm::Instruction::PtrToInt:
3343 case llvm::Instruction::IntToPtr:
3344 case llvm::Instruction::FCmp:
3345 case llvm::Instruction::URem:
3346 case llvm::Instruction::UDiv:
3347 case llvm::Instruction::Resume:
3348 case llvm::Instruction::Alloca:
3349 case llvm::Instruction::GetElementPtr:
3350 case llvm::Instruction::Fence:
3351 case llvm::Instruction::AtomicCmpXchg:
3352 case llvm::Instruction::AtomicRMW:
3353 case llvm::Instruction::BitCast:
3354 case llvm::Instruction::VAArg:
3355 case llvm::Instruction::Select:
3356 case llvm::Instruction::UserOp1:
3357 case llvm::Instruction::UserOp2:
3358 case llvm::Instruction::ExtractElement:
3359 case llvm::Instruction::InsertElement:
3360 case llvm::Instruction::ShuffleVector:
3361 case llvm::Instruction::ExtractValue:
3362 case llvm::Instruction::InsertValue:
3363 case llvm::Instruction::LandingPad:
3364 case llvm::Instruction::IndirectBr:
3365 case llvm::Instruction::Load:
3366 case llvm::Instruction::Store:
3367 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3368
3369 default:
3370 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3371 break;
buzbeead8f15e2012-06-18 14:49:45 -07003372 }
3373 }
buzbee2cfc6392012-05-07 14:51:40 -07003374
buzbee0967a252012-09-14 10:43:54 -07003375 if (headLIR != NULL) {
3376 oatApplyLocalOptimizations(cUnit, headLIR, cUnit->lastLIRInsn);
buzbee2cfc6392012-05-07 14:51:40 -07003377 }
buzbee0967a252012-09-14 10:43:54 -07003378 if (nextBB != NULL) {
3379 bb = nextBB;
3380 nextBB = NULL;
buzbee6969d502012-06-15 16:40:31 -07003381 }
buzbee6969d502012-06-15 16:40:31 -07003382 }
buzbee2cfc6392012-05-07 14:51:40 -07003383 return false;
3384}
3385
3386/*
3387 * Convert LLVM_IR to MIR:
3388 * o Iterate through the LLVM_IR and construct a graph using
3389 * standard MIR building blocks.
3390 * o Perform a basic-block optimization pass to remove unnecessary
3391 * store/load sequences.
3392 * o Convert the LLVM Value operands into RegLocations where applicable.
3393 * o Create ssaRep def/use operand arrays for each converted LLVM opcode
3394 * o Perform register promotion
3395 * o Iterate through the graph a basic block at a time, generating
3396 * LIR.
3397 * o Assemble LIR as usual.
3398 * o Profit.
3399 */
3400void oatMethodBitcode2LIR(CompilationUnit* cUnit)
3401{
buzbeead8f15e2012-06-18 14:49:45 -07003402 llvm::Function* func = cUnit->func;
3403 int numBasicBlocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003404 // Allocate a list for LIR basic block labels
3405 cUnit->blockLabelList =
buzbeea1da8a52012-07-09 14:00:21 -07003406 (LIR*)oatNew(cUnit, sizeof(LIR) * numBasicBlocks, true, kAllocLIR);
3407 LIR* labelList = cUnit->blockLabelList;
buzbee2cfc6392012-05-07 14:51:40 -07003408 int nextLabel = 0;
buzbeead8f15e2012-06-18 14:49:45 -07003409 for (llvm::Function::iterator i = func->begin(),
3410 e = func->end(); i != e; ++i) {
buzbee2cfc6392012-05-07 14:51:40 -07003411 cUnit->blockToLabelMap.Put(static_cast<llvm::BasicBlock*>(i),
3412 &labelList[nextLabel++]);
3413 }
buzbeead8f15e2012-06-18 14:49:45 -07003414
3415 /*
3416 * Keep honest - clear regLocations, Value => RegLocation,
3417 * promotion map and VmapTables.
3418 */
3419 cUnit->locMap.clear(); // Start fresh
3420 cUnit->regLocation = NULL;
3421 for (int i = 0; i < cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
3422 i++) {
3423 cUnit->promotionMap[i].coreLocation = kLocDalvikFrame;
3424 cUnit->promotionMap[i].fpLocation = kLocDalvikFrame;
3425 }
3426 cUnit->coreSpillMask = 0;
3427 cUnit->numCoreSpills = 0;
3428 cUnit->fpSpillMask = 0;
3429 cUnit->numFPSpills = 0;
3430 cUnit->coreVmapTable.clear();
3431 cUnit->fpVmapTable.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003432
3433 /*
3434 * At this point, we've lost all knowledge of register promotion.
3435 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003436 * exists - not required for correctness). Normally, this will
3437 * be the first instruction we encounter, so we won't have to iterate
3438 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003439 */
buzbeeca7a5e42012-08-20 11:12:18 -07003440 for (llvm::inst_iterator i = llvm::inst_begin(func),
3441 e = llvm::inst_end(func); i != e; ++i) {
3442 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(&*i);
3443 if (callInst != NULL) {
3444 llvm::Function* callee = callInst->getCalledFunction();
3445 greenland::IntrinsicHelper::IntrinsicId id =
3446 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3447 if (id == greenland::IntrinsicHelper::MethodInfo) {
3448 if (cUnit->printMe) {
3449 LOG(INFO) << "Found MethodInfo";
3450 }
3451 llvm::MDNode* regInfoNode = callInst->getMetadata("RegInfo");
3452 if (regInfoNode != NULL) {
3453 llvm::ConstantInt* numInsValue =
3454 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(0));
3455 llvm::ConstantInt* numRegsValue =
3456 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(1));
3457 llvm::ConstantInt* numOutsValue =
3458 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(2));
3459 llvm::ConstantInt* numCompilerTempsValue =
3460 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(3));
3461 llvm::ConstantInt* numSSARegsValue =
3462 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(4));
3463 if (cUnit->printMe) {
3464 LOG(INFO) << "RegInfo - Ins:" << numInsValue->getZExtValue()
3465 << ", Regs:" << numRegsValue->getZExtValue()
3466 << ", Outs:" << numOutsValue->getZExtValue()
3467 << ", CTemps:" << numCompilerTempsValue->getZExtValue()
3468 << ", SSARegs:" << numSSARegsValue->getZExtValue();
3469 }
3470 }
3471 llvm::MDNode* pmapInfoNode = callInst->getMetadata("PromotionMap");
3472 if (pmapInfoNode != NULL) {
3473 int elems = pmapInfoNode->getNumOperands();
3474 if (cUnit->printMe) {
3475 LOG(INFO) << "PMap size: " << elems;
3476 }
3477 for (int i = 0; i < elems; i++) {
3478 llvm::ConstantInt* rawMapData =
3479 static_cast<llvm::ConstantInt*>(pmapInfoNode->getOperand(i));
3480 uint32_t mapData = rawMapData->getZExtValue();
3481 PromotionMap* p = &cUnit->promotionMap[i];
3482 p->firstInPair = (mapData >> 24) & 0xff;
3483 p->fpReg = (mapData >> 16) & 0xff;
3484 p->coreReg = (mapData >> 8) & 0xff;
3485 p->fpLocation = static_cast<RegLocationType>((mapData >> 4) & 0xf);
3486 if (p->fpLocation == kLocPhysReg) {
3487 oatRecordFpPromotion(cUnit, p->fpReg, i);
3488 }
3489 p->coreLocation = static_cast<RegLocationType>(mapData & 0xf);
3490 if (p->coreLocation == kLocPhysReg) {
3491 oatRecordCorePromotion(cUnit, p->coreReg, i);
3492 }
3493 }
3494 if (cUnit->printMe) {
3495 oatDumpPromotionMap(cUnit);
3496 }
3497 }
3498 break;
3499 }
3500 }
3501 }
3502 oatAdjustSpillMask(cUnit);
3503 cUnit->frameSize = oatComputeFrameSize(cUnit);
buzbeead8f15e2012-06-18 14:49:45 -07003504
3505 // Create RegLocations for arguments
3506 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
3507 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
3508 for (; it != it_end; ++it) {
3509 llvm::Value* val = it;
3510 createLocFromValue(cUnit, val);
3511 }
3512 // Create RegLocations for all non-argument defintions
3513 for (llvm::inst_iterator i = llvm::inst_begin(func),
3514 e = llvm::inst_end(func); i != e; ++i) {
3515 llvm::Value* val = &*i;
3516 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
3517 createLocFromValue(cUnit, val);
3518 }
3519 }
3520
buzbee2cfc6392012-05-07 14:51:40 -07003521 // Walk the blocks, generating code.
3522 for (llvm::Function::iterator i = cUnit->func->begin(),
3523 e = cUnit->func->end(); i != e; ++i) {
3524 methodBitcodeBlockCodeGen(cUnit, static_cast<llvm::BasicBlock*>(i));
3525 }
3526
3527 handleSuspendLaunchpads(cUnit);
3528
3529 handleThrowLaunchpads(cUnit);
3530
3531 handleIntrinsicLaunchpads(cUnit);
3532
buzbee692be802012-08-29 15:52:59 -07003533 cUnit->func->eraseFromParent();
3534 cUnit->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003535}
3536
3537
3538} // namespace art