blob: 1e81458dcaa29f392a6d7a8c6257a02097cf29fc [file] [log] [blame]
buzbee2cfc6392012-05-07 14:51:40 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbee2cfc6392012-05-07 14:51:40 -070017#include "object_utils.h"
18
19#include <llvm/Support/ToolOutputFile.h>
20#include <llvm/Bitcode/ReaderWriter.h>
21#include <llvm/Analysis/Verifier.h>
22#include <llvm/Metadata.h>
23#include <llvm/ADT/DepthFirstIterator.h>
24#include <llvm/Instruction.h>
25#include <llvm/Type.h>
26#include <llvm/Instructions.h>
27#include <llvm/Support/Casting.h>
buzbeead8f15e2012-06-18 14:49:45 -070028#include <llvm/Support/InstIterator.h>
buzbee2cfc6392012-05-07 14:51:40 -070029
buzbee8320f382012-09-11 16:29:42 -070030static const char* kLabelFormat = "%c0x%x_%d";
buzbee951c0a12012-10-03 16:31:39 -070031static const char kInvalidBlock = 0xff;
buzbee8320f382012-09-11 16:29:42 -070032static const char kNormalBlock = 'L';
33static const char kCatchBlock = 'C';
buzbee2cfc6392012-05-07 14:51:40 -070034
35namespace art {
36extern const RegLocation badLoc;
buzbeeb03f4872012-06-11 15:22:11 -070037RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val);
buzbee2cfc6392012-05-07 14:51:40 -070038
39llvm::BasicBlock* getLLVMBlock(CompilationUnit* cUnit, int id)
40{
41 return cUnit->idToBlockMap.Get(id);
42}
43
44llvm::Value* getLLVMValue(CompilationUnit* cUnit, int sReg)
45{
46 return (llvm::Value*)oatGrowableListGetElement(&cUnit->llvmValues, sReg);
47}
48
49// Replace the placeholder value with the real definition
50void defineValue(CompilationUnit* cUnit, llvm::Value* val, int sReg)
51{
52 llvm::Value* placeholder = getLLVMValue(cUnit, sReg);
buzbee9a2487f2012-07-26 14:01:13 -070053 if (placeholder == NULL) {
54 // This can happen on instruction rewrite on verification failure
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070055 LOG(WARNING) << "Null placeholder";
buzbee9a2487f2012-07-26 14:01:13 -070056 return;
57 }
buzbee2cfc6392012-05-07 14:51:40 -070058 placeholder->replaceAllUsesWith(val);
59 val->takeName(placeholder);
60 cUnit->llvmValues.elemList[sReg] = (intptr_t)val;
buzbee4be777b2012-07-12 14:38:18 -070061 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(placeholder);
62 DCHECK(inst != NULL);
63 inst->eraseFromParent();
TDYa1278e950c12012-11-02 09:58:19 -070064
65 // Set vreg for debugging
66 if (!cUnit->compiler->IsDebuggingSupported()) {
67 greenland::IntrinsicHelper::IntrinsicId id =
68 greenland::IntrinsicHelper::SetVReg;
69 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
70 int vReg = SRegToVReg(cUnit, sReg);
71 llvm::Value* tableSlot = cUnit->irb->getInt32(vReg);
72 llvm::Value* args[] = { tableSlot, val };
73 cUnit->irb->CreateCall(func, args);
74 }
buzbee2cfc6392012-05-07 14:51:40 -070075}
76
77llvm::Type* llvmTypeFromLocRec(CompilationUnit* cUnit, RegLocation loc)
78{
79 llvm::Type* res = NULL;
80 if (loc.wide) {
81 if (loc.fp)
buzbee4f1181f2012-06-22 13:52:12 -070082 res = cUnit->irb->getDoubleTy();
buzbee2cfc6392012-05-07 14:51:40 -070083 else
buzbee4f1181f2012-06-22 13:52:12 -070084 res = cUnit->irb->getInt64Ty();
buzbee2cfc6392012-05-07 14:51:40 -070085 } else {
86 if (loc.fp) {
buzbee4f1181f2012-06-22 13:52:12 -070087 res = cUnit->irb->getFloatTy();
buzbee2cfc6392012-05-07 14:51:40 -070088 } else {
89 if (loc.ref)
90 res = cUnit->irb->GetJObjectTy();
91 else
buzbee4f1181f2012-06-22 13:52:12 -070092 res = cUnit->irb->getInt32Ty();
buzbee2cfc6392012-05-07 14:51:40 -070093 }
94 }
95 return res;
96}
97
buzbeead8f15e2012-06-18 14:49:45 -070098/* Create an in-memory RegLocation from an llvm Value. */
99void createLocFromValue(CompilationUnit* cUnit, llvm::Value* val)
100{
101 // NOTE: llvm takes shortcuts with c_str() - get to std::string firstt
102 std::string s(val->getName().str());
103 const char* valName = s.c_str();
buzbeead8f15e2012-06-18 14:49:45 -0700104 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
105 DCHECK(it == cUnit->locMap.end()) << " - already defined: " << valName;
106 int baseSReg = INVALID_SREG;
107 int subscript = -1;
108 sscanf(valName, "v%d_%d", &baseSReg, &subscript);
109 if ((baseSReg == INVALID_SREG) && (!strcmp(valName, "method"))) {
110 baseSReg = SSA_METHOD_BASEREG;
111 subscript = 0;
112 }
buzbeead8f15e2012-06-18 14:49:45 -0700113 DCHECK_NE(baseSReg, INVALID_SREG);
114 DCHECK_NE(subscript, -1);
115 // TODO: redo during C++'ification
116 RegLocation loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, INVALID_REG,
117 INVALID_REG, INVALID_SREG, INVALID_SREG};
118 llvm::Type* ty = val->getType();
119 loc.wide = ((ty == cUnit->irb->getInt64Ty()) ||
120 (ty == cUnit->irb->getDoubleTy()));
121 loc.defined = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700122 loc.home = false; // May change during promotion
buzbeead8f15e2012-06-18 14:49:45 -0700123 loc.sRegLow = baseSReg;
124 loc.origSReg = cUnit->locMap.size();
buzbeeca7a5e42012-08-20 11:12:18 -0700125 PromotionMap pMap = cUnit->promotionMap[baseSReg];
126 if (ty == cUnit->irb->getFloatTy()) {
127 loc.fp = true;
128 if (pMap.fpLocation == kLocPhysReg) {
129 loc.lowReg = pMap.fpReg;
130 loc.location = kLocPhysReg;
131 loc.home = true;
132 }
133 } else if (ty == cUnit->irb->getDoubleTy()) {
134 loc.fp = true;
135 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
136 if ((pMap.fpLocation == kLocPhysReg) &&
137 (pMapHigh.fpLocation == kLocPhysReg) &&
138 ((pMap.fpReg & 0x1) == 0) &&
139 (pMap.fpReg + 1 == pMapHigh.fpReg)) {
140 loc.lowReg = pMap.fpReg;
141 loc.highReg = pMapHigh.fpReg;
142 loc.location = kLocPhysReg;
143 loc.home = true;
144 }
145 } else if (ty == cUnit->irb->GetJObjectTy()) {
146 loc.ref = true;
147 if (pMap.coreLocation == kLocPhysReg) {
148 loc.lowReg = pMap.coreReg;
149 loc.location = kLocPhysReg;
150 loc.home = true;
151 }
152 } else if (ty == cUnit->irb->getInt64Ty()) {
153 loc.core = true;
154 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
155 if ((pMap.coreLocation == kLocPhysReg) &&
156 (pMapHigh.coreLocation == kLocPhysReg)) {
157 loc.lowReg = pMap.coreReg;
158 loc.highReg = pMapHigh.coreReg;
159 loc.location = kLocPhysReg;
160 loc.home = true;
161 }
162 } else {
163 loc.core = true;
164 if (pMap.coreLocation == kLocPhysReg) {
165 loc.lowReg = pMap.coreReg;
166 loc.location = kLocPhysReg;
167 loc.home = true;
168 }
169 }
170
171 if (cUnit->printMe && loc.home) {
172 if (loc.wide) {
buzbee0967a252012-09-14 10:43:54 -0700173 LOG(INFO) << "Promoted wide " << s << " to regs " << static_cast<int>(loc.lowReg)
buzbeeca7a5e42012-08-20 11:12:18 -0700174 << "/" << loc.highReg;
175 } else {
buzbee0967a252012-09-14 10:43:54 -0700176 LOG(INFO) << "Promoted " << s << " to reg " << static_cast<int>(loc.lowReg);
buzbeeca7a5e42012-08-20 11:12:18 -0700177 }
178 }
buzbeead8f15e2012-06-18 14:49:45 -0700179 cUnit->locMap.Put(val, loc);
180}
buzbee2cfc6392012-05-07 14:51:40 -0700181void initIR(CompilationUnit* cUnit)
182{
buzbee4df2bbd2012-10-11 14:46:06 -0700183 LLVMInfo* llvmInfo = cUnit->llvm_info;
184 if (llvmInfo == NULL) {
185 CompilerTls* tls = cUnit->compiler->GetTls();
186 CHECK(tls != NULL);
187 llvmInfo = static_cast<LLVMInfo*>(tls->GetLLVMInfo());
188 if (llvmInfo == NULL) {
189 llvmInfo = new LLVMInfo();
190 tls->SetLLVMInfo(llvmInfo);
191 }
192 }
193 cUnit->context = llvmInfo->GetLLVMContext();
194 cUnit->module = llvmInfo->GetLLVMModule();
195 cUnit->intrinsic_helper = llvmInfo->GetIntrinsicHelper();
196 cUnit->irb = llvmInfo->GetIRBuilder();
buzbee2cfc6392012-05-07 14:51:40 -0700197}
198
199const char* llvmSSAName(CompilationUnit* cUnit, int ssaReg) {
200 return GET_ELEM_N(cUnit->ssaStrings, char*, ssaReg);
201}
202
buzbeef58c12c2012-07-03 15:06:29 -0700203llvm::BasicBlock* findCaseTarget(CompilationUnit* cUnit, uint32_t vaddr)
204{
205 BasicBlock* bb = oatFindBlock(cUnit, vaddr);
206 DCHECK(bb != NULL);
207 return getLLVMBlock(cUnit, bb->id);
208}
209
210void convertPackedSwitch(CompilationUnit* cUnit, BasicBlock* bb,
211 int32_t tableOffset, RegLocation rlSrc)
212{
213 const Instruction::PackedSwitchPayload* payload =
214 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
215 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
216
217 llvm::Value* value = getLLVMValue(cUnit, rlSrc.origSReg);
218
219 llvm::SwitchInst* sw =
220 cUnit->irb->CreateSwitch(value, getLLVMBlock(cUnit, bb->fallThrough->id),
221 payload->case_count);
222
223 for (uint16_t i = 0; i < payload->case_count; ++i) {
224 llvm::BasicBlock* llvmBB =
225 findCaseTarget(cUnit, cUnit->currentDalvikOffset + payload->targets[i]);
226 sw->addCase(cUnit->irb->getInt32(payload->first_key + i), llvmBB);
227 }
228 llvm::MDNode* switchNode =
229 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
230 sw->setMetadata("SwitchTable", switchNode);
231 bb->taken = NULL;
232 bb->fallThrough = NULL;
233}
234
buzbeea1da8a52012-07-09 14:00:21 -0700235void convertSparseSwitch(CompilationUnit* cUnit, BasicBlock* bb,
236 int32_t tableOffset, RegLocation rlSrc)
237{
238 const Instruction::SparseSwitchPayload* payload =
239 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
240 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
241
242 const int32_t* keys = payload->GetKeys();
243 const int32_t* targets = payload->GetTargets();
244
245 llvm::Value* value = getLLVMValue(cUnit, rlSrc.origSReg);
246
247 llvm::SwitchInst* sw =
248 cUnit->irb->CreateSwitch(value, getLLVMBlock(cUnit, bb->fallThrough->id),
249 payload->case_count);
250
251 for (size_t i = 0; i < payload->case_count; ++i) {
252 llvm::BasicBlock* llvmBB =
253 findCaseTarget(cUnit, cUnit->currentDalvikOffset + targets[i]);
254 sw->addCase(cUnit->irb->getInt32(keys[i]), llvmBB);
255 }
256 llvm::MDNode* switchNode =
257 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
258 sw->setMetadata("SwitchTable", switchNode);
259 bb->taken = NULL;
260 bb->fallThrough = NULL;
261}
262
buzbee8fa0fda2012-06-27 15:44:52 -0700263void convertSget(CompilationUnit* cUnit, int32_t fieldIndex,
264 greenland::IntrinsicHelper::IntrinsicId id,
265 RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700266{
buzbee8fa0fda2012-06-27 15:44:52 -0700267 llvm::Constant* fieldIdx = cUnit->irb->getInt32(fieldIndex);
buzbee4f1181f2012-06-22 13:52:12 -0700268 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700269 llvm::Value* res = cUnit->irb->CreateCall(intr, fieldIdx);
270 defineValue(cUnit, res, rlDest.origSReg);
271}
272
273void convertSput(CompilationUnit* cUnit, int32_t fieldIndex,
274 greenland::IntrinsicHelper::IntrinsicId id,
275 RegLocation rlSrc)
276{
277 llvm::SmallVector<llvm::Value*, 2> args;
278 args.push_back(cUnit->irb->getInt32(fieldIndex));
279 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
280 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
281 cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700282}
283
buzbee101305f2012-06-28 18:00:56 -0700284void convertFillArrayData(CompilationUnit* cUnit, int32_t offset,
285 RegLocation rlArray)
286{
287 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700288 id = greenland::IntrinsicHelper::HLFillArrayData;
buzbee101305f2012-06-28 18:00:56 -0700289 llvm::SmallVector<llvm::Value*, 2> args;
290 args.push_back(cUnit->irb->getInt32(offset));
291 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
292 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
293 cUnit->irb->CreateCall(intr, args);
294}
295
buzbee2cfc6392012-05-07 14:51:40 -0700296llvm::Value* emitConst(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
297 RegLocation loc)
298{
299 greenland::IntrinsicHelper::IntrinsicId id;
300 if (loc.wide) {
301 if (loc.fp) {
302 id = greenland::IntrinsicHelper::ConstDouble;
303 } else {
304 id = greenland::IntrinsicHelper::ConstLong;
305 }
306 } else {
307 if (loc.fp) {
308 id = greenland::IntrinsicHelper::ConstFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700309 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700310 id = greenland::IntrinsicHelper::ConstObj;
311 } else {
312 id = greenland::IntrinsicHelper::ConstInt;
313 }
314 }
315 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
316 return cUnit->irb->CreateCall(intr, src);
317}
buzbeeb03f4872012-06-11 15:22:11 -0700318
319void emitPopShadowFrame(CompilationUnit* cUnit)
320{
321 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
322 greenland::IntrinsicHelper::PopShadowFrame);
323 cUnit->irb->CreateCall(intr);
324}
325
buzbee2cfc6392012-05-07 14:51:40 -0700326llvm::Value* emitCopy(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
327 RegLocation loc)
328{
329 greenland::IntrinsicHelper::IntrinsicId id;
330 if (loc.wide) {
331 if (loc.fp) {
332 id = greenland::IntrinsicHelper::CopyDouble;
333 } else {
334 id = greenland::IntrinsicHelper::CopyLong;
335 }
336 } else {
337 if (loc.fp) {
338 id = greenland::IntrinsicHelper::CopyFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700339 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700340 id = greenland::IntrinsicHelper::CopyObj;
341 } else {
342 id = greenland::IntrinsicHelper::CopyInt;
343 }
344 }
345 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
346 return cUnit->irb->CreateCall(intr, src);
347}
348
buzbee32412962012-06-26 16:27:56 -0700349void convertMoveException(CompilationUnit* cUnit, RegLocation rlDest)
350{
351 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
352 greenland::IntrinsicHelper::GetException);
353 llvm::Value* res = cUnit->irb->CreateCall(func);
354 defineValue(cUnit, res, rlDest.origSReg);
355}
356
357void convertThrow(CompilationUnit* cUnit, RegLocation rlSrc)
358{
359 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
360 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
TDYa127f71bf5a2012-07-29 20:09:52 -0700361 greenland::IntrinsicHelper::HLThrowException);
buzbee32412962012-06-26 16:27:56 -0700362 cUnit->irb->CreateCall(func, src);
buzbee32412962012-06-26 16:27:56 -0700363}
364
buzbee8fa0fda2012-06-27 15:44:52 -0700365void convertMonitorEnterExit(CompilationUnit* cUnit, int optFlags,
366 greenland::IntrinsicHelper::IntrinsicId id,
367 RegLocation rlSrc)
368{
369 llvm::SmallVector<llvm::Value*, 2> args;
370 args.push_back(cUnit->irb->getInt32(optFlags));
371 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
372 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
373 cUnit->irb->CreateCall(func, args);
374}
375
buzbee76592632012-06-29 15:18:35 -0700376void convertArrayLength(CompilationUnit* cUnit, int optFlags,
377 RegLocation rlDest, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700378{
379 llvm::SmallVector<llvm::Value*, 2> args;
380 args.push_back(cUnit->irb->getInt32(optFlags));
381 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
382 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700383 greenland::IntrinsicHelper::OptArrayLength);
buzbee76592632012-06-29 15:18:35 -0700384 llvm::Value* res = cUnit->irb->CreateCall(func, args);
385 defineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700386}
387
buzbee2cfc6392012-05-07 14:51:40 -0700388void emitSuspendCheck(CompilationUnit* cUnit)
389{
390 greenland::IntrinsicHelper::IntrinsicId id =
391 greenland::IntrinsicHelper::CheckSuspend;
392 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
393 cUnit->irb->CreateCall(intr);
394}
395
396llvm::Value* convertCompare(CompilationUnit* cUnit, ConditionCode cc,
397 llvm::Value* src1, llvm::Value* src2)
398{
399 llvm::Value* res = NULL;
buzbee76592632012-06-29 15:18:35 -0700400 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700401 switch(cc) {
402 case kCondEq: res = cUnit->irb->CreateICmpEQ(src1, src2); break;
403 case kCondNe: res = cUnit->irb->CreateICmpNE(src1, src2); break;
404 case kCondLt: res = cUnit->irb->CreateICmpSLT(src1, src2); break;
405 case kCondGe: res = cUnit->irb->CreateICmpSGE(src1, src2); break;
406 case kCondGt: res = cUnit->irb->CreateICmpSGT(src1, src2); break;
407 case kCondLe: res = cUnit->irb->CreateICmpSLE(src1, src2); break;
408 default: LOG(FATAL) << "Unexpected cc value " << cc;
409 }
410 return res;
411}
412
413void convertCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
414 ConditionCode cc, RegLocation rlSrc1,
415 RegLocation rlSrc2)
416{
417 if (bb->taken->startOffset <= mir->offset) {
418 emitSuspendCheck(cUnit);
419 }
420 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
421 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
422 llvm::Value* condValue = convertCompare(cUnit, cc, src1, src2);
423 condValue->setName(StringPrintf("t%d", cUnit->tempName++));
424 cUnit->irb->CreateCondBr(condValue, getLLVMBlock(cUnit, bb->taken->id),
425 getLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700426 // Don't redo the fallthrough branch in the BB driver
427 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700428}
429
430void convertCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb,
431 MIR* mir, ConditionCode cc, RegLocation rlSrc1)
432{
433 if (bb->taken->startOffset <= mir->offset) {
434 emitSuspendCheck(cUnit);
435 }
436 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
437 llvm::Value* src2;
438 if (rlSrc1.ref) {
439 src2 = cUnit->irb->GetJNull();
440 } else {
441 src2 = cUnit->irb->getInt32(0);
442 }
443 llvm::Value* condValue = convertCompare(cUnit, cc, src1, src2);
buzbee2cfc6392012-05-07 14:51:40 -0700444 cUnit->irb->CreateCondBr(condValue, getLLVMBlock(cUnit, bb->taken->id),
445 getLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700446 // Don't redo the fallthrough branch in the BB driver
447 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700448}
449
450llvm::Value* genDivModOp(CompilationUnit* cUnit, bool isDiv, bool isLong,
451 llvm::Value* src1, llvm::Value* src2)
452{
453 greenland::IntrinsicHelper::IntrinsicId id;
454 if (isLong) {
455 if (isDiv) {
456 id = greenland::IntrinsicHelper::DivLong;
457 } else {
458 id = greenland::IntrinsicHelper::RemLong;
459 }
Logan Chien554e6072012-07-23 20:00:01 -0700460 } else {
461 if (isDiv) {
buzbee2cfc6392012-05-07 14:51:40 -0700462 id = greenland::IntrinsicHelper::DivInt;
463 } else {
464 id = greenland::IntrinsicHelper::RemInt;
Logan Chien554e6072012-07-23 20:00:01 -0700465 }
buzbee2cfc6392012-05-07 14:51:40 -0700466 }
467 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
468 llvm::SmallVector<llvm::Value*, 2>args;
469 args.push_back(src1);
470 args.push_back(src2);
471 return cUnit->irb->CreateCall(intr, args);
472}
473
474llvm::Value* genArithOp(CompilationUnit* cUnit, OpKind op, bool isLong,
475 llvm::Value* src1, llvm::Value* src2)
476{
477 llvm::Value* res = NULL;
478 switch(op) {
479 case kOpAdd: res = cUnit->irb->CreateAdd(src1, src2); break;
480 case kOpSub: res = cUnit->irb->CreateSub(src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700481 case kOpRsub: res = cUnit->irb->CreateSub(src2, src1); break;
buzbee2cfc6392012-05-07 14:51:40 -0700482 case kOpMul: res = cUnit->irb->CreateMul(src1, src2); break;
483 case kOpOr: res = cUnit->irb->CreateOr(src1, src2); break;
484 case kOpAnd: res = cUnit->irb->CreateAnd(src1, src2); break;
485 case kOpXor: res = cUnit->irb->CreateXor(src1, src2); break;
486 case kOpDiv: res = genDivModOp(cUnit, true, isLong, src1, src2); break;
487 case kOpRem: res = genDivModOp(cUnit, false, isLong, src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700488 case kOpLsl: res = cUnit->irb->CreateShl(src1, src2); break;
489 case kOpLsr: res = cUnit->irb->CreateLShr(src1, src2); break;
490 case kOpAsr: res = cUnit->irb->CreateAShr(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700491 default:
492 LOG(FATAL) << "Invalid op " << op;
493 }
494 return res;
495}
496
497void convertFPArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
498 RegLocation rlSrc1, RegLocation rlSrc2)
499{
500 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
501 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
502 llvm::Value* res = NULL;
503 switch(op) {
504 case kOpAdd: res = cUnit->irb->CreateFAdd(src1, src2); break;
505 case kOpSub: res = cUnit->irb->CreateFSub(src1, src2); break;
506 case kOpMul: res = cUnit->irb->CreateFMul(src1, src2); break;
507 case kOpDiv: res = cUnit->irb->CreateFDiv(src1, src2); break;
508 case kOpRem: res = cUnit->irb->CreateFRem(src1, src2); break;
509 default:
510 LOG(FATAL) << "Invalid op " << op;
511 }
512 defineValue(cUnit, res, rlDest.origSReg);
513}
514
buzbee2a83e8f2012-07-13 16:42:30 -0700515void convertShift(CompilationUnit* cUnit,
516 greenland::IntrinsicHelper::IntrinsicId id,
517 RegLocation rlDest, RegLocation rlSrc1, RegLocation rlSrc2)
buzbee4f1181f2012-06-22 13:52:12 -0700518{
buzbee2a83e8f2012-07-13 16:42:30 -0700519 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
520 llvm::SmallVector<llvm::Value*, 2>args;
521 args.push_back(getLLVMValue(cUnit, rlSrc1.origSReg));
522 args.push_back(getLLVMValue(cUnit, rlSrc2.origSReg));
523 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
524 defineValue(cUnit, res, rlDest.origSReg);
525}
526
527void convertShiftLit(CompilationUnit* cUnit,
528 greenland::IntrinsicHelper::IntrinsicId id,
529 RegLocation rlDest, RegLocation rlSrc, int shiftAmount)
530{
531 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
532 llvm::SmallVector<llvm::Value*, 2>args;
533 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
534 args.push_back(cUnit->irb->getInt32(shiftAmount));
535 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700536 defineValue(cUnit, res, rlDest.origSReg);
537}
538
buzbee2cfc6392012-05-07 14:51:40 -0700539void convertArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
540 RegLocation rlSrc1, RegLocation rlSrc2)
541{
542 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
543 llvm::Value* src2 = getLLVMValue(cUnit, rlSrc2.origSReg);
buzbee4f4dfc72012-07-02 14:54:44 -0700544 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700545 llvm::Value* res = genArithOp(cUnit, op, rlDest.wide, src1, src2);
546 defineValue(cUnit, res, rlDest.origSReg);
547}
548
buzbeeb03f4872012-06-11 15:22:11 -0700549void setShadowFrameEntry(CompilationUnit* cUnit, llvm::Value* newVal)
550{
551 int index = -1;
552 DCHECK(newVal != NULL);
553 int vReg = SRegToVReg(cUnit, getLoc(cUnit, newVal).origSReg);
554 for (int i = 0; i < cUnit->numShadowFrameEntries; i++) {
555 if (cUnit->shadowMap[i] == vReg) {
556 index = i;
557 break;
558 }
559 }
TDYa127347166a2012-08-23 12:23:44 -0700560 if (index == -1) {
561 return;
562 }
buzbee6459e7c2012-10-02 14:42:41 -0700563 llvm::Type* ty = newVal->getType();
buzbeeb03f4872012-06-11 15:22:11 -0700564 greenland::IntrinsicHelper::IntrinsicId id =
565 greenland::IntrinsicHelper::SetShadowFrameEntry;
566 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
567 llvm::Value* tableSlot = cUnit->irb->getInt32(index);
buzbee6459e7c2012-10-02 14:42:41 -0700568 // If newVal is a Null pointer, we'll see it here as a const int. Replace
569 if (!ty->isPointerTy()) {
570 // TODO: assert newVal created w/ dex_lang_const_int(0) or dex_lang_const_float(0)
571 newVal = cUnit->irb->GetJNull();
572 }
buzbeeb03f4872012-06-11 15:22:11 -0700573 llvm::Value* args[] = { newVal, tableSlot };
574 cUnit->irb->CreateCall(func, args);
575}
576
buzbee2cfc6392012-05-07 14:51:40 -0700577void convertArithOpLit(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
578 RegLocation rlSrc1, int32_t imm)
579{
580 llvm::Value* src1 = getLLVMValue(cUnit, rlSrc1.origSReg);
581 llvm::Value* src2 = cUnit->irb->getInt32(imm);
582 llvm::Value* res = genArithOp(cUnit, op, rlDest.wide, src1, src2);
583 defineValue(cUnit, res, rlDest.origSReg);
584}
585
buzbee101305f2012-06-28 18:00:56 -0700586/*
587 * Process arguments for invoke. Note: this code is also used to
588 * collect and process arguments for NEW_FILLED_ARRAY and NEW_FILLED_ARRAY_RANGE.
589 * The requirements are similar.
590 */
buzbee6969d502012-06-15 16:40:31 -0700591void convertInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
buzbee76592632012-06-29 15:18:35 -0700592 InvokeType invokeType, bool isRange, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -0700593{
594 CallInfo* info = oatNewCallInfo(cUnit, bb, mir, invokeType, isRange);
595 llvm::SmallVector<llvm::Value*, 10> args;
596 // Insert the invokeType
597 args.push_back(cUnit->irb->getInt32(static_cast<int>(invokeType)));
598 // Insert the method_idx
599 args.push_back(cUnit->irb->getInt32(info->index));
600 // Insert the optimization flags
601 args.push_back(cUnit->irb->getInt32(info->optFlags));
602 // Now, insert the actual arguments
buzbee6969d502012-06-15 16:40:31 -0700603 for (int i = 0; i < info->numArgWords;) {
buzbee6969d502012-06-15 16:40:31 -0700604 llvm::Value* val = getLLVMValue(cUnit, info->args[i].origSReg);
605 args.push_back(val);
606 i += info->args[i].wide ? 2 : 1;
607 }
608 /*
609 * Choose the invoke return type based on actual usage. Note: may
610 * be different than shorty. For example, if a function return value
611 * is not used, we'll treat this as a void invoke.
612 */
613 greenland::IntrinsicHelper::IntrinsicId id;
buzbee76592632012-06-29 15:18:35 -0700614 if (isFilledNewArray) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700615 id = greenland::IntrinsicHelper::HLFilledNewArray;
buzbee101305f2012-06-28 18:00:56 -0700616 } else if (info->result.location == kLocInvalid) {
buzbee6969d502012-06-15 16:40:31 -0700617 id = greenland::IntrinsicHelper::HLInvokeVoid;
618 } else {
619 if (info->result.wide) {
620 if (info->result.fp) {
621 id = greenland::IntrinsicHelper::HLInvokeDouble;
622 } else {
buzbee8fa0fda2012-06-27 15:44:52 -0700623 id = greenland::IntrinsicHelper::HLInvokeLong;
buzbee6969d502012-06-15 16:40:31 -0700624 }
625 } else if (info->result.ref) {
626 id = greenland::IntrinsicHelper::HLInvokeObj;
627 } else if (info->result.fp) {
628 id = greenland::IntrinsicHelper::HLInvokeFloat;
629 } else {
630 id = greenland::IntrinsicHelper::HLInvokeInt;
631 }
632 }
633 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
634 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
635 if (info->result.location != kLocInvalid) {
636 defineValue(cUnit, res, info->result.origSReg);
TDYa127890ea892012-08-22 10:49:42 -0700637 if (info->result.ref) {
638 setShadowFrameEntry(cUnit, (llvm::Value*)
639 cUnit->llvmValues.elemList[info->result.origSReg]);
640 }
buzbee6969d502012-06-15 16:40:31 -0700641 }
642}
643
buzbee101305f2012-06-28 18:00:56 -0700644void convertConstObject(CompilationUnit* cUnit, uint32_t idx,
645 greenland::IntrinsicHelper::IntrinsicId id,
646 RegLocation rlDest)
buzbee6969d502012-06-15 16:40:31 -0700647{
buzbee6969d502012-06-15 16:40:31 -0700648 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee101305f2012-06-28 18:00:56 -0700649 llvm::Value* index = cUnit->irb->getInt32(idx);
buzbee6969d502012-06-15 16:40:31 -0700650 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
651 defineValue(cUnit, res, rlDest.origSReg);
652}
653
buzbee101305f2012-06-28 18:00:56 -0700654void convertCheckCast(CompilationUnit* cUnit, uint32_t type_idx,
655 RegLocation rlSrc)
656{
657 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700658 id = greenland::IntrinsicHelper::HLCheckCast;
buzbee101305f2012-06-28 18:00:56 -0700659 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
660 llvm::SmallVector<llvm::Value*, 2> args;
661 args.push_back(cUnit->irb->getInt32(type_idx));
662 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
663 cUnit->irb->CreateCall(intr, args);
664}
665
buzbee8fa0fda2012-06-27 15:44:52 -0700666void convertNewInstance(CompilationUnit* cUnit, uint32_t type_idx,
667 RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700668{
669 greenland::IntrinsicHelper::IntrinsicId id;
670 id = greenland::IntrinsicHelper::NewInstance;
671 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
672 llvm::Value* index = cUnit->irb->getInt32(type_idx);
673 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
674 defineValue(cUnit, res, rlDest.origSReg);
675}
676
buzbee8fa0fda2012-06-27 15:44:52 -0700677void convertNewArray(CompilationUnit* cUnit, uint32_t type_idx,
678 RegLocation rlDest, RegLocation rlSrc)
679{
680 greenland::IntrinsicHelper::IntrinsicId id;
681 id = greenland::IntrinsicHelper::NewArray;
682 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
683 llvm::SmallVector<llvm::Value*, 2> args;
684 args.push_back(cUnit->irb->getInt32(type_idx));
685 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
686 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
687 defineValue(cUnit, res, rlDest.origSReg);
688}
689
690void convertAget(CompilationUnit* cUnit, int optFlags,
691 greenland::IntrinsicHelper::IntrinsicId id,
692 RegLocation rlDest, RegLocation rlArray, RegLocation rlIndex)
693{
694 llvm::SmallVector<llvm::Value*, 3> args;
695 args.push_back(cUnit->irb->getInt32(optFlags));
696 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
697 args.push_back(getLLVMValue(cUnit, rlIndex.origSReg));
698 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
699 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
700 defineValue(cUnit, res, rlDest.origSReg);
701}
702
703void convertAput(CompilationUnit* cUnit, int optFlags,
704 greenland::IntrinsicHelper::IntrinsicId id,
705 RegLocation rlSrc, RegLocation rlArray, RegLocation rlIndex)
706{
707 llvm::SmallVector<llvm::Value*, 4> args;
708 args.push_back(cUnit->irb->getInt32(optFlags));
709 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
710 args.push_back(getLLVMValue(cUnit, rlArray.origSReg));
711 args.push_back(getLLVMValue(cUnit, rlIndex.origSReg));
712 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
713 cUnit->irb->CreateCall(intr, args);
714}
715
buzbee101305f2012-06-28 18:00:56 -0700716void convertIget(CompilationUnit* cUnit, int optFlags,
717 greenland::IntrinsicHelper::IntrinsicId id,
718 RegLocation rlDest, RegLocation rlObj, int fieldIndex)
719{
720 llvm::SmallVector<llvm::Value*, 3> args;
721 args.push_back(cUnit->irb->getInt32(optFlags));
722 args.push_back(getLLVMValue(cUnit, rlObj.origSReg));
723 args.push_back(cUnit->irb->getInt32(fieldIndex));
724 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
725 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
726 defineValue(cUnit, res, rlDest.origSReg);
727}
728
729void convertIput(CompilationUnit* cUnit, int optFlags,
730 greenland::IntrinsicHelper::IntrinsicId id,
731 RegLocation rlSrc, RegLocation rlObj, int fieldIndex)
732{
733 llvm::SmallVector<llvm::Value*, 4> args;
734 args.push_back(cUnit->irb->getInt32(optFlags));
735 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
736 args.push_back(getLLVMValue(cUnit, rlObj.origSReg));
737 args.push_back(cUnit->irb->getInt32(fieldIndex));
738 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
739 cUnit->irb->CreateCall(intr, args);
740}
741
buzbee8fa0fda2012-06-27 15:44:52 -0700742void convertInstanceOf(CompilationUnit* cUnit, uint32_t type_idx,
743 RegLocation rlDest, RegLocation rlSrc)
744{
745 greenland::IntrinsicHelper::IntrinsicId id;
746 id = greenland::IntrinsicHelper::InstanceOf;
747 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
748 llvm::SmallVector<llvm::Value*, 2> args;
749 args.push_back(cUnit->irb->getInt32(type_idx));
750 args.push_back(getLLVMValue(cUnit, rlSrc.origSReg));
751 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
752 defineValue(cUnit, res, rlDest.origSReg);
753}
754
buzbee101305f2012-06-28 18:00:56 -0700755void convertIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
756 RegLocation rlSrc)
757{
758 llvm::Value* res = cUnit->irb->CreateSExt(getLLVMValue(cUnit, rlSrc.origSReg),
759 cUnit->irb->getInt64Ty());
760 defineValue(cUnit, res, rlDest.origSReg);
761}
762
buzbee76592632012-06-29 15:18:35 -0700763void convertLongToInt(CompilationUnit* cUnit, RegLocation rlDest,
764 RegLocation rlSrc)
765{
766 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
767 llvm::Value* res = cUnit->irb->CreateTrunc(src, cUnit->irb->getInt32Ty());
768 defineValue(cUnit, res, rlDest.origSReg);
769}
770
771void convertFloatToDouble(CompilationUnit* cUnit, RegLocation rlDest,
772 RegLocation rlSrc)
773{
774 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
775 llvm::Value* res = cUnit->irb->CreateFPExt(src, cUnit->irb->getDoubleTy());
776 defineValue(cUnit, res, rlDest.origSReg);
777}
778
779void convertDoubleToFloat(CompilationUnit* cUnit, RegLocation rlDest,
780 RegLocation rlSrc)
781{
782 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
783 llvm::Value* res = cUnit->irb->CreateFPTrunc(src, cUnit->irb->getFloatTy());
784 defineValue(cUnit, res, rlDest.origSReg);
785}
786
787void convertWideComparison(CompilationUnit* cUnit,
788 greenland::IntrinsicHelper::IntrinsicId id,
789 RegLocation rlDest, RegLocation rlSrc1,
790 RegLocation rlSrc2)
791{
792 DCHECK_EQ(rlSrc1.fp, rlSrc2.fp);
793 DCHECK_EQ(rlSrc1.wide, rlSrc2.wide);
794 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
795 llvm::SmallVector<llvm::Value*, 2> args;
796 args.push_back(getLLVMValue(cUnit, rlSrc1.origSReg));
797 args.push_back(getLLVMValue(cUnit, rlSrc2.origSReg));
798 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
799 defineValue(cUnit, res, rlDest.origSReg);
800}
801
buzbee101305f2012-06-28 18:00:56 -0700802void convertIntNarrowing(CompilationUnit* cUnit, RegLocation rlDest,
803 RegLocation rlSrc,
804 greenland::IntrinsicHelper::IntrinsicId id)
805{
806 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700807 llvm::Value* res =
808 cUnit->irb->CreateCall(intr, getLLVMValue(cUnit, rlSrc.origSReg));
809 defineValue(cUnit, res, rlDest.origSReg);
810}
811
812void convertNeg(CompilationUnit* cUnit, RegLocation rlDest,
813 RegLocation rlSrc)
814{
815 llvm::Value* res = cUnit->irb->CreateNeg(getLLVMValue(cUnit, rlSrc.origSReg));
816 defineValue(cUnit, res, rlDest.origSReg);
817}
818
819void convertIntToFP(CompilationUnit* cUnit, llvm::Type* ty, RegLocation rlDest,
820 RegLocation rlSrc)
821{
822 llvm::Value* res =
823 cUnit->irb->CreateSIToFP(getLLVMValue(cUnit, rlSrc.origSReg), ty);
824 defineValue(cUnit, res, rlDest.origSReg);
825}
826
TDYa1274ec8ccd2012-08-11 07:04:57 -0700827void convertFPToInt(CompilationUnit* cUnit,
828 greenland::IntrinsicHelper::IntrinsicId id,
829 RegLocation rlDest,
buzbee76592632012-06-29 15:18:35 -0700830 RegLocation rlSrc)
831{
TDYa1274ec8ccd2012-08-11 07:04:57 -0700832 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
833 llvm::Value* res = cUnit->irb->CreateCall(intr, getLLVMValue(cUnit, rlSrc.origSReg));
buzbee76592632012-06-29 15:18:35 -0700834 defineValue(cUnit, res, rlDest.origSReg);
835}
836
837
838void convertNegFP(CompilationUnit* cUnit, RegLocation rlDest,
839 RegLocation rlSrc)
840{
841 llvm::Value* res =
842 cUnit->irb->CreateFNeg(getLLVMValue(cUnit, rlSrc.origSReg));
843 defineValue(cUnit, res, rlDest.origSReg);
844}
845
846void convertNot(CompilationUnit* cUnit, RegLocation rlDest,
847 RegLocation rlSrc)
848{
849 llvm::Value* src = getLLVMValue(cUnit, rlSrc.origSReg);
850 llvm::Value* res = cUnit->irb->CreateXor(src, static_cast<uint64_t>(-1));
buzbee101305f2012-06-28 18:00:56 -0700851 defineValue(cUnit, res, rlDest.origSReg);
852}
853
buzbee2cfc6392012-05-07 14:51:40 -0700854/*
855 * Target-independent code generation. Use only high-level
856 * load/store utilities here, or target-dependent genXX() handlers
857 * when necessary.
858 */
859bool convertMIRNode(CompilationUnit* cUnit, MIR* mir, BasicBlock* bb,
860 llvm::BasicBlock* llvmBB, LIR* labelList)
861{
862 bool res = false; // Assume success
863 RegLocation rlSrc[3];
864 RegLocation rlDest = badLoc;
buzbee2cfc6392012-05-07 14:51:40 -0700865 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee6969d502012-06-15 16:40:31 -0700866 uint32_t vB = mir->dalvikInsn.vB;
867 uint32_t vC = mir->dalvikInsn.vC;
buzbee8fa0fda2012-06-27 15:44:52 -0700868 int optFlags = mir->optimizationFlags;
buzbee6969d502012-06-15 16:40:31 -0700869
buzbeeb03f4872012-06-11 15:22:11 -0700870 bool objectDefinition = false;
buzbee2cfc6392012-05-07 14:51:40 -0700871
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700872 if (cUnit->printMe) {
873 if ((int)opcode < kMirOpFirst) {
874 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x"
875 << std::hex << (int)opcode;
876 } else {
877 LOG(INFO) << ".. opcode 0x" << std::hex << (int)opcode;
878 }
879 }
880
buzbee2cfc6392012-05-07 14:51:40 -0700881 /* Prep Src and Dest locations */
882 int nextSreg = 0;
883 int nextLoc = 0;
884 int attrs = oatDataFlowAttributes[opcode];
885 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
886 if (attrs & DF_UA) {
887 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700888 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700889 nextSreg+= 2;
890 } else {
891 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
892 nextSreg++;
893 }
894 }
895 if (attrs & DF_UB) {
896 if (attrs & DF_B_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700897 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700898 nextSreg+= 2;
899 } else {
900 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
901 nextSreg++;
902 }
903 }
904 if (attrs & DF_UC) {
905 if (attrs & DF_C_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700906 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700907 } else {
908 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
909 }
910 }
911 if (attrs & DF_DA) {
912 if (attrs & DF_A_WIDE) {
buzbee15bf9802012-06-12 17:49:27 -0700913 rlDest = oatGetDestWide(cUnit, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700914 } else {
buzbee15bf9802012-06-12 17:49:27 -0700915 rlDest = oatGetDest(cUnit, mir);
buzbeeb03f4872012-06-11 15:22:11 -0700916 if (rlDest.ref) {
917 objectDefinition = true;
918 }
buzbee2cfc6392012-05-07 14:51:40 -0700919 }
920 }
921
922 switch (opcode) {
923 case Instruction::NOP:
924 break;
925
926 case Instruction::MOVE:
927 case Instruction::MOVE_OBJECT:
928 case Instruction::MOVE_16:
929 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700930 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700931 case Instruction::MOVE_FROM16:
932 case Instruction::MOVE_WIDE:
933 case Instruction::MOVE_WIDE_16:
934 case Instruction::MOVE_WIDE_FROM16: {
935 /*
936 * Moves/copies are meaningless in pure SSA register form,
937 * but we need to preserve them for the conversion back into
938 * MIR (at least until we stop using the Dalvik register maps).
939 * Insert a dummy intrinsic copy call, which will be recognized
940 * by the quick path and removed by the portable path.
941 */
942 llvm::Value* src = getLLVMValue(cUnit, rlSrc[0].origSReg);
943 llvm::Value* res = emitCopy(cUnit, src, rlDest);
944 defineValue(cUnit, res, rlDest.origSReg);
945 }
946 break;
947
948 case Instruction::CONST:
949 case Instruction::CONST_4:
950 case Instruction::CONST_16: {
TDYa127347166a2012-08-23 12:23:44 -0700951 if (vB == 0) {
952 objectDefinition = true;
953 }
buzbee6969d502012-06-15 16:40:31 -0700954 llvm::Constant* immValue = cUnit->irb->GetJInt(vB);
buzbee2cfc6392012-05-07 14:51:40 -0700955 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
956 defineValue(cUnit, res, rlDest.origSReg);
957 }
958 break;
959
960 case Instruction::CONST_WIDE_16:
961 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700962 // Sign extend to 64 bits
963 int64_t imm = static_cast<int32_t>(vB);
964 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
buzbee2cfc6392012-05-07 14:51:40 -0700965 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
966 defineValue(cUnit, res, rlDest.origSReg);
967 }
968 break;
969
970 case Instruction::CONST_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700971 llvm::Constant* immValue = cUnit->irb->GetJInt(vB << 16);
buzbee2cfc6392012-05-07 14:51:40 -0700972 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
973 defineValue(cUnit, res, rlDest.origSReg);
974 }
975 break;
976
977 case Instruction::CONST_WIDE: {
978 llvm::Constant* immValue =
979 cUnit->irb->GetJLong(mir->dalvikInsn.vB_wide);
980 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
981 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700982 }
983 break;
buzbee2cfc6392012-05-07 14:51:40 -0700984 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700985 int64_t imm = static_cast<int64_t>(vB) << 48;
buzbee2cfc6392012-05-07 14:51:40 -0700986 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
987 llvm::Value* res = emitConst(cUnit, immValue, rlDest);
988 defineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700989 }
990 break;
991
buzbee8fa0fda2012-06-27 15:44:52 -0700992 case Instruction::SPUT_OBJECT:
buzbee76592632012-06-29 15:18:35 -0700993 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputObject,
buzbee8fa0fda2012-06-27 15:44:52 -0700994 rlSrc[0]);
995 break;
996 case Instruction::SPUT:
997 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -0700998 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputFloat,
buzbee8fa0fda2012-06-27 15:44:52 -0700999 rlSrc[0]);
1000 } else {
buzbee76592632012-06-29 15:18:35 -07001001 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSput, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001002 }
1003 break;
1004 case Instruction::SPUT_BOOLEAN:
buzbee76592632012-06-29 15:18:35 -07001005 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputBoolean,
buzbee8fa0fda2012-06-27 15:44:52 -07001006 rlSrc[0]);
1007 break;
1008 case Instruction::SPUT_BYTE:
buzbee76592632012-06-29 15:18:35 -07001009 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputByte, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001010 break;
1011 case Instruction::SPUT_CHAR:
buzbee76592632012-06-29 15:18:35 -07001012 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputChar, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001013 break;
1014 case Instruction::SPUT_SHORT:
buzbee76592632012-06-29 15:18:35 -07001015 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputShort, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001016 break;
1017 case Instruction::SPUT_WIDE:
1018 if (rlSrc[0].fp) {
buzbee76592632012-06-29 15:18:35 -07001019 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputDouble,
buzbee8fa0fda2012-06-27 15:44:52 -07001020 rlSrc[0]);
1021 } else {
buzbee76592632012-06-29 15:18:35 -07001022 convertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputWide,
buzbee8fa0fda2012-06-27 15:44:52 -07001023 rlSrc[0]);
1024 }
1025 break;
1026
1027 case Instruction::SGET_OBJECT:
1028 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetObject, rlDest);
1029 break;
1030 case Instruction::SGET:
1031 if (rlDest.fp) {
1032 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetFloat, rlDest);
1033 } else {
1034 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSget, rlDest);
1035 }
1036 break;
1037 case Instruction::SGET_BOOLEAN:
1038 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetBoolean, rlDest);
1039 break;
1040 case Instruction::SGET_BYTE:
1041 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetByte, rlDest);
1042 break;
1043 case Instruction::SGET_CHAR:
1044 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetChar, rlDest);
1045 break;
1046 case Instruction::SGET_SHORT:
1047 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetShort, rlDest);
1048 break;
1049 case Instruction::SGET_WIDE:
1050 if (rlDest.fp) {
1051 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetDouble,
1052 rlDest);
1053 } else {
1054 convertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetWide, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001055 }
1056 break;
buzbee2cfc6392012-05-07 14:51:40 -07001057
1058 case Instruction::RETURN_WIDE:
1059 case Instruction::RETURN:
1060 case Instruction::RETURN_OBJECT: {
TDYa1274f2935e2012-06-22 06:25:03 -07001061 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001062 emitSuspendCheck(cUnit);
1063 }
buzbeeb03f4872012-06-11 15:22:11 -07001064 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001065 cUnit->irb->CreateRet(getLLVMValue(cUnit, rlSrc[0].origSReg));
1066 bb->hasReturn = true;
1067 }
1068 break;
1069
1070 case Instruction::RETURN_VOID: {
TDYa1274f2935e2012-06-22 06:25:03 -07001071 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee2cfc6392012-05-07 14:51:40 -07001072 emitSuspendCheck(cUnit);
1073 }
buzbeeb03f4872012-06-11 15:22:11 -07001074 emitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001075 cUnit->irb->CreateRetVoid();
1076 bb->hasReturn = true;
1077 }
1078 break;
1079
1080 case Instruction::IF_EQ:
1081 convertCompareAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0], rlSrc[1]);
1082 break;
1083 case Instruction::IF_NE:
1084 convertCompareAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0], rlSrc[1]);
1085 break;
1086 case Instruction::IF_LT:
1087 convertCompareAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0], rlSrc[1]);
1088 break;
1089 case Instruction::IF_GE:
1090 convertCompareAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0], rlSrc[1]);
1091 break;
1092 case Instruction::IF_GT:
1093 convertCompareAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0], rlSrc[1]);
1094 break;
1095 case Instruction::IF_LE:
1096 convertCompareAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0], rlSrc[1]);
1097 break;
1098 case Instruction::IF_EQZ:
1099 convertCompareZeroAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0]);
1100 break;
1101 case Instruction::IF_NEZ:
1102 convertCompareZeroAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0]);
1103 break;
1104 case Instruction::IF_LTZ:
1105 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0]);
1106 break;
1107 case Instruction::IF_GEZ:
1108 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0]);
1109 break;
1110 case Instruction::IF_GTZ:
1111 convertCompareZeroAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0]);
1112 break;
1113 case Instruction::IF_LEZ:
1114 convertCompareZeroAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0]);
1115 break;
1116
1117 case Instruction::GOTO:
1118 case Instruction::GOTO_16:
1119 case Instruction::GOTO_32: {
1120 if (bb->taken->startOffset <= bb->startOffset) {
1121 emitSuspendCheck(cUnit);
1122 }
1123 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->taken->id));
1124 }
1125 break;
1126
1127 case Instruction::ADD_LONG:
1128 case Instruction::ADD_LONG_2ADDR:
1129 case Instruction::ADD_INT:
1130 case Instruction::ADD_INT_2ADDR:
1131 convertArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1132 break;
1133 case Instruction::SUB_LONG:
1134 case Instruction::SUB_LONG_2ADDR:
1135 case Instruction::SUB_INT:
1136 case Instruction::SUB_INT_2ADDR:
1137 convertArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1138 break;
1139 case Instruction::MUL_LONG:
1140 case Instruction::MUL_LONG_2ADDR:
1141 case Instruction::MUL_INT:
1142 case Instruction::MUL_INT_2ADDR:
1143 convertArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1144 break;
1145 case Instruction::DIV_LONG:
1146 case Instruction::DIV_LONG_2ADDR:
1147 case Instruction::DIV_INT:
1148 case Instruction::DIV_INT_2ADDR:
1149 convertArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1150 break;
1151 case Instruction::REM_LONG:
1152 case Instruction::REM_LONG_2ADDR:
1153 case Instruction::REM_INT:
1154 case Instruction::REM_INT_2ADDR:
1155 convertArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1156 break;
1157 case Instruction::AND_LONG:
1158 case Instruction::AND_LONG_2ADDR:
1159 case Instruction::AND_INT:
1160 case Instruction::AND_INT_2ADDR:
1161 convertArithOp(cUnit, kOpAnd, rlDest, rlSrc[0], rlSrc[1]);
1162 break;
1163 case Instruction::OR_LONG:
1164 case Instruction::OR_LONG_2ADDR:
1165 case Instruction::OR_INT:
1166 case Instruction::OR_INT_2ADDR:
1167 convertArithOp(cUnit, kOpOr, rlDest, rlSrc[0], rlSrc[1]);
1168 break;
1169 case Instruction::XOR_LONG:
1170 case Instruction::XOR_LONG_2ADDR:
1171 case Instruction::XOR_INT:
1172 case Instruction::XOR_INT_2ADDR:
1173 convertArithOp(cUnit, kOpXor, rlDest, rlSrc[0], rlSrc[1]);
1174 break;
1175 case Instruction::SHL_LONG:
1176 case Instruction::SHL_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001177 convertShift(cUnit, greenland::IntrinsicHelper::SHLLong,
1178 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001179 break;
buzbee2cfc6392012-05-07 14:51:40 -07001180 case Instruction::SHL_INT:
1181 case Instruction::SHL_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001182 convertShift(cUnit, greenland::IntrinsicHelper::SHLInt,
1183 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001184 break;
1185 case Instruction::SHR_LONG:
1186 case Instruction::SHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001187 convertShift(cUnit, greenland::IntrinsicHelper::SHRLong,
1188 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001189 break;
buzbee2cfc6392012-05-07 14:51:40 -07001190 case Instruction::SHR_INT:
1191 case Instruction::SHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001192 convertShift(cUnit, greenland::IntrinsicHelper::SHRInt,
1193 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001194 break;
1195 case Instruction::USHR_LONG:
1196 case Instruction::USHR_LONG_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001197 convertShift(cUnit, greenland::IntrinsicHelper::USHRLong,
1198 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001199 break;
buzbee2cfc6392012-05-07 14:51:40 -07001200 case Instruction::USHR_INT:
1201 case Instruction::USHR_INT_2ADDR:
buzbee2a83e8f2012-07-13 16:42:30 -07001202 convertShift(cUnit, greenland::IntrinsicHelper::USHRInt,
1203 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001204 break;
1205
1206 case Instruction::ADD_INT_LIT16:
1207 case Instruction::ADD_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001208 convertArithOpLit(cUnit, kOpAdd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001209 break;
1210 case Instruction::RSUB_INT:
1211 case Instruction::RSUB_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001212 convertArithOpLit(cUnit, kOpRsub, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001213 break;
1214 case Instruction::MUL_INT_LIT16:
1215 case Instruction::MUL_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001216 convertArithOpLit(cUnit, kOpMul, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001217 break;
1218 case Instruction::DIV_INT_LIT16:
1219 case Instruction::DIV_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001220 convertArithOpLit(cUnit, kOpDiv, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001221 break;
1222 case Instruction::REM_INT_LIT16:
1223 case Instruction::REM_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001224 convertArithOpLit(cUnit, kOpRem, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001225 break;
1226 case Instruction::AND_INT_LIT16:
1227 case Instruction::AND_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001228 convertArithOpLit(cUnit, kOpAnd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001229 break;
1230 case Instruction::OR_INT_LIT16:
1231 case Instruction::OR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001232 convertArithOpLit(cUnit, kOpOr, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001233 break;
1234 case Instruction::XOR_INT_LIT16:
1235 case Instruction::XOR_INT_LIT8:
buzbee6969d502012-06-15 16:40:31 -07001236 convertArithOpLit(cUnit, kOpXor, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001237 break;
1238 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001239 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHLInt,
1240 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001241 break;
1242 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001243 convertShiftLit(cUnit, greenland::IntrinsicHelper::SHRInt,
1244 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001245 break;
1246 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001247 convertShiftLit(cUnit, greenland::IntrinsicHelper::USHRInt,
1248 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001249 break;
1250
1251 case Instruction::ADD_FLOAT:
1252 case Instruction::ADD_FLOAT_2ADDR:
1253 case Instruction::ADD_DOUBLE:
1254 case Instruction::ADD_DOUBLE_2ADDR:
1255 convertFPArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
1256 break;
1257
1258 case Instruction::SUB_FLOAT:
1259 case Instruction::SUB_FLOAT_2ADDR:
1260 case Instruction::SUB_DOUBLE:
1261 case Instruction::SUB_DOUBLE_2ADDR:
1262 convertFPArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
1263 break;
1264
1265 case Instruction::MUL_FLOAT:
1266 case Instruction::MUL_FLOAT_2ADDR:
1267 case Instruction::MUL_DOUBLE:
1268 case Instruction::MUL_DOUBLE_2ADDR:
1269 convertFPArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
1270 break;
1271
1272 case Instruction::DIV_FLOAT:
1273 case Instruction::DIV_FLOAT_2ADDR:
1274 case Instruction::DIV_DOUBLE:
1275 case Instruction::DIV_DOUBLE_2ADDR:
1276 convertFPArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
1277 break;
1278
1279 case Instruction::REM_FLOAT:
1280 case Instruction::REM_FLOAT_2ADDR:
1281 case Instruction::REM_DOUBLE:
1282 case Instruction::REM_DOUBLE_2ADDR:
1283 convertFPArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
1284 break;
1285
buzbee6969d502012-06-15 16:40:31 -07001286 case Instruction::INVOKE_STATIC:
buzbee101305f2012-06-28 18:00:56 -07001287 convertInvoke(cUnit, bb, mir, kStatic, false /*range*/,
1288 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001289 break;
1290 case Instruction::INVOKE_STATIC_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001291 convertInvoke(cUnit, bb, mir, kStatic, true /*range*/,
1292 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001293 break;
1294
1295 case Instruction::INVOKE_DIRECT:
buzbee101305f2012-06-28 18:00:56 -07001296 convertInvoke(cUnit, bb, mir, kDirect, false /*range*/,
1297 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001298 break;
1299 case Instruction::INVOKE_DIRECT_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001300 convertInvoke(cUnit, bb, mir, kDirect, true /*range*/,
1301 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001302 break;
1303
1304 case Instruction::INVOKE_VIRTUAL:
buzbee101305f2012-06-28 18:00:56 -07001305 convertInvoke(cUnit, bb, mir, kVirtual, false /*range*/,
1306 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001307 break;
1308 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001309 convertInvoke(cUnit, bb, mir, kVirtual, true /*range*/,
1310 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001311 break;
1312
1313 case Instruction::INVOKE_SUPER:
buzbee101305f2012-06-28 18:00:56 -07001314 convertInvoke(cUnit, bb, mir, kSuper, false /*range*/,
1315 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001316 break;
1317 case Instruction::INVOKE_SUPER_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001318 convertInvoke(cUnit, bb, mir, kSuper, true /*range*/,
1319 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001320 break;
1321
1322 case Instruction::INVOKE_INTERFACE:
buzbee101305f2012-06-28 18:00:56 -07001323 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1324 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001325 break;
1326 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee101305f2012-06-28 18:00:56 -07001327 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1328 false /* NewFilledArray */);
1329 break;
1330 case Instruction::FILLED_NEW_ARRAY:
1331 convertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
1332 true /* NewFilledArray */);
1333 break;
1334 case Instruction::FILLED_NEW_ARRAY_RANGE:
1335 convertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
1336 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001337 break;
1338
1339 case Instruction::CONST_STRING:
1340 case Instruction::CONST_STRING_JUMBO:
buzbee101305f2012-06-28 18:00:56 -07001341 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstString,
1342 rlDest);
1343 break;
1344
1345 case Instruction::CONST_CLASS:
1346 convertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstClass,
1347 rlDest);
1348 break;
1349
1350 case Instruction::CHECK_CAST:
1351 convertCheckCast(cUnit, vB, rlSrc[0]);
buzbee6969d502012-06-15 16:40:31 -07001352 break;
1353
buzbee4f1181f2012-06-22 13:52:12 -07001354 case Instruction::NEW_INSTANCE:
buzbee8fa0fda2012-06-27 15:44:52 -07001355 convertNewInstance(cUnit, vB, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001356 break;
1357
buzbee32412962012-06-26 16:27:56 -07001358 case Instruction::MOVE_EXCEPTION:
1359 convertMoveException(cUnit, rlDest);
1360 break;
1361
1362 case Instruction::THROW:
1363 convertThrow(cUnit, rlSrc[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001364 /*
1365 * If this throw is standalone, terminate.
1366 * If it might rethrow, force termination
1367 * of the following block.
1368 */
1369 if (bb->fallThrough == NULL) {
1370 cUnit->irb->CreateUnreachable();
1371 } else {
1372 bb->fallThrough->fallThrough = NULL;
1373 bb->fallThrough->taken = NULL;
1374 }
buzbee32412962012-06-26 16:27:56 -07001375 break;
1376
buzbee2cfc6392012-05-07 14:51:40 -07001377 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001378 case Instruction::MOVE_RESULT:
1379 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001380 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001381 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001382 */
jeffhao9a4f0032012-08-30 16:17:40 -07001383 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001384 break;
1385
1386 case Instruction::MONITOR_ENTER:
buzbee8fa0fda2012-06-27 15:44:52 -07001387 convertMonitorEnterExit(cUnit, optFlags,
1388 greenland::IntrinsicHelper::MonitorEnter,
1389 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001390 break;
1391
1392 case Instruction::MONITOR_EXIT:
buzbee8fa0fda2012-06-27 15:44:52 -07001393 convertMonitorEnterExit(cUnit, optFlags,
1394 greenland::IntrinsicHelper::MonitorExit,
1395 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001396 break;
1397
1398 case Instruction::ARRAY_LENGTH:
buzbee76592632012-06-29 15:18:35 -07001399 convertArrayLength(cUnit, optFlags, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001400 break;
1401
1402 case Instruction::NEW_ARRAY:
1403 convertNewArray(cUnit, vC, rlDest, rlSrc[0]);
1404 break;
1405
1406 case Instruction::INSTANCE_OF:
1407 convertInstanceOf(cUnit, vC, rlDest, rlSrc[0]);
1408 break;
1409
1410 case Instruction::AGET:
1411 if (rlDest.fp) {
1412 convertAget(cUnit, optFlags,
1413 greenland::IntrinsicHelper::HLArrayGetFloat,
1414 rlDest, rlSrc[0], rlSrc[1]);
1415 } else {
1416 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGet,
1417 rlDest, rlSrc[0], rlSrc[1]);
1418 }
1419 break;
1420 case Instruction::AGET_OBJECT:
1421 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetObject,
1422 rlDest, rlSrc[0], rlSrc[1]);
1423 break;
1424 case Instruction::AGET_BOOLEAN:
1425 convertAget(cUnit, optFlags,
1426 greenland::IntrinsicHelper::HLArrayGetBoolean,
1427 rlDest, rlSrc[0], rlSrc[1]);
1428 break;
1429 case Instruction::AGET_BYTE:
1430 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetByte,
1431 rlDest, rlSrc[0], rlSrc[1]);
1432 break;
1433 case Instruction::AGET_CHAR:
1434 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetChar,
1435 rlDest, rlSrc[0], rlSrc[1]);
1436 break;
1437 case Instruction::AGET_SHORT:
1438 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetShort,
1439 rlDest, rlSrc[0], rlSrc[1]);
1440 break;
1441 case Instruction::AGET_WIDE:
1442 if (rlDest.fp) {
1443 convertAget(cUnit, optFlags,
1444 greenland::IntrinsicHelper::HLArrayGetDouble,
1445 rlDest, rlSrc[0], rlSrc[1]);
1446 } else {
1447 convertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetWide,
1448 rlDest, rlSrc[0], rlSrc[1]);
1449 }
1450 break;
1451
1452 case Instruction::APUT:
1453 if (rlSrc[0].fp) {
1454 convertAput(cUnit, optFlags,
1455 greenland::IntrinsicHelper::HLArrayPutFloat,
1456 rlSrc[0], rlSrc[1], rlSrc[2]);
1457 } else {
1458 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPut,
1459 rlSrc[0], rlSrc[1], rlSrc[2]);
1460 }
1461 break;
1462 case Instruction::APUT_OBJECT:
1463 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutObject,
1464 rlSrc[0], rlSrc[1], rlSrc[2]);
1465 break;
1466 case Instruction::APUT_BOOLEAN:
1467 convertAput(cUnit, optFlags,
1468 greenland::IntrinsicHelper::HLArrayPutBoolean,
1469 rlSrc[0], rlSrc[1], rlSrc[2]);
1470 break;
1471 case Instruction::APUT_BYTE:
1472 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutByte,
1473 rlSrc[0], rlSrc[1], rlSrc[2]);
1474 break;
1475 case Instruction::APUT_CHAR:
1476 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutChar,
1477 rlSrc[0], rlSrc[1], rlSrc[2]);
1478 break;
1479 case Instruction::APUT_SHORT:
1480 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutShort,
1481 rlSrc[0], rlSrc[1], rlSrc[2]);
1482 break;
1483 case Instruction::APUT_WIDE:
1484 if (rlSrc[0].fp) {
1485 convertAput(cUnit, optFlags,
1486 greenland::IntrinsicHelper::HLArrayPutDouble,
1487 rlSrc[0], rlSrc[1], rlSrc[2]);
1488 } else {
1489 convertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutWide,
1490 rlSrc[0], rlSrc[1], rlSrc[2]);
1491 }
1492 break;
1493
buzbee101305f2012-06-28 18:00:56 -07001494 case Instruction::IGET:
1495 if (rlDest.fp) {
1496 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetFloat,
buzbee4f4dfc72012-07-02 14:54:44 -07001497 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001498 } else {
1499 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGet,
buzbee4f4dfc72012-07-02 14:54:44 -07001500 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001501 }
buzbee2cfc6392012-05-07 14:51:40 -07001502 break;
buzbee101305f2012-06-28 18:00:56 -07001503 case Instruction::IGET_OBJECT:
1504 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetObject,
buzbee4f4dfc72012-07-02 14:54:44 -07001505 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001506 break;
1507 case Instruction::IGET_BOOLEAN:
1508 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetBoolean,
buzbee4f4dfc72012-07-02 14:54:44 -07001509 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001510 break;
1511 case Instruction::IGET_BYTE:
1512 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetByte,
buzbee4f4dfc72012-07-02 14:54:44 -07001513 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001514 break;
1515 case Instruction::IGET_CHAR:
1516 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetChar,
buzbee4f4dfc72012-07-02 14:54:44 -07001517 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001518 break;
1519 case Instruction::IGET_SHORT:
1520 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetShort,
buzbee4f4dfc72012-07-02 14:54:44 -07001521 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001522 break;
1523 case Instruction::IGET_WIDE:
1524 if (rlDest.fp) {
1525 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetDouble,
buzbee4f4dfc72012-07-02 14:54:44 -07001526 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001527 } else {
1528 convertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetWide,
buzbee4f4dfc72012-07-02 14:54:44 -07001529 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001530 }
1531 break;
1532 case Instruction::IPUT:
buzbee85eee022012-07-16 22:12:38 -07001533 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001534 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutFloat,
1535 rlSrc[0], rlSrc[1], vC);
1536 } else {
1537 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPut,
1538 rlSrc[0], rlSrc[1], vC);
1539 }
1540 break;
1541 case Instruction::IPUT_OBJECT:
1542 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutObject,
1543 rlSrc[0], rlSrc[1], vC);
1544 break;
1545 case Instruction::IPUT_BOOLEAN:
1546 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutBoolean,
1547 rlSrc[0], rlSrc[1], vC);
1548 break;
1549 case Instruction::IPUT_BYTE:
1550 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutByte,
1551 rlSrc[0], rlSrc[1], vC);
1552 break;
1553 case Instruction::IPUT_CHAR:
1554 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutChar,
1555 rlSrc[0], rlSrc[1], vC);
1556 break;
1557 case Instruction::IPUT_SHORT:
1558 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutShort,
1559 rlSrc[0], rlSrc[1], vC);
1560 break;
1561 case Instruction::IPUT_WIDE:
buzbee85eee022012-07-16 22:12:38 -07001562 if (rlSrc[0].fp) {
buzbee101305f2012-06-28 18:00:56 -07001563 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutDouble,
1564 rlSrc[0], rlSrc[1], vC);
1565 } else {
1566 convertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutWide,
1567 rlSrc[0], rlSrc[1], vC);
1568 }
buzbee2cfc6392012-05-07 14:51:40 -07001569 break;
1570
1571 case Instruction::FILL_ARRAY_DATA:
buzbee101305f2012-06-28 18:00:56 -07001572 convertFillArrayData(cUnit, vB, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001573 break;
1574
buzbee76592632012-06-29 15:18:35 -07001575 case Instruction::LONG_TO_INT:
1576 convertLongToInt(cUnit, rlDest, rlSrc[0]);
1577 break;
1578
buzbee101305f2012-06-28 18:00:56 -07001579 case Instruction::INT_TO_LONG:
1580 convertIntToLong(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001581 break;
1582
buzbee101305f2012-06-28 18:00:56 -07001583 case Instruction::INT_TO_CHAR:
1584 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1585 greenland::IntrinsicHelper::IntToChar);
1586 break;
1587 case Instruction::INT_TO_BYTE:
1588 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1589 greenland::IntrinsicHelper::IntToByte);
1590 break;
1591 case Instruction::INT_TO_SHORT:
1592 convertIntNarrowing(cUnit, rlDest, rlSrc[0],
1593 greenland::IntrinsicHelper::IntToShort);
1594 break;
1595
buzbee76592632012-06-29 15:18:35 -07001596 case Instruction::INT_TO_FLOAT:
1597 case Instruction::LONG_TO_FLOAT:
1598 convertIntToFP(cUnit, cUnit->irb->getFloatTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001599 break;
1600
buzbee76592632012-06-29 15:18:35 -07001601 case Instruction::INT_TO_DOUBLE:
1602 case Instruction::LONG_TO_DOUBLE:
1603 convertIntToFP(cUnit, cUnit->irb->getDoubleTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001604 break;
1605
buzbee76592632012-06-29 15:18:35 -07001606 case Instruction::FLOAT_TO_DOUBLE:
1607 convertFloatToDouble(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001608 break;
1609
buzbee76592632012-06-29 15:18:35 -07001610 case Instruction::DOUBLE_TO_FLOAT:
1611 convertDoubleToFloat(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001612 break;
1613
1614 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001615 case Instruction::NEG_INT:
1616 convertNeg(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001617 break;
1618
1619 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001620 case Instruction::NEG_DOUBLE:
buzbee76592632012-06-29 15:18:35 -07001621 convertNegFP(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001622 break;
1623
buzbee76592632012-06-29 15:18:35 -07001624 case Instruction::NOT_LONG:
1625 case Instruction::NOT_INT:
1626 convertNot(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001627 break;
1628
buzbee2cfc6392012-05-07 14:51:40 -07001629 case Instruction::FLOAT_TO_INT:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001630 convertFPToInt(cUnit, greenland::IntrinsicHelper::F2I, rlDest, rlSrc[0]);
1631 break;
1632
buzbee2cfc6392012-05-07 14:51:40 -07001633 case Instruction::DOUBLE_TO_INT:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001634 convertFPToInt(cUnit, greenland::IntrinsicHelper::D2I, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001635 break;
1636
buzbee76592632012-06-29 15:18:35 -07001637 case Instruction::FLOAT_TO_LONG:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001638 convertFPToInt(cUnit, greenland::IntrinsicHelper::F2L, rlDest, rlSrc[0]);
1639 break;
1640
buzbee76592632012-06-29 15:18:35 -07001641 case Instruction::DOUBLE_TO_LONG:
TDYa1274ec8ccd2012-08-11 07:04:57 -07001642 convertFPToInt(cUnit, greenland::IntrinsicHelper::D2L, rlDest, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001643 break;
1644
1645 case Instruction::CMPL_FLOAT:
1646 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplFloat,
1647 rlDest, rlSrc[0], rlSrc[1]);
1648 break;
1649 case Instruction::CMPG_FLOAT:
1650 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgFloat,
1651 rlDest, rlSrc[0], rlSrc[1]);
1652 break;
1653 case Instruction::CMPL_DOUBLE:
1654 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmplDouble,
1655 rlDest, rlSrc[0], rlSrc[1]);
1656 break;
1657 case Instruction::CMPG_DOUBLE:
1658 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgDouble,
1659 rlDest, rlSrc[0], rlSrc[1]);
1660 break;
1661 case Instruction::CMP_LONG:
1662 convertWideComparison(cUnit, greenland::IntrinsicHelper::CmpLong,
1663 rlDest, rlSrc[0], rlSrc[1]);
1664 break;
1665
buzbee76592632012-06-29 15:18:35 -07001666 case Instruction::PACKED_SWITCH:
buzbeef58c12c2012-07-03 15:06:29 -07001667 convertPackedSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001668 break;
1669
1670 case Instruction::SPARSE_SWITCH:
buzbeea1da8a52012-07-09 14:00:21 -07001671 convertSparseSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001672 break;
buzbee2cfc6392012-05-07 14:51:40 -07001673
1674 default:
buzbee32412962012-06-26 16:27:56 -07001675 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001676 res = true;
1677 }
buzbeeb03f4872012-06-11 15:22:11 -07001678 if (objectDefinition) {
1679 setShadowFrameEntry(cUnit, (llvm::Value*)
1680 cUnit->llvmValues.elemList[rlDest.origSReg]);
1681 }
buzbee2cfc6392012-05-07 14:51:40 -07001682 return res;
1683}
1684
1685/* Extended MIR instructions like PHI */
1686void convertExtendedMIR(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
1687 llvm::BasicBlock* llvmBB)
1688{
1689
1690 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1691 case kMirOpPhi: {
buzbee2cfc6392012-05-07 14:51:40 -07001692 RegLocation rlDest = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee2a83e8f2012-07-13 16:42:30 -07001693 /*
1694 * The Art compiler's Phi nodes only handle 32-bit operands,
1695 * representing wide values using a matched set of Phi nodes
1696 * for the lower and upper halves. In the llvm world, we only
1697 * want a single Phi for wides. Here we will simply discard
1698 * the Phi node representing the high word.
1699 */
1700 if (rlDest.highWord) {
1701 return; // No Phi node - handled via low word
1702 }
1703 int* incoming = (int*)mir->dalvikInsn.vB;
buzbee2cfc6392012-05-07 14:51:40 -07001704 llvm::Type* phiType =
1705 llvmTypeFromLocRec(cUnit, rlDest);
1706 llvm::PHINode* phi = cUnit->irb->CreatePHI(phiType, mir->ssaRep->numUses);
1707 for (int i = 0; i < mir->ssaRep->numUses; i++) {
1708 RegLocation loc;
buzbee2a83e8f2012-07-13 16:42:30 -07001709 // Don't check width here.
1710 loc = oatGetRawSrc(cUnit, mir, i);
1711 DCHECK_EQ(rlDest.wide, loc.wide);
1712 DCHECK_EQ(rlDest.wide & rlDest.highWord, loc.wide & loc.highWord);
1713 DCHECK_EQ(rlDest.fp, loc.fp);
1714 DCHECK_EQ(rlDest.core, loc.core);
1715 DCHECK_EQ(rlDest.ref, loc.ref);
buzbeed1643e42012-09-05 14:06:51 -07001716 SafeMap<unsigned int, unsigned int>::iterator it;
1717 it = cUnit->blockIdMap.find(incoming[i]);
1718 DCHECK(it != cUnit->blockIdMap.end());
buzbee2cfc6392012-05-07 14:51:40 -07001719 phi->addIncoming(getLLVMValue(cUnit, loc.origSReg),
buzbeed1643e42012-09-05 14:06:51 -07001720 getLLVMBlock(cUnit, it->second));
buzbee2cfc6392012-05-07 14:51:40 -07001721 }
1722 defineValue(cUnit, phi, rlDest.origSReg);
1723 break;
1724 }
1725 case kMirOpCopy: {
1726 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1727 break;
1728 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001729 case kMirOpNop:
1730 if ((mir == bb->lastMIRInsn) && (bb->taken == NULL) &&
1731 (bb->fallThrough == NULL)) {
1732 cUnit->irb->CreateUnreachable();
1733 }
1734 break;
1735
buzbeeb046e162012-10-30 15:48:42 -07001736 // TODO: need GBC intrinsic to take advantage of fused operations
buzbee2cfc6392012-05-07 14:51:40 -07001737 case kMirOpFusedCmplFloat:
buzbeeb046e162012-10-30 15:48:42 -07001738 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpFloat unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001739 break;
1740 case kMirOpFusedCmpgFloat:
buzbeeb046e162012-10-30 15:48:42 -07001741 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmgFloat unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001742 break;
1743 case kMirOpFusedCmplDouble:
buzbeeb046e162012-10-30 15:48:42 -07001744 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmplDouble unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001745 break;
1746 case kMirOpFusedCmpgDouble:
buzbeeb046e162012-10-30 15:48:42 -07001747 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpgDouble unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001748 break;
1749 case kMirOpFusedCmpLong:
buzbeeb046e162012-10-30 15:48:42 -07001750 UNIMPLEMENTED(FATAL) << "kMirOpLongCmpBranch unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001751 break;
buzbee2cfc6392012-05-07 14:51:40 -07001752 default:
1753 break;
1754 }
1755}
1756
1757void setDexOffset(CompilationUnit* cUnit, int32_t offset)
1758{
1759 cUnit->currentDalvikOffset = offset;
buzbee76592632012-06-29 15:18:35 -07001760 llvm::SmallVector<llvm::Value*, 1> arrayRef;
buzbee2cfc6392012-05-07 14:51:40 -07001761 arrayRef.push_back(cUnit->irb->getInt32(offset));
1762 llvm::MDNode* node = llvm::MDNode::get(*cUnit->context, arrayRef);
1763 cUnit->irb->SetDexOffset(node);
1764}
1765
1766// Attach method info as metadata to special intrinsic
1767void setMethodInfo(CompilationUnit* cUnit)
1768{
1769 // We don't want dex offset on this
1770 cUnit->irb->SetDexOffset(NULL);
1771 greenland::IntrinsicHelper::IntrinsicId id;
1772 id = greenland::IntrinsicHelper::MethodInfo;
1773 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1774 llvm::Instruction* inst = cUnit->irb->CreateCall(intr);
1775 llvm::SmallVector<llvm::Value*, 2> regInfo;
1776 regInfo.push_back(cUnit->irb->getInt32(cUnit->numIns));
1777 regInfo.push_back(cUnit->irb->getInt32(cUnit->numRegs));
1778 regInfo.push_back(cUnit->irb->getInt32(cUnit->numOuts));
1779 regInfo.push_back(cUnit->irb->getInt32(cUnit->numCompilerTemps));
1780 regInfo.push_back(cUnit->irb->getInt32(cUnit->numSSARegs));
1781 llvm::MDNode* regInfoNode = llvm::MDNode::get(*cUnit->context, regInfo);
1782 inst->setMetadata("RegInfo", regInfoNode);
1783 int promoSize = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
1784 llvm::SmallVector<llvm::Value*, 50> pmap;
1785 for (int i = 0; i < promoSize; i++) {
1786 PromotionMap* p = &cUnit->promotionMap[i];
1787 int32_t mapData = ((p->firstInPair & 0xff) << 24) |
1788 ((p->fpReg & 0xff) << 16) |
1789 ((p->coreReg & 0xff) << 8) |
1790 ((p->fpLocation & 0xf) << 4) |
1791 (p->coreLocation & 0xf);
1792 pmap.push_back(cUnit->irb->getInt32(mapData));
1793 }
1794 llvm::MDNode* mapNode = llvm::MDNode::get(*cUnit->context, pmap);
1795 inst->setMetadata("PromotionMap", mapNode);
1796 setDexOffset(cUnit, cUnit->currentDalvikOffset);
1797}
1798
1799/* Handle the content in each basic block */
1800bool methodBlockBitcodeConversion(CompilationUnit* cUnit, BasicBlock* bb)
1801{
buzbeed1643e42012-09-05 14:06:51 -07001802 if (bb->blockType == kDead) return false;
buzbee2cfc6392012-05-07 14:51:40 -07001803 llvm::BasicBlock* llvmBB = getLLVMBlock(cUnit, bb->id);
buzbeef5f5a122012-09-21 13:57:36 -07001804 if (llvmBB == NULL) {
1805 CHECK(bb->blockType == kExitBlock);
1806 } else {
1807 cUnit->irb->SetInsertPoint(llvmBB);
1808 setDexOffset(cUnit, bb->startOffset);
1809 }
buzbee2cfc6392012-05-07 14:51:40 -07001810
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001811 if (cUnit->printMe) {
1812 LOG(INFO) << "................................";
1813 LOG(INFO) << "Block id " << bb->id;
1814 if (llvmBB != NULL) {
1815 LOG(INFO) << "label " << llvmBB->getName().str().c_str();
1816 } else {
1817 LOG(INFO) << "llvmBB is NULL";
1818 }
1819 }
1820
buzbee2cfc6392012-05-07 14:51:40 -07001821 if (bb->blockType == kEntryBlock) {
1822 setMethodInfo(cUnit);
buzbeeb03f4872012-06-11 15:22:11 -07001823 bool *canBeRef = (bool*) oatNew(cUnit, sizeof(bool) *
1824 cUnit->numDalvikRegisters, true,
1825 kAllocMisc);
1826 for (int i = 0; i < cUnit->numSSARegs; i++) {
buzbee6ec5e232012-09-20 15:50:03 -07001827 int vReg = SRegToVReg(cUnit, i);
1828 if (vReg > SSA_METHOD_BASEREG) {
1829 canBeRef[SRegToVReg(cUnit, i)] |= cUnit->regLocation[i].ref;
1830 }
buzbeeb03f4872012-06-11 15:22:11 -07001831 }
1832 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
1833 if (canBeRef[i]) {
1834 cUnit->numShadowFrameEntries++;
1835 }
1836 }
1837 if (cUnit->numShadowFrameEntries > 0) {
1838 cUnit->shadowMap = (int*) oatNew(cUnit, sizeof(int) *
1839 cUnit->numShadowFrameEntries, true,
1840 kAllocMisc);
1841 for (int i = 0, j = 0; i < cUnit->numDalvikRegisters; i++) {
1842 if (canBeRef[i]) {
1843 cUnit->shadowMap[j++] = i;
1844 }
1845 }
buzbeeb03f4872012-06-11 15:22:11 -07001846 }
Shih-wei Liao569daf12012-08-10 23:22:33 -07001847 greenland::IntrinsicHelper::IntrinsicId id =
1848 greenland::IntrinsicHelper::AllocaShadowFrame;
1849 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1850 llvm::Value* entries = cUnit->irb->getInt32(cUnit->numShadowFrameEntries);
TDYa1278e950c12012-11-02 09:58:19 -07001851 llvm::Value* dalvikRegs = cUnit->irb->getInt32(cUnit->numDalvikRegisters);
1852 llvm::Value* args[] = { entries, dalvikRegs };
1853 cUnit->irb->CreateCall(func, args);
buzbee2cfc6392012-05-07 14:51:40 -07001854 } else if (bb->blockType == kExitBlock) {
1855 /*
1856 * Because of the differences between how MIR/LIR and llvm handle exit
1857 * blocks, we won't explicitly covert them. On the llvm-to-lir
1858 * path, it will need to be regenereated.
1859 */
1860 return false;
buzbee6969d502012-06-15 16:40:31 -07001861 } else if (bb->blockType == kExceptionHandling) {
1862 /*
1863 * Because we're deferring null checking, delete the associated empty
1864 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001865 */
1866 llvmBB->eraseFromParent();
1867 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001868 }
1869
1870 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
1871
1872 setDexOffset(cUnit, mir->offset);
1873
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001874 int opcode = mir->dalvikInsn.opcode;
1875 Instruction::Format dalvikFormat =
1876 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001877
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001878 if (opcode == kMirOpCheck) {
1879 // Combine check and work halves of throwing instruction.
1880 MIR* workHalf = mir->meta.throwInsn;
1881 mir->dalvikInsn.opcode = workHalf->dalvikInsn.opcode;
1882 opcode = mir->dalvikInsn.opcode;
1883 SSARepresentation* ssaRep = workHalf->ssaRep;
1884 workHalf->ssaRep = mir->ssaRep;
1885 mir->ssaRep = ssaRep;
1886 workHalf->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1887 if (bb->successorBlockList.blockListType == kCatch) {
1888 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
1889 greenland::IntrinsicHelper::CatchTargets);
1890 llvm::Value* switchKey =
1891 cUnit->irb->CreateCall(intr, cUnit->irb->getInt32(mir->offset));
1892 GrowableListIterator iter;
1893 oatGrowableListIteratorInit(&bb->successorBlockList.blocks, &iter);
1894 // New basic block to use for work half
1895 llvm::BasicBlock* workBB =
1896 llvm::BasicBlock::Create(*cUnit->context, "", cUnit->func);
1897 llvm::SwitchInst* sw =
1898 cUnit->irb->CreateSwitch(switchKey, workBB,
1899 bb->successorBlockList.blocks.numUsed);
1900 while (true) {
1901 SuccessorBlockInfo *successorBlockInfo =
1902 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iter);
1903 if (successorBlockInfo == NULL) break;
1904 llvm::BasicBlock *target =
1905 getLLVMBlock(cUnit, successorBlockInfo->block->id);
1906 int typeIndex = successorBlockInfo->key;
1907 sw->addCase(cUnit->irb->getInt32(typeIndex), target);
1908 }
1909 llvmBB = workBB;
1910 cUnit->irb->SetInsertPoint(llvmBB);
1911 }
1912 }
1913
1914 if (opcode >= kMirOpFirst) {
buzbee2cfc6392012-05-07 14:51:40 -07001915 convertExtendedMIR(cUnit, bb, mir, llvmBB);
1916 continue;
1917 }
1918
1919 bool notHandled = convertMIRNode(cUnit, mir, bb, llvmBB,
1920 NULL /* labelList */);
1921 if (notHandled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001922 Instruction::Code dalvikOpcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001923 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001924 mir->offset, opcode,
buzbee2cfc6392012-05-07 14:51:40 -07001925 Instruction::Name(dalvikOpcode),
1926 dalvikFormat);
1927 }
1928 }
1929
buzbee4be777b2012-07-12 14:38:18 -07001930 if (bb->blockType == kEntryBlock) {
1931 cUnit->entryTargetBB = getLLVMBlock(cUnit, bb->fallThrough->id);
1932 } else if ((bb->fallThrough != NULL) && !bb->hasReturn) {
buzbee2cfc6392012-05-07 14:51:40 -07001933 cUnit->irb->CreateBr(getLLVMBlock(cUnit, bb->fallThrough->id));
1934 }
1935
1936 return false;
1937}
1938
buzbee4f4dfc72012-07-02 14:54:44 -07001939char remapShorty(char shortyType) {
1940 /*
1941 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1942 * and longs/doubles are represented as a pair of registers. When sub-word
1943 * arguments (and method results) are passed, they are extended to Dalvik
1944 * virtual register containers. Because llvm is picky about type consistency,
1945 * we must either cast the "real" type to 32-bit container multiple Dalvik
1946 * register types, or always use the expanded values.
1947 * Here, we're doing the latter. We map the shorty signature to container
1948 * types (which is valid so long as we always do a real expansion of passed
1949 * arguments and field loads).
1950 */
1951 switch(shortyType) {
1952 case 'Z' : shortyType = 'I'; break;
1953 case 'B' : shortyType = 'I'; break;
1954 case 'S' : shortyType = 'I'; break;
1955 case 'C' : shortyType = 'I'; break;
1956 default: break;
1957 }
1958 return shortyType;
1959}
1960
buzbee2cfc6392012-05-07 14:51:40 -07001961llvm::FunctionType* getFunctionType(CompilationUnit* cUnit) {
1962
1963 // Get return type
buzbee4f4dfc72012-07-02 14:54:44 -07001964 llvm::Type* ret_type = cUnit->irb->GetJType(remapShorty(cUnit->shorty[0]),
buzbee2cfc6392012-05-07 14:51:40 -07001965 greenland::kAccurate);
1966
1967 // Get argument type
1968 std::vector<llvm::Type*> args_type;
1969
1970 // method object
1971 args_type.push_back(cUnit->irb->GetJMethodTy());
1972
1973 // Do we have a "this"?
1974 if ((cUnit->access_flags & kAccStatic) == 0) {
1975 args_type.push_back(cUnit->irb->GetJObjectTy());
1976 }
1977
1978 for (uint32_t i = 1; i < strlen(cUnit->shorty); ++i) {
buzbee4f4dfc72012-07-02 14:54:44 -07001979 args_type.push_back(cUnit->irb->GetJType(remapShorty(cUnit->shorty[i]),
buzbee2cfc6392012-05-07 14:51:40 -07001980 greenland::kAccurate));
1981 }
1982
1983 return llvm::FunctionType::get(ret_type, args_type, false);
1984}
1985
1986bool createFunction(CompilationUnit* cUnit) {
1987 std::string func_name(PrettyMethod(cUnit->method_idx, *cUnit->dex_file,
1988 /* with_signature */ false));
1989 llvm::FunctionType* func_type = getFunctionType(cUnit);
1990
1991 if (func_type == NULL) {
1992 return false;
1993 }
1994
1995 cUnit->func = llvm::Function::Create(func_type,
1996 llvm::Function::ExternalLinkage,
1997 func_name, cUnit->module);
1998
1999 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
2000 llvm::Function::arg_iterator arg_end(cUnit->func->arg_end());
2001
2002 arg_iter->setName("method");
2003 ++arg_iter;
2004
2005 int startSReg = cUnit->numRegs;
2006
2007 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
2008 arg_iter->setName(StringPrintf("v%i_0", startSReg));
2009 startSReg += cUnit->regLocation[startSReg].wide ? 2 : 1;
2010 }
2011
2012 return true;
2013}
2014
2015bool createLLVMBasicBlock(CompilationUnit* cUnit, BasicBlock* bb)
2016{
2017 // Skip the exit block
buzbeed1643e42012-09-05 14:06:51 -07002018 if ((bb->blockType == kDead) ||(bb->blockType == kExitBlock)) {
buzbee2cfc6392012-05-07 14:51:40 -07002019 cUnit->idToBlockMap.Put(bb->id, NULL);
2020 } else {
2021 int offset = bb->startOffset;
2022 bool entryBlock = (bb->blockType == kEntryBlock);
2023 llvm::BasicBlock* llvmBB =
2024 llvm::BasicBlock::Create(*cUnit->context, entryBlock ? "entry" :
buzbee8320f382012-09-11 16:29:42 -07002025 StringPrintf(kLabelFormat, bb->catchEntry ? kCatchBlock :
2026 kNormalBlock, offset, bb->id), cUnit->func);
buzbee2cfc6392012-05-07 14:51:40 -07002027 if (entryBlock) {
2028 cUnit->entryBB = llvmBB;
2029 cUnit->placeholderBB =
2030 llvm::BasicBlock::Create(*cUnit->context, "placeholder",
2031 cUnit->func);
2032 }
2033 cUnit->idToBlockMap.Put(bb->id, llvmBB);
2034 }
2035 return false;
2036}
2037
2038
2039/*
2040 * Convert MIR to LLVM_IR
2041 * o For each ssa name, create LLVM named value. Type these
2042 * appropriately, and ignore high half of wide and double operands.
2043 * o For each MIR basic block, create an LLVM basic block.
2044 * o Iterate through the MIR a basic block at a time, setting arguments
2045 * to recovered ssa name.
2046 */
2047void oatMethodMIR2Bitcode(CompilationUnit* cUnit)
2048{
2049 initIR(cUnit);
2050 oatInitGrowableList(cUnit, &cUnit->llvmValues, cUnit->numSSARegs);
2051
2052 // Create the function
2053 createFunction(cUnit);
2054
2055 // Create an LLVM basic block for each MIR block in dfs preorder
2056 oatDataFlowAnalysisDispatcher(cUnit, createLLVMBasicBlock,
2057 kPreOrderDFSTraversal, false /* isIterative */);
2058 /*
2059 * Create an llvm named value for each MIR SSA name. Note: we'll use
2060 * placeholders for all non-argument values (because we haven't seen
2061 * the definition yet).
2062 */
2063 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2064 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
2065 arg_iter++; /* Skip path method */
2066 for (int i = 0; i < cUnit->numSSARegs; i++) {
2067 llvm::Value* val;
buzbee85eee022012-07-16 22:12:38 -07002068 RegLocation rlTemp = cUnit->regLocation[i];
2069 if ((SRegToVReg(cUnit, i) < 0) || rlTemp.highWord) {
buzbee2a83e8f2012-07-13 16:42:30 -07002070 oatInsertGrowableList(cUnit, &cUnit->llvmValues, 0);
2071 } else if ((i < cUnit->numRegs) ||
2072 (i >= (cUnit->numRegs + cUnit->numIns))) {
buzbee85eee022012-07-16 22:12:38 -07002073 llvm::Constant* immValue = cUnit->regLocation[i].wide ?
2074 cUnit->irb->GetJLong(0) : cUnit->irb->GetJInt(0);
buzbee2a83e8f2012-07-13 16:42:30 -07002075 val = emitConst(cUnit, immValue, cUnit->regLocation[i]);
2076 val->setName(llvmSSAName(cUnit, i));
buzbee2cfc6392012-05-07 14:51:40 -07002077 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)val);
buzbee2cfc6392012-05-07 14:51:40 -07002078 } else {
2079 // Recover previously-created argument values
2080 llvm::Value* argVal = arg_iter++;
2081 oatInsertGrowableList(cUnit, &cUnit->llvmValues, (intptr_t)argVal);
2082 }
2083 }
buzbee2cfc6392012-05-07 14:51:40 -07002084
2085 oatDataFlowAnalysisDispatcher(cUnit, methodBlockBitcodeConversion,
2086 kPreOrderDFSTraversal, false /* Iterative */);
2087
buzbee4be777b2012-07-12 14:38:18 -07002088 /*
2089 * In a few rare cases of verification failure, the verifier will
2090 * replace one or more Dalvik opcodes with the special
2091 * throw-verification-failure opcode. This can leave the SSA graph
2092 * in an invalid state, as definitions may be lost, while uses retained.
2093 * To work around this problem, we insert placeholder definitions for
2094 * all Dalvik SSA regs in the "placeholder" block. Here, after
2095 * bitcode conversion is complete, we examine those placeholder definitions
2096 * and delete any with no references (which normally is all of them).
2097 *
2098 * If any definitions remain, we link the placeholder block into the
2099 * CFG. Otherwise, it is deleted.
2100 */
2101 for (llvm::BasicBlock::iterator it = cUnit->placeholderBB->begin(),
2102 itEnd = cUnit->placeholderBB->end(); it != itEnd;) {
2103 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2104 DCHECK(inst != NULL);
2105 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2106 DCHECK(val != NULL);
2107 if (val->getNumUses() == 0) {
2108 inst->eraseFromParent();
2109 }
2110 }
2111 setDexOffset(cUnit, 0);
2112 if (cUnit->placeholderBB->empty()) {
2113 cUnit->placeholderBB->eraseFromParent();
2114 } else {
2115 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2116 cUnit->irb->CreateBr(cUnit->entryTargetBB);
2117 cUnit->entryTargetBB = cUnit->placeholderBB;
2118 }
2119 cUnit->irb->SetInsertPoint(cUnit->entryBB);
2120 cUnit->irb->CreateBr(cUnit->entryTargetBB);
buzbee2cfc6392012-05-07 14:51:40 -07002121
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002122 if (cUnit->enableDebug & (1 << kDebugVerifyBitcode)) {
2123 if (llvm::verifyFunction(*cUnit->func, llvm::PrintMessageAction)) {
2124 LOG(INFO) << "Bitcode verification FAILED for "
2125 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
2126 << " of size " << cUnit->insnsSize;
2127 cUnit->enableDebug |= (1 << kDebugDumpBitcodeFile);
2128 }
2129 }
buzbee2cfc6392012-05-07 14:51:40 -07002130
buzbeead8f15e2012-06-18 14:49:45 -07002131 if (cUnit->enableDebug & (1 << kDebugDumpBitcodeFile)) {
2132 // Write bitcode to file
2133 std::string errmsg;
2134 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
2135 oatReplaceSpecialChars(fname);
buzbee6459e7c2012-10-02 14:42:41 -07002136 // TODO: make configurable change naming mechanism to avoid fname length issues.
buzbee4f1181f2012-06-22 13:52:12 -07002137 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002138
buzbee6459e7c2012-10-02 14:42:41 -07002139 if (fname.size() > 240) {
2140 LOG(INFO) << "Warning: bitcode filename too long. Truncated.";
2141 fname.resize(240);
2142 }
2143
buzbeead8f15e2012-06-18 14:49:45 -07002144 llvm::OwningPtr<llvm::tool_output_file> out_file(
2145 new llvm::tool_output_file(fname.c_str(), errmsg,
2146 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002147
buzbeead8f15e2012-06-18 14:49:45 -07002148 if (!errmsg.empty()) {
2149 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2150 }
2151
2152 llvm::WriteBitcodeToFile(cUnit->module, out_file->os());
2153 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002154 }
buzbee2cfc6392012-05-07 14:51:40 -07002155}
2156
2157RegLocation getLoc(CompilationUnit* cUnit, llvm::Value* val) {
2158 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002159 DCHECK(val != NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002160 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2161 if (it == cUnit->locMap.end()) {
buzbee4f1181f2012-06-22 13:52:12 -07002162 std::string valName = val->getName().str();
buzbee32412962012-06-26 16:27:56 -07002163 if (valName.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002164 // FIXME: need to be more robust, handle FP and be in a position to
2165 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002166 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2167 memset(&res, 0, sizeof(res));
2168 res.location = kLocPhysReg;
2169 res.lowReg = oatAllocTemp(cUnit);
2170 res.home = true;
2171 res.sRegLow = INVALID_SREG;
2172 res.origSReg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002173 llvm::Type* ty = val->getType();
2174 res.wide = ((ty == cUnit->irb->getInt64Ty()) ||
2175 (ty == cUnit->irb->getDoubleTy()));
2176 if (res.wide) {
2177 res.highReg = oatAllocTemp(cUnit);
2178 }
buzbee4f1181f2012-06-22 13:52:12 -07002179 cUnit->locMap.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002180 } else {
2181 DCHECK_EQ(valName[0], 'v');
2182 int baseSReg = INVALID_SREG;
2183 sscanf(valName.c_str(), "v%d_", &baseSReg);
2184 res = cUnit->regLocation[baseSReg];
2185 cUnit->locMap.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002186 }
2187 } else {
2188 res = it->second;
2189 }
2190 return res;
2191}
2192
2193Instruction::Code getDalvikOpcode(OpKind op, bool isConst, bool isWide)
2194{
2195 Instruction::Code res = Instruction::NOP;
2196 if (isWide) {
2197 switch(op) {
2198 case kOpAdd: res = Instruction::ADD_LONG; break;
2199 case kOpSub: res = Instruction::SUB_LONG; break;
2200 case kOpMul: res = Instruction::MUL_LONG; break;
2201 case kOpDiv: res = Instruction::DIV_LONG; break;
2202 case kOpRem: res = Instruction::REM_LONG; break;
2203 case kOpAnd: res = Instruction::AND_LONG; break;
2204 case kOpOr: res = Instruction::OR_LONG; break;
2205 case kOpXor: res = Instruction::XOR_LONG; break;
2206 case kOpLsl: res = Instruction::SHL_LONG; break;
2207 case kOpLsr: res = Instruction::USHR_LONG; break;
2208 case kOpAsr: res = Instruction::SHR_LONG; break;
2209 default: LOG(FATAL) << "Unexpected OpKind " << op;
2210 }
2211 } else if (isConst){
2212 switch(op) {
2213 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2214 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2215 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2216 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2217 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2218 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2219 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2220 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2221 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2222 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2223 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2224 default: LOG(FATAL) << "Unexpected OpKind " << op;
2225 }
2226 } else {
2227 switch(op) {
2228 case kOpAdd: res = Instruction::ADD_INT; break;
2229 case kOpSub: res = Instruction::SUB_INT; break;
2230 case kOpMul: res = Instruction::MUL_INT; break;
2231 case kOpDiv: res = Instruction::DIV_INT; break;
2232 case kOpRem: res = Instruction::REM_INT; break;
2233 case kOpAnd: res = Instruction::AND_INT; break;
2234 case kOpOr: res = Instruction::OR_INT; break;
2235 case kOpXor: res = Instruction::XOR_INT; break;
2236 case kOpLsl: res = Instruction::SHL_INT; break;
2237 case kOpLsr: res = Instruction::USHR_INT; break;
2238 case kOpAsr: res = Instruction::SHR_INT; break;
2239 default: LOG(FATAL) << "Unexpected OpKind " << op;
2240 }
2241 }
2242 return res;
2243}
2244
buzbee4f1181f2012-06-22 13:52:12 -07002245Instruction::Code getDalvikFPOpcode(OpKind op, bool isConst, bool isWide)
2246{
2247 Instruction::Code res = Instruction::NOP;
2248 if (isWide) {
2249 switch(op) {
2250 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2251 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2252 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2253 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2254 case kOpRem: res = Instruction::REM_DOUBLE; break;
2255 default: LOG(FATAL) << "Unexpected OpKind " << op;
2256 }
2257 } else {
2258 switch(op) {
2259 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2260 case kOpSub: res = Instruction::SUB_FLOAT; break;
2261 case kOpMul: res = Instruction::MUL_FLOAT; break;
2262 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2263 case kOpRem: res = Instruction::REM_FLOAT; break;
2264 default: LOG(FATAL) << "Unexpected OpKind " << op;
2265 }
2266 }
2267 return res;
2268}
2269
2270void cvtBinFPOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2271{
2272 RegLocation rlDest = getLoc(cUnit, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002273 /*
2274 * Normally, we won't ever generate an FP operation with an immediate
2275 * operand (not supported in Dex instruction set). However, the IR builder
2276 * may insert them - in particular for createNegFP. Recognize this case
2277 * and deal with it.
2278 */
2279 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2280 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2281 DCHECK(op2C == NULL);
2282 if ((op1C != NULL) && (op == kOpSub)) {
2283 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(1));
2284 if (rlDest.wide) {
2285 genArithOpDouble(cUnit, Instruction::NEG_DOUBLE, rlDest, rlSrc, rlSrc);
2286 } else {
2287 genArithOpFloat(cUnit, Instruction::NEG_FLOAT, rlDest, rlSrc, rlSrc);
2288 }
buzbee4f1181f2012-06-22 13:52:12 -07002289 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002290 DCHECK(op1C == NULL);
2291 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2292 RegLocation rlSrc2 = getLoc(cUnit, inst->getOperand(1));
2293 Instruction::Code dalvikOp = getDalvikFPOpcode(op, false, rlDest.wide);
2294 if (rlDest.wide) {
2295 genArithOpDouble(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2296 } else {
2297 genArithOpFloat(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2298 }
buzbee4f1181f2012-06-22 13:52:12 -07002299 }
2300}
2301
buzbee101305f2012-06-28 18:00:56 -07002302void cvtIntNarrowing(CompilationUnit* cUnit, llvm::Instruction* inst,
2303 Instruction::Code opcode)
2304{
2305 RegLocation rlDest = getLoc(cUnit, inst);
2306 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2307 genIntNarrowing(cUnit, opcode, rlDest, rlSrc);
2308}
2309
buzbee76592632012-06-29 15:18:35 -07002310void cvtIntToFP(CompilationUnit* cUnit, llvm::Instruction* inst)
2311{
2312 RegLocation rlDest = getLoc(cUnit, inst);
2313 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2314 Instruction::Code opcode;
2315 if (rlDest.wide) {
2316 if (rlSrc.wide) {
2317 opcode = Instruction::LONG_TO_DOUBLE;
2318 } else {
2319 opcode = Instruction::INT_TO_DOUBLE;
2320 }
2321 } else {
2322 if (rlSrc.wide) {
2323 opcode = Instruction::LONG_TO_FLOAT;
2324 } else {
2325 opcode = Instruction::INT_TO_FLOAT;
2326 }
2327 }
2328 genConversion(cUnit, opcode, rlDest, rlSrc);
2329}
2330
TDYa1274ec8ccd2012-08-11 07:04:57 -07002331void cvtFPToInt(CompilationUnit* cUnit, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002332{
TDYa1274ec8ccd2012-08-11 07:04:57 -07002333 RegLocation rlDest = getLoc(cUnit, call_inst);
2334 RegLocation rlSrc = getLoc(cUnit, call_inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002335 Instruction::Code opcode;
2336 if (rlDest.wide) {
2337 if (rlSrc.wide) {
2338 opcode = Instruction::DOUBLE_TO_LONG;
2339 } else {
2340 opcode = Instruction::FLOAT_TO_LONG;
2341 }
2342 } else {
2343 if (rlSrc.wide) {
2344 opcode = Instruction::DOUBLE_TO_INT;
2345 } else {
2346 opcode = Instruction::FLOAT_TO_INT;
2347 }
2348 }
2349 genConversion(cUnit, opcode, rlDest, rlSrc);
2350}
2351
2352void cvtFloatToDouble(CompilationUnit* cUnit, llvm::Instruction* inst)
2353{
2354 RegLocation rlDest = getLoc(cUnit, inst);
2355 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2356 genConversion(cUnit, Instruction::FLOAT_TO_DOUBLE, rlDest, rlSrc);
2357}
2358
2359void cvtTrunc(CompilationUnit* cUnit, llvm::Instruction* inst)
2360{
2361 RegLocation rlDest = getLoc(cUnit, inst);
2362 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2363 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
2364 rlSrc = oatWideToNarrow(cUnit, rlSrc);
2365 storeValue(cUnit, rlDest, rlSrc);
2366}
2367
2368void cvtDoubleToFloat(CompilationUnit* cUnit, llvm::Instruction* inst)
2369{
2370 RegLocation rlDest = getLoc(cUnit, inst);
2371 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2372 genConversion(cUnit, Instruction::DOUBLE_TO_FLOAT, rlDest, rlSrc);
2373}
2374
2375
buzbee101305f2012-06-28 18:00:56 -07002376void cvtIntExt(CompilationUnit* cUnit, llvm::Instruction* inst, bool isSigned)
2377{
2378 // TODO: evaluate src/tgt types and add general support for more than int to long
2379 RegLocation rlDest = getLoc(cUnit, inst);
2380 RegLocation rlSrc = getLoc(cUnit, inst->getOperand(0));
2381 DCHECK(rlDest.wide);
2382 DCHECK(!rlSrc.wide);
2383 DCHECK(!rlDest.fp);
2384 DCHECK(!rlSrc.fp);
2385 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2386 if (rlSrc.location == kLocPhysReg) {
2387 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2388 } else {
2389 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2390 }
2391 if (isSigned) {
2392 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2393 } else {
2394 loadConstant(cUnit, rlResult.highReg, 0);
2395 }
2396 storeValueWide(cUnit, rlDest, rlResult);
2397}
2398
buzbee2cfc6392012-05-07 14:51:40 -07002399void cvtBinOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
2400{
2401 RegLocation rlDest = getLoc(cUnit, inst);
2402 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002403 // Special-case RSUB/NEG
buzbee4f1181f2012-06-22 13:52:12 -07002404 llvm::ConstantInt* lhsImm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2405 if ((op == kOpSub) && (lhsImm != NULL)) {
2406 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(1));
buzbeef58c12c2012-07-03 15:06:29 -07002407 if (rlSrc1.wide) {
2408 DCHECK_EQ(lhsImm->getSExtValue(), 0);
2409 genArithOpLong(cUnit, Instruction::NEG_LONG, rlDest, rlSrc1, rlSrc1);
2410 } else {
2411 genArithOpIntLit(cUnit, Instruction::RSUB_INT, rlDest, rlSrc1,
2412 lhsImm->getSExtValue());
2413 }
buzbee4f1181f2012-06-22 13:52:12 -07002414 return;
2415 }
2416 DCHECK(lhsImm == NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002417 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2418 llvm::Value* rhs = inst->getOperand(1);
buzbee9a2487f2012-07-26 14:01:13 -07002419 llvm::ConstantInt* constRhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2420 if (!rlDest.wide && (constRhs != NULL)) {
buzbee2cfc6392012-05-07 14:51:40 -07002421 Instruction::Code dalvikOp = getDalvikOpcode(op, true, false);
buzbee9a2487f2012-07-26 14:01:13 -07002422 genArithOpIntLit(cUnit, dalvikOp, rlDest, rlSrc1, constRhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002423 } else {
2424 Instruction::Code dalvikOp = getDalvikOpcode(op, false, rlDest.wide);
buzbee9a2487f2012-07-26 14:01:13 -07002425 RegLocation rlSrc2;
2426 if (constRhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002427 // ir_builder converts NOT_LONG to xor src, -1. Restore
2428 DCHECK_EQ(dalvikOp, Instruction::XOR_LONG);
2429 DCHECK_EQ(-1L, constRhs->getSExtValue());
2430 dalvikOp = Instruction::NOT_LONG;
buzbee9a2487f2012-07-26 14:01:13 -07002431 rlSrc2 = rlSrc1;
2432 } else {
2433 rlSrc2 = getLoc(cUnit, rhs);
2434 }
buzbee2cfc6392012-05-07 14:51:40 -07002435 if (rlDest.wide) {
2436 genArithOpLong(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2437 } else {
2438 genArithOpInt(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
2439 }
2440 }
2441}
2442
buzbee2a83e8f2012-07-13 16:42:30 -07002443void cvtShiftOp(CompilationUnit* cUnit, Instruction::Code opcode,
2444 llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002445{
buzbee2a83e8f2012-07-13 16:42:30 -07002446 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2447 RegLocation rlDest = getLoc(cUnit, callInst);
2448 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2449 llvm::Value* rhs = callInst->getArgOperand(1);
2450 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2451 DCHECK(!rlDest.wide);
2452 genArithOpIntLit(cUnit, opcode, rlDest, rlSrc, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002453 } else {
buzbee2a83e8f2012-07-13 16:42:30 -07002454 RegLocation rlShift = getLoc(cUnit, rhs);
2455 if (callInst->getType() == cUnit->irb->getInt64Ty()) {
2456 genShiftOpLong(cUnit, opcode, rlDest, rlSrc, rlShift);
2457 } else {
2458 genArithOpInt(cUnit, opcode, rlDest, rlSrc, rlShift);
2459 }
buzbee101305f2012-06-28 18:00:56 -07002460 }
2461}
2462
buzbee2cfc6392012-05-07 14:51:40 -07002463void cvtBr(CompilationUnit* cUnit, llvm::Instruction* inst)
2464{
2465 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(inst);
2466 DCHECK(brInst != NULL);
2467 DCHECK(brInst->isUnconditional()); // May change - but this is all we use now
2468 llvm::BasicBlock* targetBB = brInst->getSuccessor(0);
2469 opUnconditionalBranch(cUnit, cUnit->blockToLabelMap.Get(targetBB));
2470}
2471
2472void cvtPhi(CompilationUnit* cUnit, llvm::Instruction* inst)
2473{
2474 // Nop - these have already been processed
2475}
2476
2477void cvtRet(CompilationUnit* cUnit, llvm::Instruction* inst)
2478{
2479 llvm::ReturnInst* retInst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2480 llvm::Value* retVal = retInst->getReturnValue();
2481 if (retVal != NULL) {
2482 RegLocation rlSrc = getLoc(cUnit, retVal);
2483 if (rlSrc.wide) {
2484 storeValueWide(cUnit, oatGetReturnWide(cUnit, rlSrc.fp), rlSrc);
2485 } else {
2486 storeValue(cUnit, oatGetReturn(cUnit, rlSrc.fp), rlSrc);
2487 }
2488 }
2489 genExitSequence(cUnit);
2490}
2491
2492ConditionCode getCond(llvm::ICmpInst::Predicate llvmCond)
2493{
2494 ConditionCode res = kCondAl;
2495 switch(llvmCond) {
buzbee6969d502012-06-15 16:40:31 -07002496 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002497 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2498 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2499 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002500 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002501 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002502 default: LOG(FATAL) << "Unexpected llvm condition";
2503 }
2504 return res;
2505}
2506
2507void cvtICmp(CompilationUnit* cUnit, llvm::Instruction* inst)
2508{
2509 // genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2)
2510 UNIMPLEMENTED(FATAL);
2511}
2512
2513void cvtICmpBr(CompilationUnit* cUnit, llvm::Instruction* inst,
2514 llvm::BranchInst* brInst)
2515{
2516 // Get targets
2517 llvm::BasicBlock* takenBB = brInst->getSuccessor(0);
2518 LIR* taken = cUnit->blockToLabelMap.Get(takenBB);
2519 llvm::BasicBlock* fallThroughBB = brInst->getSuccessor(1);
2520 LIR* fallThrough = cUnit->blockToLabelMap.Get(fallThroughBB);
2521 // Get comparison operands
2522 llvm::ICmpInst* iCmpInst = llvm::dyn_cast<llvm::ICmpInst>(inst);
2523 ConditionCode cond = getCond(iCmpInst->getPredicate());
2524 llvm::Value* lhs = iCmpInst->getOperand(0);
2525 // Not expecting a constant as 1st operand
2526 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
2527 RegLocation rlSrc1 = getLoc(cUnit, inst->getOperand(0));
2528 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2529 llvm::Value* rhs = inst->getOperand(1);
buzbeeb046e162012-10-30 15:48:42 -07002530 if (cUnit->instructionSet == kMips) {
2531 // Compare and branch in one shot
2532 UNIMPLEMENTED(FATAL);
2533 }
buzbee2cfc6392012-05-07 14:51:40 -07002534 //Compare, then branch
2535 // TODO: handle fused CMP_LONG/IF_xxZ case
2536 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2537 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002538 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
2539 opRegImm(cUnit, kOpCmp, rlSrc1.lowReg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002540 } else {
2541 RegLocation rlSrc2 = getLoc(cUnit, rhs);
2542 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2543 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
2544 }
2545 opCondBranch(cUnit, cond, taken);
buzbee2cfc6392012-05-07 14:51:40 -07002546 // Fallthrough
2547 opUnconditionalBranch(cUnit, fallThrough);
2548}
2549
2550void cvtCall(CompilationUnit* cUnit, llvm::CallInst* callInst,
2551 llvm::Function* callee)
2552{
2553 UNIMPLEMENTED(FATAL);
2554}
2555
buzbee2cfc6392012-05-07 14:51:40 -07002556void cvtCopy(CompilationUnit* cUnit, llvm::CallInst* callInst)
2557{
buzbee4f1181f2012-06-22 13:52:12 -07002558 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002559 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(0));
2560 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee76592632012-06-29 15:18:35 -07002561 DCHECK_EQ(rlSrc.wide, rlDest.wide);
2562 DCHECK_EQ(rlSrc.fp, rlDest.fp);
buzbee2cfc6392012-05-07 14:51:40 -07002563 if (rlSrc.wide) {
2564 storeValueWide(cUnit, rlDest, rlSrc);
2565 } else {
2566 storeValue(cUnit, rlDest, rlSrc);
2567 }
2568}
2569
2570// Note: Immediate arg is a ConstantInt regardless of result type
2571void cvtConst(CompilationUnit* cUnit, llvm::CallInst* callInst)
2572{
buzbee4f1181f2012-06-22 13:52:12 -07002573 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002574 llvm::ConstantInt* src =
2575 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2576 uint64_t immval = src->getZExtValue();
2577 RegLocation rlDest = getLoc(cUnit, callInst);
2578 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
2579 if (rlDest.wide) {
2580 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
2581 (immval) & 0xffffffff, (immval >> 32) & 0xffffffff);
2582 storeValueWide(cUnit, rlDest, rlResult);
2583 } else {
2584 loadConstantNoClobber(cUnit, rlResult.lowReg, immval & 0xffffffff);
2585 storeValue(cUnit, rlDest, rlResult);
2586 }
2587}
2588
buzbee101305f2012-06-28 18:00:56 -07002589void cvtConstObject(CompilationUnit* cUnit, llvm::CallInst* callInst,
2590 bool isString)
buzbee6969d502012-06-15 16:40:31 -07002591{
buzbee4f1181f2012-06-22 13:52:12 -07002592 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee101305f2012-06-28 18:00:56 -07002593 llvm::ConstantInt* idxVal =
buzbee6969d502012-06-15 16:40:31 -07002594 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee101305f2012-06-28 18:00:56 -07002595 uint32_t index = idxVal->getZExtValue();
buzbee6969d502012-06-15 16:40:31 -07002596 RegLocation rlDest = getLoc(cUnit, callInst);
buzbee101305f2012-06-28 18:00:56 -07002597 if (isString) {
2598 genConstString(cUnit, index, rlDest);
2599 } else {
2600 genConstClass(cUnit, index, rlDest);
2601 }
2602}
2603
2604void cvtFillArrayData(CompilationUnit* cUnit, llvm::CallInst* callInst)
2605{
2606 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2607 llvm::ConstantInt* offsetVal =
2608 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2609 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2610 genFillArrayData(cUnit, offsetVal->getSExtValue(), rlSrc);
buzbee6969d502012-06-15 16:40:31 -07002611}
2612
buzbee4f1181f2012-06-22 13:52:12 -07002613void cvtNewInstance(CompilationUnit* cUnit, llvm::CallInst* callInst)
2614{
buzbee32412962012-06-26 16:27:56 -07002615 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002616 llvm::ConstantInt* typeIdxVal =
2617 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2618 uint32_t typeIdx = typeIdxVal->getZExtValue();
2619 RegLocation rlDest = getLoc(cUnit, callInst);
2620 genNewInstance(cUnit, typeIdx, rlDest);
2621}
2622
buzbee8fa0fda2012-06-27 15:44:52 -07002623void cvtNewArray(CompilationUnit* cUnit, llvm::CallInst* callInst)
2624{
2625 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2626 llvm::ConstantInt* typeIdxVal =
2627 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2628 uint32_t typeIdx = typeIdxVal->getZExtValue();
2629 llvm::Value* len = callInst->getArgOperand(1);
2630 RegLocation rlLen = getLoc(cUnit, len);
2631 RegLocation rlDest = getLoc(cUnit, callInst);
2632 genNewArray(cUnit, typeIdx, rlDest, rlLen);
2633}
2634
2635void cvtInstanceOf(CompilationUnit* cUnit, llvm::CallInst* callInst)
2636{
2637 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2638 llvm::ConstantInt* typeIdxVal =
2639 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2640 uint32_t typeIdx = typeIdxVal->getZExtValue();
2641 llvm::Value* src = callInst->getArgOperand(1);
2642 RegLocation rlSrc = getLoc(cUnit, src);
2643 RegLocation rlDest = getLoc(cUnit, callInst);
2644 genInstanceof(cUnit, typeIdx, rlDest, rlSrc);
2645}
2646
buzbee32412962012-06-26 16:27:56 -07002647void cvtThrow(CompilationUnit* cUnit, llvm::CallInst* callInst)
2648{
2649 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
2650 llvm::Value* src = callInst->getArgOperand(0);
2651 RegLocation rlSrc = getLoc(cUnit, src);
2652 genThrow(cUnit, rlSrc);
2653}
2654
buzbee8fa0fda2012-06-27 15:44:52 -07002655void cvtMonitorEnterExit(CompilationUnit* cUnit, bool isEnter,
2656 llvm::CallInst* callInst)
2657{
2658 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2659 llvm::ConstantInt* optFlags =
2660 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2661 llvm::Value* src = callInst->getArgOperand(1);
2662 RegLocation rlSrc = getLoc(cUnit, src);
2663 if (isEnter) {
2664 genMonitorEnter(cUnit, optFlags->getZExtValue(), rlSrc);
2665 } else {
2666 genMonitorExit(cUnit, optFlags->getZExtValue(), rlSrc);
2667 }
2668}
2669
buzbee76592632012-06-29 15:18:35 -07002670void cvtArrayLength(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002671{
2672 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2673 llvm::ConstantInt* optFlags =
2674 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2675 llvm::Value* src = callInst->getArgOperand(1);
2676 RegLocation rlSrc = getLoc(cUnit, src);
2677 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2678 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg, optFlags->getZExtValue());
2679 RegLocation rlDest = getLoc(cUnit, callInst);
2680 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2681 int lenOffset = Array::LengthOffset().Int32Value();
2682 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset, rlResult.lowReg);
2683 storeValue(cUnit, rlDest, rlResult);
2684}
2685
buzbee32412962012-06-26 16:27:56 -07002686void cvtMoveException(CompilationUnit* cUnit, llvm::CallInst* callInst)
2687{
buzbee32412962012-06-26 16:27:56 -07002688 RegLocation rlDest = getLoc(cUnit, callInst);
Ian Rogers474b6da2012-09-25 00:20:38 -07002689 genMoveException(cUnit, rlDest);
buzbee32412962012-06-26 16:27:56 -07002690}
2691
buzbee4f1181f2012-06-22 13:52:12 -07002692void cvtSget(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2693 bool isObject)
2694{
buzbee32412962012-06-26 16:27:56 -07002695 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002696 llvm::ConstantInt* typeIdxVal =
2697 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2698 uint32_t typeIdx = typeIdxVal->getZExtValue();
2699 RegLocation rlDest = getLoc(cUnit, callInst);
2700 genSget(cUnit, typeIdx, rlDest, isWide, isObject);
2701}
2702
buzbee8fa0fda2012-06-27 15:44:52 -07002703void cvtSput(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide,
2704 bool isObject)
2705{
2706 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2707 llvm::ConstantInt* typeIdxVal =
2708 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2709 uint32_t typeIdx = typeIdxVal->getZExtValue();
2710 llvm::Value* src = callInst->getArgOperand(1);
2711 RegLocation rlSrc = getLoc(cUnit, src);
2712 genSput(cUnit, typeIdx, rlSrc, isWide, isObject);
2713}
2714
2715void cvtAget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2716 int scale)
2717{
2718 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2719 llvm::ConstantInt* optFlags =
2720 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2721 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(1));
2722 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(2));
2723 RegLocation rlDest = getLoc(cUnit, callInst);
2724 genArrayGet(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2725 rlDest, scale);
2726}
2727
2728void cvtAput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
buzbeef1f86362012-07-10 15:18:31 -07002729 int scale, bool isObject)
buzbee8fa0fda2012-06-27 15:44:52 -07002730{
2731 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2732 llvm::ConstantInt* optFlags =
2733 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2734 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2735 RegLocation rlArray = getLoc(cUnit, callInst->getArgOperand(2));
2736 RegLocation rlIndex = getLoc(cUnit, callInst->getArgOperand(3));
buzbeef1f86362012-07-10 15:18:31 -07002737 if (isObject) {
2738 genArrayObjPut(cUnit, optFlags->getZExtValue(), rlArray, rlIndex,
2739 rlSrc, scale);
2740 } else {
2741 genArrayPut(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
2742 rlSrc, scale);
2743 }
2744}
2745
2746void cvtAputObj(CompilationUnit* cUnit, llvm::CallInst* callInst)
2747{
2748 cvtAput(cUnit, callInst, kWord, 2, true /* isObject */);
2749}
2750
2751void cvtAputPrimitive(CompilationUnit* cUnit, llvm::CallInst* callInst,
2752 OpSize size, int scale)
2753{
2754 cvtAput(cUnit, callInst, size, scale, false /* isObject */);
buzbee8fa0fda2012-06-27 15:44:52 -07002755}
2756
buzbee101305f2012-06-28 18:00:56 -07002757void cvtIget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2758 bool isWide, bool isObj)
2759{
2760 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2761 llvm::ConstantInt* optFlags =
2762 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2763 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(1));
2764 llvm::ConstantInt* fieldIdx =
2765 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2766 RegLocation rlDest = getLoc(cUnit, callInst);
2767 genIGet(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2768 size, rlDest, rlObj, isWide, isObj);
2769}
2770
2771void cvtIput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2772 bool isWide, bool isObj)
2773{
2774 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2775 llvm::ConstantInt* optFlags =
2776 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2777 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2778 RegLocation rlObj = getLoc(cUnit, callInst->getArgOperand(2));
2779 llvm::ConstantInt* fieldIdx =
buzbee4f4dfc72012-07-02 14:54:44 -07002780 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(3));
buzbee101305f2012-06-28 18:00:56 -07002781 genIPut(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
2782 size, rlSrc, rlObj, isWide, isObj);
2783}
2784
2785void cvtCheckCast(CompilationUnit* cUnit, llvm::CallInst* callInst)
2786{
2787 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2788 llvm::ConstantInt* typeIdx =
2789 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2790 RegLocation rlSrc = getLoc(cUnit, callInst->getArgOperand(1));
2791 genCheckCast(cUnit, typeIdx->getZExtValue(), rlSrc);
2792}
2793
buzbee76592632012-06-29 15:18:35 -07002794void cvtFPCompare(CompilationUnit* cUnit, llvm::CallInst* callInst,
2795 Instruction::Code opcode)
2796{
2797 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2798 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2799 RegLocation rlDest = getLoc(cUnit, callInst);
2800 genCmpFP(cUnit, opcode, rlDest, rlSrc1, rlSrc2);
2801}
2802
2803void cvtLongCompare(CompilationUnit* cUnit, llvm::CallInst* callInst)
2804{
2805 RegLocation rlSrc1 = getLoc(cUnit, callInst->getArgOperand(0));
2806 RegLocation rlSrc2 = getLoc(cUnit, callInst->getArgOperand(1));
2807 RegLocation rlDest = getLoc(cUnit, callInst);
2808 genCmpLong(cUnit, rlDest, rlSrc1, rlSrc2);
2809}
2810
buzbeef58c12c2012-07-03 15:06:29 -07002811void cvtSwitch(CompilationUnit* cUnit, llvm::Instruction* inst)
2812{
2813 llvm::SwitchInst* swInst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2814 DCHECK(swInst != NULL);
2815 llvm::Value* testVal = swInst->getCondition();
2816 llvm::MDNode* tableOffsetNode = swInst->getMetadata("SwitchTable");
2817 DCHECK(tableOffsetNode != NULL);
2818 llvm::ConstantInt* tableOffsetValue =
2819 static_cast<llvm::ConstantInt*>(tableOffsetNode->getOperand(0));
2820 int32_t tableOffset = tableOffsetValue->getSExtValue();
2821 RegLocation rlSrc = getLoc(cUnit, testVal);
buzbeea1da8a52012-07-09 14:00:21 -07002822 const u2* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
2823 u2 tableMagic = *table;
2824 if (tableMagic == 0x100) {
2825 genPackedSwitch(cUnit, tableOffset, rlSrc);
2826 } else {
2827 DCHECK_EQ(tableMagic, 0x200);
2828 genSparseSwitch(cUnit, tableOffset, rlSrc);
2829 }
buzbeef58c12c2012-07-03 15:06:29 -07002830}
2831
buzbee6969d502012-06-15 16:40:31 -07002832void cvtInvoke(CompilationUnit* cUnit, llvm::CallInst* callInst,
buzbee76592632012-06-29 15:18:35 -07002833 bool isVoid, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -07002834{
2835 CallInfo* info = (CallInfo*)oatNew(cUnit, sizeof(CallInfo), true,
2836 kAllocMisc);
buzbee8fa0fda2012-06-27 15:44:52 -07002837 if (isVoid) {
buzbee6969d502012-06-15 16:40:31 -07002838 info->result.location = kLocInvalid;
2839 } else {
2840 info->result = getLoc(cUnit, callInst);
2841 }
2842 llvm::ConstantInt* invokeTypeVal =
2843 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2844 llvm::ConstantInt* methodIndexVal =
2845 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(1));
2846 llvm::ConstantInt* optFlagsVal =
2847 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2848 info->type = static_cast<InvokeType>(invokeTypeVal->getZExtValue());
2849 info->index = methodIndexVal->getZExtValue();
2850 info->optFlags = optFlagsVal->getZExtValue();
2851 info->offset = cUnit->currentDalvikOffset;
2852
buzbee6969d502012-06-15 16:40:31 -07002853 // Count the argument words, and then build argument array.
2854 info->numArgWords = 0;
2855 for (unsigned int i = 3; i < callInst->getNumArgOperands(); i++) {
2856 RegLocation tLoc = getLoc(cUnit, callInst->getArgOperand(i));
2857 info->numArgWords += tLoc.wide ? 2 : 1;
2858 }
2859 info->args = (info->numArgWords == 0) ? NULL : (RegLocation*)
2860 oatNew(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc);
2861 // Now, fill in the location records, synthesizing high loc of wide vals
2862 for (int i = 3, next = 0; next < info->numArgWords;) {
buzbee4f1181f2012-06-22 13:52:12 -07002863 info->args[next] = getLoc(cUnit, callInst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002864 if (info->args[next].wide) {
2865 next++;
2866 // TODO: Might make sense to mark this as an invalid loc
2867 info->args[next].origSReg = info->args[next-1].origSReg+1;
2868 info->args[next].sRegLow = info->args[next-1].sRegLow+1;
2869 }
2870 next++;
2871 }
buzbee4f4dfc72012-07-02 14:54:44 -07002872 // TODO - rework such that we no longer need isRange
2873 info->isRange = (info->numArgWords > 5);
2874
buzbee76592632012-06-29 15:18:35 -07002875 if (isFilledNewArray) {
buzbee101305f2012-06-28 18:00:56 -07002876 genFilledNewArray(cUnit, info);
2877 } else {
2878 genInvoke(cUnit, info);
2879 }
buzbee6969d502012-06-15 16:40:31 -07002880}
2881
buzbeead8f15e2012-06-18 14:49:45 -07002882/* Look up the RegLocation associated with a Value. Must already be defined */
2883RegLocation valToLoc(CompilationUnit* cUnit, llvm::Value* val)
2884{
2885 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2886 DCHECK(it != cUnit->locMap.end()) << "Missing definition";
2887 return it->second;
2888}
2889
buzbee2cfc6392012-05-07 14:51:40 -07002890bool methodBitcodeBlockCodeGen(CompilationUnit* cUnit, llvm::BasicBlock* bb)
2891{
buzbee0967a252012-09-14 10:43:54 -07002892 while (cUnit->llvmBlocks.find(bb) == cUnit->llvmBlocks.end()) {
2893 llvm::BasicBlock* nextBB = NULL;
2894 cUnit->llvmBlocks.insert(bb);
2895 bool isEntry = (bb == &cUnit->func->getEntryBlock());
2896 // Define the starting label
2897 LIR* blockLabel = cUnit->blockToLabelMap.Get(bb);
2898 // Extract the type and starting offset from the block's name
buzbee951c0a12012-10-03 16:31:39 -07002899 char blockType = kInvalidBlock;
2900 if (isEntry) {
2901 blockType = kNormalBlock;
2902 blockLabel->operands[0] = 0;
2903 } else if (!bb->hasName()) {
2904 blockType = kNormalBlock;
2905 blockLabel->operands[0] = DexFile::kDexNoIndex;
buzbee0967a252012-09-14 10:43:54 -07002906 } else {
buzbee951c0a12012-10-03 16:31:39 -07002907 std::string blockName = bb->getName().str();
2908 int dummy;
2909 sscanf(blockName.c_str(), kLabelFormat, &blockType, &blockLabel->operands[0], &dummy);
2910 cUnit->currentDalvikOffset = blockLabel->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002911 }
buzbee951c0a12012-10-03 16:31:39 -07002912 DCHECK((blockType == kNormalBlock) || (blockType == kCatchBlock));
2913 cUnit->currentDalvikOffset = blockLabel->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002914 // Set the label kind
2915 blockLabel->opcode = kPseudoNormalBlockLabel;
2916 // Insert the label
2917 oatAppendLIR(cUnit, blockLabel);
buzbee2cfc6392012-05-07 14:51:40 -07002918
buzbee0967a252012-09-14 10:43:54 -07002919 LIR* headLIR = NULL;
buzbee8320f382012-09-11 16:29:42 -07002920
buzbee0967a252012-09-14 10:43:54 -07002921 if (blockType == kCatchBlock) {
Bill Buzbeea5b30242012-09-28 07:19:44 -07002922 headLIR = newLIR0(cUnit, kPseudoExportedPC);
buzbee0967a252012-09-14 10:43:54 -07002923 }
buzbee8320f382012-09-11 16:29:42 -07002924
buzbee0967a252012-09-14 10:43:54 -07002925 // Free temp registers and reset redundant store tracking */
2926 oatResetRegPool(cUnit);
2927 oatResetDefTracking(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002928
buzbee0967a252012-09-14 10:43:54 -07002929 //TODO: restore oat incoming liveness optimization
2930 oatClobberAllRegs(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002931
buzbee0967a252012-09-14 10:43:54 -07002932 if (isEntry) {
2933 RegLocation* argLocs = (RegLocation*)
2934 oatNew(cUnit, sizeof(RegLocation) * cUnit->numIns, true, kAllocMisc);
2935 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
2936 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
2937 // Skip past Method*
2938 it++;
2939 for (unsigned i = 0; it != it_end; ++it) {
2940 llvm::Value* val = it;
2941 argLocs[i++] = valToLoc(cUnit, val);
2942 llvm::Type* ty = val->getType();
2943 if ((ty == cUnit->irb->getInt64Ty()) || (ty == cUnit->irb->getDoubleTy())) {
2944 argLocs[i] = argLocs[i-1];
2945 argLocs[i].lowReg = argLocs[i].highReg;
2946 argLocs[i].origSReg++;
2947 argLocs[i].sRegLow = INVALID_SREG;
2948 argLocs[i].highWord = true;
2949 i++;
2950 }
2951 }
2952 genEntrySequence(cUnit, argLocs, cUnit->methodLoc);
2953 }
2954
2955 // Visit all of the instructions in the block
2956 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2957 llvm::Instruction* inst = it;
2958 llvm::BasicBlock::iterator nextIt = ++it;
2959 // Extract the Dalvik offset from the instruction
2960 uint32_t opcode = inst->getOpcode();
2961 llvm::MDNode* dexOffsetNode = inst->getMetadata("DexOff");
2962 if (dexOffsetNode != NULL) {
2963 llvm::ConstantInt* dexOffsetValue =
2964 static_cast<llvm::ConstantInt*>(dexOffsetNode->getOperand(0));
2965 cUnit->currentDalvikOffset = dexOffsetValue->getZExtValue();
2966 }
2967
2968 oatResetRegPool(cUnit);
2969 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
2970 oatClobberAllRegs(cUnit);
2971 }
2972
2973 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
2974 oatResetDefTracking(cUnit);
2975 }
2976
2977 #ifndef NDEBUG
2978 /* Reset temp tracking sanity check */
2979 cUnit->liveSReg = INVALID_SREG;
2980 #endif
2981
2982 // TODO: use llvm opcode name here instead of "boundary" if verbose
2983 LIR* boundaryLIR = markBoundary(cUnit, cUnit->currentDalvikOffset, "boundary");
2984
2985 /* Remember the first LIR for thisl block*/
2986 if (headLIR == NULL) {
2987 headLIR = boundaryLIR;
2988 headLIR->defMask = ENCODE_ALL;
2989 }
2990
2991 switch(opcode) {
2992
2993 case llvm::Instruction::ICmp: {
2994 llvm::Instruction* nextInst = nextIt;
2995 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(nextInst);
2996 if (brInst != NULL /* and... */) {
2997 cvtICmpBr(cUnit, inst, brInst);
2998 ++it;
2999 } else {
3000 cvtICmp(cUnit, inst);
3001 }
3002 }
3003 break;
3004
3005 case llvm::Instruction::Call: {
3006 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(inst);
3007 llvm::Function* callee = callInst->getCalledFunction();
3008 greenland::IntrinsicHelper::IntrinsicId id =
3009 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3010 switch (id) {
3011 case greenland::IntrinsicHelper::AllocaShadowFrame:
3012 case greenland::IntrinsicHelper::SetShadowFrameEntry:
3013 case greenland::IntrinsicHelper::PopShadowFrame:
TDYa1278e950c12012-11-02 09:58:19 -07003014 case greenland::IntrinsicHelper::SetVReg:
buzbee0967a252012-09-14 10:43:54 -07003015 // Ignore shadow frame stuff for quick compiler
3016 break;
3017 case greenland::IntrinsicHelper::CopyInt:
3018 case greenland::IntrinsicHelper::CopyObj:
3019 case greenland::IntrinsicHelper::CopyFloat:
3020 case greenland::IntrinsicHelper::CopyLong:
3021 case greenland::IntrinsicHelper::CopyDouble:
3022 cvtCopy(cUnit, callInst);
3023 break;
3024 case greenland::IntrinsicHelper::ConstInt:
3025 case greenland::IntrinsicHelper::ConstObj:
3026 case greenland::IntrinsicHelper::ConstLong:
3027 case greenland::IntrinsicHelper::ConstFloat:
3028 case greenland::IntrinsicHelper::ConstDouble:
3029 cvtConst(cUnit, callInst);
3030 break;
3031 case greenland::IntrinsicHelper::DivInt:
3032 case greenland::IntrinsicHelper::DivLong:
3033 cvtBinOp(cUnit, kOpDiv, inst);
3034 break;
3035 case greenland::IntrinsicHelper::RemInt:
3036 case greenland::IntrinsicHelper::RemLong:
3037 cvtBinOp(cUnit, kOpRem, inst);
3038 break;
3039 case greenland::IntrinsicHelper::MethodInfo:
3040 // Already dealt with - just ignore it here.
3041 break;
3042 case greenland::IntrinsicHelper::CheckSuspend:
3043 genSuspendTest(cUnit, 0 /* optFlags already applied */);
3044 break;
3045 case greenland::IntrinsicHelper::HLInvokeObj:
3046 case greenland::IntrinsicHelper::HLInvokeFloat:
3047 case greenland::IntrinsicHelper::HLInvokeDouble:
3048 case greenland::IntrinsicHelper::HLInvokeLong:
3049 case greenland::IntrinsicHelper::HLInvokeInt:
3050 cvtInvoke(cUnit, callInst, false /* isVoid */, false /* newArray */);
3051 break;
3052 case greenland::IntrinsicHelper::HLInvokeVoid:
3053 cvtInvoke(cUnit, callInst, true /* isVoid */, false /* newArray */);
3054 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003055 case greenland::IntrinsicHelper::HLFilledNewArray:
buzbee0967a252012-09-14 10:43:54 -07003056 cvtInvoke(cUnit, callInst, false /* isVoid */, true /* newArray */);
3057 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003058 case greenland::IntrinsicHelper::HLFillArrayData:
buzbee0967a252012-09-14 10:43:54 -07003059 cvtFillArrayData(cUnit, callInst);
3060 break;
3061 case greenland::IntrinsicHelper::ConstString:
3062 cvtConstObject(cUnit, callInst, true /* isString */);
3063 break;
3064 case greenland::IntrinsicHelper::ConstClass:
3065 cvtConstObject(cUnit, callInst, false /* isString */);
3066 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003067 case greenland::IntrinsicHelper::HLCheckCast:
buzbee0967a252012-09-14 10:43:54 -07003068 cvtCheckCast(cUnit, callInst);
3069 break;
3070 case greenland::IntrinsicHelper::NewInstance:
3071 cvtNewInstance(cUnit, callInst);
3072 break;
3073 case greenland::IntrinsicHelper::HLSgetObject:
3074 cvtSget(cUnit, callInst, false /* wide */, true /* Object */);
3075 break;
3076 case greenland::IntrinsicHelper::HLSget:
3077 case greenland::IntrinsicHelper::HLSgetFloat:
3078 case greenland::IntrinsicHelper::HLSgetBoolean:
3079 case greenland::IntrinsicHelper::HLSgetByte:
3080 case greenland::IntrinsicHelper::HLSgetChar:
3081 case greenland::IntrinsicHelper::HLSgetShort:
3082 cvtSget(cUnit, callInst, false /* wide */, false /* Object */);
3083 break;
3084 case greenland::IntrinsicHelper::HLSgetWide:
3085 case greenland::IntrinsicHelper::HLSgetDouble:
3086 cvtSget(cUnit, callInst, true /* wide */, false /* Object */);
3087 break;
3088 case greenland::IntrinsicHelper::HLSput:
3089 case greenland::IntrinsicHelper::HLSputFloat:
3090 case greenland::IntrinsicHelper::HLSputBoolean:
3091 case greenland::IntrinsicHelper::HLSputByte:
3092 case greenland::IntrinsicHelper::HLSputChar:
3093 case greenland::IntrinsicHelper::HLSputShort:
3094 cvtSput(cUnit, callInst, false /* wide */, false /* Object */);
3095 break;
3096 case greenland::IntrinsicHelper::HLSputWide:
3097 case greenland::IntrinsicHelper::HLSputDouble:
3098 cvtSput(cUnit, callInst, true /* wide */, false /* Object */);
3099 break;
3100 case greenland::IntrinsicHelper::HLSputObject:
3101 cvtSput(cUnit, callInst, false /* wide */, true /* Object */);
3102 break;
3103 case greenland::IntrinsicHelper::GetException:
3104 cvtMoveException(cUnit, callInst);
3105 break;
TDYa127f71bf5a2012-07-29 20:09:52 -07003106 case greenland::IntrinsicHelper::HLThrowException:
buzbee0967a252012-09-14 10:43:54 -07003107 cvtThrow(cUnit, callInst);
3108 break;
3109 case greenland::IntrinsicHelper::MonitorEnter:
3110 cvtMonitorEnterExit(cUnit, true /* isEnter */, callInst);
3111 break;
3112 case greenland::IntrinsicHelper::MonitorExit:
3113 cvtMonitorEnterExit(cUnit, false /* isEnter */, callInst);
3114 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003115 case greenland::IntrinsicHelper::OptArrayLength:
buzbee0967a252012-09-14 10:43:54 -07003116 cvtArrayLength(cUnit, callInst);
3117 break;
3118 case greenland::IntrinsicHelper::NewArray:
3119 cvtNewArray(cUnit, callInst);
3120 break;
3121 case greenland::IntrinsicHelper::InstanceOf:
3122 cvtInstanceOf(cUnit, callInst);
3123 break;
3124
3125 case greenland::IntrinsicHelper::HLArrayGet:
3126 case greenland::IntrinsicHelper::HLArrayGetObject:
3127 case greenland::IntrinsicHelper::HLArrayGetFloat:
3128 cvtAget(cUnit, callInst, kWord, 2);
3129 break;
3130 case greenland::IntrinsicHelper::HLArrayGetWide:
3131 case greenland::IntrinsicHelper::HLArrayGetDouble:
3132 cvtAget(cUnit, callInst, kLong, 3);
3133 break;
3134 case greenland::IntrinsicHelper::HLArrayGetBoolean:
3135 cvtAget(cUnit, callInst, kUnsignedByte, 0);
3136 break;
3137 case greenland::IntrinsicHelper::HLArrayGetByte:
3138 cvtAget(cUnit, callInst, kSignedByte, 0);
3139 break;
3140 case greenland::IntrinsicHelper::HLArrayGetChar:
3141 cvtAget(cUnit, callInst, kUnsignedHalf, 1);
3142 break;
3143 case greenland::IntrinsicHelper::HLArrayGetShort:
3144 cvtAget(cUnit, callInst, kSignedHalf, 1);
3145 break;
3146
3147 case greenland::IntrinsicHelper::HLArrayPut:
3148 case greenland::IntrinsicHelper::HLArrayPutFloat:
3149 cvtAputPrimitive(cUnit, callInst, kWord, 2);
3150 break;
3151 case greenland::IntrinsicHelper::HLArrayPutObject:
3152 cvtAputObj(cUnit, callInst);
3153 break;
3154 case greenland::IntrinsicHelper::HLArrayPutWide:
3155 case greenland::IntrinsicHelper::HLArrayPutDouble:
3156 cvtAputPrimitive(cUnit, callInst, kLong, 3);
3157 break;
3158 case greenland::IntrinsicHelper::HLArrayPutBoolean:
3159 cvtAputPrimitive(cUnit, callInst, kUnsignedByte, 0);
3160 break;
3161 case greenland::IntrinsicHelper::HLArrayPutByte:
3162 cvtAputPrimitive(cUnit, callInst, kSignedByte, 0);
3163 break;
3164 case greenland::IntrinsicHelper::HLArrayPutChar:
3165 cvtAputPrimitive(cUnit, callInst, kUnsignedHalf, 1);
3166 break;
3167 case greenland::IntrinsicHelper::HLArrayPutShort:
3168 cvtAputPrimitive(cUnit, callInst, kSignedHalf, 1);
3169 break;
3170
3171 case greenland::IntrinsicHelper::HLIGet:
3172 case greenland::IntrinsicHelper::HLIGetFloat:
3173 cvtIget(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3174 break;
3175 case greenland::IntrinsicHelper::HLIGetObject:
3176 cvtIget(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3177 break;
3178 case greenland::IntrinsicHelper::HLIGetWide:
3179 case greenland::IntrinsicHelper::HLIGetDouble:
3180 cvtIget(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3181 break;
3182 case greenland::IntrinsicHelper::HLIGetBoolean:
3183 cvtIget(cUnit, callInst, kUnsignedByte, false /* isWide */,
3184 false /* obj */);
3185 break;
3186 case greenland::IntrinsicHelper::HLIGetByte:
3187 cvtIget(cUnit, callInst, kSignedByte, false /* isWide */,
3188 false /* obj */);
3189 break;
3190 case greenland::IntrinsicHelper::HLIGetChar:
3191 cvtIget(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3192 false /* obj */);
3193 break;
3194 case greenland::IntrinsicHelper::HLIGetShort:
3195 cvtIget(cUnit, callInst, kSignedHalf, false /* isWide */,
3196 false /* obj */);
3197 break;
3198
3199 case greenland::IntrinsicHelper::HLIPut:
3200 case greenland::IntrinsicHelper::HLIPutFloat:
3201 cvtIput(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
3202 break;
3203 case greenland::IntrinsicHelper::HLIPutObject:
3204 cvtIput(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
3205 break;
3206 case greenland::IntrinsicHelper::HLIPutWide:
3207 case greenland::IntrinsicHelper::HLIPutDouble:
3208 cvtIput(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
3209 break;
3210 case greenland::IntrinsicHelper::HLIPutBoolean:
3211 cvtIput(cUnit, callInst, kUnsignedByte, false /* isWide */,
3212 false /* obj */);
3213 break;
3214 case greenland::IntrinsicHelper::HLIPutByte:
3215 cvtIput(cUnit, callInst, kSignedByte, false /* isWide */,
3216 false /* obj */);
3217 break;
3218 case greenland::IntrinsicHelper::HLIPutChar:
3219 cvtIput(cUnit, callInst, kUnsignedHalf, false /* isWide */,
3220 false /* obj */);
3221 break;
3222 case greenland::IntrinsicHelper::HLIPutShort:
3223 cvtIput(cUnit, callInst, kSignedHalf, false /* isWide */,
3224 false /* obj */);
3225 break;
3226
3227 case greenland::IntrinsicHelper::IntToChar:
3228 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_CHAR);
3229 break;
3230 case greenland::IntrinsicHelper::IntToShort:
3231 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_SHORT);
3232 break;
3233 case greenland::IntrinsicHelper::IntToByte:
3234 cvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_BYTE);
3235 break;
3236
TDYa1274ec8ccd2012-08-11 07:04:57 -07003237 case greenland::IntrinsicHelper::F2I:
3238 case greenland::IntrinsicHelper::D2I:
3239 case greenland::IntrinsicHelper::F2L:
3240 case greenland::IntrinsicHelper::D2L:
3241 cvtFPToInt(cUnit, callInst);
3242 break;
3243
buzbee0967a252012-09-14 10:43:54 -07003244 case greenland::IntrinsicHelper::CmplFloat:
3245 cvtFPCompare(cUnit, callInst, Instruction::CMPL_FLOAT);
3246 break;
3247 case greenland::IntrinsicHelper::CmpgFloat:
3248 cvtFPCompare(cUnit, callInst, Instruction::CMPG_FLOAT);
3249 break;
3250 case greenland::IntrinsicHelper::CmplDouble:
3251 cvtFPCompare(cUnit, callInst, Instruction::CMPL_DOUBLE);
3252 break;
3253 case greenland::IntrinsicHelper::CmpgDouble:
3254 cvtFPCompare(cUnit, callInst, Instruction::CMPG_DOUBLE);
3255 break;
3256
3257 case greenland::IntrinsicHelper::CmpLong:
3258 cvtLongCompare(cUnit, callInst);
3259 break;
3260
3261 case greenland::IntrinsicHelper::SHLLong:
3262 cvtShiftOp(cUnit, Instruction::SHL_LONG, callInst);
3263 break;
3264 case greenland::IntrinsicHelper::SHRLong:
3265 cvtShiftOp(cUnit, Instruction::SHR_LONG, callInst);
3266 break;
3267 case greenland::IntrinsicHelper::USHRLong:
3268 cvtShiftOp(cUnit, Instruction::USHR_LONG, callInst);
3269 break;
3270 case greenland::IntrinsicHelper::SHLInt:
3271 cvtShiftOp(cUnit, Instruction::SHL_INT, callInst);
3272 break;
3273 case greenland::IntrinsicHelper::SHRInt:
3274 cvtShiftOp(cUnit, Instruction::SHR_INT, callInst);
3275 break;
3276 case greenland::IntrinsicHelper::USHRInt:
3277 cvtShiftOp(cUnit, Instruction::USHR_INT, callInst);
3278 break;
3279
3280 case greenland::IntrinsicHelper::CatchTargets: {
3281 llvm::SwitchInst* swInst =
3282 llvm::dyn_cast<llvm::SwitchInst>(nextIt);
3283 DCHECK(swInst != NULL);
3284 /*
3285 * Discard the edges and the following conditional branch.
3286 * Do a direct branch to the default target (which is the
3287 * "work" portion of the pair.
3288 * TODO: awful code layout - rework
3289 */
3290 llvm::BasicBlock* targetBB = swInst->getDefaultDest();
3291 DCHECK(targetBB != NULL);
3292 opUnconditionalBranch(cUnit,
3293 cUnit->blockToLabelMap.Get(targetBB));
3294 ++it;
3295 // Set next bb to default target - improves code layout
3296 nextBB = targetBB;
3297 }
3298 break;
3299
3300 default:
3301 LOG(FATAL) << "Unexpected intrinsic " << (int)id << ", "
3302 << cUnit->intrinsic_helper->GetName(id);
3303 }
3304 }
3305 break;
3306
3307 case llvm::Instruction::Br: cvtBr(cUnit, inst); break;
3308 case llvm::Instruction::Add: cvtBinOp(cUnit, kOpAdd, inst); break;
3309 case llvm::Instruction::Sub: cvtBinOp(cUnit, kOpSub, inst); break;
3310 case llvm::Instruction::Mul: cvtBinOp(cUnit, kOpMul, inst); break;
3311 case llvm::Instruction::SDiv: cvtBinOp(cUnit, kOpDiv, inst); break;
3312 case llvm::Instruction::SRem: cvtBinOp(cUnit, kOpRem, inst); break;
3313 case llvm::Instruction::And: cvtBinOp(cUnit, kOpAnd, inst); break;
3314 case llvm::Instruction::Or: cvtBinOp(cUnit, kOpOr, inst); break;
3315 case llvm::Instruction::Xor: cvtBinOp(cUnit, kOpXor, inst); break;
3316 case llvm::Instruction::PHI: cvtPhi(cUnit, inst); break;
3317 case llvm::Instruction::Ret: cvtRet(cUnit, inst); break;
3318 case llvm::Instruction::FAdd: cvtBinFPOp(cUnit, kOpAdd, inst); break;
3319 case llvm::Instruction::FSub: cvtBinFPOp(cUnit, kOpSub, inst); break;
3320 case llvm::Instruction::FMul: cvtBinFPOp(cUnit, kOpMul, inst); break;
3321 case llvm::Instruction::FDiv: cvtBinFPOp(cUnit, kOpDiv, inst); break;
3322 case llvm::Instruction::FRem: cvtBinFPOp(cUnit, kOpRem, inst); break;
3323 case llvm::Instruction::SIToFP: cvtIntToFP(cUnit, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003324 case llvm::Instruction::FPTrunc: cvtDoubleToFloat(cUnit, inst); break;
3325 case llvm::Instruction::FPExt: cvtFloatToDouble(cUnit, inst); break;
3326 case llvm::Instruction::Trunc: cvtTrunc(cUnit, inst); break;
3327
3328 case llvm::Instruction::ZExt: cvtIntExt(cUnit, inst, false /* signed */);
3329 break;
3330 case llvm::Instruction::SExt: cvtIntExt(cUnit, inst, true /* signed */);
3331 break;
3332
3333 case llvm::Instruction::Switch: cvtSwitch(cUnit, inst); break;
3334
3335 case llvm::Instruction::Unreachable:
3336 break; // FIXME: can we really ignore these?
3337
3338 case llvm::Instruction::Shl:
3339 case llvm::Instruction::LShr:
3340 case llvm::Instruction::AShr:
3341 case llvm::Instruction::Invoke:
3342 case llvm::Instruction::FPToUI:
TDYa1274ec8ccd2012-08-11 07:04:57 -07003343 case llvm::Instruction::FPToSI:
buzbee0967a252012-09-14 10:43:54 -07003344 case llvm::Instruction::UIToFP:
3345 case llvm::Instruction::PtrToInt:
3346 case llvm::Instruction::IntToPtr:
3347 case llvm::Instruction::FCmp:
3348 case llvm::Instruction::URem:
3349 case llvm::Instruction::UDiv:
3350 case llvm::Instruction::Resume:
3351 case llvm::Instruction::Alloca:
3352 case llvm::Instruction::GetElementPtr:
3353 case llvm::Instruction::Fence:
3354 case llvm::Instruction::AtomicCmpXchg:
3355 case llvm::Instruction::AtomicRMW:
3356 case llvm::Instruction::BitCast:
3357 case llvm::Instruction::VAArg:
3358 case llvm::Instruction::Select:
3359 case llvm::Instruction::UserOp1:
3360 case llvm::Instruction::UserOp2:
3361 case llvm::Instruction::ExtractElement:
3362 case llvm::Instruction::InsertElement:
3363 case llvm::Instruction::ShuffleVector:
3364 case llvm::Instruction::ExtractValue:
3365 case llvm::Instruction::InsertValue:
3366 case llvm::Instruction::LandingPad:
3367 case llvm::Instruction::IndirectBr:
3368 case llvm::Instruction::Load:
3369 case llvm::Instruction::Store:
3370 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3371
3372 default:
3373 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3374 break;
buzbeead8f15e2012-06-18 14:49:45 -07003375 }
3376 }
buzbee2cfc6392012-05-07 14:51:40 -07003377
buzbee0967a252012-09-14 10:43:54 -07003378 if (headLIR != NULL) {
3379 oatApplyLocalOptimizations(cUnit, headLIR, cUnit->lastLIRInsn);
buzbee2cfc6392012-05-07 14:51:40 -07003380 }
buzbee0967a252012-09-14 10:43:54 -07003381 if (nextBB != NULL) {
3382 bb = nextBB;
3383 nextBB = NULL;
buzbee6969d502012-06-15 16:40:31 -07003384 }
buzbee6969d502012-06-15 16:40:31 -07003385 }
buzbee2cfc6392012-05-07 14:51:40 -07003386 return false;
3387}
3388
3389/*
3390 * Convert LLVM_IR to MIR:
3391 * o Iterate through the LLVM_IR and construct a graph using
3392 * standard MIR building blocks.
3393 * o Perform a basic-block optimization pass to remove unnecessary
3394 * store/load sequences.
3395 * o Convert the LLVM Value operands into RegLocations where applicable.
3396 * o Create ssaRep def/use operand arrays for each converted LLVM opcode
3397 * o Perform register promotion
3398 * o Iterate through the graph a basic block at a time, generating
3399 * LIR.
3400 * o Assemble LIR as usual.
3401 * o Profit.
3402 */
3403void oatMethodBitcode2LIR(CompilationUnit* cUnit)
3404{
buzbeead8f15e2012-06-18 14:49:45 -07003405 llvm::Function* func = cUnit->func;
3406 int numBasicBlocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003407 // Allocate a list for LIR basic block labels
3408 cUnit->blockLabelList =
buzbeea1da8a52012-07-09 14:00:21 -07003409 (LIR*)oatNew(cUnit, sizeof(LIR) * numBasicBlocks, true, kAllocLIR);
3410 LIR* labelList = cUnit->blockLabelList;
buzbee2cfc6392012-05-07 14:51:40 -07003411 int nextLabel = 0;
buzbeead8f15e2012-06-18 14:49:45 -07003412 for (llvm::Function::iterator i = func->begin(),
3413 e = func->end(); i != e; ++i) {
buzbee2cfc6392012-05-07 14:51:40 -07003414 cUnit->blockToLabelMap.Put(static_cast<llvm::BasicBlock*>(i),
3415 &labelList[nextLabel++]);
3416 }
buzbeead8f15e2012-06-18 14:49:45 -07003417
3418 /*
3419 * Keep honest - clear regLocations, Value => RegLocation,
3420 * promotion map and VmapTables.
3421 */
3422 cUnit->locMap.clear(); // Start fresh
3423 cUnit->regLocation = NULL;
3424 for (int i = 0; i < cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
3425 i++) {
3426 cUnit->promotionMap[i].coreLocation = kLocDalvikFrame;
3427 cUnit->promotionMap[i].fpLocation = kLocDalvikFrame;
3428 }
3429 cUnit->coreSpillMask = 0;
3430 cUnit->numCoreSpills = 0;
3431 cUnit->fpSpillMask = 0;
3432 cUnit->numFPSpills = 0;
3433 cUnit->coreVmapTable.clear();
3434 cUnit->fpVmapTable.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003435
3436 /*
3437 * At this point, we've lost all knowledge of register promotion.
3438 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003439 * exists - not required for correctness). Normally, this will
3440 * be the first instruction we encounter, so we won't have to iterate
3441 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003442 */
buzbeeca7a5e42012-08-20 11:12:18 -07003443 for (llvm::inst_iterator i = llvm::inst_begin(func),
3444 e = llvm::inst_end(func); i != e; ++i) {
3445 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(&*i);
3446 if (callInst != NULL) {
3447 llvm::Function* callee = callInst->getCalledFunction();
3448 greenland::IntrinsicHelper::IntrinsicId id =
3449 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3450 if (id == greenland::IntrinsicHelper::MethodInfo) {
3451 if (cUnit->printMe) {
3452 LOG(INFO) << "Found MethodInfo";
3453 }
3454 llvm::MDNode* regInfoNode = callInst->getMetadata("RegInfo");
3455 if (regInfoNode != NULL) {
3456 llvm::ConstantInt* numInsValue =
3457 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(0));
3458 llvm::ConstantInt* numRegsValue =
3459 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(1));
3460 llvm::ConstantInt* numOutsValue =
3461 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(2));
3462 llvm::ConstantInt* numCompilerTempsValue =
3463 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(3));
3464 llvm::ConstantInt* numSSARegsValue =
3465 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(4));
3466 if (cUnit->printMe) {
3467 LOG(INFO) << "RegInfo - Ins:" << numInsValue->getZExtValue()
3468 << ", Regs:" << numRegsValue->getZExtValue()
3469 << ", Outs:" << numOutsValue->getZExtValue()
3470 << ", CTemps:" << numCompilerTempsValue->getZExtValue()
3471 << ", SSARegs:" << numSSARegsValue->getZExtValue();
3472 }
3473 }
3474 llvm::MDNode* pmapInfoNode = callInst->getMetadata("PromotionMap");
3475 if (pmapInfoNode != NULL) {
3476 int elems = pmapInfoNode->getNumOperands();
3477 if (cUnit->printMe) {
3478 LOG(INFO) << "PMap size: " << elems;
3479 }
3480 for (int i = 0; i < elems; i++) {
3481 llvm::ConstantInt* rawMapData =
3482 static_cast<llvm::ConstantInt*>(pmapInfoNode->getOperand(i));
3483 uint32_t mapData = rawMapData->getZExtValue();
3484 PromotionMap* p = &cUnit->promotionMap[i];
3485 p->firstInPair = (mapData >> 24) & 0xff;
3486 p->fpReg = (mapData >> 16) & 0xff;
3487 p->coreReg = (mapData >> 8) & 0xff;
3488 p->fpLocation = static_cast<RegLocationType>((mapData >> 4) & 0xf);
3489 if (p->fpLocation == kLocPhysReg) {
3490 oatRecordFpPromotion(cUnit, p->fpReg, i);
3491 }
3492 p->coreLocation = static_cast<RegLocationType>(mapData & 0xf);
3493 if (p->coreLocation == kLocPhysReg) {
3494 oatRecordCorePromotion(cUnit, p->coreReg, i);
3495 }
3496 }
3497 if (cUnit->printMe) {
3498 oatDumpPromotionMap(cUnit);
3499 }
3500 }
3501 break;
3502 }
3503 }
3504 }
3505 oatAdjustSpillMask(cUnit);
3506 cUnit->frameSize = oatComputeFrameSize(cUnit);
buzbeead8f15e2012-06-18 14:49:45 -07003507
3508 // Create RegLocations for arguments
3509 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
3510 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
3511 for (; it != it_end; ++it) {
3512 llvm::Value* val = it;
3513 createLocFromValue(cUnit, val);
3514 }
3515 // Create RegLocations for all non-argument defintions
3516 for (llvm::inst_iterator i = llvm::inst_begin(func),
3517 e = llvm::inst_end(func); i != e; ++i) {
3518 llvm::Value* val = &*i;
3519 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
3520 createLocFromValue(cUnit, val);
3521 }
3522 }
3523
buzbee2cfc6392012-05-07 14:51:40 -07003524 // Walk the blocks, generating code.
3525 for (llvm::Function::iterator i = cUnit->func->begin(),
3526 e = cUnit->func->end(); i != e; ++i) {
3527 methodBitcodeBlockCodeGen(cUnit, static_cast<llvm::BasicBlock*>(i));
3528 }
3529
3530 handleSuspendLaunchpads(cUnit);
3531
3532 handleThrowLaunchpads(cUnit);
3533
3534 handleIntrinsicLaunchpads(cUnit);
3535
buzbee692be802012-08-29 15:52:59 -07003536 cUnit->func->eraseFromParent();
3537 cUnit->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003538}
3539
3540
3541} // namespace art