blob: 1c576bc43a4aaaefcbb7aa3444f80a100ac4a134 [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
buzbee1bc37c62012-11-20 13:35:41 -080030#include "../compiler_internals.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080031#include "method_codegen_driver.h"
32#include "local_optimizations.h"
buzbee1bc37c62012-11-20 13:35:41 -080033#include "codegen_util.h"
34#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080035
buzbee8320f382012-09-11 16:29:42 -070036static const char* kLabelFormat = "%c0x%x_%d";
buzbee951c0a12012-10-03 16:31:39 -070037static const char kInvalidBlock = 0xff;
buzbee8320f382012-09-11 16:29:42 -070038static const char kNormalBlock = 'L';
39static const char kCatchBlock = 'C';
buzbee2cfc6392012-05-07 14:51:40 -070040
41namespace art {
buzbee1bc37c62012-11-20 13:35:41 -080042// TODO: unify badLoc
43const RegLocation badLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
44 INVALID_REG, INVALID_REG, INVALID_SREG,
45 INVALID_SREG};
buzbeeaad94382012-11-21 07:40:50 -080046static RegLocation GetLoc(CompilationUnit* cUnit, llvm::Value* val);
buzbee2cfc6392012-05-07 14:51:40 -070047
buzbeeaad94382012-11-21 07:40:50 -080048static llvm::BasicBlock* GetLLVMBlock(CompilationUnit* cUnit, int id)
buzbee2cfc6392012-05-07 14:51:40 -070049{
50 return cUnit->idToBlockMap.Get(id);
51}
52
buzbeeaad94382012-11-21 07:40:50 -080053static llvm::Value* GetLLVMValue(CompilationUnit* cUnit, int sReg)
buzbee2cfc6392012-05-07 14:51:40 -070054{
buzbee52a77fc2012-11-20 19:50:46 -080055 return reinterpret_cast<llvm::Value*>(GrowableListGetElement(&cUnit->llvmValues, sReg));
buzbee2cfc6392012-05-07 14:51:40 -070056}
57
58// Replace the placeholder value with the real definition
buzbeeaad94382012-11-21 07:40:50 -080059static void DefineValue(CompilationUnit* cUnit, llvm::Value* val, int sReg)
buzbee2cfc6392012-05-07 14:51:40 -070060{
buzbee52a77fc2012-11-20 19:50:46 -080061 llvm::Value* placeholder = GetLLVMValue(cUnit, sReg);
buzbee9a2487f2012-07-26 14:01:13 -070062 if (placeholder == NULL) {
63 // This can happen on instruction rewrite on verification failure
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070064 LOG(WARNING) << "Null placeholder";
buzbee9a2487f2012-07-26 14:01:13 -070065 return;
66 }
buzbee2cfc6392012-05-07 14:51:40 -070067 placeholder->replaceAllUsesWith(val);
68 val->takeName(placeholder);
buzbeecbd6d442012-11-17 14:11:25 -080069 cUnit->llvmValues.elemList[sReg] = reinterpret_cast<uintptr_t>(val);
buzbee4be777b2012-07-12 14:38:18 -070070 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(placeholder);
71 DCHECK(inst != NULL);
72 inst->eraseFromParent();
TDYa1278e950c12012-11-02 09:58:19 -070073
74 // Set vreg for debugging
75 if (!cUnit->compiler->IsDebuggingSupported()) {
76 greenland::IntrinsicHelper::IntrinsicId id =
77 greenland::IntrinsicHelper::SetVReg;
78 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
79 int vReg = SRegToVReg(cUnit, sReg);
80 llvm::Value* tableSlot = cUnit->irb->getInt32(vReg);
81 llvm::Value* args[] = { tableSlot, val };
82 cUnit->irb->CreateCall(func, args);
83 }
buzbee2cfc6392012-05-07 14:51:40 -070084}
85
buzbeeaad94382012-11-21 07:40:50 -080086static llvm::Type* LlvmTypeFromLocRec(CompilationUnit* cUnit, RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -070087{
88 llvm::Type* res = NULL;
89 if (loc.wide) {
90 if (loc.fp)
buzbee4f1181f2012-06-22 13:52:12 -070091 res = cUnit->irb->getDoubleTy();
buzbee2cfc6392012-05-07 14:51:40 -070092 else
buzbee4f1181f2012-06-22 13:52:12 -070093 res = cUnit->irb->getInt64Ty();
buzbee2cfc6392012-05-07 14:51:40 -070094 } else {
95 if (loc.fp) {
buzbee4f1181f2012-06-22 13:52:12 -070096 res = cUnit->irb->getFloatTy();
buzbee2cfc6392012-05-07 14:51:40 -070097 } else {
98 if (loc.ref)
99 res = cUnit->irb->GetJObjectTy();
100 else
buzbee4f1181f2012-06-22 13:52:12 -0700101 res = cUnit->irb->getInt32Ty();
buzbee2cfc6392012-05-07 14:51:40 -0700102 }
103 }
104 return res;
105}
106
buzbeead8f15e2012-06-18 14:49:45 -0700107/* Create an in-memory RegLocation from an llvm Value. */
buzbeeaad94382012-11-21 07:40:50 -0800108static void CreateLocFromValue(CompilationUnit* cUnit, llvm::Value* val)
buzbeead8f15e2012-06-18 14:49:45 -0700109{
110 // NOTE: llvm takes shortcuts with c_str() - get to std::string firstt
111 std::string s(val->getName().str());
112 const char* valName = s.c_str();
buzbeead8f15e2012-06-18 14:49:45 -0700113 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
114 DCHECK(it == cUnit->locMap.end()) << " - already defined: " << valName;
115 int baseSReg = INVALID_SREG;
116 int subscript = -1;
117 sscanf(valName, "v%d_%d", &baseSReg, &subscript);
118 if ((baseSReg == INVALID_SREG) && (!strcmp(valName, "method"))) {
119 baseSReg = SSA_METHOD_BASEREG;
120 subscript = 0;
121 }
buzbeead8f15e2012-06-18 14:49:45 -0700122 DCHECK_NE(baseSReg, INVALID_SREG);
123 DCHECK_NE(subscript, -1);
124 // TODO: redo during C++'ification
125 RegLocation loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, INVALID_REG,
126 INVALID_REG, INVALID_SREG, INVALID_SREG};
127 llvm::Type* ty = val->getType();
128 loc.wide = ((ty == cUnit->irb->getInt64Ty()) ||
129 (ty == cUnit->irb->getDoubleTy()));
130 loc.defined = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700131 loc.home = false; // May change during promotion
buzbeead8f15e2012-06-18 14:49:45 -0700132 loc.sRegLow = baseSReg;
133 loc.origSReg = cUnit->locMap.size();
buzbeeca7a5e42012-08-20 11:12:18 -0700134 PromotionMap pMap = cUnit->promotionMap[baseSReg];
135 if (ty == cUnit->irb->getFloatTy()) {
136 loc.fp = true;
137 if (pMap.fpLocation == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800138 loc.lowReg = pMap.FpReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700139 loc.location = kLocPhysReg;
140 loc.home = true;
141 }
142 } else if (ty == cUnit->irb->getDoubleTy()) {
143 loc.fp = true;
144 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
145 if ((pMap.fpLocation == kLocPhysReg) &&
146 (pMapHigh.fpLocation == kLocPhysReg) &&
buzbee52a77fc2012-11-20 19:50:46 -0800147 ((pMap.FpReg & 0x1) == 0) &&
148 (pMap.FpReg + 1 == pMapHigh.FpReg)) {
149 loc.lowReg = pMap.FpReg;
150 loc.highReg = pMapHigh.FpReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700151 loc.location = kLocPhysReg;
152 loc.home = true;
153 }
154 } else if (ty == cUnit->irb->GetJObjectTy()) {
155 loc.ref = true;
156 if (pMap.coreLocation == kLocPhysReg) {
157 loc.lowReg = pMap.coreReg;
158 loc.location = kLocPhysReg;
159 loc.home = true;
160 }
161 } else if (ty == cUnit->irb->getInt64Ty()) {
162 loc.core = true;
163 PromotionMap pMapHigh = cUnit->promotionMap[baseSReg + 1];
164 if ((pMap.coreLocation == kLocPhysReg) &&
165 (pMapHigh.coreLocation == kLocPhysReg)) {
166 loc.lowReg = pMap.coreReg;
167 loc.highReg = pMapHigh.coreReg;
168 loc.location = kLocPhysReg;
169 loc.home = true;
170 }
171 } else {
172 loc.core = true;
173 if (pMap.coreLocation == kLocPhysReg) {
174 loc.lowReg = pMap.coreReg;
175 loc.location = kLocPhysReg;
176 loc.home = true;
177 }
178 }
179
180 if (cUnit->printMe && loc.home) {
181 if (loc.wide) {
buzbeecbd6d442012-11-17 14:11:25 -0800182 LOG(INFO) << "Promoted wide " << s << " to regs " << loc.lowReg << "/" << loc.highReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700183 } else {
buzbeecbd6d442012-11-17 14:11:25 -0800184 LOG(INFO) << "Promoted " << s << " to reg " << loc.lowReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700185 }
186 }
buzbeead8f15e2012-06-18 14:49:45 -0700187 cUnit->locMap.Put(val, loc);
188}
buzbeeaad94382012-11-21 07:40:50 -0800189
190static void InitIR(CompilationUnit* cUnit)
buzbee2cfc6392012-05-07 14:51:40 -0700191{
buzbee4df2bbd2012-10-11 14:46:06 -0700192 LLVMInfo* llvmInfo = cUnit->llvm_info;
193 if (llvmInfo == NULL) {
194 CompilerTls* tls = cUnit->compiler->GetTls();
195 CHECK(tls != NULL);
196 llvmInfo = static_cast<LLVMInfo*>(tls->GetLLVMInfo());
197 if (llvmInfo == NULL) {
198 llvmInfo = new LLVMInfo();
199 tls->SetLLVMInfo(llvmInfo);
200 }
201 }
202 cUnit->context = llvmInfo->GetLLVMContext();
203 cUnit->module = llvmInfo->GetLLVMModule();
204 cUnit->intrinsic_helper = llvmInfo->GetIntrinsicHelper();
205 cUnit->irb = llvmInfo->GetIRBuilder();
buzbee2cfc6392012-05-07 14:51:40 -0700206}
207
buzbeeaad94382012-11-21 07:40:50 -0800208static const char* LlvmSSAName(CompilationUnit* cUnit, int ssaReg) {
buzbee2cfc6392012-05-07 14:51:40 -0700209 return GET_ELEM_N(cUnit->ssaStrings, char*, ssaReg);
210}
211
buzbee52a77fc2012-11-20 19:50:46 -0800212llvm::BasicBlock* FindCaseTarget(CompilationUnit* cUnit, uint32_t vaddr)
buzbeef58c12c2012-07-03 15:06:29 -0700213{
buzbee52a77fc2012-11-20 19:50:46 -0800214 BasicBlock* bb = FindBlock(cUnit, vaddr);
buzbeef58c12c2012-07-03 15:06:29 -0700215 DCHECK(bb != NULL);
buzbee52a77fc2012-11-20 19:50:46 -0800216 return GetLLVMBlock(cUnit, bb->id);
buzbeef58c12c2012-07-03 15:06:29 -0700217}
218
buzbeeaad94382012-11-21 07:40:50 -0800219static void ConvertPackedSwitch(CompilationUnit* cUnit, BasicBlock* bb,
220 int32_t tableOffset, RegLocation rlSrc)
buzbeef58c12c2012-07-03 15:06:29 -0700221{
222 const Instruction::PackedSwitchPayload* payload =
223 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
224 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
225
buzbee52a77fc2012-11-20 19:50:46 -0800226 llvm::Value* value = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbeef58c12c2012-07-03 15:06:29 -0700227
228 llvm::SwitchInst* sw =
buzbee52a77fc2012-11-20 19:50:46 -0800229 cUnit->irb->CreateSwitch(value, GetLLVMBlock(cUnit, bb->fallThrough->id),
buzbeef58c12c2012-07-03 15:06:29 -0700230 payload->case_count);
231
232 for (uint16_t i = 0; i < payload->case_count; ++i) {
233 llvm::BasicBlock* llvmBB =
buzbee52a77fc2012-11-20 19:50:46 -0800234 FindCaseTarget(cUnit, cUnit->currentDalvikOffset + payload->targets[i]);
buzbeef58c12c2012-07-03 15:06:29 -0700235 sw->addCase(cUnit->irb->getInt32(payload->first_key + i), llvmBB);
236 }
237 llvm::MDNode* switchNode =
238 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
239 sw->setMetadata("SwitchTable", switchNode);
240 bb->taken = NULL;
241 bb->fallThrough = NULL;
242}
243
buzbeeaad94382012-11-21 07:40:50 -0800244static void ConvertSparseSwitch(CompilationUnit* cUnit, BasicBlock* bb,
245 int32_t tableOffset, RegLocation rlSrc)
buzbeea1da8a52012-07-09 14:00:21 -0700246{
247 const Instruction::SparseSwitchPayload* payload =
248 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
249 cUnit->insns + cUnit->currentDalvikOffset + tableOffset);
250
251 const int32_t* keys = payload->GetKeys();
252 const int32_t* targets = payload->GetTargets();
253
buzbee52a77fc2012-11-20 19:50:46 -0800254 llvm::Value* value = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbeea1da8a52012-07-09 14:00:21 -0700255
256 llvm::SwitchInst* sw =
buzbee52a77fc2012-11-20 19:50:46 -0800257 cUnit->irb->CreateSwitch(value, GetLLVMBlock(cUnit, bb->fallThrough->id),
buzbeea1da8a52012-07-09 14:00:21 -0700258 payload->case_count);
259
260 for (size_t i = 0; i < payload->case_count; ++i) {
261 llvm::BasicBlock* llvmBB =
buzbee52a77fc2012-11-20 19:50:46 -0800262 FindCaseTarget(cUnit, cUnit->currentDalvikOffset + targets[i]);
buzbeea1da8a52012-07-09 14:00:21 -0700263 sw->addCase(cUnit->irb->getInt32(keys[i]), llvmBB);
264 }
265 llvm::MDNode* switchNode =
266 llvm::MDNode::get(*cUnit->context, cUnit->irb->getInt32(tableOffset));
267 sw->setMetadata("SwitchTable", switchNode);
268 bb->taken = NULL;
269 bb->fallThrough = NULL;
270}
271
buzbeeaad94382012-11-21 07:40:50 -0800272static void ConvertSget(CompilationUnit* cUnit, int32_t fieldIndex,
273 greenland::IntrinsicHelper::IntrinsicId id, RegLocation rlDest)
buzbee4f1181f2012-06-22 13:52:12 -0700274{
buzbee8fa0fda2012-06-27 15:44:52 -0700275 llvm::Constant* fieldIdx = cUnit->irb->getInt32(fieldIndex);
buzbee4f1181f2012-06-22 13:52:12 -0700276 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700277 llvm::Value* res = cUnit->irb->CreateCall(intr, fieldIdx);
buzbee52a77fc2012-11-20 19:50:46 -0800278 DefineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700279}
280
buzbeeaad94382012-11-21 07:40:50 -0800281static void ConvertSput(CompilationUnit* cUnit, int32_t fieldIndex,
282 greenland::IntrinsicHelper::IntrinsicId id, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700283{
284 llvm::SmallVector<llvm::Value*, 2> args;
285 args.push_back(cUnit->irb->getInt32(fieldIndex));
buzbee52a77fc2012-11-20 19:50:46 -0800286 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700287 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
288 cUnit->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700289}
290
buzbeeaad94382012-11-21 07:40:50 -0800291static void ConvertFillArrayData(CompilationUnit* cUnit, int32_t offset, RegLocation rlArray)
buzbee101305f2012-06-28 18:00:56 -0700292{
293 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700294 id = greenland::IntrinsicHelper::HLFillArrayData;
buzbee101305f2012-06-28 18:00:56 -0700295 llvm::SmallVector<llvm::Value*, 2> args;
296 args.push_back(cUnit->irb->getInt32(offset));
buzbee52a77fc2012-11-20 19:50:46 -0800297 args.push_back(GetLLVMValue(cUnit, rlArray.origSReg));
buzbee101305f2012-06-28 18:00:56 -0700298 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
299 cUnit->irb->CreateCall(intr, args);
300}
301
buzbeeaad94382012-11-21 07:40:50 -0800302static llvm::Value* EmitConst(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
303 RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -0700304{
305 greenland::IntrinsicHelper::IntrinsicId id;
306 if (loc.wide) {
307 if (loc.fp) {
308 id = greenland::IntrinsicHelper::ConstDouble;
309 } else {
310 id = greenland::IntrinsicHelper::ConstLong;
311 }
312 } else {
313 if (loc.fp) {
314 id = greenland::IntrinsicHelper::ConstFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700315 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700316 id = greenland::IntrinsicHelper::ConstObj;
317 } else {
318 id = greenland::IntrinsicHelper::ConstInt;
319 }
320 }
321 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
322 return cUnit->irb->CreateCall(intr, src);
323}
buzbeeb03f4872012-06-11 15:22:11 -0700324
buzbeeaad94382012-11-21 07:40:50 -0800325static void EmitPopShadowFrame(CompilationUnit* cUnit)
buzbeeb03f4872012-06-11 15:22:11 -0700326{
327 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
328 greenland::IntrinsicHelper::PopShadowFrame);
329 cUnit->irb->CreateCall(intr);
330}
331
buzbeeaad94382012-11-21 07:40:50 -0800332static llvm::Value* EmitCopy(CompilationUnit* cUnit, llvm::ArrayRef<llvm::Value*> src,
333 RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -0700334{
335 greenland::IntrinsicHelper::IntrinsicId id;
336 if (loc.wide) {
337 if (loc.fp) {
338 id = greenland::IntrinsicHelper::CopyDouble;
339 } else {
340 id = greenland::IntrinsicHelper::CopyLong;
341 }
342 } else {
343 if (loc.fp) {
344 id = greenland::IntrinsicHelper::CopyFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700345 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700346 id = greenland::IntrinsicHelper::CopyObj;
347 } else {
348 id = greenland::IntrinsicHelper::CopyInt;
349 }
350 }
351 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
352 return cUnit->irb->CreateCall(intr, src);
353}
354
buzbeeaad94382012-11-21 07:40:50 -0800355static void ConvertMoveException(CompilationUnit* cUnit, RegLocation rlDest)
buzbee32412962012-06-26 16:27:56 -0700356{
357 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
358 greenland::IntrinsicHelper::GetException);
359 llvm::Value* res = cUnit->irb->CreateCall(func);
buzbee52a77fc2012-11-20 19:50:46 -0800360 DefineValue(cUnit, res, rlDest.origSReg);
buzbee32412962012-06-26 16:27:56 -0700361}
362
buzbeeaad94382012-11-21 07:40:50 -0800363static void ConvertThrow(CompilationUnit* cUnit, RegLocation rlSrc)
buzbee32412962012-06-26 16:27:56 -0700364{
buzbee52a77fc2012-11-20 19:50:46 -0800365 llvm::Value* src = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbee32412962012-06-26 16:27:56 -0700366 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
TDYa127f71bf5a2012-07-29 20:09:52 -0700367 greenland::IntrinsicHelper::HLThrowException);
buzbee32412962012-06-26 16:27:56 -0700368 cUnit->irb->CreateCall(func, src);
buzbee32412962012-06-26 16:27:56 -0700369}
370
buzbeeaad94382012-11-21 07:40:50 -0800371static void ConvertMonitorEnterExit(CompilationUnit* cUnit, int optFlags,
372 greenland::IntrinsicHelper::IntrinsicId id,
373 RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700374{
375 llvm::SmallVector<llvm::Value*, 2> args;
376 args.push_back(cUnit->irb->getInt32(optFlags));
buzbee52a77fc2012-11-20 19:50:46 -0800377 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700378 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
379 cUnit->irb->CreateCall(func, args);
380}
381
buzbeeaad94382012-11-21 07:40:50 -0800382static void ConvertArrayLength(CompilationUnit* cUnit, int optFlags,
383 RegLocation rlDest, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700384{
385 llvm::SmallVector<llvm::Value*, 2> args;
386 args.push_back(cUnit->irb->getInt32(optFlags));
buzbee52a77fc2012-11-20 19:50:46 -0800387 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700388 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700389 greenland::IntrinsicHelper::OptArrayLength);
buzbee76592632012-06-29 15:18:35 -0700390 llvm::Value* res = cUnit->irb->CreateCall(func, args);
buzbee52a77fc2012-11-20 19:50:46 -0800391 DefineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700392}
393
buzbeeaad94382012-11-21 07:40:50 -0800394static void EmitSuspendCheck(CompilationUnit* cUnit)
buzbee2cfc6392012-05-07 14:51:40 -0700395{
396 greenland::IntrinsicHelper::IntrinsicId id =
397 greenland::IntrinsicHelper::CheckSuspend;
398 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
399 cUnit->irb->CreateCall(intr);
400}
401
buzbeeaad94382012-11-21 07:40:50 -0800402static llvm::Value* ConvertCompare(CompilationUnit* cUnit, ConditionCode cc,
403 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700404{
405 llvm::Value* res = NULL;
buzbee76592632012-06-29 15:18:35 -0700406 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700407 switch(cc) {
408 case kCondEq: res = cUnit->irb->CreateICmpEQ(src1, src2); break;
409 case kCondNe: res = cUnit->irb->CreateICmpNE(src1, src2); break;
410 case kCondLt: res = cUnit->irb->CreateICmpSLT(src1, src2); break;
411 case kCondGe: res = cUnit->irb->CreateICmpSGE(src1, src2); break;
412 case kCondGt: res = cUnit->irb->CreateICmpSGT(src1, src2); break;
413 case kCondLe: res = cUnit->irb->CreateICmpSLE(src1, src2); break;
414 default: LOG(FATAL) << "Unexpected cc value " << cc;
415 }
416 return res;
417}
418
buzbeeaad94382012-11-21 07:40:50 -0800419static void ConvertCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
420 ConditionCode cc, RegLocation rlSrc1, RegLocation rlSrc2)
buzbee2cfc6392012-05-07 14:51:40 -0700421{
422 if (bb->taken->startOffset <= mir->offset) {
buzbee52a77fc2012-11-20 19:50:46 -0800423 EmitSuspendCheck(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -0700424 }
buzbee52a77fc2012-11-20 19:50:46 -0800425 llvm::Value* src1 = GetLLVMValue(cUnit, rlSrc1.origSReg);
426 llvm::Value* src2 = GetLLVMValue(cUnit, rlSrc2.origSReg);
427 llvm::Value* condValue = ConvertCompare(cUnit, cc, src1, src2);
buzbee2cfc6392012-05-07 14:51:40 -0700428 condValue->setName(StringPrintf("t%d", cUnit->tempName++));
buzbee52a77fc2012-11-20 19:50:46 -0800429 cUnit->irb->CreateCondBr(condValue, GetLLVMBlock(cUnit, bb->taken->id),
430 GetLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700431 // Don't redo the fallthrough branch in the BB driver
432 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700433}
434
buzbeeaad94382012-11-21 07:40:50 -0800435static void ConvertCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb,
436 MIR* mir, ConditionCode cc, RegLocation rlSrc1)
buzbee2cfc6392012-05-07 14:51:40 -0700437{
438 if (bb->taken->startOffset <= mir->offset) {
buzbee52a77fc2012-11-20 19:50:46 -0800439 EmitSuspendCheck(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -0700440 }
buzbee52a77fc2012-11-20 19:50:46 -0800441 llvm::Value* src1 = GetLLVMValue(cUnit, rlSrc1.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700442 llvm::Value* src2;
443 if (rlSrc1.ref) {
444 src2 = cUnit->irb->GetJNull();
445 } else {
446 src2 = cUnit->irb->getInt32(0);
447 }
buzbee52a77fc2012-11-20 19:50:46 -0800448 llvm::Value* condValue = ConvertCompare(cUnit, cc, src1, src2);
449 cUnit->irb->CreateCondBr(condValue, GetLLVMBlock(cUnit, bb->taken->id),
450 GetLLVMBlock(cUnit, bb->fallThrough->id));
buzbee6969d502012-06-15 16:40:31 -0700451 // Don't redo the fallthrough branch in the BB driver
452 bb->fallThrough = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700453}
454
buzbeeaad94382012-11-21 07:40:50 -0800455static llvm::Value* GenDivModOp(CompilationUnit* cUnit, bool isDiv, bool isLong,
456 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700457{
458 greenland::IntrinsicHelper::IntrinsicId id;
459 if (isLong) {
460 if (isDiv) {
461 id = greenland::IntrinsicHelper::DivLong;
462 } else {
463 id = greenland::IntrinsicHelper::RemLong;
464 }
Logan Chien554e6072012-07-23 20:00:01 -0700465 } else {
466 if (isDiv) {
buzbee2cfc6392012-05-07 14:51:40 -0700467 id = greenland::IntrinsicHelper::DivInt;
468 } else {
469 id = greenland::IntrinsicHelper::RemInt;
Logan Chien554e6072012-07-23 20:00:01 -0700470 }
buzbee2cfc6392012-05-07 14:51:40 -0700471 }
472 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
473 llvm::SmallVector<llvm::Value*, 2>args;
474 args.push_back(src1);
475 args.push_back(src2);
476 return cUnit->irb->CreateCall(intr, args);
477}
478
buzbeeaad94382012-11-21 07:40:50 -0800479static llvm::Value* GenArithOp(CompilationUnit* cUnit, OpKind op, bool isLong,
480 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700481{
482 llvm::Value* res = NULL;
483 switch(op) {
484 case kOpAdd: res = cUnit->irb->CreateAdd(src1, src2); break;
485 case kOpSub: res = cUnit->irb->CreateSub(src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700486 case kOpRsub: res = cUnit->irb->CreateSub(src2, src1); break;
buzbee2cfc6392012-05-07 14:51:40 -0700487 case kOpMul: res = cUnit->irb->CreateMul(src1, src2); break;
488 case kOpOr: res = cUnit->irb->CreateOr(src1, src2); break;
489 case kOpAnd: res = cUnit->irb->CreateAnd(src1, src2); break;
490 case kOpXor: res = cUnit->irb->CreateXor(src1, src2); break;
buzbee52a77fc2012-11-20 19:50:46 -0800491 case kOpDiv: res = GenDivModOp(cUnit, true, isLong, src1, src2); break;
492 case kOpRem: res = GenDivModOp(cUnit, false, isLong, src1, src2); break;
buzbee4f1181f2012-06-22 13:52:12 -0700493 case kOpLsl: res = cUnit->irb->CreateShl(src1, src2); break;
494 case kOpLsr: res = cUnit->irb->CreateLShr(src1, src2); break;
495 case kOpAsr: res = cUnit->irb->CreateAShr(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700496 default:
497 LOG(FATAL) << "Invalid op " << op;
498 }
499 return res;
500}
501
buzbeeaad94382012-11-21 07:40:50 -0800502static void ConvertFPArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
503 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee2cfc6392012-05-07 14:51:40 -0700504{
buzbee52a77fc2012-11-20 19:50:46 -0800505 llvm::Value* src1 = GetLLVMValue(cUnit, rlSrc1.origSReg);
506 llvm::Value* src2 = GetLLVMValue(cUnit, rlSrc2.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700507 llvm::Value* res = NULL;
508 switch(op) {
509 case kOpAdd: res = cUnit->irb->CreateFAdd(src1, src2); break;
510 case kOpSub: res = cUnit->irb->CreateFSub(src1, src2); break;
511 case kOpMul: res = cUnit->irb->CreateFMul(src1, src2); break;
512 case kOpDiv: res = cUnit->irb->CreateFDiv(src1, src2); break;
513 case kOpRem: res = cUnit->irb->CreateFRem(src1, src2); break;
514 default:
515 LOG(FATAL) << "Invalid op " << op;
516 }
buzbee52a77fc2012-11-20 19:50:46 -0800517 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700518}
519
buzbeeaad94382012-11-21 07:40:50 -0800520static void ConvertShift(CompilationUnit* cUnit, greenland::IntrinsicHelper::IntrinsicId id,
521 RegLocation rlDest, RegLocation rlSrc1, RegLocation rlSrc2)
buzbee4f1181f2012-06-22 13:52:12 -0700522{
buzbee2a83e8f2012-07-13 16:42:30 -0700523 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
524 llvm::SmallVector<llvm::Value*, 2>args;
buzbee52a77fc2012-11-20 19:50:46 -0800525 args.push_back(GetLLVMValue(cUnit, rlSrc1.origSReg));
526 args.push_back(GetLLVMValue(cUnit, rlSrc2.origSReg));
buzbee2a83e8f2012-07-13 16:42:30 -0700527 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee52a77fc2012-11-20 19:50:46 -0800528 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2a83e8f2012-07-13 16:42:30 -0700529}
530
buzbeeaad94382012-11-21 07:40:50 -0800531static void ConvertShiftLit(CompilationUnit* cUnit, greenland::IntrinsicHelper::IntrinsicId id,
532 RegLocation rlDest, RegLocation rlSrc, int shiftAmount)
buzbee2a83e8f2012-07-13 16:42:30 -0700533{
534 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
535 llvm::SmallVector<llvm::Value*, 2>args;
buzbee52a77fc2012-11-20 19:50:46 -0800536 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee2a83e8f2012-07-13 16:42:30 -0700537 args.push_back(cUnit->irb->getInt32(shiftAmount));
538 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee52a77fc2012-11-20 19:50:46 -0800539 DefineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700540}
541
buzbeeaad94382012-11-21 07:40:50 -0800542static void ConvertArithOp(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
543 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee2cfc6392012-05-07 14:51:40 -0700544{
buzbee52a77fc2012-11-20 19:50:46 -0800545 llvm::Value* src1 = GetLLVMValue(cUnit, rlSrc1.origSReg);
546 llvm::Value* src2 = GetLLVMValue(cUnit, rlSrc2.origSReg);
buzbee4f4dfc72012-07-02 14:54:44 -0700547 DCHECK_EQ(src1->getType(), src2->getType());
buzbee52a77fc2012-11-20 19:50:46 -0800548 llvm::Value* res = GenArithOp(cUnit, op, rlDest.wide, src1, src2);
549 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700550}
551
buzbeeaad94382012-11-21 07:40:50 -0800552static void SetShadowFrameEntry(CompilationUnit* cUnit, llvm::Value* newVal)
buzbeeb03f4872012-06-11 15:22:11 -0700553{
554 int index = -1;
555 DCHECK(newVal != NULL);
buzbee52a77fc2012-11-20 19:50:46 -0800556 int vReg = SRegToVReg(cUnit, GetLoc(cUnit, newVal).origSReg);
buzbeeb03f4872012-06-11 15:22:11 -0700557 for (int i = 0; i < cUnit->numShadowFrameEntries; i++) {
558 if (cUnit->shadowMap[i] == vReg) {
559 index = i;
560 break;
561 }
562 }
TDYa127347166a2012-08-23 12:23:44 -0700563 if (index == -1) {
564 return;
565 }
buzbee6459e7c2012-10-02 14:42:41 -0700566 llvm::Type* ty = newVal->getType();
buzbeeb03f4872012-06-11 15:22:11 -0700567 greenland::IntrinsicHelper::IntrinsicId id =
568 greenland::IntrinsicHelper::SetShadowFrameEntry;
569 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
570 llvm::Value* tableSlot = cUnit->irb->getInt32(index);
buzbee6459e7c2012-10-02 14:42:41 -0700571 // If newVal is a Null pointer, we'll see it here as a const int. Replace
572 if (!ty->isPointerTy()) {
573 // TODO: assert newVal created w/ dex_lang_const_int(0) or dex_lang_const_float(0)
574 newVal = cUnit->irb->GetJNull();
575 }
buzbeeb03f4872012-06-11 15:22:11 -0700576 llvm::Value* args[] = { newVal, tableSlot };
577 cUnit->irb->CreateCall(func, args);
578}
579
buzbeeaad94382012-11-21 07:40:50 -0800580static void ConvertArithOpLit(CompilationUnit* cUnit, OpKind op, RegLocation rlDest,
581 RegLocation rlSrc1, int32_t imm)
buzbee2cfc6392012-05-07 14:51:40 -0700582{
buzbee52a77fc2012-11-20 19:50:46 -0800583 llvm::Value* src1 = GetLLVMValue(cUnit, rlSrc1.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700584 llvm::Value* src2 = cUnit->irb->getInt32(imm);
buzbee52a77fc2012-11-20 19:50:46 -0800585 llvm::Value* res = GenArithOp(cUnit, op, rlDest.wide, src1, src2);
586 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700587}
588
buzbee101305f2012-06-28 18:00:56 -0700589/*
590 * Process arguments for invoke. Note: this code is also used to
591 * collect and process arguments for NEW_FILLED_ARRAY and NEW_FILLED_ARRAY_RANGE.
592 * The requirements are similar.
593 */
buzbeeaad94382012-11-21 07:40:50 -0800594static void ConvertInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
595 InvokeType invokeType, bool isRange, bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -0700596{
buzbee52a77fc2012-11-20 19:50:46 -0800597 CallInfo* info = NewMemCallInfo(cUnit, bb, mir, invokeType, isRange);
buzbee6969d502012-06-15 16:40:31 -0700598 llvm::SmallVector<llvm::Value*, 10> args;
599 // Insert the invokeType
600 args.push_back(cUnit->irb->getInt32(static_cast<int>(invokeType)));
601 // Insert the method_idx
602 args.push_back(cUnit->irb->getInt32(info->index));
603 // Insert the optimization flags
604 args.push_back(cUnit->irb->getInt32(info->optFlags));
605 // Now, insert the actual arguments
buzbee6969d502012-06-15 16:40:31 -0700606 for (int i = 0; i < info->numArgWords;) {
buzbee52a77fc2012-11-20 19:50:46 -0800607 llvm::Value* val = GetLLVMValue(cUnit, info->args[i].origSReg);
buzbee6969d502012-06-15 16:40:31 -0700608 args.push_back(val);
609 i += info->args[i].wide ? 2 : 1;
610 }
611 /*
612 * Choose the invoke return type based on actual usage. Note: may
613 * be different than shorty. For example, if a function return value
614 * is not used, we'll treat this as a void invoke.
615 */
616 greenland::IntrinsicHelper::IntrinsicId id;
buzbee76592632012-06-29 15:18:35 -0700617 if (isFilledNewArray) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700618 id = greenland::IntrinsicHelper::HLFilledNewArray;
buzbee101305f2012-06-28 18:00:56 -0700619 } else if (info->result.location == kLocInvalid) {
buzbee6969d502012-06-15 16:40:31 -0700620 id = greenland::IntrinsicHelper::HLInvokeVoid;
621 } else {
622 if (info->result.wide) {
623 if (info->result.fp) {
624 id = greenland::IntrinsicHelper::HLInvokeDouble;
625 } else {
buzbee8fa0fda2012-06-27 15:44:52 -0700626 id = greenland::IntrinsicHelper::HLInvokeLong;
buzbee6969d502012-06-15 16:40:31 -0700627 }
628 } else if (info->result.ref) {
629 id = greenland::IntrinsicHelper::HLInvokeObj;
630 } else if (info->result.fp) {
631 id = greenland::IntrinsicHelper::HLInvokeFloat;
632 } else {
633 id = greenland::IntrinsicHelper::HLInvokeInt;
634 }
635 }
636 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
637 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
638 if (info->result.location != kLocInvalid) {
buzbee52a77fc2012-11-20 19:50:46 -0800639 DefineValue(cUnit, res, info->result.origSReg);
TDYa127890ea892012-08-22 10:49:42 -0700640 if (info->result.ref) {
buzbee52a77fc2012-11-20 19:50:46 -0800641 SetShadowFrameEntry(cUnit, reinterpret_cast<llvm::Value*>
buzbeecbd6d442012-11-17 14:11:25 -0800642 (cUnit->llvmValues.elemList[info->result.origSReg]));
TDYa127890ea892012-08-22 10:49:42 -0700643 }
buzbee6969d502012-06-15 16:40:31 -0700644 }
645}
646
buzbeeaad94382012-11-21 07:40:50 -0800647static void ConvertConstObject(CompilationUnit* cUnit, uint32_t idx,
648 greenland::IntrinsicHelper::IntrinsicId id, RegLocation rlDest)
buzbee6969d502012-06-15 16:40:31 -0700649{
buzbee6969d502012-06-15 16:40:31 -0700650 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee101305f2012-06-28 18:00:56 -0700651 llvm::Value* index = cUnit->irb->getInt32(idx);
buzbee6969d502012-06-15 16:40:31 -0700652 llvm::Value* res = cUnit->irb->CreateCall(intr, index);
buzbee52a77fc2012-11-20 19:50:46 -0800653 DefineValue(cUnit, res, rlDest.origSReg);
buzbee6969d502012-06-15 16:40:31 -0700654}
655
buzbeeaad94382012-11-21 07:40:50 -0800656static void ConvertCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee101305f2012-06-28 18:00:56 -0700657{
658 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700659 id = greenland::IntrinsicHelper::HLCheckCast;
buzbee101305f2012-06-28 18:00:56 -0700660 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
661 llvm::SmallVector<llvm::Value*, 2> args;
662 args.push_back(cUnit->irb->getInt32(type_idx));
buzbee52a77fc2012-11-20 19:50:46 -0800663 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee101305f2012-06-28 18:00:56 -0700664 cUnit->irb->CreateCall(intr, args);
665}
666
buzbeeaad94382012-11-21 07:40:50 -0800667static void ConvertNewInstance(CompilationUnit* cUnit, uint32_t type_idx, 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);
buzbee52a77fc2012-11-20 19:50:46 -0800674 DefineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700675}
676
buzbeeaad94382012-11-21 07:40:50 -0800677static void ConvertNewArray(CompilationUnit* cUnit, uint32_t type_idx,
678 RegLocation rlDest, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700679{
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));
buzbee52a77fc2012-11-20 19:50:46 -0800685 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700686 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee52a77fc2012-11-20 19:50:46 -0800687 DefineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700688}
689
buzbeeaad94382012-11-21 07:40:50 -0800690static void ConvertAget(CompilationUnit* cUnit, int optFlags,
691 greenland::IntrinsicHelper::IntrinsicId id,
692 RegLocation rlDest, RegLocation rlArray, RegLocation rlIndex)
buzbee8fa0fda2012-06-27 15:44:52 -0700693{
694 llvm::SmallVector<llvm::Value*, 3> args;
695 args.push_back(cUnit->irb->getInt32(optFlags));
buzbee52a77fc2012-11-20 19:50:46 -0800696 args.push_back(GetLLVMValue(cUnit, rlArray.origSReg));
697 args.push_back(GetLLVMValue(cUnit, rlIndex.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700698 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
699 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee52a77fc2012-11-20 19:50:46 -0800700 DefineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700701}
702
buzbeeaad94382012-11-21 07:40:50 -0800703static void ConvertAput(CompilationUnit* cUnit, int optFlags,
704 greenland::IntrinsicHelper::IntrinsicId id,
705 RegLocation rlSrc, RegLocation rlArray, RegLocation rlIndex)
buzbee8fa0fda2012-06-27 15:44:52 -0700706{
707 llvm::SmallVector<llvm::Value*, 4> args;
708 args.push_back(cUnit->irb->getInt32(optFlags));
buzbee52a77fc2012-11-20 19:50:46 -0800709 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
710 args.push_back(GetLLVMValue(cUnit, rlArray.origSReg));
711 args.push_back(GetLLVMValue(cUnit, rlIndex.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700712 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
713 cUnit->irb->CreateCall(intr, args);
714}
715
buzbeeaad94382012-11-21 07:40:50 -0800716static void ConvertIget(CompilationUnit* cUnit, int optFlags,
717 greenland::IntrinsicHelper::IntrinsicId id,
718 RegLocation rlDest, RegLocation rlObj, int fieldIndex)
buzbee101305f2012-06-28 18:00:56 -0700719{
720 llvm::SmallVector<llvm::Value*, 3> args;
721 args.push_back(cUnit->irb->getInt32(optFlags));
buzbee52a77fc2012-11-20 19:50:46 -0800722 args.push_back(GetLLVMValue(cUnit, rlObj.origSReg));
buzbee101305f2012-06-28 18:00:56 -0700723 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);
buzbee52a77fc2012-11-20 19:50:46 -0800726 DefineValue(cUnit, res, rlDest.origSReg);
buzbee101305f2012-06-28 18:00:56 -0700727}
728
buzbeeaad94382012-11-21 07:40:50 -0800729static void ConvertIput(CompilationUnit* cUnit, int optFlags,
730 greenland::IntrinsicHelper::IntrinsicId id,
731 RegLocation rlSrc, RegLocation rlObj, int fieldIndex)
buzbee101305f2012-06-28 18:00:56 -0700732{
733 llvm::SmallVector<llvm::Value*, 4> args;
734 args.push_back(cUnit->irb->getInt32(optFlags));
buzbee52a77fc2012-11-20 19:50:46 -0800735 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
736 args.push_back(GetLLVMValue(cUnit, rlObj.origSReg));
buzbee101305f2012-06-28 18:00:56 -0700737 args.push_back(cUnit->irb->getInt32(fieldIndex));
738 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
739 cUnit->irb->CreateCall(intr, args);
740}
741
buzbeeaad94382012-11-21 07:40:50 -0800742static void ConvertInstanceOf(CompilationUnit* cUnit, uint32_t type_idx,
743 RegLocation rlDest, RegLocation rlSrc)
buzbee8fa0fda2012-06-27 15:44:52 -0700744{
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));
buzbee52a77fc2012-11-20 19:50:46 -0800750 args.push_back(GetLLVMValue(cUnit, rlSrc.origSReg));
buzbee8fa0fda2012-06-27 15:44:52 -0700751 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee52a77fc2012-11-20 19:50:46 -0800752 DefineValue(cUnit, res, rlDest.origSReg);
buzbee8fa0fda2012-06-27 15:44:52 -0700753}
754
buzbeeaad94382012-11-21 07:40:50 -0800755static void ConvertIntToLong(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee101305f2012-06-28 18:00:56 -0700756{
buzbee52a77fc2012-11-20 19:50:46 -0800757 llvm::Value* res = cUnit->irb->CreateSExt(GetLLVMValue(cUnit, rlSrc.origSReg),
buzbee101305f2012-06-28 18:00:56 -0700758 cUnit->irb->getInt64Ty());
buzbee52a77fc2012-11-20 19:50:46 -0800759 DefineValue(cUnit, res, rlDest.origSReg);
buzbee101305f2012-06-28 18:00:56 -0700760}
761
buzbeeaad94382012-11-21 07:40:50 -0800762static void ConvertLongToInt(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700763{
buzbee52a77fc2012-11-20 19:50:46 -0800764 llvm::Value* src = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbee76592632012-06-29 15:18:35 -0700765 llvm::Value* res = cUnit->irb->CreateTrunc(src, cUnit->irb->getInt32Ty());
buzbee52a77fc2012-11-20 19:50:46 -0800766 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700767}
768
buzbeeaad94382012-11-21 07:40:50 -0800769static void ConvertFloatToDouble(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700770{
buzbee52a77fc2012-11-20 19:50:46 -0800771 llvm::Value* src = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbee76592632012-06-29 15:18:35 -0700772 llvm::Value* res = cUnit->irb->CreateFPExt(src, cUnit->irb->getDoubleTy());
buzbee52a77fc2012-11-20 19:50:46 -0800773 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700774}
775
buzbeeaad94382012-11-21 07:40:50 -0800776static void ConvertDoubleToFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700777{
buzbee52a77fc2012-11-20 19:50:46 -0800778 llvm::Value* src = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbee76592632012-06-29 15:18:35 -0700779 llvm::Value* res = cUnit->irb->CreateFPTrunc(src, cUnit->irb->getFloatTy());
buzbee52a77fc2012-11-20 19:50:46 -0800780 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700781}
782
buzbeeaad94382012-11-21 07:40:50 -0800783static void ConvertWideComparison(CompilationUnit* cUnit,
784 greenland::IntrinsicHelper::IntrinsicId id,
785 RegLocation rlDest, RegLocation rlSrc1,
buzbee76592632012-06-29 15:18:35 -0700786 RegLocation rlSrc2)
787{
788 DCHECK_EQ(rlSrc1.fp, rlSrc2.fp);
789 DCHECK_EQ(rlSrc1.wide, rlSrc2.wide);
790 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
791 llvm::SmallVector<llvm::Value*, 2> args;
buzbee52a77fc2012-11-20 19:50:46 -0800792 args.push_back(GetLLVMValue(cUnit, rlSrc1.origSReg));
793 args.push_back(GetLLVMValue(cUnit, rlSrc2.origSReg));
buzbee76592632012-06-29 15:18:35 -0700794 llvm::Value* res = cUnit->irb->CreateCall(intr, args);
buzbee52a77fc2012-11-20 19:50:46 -0800795 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700796}
797
buzbeeaad94382012-11-21 07:40:50 -0800798static void ConvertIntNarrowing(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc,
799 greenland::IntrinsicHelper::IntrinsicId id)
buzbee101305f2012-06-28 18:00:56 -0700800{
801 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700802 llvm::Value* res =
buzbee52a77fc2012-11-20 19:50:46 -0800803 cUnit->irb->CreateCall(intr, GetLLVMValue(cUnit, rlSrc.origSReg));
804 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700805}
806
buzbeeaad94382012-11-21 07:40:50 -0800807static void ConvertNeg(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700808{
buzbee52a77fc2012-11-20 19:50:46 -0800809 llvm::Value* res = cUnit->irb->CreateNeg(GetLLVMValue(cUnit, rlSrc.origSReg));
810 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700811}
812
buzbeeaad94382012-11-21 07:40:50 -0800813static void ConvertIntToFP(CompilationUnit* cUnit, llvm::Type* ty, RegLocation rlDest,
814 RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700815{
816 llvm::Value* res =
buzbee52a77fc2012-11-20 19:50:46 -0800817 cUnit->irb->CreateSIToFP(GetLLVMValue(cUnit, rlSrc.origSReg), ty);
818 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700819}
820
buzbeeaad94382012-11-21 07:40:50 -0800821static void ConvertFPToInt(CompilationUnit* cUnit, greenland::IntrinsicHelper::IntrinsicId id,
822 RegLocation rlDest,
buzbee76592632012-06-29 15:18:35 -0700823 RegLocation rlSrc)
824{
TDYa1274ec8ccd2012-08-11 07:04:57 -0700825 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
buzbee52a77fc2012-11-20 19:50:46 -0800826 llvm::Value* res = cUnit->irb->CreateCall(intr, GetLLVMValue(cUnit, rlSrc.origSReg));
827 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700828}
829
830
buzbeeaad94382012-11-21 07:40:50 -0800831static void ConvertNegFP(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700832{
833 llvm::Value* res =
buzbee52a77fc2012-11-20 19:50:46 -0800834 cUnit->irb->CreateFNeg(GetLLVMValue(cUnit, rlSrc.origSReg));
835 DefineValue(cUnit, res, rlDest.origSReg);
buzbee76592632012-06-29 15:18:35 -0700836}
837
buzbeeaad94382012-11-21 07:40:50 -0800838static void ConvertNot(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee76592632012-06-29 15:18:35 -0700839{
buzbee52a77fc2012-11-20 19:50:46 -0800840 llvm::Value* src = GetLLVMValue(cUnit, rlSrc.origSReg);
buzbee76592632012-06-29 15:18:35 -0700841 llvm::Value* res = cUnit->irb->CreateXor(src, static_cast<uint64_t>(-1));
buzbee52a77fc2012-11-20 19:50:46 -0800842 DefineValue(cUnit, res, rlDest.origSReg);
buzbee101305f2012-06-28 18:00:56 -0700843}
844
buzbee2cfc6392012-05-07 14:51:40 -0700845/*
846 * Target-independent code generation. Use only high-level
847 * load/store utilities here, or target-dependent genXX() handlers
848 * when necessary.
849 */
buzbeeaad94382012-11-21 07:40:50 -0800850static bool ConvertMIRNode(CompilationUnit* cUnit, MIR* mir, BasicBlock* bb,
851 llvm::BasicBlock* llvmBB, LIR* labelList)
buzbee2cfc6392012-05-07 14:51:40 -0700852{
853 bool res = false; // Assume success
854 RegLocation rlSrc[3];
855 RegLocation rlDest = badLoc;
buzbee2cfc6392012-05-07 14:51:40 -0700856 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbeecbd6d442012-11-17 14:11:25 -0800857 int opVal = opcode;
buzbee6969d502012-06-15 16:40:31 -0700858 uint32_t vB = mir->dalvikInsn.vB;
859 uint32_t vC = mir->dalvikInsn.vC;
buzbee8fa0fda2012-06-27 15:44:52 -0700860 int optFlags = mir->optimizationFlags;
buzbee6969d502012-06-15 16:40:31 -0700861
buzbeeb03f4872012-06-11 15:22:11 -0700862 bool objectDefinition = false;
buzbee2cfc6392012-05-07 14:51:40 -0700863
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700864 if (cUnit->printMe) {
buzbeecbd6d442012-11-17 14:11:25 -0800865 if (opVal < kMirOpFirst) {
866 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x" << std::hex << opVal;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700867 } else {
buzbeecbd6d442012-11-17 14:11:25 -0800868 LOG(INFO) << extendedMIROpNames[opVal - kMirOpFirst] << " 0x" << std::hex << opVal;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700869 }
870 }
871
buzbee2cfc6392012-05-07 14:51:40 -0700872 /* Prep Src and Dest locations */
873 int nextSreg = 0;
874 int nextLoc = 0;
875 int attrs = oatDataFlowAttributes[opcode];
876 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
877 if (attrs & DF_UA) {
878 if (attrs & DF_A_WIDE) {
buzbee52a77fc2012-11-20 19:50:46 -0800879 rlSrc[nextLoc++] = GetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700880 nextSreg+= 2;
881 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800882 rlSrc[nextLoc++] = GetSrc(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700883 nextSreg++;
884 }
885 }
886 if (attrs & DF_UB) {
887 if (attrs & DF_B_WIDE) {
buzbee52a77fc2012-11-20 19:50:46 -0800888 rlSrc[nextLoc++] = GetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700889 nextSreg+= 2;
890 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800891 rlSrc[nextLoc++] = GetSrc(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700892 nextSreg++;
893 }
894 }
895 if (attrs & DF_UC) {
896 if (attrs & DF_C_WIDE) {
buzbee52a77fc2012-11-20 19:50:46 -0800897 rlSrc[nextLoc++] = GetSrcWide(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700898 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800899 rlSrc[nextLoc++] = GetSrc(cUnit, mir, nextSreg);
buzbee2cfc6392012-05-07 14:51:40 -0700900 }
901 }
902 if (attrs & DF_DA) {
903 if (attrs & DF_A_WIDE) {
buzbee52a77fc2012-11-20 19:50:46 -0800904 rlDest = GetDestWide(cUnit, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700905 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800906 rlDest = GetDest(cUnit, mir);
buzbeeb03f4872012-06-11 15:22:11 -0700907 if (rlDest.ref) {
908 objectDefinition = true;
909 }
buzbee2cfc6392012-05-07 14:51:40 -0700910 }
911 }
912
913 switch (opcode) {
914 case Instruction::NOP:
915 break;
916
917 case Instruction::MOVE:
918 case Instruction::MOVE_OBJECT:
919 case Instruction::MOVE_16:
920 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700921 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700922 case Instruction::MOVE_FROM16:
923 case Instruction::MOVE_WIDE:
924 case Instruction::MOVE_WIDE_16:
925 case Instruction::MOVE_WIDE_FROM16: {
926 /*
927 * Moves/copies are meaningless in pure SSA register form,
928 * but we need to preserve them for the conversion back into
929 * MIR (at least until we stop using the Dalvik register maps).
930 * Insert a dummy intrinsic copy call, which will be recognized
931 * by the quick path and removed by the portable path.
932 */
buzbee52a77fc2012-11-20 19:50:46 -0800933 llvm::Value* src = GetLLVMValue(cUnit, rlSrc[0].origSReg);
934 llvm::Value* res = EmitCopy(cUnit, src, rlDest);
935 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700936 }
937 break;
938
939 case Instruction::CONST:
940 case Instruction::CONST_4:
941 case Instruction::CONST_16: {
TDYa127347166a2012-08-23 12:23:44 -0700942 if (vB == 0) {
943 objectDefinition = true;
944 }
buzbee6969d502012-06-15 16:40:31 -0700945 llvm::Constant* immValue = cUnit->irb->GetJInt(vB);
buzbee52a77fc2012-11-20 19:50:46 -0800946 llvm::Value* res = EmitConst(cUnit, immValue, rlDest);
947 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700948 }
949 break;
950
951 case Instruction::CONST_WIDE_16:
952 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700953 // Sign extend to 64 bits
954 int64_t imm = static_cast<int32_t>(vB);
955 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
buzbee52a77fc2012-11-20 19:50:46 -0800956 llvm::Value* res = EmitConst(cUnit, immValue, rlDest);
957 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700958 }
959 break;
960
961 case Instruction::CONST_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700962 llvm::Constant* immValue = cUnit->irb->GetJInt(vB << 16);
buzbee52a77fc2012-11-20 19:50:46 -0800963 llvm::Value* res = EmitConst(cUnit, immValue, rlDest);
964 DefineValue(cUnit, res, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -0700965 }
966 break;
967
968 case Instruction::CONST_WIDE: {
969 llvm::Constant* immValue =
970 cUnit->irb->GetJLong(mir->dalvikInsn.vB_wide);
buzbee52a77fc2012-11-20 19:50:46 -0800971 llvm::Value* res = EmitConst(cUnit, immValue, rlDest);
972 DefineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700973 }
974 break;
buzbee2cfc6392012-05-07 14:51:40 -0700975 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700976 int64_t imm = static_cast<int64_t>(vB) << 48;
buzbee2cfc6392012-05-07 14:51:40 -0700977 llvm::Constant* immValue = cUnit->irb->GetJLong(imm);
buzbee52a77fc2012-11-20 19:50:46 -0800978 llvm::Value* res = EmitConst(cUnit, immValue, rlDest);
979 DefineValue(cUnit, res, rlDest.origSReg);
buzbee4f1181f2012-06-22 13:52:12 -0700980 }
981 break;
982
buzbee8fa0fda2012-06-27 15:44:52 -0700983 case Instruction::SPUT_OBJECT:
buzbee52a77fc2012-11-20 19:50:46 -0800984 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputObject,
buzbee8fa0fda2012-06-27 15:44:52 -0700985 rlSrc[0]);
986 break;
987 case Instruction::SPUT:
988 if (rlSrc[0].fp) {
buzbee52a77fc2012-11-20 19:50:46 -0800989 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputFloat,
buzbee8fa0fda2012-06-27 15:44:52 -0700990 rlSrc[0]);
991 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800992 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSput, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700993 }
994 break;
995 case Instruction::SPUT_BOOLEAN:
buzbee52a77fc2012-11-20 19:50:46 -0800996 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputBoolean,
buzbee8fa0fda2012-06-27 15:44:52 -0700997 rlSrc[0]);
998 break;
999 case Instruction::SPUT_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001000 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputByte, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001001 break;
1002 case Instruction::SPUT_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001003 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputChar, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001004 break;
1005 case Instruction::SPUT_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001006 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputShort, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001007 break;
1008 case Instruction::SPUT_WIDE:
1009 if (rlSrc[0].fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001010 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputDouble,
buzbee8fa0fda2012-06-27 15:44:52 -07001011 rlSrc[0]);
1012 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001013 ConvertSput(cUnit, vB, greenland::IntrinsicHelper::HLSputWide,
buzbee8fa0fda2012-06-27 15:44:52 -07001014 rlSrc[0]);
1015 }
1016 break;
1017
1018 case Instruction::SGET_OBJECT:
buzbee52a77fc2012-11-20 19:50:46 -08001019 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetObject, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001020 break;
1021 case Instruction::SGET:
1022 if (rlDest.fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001023 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetFloat, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001024 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001025 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSget, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001026 }
1027 break;
1028 case Instruction::SGET_BOOLEAN:
buzbee52a77fc2012-11-20 19:50:46 -08001029 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetBoolean, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001030 break;
1031 case Instruction::SGET_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001032 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetByte, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001033 break;
1034 case Instruction::SGET_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001035 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetChar, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001036 break;
1037 case Instruction::SGET_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001038 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetShort, rlDest);
buzbee8fa0fda2012-06-27 15:44:52 -07001039 break;
1040 case Instruction::SGET_WIDE:
1041 if (rlDest.fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001042 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetDouble,
buzbee8fa0fda2012-06-27 15:44:52 -07001043 rlDest);
1044 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001045 ConvertSget(cUnit, vB, greenland::IntrinsicHelper::HLSgetWide, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001046 }
1047 break;
buzbee2cfc6392012-05-07 14:51:40 -07001048
1049 case Instruction::RETURN_WIDE:
1050 case Instruction::RETURN:
1051 case Instruction::RETURN_OBJECT: {
TDYa1274f2935e2012-06-22 06:25:03 -07001052 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee52a77fc2012-11-20 19:50:46 -08001053 EmitSuspendCheck(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001054 }
buzbee52a77fc2012-11-20 19:50:46 -08001055 EmitPopShadowFrame(cUnit);
1056 cUnit->irb->CreateRet(GetLLVMValue(cUnit, rlSrc[0].origSReg));
buzbee2cfc6392012-05-07 14:51:40 -07001057 bb->hasReturn = true;
1058 }
1059 break;
1060
1061 case Instruction::RETURN_VOID: {
TDYa1274f2935e2012-06-22 06:25:03 -07001062 if (!(cUnit->attrs & METHOD_IS_LEAF)) {
buzbee52a77fc2012-11-20 19:50:46 -08001063 EmitSuspendCheck(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001064 }
buzbee52a77fc2012-11-20 19:50:46 -08001065 EmitPopShadowFrame(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001066 cUnit->irb->CreateRetVoid();
1067 bb->hasReturn = true;
1068 }
1069 break;
1070
1071 case Instruction::IF_EQ:
buzbee52a77fc2012-11-20 19:50:46 -08001072 ConvertCompareAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001073 break;
1074 case Instruction::IF_NE:
buzbee52a77fc2012-11-20 19:50:46 -08001075 ConvertCompareAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001076 break;
1077 case Instruction::IF_LT:
buzbee52a77fc2012-11-20 19:50:46 -08001078 ConvertCompareAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001079 break;
1080 case Instruction::IF_GE:
buzbee52a77fc2012-11-20 19:50:46 -08001081 ConvertCompareAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001082 break;
1083 case Instruction::IF_GT:
buzbee52a77fc2012-11-20 19:50:46 -08001084 ConvertCompareAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001085 break;
1086 case Instruction::IF_LE:
buzbee52a77fc2012-11-20 19:50:46 -08001087 ConvertCompareAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001088 break;
1089 case Instruction::IF_EQZ:
buzbee52a77fc2012-11-20 19:50:46 -08001090 ConvertCompareZeroAndBranch(cUnit, bb, mir, kCondEq, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001091 break;
1092 case Instruction::IF_NEZ:
buzbee52a77fc2012-11-20 19:50:46 -08001093 ConvertCompareZeroAndBranch(cUnit, bb, mir, kCondNe, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001094 break;
1095 case Instruction::IF_LTZ:
buzbee52a77fc2012-11-20 19:50:46 -08001096 ConvertCompareZeroAndBranch(cUnit, bb, mir, kCondLt, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001097 break;
1098 case Instruction::IF_GEZ:
buzbee52a77fc2012-11-20 19:50:46 -08001099 ConvertCompareZeroAndBranch(cUnit, bb, mir, kCondGe, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001100 break;
1101 case Instruction::IF_GTZ:
buzbee52a77fc2012-11-20 19:50:46 -08001102 ConvertCompareZeroAndBranch(cUnit, bb, mir, kCondGt, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001103 break;
1104 case Instruction::IF_LEZ:
buzbee52a77fc2012-11-20 19:50:46 -08001105 ConvertCompareZeroAndBranch(cUnit, bb, mir, kCondLe, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001106 break;
1107
1108 case Instruction::GOTO:
1109 case Instruction::GOTO_16:
1110 case Instruction::GOTO_32: {
1111 if (bb->taken->startOffset <= bb->startOffset) {
buzbee52a77fc2012-11-20 19:50:46 -08001112 EmitSuspendCheck(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001113 }
buzbee52a77fc2012-11-20 19:50:46 -08001114 cUnit->irb->CreateBr(GetLLVMBlock(cUnit, bb->taken->id));
buzbee2cfc6392012-05-07 14:51:40 -07001115 }
1116 break;
1117
1118 case Instruction::ADD_LONG:
1119 case Instruction::ADD_LONG_2ADDR:
1120 case Instruction::ADD_INT:
1121 case Instruction::ADD_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001122 ConvertArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001123 break;
1124 case Instruction::SUB_LONG:
1125 case Instruction::SUB_LONG_2ADDR:
1126 case Instruction::SUB_INT:
1127 case Instruction::SUB_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001128 ConvertArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001129 break;
1130 case Instruction::MUL_LONG:
1131 case Instruction::MUL_LONG_2ADDR:
1132 case Instruction::MUL_INT:
1133 case Instruction::MUL_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001134 ConvertArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001135 break;
1136 case Instruction::DIV_LONG:
1137 case Instruction::DIV_LONG_2ADDR:
1138 case Instruction::DIV_INT:
1139 case Instruction::DIV_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001140 ConvertArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001141 break;
1142 case Instruction::REM_LONG:
1143 case Instruction::REM_LONG_2ADDR:
1144 case Instruction::REM_INT:
1145 case Instruction::REM_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001146 ConvertArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001147 break;
1148 case Instruction::AND_LONG:
1149 case Instruction::AND_LONG_2ADDR:
1150 case Instruction::AND_INT:
1151 case Instruction::AND_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001152 ConvertArithOp(cUnit, kOpAnd, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001153 break;
1154 case Instruction::OR_LONG:
1155 case Instruction::OR_LONG_2ADDR:
1156 case Instruction::OR_INT:
1157 case Instruction::OR_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001158 ConvertArithOp(cUnit, kOpOr, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001159 break;
1160 case Instruction::XOR_LONG:
1161 case Instruction::XOR_LONG_2ADDR:
1162 case Instruction::XOR_INT:
1163 case Instruction::XOR_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001164 ConvertArithOp(cUnit, kOpXor, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001165 break;
1166 case Instruction::SHL_LONG:
1167 case Instruction::SHL_LONG_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001168 ConvertShift(cUnit, greenland::IntrinsicHelper::SHLLong,
buzbee2a83e8f2012-07-13 16:42:30 -07001169 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001170 break;
buzbee2cfc6392012-05-07 14:51:40 -07001171 case Instruction::SHL_INT:
1172 case Instruction::SHL_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001173 ConvertShift(cUnit, greenland::IntrinsicHelper::SHLInt,
buzbee2a83e8f2012-07-13 16:42:30 -07001174 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001175 break;
1176 case Instruction::SHR_LONG:
1177 case Instruction::SHR_LONG_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001178 ConvertShift(cUnit, greenland::IntrinsicHelper::SHRLong,
buzbee2a83e8f2012-07-13 16:42:30 -07001179 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001180 break;
buzbee2cfc6392012-05-07 14:51:40 -07001181 case Instruction::SHR_INT:
1182 case Instruction::SHR_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001183 ConvertShift(cUnit, greenland::IntrinsicHelper::SHRInt,
buzbee2a83e8f2012-07-13 16:42:30 -07001184 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001185 break;
1186 case Instruction::USHR_LONG:
1187 case Instruction::USHR_LONG_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001188 ConvertShift(cUnit, greenland::IntrinsicHelper::USHRLong,
buzbee2a83e8f2012-07-13 16:42:30 -07001189 rlDest, rlSrc[0], rlSrc[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001190 break;
buzbee2cfc6392012-05-07 14:51:40 -07001191 case Instruction::USHR_INT:
1192 case Instruction::USHR_INT_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001193 ConvertShift(cUnit, greenland::IntrinsicHelper::USHRInt,
buzbee2a83e8f2012-07-13 16:42:30 -07001194 rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001195 break;
1196
1197 case Instruction::ADD_INT_LIT16:
1198 case Instruction::ADD_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001199 ConvertArithOpLit(cUnit, kOpAdd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001200 break;
1201 case Instruction::RSUB_INT:
1202 case Instruction::RSUB_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001203 ConvertArithOpLit(cUnit, kOpRsub, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001204 break;
1205 case Instruction::MUL_INT_LIT16:
1206 case Instruction::MUL_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001207 ConvertArithOpLit(cUnit, kOpMul, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001208 break;
1209 case Instruction::DIV_INT_LIT16:
1210 case Instruction::DIV_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001211 ConvertArithOpLit(cUnit, kOpDiv, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001212 break;
1213 case Instruction::REM_INT_LIT16:
1214 case Instruction::REM_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001215 ConvertArithOpLit(cUnit, kOpRem, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001216 break;
1217 case Instruction::AND_INT_LIT16:
1218 case Instruction::AND_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001219 ConvertArithOpLit(cUnit, kOpAnd, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001220 break;
1221 case Instruction::OR_INT_LIT16:
1222 case Instruction::OR_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001223 ConvertArithOpLit(cUnit, kOpOr, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001224 break;
1225 case Instruction::XOR_INT_LIT16:
1226 case Instruction::XOR_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001227 ConvertArithOpLit(cUnit, kOpXor, rlDest, rlSrc[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001228 break;
1229 case Instruction::SHL_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001230 ConvertShiftLit(cUnit, greenland::IntrinsicHelper::SHLInt,
buzbee2a83e8f2012-07-13 16:42:30 -07001231 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001232 break;
1233 case Instruction::SHR_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001234 ConvertShiftLit(cUnit, greenland::IntrinsicHelper::SHRInt,
buzbee2a83e8f2012-07-13 16:42:30 -07001235 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001236 break;
1237 case Instruction::USHR_INT_LIT8:
buzbee52a77fc2012-11-20 19:50:46 -08001238 ConvertShiftLit(cUnit, greenland::IntrinsicHelper::USHRInt,
buzbee2a83e8f2012-07-13 16:42:30 -07001239 rlDest, rlSrc[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001240 break;
1241
1242 case Instruction::ADD_FLOAT:
1243 case Instruction::ADD_FLOAT_2ADDR:
1244 case Instruction::ADD_DOUBLE:
1245 case Instruction::ADD_DOUBLE_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001246 ConvertFPArithOp(cUnit, kOpAdd, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001247 break;
1248
1249 case Instruction::SUB_FLOAT:
1250 case Instruction::SUB_FLOAT_2ADDR:
1251 case Instruction::SUB_DOUBLE:
1252 case Instruction::SUB_DOUBLE_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001253 ConvertFPArithOp(cUnit, kOpSub, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001254 break;
1255
1256 case Instruction::MUL_FLOAT:
1257 case Instruction::MUL_FLOAT_2ADDR:
1258 case Instruction::MUL_DOUBLE:
1259 case Instruction::MUL_DOUBLE_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001260 ConvertFPArithOp(cUnit, kOpMul, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001261 break;
1262
1263 case Instruction::DIV_FLOAT:
1264 case Instruction::DIV_FLOAT_2ADDR:
1265 case Instruction::DIV_DOUBLE:
1266 case Instruction::DIV_DOUBLE_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001267 ConvertFPArithOp(cUnit, kOpDiv, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001268 break;
1269
1270 case Instruction::REM_FLOAT:
1271 case Instruction::REM_FLOAT_2ADDR:
1272 case Instruction::REM_DOUBLE:
1273 case Instruction::REM_DOUBLE_2ADDR:
buzbee52a77fc2012-11-20 19:50:46 -08001274 ConvertFPArithOp(cUnit, kOpRem, rlDest, rlSrc[0], rlSrc[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001275 break;
1276
buzbee6969d502012-06-15 16:40:31 -07001277 case Instruction::INVOKE_STATIC:
buzbee52a77fc2012-11-20 19:50:46 -08001278 ConvertInvoke(cUnit, bb, mir, kStatic, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001279 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001280 break;
1281 case Instruction::INVOKE_STATIC_RANGE:
buzbee52a77fc2012-11-20 19:50:46 -08001282 ConvertInvoke(cUnit, bb, mir, kStatic, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001283 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001284 break;
1285
1286 case Instruction::INVOKE_DIRECT:
buzbee52a77fc2012-11-20 19:50:46 -08001287 ConvertInvoke(cUnit, bb, mir, kDirect, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001288 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001289 break;
1290 case Instruction::INVOKE_DIRECT_RANGE:
buzbee52a77fc2012-11-20 19:50:46 -08001291 ConvertInvoke(cUnit, bb, mir, kDirect, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001292 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001293 break;
1294
1295 case Instruction::INVOKE_VIRTUAL:
buzbee52a77fc2012-11-20 19:50:46 -08001296 ConvertInvoke(cUnit, bb, mir, kVirtual, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001297 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001298 break;
1299 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee52a77fc2012-11-20 19:50:46 -08001300 ConvertInvoke(cUnit, bb, mir, kVirtual, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001301 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001302 break;
1303
1304 case Instruction::INVOKE_SUPER:
buzbee52a77fc2012-11-20 19:50:46 -08001305 ConvertInvoke(cUnit, bb, mir, kSuper, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001306 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001307 break;
1308 case Instruction::INVOKE_SUPER_RANGE:
buzbee52a77fc2012-11-20 19:50:46 -08001309 ConvertInvoke(cUnit, bb, mir, kSuper, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001310 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001311 break;
1312
1313 case Instruction::INVOKE_INTERFACE:
buzbee52a77fc2012-11-20 19:50:46 -08001314 ConvertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001315 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001316 break;
1317 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee52a77fc2012-11-20 19:50:46 -08001318 ConvertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001319 false /* NewFilledArray */);
1320 break;
1321 case Instruction::FILLED_NEW_ARRAY:
buzbee52a77fc2012-11-20 19:50:46 -08001322 ConvertInvoke(cUnit, bb, mir, kInterface, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001323 true /* NewFilledArray */);
1324 break;
1325 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbee52a77fc2012-11-20 19:50:46 -08001326 ConvertInvoke(cUnit, bb, mir, kInterface, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001327 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001328 break;
1329
1330 case Instruction::CONST_STRING:
1331 case Instruction::CONST_STRING_JUMBO:
buzbee52a77fc2012-11-20 19:50:46 -08001332 ConvertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstString,
buzbee101305f2012-06-28 18:00:56 -07001333 rlDest);
1334 break;
1335
1336 case Instruction::CONST_CLASS:
buzbee52a77fc2012-11-20 19:50:46 -08001337 ConvertConstObject(cUnit, vB, greenland::IntrinsicHelper::ConstClass,
buzbee101305f2012-06-28 18:00:56 -07001338 rlDest);
1339 break;
1340
1341 case Instruction::CHECK_CAST:
buzbee52a77fc2012-11-20 19:50:46 -08001342 ConvertCheckCast(cUnit, vB, rlSrc[0]);
buzbee6969d502012-06-15 16:40:31 -07001343 break;
1344
buzbee4f1181f2012-06-22 13:52:12 -07001345 case Instruction::NEW_INSTANCE:
buzbee52a77fc2012-11-20 19:50:46 -08001346 ConvertNewInstance(cUnit, vB, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07001347 break;
1348
buzbee32412962012-06-26 16:27:56 -07001349 case Instruction::MOVE_EXCEPTION:
buzbee52a77fc2012-11-20 19:50:46 -08001350 ConvertMoveException(cUnit, rlDest);
buzbee32412962012-06-26 16:27:56 -07001351 break;
1352
1353 case Instruction::THROW:
buzbee52a77fc2012-11-20 19:50:46 -08001354 ConvertThrow(cUnit, rlSrc[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001355 /*
1356 * If this throw is standalone, terminate.
1357 * If it might rethrow, force termination
1358 * of the following block.
1359 */
1360 if (bb->fallThrough == NULL) {
1361 cUnit->irb->CreateUnreachable();
1362 } else {
1363 bb->fallThrough->fallThrough = NULL;
1364 bb->fallThrough->taken = NULL;
1365 }
buzbee32412962012-06-26 16:27:56 -07001366 break;
1367
buzbee2cfc6392012-05-07 14:51:40 -07001368 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001369 case Instruction::MOVE_RESULT:
1370 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001371 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001372 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001373 */
jeffhao9a4f0032012-08-30 16:17:40 -07001374 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001375 break;
1376
1377 case Instruction::MONITOR_ENTER:
buzbee52a77fc2012-11-20 19:50:46 -08001378 ConvertMonitorEnterExit(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001379 greenland::IntrinsicHelper::MonitorEnter,
1380 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001381 break;
1382
1383 case Instruction::MONITOR_EXIT:
buzbee52a77fc2012-11-20 19:50:46 -08001384 ConvertMonitorEnterExit(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001385 greenland::IntrinsicHelper::MonitorExit,
1386 rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001387 break;
1388
1389 case Instruction::ARRAY_LENGTH:
buzbee52a77fc2012-11-20 19:50:46 -08001390 ConvertArrayLength(cUnit, optFlags, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001391 break;
1392
1393 case Instruction::NEW_ARRAY:
buzbee52a77fc2012-11-20 19:50:46 -08001394 ConvertNewArray(cUnit, vC, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001395 break;
1396
1397 case Instruction::INSTANCE_OF:
buzbee52a77fc2012-11-20 19:50:46 -08001398 ConvertInstanceOf(cUnit, vC, rlDest, rlSrc[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001399 break;
1400
1401 case Instruction::AGET:
1402 if (rlDest.fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001403 ConvertAget(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001404 greenland::IntrinsicHelper::HLArrayGetFloat,
1405 rlDest, rlSrc[0], rlSrc[1]);
1406 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001407 ConvertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGet,
buzbee8fa0fda2012-06-27 15:44:52 -07001408 rlDest, rlSrc[0], rlSrc[1]);
1409 }
1410 break;
1411 case Instruction::AGET_OBJECT:
buzbee52a77fc2012-11-20 19:50:46 -08001412 ConvertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetObject,
buzbee8fa0fda2012-06-27 15:44:52 -07001413 rlDest, rlSrc[0], rlSrc[1]);
1414 break;
1415 case Instruction::AGET_BOOLEAN:
buzbee52a77fc2012-11-20 19:50:46 -08001416 ConvertAget(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001417 greenland::IntrinsicHelper::HLArrayGetBoolean,
1418 rlDest, rlSrc[0], rlSrc[1]);
1419 break;
1420 case Instruction::AGET_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001421 ConvertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetByte,
buzbee8fa0fda2012-06-27 15:44:52 -07001422 rlDest, rlSrc[0], rlSrc[1]);
1423 break;
1424 case Instruction::AGET_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001425 ConvertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetChar,
buzbee8fa0fda2012-06-27 15:44:52 -07001426 rlDest, rlSrc[0], rlSrc[1]);
1427 break;
1428 case Instruction::AGET_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001429 ConvertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetShort,
buzbee8fa0fda2012-06-27 15:44:52 -07001430 rlDest, rlSrc[0], rlSrc[1]);
1431 break;
1432 case Instruction::AGET_WIDE:
1433 if (rlDest.fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001434 ConvertAget(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001435 greenland::IntrinsicHelper::HLArrayGetDouble,
1436 rlDest, rlSrc[0], rlSrc[1]);
1437 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001438 ConvertAget(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayGetWide,
buzbee8fa0fda2012-06-27 15:44:52 -07001439 rlDest, rlSrc[0], rlSrc[1]);
1440 }
1441 break;
1442
1443 case Instruction::APUT:
1444 if (rlSrc[0].fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001445 ConvertAput(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001446 greenland::IntrinsicHelper::HLArrayPutFloat,
1447 rlSrc[0], rlSrc[1], rlSrc[2]);
1448 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001449 ConvertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPut,
buzbee8fa0fda2012-06-27 15:44:52 -07001450 rlSrc[0], rlSrc[1], rlSrc[2]);
1451 }
1452 break;
1453 case Instruction::APUT_OBJECT:
buzbee52a77fc2012-11-20 19:50:46 -08001454 ConvertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutObject,
buzbee8fa0fda2012-06-27 15:44:52 -07001455 rlSrc[0], rlSrc[1], rlSrc[2]);
1456 break;
1457 case Instruction::APUT_BOOLEAN:
buzbee52a77fc2012-11-20 19:50:46 -08001458 ConvertAput(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001459 greenland::IntrinsicHelper::HLArrayPutBoolean,
1460 rlSrc[0], rlSrc[1], rlSrc[2]);
1461 break;
1462 case Instruction::APUT_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001463 ConvertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutByte,
buzbee8fa0fda2012-06-27 15:44:52 -07001464 rlSrc[0], rlSrc[1], rlSrc[2]);
1465 break;
1466 case Instruction::APUT_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001467 ConvertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutChar,
buzbee8fa0fda2012-06-27 15:44:52 -07001468 rlSrc[0], rlSrc[1], rlSrc[2]);
1469 break;
1470 case Instruction::APUT_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001471 ConvertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutShort,
buzbee8fa0fda2012-06-27 15:44:52 -07001472 rlSrc[0], rlSrc[1], rlSrc[2]);
1473 break;
1474 case Instruction::APUT_WIDE:
1475 if (rlSrc[0].fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001476 ConvertAput(cUnit, optFlags,
buzbee8fa0fda2012-06-27 15:44:52 -07001477 greenland::IntrinsicHelper::HLArrayPutDouble,
1478 rlSrc[0], rlSrc[1], rlSrc[2]);
1479 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001480 ConvertAput(cUnit, optFlags, greenland::IntrinsicHelper::HLArrayPutWide,
buzbee8fa0fda2012-06-27 15:44:52 -07001481 rlSrc[0], rlSrc[1], rlSrc[2]);
1482 }
1483 break;
1484
buzbee101305f2012-06-28 18:00:56 -07001485 case Instruction::IGET:
1486 if (rlDest.fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001487 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetFloat,
buzbee4f4dfc72012-07-02 14:54:44 -07001488 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001489 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001490 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGet,
buzbee4f4dfc72012-07-02 14:54:44 -07001491 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001492 }
buzbee2cfc6392012-05-07 14:51:40 -07001493 break;
buzbee101305f2012-06-28 18:00:56 -07001494 case Instruction::IGET_OBJECT:
buzbee52a77fc2012-11-20 19:50:46 -08001495 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetObject,
buzbee4f4dfc72012-07-02 14:54:44 -07001496 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001497 break;
1498 case Instruction::IGET_BOOLEAN:
buzbee52a77fc2012-11-20 19:50:46 -08001499 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetBoolean,
buzbee4f4dfc72012-07-02 14:54:44 -07001500 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001501 break;
1502 case Instruction::IGET_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001503 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetByte,
buzbee4f4dfc72012-07-02 14:54:44 -07001504 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001505 break;
1506 case Instruction::IGET_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001507 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetChar,
buzbee4f4dfc72012-07-02 14:54:44 -07001508 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001509 break;
1510 case Instruction::IGET_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001511 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetShort,
buzbee4f4dfc72012-07-02 14:54:44 -07001512 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001513 break;
1514 case Instruction::IGET_WIDE:
1515 if (rlDest.fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001516 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetDouble,
buzbee4f4dfc72012-07-02 14:54:44 -07001517 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001518 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001519 ConvertIget(cUnit, optFlags, greenland::IntrinsicHelper::HLIGetWide,
buzbee4f4dfc72012-07-02 14:54:44 -07001520 rlDest, rlSrc[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001521 }
1522 break;
1523 case Instruction::IPUT:
buzbee85eee022012-07-16 22:12:38 -07001524 if (rlSrc[0].fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001525 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutFloat,
buzbee101305f2012-06-28 18:00:56 -07001526 rlSrc[0], rlSrc[1], vC);
1527 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001528 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPut,
buzbee101305f2012-06-28 18:00:56 -07001529 rlSrc[0], rlSrc[1], vC);
1530 }
1531 break;
1532 case Instruction::IPUT_OBJECT:
buzbee52a77fc2012-11-20 19:50:46 -08001533 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutObject,
buzbee101305f2012-06-28 18:00:56 -07001534 rlSrc[0], rlSrc[1], vC);
1535 break;
1536 case Instruction::IPUT_BOOLEAN:
buzbee52a77fc2012-11-20 19:50:46 -08001537 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutBoolean,
buzbee101305f2012-06-28 18:00:56 -07001538 rlSrc[0], rlSrc[1], vC);
1539 break;
1540 case Instruction::IPUT_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001541 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutByte,
buzbee101305f2012-06-28 18:00:56 -07001542 rlSrc[0], rlSrc[1], vC);
1543 break;
1544 case Instruction::IPUT_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001545 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutChar,
buzbee101305f2012-06-28 18:00:56 -07001546 rlSrc[0], rlSrc[1], vC);
1547 break;
1548 case Instruction::IPUT_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001549 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutShort,
buzbee101305f2012-06-28 18:00:56 -07001550 rlSrc[0], rlSrc[1], vC);
1551 break;
1552 case Instruction::IPUT_WIDE:
buzbee85eee022012-07-16 22:12:38 -07001553 if (rlSrc[0].fp) {
buzbee52a77fc2012-11-20 19:50:46 -08001554 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutDouble,
buzbee101305f2012-06-28 18:00:56 -07001555 rlSrc[0], rlSrc[1], vC);
1556 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001557 ConvertIput(cUnit, optFlags, greenland::IntrinsicHelper::HLIPutWide,
buzbee101305f2012-06-28 18:00:56 -07001558 rlSrc[0], rlSrc[1], vC);
1559 }
buzbee2cfc6392012-05-07 14:51:40 -07001560 break;
1561
1562 case Instruction::FILL_ARRAY_DATA:
buzbee52a77fc2012-11-20 19:50:46 -08001563 ConvertFillArrayData(cUnit, vB, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001564 break;
1565
buzbee76592632012-06-29 15:18:35 -07001566 case Instruction::LONG_TO_INT:
buzbee52a77fc2012-11-20 19:50:46 -08001567 ConvertLongToInt(cUnit, rlDest, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001568 break;
1569
buzbee101305f2012-06-28 18:00:56 -07001570 case Instruction::INT_TO_LONG:
buzbee52a77fc2012-11-20 19:50:46 -08001571 ConvertIntToLong(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001572 break;
1573
buzbee101305f2012-06-28 18:00:56 -07001574 case Instruction::INT_TO_CHAR:
buzbee52a77fc2012-11-20 19:50:46 -08001575 ConvertIntNarrowing(cUnit, rlDest, rlSrc[0],
buzbee101305f2012-06-28 18:00:56 -07001576 greenland::IntrinsicHelper::IntToChar);
1577 break;
1578 case Instruction::INT_TO_BYTE:
buzbee52a77fc2012-11-20 19:50:46 -08001579 ConvertIntNarrowing(cUnit, rlDest, rlSrc[0],
buzbee101305f2012-06-28 18:00:56 -07001580 greenland::IntrinsicHelper::IntToByte);
1581 break;
1582 case Instruction::INT_TO_SHORT:
buzbee52a77fc2012-11-20 19:50:46 -08001583 ConvertIntNarrowing(cUnit, rlDest, rlSrc[0],
buzbee101305f2012-06-28 18:00:56 -07001584 greenland::IntrinsicHelper::IntToShort);
1585 break;
1586
buzbee76592632012-06-29 15:18:35 -07001587 case Instruction::INT_TO_FLOAT:
1588 case Instruction::LONG_TO_FLOAT:
buzbee52a77fc2012-11-20 19:50:46 -08001589 ConvertIntToFP(cUnit, cUnit->irb->getFloatTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001590 break;
1591
buzbee76592632012-06-29 15:18:35 -07001592 case Instruction::INT_TO_DOUBLE:
1593 case Instruction::LONG_TO_DOUBLE:
buzbee52a77fc2012-11-20 19:50:46 -08001594 ConvertIntToFP(cUnit, cUnit->irb->getDoubleTy(), rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001595 break;
1596
buzbee76592632012-06-29 15:18:35 -07001597 case Instruction::FLOAT_TO_DOUBLE:
buzbee52a77fc2012-11-20 19:50:46 -08001598 ConvertFloatToDouble(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001599 break;
1600
buzbee76592632012-06-29 15:18:35 -07001601 case Instruction::DOUBLE_TO_FLOAT:
buzbee52a77fc2012-11-20 19:50:46 -08001602 ConvertDoubleToFloat(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001603 break;
1604
1605 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001606 case Instruction::NEG_INT:
buzbee52a77fc2012-11-20 19:50:46 -08001607 ConvertNeg(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001608 break;
1609
1610 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001611 case Instruction::NEG_DOUBLE:
buzbee52a77fc2012-11-20 19:50:46 -08001612 ConvertNegFP(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001613 break;
1614
buzbee76592632012-06-29 15:18:35 -07001615 case Instruction::NOT_LONG:
1616 case Instruction::NOT_INT:
buzbee52a77fc2012-11-20 19:50:46 -08001617 ConvertNot(cUnit, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001618 break;
1619
buzbee2cfc6392012-05-07 14:51:40 -07001620 case Instruction::FLOAT_TO_INT:
buzbee52a77fc2012-11-20 19:50:46 -08001621 ConvertFPToInt(cUnit, greenland::IntrinsicHelper::F2I, rlDest, rlSrc[0]);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001622 break;
1623
buzbee2cfc6392012-05-07 14:51:40 -07001624 case Instruction::DOUBLE_TO_INT:
buzbee52a77fc2012-11-20 19:50:46 -08001625 ConvertFPToInt(cUnit, greenland::IntrinsicHelper::D2I, rlDest, rlSrc[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001626 break;
1627
buzbee76592632012-06-29 15:18:35 -07001628 case Instruction::FLOAT_TO_LONG:
buzbee52a77fc2012-11-20 19:50:46 -08001629 ConvertFPToInt(cUnit, greenland::IntrinsicHelper::F2L, rlDest, rlSrc[0]);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001630 break;
1631
buzbee76592632012-06-29 15:18:35 -07001632 case Instruction::DOUBLE_TO_LONG:
buzbee52a77fc2012-11-20 19:50:46 -08001633 ConvertFPToInt(cUnit, greenland::IntrinsicHelper::D2L, rlDest, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001634 break;
1635
1636 case Instruction::CMPL_FLOAT:
buzbee52a77fc2012-11-20 19:50:46 -08001637 ConvertWideComparison(cUnit, greenland::IntrinsicHelper::CmplFloat,
buzbee76592632012-06-29 15:18:35 -07001638 rlDest, rlSrc[0], rlSrc[1]);
1639 break;
1640 case Instruction::CMPG_FLOAT:
buzbee52a77fc2012-11-20 19:50:46 -08001641 ConvertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgFloat,
buzbee76592632012-06-29 15:18:35 -07001642 rlDest, rlSrc[0], rlSrc[1]);
1643 break;
1644 case Instruction::CMPL_DOUBLE:
buzbee52a77fc2012-11-20 19:50:46 -08001645 ConvertWideComparison(cUnit, greenland::IntrinsicHelper::CmplDouble,
buzbee76592632012-06-29 15:18:35 -07001646 rlDest, rlSrc[0], rlSrc[1]);
1647 break;
1648 case Instruction::CMPG_DOUBLE:
buzbee52a77fc2012-11-20 19:50:46 -08001649 ConvertWideComparison(cUnit, greenland::IntrinsicHelper::CmpgDouble,
buzbee76592632012-06-29 15:18:35 -07001650 rlDest, rlSrc[0], rlSrc[1]);
1651 break;
1652 case Instruction::CMP_LONG:
buzbee52a77fc2012-11-20 19:50:46 -08001653 ConvertWideComparison(cUnit, greenland::IntrinsicHelper::CmpLong,
buzbee76592632012-06-29 15:18:35 -07001654 rlDest, rlSrc[0], rlSrc[1]);
1655 break;
1656
buzbee76592632012-06-29 15:18:35 -07001657 case Instruction::PACKED_SWITCH:
buzbee52a77fc2012-11-20 19:50:46 -08001658 ConvertPackedSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001659 break;
1660
1661 case Instruction::SPARSE_SWITCH:
buzbee52a77fc2012-11-20 19:50:46 -08001662 ConvertSparseSwitch(cUnit, bb, vB, rlSrc[0]);
buzbee76592632012-06-29 15:18:35 -07001663 break;
buzbee2cfc6392012-05-07 14:51:40 -07001664
1665 default:
buzbee32412962012-06-26 16:27:56 -07001666 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001667 res = true;
1668 }
buzbeeb03f4872012-06-11 15:22:11 -07001669 if (objectDefinition) {
buzbee52a77fc2012-11-20 19:50:46 -08001670 SetShadowFrameEntry(cUnit, reinterpret_cast<llvm::Value*>
buzbeecbd6d442012-11-17 14:11:25 -08001671 (cUnit->llvmValues.elemList[rlDest.origSReg]));
buzbeeb03f4872012-06-11 15:22:11 -07001672 }
buzbee2cfc6392012-05-07 14:51:40 -07001673 return res;
1674}
1675
1676/* Extended MIR instructions like PHI */
buzbeeaad94382012-11-21 07:40:50 -08001677static void ConvertExtendedMIR(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
1678 llvm::BasicBlock* llvmBB)
buzbee2cfc6392012-05-07 14:51:40 -07001679{
1680
buzbeecbd6d442012-11-17 14:11:25 -08001681 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
buzbee2cfc6392012-05-07 14:51:40 -07001682 case kMirOpPhi: {
buzbee2cfc6392012-05-07 14:51:40 -07001683 RegLocation rlDest = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee2a83e8f2012-07-13 16:42:30 -07001684 /*
1685 * The Art compiler's Phi nodes only handle 32-bit operands,
1686 * representing wide values using a matched set of Phi nodes
1687 * for the lower and upper halves. In the llvm world, we only
1688 * want a single Phi for wides. Here we will simply discard
1689 * the Phi node representing the high word.
1690 */
1691 if (rlDest.highWord) {
1692 return; // No Phi node - handled via low word
1693 }
buzbeecbd6d442012-11-17 14:11:25 -08001694 int* incoming = reinterpret_cast<int*>(mir->dalvikInsn.vB);
buzbee2cfc6392012-05-07 14:51:40 -07001695 llvm::Type* phiType =
buzbee52a77fc2012-11-20 19:50:46 -08001696 LlvmTypeFromLocRec(cUnit, rlDest);
buzbee2cfc6392012-05-07 14:51:40 -07001697 llvm::PHINode* phi = cUnit->irb->CreatePHI(phiType, mir->ssaRep->numUses);
1698 for (int i = 0; i < mir->ssaRep->numUses; i++) {
1699 RegLocation loc;
buzbee2a83e8f2012-07-13 16:42:30 -07001700 // Don't check width here.
buzbee52a77fc2012-11-20 19:50:46 -08001701 loc = GetRawSrc(cUnit, mir, i);
buzbee2a83e8f2012-07-13 16:42:30 -07001702 DCHECK_EQ(rlDest.wide, loc.wide);
1703 DCHECK_EQ(rlDest.wide & rlDest.highWord, loc.wide & loc.highWord);
1704 DCHECK_EQ(rlDest.fp, loc.fp);
1705 DCHECK_EQ(rlDest.core, loc.core);
1706 DCHECK_EQ(rlDest.ref, loc.ref);
buzbeed1643e42012-09-05 14:06:51 -07001707 SafeMap<unsigned int, unsigned int>::iterator it;
1708 it = cUnit->blockIdMap.find(incoming[i]);
1709 DCHECK(it != cUnit->blockIdMap.end());
buzbee52a77fc2012-11-20 19:50:46 -08001710 phi->addIncoming(GetLLVMValue(cUnit, loc.origSReg),
1711 GetLLVMBlock(cUnit, it->second));
buzbee2cfc6392012-05-07 14:51:40 -07001712 }
buzbee52a77fc2012-11-20 19:50:46 -08001713 DefineValue(cUnit, phi, rlDest.origSReg);
buzbee2cfc6392012-05-07 14:51:40 -07001714 break;
1715 }
1716 case kMirOpCopy: {
1717 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1718 break;
1719 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001720 case kMirOpNop:
1721 if ((mir == bb->lastMIRInsn) && (bb->taken == NULL) &&
1722 (bb->fallThrough == NULL)) {
1723 cUnit->irb->CreateUnreachable();
1724 }
1725 break;
1726
buzbeeb046e162012-10-30 15:48:42 -07001727 // TODO: need GBC intrinsic to take advantage of fused operations
buzbee2cfc6392012-05-07 14:51:40 -07001728 case kMirOpFusedCmplFloat:
buzbeeb046e162012-10-30 15:48:42 -07001729 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpFloat unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001730 break;
1731 case kMirOpFusedCmpgFloat:
buzbeeb046e162012-10-30 15:48:42 -07001732 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmgFloat unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001733 break;
1734 case kMirOpFusedCmplDouble:
buzbeeb046e162012-10-30 15:48:42 -07001735 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmplDouble unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001736 break;
1737 case kMirOpFusedCmpgDouble:
buzbeeb046e162012-10-30 15:48:42 -07001738 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpgDouble unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001739 break;
1740 case kMirOpFusedCmpLong:
buzbeeb046e162012-10-30 15:48:42 -07001741 UNIMPLEMENTED(FATAL) << "kMirOpLongCmpBranch unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001742 break;
buzbee2cfc6392012-05-07 14:51:40 -07001743 default:
1744 break;
1745 }
1746}
1747
buzbeeaad94382012-11-21 07:40:50 -08001748static void SetDexOffset(CompilationUnit* cUnit, int32_t offset)
buzbee2cfc6392012-05-07 14:51:40 -07001749{
1750 cUnit->currentDalvikOffset = offset;
buzbee76592632012-06-29 15:18:35 -07001751 llvm::SmallVector<llvm::Value*, 1> arrayRef;
buzbee2cfc6392012-05-07 14:51:40 -07001752 arrayRef.push_back(cUnit->irb->getInt32(offset));
1753 llvm::MDNode* node = llvm::MDNode::get(*cUnit->context, arrayRef);
1754 cUnit->irb->SetDexOffset(node);
1755}
1756
1757// Attach method info as metadata to special intrinsic
buzbeeaad94382012-11-21 07:40:50 -08001758static void SetMethodInfo(CompilationUnit* cUnit)
buzbee2cfc6392012-05-07 14:51:40 -07001759{
1760 // We don't want dex offset on this
1761 cUnit->irb->SetDexOffset(NULL);
1762 greenland::IntrinsicHelper::IntrinsicId id;
1763 id = greenland::IntrinsicHelper::MethodInfo;
1764 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1765 llvm::Instruction* inst = cUnit->irb->CreateCall(intr);
1766 llvm::SmallVector<llvm::Value*, 2> regInfo;
1767 regInfo.push_back(cUnit->irb->getInt32(cUnit->numIns));
1768 regInfo.push_back(cUnit->irb->getInt32(cUnit->numRegs));
1769 regInfo.push_back(cUnit->irb->getInt32(cUnit->numOuts));
1770 regInfo.push_back(cUnit->irb->getInt32(cUnit->numCompilerTemps));
1771 regInfo.push_back(cUnit->irb->getInt32(cUnit->numSSARegs));
1772 llvm::MDNode* regInfoNode = llvm::MDNode::get(*cUnit->context, regInfo);
1773 inst->setMetadata("RegInfo", regInfoNode);
1774 int promoSize = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
1775 llvm::SmallVector<llvm::Value*, 50> pmap;
1776 for (int i = 0; i < promoSize; i++) {
1777 PromotionMap* p = &cUnit->promotionMap[i];
1778 int32_t mapData = ((p->firstInPair & 0xff) << 24) |
buzbee52a77fc2012-11-20 19:50:46 -08001779 ((p->FpReg & 0xff) << 16) |
buzbee2cfc6392012-05-07 14:51:40 -07001780 ((p->coreReg & 0xff) << 8) |
1781 ((p->fpLocation & 0xf) << 4) |
1782 (p->coreLocation & 0xf);
1783 pmap.push_back(cUnit->irb->getInt32(mapData));
1784 }
1785 llvm::MDNode* mapNode = llvm::MDNode::get(*cUnit->context, pmap);
1786 inst->setMetadata("PromotionMap", mapNode);
buzbee52a77fc2012-11-20 19:50:46 -08001787 SetDexOffset(cUnit, cUnit->currentDalvikOffset);
buzbee2cfc6392012-05-07 14:51:40 -07001788}
1789
1790/* Handle the content in each basic block */
buzbeeaad94382012-11-21 07:40:50 -08001791static bool BlockBitcodeConversion(CompilationUnit* cUnit, BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07001792{
buzbeed1643e42012-09-05 14:06:51 -07001793 if (bb->blockType == kDead) return false;
buzbee52a77fc2012-11-20 19:50:46 -08001794 llvm::BasicBlock* llvmBB = GetLLVMBlock(cUnit, bb->id);
buzbeef5f5a122012-09-21 13:57:36 -07001795 if (llvmBB == NULL) {
1796 CHECK(bb->blockType == kExitBlock);
1797 } else {
1798 cUnit->irb->SetInsertPoint(llvmBB);
buzbee52a77fc2012-11-20 19:50:46 -08001799 SetDexOffset(cUnit, bb->startOffset);
buzbeef5f5a122012-09-21 13:57:36 -07001800 }
buzbee2cfc6392012-05-07 14:51:40 -07001801
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001802 if (cUnit->printMe) {
1803 LOG(INFO) << "................................";
1804 LOG(INFO) << "Block id " << bb->id;
1805 if (llvmBB != NULL) {
1806 LOG(INFO) << "label " << llvmBB->getName().str().c_str();
1807 } else {
1808 LOG(INFO) << "llvmBB is NULL";
1809 }
1810 }
1811
buzbee2cfc6392012-05-07 14:51:40 -07001812 if (bb->blockType == kEntryBlock) {
buzbee52a77fc2012-11-20 19:50:46 -08001813 SetMethodInfo(cUnit);
1814 bool *canBeRef = static_cast<bool*>(NewMem(cUnit, sizeof(bool) * cUnit->numDalvikRegisters,
buzbeecbd6d442012-11-17 14:11:25 -08001815 true, kAllocMisc));
buzbeeb03f4872012-06-11 15:22:11 -07001816 for (int i = 0; i < cUnit->numSSARegs; i++) {
buzbee6ec5e232012-09-20 15:50:03 -07001817 int vReg = SRegToVReg(cUnit, i);
1818 if (vReg > SSA_METHOD_BASEREG) {
1819 canBeRef[SRegToVReg(cUnit, i)] |= cUnit->regLocation[i].ref;
1820 }
buzbeeb03f4872012-06-11 15:22:11 -07001821 }
1822 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
1823 if (canBeRef[i]) {
1824 cUnit->numShadowFrameEntries++;
1825 }
1826 }
1827 if (cUnit->numShadowFrameEntries > 0) {
buzbee52a77fc2012-11-20 19:50:46 -08001828 cUnit->shadowMap = static_cast<int*>(NewMem(cUnit, sizeof(int) * cUnit->numShadowFrameEntries,
buzbeecbd6d442012-11-17 14:11:25 -08001829 true, kAllocMisc));
buzbeeb03f4872012-06-11 15:22:11 -07001830 for (int i = 0, j = 0; i < cUnit->numDalvikRegisters; i++) {
1831 if (canBeRef[i]) {
1832 cUnit->shadowMap[j++] = i;
1833 }
1834 }
buzbeeb03f4872012-06-11 15:22:11 -07001835 }
Shih-wei Liao569daf12012-08-10 23:22:33 -07001836 greenland::IntrinsicHelper::IntrinsicId id =
1837 greenland::IntrinsicHelper::AllocaShadowFrame;
1838 llvm::Function* func = cUnit->intrinsic_helper->GetIntrinsicFunction(id);
1839 llvm::Value* entries = cUnit->irb->getInt32(cUnit->numShadowFrameEntries);
TDYa1278e950c12012-11-02 09:58:19 -07001840 llvm::Value* dalvikRegs = cUnit->irb->getInt32(cUnit->numDalvikRegisters);
1841 llvm::Value* args[] = { entries, dalvikRegs };
1842 cUnit->irb->CreateCall(func, args);
buzbee2cfc6392012-05-07 14:51:40 -07001843 } else if (bb->blockType == kExitBlock) {
1844 /*
1845 * Because of the differences between how MIR/LIR and llvm handle exit
1846 * blocks, we won't explicitly covert them. On the llvm-to-lir
1847 * path, it will need to be regenereated.
1848 */
1849 return false;
buzbee6969d502012-06-15 16:40:31 -07001850 } else if (bb->blockType == kExceptionHandling) {
1851 /*
1852 * Because we're deferring null checking, delete the associated empty
1853 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001854 */
1855 llvmBB->eraseFromParent();
1856 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001857 }
1858
1859 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
1860
buzbee52a77fc2012-11-20 19:50:46 -08001861 SetDexOffset(cUnit, mir->offset);
buzbee2cfc6392012-05-07 14:51:40 -07001862
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001863 int opcode = mir->dalvikInsn.opcode;
1864 Instruction::Format dalvikFormat =
1865 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001866
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001867 if (opcode == kMirOpCheck) {
1868 // Combine check and work halves of throwing instruction.
1869 MIR* workHalf = mir->meta.throwInsn;
1870 mir->dalvikInsn.opcode = workHalf->dalvikInsn.opcode;
1871 opcode = mir->dalvikInsn.opcode;
1872 SSARepresentation* ssaRep = workHalf->ssaRep;
1873 workHalf->ssaRep = mir->ssaRep;
1874 mir->ssaRep = ssaRep;
1875 workHalf->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1876 if (bb->successorBlockList.blockListType == kCatch) {
1877 llvm::Function* intr = cUnit->intrinsic_helper->GetIntrinsicFunction(
1878 greenland::IntrinsicHelper::CatchTargets);
1879 llvm::Value* switchKey =
1880 cUnit->irb->CreateCall(intr, cUnit->irb->getInt32(mir->offset));
1881 GrowableListIterator iter;
buzbee52a77fc2012-11-20 19:50:46 -08001882 GrowableListIteratorInit(&bb->successorBlockList.blocks, &iter);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001883 // New basic block to use for work half
1884 llvm::BasicBlock* workBB =
1885 llvm::BasicBlock::Create(*cUnit->context, "", cUnit->func);
1886 llvm::SwitchInst* sw =
1887 cUnit->irb->CreateSwitch(switchKey, workBB,
1888 bb->successorBlockList.blocks.numUsed);
1889 while (true) {
1890 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -08001891 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iter));
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001892 if (successorBlockInfo == NULL) break;
1893 llvm::BasicBlock *target =
buzbee52a77fc2012-11-20 19:50:46 -08001894 GetLLVMBlock(cUnit, successorBlockInfo->block->id);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001895 int typeIndex = successorBlockInfo->key;
1896 sw->addCase(cUnit->irb->getInt32(typeIndex), target);
1897 }
1898 llvmBB = workBB;
1899 cUnit->irb->SetInsertPoint(llvmBB);
1900 }
1901 }
1902
1903 if (opcode >= kMirOpFirst) {
buzbee52a77fc2012-11-20 19:50:46 -08001904 ConvertExtendedMIR(cUnit, bb, mir, llvmBB);
buzbee2cfc6392012-05-07 14:51:40 -07001905 continue;
1906 }
1907
buzbee52a77fc2012-11-20 19:50:46 -08001908 bool notHandled = ConvertMIRNode(cUnit, mir, bb, llvmBB,
buzbee2cfc6392012-05-07 14:51:40 -07001909 NULL /* labelList */);
1910 if (notHandled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001911 Instruction::Code dalvikOpcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001912 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001913 mir->offset, opcode,
buzbee2cfc6392012-05-07 14:51:40 -07001914 Instruction::Name(dalvikOpcode),
1915 dalvikFormat);
1916 }
1917 }
1918
buzbee4be777b2012-07-12 14:38:18 -07001919 if (bb->blockType == kEntryBlock) {
buzbee52a77fc2012-11-20 19:50:46 -08001920 cUnit->entryTargetBB = GetLLVMBlock(cUnit, bb->fallThrough->id);
buzbee4be777b2012-07-12 14:38:18 -07001921 } else if ((bb->fallThrough != NULL) && !bb->hasReturn) {
buzbee52a77fc2012-11-20 19:50:46 -08001922 cUnit->irb->CreateBr(GetLLVMBlock(cUnit, bb->fallThrough->id));
buzbee2cfc6392012-05-07 14:51:40 -07001923 }
1924
1925 return false;
1926}
1927
buzbee52a77fc2012-11-20 19:50:46 -08001928char RemapShorty(char shortyType) {
buzbee4f4dfc72012-07-02 14:54:44 -07001929 /*
1930 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1931 * and longs/doubles are represented as a pair of registers. When sub-word
1932 * arguments (and method results) are passed, they are extended to Dalvik
1933 * virtual register containers. Because llvm is picky about type consistency,
1934 * we must either cast the "real" type to 32-bit container multiple Dalvik
1935 * register types, or always use the expanded values.
1936 * Here, we're doing the latter. We map the shorty signature to container
1937 * types (which is valid so long as we always do a real expansion of passed
1938 * arguments and field loads).
1939 */
1940 switch(shortyType) {
1941 case 'Z' : shortyType = 'I'; break;
1942 case 'B' : shortyType = 'I'; break;
1943 case 'S' : shortyType = 'I'; break;
1944 case 'C' : shortyType = 'I'; break;
1945 default: break;
1946 }
1947 return shortyType;
1948}
1949
buzbeeaad94382012-11-21 07:40:50 -08001950static llvm::FunctionType* GetFunctionType(CompilationUnit* cUnit) {
buzbee2cfc6392012-05-07 14:51:40 -07001951
1952 // Get return type
buzbee52a77fc2012-11-20 19:50:46 -08001953 llvm::Type* ret_type = cUnit->irb->GetJType(RemapShorty(cUnit->shorty[0]),
buzbee2cfc6392012-05-07 14:51:40 -07001954 greenland::kAccurate);
1955
1956 // Get argument type
1957 std::vector<llvm::Type*> args_type;
1958
1959 // method object
1960 args_type.push_back(cUnit->irb->GetJMethodTy());
1961
1962 // Do we have a "this"?
1963 if ((cUnit->access_flags & kAccStatic) == 0) {
1964 args_type.push_back(cUnit->irb->GetJObjectTy());
1965 }
1966
1967 for (uint32_t i = 1; i < strlen(cUnit->shorty); ++i) {
buzbee52a77fc2012-11-20 19:50:46 -08001968 args_type.push_back(cUnit->irb->GetJType(RemapShorty(cUnit->shorty[i]),
buzbee2cfc6392012-05-07 14:51:40 -07001969 greenland::kAccurate));
1970 }
1971
1972 return llvm::FunctionType::get(ret_type, args_type, false);
1973}
1974
buzbeeaad94382012-11-21 07:40:50 -08001975static bool CreateFunction(CompilationUnit* cUnit) {
buzbee2cfc6392012-05-07 14:51:40 -07001976 std::string func_name(PrettyMethod(cUnit->method_idx, *cUnit->dex_file,
1977 /* with_signature */ false));
buzbee52a77fc2012-11-20 19:50:46 -08001978 llvm::FunctionType* func_type = GetFunctionType(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07001979
1980 if (func_type == NULL) {
1981 return false;
1982 }
1983
1984 cUnit->func = llvm::Function::Create(func_type,
1985 llvm::Function::ExternalLinkage,
1986 func_name, cUnit->module);
1987
1988 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
1989 llvm::Function::arg_iterator arg_end(cUnit->func->arg_end());
1990
1991 arg_iter->setName("method");
1992 ++arg_iter;
1993
1994 int startSReg = cUnit->numRegs;
1995
1996 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
1997 arg_iter->setName(StringPrintf("v%i_0", startSReg));
1998 startSReg += cUnit->regLocation[startSReg].wide ? 2 : 1;
1999 }
2000
2001 return true;
2002}
2003
buzbeeaad94382012-11-21 07:40:50 -08002004static bool CreateLLVMBasicBlock(CompilationUnit* cUnit, BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07002005{
2006 // Skip the exit block
buzbeed1643e42012-09-05 14:06:51 -07002007 if ((bb->blockType == kDead) ||(bb->blockType == kExitBlock)) {
buzbee2cfc6392012-05-07 14:51:40 -07002008 cUnit->idToBlockMap.Put(bb->id, NULL);
2009 } else {
2010 int offset = bb->startOffset;
2011 bool entryBlock = (bb->blockType == kEntryBlock);
2012 llvm::BasicBlock* llvmBB =
2013 llvm::BasicBlock::Create(*cUnit->context, entryBlock ? "entry" :
buzbee8320f382012-09-11 16:29:42 -07002014 StringPrintf(kLabelFormat, bb->catchEntry ? kCatchBlock :
2015 kNormalBlock, offset, bb->id), cUnit->func);
buzbee2cfc6392012-05-07 14:51:40 -07002016 if (entryBlock) {
2017 cUnit->entryBB = llvmBB;
2018 cUnit->placeholderBB =
2019 llvm::BasicBlock::Create(*cUnit->context, "placeholder",
2020 cUnit->func);
2021 }
2022 cUnit->idToBlockMap.Put(bb->id, llvmBB);
2023 }
2024 return false;
2025}
2026
2027
2028/*
2029 * Convert MIR to LLVM_IR
2030 * o For each ssa name, create LLVM named value. Type these
2031 * appropriately, and ignore high half of wide and double operands.
2032 * o For each MIR basic block, create an LLVM basic block.
2033 * o Iterate through the MIR a basic block at a time, setting arguments
2034 * to recovered ssa name.
2035 */
buzbee52a77fc2012-11-20 19:50:46 -08002036void MethodMIR2Bitcode(CompilationUnit* cUnit)
buzbee2cfc6392012-05-07 14:51:40 -07002037{
buzbee52a77fc2012-11-20 19:50:46 -08002038 InitIR(cUnit);
2039 CompilerInitGrowableList(cUnit, &cUnit->llvmValues, cUnit->numSSARegs);
buzbee2cfc6392012-05-07 14:51:40 -07002040
2041 // Create the function
buzbee52a77fc2012-11-20 19:50:46 -08002042 CreateFunction(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002043
2044 // Create an LLVM basic block for each MIR block in dfs preorder
buzbee52a77fc2012-11-20 19:50:46 -08002045 DataFlowAnalysisDispatcher(cUnit, CreateLLVMBasicBlock,
buzbee2cfc6392012-05-07 14:51:40 -07002046 kPreOrderDFSTraversal, false /* isIterative */);
2047 /*
2048 * Create an llvm named value for each MIR SSA name. Note: we'll use
2049 * placeholders for all non-argument values (because we haven't seen
2050 * the definition yet).
2051 */
2052 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2053 llvm::Function::arg_iterator arg_iter(cUnit->func->arg_begin());
2054 arg_iter++; /* Skip path method */
2055 for (int i = 0; i < cUnit->numSSARegs; i++) {
2056 llvm::Value* val;
buzbee85eee022012-07-16 22:12:38 -07002057 RegLocation rlTemp = cUnit->regLocation[i];
2058 if ((SRegToVReg(cUnit, i) < 0) || rlTemp.highWord) {
buzbee52a77fc2012-11-20 19:50:46 -08002059 InsertGrowableList(cUnit, &cUnit->llvmValues, 0);
buzbee2a83e8f2012-07-13 16:42:30 -07002060 } else if ((i < cUnit->numRegs) ||
2061 (i >= (cUnit->numRegs + cUnit->numIns))) {
buzbee85eee022012-07-16 22:12:38 -07002062 llvm::Constant* immValue = cUnit->regLocation[i].wide ?
2063 cUnit->irb->GetJLong(0) : cUnit->irb->GetJInt(0);
buzbee52a77fc2012-11-20 19:50:46 -08002064 val = EmitConst(cUnit, immValue, cUnit->regLocation[i]);
2065 val->setName(LlvmSSAName(cUnit, i));
2066 InsertGrowableList(cUnit, &cUnit->llvmValues, reinterpret_cast<uintptr_t>(val));
buzbee2cfc6392012-05-07 14:51:40 -07002067 } else {
2068 // Recover previously-created argument values
2069 llvm::Value* argVal = arg_iter++;
buzbee52a77fc2012-11-20 19:50:46 -08002070 InsertGrowableList(cUnit, &cUnit->llvmValues, reinterpret_cast<uintptr_t>(argVal));
buzbee2cfc6392012-05-07 14:51:40 -07002071 }
2072 }
buzbee2cfc6392012-05-07 14:51:40 -07002073
buzbeeaad94382012-11-21 07:40:50 -08002074 DataFlowAnalysisDispatcher(cUnit, BlockBitcodeConversion,
buzbee2cfc6392012-05-07 14:51:40 -07002075 kPreOrderDFSTraversal, false /* Iterative */);
2076
buzbee4be777b2012-07-12 14:38:18 -07002077 /*
2078 * In a few rare cases of verification failure, the verifier will
2079 * replace one or more Dalvik opcodes with the special
2080 * throw-verification-failure opcode. This can leave the SSA graph
2081 * in an invalid state, as definitions may be lost, while uses retained.
2082 * To work around this problem, we insert placeholder definitions for
2083 * all Dalvik SSA regs in the "placeholder" block. Here, after
2084 * bitcode conversion is complete, we examine those placeholder definitions
2085 * and delete any with no references (which normally is all of them).
2086 *
2087 * If any definitions remain, we link the placeholder block into the
2088 * CFG. Otherwise, it is deleted.
2089 */
2090 for (llvm::BasicBlock::iterator it = cUnit->placeholderBB->begin(),
2091 itEnd = cUnit->placeholderBB->end(); it != itEnd;) {
2092 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2093 DCHECK(inst != NULL);
2094 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2095 DCHECK(val != NULL);
2096 if (val->getNumUses() == 0) {
2097 inst->eraseFromParent();
2098 }
2099 }
buzbee52a77fc2012-11-20 19:50:46 -08002100 SetDexOffset(cUnit, 0);
buzbee4be777b2012-07-12 14:38:18 -07002101 if (cUnit->placeholderBB->empty()) {
2102 cUnit->placeholderBB->eraseFromParent();
2103 } else {
2104 cUnit->irb->SetInsertPoint(cUnit->placeholderBB);
2105 cUnit->irb->CreateBr(cUnit->entryTargetBB);
2106 cUnit->entryTargetBB = cUnit->placeholderBB;
2107 }
2108 cUnit->irb->SetInsertPoint(cUnit->entryBB);
2109 cUnit->irb->CreateBr(cUnit->entryTargetBB);
buzbee2cfc6392012-05-07 14:51:40 -07002110
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002111 if (cUnit->enableDebug & (1 << kDebugVerifyBitcode)) {
2112 if (llvm::verifyFunction(*cUnit->func, llvm::PrintMessageAction)) {
2113 LOG(INFO) << "Bitcode verification FAILED for "
2114 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
2115 << " of size " << cUnit->insnsSize;
2116 cUnit->enableDebug |= (1 << kDebugDumpBitcodeFile);
2117 }
2118 }
buzbee2cfc6392012-05-07 14:51:40 -07002119
buzbeead8f15e2012-06-18 14:49:45 -07002120 if (cUnit->enableDebug & (1 << kDebugDumpBitcodeFile)) {
2121 // Write bitcode to file
2122 std::string errmsg;
2123 std::string fname(PrettyMethod(cUnit->method_idx, *cUnit->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -08002124 ReplaceSpecialChars(fname);
buzbee6459e7c2012-10-02 14:42:41 -07002125 // TODO: make configurable change naming mechanism to avoid fname length issues.
buzbee4f1181f2012-06-22 13:52:12 -07002126 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002127
buzbee6459e7c2012-10-02 14:42:41 -07002128 if (fname.size() > 240) {
2129 LOG(INFO) << "Warning: bitcode filename too long. Truncated.";
2130 fname.resize(240);
2131 }
2132
buzbeead8f15e2012-06-18 14:49:45 -07002133 llvm::OwningPtr<llvm::tool_output_file> out_file(
2134 new llvm::tool_output_file(fname.c_str(), errmsg,
2135 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002136
buzbeead8f15e2012-06-18 14:49:45 -07002137 if (!errmsg.empty()) {
2138 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2139 }
2140
2141 llvm::WriteBitcodeToFile(cUnit->module, out_file->os());
2142 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002143 }
buzbee2cfc6392012-05-07 14:51:40 -07002144}
2145
buzbeeaad94382012-11-21 07:40:50 -08002146static RegLocation GetLoc(CompilationUnit* cUnit, llvm::Value* val) {
buzbee2cfc6392012-05-07 14:51:40 -07002147 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002148 DCHECK(val != NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002149 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2150 if (it == cUnit->locMap.end()) {
buzbee4f1181f2012-06-22 13:52:12 -07002151 std::string valName = val->getName().str();
buzbee32412962012-06-26 16:27:56 -07002152 if (valName.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002153 // FIXME: need to be more robust, handle FP and be in a position to
2154 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002155 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2156 memset(&res, 0, sizeof(res));
2157 res.location = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -08002158 res.lowReg = AllocTemp(cUnit);
buzbee4f1181f2012-06-22 13:52:12 -07002159 res.home = true;
2160 res.sRegLow = INVALID_SREG;
2161 res.origSReg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002162 llvm::Type* ty = val->getType();
2163 res.wide = ((ty == cUnit->irb->getInt64Ty()) ||
2164 (ty == cUnit->irb->getDoubleTy()));
2165 if (res.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002166 res.highReg = AllocTemp(cUnit);
buzbee101305f2012-06-28 18:00:56 -07002167 }
buzbee4f1181f2012-06-22 13:52:12 -07002168 cUnit->locMap.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002169 } else {
2170 DCHECK_EQ(valName[0], 'v');
2171 int baseSReg = INVALID_SREG;
2172 sscanf(valName.c_str(), "v%d_", &baseSReg);
2173 res = cUnit->regLocation[baseSReg];
2174 cUnit->locMap.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002175 }
2176 } else {
2177 res = it->second;
2178 }
2179 return res;
2180}
2181
buzbeeaad94382012-11-21 07:40:50 -08002182static Instruction::Code GetDalvikOpcode(OpKind op, bool isConst, bool isWide)
buzbee2cfc6392012-05-07 14:51:40 -07002183{
2184 Instruction::Code res = Instruction::NOP;
2185 if (isWide) {
2186 switch(op) {
2187 case kOpAdd: res = Instruction::ADD_LONG; break;
2188 case kOpSub: res = Instruction::SUB_LONG; break;
2189 case kOpMul: res = Instruction::MUL_LONG; break;
2190 case kOpDiv: res = Instruction::DIV_LONG; break;
2191 case kOpRem: res = Instruction::REM_LONG; break;
2192 case kOpAnd: res = Instruction::AND_LONG; break;
2193 case kOpOr: res = Instruction::OR_LONG; break;
2194 case kOpXor: res = Instruction::XOR_LONG; break;
2195 case kOpLsl: res = Instruction::SHL_LONG; break;
2196 case kOpLsr: res = Instruction::USHR_LONG; break;
2197 case kOpAsr: res = Instruction::SHR_LONG; break;
2198 default: LOG(FATAL) << "Unexpected OpKind " << op;
2199 }
2200 } else if (isConst){
2201 switch(op) {
2202 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2203 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2204 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2205 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2206 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2207 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2208 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2209 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2210 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2211 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2212 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2213 default: LOG(FATAL) << "Unexpected OpKind " << op;
2214 }
2215 } else {
2216 switch(op) {
2217 case kOpAdd: res = Instruction::ADD_INT; break;
2218 case kOpSub: res = Instruction::SUB_INT; break;
2219 case kOpMul: res = Instruction::MUL_INT; break;
2220 case kOpDiv: res = Instruction::DIV_INT; break;
2221 case kOpRem: res = Instruction::REM_INT; break;
2222 case kOpAnd: res = Instruction::AND_INT; break;
2223 case kOpOr: res = Instruction::OR_INT; break;
2224 case kOpXor: res = Instruction::XOR_INT; break;
2225 case kOpLsl: res = Instruction::SHL_INT; break;
2226 case kOpLsr: res = Instruction::USHR_INT; break;
2227 case kOpAsr: res = Instruction::SHR_INT; break;
2228 default: LOG(FATAL) << "Unexpected OpKind " << op;
2229 }
2230 }
2231 return res;
2232}
2233
buzbeeaad94382012-11-21 07:40:50 -08002234static Instruction::Code GetDalvikFPOpcode(OpKind op, bool isConst, bool isWide)
buzbee4f1181f2012-06-22 13:52:12 -07002235{
2236 Instruction::Code res = Instruction::NOP;
2237 if (isWide) {
2238 switch(op) {
2239 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2240 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2241 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2242 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2243 case kOpRem: res = Instruction::REM_DOUBLE; break;
2244 default: LOG(FATAL) << "Unexpected OpKind " << op;
2245 }
2246 } else {
2247 switch(op) {
2248 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2249 case kOpSub: res = Instruction::SUB_FLOAT; break;
2250 case kOpMul: res = Instruction::MUL_FLOAT; break;
2251 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2252 case kOpRem: res = Instruction::REM_FLOAT; break;
2253 default: LOG(FATAL) << "Unexpected OpKind " << op;
2254 }
2255 }
2256 return res;
2257}
2258
buzbeeaad94382012-11-21 07:40:50 -08002259static void CvtBinFPOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
buzbee4f1181f2012-06-22 13:52:12 -07002260{
buzbee52a77fc2012-11-20 19:50:46 -08002261 RegLocation rlDest = GetLoc(cUnit, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002262 /*
2263 * Normally, we won't ever generate an FP operation with an immediate
2264 * operand (not supported in Dex instruction set). However, the IR builder
2265 * may insert them - in particular for createNegFP. Recognize this case
2266 * and deal with it.
2267 */
2268 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2269 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2270 DCHECK(op2C == NULL);
2271 if ((op1C != NULL) && (op == kOpSub)) {
buzbee52a77fc2012-11-20 19:50:46 -08002272 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(1));
buzbee4f4dfc72012-07-02 14:54:44 -07002273 if (rlDest.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002274 GenArithOpDouble(cUnit, Instruction::NEG_DOUBLE, rlDest, rlSrc, rlSrc);
buzbee4f4dfc72012-07-02 14:54:44 -07002275 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002276 GenArithOpFloat(cUnit, Instruction::NEG_FLOAT, rlDest, rlSrc, rlSrc);
buzbee4f4dfc72012-07-02 14:54:44 -07002277 }
buzbee4f1181f2012-06-22 13:52:12 -07002278 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002279 DCHECK(op1C == NULL);
buzbee52a77fc2012-11-20 19:50:46 -08002280 RegLocation rlSrc1 = GetLoc(cUnit, inst->getOperand(0));
2281 RegLocation rlSrc2 = GetLoc(cUnit, inst->getOperand(1));
2282 Instruction::Code dalvikOp = GetDalvikFPOpcode(op, false, rlDest.wide);
buzbee4f4dfc72012-07-02 14:54:44 -07002283 if (rlDest.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002284 GenArithOpDouble(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
buzbee4f4dfc72012-07-02 14:54:44 -07002285 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002286 GenArithOpFloat(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
buzbee4f4dfc72012-07-02 14:54:44 -07002287 }
buzbee4f1181f2012-06-22 13:52:12 -07002288 }
2289}
2290
buzbeeaad94382012-11-21 07:40:50 -08002291static void CvtIntNarrowing(CompilationUnit* cUnit, llvm::Instruction* inst,
buzbee101305f2012-06-28 18:00:56 -07002292 Instruction::Code opcode)
2293{
buzbee52a77fc2012-11-20 19:50:46 -08002294 RegLocation rlDest = GetLoc(cUnit, inst);
2295 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(0));
2296 GenIntNarrowing(cUnit, opcode, rlDest, rlSrc);
buzbee101305f2012-06-28 18:00:56 -07002297}
2298
buzbeeaad94382012-11-21 07:40:50 -08002299static void CvtIntToFP(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002300{
buzbee52a77fc2012-11-20 19:50:46 -08002301 RegLocation rlDest = GetLoc(cUnit, inst);
2302 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002303 Instruction::Code opcode;
2304 if (rlDest.wide) {
2305 if (rlSrc.wide) {
2306 opcode = Instruction::LONG_TO_DOUBLE;
2307 } else {
2308 opcode = Instruction::INT_TO_DOUBLE;
2309 }
2310 } else {
2311 if (rlSrc.wide) {
2312 opcode = Instruction::LONG_TO_FLOAT;
2313 } else {
2314 opcode = Instruction::INT_TO_FLOAT;
2315 }
2316 }
buzbee52a77fc2012-11-20 19:50:46 -08002317 GenConversion(cUnit, opcode, rlDest, rlSrc);
buzbee76592632012-06-29 15:18:35 -07002318}
2319
buzbeeaad94382012-11-21 07:40:50 -08002320static void CvtFPToInt(CompilationUnit* cUnit, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002321{
buzbee52a77fc2012-11-20 19:50:46 -08002322 RegLocation rlDest = GetLoc(cUnit, call_inst);
2323 RegLocation rlSrc = GetLoc(cUnit, call_inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002324 Instruction::Code opcode;
2325 if (rlDest.wide) {
2326 if (rlSrc.wide) {
2327 opcode = Instruction::DOUBLE_TO_LONG;
2328 } else {
2329 opcode = Instruction::FLOAT_TO_LONG;
2330 }
2331 } else {
2332 if (rlSrc.wide) {
2333 opcode = Instruction::DOUBLE_TO_INT;
2334 } else {
2335 opcode = Instruction::FLOAT_TO_INT;
2336 }
2337 }
buzbee52a77fc2012-11-20 19:50:46 -08002338 GenConversion(cUnit, opcode, rlDest, rlSrc);
buzbee76592632012-06-29 15:18:35 -07002339}
2340
buzbeeaad94382012-11-21 07:40:50 -08002341static void CvtFloatToDouble(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002342{
buzbee52a77fc2012-11-20 19:50:46 -08002343 RegLocation rlDest = GetLoc(cUnit, inst);
2344 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(0));
2345 GenConversion(cUnit, Instruction::FLOAT_TO_DOUBLE, rlDest, rlSrc);
buzbee76592632012-06-29 15:18:35 -07002346}
2347
buzbeeaad94382012-11-21 07:40:50 -08002348static void CvtTrunc(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002349{
buzbee52a77fc2012-11-20 19:50:46 -08002350 RegLocation rlDest = GetLoc(cUnit, inst);
2351 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(0));
2352 rlSrc = UpdateLocWide(cUnit, rlSrc);
2353 rlSrc = WideToNarrow(cUnit, rlSrc);
2354 StoreValue(cUnit, rlDest, rlSrc);
buzbee76592632012-06-29 15:18:35 -07002355}
2356
buzbeeaad94382012-11-21 07:40:50 -08002357static void CvtDoubleToFloat(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002358{
buzbee52a77fc2012-11-20 19:50:46 -08002359 RegLocation rlDest = GetLoc(cUnit, inst);
2360 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(0));
2361 GenConversion(cUnit, Instruction::DOUBLE_TO_FLOAT, rlDest, rlSrc);
buzbee76592632012-06-29 15:18:35 -07002362}
2363
2364
buzbeeaad94382012-11-21 07:40:50 -08002365static void CvtIntExt(CompilationUnit* cUnit, llvm::Instruction* inst, bool isSigned)
buzbee101305f2012-06-28 18:00:56 -07002366{
2367 // TODO: evaluate src/tgt types and add general support for more than int to long
buzbee52a77fc2012-11-20 19:50:46 -08002368 RegLocation rlDest = GetLoc(cUnit, inst);
2369 RegLocation rlSrc = GetLoc(cUnit, inst->getOperand(0));
buzbee101305f2012-06-28 18:00:56 -07002370 DCHECK(rlDest.wide);
2371 DCHECK(!rlSrc.wide);
2372 DCHECK(!rlDest.fp);
2373 DCHECK(!rlSrc.fp);
buzbee52a77fc2012-11-20 19:50:46 -08002374 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee101305f2012-06-28 18:00:56 -07002375 if (rlSrc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -08002376 OpRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
buzbee101305f2012-06-28 18:00:56 -07002377 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002378 LoadValueDirect(cUnit, rlSrc, rlResult.lowReg);
buzbee101305f2012-06-28 18:00:56 -07002379 }
2380 if (isSigned) {
buzbee52a77fc2012-11-20 19:50:46 -08002381 OpRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
buzbee101305f2012-06-28 18:00:56 -07002382 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002383 LoadConstant(cUnit, rlResult.highReg, 0);
buzbee101305f2012-06-28 18:00:56 -07002384 }
buzbee52a77fc2012-11-20 19:50:46 -08002385 StoreValueWide(cUnit, rlDest, rlResult);
buzbee101305f2012-06-28 18:00:56 -07002386}
2387
buzbeeaad94382012-11-21 07:40:50 -08002388static void CvtBinOp(CompilationUnit* cUnit, OpKind op, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002389{
buzbee52a77fc2012-11-20 19:50:46 -08002390 RegLocation rlDest = GetLoc(cUnit, inst);
buzbee2cfc6392012-05-07 14:51:40 -07002391 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002392 // Special-case RSUB/NEG
buzbee4f1181f2012-06-22 13:52:12 -07002393 llvm::ConstantInt* lhsImm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2394 if ((op == kOpSub) && (lhsImm != NULL)) {
buzbee52a77fc2012-11-20 19:50:46 -08002395 RegLocation rlSrc1 = GetLoc(cUnit, inst->getOperand(1));
buzbeef58c12c2012-07-03 15:06:29 -07002396 if (rlSrc1.wide) {
2397 DCHECK_EQ(lhsImm->getSExtValue(), 0);
buzbee52a77fc2012-11-20 19:50:46 -08002398 GenArithOpLong(cUnit, Instruction::NEG_LONG, rlDest, rlSrc1, rlSrc1);
buzbeef58c12c2012-07-03 15:06:29 -07002399 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002400 GenArithOpIntLit(cUnit, Instruction::RSUB_INT, rlDest, rlSrc1,
buzbeef58c12c2012-07-03 15:06:29 -07002401 lhsImm->getSExtValue());
2402 }
buzbee4f1181f2012-06-22 13:52:12 -07002403 return;
2404 }
2405 DCHECK(lhsImm == NULL);
buzbee52a77fc2012-11-20 19:50:46 -08002406 RegLocation rlSrc1 = GetLoc(cUnit, inst->getOperand(0));
buzbee2cfc6392012-05-07 14:51:40 -07002407 llvm::Value* rhs = inst->getOperand(1);
buzbee9a2487f2012-07-26 14:01:13 -07002408 llvm::ConstantInt* constRhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2409 if (!rlDest.wide && (constRhs != NULL)) {
buzbee52a77fc2012-11-20 19:50:46 -08002410 Instruction::Code dalvikOp = GetDalvikOpcode(op, true, false);
2411 GenArithOpIntLit(cUnit, dalvikOp, rlDest, rlSrc1, constRhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002412 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002413 Instruction::Code dalvikOp = GetDalvikOpcode(op, false, rlDest.wide);
buzbee9a2487f2012-07-26 14:01:13 -07002414 RegLocation rlSrc2;
2415 if (constRhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002416 // ir_builder converts NOT_LONG to xor src, -1. Restore
2417 DCHECK_EQ(dalvikOp, Instruction::XOR_LONG);
2418 DCHECK_EQ(-1L, constRhs->getSExtValue());
2419 dalvikOp = Instruction::NOT_LONG;
buzbee9a2487f2012-07-26 14:01:13 -07002420 rlSrc2 = rlSrc1;
2421 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002422 rlSrc2 = GetLoc(cUnit, rhs);
buzbee9a2487f2012-07-26 14:01:13 -07002423 }
buzbee2cfc6392012-05-07 14:51:40 -07002424 if (rlDest.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002425 GenArithOpLong(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
buzbee2cfc6392012-05-07 14:51:40 -07002426 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002427 GenArithOpInt(cUnit, dalvikOp, rlDest, rlSrc1, rlSrc2);
buzbee2cfc6392012-05-07 14:51:40 -07002428 }
2429 }
2430}
2431
buzbeeaad94382012-11-21 07:40:50 -08002432static void CvtShiftOp(CompilationUnit* cUnit, Instruction::Code opcode, llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002433{
buzbee2a83e8f2012-07-13 16:42:30 -07002434 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
buzbee52a77fc2012-11-20 19:50:46 -08002435 RegLocation rlDest = GetLoc(cUnit, callInst);
2436 RegLocation rlSrc = GetLoc(cUnit, callInst->getArgOperand(0));
buzbee2a83e8f2012-07-13 16:42:30 -07002437 llvm::Value* rhs = callInst->getArgOperand(1);
2438 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
2439 DCHECK(!rlDest.wide);
buzbee52a77fc2012-11-20 19:50:46 -08002440 GenArithOpIntLit(cUnit, opcode, rlDest, rlSrc, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002441 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002442 RegLocation rlShift = GetLoc(cUnit, rhs);
buzbee2a83e8f2012-07-13 16:42:30 -07002443 if (callInst->getType() == cUnit->irb->getInt64Ty()) {
buzbee52a77fc2012-11-20 19:50:46 -08002444 GenShiftOpLong(cUnit, opcode, rlDest, rlSrc, rlShift);
buzbee2a83e8f2012-07-13 16:42:30 -07002445 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002446 GenArithOpInt(cUnit, opcode, rlDest, rlSrc, rlShift);
buzbee2a83e8f2012-07-13 16:42:30 -07002447 }
buzbee101305f2012-06-28 18:00:56 -07002448 }
2449}
2450
buzbeeaad94382012-11-21 07:40:50 -08002451static void CvtBr(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002452{
2453 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(inst);
2454 DCHECK(brInst != NULL);
2455 DCHECK(brInst->isUnconditional()); // May change - but this is all we use now
2456 llvm::BasicBlock* targetBB = brInst->getSuccessor(0);
buzbee52a77fc2012-11-20 19:50:46 -08002457 OpUnconditionalBranch(cUnit, cUnit->blockToLabelMap.Get(targetBB));
buzbee2cfc6392012-05-07 14:51:40 -07002458}
2459
buzbeeaad94382012-11-21 07:40:50 -08002460static void CvtPhi(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002461{
2462 // Nop - these have already been processed
2463}
2464
buzbeeaad94382012-11-21 07:40:50 -08002465static void CvtRet(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002466{
2467 llvm::ReturnInst* retInst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2468 llvm::Value* retVal = retInst->getReturnValue();
2469 if (retVal != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -08002470 RegLocation rlSrc = GetLoc(cUnit, retVal);
buzbee2cfc6392012-05-07 14:51:40 -07002471 if (rlSrc.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002472 StoreValueWide(cUnit, GetReturnWide(cUnit, rlSrc.fp), rlSrc);
buzbee2cfc6392012-05-07 14:51:40 -07002473 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002474 StoreValue(cUnit, GetReturn(cUnit, rlSrc.fp), rlSrc);
buzbee2cfc6392012-05-07 14:51:40 -07002475 }
2476 }
buzbee52a77fc2012-11-20 19:50:46 -08002477 GenExitSequence(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002478}
2479
buzbeeaad94382012-11-21 07:40:50 -08002480static ConditionCode GetCond(llvm::ICmpInst::Predicate llvmCond)
buzbee2cfc6392012-05-07 14:51:40 -07002481{
2482 ConditionCode res = kCondAl;
2483 switch(llvmCond) {
buzbee6969d502012-06-15 16:40:31 -07002484 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002485 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2486 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2487 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002488 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002489 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002490 default: LOG(FATAL) << "Unexpected llvm condition";
2491 }
2492 return res;
2493}
2494
buzbeeaad94382012-11-21 07:40:50 -08002495static void CvtICmp(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002496{
buzbee52a77fc2012-11-20 19:50:46 -08002497 // GenCmpLong(cUnit, rlDest, rlSrc1, rlSrc2)
buzbee2cfc6392012-05-07 14:51:40 -07002498 UNIMPLEMENTED(FATAL);
2499}
2500
buzbeeaad94382012-11-21 07:40:50 -08002501static void CvtICmpBr(CompilationUnit* cUnit, llvm::Instruction* inst,
buzbee2cfc6392012-05-07 14:51:40 -07002502 llvm::BranchInst* brInst)
2503{
2504 // Get targets
2505 llvm::BasicBlock* takenBB = brInst->getSuccessor(0);
2506 LIR* taken = cUnit->blockToLabelMap.Get(takenBB);
2507 llvm::BasicBlock* fallThroughBB = brInst->getSuccessor(1);
2508 LIR* fallThrough = cUnit->blockToLabelMap.Get(fallThroughBB);
2509 // Get comparison operands
2510 llvm::ICmpInst* iCmpInst = llvm::dyn_cast<llvm::ICmpInst>(inst);
buzbee52a77fc2012-11-20 19:50:46 -08002511 ConditionCode cond = GetCond(iCmpInst->getPredicate());
buzbee2cfc6392012-05-07 14:51:40 -07002512 llvm::Value* lhs = iCmpInst->getOperand(0);
2513 // Not expecting a constant as 1st operand
2514 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
buzbee52a77fc2012-11-20 19:50:46 -08002515 RegLocation rlSrc1 = GetLoc(cUnit, inst->getOperand(0));
2516 rlSrc1 = LoadValue(cUnit, rlSrc1, kCoreReg);
buzbee2cfc6392012-05-07 14:51:40 -07002517 llvm::Value* rhs = inst->getOperand(1);
buzbeeb046e162012-10-30 15:48:42 -07002518 if (cUnit->instructionSet == kMips) {
2519 // Compare and branch in one shot
2520 UNIMPLEMENTED(FATAL);
2521 }
buzbee2cfc6392012-05-07 14:51:40 -07002522 //Compare, then branch
2523 // TODO: handle fused CMP_LONG/IF_xxZ case
2524 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
buzbee52a77fc2012-11-20 19:50:46 -08002525 OpRegImm(cUnit, kOpCmp, rlSrc1.lowReg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002526 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -08002527 OpRegImm(cUnit, kOpCmp, rlSrc1.lowReg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002528 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002529 RegLocation rlSrc2 = GetLoc(cUnit, rhs);
2530 rlSrc2 = LoadValue(cUnit, rlSrc2, kCoreReg);
2531 OpRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee2cfc6392012-05-07 14:51:40 -07002532 }
buzbee52a77fc2012-11-20 19:50:46 -08002533 OpCondBranch(cUnit, cond, taken);
buzbee2cfc6392012-05-07 14:51:40 -07002534 // Fallthrough
buzbee52a77fc2012-11-20 19:50:46 -08002535 OpUnconditionalBranch(cUnit, fallThrough);
buzbee2cfc6392012-05-07 14:51:40 -07002536}
2537
buzbeeaad94382012-11-21 07:40:50 -08002538static void CvtCopy(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee2cfc6392012-05-07 14:51:40 -07002539{
buzbee4f1181f2012-06-22 13:52:12 -07002540 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee52a77fc2012-11-20 19:50:46 -08002541 RegLocation rlSrc = GetLoc(cUnit, callInst->getArgOperand(0));
2542 RegLocation rlDest = GetLoc(cUnit, callInst);
buzbee76592632012-06-29 15:18:35 -07002543 DCHECK_EQ(rlSrc.wide, rlDest.wide);
2544 DCHECK_EQ(rlSrc.fp, rlDest.fp);
buzbee2cfc6392012-05-07 14:51:40 -07002545 if (rlSrc.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002546 StoreValueWide(cUnit, rlDest, rlSrc);
buzbee2cfc6392012-05-07 14:51:40 -07002547 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002548 StoreValue(cUnit, rlDest, rlSrc);
buzbee2cfc6392012-05-07 14:51:40 -07002549 }
2550}
2551
2552// Note: Immediate arg is a ConstantInt regardless of result type
buzbeeaad94382012-11-21 07:40:50 -08002553static void CvtConst(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee2cfc6392012-05-07 14:51:40 -07002554{
buzbee4f1181f2012-06-22 13:52:12 -07002555 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002556 llvm::ConstantInt* src =
2557 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2558 uint64_t immval = src->getZExtValue();
buzbee52a77fc2012-11-20 19:50:46 -08002559 RegLocation rlDest = GetLoc(cUnit, callInst);
2560 RegLocation rlResult = EvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee2cfc6392012-05-07 14:51:40 -07002561 if (rlDest.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08002562 LoadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee2cfc6392012-05-07 14:51:40 -07002563 (immval) & 0xffffffff, (immval >> 32) & 0xffffffff);
buzbee52a77fc2012-11-20 19:50:46 -08002564 StoreValueWide(cUnit, rlDest, rlResult);
buzbee2cfc6392012-05-07 14:51:40 -07002565 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002566 LoadConstantNoClobber(cUnit, rlResult.lowReg, immval & 0xffffffff);
2567 StoreValue(cUnit, rlDest, rlResult);
buzbee2cfc6392012-05-07 14:51:40 -07002568 }
2569}
2570
buzbeeaad94382012-11-21 07:40:50 -08002571static void CvtConstObject(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isString)
buzbee6969d502012-06-15 16:40:31 -07002572{
buzbee4f1181f2012-06-22 13:52:12 -07002573 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee101305f2012-06-28 18:00:56 -07002574 llvm::ConstantInt* idxVal =
buzbee6969d502012-06-15 16:40:31 -07002575 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee101305f2012-06-28 18:00:56 -07002576 uint32_t index = idxVal->getZExtValue();
buzbee52a77fc2012-11-20 19:50:46 -08002577 RegLocation rlDest = GetLoc(cUnit, callInst);
buzbee101305f2012-06-28 18:00:56 -07002578 if (isString) {
buzbee52a77fc2012-11-20 19:50:46 -08002579 GenConstString(cUnit, index, rlDest);
buzbee101305f2012-06-28 18:00:56 -07002580 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002581 GenConstClass(cUnit, index, rlDest);
buzbee101305f2012-06-28 18:00:56 -07002582 }
2583}
2584
buzbeeaad94382012-11-21 07:40:50 -08002585static void CvtFillArrayData(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002586{
2587 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2588 llvm::ConstantInt* offsetVal =
2589 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee52a77fc2012-11-20 19:50:46 -08002590 RegLocation rlSrc = GetLoc(cUnit, callInst->getArgOperand(1));
2591 GenFillArrayData(cUnit, offsetVal->getSExtValue(), rlSrc);
buzbee6969d502012-06-15 16:40:31 -07002592}
2593
buzbeeaad94382012-11-21 07:40:50 -08002594static void CvtNewInstance(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee4f1181f2012-06-22 13:52:12 -07002595{
buzbee32412962012-06-26 16:27:56 -07002596 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002597 llvm::ConstantInt* typeIdxVal =
2598 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2599 uint32_t typeIdx = typeIdxVal->getZExtValue();
buzbee52a77fc2012-11-20 19:50:46 -08002600 RegLocation rlDest = GetLoc(cUnit, callInst);
2601 GenNewInstance(cUnit, typeIdx, rlDest);
buzbee4f1181f2012-06-22 13:52:12 -07002602}
2603
buzbeeaad94382012-11-21 07:40:50 -08002604static void CvtNewArray(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002605{
2606 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2607 llvm::ConstantInt* typeIdxVal =
2608 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2609 uint32_t typeIdx = typeIdxVal->getZExtValue();
2610 llvm::Value* len = callInst->getArgOperand(1);
buzbee52a77fc2012-11-20 19:50:46 -08002611 RegLocation rlLen = GetLoc(cUnit, len);
2612 RegLocation rlDest = GetLoc(cUnit, callInst);
2613 GenNewArray(cUnit, typeIdx, rlDest, rlLen);
buzbee8fa0fda2012-06-27 15:44:52 -07002614}
2615
buzbeeaad94382012-11-21 07:40:50 -08002616static void CvtInstanceOf(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002617{
2618 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2619 llvm::ConstantInt* typeIdxVal =
2620 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2621 uint32_t typeIdx = typeIdxVal->getZExtValue();
2622 llvm::Value* src = callInst->getArgOperand(1);
buzbee52a77fc2012-11-20 19:50:46 -08002623 RegLocation rlSrc = GetLoc(cUnit, src);
2624 RegLocation rlDest = GetLoc(cUnit, callInst);
2625 GenInstanceof(cUnit, typeIdx, rlDest, rlSrc);
buzbee8fa0fda2012-06-27 15:44:52 -07002626}
2627
buzbeeaad94382012-11-21 07:40:50 -08002628static void CvtThrow(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee32412962012-06-26 16:27:56 -07002629{
2630 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
2631 llvm::Value* src = callInst->getArgOperand(0);
buzbee52a77fc2012-11-20 19:50:46 -08002632 RegLocation rlSrc = GetLoc(cUnit, src);
2633 GenThrow(cUnit, rlSrc);
buzbee32412962012-06-26 16:27:56 -07002634}
2635
buzbeeaad94382012-11-21 07:40:50 -08002636static void CvtMonitorEnterExit(CompilationUnit* cUnit, bool isEnter,
buzbee8fa0fda2012-06-27 15:44:52 -07002637 llvm::CallInst* callInst)
2638{
2639 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2640 llvm::ConstantInt* optFlags =
2641 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2642 llvm::Value* src = callInst->getArgOperand(1);
buzbee52a77fc2012-11-20 19:50:46 -08002643 RegLocation rlSrc = GetLoc(cUnit, src);
buzbee8fa0fda2012-06-27 15:44:52 -07002644 if (isEnter) {
buzbee52a77fc2012-11-20 19:50:46 -08002645 GenMonitorEnter(cUnit, optFlags->getZExtValue(), rlSrc);
buzbee8fa0fda2012-06-27 15:44:52 -07002646 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002647 GenMonitorExit(cUnit, optFlags->getZExtValue(), rlSrc);
buzbee8fa0fda2012-06-27 15:44:52 -07002648 }
2649}
2650
buzbeeaad94382012-11-21 07:40:50 -08002651static void CvtArrayLength(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee8fa0fda2012-06-27 15:44:52 -07002652{
2653 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2654 llvm::ConstantInt* optFlags =
2655 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2656 llvm::Value* src = callInst->getArgOperand(1);
buzbee52a77fc2012-11-20 19:50:46 -08002657 RegLocation rlSrc = GetLoc(cUnit, src);
2658 rlSrc = LoadValue(cUnit, rlSrc, kCoreReg);
2659 GenNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg, optFlags->getZExtValue());
2660 RegLocation rlDest = GetLoc(cUnit, callInst);
2661 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee8fa0fda2012-06-27 15:44:52 -07002662 int lenOffset = Array::LengthOffset().Int32Value();
buzbee52a77fc2012-11-20 19:50:46 -08002663 LoadWordDisp(cUnit, rlSrc.lowReg, lenOffset, rlResult.lowReg);
2664 StoreValue(cUnit, rlDest, rlResult);
buzbee8fa0fda2012-06-27 15:44:52 -07002665}
2666
buzbeeaad94382012-11-21 07:40:50 -08002667static void CvtMoveException(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee32412962012-06-26 16:27:56 -07002668{
buzbee52a77fc2012-11-20 19:50:46 -08002669 RegLocation rlDest = GetLoc(cUnit, callInst);
2670 GenMoveException(cUnit, rlDest);
buzbee32412962012-06-26 16:27:56 -07002671}
2672
buzbeeaad94382012-11-21 07:40:50 -08002673static void CvtSget(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide, bool isObject)
buzbee4f1181f2012-06-22 13:52:12 -07002674{
buzbee32412962012-06-26 16:27:56 -07002675 DCHECK_EQ(callInst->getNumArgOperands(), 1U);
buzbee4f1181f2012-06-22 13:52:12 -07002676 llvm::ConstantInt* typeIdxVal =
2677 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2678 uint32_t typeIdx = typeIdxVal->getZExtValue();
buzbee52a77fc2012-11-20 19:50:46 -08002679 RegLocation rlDest = GetLoc(cUnit, callInst);
2680 GenSget(cUnit, typeIdx, rlDest, isWide, isObject);
buzbee4f1181f2012-06-22 13:52:12 -07002681}
2682
buzbeeaad94382012-11-21 07:40:50 -08002683static void CvtSput(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isWide, bool isObject)
buzbee8fa0fda2012-06-27 15:44:52 -07002684{
2685 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2686 llvm::ConstantInt* typeIdxVal =
2687 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2688 uint32_t typeIdx = typeIdxVal->getZExtValue();
2689 llvm::Value* src = callInst->getArgOperand(1);
buzbee52a77fc2012-11-20 19:50:46 -08002690 RegLocation rlSrc = GetLoc(cUnit, src);
2691 GenSput(cUnit, typeIdx, rlSrc, isWide, isObject);
buzbee8fa0fda2012-06-27 15:44:52 -07002692}
2693
buzbeeaad94382012-11-21 07:40:50 -08002694static void CvtAget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size, int scale)
buzbee8fa0fda2012-06-27 15:44:52 -07002695{
2696 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2697 llvm::ConstantInt* optFlags =
2698 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee52a77fc2012-11-20 19:50:46 -08002699 RegLocation rlArray = GetLoc(cUnit, callInst->getArgOperand(1));
2700 RegLocation rlIndex = GetLoc(cUnit, callInst->getArgOperand(2));
2701 RegLocation rlDest = GetLoc(cUnit, callInst);
2702 GenArrayGet(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
buzbee8fa0fda2012-06-27 15:44:52 -07002703 rlDest, scale);
2704}
2705
buzbeeaad94382012-11-21 07:40:50 -08002706static void CvtAput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2707 int scale, bool isObject)
buzbee8fa0fda2012-06-27 15:44:52 -07002708{
2709 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2710 llvm::ConstantInt* optFlags =
2711 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee52a77fc2012-11-20 19:50:46 -08002712 RegLocation rlSrc = GetLoc(cUnit, callInst->getArgOperand(1));
2713 RegLocation rlArray = GetLoc(cUnit, callInst->getArgOperand(2));
2714 RegLocation rlIndex = GetLoc(cUnit, callInst->getArgOperand(3));
buzbeef1f86362012-07-10 15:18:31 -07002715 if (isObject) {
buzbee52a77fc2012-11-20 19:50:46 -08002716 GenArrayObjPut(cUnit, optFlags->getZExtValue(), rlArray, rlIndex,
buzbeef1f86362012-07-10 15:18:31 -07002717 rlSrc, scale);
2718 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002719 GenArrayPut(cUnit, optFlags->getZExtValue(), size, rlArray, rlIndex,
buzbeef1f86362012-07-10 15:18:31 -07002720 rlSrc, scale);
2721 }
2722}
2723
buzbeeaad94382012-11-21 07:40:50 -08002724static void CvtAputObj(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbeef1f86362012-07-10 15:18:31 -07002725{
buzbee52a77fc2012-11-20 19:50:46 -08002726 CvtAput(cUnit, callInst, kWord, 2, true /* isObject */);
buzbeef1f86362012-07-10 15:18:31 -07002727}
2728
buzbeeaad94382012-11-21 07:40:50 -08002729static void CvtAputPrimitive(CompilationUnit* cUnit, llvm::CallInst* callInst,
buzbeef1f86362012-07-10 15:18:31 -07002730 OpSize size, int scale)
2731{
buzbee52a77fc2012-11-20 19:50:46 -08002732 CvtAput(cUnit, callInst, size, scale, false /* isObject */);
buzbee8fa0fda2012-06-27 15:44:52 -07002733}
2734
buzbeeaad94382012-11-21 07:40:50 -08002735static void CvtIget(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2736 bool isWide, bool isObj)
buzbee101305f2012-06-28 18:00:56 -07002737{
2738 DCHECK_EQ(callInst->getNumArgOperands(), 3U);
2739 llvm::ConstantInt* optFlags =
2740 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee52a77fc2012-11-20 19:50:46 -08002741 RegLocation rlObj = GetLoc(cUnit, callInst->getArgOperand(1));
buzbee101305f2012-06-28 18:00:56 -07002742 llvm::ConstantInt* fieldIdx =
2743 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
buzbee52a77fc2012-11-20 19:50:46 -08002744 RegLocation rlDest = GetLoc(cUnit, callInst);
2745 GenIGet(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
buzbee101305f2012-06-28 18:00:56 -07002746 size, rlDest, rlObj, isWide, isObj);
2747}
2748
buzbeeaad94382012-11-21 07:40:50 -08002749static void CvtIput(CompilationUnit* cUnit, llvm::CallInst* callInst, OpSize size,
2750 bool isWide, bool isObj)
buzbee101305f2012-06-28 18:00:56 -07002751{
2752 DCHECK_EQ(callInst->getNumArgOperands(), 4U);
2753 llvm::ConstantInt* optFlags =
2754 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee52a77fc2012-11-20 19:50:46 -08002755 RegLocation rlSrc = GetLoc(cUnit, callInst->getArgOperand(1));
2756 RegLocation rlObj = GetLoc(cUnit, callInst->getArgOperand(2));
buzbee101305f2012-06-28 18:00:56 -07002757 llvm::ConstantInt* fieldIdx =
buzbee4f4dfc72012-07-02 14:54:44 -07002758 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(3));
buzbee52a77fc2012-11-20 19:50:46 -08002759 GenIPut(cUnit, fieldIdx->getZExtValue(), optFlags->getZExtValue(),
buzbee101305f2012-06-28 18:00:56 -07002760 size, rlSrc, rlObj, isWide, isObj);
2761}
2762
buzbeeaad94382012-11-21 07:40:50 -08002763static void CvtCheckCast(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee101305f2012-06-28 18:00:56 -07002764{
2765 DCHECK_EQ(callInst->getNumArgOperands(), 2U);
2766 llvm::ConstantInt* typeIdx =
2767 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
buzbee52a77fc2012-11-20 19:50:46 -08002768 RegLocation rlSrc = GetLoc(cUnit, callInst->getArgOperand(1));
2769 GenCheckCast(cUnit, typeIdx->getZExtValue(), rlSrc);
buzbee101305f2012-06-28 18:00:56 -07002770}
2771
buzbeeaad94382012-11-21 07:40:50 -08002772static void CvtFPCompare(CompilationUnit* cUnit, llvm::CallInst* callInst,
2773 Instruction::Code opcode)
buzbee76592632012-06-29 15:18:35 -07002774{
buzbee52a77fc2012-11-20 19:50:46 -08002775 RegLocation rlSrc1 = GetLoc(cUnit, callInst->getArgOperand(0));
2776 RegLocation rlSrc2 = GetLoc(cUnit, callInst->getArgOperand(1));
2777 RegLocation rlDest = GetLoc(cUnit, callInst);
2778 GenCmpFP(cUnit, opcode, rlDest, rlSrc1, rlSrc2);
buzbee76592632012-06-29 15:18:35 -07002779}
2780
buzbeeaad94382012-11-21 07:40:50 -08002781static void CvtLongCompare(CompilationUnit* cUnit, llvm::CallInst* callInst)
buzbee76592632012-06-29 15:18:35 -07002782{
buzbee52a77fc2012-11-20 19:50:46 -08002783 RegLocation rlSrc1 = GetLoc(cUnit, callInst->getArgOperand(0));
2784 RegLocation rlSrc2 = GetLoc(cUnit, callInst->getArgOperand(1));
2785 RegLocation rlDest = GetLoc(cUnit, callInst);
2786 GenCmpLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbee76592632012-06-29 15:18:35 -07002787}
2788
buzbeeaad94382012-11-21 07:40:50 -08002789static void CvtSwitch(CompilationUnit* cUnit, llvm::Instruction* inst)
buzbeef58c12c2012-07-03 15:06:29 -07002790{
2791 llvm::SwitchInst* swInst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2792 DCHECK(swInst != NULL);
2793 llvm::Value* testVal = swInst->getCondition();
2794 llvm::MDNode* tableOffsetNode = swInst->getMetadata("SwitchTable");
2795 DCHECK(tableOffsetNode != NULL);
2796 llvm::ConstantInt* tableOffsetValue =
2797 static_cast<llvm::ConstantInt*>(tableOffsetNode->getOperand(0));
2798 int32_t tableOffset = tableOffsetValue->getSExtValue();
buzbee52a77fc2012-11-20 19:50:46 -08002799 RegLocation rlSrc = GetLoc(cUnit, testVal);
buzbeeeaf09bc2012-11-15 14:51:41 -08002800 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
2801 uint16_t tableMagic = *table;
buzbeea1da8a52012-07-09 14:00:21 -07002802 if (tableMagic == 0x100) {
buzbee52a77fc2012-11-20 19:50:46 -08002803 GenPackedSwitch(cUnit, tableOffset, rlSrc);
buzbeea1da8a52012-07-09 14:00:21 -07002804 } else {
2805 DCHECK_EQ(tableMagic, 0x200);
buzbee52a77fc2012-11-20 19:50:46 -08002806 GenSparseSwitch(cUnit, tableOffset, rlSrc);
buzbeea1da8a52012-07-09 14:00:21 -07002807 }
buzbeef58c12c2012-07-03 15:06:29 -07002808}
2809
buzbeeaad94382012-11-21 07:40:50 -08002810static void CvtInvoke(CompilationUnit* cUnit, llvm::CallInst* callInst, bool isVoid,
2811 bool isFilledNewArray)
buzbee6969d502012-06-15 16:40:31 -07002812{
buzbee52a77fc2012-11-20 19:50:46 -08002813 CallInfo* info = static_cast<CallInfo*>(NewMem(cUnit, sizeof(CallInfo), true, kAllocMisc));
buzbee8fa0fda2012-06-27 15:44:52 -07002814 if (isVoid) {
buzbee6969d502012-06-15 16:40:31 -07002815 info->result.location = kLocInvalid;
2816 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002817 info->result = GetLoc(cUnit, callInst);
buzbee6969d502012-06-15 16:40:31 -07002818 }
2819 llvm::ConstantInt* invokeTypeVal =
2820 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(0));
2821 llvm::ConstantInt* methodIndexVal =
2822 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(1));
2823 llvm::ConstantInt* optFlagsVal =
2824 llvm::dyn_cast<llvm::ConstantInt>(callInst->getArgOperand(2));
2825 info->type = static_cast<InvokeType>(invokeTypeVal->getZExtValue());
2826 info->index = methodIndexVal->getZExtValue();
2827 info->optFlags = optFlagsVal->getZExtValue();
2828 info->offset = cUnit->currentDalvikOffset;
2829
buzbee6969d502012-06-15 16:40:31 -07002830 // Count the argument words, and then build argument array.
2831 info->numArgWords = 0;
2832 for (unsigned int i = 3; i < callInst->getNumArgOperands(); i++) {
buzbee52a77fc2012-11-20 19:50:46 -08002833 RegLocation tLoc = GetLoc(cUnit, callInst->getArgOperand(i));
buzbee6969d502012-06-15 16:40:31 -07002834 info->numArgWords += tLoc.wide ? 2 : 1;
2835 }
buzbeecbd6d442012-11-17 14:11:25 -08002836 info->args = (info->numArgWords == 0) ? NULL : static_cast<RegLocation*>
buzbee52a77fc2012-11-20 19:50:46 -08002837 (NewMem(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc));
buzbee6969d502012-06-15 16:40:31 -07002838 // Now, fill in the location records, synthesizing high loc of wide vals
2839 for (int i = 3, next = 0; next < info->numArgWords;) {
buzbee52a77fc2012-11-20 19:50:46 -08002840 info->args[next] = GetLoc(cUnit, callInst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002841 if (info->args[next].wide) {
2842 next++;
2843 // TODO: Might make sense to mark this as an invalid loc
2844 info->args[next].origSReg = info->args[next-1].origSReg+1;
2845 info->args[next].sRegLow = info->args[next-1].sRegLow+1;
2846 }
2847 next++;
2848 }
buzbee4f4dfc72012-07-02 14:54:44 -07002849 // TODO - rework such that we no longer need isRange
2850 info->isRange = (info->numArgWords > 5);
2851
buzbee76592632012-06-29 15:18:35 -07002852 if (isFilledNewArray) {
buzbee52a77fc2012-11-20 19:50:46 -08002853 GenFilledNewArray(cUnit, info);
buzbee101305f2012-06-28 18:00:56 -07002854 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002855 GenInvoke(cUnit, info);
buzbee101305f2012-06-28 18:00:56 -07002856 }
buzbee6969d502012-06-15 16:40:31 -07002857}
2858
buzbeead8f15e2012-06-18 14:49:45 -07002859/* Look up the RegLocation associated with a Value. Must already be defined */
buzbeeaad94382012-11-21 07:40:50 -08002860static RegLocation ValToLoc(CompilationUnit* cUnit, llvm::Value* val)
buzbeead8f15e2012-06-18 14:49:45 -07002861{
2862 SafeMap<llvm::Value*, RegLocation>::iterator it = cUnit->locMap.find(val);
2863 DCHECK(it != cUnit->locMap.end()) << "Missing definition";
2864 return it->second;
2865}
2866
buzbeeaad94382012-11-21 07:40:50 -08002867static bool BitcodeBlockCodeGen(CompilationUnit* cUnit, llvm::BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07002868{
buzbee0967a252012-09-14 10:43:54 -07002869 while (cUnit->llvmBlocks.find(bb) == cUnit->llvmBlocks.end()) {
2870 llvm::BasicBlock* nextBB = NULL;
2871 cUnit->llvmBlocks.insert(bb);
2872 bool isEntry = (bb == &cUnit->func->getEntryBlock());
2873 // Define the starting label
2874 LIR* blockLabel = cUnit->blockToLabelMap.Get(bb);
2875 // Extract the type and starting offset from the block's name
buzbee951c0a12012-10-03 16:31:39 -07002876 char blockType = kInvalidBlock;
2877 if (isEntry) {
2878 blockType = kNormalBlock;
2879 blockLabel->operands[0] = 0;
2880 } else if (!bb->hasName()) {
2881 blockType = kNormalBlock;
2882 blockLabel->operands[0] = DexFile::kDexNoIndex;
buzbee0967a252012-09-14 10:43:54 -07002883 } else {
buzbee951c0a12012-10-03 16:31:39 -07002884 std::string blockName = bb->getName().str();
2885 int dummy;
2886 sscanf(blockName.c_str(), kLabelFormat, &blockType, &blockLabel->operands[0], &dummy);
2887 cUnit->currentDalvikOffset = blockLabel->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002888 }
buzbee951c0a12012-10-03 16:31:39 -07002889 DCHECK((blockType == kNormalBlock) || (blockType == kCatchBlock));
2890 cUnit->currentDalvikOffset = blockLabel->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002891 // Set the label kind
2892 blockLabel->opcode = kPseudoNormalBlockLabel;
2893 // Insert the label
buzbee52a77fc2012-11-20 19:50:46 -08002894 AppendLIR(cUnit, blockLabel);
buzbee2cfc6392012-05-07 14:51:40 -07002895
buzbee0967a252012-09-14 10:43:54 -07002896 LIR* headLIR = NULL;
buzbee8320f382012-09-11 16:29:42 -07002897
buzbee0967a252012-09-14 10:43:54 -07002898 if (blockType == kCatchBlock) {
buzbee52a77fc2012-11-20 19:50:46 -08002899 headLIR = NewLIR0(cUnit, kPseudoExportedPC);
buzbee0967a252012-09-14 10:43:54 -07002900 }
buzbee8320f382012-09-11 16:29:42 -07002901
buzbee0967a252012-09-14 10:43:54 -07002902 // Free temp registers and reset redundant store tracking */
buzbee52a77fc2012-11-20 19:50:46 -08002903 ResetRegPool(cUnit);
2904 ResetDefTracking(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002905
buzbee0967a252012-09-14 10:43:54 -07002906 //TODO: restore oat incoming liveness optimization
buzbee52a77fc2012-11-20 19:50:46 -08002907 ClobberAllRegs(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07002908
buzbee0967a252012-09-14 10:43:54 -07002909 if (isEntry) {
buzbee52a77fc2012-11-20 19:50:46 -08002910 RegLocation* ArgLocs = static_cast<RegLocation*>
2911 (NewMem(cUnit, sizeof(RegLocation) * cUnit->numIns, true, kAllocMisc));
buzbee0967a252012-09-14 10:43:54 -07002912 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
2913 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
2914 // Skip past Method*
2915 it++;
2916 for (unsigned i = 0; it != it_end; ++it) {
2917 llvm::Value* val = it;
buzbee52a77fc2012-11-20 19:50:46 -08002918 ArgLocs[i++] = ValToLoc(cUnit, val);
buzbee0967a252012-09-14 10:43:54 -07002919 llvm::Type* ty = val->getType();
2920 if ((ty == cUnit->irb->getInt64Ty()) || (ty == cUnit->irb->getDoubleTy())) {
buzbee52a77fc2012-11-20 19:50:46 -08002921 ArgLocs[i] = ArgLocs[i-1];
2922 ArgLocs[i].lowReg = ArgLocs[i].highReg;
2923 ArgLocs[i].origSReg++;
2924 ArgLocs[i].sRegLow = INVALID_SREG;
2925 ArgLocs[i].highWord = true;
buzbee0967a252012-09-14 10:43:54 -07002926 i++;
2927 }
2928 }
buzbee52a77fc2012-11-20 19:50:46 -08002929 GenEntrySequence(cUnit, ArgLocs, cUnit->methodLoc);
buzbee0967a252012-09-14 10:43:54 -07002930 }
2931
2932 // Visit all of the instructions in the block
2933 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2934 llvm::Instruction* inst = it;
2935 llvm::BasicBlock::iterator nextIt = ++it;
2936 // Extract the Dalvik offset from the instruction
2937 uint32_t opcode = inst->getOpcode();
2938 llvm::MDNode* dexOffsetNode = inst->getMetadata("DexOff");
2939 if (dexOffsetNode != NULL) {
2940 llvm::ConstantInt* dexOffsetValue =
2941 static_cast<llvm::ConstantInt*>(dexOffsetNode->getOperand(0));
2942 cUnit->currentDalvikOffset = dexOffsetValue->getZExtValue();
2943 }
2944
buzbee52a77fc2012-11-20 19:50:46 -08002945 ResetRegPool(cUnit);
buzbee0967a252012-09-14 10:43:54 -07002946 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
buzbee52a77fc2012-11-20 19:50:46 -08002947 ClobberAllRegs(cUnit);
buzbee0967a252012-09-14 10:43:54 -07002948 }
2949
2950 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
buzbee52a77fc2012-11-20 19:50:46 -08002951 ResetDefTracking(cUnit);
buzbee0967a252012-09-14 10:43:54 -07002952 }
2953
2954 #ifndef NDEBUG
2955 /* Reset temp tracking sanity check */
2956 cUnit->liveSReg = INVALID_SREG;
2957 #endif
2958
2959 // TODO: use llvm opcode name here instead of "boundary" if verbose
buzbee52a77fc2012-11-20 19:50:46 -08002960 LIR* boundaryLIR = MarkBoundary(cUnit, cUnit->currentDalvikOffset, "boundary");
buzbee0967a252012-09-14 10:43:54 -07002961
2962 /* Remember the first LIR for thisl block*/
2963 if (headLIR == NULL) {
2964 headLIR = boundaryLIR;
2965 headLIR->defMask = ENCODE_ALL;
2966 }
2967
2968 switch(opcode) {
2969
2970 case llvm::Instruction::ICmp: {
2971 llvm::Instruction* nextInst = nextIt;
2972 llvm::BranchInst* brInst = llvm::dyn_cast<llvm::BranchInst>(nextInst);
2973 if (brInst != NULL /* and... */) {
buzbee52a77fc2012-11-20 19:50:46 -08002974 CvtICmpBr(cUnit, inst, brInst);
buzbee0967a252012-09-14 10:43:54 -07002975 ++it;
2976 } else {
buzbee52a77fc2012-11-20 19:50:46 -08002977 CvtICmp(cUnit, inst);
buzbee0967a252012-09-14 10:43:54 -07002978 }
2979 }
2980 break;
2981
2982 case llvm::Instruction::Call: {
2983 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(inst);
2984 llvm::Function* callee = callInst->getCalledFunction();
2985 greenland::IntrinsicHelper::IntrinsicId id =
2986 cUnit->intrinsic_helper->GetIntrinsicId(callee);
2987 switch (id) {
2988 case greenland::IntrinsicHelper::AllocaShadowFrame:
2989 case greenland::IntrinsicHelper::SetShadowFrameEntry:
2990 case greenland::IntrinsicHelper::PopShadowFrame:
TDYa1278e950c12012-11-02 09:58:19 -07002991 case greenland::IntrinsicHelper::SetVReg:
buzbee0967a252012-09-14 10:43:54 -07002992 // Ignore shadow frame stuff for quick compiler
2993 break;
2994 case greenland::IntrinsicHelper::CopyInt:
2995 case greenland::IntrinsicHelper::CopyObj:
2996 case greenland::IntrinsicHelper::CopyFloat:
2997 case greenland::IntrinsicHelper::CopyLong:
2998 case greenland::IntrinsicHelper::CopyDouble:
buzbee52a77fc2012-11-20 19:50:46 -08002999 CvtCopy(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003000 break;
3001 case greenland::IntrinsicHelper::ConstInt:
3002 case greenland::IntrinsicHelper::ConstObj:
3003 case greenland::IntrinsicHelper::ConstLong:
3004 case greenland::IntrinsicHelper::ConstFloat:
3005 case greenland::IntrinsicHelper::ConstDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003006 CvtConst(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003007 break;
3008 case greenland::IntrinsicHelper::DivInt:
3009 case greenland::IntrinsicHelper::DivLong:
buzbee52a77fc2012-11-20 19:50:46 -08003010 CvtBinOp(cUnit, kOpDiv, inst);
buzbee0967a252012-09-14 10:43:54 -07003011 break;
3012 case greenland::IntrinsicHelper::RemInt:
3013 case greenland::IntrinsicHelper::RemLong:
buzbee52a77fc2012-11-20 19:50:46 -08003014 CvtBinOp(cUnit, kOpRem, inst);
buzbee0967a252012-09-14 10:43:54 -07003015 break;
3016 case greenland::IntrinsicHelper::MethodInfo:
3017 // Already dealt with - just ignore it here.
3018 break;
3019 case greenland::IntrinsicHelper::CheckSuspend:
buzbee52a77fc2012-11-20 19:50:46 -08003020 GenSuspendTest(cUnit, 0 /* optFlags already applied */);
buzbee0967a252012-09-14 10:43:54 -07003021 break;
3022 case greenland::IntrinsicHelper::HLInvokeObj:
3023 case greenland::IntrinsicHelper::HLInvokeFloat:
3024 case greenland::IntrinsicHelper::HLInvokeDouble:
3025 case greenland::IntrinsicHelper::HLInvokeLong:
3026 case greenland::IntrinsicHelper::HLInvokeInt:
buzbee52a77fc2012-11-20 19:50:46 -08003027 CvtInvoke(cUnit, callInst, false /* isVoid */, false /* newArray */);
buzbee0967a252012-09-14 10:43:54 -07003028 break;
3029 case greenland::IntrinsicHelper::HLInvokeVoid:
buzbee52a77fc2012-11-20 19:50:46 -08003030 CvtInvoke(cUnit, callInst, true /* isVoid */, false /* newArray */);
buzbee0967a252012-09-14 10:43:54 -07003031 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003032 case greenland::IntrinsicHelper::HLFilledNewArray:
buzbee52a77fc2012-11-20 19:50:46 -08003033 CvtInvoke(cUnit, callInst, false /* isVoid */, true /* newArray */);
buzbee0967a252012-09-14 10:43:54 -07003034 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003035 case greenland::IntrinsicHelper::HLFillArrayData:
buzbee52a77fc2012-11-20 19:50:46 -08003036 CvtFillArrayData(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003037 break;
3038 case greenland::IntrinsicHelper::ConstString:
buzbee52a77fc2012-11-20 19:50:46 -08003039 CvtConstObject(cUnit, callInst, true /* isString */);
buzbee0967a252012-09-14 10:43:54 -07003040 break;
3041 case greenland::IntrinsicHelper::ConstClass:
buzbee52a77fc2012-11-20 19:50:46 -08003042 CvtConstObject(cUnit, callInst, false /* isString */);
buzbee0967a252012-09-14 10:43:54 -07003043 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003044 case greenland::IntrinsicHelper::HLCheckCast:
buzbee52a77fc2012-11-20 19:50:46 -08003045 CvtCheckCast(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003046 break;
3047 case greenland::IntrinsicHelper::NewInstance:
buzbee52a77fc2012-11-20 19:50:46 -08003048 CvtNewInstance(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003049 break;
3050 case greenland::IntrinsicHelper::HLSgetObject:
buzbee52a77fc2012-11-20 19:50:46 -08003051 CvtSget(cUnit, callInst, false /* wide */, true /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003052 break;
3053 case greenland::IntrinsicHelper::HLSget:
3054 case greenland::IntrinsicHelper::HLSgetFloat:
3055 case greenland::IntrinsicHelper::HLSgetBoolean:
3056 case greenland::IntrinsicHelper::HLSgetByte:
3057 case greenland::IntrinsicHelper::HLSgetChar:
3058 case greenland::IntrinsicHelper::HLSgetShort:
buzbee52a77fc2012-11-20 19:50:46 -08003059 CvtSget(cUnit, callInst, false /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003060 break;
3061 case greenland::IntrinsicHelper::HLSgetWide:
3062 case greenland::IntrinsicHelper::HLSgetDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003063 CvtSget(cUnit, callInst, true /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003064 break;
3065 case greenland::IntrinsicHelper::HLSput:
3066 case greenland::IntrinsicHelper::HLSputFloat:
3067 case greenland::IntrinsicHelper::HLSputBoolean:
3068 case greenland::IntrinsicHelper::HLSputByte:
3069 case greenland::IntrinsicHelper::HLSputChar:
3070 case greenland::IntrinsicHelper::HLSputShort:
buzbee52a77fc2012-11-20 19:50:46 -08003071 CvtSput(cUnit, callInst, false /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003072 break;
3073 case greenland::IntrinsicHelper::HLSputWide:
3074 case greenland::IntrinsicHelper::HLSputDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003075 CvtSput(cUnit, callInst, true /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003076 break;
3077 case greenland::IntrinsicHelper::HLSputObject:
buzbee52a77fc2012-11-20 19:50:46 -08003078 CvtSput(cUnit, callInst, false /* wide */, true /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003079 break;
3080 case greenland::IntrinsicHelper::GetException:
buzbee52a77fc2012-11-20 19:50:46 -08003081 CvtMoveException(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003082 break;
TDYa127f71bf5a2012-07-29 20:09:52 -07003083 case greenland::IntrinsicHelper::HLThrowException:
buzbee52a77fc2012-11-20 19:50:46 -08003084 CvtThrow(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003085 break;
3086 case greenland::IntrinsicHelper::MonitorEnter:
buzbee52a77fc2012-11-20 19:50:46 -08003087 CvtMonitorEnterExit(cUnit, true /* isEnter */, callInst);
buzbee0967a252012-09-14 10:43:54 -07003088 break;
3089 case greenland::IntrinsicHelper::MonitorExit:
buzbee52a77fc2012-11-20 19:50:46 -08003090 CvtMonitorEnterExit(cUnit, false /* isEnter */, callInst);
buzbee0967a252012-09-14 10:43:54 -07003091 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003092 case greenland::IntrinsicHelper::OptArrayLength:
buzbee52a77fc2012-11-20 19:50:46 -08003093 CvtArrayLength(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003094 break;
3095 case greenland::IntrinsicHelper::NewArray:
buzbee52a77fc2012-11-20 19:50:46 -08003096 CvtNewArray(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003097 break;
3098 case greenland::IntrinsicHelper::InstanceOf:
buzbee52a77fc2012-11-20 19:50:46 -08003099 CvtInstanceOf(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003100 break;
3101
3102 case greenland::IntrinsicHelper::HLArrayGet:
3103 case greenland::IntrinsicHelper::HLArrayGetObject:
3104 case greenland::IntrinsicHelper::HLArrayGetFloat:
buzbee52a77fc2012-11-20 19:50:46 -08003105 CvtAget(cUnit, callInst, kWord, 2);
buzbee0967a252012-09-14 10:43:54 -07003106 break;
3107 case greenland::IntrinsicHelper::HLArrayGetWide:
3108 case greenland::IntrinsicHelper::HLArrayGetDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003109 CvtAget(cUnit, callInst, kLong, 3);
buzbee0967a252012-09-14 10:43:54 -07003110 break;
3111 case greenland::IntrinsicHelper::HLArrayGetBoolean:
buzbee52a77fc2012-11-20 19:50:46 -08003112 CvtAget(cUnit, callInst, kUnsignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003113 break;
3114 case greenland::IntrinsicHelper::HLArrayGetByte:
buzbee52a77fc2012-11-20 19:50:46 -08003115 CvtAget(cUnit, callInst, kSignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003116 break;
3117 case greenland::IntrinsicHelper::HLArrayGetChar:
buzbee52a77fc2012-11-20 19:50:46 -08003118 CvtAget(cUnit, callInst, kUnsignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003119 break;
3120 case greenland::IntrinsicHelper::HLArrayGetShort:
buzbee52a77fc2012-11-20 19:50:46 -08003121 CvtAget(cUnit, callInst, kSignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003122 break;
3123
3124 case greenland::IntrinsicHelper::HLArrayPut:
3125 case greenland::IntrinsicHelper::HLArrayPutFloat:
buzbee52a77fc2012-11-20 19:50:46 -08003126 CvtAputPrimitive(cUnit, callInst, kWord, 2);
buzbee0967a252012-09-14 10:43:54 -07003127 break;
3128 case greenland::IntrinsicHelper::HLArrayPutObject:
buzbee52a77fc2012-11-20 19:50:46 -08003129 CvtAputObj(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003130 break;
3131 case greenland::IntrinsicHelper::HLArrayPutWide:
3132 case greenland::IntrinsicHelper::HLArrayPutDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003133 CvtAputPrimitive(cUnit, callInst, kLong, 3);
buzbee0967a252012-09-14 10:43:54 -07003134 break;
3135 case greenland::IntrinsicHelper::HLArrayPutBoolean:
buzbee52a77fc2012-11-20 19:50:46 -08003136 CvtAputPrimitive(cUnit, callInst, kUnsignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003137 break;
3138 case greenland::IntrinsicHelper::HLArrayPutByte:
buzbee52a77fc2012-11-20 19:50:46 -08003139 CvtAputPrimitive(cUnit, callInst, kSignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003140 break;
3141 case greenland::IntrinsicHelper::HLArrayPutChar:
buzbee52a77fc2012-11-20 19:50:46 -08003142 CvtAputPrimitive(cUnit, callInst, kUnsignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003143 break;
3144 case greenland::IntrinsicHelper::HLArrayPutShort:
buzbee52a77fc2012-11-20 19:50:46 -08003145 CvtAputPrimitive(cUnit, callInst, kSignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003146 break;
3147
3148 case greenland::IntrinsicHelper::HLIGet:
3149 case greenland::IntrinsicHelper::HLIGetFloat:
buzbee52a77fc2012-11-20 19:50:46 -08003150 CvtIget(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003151 break;
3152 case greenland::IntrinsicHelper::HLIGetObject:
buzbee52a77fc2012-11-20 19:50:46 -08003153 CvtIget(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003154 break;
3155 case greenland::IntrinsicHelper::HLIGetWide:
3156 case greenland::IntrinsicHelper::HLIGetDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003157 CvtIget(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003158 break;
3159 case greenland::IntrinsicHelper::HLIGetBoolean:
buzbee52a77fc2012-11-20 19:50:46 -08003160 CvtIget(cUnit, callInst, kUnsignedByte, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003161 false /* obj */);
3162 break;
3163 case greenland::IntrinsicHelper::HLIGetByte:
buzbee52a77fc2012-11-20 19:50:46 -08003164 CvtIget(cUnit, callInst, kSignedByte, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003165 false /* obj */);
3166 break;
3167 case greenland::IntrinsicHelper::HLIGetChar:
buzbee52a77fc2012-11-20 19:50:46 -08003168 CvtIget(cUnit, callInst, kUnsignedHalf, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003169 false /* obj */);
3170 break;
3171 case greenland::IntrinsicHelper::HLIGetShort:
buzbee52a77fc2012-11-20 19:50:46 -08003172 CvtIget(cUnit, callInst, kSignedHalf, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003173 false /* obj */);
3174 break;
3175
3176 case greenland::IntrinsicHelper::HLIPut:
3177 case greenland::IntrinsicHelper::HLIPutFloat:
buzbee52a77fc2012-11-20 19:50:46 -08003178 CvtIput(cUnit, callInst, kWord, false /* isWide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003179 break;
3180 case greenland::IntrinsicHelper::HLIPutObject:
buzbee52a77fc2012-11-20 19:50:46 -08003181 CvtIput(cUnit, callInst, kWord, false /* isWide */, true /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003182 break;
3183 case greenland::IntrinsicHelper::HLIPutWide:
3184 case greenland::IntrinsicHelper::HLIPutDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003185 CvtIput(cUnit, callInst, kLong, true /* isWide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003186 break;
3187 case greenland::IntrinsicHelper::HLIPutBoolean:
buzbee52a77fc2012-11-20 19:50:46 -08003188 CvtIput(cUnit, callInst, kUnsignedByte, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003189 false /* obj */);
3190 break;
3191 case greenland::IntrinsicHelper::HLIPutByte:
buzbee52a77fc2012-11-20 19:50:46 -08003192 CvtIput(cUnit, callInst, kSignedByte, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003193 false /* obj */);
3194 break;
3195 case greenland::IntrinsicHelper::HLIPutChar:
buzbee52a77fc2012-11-20 19:50:46 -08003196 CvtIput(cUnit, callInst, kUnsignedHalf, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003197 false /* obj */);
3198 break;
3199 case greenland::IntrinsicHelper::HLIPutShort:
buzbee52a77fc2012-11-20 19:50:46 -08003200 CvtIput(cUnit, callInst, kSignedHalf, false /* isWide */,
buzbee0967a252012-09-14 10:43:54 -07003201 false /* obj */);
3202 break;
3203
3204 case greenland::IntrinsicHelper::IntToChar:
buzbee52a77fc2012-11-20 19:50:46 -08003205 CvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_CHAR);
buzbee0967a252012-09-14 10:43:54 -07003206 break;
3207 case greenland::IntrinsicHelper::IntToShort:
buzbee52a77fc2012-11-20 19:50:46 -08003208 CvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_SHORT);
buzbee0967a252012-09-14 10:43:54 -07003209 break;
3210 case greenland::IntrinsicHelper::IntToByte:
buzbee52a77fc2012-11-20 19:50:46 -08003211 CvtIntNarrowing(cUnit, callInst, Instruction::INT_TO_BYTE);
buzbee0967a252012-09-14 10:43:54 -07003212 break;
3213
TDYa1274ec8ccd2012-08-11 07:04:57 -07003214 case greenland::IntrinsicHelper::F2I:
3215 case greenland::IntrinsicHelper::D2I:
3216 case greenland::IntrinsicHelper::F2L:
3217 case greenland::IntrinsicHelper::D2L:
buzbee52a77fc2012-11-20 19:50:46 -08003218 CvtFPToInt(cUnit, callInst);
TDYa1274ec8ccd2012-08-11 07:04:57 -07003219 break;
3220
buzbee0967a252012-09-14 10:43:54 -07003221 case greenland::IntrinsicHelper::CmplFloat:
buzbee52a77fc2012-11-20 19:50:46 -08003222 CvtFPCompare(cUnit, callInst, Instruction::CMPL_FLOAT);
buzbee0967a252012-09-14 10:43:54 -07003223 break;
3224 case greenland::IntrinsicHelper::CmpgFloat:
buzbee52a77fc2012-11-20 19:50:46 -08003225 CvtFPCompare(cUnit, callInst, Instruction::CMPG_FLOAT);
buzbee0967a252012-09-14 10:43:54 -07003226 break;
3227 case greenland::IntrinsicHelper::CmplDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003228 CvtFPCompare(cUnit, callInst, Instruction::CMPL_DOUBLE);
buzbee0967a252012-09-14 10:43:54 -07003229 break;
3230 case greenland::IntrinsicHelper::CmpgDouble:
buzbee52a77fc2012-11-20 19:50:46 -08003231 CvtFPCompare(cUnit, callInst, Instruction::CMPG_DOUBLE);
buzbee0967a252012-09-14 10:43:54 -07003232 break;
3233
3234 case greenland::IntrinsicHelper::CmpLong:
buzbee52a77fc2012-11-20 19:50:46 -08003235 CvtLongCompare(cUnit, callInst);
buzbee0967a252012-09-14 10:43:54 -07003236 break;
3237
3238 case greenland::IntrinsicHelper::SHLLong:
buzbee52a77fc2012-11-20 19:50:46 -08003239 CvtShiftOp(cUnit, Instruction::SHL_LONG, callInst);
buzbee0967a252012-09-14 10:43:54 -07003240 break;
3241 case greenland::IntrinsicHelper::SHRLong:
buzbee52a77fc2012-11-20 19:50:46 -08003242 CvtShiftOp(cUnit, Instruction::SHR_LONG, callInst);
buzbee0967a252012-09-14 10:43:54 -07003243 break;
3244 case greenland::IntrinsicHelper::USHRLong:
buzbee52a77fc2012-11-20 19:50:46 -08003245 CvtShiftOp(cUnit, Instruction::USHR_LONG, callInst);
buzbee0967a252012-09-14 10:43:54 -07003246 break;
3247 case greenland::IntrinsicHelper::SHLInt:
buzbee52a77fc2012-11-20 19:50:46 -08003248 CvtShiftOp(cUnit, Instruction::SHL_INT, callInst);
buzbee0967a252012-09-14 10:43:54 -07003249 break;
3250 case greenland::IntrinsicHelper::SHRInt:
buzbee52a77fc2012-11-20 19:50:46 -08003251 CvtShiftOp(cUnit, Instruction::SHR_INT, callInst);
buzbee0967a252012-09-14 10:43:54 -07003252 break;
3253 case greenland::IntrinsicHelper::USHRInt:
buzbee52a77fc2012-11-20 19:50:46 -08003254 CvtShiftOp(cUnit, Instruction::USHR_INT, callInst);
buzbee0967a252012-09-14 10:43:54 -07003255 break;
3256
3257 case greenland::IntrinsicHelper::CatchTargets: {
3258 llvm::SwitchInst* swInst =
3259 llvm::dyn_cast<llvm::SwitchInst>(nextIt);
3260 DCHECK(swInst != NULL);
3261 /*
3262 * Discard the edges and the following conditional branch.
3263 * Do a direct branch to the default target (which is the
3264 * "work" portion of the pair.
3265 * TODO: awful code layout - rework
3266 */
3267 llvm::BasicBlock* targetBB = swInst->getDefaultDest();
3268 DCHECK(targetBB != NULL);
buzbee52a77fc2012-11-20 19:50:46 -08003269 OpUnconditionalBranch(cUnit,
buzbee0967a252012-09-14 10:43:54 -07003270 cUnit->blockToLabelMap.Get(targetBB));
3271 ++it;
3272 // Set next bb to default target - improves code layout
3273 nextBB = targetBB;
3274 }
3275 break;
3276
3277 default:
buzbeecbd6d442012-11-17 14:11:25 -08003278 LOG(FATAL) << "Unexpected intrinsic " << cUnit->intrinsic_helper->GetName(id);
buzbee0967a252012-09-14 10:43:54 -07003279 }
3280 }
3281 break;
3282
buzbee52a77fc2012-11-20 19:50:46 -08003283 case llvm::Instruction::Br: CvtBr(cUnit, inst); break;
3284 case llvm::Instruction::Add: CvtBinOp(cUnit, kOpAdd, inst); break;
3285 case llvm::Instruction::Sub: CvtBinOp(cUnit, kOpSub, inst); break;
3286 case llvm::Instruction::Mul: CvtBinOp(cUnit, kOpMul, inst); break;
3287 case llvm::Instruction::SDiv: CvtBinOp(cUnit, kOpDiv, inst); break;
3288 case llvm::Instruction::SRem: CvtBinOp(cUnit, kOpRem, inst); break;
3289 case llvm::Instruction::And: CvtBinOp(cUnit, kOpAnd, inst); break;
3290 case llvm::Instruction::Or: CvtBinOp(cUnit, kOpOr, inst); break;
3291 case llvm::Instruction::Xor: CvtBinOp(cUnit, kOpXor, inst); break;
3292 case llvm::Instruction::PHI: CvtPhi(cUnit, inst); break;
3293 case llvm::Instruction::Ret: CvtRet(cUnit, inst); break;
3294 case llvm::Instruction::FAdd: CvtBinFPOp(cUnit, kOpAdd, inst); break;
3295 case llvm::Instruction::FSub: CvtBinFPOp(cUnit, kOpSub, inst); break;
3296 case llvm::Instruction::FMul: CvtBinFPOp(cUnit, kOpMul, inst); break;
3297 case llvm::Instruction::FDiv: CvtBinFPOp(cUnit, kOpDiv, inst); break;
3298 case llvm::Instruction::FRem: CvtBinFPOp(cUnit, kOpRem, inst); break;
3299 case llvm::Instruction::SIToFP: CvtIntToFP(cUnit, inst); break;
3300 case llvm::Instruction::FPTrunc: CvtDoubleToFloat(cUnit, inst); break;
3301 case llvm::Instruction::FPExt: CvtFloatToDouble(cUnit, inst); break;
3302 case llvm::Instruction::Trunc: CvtTrunc(cUnit, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003303
buzbee52a77fc2012-11-20 19:50:46 -08003304 case llvm::Instruction::ZExt: CvtIntExt(cUnit, inst, false /* signed */);
buzbee0967a252012-09-14 10:43:54 -07003305 break;
buzbee52a77fc2012-11-20 19:50:46 -08003306 case llvm::Instruction::SExt: CvtIntExt(cUnit, inst, true /* signed */);
buzbee0967a252012-09-14 10:43:54 -07003307 break;
3308
buzbee52a77fc2012-11-20 19:50:46 -08003309 case llvm::Instruction::Switch: CvtSwitch(cUnit, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003310
3311 case llvm::Instruction::Unreachable:
3312 break; // FIXME: can we really ignore these?
3313
3314 case llvm::Instruction::Shl:
3315 case llvm::Instruction::LShr:
3316 case llvm::Instruction::AShr:
3317 case llvm::Instruction::Invoke:
3318 case llvm::Instruction::FPToUI:
TDYa1274ec8ccd2012-08-11 07:04:57 -07003319 case llvm::Instruction::FPToSI:
buzbee0967a252012-09-14 10:43:54 -07003320 case llvm::Instruction::UIToFP:
3321 case llvm::Instruction::PtrToInt:
3322 case llvm::Instruction::IntToPtr:
3323 case llvm::Instruction::FCmp:
3324 case llvm::Instruction::URem:
3325 case llvm::Instruction::UDiv:
3326 case llvm::Instruction::Resume:
3327 case llvm::Instruction::Alloca:
3328 case llvm::Instruction::GetElementPtr:
3329 case llvm::Instruction::Fence:
3330 case llvm::Instruction::AtomicCmpXchg:
3331 case llvm::Instruction::AtomicRMW:
3332 case llvm::Instruction::BitCast:
3333 case llvm::Instruction::VAArg:
3334 case llvm::Instruction::Select:
3335 case llvm::Instruction::UserOp1:
3336 case llvm::Instruction::UserOp2:
3337 case llvm::Instruction::ExtractElement:
3338 case llvm::Instruction::InsertElement:
3339 case llvm::Instruction::ShuffleVector:
3340 case llvm::Instruction::ExtractValue:
3341 case llvm::Instruction::InsertValue:
3342 case llvm::Instruction::LandingPad:
3343 case llvm::Instruction::IndirectBr:
3344 case llvm::Instruction::Load:
3345 case llvm::Instruction::Store:
3346 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3347
3348 default:
3349 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3350 break;
buzbeead8f15e2012-06-18 14:49:45 -07003351 }
3352 }
buzbee2cfc6392012-05-07 14:51:40 -07003353
buzbee0967a252012-09-14 10:43:54 -07003354 if (headLIR != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -08003355 ApplyLocalOptimizations(cUnit, headLIR, cUnit->lastLIRInsn);
buzbee2cfc6392012-05-07 14:51:40 -07003356 }
buzbee0967a252012-09-14 10:43:54 -07003357 if (nextBB != NULL) {
3358 bb = nextBB;
3359 nextBB = NULL;
buzbee6969d502012-06-15 16:40:31 -07003360 }
buzbee6969d502012-06-15 16:40:31 -07003361 }
buzbee2cfc6392012-05-07 14:51:40 -07003362 return false;
3363}
3364
3365/*
3366 * Convert LLVM_IR to MIR:
3367 * o Iterate through the LLVM_IR and construct a graph using
3368 * standard MIR building blocks.
3369 * o Perform a basic-block optimization pass to remove unnecessary
3370 * store/load sequences.
3371 * o Convert the LLVM Value operands into RegLocations where applicable.
3372 * o Create ssaRep def/use operand arrays for each converted LLVM opcode
3373 * o Perform register promotion
3374 * o Iterate through the graph a basic block at a time, generating
3375 * LIR.
3376 * o Assemble LIR as usual.
3377 * o Profit.
3378 */
buzbee52a77fc2012-11-20 19:50:46 -08003379void MethodBitcode2LIR(CompilationUnit* cUnit)
buzbee2cfc6392012-05-07 14:51:40 -07003380{
buzbeead8f15e2012-06-18 14:49:45 -07003381 llvm::Function* func = cUnit->func;
3382 int numBasicBlocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003383 // Allocate a list for LIR basic block labels
3384 cUnit->blockLabelList =
buzbee52a77fc2012-11-20 19:50:46 -08003385 static_cast<LIR*>(NewMem(cUnit, sizeof(LIR) * numBasicBlocks, true, kAllocLIR));
buzbeea1da8a52012-07-09 14:00:21 -07003386 LIR* labelList = cUnit->blockLabelList;
buzbee2cfc6392012-05-07 14:51:40 -07003387 int nextLabel = 0;
buzbeead8f15e2012-06-18 14:49:45 -07003388 for (llvm::Function::iterator i = func->begin(),
3389 e = func->end(); i != e; ++i) {
buzbee2cfc6392012-05-07 14:51:40 -07003390 cUnit->blockToLabelMap.Put(static_cast<llvm::BasicBlock*>(i),
3391 &labelList[nextLabel++]);
3392 }
buzbeead8f15e2012-06-18 14:49:45 -07003393
3394 /*
3395 * Keep honest - clear regLocations, Value => RegLocation,
3396 * promotion map and VmapTables.
3397 */
3398 cUnit->locMap.clear(); // Start fresh
3399 cUnit->regLocation = NULL;
3400 for (int i = 0; i < cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
3401 i++) {
3402 cUnit->promotionMap[i].coreLocation = kLocDalvikFrame;
3403 cUnit->promotionMap[i].fpLocation = kLocDalvikFrame;
3404 }
3405 cUnit->coreSpillMask = 0;
3406 cUnit->numCoreSpills = 0;
3407 cUnit->fpSpillMask = 0;
3408 cUnit->numFPSpills = 0;
3409 cUnit->coreVmapTable.clear();
3410 cUnit->fpVmapTable.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003411
3412 /*
3413 * At this point, we've lost all knowledge of register promotion.
3414 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003415 * exists - not required for correctness). Normally, this will
3416 * be the first instruction we encounter, so we won't have to iterate
3417 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003418 */
buzbeeca7a5e42012-08-20 11:12:18 -07003419 for (llvm::inst_iterator i = llvm::inst_begin(func),
3420 e = llvm::inst_end(func); i != e; ++i) {
3421 llvm::CallInst* callInst = llvm::dyn_cast<llvm::CallInst>(&*i);
3422 if (callInst != NULL) {
3423 llvm::Function* callee = callInst->getCalledFunction();
3424 greenland::IntrinsicHelper::IntrinsicId id =
3425 cUnit->intrinsic_helper->GetIntrinsicId(callee);
3426 if (id == greenland::IntrinsicHelper::MethodInfo) {
3427 if (cUnit->printMe) {
3428 LOG(INFO) << "Found MethodInfo";
3429 }
3430 llvm::MDNode* regInfoNode = callInst->getMetadata("RegInfo");
3431 if (regInfoNode != NULL) {
3432 llvm::ConstantInt* numInsValue =
3433 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(0));
3434 llvm::ConstantInt* numRegsValue =
3435 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(1));
3436 llvm::ConstantInt* numOutsValue =
3437 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(2));
3438 llvm::ConstantInt* numCompilerTempsValue =
3439 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(3));
3440 llvm::ConstantInt* numSSARegsValue =
3441 static_cast<llvm::ConstantInt*>(regInfoNode->getOperand(4));
3442 if (cUnit->printMe) {
3443 LOG(INFO) << "RegInfo - Ins:" << numInsValue->getZExtValue()
3444 << ", Regs:" << numRegsValue->getZExtValue()
3445 << ", Outs:" << numOutsValue->getZExtValue()
3446 << ", CTemps:" << numCompilerTempsValue->getZExtValue()
3447 << ", SSARegs:" << numSSARegsValue->getZExtValue();
3448 }
3449 }
3450 llvm::MDNode* pmapInfoNode = callInst->getMetadata("PromotionMap");
3451 if (pmapInfoNode != NULL) {
3452 int elems = pmapInfoNode->getNumOperands();
3453 if (cUnit->printMe) {
3454 LOG(INFO) << "PMap size: " << elems;
3455 }
3456 for (int i = 0; i < elems; i++) {
3457 llvm::ConstantInt* rawMapData =
3458 static_cast<llvm::ConstantInt*>(pmapInfoNode->getOperand(i));
3459 uint32_t mapData = rawMapData->getZExtValue();
3460 PromotionMap* p = &cUnit->promotionMap[i];
3461 p->firstInPair = (mapData >> 24) & 0xff;
buzbee52a77fc2012-11-20 19:50:46 -08003462 p->FpReg = (mapData >> 16) & 0xff;
buzbeeca7a5e42012-08-20 11:12:18 -07003463 p->coreReg = (mapData >> 8) & 0xff;
3464 p->fpLocation = static_cast<RegLocationType>((mapData >> 4) & 0xf);
3465 if (p->fpLocation == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -08003466 RecordFpPromotion(cUnit, p->FpReg, i);
buzbeeca7a5e42012-08-20 11:12:18 -07003467 }
3468 p->coreLocation = static_cast<RegLocationType>(mapData & 0xf);
3469 if (p->coreLocation == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -08003470 RecordCorePromotion(cUnit, p->coreReg, i);
buzbeeca7a5e42012-08-20 11:12:18 -07003471 }
3472 }
3473 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -08003474 DumpPromotionMap(cUnit);
buzbeeca7a5e42012-08-20 11:12:18 -07003475 }
3476 }
3477 break;
3478 }
3479 }
3480 }
buzbee52a77fc2012-11-20 19:50:46 -08003481 AdjustSpillMask(cUnit);
3482 cUnit->frameSize = ComputeFrameSize(cUnit);
buzbeead8f15e2012-06-18 14:49:45 -07003483
3484 // Create RegLocations for arguments
3485 llvm::Function::arg_iterator it(cUnit->func->arg_begin());
3486 llvm::Function::arg_iterator it_end(cUnit->func->arg_end());
3487 for (; it != it_end; ++it) {
3488 llvm::Value* val = it;
buzbee52a77fc2012-11-20 19:50:46 -08003489 CreateLocFromValue(cUnit, val);
buzbeead8f15e2012-06-18 14:49:45 -07003490 }
3491 // Create RegLocations for all non-argument defintions
3492 for (llvm::inst_iterator i = llvm::inst_begin(func),
3493 e = llvm::inst_end(func); i != e; ++i) {
3494 llvm::Value* val = &*i;
3495 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
buzbee52a77fc2012-11-20 19:50:46 -08003496 CreateLocFromValue(cUnit, val);
buzbeead8f15e2012-06-18 14:49:45 -07003497 }
3498 }
3499
buzbee2cfc6392012-05-07 14:51:40 -07003500 // Walk the blocks, generating code.
3501 for (llvm::Function::iterator i = cUnit->func->begin(),
3502 e = cUnit->func->end(); i != e; ++i) {
buzbeeaad94382012-11-21 07:40:50 -08003503 BitcodeBlockCodeGen(cUnit, static_cast<llvm::BasicBlock*>(i));
buzbee2cfc6392012-05-07 14:51:40 -07003504 }
3505
buzbee52a77fc2012-11-20 19:50:46 -08003506 HandleSuspendLaunchPads(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07003507
buzbee52a77fc2012-11-20 19:50:46 -08003508 HandleThrowLaunchPads(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07003509
buzbee52a77fc2012-11-20 19:50:46 -08003510 HandleIntrinsicLaunchPads(cUnit);
buzbee2cfc6392012-05-07 14:51:40 -07003511
buzbee692be802012-08-29 15:52:59 -07003512 cUnit->func->eraseFromParent();
3513 cUnit->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003514}
3515
3516
3517} // namespace art