blob: 055a99d0ec24422b0913f41ab802d321afcc99b9 [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
17#if defined(ART_USE_QUICK_COMPILER)
18
19#include "object_utils.h"
20
21#include <llvm/Support/ToolOutputFile.h>
22#include <llvm/Bitcode/ReaderWriter.h>
23#include <llvm/Analysis/Verifier.h>
24#include <llvm/Metadata.h>
25#include <llvm/ADT/DepthFirstIterator.h>
26#include <llvm/Instruction.h>
27#include <llvm/Type.h>
28#include <llvm/Instructions.h>
29#include <llvm/Support/Casting.h>
buzbeead8f15e2012-06-18 14:49:45 -070030#include <llvm/Support/InstIterator.h>
buzbee2cfc6392012-05-07 14:51:40 -070031
buzbee8320f382012-09-11 16:29:42 -070032static const char* kLabelFormat = "%c0x%x_%d";
33static const char kNormalBlock = 'L';
34static const char kCatchBlock = 'C';
buzbee2cfc6392012-05-07 14:51:40 -070035
36namespace art {
37extern const RegLocation badLoc;
buzbeeb03f4872012-06-11 15:22:11 -070038RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val);
buzbee2cfc6392012-05-07 14:51:40 -070039
40llvm::BasicBlock* getLLVMBlock(CompilationUnit* cUnit, int id)
41{
42 return cUnit->idToBlockMap.Get(id);
43}
44
45llvm::Value* getLLVMValue(CompilationUnit* cUnit, int sReg)
46{
47 return (llvm::Value*)oatGrowableListGetElement(&cUnit->llvmValues, sReg);
48}
49
50// Replace the placeholder value with the real definition
51void defineValue(CompilationUnit* cUnit, llvm::Value* val, int sReg)
52{
53 llvm::Value* placeholder = getLLVMValue(cUnit, sReg);
buzbee9a2487f2012-07-26 14:01:13 -070054 if (placeholder == NULL) {
55 // This can happen on instruction rewrite on verification failure
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070056 LOG(WARNING) << "Null placeholder";
buzbee9a2487f2012-07-26 14:01:13 -070057 return;
58 }
buzbee2cfc6392012-05-07 14:51:40 -070059 placeholder->replaceAllUsesWith(val);
60 val->takeName(placeholder);
61 cUnit->llvmValues.elemList[sReg] = (intptr_t)val;
buzbee4be777b2012-07-12 14:38:18 -070062 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(placeholder);
63 DCHECK(inst != NULL);
64 inst->eraseFromParent();
buzbee2cfc6392012-05-07 14:51:40 -070065}
66
67llvm::Type* llvmTypeFromLocRec(CompilationUnit* cUnit, RegLocation loc)
68{
69 llvm::Type* res = NULL;
70 if (loc.wide) {
71 if (loc.fp)
buzbee4f1181f2012-06-22 13:52:12 -070072 res = cUnit->irb->getDoubleTy();
buzbee2cfc6392012-05-07 14:51:40 -070073 else
buzbee4f1181f2012-06-22 13:52:12 -070074 res = cUnit->irb->getInt64Ty();
buzbee2cfc6392012-05-07 14:51:40 -070075 } else {
76 if (loc.fp) {
buzbee4f1181f2012-06-22 13:52:12 -070077 res = cUnit->irb->getFloatTy();
buzbee2cfc6392012-05-07 14:51:40 -070078 } else {
79 if (loc.ref)
80 res = cUnit->irb->GetJObjectTy();
81 else
buzbee4f1181f2012-06-22 13:52:12 -070082 res = cUnit->irb->getInt32Ty();
buzbee2cfc6392012-05-07 14:51:40 -070083 }
84 }
85 return res;
86}
87
buzbeead8f15e2012-06-18 14:49:45 -070088/* Create an in-memory RegLocation from an llvm Value. */
89void createLocFromValue(CompilationUnit* cUnit, llvm::Value* val)
90{
91 // NOTE: llvm takes shortcuts with c_str() - get to std::string firstt
92 std::string s(val->getName().str());
93 const char* valName = s.c_str();
buzbeead8f15e2012-06-18 14:49:45 -070094 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
95 DCHECK(it == cUnit->locMap.end()) << " - already defined: " << valName;
96 int baseSReg = INVALID_SREG;
97 int subscript = -1;
98 sscanf(valName, "v%d_%d", &baseSReg, &subscript);
99 if ((baseSReg == INVALID_SREG) && (!strcmp(valName, "method"))) {
100 baseSReg = SSA_METHOD_BASEREG;
101 subscript = 0;
102 }
buzbeead8f15e2012-06-18 14:49:45 -0700103 DCHECK_NE(baseSReg, INVALID_SREG);
104 DCHECK_NE(subscript, -1);
105 // TODO: redo during C++'ification
106 RegLocation loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, INVALID_REG,
107 INVALID_REG, INVALID_SREG, INVALID_SREG};
108 llvm::Type* ty = val->getType();
109 loc.wide = ((ty == cUnit->irb->getInt64Ty()) ||
110 (ty == cUnit->irb->getDoubleTy()));
111 loc.defined = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700112 loc.home = false; // May change during promotion
buzbeead8f15e2012-06-18 14:49:45 -0700113 loc.sRegLow = baseSReg;
114 loc.origSReg = cUnit->locMap.size();
buzbeeca7a5e42012-08-20 11:12:18 -0700115 PromotionMap pMap = cUnit->promotionMap[baseSReg];
116 if (ty == cUnit->irb->getFloatTy()) {
117 loc.fp = true;
118 if (pMap.fpLocation == kLocPhysReg) {
119 loc.lowReg = pMap.fpReg;
120 loc.location = kLocPhysReg;
121 loc.home = true;
122 }
123 } else if (ty == cUnit->irb->getDoubleTy()) {
124 loc.fp = true;
125 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
126 if ((pMap.fpLocation == kLocPhysReg) &&
127 (pMapHigh.fpLocation == kLocPhysReg) &&
128 ((pMap.fpReg & 0x1) == 0) &&
129 (pMap.fpReg + 1 == pMapHigh.fpReg)) {
130 loc.lowReg = pMap.fpReg;
131 loc.highReg = pMapHigh.fpReg;
132 loc.location = kLocPhysReg;
133 loc.home = true;
134 }
135 } else if (ty == cUnit->irb->GetJObjectTy()) {
136 loc.ref = true;
137 if (pMap.coreLocation == kLocPhysReg) {
138 loc.lowReg = pMap.coreReg;
139 loc.location = kLocPhysReg;
140 loc.home = true;
141 }
142 } else if (ty == cUnit->irb->getInt64Ty()) {
143 loc.core = true;
144 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
145 if ((pMap.coreLocation == kLocPhysReg) &&
146 (pMapHigh.coreLocation == kLocPhysReg)) {
147 loc.lowReg = pMap.coreReg;
148 loc.highReg = pMapHigh.coreReg;
149 loc.location = kLocPhysReg;
150 loc.home = true;
151 }
152 } else {
153 loc.core = true;
154 if (pMap.coreLocation == kLocPhysReg) {
155 loc.lowReg = pMap.coreReg;
156 loc.location = kLocPhysReg;
157 loc.home = true;
158 }
159 }
160
161 if (cUnit->printMe && loc.home) {
162 if (loc.wide) {
buzbee0967a252012-09-14 10:43:54 -0700163 LOG(INFO) << "Promoted wide " << s << " to regs " << static_cast<int>(loc.lowReg)
buzbeeca7a5e42012-08-20 11:12:18 -0700164 << "/" << loc.highReg;
165 } else {
buzbee0967a252012-09-14 10:43:54 -0700166 LOG(INFO) << "Promoted " << s << " to reg " << static_cast<int>(loc.lowReg);
buzbeeca7a5e42012-08-20 11:12:18 -0700167 }
168 }
buzbeead8f15e2012-06-18 14:49:45 -0700169 cUnit->locMap.Put(val, loc);
170}
buzbee2cfc6392012-05-07 14:51:40 -0700171void initIR(CompilationUnit* cUnit)
172{
buzbee692be802012-08-29 15:52:59 -0700173 QuickCompiler* quick =
174 reinterpret_cast<QuickCompiler*>(cUnit->compiler->GetCompilerContext());
175 cUnit->context = quick->GetLLVMContext();
176 cUnit->module = quick->GetLLVMModule();
177 cUnit->intrinsic_helper = quick->GetIntrinsicHelper();
178 cUnit->irb = quick->GetIRBuilder();
buzbee2cfc6392012-05-07 14:51:40 -0700179}
180
181const char* llvmSSAName(CompilationUnit* cUnit, int ssaReg) {
182 return GET_ELEM_N(cUnit->ssaStrings, char*, ssaReg);
183}
184
buzbeef58c12c2012-07-03 15:06:29 -0700185llvm::BasicBlock* findCaseTarget(CompilationUnit* cUnit, uint32_t vaddr)
186{
187 BasicBlock* bb = oatFindBlock(cUnit, vaddr);
188 DCHECK(bb != NULL);
189 return getLLVMBlock(cUnit, bb->id);
190}
191
192void convertPackedSwitch(CompilationUnit* cUnit, BasicBlock* bb,
193 int32_t tableOffset, RegLocation rlSrc)
194{
195 const Instruction::PackedSwitchPayload* payload =
196 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
197 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
198
199 llvm::Value* value = getLLVMValue(cUnit, rlSrc.origSReg);
200
201 llvm::SwitchInst* sw =
202 cUnit->irb->CreateSwitch(value, getLLVMBlock(cUnit, bb->fallThrough->id),
203 payload->case_count);
204
205 for (uint16_t i = 0; i < payload->case_count; ++i) {
206 llvm::BasicBlock* llvmBB =
207 findCaseTarget(cUnit, cUnit->currentDalvikOffset + payload->targets[i]);
208 sw->addCase(cUnit->irb->getInt32(payload->first_key + i), llvmBB);
209 }
210 llvm::MDNode* switchNode =
211 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
212 sw->setMetadata("SwitchTable", switchNode);
213 bb->taken = NULL;
214 bb->fallThrough = NULL;
215}
216
buzbeea1da8a52012-07-09 14:00:21 -0700217void convertSparseSwitch(CompilationUnit* cUnit, BasicBlock* bb,
218 int32_t tableOffset, RegLocation rlSrc)
219{
220 const Instruction::SparseSwitchPayload* payload =
221 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
222 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
223
224 const int32_t* keys = payload->GetKeys();
225 const int32_t* targets = payload->GetTargets();
226
227 llvm::Value* value = getLLVMValue(cUnit, rlSrc.origSReg);
228
229 llvm::SwitchInst* sw =
230 cUnit->irb->CreateSwitch(value, getLLVMBlock(cUnit, bb->fallThrough->id),
231 payload->case_count);
232
233 for (size_t i = 0; i < payload->case_count; ++i) {
234 llvm::BasicBlock* llvmBB =
235 findCaseTarget(cUnit, cUnit->currentDalvikOffset + targets[i]);
236 sw->addCase(cUnit->irb->getInt32(keys[i]), llvmBB);
237 }
238 llvm::MDNode* switchNode =
239 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
240 sw->setMetadata("SwitchTable", switchNode);
241 bb->taken = NULL;
242 bb->fallThrough = NULL;
243}
244
buzbee8fa0fda2012-06-27 15:44:52 -0700245void convertSget(CompilationUnit* cUnit, int32_t fieldIndex,
246 greenland::IntrinsicHelper::IntrinsicId id,
247 RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700248{
buzbee8fa0fda2012-06-27 15:44:52 -0700249 llvm::Constant* fieldIdx = cUnit->irb->getInt32(fieldIndex);
buzbee4f1181f2012-06-22 13:52:12 -0700250 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700251 llvm::Value* res = cUnit->irb->CreateCall(intr, fieldIdx);
252 defineValue(cUnit, res, rlDest.origSReg);
253}
254
255void convertSput(CompilationUnit* cUnit, int32_t fieldIndex,
256 greenland::IntrinsicHelper::IntrinsicId id,
257 RegLocation rlSrc)
258{
259 llvm::SmallVector<llvm::Value*, 2> args;
260 args.push_back(cUnit->irb->getInt32(fieldIndex));
261 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
262 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
263 cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700264}
265
buzbee101305f2012-06-28 18:00:56 -0700266void convertFillArrayData(CompilationUnit* cUnit, int32_t offset,
267 RegLocation rlArray)
268{
269 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700270 id = greenland::IntrinsicHelper::HLFillArrayData;
buzbee101305f2012-06-28 18:00:56 -0700271 llvm::SmallVector<llvm::Value*, 2> args;
272 args.push_back(cUnit->irb->getInt32(offset));
273 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
274 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
275 cUnit->irb->CreateCall(intr, args);
276}
277
buzbee2cfc6392012-05-07 14:51:40 -0700278llvm::Value* emitConst(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
279 RegLocation loc)
280{
281 greenland::IntrinsicHelper::IntrinsicId id;
282 if (loc.wide) {
283 if (loc.fp) {
284 id = greenland::IntrinsicHelper::ConstDouble;
285 } else {
286 id = greenland::IntrinsicHelper::ConstLong;
287 }
288 } else {
289 if (loc.fp) {
290 id = greenland::IntrinsicHelper::ConstFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700291 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700292 id = greenland::IntrinsicHelper::ConstObj;
293 } else {
294 id = greenland::IntrinsicHelper::ConstInt;
295 }
296 }
297 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
298 return cUnit->irb->CreateCall(intr, src);
299}
buzbeeb03f4872012-06-11 15:22:11 -0700300
301void emitPopShadowFrame(CompilationUnit* cUnit)
302{
303 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
304 greenland::IntrinsicHelper::PopShadowFrame);
305 cUnit->irb->CreateCall(intr);
306}
307
buzbee2cfc6392012-05-07 14:51:40 -0700308llvm::Value* emitCopy(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
309 RegLocation loc)
310{
311 greenland::IntrinsicHelper::IntrinsicId id;
312 if (loc.wide) {
313 if (loc.fp) {
314 id = greenland::IntrinsicHelper::CopyDouble;
315 } else {
316 id = greenland::IntrinsicHelper::CopyLong;
317 }
318 } else {
319 if (loc.fp) {
320 id = greenland::IntrinsicHelper::CopyFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700321 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700322 id = greenland::IntrinsicHelper::CopyObj;
323 } else {
324 id = greenland::IntrinsicHelper::CopyInt;
325 }
326 }
327 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
328 return cUnit->irb->CreateCall(intr, src);
329}
330
buzbee32412962012-06-26 16:27:56 -0700331void convertMoveException(CompilationUnit* cUnit, RegLocation rlDest)
332{
333 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
334 greenland::IntrinsicHelper::GetException);
335 llvm::Value* res = cUnit->irb->CreateCall(func);
336 defineValue(cUnit, res, rlDest.origSReg);
337}
338
339void convertThrow(CompilationUnit* cUnit, RegLocation rlSrc)
340{
341 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
342 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
TDYa127f71bf5a2012-07-29 20:09:52 -0700343 greenland::IntrinsicHelper::HLThrowException);
buzbee32412962012-06-26 16:27:56 -0700344 cUnit->irb->CreateCall(func, src);
buzbee32412962012-06-26 16:27:56 -0700345}
346
buzbee8fa0fda2012-06-27 15:44:52 -0700347void convertMonitorEnterExit(CompilationUnit* cUnit, int optFlags,
348 greenland::IntrinsicHelper::IntrinsicId id,
349 RegLocation rlSrc)
350{
351 llvm::SmallVector<llvm::Value*, 2> args;
352 args.push_back(cUnit->irb->getInt32(optFlags));
353 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
354 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
355 cUnit->irb->CreateCall(func, args);
356}
357
buzbee76592632012-06-29 15:18:35 -0700358void convertArrayLength(CompilationUnit* cUnit, int optFlags,
359 RegLocation rlDest, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700360{
361 llvm::SmallVector<llvm::Value*, 2> args;
362 args.push_back(cUnit->irb->getInt32(optFlags));
363 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
364 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700365 greenland::IntrinsicHelper::OptArrayLength);
buzbee76592632012-06-29 15:18:35 -0700366 llvm::Value* res = cUnit->irb->CreateCall(func, args);
367 defineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700368}
369
buzbee2cfc6392012-05-07 14:51:40 -0700370void emitSuspendCheck(CompilationUnit* cUnit)
371{
372 greenland::IntrinsicHelper::IntrinsicId id =
373 greenland::IntrinsicHelper::CheckSuspend;
374 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
375 cUnit->irb->CreateCall(intr);
376}
377
378llvm::Value* convertCompare(CompilationUnit* cUnit, ConditionCode cc,
379 llvm::Value* src1, llvm::Value* src2)
380{
381 llvm::Value* res = NULL;
buzbee76592632012-06-29 15:18:35 -0700382 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700383 switch(cc) {
384 case kCondEq: res = cUnit->irb->CreateICmpEQ(src1, src2); break;
385 case kCondNe: res = cUnit->irb->CreateICmpNE(src1, src2); break;
386 case kCondLt: res = cUnit->irb->CreateICmpSLT(src1, src2); break;
387 case kCondGe: res = cUnit->irb->CreateICmpSGE(src1, src2); break;
388 case kCondGt: res = cUnit->irb->CreateICmpSGT(src1, src2); break;
389 case kCondLe: res = cUnit->irb->CreateICmpSLE(src1, src2); break;
390 default: LOG(FATAL) << "Unexpected cc value " << cc;
391 }
392 return res;
393}
394
395void convertCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
396 ConditionCode cc, RegLocation rlSrc1,
397 RegLocation rlSrc2)
398{
399 if (bb->taken->startOffset <= mir->offset) {
400 emitSuspendCheck(cUnit);
401 }
402 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
403 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
404 llvm::Value* condValue = convertCompare(cUnit, cc, src1, src2);
405 condValue->setName(StringPrintf("t%d", cUnit->tempName++));
406 cUnit->irb->CreateCondBr(condValue, getLLVMBlock(cUnit, bb->taken->id),
407 getLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700408 // Don't redo the fallthrough branch in the BB driver
409 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700410}
411
412void convertCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb,
413 MIR* mir, ConditionCode cc, RegLocation rlSrc1)
414{
415 if (bb->taken->startOffset <= mir->offset) {
416 emitSuspendCheck(cUnit);
417 }
418 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
419 llvm::Value* src2;
420 if (rlSrc1.ref) {
421 src2 = cUnit->irb->GetJNull();
422 } else {
423 src2 = cUnit->irb->getInt32(0);
424 }
425 llvm::Value* condValue = convertCompare(cUnit, cc, src1, src2);
buzbee2cfc6392012-05-07 14:51:40 -0700426 cUnit->irb->CreateCondBr(condValue, getLLVMBlock(cUnit, bb->taken->id),
427 getLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700428 // Don't redo the fallthrough branch in the BB driver
429 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700430}
431
432llvm::Value* genDivModOp(CompilationUnit* cUnit, bool isDiv, bool isLong,
433 llvm::Value* src1, llvm::Value* src2)
434{
435 greenland::IntrinsicHelper::IntrinsicId id;
436 if (isLong) {
437 if (isDiv) {
438 id = greenland::IntrinsicHelper::DivLong;
439 } else {
440 id = greenland::IntrinsicHelper::RemLong;
441 }
Logan Chien554e6072012-07-23 20:00:01 -0700442 } else {
443 if (isDiv) {
buzbee2cfc6392012-05-07 14:51:40 -0700444 id = greenland::IntrinsicHelper::DivInt;
445 } else {
446 id = greenland::IntrinsicHelper::RemInt;
Logan Chien554e6072012-07-23 20:00:01 -0700447 }
buzbee2cfc6392012-05-07 14:51:40 -0700448 }
449 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
450 llvm::SmallVector<llvm::Value*, 2>args;
451 args.push_back(src1);
452 args.push_back(src2);
453 return cUnit->irb->CreateCall(intr, args);
454}
455
456llvm::Value* genArithOp(CompilationUnit* cUnit, OpKind op, bool isLong,
457 llvm::Value* src1, llvm::Value* src2)
458{
459 llvm::Value* res = NULL;
460 switch(op) {
461 case kOpAdd: res = cUnit->irb->CreateAdd(src1, src2); break;
462 case kOpSub: res = cUnit->irb->CreateSub(src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700463 case kOpRsub: res = cUnit->irb->CreateSub(src2, src1); break;
buzbee2cfc6392012-05-07 14:51:40 -0700464 case kOpMul: res = cUnit->irb->CreateMul(src1, src2); break;
465 case kOpOr: res = cUnit->irb->CreateOr(src1, src2); break;
466 case kOpAnd: res = cUnit->irb->CreateAnd(src1, src2); break;
467 case kOpXor: res = cUnit->irb->CreateXor(src1, src2); break;
468 case kOpDiv: res = genDivModOp(cUnit, true, isLong, src1, src2); break;
469 case kOpRem: res = genDivModOp(cUnit, false, isLong, src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700470 case kOpLsl: res = cUnit->irb->CreateShl(src1, src2); break;
471 case kOpLsr: res = cUnit->irb->CreateLShr(src1, src2); break;
472 case kOpAsr: res = cUnit->irb->CreateAShr(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700473 default:
474 LOG(FATAL) << "Invalid op " << op;
475 }
476 return res;
477}
478
479void convertFPArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
480 RegLocation rlSrc1, RegLocation rlSrc2)
481{
482 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
483 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
484 llvm::Value* res = NULL;
485 switch(op) {
486 case kOpAdd: res = cUnit->irb->CreateFAdd(src1, src2); break;
487 case kOpSub: res = cUnit->irb->CreateFSub(src1, src2); break;
488 case kOpMul: res = cUnit->irb->CreateFMul(src1, src2); break;
489 case kOpDiv: res = cUnit->irb->CreateFDiv(src1, src2); break;
490 case kOpRem: res = cUnit->irb->CreateFRem(src1, src2); break;
491 default:
492 LOG(FATAL) << "Invalid op " << op;
493 }
494 defineValue(cUnit, res, rlDest.origSReg);
495}
496
buzbee2a83e8f2012-07-13 16:42:30 -0700497void convertShift(CompilationUnit* cUnit,
498 greenland::IntrinsicHelper::IntrinsicId id,
499 RegLocation rlDest, RegLocation rlSrc1, RegLocation rlSrc2)
buzbee4f1181f2012-06-22 13:52:12 -0700500{
buzbee2a83e8f2012-07-13 16:42:30 -0700501 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
502 llvm::SmallVector<llvm::Value*, 2>args;
503 args.push_back(getLLVMValue(cUnit, rlSrc1.origSReg));
504 args.push_back(getLLVMValue(cUnit, rlSrc2.origSReg));
505 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
506 defineValue(cUnit, res, rlDest.origSReg);
507}
508
509void convertShiftLit(CompilationUnit* cUnit,
510 greenland::IntrinsicHelper::IntrinsicId id,
511 RegLocation rlDest, RegLocation rlSrc, int shiftAmount)
512{
513 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
514 llvm::SmallVector<llvm::Value*, 2>args;
515 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
516 args.push_back(cUnit->irb->getInt32(shiftAmount));
517 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700518 defineValue(cUnit, res, rlDest.origSReg);
519}
520
buzbee2cfc6392012-05-07 14:51:40 -0700521void convertArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
522 RegLocation rlSrc1, RegLocation rlSrc2)
523{
524 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
525 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
buzbee4f4dfc72012-07-02 14:54:44 -0700526 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700527 llvm::Value* res = genArithOp(cUnit, op, rlDest.wide, src1, src2);
528 defineValue(cUnit, res, rlDest.origSReg);
529}
530
buzbeeb03f4872012-06-11 15:22:11 -0700531void setShadowFrameEntry(CompilationUnit* cUnit, llvm::Value* newVal)
532{
533 int index = -1;
534 DCHECK(newVal != NULL);
535 int vReg = SRegToVReg(cUnit, getLoc(cUnit, newVal).origSReg);
536 for (int i = 0; i < cUnit->numShadowFrameEntries; i++) {
537 if (cUnit->shadowMap[i] == vReg) {
538 index = i;
539 break;
540 }
541 }
Elliott Hughes74847412012-06-20 18:10:21 -0700542 DCHECK_NE(index, -1) << "Corrupt shadowMap";
buzbeeb03f4872012-06-11 15:22:11 -0700543 greenland::IntrinsicHelper::IntrinsicId id =
544 greenland::IntrinsicHelper::SetShadowFrameEntry;
545 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
546 llvm::Value* tableSlot = cUnit->irb->getInt32(index);
547 llvm::Value* args[] = { newVal, tableSlot };
548 cUnit->irb->CreateCall(func, args);
549}
550
buzbee2cfc6392012-05-07 14:51:40 -0700551void convertArithOpLit(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
552 RegLocation rlSrc1, int32_t imm)
553{
554 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
555 llvm::Value* src2 = cUnit->irb->getInt32(imm);
556 llvm::Value* res = genArithOp(cUnit, op, rlDest.wide, src1, src2);
557 defineValue(cUnit, res, rlDest.origSReg);
558}
559
buzbee101305f2012-06-28 18:00:56 -0700560/*
561 * Process arguments for invoke. Note: this code is also used to
562 * collect and process arguments for NEW_FILLED_ARRAY and NEW_FILLED_ARRAY_RANGE.
563 * The requirements are similar.
564 */
buzbee6969d502012-06-15 16:40:31 -0700565void convertInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
buzbee76592632012-06-29 15:18:35 -0700566 InvokeType invokeType, bool isRange, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -0700567{
568 CallInfo* info = oatNewCallInfo(cUnit, bb, mir, invokeType, isRange);
569 llvm::SmallVector<llvm::Value*, 10> args;
570 // Insert the invokeType
571 args.push_back(cUnit->irb->getInt32(static_cast<int>(invokeType)));
572 // Insert the method_idx
573 args.push_back(cUnit->irb->getInt32(info->index));
574 // Insert the optimization flags
575 args.push_back(cUnit->irb->getInt32(info->optFlags));
576 // Now, insert the actual arguments
buzbee6969d502012-06-15 16:40:31 -0700577 for (int i = 0; i < info->numArgWords;) {
buzbee6969d502012-06-15 16:40:31 -0700578 llvm::Value* val = getLLVMValue(cUnit, info->args[i].origSReg);
579 args.push_back(val);
580 i += info->args[i].wide ? 2 : 1;
581 }
582 /*
583 * Choose the invoke return type based on actual usage. Note: may
584 * be different than shorty. For example, if a function return value
585 * is not used, we'll treat this as a void invoke.
586 */
587 greenland::IntrinsicHelper::IntrinsicId id;
buzbee76592632012-06-29 15:18:35 -0700588 if (isFilledNewArray) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700589 id = greenland::IntrinsicHelper::HLFilledNewArray;
buzbee101305f2012-06-28 18:00:56 -0700590 } else if (info->result.location == kLocInvalid) {
buzbee6969d502012-06-15 16:40:31 -0700591 id = greenland::IntrinsicHelper::HLInvokeVoid;
592 } else {
593 if (info->result.wide) {
594 if (info->result.fp) {
595 id = greenland::IntrinsicHelper::HLInvokeDouble;
596 } else {
buzbee8fa0fda2012-06-27 15:44:52 -0700597 id = greenland::IntrinsicHelper::HLInvokeLong;
buzbee6969d502012-06-15 16:40:31 -0700598 }
599 } else if (info->result.ref) {
600 id = greenland::IntrinsicHelper::HLInvokeObj;
601 } else if (info->result.fp) {
602 id = greenland::IntrinsicHelper::HLInvokeFloat;
603 } else {
604 id = greenland::IntrinsicHelper::HLInvokeInt;
605 }
606 }
607 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
608 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
609 if (info->result.location != kLocInvalid) {
610 defineValue(cUnit, res, info->result.origSReg);
611 }
612}
613
buzbee101305f2012-06-28 18:00:56 -0700614void convertConstObject(CompilationUnit* cUnit, uint32_t idx,
615 greenland::IntrinsicHelper::IntrinsicId id,
616 RegLocation rlDest)
buzbee6969d502012-06-15 16:40:31 -0700617{
buzbee6969d502012-06-15 16:40:31 -0700618 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee101305f2012-06-28 18:00:56 -0700619 llvm::Value* index = cUnit->irb->getInt32(idx);
buzbee6969d502012-06-15 16:40:31 -0700620 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
621 defineValue(cUnit, res, rlDest.origSReg);
622}
623
buzbee101305f2012-06-28 18:00:56 -0700624void convertCheckCast(CompilationUnit* cUnit, uint32_t type_idx,
625 RegLocation rlSrc)
626{
627 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700628 id = greenland::IntrinsicHelper::HLCheckCast;
buzbee101305f2012-06-28 18:00:56 -0700629 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
630 llvm::SmallVector<llvm::Value*, 2> args;
631 args.push_back(cUnit->irb->getInt32(type_idx));
632 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
633 cUnit->irb->CreateCall(intr, args);
634}
635
buzbee8fa0fda2012-06-27 15:44:52 -0700636void convertNewInstance(CompilationUnit* cUnit, uint32_t type_idx,
637 RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700638{
639 greenland::IntrinsicHelper::IntrinsicId id;
640 id = greenland::IntrinsicHelper::NewInstance;
641 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
642 llvm::Value* index = cUnit->irb->getInt32(type_idx);
643 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
644 defineValue(cUnit, res, rlDest.origSReg);
645}
646
buzbee8fa0fda2012-06-27 15:44:52 -0700647void convertNewArray(CompilationUnit* cUnit, uint32_t type_idx,
648 RegLocation rlDest, RegLocation rlSrc)
649{
650 greenland::IntrinsicHelper::IntrinsicId id;
651 id = greenland::IntrinsicHelper::NewArray;
652 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
653 llvm::SmallVector<llvm::Value*, 2> args;
654 args.push_back(cUnit->irb->getInt32(type_idx));
655 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
656 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
657 defineValue(cUnit, res, rlDest.origSReg);
658}
659
660void convertAget(CompilationUnit* cUnit, int optFlags,
661 greenland::IntrinsicHelper::IntrinsicId id,
662 RegLocation rlDest, RegLocation rlArray, RegLocation rlIndex)
663{
664 llvm::SmallVector<llvm::Value*, 3> args;
665 args.push_back(cUnit->irb->getInt32(optFlags));
666 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
667 args.push_back(getLLVMValue(cUnit, rlIndex.origSReg));
668 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
669 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
670 defineValue(cUnit, res, rlDest.origSReg);
671}
672
673void convertAput(CompilationUnit* cUnit, int optFlags,
674 greenland::IntrinsicHelper::IntrinsicId id,
675 RegLocation rlSrc, RegLocation rlArray, RegLocation rlIndex)
676{
677 llvm::SmallVector<llvm::Value*, 4> args;
678 args.push_back(cUnit->irb->getInt32(optFlags));
679 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
680 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
681 args.push_back(getLLVMValue(cUnit, rlIndex.origSReg));
682 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
683 cUnit->irb->CreateCall(intr, args);
684}
685
buzbee101305f2012-06-28 18:00:56 -0700686void convertIget(CompilationUnit* cUnit, int optFlags,
687 greenland::IntrinsicHelper::IntrinsicId id,
688 RegLocation rlDest, RegLocation rlObj, int fieldIndex)
689{
690 llvm::SmallVector<llvm::Value*, 3> args;
691 args.push_back(cUnit->irb->getInt32(optFlags));
692 args.push_back(getLLVMValue(cUnit, rlObj.origSReg));
693 args.push_back(cUnit->irb->getInt32(fieldIndex));
694 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
695 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
696 defineValue(cUnit, res, rlDest.origSReg);
697}
698
699void convertIput(CompilationUnit* cUnit, int optFlags,
700 greenland::IntrinsicHelper::IntrinsicId id,
701 RegLocation rlSrc, RegLocation rlObj, int fieldIndex)
702{
703 llvm::SmallVector<llvm::Value*, 4> args;
704 args.push_back(cUnit->irb->getInt32(optFlags));
705 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
706 args.push_back(getLLVMValue(cUnit, rlObj.origSReg));
707 args.push_back(cUnit->irb->getInt32(fieldIndex));
708 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
709 cUnit->irb->CreateCall(intr, args);
710}
711
buzbee8fa0fda2012-06-27 15:44:52 -0700712void convertInstanceOf(CompilationUnit* cUnit, uint32_t type_idx,
713 RegLocation rlDest, RegLocation rlSrc)
714{
715 greenland::IntrinsicHelper::IntrinsicId id;
716 id = greenland::IntrinsicHelper::InstanceOf;
717 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
718 llvm::SmallVector<llvm::Value*, 2> args;
719 args.push_back(cUnit->irb->getInt32(type_idx));
720 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
721 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
722 defineValue(cUnit, res, rlDest.origSReg);
723}
724
buzbee101305f2012-06-28 18:00:56 -0700725void convertIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
726 RegLocation rlSrc)
727{
728 llvm::Value* res = cUnit->irb->CreateSExt(getLLVMValue(cUnit, rlSrc.origSReg),
729 cUnit->irb->getInt64Ty());
730 defineValue(cUnit, res, rlDest.origSReg);
731}
732
buzbee76592632012-06-29 15:18:35 -0700733void convertLongToInt(CompilationUnit* cUnit, RegLocation rlDest,
734 RegLocation rlSrc)
735{
736 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
737 llvm::Value* res = cUnit->irb->CreateTrunc(src, cUnit->irb->getInt32Ty());
738 defineValue(cUnit, res, rlDest.origSReg);
739}
740
741void convertFloatToDouble(CompilationUnit* cUnit, RegLocation rlDest,
742 RegLocation rlSrc)
743{
744 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
745 llvm::Value* res = cUnit->irb->CreateFPExt(src, cUnit->irb->getDoubleTy());
746 defineValue(cUnit, res, rlDest.origSReg);
747}
748
749void convertDoubleToFloat(CompilationUnit* cUnit, RegLocation rlDest,
750 RegLocation rlSrc)
751{
752 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
753 llvm::Value* res = cUnit->irb->CreateFPTrunc(src, cUnit->irb->getFloatTy());
754 defineValue(cUnit, res, rlDest.origSReg);
755}
756
757void convertWideComparison(CompilationUnit* cUnit,
758 greenland::IntrinsicHelper::IntrinsicId id,
759 RegLocation rlDest, RegLocation rlSrc1,
760 RegLocation rlSrc2)
761{
762 DCHECK_EQ(rlSrc1.fp, rlSrc2.fp);
763 DCHECK_EQ(rlSrc1.wide, rlSrc2.wide);
764 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
765 llvm::SmallVector<llvm::Value*, 2> args;
766 args.push_back(getLLVMValue(cUnit, rlSrc1.origSReg));
767 args.push_back(getLLVMValue(cUnit, rlSrc2.origSReg));
768 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
769 defineValue(cUnit, res, rlDest.origSReg);
770}
771
buzbee101305f2012-06-28 18:00:56 -0700772void convertIntNarrowing(CompilationUnit* cUnit, RegLocation rlDest,
773 RegLocation rlSrc,
774 greenland::IntrinsicHelper::IntrinsicId id)
775{
776 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700777 llvm::Value* res =
778 cUnit->irb->CreateCall(intr, getLLVMValue(cUnit, rlSrc.origSReg));
779 defineValue(cUnit, res, rlDest.origSReg);
780}
781
782void convertNeg(CompilationUnit* cUnit, RegLocation rlDest,
783 RegLocation rlSrc)
784{
785 llvm::Value* res = cUnit->irb->CreateNeg(getLLVMValue(cUnit, rlSrc.origSReg));
786 defineValue(cUnit, res, rlDest.origSReg);
787}
788
789void convertIntToFP(CompilationUnit* cUnit, llvm::Type* ty, RegLocation rlDest,
790 RegLocation rlSrc)
791{
792 llvm::Value* res =
793 cUnit->irb->CreateSIToFP(getLLVMValue(cUnit, rlSrc.origSReg), ty);
794 defineValue(cUnit, res, rlDest.origSReg);
795}
796
TDYa1274ec8ccd2012-08-11 07:04:57 -0700797void convertFPToInt(CompilationUnit* cUnit,
798 greenland::IntrinsicHelper::IntrinsicId id,
799 RegLocation rlDest,
buzbee76592632012-06-29 15:18:35 -0700800 RegLocation rlSrc)
801{
TDYa1274ec8ccd2012-08-11 07:04:57 -0700802 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
803 llvm::Value* res = cUnit->irb->CreateCall(intr, getLLVMValue(cUnit, rlSrc.origSReg));
buzbee76592632012-06-29 15:18:35 -0700804 defineValue(cUnit, res, rlDest.origSReg);
805}
806
807
808void convertNegFP(CompilationUnit* cUnit, RegLocation rlDest,
809 RegLocation rlSrc)
810{
811 llvm::Value* res =
812 cUnit->irb->CreateFNeg(getLLVMValue(cUnit, rlSrc.origSReg));
813 defineValue(cUnit, res, rlDest.origSReg);
814}
815
816void convertNot(CompilationUnit* cUnit, RegLocation rlDest,
817 RegLocation rlSrc)
818{
819 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
820 llvm::Value* res = cUnit->irb->CreateXor(src, static_cast<uint64_t>(-1));
buzbee101305f2012-06-28 18:00:56 -0700821 defineValue(cUnit, res, rlDest.origSReg);
822}
823
buzbee2cfc6392012-05-07 14:51:40 -0700824/*
825 * Target-independent code generation. Use only high-level
826 * load/store utilities here, or target-dependent genXX() handlers
827 * when necessary.
828 */
829bool convertMIRNode(CompilationUnit* cUnit, MIR* mir, BasicBlock* bb,
830 llvm::BasicBlock* llvmBB, LIR* labelList)
831{
832 bool res = false; // Assume success
833 RegLocation rlSrc[3];
834 RegLocation rlDest = badLoc;
buzbee2cfc6392012-05-07 14:51:40 -0700835 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee6969d502012-06-15 16:40:31 -0700836 uint32_t vB = mir->dalvikInsn.vB;
837 uint32_t vC = mir->dalvikInsn.vC;
buzbee8fa0fda2012-06-27 15:44:52 -0700838 int optFlags = mir->optimizationFlags;
buzbee6969d502012-06-15 16:40:31 -0700839
buzbeeb03f4872012-06-11 15:22:11 -0700840 bool objectDefinition = false;
buzbee2cfc6392012-05-07 14:51:40 -0700841
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700842 if (cUnit->printMe) {
843 if ((int)opcode < kMirOpFirst) {
844 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x"
845 << std::hex << (int)opcode;
846 } else {
847 LOG(INFO) << ".. opcode 0x" << std::hex << (int)opcode;
848 }
849 }
850
buzbee2cfc6392012-05-07 14:51:40 -0700851 /* Prep Src and Dest locations */
852 int nextSreg = 0;
853 int nextLoc = 0;
854 int attrs = oatDataFlowAttributes[opcode];
855 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
856 if (attrs & DF_UA) {
857 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700858 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700859 nextSreg+= 2;
860 } else {
861 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
862 nextSreg++;
863 }
864 }
865 if (attrs & DF_UB) {
866 if (attrs & DF_B_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700867 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700868 nextSreg+= 2;
869 } else {
870 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
871 nextSreg++;
872 }
873 }
874 if (attrs & DF_UC) {
875 if (attrs & DF_C_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700876 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700877 } else {
878 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
879 }
880 }
881 if (attrs & DF_DA) {
882 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700883 rlDest = oatGetDestWide(cUnit, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700884 } else {
buzbee15bf9802012-06-12 17:49:27 -0700885 rlDest = oatGetDest(cUnit, mir);
buzbeeb03f4872012-06-11 15:22:11 -0700886 if (rlDest.ref) {
887 objectDefinition = true;
888 }
buzbee2cfc6392012-05-07 14:51:40 -0700889 }
890 }
891
892 switch (opcode) {
893 case Instruction::NOP:
894 break;
895
896 case Instruction::MOVE:
897 case Instruction::MOVE_OBJECT:
898 case Instruction::MOVE_16:
899 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700900 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700901 case Instruction::MOVE_FROM16:
902 case Instruction::MOVE_WIDE:
903 case Instruction::MOVE_WIDE_16:
904 case Instruction::MOVE_WIDE_FROM16: {
905 /*
906 * Moves/copies are meaningless in pure SSA register form,
907 * but we need to preserve them for the conversion back into
908 * MIR (at least until we stop using the Dalvik register maps).
909 * Insert a dummy intrinsic copy call, which will be recognized
910 * by the quick path and removed by the portable path.
911 */
912 llvm::Value* src = getLLVMValue(cUnit, rlSrc[0].origSReg);
913 llvm::Value* res = emitCopy(cUnit, src, rlDest);
914 defineValue(cUnit, res, rlDest.origSReg);
915 }
916 break;
917
918 case Instruction::CONST:
919 case Instruction::CONST_4:
920 case Instruction::CONST_16: {
buzbee6969d502012-06-15 16:40:31 -0700921 llvm::Constant* immValue = cUnit->irb->GetJInt(vB);
buzbee2cfc6392012-05-07 14:51:40 -0700922 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
923 defineValue(cUnit, res, rlDest.origSReg);
924 }
925 break;
926
927 case Instruction::CONST_WIDE_16:
928 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700929 // Sign extend to 64 bits
930 int64_t imm = static_cast<int32_t>(vB);
931 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
buzbee2cfc6392012-05-07 14:51:40 -0700932 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
933 defineValue(cUnit, res, rlDest.origSReg);
934 }
935 break;
936
937 case Instruction::CONST_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700938 llvm::Constant* immValue = cUnit->irb->GetJInt(vB << 16);
buzbee2cfc6392012-05-07 14:51:40 -0700939 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
940 defineValue(cUnit, res, rlDest.origSReg);
941 }
942 break;
943
944 case Instruction::CONST_WIDE: {
945 llvm::Constant* immValue =
946 cUnit->irb->GetJLong(mir->dalvikInsn.vB_wide);
947 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
948 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700949 }
950 break;
buzbee2cfc6392012-05-07 14:51:40 -0700951 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700952 int64_t imm = static_cast<int64_t>(vB) << 48;
buzbee2cfc6392012-05-07 14:51:40 -0700953 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
954 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
955 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700956 }
957 break;
958
buzbee8fa0fda2012-06-27 15:44:52 -0700959 case Instruction::SPUT_OBJECT:
buzbee76592632012-06-29 15:18:35 -0700960 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputObject,
buzbee8fa0fda2012-06-27 15:44:52 -0700961 rlSrc[0]);
962 break;
963 case Instruction::SPUT:
964 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -0700965 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputFloat,
buzbee8fa0fda2012-06-27 15:44:52 -0700966 rlSrc[0]);
967 } else {
buzbee76592632012-06-29 15:18:35 -0700968 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSput, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700969 }
970 break;
971 case Instruction::SPUT_BOOLEAN:
buzbee76592632012-06-29 15:18:35 -0700972 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputBoolean,
buzbee8fa0fda2012-06-27 15:44:52 -0700973 rlSrc[0]);
974 break;
975 case Instruction::SPUT_BYTE:
buzbee76592632012-06-29 15:18:35 -0700976 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputByte, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700977 break;
978 case Instruction::SPUT_CHAR:
buzbee76592632012-06-29 15:18:35 -0700979 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputChar, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700980 break;
981 case Instruction::SPUT_SHORT:
buzbee76592632012-06-29 15:18:35 -0700982 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputShort, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700983 break;
984 case Instruction::SPUT_WIDE:
985 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -0700986 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputDouble,
buzbee8fa0fda2012-06-27 15:44:52 -0700987 rlSrc[0]);
988 } else {
buzbee76592632012-06-29 15:18:35 -0700989 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputWide,
buzbee8fa0fda2012-06-27 15:44:52 -0700990 rlSrc[0]);
991 }
992 break;
993
994 case Instruction::SGET_OBJECT:
995 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetObject, rlDest);
996 break;
997 case Instruction::SGET:
998 if (rlDest.fp) {
999 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetFloat, rlDest);
1000 } else {
1001 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSget, rlDest);
1002 }
1003 break;
1004 case Instruction::SGET_BOOLEAN:
1005 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetBoolean, rlDest);
1006 break;
1007 case Instruction::SGET_BYTE:
1008 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetByte, rlDest);
1009 break;
1010 case Instruction::SGET_CHAR:
1011 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetChar, rlDest);
1012 break;
1013 case Instruction::SGET_SHORT:
1014 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetShort, rlDest);
1015 break;
1016 case Instruction::SGET_WIDE:
1017 if (rlDest.fp) {
1018 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetDouble,
1019 rlDest);
1020 } else {
1021 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetWide, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001022 }
1023 break;
buzbee2cfc6392012-05-07 14:51:40 -07001024
1025 case Instruction::RETURN_WIDE:
1026 case Instruction::RETURN:
1027 case Instruction::RETURN_OBJECT: {
TDYa1274f2935e2012-06-22 06:25:03 -07001028 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001029 emitSuspendCheck(cUnit);
1030 }
buzbeeb03f4872012-06-11 15:22:11 -07001031 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001032 cUnit->irb->CreateRet(getLLVMValue(cUnit, rlSrc[0].origSReg));
1033 bb->hasReturn = true;
1034 }
1035 break;
1036
1037 case Instruction::RETURN_VOID: {
TDYa1274f2935e2012-06-22 06:25:03 -07001038 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001039 emitSuspendCheck(cUnit);
1040 }
buzbeeb03f4872012-06-11 15:22:11 -07001041 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001042 cUnit->irb->CreateRetVoid();
1043 bb->hasReturn = true;
1044 }
1045 break;
1046
1047 case Instruction::IF_EQ:
1048 convertCompareAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0], rlSrc[1]);
1049 break;
1050 case Instruction::IF_NE:
1051 convertCompareAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0], rlSrc[1]);
1052 break;
1053 case Instruction::IF_LT:
1054 convertCompareAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0], rlSrc[1]);
1055 break;
1056 case Instruction::IF_GE:
1057 convertCompareAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0], rlSrc[1]);
1058 break;
1059 case Instruction::IF_GT:
1060 convertCompareAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0], rlSrc[1]);
1061 break;
1062 case Instruction::IF_LE:
1063 convertCompareAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0], rlSrc[1]);
1064 break;
1065 case Instruction::IF_EQZ:
1066 convertCompareZeroAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0]);
1067 break;
1068 case Instruction::IF_NEZ:
1069 convertCompareZeroAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0]);
1070 break;
1071 case Instruction::IF_LTZ:
1072 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0]);
1073 break;
1074 case Instruction::IF_GEZ:
1075 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0]);
1076 break;
1077 case Instruction::IF_GTZ:
1078 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0]);
1079 break;
1080 case Instruction::IF_LEZ:
1081 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0]);
1082 break;
1083
1084 case Instruction::GOTO:
1085 case Instruction::GOTO_16:
1086 case Instruction::GOTO_32: {
1087 if (bb->taken->startOffset <= bb->startOffset) {
1088 emitSuspendCheck(cUnit);
1089 }
1090 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->taken->id));
1091 }
1092 break;
1093
1094 case Instruction::ADD_LONG:
1095 case Instruction::ADD_LONG_2ADDR:
1096 case Instruction::ADD_INT:
1097 case Instruction::ADD_INT_2ADDR:
1098 convertArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1099 break;
1100 case Instruction::SUB_LONG:
1101 case Instruction::SUB_LONG_2ADDR:
1102 case Instruction::SUB_INT:
1103 case Instruction::SUB_INT_2ADDR:
1104 convertArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1105 break;
1106 case Instruction::MUL_LONG:
1107 case Instruction::MUL_LONG_2ADDR:
1108 case Instruction::MUL_INT:
1109 case Instruction::MUL_INT_2ADDR:
1110 convertArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1111 break;
1112 case Instruction::DIV_LONG:
1113 case Instruction::DIV_LONG_2ADDR:
1114 case Instruction::DIV_INT:
1115 case Instruction::DIV_INT_2ADDR:
1116 convertArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1117 break;
1118 case Instruction::REM_LONG:
1119 case Instruction::REM_LONG_2ADDR:
1120 case Instruction::REM_INT:
1121 case Instruction::REM_INT_2ADDR:
1122 convertArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1123 break;
1124 case Instruction::AND_LONG:
1125 case Instruction::AND_LONG_2ADDR:
1126 case Instruction::AND_INT:
1127 case Instruction::AND_INT_2ADDR:
1128 convertArithOp(cUnit, kOpAnd, rlDest, rlSrc[0], rlSrc[1]);
1129 break;
1130 case Instruction::OR_LONG:
1131 case Instruction::OR_LONG_2ADDR:
1132 case Instruction::OR_INT:
1133 case Instruction::OR_INT_2ADDR:
1134 convertArithOp(cUnit, kOpOr, rlDest, rlSrc[0], rlSrc[1]);
1135 break;
1136 case Instruction::XOR_LONG:
1137 case Instruction::XOR_LONG_2ADDR:
1138 case Instruction::XOR_INT:
1139 case Instruction::XOR_INT_2ADDR:
1140 convertArithOp(cUnit, kOpXor, rlDest, rlSrc[0], rlSrc[1]);
1141 break;
1142 case Instruction::SHL_LONG:
1143 case Instruction::SHL_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001144 convertShift(cUnit, greenland::IntrinsicHelper::SHLLong,
1145 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001146 break;
buzbee2cfc6392012-05-07 14:51:40 -07001147 case Instruction::SHL_INT:
1148 case Instruction::SHL_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001149 convertShift(cUnit, greenland::IntrinsicHelper::SHLInt,
1150 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001151 break;
1152 case Instruction::SHR_LONG:
1153 case Instruction::SHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001154 convertShift(cUnit, greenland::IntrinsicHelper::SHRLong,
1155 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001156 break;
buzbee2cfc6392012-05-07 14:51:40 -07001157 case Instruction::SHR_INT:
1158 case Instruction::SHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001159 convertShift(cUnit, greenland::IntrinsicHelper::SHRInt,
1160 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001161 break;
1162 case Instruction::USHR_LONG:
1163 case Instruction::USHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001164 convertShift(cUnit, greenland::IntrinsicHelper::USHRLong,
1165 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001166 break;
buzbee2cfc6392012-05-07 14:51:40 -07001167 case Instruction::USHR_INT:
1168 case Instruction::USHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001169 convertShift(cUnit, greenland::IntrinsicHelper::USHRInt,
1170 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001171 break;
1172
1173 case Instruction::ADD_INT_LIT16:
1174 case Instruction::ADD_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001175 convertArithOpLit(cUnit, kOpAdd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001176 break;
1177 case Instruction::RSUB_INT:
1178 case Instruction::RSUB_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001179 convertArithOpLit(cUnit, kOpRsub, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001180 break;
1181 case Instruction::MUL_INT_LIT16:
1182 case Instruction::MUL_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001183 convertArithOpLit(cUnit, kOpMul, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001184 break;
1185 case Instruction::DIV_INT_LIT16:
1186 case Instruction::DIV_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001187 convertArithOpLit(cUnit, kOpDiv, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001188 break;
1189 case Instruction::REM_INT_LIT16:
1190 case Instruction::REM_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001191 convertArithOpLit(cUnit, kOpRem, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001192 break;
1193 case Instruction::AND_INT_LIT16:
1194 case Instruction::AND_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001195 convertArithOpLit(cUnit, kOpAnd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001196 break;
1197 case Instruction::OR_INT_LIT16:
1198 case Instruction::OR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001199 convertArithOpLit(cUnit, kOpOr, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001200 break;
1201 case Instruction::XOR_INT_LIT16:
1202 case Instruction::XOR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001203 convertArithOpLit(cUnit, kOpXor, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001204 break;
1205 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001206 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHLInt,
1207 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001208 break;
1209 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001210 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHRInt,
1211 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001212 break;
1213 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001214 convertShiftLit(cUnit, greenland::IntrinsicHelper::USHRInt,
1215 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001216 break;
1217
1218 case Instruction::ADD_FLOAT:
1219 case Instruction::ADD_FLOAT_2ADDR:
1220 case Instruction::ADD_DOUBLE:
1221 case Instruction::ADD_DOUBLE_2ADDR:
1222 convertFPArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1223 break;
1224
1225 case Instruction::SUB_FLOAT:
1226 case Instruction::SUB_FLOAT_2ADDR:
1227 case Instruction::SUB_DOUBLE:
1228 case Instruction::SUB_DOUBLE_2ADDR:
1229 convertFPArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1230 break;
1231
1232 case Instruction::MUL_FLOAT:
1233 case Instruction::MUL_FLOAT_2ADDR:
1234 case Instruction::MUL_DOUBLE:
1235 case Instruction::MUL_DOUBLE_2ADDR:
1236 convertFPArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1237 break;
1238
1239 case Instruction::DIV_FLOAT:
1240 case Instruction::DIV_FLOAT_2ADDR:
1241 case Instruction::DIV_DOUBLE:
1242 case Instruction::DIV_DOUBLE_2ADDR:
1243 convertFPArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1244 break;
1245
1246 case Instruction::REM_FLOAT:
1247 case Instruction::REM_FLOAT_2ADDR:
1248 case Instruction::REM_DOUBLE:
1249 case Instruction::REM_DOUBLE_2ADDR:
1250 convertFPArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1251 break;
1252
buzbee6969d502012-06-15 16:40:31 -07001253 case Instruction::INVOKE_STATIC:
buzbee101305f2012-06-28 18:00:56 -07001254 convertInvoke(cUnit, bb, mir, kStatic, false /*range*/,
1255 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001256 break;
1257 case Instruction::INVOKE_STATIC_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001258 convertInvoke(cUnit, bb, mir, kStatic, true /*range*/,
1259 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001260 break;
1261
1262 case Instruction::INVOKE_DIRECT:
buzbee101305f2012-06-28 18:00:56 -07001263 convertInvoke(cUnit, bb, mir, kDirect, false /*range*/,
1264 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001265 break;
1266 case Instruction::INVOKE_DIRECT_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001267 convertInvoke(cUnit, bb, mir, kDirect, true /*range*/,
1268 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001269 break;
1270
1271 case Instruction::INVOKE_VIRTUAL:
buzbee101305f2012-06-28 18:00:56 -07001272 convertInvoke(cUnit, bb, mir, kVirtual, false /*range*/,
1273 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001274 break;
1275 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001276 convertInvoke(cUnit, bb, mir, kVirtual, true /*range*/,
1277 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001278 break;
1279
1280 case Instruction::INVOKE_SUPER:
buzbee101305f2012-06-28 18:00:56 -07001281 convertInvoke(cUnit, bb, mir, kSuper, false /*range*/,
1282 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001283 break;
1284 case Instruction::INVOKE_SUPER_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001285 convertInvoke(cUnit, bb, mir, kSuper, true /*range*/,
1286 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001287 break;
1288
1289 case Instruction::INVOKE_INTERFACE:
buzbee101305f2012-06-28 18:00:56 -07001290 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1291 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001292 break;
1293 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001294 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1295 false /* NewFilledArray */);
1296 break;
1297 case Instruction::FILLED_NEW_ARRAY:
1298 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1299 true /* NewFilledArray */);
1300 break;
1301 case Instruction::FILLED_NEW_ARRAY_RANGE:
1302 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1303 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001304 break;
1305
1306 case Instruction::CONST_STRING:
1307 case Instruction::CONST_STRING_JUMBO:
buzbee101305f2012-06-28 18:00:56 -07001308 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstString,
1309 rlDest);
1310 break;
1311
1312 case Instruction::CONST_CLASS:
1313 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstClass,
1314 rlDest);
1315 break;
1316
1317 case Instruction::CHECK_CAST:
1318 convertCheckCast(cUnit, vB, rlSrc[0]);
buzbee6969d502012-06-15 16:40:31 -07001319 break;
1320
buzbee4f1181f2012-06-22 13:52:12 -07001321 case Instruction::NEW_INSTANCE:
buzbee8fa0fda2012-06-27 15:44:52 -07001322 convertNewInstance(cUnit, vB, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001323 break;
1324
buzbee32412962012-06-26 16:27:56 -07001325 case Instruction::MOVE_EXCEPTION:
1326 convertMoveException(cUnit, rlDest);
1327 break;
1328
1329 case Instruction::THROW:
1330 convertThrow(cUnit, rlSrc[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001331 /*
1332 * If this throw is standalone, terminate.
1333 * If it might rethrow, force termination
1334 * of the following block.
1335 */
1336 if (bb->fallThrough == NULL) {
1337 cUnit->irb->CreateUnreachable();
1338 } else {
1339 bb->fallThrough->fallThrough = NULL;
1340 bb->fallThrough->taken = NULL;
1341 }
buzbee32412962012-06-26 16:27:56 -07001342 break;
1343
buzbee2cfc6392012-05-07 14:51:40 -07001344 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001345 case Instruction::MOVE_RESULT:
1346 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001347 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001348 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001349 */
jeffhao9a4f0032012-08-30 16:17:40 -07001350 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001351 break;
1352
1353 case Instruction::MONITOR_ENTER:
buzbee8fa0fda2012-06-27 15:44:52 -07001354 convertMonitorEnterExit(cUnit, optFlags,
1355 greenland::IntrinsicHelper::MonitorEnter,
1356 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001357 break;
1358
1359 case Instruction::MONITOR_EXIT:
buzbee8fa0fda2012-06-27 15:44:52 -07001360 convertMonitorEnterExit(cUnit, optFlags,
1361 greenland::IntrinsicHelper::MonitorExit,
1362 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001363 break;
1364
1365 case Instruction::ARRAY_LENGTH:
buzbee76592632012-06-29 15:18:35 -07001366 convertArrayLength(cUnit, optFlags, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001367 break;
1368
1369 case Instruction::NEW_ARRAY:
1370 convertNewArray(cUnit, vC, rlDest, rlSrc[0]);
1371 break;
1372
1373 case Instruction::INSTANCE_OF:
1374 convertInstanceOf(cUnit, vC, rlDest, rlSrc[0]);
1375 break;
1376
1377 case Instruction::AGET:
1378 if (rlDest.fp) {
1379 convertAget(cUnit, optFlags,
1380 greenland::IntrinsicHelper::HLArrayGetFloat,
1381 rlDest, rlSrc[0], rlSrc[1]);
1382 } else {
1383 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGet,
1384 rlDest, rlSrc[0], rlSrc[1]);
1385 }
1386 break;
1387 case Instruction::AGET_OBJECT:
1388 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetObject,
1389 rlDest, rlSrc[0], rlSrc[1]);
1390 break;
1391 case Instruction::AGET_BOOLEAN:
1392 convertAget(cUnit, optFlags,
1393 greenland::IntrinsicHelper::HLArrayGetBoolean,
1394 rlDest, rlSrc[0], rlSrc[1]);
1395 break;
1396 case Instruction::AGET_BYTE:
1397 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetByte,
1398 rlDest, rlSrc[0], rlSrc[1]);
1399 break;
1400 case Instruction::AGET_CHAR:
1401 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetChar,
1402 rlDest, rlSrc[0], rlSrc[1]);
1403 break;
1404 case Instruction::AGET_SHORT:
1405 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetShort,
1406 rlDest, rlSrc[0], rlSrc[1]);
1407 break;
1408 case Instruction::AGET_WIDE:
1409 if (rlDest.fp) {
1410 convertAget(cUnit, optFlags,
1411 greenland::IntrinsicHelper::HLArrayGetDouble,
1412 rlDest, rlSrc[0], rlSrc[1]);
1413 } else {
1414 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetWide,
1415 rlDest, rlSrc[0], rlSrc[1]);
1416 }
1417 break;
1418
1419 case Instruction::APUT:
1420 if (rlSrc[0].fp) {
1421 convertAput(cUnit, optFlags,
1422 greenland::IntrinsicHelper::HLArrayPutFloat,
1423 rlSrc[0], rlSrc[1], rlSrc[2]);
1424 } else {
1425 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPut,
1426 rlSrc[0], rlSrc[1], rlSrc[2]);
1427 }
1428 break;
1429 case Instruction::APUT_OBJECT:
1430 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutObject,
1431 rlSrc[0], rlSrc[1], rlSrc[2]);
1432 break;
1433 case Instruction::APUT_BOOLEAN:
1434 convertAput(cUnit, optFlags,
1435 greenland::IntrinsicHelper::HLArrayPutBoolean,
1436 rlSrc[0], rlSrc[1], rlSrc[2]);
1437 break;
1438 case Instruction::APUT_BYTE:
1439 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutByte,
1440 rlSrc[0], rlSrc[1], rlSrc[2]);
1441 break;
1442 case Instruction::APUT_CHAR:
1443 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutChar,
1444 rlSrc[0], rlSrc[1], rlSrc[2]);
1445 break;
1446 case Instruction::APUT_SHORT:
1447 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutShort,
1448 rlSrc[0], rlSrc[1], rlSrc[2]);
1449 break;
1450 case Instruction::APUT_WIDE:
1451 if (rlSrc[0].fp) {
1452 convertAput(cUnit, optFlags,
1453 greenland::IntrinsicHelper::HLArrayPutDouble,
1454 rlSrc[0], rlSrc[1], rlSrc[2]);
1455 } else {
1456 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutWide,
1457 rlSrc[0], rlSrc[1], rlSrc[2]);
1458 }
1459 break;
1460
buzbee101305f2012-06-28 18:00:56 -07001461 case Instruction::IGET:
1462 if (rlDest.fp) {
1463 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetFloat,
buzbee4f4dfc72012-07-02 14:54:44 -07001464 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001465 } else {
1466 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGet,
buzbee4f4dfc72012-07-02 14:54:44 -07001467 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001468 }
buzbee2cfc6392012-05-07 14:51:40 -07001469 break;
buzbee101305f2012-06-28 18:00:56 -07001470 case Instruction::IGET_OBJECT:
1471 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetObject,
buzbee4f4dfc72012-07-02 14:54:44 -07001472 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001473 break;
1474 case Instruction::IGET_BOOLEAN:
1475 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetBoolean,
buzbee4f4dfc72012-07-02 14:54:44 -07001476 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001477 break;
1478 case Instruction::IGET_BYTE:
1479 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetByte,
buzbee4f4dfc72012-07-02 14:54:44 -07001480 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001481 break;
1482 case Instruction::IGET_CHAR:
1483 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetChar,
buzbee4f4dfc72012-07-02 14:54:44 -07001484 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001485 break;
1486 case Instruction::IGET_SHORT:
1487 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetShort,
buzbee4f4dfc72012-07-02 14:54:44 -07001488 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001489 break;
1490 case Instruction::IGET_WIDE:
1491 if (rlDest.fp) {
1492 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetDouble,
buzbee4f4dfc72012-07-02 14:54:44 -07001493 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001494 } else {
1495 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetWide,
buzbee4f4dfc72012-07-02 14:54:44 -07001496 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001497 }
1498 break;
1499 case Instruction::IPUT:
buzbee85eee022012-07-16 22:12:38 -07001500 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001501 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutFloat,
1502 rlSrc[0], rlSrc[1], vC);
1503 } else {
1504 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPut,
1505 rlSrc[0], rlSrc[1], vC);
1506 }
1507 break;
1508 case Instruction::IPUT_OBJECT:
1509 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutObject,
1510 rlSrc[0], rlSrc[1], vC);
1511 break;
1512 case Instruction::IPUT_BOOLEAN:
1513 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutBoolean,
1514 rlSrc[0], rlSrc[1], vC);
1515 break;
1516 case Instruction::IPUT_BYTE:
1517 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutByte,
1518 rlSrc[0], rlSrc[1], vC);
1519 break;
1520 case Instruction::IPUT_CHAR:
1521 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutChar,
1522 rlSrc[0], rlSrc[1], vC);
1523 break;
1524 case Instruction::IPUT_SHORT:
1525 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutShort,
1526 rlSrc[0], rlSrc[1], vC);
1527 break;
1528 case Instruction::IPUT_WIDE:
buzbee85eee022012-07-16 22:12:38 -07001529 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001530 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutDouble,
1531 rlSrc[0], rlSrc[1], vC);
1532 } else {
1533 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutWide,
1534 rlSrc[0], rlSrc[1], vC);
1535 }
buzbee2cfc6392012-05-07 14:51:40 -07001536 break;
1537
1538 case Instruction::FILL_ARRAY_DATA:
buzbee101305f2012-06-28 18:00:56 -07001539 convertFillArrayData(cUnit, vB, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001540 break;
1541
buzbee76592632012-06-29 15:18:35 -07001542 case Instruction::LONG_TO_INT:
1543 convertLongToInt(cUnit, rlDest, rlSrc[0]);
1544 break;
1545
buzbee101305f2012-06-28 18:00:56 -07001546 case Instruction::INT_TO_LONG:
1547 convertIntToLong(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001548 break;
1549
buzbee101305f2012-06-28 18:00:56 -07001550 case Instruction::INT_TO_CHAR:
1551 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1552 greenland::IntrinsicHelper::IntToChar);
1553 break;
1554 case Instruction::INT_TO_BYTE:
1555 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1556 greenland::IntrinsicHelper::IntToByte);
1557 break;
1558 case Instruction::INT_TO_SHORT:
1559 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1560 greenland::IntrinsicHelper::IntToShort);
1561 break;
1562
buzbee76592632012-06-29 15:18:35 -07001563 case Instruction::INT_TO_FLOAT:
1564 case Instruction::LONG_TO_FLOAT:
1565 convertIntToFP(cUnit, cUnit->irb->getFloatTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001566 break;
1567
buzbee76592632012-06-29 15:18:35 -07001568 case Instruction::INT_TO_DOUBLE:
1569 case Instruction::LONG_TO_DOUBLE:
1570 convertIntToFP(cUnit, cUnit->irb->getDoubleTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001571 break;
1572
buzbee76592632012-06-29 15:18:35 -07001573 case Instruction::FLOAT_TO_DOUBLE:
1574 convertFloatToDouble(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001575 break;
1576
buzbee76592632012-06-29 15:18:35 -07001577 case Instruction::DOUBLE_TO_FLOAT:
1578 convertDoubleToFloat(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001579 break;
1580
1581 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001582 case Instruction::NEG_INT:
1583 convertNeg(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001584 break;
1585
1586 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001587 case Instruction::NEG_DOUBLE:
buzbee76592632012-06-29 15:18:35 -07001588 convertNegFP(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001589 break;
1590
buzbee76592632012-06-29 15:18:35 -07001591 case Instruction::NOT_LONG:
1592 case Instruction::NOT_INT:
1593 convertNot(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001594 break;
1595
buzbee2cfc6392012-05-07 14:51:40 -07001596 case Instruction::FLOAT_TO_INT:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001597 convertFPToInt(cUnit, greenland::IntrinsicHelper::F2I, rlDest, rlSrc[0]);
1598 break;
1599
buzbee2cfc6392012-05-07 14:51:40 -07001600 case Instruction::DOUBLE_TO_INT:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001601 convertFPToInt(cUnit, greenland::IntrinsicHelper::D2I, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001602 break;
1603
buzbee76592632012-06-29 15:18:35 -07001604 case Instruction::FLOAT_TO_LONG:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001605 convertFPToInt(cUnit, greenland::IntrinsicHelper::F2L, rlDest, rlSrc[0]);
1606 break;
1607
buzbee76592632012-06-29 15:18:35 -07001608 case Instruction::DOUBLE_TO_LONG:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001609 convertFPToInt(cUnit, greenland::IntrinsicHelper::D2L, rlDest, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001610 break;
1611
1612 case Instruction::CMPL_FLOAT:
1613 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplFloat,
1614 rlDest, rlSrc[0], rlSrc[1]);
1615 break;
1616 case Instruction::CMPG_FLOAT:
1617 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgFloat,
1618 rlDest, rlSrc[0], rlSrc[1]);
1619 break;
1620 case Instruction::CMPL_DOUBLE:
1621 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplDouble,
1622 rlDest, rlSrc[0], rlSrc[1]);
1623 break;
1624 case Instruction::CMPG_DOUBLE:
1625 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgDouble,
1626 rlDest, rlSrc[0], rlSrc[1]);
1627 break;
1628 case Instruction::CMP_LONG:
1629 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpLong,
1630 rlDest, rlSrc[0], rlSrc[1]);
1631 break;
1632
buzbee76592632012-06-29 15:18:35 -07001633 case Instruction::PACKED_SWITCH:
buzbeef58c12c2012-07-03 15:06:29 -07001634 convertPackedSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001635 break;
1636
1637 case Instruction::SPARSE_SWITCH:
buzbeea1da8a52012-07-09 14:00:21 -07001638 convertSparseSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001639 break;
buzbee2cfc6392012-05-07 14:51:40 -07001640
1641 default:
buzbee32412962012-06-26 16:27:56 -07001642 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001643 res = true;
1644 }
buzbeeb03f4872012-06-11 15:22:11 -07001645 if (objectDefinition) {
1646 setShadowFrameEntry(cUnit, (llvm::Value*)
1647 cUnit->llvmValues.elemList[rlDest.origSReg]);
1648 }
buzbee2cfc6392012-05-07 14:51:40 -07001649 return res;
1650}
1651
1652/* Extended MIR instructions like PHI */
1653void convertExtendedMIR(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
1654 llvm::BasicBlock* llvmBB)
1655{
1656
1657 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1658 case kMirOpPhi: {
buzbee2cfc6392012-05-07 14:51:40 -07001659 RegLocation rlDest = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee2a83e8f2012-07-13 16:42:30 -07001660 /*
1661 * The Art compiler's Phi nodes only handle 32-bit operands,
1662 * representing wide values using a matched set of Phi nodes
1663 * for the lower and upper halves. In the llvm world, we only
1664 * want a single Phi for wides. Here we will simply discard
1665 * the Phi node representing the high word.
1666 */
1667 if (rlDest.highWord) {
1668 return; // No Phi node - handled via low word
1669 }
1670 int* incoming = (int*)mir->dalvikInsn.vB;
buzbee2cfc6392012-05-07 14:51:40 -07001671 llvm::Type* phiType =
1672 llvmTypeFromLocRec(cUnit, rlDest);
1673 llvm::PHINode* phi = cUnit->irb->CreatePHI(phiType, mir->ssaRep->numUses);
1674 for (int i = 0; i < mir->ssaRep->numUses; i++) {
1675 RegLocation loc;
buzbee2a83e8f2012-07-13 16:42:30 -07001676 // Don't check width here.
1677 loc = oatGetRawSrc(cUnit, mir, i);
1678 DCHECK_EQ(rlDest.wide, loc.wide);
1679 DCHECK_EQ(rlDest.wide & rlDest.highWord, loc.wide & loc.highWord);
1680 DCHECK_EQ(rlDest.fp, loc.fp);
1681 DCHECK_EQ(rlDest.core, loc.core);
1682 DCHECK_EQ(rlDest.ref, loc.ref);
buzbeed1643e42012-09-05 14:06:51 -07001683 SafeMap<unsigned int, unsigned int>::iterator it;
1684 it = cUnit->blockIdMap.find(incoming[i]);
1685 DCHECK(it != cUnit->blockIdMap.end());
buzbee2cfc6392012-05-07 14:51:40 -07001686 phi->addIncoming(getLLVMValue(cUnit, loc.origSReg),
buzbeed1643e42012-09-05 14:06:51 -07001687 getLLVMBlock(cUnit, it->second));
buzbee2cfc6392012-05-07 14:51:40 -07001688 }
1689 defineValue(cUnit, phi, rlDest.origSReg);
1690 break;
1691 }
1692 case kMirOpCopy: {
1693 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1694 break;
1695 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001696 case kMirOpNop:
1697 if ((mir == bb->lastMIRInsn) && (bb->taken == NULL) &&
1698 (bb->fallThrough == NULL)) {
1699 cUnit->irb->CreateUnreachable();
1700 }
1701 break;
1702
buzbee2cfc6392012-05-07 14:51:40 -07001703#if defined(TARGET_ARM)
1704 case kMirOpFusedCmplFloat:
1705 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmpFloat";
1706 break;
1707 case kMirOpFusedCmpgFloat:
1708 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmgFloat";
1709 break;
1710 case kMirOpFusedCmplDouble:
1711 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmplDouble";
1712 break;
1713 case kMirOpFusedCmpgDouble:
1714 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmpgDouble";
1715 break;
1716 case kMirOpFusedCmpLong:
1717 UNIMPLEMENTED(WARNING) << "unimp kMirOpLongCmpBranch";
1718 break;
1719#endif
1720 default:
1721 break;
1722 }
1723}
1724
1725void setDexOffset(CompilationUnit* cUnit, int32_t offset)
1726{
1727 cUnit->currentDalvikOffset = offset;
buzbee76592632012-06-29 15:18:35 -07001728 llvm::SmallVector<llvm::Value*, 1> arrayRef;
buzbee2cfc6392012-05-07 14:51:40 -07001729 arrayRef.push_back(cUnit->irb->getInt32(offset));
1730 llvm::MDNode* node = llvm::MDNode::get(*cUnit->context, arrayRef);
1731 cUnit->irb->SetDexOffset(node);
1732}
1733
1734// Attach method info as metadata to special intrinsic
1735void setMethodInfo(CompilationUnit* cUnit)
1736{
1737 // We don't want dex offset on this
1738 cUnit->irb->SetDexOffset(NULL);
1739 greenland::IntrinsicHelper::IntrinsicId id;
1740 id = greenland::IntrinsicHelper::MethodInfo;
1741 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1742 llvm::Instruction* inst = cUnit->irb->CreateCall(intr);
1743 llvm::SmallVector<llvm::Value*, 2> regInfo;
1744 regInfo.push_back(cUnit->irb->getInt32(cUnit->numIns));
1745 regInfo.push_back(cUnit->irb->getInt32(cUnit->numRegs));
1746 regInfo.push_back(cUnit->irb->getInt32(cUnit->numOuts));
1747 regInfo.push_back(cUnit->irb->getInt32(cUnit->numCompilerTemps));
1748 regInfo.push_back(cUnit->irb->getInt32(cUnit->numSSARegs));
1749 llvm::MDNode* regInfoNode = llvm::MDNode::get(*cUnit->context, regInfo);
1750 inst->setMetadata("RegInfo", regInfoNode);
1751 int promoSize = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
1752 llvm::SmallVector<llvm::Value*, 50> pmap;
1753 for (int i = 0; i < promoSize; i++) {
1754 PromotionMap* p = &cUnit->promotionMap[i];
1755 int32_t mapData = ((p->firstInPair & 0xff) << 24) |
1756 ((p->fpReg & 0xff) << 16) |
1757 ((p->coreReg & 0xff) << 8) |
1758 ((p->fpLocation & 0xf) << 4) |
1759 (p->coreLocation & 0xf);
1760 pmap.push_back(cUnit->irb->getInt32(mapData));
1761 }
1762 llvm::MDNode* mapNode = llvm::MDNode::get(*cUnit->context, pmap);
1763 inst->setMetadata("PromotionMap", mapNode);
1764 setDexOffset(cUnit, cUnit->currentDalvikOffset);
1765}
1766
1767/* Handle the content in each basic block */
1768bool methodBlockBitcodeConversion(CompilationUnit* cUnit, BasicBlock* bb)
1769{
buzbeed1643e42012-09-05 14:06:51 -07001770 if (bb->blockType == kDead) return false;
buzbee2cfc6392012-05-07 14:51:40 -07001771 llvm::BasicBlock* llvmBB = getLLVMBlock(cUnit, bb->id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001772 if (llvmBB != NULL) {
1773 cUnit->irb->SetInsertPoint(llvmBB);
1774 setDexOffset(cUnit, bb->startOffset);
1775 }
buzbee2cfc6392012-05-07 14:51:40 -07001776
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001777 if (cUnit->printMe) {
1778 LOG(INFO) << "................................";
1779 LOG(INFO) << "Block id " << bb->id;
1780 if (llvmBB != NULL) {
1781 LOG(INFO) << "label " << llvmBB->getName().str().c_str();
1782 } else {
1783 LOG(INFO) << "llvmBB is NULL";
1784 }
1785 }
1786
buzbee2cfc6392012-05-07 14:51:40 -07001787 if (bb->blockType == kEntryBlock) {
1788 setMethodInfo(cUnit);
buzbeeb03f4872012-06-11 15:22:11 -07001789 bool *canBeRef = (bool*) oatNew(cUnit, sizeof(bool) *
1790 cUnit->numDalvikRegisters, true,
1791 kAllocMisc);
1792 for (int i = 0; i < cUnit->numSSARegs; i++) {
1793 canBeRef[SRegToVReg(cUnit, i)] |= cUnit->regLocation[i].ref;
1794 }
1795 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
1796 if (canBeRef[i]) {
1797 cUnit->numShadowFrameEntries++;
1798 }
1799 }
1800 if (cUnit->numShadowFrameEntries > 0) {
1801 cUnit->shadowMap = (int*) oatNew(cUnit, sizeof(int) *
1802 cUnit->numShadowFrameEntries, true,
1803 kAllocMisc);
1804 for (int i = 0, j = 0; i < cUnit->numDalvikRegisters; i++) {
1805 if (canBeRef[i]) {
1806 cUnit->shadowMap[j++] = i;
1807 }
1808 }
1809 greenland::IntrinsicHelper::IntrinsicId id =
1810 greenland::IntrinsicHelper::AllocaShadowFrame;
1811 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1812 llvm::Value* entries = cUnit->irb->getInt32(cUnit->numShadowFrameEntries);
1813 cUnit->irb->CreateCall(func, entries);
1814 }
buzbee2cfc6392012-05-07 14:51:40 -07001815 } else if (bb->blockType == kExitBlock) {
1816 /*
1817 * Because of the differences between how MIR/LIR and llvm handle exit
1818 * blocks, we won't explicitly covert them. On the llvm-to-lir
1819 * path, it will need to be regenereated.
1820 */
1821 return false;
buzbee6969d502012-06-15 16:40:31 -07001822 } else if (bb->blockType == kExceptionHandling) {
1823 /*
1824 * Because we're deferring null checking, delete the associated empty
1825 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001826 */
1827 llvmBB->eraseFromParent();
1828 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001829 }
1830
1831 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
1832
1833 setDexOffset(cUnit, mir->offset);
1834
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001835 int opcode = mir->dalvikInsn.opcode;
1836 Instruction::Format dalvikFormat =
1837 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001838
1839 /* If we're compiling for the debugger, generate an update callout */
1840 if (cUnit->genDebugger) {
1841 UNIMPLEMENTED(FATAL) << "Need debug codegen";
1842 //genDebuggerUpdate(cUnit, mir->offset);
1843 }
1844
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001845 if (opcode == kMirOpCheck) {
1846 // Combine check and work halves of throwing instruction.
1847 MIR* workHalf = mir->meta.throwInsn;
1848 mir->dalvikInsn.opcode = workHalf->dalvikInsn.opcode;
1849 opcode = mir->dalvikInsn.opcode;
1850 SSARepresentation* ssaRep = workHalf->ssaRep;
1851 workHalf->ssaRep = mir->ssaRep;
1852 mir->ssaRep = ssaRep;
1853 workHalf->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1854 if (bb->successorBlockList.blockListType == kCatch) {
1855 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
1856 greenland::IntrinsicHelper::CatchTargets);
1857 llvm::Value* switchKey =
1858 cUnit->irb->CreateCall(intr, cUnit->irb->getInt32(mir->offset));
1859 GrowableListIterator iter;
1860 oatGrowableListIteratorInit(&bb->successorBlockList.blocks, &iter);
1861 // New basic block to use for work half
1862 llvm::BasicBlock* workBB =
1863 llvm::BasicBlock::Create(*cUnit->context, "", cUnit->func);
1864 llvm::SwitchInst* sw =
1865 cUnit->irb->CreateSwitch(switchKey, workBB,
1866 bb->successorBlockList.blocks.numUsed);
1867 while (true) {
1868 SuccessorBlockInfo *successorBlockInfo =
1869 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iter);
1870 if (successorBlockInfo == NULL) break;
1871 llvm::BasicBlock *target =
1872 getLLVMBlock(cUnit, successorBlockInfo->block->id);
1873 int typeIndex = successorBlockInfo->key;
1874 sw->addCase(cUnit->irb->getInt32(typeIndex), target);
1875 }
1876 llvmBB = workBB;
1877 cUnit->irb->SetInsertPoint(llvmBB);
1878 }
1879 }
1880
1881 if (opcode >= kMirOpFirst) {
buzbee2cfc6392012-05-07 14:51:40 -07001882 convertExtendedMIR(cUnit, bb, mir, llvmBB);
1883 continue;
1884 }
1885
1886 bool notHandled = convertMIRNode(cUnit, mir, bb, llvmBB,
1887 NULL /* labelList */);
1888 if (notHandled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001889 Instruction::Code dalvikOpcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001890 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001891 mir->offset, opcode,
buzbee2cfc6392012-05-07 14:51:40 -07001892 Instruction::Name(dalvikOpcode),
1893 dalvikFormat);
1894 }
1895 }
1896
buzbee4be777b2012-07-12 14:38:18 -07001897 if (bb->blockType == kEntryBlock) {
1898 cUnit->entryTargetBB = getLLVMBlock(cUnit, bb->fallThrough->id);
1899 } else if ((bb->fallThrough != NULL) && !bb->hasReturn) {
buzbee2cfc6392012-05-07 14:51:40 -07001900 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->fallThrough->id));
1901 }
1902
1903 return false;
1904}
1905
buzbee4f4dfc72012-07-02 14:54:44 -07001906char remapShorty(char shortyType) {
1907 /*
1908 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1909 * and longs/doubles are represented as a pair of registers. When sub-word
1910 * arguments (and method results) are passed, they are extended to Dalvik
1911 * virtual register containers. Because llvm is picky about type consistency,
1912 * we must either cast the "real" type to 32-bit container multiple Dalvik
1913 * register types, or always use the expanded values.
1914 * Here, we're doing the latter. We map the shorty signature to container
1915 * types (which is valid so long as we always do a real expansion of passed
1916 * arguments and field loads).
1917 */
1918 switch(shortyType) {
1919 case 'Z' : shortyType = 'I'; break;
1920 case 'B' : shortyType = 'I'; break;
1921 case 'S' : shortyType = 'I'; break;
1922 case 'C' : shortyType = 'I'; break;
1923 default: break;
1924 }
1925 return shortyType;
1926}
1927
buzbee2cfc6392012-05-07 14:51:40 -07001928llvm::FunctionType* getFunctionType(CompilationUnit* cUnit) {
1929
1930 // Get return type
buzbee4f4dfc72012-07-02 14:54:44 -07001931 llvm::Type* ret_type = cUnit->irb->GetJType(remapShorty(cUnit->shorty[0]),
buzbee2cfc6392012-05-07 14:51:40 -07001932 greenland::kAccurate);
1933
1934 // Get argument type
1935 std::vector<llvm::Type*> args_type;
1936
1937 // method object
1938 args_type.push_back(cUnit->irb->GetJMethodTy());
1939
1940 // Do we have a "this"?
1941 if ((cUnit->access_flags & kAccStatic) == 0) {
1942 args_type.push_back(cUnit->irb->GetJObjectTy());
1943 }
1944
1945 for (uint32_t i = 1; i < strlen(cUnit->shorty); ++i) {
buzbee4f4dfc72012-07-02 14:54:44 -07001946 args_type.push_back(cUnit->irb->GetJType(remapShorty(cUnit->shorty[i]),
buzbee2cfc6392012-05-07 14:51:40 -07001947 greenland::kAccurate));
1948 }
1949
1950 return llvm::FunctionType::get(ret_type, args_type, false);
1951}
1952
1953bool createFunction(CompilationUnit* cUnit) {
1954 std::string func_name(PrettyMethod(cUnit->method_idx, *cUnit->dex_file,
1955 /* with_signature */ false));
1956 llvm::FunctionType* func_type = getFunctionType(cUnit);
1957
1958 if (func_type == NULL) {
1959 return false;
1960 }
1961
1962 cUnit->func = llvm::Function::Create(func_type,
1963 llvm::Function::ExternalLinkage,
1964 func_name, cUnit->module);
1965
1966 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
1967 llvm::Function::arg_iterator arg_end(cUnit->func->arg_end());
1968
1969 arg_iter->setName("method");
1970 ++arg_iter;
1971
1972 int startSReg = cUnit->numRegs;
1973
1974 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
1975 arg_iter->setName(StringPrintf("v%i_0", startSReg));
1976 startSReg += cUnit->regLocation[startSReg].wide ? 2 : 1;
1977 }
1978
1979 return true;
1980}
1981
1982bool createLLVMBasicBlock(CompilationUnit* cUnit, BasicBlock* bb)
1983{
1984 // Skip the exit block
buzbeed1643e42012-09-05 14:06:51 -07001985 if ((bb->blockType == kDead) ||(bb->blockType == kExitBlock)) {
buzbee2cfc6392012-05-07 14:51:40 -07001986 cUnit->idToBlockMap.Put(bb->id, NULL);
1987 } else {
1988 int offset = bb->startOffset;
1989 bool entryBlock = (bb->blockType == kEntryBlock);
1990 llvm::BasicBlock* llvmBB =
1991 llvm::BasicBlock::Create(*cUnit->context, entryBlock ? "entry" :
buzbee8320f382012-09-11 16:29:42 -07001992 StringPrintf(kLabelFormat, bb->catchEntry ? kCatchBlock :
1993 kNormalBlock, offset, bb->id), cUnit->func);
buzbee2cfc6392012-05-07 14:51:40 -07001994 if (entryBlock) {
1995 cUnit->entryBB = llvmBB;
1996 cUnit->placeholderBB =
1997 llvm::BasicBlock::Create(*cUnit->context, "placeholder",
1998 cUnit->func);
1999 }
2000 cUnit->idToBlockMap.Put(bb->id, llvmBB);
2001 }
2002 return false;
2003}
2004
2005
2006/*
2007 * Convert MIR to LLVM_IR
2008 * o For each ssa name, create LLVM named value. Type these
2009 * appropriately, and ignore high half of wide and double operands.
2010 * o For each MIR basic block, create an LLVM basic block.
2011 * o Iterate through the MIR a basic block at a time, setting arguments
2012 * to recovered ssa name.
2013 */
2014void oatMethodMIR2Bitcode(CompilationUnit* cUnit)
2015{
2016 initIR(cUnit);
2017 oatInitGrowableList(cUnit, &cUnit->llvmValues, cUnit->numSSARegs);
2018
2019 // Create the function
2020 createFunction(cUnit);
2021
2022 // Create an LLVM basic block for each MIR block in dfs preorder
2023 oatDataFlowAnalysisDispatcher(cUnit, createLLVMBasicBlock,
2024 kPreOrderDFSTraversal, false /* isIterative */);
2025 /*
2026 * Create an llvm named value for each MIR SSA name. Note: we'll use
2027 * placeholders for all non-argument values (because we haven't seen
2028 * the definition yet).
2029 */
2030 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2031 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
2032 arg_iter++; /* Skip path method */
2033 for (int i = 0; i < cUnit->numSSARegs; i++) {
2034 llvm::Value* val;
buzbee85eee022012-07-16 22:12:38 -07002035 RegLocation rlTemp = cUnit->regLocation[i];
2036 if ((SRegToVReg(cUnit, i) < 0) || rlTemp.highWord) {
buzbee2a83e8f2012-07-13 16:42:30 -07002037 oatInsertGrowableList(cUnit, &cUnit->llvmValues, 0);
2038 } else if ((i < cUnit->numRegs) ||
2039 (i >= (cUnit->numRegs + cUnit->numIns))) {
buzbee85eee022012-07-16 22:12:38 -07002040 llvm::Constant* immValue = cUnit->regLocation[i].wide ?
2041 cUnit->irb->GetJLong(0) : cUnit->irb->GetJInt(0);
buzbee2a83e8f2012-07-13 16:42:30 -07002042 val = emitConst(cUnit, immValue, cUnit->regLocation[i]);
2043 val->setName(llvmSSAName(cUnit, i));
buzbee2cfc6392012-05-07 14:51:40 -07002044 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)val);
buzbee2cfc6392012-05-07 14:51:40 -07002045 } else {
2046 // Recover previously-created argument values
2047 llvm::Value* argVal = arg_iter++;
2048 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)argVal);
2049 }
2050 }
buzbee2cfc6392012-05-07 14:51:40 -07002051
2052 oatDataFlowAnalysisDispatcher(cUnit, methodBlockBitcodeConversion,
2053 kPreOrderDFSTraversal, false /* Iterative */);
2054
buzbee4be777b2012-07-12 14:38:18 -07002055 /*
2056 * In a few rare cases of verification failure, the verifier will
2057 * replace one or more Dalvik opcodes with the special
2058 * throw-verification-failure opcode. This can leave the SSA graph
2059 * in an invalid state, as definitions may be lost, while uses retained.
2060 * To work around this problem, we insert placeholder definitions for
2061 * all Dalvik SSA regs in the "placeholder" block. Here, after
2062 * bitcode conversion is complete, we examine those placeholder definitions
2063 * and delete any with no references (which normally is all of them).
2064 *
2065 * If any definitions remain, we link the placeholder block into the
2066 * CFG. Otherwise, it is deleted.
2067 */
2068 for (llvm::BasicBlock::iterator it = cUnit->placeholderBB->begin(),
2069 itEnd = cUnit->placeholderBB->end(); it != itEnd;) {
2070 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2071 DCHECK(inst != NULL);
2072 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2073 DCHECK(val != NULL);
2074 if (val->getNumUses() == 0) {
2075 inst->eraseFromParent();
2076 }
2077 }
2078 setDexOffset(cUnit, 0);
2079 if (cUnit->placeholderBB->empty()) {
2080 cUnit->placeholderBB->eraseFromParent();
2081 } else {
2082 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2083 cUnit->irb->CreateBr(cUnit->entryTargetBB);
2084 cUnit->entryTargetBB = cUnit->placeholderBB;
2085 }
2086 cUnit->irb->SetInsertPoint(cUnit->entryBB);
2087 cUnit->irb->CreateBr(cUnit->entryTargetBB);
buzbee2cfc6392012-05-07 14:51:40 -07002088
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002089 if (cUnit->enableDebug & (1 << kDebugVerifyBitcode)) {
2090 if (llvm::verifyFunction(*cUnit->func, llvm::PrintMessageAction)) {
2091 LOG(INFO) << "Bitcode verification FAILED for "
2092 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
2093 << " of size " << cUnit->insnsSize;
2094 cUnit->enableDebug |= (1 << kDebugDumpBitcodeFile);
2095 }
2096 }
buzbee2cfc6392012-05-07 14:51:40 -07002097
buzbeead8f15e2012-06-18 14:49:45 -07002098 if (cUnit->enableDebug & (1 << kDebugDumpBitcodeFile)) {
2099 // Write bitcode to file
2100 std::string errmsg;
2101 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
2102 oatReplaceSpecialChars(fname);
2103 // TODO: make configurable
buzbee4f1181f2012-06-22 13:52:12 -07002104 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002105
buzbeead8f15e2012-06-18 14:49:45 -07002106 llvm::OwningPtr<llvm::tool_output_file> out_file(
2107 new llvm::tool_output_file(fname.c_str(), errmsg,
2108 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002109
buzbeead8f15e2012-06-18 14:49:45 -07002110 if (!errmsg.empty()) {
2111 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2112 }
2113
2114 llvm::WriteBitcodeToFile(cUnit->module, out_file->os());
2115 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002116 }
buzbee2cfc6392012-05-07 14:51:40 -07002117}
2118
2119RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val) {
2120 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002121 DCHECK(val != NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002122 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2123 if (it == cUnit->locMap.end()) {
buzbee4f1181f2012-06-22 13:52:12 -07002124 std::string valName = val->getName().str();
buzbee32412962012-06-26 16:27:56 -07002125 if (valName.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002126 // FIXME: need to be more robust, handle FP and be in a position to
2127 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002128 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2129 memset(&res, 0, sizeof(res));
2130 res.location = kLocPhysReg;
2131 res.lowReg = oatAllocTemp(cUnit);
2132 res.home = true;
2133 res.sRegLow = INVALID_SREG;
2134 res.origSReg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002135 llvm::Type* ty = val->getType();
2136 res.wide = ((ty == cUnit->irb->getInt64Ty()) ||
2137 (ty == cUnit->irb->getDoubleTy()));
2138 if (res.wide) {
2139 res.highReg = oatAllocTemp(cUnit);
2140 }
buzbee4f1181f2012-06-22 13:52:12 -07002141 cUnit->locMap.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002142 } else {
2143 DCHECK_EQ(valName[0], 'v');
2144 int baseSReg = INVALID_SREG;
2145 sscanf(valName.c_str(), "v%d_", &baseSReg);
2146 res = cUnit->regLocation[baseSReg];
2147 cUnit->locMap.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002148 }
2149 } else {
2150 res = it->second;
2151 }
2152 return res;
2153}
2154
2155Instruction::Code getDalvikOpcode(OpKind op, bool isConst, bool isWide)
2156{
2157 Instruction::Code res = Instruction::NOP;
2158 if (isWide) {
2159 switch(op) {
2160 case kOpAdd: res = Instruction::ADD_LONG; break;
2161 case kOpSub: res = Instruction::SUB_LONG; break;
2162 case kOpMul: res = Instruction::MUL_LONG; break;
2163 case kOpDiv: res = Instruction::DIV_LONG; break;
2164 case kOpRem: res = Instruction::REM_LONG; break;
2165 case kOpAnd: res = Instruction::AND_LONG; break;
2166 case kOpOr: res = Instruction::OR_LONG; break;
2167 case kOpXor: res = Instruction::XOR_LONG; break;
2168 case kOpLsl: res = Instruction::SHL_LONG; break;
2169 case kOpLsr: res = Instruction::USHR_LONG; break;
2170 case kOpAsr: res = Instruction::SHR_LONG; break;
2171 default: LOG(FATAL) << "Unexpected OpKind " << op;
2172 }
2173 } else if (isConst){
2174 switch(op) {
2175 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2176 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2177 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2178 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2179 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2180 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2181 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2182 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2183 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2184 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2185 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2186 default: LOG(FATAL) << "Unexpected OpKind " << op;
2187 }
2188 } else {
2189 switch(op) {
2190 case kOpAdd: res = Instruction::ADD_INT; break;
2191 case kOpSub: res = Instruction::SUB_INT; break;
2192 case kOpMul: res = Instruction::MUL_INT; break;
2193 case kOpDiv: res = Instruction::DIV_INT; break;
2194 case kOpRem: res = Instruction::REM_INT; break;
2195 case kOpAnd: res = Instruction::AND_INT; break;
2196 case kOpOr: res = Instruction::OR_INT; break;
2197 case kOpXor: res = Instruction::XOR_INT; break;
2198 case kOpLsl: res = Instruction::SHL_INT; break;
2199 case kOpLsr: res = Instruction::USHR_INT; break;
2200 case kOpAsr: res = Instruction::SHR_INT; break;
2201 default: LOG(FATAL) << "Unexpected OpKind " << op;
2202 }
2203 }
2204 return res;
2205}
2206
buzbee4f1181f2012-06-22 13:52:12 -07002207Instruction::Code getDalvikFPOpcode(OpKind op, bool isConst, bool isWide)
2208{
2209 Instruction::Code res = Instruction::NOP;
2210 if (isWide) {
2211 switch(op) {
2212 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2213 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2214 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2215 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2216 case kOpRem: res = Instruction::REM_DOUBLE; break;
2217 default: LOG(FATAL) << "Unexpected OpKind " << op;
2218 }
2219 } else {
2220 switch(op) {
2221 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2222 case kOpSub: res = Instruction::SUB_FLOAT; break;
2223 case kOpMul: res = Instruction::MUL_FLOAT; break;
2224 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2225 case kOpRem: res = Instruction::REM_FLOAT; break;
2226 default: LOG(FATAL) << "Unexpected OpKind " << op;
2227 }
2228 }
2229 return res;
2230}
2231
2232void cvtBinFPOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2233{
2234 RegLocation rlDest = getLoc(cUnit, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002235 /*
2236 * Normally, we won't ever generate an FP operation with an immediate
2237 * operand (not supported in Dex instruction set). However, the IR builder
2238 * may insert them - in particular for createNegFP. Recognize this case
2239 * and deal with it.
2240 */
2241 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2242 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2243 DCHECK(op2C == NULL);
2244 if ((op1C != NULL) && (op == kOpSub)) {
2245 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(1));
2246 if (rlDest.wide) {
2247 genArithOpDouble(cUnit, Instruction::NEG_DOUBLE, rlDest, rlSrc, rlSrc);
2248 } else {
2249 genArithOpFloat(cUnit, Instruction::NEG_FLOAT, rlDest, rlSrc, rlSrc);
2250 }
buzbee4f1181f2012-06-22 13:52:12 -07002251 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002252 DCHECK(op1C == NULL);
2253 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2254 RegLocation rlSrc2 = getLoc(cUnit, inst->getOperand(1));
2255 Instruction::Code dalvikOp = getDalvikFPOpcode(op, false, rlDest.wide);
2256 if (rlDest.wide) {
2257 genArithOpDouble(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2258 } else {
2259 genArithOpFloat(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2260 }
buzbee4f1181f2012-06-22 13:52:12 -07002261 }
2262}
2263
buzbee101305f2012-06-28 18:00:56 -07002264void cvtIntNarrowing(CompilationUnit* cUnit, llvm::Instruction* inst,
2265 Instruction::Code opcode)
2266{
2267 RegLocation rlDest = getLoc(cUnit, inst);
2268 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2269 genIntNarrowing(cUnit, opcode, rlDest, rlSrc);
2270}
2271
buzbee76592632012-06-29 15:18:35 -07002272void cvtIntToFP(CompilationUnit* cUnit, llvm::Instruction* inst)
2273{
2274 RegLocation rlDest = getLoc(cUnit, inst);
2275 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2276 Instruction::Code opcode;
2277 if (rlDest.wide) {
2278 if (rlSrc.wide) {
2279 opcode = Instruction::LONG_TO_DOUBLE;
2280 } else {
2281 opcode = Instruction::INT_TO_DOUBLE;
2282 }
2283 } else {
2284 if (rlSrc.wide) {
2285 opcode = Instruction::LONG_TO_FLOAT;
2286 } else {
2287 opcode = Instruction::INT_TO_FLOAT;
2288 }
2289 }
2290 genConversion(cUnit, opcode, rlDest, rlSrc);
2291}
2292
TDYa1274ec8ccd2012-08-11 07:04:57 -07002293void cvtFPToInt(CompilationUnit* cUnit, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002294{
TDYa1274ec8ccd2012-08-11 07:04:57 -07002295 RegLocation rlDest = getLoc(cUnit, call_inst);
2296 RegLocation rlSrc = getLoc(cUnit, call_inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002297 Instruction::Code opcode;
2298 if (rlDest.wide) {
2299 if (rlSrc.wide) {
2300 opcode = Instruction::DOUBLE_TO_LONG;
2301 } else {
2302 opcode = Instruction::FLOAT_TO_LONG;
2303 }
2304 } else {
2305 if (rlSrc.wide) {
2306 opcode = Instruction::DOUBLE_TO_INT;
2307 } else {
2308 opcode = Instruction::FLOAT_TO_INT;
2309 }
2310 }
2311 genConversion(cUnit, opcode, rlDest, rlSrc);
2312}
2313
2314void cvtFloatToDouble(CompilationUnit* cUnit, llvm::Instruction* inst)
2315{
2316 RegLocation rlDest = getLoc(cUnit, inst);
2317 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2318 genConversion(cUnit, Instruction::FLOAT_TO_DOUBLE, rlDest, rlSrc);
2319}
2320
2321void cvtTrunc(CompilationUnit* cUnit, llvm::Instruction* inst)
2322{
2323 RegLocation rlDest = getLoc(cUnit, inst);
2324 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2325 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
2326 rlSrc = oatWideToNarrow(cUnit, rlSrc);
2327 storeValue(cUnit, rlDest, rlSrc);
2328}
2329
2330void cvtDoubleToFloat(CompilationUnit* cUnit, llvm::Instruction* inst)
2331{
2332 RegLocation rlDest = getLoc(cUnit, inst);
2333 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2334 genConversion(cUnit, Instruction::DOUBLE_TO_FLOAT, rlDest, rlSrc);
2335}
2336
2337
buzbee101305f2012-06-28 18:00:56 -07002338void cvtIntExt(CompilationUnit* cUnit, llvm::Instruction* inst, bool isSigned)
2339{
2340 // TODO: evaluate src/tgt types and add general support for more than int to long
2341 RegLocation rlDest = getLoc(cUnit, inst);
2342 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2343 DCHECK(rlDest.wide);
2344 DCHECK(!rlSrc.wide);
2345 DCHECK(!rlDest.fp);
2346 DCHECK(!rlSrc.fp);
2347 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2348 if (rlSrc.location == kLocPhysReg) {
2349 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2350 } else {
2351 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2352 }
2353 if (isSigned) {
2354 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2355 } else {
2356 loadConstant(cUnit, rlResult.highReg, 0);
2357 }
2358 storeValueWide(cUnit, rlDest, rlResult);
2359}
2360
buzbee2cfc6392012-05-07 14:51:40 -07002361void cvtBinOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2362{
2363 RegLocation rlDest = getLoc(cUnit, inst);
2364 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002365 // Special-case RSUB/NEG
buzbee4f1181f2012-06-22 13:52:12 -07002366 llvm::ConstantInt* lhsImm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2367 if ((op == kOpSub) && (lhsImm != NULL)) {
2368 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(1));
buzbeef58c12c2012-07-03 15:06:29 -07002369 if (rlSrc1.wide) {
2370 DCHECK_EQ(lhsImm->getSExtValue(), 0);
2371 genArithOpLong(cUnit, Instruction::NEG_LONG, rlDest, rlSrc1, rlSrc1);
2372 } else {
2373 genArithOpIntLit(cUnit, Instruction::RSUB_INT, rlDest, rlSrc1,
2374 lhsImm->getSExtValue());
2375 }
buzbee4f1181f2012-06-22 13:52:12 -07002376 return;
2377 }
2378 DCHECK(lhsImm == NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002379 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2380 llvm::Value* rhs = inst->getOperand(1);
buzbee9a2487f2012-07-26 14:01:13 -07002381 llvm::ConstantInt* constRhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2382 if (!rlDest.wide && (constRhs != NULL)) {
buzbee2cfc6392012-05-07 14:51:40 -07002383 Instruction::Code dalvikOp = getDalvikOpcode(op, true, false);
buzbee9a2487f2012-07-26 14:01:13 -07002384 genArithOpIntLit(cUnit, dalvikOp, rlDest, rlSrc1, constRhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002385 } else {
2386 Instruction::Code dalvikOp = getDalvikOpcode(op, false, rlDest.wide);
buzbee9a2487f2012-07-26 14:01:13 -07002387 RegLocation rlSrc2;
2388 if (constRhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002389 // ir_builder converts NOT_LONG to xor src, -1. Restore
2390 DCHECK_EQ(dalvikOp, Instruction::XOR_LONG);
2391 DCHECK_EQ(-1L, constRhs->getSExtValue());
2392 dalvikOp = Instruction::NOT_LONG;
buzbee9a2487f2012-07-26 14:01:13 -07002393 rlSrc2 = rlSrc1;
2394 } else {
2395 rlSrc2 = getLoc(cUnit, rhs);
2396 }
buzbee2cfc6392012-05-07 14:51:40 -07002397 if (rlDest.wide) {
2398 genArithOpLong(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2399 } else {
2400 genArithOpInt(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2401 }
2402 }
2403}
2404
buzbee2a83e8f2012-07-13 16:42:30 -07002405void cvtShiftOp(CompilationUnit* cUnit, Instruction::Code opcode,
2406 llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002407{
buzbee2a83e8f2012-07-13 16:42:30 -07002408 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2409 RegLocation rlDest = getLoc(cUnit, callInst);
2410 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2411 llvm::Value* rhs = callInst->getArgOperand(1);
2412 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2413 DCHECK(!rlDest.wide);
2414 genArithOpIntLit(cUnit, opcode, rlDest, rlSrc, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002415 } else {
buzbee2a83e8f2012-07-13 16:42:30 -07002416 RegLocation rlShift = getLoc(cUnit, rhs);
2417 if (callInst->getType() == cUnit->irb->getInt64Ty()) {
2418 genShiftOpLong(cUnit, opcode, rlDest, rlSrc, rlShift);
2419 } else {
2420 genArithOpInt(cUnit, opcode, rlDest, rlSrc, rlShift);
2421 }
buzbee101305f2012-06-28 18:00:56 -07002422 }
2423}
2424
buzbee2cfc6392012-05-07 14:51:40 -07002425void cvtBr(CompilationUnit* cUnit, llvm::Instruction* inst)
2426{
2427 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(inst);
2428 DCHECK(brInst != NULL);
2429 DCHECK(brInst->isUnconditional()); // May change - but this is all we use now
2430 llvm::BasicBlock* targetBB = brInst->getSuccessor(0);
2431 opUnconditionalBranch(cUnit, cUnit->blockToLabelMap.Get(targetBB));
2432}
2433
2434void cvtPhi(CompilationUnit* cUnit, llvm::Instruction* inst)
2435{
2436 // Nop - these have already been processed
2437}
2438
2439void cvtRet(CompilationUnit* cUnit, llvm::Instruction* inst)
2440{
2441 llvm::ReturnInst* retInst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2442 llvm::Value* retVal = retInst->getReturnValue();
2443 if (retVal != NULL) {
2444 RegLocation rlSrc = getLoc(cUnit, retVal);
2445 if (rlSrc.wide) {
2446 storeValueWide(cUnit, oatGetReturnWide(cUnit, rlSrc.fp), rlSrc);
2447 } else {
2448 storeValue(cUnit, oatGetReturn(cUnit, rlSrc.fp), rlSrc);
2449 }
2450 }
2451 genExitSequence(cUnit);
2452}
2453
2454ConditionCode getCond(llvm::ICmpInst::Predicate llvmCond)
2455{
2456 ConditionCode res = kCondAl;
2457 switch(llvmCond) {
buzbee6969d502012-06-15 16:40:31 -07002458 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002459 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2460 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2461 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002462 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002463 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002464 default: LOG(FATAL) << "Unexpected llvm condition";
2465 }
2466 return res;
2467}
2468
2469void cvtICmp(CompilationUnit* cUnit, llvm::Instruction* inst)
2470{
2471 // genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2)
2472 UNIMPLEMENTED(FATAL);
2473}
2474
2475void cvtICmpBr(CompilationUnit* cUnit, llvm::Instruction* inst,
2476 llvm::BranchInst* brInst)
2477{
2478 // Get targets
2479 llvm::BasicBlock* takenBB = brInst->getSuccessor(0);
2480 LIR* taken = cUnit->blockToLabelMap.Get(takenBB);
2481 llvm::BasicBlock* fallThroughBB = brInst->getSuccessor(1);
2482 LIR* fallThrough = cUnit->blockToLabelMap.Get(fallThroughBB);
2483 // Get comparison operands
2484 llvm::ICmpInst* iCmpInst = llvm::dyn_cast<llvm::ICmpInst>(inst);
2485 ConditionCode cond = getCond(iCmpInst->getPredicate());
2486 llvm::Value* lhs = iCmpInst->getOperand(0);
2487 // Not expecting a constant as 1st operand
2488 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
2489 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2490 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2491 llvm::Value* rhs = inst->getOperand(1);
2492#if defined(TARGET_MIPS)
2493 // Compare and branch in one shot
2494 (void)taken;
2495 (void)cond;
2496 (void)rhs;
2497 UNIMPLEMENTED(FATAL);
2498#else
2499 //Compare, then branch
2500 // TODO: handle fused CMP_LONG/IF_xxZ case
2501 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2502 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002503 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
2504 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002505 } else {
2506 RegLocation rlSrc2 = getLoc(cUnit, rhs);
2507 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2508 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
2509 }
2510 opCondBranch(cUnit, cond, taken);
2511#endif
2512 // Fallthrough
2513 opUnconditionalBranch(cUnit, fallThrough);
2514}
2515
2516void cvtCall(CompilationUnit* cUnit, llvm::CallInst* callInst,
2517 llvm::Function* callee)
2518{
2519 UNIMPLEMENTED(FATAL);
2520}
2521
buzbee2cfc6392012-05-07 14:51:40 -07002522void cvtCopy(CompilationUnit* cUnit, llvm::CallInst* callInst)
2523{
buzbee4f1181f2012-06-22 13:52:12 -07002524 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002525 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2526 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee76592632012-06-29 15:18:35 -07002527 DCHECK_EQ(rlSrc.wide, rlDest.wide);
2528 DCHECK_EQ(rlSrc.fp, rlDest.fp);
buzbee2cfc6392012-05-07 14:51:40 -07002529 if (rlSrc.wide) {
2530 storeValueWide(cUnit, rlDest, rlSrc);
2531 } else {
2532 storeValue(cUnit, rlDest, rlSrc);
2533 }
2534}
2535
2536// Note: Immediate arg is a ConstantInt regardless of result type
2537void cvtConst(CompilationUnit* cUnit, llvm::CallInst* callInst)
2538{
buzbee4f1181f2012-06-22 13:52:12 -07002539 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002540 llvm::ConstantInt* src =
2541 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2542 uint64_t immval = src->getZExtValue();
2543 RegLocation rlDest = getLoc(cUnit, callInst);
2544 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
2545 if (rlDest.wide) {
2546 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
2547 (immval) & 0xffffffff, (immval >> 32) & 0xffffffff);
2548 storeValueWide(cUnit, rlDest, rlResult);
2549 } else {
2550 loadConstantNoClobber(cUnit, rlResult.lowReg, immval & 0xffffffff);
2551 storeValue(cUnit, rlDest, rlResult);
2552 }
2553}
2554
buzbee101305f2012-06-28 18:00:56 -07002555void cvtConstObject(CompilationUnit* cUnit, llvm::CallInst* callInst,
2556 bool isString)
buzbee6969d502012-06-15 16:40:31 -07002557{
buzbee4f1181f2012-06-22 13:52:12 -07002558 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee101305f2012-06-28 18:00:56 -07002559 llvm::ConstantInt* idxVal =
buzbee6969d502012-06-15 16:40:31 -07002560 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee101305f2012-06-28 18:00:56 -07002561 uint32_t index = idxVal->getZExtValue();
buzbee6969d502012-06-15 16:40:31 -07002562 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee101305f2012-06-28 18:00:56 -07002563 if (isString) {
2564 genConstString(cUnit, index, rlDest);
2565 } else {
2566 genConstClass(cUnit, index, rlDest);
2567 }
2568}
2569
2570void cvtFillArrayData(CompilationUnit* cUnit, llvm::CallInst* callInst)
2571{
2572 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2573 llvm::ConstantInt* offsetVal =
2574 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2575 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2576 genFillArrayData(cUnit, offsetVal->getSExtValue(), rlSrc);
buzbee6969d502012-06-15 16:40:31 -07002577}
2578
buzbee4f1181f2012-06-22 13:52:12 -07002579void cvtNewInstance(CompilationUnit* cUnit, llvm::CallInst* callInst)
2580{
buzbee32412962012-06-26 16:27:56 -07002581 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002582 llvm::ConstantInt* typeIdxVal =
2583 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2584 uint32_t typeIdx = typeIdxVal->getZExtValue();
2585 RegLocation rlDest = getLoc(cUnit, callInst);
2586 genNewInstance(cUnit, typeIdx, rlDest);
2587}
2588
buzbee8fa0fda2012-06-27 15:44:52 -07002589void cvtNewArray(CompilationUnit* cUnit, llvm::CallInst* callInst)
2590{
2591 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2592 llvm::ConstantInt* typeIdxVal =
2593 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2594 uint32_t typeIdx = typeIdxVal->getZExtValue();
2595 llvm::Value* len = callInst->getArgOperand(1);
2596 RegLocation rlLen = getLoc(cUnit, len);
2597 RegLocation rlDest = getLoc(cUnit, callInst);
2598 genNewArray(cUnit, typeIdx, rlDest, rlLen);
2599}
2600
2601void cvtInstanceOf(CompilationUnit* cUnit, llvm::CallInst* callInst)
2602{
2603 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2604 llvm::ConstantInt* typeIdxVal =
2605 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2606 uint32_t typeIdx = typeIdxVal->getZExtValue();
2607 llvm::Value* src = callInst->getArgOperand(1);
2608 RegLocation rlSrc = getLoc(cUnit, src);
2609 RegLocation rlDest = getLoc(cUnit, callInst);
2610 genInstanceof(cUnit, typeIdx, rlDest, rlSrc);
2611}
2612
buzbee32412962012-06-26 16:27:56 -07002613void cvtThrow(CompilationUnit* cUnit, llvm::CallInst* callInst)
2614{
2615 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
2616 llvm::Value* src = callInst->getArgOperand(0);
2617 RegLocation rlSrc = getLoc(cUnit, src);
2618 genThrow(cUnit, rlSrc);
2619}
2620
buzbee8fa0fda2012-06-27 15:44:52 -07002621void cvtMonitorEnterExit(CompilationUnit* cUnit, bool isEnter,
2622 llvm::CallInst* callInst)
2623{
2624 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2625 llvm::ConstantInt* optFlags =
2626 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2627 llvm::Value* src = callInst->getArgOperand(1);
2628 RegLocation rlSrc = getLoc(cUnit, src);
2629 if (isEnter) {
2630 genMonitorEnter(cUnit, optFlags->getZExtValue(), rlSrc);
2631 } else {
2632 genMonitorExit(cUnit, optFlags->getZExtValue(), rlSrc);
2633 }
2634}
2635
buzbee76592632012-06-29 15:18:35 -07002636void cvtArrayLength(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002637{
2638 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2639 llvm::ConstantInt* optFlags =
2640 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2641 llvm::Value* src = callInst->getArgOperand(1);
2642 RegLocation rlSrc = getLoc(cUnit, src);
2643 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2644 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg, optFlags->getZExtValue());
2645 RegLocation rlDest = getLoc(cUnit, callInst);
2646 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2647 int lenOffset = Array::LengthOffset().Int32Value();
2648 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset, rlResult.lowReg);
2649 storeValue(cUnit, rlDest, rlResult);
2650}
2651
buzbee32412962012-06-26 16:27:56 -07002652void cvtMoveException(CompilationUnit* cUnit, llvm::CallInst* callInst)
2653{
2654 DCHECK_EQ(callInst->getNumArgOperands(), 0U);
2655 int exOffset = Thread::ExceptionOffset().Int32Value();
2656 RegLocation rlDest = getLoc(cUnit, callInst);
2657 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2658#if defined(TARGET_X86)
2659 newLIR2(cUnit, kX86Mov32RT, rlResult.lowReg, exOffset);
2660 newLIR2(cUnit, kX86Mov32TI, exOffset, 0);
2661#else
2662 int resetReg = oatAllocTemp(cUnit);
2663 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
2664 loadConstant(cUnit, resetReg, 0);
2665 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
2666 oatFreeTemp(cUnit, resetReg);
2667#endif
2668 storeValue(cUnit, rlDest, rlResult);
2669}
2670
buzbee4f1181f2012-06-22 13:52:12 -07002671void cvtSget(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2672 bool isObject)
2673{
buzbee32412962012-06-26 16:27:56 -07002674 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002675 llvm::ConstantInt* typeIdxVal =
2676 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2677 uint32_t typeIdx = typeIdxVal->getZExtValue();
2678 RegLocation rlDest = getLoc(cUnit, callInst);
2679 genSget(cUnit, typeIdx, rlDest, isWide, isObject);
2680}
2681
buzbee8fa0fda2012-06-27 15:44:52 -07002682void cvtSput(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2683 bool isObject)
2684{
2685 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2686 llvm::ConstantInt* typeIdxVal =
2687 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2688 uint32_t typeIdx = typeIdxVal->getZExtValue();
2689 llvm::Value* src = callInst->getArgOperand(1);
2690 RegLocation rlSrc = getLoc(cUnit, src);
2691 genSput(cUnit, typeIdx, rlSrc, isWide, isObject);
2692}
2693
2694void cvtAget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2695 int scale)
2696{
2697 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2698 llvm::ConstantInt* optFlags =
2699 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2700 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(1));
2701 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(2));
2702 RegLocation rlDest = getLoc(cUnit, callInst);
2703 genArrayGet(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2704 rlDest, scale);
2705}
2706
2707void cvtAput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
buzbeef1f86362012-07-10 15:18:31 -07002708 int scale, bool isObject)
buzbee8fa0fda2012-06-27 15:44:52 -07002709{
2710 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2711 llvm::ConstantInt* optFlags =
2712 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2713 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2714 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(2));
2715 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(3));
buzbeef1f86362012-07-10 15:18:31 -07002716 if (isObject) {
2717 genArrayObjPut(cUnit, optFlags->getZExtValue(), rlArray, rlIndex,
2718 rlSrc, scale);
2719 } else {
2720 genArrayPut(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2721 rlSrc, scale);
2722 }
2723}
2724
2725void cvtAputObj(CompilationUnit* cUnit, llvm::CallInst* callInst)
2726{
2727 cvtAput(cUnit, callInst, kWord, 2, true /* isObject */);
2728}
2729
2730void cvtAputPrimitive(CompilationUnit* cUnit, llvm::CallInst* callInst,
2731 OpSize size, int scale)
2732{
2733 cvtAput(cUnit, callInst, size, scale, false /* isObject */);
buzbee8fa0fda2012-06-27 15:44:52 -07002734}
2735
buzbee101305f2012-06-28 18:00:56 -07002736void cvtIget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2737 bool isWide, bool isObj)
2738{
2739 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2740 llvm::ConstantInt* optFlags =
2741 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2742 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(1));
2743 llvm::ConstantInt* fieldIdx =
2744 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2745 RegLocation rlDest = getLoc(cUnit, callInst);
2746 genIGet(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2747 size, rlDest, rlObj, isWide, isObj);
2748}
2749
2750void cvtIput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2751 bool isWide, bool isObj)
2752{
2753 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2754 llvm::ConstantInt* optFlags =
2755 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2756 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2757 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(2));
2758 llvm::ConstantInt* fieldIdx =
buzbee4f4dfc72012-07-02 14:54:44 -07002759 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(3));
buzbee101305f2012-06-28 18:00:56 -07002760 genIPut(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2761 size, rlSrc, rlObj, isWide, isObj);
2762}
2763
2764void cvtCheckCast(CompilationUnit* cUnit, llvm::CallInst* callInst)
2765{
2766 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2767 llvm::ConstantInt* typeIdx =
2768 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2769 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2770 genCheckCast(cUnit, typeIdx->getZExtValue(), rlSrc);
2771}
2772
buzbee76592632012-06-29 15:18:35 -07002773void cvtFPCompare(CompilationUnit* cUnit, llvm::CallInst* callInst,
2774 Instruction::Code opcode)
2775{
2776 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2777 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2778 RegLocation rlDest = getLoc(cUnit, callInst);
2779 genCmpFP(cUnit, opcode, rlDest, rlSrc1, rlSrc2);
2780}
2781
2782void cvtLongCompare(CompilationUnit* cUnit, llvm::CallInst* callInst)
2783{
2784 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2785 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2786 RegLocation rlDest = getLoc(cUnit, callInst);
2787 genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2);
2788}
2789
buzbeef58c12c2012-07-03 15:06:29 -07002790void cvtSwitch(CompilationUnit* cUnit, llvm::Instruction* inst)
2791{
2792 llvm::SwitchInst* swInst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2793 DCHECK(swInst != NULL);
2794 llvm::Value* testVal = swInst->getCondition();
2795 llvm::MDNode* tableOffsetNode = swInst->getMetadata("SwitchTable");
2796 DCHECK(tableOffsetNode != NULL);
2797 llvm::ConstantInt* tableOffsetValue =
2798 static_cast<llvm::ConstantInt*>(tableOffsetNode->getOperand(0));
2799 int32_t tableOffset = tableOffsetValue->getSExtValue();
2800 RegLocation rlSrc = getLoc(cUnit, testVal);
buzbeea1da8a52012-07-09 14:00:21 -07002801 const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
2802 u2 tableMagic = *table;
2803 if (tableMagic == 0x100) {
2804 genPackedSwitch(cUnit, tableOffset, rlSrc);
2805 } else {
2806 DCHECK_EQ(tableMagic, 0x200);
2807 genSparseSwitch(cUnit, tableOffset, rlSrc);
2808 }
buzbeef58c12c2012-07-03 15:06:29 -07002809}
2810
buzbee6969d502012-06-15 16:40:31 -07002811void cvtInvoke(CompilationUnit* cUnit, llvm::CallInst* callInst,
buzbee76592632012-06-29 15:18:35 -07002812 bool isVoid, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -07002813{
2814 CallInfo* info = (CallInfo*)oatNew(cUnit, sizeof(CallInfo), true,
2815 kAllocMisc);
buzbee8fa0fda2012-06-27 15:44:52 -07002816 if (isVoid) {
buzbee6969d502012-06-15 16:40:31 -07002817 info->result.location = kLocInvalid;
2818 } else {
2819 info->result = getLoc(cUnit, callInst);
2820 }
2821 llvm::ConstantInt* invokeTypeVal =
2822 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2823 llvm::ConstantInt* methodIndexVal =
2824 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(1));
2825 llvm::ConstantInt* optFlagsVal =
2826 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2827 info->type = static_cast<InvokeType>(invokeTypeVal->getZExtValue());
2828 info->index = methodIndexVal->getZExtValue();
2829 info->optFlags = optFlagsVal->getZExtValue();
2830 info->offset = cUnit->currentDalvikOffset;
2831
buzbee6969d502012-06-15 16:40:31 -07002832 // Count the argument words, and then build argument array.
2833 info->numArgWords = 0;
2834 for (unsigned int i = 3; i < callInst->getNumArgOperands(); i++) {
2835 RegLocation tLoc = getLoc(cUnit, callInst->getArgOperand(i));
2836 info->numArgWords += tLoc.wide ? 2 : 1;
2837 }
2838 info->args = (info->numArgWords == 0) ? NULL : (RegLocation*)
2839 oatNew(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc);
2840 // Now, fill in the location records, synthesizing high loc of wide vals
2841 for (int i = 3, next = 0; next < info->numArgWords;) {
buzbee4f1181f2012-06-22 13:52:12 -07002842 info->args[next] = getLoc(cUnit, callInst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002843 if (info->args[next].wide) {
2844 next++;
2845 // TODO: Might make sense to mark this as an invalid loc
2846 info->args[next].origSReg = info->args[next-1].origSReg+1;
2847 info->args[next].sRegLow = info->args[next-1].sRegLow+1;
2848 }
2849 next++;
2850 }
buzbee4f4dfc72012-07-02 14:54:44 -07002851 // TODO - rework such that we no longer need isRange
2852 info->isRange = (info->numArgWords > 5);
2853
buzbee76592632012-06-29 15:18:35 -07002854 if (isFilledNewArray) {
buzbee101305f2012-06-28 18:00:56 -07002855 genFilledNewArray(cUnit, info);
2856 } else {
2857 genInvoke(cUnit, info);
2858 }
buzbee6969d502012-06-15 16:40:31 -07002859}
2860
buzbeead8f15e2012-06-18 14:49:45 -07002861/* Look up the RegLocation associated with a Value. Must already be defined */
2862RegLocation valToLoc(CompilationUnit* cUnit, llvm::Value* val)
2863{
2864 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2865 DCHECK(it != cUnit->locMap.end()) << "Missing definition";
2866 return it->second;
2867}
2868
buzbee2cfc6392012-05-07 14:51:40 -07002869bool methodBitcodeBlockCodeGen(CompilationUnit* cUnit, llvm::BasicBlock* bb)
2870{
buzbee0967a252012-09-14 10:43:54 -07002871 while (cUnit->llvmBlocks.find(bb) == cUnit->llvmBlocks.end()) {
2872 llvm::BasicBlock* nextBB = NULL;
2873 cUnit->llvmBlocks.insert(bb);
2874 bool isEntry = (bb == &cUnit->func->getEntryBlock());
2875 // Define the starting label
2876 LIR* blockLabel = cUnit->blockToLabelMap.Get(bb);
2877 // Extract the type and starting offset from the block's name
2878 char blockType = kNormalBlock;
2879 if (!isEntry) {
2880 const char* blockName = bb->getName().str().c_str();
2881 int dummy;
2882 sscanf(blockName, kLabelFormat, &blockType, &blockLabel->operands[0], &dummy);
2883 cUnit->currentDalvikOffset = blockLabel->operands[0];
2884 } else {
2885 cUnit->currentDalvikOffset = 0;
2886 }
2887 // Set the label kind
2888 blockLabel->opcode = kPseudoNormalBlockLabel;
2889 // Insert the label
2890 oatAppendLIR(cUnit, blockLabel);
buzbee2cfc6392012-05-07 14:51:40 -07002891
buzbee0967a252012-09-14 10:43:54 -07002892 LIR* headLIR = NULL;
buzbee8320f382012-09-11 16:29:42 -07002893
buzbee0967a252012-09-14 10:43:54 -07002894 if (blockType == kCatchBlock) {
2895 headLIR = newLIR0(cUnit, kPseudoSafepointPC);
2896 }
buzbee8320f382012-09-11 16:29:42 -07002897
buzbee0967a252012-09-14 10:43:54 -07002898 // Free temp registers and reset redundant store tracking */
2899 oatResetRegPool(cUnit);
2900 oatResetDefTracking(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002901
buzbee0967a252012-09-14 10:43:54 -07002902 //TODO: restore oat incoming liveness optimization
2903 oatClobberAllRegs(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002904
buzbee0967a252012-09-14 10:43:54 -07002905 if (isEntry) {
2906 RegLocation* argLocs = (RegLocation*)
2907 oatNew(cUnit, sizeof(RegLocation) * cUnit->numIns, true, kAllocMisc);
2908 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
2909 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
2910 // Skip past Method*
2911 it++;
2912 for (unsigned i = 0; it != it_end; ++it) {
2913 llvm::Value* val = it;
2914 argLocs[i++] = valToLoc(cUnit, val);
2915 llvm::Type* ty = val->getType();
2916 if ((ty == cUnit->irb->getInt64Ty()) || (ty == cUnit->irb->getDoubleTy())) {
2917 argLocs[i] = argLocs[i-1];
2918 argLocs[i].lowReg = argLocs[i].highReg;
2919 argLocs[i].origSReg++;
2920 argLocs[i].sRegLow = INVALID_SREG;
2921 argLocs[i].highWord = true;
2922 i++;
2923 }
2924 }
2925 genEntrySequence(cUnit, argLocs, cUnit->methodLoc);
2926 }
2927
2928 // Visit all of the instructions in the block
2929 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2930 llvm::Instruction* inst = it;
2931 llvm::BasicBlock::iterator nextIt = ++it;
2932 // Extract the Dalvik offset from the instruction
2933 uint32_t opcode = inst->getOpcode();
2934 llvm::MDNode* dexOffsetNode = inst->getMetadata("DexOff");
2935 if (dexOffsetNode != NULL) {
2936 llvm::ConstantInt* dexOffsetValue =
2937 static_cast<llvm::ConstantInt*>(dexOffsetNode->getOperand(0));
2938 cUnit->currentDalvikOffset = dexOffsetValue->getZExtValue();
2939 }
2940
2941 oatResetRegPool(cUnit);
2942 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
2943 oatClobberAllRegs(cUnit);
2944 }
2945
2946 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
2947 oatResetDefTracking(cUnit);
2948 }
2949
2950 #ifndef NDEBUG
2951 /* Reset temp tracking sanity check */
2952 cUnit->liveSReg = INVALID_SREG;
2953 #endif
2954
2955 // TODO: use llvm opcode name here instead of "boundary" if verbose
2956 LIR* boundaryLIR = markBoundary(cUnit, cUnit->currentDalvikOffset, "boundary");
2957
2958 /* Remember the first LIR for thisl block*/
2959 if (headLIR == NULL) {
2960 headLIR = boundaryLIR;
2961 headLIR->defMask = ENCODE_ALL;
2962 }
2963
2964 switch(opcode) {
2965
2966 case llvm::Instruction::ICmp: {
2967 llvm::Instruction* nextInst = nextIt;
2968 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(nextInst);
2969 if (brInst != NULL /* and... */) {
2970 cvtICmpBr(cUnit, inst, brInst);
2971 ++it;
2972 } else {
2973 cvtICmp(cUnit, inst);
2974 }
2975 }
2976 break;
2977
2978 case llvm::Instruction::Call: {
2979 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(inst);
2980 llvm::Function* callee = callInst->getCalledFunction();
2981 greenland::IntrinsicHelper::IntrinsicId id =
2982 cUnit->intrinsic_helper->GetIntrinsicId(callee);
2983 switch (id) {
2984 case greenland::IntrinsicHelper::AllocaShadowFrame:
2985 case greenland::IntrinsicHelper::SetShadowFrameEntry:
2986 case greenland::IntrinsicHelper::PopShadowFrame:
2987 // Ignore shadow frame stuff for quick compiler
2988 break;
2989 case greenland::IntrinsicHelper::CopyInt:
2990 case greenland::IntrinsicHelper::CopyObj:
2991 case greenland::IntrinsicHelper::CopyFloat:
2992 case greenland::IntrinsicHelper::CopyLong:
2993 case greenland::IntrinsicHelper::CopyDouble:
2994 cvtCopy(cUnit, callInst);
2995 break;
2996 case greenland::IntrinsicHelper::ConstInt:
2997 case greenland::IntrinsicHelper::ConstObj:
2998 case greenland::IntrinsicHelper::ConstLong:
2999 case greenland::IntrinsicHelper::ConstFloat:
3000 case greenland::IntrinsicHelper::ConstDouble:
3001 cvtConst(cUnit, callInst);
3002 break;
3003 case greenland::IntrinsicHelper::DivInt:
3004 case greenland::IntrinsicHelper::DivLong:
3005 cvtBinOp(cUnit, kOpDiv, inst);
3006 break;
3007 case greenland::IntrinsicHelper::RemInt:
3008 case greenland::IntrinsicHelper::RemLong:
3009 cvtBinOp(cUnit, kOpRem, inst);
3010 break;
3011 case greenland::IntrinsicHelper::MethodInfo:
3012 // Already dealt with - just ignore it here.
3013 break;
3014 case greenland::IntrinsicHelper::CheckSuspend:
3015 genSuspendTest(cUnit, 0 /* optFlags already applied */);
3016 break;
3017 case greenland::IntrinsicHelper::HLInvokeObj:
3018 case greenland::IntrinsicHelper::HLInvokeFloat:
3019 case greenland::IntrinsicHelper::HLInvokeDouble:
3020 case greenland::IntrinsicHelper::HLInvokeLong:
3021 case greenland::IntrinsicHelper::HLInvokeInt:
3022 cvtInvoke(cUnit, callInst, false /* isVoid */, false /* newArray */);
3023 break;
3024 case greenland::IntrinsicHelper::HLInvokeVoid:
3025 cvtInvoke(cUnit, callInst, true /* isVoid */, false /* newArray */);
3026 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003027 case greenland::IntrinsicHelper::HLFilledNewArray:
buzbee0967a252012-09-14 10:43:54 -07003028 cvtInvoke(cUnit, callInst, false /* isVoid */, true /* newArray */);
3029 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003030 case greenland::IntrinsicHelper::HLFillArrayData:
buzbee0967a252012-09-14 10:43:54 -07003031 cvtFillArrayData(cUnit, callInst);
3032 break;
3033 case greenland::IntrinsicHelper::ConstString:
3034 cvtConstObject(cUnit, callInst, true /* isString */);
3035 break;
3036 case greenland::IntrinsicHelper::ConstClass:
3037 cvtConstObject(cUnit, callInst, false /* isString */);
3038 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003039 case greenland::IntrinsicHelper::HLCheckCast:
buzbee0967a252012-09-14 10:43:54 -07003040 cvtCheckCast(cUnit, callInst);
3041 break;
3042 case greenland::IntrinsicHelper::NewInstance:
3043 cvtNewInstance(cUnit, callInst);
3044 break;
3045 case greenland::IntrinsicHelper::HLSgetObject:
3046 cvtSget(cUnit, callInst, false /* wide */, true /* Object */);
3047 break;
3048 case greenland::IntrinsicHelper::HLSget:
3049 case greenland::IntrinsicHelper::HLSgetFloat:
3050 case greenland::IntrinsicHelper::HLSgetBoolean:
3051 case greenland::IntrinsicHelper::HLSgetByte:
3052 case greenland::IntrinsicHelper::HLSgetChar:
3053 case greenland::IntrinsicHelper::HLSgetShort:
3054 cvtSget(cUnit, callInst, false /* wide */, false /* Object */);
3055 break;
3056 case greenland::IntrinsicHelper::HLSgetWide:
3057 case greenland::IntrinsicHelper::HLSgetDouble:
3058 cvtSget(cUnit, callInst, true /* wide */, false /* Object */);
3059 break;
3060 case greenland::IntrinsicHelper::HLSput:
3061 case greenland::IntrinsicHelper::HLSputFloat:
3062 case greenland::IntrinsicHelper::HLSputBoolean:
3063 case greenland::IntrinsicHelper::HLSputByte:
3064 case greenland::IntrinsicHelper::HLSputChar:
3065 case greenland::IntrinsicHelper::HLSputShort:
3066 cvtSput(cUnit, callInst, false /* wide */, false /* Object */);
3067 break;
3068 case greenland::IntrinsicHelper::HLSputWide:
3069 case greenland::IntrinsicHelper::HLSputDouble:
3070 cvtSput(cUnit, callInst, true /* wide */, false /* Object */);
3071 break;
3072 case greenland::IntrinsicHelper::HLSputObject:
3073 cvtSput(cUnit, callInst, false /* wide */, true /* Object */);
3074 break;
3075 case greenland::IntrinsicHelper::GetException:
3076 cvtMoveException(cUnit, callInst);
3077 break;
TDYa127f71bf5a2012-07-29 20:09:52 -07003078 case greenland::IntrinsicHelper::HLThrowException:
buzbee0967a252012-09-14 10:43:54 -07003079 cvtThrow(cUnit, callInst);
3080 break;
3081 case greenland::IntrinsicHelper::MonitorEnter:
3082 cvtMonitorEnterExit(cUnit, true /* isEnter */, callInst);
3083 break;
3084 case greenland::IntrinsicHelper::MonitorExit:
3085 cvtMonitorEnterExit(cUnit, false /* isEnter */, callInst);
3086 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003087 case greenland::IntrinsicHelper::OptArrayLength:
buzbee0967a252012-09-14 10:43:54 -07003088 cvtArrayLength(cUnit, callInst);
3089 break;
3090 case greenland::IntrinsicHelper::NewArray:
3091 cvtNewArray(cUnit, callInst);
3092 break;
3093 case greenland::IntrinsicHelper::InstanceOf:
3094 cvtInstanceOf(cUnit, callInst);
3095 break;
3096
3097 case greenland::IntrinsicHelper::HLArrayGet:
3098 case greenland::IntrinsicHelper::HLArrayGetObject:
3099 case greenland::IntrinsicHelper::HLArrayGetFloat:
3100 cvtAget(cUnit, callInst, kWord, 2);
3101 break;
3102 case greenland::IntrinsicHelper::HLArrayGetWide:
3103 case greenland::IntrinsicHelper::HLArrayGetDouble:
3104 cvtAget(cUnit, callInst, kLong, 3);
3105 break;
3106 case greenland::IntrinsicHelper::HLArrayGetBoolean:
3107 cvtAget(cUnit, callInst, kUnsignedByte, 0);
3108 break;
3109 case greenland::IntrinsicHelper::HLArrayGetByte:
3110 cvtAget(cUnit, callInst, kSignedByte, 0);
3111 break;
3112 case greenland::IntrinsicHelper::HLArrayGetChar:
3113 cvtAget(cUnit, callInst, kUnsignedHalf, 1);
3114 break;
3115 case greenland::IntrinsicHelper::HLArrayGetShort:
3116 cvtAget(cUnit, callInst, kSignedHalf, 1);
3117 break;
3118
3119 case greenland::IntrinsicHelper::HLArrayPut:
3120 case greenland::IntrinsicHelper::HLArrayPutFloat:
3121 cvtAputPrimitive(cUnit, callInst, kWord, 2);
3122 break;
3123 case greenland::IntrinsicHelper::HLArrayPutObject:
3124 cvtAputObj(cUnit, callInst);
3125 break;
3126 case greenland::IntrinsicHelper::HLArrayPutWide:
3127 case greenland::IntrinsicHelper::HLArrayPutDouble:
3128 cvtAputPrimitive(cUnit, callInst, kLong, 3);
3129 break;
3130 case greenland::IntrinsicHelper::HLArrayPutBoolean:
3131 cvtAputPrimitive(cUnit, callInst, kUnsignedByte, 0);
3132 break;
3133 case greenland::IntrinsicHelper::HLArrayPutByte:
3134 cvtAputPrimitive(cUnit, callInst, kSignedByte, 0);
3135 break;
3136 case greenland::IntrinsicHelper::HLArrayPutChar:
3137 cvtAputPrimitive(cUnit, callInst, kUnsignedHalf, 1);
3138 break;
3139 case greenland::IntrinsicHelper::HLArrayPutShort:
3140 cvtAputPrimitive(cUnit, callInst, kSignedHalf, 1);
3141 break;
3142
3143 case greenland::IntrinsicHelper::HLIGet:
3144 case greenland::IntrinsicHelper::HLIGetFloat:
3145 cvtIget(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3146 break;
3147 case greenland::IntrinsicHelper::HLIGetObject:
3148 cvtIget(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3149 break;
3150 case greenland::IntrinsicHelper::HLIGetWide:
3151 case greenland::IntrinsicHelper::HLIGetDouble:
3152 cvtIget(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3153 break;
3154 case greenland::IntrinsicHelper::HLIGetBoolean:
3155 cvtIget(cUnit, callInst, kUnsignedByte, false /* isWide */,
3156 false /* obj */);
3157 break;
3158 case greenland::IntrinsicHelper::HLIGetByte:
3159 cvtIget(cUnit, callInst, kSignedByte, false /* isWide */,
3160 false /* obj */);
3161 break;
3162 case greenland::IntrinsicHelper::HLIGetChar:
3163 cvtIget(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3164 false /* obj */);
3165 break;
3166 case greenland::IntrinsicHelper::HLIGetShort:
3167 cvtIget(cUnit, callInst, kSignedHalf, false /* isWide */,
3168 false /* obj */);
3169 break;
3170
3171 case greenland::IntrinsicHelper::HLIPut:
3172 case greenland::IntrinsicHelper::HLIPutFloat:
3173 cvtIput(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3174 break;
3175 case greenland::IntrinsicHelper::HLIPutObject:
3176 cvtIput(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3177 break;
3178 case greenland::IntrinsicHelper::HLIPutWide:
3179 case greenland::IntrinsicHelper::HLIPutDouble:
3180 cvtIput(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3181 break;
3182 case greenland::IntrinsicHelper::HLIPutBoolean:
3183 cvtIput(cUnit, callInst, kUnsignedByte, false /* isWide */,
3184 false /* obj */);
3185 break;
3186 case greenland::IntrinsicHelper::HLIPutByte:
3187 cvtIput(cUnit, callInst, kSignedByte, false /* isWide */,
3188 false /* obj */);
3189 break;
3190 case greenland::IntrinsicHelper::HLIPutChar:
3191 cvtIput(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3192 false /* obj */);
3193 break;
3194 case greenland::IntrinsicHelper::HLIPutShort:
3195 cvtIput(cUnit, callInst, kSignedHalf, false /* isWide */,
3196 false /* obj */);
3197 break;
3198
3199 case greenland::IntrinsicHelper::IntToChar:
3200 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_CHAR);
3201 break;
3202 case greenland::IntrinsicHelper::IntToShort:
3203 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_SHORT);
3204 break;
3205 case greenland::IntrinsicHelper::IntToByte:
3206 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_BYTE);
3207 break;
3208
TDYa1274ec8ccd2012-08-11 07:04:57 -07003209 case greenland::IntrinsicHelper::F2I:
3210 case greenland::IntrinsicHelper::D2I:
3211 case greenland::IntrinsicHelper::F2L:
3212 case greenland::IntrinsicHelper::D2L:
3213 cvtFPToInt(cUnit, callInst);
3214 break;
3215
buzbee0967a252012-09-14 10:43:54 -07003216 case greenland::IntrinsicHelper::CmplFloat:
3217 cvtFPCompare(cUnit, callInst, Instruction::CMPL_FLOAT);
3218 break;
3219 case greenland::IntrinsicHelper::CmpgFloat:
3220 cvtFPCompare(cUnit, callInst, Instruction::CMPG_FLOAT);
3221 break;
3222 case greenland::IntrinsicHelper::CmplDouble:
3223 cvtFPCompare(cUnit, callInst, Instruction::CMPL_DOUBLE);
3224 break;
3225 case greenland::IntrinsicHelper::CmpgDouble:
3226 cvtFPCompare(cUnit, callInst, Instruction::CMPG_DOUBLE);
3227 break;
3228
3229 case greenland::IntrinsicHelper::CmpLong:
3230 cvtLongCompare(cUnit, callInst);
3231 break;
3232
3233 case greenland::IntrinsicHelper::SHLLong:
3234 cvtShiftOp(cUnit, Instruction::SHL_LONG, callInst);
3235 break;
3236 case greenland::IntrinsicHelper::SHRLong:
3237 cvtShiftOp(cUnit, Instruction::SHR_LONG, callInst);
3238 break;
3239 case greenland::IntrinsicHelper::USHRLong:
3240 cvtShiftOp(cUnit, Instruction::USHR_LONG, callInst);
3241 break;
3242 case greenland::IntrinsicHelper::SHLInt:
3243 cvtShiftOp(cUnit, Instruction::SHL_INT, callInst);
3244 break;
3245 case greenland::IntrinsicHelper::SHRInt:
3246 cvtShiftOp(cUnit, Instruction::SHR_INT, callInst);
3247 break;
3248 case greenland::IntrinsicHelper::USHRInt:
3249 cvtShiftOp(cUnit, Instruction::USHR_INT, callInst);
3250 break;
3251
3252 case greenland::IntrinsicHelper::CatchTargets: {
3253 llvm::SwitchInst* swInst =
3254 llvm::dyn_cast<llvm::SwitchInst>(nextIt);
3255 DCHECK(swInst != NULL);
3256 /*
3257 * Discard the edges and the following conditional branch.
3258 * Do a direct branch to the default target (which is the
3259 * "work" portion of the pair.
3260 * TODO: awful code layout - rework
3261 */
3262 llvm::BasicBlock* targetBB = swInst->getDefaultDest();
3263 DCHECK(targetBB != NULL);
3264 opUnconditionalBranch(cUnit,
3265 cUnit->blockToLabelMap.Get(targetBB));
3266 ++it;
3267 // Set next bb to default target - improves code layout
3268 nextBB = targetBB;
3269 }
3270 break;
3271
3272 default:
3273 LOG(FATAL) << "Unexpected intrinsic " << (int)id << ", "
3274 << cUnit->intrinsic_helper->GetName(id);
3275 }
3276 }
3277 break;
3278
3279 case llvm::Instruction::Br: cvtBr(cUnit, inst); break;
3280 case llvm::Instruction::Add: cvtBinOp(cUnit, kOpAdd, inst); break;
3281 case llvm::Instruction::Sub: cvtBinOp(cUnit, kOpSub, inst); break;
3282 case llvm::Instruction::Mul: cvtBinOp(cUnit, kOpMul, inst); break;
3283 case llvm::Instruction::SDiv: cvtBinOp(cUnit, kOpDiv, inst); break;
3284 case llvm::Instruction::SRem: cvtBinOp(cUnit, kOpRem, inst); break;
3285 case llvm::Instruction::And: cvtBinOp(cUnit, kOpAnd, inst); break;
3286 case llvm::Instruction::Or: cvtBinOp(cUnit, kOpOr, inst); break;
3287 case llvm::Instruction::Xor: cvtBinOp(cUnit, kOpXor, inst); break;
3288 case llvm::Instruction::PHI: cvtPhi(cUnit, inst); break;
3289 case llvm::Instruction::Ret: cvtRet(cUnit, inst); break;
3290 case llvm::Instruction::FAdd: cvtBinFPOp(cUnit, kOpAdd, inst); break;
3291 case llvm::Instruction::FSub: cvtBinFPOp(cUnit, kOpSub, inst); break;
3292 case llvm::Instruction::FMul: cvtBinFPOp(cUnit, kOpMul, inst); break;
3293 case llvm::Instruction::FDiv: cvtBinFPOp(cUnit, kOpDiv, inst); break;
3294 case llvm::Instruction::FRem: cvtBinFPOp(cUnit, kOpRem, inst); break;
3295 case llvm::Instruction::SIToFP: cvtIntToFP(cUnit, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003296 case llvm::Instruction::FPTrunc: cvtDoubleToFloat(cUnit, inst); break;
3297 case llvm::Instruction::FPExt: cvtFloatToDouble(cUnit, inst); break;
3298 case llvm::Instruction::Trunc: cvtTrunc(cUnit, inst); break;
3299
3300 case llvm::Instruction::ZExt: cvtIntExt(cUnit, inst, false /* signed */);
3301 break;
3302 case llvm::Instruction::SExt: cvtIntExt(cUnit, inst, true /* signed */);
3303 break;
3304
3305 case llvm::Instruction::Switch: cvtSwitch(cUnit, inst); break;
3306
3307 case llvm::Instruction::Unreachable:
3308 break; // FIXME: can we really ignore these?
3309
3310 case llvm::Instruction::Shl:
3311 case llvm::Instruction::LShr:
3312 case llvm::Instruction::AShr:
3313 case llvm::Instruction::Invoke:
3314 case llvm::Instruction::FPToUI:
TDYa1274ec8ccd2012-08-11 07:04:57 -07003315 case llvm::Instruction::FPToSI:
buzbee0967a252012-09-14 10:43:54 -07003316 case llvm::Instruction::UIToFP:
3317 case llvm::Instruction::PtrToInt:
3318 case llvm::Instruction::IntToPtr:
3319 case llvm::Instruction::FCmp:
3320 case llvm::Instruction::URem:
3321 case llvm::Instruction::UDiv:
3322 case llvm::Instruction::Resume:
3323 case llvm::Instruction::Alloca:
3324 case llvm::Instruction::GetElementPtr:
3325 case llvm::Instruction::Fence:
3326 case llvm::Instruction::AtomicCmpXchg:
3327 case llvm::Instruction::AtomicRMW:
3328 case llvm::Instruction::BitCast:
3329 case llvm::Instruction::VAArg:
3330 case llvm::Instruction::Select:
3331 case llvm::Instruction::UserOp1:
3332 case llvm::Instruction::UserOp2:
3333 case llvm::Instruction::ExtractElement:
3334 case llvm::Instruction::InsertElement:
3335 case llvm::Instruction::ShuffleVector:
3336 case llvm::Instruction::ExtractValue:
3337 case llvm::Instruction::InsertValue:
3338 case llvm::Instruction::LandingPad:
3339 case llvm::Instruction::IndirectBr:
3340 case llvm::Instruction::Load:
3341 case llvm::Instruction::Store:
3342 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3343
3344 default:
3345 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3346 break;
buzbeead8f15e2012-06-18 14:49:45 -07003347 }
3348 }
buzbee2cfc6392012-05-07 14:51:40 -07003349
buzbee0967a252012-09-14 10:43:54 -07003350 if (headLIR != NULL) {
3351 oatApplyLocalOptimizations(cUnit, headLIR, cUnit->lastLIRInsn);
buzbee2cfc6392012-05-07 14:51:40 -07003352 }
buzbee0967a252012-09-14 10:43:54 -07003353 if (nextBB != NULL) {
3354 bb = nextBB;
3355 nextBB = NULL;
buzbee6969d502012-06-15 16:40:31 -07003356 }
buzbee6969d502012-06-15 16:40:31 -07003357 }
buzbee2cfc6392012-05-07 14:51:40 -07003358 return false;
3359}
3360
3361/*
3362 * Convert LLVM_IR to MIR:
3363 * o Iterate through the LLVM_IR and construct a graph using
3364 * standard MIR building blocks.
3365 * o Perform a basic-block optimization pass to remove unnecessary
3366 * store/load sequences.
3367 * o Convert the LLVM Value operands into RegLocations where applicable.
3368 * o Create ssaRep def/use operand arrays for each converted LLVM opcode
3369 * o Perform register promotion
3370 * o Iterate through the graph a basic block at a time, generating
3371 * LIR.
3372 * o Assemble LIR as usual.
3373 * o Profit.
3374 */
3375void oatMethodBitcode2LIR(CompilationUnit* cUnit)
3376{
buzbeead8f15e2012-06-18 14:49:45 -07003377 llvm::Function* func = cUnit->func;
3378 int numBasicBlocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003379 // Allocate a list for LIR basic block labels
3380 cUnit->blockLabelList =
buzbeea1da8a52012-07-09 14:00:21 -07003381 (LIR*)oatNew(cUnit, sizeof(LIR) * numBasicBlocks, true, kAllocLIR);
3382 LIR* labelList = cUnit->blockLabelList;
buzbee2cfc6392012-05-07 14:51:40 -07003383 int nextLabel = 0;
buzbeead8f15e2012-06-18 14:49:45 -07003384 for (llvm::Function::iterator i = func->begin(),
3385 e = func->end(); i != e; ++i) {
buzbee2cfc6392012-05-07 14:51:40 -07003386 cUnit->blockToLabelMap.Put(static_cast<llvm::BasicBlock*>(i),
3387 &labelList[nextLabel++]);
3388 }
buzbeead8f15e2012-06-18 14:49:45 -07003389
3390 /*
3391 * Keep honest - clear regLocations, Value => RegLocation,
3392 * promotion map and VmapTables.
3393 */
3394 cUnit->locMap.clear(); // Start fresh
3395 cUnit->regLocation = NULL;
3396 for (int i = 0; i < cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
3397 i++) {
3398 cUnit->promotionMap[i].coreLocation = kLocDalvikFrame;
3399 cUnit->promotionMap[i].fpLocation = kLocDalvikFrame;
3400 }
3401 cUnit->coreSpillMask = 0;
3402 cUnit->numCoreSpills = 0;
3403 cUnit->fpSpillMask = 0;
3404 cUnit->numFPSpills = 0;
3405 cUnit->coreVmapTable.clear();
3406 cUnit->fpVmapTable.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003407
3408 /*
3409 * At this point, we've lost all knowledge of register promotion.
3410 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003411 * exists - not required for correctness). Normally, this will
3412 * be the first instruction we encounter, so we won't have to iterate
3413 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003414 */
buzbeeca7a5e42012-08-20 11:12:18 -07003415 for (llvm::inst_iterator i = llvm::inst_begin(func),
3416 e = llvm::inst_end(func); i != e; ++i) {
3417 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(&*i);
3418 if (callInst != NULL) {
3419 llvm::Function* callee = callInst->getCalledFunction();
3420 greenland::IntrinsicHelper::IntrinsicId id =
3421 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3422 if (id == greenland::IntrinsicHelper::MethodInfo) {
3423 if (cUnit->printMe) {
3424 LOG(INFO) << "Found MethodInfo";
3425 }
3426 llvm::MDNode* regInfoNode = callInst->getMetadata("RegInfo");
3427 if (regInfoNode != NULL) {
3428 llvm::ConstantInt* numInsValue =
3429 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(0));
3430 llvm::ConstantInt* numRegsValue =
3431 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(1));
3432 llvm::ConstantInt* numOutsValue =
3433 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(2));
3434 llvm::ConstantInt* numCompilerTempsValue =
3435 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(3));
3436 llvm::ConstantInt* numSSARegsValue =
3437 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(4));
3438 if (cUnit->printMe) {
3439 LOG(INFO) << "RegInfo - Ins:" << numInsValue->getZExtValue()
3440 << ", Regs:" << numRegsValue->getZExtValue()
3441 << ", Outs:" << numOutsValue->getZExtValue()
3442 << ", CTemps:" << numCompilerTempsValue->getZExtValue()
3443 << ", SSARegs:" << numSSARegsValue->getZExtValue();
3444 }
3445 }
3446 llvm::MDNode* pmapInfoNode = callInst->getMetadata("PromotionMap");
3447 if (pmapInfoNode != NULL) {
3448 int elems = pmapInfoNode->getNumOperands();
3449 if (cUnit->printMe) {
3450 LOG(INFO) << "PMap size: " << elems;
3451 }
3452 for (int i = 0; i < elems; i++) {
3453 llvm::ConstantInt* rawMapData =
3454 static_cast<llvm::ConstantInt*>(pmapInfoNode->getOperand(i));
3455 uint32_t mapData = rawMapData->getZExtValue();
3456 PromotionMap* p = &cUnit->promotionMap[i];
3457 p->firstInPair = (mapData >> 24) & 0xff;
3458 p->fpReg = (mapData >> 16) & 0xff;
3459 p->coreReg = (mapData >> 8) & 0xff;
3460 p->fpLocation = static_cast<RegLocationType>((mapData >> 4) & 0xf);
3461 if (p->fpLocation == kLocPhysReg) {
3462 oatRecordFpPromotion(cUnit, p->fpReg, i);
3463 }
3464 p->coreLocation = static_cast<RegLocationType>(mapData & 0xf);
3465 if (p->coreLocation == kLocPhysReg) {
3466 oatRecordCorePromotion(cUnit, p->coreReg, i);
3467 }
3468 }
3469 if (cUnit->printMe) {
3470 oatDumpPromotionMap(cUnit);
3471 }
3472 }
3473 break;
3474 }
3475 }
3476 }
3477 oatAdjustSpillMask(cUnit);
3478 cUnit->frameSize = oatComputeFrameSize(cUnit);
buzbeead8f15e2012-06-18 14:49:45 -07003479
3480 // Create RegLocations for arguments
3481 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
3482 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
3483 for (; it != it_end; ++it) {
3484 llvm::Value* val = it;
3485 createLocFromValue(cUnit, val);
3486 }
3487 // Create RegLocations for all non-argument defintions
3488 for (llvm::inst_iterator i = llvm::inst_begin(func),
3489 e = llvm::inst_end(func); i != e; ++i) {
3490 llvm::Value* val = &*i;
3491 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
3492 createLocFromValue(cUnit, val);
3493 }
3494 }
3495
buzbee2cfc6392012-05-07 14:51:40 -07003496 // Walk the blocks, generating code.
3497 for (llvm::Function::iterator i = cUnit->func->begin(),
3498 e = cUnit->func->end(); i != e; ++i) {
3499 methodBitcodeBlockCodeGen(cUnit, static_cast<llvm::BasicBlock*>(i));
3500 }
3501
3502 handleSuspendLaunchpads(cUnit);
3503
3504 handleThrowLaunchpads(cUnit);
3505
3506 handleIntrinsicLaunchpads(cUnit);
3507
buzbee692be802012-08-29 15:52:59 -07003508 cUnit->func->eraseFromParent();
3509 cUnit->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003510}
3511
3512
3513} // namespace art
3514
3515#endif // ART_USE_QUICK_COMPILER