blob: fcf2151a74b418c0e345e79c3d9a11cecdc8f226 [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(
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700343 greenland::IntrinsicHelper::ThrowException);
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
797void convertFPToInt(CompilationUnit* cUnit, llvm::Type* ty, RegLocation rlDest,
798 RegLocation rlSrc)
799{
800 llvm::Value* res =
801 cUnit->irb->CreateFPToSI(getLLVMValue(cUnit, rlSrc.origSReg), ty);
802 defineValue(cUnit, res, rlDest.origSReg);
803}
804
805
806void convertNegFP(CompilationUnit* cUnit, RegLocation rlDest,
807 RegLocation rlSrc)
808{
809 llvm::Value* res =
810 cUnit->irb->CreateFNeg(getLLVMValue(cUnit, rlSrc.origSReg));
811 defineValue(cUnit, res, rlDest.origSReg);
812}
813
814void convertNot(CompilationUnit* cUnit, RegLocation rlDest,
815 RegLocation rlSrc)
816{
817 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
818 llvm::Value* res = cUnit->irb->CreateXor(src, static_cast<uint64_t>(-1));
buzbee101305f2012-06-28 18:00:56 -0700819 defineValue(cUnit, res, rlDest.origSReg);
820}
821
buzbee2cfc6392012-05-07 14:51:40 -0700822/*
823 * Target-independent code generation. Use only high-level
824 * load/store utilities here, or target-dependent genXX() handlers
825 * when necessary.
826 */
827bool convertMIRNode(CompilationUnit* cUnit, MIR* mir, BasicBlock* bb,
828 llvm::BasicBlock* llvmBB, LIR* labelList)
829{
830 bool res = false; // Assume success
831 RegLocation rlSrc[3];
832 RegLocation rlDest = badLoc;
buzbee2cfc6392012-05-07 14:51:40 -0700833 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee6969d502012-06-15 16:40:31 -0700834 uint32_t vB = mir->dalvikInsn.vB;
835 uint32_t vC = mir->dalvikInsn.vC;
buzbee8fa0fda2012-06-27 15:44:52 -0700836 int optFlags = mir->optimizationFlags;
buzbee6969d502012-06-15 16:40:31 -0700837
buzbeeb03f4872012-06-11 15:22:11 -0700838 bool objectDefinition = false;
buzbee2cfc6392012-05-07 14:51:40 -0700839
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700840 if (cUnit->printMe) {
841 if ((int)opcode < kMirOpFirst) {
842 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x"
843 << std::hex << (int)opcode;
844 } else {
845 LOG(INFO) << ".. opcode 0x" << std::hex << (int)opcode;
846 }
847 }
848
buzbee2cfc6392012-05-07 14:51:40 -0700849 /* Prep Src and Dest locations */
850 int nextSreg = 0;
851 int nextLoc = 0;
852 int attrs = oatDataFlowAttributes[opcode];
853 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
854 if (attrs & DF_UA) {
855 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700856 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700857 nextSreg+= 2;
858 } else {
859 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
860 nextSreg++;
861 }
862 }
863 if (attrs & DF_UB) {
864 if (attrs & DF_B_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700865 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700866 nextSreg+= 2;
867 } else {
868 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
869 nextSreg++;
870 }
871 }
872 if (attrs & DF_UC) {
873 if (attrs & DF_C_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700874 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700875 } else {
876 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
877 }
878 }
879 if (attrs & DF_DA) {
880 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700881 rlDest = oatGetDestWide(cUnit, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700882 } else {
buzbee15bf9802012-06-12 17:49:27 -0700883 rlDest = oatGetDest(cUnit, mir);
buzbeeb03f4872012-06-11 15:22:11 -0700884 if (rlDest.ref) {
885 objectDefinition = true;
886 }
buzbee2cfc6392012-05-07 14:51:40 -0700887 }
888 }
889
890 switch (opcode) {
891 case Instruction::NOP:
892 break;
893
894 case Instruction::MOVE:
895 case Instruction::MOVE_OBJECT:
896 case Instruction::MOVE_16:
897 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700898 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700899 case Instruction::MOVE_FROM16:
900 case Instruction::MOVE_WIDE:
901 case Instruction::MOVE_WIDE_16:
902 case Instruction::MOVE_WIDE_FROM16: {
903 /*
904 * Moves/copies are meaningless in pure SSA register form,
905 * but we need to preserve them for the conversion back into
906 * MIR (at least until we stop using the Dalvik register maps).
907 * Insert a dummy intrinsic copy call, which will be recognized
908 * by the quick path and removed by the portable path.
909 */
910 llvm::Value* src = getLLVMValue(cUnit, rlSrc[0].origSReg);
911 llvm::Value* res = emitCopy(cUnit, src, rlDest);
912 defineValue(cUnit, res, rlDest.origSReg);
913 }
914 break;
915
916 case Instruction::CONST:
917 case Instruction::CONST_4:
918 case Instruction::CONST_16: {
buzbee6969d502012-06-15 16:40:31 -0700919 llvm::Constant* immValue = cUnit->irb->GetJInt(vB);
buzbee2cfc6392012-05-07 14:51:40 -0700920 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
921 defineValue(cUnit, res, rlDest.origSReg);
922 }
923 break;
924
925 case Instruction::CONST_WIDE_16:
926 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700927 // Sign extend to 64 bits
928 int64_t imm = static_cast<int32_t>(vB);
929 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
buzbee2cfc6392012-05-07 14:51:40 -0700930 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
931 defineValue(cUnit, res, rlDest.origSReg);
932 }
933 break;
934
935 case Instruction::CONST_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700936 llvm::Constant* immValue = cUnit->irb->GetJInt(vB << 16);
buzbee2cfc6392012-05-07 14:51:40 -0700937 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
938 defineValue(cUnit, res, rlDest.origSReg);
939 }
940 break;
941
942 case Instruction::CONST_WIDE: {
943 llvm::Constant* immValue =
944 cUnit->irb->GetJLong(mir->dalvikInsn.vB_wide);
945 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
946 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700947 }
948 break;
buzbee2cfc6392012-05-07 14:51:40 -0700949 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700950 int64_t imm = static_cast<int64_t>(vB) << 48;
buzbee2cfc6392012-05-07 14:51:40 -0700951 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
952 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
953 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700954 }
955 break;
956
buzbee8fa0fda2012-06-27 15:44:52 -0700957 case Instruction::SPUT_OBJECT:
buzbee76592632012-06-29 15:18:35 -0700958 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputObject,
buzbee8fa0fda2012-06-27 15:44:52 -0700959 rlSrc[0]);
960 break;
961 case Instruction::SPUT:
962 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -0700963 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputFloat,
buzbee8fa0fda2012-06-27 15:44:52 -0700964 rlSrc[0]);
965 } else {
buzbee76592632012-06-29 15:18:35 -0700966 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSput, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700967 }
968 break;
969 case Instruction::SPUT_BOOLEAN:
buzbee76592632012-06-29 15:18:35 -0700970 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputBoolean,
buzbee8fa0fda2012-06-27 15:44:52 -0700971 rlSrc[0]);
972 break;
973 case Instruction::SPUT_BYTE:
buzbee76592632012-06-29 15:18:35 -0700974 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputByte, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700975 break;
976 case Instruction::SPUT_CHAR:
buzbee76592632012-06-29 15:18:35 -0700977 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputChar, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700978 break;
979 case Instruction::SPUT_SHORT:
buzbee76592632012-06-29 15:18:35 -0700980 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputShort, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700981 break;
982 case Instruction::SPUT_WIDE:
983 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -0700984 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputDouble,
buzbee8fa0fda2012-06-27 15:44:52 -0700985 rlSrc[0]);
986 } else {
buzbee76592632012-06-29 15:18:35 -0700987 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputWide,
buzbee8fa0fda2012-06-27 15:44:52 -0700988 rlSrc[0]);
989 }
990 break;
991
992 case Instruction::SGET_OBJECT:
993 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetObject, rlDest);
994 break;
995 case Instruction::SGET:
996 if (rlDest.fp) {
997 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetFloat, rlDest);
998 } else {
999 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSget, rlDest);
1000 }
1001 break;
1002 case Instruction::SGET_BOOLEAN:
1003 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetBoolean, rlDest);
1004 break;
1005 case Instruction::SGET_BYTE:
1006 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetByte, rlDest);
1007 break;
1008 case Instruction::SGET_CHAR:
1009 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetChar, rlDest);
1010 break;
1011 case Instruction::SGET_SHORT:
1012 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetShort, rlDest);
1013 break;
1014 case Instruction::SGET_WIDE:
1015 if (rlDest.fp) {
1016 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetDouble,
1017 rlDest);
1018 } else {
1019 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetWide, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001020 }
1021 break;
buzbee2cfc6392012-05-07 14:51:40 -07001022
1023 case Instruction::RETURN_WIDE:
1024 case Instruction::RETURN:
1025 case Instruction::RETURN_OBJECT: {
TDYa1274f2935e2012-06-22 06:25:03 -07001026 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001027 emitSuspendCheck(cUnit);
1028 }
buzbeeb03f4872012-06-11 15:22:11 -07001029 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001030 cUnit->irb->CreateRet(getLLVMValue(cUnit, rlSrc[0].origSReg));
1031 bb->hasReturn = true;
1032 }
1033 break;
1034
1035 case Instruction::RETURN_VOID: {
TDYa1274f2935e2012-06-22 06:25:03 -07001036 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001037 emitSuspendCheck(cUnit);
1038 }
buzbeeb03f4872012-06-11 15:22:11 -07001039 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001040 cUnit->irb->CreateRetVoid();
1041 bb->hasReturn = true;
1042 }
1043 break;
1044
1045 case Instruction::IF_EQ:
1046 convertCompareAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0], rlSrc[1]);
1047 break;
1048 case Instruction::IF_NE:
1049 convertCompareAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0], rlSrc[1]);
1050 break;
1051 case Instruction::IF_LT:
1052 convertCompareAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0], rlSrc[1]);
1053 break;
1054 case Instruction::IF_GE:
1055 convertCompareAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0], rlSrc[1]);
1056 break;
1057 case Instruction::IF_GT:
1058 convertCompareAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0], rlSrc[1]);
1059 break;
1060 case Instruction::IF_LE:
1061 convertCompareAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0], rlSrc[1]);
1062 break;
1063 case Instruction::IF_EQZ:
1064 convertCompareZeroAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0]);
1065 break;
1066 case Instruction::IF_NEZ:
1067 convertCompareZeroAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0]);
1068 break;
1069 case Instruction::IF_LTZ:
1070 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0]);
1071 break;
1072 case Instruction::IF_GEZ:
1073 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0]);
1074 break;
1075 case Instruction::IF_GTZ:
1076 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0]);
1077 break;
1078 case Instruction::IF_LEZ:
1079 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0]);
1080 break;
1081
1082 case Instruction::GOTO:
1083 case Instruction::GOTO_16:
1084 case Instruction::GOTO_32: {
1085 if (bb->taken->startOffset <= bb->startOffset) {
1086 emitSuspendCheck(cUnit);
1087 }
1088 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->taken->id));
1089 }
1090 break;
1091
1092 case Instruction::ADD_LONG:
1093 case Instruction::ADD_LONG_2ADDR:
1094 case Instruction::ADD_INT:
1095 case Instruction::ADD_INT_2ADDR:
1096 convertArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1097 break;
1098 case Instruction::SUB_LONG:
1099 case Instruction::SUB_LONG_2ADDR:
1100 case Instruction::SUB_INT:
1101 case Instruction::SUB_INT_2ADDR:
1102 convertArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1103 break;
1104 case Instruction::MUL_LONG:
1105 case Instruction::MUL_LONG_2ADDR:
1106 case Instruction::MUL_INT:
1107 case Instruction::MUL_INT_2ADDR:
1108 convertArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1109 break;
1110 case Instruction::DIV_LONG:
1111 case Instruction::DIV_LONG_2ADDR:
1112 case Instruction::DIV_INT:
1113 case Instruction::DIV_INT_2ADDR:
1114 convertArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1115 break;
1116 case Instruction::REM_LONG:
1117 case Instruction::REM_LONG_2ADDR:
1118 case Instruction::REM_INT:
1119 case Instruction::REM_INT_2ADDR:
1120 convertArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1121 break;
1122 case Instruction::AND_LONG:
1123 case Instruction::AND_LONG_2ADDR:
1124 case Instruction::AND_INT:
1125 case Instruction::AND_INT_2ADDR:
1126 convertArithOp(cUnit, kOpAnd, rlDest, rlSrc[0], rlSrc[1]);
1127 break;
1128 case Instruction::OR_LONG:
1129 case Instruction::OR_LONG_2ADDR:
1130 case Instruction::OR_INT:
1131 case Instruction::OR_INT_2ADDR:
1132 convertArithOp(cUnit, kOpOr, rlDest, rlSrc[0], rlSrc[1]);
1133 break;
1134 case Instruction::XOR_LONG:
1135 case Instruction::XOR_LONG_2ADDR:
1136 case Instruction::XOR_INT:
1137 case Instruction::XOR_INT_2ADDR:
1138 convertArithOp(cUnit, kOpXor, rlDest, rlSrc[0], rlSrc[1]);
1139 break;
1140 case Instruction::SHL_LONG:
1141 case Instruction::SHL_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001142 convertShift(cUnit, greenland::IntrinsicHelper::SHLLong,
1143 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001144 break;
buzbee2cfc6392012-05-07 14:51:40 -07001145 case Instruction::SHL_INT:
1146 case Instruction::SHL_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001147 convertShift(cUnit, greenland::IntrinsicHelper::SHLInt,
1148 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001149 break;
1150 case Instruction::SHR_LONG:
1151 case Instruction::SHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001152 convertShift(cUnit, greenland::IntrinsicHelper::SHRLong,
1153 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001154 break;
buzbee2cfc6392012-05-07 14:51:40 -07001155 case Instruction::SHR_INT:
1156 case Instruction::SHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001157 convertShift(cUnit, greenland::IntrinsicHelper::SHRInt,
1158 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001159 break;
1160 case Instruction::USHR_LONG:
1161 case Instruction::USHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001162 convertShift(cUnit, greenland::IntrinsicHelper::USHRLong,
1163 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001164 break;
buzbee2cfc6392012-05-07 14:51:40 -07001165 case Instruction::USHR_INT:
1166 case Instruction::USHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001167 convertShift(cUnit, greenland::IntrinsicHelper::USHRInt,
1168 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001169 break;
1170
1171 case Instruction::ADD_INT_LIT16:
1172 case Instruction::ADD_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001173 convertArithOpLit(cUnit, kOpAdd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001174 break;
1175 case Instruction::RSUB_INT:
1176 case Instruction::RSUB_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001177 convertArithOpLit(cUnit, kOpRsub, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001178 break;
1179 case Instruction::MUL_INT_LIT16:
1180 case Instruction::MUL_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001181 convertArithOpLit(cUnit, kOpMul, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001182 break;
1183 case Instruction::DIV_INT_LIT16:
1184 case Instruction::DIV_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001185 convertArithOpLit(cUnit, kOpDiv, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001186 break;
1187 case Instruction::REM_INT_LIT16:
1188 case Instruction::REM_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001189 convertArithOpLit(cUnit, kOpRem, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001190 break;
1191 case Instruction::AND_INT_LIT16:
1192 case Instruction::AND_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001193 convertArithOpLit(cUnit, kOpAnd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001194 break;
1195 case Instruction::OR_INT_LIT16:
1196 case Instruction::OR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001197 convertArithOpLit(cUnit, kOpOr, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001198 break;
1199 case Instruction::XOR_INT_LIT16:
1200 case Instruction::XOR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001201 convertArithOpLit(cUnit, kOpXor, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001202 break;
1203 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001204 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHLInt,
1205 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001206 break;
1207 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001208 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHRInt,
1209 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001210 break;
1211 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001212 convertShiftLit(cUnit, greenland::IntrinsicHelper::USHRInt,
1213 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001214 break;
1215
1216 case Instruction::ADD_FLOAT:
1217 case Instruction::ADD_FLOAT_2ADDR:
1218 case Instruction::ADD_DOUBLE:
1219 case Instruction::ADD_DOUBLE_2ADDR:
1220 convertFPArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1221 break;
1222
1223 case Instruction::SUB_FLOAT:
1224 case Instruction::SUB_FLOAT_2ADDR:
1225 case Instruction::SUB_DOUBLE:
1226 case Instruction::SUB_DOUBLE_2ADDR:
1227 convertFPArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1228 break;
1229
1230 case Instruction::MUL_FLOAT:
1231 case Instruction::MUL_FLOAT_2ADDR:
1232 case Instruction::MUL_DOUBLE:
1233 case Instruction::MUL_DOUBLE_2ADDR:
1234 convertFPArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1235 break;
1236
1237 case Instruction::DIV_FLOAT:
1238 case Instruction::DIV_FLOAT_2ADDR:
1239 case Instruction::DIV_DOUBLE:
1240 case Instruction::DIV_DOUBLE_2ADDR:
1241 convertFPArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1242 break;
1243
1244 case Instruction::REM_FLOAT:
1245 case Instruction::REM_FLOAT_2ADDR:
1246 case Instruction::REM_DOUBLE:
1247 case Instruction::REM_DOUBLE_2ADDR:
1248 convertFPArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1249 break;
1250
buzbee6969d502012-06-15 16:40:31 -07001251 case Instruction::INVOKE_STATIC:
buzbee101305f2012-06-28 18:00:56 -07001252 convertInvoke(cUnit, bb, mir, kStatic, false /*range*/,
1253 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001254 break;
1255 case Instruction::INVOKE_STATIC_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001256 convertInvoke(cUnit, bb, mir, kStatic, true /*range*/,
1257 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001258 break;
1259
1260 case Instruction::INVOKE_DIRECT:
buzbee101305f2012-06-28 18:00:56 -07001261 convertInvoke(cUnit, bb, mir, kDirect, false /*range*/,
1262 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001263 break;
1264 case Instruction::INVOKE_DIRECT_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001265 convertInvoke(cUnit, bb, mir, kDirect, true /*range*/,
1266 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001267 break;
1268
1269 case Instruction::INVOKE_VIRTUAL:
buzbee101305f2012-06-28 18:00:56 -07001270 convertInvoke(cUnit, bb, mir, kVirtual, false /*range*/,
1271 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001272 break;
1273 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001274 convertInvoke(cUnit, bb, mir, kVirtual, true /*range*/,
1275 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001276 break;
1277
1278 case Instruction::INVOKE_SUPER:
buzbee101305f2012-06-28 18:00:56 -07001279 convertInvoke(cUnit, bb, mir, kSuper, false /*range*/,
1280 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001281 break;
1282 case Instruction::INVOKE_SUPER_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001283 convertInvoke(cUnit, bb, mir, kSuper, true /*range*/,
1284 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001285 break;
1286
1287 case Instruction::INVOKE_INTERFACE:
buzbee101305f2012-06-28 18:00:56 -07001288 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1289 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001290 break;
1291 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001292 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1293 false /* NewFilledArray */);
1294 break;
1295 case Instruction::FILLED_NEW_ARRAY:
1296 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1297 true /* NewFilledArray */);
1298 break;
1299 case Instruction::FILLED_NEW_ARRAY_RANGE:
1300 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1301 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001302 break;
1303
1304 case Instruction::CONST_STRING:
1305 case Instruction::CONST_STRING_JUMBO:
buzbee101305f2012-06-28 18:00:56 -07001306 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstString,
1307 rlDest);
1308 break;
1309
1310 case Instruction::CONST_CLASS:
1311 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstClass,
1312 rlDest);
1313 break;
1314
1315 case Instruction::CHECK_CAST:
1316 convertCheckCast(cUnit, vB, rlSrc[0]);
buzbee6969d502012-06-15 16:40:31 -07001317 break;
1318
buzbee4f1181f2012-06-22 13:52:12 -07001319 case Instruction::NEW_INSTANCE:
buzbee8fa0fda2012-06-27 15:44:52 -07001320 convertNewInstance(cUnit, vB, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001321 break;
1322
buzbee32412962012-06-26 16:27:56 -07001323 case Instruction::MOVE_EXCEPTION:
1324 convertMoveException(cUnit, rlDest);
1325 break;
1326
1327 case Instruction::THROW:
1328 convertThrow(cUnit, rlSrc[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001329 /*
1330 * If this throw is standalone, terminate.
1331 * If it might rethrow, force termination
1332 * of the following block.
1333 */
1334 if (bb->fallThrough == NULL) {
1335 cUnit->irb->CreateUnreachable();
1336 } else {
1337 bb->fallThrough->fallThrough = NULL;
1338 bb->fallThrough->taken = NULL;
1339 }
buzbee32412962012-06-26 16:27:56 -07001340 break;
1341
buzbee2cfc6392012-05-07 14:51:40 -07001342 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001343 case Instruction::MOVE_RESULT:
1344 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001345 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001346 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001347 */
jeffhao9a4f0032012-08-30 16:17:40 -07001348 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001349 break;
1350
1351 case Instruction::MONITOR_ENTER:
buzbee8fa0fda2012-06-27 15:44:52 -07001352 convertMonitorEnterExit(cUnit, optFlags,
1353 greenland::IntrinsicHelper::MonitorEnter,
1354 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001355 break;
1356
1357 case Instruction::MONITOR_EXIT:
buzbee8fa0fda2012-06-27 15:44:52 -07001358 convertMonitorEnterExit(cUnit, optFlags,
1359 greenland::IntrinsicHelper::MonitorExit,
1360 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001361 break;
1362
1363 case Instruction::ARRAY_LENGTH:
buzbee76592632012-06-29 15:18:35 -07001364 convertArrayLength(cUnit, optFlags, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001365 break;
1366
1367 case Instruction::NEW_ARRAY:
1368 convertNewArray(cUnit, vC, rlDest, rlSrc[0]);
1369 break;
1370
1371 case Instruction::INSTANCE_OF:
1372 convertInstanceOf(cUnit, vC, rlDest, rlSrc[0]);
1373 break;
1374
1375 case Instruction::AGET:
1376 if (rlDest.fp) {
1377 convertAget(cUnit, optFlags,
1378 greenland::IntrinsicHelper::HLArrayGetFloat,
1379 rlDest, rlSrc[0], rlSrc[1]);
1380 } else {
1381 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGet,
1382 rlDest, rlSrc[0], rlSrc[1]);
1383 }
1384 break;
1385 case Instruction::AGET_OBJECT:
1386 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetObject,
1387 rlDest, rlSrc[0], rlSrc[1]);
1388 break;
1389 case Instruction::AGET_BOOLEAN:
1390 convertAget(cUnit, optFlags,
1391 greenland::IntrinsicHelper::HLArrayGetBoolean,
1392 rlDest, rlSrc[0], rlSrc[1]);
1393 break;
1394 case Instruction::AGET_BYTE:
1395 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetByte,
1396 rlDest, rlSrc[0], rlSrc[1]);
1397 break;
1398 case Instruction::AGET_CHAR:
1399 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetChar,
1400 rlDest, rlSrc[0], rlSrc[1]);
1401 break;
1402 case Instruction::AGET_SHORT:
1403 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetShort,
1404 rlDest, rlSrc[0], rlSrc[1]);
1405 break;
1406 case Instruction::AGET_WIDE:
1407 if (rlDest.fp) {
1408 convertAget(cUnit, optFlags,
1409 greenland::IntrinsicHelper::HLArrayGetDouble,
1410 rlDest, rlSrc[0], rlSrc[1]);
1411 } else {
1412 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetWide,
1413 rlDest, rlSrc[0], rlSrc[1]);
1414 }
1415 break;
1416
1417 case Instruction::APUT:
1418 if (rlSrc[0].fp) {
1419 convertAput(cUnit, optFlags,
1420 greenland::IntrinsicHelper::HLArrayPutFloat,
1421 rlSrc[0], rlSrc[1], rlSrc[2]);
1422 } else {
1423 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPut,
1424 rlSrc[0], rlSrc[1], rlSrc[2]);
1425 }
1426 break;
1427 case Instruction::APUT_OBJECT:
1428 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutObject,
1429 rlSrc[0], rlSrc[1], rlSrc[2]);
1430 break;
1431 case Instruction::APUT_BOOLEAN:
1432 convertAput(cUnit, optFlags,
1433 greenland::IntrinsicHelper::HLArrayPutBoolean,
1434 rlSrc[0], rlSrc[1], rlSrc[2]);
1435 break;
1436 case Instruction::APUT_BYTE:
1437 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutByte,
1438 rlSrc[0], rlSrc[1], rlSrc[2]);
1439 break;
1440 case Instruction::APUT_CHAR:
1441 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutChar,
1442 rlSrc[0], rlSrc[1], rlSrc[2]);
1443 break;
1444 case Instruction::APUT_SHORT:
1445 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutShort,
1446 rlSrc[0], rlSrc[1], rlSrc[2]);
1447 break;
1448 case Instruction::APUT_WIDE:
1449 if (rlSrc[0].fp) {
1450 convertAput(cUnit, optFlags,
1451 greenland::IntrinsicHelper::HLArrayPutDouble,
1452 rlSrc[0], rlSrc[1], rlSrc[2]);
1453 } else {
1454 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutWide,
1455 rlSrc[0], rlSrc[1], rlSrc[2]);
1456 }
1457 break;
1458
buzbee101305f2012-06-28 18:00:56 -07001459 case Instruction::IGET:
1460 if (rlDest.fp) {
1461 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetFloat,
buzbee4f4dfc72012-07-02 14:54:44 -07001462 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001463 } else {
1464 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGet,
buzbee4f4dfc72012-07-02 14:54:44 -07001465 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001466 }
buzbee2cfc6392012-05-07 14:51:40 -07001467 break;
buzbee101305f2012-06-28 18:00:56 -07001468 case Instruction::IGET_OBJECT:
1469 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetObject,
buzbee4f4dfc72012-07-02 14:54:44 -07001470 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001471 break;
1472 case Instruction::IGET_BOOLEAN:
1473 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetBoolean,
buzbee4f4dfc72012-07-02 14:54:44 -07001474 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001475 break;
1476 case Instruction::IGET_BYTE:
1477 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetByte,
buzbee4f4dfc72012-07-02 14:54:44 -07001478 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001479 break;
1480 case Instruction::IGET_CHAR:
1481 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetChar,
buzbee4f4dfc72012-07-02 14:54:44 -07001482 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001483 break;
1484 case Instruction::IGET_SHORT:
1485 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetShort,
buzbee4f4dfc72012-07-02 14:54:44 -07001486 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001487 break;
1488 case Instruction::IGET_WIDE:
1489 if (rlDest.fp) {
1490 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetDouble,
buzbee4f4dfc72012-07-02 14:54:44 -07001491 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001492 } else {
1493 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetWide,
buzbee4f4dfc72012-07-02 14:54:44 -07001494 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001495 }
1496 break;
1497 case Instruction::IPUT:
buzbee85eee022012-07-16 22:12:38 -07001498 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001499 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutFloat,
1500 rlSrc[0], rlSrc[1], vC);
1501 } else {
1502 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPut,
1503 rlSrc[0], rlSrc[1], vC);
1504 }
1505 break;
1506 case Instruction::IPUT_OBJECT:
1507 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutObject,
1508 rlSrc[0], rlSrc[1], vC);
1509 break;
1510 case Instruction::IPUT_BOOLEAN:
1511 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutBoolean,
1512 rlSrc[0], rlSrc[1], vC);
1513 break;
1514 case Instruction::IPUT_BYTE:
1515 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutByte,
1516 rlSrc[0], rlSrc[1], vC);
1517 break;
1518 case Instruction::IPUT_CHAR:
1519 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutChar,
1520 rlSrc[0], rlSrc[1], vC);
1521 break;
1522 case Instruction::IPUT_SHORT:
1523 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutShort,
1524 rlSrc[0], rlSrc[1], vC);
1525 break;
1526 case Instruction::IPUT_WIDE:
buzbee85eee022012-07-16 22:12:38 -07001527 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001528 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutDouble,
1529 rlSrc[0], rlSrc[1], vC);
1530 } else {
1531 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutWide,
1532 rlSrc[0], rlSrc[1], vC);
1533 }
buzbee2cfc6392012-05-07 14:51:40 -07001534 break;
1535
1536 case Instruction::FILL_ARRAY_DATA:
buzbee101305f2012-06-28 18:00:56 -07001537 convertFillArrayData(cUnit, vB, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001538 break;
1539
buzbee76592632012-06-29 15:18:35 -07001540 case Instruction::LONG_TO_INT:
1541 convertLongToInt(cUnit, rlDest, rlSrc[0]);
1542 break;
1543
buzbee101305f2012-06-28 18:00:56 -07001544 case Instruction::INT_TO_LONG:
1545 convertIntToLong(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001546 break;
1547
buzbee101305f2012-06-28 18:00:56 -07001548 case Instruction::INT_TO_CHAR:
1549 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1550 greenland::IntrinsicHelper::IntToChar);
1551 break;
1552 case Instruction::INT_TO_BYTE:
1553 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1554 greenland::IntrinsicHelper::IntToByte);
1555 break;
1556 case Instruction::INT_TO_SHORT:
1557 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1558 greenland::IntrinsicHelper::IntToShort);
1559 break;
1560
buzbee76592632012-06-29 15:18:35 -07001561 case Instruction::INT_TO_FLOAT:
1562 case Instruction::LONG_TO_FLOAT:
1563 convertIntToFP(cUnit, cUnit->irb->getFloatTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001564 break;
1565
buzbee76592632012-06-29 15:18:35 -07001566 case Instruction::INT_TO_DOUBLE:
1567 case Instruction::LONG_TO_DOUBLE:
1568 convertIntToFP(cUnit, cUnit->irb->getDoubleTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001569 break;
1570
buzbee76592632012-06-29 15:18:35 -07001571 case Instruction::FLOAT_TO_DOUBLE:
1572 convertFloatToDouble(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001573 break;
1574
buzbee76592632012-06-29 15:18:35 -07001575 case Instruction::DOUBLE_TO_FLOAT:
1576 convertDoubleToFloat(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001577 break;
1578
1579 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001580 case Instruction::NEG_INT:
1581 convertNeg(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001582 break;
1583
1584 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001585 case Instruction::NEG_DOUBLE:
buzbee76592632012-06-29 15:18:35 -07001586 convertNegFP(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001587 break;
1588
buzbee76592632012-06-29 15:18:35 -07001589 case Instruction::NOT_LONG:
1590 case Instruction::NOT_INT:
1591 convertNot(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001592 break;
1593
buzbee2cfc6392012-05-07 14:51:40 -07001594 case Instruction::FLOAT_TO_INT:
buzbee2cfc6392012-05-07 14:51:40 -07001595 case Instruction::DOUBLE_TO_INT:
buzbee76592632012-06-29 15:18:35 -07001596 convertFPToInt(cUnit, cUnit->irb->getInt32Ty(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001597 break;
1598
buzbee76592632012-06-29 15:18:35 -07001599 case Instruction::FLOAT_TO_LONG:
1600 case Instruction::DOUBLE_TO_LONG:
1601 convertFPToInt(cUnit, cUnit->irb->getInt64Ty(), rlDest, rlSrc[0]);
1602 break;
1603
1604 case Instruction::CMPL_FLOAT:
1605 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplFloat,
1606 rlDest, rlSrc[0], rlSrc[1]);
1607 break;
1608 case Instruction::CMPG_FLOAT:
1609 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgFloat,
1610 rlDest, rlSrc[0], rlSrc[1]);
1611 break;
1612 case Instruction::CMPL_DOUBLE:
1613 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplDouble,
1614 rlDest, rlSrc[0], rlSrc[1]);
1615 break;
1616 case Instruction::CMPG_DOUBLE:
1617 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgDouble,
1618 rlDest, rlSrc[0], rlSrc[1]);
1619 break;
1620 case Instruction::CMP_LONG:
1621 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpLong,
1622 rlDest, rlSrc[0], rlSrc[1]);
1623 break;
1624
buzbee76592632012-06-29 15:18:35 -07001625 case Instruction::PACKED_SWITCH:
buzbeef58c12c2012-07-03 15:06:29 -07001626 convertPackedSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001627 break;
1628
1629 case Instruction::SPARSE_SWITCH:
buzbeea1da8a52012-07-09 14:00:21 -07001630 convertSparseSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001631 break;
buzbee2cfc6392012-05-07 14:51:40 -07001632
1633 default:
buzbee32412962012-06-26 16:27:56 -07001634 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001635 res = true;
1636 }
buzbeeb03f4872012-06-11 15:22:11 -07001637 if (objectDefinition) {
1638 setShadowFrameEntry(cUnit, (llvm::Value*)
1639 cUnit->llvmValues.elemList[rlDest.origSReg]);
1640 }
buzbee2cfc6392012-05-07 14:51:40 -07001641 return res;
1642}
1643
1644/* Extended MIR instructions like PHI */
1645void convertExtendedMIR(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
1646 llvm::BasicBlock* llvmBB)
1647{
1648
1649 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1650 case kMirOpPhi: {
buzbee2cfc6392012-05-07 14:51:40 -07001651 RegLocation rlDest = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee2a83e8f2012-07-13 16:42:30 -07001652 /*
1653 * The Art compiler's Phi nodes only handle 32-bit operands,
1654 * representing wide values using a matched set of Phi nodes
1655 * for the lower and upper halves. In the llvm world, we only
1656 * want a single Phi for wides. Here we will simply discard
1657 * the Phi node representing the high word.
1658 */
1659 if (rlDest.highWord) {
1660 return; // No Phi node - handled via low word
1661 }
1662 int* incoming = (int*)mir->dalvikInsn.vB;
buzbee2cfc6392012-05-07 14:51:40 -07001663 llvm::Type* phiType =
1664 llvmTypeFromLocRec(cUnit, rlDest);
1665 llvm::PHINode* phi = cUnit->irb->CreatePHI(phiType, mir->ssaRep->numUses);
1666 for (int i = 0; i < mir->ssaRep->numUses; i++) {
1667 RegLocation loc;
buzbee2a83e8f2012-07-13 16:42:30 -07001668 // Don't check width here.
1669 loc = oatGetRawSrc(cUnit, mir, i);
1670 DCHECK_EQ(rlDest.wide, loc.wide);
1671 DCHECK_EQ(rlDest.wide & rlDest.highWord, loc.wide & loc.highWord);
1672 DCHECK_EQ(rlDest.fp, loc.fp);
1673 DCHECK_EQ(rlDest.core, loc.core);
1674 DCHECK_EQ(rlDest.ref, loc.ref);
buzbeed1643e42012-09-05 14:06:51 -07001675 SafeMap<unsigned int, unsigned int>::iterator it;
1676 it = cUnit->blockIdMap.find(incoming[i]);
1677 DCHECK(it != cUnit->blockIdMap.end());
buzbee2cfc6392012-05-07 14:51:40 -07001678 phi->addIncoming(getLLVMValue(cUnit, loc.origSReg),
buzbeed1643e42012-09-05 14:06:51 -07001679 getLLVMBlock(cUnit, it->second));
buzbee2cfc6392012-05-07 14:51:40 -07001680 }
1681 defineValue(cUnit, phi, rlDest.origSReg);
1682 break;
1683 }
1684 case kMirOpCopy: {
1685 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1686 break;
1687 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001688 case kMirOpNop:
1689 if ((mir == bb->lastMIRInsn) && (bb->taken == NULL) &&
1690 (bb->fallThrough == NULL)) {
1691 cUnit->irb->CreateUnreachable();
1692 }
1693 break;
1694
buzbee2cfc6392012-05-07 14:51:40 -07001695#if defined(TARGET_ARM)
1696 case kMirOpFusedCmplFloat:
1697 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmpFloat";
1698 break;
1699 case kMirOpFusedCmpgFloat:
1700 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmgFloat";
1701 break;
1702 case kMirOpFusedCmplDouble:
1703 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmplDouble";
1704 break;
1705 case kMirOpFusedCmpgDouble:
1706 UNIMPLEMENTED(WARNING) << "unimp kMirOpFusedCmpgDouble";
1707 break;
1708 case kMirOpFusedCmpLong:
1709 UNIMPLEMENTED(WARNING) << "unimp kMirOpLongCmpBranch";
1710 break;
1711#endif
1712 default:
1713 break;
1714 }
1715}
1716
1717void setDexOffset(CompilationUnit* cUnit, int32_t offset)
1718{
1719 cUnit->currentDalvikOffset = offset;
buzbee76592632012-06-29 15:18:35 -07001720 llvm::SmallVector<llvm::Value*, 1> arrayRef;
buzbee2cfc6392012-05-07 14:51:40 -07001721 arrayRef.push_back(cUnit->irb->getInt32(offset));
1722 llvm::MDNode* node = llvm::MDNode::get(*cUnit->context, arrayRef);
1723 cUnit->irb->SetDexOffset(node);
1724}
1725
1726// Attach method info as metadata to special intrinsic
1727void setMethodInfo(CompilationUnit* cUnit)
1728{
1729 // We don't want dex offset on this
1730 cUnit->irb->SetDexOffset(NULL);
1731 greenland::IntrinsicHelper::IntrinsicId id;
1732 id = greenland::IntrinsicHelper::MethodInfo;
1733 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1734 llvm::Instruction* inst = cUnit->irb->CreateCall(intr);
1735 llvm::SmallVector<llvm::Value*, 2> regInfo;
1736 regInfo.push_back(cUnit->irb->getInt32(cUnit->numIns));
1737 regInfo.push_back(cUnit->irb->getInt32(cUnit->numRegs));
1738 regInfo.push_back(cUnit->irb->getInt32(cUnit->numOuts));
1739 regInfo.push_back(cUnit->irb->getInt32(cUnit->numCompilerTemps));
1740 regInfo.push_back(cUnit->irb->getInt32(cUnit->numSSARegs));
1741 llvm::MDNode* regInfoNode = llvm::MDNode::get(*cUnit->context, regInfo);
1742 inst->setMetadata("RegInfo", regInfoNode);
1743 int promoSize = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
1744 llvm::SmallVector<llvm::Value*, 50> pmap;
1745 for (int i = 0; i < promoSize; i++) {
1746 PromotionMap* p = &cUnit->promotionMap[i];
1747 int32_t mapData = ((p->firstInPair & 0xff) << 24) |
1748 ((p->fpReg & 0xff) << 16) |
1749 ((p->coreReg & 0xff) << 8) |
1750 ((p->fpLocation & 0xf) << 4) |
1751 (p->coreLocation & 0xf);
1752 pmap.push_back(cUnit->irb->getInt32(mapData));
1753 }
1754 llvm::MDNode* mapNode = llvm::MDNode::get(*cUnit->context, pmap);
1755 inst->setMetadata("PromotionMap", mapNode);
1756 setDexOffset(cUnit, cUnit->currentDalvikOffset);
1757}
1758
1759/* Handle the content in each basic block */
1760bool methodBlockBitcodeConversion(CompilationUnit* cUnit, BasicBlock* bb)
1761{
buzbeed1643e42012-09-05 14:06:51 -07001762 if (bb->blockType == kDead) return false;
buzbee2cfc6392012-05-07 14:51:40 -07001763 llvm::BasicBlock* llvmBB = getLLVMBlock(cUnit, bb->id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001764 if (llvmBB != NULL) {
1765 cUnit->irb->SetInsertPoint(llvmBB);
1766 setDexOffset(cUnit, bb->startOffset);
1767 }
buzbee2cfc6392012-05-07 14:51:40 -07001768
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001769 if (cUnit->printMe) {
1770 LOG(INFO) << "................................";
1771 LOG(INFO) << "Block id " << bb->id;
1772 if (llvmBB != NULL) {
1773 LOG(INFO) << "label " << llvmBB->getName().str().c_str();
1774 } else {
1775 LOG(INFO) << "llvmBB is NULL";
1776 }
1777 }
1778
buzbee2cfc6392012-05-07 14:51:40 -07001779 if (bb->blockType == kEntryBlock) {
1780 setMethodInfo(cUnit);
buzbeeb03f4872012-06-11 15:22:11 -07001781 bool *canBeRef = (bool*) oatNew(cUnit, sizeof(bool) *
1782 cUnit->numDalvikRegisters, true,
1783 kAllocMisc);
1784 for (int i = 0; i < cUnit->numSSARegs; i++) {
1785 canBeRef[SRegToVReg(cUnit, i)] |= cUnit->regLocation[i].ref;
1786 }
1787 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
1788 if (canBeRef[i]) {
1789 cUnit->numShadowFrameEntries++;
1790 }
1791 }
1792 if (cUnit->numShadowFrameEntries > 0) {
1793 cUnit->shadowMap = (int*) oatNew(cUnit, sizeof(int) *
1794 cUnit->numShadowFrameEntries, true,
1795 kAllocMisc);
1796 for (int i = 0, j = 0; i < cUnit->numDalvikRegisters; i++) {
1797 if (canBeRef[i]) {
1798 cUnit->shadowMap[j++] = i;
1799 }
1800 }
1801 greenland::IntrinsicHelper::IntrinsicId id =
1802 greenland::IntrinsicHelper::AllocaShadowFrame;
1803 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1804 llvm::Value* entries = cUnit->irb->getInt32(cUnit->numShadowFrameEntries);
1805 cUnit->irb->CreateCall(func, entries);
1806 }
buzbee2cfc6392012-05-07 14:51:40 -07001807 } else if (bb->blockType == kExitBlock) {
1808 /*
1809 * Because of the differences between how MIR/LIR and llvm handle exit
1810 * blocks, we won't explicitly covert them. On the llvm-to-lir
1811 * path, it will need to be regenereated.
1812 */
1813 return false;
buzbee6969d502012-06-15 16:40:31 -07001814 } else if (bb->blockType == kExceptionHandling) {
1815 /*
1816 * Because we're deferring null checking, delete the associated empty
1817 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001818 */
1819 llvmBB->eraseFromParent();
1820 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001821 }
1822
1823 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
1824
1825 setDexOffset(cUnit, mir->offset);
1826
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001827 int opcode = mir->dalvikInsn.opcode;
1828 Instruction::Format dalvikFormat =
1829 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001830
1831 /* If we're compiling for the debugger, generate an update callout */
1832 if (cUnit->genDebugger) {
1833 UNIMPLEMENTED(FATAL) << "Need debug codegen";
1834 //genDebuggerUpdate(cUnit, mir->offset);
1835 }
1836
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001837 if (opcode == kMirOpCheck) {
1838 // Combine check and work halves of throwing instruction.
1839 MIR* workHalf = mir->meta.throwInsn;
1840 mir->dalvikInsn.opcode = workHalf->dalvikInsn.opcode;
1841 opcode = mir->dalvikInsn.opcode;
1842 SSARepresentation* ssaRep = workHalf->ssaRep;
1843 workHalf->ssaRep = mir->ssaRep;
1844 mir->ssaRep = ssaRep;
1845 workHalf->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1846 if (bb->successorBlockList.blockListType == kCatch) {
1847 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
1848 greenland::IntrinsicHelper::CatchTargets);
1849 llvm::Value* switchKey =
1850 cUnit->irb->CreateCall(intr, cUnit->irb->getInt32(mir->offset));
1851 GrowableListIterator iter;
1852 oatGrowableListIteratorInit(&bb->successorBlockList.blocks, &iter);
1853 // New basic block to use for work half
1854 llvm::BasicBlock* workBB =
1855 llvm::BasicBlock::Create(*cUnit->context, "", cUnit->func);
1856 llvm::SwitchInst* sw =
1857 cUnit->irb->CreateSwitch(switchKey, workBB,
1858 bb->successorBlockList.blocks.numUsed);
1859 while (true) {
1860 SuccessorBlockInfo *successorBlockInfo =
1861 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iter);
1862 if (successorBlockInfo == NULL) break;
1863 llvm::BasicBlock *target =
1864 getLLVMBlock(cUnit, successorBlockInfo->block->id);
1865 int typeIndex = successorBlockInfo->key;
1866 sw->addCase(cUnit->irb->getInt32(typeIndex), target);
1867 }
1868 llvmBB = workBB;
1869 cUnit->irb->SetInsertPoint(llvmBB);
1870 }
1871 }
1872
1873 if (opcode >= kMirOpFirst) {
buzbee2cfc6392012-05-07 14:51:40 -07001874 convertExtendedMIR(cUnit, bb, mir, llvmBB);
1875 continue;
1876 }
1877
1878 bool notHandled = convertMIRNode(cUnit, mir, bb, llvmBB,
1879 NULL /* labelList */);
1880 if (notHandled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001881 Instruction::Code dalvikOpcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001882 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001883 mir->offset, opcode,
buzbee2cfc6392012-05-07 14:51:40 -07001884 Instruction::Name(dalvikOpcode),
1885 dalvikFormat);
1886 }
1887 }
1888
buzbee4be777b2012-07-12 14:38:18 -07001889 if (bb->blockType == kEntryBlock) {
1890 cUnit->entryTargetBB = getLLVMBlock(cUnit, bb->fallThrough->id);
1891 } else if ((bb->fallThrough != NULL) && !bb->hasReturn) {
buzbee2cfc6392012-05-07 14:51:40 -07001892 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->fallThrough->id));
1893 }
1894
1895 return false;
1896}
1897
buzbee4f4dfc72012-07-02 14:54:44 -07001898char remapShorty(char shortyType) {
1899 /*
1900 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1901 * and longs/doubles are represented as a pair of registers. When sub-word
1902 * arguments (and method results) are passed, they are extended to Dalvik
1903 * virtual register containers. Because llvm is picky about type consistency,
1904 * we must either cast the "real" type to 32-bit container multiple Dalvik
1905 * register types, or always use the expanded values.
1906 * Here, we're doing the latter. We map the shorty signature to container
1907 * types (which is valid so long as we always do a real expansion of passed
1908 * arguments and field loads).
1909 */
1910 switch(shortyType) {
1911 case 'Z' : shortyType = 'I'; break;
1912 case 'B' : shortyType = 'I'; break;
1913 case 'S' : shortyType = 'I'; break;
1914 case 'C' : shortyType = 'I'; break;
1915 default: break;
1916 }
1917 return shortyType;
1918}
1919
buzbee2cfc6392012-05-07 14:51:40 -07001920llvm::FunctionType* getFunctionType(CompilationUnit* cUnit) {
1921
1922 // Get return type
buzbee4f4dfc72012-07-02 14:54:44 -07001923 llvm::Type* ret_type = cUnit->irb->GetJType(remapShorty(cUnit->shorty[0]),
buzbee2cfc6392012-05-07 14:51:40 -07001924 greenland::kAccurate);
1925
1926 // Get argument type
1927 std::vector<llvm::Type*> args_type;
1928
1929 // method object
1930 args_type.push_back(cUnit->irb->GetJMethodTy());
1931
1932 // Do we have a "this"?
1933 if ((cUnit->access_flags & kAccStatic) == 0) {
1934 args_type.push_back(cUnit->irb->GetJObjectTy());
1935 }
1936
1937 for (uint32_t i = 1; i < strlen(cUnit->shorty); ++i) {
buzbee4f4dfc72012-07-02 14:54:44 -07001938 args_type.push_back(cUnit->irb->GetJType(remapShorty(cUnit->shorty[i]),
buzbee2cfc6392012-05-07 14:51:40 -07001939 greenland::kAccurate));
1940 }
1941
1942 return llvm::FunctionType::get(ret_type, args_type, false);
1943}
1944
1945bool createFunction(CompilationUnit* cUnit) {
1946 std::string func_name(PrettyMethod(cUnit->method_idx, *cUnit->dex_file,
1947 /* with_signature */ false));
1948 llvm::FunctionType* func_type = getFunctionType(cUnit);
1949
1950 if (func_type == NULL) {
1951 return false;
1952 }
1953
1954 cUnit->func = llvm::Function::Create(func_type,
1955 llvm::Function::ExternalLinkage,
1956 func_name, cUnit->module);
1957
1958 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
1959 llvm::Function::arg_iterator arg_end(cUnit->func->arg_end());
1960
1961 arg_iter->setName("method");
1962 ++arg_iter;
1963
1964 int startSReg = cUnit->numRegs;
1965
1966 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
1967 arg_iter->setName(StringPrintf("v%i_0", startSReg));
1968 startSReg += cUnit->regLocation[startSReg].wide ? 2 : 1;
1969 }
1970
1971 return true;
1972}
1973
1974bool createLLVMBasicBlock(CompilationUnit* cUnit, BasicBlock* bb)
1975{
1976 // Skip the exit block
buzbeed1643e42012-09-05 14:06:51 -07001977 if ((bb->blockType == kDead) ||(bb->blockType == kExitBlock)) {
buzbee2cfc6392012-05-07 14:51:40 -07001978 cUnit->idToBlockMap.Put(bb->id, NULL);
1979 } else {
1980 int offset = bb->startOffset;
1981 bool entryBlock = (bb->blockType == kEntryBlock);
1982 llvm::BasicBlock* llvmBB =
1983 llvm::BasicBlock::Create(*cUnit->context, entryBlock ? "entry" :
buzbee8320f382012-09-11 16:29:42 -07001984 StringPrintf(kLabelFormat, bb->catchEntry ? kCatchBlock :
1985 kNormalBlock, offset, bb->id), cUnit->func);
buzbee2cfc6392012-05-07 14:51:40 -07001986 if (entryBlock) {
1987 cUnit->entryBB = llvmBB;
1988 cUnit->placeholderBB =
1989 llvm::BasicBlock::Create(*cUnit->context, "placeholder",
1990 cUnit->func);
1991 }
1992 cUnit->idToBlockMap.Put(bb->id, llvmBB);
1993 }
1994 return false;
1995}
1996
1997
1998/*
1999 * Convert MIR to LLVM_IR
2000 * o For each ssa name, create LLVM named value. Type these
2001 * appropriately, and ignore high half of wide and double operands.
2002 * o For each MIR basic block, create an LLVM basic block.
2003 * o Iterate through the MIR a basic block at a time, setting arguments
2004 * to recovered ssa name.
2005 */
2006void oatMethodMIR2Bitcode(CompilationUnit* cUnit)
2007{
2008 initIR(cUnit);
2009 oatInitGrowableList(cUnit, &cUnit->llvmValues, cUnit->numSSARegs);
2010
2011 // Create the function
2012 createFunction(cUnit);
2013
2014 // Create an LLVM basic block for each MIR block in dfs preorder
2015 oatDataFlowAnalysisDispatcher(cUnit, createLLVMBasicBlock,
2016 kPreOrderDFSTraversal, false /* isIterative */);
2017 /*
2018 * Create an llvm named value for each MIR SSA name. Note: we'll use
2019 * placeholders for all non-argument values (because we haven't seen
2020 * the definition yet).
2021 */
2022 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2023 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
2024 arg_iter++; /* Skip path method */
2025 for (int i = 0; i < cUnit->numSSARegs; i++) {
2026 llvm::Value* val;
buzbee85eee022012-07-16 22:12:38 -07002027 RegLocation rlTemp = cUnit->regLocation[i];
2028 if ((SRegToVReg(cUnit, i) < 0) || rlTemp.highWord) {
buzbee2a83e8f2012-07-13 16:42:30 -07002029 oatInsertGrowableList(cUnit, &cUnit->llvmValues, 0);
2030 } else if ((i < cUnit->numRegs) ||
2031 (i >= (cUnit->numRegs + cUnit->numIns))) {
buzbee85eee022012-07-16 22:12:38 -07002032 llvm::Constant* immValue = cUnit->regLocation[i].wide ?
2033 cUnit->irb->GetJLong(0) : cUnit->irb->GetJInt(0);
buzbee2a83e8f2012-07-13 16:42:30 -07002034 val = emitConst(cUnit, immValue, cUnit->regLocation[i]);
2035 val->setName(llvmSSAName(cUnit, i));
buzbee2cfc6392012-05-07 14:51:40 -07002036 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)val);
buzbee2cfc6392012-05-07 14:51:40 -07002037 } else {
2038 // Recover previously-created argument values
2039 llvm::Value* argVal = arg_iter++;
2040 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)argVal);
2041 }
2042 }
buzbee2cfc6392012-05-07 14:51:40 -07002043
2044 oatDataFlowAnalysisDispatcher(cUnit, methodBlockBitcodeConversion,
2045 kPreOrderDFSTraversal, false /* Iterative */);
2046
buzbee4be777b2012-07-12 14:38:18 -07002047 /*
2048 * In a few rare cases of verification failure, the verifier will
2049 * replace one or more Dalvik opcodes with the special
2050 * throw-verification-failure opcode. This can leave the SSA graph
2051 * in an invalid state, as definitions may be lost, while uses retained.
2052 * To work around this problem, we insert placeholder definitions for
2053 * all Dalvik SSA regs in the "placeholder" block. Here, after
2054 * bitcode conversion is complete, we examine those placeholder definitions
2055 * and delete any with no references (which normally is all of them).
2056 *
2057 * If any definitions remain, we link the placeholder block into the
2058 * CFG. Otherwise, it is deleted.
2059 */
2060 for (llvm::BasicBlock::iterator it = cUnit->placeholderBB->begin(),
2061 itEnd = cUnit->placeholderBB->end(); it != itEnd;) {
2062 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2063 DCHECK(inst != NULL);
2064 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2065 DCHECK(val != NULL);
2066 if (val->getNumUses() == 0) {
2067 inst->eraseFromParent();
2068 }
2069 }
2070 setDexOffset(cUnit, 0);
2071 if (cUnit->placeholderBB->empty()) {
2072 cUnit->placeholderBB->eraseFromParent();
2073 } else {
2074 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2075 cUnit->irb->CreateBr(cUnit->entryTargetBB);
2076 cUnit->entryTargetBB = cUnit->placeholderBB;
2077 }
2078 cUnit->irb->SetInsertPoint(cUnit->entryBB);
2079 cUnit->irb->CreateBr(cUnit->entryTargetBB);
buzbee2cfc6392012-05-07 14:51:40 -07002080
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002081 if (cUnit->enableDebug & (1 << kDebugVerifyBitcode)) {
2082 if (llvm::verifyFunction(*cUnit->func, llvm::PrintMessageAction)) {
2083 LOG(INFO) << "Bitcode verification FAILED for "
2084 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
2085 << " of size " << cUnit->insnsSize;
2086 cUnit->enableDebug |= (1 << kDebugDumpBitcodeFile);
2087 }
2088 }
buzbee2cfc6392012-05-07 14:51:40 -07002089
buzbeead8f15e2012-06-18 14:49:45 -07002090 if (cUnit->enableDebug & (1 << kDebugDumpBitcodeFile)) {
2091 // Write bitcode to file
2092 std::string errmsg;
2093 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
2094 oatReplaceSpecialChars(fname);
2095 // TODO: make configurable
buzbee4f1181f2012-06-22 13:52:12 -07002096 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002097
buzbeead8f15e2012-06-18 14:49:45 -07002098 llvm::OwningPtr<llvm::tool_output_file> out_file(
2099 new llvm::tool_output_file(fname.c_str(), errmsg,
2100 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002101
buzbeead8f15e2012-06-18 14:49:45 -07002102 if (!errmsg.empty()) {
2103 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2104 }
2105
2106 llvm::WriteBitcodeToFile(cUnit->module, out_file->os());
2107 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002108 }
buzbee2cfc6392012-05-07 14:51:40 -07002109}
2110
2111RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val) {
2112 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002113 DCHECK(val != NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002114 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2115 if (it == cUnit->locMap.end()) {
buzbee4f1181f2012-06-22 13:52:12 -07002116 std::string valName = val->getName().str();
buzbee32412962012-06-26 16:27:56 -07002117 if (valName.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002118 // FIXME: need to be more robust, handle FP and be in a position to
2119 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002120 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2121 memset(&res, 0, sizeof(res));
2122 res.location = kLocPhysReg;
2123 res.lowReg = oatAllocTemp(cUnit);
2124 res.home = true;
2125 res.sRegLow = INVALID_SREG;
2126 res.origSReg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002127 llvm::Type* ty = val->getType();
2128 res.wide = ((ty == cUnit->irb->getInt64Ty()) ||
2129 (ty == cUnit->irb->getDoubleTy()));
2130 if (res.wide) {
2131 res.highReg = oatAllocTemp(cUnit);
2132 }
buzbee4f1181f2012-06-22 13:52:12 -07002133 cUnit->locMap.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002134 } else {
2135 DCHECK_EQ(valName[0], 'v');
2136 int baseSReg = INVALID_SREG;
2137 sscanf(valName.c_str(), "v%d_", &baseSReg);
2138 res = cUnit->regLocation[baseSReg];
2139 cUnit->locMap.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002140 }
2141 } else {
2142 res = it->second;
2143 }
2144 return res;
2145}
2146
2147Instruction::Code getDalvikOpcode(OpKind op, bool isConst, bool isWide)
2148{
2149 Instruction::Code res = Instruction::NOP;
2150 if (isWide) {
2151 switch(op) {
2152 case kOpAdd: res = Instruction::ADD_LONG; break;
2153 case kOpSub: res = Instruction::SUB_LONG; break;
2154 case kOpMul: res = Instruction::MUL_LONG; break;
2155 case kOpDiv: res = Instruction::DIV_LONG; break;
2156 case kOpRem: res = Instruction::REM_LONG; break;
2157 case kOpAnd: res = Instruction::AND_LONG; break;
2158 case kOpOr: res = Instruction::OR_LONG; break;
2159 case kOpXor: res = Instruction::XOR_LONG; break;
2160 case kOpLsl: res = Instruction::SHL_LONG; break;
2161 case kOpLsr: res = Instruction::USHR_LONG; break;
2162 case kOpAsr: res = Instruction::SHR_LONG; break;
2163 default: LOG(FATAL) << "Unexpected OpKind " << op;
2164 }
2165 } else if (isConst){
2166 switch(op) {
2167 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2168 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2169 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2170 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2171 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2172 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2173 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2174 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2175 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2176 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2177 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2178 default: LOG(FATAL) << "Unexpected OpKind " << op;
2179 }
2180 } else {
2181 switch(op) {
2182 case kOpAdd: res = Instruction::ADD_INT; break;
2183 case kOpSub: res = Instruction::SUB_INT; break;
2184 case kOpMul: res = Instruction::MUL_INT; break;
2185 case kOpDiv: res = Instruction::DIV_INT; break;
2186 case kOpRem: res = Instruction::REM_INT; break;
2187 case kOpAnd: res = Instruction::AND_INT; break;
2188 case kOpOr: res = Instruction::OR_INT; break;
2189 case kOpXor: res = Instruction::XOR_INT; break;
2190 case kOpLsl: res = Instruction::SHL_INT; break;
2191 case kOpLsr: res = Instruction::USHR_INT; break;
2192 case kOpAsr: res = Instruction::SHR_INT; break;
2193 default: LOG(FATAL) << "Unexpected OpKind " << op;
2194 }
2195 }
2196 return res;
2197}
2198
buzbee4f1181f2012-06-22 13:52:12 -07002199Instruction::Code getDalvikFPOpcode(OpKind op, bool isConst, bool isWide)
2200{
2201 Instruction::Code res = Instruction::NOP;
2202 if (isWide) {
2203 switch(op) {
2204 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2205 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2206 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2207 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2208 case kOpRem: res = Instruction::REM_DOUBLE; break;
2209 default: LOG(FATAL) << "Unexpected OpKind " << op;
2210 }
2211 } else {
2212 switch(op) {
2213 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2214 case kOpSub: res = Instruction::SUB_FLOAT; break;
2215 case kOpMul: res = Instruction::MUL_FLOAT; break;
2216 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2217 case kOpRem: res = Instruction::REM_FLOAT; break;
2218 default: LOG(FATAL) << "Unexpected OpKind " << op;
2219 }
2220 }
2221 return res;
2222}
2223
2224void cvtBinFPOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2225{
2226 RegLocation rlDest = getLoc(cUnit, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002227 /*
2228 * Normally, we won't ever generate an FP operation with an immediate
2229 * operand (not supported in Dex instruction set). However, the IR builder
2230 * may insert them - in particular for createNegFP. Recognize this case
2231 * and deal with it.
2232 */
2233 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2234 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2235 DCHECK(op2C == NULL);
2236 if ((op1C != NULL) && (op == kOpSub)) {
2237 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(1));
2238 if (rlDest.wide) {
2239 genArithOpDouble(cUnit, Instruction::NEG_DOUBLE, rlDest, rlSrc, rlSrc);
2240 } else {
2241 genArithOpFloat(cUnit, Instruction::NEG_FLOAT, rlDest, rlSrc, rlSrc);
2242 }
buzbee4f1181f2012-06-22 13:52:12 -07002243 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002244 DCHECK(op1C == NULL);
2245 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2246 RegLocation rlSrc2 = getLoc(cUnit, inst->getOperand(1));
2247 Instruction::Code dalvikOp = getDalvikFPOpcode(op, false, rlDest.wide);
2248 if (rlDest.wide) {
2249 genArithOpDouble(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2250 } else {
2251 genArithOpFloat(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2252 }
buzbee4f1181f2012-06-22 13:52:12 -07002253 }
2254}
2255
buzbee101305f2012-06-28 18:00:56 -07002256void cvtIntNarrowing(CompilationUnit* cUnit, llvm::Instruction* inst,
2257 Instruction::Code opcode)
2258{
2259 RegLocation rlDest = getLoc(cUnit, inst);
2260 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2261 genIntNarrowing(cUnit, opcode, rlDest, rlSrc);
2262}
2263
buzbee76592632012-06-29 15:18:35 -07002264void cvtIntToFP(CompilationUnit* cUnit, llvm::Instruction* inst)
2265{
2266 RegLocation rlDest = getLoc(cUnit, inst);
2267 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2268 Instruction::Code opcode;
2269 if (rlDest.wide) {
2270 if (rlSrc.wide) {
2271 opcode = Instruction::LONG_TO_DOUBLE;
2272 } else {
2273 opcode = Instruction::INT_TO_DOUBLE;
2274 }
2275 } else {
2276 if (rlSrc.wide) {
2277 opcode = Instruction::LONG_TO_FLOAT;
2278 } else {
2279 opcode = Instruction::INT_TO_FLOAT;
2280 }
2281 }
2282 genConversion(cUnit, opcode, rlDest, rlSrc);
2283}
2284
2285void cvtFPToInt(CompilationUnit* cUnit, llvm::Instruction* inst)
2286{
2287 RegLocation rlDest = getLoc(cUnit, inst);
2288 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2289 Instruction::Code opcode;
2290 if (rlDest.wide) {
2291 if (rlSrc.wide) {
2292 opcode = Instruction::DOUBLE_TO_LONG;
2293 } else {
2294 opcode = Instruction::FLOAT_TO_LONG;
2295 }
2296 } else {
2297 if (rlSrc.wide) {
2298 opcode = Instruction::DOUBLE_TO_INT;
2299 } else {
2300 opcode = Instruction::FLOAT_TO_INT;
2301 }
2302 }
2303 genConversion(cUnit, opcode, rlDest, rlSrc);
2304}
2305
2306void cvtFloatToDouble(CompilationUnit* cUnit, llvm::Instruction* inst)
2307{
2308 RegLocation rlDest = getLoc(cUnit, inst);
2309 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2310 genConversion(cUnit, Instruction::FLOAT_TO_DOUBLE, rlDest, rlSrc);
2311}
2312
2313void cvtTrunc(CompilationUnit* cUnit, llvm::Instruction* inst)
2314{
2315 RegLocation rlDest = getLoc(cUnit, inst);
2316 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2317 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
2318 rlSrc = oatWideToNarrow(cUnit, rlSrc);
2319 storeValue(cUnit, rlDest, rlSrc);
2320}
2321
2322void cvtDoubleToFloat(CompilationUnit* cUnit, llvm::Instruction* inst)
2323{
2324 RegLocation rlDest = getLoc(cUnit, inst);
2325 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2326 genConversion(cUnit, Instruction::DOUBLE_TO_FLOAT, rlDest, rlSrc);
2327}
2328
2329
buzbee101305f2012-06-28 18:00:56 -07002330void cvtIntExt(CompilationUnit* cUnit, llvm::Instruction* inst, bool isSigned)
2331{
2332 // TODO: evaluate src/tgt types and add general support for more than int to long
2333 RegLocation rlDest = getLoc(cUnit, inst);
2334 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2335 DCHECK(rlDest.wide);
2336 DCHECK(!rlSrc.wide);
2337 DCHECK(!rlDest.fp);
2338 DCHECK(!rlSrc.fp);
2339 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2340 if (rlSrc.location == kLocPhysReg) {
2341 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2342 } else {
2343 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2344 }
2345 if (isSigned) {
2346 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2347 } else {
2348 loadConstant(cUnit, rlResult.highReg, 0);
2349 }
2350 storeValueWide(cUnit, rlDest, rlResult);
2351}
2352
buzbee2cfc6392012-05-07 14:51:40 -07002353void cvtBinOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2354{
2355 RegLocation rlDest = getLoc(cUnit, inst);
2356 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002357 // Special-case RSUB/NEG
buzbee4f1181f2012-06-22 13:52:12 -07002358 llvm::ConstantInt* lhsImm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2359 if ((op == kOpSub) && (lhsImm != NULL)) {
2360 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(1));
buzbeef58c12c2012-07-03 15:06:29 -07002361 if (rlSrc1.wide) {
2362 DCHECK_EQ(lhsImm->getSExtValue(), 0);
2363 genArithOpLong(cUnit, Instruction::NEG_LONG, rlDest, rlSrc1, rlSrc1);
2364 } else {
2365 genArithOpIntLit(cUnit, Instruction::RSUB_INT, rlDest, rlSrc1,
2366 lhsImm->getSExtValue());
2367 }
buzbee4f1181f2012-06-22 13:52:12 -07002368 return;
2369 }
2370 DCHECK(lhsImm == NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002371 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2372 llvm::Value* rhs = inst->getOperand(1);
buzbee9a2487f2012-07-26 14:01:13 -07002373 llvm::ConstantInt* constRhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2374 if (!rlDest.wide && (constRhs != NULL)) {
buzbee2cfc6392012-05-07 14:51:40 -07002375 Instruction::Code dalvikOp = getDalvikOpcode(op, true, false);
buzbee9a2487f2012-07-26 14:01:13 -07002376 genArithOpIntLit(cUnit, dalvikOp, rlDest, rlSrc1, constRhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002377 } else {
2378 Instruction::Code dalvikOp = getDalvikOpcode(op, false, rlDest.wide);
buzbee9a2487f2012-07-26 14:01:13 -07002379 RegLocation rlSrc2;
2380 if (constRhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002381 // ir_builder converts NOT_LONG to xor src, -1. Restore
2382 DCHECK_EQ(dalvikOp, Instruction::XOR_LONG);
2383 DCHECK_EQ(-1L, constRhs->getSExtValue());
2384 dalvikOp = Instruction::NOT_LONG;
buzbee9a2487f2012-07-26 14:01:13 -07002385 rlSrc2 = rlSrc1;
2386 } else {
2387 rlSrc2 = getLoc(cUnit, rhs);
2388 }
buzbee2cfc6392012-05-07 14:51:40 -07002389 if (rlDest.wide) {
2390 genArithOpLong(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2391 } else {
2392 genArithOpInt(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2393 }
2394 }
2395}
2396
buzbee2a83e8f2012-07-13 16:42:30 -07002397void cvtShiftOp(CompilationUnit* cUnit, Instruction::Code opcode,
2398 llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002399{
buzbee2a83e8f2012-07-13 16:42:30 -07002400 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2401 RegLocation rlDest = getLoc(cUnit, callInst);
2402 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2403 llvm::Value* rhs = callInst->getArgOperand(1);
2404 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2405 DCHECK(!rlDest.wide);
2406 genArithOpIntLit(cUnit, opcode, rlDest, rlSrc, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002407 } else {
buzbee2a83e8f2012-07-13 16:42:30 -07002408 RegLocation rlShift = getLoc(cUnit, rhs);
2409 if (callInst->getType() == cUnit->irb->getInt64Ty()) {
2410 genShiftOpLong(cUnit, opcode, rlDest, rlSrc, rlShift);
2411 } else {
2412 genArithOpInt(cUnit, opcode, rlDest, rlSrc, rlShift);
2413 }
buzbee101305f2012-06-28 18:00:56 -07002414 }
2415}
2416
buzbee2cfc6392012-05-07 14:51:40 -07002417void cvtBr(CompilationUnit* cUnit, llvm::Instruction* inst)
2418{
2419 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(inst);
2420 DCHECK(brInst != NULL);
2421 DCHECK(brInst->isUnconditional()); // May change - but this is all we use now
2422 llvm::BasicBlock* targetBB = brInst->getSuccessor(0);
2423 opUnconditionalBranch(cUnit, cUnit->blockToLabelMap.Get(targetBB));
2424}
2425
2426void cvtPhi(CompilationUnit* cUnit, llvm::Instruction* inst)
2427{
2428 // Nop - these have already been processed
2429}
2430
2431void cvtRet(CompilationUnit* cUnit, llvm::Instruction* inst)
2432{
2433 llvm::ReturnInst* retInst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2434 llvm::Value* retVal = retInst->getReturnValue();
2435 if (retVal != NULL) {
2436 RegLocation rlSrc = getLoc(cUnit, retVal);
2437 if (rlSrc.wide) {
2438 storeValueWide(cUnit, oatGetReturnWide(cUnit, rlSrc.fp), rlSrc);
2439 } else {
2440 storeValue(cUnit, oatGetReturn(cUnit, rlSrc.fp), rlSrc);
2441 }
2442 }
2443 genExitSequence(cUnit);
2444}
2445
2446ConditionCode getCond(llvm::ICmpInst::Predicate llvmCond)
2447{
2448 ConditionCode res = kCondAl;
2449 switch(llvmCond) {
buzbee6969d502012-06-15 16:40:31 -07002450 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002451 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2452 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2453 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002454 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002455 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002456 default: LOG(FATAL) << "Unexpected llvm condition";
2457 }
2458 return res;
2459}
2460
2461void cvtICmp(CompilationUnit* cUnit, llvm::Instruction* inst)
2462{
2463 // genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2)
2464 UNIMPLEMENTED(FATAL);
2465}
2466
2467void cvtICmpBr(CompilationUnit* cUnit, llvm::Instruction* inst,
2468 llvm::BranchInst* brInst)
2469{
2470 // Get targets
2471 llvm::BasicBlock* takenBB = brInst->getSuccessor(0);
2472 LIR* taken = cUnit->blockToLabelMap.Get(takenBB);
2473 llvm::BasicBlock* fallThroughBB = brInst->getSuccessor(1);
2474 LIR* fallThrough = cUnit->blockToLabelMap.Get(fallThroughBB);
2475 // Get comparison operands
2476 llvm::ICmpInst* iCmpInst = llvm::dyn_cast<llvm::ICmpInst>(inst);
2477 ConditionCode cond = getCond(iCmpInst->getPredicate());
2478 llvm::Value* lhs = iCmpInst->getOperand(0);
2479 // Not expecting a constant as 1st operand
2480 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
2481 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2482 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2483 llvm::Value* rhs = inst->getOperand(1);
2484#if defined(TARGET_MIPS)
2485 // Compare and branch in one shot
2486 (void)taken;
2487 (void)cond;
2488 (void)rhs;
2489 UNIMPLEMENTED(FATAL);
2490#else
2491 //Compare, then branch
2492 // TODO: handle fused CMP_LONG/IF_xxZ case
2493 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2494 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002495 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
2496 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002497 } else {
2498 RegLocation rlSrc2 = getLoc(cUnit, rhs);
2499 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2500 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
2501 }
2502 opCondBranch(cUnit, cond, taken);
2503#endif
2504 // Fallthrough
2505 opUnconditionalBranch(cUnit, fallThrough);
2506}
2507
2508void cvtCall(CompilationUnit* cUnit, llvm::CallInst* callInst,
2509 llvm::Function* callee)
2510{
2511 UNIMPLEMENTED(FATAL);
2512}
2513
buzbee2cfc6392012-05-07 14:51:40 -07002514void cvtCopy(CompilationUnit* cUnit, llvm::CallInst* callInst)
2515{
buzbee4f1181f2012-06-22 13:52:12 -07002516 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002517 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2518 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee76592632012-06-29 15:18:35 -07002519 DCHECK_EQ(rlSrc.wide, rlDest.wide);
2520 DCHECK_EQ(rlSrc.fp, rlDest.fp);
buzbee2cfc6392012-05-07 14:51:40 -07002521 if (rlSrc.wide) {
2522 storeValueWide(cUnit, rlDest, rlSrc);
2523 } else {
2524 storeValue(cUnit, rlDest, rlSrc);
2525 }
2526}
2527
2528// Note: Immediate arg is a ConstantInt regardless of result type
2529void cvtConst(CompilationUnit* cUnit, llvm::CallInst* callInst)
2530{
buzbee4f1181f2012-06-22 13:52:12 -07002531 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002532 llvm::ConstantInt* src =
2533 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2534 uint64_t immval = src->getZExtValue();
2535 RegLocation rlDest = getLoc(cUnit, callInst);
2536 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
2537 if (rlDest.wide) {
2538 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
2539 (immval) & 0xffffffff, (immval >> 32) & 0xffffffff);
2540 storeValueWide(cUnit, rlDest, rlResult);
2541 } else {
2542 loadConstantNoClobber(cUnit, rlResult.lowReg, immval & 0xffffffff);
2543 storeValue(cUnit, rlDest, rlResult);
2544 }
2545}
2546
buzbee101305f2012-06-28 18:00:56 -07002547void cvtConstObject(CompilationUnit* cUnit, llvm::CallInst* callInst,
2548 bool isString)
buzbee6969d502012-06-15 16:40:31 -07002549{
buzbee4f1181f2012-06-22 13:52:12 -07002550 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee101305f2012-06-28 18:00:56 -07002551 llvm::ConstantInt* idxVal =
buzbee6969d502012-06-15 16:40:31 -07002552 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee101305f2012-06-28 18:00:56 -07002553 uint32_t index = idxVal->getZExtValue();
buzbee6969d502012-06-15 16:40:31 -07002554 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee101305f2012-06-28 18:00:56 -07002555 if (isString) {
2556 genConstString(cUnit, index, rlDest);
2557 } else {
2558 genConstClass(cUnit, index, rlDest);
2559 }
2560}
2561
2562void cvtFillArrayData(CompilationUnit* cUnit, llvm::CallInst* callInst)
2563{
2564 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2565 llvm::ConstantInt* offsetVal =
2566 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2567 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2568 genFillArrayData(cUnit, offsetVal->getSExtValue(), rlSrc);
buzbee6969d502012-06-15 16:40:31 -07002569}
2570
buzbee4f1181f2012-06-22 13:52:12 -07002571void cvtNewInstance(CompilationUnit* cUnit, llvm::CallInst* callInst)
2572{
buzbee32412962012-06-26 16:27:56 -07002573 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002574 llvm::ConstantInt* typeIdxVal =
2575 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2576 uint32_t typeIdx = typeIdxVal->getZExtValue();
2577 RegLocation rlDest = getLoc(cUnit, callInst);
2578 genNewInstance(cUnit, typeIdx, rlDest);
2579}
2580
buzbee8fa0fda2012-06-27 15:44:52 -07002581void cvtNewArray(CompilationUnit* cUnit, llvm::CallInst* callInst)
2582{
2583 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2584 llvm::ConstantInt* typeIdxVal =
2585 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2586 uint32_t typeIdx = typeIdxVal->getZExtValue();
2587 llvm::Value* len = callInst->getArgOperand(1);
2588 RegLocation rlLen = getLoc(cUnit, len);
2589 RegLocation rlDest = getLoc(cUnit, callInst);
2590 genNewArray(cUnit, typeIdx, rlDest, rlLen);
2591}
2592
2593void cvtInstanceOf(CompilationUnit* cUnit, llvm::CallInst* callInst)
2594{
2595 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2596 llvm::ConstantInt* typeIdxVal =
2597 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2598 uint32_t typeIdx = typeIdxVal->getZExtValue();
2599 llvm::Value* src = callInst->getArgOperand(1);
2600 RegLocation rlSrc = getLoc(cUnit, src);
2601 RegLocation rlDest = getLoc(cUnit, callInst);
2602 genInstanceof(cUnit, typeIdx, rlDest, rlSrc);
2603}
2604
buzbee32412962012-06-26 16:27:56 -07002605void cvtThrow(CompilationUnit* cUnit, llvm::CallInst* callInst)
2606{
2607 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
2608 llvm::Value* src = callInst->getArgOperand(0);
2609 RegLocation rlSrc = getLoc(cUnit, src);
2610 genThrow(cUnit, rlSrc);
2611}
2612
buzbee8fa0fda2012-06-27 15:44:52 -07002613void cvtMonitorEnterExit(CompilationUnit* cUnit, bool isEnter,
2614 llvm::CallInst* callInst)
2615{
2616 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2617 llvm::ConstantInt* optFlags =
2618 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2619 llvm::Value* src = callInst->getArgOperand(1);
2620 RegLocation rlSrc = getLoc(cUnit, src);
2621 if (isEnter) {
2622 genMonitorEnter(cUnit, optFlags->getZExtValue(), rlSrc);
2623 } else {
2624 genMonitorExit(cUnit, optFlags->getZExtValue(), rlSrc);
2625 }
2626}
2627
buzbee76592632012-06-29 15:18:35 -07002628void cvtArrayLength(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002629{
2630 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2631 llvm::ConstantInt* optFlags =
2632 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2633 llvm::Value* src = callInst->getArgOperand(1);
2634 RegLocation rlSrc = getLoc(cUnit, src);
2635 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2636 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg, optFlags->getZExtValue());
2637 RegLocation rlDest = getLoc(cUnit, callInst);
2638 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2639 int lenOffset = Array::LengthOffset().Int32Value();
2640 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset, rlResult.lowReg);
2641 storeValue(cUnit, rlDest, rlResult);
2642}
2643
buzbee32412962012-06-26 16:27:56 -07002644void cvtMoveException(CompilationUnit* cUnit, llvm::CallInst* callInst)
2645{
2646 DCHECK_EQ(callInst->getNumArgOperands(), 0U);
2647 int exOffset = Thread::ExceptionOffset().Int32Value();
2648 RegLocation rlDest = getLoc(cUnit, callInst);
2649 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2650#if defined(TARGET_X86)
2651 newLIR2(cUnit, kX86Mov32RT, rlResult.lowReg, exOffset);
2652 newLIR2(cUnit, kX86Mov32TI, exOffset, 0);
2653#else
2654 int resetReg = oatAllocTemp(cUnit);
2655 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
2656 loadConstant(cUnit, resetReg, 0);
2657 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
2658 oatFreeTemp(cUnit, resetReg);
2659#endif
2660 storeValue(cUnit, rlDest, rlResult);
2661}
2662
buzbee4f1181f2012-06-22 13:52:12 -07002663void cvtSget(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2664 bool isObject)
2665{
buzbee32412962012-06-26 16:27:56 -07002666 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002667 llvm::ConstantInt* typeIdxVal =
2668 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2669 uint32_t typeIdx = typeIdxVal->getZExtValue();
2670 RegLocation rlDest = getLoc(cUnit, callInst);
2671 genSget(cUnit, typeIdx, rlDest, isWide, isObject);
2672}
2673
buzbee8fa0fda2012-06-27 15:44:52 -07002674void cvtSput(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2675 bool isObject)
2676{
2677 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2678 llvm::ConstantInt* typeIdxVal =
2679 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2680 uint32_t typeIdx = typeIdxVal->getZExtValue();
2681 llvm::Value* src = callInst->getArgOperand(1);
2682 RegLocation rlSrc = getLoc(cUnit, src);
2683 genSput(cUnit, typeIdx, rlSrc, isWide, isObject);
2684}
2685
2686void cvtAget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2687 int scale)
2688{
2689 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2690 llvm::ConstantInt* optFlags =
2691 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2692 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(1));
2693 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(2));
2694 RegLocation rlDest = getLoc(cUnit, callInst);
2695 genArrayGet(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2696 rlDest, scale);
2697}
2698
2699void cvtAput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
buzbeef1f86362012-07-10 15:18:31 -07002700 int scale, bool isObject)
buzbee8fa0fda2012-06-27 15:44:52 -07002701{
2702 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2703 llvm::ConstantInt* optFlags =
2704 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2705 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2706 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(2));
2707 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(3));
buzbeef1f86362012-07-10 15:18:31 -07002708 if (isObject) {
2709 genArrayObjPut(cUnit, optFlags->getZExtValue(), rlArray, rlIndex,
2710 rlSrc, scale);
2711 } else {
2712 genArrayPut(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2713 rlSrc, scale);
2714 }
2715}
2716
2717void cvtAputObj(CompilationUnit* cUnit, llvm::CallInst* callInst)
2718{
2719 cvtAput(cUnit, callInst, kWord, 2, true /* isObject */);
2720}
2721
2722void cvtAputPrimitive(CompilationUnit* cUnit, llvm::CallInst* callInst,
2723 OpSize size, int scale)
2724{
2725 cvtAput(cUnit, callInst, size, scale, false /* isObject */);
buzbee8fa0fda2012-06-27 15:44:52 -07002726}
2727
buzbee101305f2012-06-28 18:00:56 -07002728void cvtIget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2729 bool isWide, bool isObj)
2730{
2731 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2732 llvm::ConstantInt* optFlags =
2733 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2734 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(1));
2735 llvm::ConstantInt* fieldIdx =
2736 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2737 RegLocation rlDest = getLoc(cUnit, callInst);
2738 genIGet(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2739 size, rlDest, rlObj, isWide, isObj);
2740}
2741
2742void cvtIput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2743 bool isWide, bool isObj)
2744{
2745 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2746 llvm::ConstantInt* optFlags =
2747 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2748 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2749 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(2));
2750 llvm::ConstantInt* fieldIdx =
buzbee4f4dfc72012-07-02 14:54:44 -07002751 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(3));
buzbee101305f2012-06-28 18:00:56 -07002752 genIPut(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2753 size, rlSrc, rlObj, isWide, isObj);
2754}
2755
2756void cvtCheckCast(CompilationUnit* cUnit, llvm::CallInst* callInst)
2757{
2758 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2759 llvm::ConstantInt* typeIdx =
2760 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2761 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2762 genCheckCast(cUnit, typeIdx->getZExtValue(), rlSrc);
2763}
2764
buzbee76592632012-06-29 15:18:35 -07002765void cvtFPCompare(CompilationUnit* cUnit, llvm::CallInst* callInst,
2766 Instruction::Code opcode)
2767{
2768 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2769 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2770 RegLocation rlDest = getLoc(cUnit, callInst);
2771 genCmpFP(cUnit, opcode, rlDest, rlSrc1, rlSrc2);
2772}
2773
2774void cvtLongCompare(CompilationUnit* cUnit, llvm::CallInst* callInst)
2775{
2776 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2777 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2778 RegLocation rlDest = getLoc(cUnit, callInst);
2779 genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2);
2780}
2781
buzbeef58c12c2012-07-03 15:06:29 -07002782void cvtSwitch(CompilationUnit* cUnit, llvm::Instruction* inst)
2783{
2784 llvm::SwitchInst* swInst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2785 DCHECK(swInst != NULL);
2786 llvm::Value* testVal = swInst->getCondition();
2787 llvm::MDNode* tableOffsetNode = swInst->getMetadata("SwitchTable");
2788 DCHECK(tableOffsetNode != NULL);
2789 llvm::ConstantInt* tableOffsetValue =
2790 static_cast<llvm::ConstantInt*>(tableOffsetNode->getOperand(0));
2791 int32_t tableOffset = tableOffsetValue->getSExtValue();
2792 RegLocation rlSrc = getLoc(cUnit, testVal);
buzbeea1da8a52012-07-09 14:00:21 -07002793 const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
2794 u2 tableMagic = *table;
2795 if (tableMagic == 0x100) {
2796 genPackedSwitch(cUnit, tableOffset, rlSrc);
2797 } else {
2798 DCHECK_EQ(tableMagic, 0x200);
2799 genSparseSwitch(cUnit, tableOffset, rlSrc);
2800 }
buzbeef58c12c2012-07-03 15:06:29 -07002801}
2802
buzbee6969d502012-06-15 16:40:31 -07002803void cvtInvoke(CompilationUnit* cUnit, llvm::CallInst* callInst,
buzbee76592632012-06-29 15:18:35 -07002804 bool isVoid, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -07002805{
2806 CallInfo* info = (CallInfo*)oatNew(cUnit, sizeof(CallInfo), true,
2807 kAllocMisc);
buzbee8fa0fda2012-06-27 15:44:52 -07002808 if (isVoid) {
buzbee6969d502012-06-15 16:40:31 -07002809 info->result.location = kLocInvalid;
2810 } else {
2811 info->result = getLoc(cUnit, callInst);
2812 }
2813 llvm::ConstantInt* invokeTypeVal =
2814 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2815 llvm::ConstantInt* methodIndexVal =
2816 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(1));
2817 llvm::ConstantInt* optFlagsVal =
2818 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2819 info->type = static_cast<InvokeType>(invokeTypeVal->getZExtValue());
2820 info->index = methodIndexVal->getZExtValue();
2821 info->optFlags = optFlagsVal->getZExtValue();
2822 info->offset = cUnit->currentDalvikOffset;
2823
buzbee6969d502012-06-15 16:40:31 -07002824 // Count the argument words, and then build argument array.
2825 info->numArgWords = 0;
2826 for (unsigned int i = 3; i < callInst->getNumArgOperands(); i++) {
2827 RegLocation tLoc = getLoc(cUnit, callInst->getArgOperand(i));
2828 info->numArgWords += tLoc.wide ? 2 : 1;
2829 }
2830 info->args = (info->numArgWords == 0) ? NULL : (RegLocation*)
2831 oatNew(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc);
2832 // Now, fill in the location records, synthesizing high loc of wide vals
2833 for (int i = 3, next = 0; next < info->numArgWords;) {
buzbee4f1181f2012-06-22 13:52:12 -07002834 info->args[next] = getLoc(cUnit, callInst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002835 if (info->args[next].wide) {
2836 next++;
2837 // TODO: Might make sense to mark this as an invalid loc
2838 info->args[next].origSReg = info->args[next-1].origSReg+1;
2839 info->args[next].sRegLow = info->args[next-1].sRegLow+1;
2840 }
2841 next++;
2842 }
buzbee4f4dfc72012-07-02 14:54:44 -07002843 // TODO - rework such that we no longer need isRange
2844 info->isRange = (info->numArgWords > 5);
2845
buzbee76592632012-06-29 15:18:35 -07002846 if (isFilledNewArray) {
buzbee101305f2012-06-28 18:00:56 -07002847 genFilledNewArray(cUnit, info);
2848 } else {
2849 genInvoke(cUnit, info);
2850 }
buzbee6969d502012-06-15 16:40:31 -07002851}
2852
buzbeead8f15e2012-06-18 14:49:45 -07002853/* Look up the RegLocation associated with a Value. Must already be defined */
2854RegLocation valToLoc(CompilationUnit* cUnit, llvm::Value* val)
2855{
2856 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2857 DCHECK(it != cUnit->locMap.end()) << "Missing definition";
2858 return it->second;
2859}
2860
buzbee2cfc6392012-05-07 14:51:40 -07002861bool methodBitcodeBlockCodeGen(CompilationUnit* cUnit, llvm::BasicBlock* bb)
2862{
buzbee0967a252012-09-14 10:43:54 -07002863 while (cUnit->llvmBlocks.find(bb) == cUnit->llvmBlocks.end()) {
2864 llvm::BasicBlock* nextBB = NULL;
2865 cUnit->llvmBlocks.insert(bb);
2866 bool isEntry = (bb == &cUnit->func->getEntryBlock());
2867 // Define the starting label
2868 LIR* blockLabel = cUnit->blockToLabelMap.Get(bb);
2869 // Extract the type and starting offset from the block's name
2870 char blockType = kNormalBlock;
2871 if (!isEntry) {
2872 const char* blockName = bb->getName().str().c_str();
2873 int dummy;
2874 sscanf(blockName, kLabelFormat, &blockType, &blockLabel->operands[0], &dummy);
2875 cUnit->currentDalvikOffset = blockLabel->operands[0];
2876 } else {
2877 cUnit->currentDalvikOffset = 0;
2878 }
2879 // Set the label kind
2880 blockLabel->opcode = kPseudoNormalBlockLabel;
2881 // Insert the label
2882 oatAppendLIR(cUnit, blockLabel);
buzbee2cfc6392012-05-07 14:51:40 -07002883
buzbee0967a252012-09-14 10:43:54 -07002884 LIR* headLIR = NULL;
buzbee8320f382012-09-11 16:29:42 -07002885
buzbee0967a252012-09-14 10:43:54 -07002886 if (blockType == kCatchBlock) {
2887 headLIR = newLIR0(cUnit, kPseudoSafepointPC);
2888 }
buzbee8320f382012-09-11 16:29:42 -07002889
buzbee0967a252012-09-14 10:43:54 -07002890 // Free temp registers and reset redundant store tracking */
2891 oatResetRegPool(cUnit);
2892 oatResetDefTracking(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002893
buzbee0967a252012-09-14 10:43:54 -07002894 //TODO: restore oat incoming liveness optimization
2895 oatClobberAllRegs(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002896
buzbee0967a252012-09-14 10:43:54 -07002897 if (isEntry) {
2898 RegLocation* argLocs = (RegLocation*)
2899 oatNew(cUnit, sizeof(RegLocation) * cUnit->numIns, true, kAllocMisc);
2900 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
2901 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
2902 // Skip past Method*
2903 it++;
2904 for (unsigned i = 0; it != it_end; ++it) {
2905 llvm::Value* val = it;
2906 argLocs[i++] = valToLoc(cUnit, val);
2907 llvm::Type* ty = val->getType();
2908 if ((ty == cUnit->irb->getInt64Ty()) || (ty == cUnit->irb->getDoubleTy())) {
2909 argLocs[i] = argLocs[i-1];
2910 argLocs[i].lowReg = argLocs[i].highReg;
2911 argLocs[i].origSReg++;
2912 argLocs[i].sRegLow = INVALID_SREG;
2913 argLocs[i].highWord = true;
2914 i++;
2915 }
2916 }
2917 genEntrySequence(cUnit, argLocs, cUnit->methodLoc);
2918 }
2919
2920 // Visit all of the instructions in the block
2921 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2922 llvm::Instruction* inst = it;
2923 llvm::BasicBlock::iterator nextIt = ++it;
2924 // Extract the Dalvik offset from the instruction
2925 uint32_t opcode = inst->getOpcode();
2926 llvm::MDNode* dexOffsetNode = inst->getMetadata("DexOff");
2927 if (dexOffsetNode != NULL) {
2928 llvm::ConstantInt* dexOffsetValue =
2929 static_cast<llvm::ConstantInt*>(dexOffsetNode->getOperand(0));
2930 cUnit->currentDalvikOffset = dexOffsetValue->getZExtValue();
2931 }
2932
2933 oatResetRegPool(cUnit);
2934 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
2935 oatClobberAllRegs(cUnit);
2936 }
2937
2938 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
2939 oatResetDefTracking(cUnit);
2940 }
2941
2942 #ifndef NDEBUG
2943 /* Reset temp tracking sanity check */
2944 cUnit->liveSReg = INVALID_SREG;
2945 #endif
2946
2947 // TODO: use llvm opcode name here instead of "boundary" if verbose
2948 LIR* boundaryLIR = markBoundary(cUnit, cUnit->currentDalvikOffset, "boundary");
2949
2950 /* Remember the first LIR for thisl block*/
2951 if (headLIR == NULL) {
2952 headLIR = boundaryLIR;
2953 headLIR->defMask = ENCODE_ALL;
2954 }
2955
2956 switch(opcode) {
2957
2958 case llvm::Instruction::ICmp: {
2959 llvm::Instruction* nextInst = nextIt;
2960 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(nextInst);
2961 if (brInst != NULL /* and... */) {
2962 cvtICmpBr(cUnit, inst, brInst);
2963 ++it;
2964 } else {
2965 cvtICmp(cUnit, inst);
2966 }
2967 }
2968 break;
2969
2970 case llvm::Instruction::Call: {
2971 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(inst);
2972 llvm::Function* callee = callInst->getCalledFunction();
2973 greenland::IntrinsicHelper::IntrinsicId id =
2974 cUnit->intrinsic_helper->GetIntrinsicId(callee);
2975 switch (id) {
2976 case greenland::IntrinsicHelper::AllocaShadowFrame:
2977 case greenland::IntrinsicHelper::SetShadowFrameEntry:
2978 case greenland::IntrinsicHelper::PopShadowFrame:
2979 // Ignore shadow frame stuff for quick compiler
2980 break;
2981 case greenland::IntrinsicHelper::CopyInt:
2982 case greenland::IntrinsicHelper::CopyObj:
2983 case greenland::IntrinsicHelper::CopyFloat:
2984 case greenland::IntrinsicHelper::CopyLong:
2985 case greenland::IntrinsicHelper::CopyDouble:
2986 cvtCopy(cUnit, callInst);
2987 break;
2988 case greenland::IntrinsicHelper::ConstInt:
2989 case greenland::IntrinsicHelper::ConstObj:
2990 case greenland::IntrinsicHelper::ConstLong:
2991 case greenland::IntrinsicHelper::ConstFloat:
2992 case greenland::IntrinsicHelper::ConstDouble:
2993 cvtConst(cUnit, callInst);
2994 break;
2995 case greenland::IntrinsicHelper::DivInt:
2996 case greenland::IntrinsicHelper::DivLong:
2997 cvtBinOp(cUnit, kOpDiv, inst);
2998 break;
2999 case greenland::IntrinsicHelper::RemInt:
3000 case greenland::IntrinsicHelper::RemLong:
3001 cvtBinOp(cUnit, kOpRem, inst);
3002 break;
3003 case greenland::IntrinsicHelper::MethodInfo:
3004 // Already dealt with - just ignore it here.
3005 break;
3006 case greenland::IntrinsicHelper::CheckSuspend:
3007 genSuspendTest(cUnit, 0 /* optFlags already applied */);
3008 break;
3009 case greenland::IntrinsicHelper::HLInvokeObj:
3010 case greenland::IntrinsicHelper::HLInvokeFloat:
3011 case greenland::IntrinsicHelper::HLInvokeDouble:
3012 case greenland::IntrinsicHelper::HLInvokeLong:
3013 case greenland::IntrinsicHelper::HLInvokeInt:
3014 cvtInvoke(cUnit, callInst, false /* isVoid */, false /* newArray */);
3015 break;
3016 case greenland::IntrinsicHelper::HLInvokeVoid:
3017 cvtInvoke(cUnit, callInst, true /* isVoid */, false /* newArray */);
3018 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003019 case greenland::IntrinsicHelper::HLFilledNewArray:
buzbee0967a252012-09-14 10:43:54 -07003020 cvtInvoke(cUnit, callInst, false /* isVoid */, true /* newArray */);
3021 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003022 case greenland::IntrinsicHelper::HLFillArrayData:
buzbee0967a252012-09-14 10:43:54 -07003023 cvtFillArrayData(cUnit, callInst);
3024 break;
3025 case greenland::IntrinsicHelper::ConstString:
3026 cvtConstObject(cUnit, callInst, true /* isString */);
3027 break;
3028 case greenland::IntrinsicHelper::ConstClass:
3029 cvtConstObject(cUnit, callInst, false /* isString */);
3030 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003031 case greenland::IntrinsicHelper::HLCheckCast:
buzbee0967a252012-09-14 10:43:54 -07003032 cvtCheckCast(cUnit, callInst);
3033 break;
3034 case greenland::IntrinsicHelper::NewInstance:
3035 cvtNewInstance(cUnit, callInst);
3036 break;
3037 case greenland::IntrinsicHelper::HLSgetObject:
3038 cvtSget(cUnit, callInst, false /* wide */, true /* Object */);
3039 break;
3040 case greenland::IntrinsicHelper::HLSget:
3041 case greenland::IntrinsicHelper::HLSgetFloat:
3042 case greenland::IntrinsicHelper::HLSgetBoolean:
3043 case greenland::IntrinsicHelper::HLSgetByte:
3044 case greenland::IntrinsicHelper::HLSgetChar:
3045 case greenland::IntrinsicHelper::HLSgetShort:
3046 cvtSget(cUnit, callInst, false /* wide */, false /* Object */);
3047 break;
3048 case greenland::IntrinsicHelper::HLSgetWide:
3049 case greenland::IntrinsicHelper::HLSgetDouble:
3050 cvtSget(cUnit, callInst, true /* wide */, false /* Object */);
3051 break;
3052 case greenland::IntrinsicHelper::HLSput:
3053 case greenland::IntrinsicHelper::HLSputFloat:
3054 case greenland::IntrinsicHelper::HLSputBoolean:
3055 case greenland::IntrinsicHelper::HLSputByte:
3056 case greenland::IntrinsicHelper::HLSputChar:
3057 case greenland::IntrinsicHelper::HLSputShort:
3058 cvtSput(cUnit, callInst, false /* wide */, false /* Object */);
3059 break;
3060 case greenland::IntrinsicHelper::HLSputWide:
3061 case greenland::IntrinsicHelper::HLSputDouble:
3062 cvtSput(cUnit, callInst, true /* wide */, false /* Object */);
3063 break;
3064 case greenland::IntrinsicHelper::HLSputObject:
3065 cvtSput(cUnit, callInst, false /* wide */, true /* Object */);
3066 break;
3067 case greenland::IntrinsicHelper::GetException:
3068 cvtMoveException(cUnit, callInst);
3069 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003070 case greenland::IntrinsicHelper::ThrowException:
buzbee0967a252012-09-14 10:43:54 -07003071 cvtThrow(cUnit, callInst);
3072 break;
3073 case greenland::IntrinsicHelper::MonitorEnter:
3074 cvtMonitorEnterExit(cUnit, true /* isEnter */, callInst);
3075 break;
3076 case greenland::IntrinsicHelper::MonitorExit:
3077 cvtMonitorEnterExit(cUnit, false /* isEnter */, callInst);
3078 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003079 case greenland::IntrinsicHelper::OptArrayLength:
buzbee0967a252012-09-14 10:43:54 -07003080 cvtArrayLength(cUnit, callInst);
3081 break;
3082 case greenland::IntrinsicHelper::NewArray:
3083 cvtNewArray(cUnit, callInst);
3084 break;
3085 case greenland::IntrinsicHelper::InstanceOf:
3086 cvtInstanceOf(cUnit, callInst);
3087 break;
3088
3089 case greenland::IntrinsicHelper::HLArrayGet:
3090 case greenland::IntrinsicHelper::HLArrayGetObject:
3091 case greenland::IntrinsicHelper::HLArrayGetFloat:
3092 cvtAget(cUnit, callInst, kWord, 2);
3093 break;
3094 case greenland::IntrinsicHelper::HLArrayGetWide:
3095 case greenland::IntrinsicHelper::HLArrayGetDouble:
3096 cvtAget(cUnit, callInst, kLong, 3);
3097 break;
3098 case greenland::IntrinsicHelper::HLArrayGetBoolean:
3099 cvtAget(cUnit, callInst, kUnsignedByte, 0);
3100 break;
3101 case greenland::IntrinsicHelper::HLArrayGetByte:
3102 cvtAget(cUnit, callInst, kSignedByte, 0);
3103 break;
3104 case greenland::IntrinsicHelper::HLArrayGetChar:
3105 cvtAget(cUnit, callInst, kUnsignedHalf, 1);
3106 break;
3107 case greenland::IntrinsicHelper::HLArrayGetShort:
3108 cvtAget(cUnit, callInst, kSignedHalf, 1);
3109 break;
3110
3111 case greenland::IntrinsicHelper::HLArrayPut:
3112 case greenland::IntrinsicHelper::HLArrayPutFloat:
3113 cvtAputPrimitive(cUnit, callInst, kWord, 2);
3114 break;
3115 case greenland::IntrinsicHelper::HLArrayPutObject:
3116 cvtAputObj(cUnit, callInst);
3117 break;
3118 case greenland::IntrinsicHelper::HLArrayPutWide:
3119 case greenland::IntrinsicHelper::HLArrayPutDouble:
3120 cvtAputPrimitive(cUnit, callInst, kLong, 3);
3121 break;
3122 case greenland::IntrinsicHelper::HLArrayPutBoolean:
3123 cvtAputPrimitive(cUnit, callInst, kUnsignedByte, 0);
3124 break;
3125 case greenland::IntrinsicHelper::HLArrayPutByte:
3126 cvtAputPrimitive(cUnit, callInst, kSignedByte, 0);
3127 break;
3128 case greenland::IntrinsicHelper::HLArrayPutChar:
3129 cvtAputPrimitive(cUnit, callInst, kUnsignedHalf, 1);
3130 break;
3131 case greenland::IntrinsicHelper::HLArrayPutShort:
3132 cvtAputPrimitive(cUnit, callInst, kSignedHalf, 1);
3133 break;
3134
3135 case greenland::IntrinsicHelper::HLIGet:
3136 case greenland::IntrinsicHelper::HLIGetFloat:
3137 cvtIget(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3138 break;
3139 case greenland::IntrinsicHelper::HLIGetObject:
3140 cvtIget(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3141 break;
3142 case greenland::IntrinsicHelper::HLIGetWide:
3143 case greenland::IntrinsicHelper::HLIGetDouble:
3144 cvtIget(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3145 break;
3146 case greenland::IntrinsicHelper::HLIGetBoolean:
3147 cvtIget(cUnit, callInst, kUnsignedByte, false /* isWide */,
3148 false /* obj */);
3149 break;
3150 case greenland::IntrinsicHelper::HLIGetByte:
3151 cvtIget(cUnit, callInst, kSignedByte, false /* isWide */,
3152 false /* obj */);
3153 break;
3154 case greenland::IntrinsicHelper::HLIGetChar:
3155 cvtIget(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3156 false /* obj */);
3157 break;
3158 case greenland::IntrinsicHelper::HLIGetShort:
3159 cvtIget(cUnit, callInst, kSignedHalf, false /* isWide */,
3160 false /* obj */);
3161 break;
3162
3163 case greenland::IntrinsicHelper::HLIPut:
3164 case greenland::IntrinsicHelper::HLIPutFloat:
3165 cvtIput(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3166 break;
3167 case greenland::IntrinsicHelper::HLIPutObject:
3168 cvtIput(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3169 break;
3170 case greenland::IntrinsicHelper::HLIPutWide:
3171 case greenland::IntrinsicHelper::HLIPutDouble:
3172 cvtIput(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3173 break;
3174 case greenland::IntrinsicHelper::HLIPutBoolean:
3175 cvtIput(cUnit, callInst, kUnsignedByte, false /* isWide */,
3176 false /* obj */);
3177 break;
3178 case greenland::IntrinsicHelper::HLIPutByte:
3179 cvtIput(cUnit, callInst, kSignedByte, false /* isWide */,
3180 false /* obj */);
3181 break;
3182 case greenland::IntrinsicHelper::HLIPutChar:
3183 cvtIput(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3184 false /* obj */);
3185 break;
3186 case greenland::IntrinsicHelper::HLIPutShort:
3187 cvtIput(cUnit, callInst, kSignedHalf, false /* isWide */,
3188 false /* obj */);
3189 break;
3190
3191 case greenland::IntrinsicHelper::IntToChar:
3192 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_CHAR);
3193 break;
3194 case greenland::IntrinsicHelper::IntToShort:
3195 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_SHORT);
3196 break;
3197 case greenland::IntrinsicHelper::IntToByte:
3198 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_BYTE);
3199 break;
3200
3201 case greenland::IntrinsicHelper::CmplFloat:
3202 cvtFPCompare(cUnit, callInst, Instruction::CMPL_FLOAT);
3203 break;
3204 case greenland::IntrinsicHelper::CmpgFloat:
3205 cvtFPCompare(cUnit, callInst, Instruction::CMPG_FLOAT);
3206 break;
3207 case greenland::IntrinsicHelper::CmplDouble:
3208 cvtFPCompare(cUnit, callInst, Instruction::CMPL_DOUBLE);
3209 break;
3210 case greenland::IntrinsicHelper::CmpgDouble:
3211 cvtFPCompare(cUnit, callInst, Instruction::CMPG_DOUBLE);
3212 break;
3213
3214 case greenland::IntrinsicHelper::CmpLong:
3215 cvtLongCompare(cUnit, callInst);
3216 break;
3217
3218 case greenland::IntrinsicHelper::SHLLong:
3219 cvtShiftOp(cUnit, Instruction::SHL_LONG, callInst);
3220 break;
3221 case greenland::IntrinsicHelper::SHRLong:
3222 cvtShiftOp(cUnit, Instruction::SHR_LONG, callInst);
3223 break;
3224 case greenland::IntrinsicHelper::USHRLong:
3225 cvtShiftOp(cUnit, Instruction::USHR_LONG, callInst);
3226 break;
3227 case greenland::IntrinsicHelper::SHLInt:
3228 cvtShiftOp(cUnit, Instruction::SHL_INT, callInst);
3229 break;
3230 case greenland::IntrinsicHelper::SHRInt:
3231 cvtShiftOp(cUnit, Instruction::SHR_INT, callInst);
3232 break;
3233 case greenland::IntrinsicHelper::USHRInt:
3234 cvtShiftOp(cUnit, Instruction::USHR_INT, callInst);
3235 break;
3236
3237 case greenland::IntrinsicHelper::CatchTargets: {
3238 llvm::SwitchInst* swInst =
3239 llvm::dyn_cast<llvm::SwitchInst>(nextIt);
3240 DCHECK(swInst != NULL);
3241 /*
3242 * Discard the edges and the following conditional branch.
3243 * Do a direct branch to the default target (which is the
3244 * "work" portion of the pair.
3245 * TODO: awful code layout - rework
3246 */
3247 llvm::BasicBlock* targetBB = swInst->getDefaultDest();
3248 DCHECK(targetBB != NULL);
3249 opUnconditionalBranch(cUnit,
3250 cUnit->blockToLabelMap.Get(targetBB));
3251 ++it;
3252 // Set next bb to default target - improves code layout
3253 nextBB = targetBB;
3254 }
3255 break;
3256
3257 default:
3258 LOG(FATAL) << "Unexpected intrinsic " << (int)id << ", "
3259 << cUnit->intrinsic_helper->GetName(id);
3260 }
3261 }
3262 break;
3263
3264 case llvm::Instruction::Br: cvtBr(cUnit, inst); break;
3265 case llvm::Instruction::Add: cvtBinOp(cUnit, kOpAdd, inst); break;
3266 case llvm::Instruction::Sub: cvtBinOp(cUnit, kOpSub, inst); break;
3267 case llvm::Instruction::Mul: cvtBinOp(cUnit, kOpMul, inst); break;
3268 case llvm::Instruction::SDiv: cvtBinOp(cUnit, kOpDiv, inst); break;
3269 case llvm::Instruction::SRem: cvtBinOp(cUnit, kOpRem, inst); break;
3270 case llvm::Instruction::And: cvtBinOp(cUnit, kOpAnd, inst); break;
3271 case llvm::Instruction::Or: cvtBinOp(cUnit, kOpOr, inst); break;
3272 case llvm::Instruction::Xor: cvtBinOp(cUnit, kOpXor, inst); break;
3273 case llvm::Instruction::PHI: cvtPhi(cUnit, inst); break;
3274 case llvm::Instruction::Ret: cvtRet(cUnit, inst); break;
3275 case llvm::Instruction::FAdd: cvtBinFPOp(cUnit, kOpAdd, inst); break;
3276 case llvm::Instruction::FSub: cvtBinFPOp(cUnit, kOpSub, inst); break;
3277 case llvm::Instruction::FMul: cvtBinFPOp(cUnit, kOpMul, inst); break;
3278 case llvm::Instruction::FDiv: cvtBinFPOp(cUnit, kOpDiv, inst); break;
3279 case llvm::Instruction::FRem: cvtBinFPOp(cUnit, kOpRem, inst); break;
3280 case llvm::Instruction::SIToFP: cvtIntToFP(cUnit, inst); break;
3281 case llvm::Instruction::FPToSI: cvtFPToInt(cUnit, inst); break;
3282 case llvm::Instruction::FPTrunc: cvtDoubleToFloat(cUnit, inst); break;
3283 case llvm::Instruction::FPExt: cvtFloatToDouble(cUnit, inst); break;
3284 case llvm::Instruction::Trunc: cvtTrunc(cUnit, inst); break;
3285
3286 case llvm::Instruction::ZExt: cvtIntExt(cUnit, inst, false /* signed */);
3287 break;
3288 case llvm::Instruction::SExt: cvtIntExt(cUnit, inst, true /* signed */);
3289 break;
3290
3291 case llvm::Instruction::Switch: cvtSwitch(cUnit, inst); break;
3292
3293 case llvm::Instruction::Unreachable:
3294 break; // FIXME: can we really ignore these?
3295
3296 case llvm::Instruction::Shl:
3297 case llvm::Instruction::LShr:
3298 case llvm::Instruction::AShr:
3299 case llvm::Instruction::Invoke:
3300 case llvm::Instruction::FPToUI:
3301 case llvm::Instruction::UIToFP:
3302 case llvm::Instruction::PtrToInt:
3303 case llvm::Instruction::IntToPtr:
3304 case llvm::Instruction::FCmp:
3305 case llvm::Instruction::URem:
3306 case llvm::Instruction::UDiv:
3307 case llvm::Instruction::Resume:
3308 case llvm::Instruction::Alloca:
3309 case llvm::Instruction::GetElementPtr:
3310 case llvm::Instruction::Fence:
3311 case llvm::Instruction::AtomicCmpXchg:
3312 case llvm::Instruction::AtomicRMW:
3313 case llvm::Instruction::BitCast:
3314 case llvm::Instruction::VAArg:
3315 case llvm::Instruction::Select:
3316 case llvm::Instruction::UserOp1:
3317 case llvm::Instruction::UserOp2:
3318 case llvm::Instruction::ExtractElement:
3319 case llvm::Instruction::InsertElement:
3320 case llvm::Instruction::ShuffleVector:
3321 case llvm::Instruction::ExtractValue:
3322 case llvm::Instruction::InsertValue:
3323 case llvm::Instruction::LandingPad:
3324 case llvm::Instruction::IndirectBr:
3325 case llvm::Instruction::Load:
3326 case llvm::Instruction::Store:
3327 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3328
3329 default:
3330 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3331 break;
buzbeead8f15e2012-06-18 14:49:45 -07003332 }
3333 }
buzbee2cfc6392012-05-07 14:51:40 -07003334
buzbee0967a252012-09-14 10:43:54 -07003335 if (headLIR != NULL) {
3336 oatApplyLocalOptimizations(cUnit, headLIR, cUnit->lastLIRInsn);
buzbee2cfc6392012-05-07 14:51:40 -07003337 }
buzbee0967a252012-09-14 10:43:54 -07003338 if (nextBB != NULL) {
3339 bb = nextBB;
3340 nextBB = NULL;
buzbee6969d502012-06-15 16:40:31 -07003341 }
buzbee6969d502012-06-15 16:40:31 -07003342 }
buzbee2cfc6392012-05-07 14:51:40 -07003343 return false;
3344}
3345
3346/*
3347 * Convert LLVM_IR to MIR:
3348 * o Iterate through the LLVM_IR and construct a graph using
3349 * standard MIR building blocks.
3350 * o Perform a basic-block optimization pass to remove unnecessary
3351 * store/load sequences.
3352 * o Convert the LLVM Value operands into RegLocations where applicable.
3353 * o Create ssaRep def/use operand arrays for each converted LLVM opcode
3354 * o Perform register promotion
3355 * o Iterate through the graph a basic block at a time, generating
3356 * LIR.
3357 * o Assemble LIR as usual.
3358 * o Profit.
3359 */
3360void oatMethodBitcode2LIR(CompilationUnit* cUnit)
3361{
buzbeead8f15e2012-06-18 14:49:45 -07003362 llvm::Function* func = cUnit->func;
3363 int numBasicBlocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003364 // Allocate a list for LIR basic block labels
3365 cUnit->blockLabelList =
buzbeea1da8a52012-07-09 14:00:21 -07003366 (LIR*)oatNew(cUnit, sizeof(LIR) * numBasicBlocks, true, kAllocLIR);
3367 LIR* labelList = cUnit->blockLabelList;
buzbee2cfc6392012-05-07 14:51:40 -07003368 int nextLabel = 0;
buzbeead8f15e2012-06-18 14:49:45 -07003369 for (llvm::Function::iterator i = func->begin(),
3370 e = func->end(); i != e; ++i) {
buzbee2cfc6392012-05-07 14:51:40 -07003371 cUnit->blockToLabelMap.Put(static_cast<llvm::BasicBlock*>(i),
3372 &labelList[nextLabel++]);
3373 }
buzbeead8f15e2012-06-18 14:49:45 -07003374
3375 /*
3376 * Keep honest - clear regLocations, Value => RegLocation,
3377 * promotion map and VmapTables.
3378 */
3379 cUnit->locMap.clear(); // Start fresh
3380 cUnit->regLocation = NULL;
3381 for (int i = 0; i < cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
3382 i++) {
3383 cUnit->promotionMap[i].coreLocation = kLocDalvikFrame;
3384 cUnit->promotionMap[i].fpLocation = kLocDalvikFrame;
3385 }
3386 cUnit->coreSpillMask = 0;
3387 cUnit->numCoreSpills = 0;
3388 cUnit->fpSpillMask = 0;
3389 cUnit->numFPSpills = 0;
3390 cUnit->coreVmapTable.clear();
3391 cUnit->fpVmapTable.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003392
3393 /*
3394 * At this point, we've lost all knowledge of register promotion.
3395 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003396 * exists - not required for correctness). Normally, this will
3397 * be the first instruction we encounter, so we won't have to iterate
3398 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003399 */
buzbeeca7a5e42012-08-20 11:12:18 -07003400 for (llvm::inst_iterator i = llvm::inst_begin(func),
3401 e = llvm::inst_end(func); i != e; ++i) {
3402 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(&*i);
3403 if (callInst != NULL) {
3404 llvm::Function* callee = callInst->getCalledFunction();
3405 greenland::IntrinsicHelper::IntrinsicId id =
3406 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3407 if (id == greenland::IntrinsicHelper::MethodInfo) {
3408 if (cUnit->printMe) {
3409 LOG(INFO) << "Found MethodInfo";
3410 }
3411 llvm::MDNode* regInfoNode = callInst->getMetadata("RegInfo");
3412 if (regInfoNode != NULL) {
3413 llvm::ConstantInt* numInsValue =
3414 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(0));
3415 llvm::ConstantInt* numRegsValue =
3416 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(1));
3417 llvm::ConstantInt* numOutsValue =
3418 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(2));
3419 llvm::ConstantInt* numCompilerTempsValue =
3420 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(3));
3421 llvm::ConstantInt* numSSARegsValue =
3422 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(4));
3423 if (cUnit->printMe) {
3424 LOG(INFO) << "RegInfo - Ins:" << numInsValue->getZExtValue()
3425 << ", Regs:" << numRegsValue->getZExtValue()
3426 << ", Outs:" << numOutsValue->getZExtValue()
3427 << ", CTemps:" << numCompilerTempsValue->getZExtValue()
3428 << ", SSARegs:" << numSSARegsValue->getZExtValue();
3429 }
3430 }
3431 llvm::MDNode* pmapInfoNode = callInst->getMetadata("PromotionMap");
3432 if (pmapInfoNode != NULL) {
3433 int elems = pmapInfoNode->getNumOperands();
3434 if (cUnit->printMe) {
3435 LOG(INFO) << "PMap size: " << elems;
3436 }
3437 for (int i = 0; i < elems; i++) {
3438 llvm::ConstantInt* rawMapData =
3439 static_cast<llvm::ConstantInt*>(pmapInfoNode->getOperand(i));
3440 uint32_t mapData = rawMapData->getZExtValue();
3441 PromotionMap* p = &cUnit->promotionMap[i];
3442 p->firstInPair = (mapData >> 24) & 0xff;
3443 p->fpReg = (mapData >> 16) & 0xff;
3444 p->coreReg = (mapData >> 8) & 0xff;
3445 p->fpLocation = static_cast<RegLocationType>((mapData >> 4) & 0xf);
3446 if (p->fpLocation == kLocPhysReg) {
3447 oatRecordFpPromotion(cUnit, p->fpReg, i);
3448 }
3449 p->coreLocation = static_cast<RegLocationType>(mapData & 0xf);
3450 if (p->coreLocation == kLocPhysReg) {
3451 oatRecordCorePromotion(cUnit, p->coreReg, i);
3452 }
3453 }
3454 if (cUnit->printMe) {
3455 oatDumpPromotionMap(cUnit);
3456 }
3457 }
3458 break;
3459 }
3460 }
3461 }
3462 oatAdjustSpillMask(cUnit);
3463 cUnit->frameSize = oatComputeFrameSize(cUnit);
buzbeead8f15e2012-06-18 14:49:45 -07003464
3465 // Create RegLocations for arguments
3466 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
3467 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
3468 for (; it != it_end; ++it) {
3469 llvm::Value* val = it;
3470 createLocFromValue(cUnit, val);
3471 }
3472 // Create RegLocations for all non-argument defintions
3473 for (llvm::inst_iterator i = llvm::inst_begin(func),
3474 e = llvm::inst_end(func); i != e; ++i) {
3475 llvm::Value* val = &*i;
3476 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
3477 createLocFromValue(cUnit, val);
3478 }
3479 }
3480
buzbee2cfc6392012-05-07 14:51:40 -07003481 // Walk the blocks, generating code.
3482 for (llvm::Function::iterator i = cUnit->func->begin(),
3483 e = cUnit->func->end(); i != e; ++i) {
3484 methodBitcodeBlockCodeGen(cUnit, static_cast<llvm::BasicBlock*>(i));
3485 }
3486
3487 handleSuspendLaunchpads(cUnit);
3488
3489 handleThrowLaunchpads(cUnit);
3490
3491 handleIntrinsicLaunchpads(cUnit);
3492
buzbee692be802012-08-29 15:52:59 -07003493 cUnit->func->eraseFromParent();
3494 cUnit->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003495}
3496
3497
3498} // namespace art
3499
3500#endif // ART_USE_QUICK_COMPILER