blob: 6758bb9f69af48da9108b76ce505f4be8448e7a5 [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 "local_optimizations.h"
buzbee1bc37c62012-11-20 13:35:41 -080032#include "codegen_util.h"
33#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080034
buzbee8320f382012-09-11 16:29:42 -070035static const char* kLabelFormat = "%c0x%x_%d";
buzbee951c0a12012-10-03 16:31:39 -070036static const char kInvalidBlock = 0xff;
buzbee8320f382012-09-11 16:29:42 -070037static const char kNormalBlock = 'L';
38static const char kCatchBlock = 'C';
buzbee2cfc6392012-05-07 14:51:40 -070039
40namespace art {
buzbeefa57c472012-11-21 12:06:18 -080041static RegLocation GetLoc(CompilationUnit* cu, llvm::Value* val);
buzbee2cfc6392012-05-07 14:51:40 -070042
buzbeefa57c472012-11-21 12:06:18 -080043static llvm::BasicBlock* GetLLVMBlock(CompilationUnit* cu, int id)
buzbee2cfc6392012-05-07 14:51:40 -070044{
buzbeefa57c472012-11-21 12:06:18 -080045 return cu->id_to_block_map.Get(id);
buzbee2cfc6392012-05-07 14:51:40 -070046}
47
buzbeefa57c472012-11-21 12:06:18 -080048static llvm::Value* GetLLVMValue(CompilationUnit* cu, int s_reg)
buzbee2cfc6392012-05-07 14:51:40 -070049{
buzbeefa57c472012-11-21 12:06:18 -080050 return reinterpret_cast<llvm::Value*>(GrowableListGetElement(&cu->llvm_values, s_reg));
buzbee2cfc6392012-05-07 14:51:40 -070051}
52
53// Replace the placeholder value with the real definition
buzbeefa57c472012-11-21 12:06:18 -080054static void DefineValue(CompilationUnit* cu, llvm::Value* val, int s_reg)
buzbee2cfc6392012-05-07 14:51:40 -070055{
buzbeefa57c472012-11-21 12:06:18 -080056 llvm::Value* placeholder = GetLLVMValue(cu, s_reg);
buzbee9a2487f2012-07-26 14:01:13 -070057 if (placeholder == NULL) {
58 // This can happen on instruction rewrite on verification failure
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070059 LOG(WARNING) << "Null placeholder";
buzbee9a2487f2012-07-26 14:01:13 -070060 return;
61 }
buzbee2cfc6392012-05-07 14:51:40 -070062 placeholder->replaceAllUsesWith(val);
63 val->takeName(placeholder);
buzbeefa57c472012-11-21 12:06:18 -080064 cu->llvm_values.elem_list[s_reg] = reinterpret_cast<uintptr_t>(val);
buzbee4be777b2012-07-12 14:38:18 -070065 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(placeholder);
66 DCHECK(inst != NULL);
67 inst->eraseFromParent();
TDYa1278e950c12012-11-02 09:58:19 -070068
69 // Set vreg for debugging
buzbeefa57c472012-11-21 12:06:18 -080070 if (!cu->compiler->IsDebuggingSupported()) {
TDYa1278e950c12012-11-02 09:58:19 -070071 greenland::IntrinsicHelper::IntrinsicId id =
72 greenland::IntrinsicHelper::SetVReg;
buzbeefa57c472012-11-21 12:06:18 -080073 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(id);
74 int v_reg = SRegToVReg(cu, s_reg);
75 llvm::Value* table_slot = cu->irb->getInt32(v_reg);
76 llvm::Value* args[] = { table_slot, val };
77 cu->irb->CreateCall(func, args);
TDYa1278e950c12012-11-02 09:58:19 -070078 }
buzbee2cfc6392012-05-07 14:51:40 -070079}
80
buzbeefa57c472012-11-21 12:06:18 -080081static llvm::Type* LlvmTypeFromLocRec(CompilationUnit* cu, RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -070082{
83 llvm::Type* res = NULL;
84 if (loc.wide) {
85 if (loc.fp)
buzbeefa57c472012-11-21 12:06:18 -080086 res = cu->irb->getDoubleTy();
buzbee2cfc6392012-05-07 14:51:40 -070087 else
buzbeefa57c472012-11-21 12:06:18 -080088 res = cu->irb->getInt64Ty();
buzbee2cfc6392012-05-07 14:51:40 -070089 } else {
90 if (loc.fp) {
buzbeefa57c472012-11-21 12:06:18 -080091 res = cu->irb->getFloatTy();
buzbee2cfc6392012-05-07 14:51:40 -070092 } else {
93 if (loc.ref)
buzbeefa57c472012-11-21 12:06:18 -080094 res = cu->irb->GetJObjectTy();
buzbee2cfc6392012-05-07 14:51:40 -070095 else
buzbeefa57c472012-11-21 12:06:18 -080096 res = cu->irb->getInt32Ty();
buzbee2cfc6392012-05-07 14:51:40 -070097 }
98 }
99 return res;
100}
101
buzbeead8f15e2012-06-18 14:49:45 -0700102/* Create an in-memory RegLocation from an llvm Value. */
buzbeefa57c472012-11-21 12:06:18 -0800103static void CreateLocFromValue(CompilationUnit* cu, llvm::Value* val)
buzbeead8f15e2012-06-18 14:49:45 -0700104{
105 // NOTE: llvm takes shortcuts with c_str() - get to std::string firstt
106 std::string s(val->getName().str());
buzbeefa57c472012-11-21 12:06:18 -0800107 const char* val_name = s.c_str();
108 SafeMap<llvm::Value*, RegLocation>::iterator it = cu->loc_map.find(val);
109 DCHECK(it == cu->loc_map.end()) << " - already defined: " << val_name;
110 int base_sreg = INVALID_SREG;
buzbeead8f15e2012-06-18 14:49:45 -0700111 int subscript = -1;
buzbeefa57c472012-11-21 12:06:18 -0800112 sscanf(val_name, "v%d_%d", &base_sreg, &subscript);
113 if ((base_sreg == INVALID_SREG) && (!strcmp(val_name, "method"))) {
114 base_sreg = SSA_METHOD_BASEREG;
buzbeead8f15e2012-06-18 14:49:45 -0700115 subscript = 0;
116 }
buzbeefa57c472012-11-21 12:06:18 -0800117 DCHECK_NE(base_sreg, INVALID_SREG);
buzbeead8f15e2012-06-18 14:49:45 -0700118 DCHECK_NE(subscript, -1);
119 // TODO: redo during C++'ification
120 RegLocation loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, INVALID_REG,
121 INVALID_REG, INVALID_SREG, INVALID_SREG};
122 llvm::Type* ty = val->getType();
buzbeefa57c472012-11-21 12:06:18 -0800123 loc.wide = ((ty == cu->irb->getInt64Ty()) ||
124 (ty == cu->irb->getDoubleTy()));
buzbeead8f15e2012-06-18 14:49:45 -0700125 loc.defined = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700126 loc.home = false; // May change during promotion
buzbeefa57c472012-11-21 12:06:18 -0800127 loc.s_reg_low = base_sreg;
128 loc.orig_sreg = cu->loc_map.size();
129 PromotionMap p_map = cu->promotion_map[base_sreg];
130 if (ty == cu->irb->getFloatTy()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700131 loc.fp = true;
buzbeefa57c472012-11-21 12:06:18 -0800132 if (p_map.fp_location == kLocPhysReg) {
133 loc.low_reg = p_map.FpReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700134 loc.location = kLocPhysReg;
135 loc.home = true;
136 }
buzbeefa57c472012-11-21 12:06:18 -0800137 } else if (ty == cu->irb->getDoubleTy()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700138 loc.fp = true;
buzbeefa57c472012-11-21 12:06:18 -0800139 PromotionMap p_map_high = cu->promotion_map[base_sreg + 1];
140 if ((p_map.fp_location == kLocPhysReg) &&
141 (p_map_high.fp_location == kLocPhysReg) &&
142 ((p_map.FpReg & 0x1) == 0) &&
143 (p_map.FpReg + 1 == p_map_high.FpReg)) {
144 loc.low_reg = p_map.FpReg;
145 loc.high_reg = p_map_high.FpReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700146 loc.location = kLocPhysReg;
147 loc.home = true;
148 }
buzbeefa57c472012-11-21 12:06:18 -0800149 } else if (ty == cu->irb->GetJObjectTy()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700150 loc.ref = true;
buzbeefa57c472012-11-21 12:06:18 -0800151 if (p_map.core_location == kLocPhysReg) {
152 loc.low_reg = p_map.core_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700153 loc.location = kLocPhysReg;
154 loc.home = true;
155 }
buzbeefa57c472012-11-21 12:06:18 -0800156 } else if (ty == cu->irb->getInt64Ty()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700157 loc.core = true;
buzbeefa57c472012-11-21 12:06:18 -0800158 PromotionMap p_map_high = cu->promotion_map[base_sreg + 1];
159 if ((p_map.core_location == kLocPhysReg) &&
160 (p_map_high.core_location == kLocPhysReg)) {
161 loc.low_reg = p_map.core_reg;
162 loc.high_reg = p_map_high.core_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700163 loc.location = kLocPhysReg;
164 loc.home = true;
165 }
166 } else {
167 loc.core = true;
buzbeefa57c472012-11-21 12:06:18 -0800168 if (p_map.core_location == kLocPhysReg) {
169 loc.low_reg = p_map.core_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700170 loc.location = kLocPhysReg;
171 loc.home = true;
172 }
173 }
174
buzbeefa57c472012-11-21 12:06:18 -0800175 if (cu->verbose && loc.home) {
buzbeeca7a5e42012-08-20 11:12:18 -0700176 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -0800177 LOG(INFO) << "Promoted wide " << s << " to regs " << loc.low_reg << "/" << loc.high_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700178 } else {
buzbeefa57c472012-11-21 12:06:18 -0800179 LOG(INFO) << "Promoted " << s << " to reg " << loc.low_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700180 }
181 }
buzbeefa57c472012-11-21 12:06:18 -0800182 cu->loc_map.Put(val, loc);
buzbeead8f15e2012-06-18 14:49:45 -0700183}
buzbeeaad94382012-11-21 07:40:50 -0800184
buzbeefa57c472012-11-21 12:06:18 -0800185static void InitIR(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -0700186{
buzbeefa57c472012-11-21 12:06:18 -0800187 LLVMInfo* llvm_info = cu->llvm_info;
188 if (llvm_info == NULL) {
189 CompilerTls* tls = cu->compiler->GetTls();
buzbee4df2bbd2012-10-11 14:46:06 -0700190 CHECK(tls != NULL);
buzbeefa57c472012-11-21 12:06:18 -0800191 llvm_info = static_cast<LLVMInfo*>(tls->GetLLVMInfo());
192 if (llvm_info == NULL) {
193 llvm_info = new LLVMInfo();
194 tls->SetLLVMInfo(llvm_info);
buzbee4df2bbd2012-10-11 14:46:06 -0700195 }
196 }
buzbeefa57c472012-11-21 12:06:18 -0800197 cu->context = llvm_info->GetLLVMContext();
198 cu->module = llvm_info->GetLLVMModule();
199 cu->intrinsic_helper = llvm_info->GetIntrinsicHelper();
200 cu->irb = llvm_info->GetIRBuilder();
buzbee2cfc6392012-05-07 14:51:40 -0700201}
202
buzbeefa57c472012-11-21 12:06:18 -0800203static const char* LlvmSSAName(CompilationUnit* cu, int ssa_reg) {
204 return GET_ELEM_N(cu->ssa_strings, char*, ssa_reg);
buzbee2cfc6392012-05-07 14:51:40 -0700205}
206
buzbeefa57c472012-11-21 12:06:18 -0800207llvm::BasicBlock* FindCaseTarget(CompilationUnit* cu, uint32_t vaddr)
buzbeef58c12c2012-07-03 15:06:29 -0700208{
buzbeefa57c472012-11-21 12:06:18 -0800209 BasicBlock* bb = FindBlock(cu, vaddr);
buzbeef58c12c2012-07-03 15:06:29 -0700210 DCHECK(bb != NULL);
buzbeefa57c472012-11-21 12:06:18 -0800211 return GetLLVMBlock(cu, bb->id);
buzbeef58c12c2012-07-03 15:06:29 -0700212}
213
buzbeefa57c472012-11-21 12:06:18 -0800214static void ConvertPackedSwitch(CompilationUnit* cu, BasicBlock* bb,
215 int32_t table_offset, RegLocation rl_src)
buzbeef58c12c2012-07-03 15:06:29 -0700216{
217 const Instruction::PackedSwitchPayload* payload =
218 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
buzbeefa57c472012-11-21 12:06:18 -0800219 cu->insns + cu->current_dalvik_offset + table_offset);
buzbeef58c12c2012-07-03 15:06:29 -0700220
buzbeefa57c472012-11-21 12:06:18 -0800221 llvm::Value* value = GetLLVMValue(cu, rl_src.orig_sreg);
buzbeef58c12c2012-07-03 15:06:29 -0700222
223 llvm::SwitchInst* sw =
buzbeefa57c472012-11-21 12:06:18 -0800224 cu->irb->CreateSwitch(value, GetLLVMBlock(cu, bb->fall_through->id),
buzbeef58c12c2012-07-03 15:06:29 -0700225 payload->case_count);
226
227 for (uint16_t i = 0; i < payload->case_count; ++i) {
buzbeefa57c472012-11-21 12:06:18 -0800228 llvm::BasicBlock* llvm_bb =
229 FindCaseTarget(cu, cu->current_dalvik_offset + payload->targets[i]);
230 sw->addCase(cu->irb->getInt32(payload->first_key + i), llvm_bb);
buzbeef58c12c2012-07-03 15:06:29 -0700231 }
buzbeefa57c472012-11-21 12:06:18 -0800232 llvm::MDNode* switch_node =
233 llvm::MDNode::get(*cu->context, cu->irb->getInt32(table_offset));
234 sw->setMetadata("SwitchTable", switch_node);
buzbeef58c12c2012-07-03 15:06:29 -0700235 bb->taken = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800236 bb->fall_through = NULL;
buzbeef58c12c2012-07-03 15:06:29 -0700237}
238
buzbeefa57c472012-11-21 12:06:18 -0800239static void ConvertSparseSwitch(CompilationUnit* cu, BasicBlock* bb,
240 int32_t table_offset, RegLocation rl_src)
buzbeea1da8a52012-07-09 14:00:21 -0700241{
242 const Instruction::SparseSwitchPayload* payload =
243 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
buzbeefa57c472012-11-21 12:06:18 -0800244 cu->insns + cu->current_dalvik_offset + table_offset);
buzbeea1da8a52012-07-09 14:00:21 -0700245
246 const int32_t* keys = payload->GetKeys();
247 const int32_t* targets = payload->GetTargets();
248
buzbeefa57c472012-11-21 12:06:18 -0800249 llvm::Value* value = GetLLVMValue(cu, rl_src.orig_sreg);
buzbeea1da8a52012-07-09 14:00:21 -0700250
251 llvm::SwitchInst* sw =
buzbeefa57c472012-11-21 12:06:18 -0800252 cu->irb->CreateSwitch(value, GetLLVMBlock(cu, bb->fall_through->id),
buzbeea1da8a52012-07-09 14:00:21 -0700253 payload->case_count);
254
255 for (size_t i = 0; i < payload->case_count; ++i) {
buzbeefa57c472012-11-21 12:06:18 -0800256 llvm::BasicBlock* llvm_bb =
257 FindCaseTarget(cu, cu->current_dalvik_offset + targets[i]);
258 sw->addCase(cu->irb->getInt32(keys[i]), llvm_bb);
buzbeea1da8a52012-07-09 14:00:21 -0700259 }
buzbeefa57c472012-11-21 12:06:18 -0800260 llvm::MDNode* switch_node =
261 llvm::MDNode::get(*cu->context, cu->irb->getInt32(table_offset));
262 sw->setMetadata("SwitchTable", switch_node);
buzbeea1da8a52012-07-09 14:00:21 -0700263 bb->taken = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800264 bb->fall_through = NULL;
buzbeea1da8a52012-07-09 14:00:21 -0700265}
266
buzbeefa57c472012-11-21 12:06:18 -0800267static void ConvertSget(CompilationUnit* cu, int32_t field_index,
268 greenland::IntrinsicHelper::IntrinsicId id, RegLocation rl_dest)
buzbee4f1181f2012-06-22 13:52:12 -0700269{
buzbeefa57c472012-11-21 12:06:18 -0800270 llvm::Constant* field_idx = cu->irb->getInt32(field_index);
271 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
272 llvm::Value* res = cu->irb->CreateCall(intr, field_idx);
273 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700274}
275
buzbeefa57c472012-11-21 12:06:18 -0800276static void ConvertSput(CompilationUnit* cu, int32_t field_index,
277 greenland::IntrinsicHelper::IntrinsicId id, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700278{
279 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800280 args.push_back(cu->irb->getInt32(field_index));
281 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
282 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
283 cu->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700284}
285
buzbeefa57c472012-11-21 12:06:18 -0800286static void ConvertFillArrayData(CompilationUnit* cu, int32_t offset, RegLocation rl_array)
buzbee101305f2012-06-28 18:00:56 -0700287{
288 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700289 id = greenland::IntrinsicHelper::HLFillArrayData;
buzbee101305f2012-06-28 18:00:56 -0700290 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800291 args.push_back(cu->irb->getInt32(offset));
292 args.push_back(GetLLVMValue(cu, rl_array.orig_sreg));
293 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
294 cu->irb->CreateCall(intr, args);
buzbee101305f2012-06-28 18:00:56 -0700295}
296
buzbeefa57c472012-11-21 12:06:18 -0800297static llvm::Value* EmitConst(CompilationUnit* cu, llvm::ArrayRef<llvm::Value*> src,
buzbeeaad94382012-11-21 07:40:50 -0800298 RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -0700299{
300 greenland::IntrinsicHelper::IntrinsicId id;
301 if (loc.wide) {
302 if (loc.fp) {
303 id = greenland::IntrinsicHelper::ConstDouble;
304 } else {
305 id = greenland::IntrinsicHelper::ConstLong;
306 }
307 } else {
308 if (loc.fp) {
309 id = greenland::IntrinsicHelper::ConstFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700310 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700311 id = greenland::IntrinsicHelper::ConstObj;
312 } else {
313 id = greenland::IntrinsicHelper::ConstInt;
314 }
315 }
buzbeefa57c472012-11-21 12:06:18 -0800316 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
317 return cu->irb->CreateCall(intr, src);
buzbee2cfc6392012-05-07 14:51:40 -0700318}
buzbeeb03f4872012-06-11 15:22:11 -0700319
buzbeefa57c472012-11-21 12:06:18 -0800320static void EmitPopShadowFrame(CompilationUnit* cu)
buzbeeb03f4872012-06-11 15:22:11 -0700321{
buzbeefa57c472012-11-21 12:06:18 -0800322 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(
buzbeeb03f4872012-06-11 15:22:11 -0700323 greenland::IntrinsicHelper::PopShadowFrame);
buzbeefa57c472012-11-21 12:06:18 -0800324 cu->irb->CreateCall(intr);
buzbeeb03f4872012-06-11 15:22:11 -0700325}
326
buzbeefa57c472012-11-21 12:06:18 -0800327static llvm::Value* EmitCopy(CompilationUnit* cu, llvm::ArrayRef<llvm::Value*> src,
buzbeeaad94382012-11-21 07:40:50 -0800328 RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -0700329{
330 greenland::IntrinsicHelper::IntrinsicId id;
331 if (loc.wide) {
332 if (loc.fp) {
333 id = greenland::IntrinsicHelper::CopyDouble;
334 } else {
335 id = greenland::IntrinsicHelper::CopyLong;
336 }
337 } else {
338 if (loc.fp) {
339 id = greenland::IntrinsicHelper::CopyFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700340 } else if (loc.ref) {
buzbee2cfc6392012-05-07 14:51:40 -0700341 id = greenland::IntrinsicHelper::CopyObj;
342 } else {
343 id = greenland::IntrinsicHelper::CopyInt;
344 }
345 }
buzbeefa57c472012-11-21 12:06:18 -0800346 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
347 return cu->irb->CreateCall(intr, src);
buzbee2cfc6392012-05-07 14:51:40 -0700348}
349
buzbeefa57c472012-11-21 12:06:18 -0800350static void ConvertMoveException(CompilationUnit* cu, RegLocation rl_dest)
buzbee32412962012-06-26 16:27:56 -0700351{
buzbeefa57c472012-11-21 12:06:18 -0800352 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(
buzbee32412962012-06-26 16:27:56 -0700353 greenland::IntrinsicHelper::GetException);
buzbeefa57c472012-11-21 12:06:18 -0800354 llvm::Value* res = cu->irb->CreateCall(func);
355 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee32412962012-06-26 16:27:56 -0700356}
357
buzbeefa57c472012-11-21 12:06:18 -0800358static void ConvertThrow(CompilationUnit* cu, RegLocation rl_src)
buzbee32412962012-06-26 16:27:56 -0700359{
buzbeefa57c472012-11-21 12:06:18 -0800360 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
361 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(
TDYa127f71bf5a2012-07-29 20:09:52 -0700362 greenland::IntrinsicHelper::HLThrowException);
buzbeefa57c472012-11-21 12:06:18 -0800363 cu->irb->CreateCall(func, src);
buzbee32412962012-06-26 16:27:56 -0700364}
365
buzbeefa57c472012-11-21 12:06:18 -0800366static void ConvertMonitorEnterExit(CompilationUnit* cu, int opt_flags,
buzbeeaad94382012-11-21 07:40:50 -0800367 greenland::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800368 RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700369{
370 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800371 args.push_back(cu->irb->getInt32(opt_flags));
372 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
373 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(id);
374 cu->irb->CreateCall(func, args);
buzbee8fa0fda2012-06-27 15:44:52 -0700375}
376
buzbeefa57c472012-11-21 12:06:18 -0800377static void ConvertArrayLength(CompilationUnit* cu, int opt_flags,
378 RegLocation rl_dest, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700379{
380 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800381 args.push_back(cu->irb->getInt32(opt_flags));
382 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
383 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700384 greenland::IntrinsicHelper::OptArrayLength);
buzbeefa57c472012-11-21 12:06:18 -0800385 llvm::Value* res = cu->irb->CreateCall(func, args);
386 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700387}
388
buzbeefa57c472012-11-21 12:06:18 -0800389static void EmitSuspendCheck(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -0700390{
391 greenland::IntrinsicHelper::IntrinsicId id =
392 greenland::IntrinsicHelper::CheckSuspend;
buzbeefa57c472012-11-21 12:06:18 -0800393 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
394 cu->irb->CreateCall(intr);
buzbee2cfc6392012-05-07 14:51:40 -0700395}
396
buzbeefa57c472012-11-21 12:06:18 -0800397static llvm::Value* ConvertCompare(CompilationUnit* cu, ConditionCode cc,
buzbeeaad94382012-11-21 07:40:50 -0800398 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700399{
400 llvm::Value* res = NULL;
buzbee76592632012-06-29 15:18:35 -0700401 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700402 switch(cc) {
buzbeefa57c472012-11-21 12:06:18 -0800403 case kCondEq: res = cu->irb->CreateICmpEQ(src1, src2); break;
404 case kCondNe: res = cu->irb->CreateICmpNE(src1, src2); break;
405 case kCondLt: res = cu->irb->CreateICmpSLT(src1, src2); break;
406 case kCondGe: res = cu->irb->CreateICmpSGE(src1, src2); break;
407 case kCondGt: res = cu->irb->CreateICmpSGT(src1, src2); break;
408 case kCondLe: res = cu->irb->CreateICmpSLE(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700409 default: LOG(FATAL) << "Unexpected cc value " << cc;
410 }
411 return res;
412}
413
buzbeefa57c472012-11-21 12:06:18 -0800414static void ConvertCompareAndBranch(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
415 ConditionCode cc, RegLocation rl_src1, RegLocation rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -0700416{
buzbeefa57c472012-11-21 12:06:18 -0800417 if (bb->taken->start_offset <= mir->offset) {
418 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -0700419 }
buzbeefa57c472012-11-21 12:06:18 -0800420 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
421 llvm::Value* src2 = GetLLVMValue(cu, rl_src2.orig_sreg);
422 llvm::Value* cond_value = ConvertCompare(cu, cc, src1, src2);
423 cond_value->setName(StringPrintf("t%d", cu->temp_name++));
424 cu->irb->CreateCondBr(cond_value, GetLLVMBlock(cu, bb->taken->id),
425 GetLLVMBlock(cu, bb->fall_through->id));
buzbee6969d502012-06-15 16:40:31 -0700426 // Don't redo the fallthrough branch in the BB driver
buzbeefa57c472012-11-21 12:06:18 -0800427 bb->fall_through = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700428}
429
buzbeefa57c472012-11-21 12:06:18 -0800430static void ConvertCompareZeroAndBranch(CompilationUnit* cu, BasicBlock* bb,
431 MIR* mir, ConditionCode cc, RegLocation rl_src1)
buzbee2cfc6392012-05-07 14:51:40 -0700432{
buzbeefa57c472012-11-21 12:06:18 -0800433 if (bb->taken->start_offset <= mir->offset) {
434 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -0700435 }
buzbeefa57c472012-11-21 12:06:18 -0800436 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700437 llvm::Value* src2;
buzbeefa57c472012-11-21 12:06:18 -0800438 if (rl_src1.ref) {
439 src2 = cu->irb->GetJNull();
buzbee2cfc6392012-05-07 14:51:40 -0700440 } else {
buzbeefa57c472012-11-21 12:06:18 -0800441 src2 = cu->irb->getInt32(0);
buzbee2cfc6392012-05-07 14:51:40 -0700442 }
buzbeefa57c472012-11-21 12:06:18 -0800443 llvm::Value* cond_value = ConvertCompare(cu, cc, src1, src2);
444 cu->irb->CreateCondBr(cond_value, GetLLVMBlock(cu, bb->taken->id),
445 GetLLVMBlock(cu, bb->fall_through->id));
buzbee6969d502012-06-15 16:40:31 -0700446 // Don't redo the fallthrough branch in the BB driver
buzbeefa57c472012-11-21 12:06:18 -0800447 bb->fall_through = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700448}
449
buzbeefa57c472012-11-21 12:06:18 -0800450static llvm::Value* GenDivModOp(CompilationUnit* cu, bool is_div, bool is_long,
buzbeeaad94382012-11-21 07:40:50 -0800451 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700452{
453 greenland::IntrinsicHelper::IntrinsicId id;
buzbeefa57c472012-11-21 12:06:18 -0800454 if (is_long) {
455 if (is_div) {
buzbee2cfc6392012-05-07 14:51:40 -0700456 id = greenland::IntrinsicHelper::DivLong;
457 } else {
458 id = greenland::IntrinsicHelper::RemLong;
459 }
Logan Chien554e6072012-07-23 20:00:01 -0700460 } else {
buzbeefa57c472012-11-21 12:06:18 -0800461 if (is_div) {
buzbee2cfc6392012-05-07 14:51:40 -0700462 id = greenland::IntrinsicHelper::DivInt;
463 } else {
464 id = greenland::IntrinsicHelper::RemInt;
Logan Chien554e6072012-07-23 20:00:01 -0700465 }
buzbee2cfc6392012-05-07 14:51:40 -0700466 }
buzbeefa57c472012-11-21 12:06:18 -0800467 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee2cfc6392012-05-07 14:51:40 -0700468 llvm::SmallVector<llvm::Value*, 2>args;
469 args.push_back(src1);
470 args.push_back(src2);
buzbeefa57c472012-11-21 12:06:18 -0800471 return cu->irb->CreateCall(intr, args);
buzbee2cfc6392012-05-07 14:51:40 -0700472}
473
buzbeefa57c472012-11-21 12:06:18 -0800474static llvm::Value* GenArithOp(CompilationUnit* cu, OpKind op, bool is_long,
buzbeeaad94382012-11-21 07:40:50 -0800475 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700476{
477 llvm::Value* res = NULL;
478 switch(op) {
buzbeefa57c472012-11-21 12:06:18 -0800479 case kOpAdd: res = cu->irb->CreateAdd(src1, src2); break;
480 case kOpSub: res = cu->irb->CreateSub(src1, src2); break;
481 case kOpRsub: res = cu->irb->CreateSub(src2, src1); break;
482 case kOpMul: res = cu->irb->CreateMul(src1, src2); break;
483 case kOpOr: res = cu->irb->CreateOr(src1, src2); break;
484 case kOpAnd: res = cu->irb->CreateAnd(src1, src2); break;
485 case kOpXor: res = cu->irb->CreateXor(src1, src2); break;
486 case kOpDiv: res = GenDivModOp(cu, true, is_long, src1, src2); break;
487 case kOpRem: res = GenDivModOp(cu, false, is_long, src1, src2); break;
488 case kOpLsl: res = cu->irb->CreateShl(src1, src2); break;
489 case kOpLsr: res = cu->irb->CreateLShr(src1, src2); break;
490 case kOpAsr: res = cu->irb->CreateAShr(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700491 default:
492 LOG(FATAL) << "Invalid op " << op;
493 }
494 return res;
495}
496
buzbeefa57c472012-11-21 12:06:18 -0800497static void ConvertFPArithOp(CompilationUnit* cu, OpKind op, RegLocation rl_dest,
498 RegLocation rl_src1, RegLocation rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -0700499{
buzbeefa57c472012-11-21 12:06:18 -0800500 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
501 llvm::Value* src2 = GetLLVMValue(cu, rl_src2.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700502 llvm::Value* res = NULL;
503 switch(op) {
buzbeefa57c472012-11-21 12:06:18 -0800504 case kOpAdd: res = cu->irb->CreateFAdd(src1, src2); break;
505 case kOpSub: res = cu->irb->CreateFSub(src1, src2); break;
506 case kOpMul: res = cu->irb->CreateFMul(src1, src2); break;
507 case kOpDiv: res = cu->irb->CreateFDiv(src1, src2); break;
508 case kOpRem: res = cu->irb->CreateFRem(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700509 default:
510 LOG(FATAL) << "Invalid op " << op;
511 }
buzbeefa57c472012-11-21 12:06:18 -0800512 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700513}
514
buzbeefa57c472012-11-21 12:06:18 -0800515static void ConvertShift(CompilationUnit* cu, greenland::IntrinsicHelper::IntrinsicId id,
516 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee4f1181f2012-06-22 13:52:12 -0700517{
buzbeefa57c472012-11-21 12:06:18 -0800518 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee2a83e8f2012-07-13 16:42:30 -0700519 llvm::SmallVector<llvm::Value*, 2>args;
buzbeefa57c472012-11-21 12:06:18 -0800520 args.push_back(GetLLVMValue(cu, rl_src1.orig_sreg));
521 args.push_back(GetLLVMValue(cu, rl_src2.orig_sreg));
522 llvm::Value* res = cu->irb->CreateCall(intr, args);
523 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2a83e8f2012-07-13 16:42:30 -0700524}
525
buzbeefa57c472012-11-21 12:06:18 -0800526static void ConvertShiftLit(CompilationUnit* cu, greenland::IntrinsicHelper::IntrinsicId id,
527 RegLocation rl_dest, RegLocation rl_src, int shift_amount)
buzbee2a83e8f2012-07-13 16:42:30 -0700528{
buzbeefa57c472012-11-21 12:06:18 -0800529 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee2a83e8f2012-07-13 16:42:30 -0700530 llvm::SmallVector<llvm::Value*, 2>args;
buzbeefa57c472012-11-21 12:06:18 -0800531 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
532 args.push_back(cu->irb->getInt32(shift_amount));
533 llvm::Value* res = cu->irb->CreateCall(intr, args);
534 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700535}
536
buzbeefa57c472012-11-21 12:06:18 -0800537static void ConvertArithOp(CompilationUnit* cu, OpKind op, RegLocation rl_dest,
538 RegLocation rl_src1, RegLocation rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -0700539{
buzbeefa57c472012-11-21 12:06:18 -0800540 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
541 llvm::Value* src2 = GetLLVMValue(cu, rl_src2.orig_sreg);
buzbee4f4dfc72012-07-02 14:54:44 -0700542 DCHECK_EQ(src1->getType(), src2->getType());
buzbeefa57c472012-11-21 12:06:18 -0800543 llvm::Value* res = GenArithOp(cu, op, rl_dest.wide, src1, src2);
544 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700545}
546
buzbeefa57c472012-11-21 12:06:18 -0800547static void ConvertArithOpLit(CompilationUnit* cu, OpKind op, RegLocation rl_dest,
548 RegLocation rl_src1, int32_t imm)
buzbee2cfc6392012-05-07 14:51:40 -0700549{
buzbeefa57c472012-11-21 12:06:18 -0800550 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
551 llvm::Value* src2 = cu->irb->getInt32(imm);
552 llvm::Value* res = GenArithOp(cu, op, rl_dest.wide, src1, src2);
553 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700554}
555
buzbee101305f2012-06-28 18:00:56 -0700556/*
557 * Process arguments for invoke. Note: this code is also used to
558 * collect and process arguments for NEW_FILLED_ARRAY and NEW_FILLED_ARRAY_RANGE.
559 * The requirements are similar.
560 */
buzbeefa57c472012-11-21 12:06:18 -0800561static void ConvertInvoke(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
562 InvokeType invoke_type, bool is_range, bool is_filled_new_array)
buzbee6969d502012-06-15 16:40:31 -0700563{
buzbee02031b12012-11-23 09:41:35 -0800564 Codegen* cg = cu->cg.get();
565 CallInfo* info = cg->NewMemCallInfo(cu, bb, mir, invoke_type, is_range);
buzbee6969d502012-06-15 16:40:31 -0700566 llvm::SmallVector<llvm::Value*, 10> args;
buzbeefa57c472012-11-21 12:06:18 -0800567 // Insert the invoke_type
568 args.push_back(cu->irb->getInt32(static_cast<int>(invoke_type)));
buzbee6969d502012-06-15 16:40:31 -0700569 // Insert the method_idx
buzbeefa57c472012-11-21 12:06:18 -0800570 args.push_back(cu->irb->getInt32(info->index));
buzbee6969d502012-06-15 16:40:31 -0700571 // Insert the optimization flags
buzbeefa57c472012-11-21 12:06:18 -0800572 args.push_back(cu->irb->getInt32(info->opt_flags));
buzbee6969d502012-06-15 16:40:31 -0700573 // Now, insert the actual arguments
buzbeefa57c472012-11-21 12:06:18 -0800574 for (int i = 0; i < info->num_arg_words;) {
575 llvm::Value* val = GetLLVMValue(cu, info->args[i].orig_sreg);
buzbee6969d502012-06-15 16:40:31 -0700576 args.push_back(val);
577 i += info->args[i].wide ? 2 : 1;
578 }
579 /*
580 * Choose the invoke return type based on actual usage. Note: may
581 * be different than shorty. For example, if a function return value
582 * is not used, we'll treat this as a void invoke.
583 */
584 greenland::IntrinsicHelper::IntrinsicId id;
buzbeefa57c472012-11-21 12:06:18 -0800585 if (is_filled_new_array) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700586 id = greenland::IntrinsicHelper::HLFilledNewArray;
buzbee101305f2012-06-28 18:00:56 -0700587 } else if (info->result.location == kLocInvalid) {
buzbee6969d502012-06-15 16:40:31 -0700588 id = greenland::IntrinsicHelper::HLInvokeVoid;
589 } else {
590 if (info->result.wide) {
591 if (info->result.fp) {
592 id = greenland::IntrinsicHelper::HLInvokeDouble;
593 } else {
buzbee8fa0fda2012-06-27 15:44:52 -0700594 id = greenland::IntrinsicHelper::HLInvokeLong;
buzbee6969d502012-06-15 16:40:31 -0700595 }
596 } else if (info->result.ref) {
597 id = greenland::IntrinsicHelper::HLInvokeObj;
598 } else if (info->result.fp) {
599 id = greenland::IntrinsicHelper::HLInvokeFloat;
600 } else {
601 id = greenland::IntrinsicHelper::HLInvokeInt;
602 }
603 }
buzbeefa57c472012-11-21 12:06:18 -0800604 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
605 llvm::Value* res = cu->irb->CreateCall(intr, args);
buzbee6969d502012-06-15 16:40:31 -0700606 if (info->result.location != kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800607 DefineValue(cu, res, info->result.orig_sreg);
buzbee6969d502012-06-15 16:40:31 -0700608 }
609}
610
buzbeefa57c472012-11-21 12:06:18 -0800611static void ConvertConstObject(CompilationUnit* cu, uint32_t idx,
612 greenland::IntrinsicHelper::IntrinsicId id, RegLocation rl_dest)
buzbee6969d502012-06-15 16:40:31 -0700613{
buzbeefa57c472012-11-21 12:06:18 -0800614 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
615 llvm::Value* index = cu->irb->getInt32(idx);
616 llvm::Value* res = cu->irb->CreateCall(intr, index);
617 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee6969d502012-06-15 16:40:31 -0700618}
619
buzbeefa57c472012-11-21 12:06:18 -0800620static void ConvertCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee101305f2012-06-28 18:00:56 -0700621{
622 greenland::IntrinsicHelper::IntrinsicId id;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700623 id = greenland::IntrinsicHelper::HLCheckCast;
buzbeefa57c472012-11-21 12:06:18 -0800624 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee101305f2012-06-28 18:00:56 -0700625 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800626 args.push_back(cu->irb->getInt32(type_idx));
627 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
628 cu->irb->CreateCall(intr, args);
buzbee101305f2012-06-28 18:00:56 -0700629}
630
buzbeefa57c472012-11-21 12:06:18 -0800631static void ConvertNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee4f1181f2012-06-22 13:52:12 -0700632{
633 greenland::IntrinsicHelper::IntrinsicId id;
634 id = greenland::IntrinsicHelper::NewInstance;
buzbeefa57c472012-11-21 12:06:18 -0800635 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
636 llvm::Value* index = cu->irb->getInt32(type_idx);
637 llvm::Value* res = cu->irb->CreateCall(intr, index);
638 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700639}
640
buzbeefa57c472012-11-21 12:06:18 -0800641static void ConvertNewArray(CompilationUnit* cu, uint32_t type_idx,
642 RegLocation rl_dest, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700643{
644 greenland::IntrinsicHelper::IntrinsicId id;
645 id = greenland::IntrinsicHelper::NewArray;
buzbeefa57c472012-11-21 12:06:18 -0800646 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700647 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800648 args.push_back(cu->irb->getInt32(type_idx));
649 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
650 llvm::Value* res = cu->irb->CreateCall(intr, args);
651 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700652}
653
buzbeefa57c472012-11-21 12:06:18 -0800654static void ConvertAget(CompilationUnit* cu, int opt_flags,
buzbeeaad94382012-11-21 07:40:50 -0800655 greenland::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800656 RegLocation rl_dest, RegLocation rl_array, RegLocation rl_index)
buzbee8fa0fda2012-06-27 15:44:52 -0700657{
658 llvm::SmallVector<llvm::Value*, 3> args;
buzbeefa57c472012-11-21 12:06:18 -0800659 args.push_back(cu->irb->getInt32(opt_flags));
660 args.push_back(GetLLVMValue(cu, rl_array.orig_sreg));
661 args.push_back(GetLLVMValue(cu, rl_index.orig_sreg));
662 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
663 llvm::Value* res = cu->irb->CreateCall(intr, args);
664 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700665}
666
buzbeefa57c472012-11-21 12:06:18 -0800667static void ConvertAput(CompilationUnit* cu, int opt_flags,
buzbeeaad94382012-11-21 07:40:50 -0800668 greenland::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800669 RegLocation rl_src, RegLocation rl_array, RegLocation rl_index)
buzbee8fa0fda2012-06-27 15:44:52 -0700670{
671 llvm::SmallVector<llvm::Value*, 4> args;
buzbeefa57c472012-11-21 12:06:18 -0800672 args.push_back(cu->irb->getInt32(opt_flags));
673 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
674 args.push_back(GetLLVMValue(cu, rl_array.orig_sreg));
675 args.push_back(GetLLVMValue(cu, rl_index.orig_sreg));
676 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
677 cu->irb->CreateCall(intr, args);
buzbee8fa0fda2012-06-27 15:44:52 -0700678}
679
buzbeefa57c472012-11-21 12:06:18 -0800680static void ConvertIget(CompilationUnit* cu, int opt_flags,
buzbeeaad94382012-11-21 07:40:50 -0800681 greenland::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800682 RegLocation rl_dest, RegLocation rl_obj, int field_index)
buzbee101305f2012-06-28 18:00:56 -0700683{
684 llvm::SmallVector<llvm::Value*, 3> args;
buzbeefa57c472012-11-21 12:06:18 -0800685 args.push_back(cu->irb->getInt32(opt_flags));
686 args.push_back(GetLLVMValue(cu, rl_obj.orig_sreg));
687 args.push_back(cu->irb->getInt32(field_index));
688 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
689 llvm::Value* res = cu->irb->CreateCall(intr, args);
690 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee101305f2012-06-28 18:00:56 -0700691}
692
buzbeefa57c472012-11-21 12:06:18 -0800693static void ConvertIput(CompilationUnit* cu, int opt_flags,
buzbeeaad94382012-11-21 07:40:50 -0800694 greenland::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800695 RegLocation rl_src, RegLocation rl_obj, int field_index)
buzbee101305f2012-06-28 18:00:56 -0700696{
697 llvm::SmallVector<llvm::Value*, 4> args;
buzbeefa57c472012-11-21 12:06:18 -0800698 args.push_back(cu->irb->getInt32(opt_flags));
699 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
700 args.push_back(GetLLVMValue(cu, rl_obj.orig_sreg));
701 args.push_back(cu->irb->getInt32(field_index));
702 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
703 cu->irb->CreateCall(intr, args);
buzbee101305f2012-06-28 18:00:56 -0700704}
705
buzbeefa57c472012-11-21 12:06:18 -0800706static void ConvertInstanceOf(CompilationUnit* cu, uint32_t type_idx,
707 RegLocation rl_dest, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700708{
709 greenland::IntrinsicHelper::IntrinsicId id;
710 id = greenland::IntrinsicHelper::InstanceOf;
buzbeefa57c472012-11-21 12:06:18 -0800711 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700712 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800713 args.push_back(cu->irb->getInt32(type_idx));
714 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
715 llvm::Value* res = cu->irb->CreateCall(intr, args);
716 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700717}
718
buzbeefa57c472012-11-21 12:06:18 -0800719static void ConvertIntToLong(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee101305f2012-06-28 18:00:56 -0700720{
buzbeefa57c472012-11-21 12:06:18 -0800721 llvm::Value* res = cu->irb->CreateSExt(GetLLVMValue(cu, rl_src.orig_sreg),
722 cu->irb->getInt64Ty());
723 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee101305f2012-06-28 18:00:56 -0700724}
725
buzbeefa57c472012-11-21 12:06:18 -0800726static void ConvertLongToInt(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700727{
buzbeefa57c472012-11-21 12:06:18 -0800728 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
729 llvm::Value* res = cu->irb->CreateTrunc(src, cu->irb->getInt32Ty());
730 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700731}
732
buzbeefa57c472012-11-21 12:06:18 -0800733static void ConvertFloatToDouble(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700734{
buzbeefa57c472012-11-21 12:06:18 -0800735 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
736 llvm::Value* res = cu->irb->CreateFPExt(src, cu->irb->getDoubleTy());
737 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700738}
739
buzbeefa57c472012-11-21 12:06:18 -0800740static void ConvertDoubleToFloat(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700741{
buzbeefa57c472012-11-21 12:06:18 -0800742 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
743 llvm::Value* res = cu->irb->CreateFPTrunc(src, cu->irb->getFloatTy());
744 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700745}
746
buzbeefa57c472012-11-21 12:06:18 -0800747static void ConvertWideComparison(CompilationUnit* cu,
buzbeeaad94382012-11-21 07:40:50 -0800748 greenland::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800749 RegLocation rl_dest, RegLocation rl_src1,
750 RegLocation rl_src2)
buzbee76592632012-06-29 15:18:35 -0700751{
buzbeefa57c472012-11-21 12:06:18 -0800752 DCHECK_EQ(rl_src1.fp, rl_src2.fp);
753 DCHECK_EQ(rl_src1.wide, rl_src2.wide);
754 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700755 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800756 args.push_back(GetLLVMValue(cu, rl_src1.orig_sreg));
757 args.push_back(GetLLVMValue(cu, rl_src2.orig_sreg));
758 llvm::Value* res = cu->irb->CreateCall(intr, args);
759 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700760}
761
buzbeefa57c472012-11-21 12:06:18 -0800762static void ConvertIntNarrowing(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src,
buzbeeaad94382012-11-21 07:40:50 -0800763 greenland::IntrinsicHelper::IntrinsicId id)
buzbee101305f2012-06-28 18:00:56 -0700764{
buzbeefa57c472012-11-21 12:06:18 -0800765 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700766 llvm::Value* res =
buzbeefa57c472012-11-21 12:06:18 -0800767 cu->irb->CreateCall(intr, GetLLVMValue(cu, rl_src.orig_sreg));
768 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700769}
770
buzbeefa57c472012-11-21 12:06:18 -0800771static void ConvertNeg(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700772{
buzbeefa57c472012-11-21 12:06:18 -0800773 llvm::Value* res = cu->irb->CreateNeg(GetLLVMValue(cu, rl_src.orig_sreg));
774 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700775}
776
buzbeefa57c472012-11-21 12:06:18 -0800777static void ConvertIntToFP(CompilationUnit* cu, llvm::Type* ty, RegLocation rl_dest,
778 RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700779{
780 llvm::Value* res =
buzbeefa57c472012-11-21 12:06:18 -0800781 cu->irb->CreateSIToFP(GetLLVMValue(cu, rl_src.orig_sreg), ty);
782 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700783}
784
buzbeefa57c472012-11-21 12:06:18 -0800785static void ConvertFPToInt(CompilationUnit* cu, greenland::IntrinsicHelper::IntrinsicId id,
786 RegLocation rl_dest,
787 RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700788{
buzbeefa57c472012-11-21 12:06:18 -0800789 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
790 llvm::Value* res = cu->irb->CreateCall(intr, GetLLVMValue(cu, rl_src.orig_sreg));
791 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700792}
793
794
buzbeefa57c472012-11-21 12:06:18 -0800795static void ConvertNegFP(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700796{
797 llvm::Value* res =
buzbeefa57c472012-11-21 12:06:18 -0800798 cu->irb->CreateFNeg(GetLLVMValue(cu, rl_src.orig_sreg));
799 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700800}
801
buzbeefa57c472012-11-21 12:06:18 -0800802static void ConvertNot(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700803{
buzbeefa57c472012-11-21 12:06:18 -0800804 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
805 llvm::Value* res = cu->irb->CreateXor(src, static_cast<uint64_t>(-1));
806 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee101305f2012-06-28 18:00:56 -0700807}
808
buzbee2cfc6392012-05-07 14:51:40 -0700809/*
810 * Target-independent code generation. Use only high-level
811 * load/store utilities here, or target-dependent genXX() handlers
812 * when necessary.
813 */
buzbeefa57c472012-11-21 12:06:18 -0800814static bool ConvertMIRNode(CompilationUnit* cu, MIR* mir, BasicBlock* bb,
815 llvm::BasicBlock* llvm_bb, LIR* label_list)
buzbee2cfc6392012-05-07 14:51:40 -0700816{
817 bool res = false; // Assume success
buzbeefa57c472012-11-21 12:06:18 -0800818 RegLocation rl_src[3];
buzbee02031b12012-11-23 09:41:35 -0800819 RegLocation rl_dest = GetBadLoc();
buzbee2cfc6392012-05-07 14:51:40 -0700820 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800821 int op_val = opcode;
buzbee6969d502012-06-15 16:40:31 -0700822 uint32_t vB = mir->dalvikInsn.vB;
823 uint32_t vC = mir->dalvikInsn.vC;
buzbeefa57c472012-11-21 12:06:18 -0800824 int opt_flags = mir->optimization_flags;
buzbee6969d502012-06-15 16:40:31 -0700825
buzbeefa57c472012-11-21 12:06:18 -0800826 if (cu->verbose) {
827 if (op_val < kMirOpFirst) {
828 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x" << std::hex << op_val;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700829 } else {
buzbeefa57c472012-11-21 12:06:18 -0800830 LOG(INFO) << extended_mir_op_names[op_val - kMirOpFirst] << " 0x" << std::hex << op_val;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700831 }
832 }
833
buzbee2cfc6392012-05-07 14:51:40 -0700834 /* Prep Src and Dest locations */
buzbeefa57c472012-11-21 12:06:18 -0800835 int next_sreg = 0;
836 int next_loc = 0;
837 int attrs = oat_data_flow_attributes[opcode];
buzbee02031b12012-11-23 09:41:35 -0800838 rl_src[0] = rl_src[1] = rl_src[2] = GetBadLoc();
buzbee2cfc6392012-05-07 14:51:40 -0700839 if (attrs & DF_UA) {
840 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800841 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
842 next_sreg+= 2;
buzbee2cfc6392012-05-07 14:51:40 -0700843 } else {
buzbeefa57c472012-11-21 12:06:18 -0800844 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
845 next_sreg++;
buzbee2cfc6392012-05-07 14:51:40 -0700846 }
847 }
848 if (attrs & DF_UB) {
849 if (attrs & DF_B_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800850 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
851 next_sreg+= 2;
buzbee2cfc6392012-05-07 14:51:40 -0700852 } else {
buzbeefa57c472012-11-21 12:06:18 -0800853 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
854 next_sreg++;
buzbee2cfc6392012-05-07 14:51:40 -0700855 }
856 }
857 if (attrs & DF_UC) {
858 if (attrs & DF_C_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800859 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700860 } else {
buzbeefa57c472012-11-21 12:06:18 -0800861 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700862 }
863 }
864 if (attrs & DF_DA) {
865 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800866 rl_dest = GetDestWide(cu, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700867 } else {
buzbeefa57c472012-11-21 12:06:18 -0800868 rl_dest = GetDest(cu, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700869 }
870 }
871
872 switch (opcode) {
873 case Instruction::NOP:
874 break;
875
876 case Instruction::MOVE:
877 case Instruction::MOVE_OBJECT:
878 case Instruction::MOVE_16:
879 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700880 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700881 case Instruction::MOVE_FROM16:
882 case Instruction::MOVE_WIDE:
883 case Instruction::MOVE_WIDE_16:
884 case Instruction::MOVE_WIDE_FROM16: {
885 /*
886 * Moves/copies are meaningless in pure SSA register form,
887 * but we need to preserve them for the conversion back into
888 * MIR (at least until we stop using the Dalvik register maps).
889 * Insert a dummy intrinsic copy call, which will be recognized
890 * by the quick path and removed by the portable path.
891 */
buzbeefa57c472012-11-21 12:06:18 -0800892 llvm::Value* src = GetLLVMValue(cu, rl_src[0].orig_sreg);
893 llvm::Value* res = EmitCopy(cu, src, rl_dest);
894 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700895 }
896 break;
897
898 case Instruction::CONST:
899 case Instruction::CONST_4:
900 case Instruction::CONST_16: {
buzbeefa57c472012-11-21 12:06:18 -0800901 llvm::Constant* imm_value = cu->irb->GetJInt(vB);
902 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
903 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700904 }
905 break;
906
907 case Instruction::CONST_WIDE_16:
908 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700909 // Sign extend to 64 bits
910 int64_t imm = static_cast<int32_t>(vB);
buzbeefa57c472012-11-21 12:06:18 -0800911 llvm::Constant* imm_value = cu->irb->GetJLong(imm);
912 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
913 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700914 }
915 break;
916
917 case Instruction::CONST_HIGH16: {
buzbeefa57c472012-11-21 12:06:18 -0800918 llvm::Constant* imm_value = cu->irb->GetJInt(vB << 16);
919 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
920 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700921 }
922 break;
923
924 case Instruction::CONST_WIDE: {
buzbeefa57c472012-11-21 12:06:18 -0800925 llvm::Constant* imm_value =
926 cu->irb->GetJLong(mir->dalvikInsn.vB_wide);
927 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
928 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700929 }
930 break;
buzbee2cfc6392012-05-07 14:51:40 -0700931 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700932 int64_t imm = static_cast<int64_t>(vB) << 48;
buzbeefa57c472012-11-21 12:06:18 -0800933 llvm::Constant* imm_value = cu->irb->GetJLong(imm);
934 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
935 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700936 }
937 break;
938
buzbee8fa0fda2012-06-27 15:44:52 -0700939 case Instruction::SPUT_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -0800940 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputObject,
941 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700942 break;
943 case Instruction::SPUT:
buzbeefa57c472012-11-21 12:06:18 -0800944 if (rl_src[0].fp) {
945 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputFloat,
946 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700947 } else {
buzbeefa57c472012-11-21 12:06:18 -0800948 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSput, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700949 }
950 break;
951 case Instruction::SPUT_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -0800952 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputBoolean,
953 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700954 break;
955 case Instruction::SPUT_BYTE:
buzbeefa57c472012-11-21 12:06:18 -0800956 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputByte, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700957 break;
958 case Instruction::SPUT_CHAR:
buzbeefa57c472012-11-21 12:06:18 -0800959 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputChar, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700960 break;
961 case Instruction::SPUT_SHORT:
buzbeefa57c472012-11-21 12:06:18 -0800962 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputShort, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700963 break;
964 case Instruction::SPUT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800965 if (rl_src[0].fp) {
966 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputDouble,
967 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700968 } else {
buzbeefa57c472012-11-21 12:06:18 -0800969 ConvertSput(cu, vB, greenland::IntrinsicHelper::HLSputWide,
970 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700971 }
972 break;
973
974 case Instruction::SGET_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -0800975 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetObject, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700976 break;
977 case Instruction::SGET:
buzbeefa57c472012-11-21 12:06:18 -0800978 if (rl_dest.fp) {
979 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetFloat, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700980 } else {
buzbeefa57c472012-11-21 12:06:18 -0800981 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSget, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700982 }
983 break;
984 case Instruction::SGET_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -0800985 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetBoolean, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700986 break;
987 case Instruction::SGET_BYTE:
buzbeefa57c472012-11-21 12:06:18 -0800988 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetByte, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700989 break;
990 case Instruction::SGET_CHAR:
buzbeefa57c472012-11-21 12:06:18 -0800991 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetChar, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700992 break;
993 case Instruction::SGET_SHORT:
buzbeefa57c472012-11-21 12:06:18 -0800994 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetShort, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700995 break;
996 case Instruction::SGET_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800997 if (rl_dest.fp) {
998 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetDouble,
999 rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -07001000 } else {
buzbeefa57c472012-11-21 12:06:18 -08001001 ConvertSget(cu, vB, greenland::IntrinsicHelper::HLSgetWide, rl_dest);
buzbee4f1181f2012-06-22 13:52:12 -07001002 }
1003 break;
buzbee2cfc6392012-05-07 14:51:40 -07001004
1005 case Instruction::RETURN_WIDE:
1006 case Instruction::RETURN:
1007 case Instruction::RETURN_OBJECT: {
buzbeefa57c472012-11-21 12:06:18 -08001008 if (!(cu->attrs & METHOD_IS_LEAF)) {
1009 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001010 }
buzbeefa57c472012-11-21 12:06:18 -08001011 EmitPopShadowFrame(cu);
1012 cu->irb->CreateRet(GetLLVMValue(cu, rl_src[0].orig_sreg));
1013 bb->has_return = true;
buzbee2cfc6392012-05-07 14:51:40 -07001014 }
1015 break;
1016
1017 case Instruction::RETURN_VOID: {
buzbeefa57c472012-11-21 12:06:18 -08001018 if (!(cu->attrs & METHOD_IS_LEAF)) {
1019 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001020 }
buzbeefa57c472012-11-21 12:06:18 -08001021 EmitPopShadowFrame(cu);
1022 cu->irb->CreateRetVoid();
1023 bb->has_return = true;
buzbee2cfc6392012-05-07 14:51:40 -07001024 }
1025 break;
1026
1027 case Instruction::IF_EQ:
buzbeefa57c472012-11-21 12:06:18 -08001028 ConvertCompareAndBranch(cu, bb, mir, kCondEq, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001029 break;
1030 case Instruction::IF_NE:
buzbeefa57c472012-11-21 12:06:18 -08001031 ConvertCompareAndBranch(cu, bb, mir, kCondNe, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001032 break;
1033 case Instruction::IF_LT:
buzbeefa57c472012-11-21 12:06:18 -08001034 ConvertCompareAndBranch(cu, bb, mir, kCondLt, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001035 break;
1036 case Instruction::IF_GE:
buzbeefa57c472012-11-21 12:06:18 -08001037 ConvertCompareAndBranch(cu, bb, mir, kCondGe, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001038 break;
1039 case Instruction::IF_GT:
buzbeefa57c472012-11-21 12:06:18 -08001040 ConvertCompareAndBranch(cu, bb, mir, kCondGt, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001041 break;
1042 case Instruction::IF_LE:
buzbeefa57c472012-11-21 12:06:18 -08001043 ConvertCompareAndBranch(cu, bb, mir, kCondLe, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001044 break;
1045 case Instruction::IF_EQZ:
buzbeefa57c472012-11-21 12:06:18 -08001046 ConvertCompareZeroAndBranch(cu, bb, mir, kCondEq, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001047 break;
1048 case Instruction::IF_NEZ:
buzbeefa57c472012-11-21 12:06:18 -08001049 ConvertCompareZeroAndBranch(cu, bb, mir, kCondNe, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001050 break;
1051 case Instruction::IF_LTZ:
buzbeefa57c472012-11-21 12:06:18 -08001052 ConvertCompareZeroAndBranch(cu, bb, mir, kCondLt, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001053 break;
1054 case Instruction::IF_GEZ:
buzbeefa57c472012-11-21 12:06:18 -08001055 ConvertCompareZeroAndBranch(cu, bb, mir, kCondGe, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001056 break;
1057 case Instruction::IF_GTZ:
buzbeefa57c472012-11-21 12:06:18 -08001058 ConvertCompareZeroAndBranch(cu, bb, mir, kCondGt, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001059 break;
1060 case Instruction::IF_LEZ:
buzbeefa57c472012-11-21 12:06:18 -08001061 ConvertCompareZeroAndBranch(cu, bb, mir, kCondLe, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001062 break;
1063
1064 case Instruction::GOTO:
1065 case Instruction::GOTO_16:
1066 case Instruction::GOTO_32: {
buzbeefa57c472012-11-21 12:06:18 -08001067 if (bb->taken->start_offset <= bb->start_offset) {
1068 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001069 }
buzbeefa57c472012-11-21 12:06:18 -08001070 cu->irb->CreateBr(GetLLVMBlock(cu, bb->taken->id));
buzbee2cfc6392012-05-07 14:51:40 -07001071 }
1072 break;
1073
1074 case Instruction::ADD_LONG:
1075 case Instruction::ADD_LONG_2ADDR:
1076 case Instruction::ADD_INT:
1077 case Instruction::ADD_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001078 ConvertArithOp(cu, kOpAdd, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001079 break;
1080 case Instruction::SUB_LONG:
1081 case Instruction::SUB_LONG_2ADDR:
1082 case Instruction::SUB_INT:
1083 case Instruction::SUB_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001084 ConvertArithOp(cu, kOpSub, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001085 break;
1086 case Instruction::MUL_LONG:
1087 case Instruction::MUL_LONG_2ADDR:
1088 case Instruction::MUL_INT:
1089 case Instruction::MUL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001090 ConvertArithOp(cu, kOpMul, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001091 break;
1092 case Instruction::DIV_LONG:
1093 case Instruction::DIV_LONG_2ADDR:
1094 case Instruction::DIV_INT:
1095 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001096 ConvertArithOp(cu, kOpDiv, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001097 break;
1098 case Instruction::REM_LONG:
1099 case Instruction::REM_LONG_2ADDR:
1100 case Instruction::REM_INT:
1101 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001102 ConvertArithOp(cu, kOpRem, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001103 break;
1104 case Instruction::AND_LONG:
1105 case Instruction::AND_LONG_2ADDR:
1106 case Instruction::AND_INT:
1107 case Instruction::AND_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001108 ConvertArithOp(cu, kOpAnd, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001109 break;
1110 case Instruction::OR_LONG:
1111 case Instruction::OR_LONG_2ADDR:
1112 case Instruction::OR_INT:
1113 case Instruction::OR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001114 ConvertArithOp(cu, kOpOr, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001115 break;
1116 case Instruction::XOR_LONG:
1117 case Instruction::XOR_LONG_2ADDR:
1118 case Instruction::XOR_INT:
1119 case Instruction::XOR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001120 ConvertArithOp(cu, kOpXor, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001121 break;
1122 case Instruction::SHL_LONG:
1123 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001124 ConvertShift(cu, greenland::IntrinsicHelper::SHLLong,
1125 rl_dest, rl_src[0], rl_src[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001126 break;
buzbee2cfc6392012-05-07 14:51:40 -07001127 case Instruction::SHL_INT:
1128 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001129 ConvertShift(cu, greenland::IntrinsicHelper::SHLInt,
1130 rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001131 break;
1132 case Instruction::SHR_LONG:
1133 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001134 ConvertShift(cu, greenland::IntrinsicHelper::SHRLong,
1135 rl_dest, rl_src[0], rl_src[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001136 break;
buzbee2cfc6392012-05-07 14:51:40 -07001137 case Instruction::SHR_INT:
1138 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001139 ConvertShift(cu, greenland::IntrinsicHelper::SHRInt,
1140 rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001141 break;
1142 case Instruction::USHR_LONG:
1143 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001144 ConvertShift(cu, greenland::IntrinsicHelper::USHRLong,
1145 rl_dest, rl_src[0], rl_src[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001146 break;
buzbee2cfc6392012-05-07 14:51:40 -07001147 case Instruction::USHR_INT:
1148 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001149 ConvertShift(cu, greenland::IntrinsicHelper::USHRInt,
1150 rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001151 break;
1152
1153 case Instruction::ADD_INT_LIT16:
1154 case Instruction::ADD_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001155 ConvertArithOpLit(cu, kOpAdd, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001156 break;
1157 case Instruction::RSUB_INT:
1158 case Instruction::RSUB_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001159 ConvertArithOpLit(cu, kOpRsub, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001160 break;
1161 case Instruction::MUL_INT_LIT16:
1162 case Instruction::MUL_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001163 ConvertArithOpLit(cu, kOpMul, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001164 break;
1165 case Instruction::DIV_INT_LIT16:
1166 case Instruction::DIV_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001167 ConvertArithOpLit(cu, kOpDiv, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001168 break;
1169 case Instruction::REM_INT_LIT16:
1170 case Instruction::REM_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001171 ConvertArithOpLit(cu, kOpRem, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001172 break;
1173 case Instruction::AND_INT_LIT16:
1174 case Instruction::AND_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001175 ConvertArithOpLit(cu, kOpAnd, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001176 break;
1177 case Instruction::OR_INT_LIT16:
1178 case Instruction::OR_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001179 ConvertArithOpLit(cu, kOpOr, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001180 break;
1181 case Instruction::XOR_INT_LIT16:
1182 case Instruction::XOR_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001183 ConvertArithOpLit(cu, kOpXor, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001184 break;
1185 case Instruction::SHL_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001186 ConvertShiftLit(cu, greenland::IntrinsicHelper::SHLInt,
1187 rl_dest, rl_src[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001188 break;
1189 case Instruction::SHR_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001190 ConvertShiftLit(cu, greenland::IntrinsicHelper::SHRInt,
1191 rl_dest, rl_src[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001192 break;
1193 case Instruction::USHR_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001194 ConvertShiftLit(cu, greenland::IntrinsicHelper::USHRInt,
1195 rl_dest, rl_src[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001196 break;
1197
1198 case Instruction::ADD_FLOAT:
1199 case Instruction::ADD_FLOAT_2ADDR:
1200 case Instruction::ADD_DOUBLE:
1201 case Instruction::ADD_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001202 ConvertFPArithOp(cu, kOpAdd, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001203 break;
1204
1205 case Instruction::SUB_FLOAT:
1206 case Instruction::SUB_FLOAT_2ADDR:
1207 case Instruction::SUB_DOUBLE:
1208 case Instruction::SUB_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001209 ConvertFPArithOp(cu, kOpSub, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001210 break;
1211
1212 case Instruction::MUL_FLOAT:
1213 case Instruction::MUL_FLOAT_2ADDR:
1214 case Instruction::MUL_DOUBLE:
1215 case Instruction::MUL_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001216 ConvertFPArithOp(cu, kOpMul, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001217 break;
1218
1219 case Instruction::DIV_FLOAT:
1220 case Instruction::DIV_FLOAT_2ADDR:
1221 case Instruction::DIV_DOUBLE:
1222 case Instruction::DIV_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001223 ConvertFPArithOp(cu, kOpDiv, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001224 break;
1225
1226 case Instruction::REM_FLOAT:
1227 case Instruction::REM_FLOAT_2ADDR:
1228 case Instruction::REM_DOUBLE:
1229 case Instruction::REM_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001230 ConvertFPArithOp(cu, kOpRem, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001231 break;
1232
buzbee6969d502012-06-15 16:40:31 -07001233 case Instruction::INVOKE_STATIC:
buzbeefa57c472012-11-21 12:06:18 -08001234 ConvertInvoke(cu, bb, mir, kStatic, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001235 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001236 break;
1237 case Instruction::INVOKE_STATIC_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001238 ConvertInvoke(cu, bb, mir, kStatic, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001239 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001240 break;
1241
1242 case Instruction::INVOKE_DIRECT:
buzbeefa57c472012-11-21 12:06:18 -08001243 ConvertInvoke(cu, bb, mir, kDirect, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001244 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001245 break;
1246 case Instruction::INVOKE_DIRECT_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001247 ConvertInvoke(cu, bb, mir, kDirect, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001248 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001249 break;
1250
1251 case Instruction::INVOKE_VIRTUAL:
buzbeefa57c472012-11-21 12:06:18 -08001252 ConvertInvoke(cu, bb, mir, kVirtual, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001253 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001254 break;
1255 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001256 ConvertInvoke(cu, bb, mir, kVirtual, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001257 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001258 break;
1259
1260 case Instruction::INVOKE_SUPER:
buzbeefa57c472012-11-21 12:06:18 -08001261 ConvertInvoke(cu, bb, mir, kSuper, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001262 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001263 break;
1264 case Instruction::INVOKE_SUPER_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001265 ConvertInvoke(cu, bb, mir, kSuper, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001266 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001267 break;
1268
1269 case Instruction::INVOKE_INTERFACE:
buzbeefa57c472012-11-21 12:06:18 -08001270 ConvertInvoke(cu, bb, mir, kInterface, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001271 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001272 break;
1273 case Instruction::INVOKE_INTERFACE_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001274 ConvertInvoke(cu, bb, mir, kInterface, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001275 false /* NewFilledArray */);
1276 break;
1277 case Instruction::FILLED_NEW_ARRAY:
buzbeefa57c472012-11-21 12:06:18 -08001278 ConvertInvoke(cu, bb, mir, kInterface, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001279 true /* NewFilledArray */);
1280 break;
1281 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001282 ConvertInvoke(cu, bb, mir, kInterface, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001283 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001284 break;
1285
1286 case Instruction::CONST_STRING:
1287 case Instruction::CONST_STRING_JUMBO:
buzbeefa57c472012-11-21 12:06:18 -08001288 ConvertConstObject(cu, vB, greenland::IntrinsicHelper::ConstString,
1289 rl_dest);
buzbee101305f2012-06-28 18:00:56 -07001290 break;
1291
1292 case Instruction::CONST_CLASS:
buzbeefa57c472012-11-21 12:06:18 -08001293 ConvertConstObject(cu, vB, greenland::IntrinsicHelper::ConstClass,
1294 rl_dest);
buzbee101305f2012-06-28 18:00:56 -07001295 break;
1296
1297 case Instruction::CHECK_CAST:
buzbeefa57c472012-11-21 12:06:18 -08001298 ConvertCheckCast(cu, vB, rl_src[0]);
buzbee6969d502012-06-15 16:40:31 -07001299 break;
1300
buzbee4f1181f2012-06-22 13:52:12 -07001301 case Instruction::NEW_INSTANCE:
buzbeefa57c472012-11-21 12:06:18 -08001302 ConvertNewInstance(cu, vB, rl_dest);
buzbee4f1181f2012-06-22 13:52:12 -07001303 break;
1304
buzbee32412962012-06-26 16:27:56 -07001305 case Instruction::MOVE_EXCEPTION:
buzbeefa57c472012-11-21 12:06:18 -08001306 ConvertMoveException(cu, rl_dest);
buzbee32412962012-06-26 16:27:56 -07001307 break;
1308
1309 case Instruction::THROW:
buzbeefa57c472012-11-21 12:06:18 -08001310 ConvertThrow(cu, rl_src[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001311 /*
1312 * If this throw is standalone, terminate.
1313 * If it might rethrow, force termination
1314 * of the following block.
1315 */
buzbeefa57c472012-11-21 12:06:18 -08001316 if (bb->fall_through == NULL) {
1317 cu->irb->CreateUnreachable();
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001318 } else {
buzbeefa57c472012-11-21 12:06:18 -08001319 bb->fall_through->fall_through = NULL;
1320 bb->fall_through->taken = NULL;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001321 }
buzbee32412962012-06-26 16:27:56 -07001322 break;
1323
buzbee2cfc6392012-05-07 14:51:40 -07001324 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001325 case Instruction::MOVE_RESULT:
1326 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001327 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001328 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001329 */
jeffhao9a4f0032012-08-30 16:17:40 -07001330 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001331 break;
1332
1333 case Instruction::MONITOR_ENTER:
buzbeefa57c472012-11-21 12:06:18 -08001334 ConvertMonitorEnterExit(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001335 greenland::IntrinsicHelper::MonitorEnter,
buzbeefa57c472012-11-21 12:06:18 -08001336 rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001337 break;
1338
1339 case Instruction::MONITOR_EXIT:
buzbeefa57c472012-11-21 12:06:18 -08001340 ConvertMonitorEnterExit(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001341 greenland::IntrinsicHelper::MonitorExit,
buzbeefa57c472012-11-21 12:06:18 -08001342 rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001343 break;
1344
1345 case Instruction::ARRAY_LENGTH:
buzbeefa57c472012-11-21 12:06:18 -08001346 ConvertArrayLength(cu, opt_flags, rl_dest, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001347 break;
1348
1349 case Instruction::NEW_ARRAY:
buzbeefa57c472012-11-21 12:06:18 -08001350 ConvertNewArray(cu, vC, rl_dest, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001351 break;
1352
1353 case Instruction::INSTANCE_OF:
buzbeefa57c472012-11-21 12:06:18 -08001354 ConvertInstanceOf(cu, vC, rl_dest, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001355 break;
1356
1357 case Instruction::AGET:
buzbeefa57c472012-11-21 12:06:18 -08001358 if (rl_dest.fp) {
1359 ConvertAget(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001360 greenland::IntrinsicHelper::HLArrayGetFloat,
buzbeefa57c472012-11-21 12:06:18 -08001361 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001362 } else {
buzbeefa57c472012-11-21 12:06:18 -08001363 ConvertAget(cu, opt_flags, greenland::IntrinsicHelper::HLArrayGet,
1364 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001365 }
1366 break;
1367 case Instruction::AGET_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -08001368 ConvertAget(cu, opt_flags, greenland::IntrinsicHelper::HLArrayGetObject,
1369 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001370 break;
1371 case Instruction::AGET_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -08001372 ConvertAget(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001373 greenland::IntrinsicHelper::HLArrayGetBoolean,
buzbeefa57c472012-11-21 12:06:18 -08001374 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001375 break;
1376 case Instruction::AGET_BYTE:
buzbeefa57c472012-11-21 12:06:18 -08001377 ConvertAget(cu, opt_flags, greenland::IntrinsicHelper::HLArrayGetByte,
1378 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001379 break;
1380 case Instruction::AGET_CHAR:
buzbeefa57c472012-11-21 12:06:18 -08001381 ConvertAget(cu, opt_flags, greenland::IntrinsicHelper::HLArrayGetChar,
1382 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001383 break;
1384 case Instruction::AGET_SHORT:
buzbeefa57c472012-11-21 12:06:18 -08001385 ConvertAget(cu, opt_flags, greenland::IntrinsicHelper::HLArrayGetShort,
1386 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001387 break;
1388 case Instruction::AGET_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001389 if (rl_dest.fp) {
1390 ConvertAget(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001391 greenland::IntrinsicHelper::HLArrayGetDouble,
buzbeefa57c472012-11-21 12:06:18 -08001392 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001393 } else {
buzbeefa57c472012-11-21 12:06:18 -08001394 ConvertAget(cu, opt_flags, greenland::IntrinsicHelper::HLArrayGetWide,
1395 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001396 }
1397 break;
1398
1399 case Instruction::APUT:
buzbeefa57c472012-11-21 12:06:18 -08001400 if (rl_src[0].fp) {
1401 ConvertAput(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001402 greenland::IntrinsicHelper::HLArrayPutFloat,
buzbeefa57c472012-11-21 12:06:18 -08001403 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001404 } else {
buzbeefa57c472012-11-21 12:06:18 -08001405 ConvertAput(cu, opt_flags, greenland::IntrinsicHelper::HLArrayPut,
1406 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001407 }
1408 break;
1409 case Instruction::APUT_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -08001410 ConvertAput(cu, opt_flags, greenland::IntrinsicHelper::HLArrayPutObject,
1411 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001412 break;
1413 case Instruction::APUT_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -08001414 ConvertAput(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001415 greenland::IntrinsicHelper::HLArrayPutBoolean,
buzbeefa57c472012-11-21 12:06:18 -08001416 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001417 break;
1418 case Instruction::APUT_BYTE:
buzbeefa57c472012-11-21 12:06:18 -08001419 ConvertAput(cu, opt_flags, greenland::IntrinsicHelper::HLArrayPutByte,
1420 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001421 break;
1422 case Instruction::APUT_CHAR:
buzbeefa57c472012-11-21 12:06:18 -08001423 ConvertAput(cu, opt_flags, greenland::IntrinsicHelper::HLArrayPutChar,
1424 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001425 break;
1426 case Instruction::APUT_SHORT:
buzbeefa57c472012-11-21 12:06:18 -08001427 ConvertAput(cu, opt_flags, greenland::IntrinsicHelper::HLArrayPutShort,
1428 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001429 break;
1430 case Instruction::APUT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001431 if (rl_src[0].fp) {
1432 ConvertAput(cu, opt_flags,
buzbee8fa0fda2012-06-27 15:44:52 -07001433 greenland::IntrinsicHelper::HLArrayPutDouble,
buzbeefa57c472012-11-21 12:06:18 -08001434 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001435 } else {
buzbeefa57c472012-11-21 12:06:18 -08001436 ConvertAput(cu, opt_flags, greenland::IntrinsicHelper::HLArrayPutWide,
1437 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001438 }
1439 break;
1440
buzbee101305f2012-06-28 18:00:56 -07001441 case Instruction::IGET:
buzbeefa57c472012-11-21 12:06:18 -08001442 if (rl_dest.fp) {
1443 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetFloat,
1444 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001445 } else {
buzbeefa57c472012-11-21 12:06:18 -08001446 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGet,
1447 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001448 }
buzbee2cfc6392012-05-07 14:51:40 -07001449 break;
buzbee101305f2012-06-28 18:00:56 -07001450 case Instruction::IGET_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -08001451 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetObject,
1452 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001453 break;
1454 case Instruction::IGET_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -08001455 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetBoolean,
1456 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001457 break;
1458 case Instruction::IGET_BYTE:
buzbeefa57c472012-11-21 12:06:18 -08001459 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetByte,
1460 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001461 break;
1462 case Instruction::IGET_CHAR:
buzbeefa57c472012-11-21 12:06:18 -08001463 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetChar,
1464 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001465 break;
1466 case Instruction::IGET_SHORT:
buzbeefa57c472012-11-21 12:06:18 -08001467 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetShort,
1468 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001469 break;
1470 case Instruction::IGET_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001471 if (rl_dest.fp) {
1472 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetDouble,
1473 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001474 } else {
buzbeefa57c472012-11-21 12:06:18 -08001475 ConvertIget(cu, opt_flags, greenland::IntrinsicHelper::HLIGetWide,
1476 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001477 }
1478 break;
1479 case Instruction::IPUT:
buzbeefa57c472012-11-21 12:06:18 -08001480 if (rl_src[0].fp) {
1481 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutFloat,
1482 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001483 } else {
buzbeefa57c472012-11-21 12:06:18 -08001484 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPut,
1485 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001486 }
1487 break;
1488 case Instruction::IPUT_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -08001489 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutObject,
1490 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001491 break;
1492 case Instruction::IPUT_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -08001493 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutBoolean,
1494 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001495 break;
1496 case Instruction::IPUT_BYTE:
buzbeefa57c472012-11-21 12:06:18 -08001497 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutByte,
1498 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001499 break;
1500 case Instruction::IPUT_CHAR:
buzbeefa57c472012-11-21 12:06:18 -08001501 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutChar,
1502 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001503 break;
1504 case Instruction::IPUT_SHORT:
buzbeefa57c472012-11-21 12:06:18 -08001505 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutShort,
1506 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001507 break;
1508 case Instruction::IPUT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001509 if (rl_src[0].fp) {
1510 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutDouble,
1511 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001512 } else {
buzbeefa57c472012-11-21 12:06:18 -08001513 ConvertIput(cu, opt_flags, greenland::IntrinsicHelper::HLIPutWide,
1514 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001515 }
buzbee2cfc6392012-05-07 14:51:40 -07001516 break;
1517
1518 case Instruction::FILL_ARRAY_DATA:
buzbeefa57c472012-11-21 12:06:18 -08001519 ConvertFillArrayData(cu, vB, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001520 break;
1521
buzbee76592632012-06-29 15:18:35 -07001522 case Instruction::LONG_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -08001523 ConvertLongToInt(cu, rl_dest, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001524 break;
1525
buzbee101305f2012-06-28 18:00:56 -07001526 case Instruction::INT_TO_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001527 ConvertIntToLong(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001528 break;
1529
buzbee101305f2012-06-28 18:00:56 -07001530 case Instruction::INT_TO_CHAR:
buzbeefa57c472012-11-21 12:06:18 -08001531 ConvertIntNarrowing(cu, rl_dest, rl_src[0],
buzbee101305f2012-06-28 18:00:56 -07001532 greenland::IntrinsicHelper::IntToChar);
1533 break;
1534 case Instruction::INT_TO_BYTE:
buzbeefa57c472012-11-21 12:06:18 -08001535 ConvertIntNarrowing(cu, rl_dest, rl_src[0],
buzbee101305f2012-06-28 18:00:56 -07001536 greenland::IntrinsicHelper::IntToByte);
1537 break;
1538 case Instruction::INT_TO_SHORT:
buzbeefa57c472012-11-21 12:06:18 -08001539 ConvertIntNarrowing(cu, rl_dest, rl_src[0],
buzbee101305f2012-06-28 18:00:56 -07001540 greenland::IntrinsicHelper::IntToShort);
1541 break;
1542
buzbee76592632012-06-29 15:18:35 -07001543 case Instruction::INT_TO_FLOAT:
1544 case Instruction::LONG_TO_FLOAT:
buzbeefa57c472012-11-21 12:06:18 -08001545 ConvertIntToFP(cu, cu->irb->getFloatTy(), rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001546 break;
1547
buzbee76592632012-06-29 15:18:35 -07001548 case Instruction::INT_TO_DOUBLE:
1549 case Instruction::LONG_TO_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001550 ConvertIntToFP(cu, cu->irb->getDoubleTy(), rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001551 break;
1552
buzbee76592632012-06-29 15:18:35 -07001553 case Instruction::FLOAT_TO_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001554 ConvertFloatToDouble(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001555 break;
1556
buzbee76592632012-06-29 15:18:35 -07001557 case Instruction::DOUBLE_TO_FLOAT:
buzbeefa57c472012-11-21 12:06:18 -08001558 ConvertDoubleToFloat(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001559 break;
1560
1561 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001562 case Instruction::NEG_INT:
buzbeefa57c472012-11-21 12:06:18 -08001563 ConvertNeg(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001564 break;
1565
1566 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001567 case Instruction::NEG_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001568 ConvertNegFP(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001569 break;
1570
buzbee76592632012-06-29 15:18:35 -07001571 case Instruction::NOT_LONG:
1572 case Instruction::NOT_INT:
buzbeefa57c472012-11-21 12:06:18 -08001573 ConvertNot(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001574 break;
1575
buzbee2cfc6392012-05-07 14:51:40 -07001576 case Instruction::FLOAT_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -08001577 ConvertFPToInt(cu, greenland::IntrinsicHelper::F2I, rl_dest, rl_src[0]);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001578 break;
1579
buzbee2cfc6392012-05-07 14:51:40 -07001580 case Instruction::DOUBLE_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -08001581 ConvertFPToInt(cu, greenland::IntrinsicHelper::D2I, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001582 break;
1583
buzbee76592632012-06-29 15:18:35 -07001584 case Instruction::FLOAT_TO_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001585 ConvertFPToInt(cu, greenland::IntrinsicHelper::F2L, rl_dest, rl_src[0]);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001586 break;
1587
buzbee76592632012-06-29 15:18:35 -07001588 case Instruction::DOUBLE_TO_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001589 ConvertFPToInt(cu, greenland::IntrinsicHelper::D2L, rl_dest, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001590 break;
1591
1592 case Instruction::CMPL_FLOAT:
buzbeefa57c472012-11-21 12:06:18 -08001593 ConvertWideComparison(cu, greenland::IntrinsicHelper::CmplFloat,
1594 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001595 break;
1596 case Instruction::CMPG_FLOAT:
buzbeefa57c472012-11-21 12:06:18 -08001597 ConvertWideComparison(cu, greenland::IntrinsicHelper::CmpgFloat,
1598 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001599 break;
1600 case Instruction::CMPL_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001601 ConvertWideComparison(cu, greenland::IntrinsicHelper::CmplDouble,
1602 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001603 break;
1604 case Instruction::CMPG_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001605 ConvertWideComparison(cu, greenland::IntrinsicHelper::CmpgDouble,
1606 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001607 break;
1608 case Instruction::CMP_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001609 ConvertWideComparison(cu, greenland::IntrinsicHelper::CmpLong,
1610 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001611 break;
1612
buzbee76592632012-06-29 15:18:35 -07001613 case Instruction::PACKED_SWITCH:
buzbeefa57c472012-11-21 12:06:18 -08001614 ConvertPackedSwitch(cu, bb, vB, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001615 break;
1616
1617 case Instruction::SPARSE_SWITCH:
buzbeefa57c472012-11-21 12:06:18 -08001618 ConvertSparseSwitch(cu, bb, vB, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001619 break;
buzbee2cfc6392012-05-07 14:51:40 -07001620
1621 default:
buzbee32412962012-06-26 16:27:56 -07001622 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001623 res = true;
1624 }
1625 return res;
1626}
1627
1628/* Extended MIR instructions like PHI */
buzbeefa57c472012-11-21 12:06:18 -08001629static void ConvertExtendedMIR(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
1630 llvm::BasicBlock* llvm_bb)
buzbee2cfc6392012-05-07 14:51:40 -07001631{
1632
buzbeecbd6d442012-11-17 14:11:25 -08001633 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
buzbee2cfc6392012-05-07 14:51:40 -07001634 case kMirOpPhi: {
buzbeefa57c472012-11-21 12:06:18 -08001635 RegLocation rl_dest = cu->reg_location[mir->ssa_rep->defs[0]];
buzbee2a83e8f2012-07-13 16:42:30 -07001636 /*
1637 * The Art compiler's Phi nodes only handle 32-bit operands,
1638 * representing wide values using a matched set of Phi nodes
1639 * for the lower and upper halves. In the llvm world, we only
1640 * want a single Phi for wides. Here we will simply discard
1641 * the Phi node representing the high word.
1642 */
buzbeefa57c472012-11-21 12:06:18 -08001643 if (rl_dest.high_word) {
buzbee2a83e8f2012-07-13 16:42:30 -07001644 return; // No Phi node - handled via low word
1645 }
buzbeecbd6d442012-11-17 14:11:25 -08001646 int* incoming = reinterpret_cast<int*>(mir->dalvikInsn.vB);
buzbeefa57c472012-11-21 12:06:18 -08001647 llvm::Type* phi_type =
1648 LlvmTypeFromLocRec(cu, rl_dest);
1649 llvm::PHINode* phi = cu->irb->CreatePHI(phi_type, mir->ssa_rep->num_uses);
1650 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
buzbee2cfc6392012-05-07 14:51:40 -07001651 RegLocation loc;
buzbee2a83e8f2012-07-13 16:42:30 -07001652 // Don't check width here.
buzbeefa57c472012-11-21 12:06:18 -08001653 loc = GetRawSrc(cu, mir, i);
1654 DCHECK_EQ(rl_dest.wide, loc.wide);
1655 DCHECK_EQ(rl_dest.wide & rl_dest.high_word, loc.wide & loc.high_word);
1656 DCHECK_EQ(rl_dest.fp, loc.fp);
1657 DCHECK_EQ(rl_dest.core, loc.core);
1658 DCHECK_EQ(rl_dest.ref, loc.ref);
buzbeed1643e42012-09-05 14:06:51 -07001659 SafeMap<unsigned int, unsigned int>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -08001660 it = cu->block_id_map.find(incoming[i]);
1661 DCHECK(it != cu->block_id_map.end());
1662 phi->addIncoming(GetLLVMValue(cu, loc.orig_sreg),
1663 GetLLVMBlock(cu, it->second));
buzbee2cfc6392012-05-07 14:51:40 -07001664 }
buzbeefa57c472012-11-21 12:06:18 -08001665 DefineValue(cu, phi, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -07001666 break;
1667 }
1668 case kMirOpCopy: {
1669 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1670 break;
1671 }
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001672 case kMirOpNop:
buzbeefa57c472012-11-21 12:06:18 -08001673 if ((mir == bb->last_mir_insn) && (bb->taken == NULL) &&
1674 (bb->fall_through == NULL)) {
1675 cu->irb->CreateUnreachable();
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001676 }
1677 break;
1678
buzbeeb046e162012-10-30 15:48:42 -07001679 // TODO: need GBC intrinsic to take advantage of fused operations
buzbee2cfc6392012-05-07 14:51:40 -07001680 case kMirOpFusedCmplFloat:
buzbeeb046e162012-10-30 15:48:42 -07001681 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpFloat unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001682 break;
1683 case kMirOpFusedCmpgFloat:
buzbeeb046e162012-10-30 15:48:42 -07001684 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmgFloat unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001685 break;
1686 case kMirOpFusedCmplDouble:
buzbeeb046e162012-10-30 15:48:42 -07001687 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmplDouble unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001688 break;
1689 case kMirOpFusedCmpgDouble:
buzbeeb046e162012-10-30 15:48:42 -07001690 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpgDouble unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001691 break;
1692 case kMirOpFusedCmpLong:
buzbeeb046e162012-10-30 15:48:42 -07001693 UNIMPLEMENTED(FATAL) << "kMirOpLongCmpBranch unsupported";
buzbee2cfc6392012-05-07 14:51:40 -07001694 break;
buzbee2cfc6392012-05-07 14:51:40 -07001695 default:
1696 break;
1697 }
1698}
1699
buzbeefa57c472012-11-21 12:06:18 -08001700static void SetDexOffset(CompilationUnit* cu, int32_t offset)
buzbee2cfc6392012-05-07 14:51:40 -07001701{
buzbeefa57c472012-11-21 12:06:18 -08001702 cu->current_dalvik_offset = offset;
1703 llvm::SmallVector<llvm::Value*, 1> array_ref;
1704 array_ref.push_back(cu->irb->getInt32(offset));
1705 llvm::MDNode* node = llvm::MDNode::get(*cu->context, array_ref);
1706 cu->irb->SetDexOffset(node);
buzbee2cfc6392012-05-07 14:51:40 -07001707}
1708
1709// Attach method info as metadata to special intrinsic
buzbeefa57c472012-11-21 12:06:18 -08001710static void SetMethodInfo(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -07001711{
1712 // We don't want dex offset on this
buzbeefa57c472012-11-21 12:06:18 -08001713 cu->irb->SetDexOffset(NULL);
buzbee2cfc6392012-05-07 14:51:40 -07001714 greenland::IntrinsicHelper::IntrinsicId id;
1715 id = greenland::IntrinsicHelper::MethodInfo;
buzbeefa57c472012-11-21 12:06:18 -08001716 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
1717 llvm::Instruction* inst = cu->irb->CreateCall(intr);
1718 llvm::SmallVector<llvm::Value*, 2> reg_info;
1719 reg_info.push_back(cu->irb->getInt32(cu->num_ins));
1720 reg_info.push_back(cu->irb->getInt32(cu->num_regs));
1721 reg_info.push_back(cu->irb->getInt32(cu->num_outs));
1722 reg_info.push_back(cu->irb->getInt32(cu->num_compiler_temps));
1723 reg_info.push_back(cu->irb->getInt32(cu->num_ssa_regs));
1724 llvm::MDNode* reg_info_node = llvm::MDNode::get(*cu->context, reg_info);
1725 inst->setMetadata("RegInfo", reg_info_node);
1726 int promo_size = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
buzbee2cfc6392012-05-07 14:51:40 -07001727 llvm::SmallVector<llvm::Value*, 50> pmap;
buzbeefa57c472012-11-21 12:06:18 -08001728 for (int i = 0; i < promo_size; i++) {
1729 PromotionMap* p = &cu->promotion_map[i];
1730 int32_t map_data = ((p->first_in_pair & 0xff) << 24) |
buzbee52a77fc2012-11-20 19:50:46 -08001731 ((p->FpReg & 0xff) << 16) |
buzbeefa57c472012-11-21 12:06:18 -08001732 ((p->core_reg & 0xff) << 8) |
1733 ((p->fp_location & 0xf) << 4) |
1734 (p->core_location & 0xf);
1735 pmap.push_back(cu->irb->getInt32(map_data));
buzbee2cfc6392012-05-07 14:51:40 -07001736 }
buzbeefa57c472012-11-21 12:06:18 -08001737 llvm::MDNode* map_node = llvm::MDNode::get(*cu->context, pmap);
1738 inst->setMetadata("PromotionMap", map_node);
1739 SetDexOffset(cu, cu->current_dalvik_offset);
buzbee2cfc6392012-05-07 14:51:40 -07001740}
1741
1742/* Handle the content in each basic block */
buzbeefa57c472012-11-21 12:06:18 -08001743static bool BlockBitcodeConversion(CompilationUnit* cu, BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07001744{
buzbeefa57c472012-11-21 12:06:18 -08001745 if (bb->block_type == kDead) return false;
1746 llvm::BasicBlock* llvm_bb = GetLLVMBlock(cu, bb->id);
1747 if (llvm_bb == NULL) {
1748 CHECK(bb->block_type == kExitBlock);
buzbeef5f5a122012-09-21 13:57:36 -07001749 } else {
buzbeefa57c472012-11-21 12:06:18 -08001750 cu->irb->SetInsertPoint(llvm_bb);
1751 SetDexOffset(cu, bb->start_offset);
buzbeef5f5a122012-09-21 13:57:36 -07001752 }
buzbee2cfc6392012-05-07 14:51:40 -07001753
buzbeefa57c472012-11-21 12:06:18 -08001754 if (cu->verbose) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001755 LOG(INFO) << "................................";
1756 LOG(INFO) << "Block id " << bb->id;
buzbeefa57c472012-11-21 12:06:18 -08001757 if (llvm_bb != NULL) {
1758 LOG(INFO) << "label " << llvm_bb->getName().str().c_str();
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001759 } else {
buzbeefa57c472012-11-21 12:06:18 -08001760 LOG(INFO) << "llvm_bb is NULL";
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001761 }
1762 }
1763
buzbeefa57c472012-11-21 12:06:18 -08001764 if (bb->block_type == kEntryBlock) {
1765 SetMethodInfo(cu);
1766 bool *can_be_ref = static_cast<bool*>(NewMem(cu, sizeof(bool) * cu->num_dalvik_registers,
buzbeecbd6d442012-11-17 14:11:25 -08001767 true, kAllocMisc));
buzbeefa57c472012-11-21 12:06:18 -08001768 for (int i = 0; i < cu->num_ssa_regs; i++) {
1769 int v_reg = SRegToVReg(cu, i);
1770 if (v_reg > SSA_METHOD_BASEREG) {
1771 can_be_ref[SRegToVReg(cu, i)] |= cu->reg_location[i].ref;
buzbee6ec5e232012-09-20 15:50:03 -07001772 }
buzbeeb03f4872012-06-11 15:22:11 -07001773 }
buzbeefa57c472012-11-21 12:06:18 -08001774 for (int i = 0; i < cu->num_dalvik_registers; i++) {
1775 if (can_be_ref[i]) {
1776 cu->num_shadow_frame_entries++;
buzbeeb03f4872012-06-11 15:22:11 -07001777 }
1778 }
buzbeefa57c472012-11-21 12:06:18 -08001779 if (cu->num_shadow_frame_entries > 0) {
1780 cu->shadow_map = static_cast<int*>(NewMem(cu, sizeof(int) * cu->num_shadow_frame_entries,
buzbeecbd6d442012-11-17 14:11:25 -08001781 true, kAllocMisc));
buzbeefa57c472012-11-21 12:06:18 -08001782 for (int i = 0, j = 0; i < cu->num_dalvik_registers; i++) {
1783 if (can_be_ref[i]) {
1784 cu->shadow_map[j++] = i;
buzbeeb03f4872012-06-11 15:22:11 -07001785 }
1786 }
buzbeeb03f4872012-06-11 15:22:11 -07001787 }
Shih-wei Liao569daf12012-08-10 23:22:33 -07001788 greenland::IntrinsicHelper::IntrinsicId id =
1789 greenland::IntrinsicHelper::AllocaShadowFrame;
buzbeefa57c472012-11-21 12:06:18 -08001790 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(id);
1791 llvm::Value* entries = cu->irb->getInt32(cu->num_shadow_frame_entries);
1792 llvm::Value* dalvik_regs = cu->irb->getInt32(cu->num_dalvik_registers);
1793 llvm::Value* args[] = { entries, dalvik_regs };
1794 cu->irb->CreateCall(func, args);
1795 } else if (bb->block_type == kExitBlock) {
buzbee2cfc6392012-05-07 14:51:40 -07001796 /*
1797 * Because of the differences between how MIR/LIR and llvm handle exit
1798 * blocks, we won't explicitly covert them. On the llvm-to-lir
1799 * path, it will need to be regenereated.
1800 */
1801 return false;
buzbeefa57c472012-11-21 12:06:18 -08001802 } else if (bb->block_type == kExceptionHandling) {
buzbee6969d502012-06-15 16:40:31 -07001803 /*
1804 * Because we're deferring null checking, delete the associated empty
1805 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001806 */
buzbeefa57c472012-11-21 12:06:18 -08001807 llvm_bb->eraseFromParent();
buzbee6969d502012-06-15 16:40:31 -07001808 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001809 }
1810
buzbee28c9a832012-11-21 15:39:13 -08001811 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbee2cfc6392012-05-07 14:51:40 -07001812
buzbeefa57c472012-11-21 12:06:18 -08001813 SetDexOffset(cu, mir->offset);
buzbee2cfc6392012-05-07 14:51:40 -07001814
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001815 int opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -08001816 Instruction::Format dalvik_format =
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001817 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001818
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001819 if (opcode == kMirOpCheck) {
1820 // Combine check and work halves of throwing instruction.
buzbeefa57c472012-11-21 12:06:18 -08001821 MIR* work_half = mir->meta.throw_insn;
1822 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001823 opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -08001824 SSARepresentation* ssa_rep = work_half->ssa_rep;
1825 work_half->ssa_rep = mir->ssa_rep;
1826 mir->ssa_rep = ssa_rep;
buzbeea169e1d2012-12-05 14:26:44 -08001827 work_half->meta.original_opcode = work_half->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -08001828 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1829 if (bb->successor_block_list.block_list_type == kCatch) {
1830 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001831 greenland::IntrinsicHelper::CatchTargets);
buzbeefa57c472012-11-21 12:06:18 -08001832 llvm::Value* switch_key =
1833 cu->irb->CreateCall(intr, cu->irb->getInt32(mir->offset));
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001834 GrowableListIterator iter;
buzbeefa57c472012-11-21 12:06:18 -08001835 GrowableListIteratorInit(&bb->successor_block_list.blocks, &iter);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001836 // New basic block to use for work half
buzbeefa57c472012-11-21 12:06:18 -08001837 llvm::BasicBlock* work_bb =
1838 llvm::BasicBlock::Create(*cu->context, "", cu->func);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001839 llvm::SwitchInst* sw =
buzbeefa57c472012-11-21 12:06:18 -08001840 cu->irb->CreateSwitch(switch_key, work_bb,
1841 bb->successor_block_list.blocks.num_used);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001842 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001843 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -08001844 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iter));
buzbeefa57c472012-11-21 12:06:18 -08001845 if (successor_block_info == NULL) break;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001846 llvm::BasicBlock *target =
buzbeefa57c472012-11-21 12:06:18 -08001847 GetLLVMBlock(cu, successor_block_info->block->id);
1848 int type_index = successor_block_info->key;
1849 sw->addCase(cu->irb->getInt32(type_index), target);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001850 }
buzbeefa57c472012-11-21 12:06:18 -08001851 llvm_bb = work_bb;
1852 cu->irb->SetInsertPoint(llvm_bb);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001853 }
1854 }
1855
1856 if (opcode >= kMirOpFirst) {
buzbeefa57c472012-11-21 12:06:18 -08001857 ConvertExtendedMIR(cu, bb, mir, llvm_bb);
buzbee2cfc6392012-05-07 14:51:40 -07001858 continue;
1859 }
1860
buzbeefa57c472012-11-21 12:06:18 -08001861 bool not_handled = ConvertMIRNode(cu, mir, bb, llvm_bb,
1862 NULL /* label_list */);
1863 if (not_handled) {
1864 Instruction::Code dalvik_opcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001865 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001866 mir->offset, opcode,
buzbeefa57c472012-11-21 12:06:18 -08001867 Instruction::Name(dalvik_opcode),
1868 dalvik_format);
buzbee2cfc6392012-05-07 14:51:40 -07001869 }
1870 }
1871
buzbeefa57c472012-11-21 12:06:18 -08001872 if (bb->block_type == kEntryBlock) {
1873 cu->entryTarget_bb = GetLLVMBlock(cu, bb->fall_through->id);
1874 } else if ((bb->fall_through != NULL) && !bb->has_return) {
1875 cu->irb->CreateBr(GetLLVMBlock(cu, bb->fall_through->id));
buzbee2cfc6392012-05-07 14:51:40 -07001876 }
1877
1878 return false;
1879}
1880
buzbeefa57c472012-11-21 12:06:18 -08001881char RemapShorty(char shorty_type) {
buzbee4f4dfc72012-07-02 14:54:44 -07001882 /*
1883 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1884 * and longs/doubles are represented as a pair of registers. When sub-word
1885 * arguments (and method results) are passed, they are extended to Dalvik
1886 * virtual register containers. Because llvm is picky about type consistency,
1887 * we must either cast the "real" type to 32-bit container multiple Dalvik
1888 * register types, or always use the expanded values.
1889 * Here, we're doing the latter. We map the shorty signature to container
1890 * types (which is valid so long as we always do a real expansion of passed
1891 * arguments and field loads).
1892 */
buzbeefa57c472012-11-21 12:06:18 -08001893 switch(shorty_type) {
1894 case 'Z' : shorty_type = 'I'; break;
1895 case 'B' : shorty_type = 'I'; break;
1896 case 'S' : shorty_type = 'I'; break;
1897 case 'C' : shorty_type = 'I'; break;
buzbee4f4dfc72012-07-02 14:54:44 -07001898 default: break;
1899 }
buzbeefa57c472012-11-21 12:06:18 -08001900 return shorty_type;
buzbee4f4dfc72012-07-02 14:54:44 -07001901}
1902
buzbeefa57c472012-11-21 12:06:18 -08001903static llvm::FunctionType* GetFunctionType(CompilationUnit* cu) {
buzbee2cfc6392012-05-07 14:51:40 -07001904
1905 // Get return type
buzbeefa57c472012-11-21 12:06:18 -08001906 llvm::Type* ret_type = cu->irb->GetJType(RemapShorty(cu->shorty[0]),
buzbee2cfc6392012-05-07 14:51:40 -07001907 greenland::kAccurate);
1908
1909 // Get argument type
1910 std::vector<llvm::Type*> args_type;
1911
1912 // method object
buzbeefa57c472012-11-21 12:06:18 -08001913 args_type.push_back(cu->irb->GetJMethodTy());
buzbee2cfc6392012-05-07 14:51:40 -07001914
1915 // Do we have a "this"?
buzbeefa57c472012-11-21 12:06:18 -08001916 if ((cu->access_flags & kAccStatic) == 0) {
1917 args_type.push_back(cu->irb->GetJObjectTy());
buzbee2cfc6392012-05-07 14:51:40 -07001918 }
1919
buzbeefa57c472012-11-21 12:06:18 -08001920 for (uint32_t i = 1; i < strlen(cu->shorty); ++i) {
1921 args_type.push_back(cu->irb->GetJType(RemapShorty(cu->shorty[i]),
buzbee2cfc6392012-05-07 14:51:40 -07001922 greenland::kAccurate));
1923 }
1924
1925 return llvm::FunctionType::get(ret_type, args_type, false);
1926}
1927
buzbeefa57c472012-11-21 12:06:18 -08001928static bool CreateFunction(CompilationUnit* cu) {
1929 std::string func_name(PrettyMethod(cu->method_idx, *cu->dex_file,
buzbee2cfc6392012-05-07 14:51:40 -07001930 /* with_signature */ false));
buzbeefa57c472012-11-21 12:06:18 -08001931 llvm::FunctionType* func_type = GetFunctionType(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001932
1933 if (func_type == NULL) {
1934 return false;
1935 }
1936
buzbeefa57c472012-11-21 12:06:18 -08001937 cu->func = llvm::Function::Create(func_type,
buzbee2cfc6392012-05-07 14:51:40 -07001938 llvm::Function::ExternalLinkage,
buzbeefa57c472012-11-21 12:06:18 -08001939 func_name, cu->module);
buzbee2cfc6392012-05-07 14:51:40 -07001940
buzbeefa57c472012-11-21 12:06:18 -08001941 llvm::Function::arg_iterator arg_iter(cu->func->arg_begin());
1942 llvm::Function::arg_iterator arg_end(cu->func->arg_end());
buzbee2cfc6392012-05-07 14:51:40 -07001943
1944 arg_iter->setName("method");
1945 ++arg_iter;
1946
buzbeefa57c472012-11-21 12:06:18 -08001947 int start_sreg = cu->num_regs;
buzbee2cfc6392012-05-07 14:51:40 -07001948
1949 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
buzbeefa57c472012-11-21 12:06:18 -08001950 arg_iter->setName(StringPrintf("v%i_0", start_sreg));
1951 start_sreg += cu->reg_location[start_sreg].wide ? 2 : 1;
buzbee2cfc6392012-05-07 14:51:40 -07001952 }
1953
1954 return true;
1955}
1956
buzbeefa57c472012-11-21 12:06:18 -08001957static bool CreateLLVMBasicBlock(CompilationUnit* cu, BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07001958{
1959 // Skip the exit block
buzbeefa57c472012-11-21 12:06:18 -08001960 if ((bb->block_type == kDead) ||(bb->block_type == kExitBlock)) {
1961 cu->id_to_block_map.Put(bb->id, NULL);
buzbee2cfc6392012-05-07 14:51:40 -07001962 } else {
buzbeefa57c472012-11-21 12:06:18 -08001963 int offset = bb->start_offset;
1964 bool entry_block = (bb->block_type == kEntryBlock);
1965 llvm::BasicBlock* llvm_bb =
1966 llvm::BasicBlock::Create(*cu->context, entry_block ? "entry" :
1967 StringPrintf(kLabelFormat, bb->catch_entry ? kCatchBlock :
1968 kNormalBlock, offset, bb->id), cu->func);
1969 if (entry_block) {
1970 cu->entry_bb = llvm_bb;
1971 cu->placeholder_bb =
1972 llvm::BasicBlock::Create(*cu->context, "placeholder",
1973 cu->func);
buzbee2cfc6392012-05-07 14:51:40 -07001974 }
buzbeefa57c472012-11-21 12:06:18 -08001975 cu->id_to_block_map.Put(bb->id, llvm_bb);
buzbee2cfc6392012-05-07 14:51:40 -07001976 }
1977 return false;
1978}
1979
1980
1981/*
1982 * Convert MIR to LLVM_IR
1983 * o For each ssa name, create LLVM named value. Type these
1984 * appropriately, and ignore high half of wide and double operands.
1985 * o For each MIR basic block, create an LLVM basic block.
1986 * o Iterate through the MIR a basic block at a time, setting arguments
1987 * to recovered ssa name.
1988 */
buzbeefa57c472012-11-21 12:06:18 -08001989void MethodMIR2Bitcode(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -07001990{
buzbeefa57c472012-11-21 12:06:18 -08001991 InitIR(cu);
1992 CompilerInitGrowableList(cu, &cu->llvm_values, cu->num_ssa_regs);
buzbee2cfc6392012-05-07 14:51:40 -07001993
1994 // Create the function
buzbeefa57c472012-11-21 12:06:18 -08001995 CreateFunction(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001996
1997 // Create an LLVM basic block for each MIR block in dfs preorder
buzbeefa57c472012-11-21 12:06:18 -08001998 DataFlowAnalysisDispatcher(cu, CreateLLVMBasicBlock,
1999 kPreOrderDFSTraversal, false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -07002000 /*
2001 * Create an llvm named value for each MIR SSA name. Note: we'll use
2002 * placeholders for all non-argument values (because we haven't seen
2003 * the definition yet).
2004 */
buzbeefa57c472012-11-21 12:06:18 -08002005 cu->irb->SetInsertPoint(cu->placeholder_bb);
2006 llvm::Function::arg_iterator arg_iter(cu->func->arg_begin());
buzbee2cfc6392012-05-07 14:51:40 -07002007 arg_iter++; /* Skip path method */
buzbeefa57c472012-11-21 12:06:18 -08002008 for (int i = 0; i < cu->num_ssa_regs; i++) {
buzbee2cfc6392012-05-07 14:51:40 -07002009 llvm::Value* val;
buzbeefa57c472012-11-21 12:06:18 -08002010 RegLocation rl_temp = cu->reg_location[i];
2011 if ((SRegToVReg(cu, i) < 0) || rl_temp.high_word) {
2012 InsertGrowableList(cu, &cu->llvm_values, 0);
2013 } else if ((i < cu->num_regs) ||
2014 (i >= (cu->num_regs + cu->num_ins))) {
2015 llvm::Constant* imm_value = cu->reg_location[i].wide ?
2016 cu->irb->GetJLong(0) : cu->irb->GetJInt(0);
2017 val = EmitConst(cu, imm_value, cu->reg_location[i]);
2018 val->setName(LlvmSSAName(cu, i));
2019 InsertGrowableList(cu, &cu->llvm_values, reinterpret_cast<uintptr_t>(val));
buzbee2cfc6392012-05-07 14:51:40 -07002020 } else {
2021 // Recover previously-created argument values
buzbeefa57c472012-11-21 12:06:18 -08002022 llvm::Value* arg_val = arg_iter++;
2023 InsertGrowableList(cu, &cu->llvm_values, reinterpret_cast<uintptr_t>(arg_val));
buzbee2cfc6392012-05-07 14:51:40 -07002024 }
2025 }
buzbee2cfc6392012-05-07 14:51:40 -07002026
buzbeefa57c472012-11-21 12:06:18 -08002027 DataFlowAnalysisDispatcher(cu, BlockBitcodeConversion,
buzbee2cfc6392012-05-07 14:51:40 -07002028 kPreOrderDFSTraversal, false /* Iterative */);
2029
buzbee4be777b2012-07-12 14:38:18 -07002030 /*
2031 * In a few rare cases of verification failure, the verifier will
2032 * replace one or more Dalvik opcodes with the special
2033 * throw-verification-failure opcode. This can leave the SSA graph
2034 * in an invalid state, as definitions may be lost, while uses retained.
2035 * To work around this problem, we insert placeholder definitions for
2036 * all Dalvik SSA regs in the "placeholder" block. Here, after
2037 * bitcode conversion is complete, we examine those placeholder definitions
2038 * and delete any with no references (which normally is all of them).
2039 *
2040 * If any definitions remain, we link the placeholder block into the
2041 * CFG. Otherwise, it is deleted.
2042 */
buzbeefa57c472012-11-21 12:06:18 -08002043 for (llvm::BasicBlock::iterator it = cu->placeholder_bb->begin(),
2044 it_end = cu->placeholder_bb->end(); it != it_end;) {
buzbee4be777b2012-07-12 14:38:18 -07002045 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2046 DCHECK(inst != NULL);
2047 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2048 DCHECK(val != NULL);
2049 if (val->getNumUses() == 0) {
2050 inst->eraseFromParent();
2051 }
2052 }
buzbeefa57c472012-11-21 12:06:18 -08002053 SetDexOffset(cu, 0);
2054 if (cu->placeholder_bb->empty()) {
2055 cu->placeholder_bb->eraseFromParent();
buzbee4be777b2012-07-12 14:38:18 -07002056 } else {
buzbeefa57c472012-11-21 12:06:18 -08002057 cu->irb->SetInsertPoint(cu->placeholder_bb);
2058 cu->irb->CreateBr(cu->entryTarget_bb);
2059 cu->entryTarget_bb = cu->placeholder_bb;
buzbee4be777b2012-07-12 14:38:18 -07002060 }
buzbeefa57c472012-11-21 12:06:18 -08002061 cu->irb->SetInsertPoint(cu->entry_bb);
2062 cu->irb->CreateBr(cu->entryTarget_bb);
buzbee2cfc6392012-05-07 14:51:40 -07002063
buzbeefa57c472012-11-21 12:06:18 -08002064 if (cu->enable_debug & (1 << kDebugVerifyBitcode)) {
2065 if (llvm::verifyFunction(*cu->func, llvm::PrintMessageAction)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002066 LOG(INFO) << "Bitcode verification FAILED for "
buzbeefa57c472012-11-21 12:06:18 -08002067 << PrettyMethod(cu->method_idx, *cu->dex_file)
2068 << " of size " << cu->insns_size;
2069 cu->enable_debug |= (1 << kDebugDumpBitcodeFile);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002070 }
2071 }
buzbee2cfc6392012-05-07 14:51:40 -07002072
buzbeefa57c472012-11-21 12:06:18 -08002073 if (cu->enable_debug & (1 << kDebugDumpBitcodeFile)) {
buzbeead8f15e2012-06-18 14:49:45 -07002074 // Write bitcode to file
2075 std::string errmsg;
buzbeefa57c472012-11-21 12:06:18 -08002076 std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -08002077 ReplaceSpecialChars(fname);
buzbee6459e7c2012-10-02 14:42:41 -07002078 // TODO: make configurable change naming mechanism to avoid fname length issues.
buzbee4f1181f2012-06-22 13:52:12 -07002079 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002080
buzbee6459e7c2012-10-02 14:42:41 -07002081 if (fname.size() > 240) {
2082 LOG(INFO) << "Warning: bitcode filename too long. Truncated.";
2083 fname.resize(240);
2084 }
2085
buzbeead8f15e2012-06-18 14:49:45 -07002086 llvm::OwningPtr<llvm::tool_output_file> out_file(
2087 new llvm::tool_output_file(fname.c_str(), errmsg,
2088 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002089
buzbeead8f15e2012-06-18 14:49:45 -07002090 if (!errmsg.empty()) {
2091 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2092 }
2093
buzbeefa57c472012-11-21 12:06:18 -08002094 llvm::WriteBitcodeToFile(cu->module, out_file->os());
buzbeead8f15e2012-06-18 14:49:45 -07002095 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002096 }
buzbee2cfc6392012-05-07 14:51:40 -07002097}
2098
buzbeefa57c472012-11-21 12:06:18 -08002099static RegLocation GetLoc(CompilationUnit* cu, llvm::Value* val) {
buzbee2cfc6392012-05-07 14:51:40 -07002100 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002101 DCHECK(val != NULL);
buzbeefa57c472012-11-21 12:06:18 -08002102 SafeMap<llvm::Value*, RegLocation>::iterator it = cu->loc_map.find(val);
2103 if (it == cu->loc_map.end()) {
2104 std::string val_name = val->getName().str();
2105 if (val_name.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002106 // FIXME: need to be more robust, handle FP and be in a position to
2107 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002108 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2109 memset(&res, 0, sizeof(res));
2110 res.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08002111 res.low_reg = AllocTemp(cu);
buzbee4f1181f2012-06-22 13:52:12 -07002112 res.home = true;
buzbeefa57c472012-11-21 12:06:18 -08002113 res.s_reg_low = INVALID_SREG;
2114 res.orig_sreg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002115 llvm::Type* ty = val->getType();
buzbeefa57c472012-11-21 12:06:18 -08002116 res.wide = ((ty == cu->irb->getInt64Ty()) ||
2117 (ty == cu->irb->getDoubleTy()));
buzbee101305f2012-06-28 18:00:56 -07002118 if (res.wide) {
buzbeefa57c472012-11-21 12:06:18 -08002119 res.high_reg = AllocTemp(cu);
buzbee101305f2012-06-28 18:00:56 -07002120 }
buzbeefa57c472012-11-21 12:06:18 -08002121 cu->loc_map.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002122 } else {
buzbeefa57c472012-11-21 12:06:18 -08002123 DCHECK_EQ(val_name[0], 'v');
2124 int base_sreg = INVALID_SREG;
2125 sscanf(val_name.c_str(), "v%d_", &base_sreg);
2126 res = cu->reg_location[base_sreg];
2127 cu->loc_map.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002128 }
2129 } else {
2130 res = it->second;
2131 }
2132 return res;
2133}
2134
buzbeefa57c472012-11-21 12:06:18 -08002135static Instruction::Code GetDalvikOpcode(OpKind op, bool is_const, bool is_wide)
buzbee2cfc6392012-05-07 14:51:40 -07002136{
2137 Instruction::Code res = Instruction::NOP;
buzbeefa57c472012-11-21 12:06:18 -08002138 if (is_wide) {
buzbee2cfc6392012-05-07 14:51:40 -07002139 switch(op) {
2140 case kOpAdd: res = Instruction::ADD_LONG; break;
2141 case kOpSub: res = Instruction::SUB_LONG; break;
2142 case kOpMul: res = Instruction::MUL_LONG; break;
2143 case kOpDiv: res = Instruction::DIV_LONG; break;
2144 case kOpRem: res = Instruction::REM_LONG; break;
2145 case kOpAnd: res = Instruction::AND_LONG; break;
2146 case kOpOr: res = Instruction::OR_LONG; break;
2147 case kOpXor: res = Instruction::XOR_LONG; break;
2148 case kOpLsl: res = Instruction::SHL_LONG; break;
2149 case kOpLsr: res = Instruction::USHR_LONG; break;
2150 case kOpAsr: res = Instruction::SHR_LONG; break;
2151 default: LOG(FATAL) << "Unexpected OpKind " << op;
2152 }
buzbeefa57c472012-11-21 12:06:18 -08002153 } else if (is_const){
buzbee2cfc6392012-05-07 14:51:40 -07002154 switch(op) {
2155 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2156 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2157 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2158 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2159 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2160 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2161 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2162 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2163 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2164 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2165 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2166 default: LOG(FATAL) << "Unexpected OpKind " << op;
2167 }
2168 } else {
2169 switch(op) {
2170 case kOpAdd: res = Instruction::ADD_INT; break;
2171 case kOpSub: res = Instruction::SUB_INT; break;
2172 case kOpMul: res = Instruction::MUL_INT; break;
2173 case kOpDiv: res = Instruction::DIV_INT; break;
2174 case kOpRem: res = Instruction::REM_INT; break;
2175 case kOpAnd: res = Instruction::AND_INT; break;
2176 case kOpOr: res = Instruction::OR_INT; break;
2177 case kOpXor: res = Instruction::XOR_INT; break;
2178 case kOpLsl: res = Instruction::SHL_INT; break;
2179 case kOpLsr: res = Instruction::USHR_INT; break;
2180 case kOpAsr: res = Instruction::SHR_INT; break;
2181 default: LOG(FATAL) << "Unexpected OpKind " << op;
2182 }
2183 }
2184 return res;
2185}
2186
buzbeefa57c472012-11-21 12:06:18 -08002187static Instruction::Code GetDalvikFPOpcode(OpKind op, bool is_const, bool is_wide)
buzbee4f1181f2012-06-22 13:52:12 -07002188{
2189 Instruction::Code res = Instruction::NOP;
buzbeefa57c472012-11-21 12:06:18 -08002190 if (is_wide) {
buzbee4f1181f2012-06-22 13:52:12 -07002191 switch(op) {
2192 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2193 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2194 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2195 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2196 case kOpRem: res = Instruction::REM_DOUBLE; break;
2197 default: LOG(FATAL) << "Unexpected OpKind " << op;
2198 }
2199 } else {
2200 switch(op) {
2201 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2202 case kOpSub: res = Instruction::SUB_FLOAT; break;
2203 case kOpMul: res = Instruction::MUL_FLOAT; break;
2204 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2205 case kOpRem: res = Instruction::REM_FLOAT; break;
2206 default: LOG(FATAL) << "Unexpected OpKind " << op;
2207 }
2208 }
2209 return res;
2210}
2211
buzbeefa57c472012-11-21 12:06:18 -08002212static void CvtBinFPOp(CompilationUnit* cu, OpKind op, llvm::Instruction* inst)
buzbee4f1181f2012-06-22 13:52:12 -07002213{
buzbee02031b12012-11-23 09:41:35 -08002214 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002215 RegLocation rl_dest = GetLoc(cu, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002216 /*
2217 * Normally, we won't ever generate an FP operation with an immediate
2218 * operand (not supported in Dex instruction set). However, the IR builder
buzbeefa57c472012-11-21 12:06:18 -08002219 * may insert them - in particular for create_neg_fp. Recognize this case
buzbee4f4dfc72012-07-02 14:54:44 -07002220 * and deal with it.
2221 */
2222 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2223 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2224 DCHECK(op2C == NULL);
2225 if ((op1C != NULL) && (op == kOpSub)) {
buzbeefa57c472012-11-21 12:06:18 -08002226 RegLocation rl_src = GetLoc(cu, inst->getOperand(1));
2227 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002228 cg->GenArithOpDouble(cu, Instruction::NEG_DOUBLE, rl_dest, rl_src, rl_src);
buzbee4f4dfc72012-07-02 14:54:44 -07002229 } else {
buzbee02031b12012-11-23 09:41:35 -08002230 cg->GenArithOpFloat(cu, Instruction::NEG_FLOAT, rl_dest, rl_src, rl_src);
buzbee4f4dfc72012-07-02 14:54:44 -07002231 }
buzbee4f1181f2012-06-22 13:52:12 -07002232 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002233 DCHECK(op1C == NULL);
buzbeefa57c472012-11-21 12:06:18 -08002234 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(0));
2235 RegLocation rl_src2 = GetLoc(cu, inst->getOperand(1));
2236 Instruction::Code dalvik_op = GetDalvikFPOpcode(op, false, rl_dest.wide);
2237 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002238 cg->GenArithOpDouble(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee4f4dfc72012-07-02 14:54:44 -07002239 } else {
buzbee02031b12012-11-23 09:41:35 -08002240 cg->GenArithOpFloat(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee4f4dfc72012-07-02 14:54:44 -07002241 }
buzbee4f1181f2012-06-22 13:52:12 -07002242 }
2243}
2244
buzbeefa57c472012-11-21 12:06:18 -08002245static void CvtIntNarrowing(CompilationUnit* cu, llvm::Instruction* inst,
buzbee101305f2012-06-28 18:00:56 -07002246 Instruction::Code opcode)
2247{
buzbee02031b12012-11-23 09:41:35 -08002248 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002249 RegLocation rl_dest = GetLoc(cu, inst);
2250 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002251 cg->GenIntNarrowing(cu, opcode, rl_dest, rl_src);
buzbee101305f2012-06-28 18:00:56 -07002252}
2253
buzbeefa57c472012-11-21 12:06:18 -08002254static void CvtIntToFP(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002255{
buzbee02031b12012-11-23 09:41:35 -08002256 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002257 RegLocation rl_dest = GetLoc(cu, inst);
2258 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002259 Instruction::Code opcode;
buzbeefa57c472012-11-21 12:06:18 -08002260 if (rl_dest.wide) {
2261 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002262 opcode = Instruction::LONG_TO_DOUBLE;
2263 } else {
2264 opcode = Instruction::INT_TO_DOUBLE;
2265 }
2266 } else {
buzbeefa57c472012-11-21 12:06:18 -08002267 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002268 opcode = Instruction::LONG_TO_FLOAT;
2269 } else {
2270 opcode = Instruction::INT_TO_FLOAT;
2271 }
2272 }
buzbee02031b12012-11-23 09:41:35 -08002273 cg->GenConversion(cu, opcode, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002274}
2275
buzbeefa57c472012-11-21 12:06:18 -08002276static void CvtFPToInt(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002277{
buzbee02031b12012-11-23 09:41:35 -08002278 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002279 RegLocation rl_dest = GetLoc(cu, call_inst);
2280 RegLocation rl_src = GetLoc(cu, call_inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002281 Instruction::Code opcode;
buzbeefa57c472012-11-21 12:06:18 -08002282 if (rl_dest.wide) {
2283 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002284 opcode = Instruction::DOUBLE_TO_LONG;
2285 } else {
2286 opcode = Instruction::FLOAT_TO_LONG;
2287 }
2288 } else {
buzbeefa57c472012-11-21 12:06:18 -08002289 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002290 opcode = Instruction::DOUBLE_TO_INT;
2291 } else {
2292 opcode = Instruction::FLOAT_TO_INT;
2293 }
2294 }
buzbee02031b12012-11-23 09:41:35 -08002295 cg->GenConversion(cu, opcode, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002296}
2297
buzbeefa57c472012-11-21 12:06:18 -08002298static void CvtFloatToDouble(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002299{
buzbee02031b12012-11-23 09:41:35 -08002300 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002301 RegLocation rl_dest = GetLoc(cu, inst);
2302 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002303 cg->GenConversion(cu, Instruction::FLOAT_TO_DOUBLE, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002304}
2305
buzbeefa57c472012-11-21 12:06:18 -08002306static void CvtTrunc(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002307{
buzbee02031b12012-11-23 09:41:35 -08002308 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002309 RegLocation rl_dest = GetLoc(cu, inst);
2310 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
2311 rl_src = UpdateLocWide(cu, rl_src);
2312 rl_src = WideToNarrow(cu, rl_src);
buzbee02031b12012-11-23 09:41:35 -08002313 cg->StoreValue(cu, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002314}
2315
buzbeefa57c472012-11-21 12:06:18 -08002316static void CvtDoubleToFloat(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002317{
buzbee02031b12012-11-23 09:41:35 -08002318 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002319 RegLocation rl_dest = GetLoc(cu, inst);
2320 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002321 cg->GenConversion(cu, Instruction::DOUBLE_TO_FLOAT, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002322}
2323
2324
buzbeefa57c472012-11-21 12:06:18 -08002325static void CvtIntExt(CompilationUnit* cu, llvm::Instruction* inst, bool is_signed)
buzbee101305f2012-06-28 18:00:56 -07002326{
buzbee02031b12012-11-23 09:41:35 -08002327 Codegen* cg = cu->cg.get();
buzbee101305f2012-06-28 18:00:56 -07002328 // TODO: evaluate src/tgt types and add general support for more than int to long
buzbeefa57c472012-11-21 12:06:18 -08002329 RegLocation rl_dest = GetLoc(cu, inst);
2330 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
2331 DCHECK(rl_dest.wide);
2332 DCHECK(!rl_src.wide);
2333 DCHECK(!rl_dest.fp);
2334 DCHECK(!rl_src.fp);
2335 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
2336 if (rl_src.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -08002337 cg->OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
buzbee101305f2012-06-28 18:00:56 -07002338 } else {
buzbee02031b12012-11-23 09:41:35 -08002339 cg->LoadValueDirect(cu, rl_src, rl_result.low_reg);
buzbee101305f2012-06-28 18:00:56 -07002340 }
buzbeefa57c472012-11-21 12:06:18 -08002341 if (is_signed) {
buzbee02031b12012-11-23 09:41:35 -08002342 cg->OpRegRegImm(cu, kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
buzbee101305f2012-06-28 18:00:56 -07002343 } else {
buzbee02031b12012-11-23 09:41:35 -08002344 cg->LoadConstant(cu, rl_result.high_reg, 0);
buzbee101305f2012-06-28 18:00:56 -07002345 }
buzbee02031b12012-11-23 09:41:35 -08002346 cg->StoreValueWide(cu, rl_dest, rl_result);
buzbee101305f2012-06-28 18:00:56 -07002347}
2348
buzbeefa57c472012-11-21 12:06:18 -08002349static void CvtBinOp(CompilationUnit* cu, OpKind op, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002350{
buzbee02031b12012-11-23 09:41:35 -08002351 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002352 RegLocation rl_dest = GetLoc(cu, inst);
buzbee2cfc6392012-05-07 14:51:40 -07002353 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002354 // Special-case RSUB/NEG
buzbeefa57c472012-11-21 12:06:18 -08002355 llvm::ConstantInt* lhs_imm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2356 if ((op == kOpSub) && (lhs_imm != NULL)) {
2357 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(1));
2358 if (rl_src1.wide) {
2359 DCHECK_EQ(lhs_imm->getSExtValue(), 0);
buzbee02031b12012-11-23 09:41:35 -08002360 cg->GenArithOpLong(cu, Instruction::NEG_LONG, rl_dest, rl_src1, rl_src1);
buzbeef58c12c2012-07-03 15:06:29 -07002361 } else {
buzbee02031b12012-11-23 09:41:35 -08002362 cg->GenArithOpIntLit(cu, Instruction::RSUB_INT, rl_dest, rl_src1,
buzbeefa57c472012-11-21 12:06:18 -08002363 lhs_imm->getSExtValue());
buzbeef58c12c2012-07-03 15:06:29 -07002364 }
buzbee4f1181f2012-06-22 13:52:12 -07002365 return;
2366 }
buzbeefa57c472012-11-21 12:06:18 -08002367 DCHECK(lhs_imm == NULL);
2368 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(0));
buzbee2cfc6392012-05-07 14:51:40 -07002369 llvm::Value* rhs = inst->getOperand(1);
buzbeefa57c472012-11-21 12:06:18 -08002370 llvm::ConstantInt* const_rhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2371 if (!rl_dest.wide && (const_rhs != NULL)) {
2372 Instruction::Code dalvik_op = GetDalvikOpcode(op, true, false);
buzbee02031b12012-11-23 09:41:35 -08002373 cg->GenArithOpIntLit(cu, dalvik_op, rl_dest, rl_src1, const_rhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002374 } else {
buzbeefa57c472012-11-21 12:06:18 -08002375 Instruction::Code dalvik_op = GetDalvikOpcode(op, false, rl_dest.wide);
2376 RegLocation rl_src2;
2377 if (const_rhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002378 // ir_builder converts NOT_LONG to xor src, -1. Restore
buzbeefa57c472012-11-21 12:06:18 -08002379 DCHECK_EQ(dalvik_op, Instruction::XOR_LONG);
2380 DCHECK_EQ(-1L, const_rhs->getSExtValue());
2381 dalvik_op = Instruction::NOT_LONG;
2382 rl_src2 = rl_src1;
buzbee9a2487f2012-07-26 14:01:13 -07002383 } else {
buzbeefa57c472012-11-21 12:06:18 -08002384 rl_src2 = GetLoc(cu, rhs);
buzbee9a2487f2012-07-26 14:01:13 -07002385 }
buzbeefa57c472012-11-21 12:06:18 -08002386 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002387 cg->GenArithOpLong(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee2cfc6392012-05-07 14:51:40 -07002388 } else {
buzbee02031b12012-11-23 09:41:35 -08002389 cg->GenArithOpInt(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee2cfc6392012-05-07 14:51:40 -07002390 }
2391 }
2392}
2393
buzbeefa57c472012-11-21 12:06:18 -08002394static void CvtShiftOp(CompilationUnit* cu, Instruction::Code opcode, llvm::CallInst* call_inst)
buzbee101305f2012-06-28 18:00:56 -07002395{
buzbee02031b12012-11-23 09:41:35 -08002396 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002397 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2398 RegLocation rl_dest = GetLoc(cu, call_inst);
2399 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(0));
2400 llvm::Value* rhs = call_inst->getArgOperand(1);
buzbee2a83e8f2012-07-13 16:42:30 -07002401 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
buzbeefa57c472012-11-21 12:06:18 -08002402 DCHECK(!rl_dest.wide);
buzbee02031b12012-11-23 09:41:35 -08002403 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002404 } else {
buzbeefa57c472012-11-21 12:06:18 -08002405 RegLocation rl_shift = GetLoc(cu, rhs);
2406 if (call_inst->getType() == cu->irb->getInt64Ty()) {
buzbee02031b12012-11-23 09:41:35 -08002407 cg->GenShiftOpLong(cu, opcode, rl_dest, rl_src, rl_shift);
buzbee2a83e8f2012-07-13 16:42:30 -07002408 } else {
buzbee02031b12012-11-23 09:41:35 -08002409 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src, rl_shift);
buzbee2a83e8f2012-07-13 16:42:30 -07002410 }
buzbee101305f2012-06-28 18:00:56 -07002411 }
2412}
2413
buzbeefa57c472012-11-21 12:06:18 -08002414static void CvtBr(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002415{
buzbee02031b12012-11-23 09:41:35 -08002416 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002417 llvm::BranchInst* br_inst = llvm::dyn_cast<llvm::BranchInst>(inst);
2418 DCHECK(br_inst != NULL);
2419 DCHECK(br_inst->isUnconditional()); // May change - but this is all we use now
2420 llvm::BasicBlock* target_bb = br_inst->getSuccessor(0);
buzbee02031b12012-11-23 09:41:35 -08002421 cg->OpUnconditionalBranch(cu, cu->block_to_label_map.Get(target_bb));
buzbee2cfc6392012-05-07 14:51:40 -07002422}
2423
buzbeefa57c472012-11-21 12:06:18 -08002424static void CvtPhi(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002425{
2426 // Nop - these have already been processed
2427}
2428
buzbeefa57c472012-11-21 12:06:18 -08002429static void CvtRet(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002430{
buzbee02031b12012-11-23 09:41:35 -08002431 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002432 llvm::ReturnInst* ret_inst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2433 llvm::Value* ret_val = ret_inst->getReturnValue();
2434 if (ret_val != NULL) {
2435 RegLocation rl_src = GetLoc(cu, ret_val);
2436 if (rl_src.wide) {
buzbee02031b12012-11-23 09:41:35 -08002437 cg->StoreValueWide(cu, GetReturnWide(cu, rl_src.fp), rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002438 } else {
buzbee02031b12012-11-23 09:41:35 -08002439 cg->StoreValue(cu, GetReturn(cu, rl_src.fp), rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002440 }
2441 }
buzbee02031b12012-11-23 09:41:35 -08002442 cg->GenExitSequence(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002443}
2444
buzbeefa57c472012-11-21 12:06:18 -08002445static ConditionCode GetCond(llvm::ICmpInst::Predicate llvm_cond)
buzbee2cfc6392012-05-07 14:51:40 -07002446{
2447 ConditionCode res = kCondAl;
buzbeefa57c472012-11-21 12:06:18 -08002448 switch(llvm_cond) {
buzbee6969d502012-06-15 16:40:31 -07002449 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002450 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2451 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2452 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002453 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002454 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002455 default: LOG(FATAL) << "Unexpected llvm condition";
2456 }
2457 return res;
2458}
2459
buzbeefa57c472012-11-21 12:06:18 -08002460static void CvtICmp(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002461{
buzbee02031b12012-11-23 09:41:35 -08002462 // cg->GenCmpLong(cu, rl_dest, rl_src1, rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -07002463 UNIMPLEMENTED(FATAL);
2464}
2465
buzbeefa57c472012-11-21 12:06:18 -08002466static void CvtICmpBr(CompilationUnit* cu, llvm::Instruction* inst,
2467 llvm::BranchInst* br_inst)
buzbee2cfc6392012-05-07 14:51:40 -07002468{
buzbee02031b12012-11-23 09:41:35 -08002469 Codegen* cg = cu->cg.get();
buzbee2cfc6392012-05-07 14:51:40 -07002470 // Get targets
buzbeefa57c472012-11-21 12:06:18 -08002471 llvm::BasicBlock* taken_bb = br_inst->getSuccessor(0);
2472 LIR* taken = cu->block_to_label_map.Get(taken_bb);
2473 llvm::BasicBlock* fallthrough_bb = br_inst->getSuccessor(1);
2474 LIR* fall_through = cu->block_to_label_map.Get(fallthrough_bb);
buzbee2cfc6392012-05-07 14:51:40 -07002475 // Get comparison operands
buzbeefa57c472012-11-21 12:06:18 -08002476 llvm::ICmpInst* i_cmp_inst = llvm::dyn_cast<llvm::ICmpInst>(inst);
2477 ConditionCode cond = GetCond(i_cmp_inst->getPredicate());
2478 llvm::Value* lhs = i_cmp_inst->getOperand(0);
buzbee2cfc6392012-05-07 14:51:40 -07002479 // Not expecting a constant as 1st operand
2480 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
buzbeefa57c472012-11-21 12:06:18 -08002481 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002482 rl_src1 = cg->LoadValue(cu, rl_src1, kCoreReg);
buzbee2cfc6392012-05-07 14:51:40 -07002483 llvm::Value* rhs = inst->getOperand(1);
buzbeefa57c472012-11-21 12:06:18 -08002484 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -07002485 // Compare and branch in one shot
2486 UNIMPLEMENTED(FATAL);
2487 }
buzbee2cfc6392012-05-07 14:51:40 -07002488 //Compare, then branch
2489 // TODO: handle fused CMP_LONG/IF_xxZ case
2490 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
buzbee02031b12012-11-23 09:41:35 -08002491 cg->OpRegImm(cu, kOpCmp, rl_src1.low_reg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002492 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
buzbee02031b12012-11-23 09:41:35 -08002493 cg->OpRegImm(cu, kOpCmp, rl_src1.low_reg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002494 } else {
buzbeefa57c472012-11-21 12:06:18 -08002495 RegLocation rl_src2 = GetLoc(cu, rhs);
buzbee02031b12012-11-23 09:41:35 -08002496 rl_src2 = cg->LoadValue(cu, rl_src2, kCoreReg);
2497 cg->OpRegReg(cu, kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
buzbee2cfc6392012-05-07 14:51:40 -07002498 }
buzbee02031b12012-11-23 09:41:35 -08002499 cg->OpCondBranch(cu, cond, taken);
buzbee2cfc6392012-05-07 14:51:40 -07002500 // Fallthrough
buzbee02031b12012-11-23 09:41:35 -08002501 cg->OpUnconditionalBranch(cu, fall_through);
buzbee2cfc6392012-05-07 14:51:40 -07002502}
2503
buzbeefa57c472012-11-21 12:06:18 -08002504static void CvtCopy(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee2cfc6392012-05-07 14:51:40 -07002505{
buzbee02031b12012-11-23 09:41:35 -08002506 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002507 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2508 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(0));
2509 RegLocation rl_dest = GetLoc(cu, call_inst);
2510 DCHECK_EQ(rl_src.wide, rl_dest.wide);
2511 DCHECK_EQ(rl_src.fp, rl_dest.fp);
2512 if (rl_src.wide) {
buzbee02031b12012-11-23 09:41:35 -08002513 cg->StoreValueWide(cu, rl_dest, rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002514 } else {
buzbee02031b12012-11-23 09:41:35 -08002515 cg->StoreValue(cu, rl_dest, rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002516 }
2517}
2518
2519// Note: Immediate arg is a ConstantInt regardless of result type
buzbeefa57c472012-11-21 12:06:18 -08002520static void CvtConst(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee2cfc6392012-05-07 14:51:40 -07002521{
buzbee02031b12012-11-23 09:41:35 -08002522 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002523 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002524 llvm::ConstantInt* src =
buzbeefa57c472012-11-21 12:06:18 -08002525 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
buzbee2cfc6392012-05-07 14:51:40 -07002526 uint64_t immval = src->getZExtValue();
buzbeefa57c472012-11-21 12:06:18 -08002527 RegLocation rl_dest = GetLoc(cu, call_inst);
2528 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
2529 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002530 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
buzbee2cfc6392012-05-07 14:51:40 -07002531 (immval) & 0xffffffff, (immval >> 32) & 0xffffffff);
buzbee02031b12012-11-23 09:41:35 -08002532 cg->StoreValueWide(cu, rl_dest, rl_result);
buzbee2cfc6392012-05-07 14:51:40 -07002533 } else {
buzbee7da142f2012-11-29 16:33:42 -08002534 int immediate = immval & 0xffffffff;
2535 cg->LoadConstantNoClobber(cu, rl_result.low_reg, immediate);
buzbee02031b12012-11-23 09:41:35 -08002536 cg->StoreValue(cu, rl_dest, rl_result);
buzbee7da142f2012-11-29 16:33:42 -08002537 if (immediate == 0) {
2538 cg->Workaround7250540(cu, rl_dest, rl_result.low_reg);
2539 }
buzbee2cfc6392012-05-07 14:51:40 -07002540 }
2541}
2542
buzbeefa57c472012-11-21 12:06:18 -08002543static void CvtConstObject(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_string)
buzbee6969d502012-06-15 16:40:31 -07002544{
buzbee02031b12012-11-23 09:41:35 -08002545 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002546 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2547 llvm::ConstantInt* idx_val =
2548 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2549 uint32_t index = idx_val->getZExtValue();
2550 RegLocation rl_dest = GetLoc(cu, call_inst);
2551 if (is_string) {
buzbee02031b12012-11-23 09:41:35 -08002552 cg->GenConstString(cu, index, rl_dest);
buzbee101305f2012-06-28 18:00:56 -07002553 } else {
buzbee02031b12012-11-23 09:41:35 -08002554 cg->GenConstClass(cu, index, rl_dest);
buzbee101305f2012-06-28 18:00:56 -07002555 }
2556}
2557
buzbeefa57c472012-11-21 12:06:18 -08002558static void CvtFillArrayData(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee101305f2012-06-28 18:00:56 -07002559{
buzbee02031b12012-11-23 09:41:35 -08002560 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002561 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2562 llvm::ConstantInt* offset_val =
2563 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2564 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
buzbee02031b12012-11-23 09:41:35 -08002565 cg->GenFillArrayData(cu, offset_val->getSExtValue(), rl_src);
buzbee6969d502012-06-15 16:40:31 -07002566}
2567
buzbeefa57c472012-11-21 12:06:18 -08002568static void CvtNewInstance(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee4f1181f2012-06-22 13:52:12 -07002569{
buzbee02031b12012-11-23 09:41:35 -08002570 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002571 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2572 llvm::ConstantInt* type_idx_val =
2573 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2574 uint32_t type_idx = type_idx_val->getZExtValue();
2575 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002576 cg->GenNewInstance(cu, type_idx, rl_dest);
buzbee4f1181f2012-06-22 13:52:12 -07002577}
2578
buzbeefa57c472012-11-21 12:06:18 -08002579static void CvtNewArray(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002580{
buzbee02031b12012-11-23 09:41:35 -08002581 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002582 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2583 llvm::ConstantInt* type_idx_val =
2584 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2585 uint32_t type_idx = type_idx_val->getZExtValue();
2586 llvm::Value* len = call_inst->getArgOperand(1);
2587 RegLocation rl_len = GetLoc(cu, len);
2588 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002589 cg->GenNewArray(cu, type_idx, rl_dest, rl_len);
buzbee8fa0fda2012-06-27 15:44:52 -07002590}
2591
buzbeefa57c472012-11-21 12:06:18 -08002592static void CvtInstanceOf(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002593{
buzbee02031b12012-11-23 09:41:35 -08002594 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002595 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2596 llvm::ConstantInt* type_idx_val =
2597 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2598 uint32_t type_idx = type_idx_val->getZExtValue();
2599 llvm::Value* src = call_inst->getArgOperand(1);
2600 RegLocation rl_src = GetLoc(cu, src);
2601 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002602 cg->GenInstanceof(cu, type_idx, rl_dest, rl_src);
buzbee8fa0fda2012-06-27 15:44:52 -07002603}
2604
buzbeefa57c472012-11-21 12:06:18 -08002605static void CvtThrow(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee32412962012-06-26 16:27:56 -07002606{
buzbee02031b12012-11-23 09:41:35 -08002607 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002608 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2609 llvm::Value* src = call_inst->getArgOperand(0);
2610 RegLocation rl_src = GetLoc(cu, src);
buzbee02031b12012-11-23 09:41:35 -08002611 cg->GenThrow(cu, rl_src);
buzbee32412962012-06-26 16:27:56 -07002612}
2613
buzbeefa57c472012-11-21 12:06:18 -08002614static void CvtMonitorEnterExit(CompilationUnit* cu, bool is_enter,
2615 llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002616{
buzbee02031b12012-11-23 09:41:35 -08002617 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002618 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2619 llvm::ConstantInt* opt_flags =
2620 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2621 llvm::Value* src = call_inst->getArgOperand(1);
2622 RegLocation rl_src = GetLoc(cu, src);
2623 if (is_enter) {
buzbee02031b12012-11-23 09:41:35 -08002624 cg->GenMonitorEnter(cu, opt_flags->getZExtValue(), rl_src);
buzbee8fa0fda2012-06-27 15:44:52 -07002625 } else {
buzbee02031b12012-11-23 09:41:35 -08002626 cg->GenMonitorExit(cu, opt_flags->getZExtValue(), rl_src);
buzbee8fa0fda2012-06-27 15:44:52 -07002627 }
2628}
2629
buzbeefa57c472012-11-21 12:06:18 -08002630static void CvtArrayLength(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002631{
buzbee02031b12012-11-23 09:41:35 -08002632 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002633 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2634 llvm::ConstantInt* opt_flags =
2635 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2636 llvm::Value* src = call_inst->getArgOperand(1);
2637 RegLocation rl_src = GetLoc(cu, src);
buzbee02031b12012-11-23 09:41:35 -08002638 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
2639 cg->GenNullCheck(cu, rl_src.s_reg_low, rl_src.low_reg, opt_flags->getZExtValue());
buzbeefa57c472012-11-21 12:06:18 -08002640 RegLocation rl_dest = GetLoc(cu, call_inst);
2641 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
2642 int len_offset = Array::LengthOffset().Int32Value();
buzbee02031b12012-11-23 09:41:35 -08002643 cg->LoadWordDisp(cu, rl_src.low_reg, len_offset, rl_result.low_reg);
2644 cg->StoreValue(cu, rl_dest, rl_result);
buzbee8fa0fda2012-06-27 15:44:52 -07002645}
2646
buzbeefa57c472012-11-21 12:06:18 -08002647static void CvtMoveException(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee32412962012-06-26 16:27:56 -07002648{
buzbee02031b12012-11-23 09:41:35 -08002649 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002650 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002651 cg->GenMoveException(cu, rl_dest);
buzbee32412962012-06-26 16:27:56 -07002652}
2653
buzbeefa57c472012-11-21 12:06:18 -08002654static void CvtSget(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_wide, bool is_object)
buzbee4f1181f2012-06-22 13:52:12 -07002655{
buzbee02031b12012-11-23 09:41:35 -08002656 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002657 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2658 llvm::ConstantInt* type_idx_val =
2659 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2660 uint32_t type_idx = type_idx_val->getZExtValue();
2661 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002662 cg->GenSget(cu, type_idx, rl_dest, is_wide, is_object);
buzbee4f1181f2012-06-22 13:52:12 -07002663}
2664
buzbeefa57c472012-11-21 12:06:18 -08002665static void CvtSput(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_wide, bool is_object)
buzbee8fa0fda2012-06-27 15:44:52 -07002666{
buzbee02031b12012-11-23 09:41:35 -08002667 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002668 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2669 llvm::ConstantInt* type_idx_val =
2670 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2671 uint32_t type_idx = type_idx_val->getZExtValue();
2672 llvm::Value* src = call_inst->getArgOperand(1);
2673 RegLocation rl_src = GetLoc(cu, src);
buzbee02031b12012-11-23 09:41:35 -08002674 cg->GenSput(cu, type_idx, rl_src, is_wide, is_object);
buzbee8fa0fda2012-06-27 15:44:52 -07002675}
2676
buzbeefa57c472012-11-21 12:06:18 -08002677static void CvtAget(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size, int scale)
buzbee8fa0fda2012-06-27 15:44:52 -07002678{
buzbee02031b12012-11-23 09:41:35 -08002679 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002680 DCHECK_EQ(call_inst->getNumArgOperands(), 3U);
2681 llvm::ConstantInt* opt_flags =
2682 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2683 RegLocation rl_array = GetLoc(cu, call_inst->getArgOperand(1));
2684 RegLocation rl_index = GetLoc(cu, call_inst->getArgOperand(2));
2685 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002686 cg->GenArrayGet(cu, opt_flags->getZExtValue(), size, rl_array, rl_index,
buzbeefa57c472012-11-21 12:06:18 -08002687 rl_dest, scale);
buzbee8fa0fda2012-06-27 15:44:52 -07002688}
2689
buzbeefa57c472012-11-21 12:06:18 -08002690static void CvtAput(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size,
2691 int scale, bool is_object)
buzbee8fa0fda2012-06-27 15:44:52 -07002692{
buzbee02031b12012-11-23 09:41:35 -08002693 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002694 DCHECK_EQ(call_inst->getNumArgOperands(), 4U);
2695 llvm::ConstantInt* opt_flags =
2696 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2697 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
2698 RegLocation rl_array = GetLoc(cu, call_inst->getArgOperand(2));
2699 RegLocation rl_index = GetLoc(cu, call_inst->getArgOperand(3));
2700 if (is_object) {
buzbee02031b12012-11-23 09:41:35 -08002701 cg->GenArrayObjPut(cu, opt_flags->getZExtValue(), rl_array, rl_index,
buzbeefa57c472012-11-21 12:06:18 -08002702 rl_src, scale);
buzbeef1f86362012-07-10 15:18:31 -07002703 } else {
buzbee02031b12012-11-23 09:41:35 -08002704 cg->GenArrayPut(cu, opt_flags->getZExtValue(), size, rl_array, rl_index,
buzbeefa57c472012-11-21 12:06:18 -08002705 rl_src, scale);
buzbeef1f86362012-07-10 15:18:31 -07002706 }
2707}
2708
buzbeefa57c472012-11-21 12:06:18 -08002709static void CvtAputObj(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbeef1f86362012-07-10 15:18:31 -07002710{
buzbeefa57c472012-11-21 12:06:18 -08002711 CvtAput(cu, call_inst, kWord, 2, true /* is_object */);
buzbeef1f86362012-07-10 15:18:31 -07002712}
2713
buzbeefa57c472012-11-21 12:06:18 -08002714static void CvtAputPrimitive(CompilationUnit* cu, llvm::CallInst* call_inst,
buzbeef1f86362012-07-10 15:18:31 -07002715 OpSize size, int scale)
2716{
buzbeefa57c472012-11-21 12:06:18 -08002717 CvtAput(cu, call_inst, size, scale, false /* is_object */);
buzbee8fa0fda2012-06-27 15:44:52 -07002718}
2719
buzbeefa57c472012-11-21 12:06:18 -08002720static void CvtIget(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size,
2721 bool is_wide, bool is_obj)
buzbee101305f2012-06-28 18:00:56 -07002722{
buzbee02031b12012-11-23 09:41:35 -08002723 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002724 DCHECK_EQ(call_inst->getNumArgOperands(), 3U);
2725 llvm::ConstantInt* opt_flags =
2726 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2727 RegLocation rl_obj = GetLoc(cu, call_inst->getArgOperand(1));
2728 llvm::ConstantInt* field_idx =
2729 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(2));
2730 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002731 cg->GenIGet(cu, field_idx->getZExtValue(), opt_flags->getZExtValue(),
buzbeefa57c472012-11-21 12:06:18 -08002732 size, rl_dest, rl_obj, is_wide, is_obj);
buzbee101305f2012-06-28 18:00:56 -07002733}
2734
buzbeefa57c472012-11-21 12:06:18 -08002735static void CvtIput(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size,
2736 bool is_wide, bool is_obj)
buzbee101305f2012-06-28 18:00:56 -07002737{
buzbee02031b12012-11-23 09:41:35 -08002738 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002739 DCHECK_EQ(call_inst->getNumArgOperands(), 4U);
2740 llvm::ConstantInt* opt_flags =
2741 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2742 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
2743 RegLocation rl_obj = GetLoc(cu, call_inst->getArgOperand(2));
2744 llvm::ConstantInt* field_idx =
2745 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(3));
buzbee02031b12012-11-23 09:41:35 -08002746 cg->GenIPut(cu, field_idx->getZExtValue(), opt_flags->getZExtValue(),
buzbeefa57c472012-11-21 12:06:18 -08002747 size, rl_src, rl_obj, is_wide, is_obj);
buzbee101305f2012-06-28 18:00:56 -07002748}
2749
buzbeefa57c472012-11-21 12:06:18 -08002750static void CvtCheckCast(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee101305f2012-06-28 18:00:56 -07002751{
buzbee02031b12012-11-23 09:41:35 -08002752 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002753 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2754 llvm::ConstantInt* type_idx =
2755 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2756 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
buzbee02031b12012-11-23 09:41:35 -08002757 cg->GenCheckCast(cu, type_idx->getZExtValue(), rl_src);
buzbee101305f2012-06-28 18:00:56 -07002758}
2759
buzbeefa57c472012-11-21 12:06:18 -08002760static void CvtFPCompare(CompilationUnit* cu, llvm::CallInst* call_inst,
buzbeeaad94382012-11-21 07:40:50 -08002761 Instruction::Code opcode)
buzbee76592632012-06-29 15:18:35 -07002762{
buzbee02031b12012-11-23 09:41:35 -08002763 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002764 RegLocation rl_src1 = GetLoc(cu, call_inst->getArgOperand(0));
2765 RegLocation rl_src2 = GetLoc(cu, call_inst->getArgOperand(1));
2766 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002767 cg->GenCmpFP(cu, opcode, rl_dest, rl_src1, rl_src2);
buzbee76592632012-06-29 15:18:35 -07002768}
2769
buzbeefa57c472012-11-21 12:06:18 -08002770static void CvtLongCompare(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002771{
buzbee02031b12012-11-23 09:41:35 -08002772 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002773 RegLocation rl_src1 = GetLoc(cu, call_inst->getArgOperand(0));
2774 RegLocation rl_src2 = GetLoc(cu, call_inst->getArgOperand(1));
2775 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002776 cg->GenCmpLong(cu, rl_dest, rl_src1, rl_src2);
buzbee76592632012-06-29 15:18:35 -07002777}
2778
buzbeefa57c472012-11-21 12:06:18 -08002779static void CvtSwitch(CompilationUnit* cu, llvm::Instruction* inst)
buzbeef58c12c2012-07-03 15:06:29 -07002780{
buzbee02031b12012-11-23 09:41:35 -08002781 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002782 llvm::SwitchInst* sw_inst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2783 DCHECK(sw_inst != NULL);
2784 llvm::Value* test_val = sw_inst->getCondition();
2785 llvm::MDNode* table_offset_node = sw_inst->getMetadata("SwitchTable");
2786 DCHECK(table_offset_node != NULL);
2787 llvm::ConstantInt* table_offset_value =
2788 static_cast<llvm::ConstantInt*>(table_offset_node->getOperand(0));
2789 int32_t table_offset = table_offset_value->getSExtValue();
2790 RegLocation rl_src = GetLoc(cu, test_val);
2791 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
2792 uint16_t table_magic = *table;
2793 if (table_magic == 0x100) {
buzbee02031b12012-11-23 09:41:35 -08002794 cg->GenPackedSwitch(cu, table_offset, rl_src);
buzbeea1da8a52012-07-09 14:00:21 -07002795 } else {
buzbeefa57c472012-11-21 12:06:18 -08002796 DCHECK_EQ(table_magic, 0x200);
buzbee02031b12012-11-23 09:41:35 -08002797 cg->GenSparseSwitch(cu, table_offset, rl_src);
buzbeea1da8a52012-07-09 14:00:21 -07002798 }
buzbeef58c12c2012-07-03 15:06:29 -07002799}
2800
buzbeefa57c472012-11-21 12:06:18 -08002801static void CvtInvoke(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_void,
2802 bool is_filled_new_array)
buzbee6969d502012-06-15 16:40:31 -07002803{
buzbee02031b12012-11-23 09:41:35 -08002804 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002805 CallInfo* info = static_cast<CallInfo*>(NewMem(cu, sizeof(CallInfo), true, kAllocMisc));
2806 if (is_void) {
buzbee6969d502012-06-15 16:40:31 -07002807 info->result.location = kLocInvalid;
2808 } else {
buzbeefa57c472012-11-21 12:06:18 -08002809 info->result = GetLoc(cu, call_inst);
buzbee6969d502012-06-15 16:40:31 -07002810 }
buzbeefa57c472012-11-21 12:06:18 -08002811 llvm::ConstantInt* invoke_type_val =
2812 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2813 llvm::ConstantInt* method_index_val =
2814 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(1));
2815 llvm::ConstantInt* opt_flags_val =
2816 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(2));
2817 info->type = static_cast<InvokeType>(invoke_type_val->getZExtValue());
2818 info->index = method_index_val->getZExtValue();
2819 info->opt_flags = opt_flags_val->getZExtValue();
2820 info->offset = cu->current_dalvik_offset;
buzbee6969d502012-06-15 16:40:31 -07002821
buzbee6969d502012-06-15 16:40:31 -07002822 // Count the argument words, and then build argument array.
buzbeefa57c472012-11-21 12:06:18 -08002823 info->num_arg_words = 0;
2824 for (unsigned int i = 3; i < call_inst->getNumArgOperands(); i++) {
2825 RegLocation t_loc = GetLoc(cu, call_inst->getArgOperand(i));
2826 info->num_arg_words += t_loc.wide ? 2 : 1;
buzbee6969d502012-06-15 16:40:31 -07002827 }
buzbeefa57c472012-11-21 12:06:18 -08002828 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
2829 (NewMem(cu, sizeof(RegLocation) * info->num_arg_words, false, kAllocMisc));
buzbee6969d502012-06-15 16:40:31 -07002830 // Now, fill in the location records, synthesizing high loc of wide vals
buzbeefa57c472012-11-21 12:06:18 -08002831 for (int i = 3, next = 0; next < info->num_arg_words;) {
2832 info->args[next] = GetLoc(cu, call_inst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002833 if (info->args[next].wide) {
2834 next++;
2835 // TODO: Might make sense to mark this as an invalid loc
buzbeefa57c472012-11-21 12:06:18 -08002836 info->args[next].orig_sreg = info->args[next-1].orig_sreg+1;
2837 info->args[next].s_reg_low = info->args[next-1].s_reg_low+1;
buzbee6969d502012-06-15 16:40:31 -07002838 }
2839 next++;
2840 }
buzbeefa57c472012-11-21 12:06:18 -08002841 // TODO - rework such that we no longer need is_range
2842 info->is_range = (info->num_arg_words > 5);
buzbee4f4dfc72012-07-02 14:54:44 -07002843
buzbeefa57c472012-11-21 12:06:18 -08002844 if (is_filled_new_array) {
buzbee02031b12012-11-23 09:41:35 -08002845 cg->GenFilledNewArray(cu, info);
buzbee101305f2012-06-28 18:00:56 -07002846 } else {
buzbee02031b12012-11-23 09:41:35 -08002847 cg->GenInvoke(cu, info);
buzbee101305f2012-06-28 18:00:56 -07002848 }
buzbee6969d502012-06-15 16:40:31 -07002849}
2850
buzbeead8f15e2012-06-18 14:49:45 -07002851/* Look up the RegLocation associated with a Value. Must already be defined */
buzbeefa57c472012-11-21 12:06:18 -08002852static RegLocation ValToLoc(CompilationUnit* cu, llvm::Value* val)
buzbeead8f15e2012-06-18 14:49:45 -07002853{
buzbeefa57c472012-11-21 12:06:18 -08002854 SafeMap<llvm::Value*, RegLocation>::iterator it = cu->loc_map.find(val);
2855 DCHECK(it != cu->loc_map.end()) << "Missing definition";
buzbeead8f15e2012-06-18 14:49:45 -07002856 return it->second;
2857}
2858
buzbeefa57c472012-11-21 12:06:18 -08002859static bool BitcodeBlockCodeGen(CompilationUnit* cu, llvm::BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07002860{
buzbee02031b12012-11-23 09:41:35 -08002861 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002862 while (cu->llvm_blocks.find(bb) == cu->llvm_blocks.end()) {
2863 llvm::BasicBlock* next_bb = NULL;
2864 cu->llvm_blocks.insert(bb);
2865 bool is_entry = (bb == &cu->func->getEntryBlock());
buzbee0967a252012-09-14 10:43:54 -07002866 // Define the starting label
buzbeefa57c472012-11-21 12:06:18 -08002867 LIR* block_label = cu->block_to_label_map.Get(bb);
buzbee0967a252012-09-14 10:43:54 -07002868 // Extract the type and starting offset from the block's name
buzbeefa57c472012-11-21 12:06:18 -08002869 char block_type = kInvalidBlock;
2870 if (is_entry) {
2871 block_type = kNormalBlock;
2872 block_label->operands[0] = 0;
buzbee951c0a12012-10-03 16:31:39 -07002873 } else if (!bb->hasName()) {
buzbeefa57c472012-11-21 12:06:18 -08002874 block_type = kNormalBlock;
2875 block_label->operands[0] = DexFile::kDexNoIndex;
buzbee0967a252012-09-14 10:43:54 -07002876 } else {
buzbeefa57c472012-11-21 12:06:18 -08002877 std::string block_name = bb->getName().str();
buzbee951c0a12012-10-03 16:31:39 -07002878 int dummy;
buzbeefa57c472012-11-21 12:06:18 -08002879 sscanf(block_name.c_str(), kLabelFormat, &block_type, &block_label->operands[0], &dummy);
2880 cu->current_dalvik_offset = block_label->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002881 }
buzbeefa57c472012-11-21 12:06:18 -08002882 DCHECK((block_type == kNormalBlock) || (block_type == kCatchBlock));
2883 cu->current_dalvik_offset = block_label->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002884 // Set the label kind
buzbeefa57c472012-11-21 12:06:18 -08002885 block_label->opcode = kPseudoNormalBlockLabel;
buzbee0967a252012-09-14 10:43:54 -07002886 // Insert the label
buzbeefa57c472012-11-21 12:06:18 -08002887 AppendLIR(cu, block_label);
buzbee2cfc6392012-05-07 14:51:40 -07002888
buzbeefa57c472012-11-21 12:06:18 -08002889 LIR* head_lir = NULL;
buzbee8320f382012-09-11 16:29:42 -07002890
buzbeefa57c472012-11-21 12:06:18 -08002891 if (block_type == kCatchBlock) {
2892 head_lir = NewLIR0(cu, kPseudoExportedPC);
buzbee0967a252012-09-14 10:43:54 -07002893 }
buzbee8320f382012-09-11 16:29:42 -07002894
buzbee0967a252012-09-14 10:43:54 -07002895 // Free temp registers and reset redundant store tracking */
buzbeefa57c472012-11-21 12:06:18 -08002896 ResetRegPool(cu);
2897 ResetDefTracking(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002898
buzbee0967a252012-09-14 10:43:54 -07002899 //TODO: restore oat incoming liveness optimization
buzbeefa57c472012-11-21 12:06:18 -08002900 ClobberAllRegs(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002901
buzbeefa57c472012-11-21 12:06:18 -08002902 if (is_entry) {
buzbee52a77fc2012-11-20 19:50:46 -08002903 RegLocation* ArgLocs = static_cast<RegLocation*>
buzbeefa57c472012-11-21 12:06:18 -08002904 (NewMem(cu, sizeof(RegLocation) * cu->num_ins, true, kAllocMisc));
2905 llvm::Function::arg_iterator it(cu->func->arg_begin());
2906 llvm::Function::arg_iterator it_end(cu->func->arg_end());
buzbee0967a252012-09-14 10:43:54 -07002907 // Skip past Method*
2908 it++;
2909 for (unsigned i = 0; it != it_end; ++it) {
2910 llvm::Value* val = it;
buzbeefa57c472012-11-21 12:06:18 -08002911 ArgLocs[i++] = ValToLoc(cu, val);
buzbee0967a252012-09-14 10:43:54 -07002912 llvm::Type* ty = val->getType();
buzbeefa57c472012-11-21 12:06:18 -08002913 if ((ty == cu->irb->getInt64Ty()) || (ty == cu->irb->getDoubleTy())) {
buzbee52a77fc2012-11-20 19:50:46 -08002914 ArgLocs[i] = ArgLocs[i-1];
buzbeefa57c472012-11-21 12:06:18 -08002915 ArgLocs[i].low_reg = ArgLocs[i].high_reg;
2916 ArgLocs[i].orig_sreg++;
2917 ArgLocs[i].s_reg_low = INVALID_SREG;
2918 ArgLocs[i].high_word = true;
buzbee0967a252012-09-14 10:43:54 -07002919 i++;
2920 }
2921 }
buzbee02031b12012-11-23 09:41:35 -08002922 cg->GenEntrySequence(cu, ArgLocs, cu->method_loc);
buzbee0967a252012-09-14 10:43:54 -07002923 }
2924
2925 // Visit all of the instructions in the block
2926 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2927 llvm::Instruction* inst = it;
buzbeefa57c472012-11-21 12:06:18 -08002928 llvm::BasicBlock::iterator next_it = ++it;
buzbee0967a252012-09-14 10:43:54 -07002929 // Extract the Dalvik offset from the instruction
2930 uint32_t opcode = inst->getOpcode();
buzbeefa57c472012-11-21 12:06:18 -08002931 llvm::MDNode* dex_offset_node = inst->getMetadata("DexOff");
2932 if (dex_offset_node != NULL) {
2933 llvm::ConstantInt* dex_offset_value =
2934 static_cast<llvm::ConstantInt*>(dex_offset_node->getOperand(0));
2935 cu->current_dalvik_offset = dex_offset_value->getZExtValue();
buzbee0967a252012-09-14 10:43:54 -07002936 }
2937
buzbeefa57c472012-11-21 12:06:18 -08002938 ResetRegPool(cu);
2939 if (cu->disable_opt & (1 << kTrackLiveTemps)) {
2940 ClobberAllRegs(cu);
buzbee0967a252012-09-14 10:43:54 -07002941 }
2942
buzbeefa57c472012-11-21 12:06:18 -08002943 if (cu->disable_opt & (1 << kSuppressLoads)) {
2944 ResetDefTracking(cu);
buzbee0967a252012-09-14 10:43:54 -07002945 }
2946
2947 #ifndef NDEBUG
2948 /* Reset temp tracking sanity check */
buzbeefa57c472012-11-21 12:06:18 -08002949 cu->live_sreg = INVALID_SREG;
buzbee0967a252012-09-14 10:43:54 -07002950 #endif
2951
2952 // TODO: use llvm opcode name here instead of "boundary" if verbose
buzbeefa57c472012-11-21 12:06:18 -08002953 LIR* boundary_lir = MarkBoundary(cu, cu->current_dalvik_offset, "boundary");
buzbee0967a252012-09-14 10:43:54 -07002954
2955 /* Remember the first LIR for thisl block*/
buzbeefa57c472012-11-21 12:06:18 -08002956 if (head_lir == NULL) {
2957 head_lir = boundary_lir;
2958 head_lir->def_mask = ENCODE_ALL;
buzbee0967a252012-09-14 10:43:54 -07002959 }
2960
2961 switch(opcode) {
2962
2963 case llvm::Instruction::ICmp: {
buzbeefa57c472012-11-21 12:06:18 -08002964 llvm::Instruction* next_inst = next_it;
2965 llvm::BranchInst* br_inst = llvm::dyn_cast<llvm::BranchInst>(next_inst);
2966 if (br_inst != NULL /* and... */) {
2967 CvtICmpBr(cu, inst, br_inst);
buzbee0967a252012-09-14 10:43:54 -07002968 ++it;
2969 } else {
buzbeefa57c472012-11-21 12:06:18 -08002970 CvtICmp(cu, inst);
buzbee0967a252012-09-14 10:43:54 -07002971 }
2972 }
2973 break;
2974
2975 case llvm::Instruction::Call: {
buzbeefa57c472012-11-21 12:06:18 -08002976 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst);
2977 llvm::Function* callee = call_inst->getCalledFunction();
buzbee0967a252012-09-14 10:43:54 -07002978 greenland::IntrinsicHelper::IntrinsicId id =
buzbeefa57c472012-11-21 12:06:18 -08002979 cu->intrinsic_helper->GetIntrinsicId(callee);
buzbee0967a252012-09-14 10:43:54 -07002980 switch (id) {
2981 case greenland::IntrinsicHelper::AllocaShadowFrame:
buzbee0967a252012-09-14 10:43:54 -07002982 case greenland::IntrinsicHelper::PopShadowFrame:
TDYa1278e950c12012-11-02 09:58:19 -07002983 case greenland::IntrinsicHelper::SetVReg:
buzbee0967a252012-09-14 10:43:54 -07002984 // Ignore shadow frame stuff for quick compiler
2985 break;
2986 case greenland::IntrinsicHelper::CopyInt:
2987 case greenland::IntrinsicHelper::CopyObj:
2988 case greenland::IntrinsicHelper::CopyFloat:
2989 case greenland::IntrinsicHelper::CopyLong:
2990 case greenland::IntrinsicHelper::CopyDouble:
buzbeefa57c472012-11-21 12:06:18 -08002991 CvtCopy(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07002992 break;
2993 case greenland::IntrinsicHelper::ConstInt:
2994 case greenland::IntrinsicHelper::ConstObj:
2995 case greenland::IntrinsicHelper::ConstLong:
2996 case greenland::IntrinsicHelper::ConstFloat:
2997 case greenland::IntrinsicHelper::ConstDouble:
buzbeefa57c472012-11-21 12:06:18 -08002998 CvtConst(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07002999 break;
3000 case greenland::IntrinsicHelper::DivInt:
3001 case greenland::IntrinsicHelper::DivLong:
buzbeefa57c472012-11-21 12:06:18 -08003002 CvtBinOp(cu, kOpDiv, inst);
buzbee0967a252012-09-14 10:43:54 -07003003 break;
3004 case greenland::IntrinsicHelper::RemInt:
3005 case greenland::IntrinsicHelper::RemLong:
buzbeefa57c472012-11-21 12:06:18 -08003006 CvtBinOp(cu, kOpRem, inst);
buzbee0967a252012-09-14 10:43:54 -07003007 break;
3008 case greenland::IntrinsicHelper::MethodInfo:
3009 // Already dealt with - just ignore it here.
3010 break;
3011 case greenland::IntrinsicHelper::CheckSuspend:
buzbee02031b12012-11-23 09:41:35 -08003012 cg->GenSuspendTest(cu, 0 /* opt_flags already applied */);
buzbee0967a252012-09-14 10:43:54 -07003013 break;
3014 case greenland::IntrinsicHelper::HLInvokeObj:
3015 case greenland::IntrinsicHelper::HLInvokeFloat:
3016 case greenland::IntrinsicHelper::HLInvokeDouble:
3017 case greenland::IntrinsicHelper::HLInvokeLong:
3018 case greenland::IntrinsicHelper::HLInvokeInt:
buzbeefa57c472012-11-21 12:06:18 -08003019 CvtInvoke(cu, call_inst, false /* is_void */, false /* new_array */);
buzbee0967a252012-09-14 10:43:54 -07003020 break;
3021 case greenland::IntrinsicHelper::HLInvokeVoid:
buzbeefa57c472012-11-21 12:06:18 -08003022 CvtInvoke(cu, call_inst, true /* is_void */, false /* new_array */);
buzbee0967a252012-09-14 10:43:54 -07003023 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003024 case greenland::IntrinsicHelper::HLFilledNewArray:
buzbeefa57c472012-11-21 12:06:18 -08003025 CvtInvoke(cu, call_inst, false /* is_void */, true /* new_array */);
buzbee0967a252012-09-14 10:43:54 -07003026 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003027 case greenland::IntrinsicHelper::HLFillArrayData:
buzbeefa57c472012-11-21 12:06:18 -08003028 CvtFillArrayData(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003029 break;
3030 case greenland::IntrinsicHelper::ConstString:
buzbeefa57c472012-11-21 12:06:18 -08003031 CvtConstObject(cu, call_inst, true /* is_string */);
buzbee0967a252012-09-14 10:43:54 -07003032 break;
3033 case greenland::IntrinsicHelper::ConstClass:
buzbeefa57c472012-11-21 12:06:18 -08003034 CvtConstObject(cu, call_inst, false /* is_string */);
buzbee0967a252012-09-14 10:43:54 -07003035 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003036 case greenland::IntrinsicHelper::HLCheckCast:
buzbeefa57c472012-11-21 12:06:18 -08003037 CvtCheckCast(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003038 break;
3039 case greenland::IntrinsicHelper::NewInstance:
buzbeefa57c472012-11-21 12:06:18 -08003040 CvtNewInstance(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003041 break;
3042 case greenland::IntrinsicHelper::HLSgetObject:
buzbeefa57c472012-11-21 12:06:18 -08003043 CvtSget(cu, call_inst, false /* wide */, true /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003044 break;
3045 case greenland::IntrinsicHelper::HLSget:
3046 case greenland::IntrinsicHelper::HLSgetFloat:
3047 case greenland::IntrinsicHelper::HLSgetBoolean:
3048 case greenland::IntrinsicHelper::HLSgetByte:
3049 case greenland::IntrinsicHelper::HLSgetChar:
3050 case greenland::IntrinsicHelper::HLSgetShort:
buzbeefa57c472012-11-21 12:06:18 -08003051 CvtSget(cu, call_inst, false /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003052 break;
3053 case greenland::IntrinsicHelper::HLSgetWide:
3054 case greenland::IntrinsicHelper::HLSgetDouble:
buzbeefa57c472012-11-21 12:06:18 -08003055 CvtSget(cu, call_inst, true /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003056 break;
3057 case greenland::IntrinsicHelper::HLSput:
3058 case greenland::IntrinsicHelper::HLSputFloat:
3059 case greenland::IntrinsicHelper::HLSputBoolean:
3060 case greenland::IntrinsicHelper::HLSputByte:
3061 case greenland::IntrinsicHelper::HLSputChar:
3062 case greenland::IntrinsicHelper::HLSputShort:
buzbeefa57c472012-11-21 12:06:18 -08003063 CvtSput(cu, call_inst, false /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003064 break;
3065 case greenland::IntrinsicHelper::HLSputWide:
3066 case greenland::IntrinsicHelper::HLSputDouble:
buzbeefa57c472012-11-21 12:06:18 -08003067 CvtSput(cu, call_inst, true /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003068 break;
3069 case greenland::IntrinsicHelper::HLSputObject:
buzbeefa57c472012-11-21 12:06:18 -08003070 CvtSput(cu, call_inst, false /* wide */, true /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003071 break;
3072 case greenland::IntrinsicHelper::GetException:
buzbeefa57c472012-11-21 12:06:18 -08003073 CvtMoveException(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003074 break;
TDYa127f71bf5a2012-07-29 20:09:52 -07003075 case greenland::IntrinsicHelper::HLThrowException:
buzbeefa57c472012-11-21 12:06:18 -08003076 CvtThrow(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003077 break;
3078 case greenland::IntrinsicHelper::MonitorEnter:
buzbeefa57c472012-11-21 12:06:18 -08003079 CvtMonitorEnterExit(cu, true /* is_enter */, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003080 break;
3081 case greenland::IntrinsicHelper::MonitorExit:
buzbeefa57c472012-11-21 12:06:18 -08003082 CvtMonitorEnterExit(cu, false /* is_enter */, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003083 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003084 case greenland::IntrinsicHelper::OptArrayLength:
buzbeefa57c472012-11-21 12:06:18 -08003085 CvtArrayLength(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003086 break;
3087 case greenland::IntrinsicHelper::NewArray:
buzbeefa57c472012-11-21 12:06:18 -08003088 CvtNewArray(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003089 break;
3090 case greenland::IntrinsicHelper::InstanceOf:
buzbeefa57c472012-11-21 12:06:18 -08003091 CvtInstanceOf(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003092 break;
3093
3094 case greenland::IntrinsicHelper::HLArrayGet:
3095 case greenland::IntrinsicHelper::HLArrayGetObject:
3096 case greenland::IntrinsicHelper::HLArrayGetFloat:
buzbeefa57c472012-11-21 12:06:18 -08003097 CvtAget(cu, call_inst, kWord, 2);
buzbee0967a252012-09-14 10:43:54 -07003098 break;
3099 case greenland::IntrinsicHelper::HLArrayGetWide:
3100 case greenland::IntrinsicHelper::HLArrayGetDouble:
buzbeefa57c472012-11-21 12:06:18 -08003101 CvtAget(cu, call_inst, kLong, 3);
buzbee0967a252012-09-14 10:43:54 -07003102 break;
3103 case greenland::IntrinsicHelper::HLArrayGetBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003104 CvtAget(cu, call_inst, kUnsignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003105 break;
3106 case greenland::IntrinsicHelper::HLArrayGetByte:
buzbeefa57c472012-11-21 12:06:18 -08003107 CvtAget(cu, call_inst, kSignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003108 break;
3109 case greenland::IntrinsicHelper::HLArrayGetChar:
buzbeefa57c472012-11-21 12:06:18 -08003110 CvtAget(cu, call_inst, kUnsignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003111 break;
3112 case greenland::IntrinsicHelper::HLArrayGetShort:
buzbeefa57c472012-11-21 12:06:18 -08003113 CvtAget(cu, call_inst, kSignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003114 break;
3115
3116 case greenland::IntrinsicHelper::HLArrayPut:
3117 case greenland::IntrinsicHelper::HLArrayPutFloat:
buzbeefa57c472012-11-21 12:06:18 -08003118 CvtAputPrimitive(cu, call_inst, kWord, 2);
buzbee0967a252012-09-14 10:43:54 -07003119 break;
3120 case greenland::IntrinsicHelper::HLArrayPutObject:
buzbeefa57c472012-11-21 12:06:18 -08003121 CvtAputObj(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003122 break;
3123 case greenland::IntrinsicHelper::HLArrayPutWide:
3124 case greenland::IntrinsicHelper::HLArrayPutDouble:
buzbeefa57c472012-11-21 12:06:18 -08003125 CvtAputPrimitive(cu, call_inst, kLong, 3);
buzbee0967a252012-09-14 10:43:54 -07003126 break;
3127 case greenland::IntrinsicHelper::HLArrayPutBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003128 CvtAputPrimitive(cu, call_inst, kUnsignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003129 break;
3130 case greenland::IntrinsicHelper::HLArrayPutByte:
buzbeefa57c472012-11-21 12:06:18 -08003131 CvtAputPrimitive(cu, call_inst, kSignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003132 break;
3133 case greenland::IntrinsicHelper::HLArrayPutChar:
buzbeefa57c472012-11-21 12:06:18 -08003134 CvtAputPrimitive(cu, call_inst, kUnsignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003135 break;
3136 case greenland::IntrinsicHelper::HLArrayPutShort:
buzbeefa57c472012-11-21 12:06:18 -08003137 CvtAputPrimitive(cu, call_inst, kSignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003138 break;
3139
3140 case greenland::IntrinsicHelper::HLIGet:
3141 case greenland::IntrinsicHelper::HLIGetFloat:
buzbeefa57c472012-11-21 12:06:18 -08003142 CvtIget(cu, call_inst, kWord, false /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003143 break;
3144 case greenland::IntrinsicHelper::HLIGetObject:
buzbeefa57c472012-11-21 12:06:18 -08003145 CvtIget(cu, call_inst, kWord, false /* is_wide */, true /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003146 break;
3147 case greenland::IntrinsicHelper::HLIGetWide:
3148 case greenland::IntrinsicHelper::HLIGetDouble:
buzbeefa57c472012-11-21 12:06:18 -08003149 CvtIget(cu, call_inst, kLong, true /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003150 break;
3151 case greenland::IntrinsicHelper::HLIGetBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003152 CvtIget(cu, call_inst, kUnsignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003153 false /* obj */);
3154 break;
3155 case greenland::IntrinsicHelper::HLIGetByte:
buzbeefa57c472012-11-21 12:06:18 -08003156 CvtIget(cu, call_inst, kSignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003157 false /* obj */);
3158 break;
3159 case greenland::IntrinsicHelper::HLIGetChar:
buzbeefa57c472012-11-21 12:06:18 -08003160 CvtIget(cu, call_inst, kUnsignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003161 false /* obj */);
3162 break;
3163 case greenland::IntrinsicHelper::HLIGetShort:
buzbeefa57c472012-11-21 12:06:18 -08003164 CvtIget(cu, call_inst, kSignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003165 false /* obj */);
3166 break;
3167
3168 case greenland::IntrinsicHelper::HLIPut:
3169 case greenland::IntrinsicHelper::HLIPutFloat:
buzbeefa57c472012-11-21 12:06:18 -08003170 CvtIput(cu, call_inst, kWord, false /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003171 break;
3172 case greenland::IntrinsicHelper::HLIPutObject:
buzbeefa57c472012-11-21 12:06:18 -08003173 CvtIput(cu, call_inst, kWord, false /* is_wide */, true /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003174 break;
3175 case greenland::IntrinsicHelper::HLIPutWide:
3176 case greenland::IntrinsicHelper::HLIPutDouble:
buzbeefa57c472012-11-21 12:06:18 -08003177 CvtIput(cu, call_inst, kLong, true /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003178 break;
3179 case greenland::IntrinsicHelper::HLIPutBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003180 CvtIput(cu, call_inst, kUnsignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003181 false /* obj */);
3182 break;
3183 case greenland::IntrinsicHelper::HLIPutByte:
buzbeefa57c472012-11-21 12:06:18 -08003184 CvtIput(cu, call_inst, kSignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003185 false /* obj */);
3186 break;
3187 case greenland::IntrinsicHelper::HLIPutChar:
buzbeefa57c472012-11-21 12:06:18 -08003188 CvtIput(cu, call_inst, kUnsignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003189 false /* obj */);
3190 break;
3191 case greenland::IntrinsicHelper::HLIPutShort:
buzbeefa57c472012-11-21 12:06:18 -08003192 CvtIput(cu, call_inst, kSignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003193 false /* obj */);
3194 break;
3195
3196 case greenland::IntrinsicHelper::IntToChar:
buzbeefa57c472012-11-21 12:06:18 -08003197 CvtIntNarrowing(cu, call_inst, Instruction::INT_TO_CHAR);
buzbee0967a252012-09-14 10:43:54 -07003198 break;
3199 case greenland::IntrinsicHelper::IntToShort:
buzbeefa57c472012-11-21 12:06:18 -08003200 CvtIntNarrowing(cu, call_inst, Instruction::INT_TO_SHORT);
buzbee0967a252012-09-14 10:43:54 -07003201 break;
3202 case greenland::IntrinsicHelper::IntToByte:
buzbeefa57c472012-11-21 12:06:18 -08003203 CvtIntNarrowing(cu, call_inst, Instruction::INT_TO_BYTE);
buzbee0967a252012-09-14 10:43:54 -07003204 break;
3205
TDYa1274ec8ccd2012-08-11 07:04:57 -07003206 case greenland::IntrinsicHelper::F2I:
3207 case greenland::IntrinsicHelper::D2I:
3208 case greenland::IntrinsicHelper::F2L:
3209 case greenland::IntrinsicHelper::D2L:
buzbeefa57c472012-11-21 12:06:18 -08003210 CvtFPToInt(cu, call_inst);
TDYa1274ec8ccd2012-08-11 07:04:57 -07003211 break;
3212
buzbee0967a252012-09-14 10:43:54 -07003213 case greenland::IntrinsicHelper::CmplFloat:
buzbeefa57c472012-11-21 12:06:18 -08003214 CvtFPCompare(cu, call_inst, Instruction::CMPL_FLOAT);
buzbee0967a252012-09-14 10:43:54 -07003215 break;
3216 case greenland::IntrinsicHelper::CmpgFloat:
buzbeefa57c472012-11-21 12:06:18 -08003217 CvtFPCompare(cu, call_inst, Instruction::CMPG_FLOAT);
buzbee0967a252012-09-14 10:43:54 -07003218 break;
3219 case greenland::IntrinsicHelper::CmplDouble:
buzbeefa57c472012-11-21 12:06:18 -08003220 CvtFPCompare(cu, call_inst, Instruction::CMPL_DOUBLE);
buzbee0967a252012-09-14 10:43:54 -07003221 break;
3222 case greenland::IntrinsicHelper::CmpgDouble:
buzbeefa57c472012-11-21 12:06:18 -08003223 CvtFPCompare(cu, call_inst, Instruction::CMPG_DOUBLE);
buzbee0967a252012-09-14 10:43:54 -07003224 break;
3225
3226 case greenland::IntrinsicHelper::CmpLong:
buzbeefa57c472012-11-21 12:06:18 -08003227 CvtLongCompare(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003228 break;
3229
3230 case greenland::IntrinsicHelper::SHLLong:
buzbeefa57c472012-11-21 12:06:18 -08003231 CvtShiftOp(cu, Instruction::SHL_LONG, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003232 break;
3233 case greenland::IntrinsicHelper::SHRLong:
buzbeefa57c472012-11-21 12:06:18 -08003234 CvtShiftOp(cu, Instruction::SHR_LONG, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003235 break;
3236 case greenland::IntrinsicHelper::USHRLong:
buzbeefa57c472012-11-21 12:06:18 -08003237 CvtShiftOp(cu, Instruction::USHR_LONG, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003238 break;
3239 case greenland::IntrinsicHelper::SHLInt:
buzbeefa57c472012-11-21 12:06:18 -08003240 CvtShiftOp(cu, Instruction::SHL_INT, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003241 break;
3242 case greenland::IntrinsicHelper::SHRInt:
buzbeefa57c472012-11-21 12:06:18 -08003243 CvtShiftOp(cu, Instruction::SHR_INT, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003244 break;
3245 case greenland::IntrinsicHelper::USHRInt:
buzbeefa57c472012-11-21 12:06:18 -08003246 CvtShiftOp(cu, Instruction::USHR_INT, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003247 break;
3248
3249 case greenland::IntrinsicHelper::CatchTargets: {
buzbeefa57c472012-11-21 12:06:18 -08003250 llvm::SwitchInst* sw_inst =
3251 llvm::dyn_cast<llvm::SwitchInst>(next_it);
3252 DCHECK(sw_inst != NULL);
buzbee0967a252012-09-14 10:43:54 -07003253 /*
3254 * Discard the edges and the following conditional branch.
3255 * Do a direct branch to the default target (which is the
3256 * "work" portion of the pair.
3257 * TODO: awful code layout - rework
3258 */
buzbeefa57c472012-11-21 12:06:18 -08003259 llvm::BasicBlock* target_bb = sw_inst->getDefaultDest();
3260 DCHECK(target_bb != NULL);
buzbee02031b12012-11-23 09:41:35 -08003261 cg->OpUnconditionalBranch(cu, cu->block_to_label_map.Get(target_bb));
buzbee0967a252012-09-14 10:43:54 -07003262 ++it;
3263 // Set next bb to default target - improves code layout
buzbeefa57c472012-11-21 12:06:18 -08003264 next_bb = target_bb;
buzbee0967a252012-09-14 10:43:54 -07003265 }
3266 break;
3267
3268 default:
buzbeefa57c472012-11-21 12:06:18 -08003269 LOG(FATAL) << "Unexpected intrinsic " << cu->intrinsic_helper->GetName(id);
buzbee0967a252012-09-14 10:43:54 -07003270 }
3271 }
3272 break;
3273
buzbeefa57c472012-11-21 12:06:18 -08003274 case llvm::Instruction::Br: CvtBr(cu, inst); break;
3275 case llvm::Instruction::Add: CvtBinOp(cu, kOpAdd, inst); break;
3276 case llvm::Instruction::Sub: CvtBinOp(cu, kOpSub, inst); break;
3277 case llvm::Instruction::Mul: CvtBinOp(cu, kOpMul, inst); break;
3278 case llvm::Instruction::SDiv: CvtBinOp(cu, kOpDiv, inst); break;
3279 case llvm::Instruction::SRem: CvtBinOp(cu, kOpRem, inst); break;
3280 case llvm::Instruction::And: CvtBinOp(cu, kOpAnd, inst); break;
3281 case llvm::Instruction::Or: CvtBinOp(cu, kOpOr, inst); break;
3282 case llvm::Instruction::Xor: CvtBinOp(cu, kOpXor, inst); break;
3283 case llvm::Instruction::PHI: CvtPhi(cu, inst); break;
3284 case llvm::Instruction::Ret: CvtRet(cu, inst); break;
3285 case llvm::Instruction::FAdd: CvtBinFPOp(cu, kOpAdd, inst); break;
3286 case llvm::Instruction::FSub: CvtBinFPOp(cu, kOpSub, inst); break;
3287 case llvm::Instruction::FMul: CvtBinFPOp(cu, kOpMul, inst); break;
3288 case llvm::Instruction::FDiv: CvtBinFPOp(cu, kOpDiv, inst); break;
3289 case llvm::Instruction::FRem: CvtBinFPOp(cu, kOpRem, inst); break;
3290 case llvm::Instruction::SIToFP: CvtIntToFP(cu, inst); break;
3291 case llvm::Instruction::FPTrunc: CvtDoubleToFloat(cu, inst); break;
3292 case llvm::Instruction::FPExt: CvtFloatToDouble(cu, inst); break;
3293 case llvm::Instruction::Trunc: CvtTrunc(cu, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003294
buzbeefa57c472012-11-21 12:06:18 -08003295 case llvm::Instruction::ZExt: CvtIntExt(cu, inst, false /* signed */);
buzbee0967a252012-09-14 10:43:54 -07003296 break;
buzbeefa57c472012-11-21 12:06:18 -08003297 case llvm::Instruction::SExt: CvtIntExt(cu, inst, true /* signed */);
buzbee0967a252012-09-14 10:43:54 -07003298 break;
3299
buzbeefa57c472012-11-21 12:06:18 -08003300 case llvm::Instruction::Switch: CvtSwitch(cu, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003301
3302 case llvm::Instruction::Unreachable:
3303 break; // FIXME: can we really ignore these?
3304
3305 case llvm::Instruction::Shl:
3306 case llvm::Instruction::LShr:
3307 case llvm::Instruction::AShr:
3308 case llvm::Instruction::Invoke:
3309 case llvm::Instruction::FPToUI:
TDYa1274ec8ccd2012-08-11 07:04:57 -07003310 case llvm::Instruction::FPToSI:
buzbee0967a252012-09-14 10:43:54 -07003311 case llvm::Instruction::UIToFP:
3312 case llvm::Instruction::PtrToInt:
3313 case llvm::Instruction::IntToPtr:
3314 case llvm::Instruction::FCmp:
3315 case llvm::Instruction::URem:
3316 case llvm::Instruction::UDiv:
3317 case llvm::Instruction::Resume:
3318 case llvm::Instruction::Alloca:
3319 case llvm::Instruction::GetElementPtr:
3320 case llvm::Instruction::Fence:
3321 case llvm::Instruction::AtomicCmpXchg:
3322 case llvm::Instruction::AtomicRMW:
3323 case llvm::Instruction::BitCast:
3324 case llvm::Instruction::VAArg:
3325 case llvm::Instruction::Select:
3326 case llvm::Instruction::UserOp1:
3327 case llvm::Instruction::UserOp2:
3328 case llvm::Instruction::ExtractElement:
3329 case llvm::Instruction::InsertElement:
3330 case llvm::Instruction::ShuffleVector:
3331 case llvm::Instruction::ExtractValue:
3332 case llvm::Instruction::InsertValue:
3333 case llvm::Instruction::LandingPad:
3334 case llvm::Instruction::IndirectBr:
3335 case llvm::Instruction::Load:
3336 case llvm::Instruction::Store:
3337 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3338
3339 default:
3340 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3341 break;
buzbeead8f15e2012-06-18 14:49:45 -07003342 }
3343 }
buzbee2cfc6392012-05-07 14:51:40 -07003344
buzbeefa57c472012-11-21 12:06:18 -08003345 if (head_lir != NULL) {
3346 ApplyLocalOptimizations(cu, head_lir, cu->last_lir_insn);
buzbee2cfc6392012-05-07 14:51:40 -07003347 }
buzbeefa57c472012-11-21 12:06:18 -08003348 if (next_bb != NULL) {
3349 bb = next_bb;
3350 next_bb = NULL;
buzbee6969d502012-06-15 16:40:31 -07003351 }
buzbee6969d502012-06-15 16:40:31 -07003352 }
buzbee2cfc6392012-05-07 14:51:40 -07003353 return false;
3354}
3355
3356/*
3357 * Convert LLVM_IR to MIR:
3358 * o Iterate through the LLVM_IR and construct a graph using
3359 * standard MIR building blocks.
3360 * o Perform a basic-block optimization pass to remove unnecessary
3361 * store/load sequences.
3362 * o Convert the LLVM Value operands into RegLocations where applicable.
buzbeefa57c472012-11-21 12:06:18 -08003363 * o Create ssa_rep def/use operand arrays for each converted LLVM opcode
buzbee2cfc6392012-05-07 14:51:40 -07003364 * o Perform register promotion
3365 * o Iterate through the graph a basic block at a time, generating
3366 * LIR.
3367 * o Assemble LIR as usual.
3368 * o Profit.
3369 */
buzbeefa57c472012-11-21 12:06:18 -08003370void MethodBitcode2LIR(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -07003371{
buzbee02031b12012-11-23 09:41:35 -08003372 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08003373 llvm::Function* func = cu->func;
3374 int num_basic_blocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003375 // Allocate a list for LIR basic block labels
buzbeefa57c472012-11-21 12:06:18 -08003376 cu->block_label_list =
3377 static_cast<LIR*>(NewMem(cu, sizeof(LIR) * num_basic_blocks, true, kAllocLIR));
3378 LIR* label_list = cu->block_label_list;
3379 int next_label = 0;
buzbee28c9a832012-11-21 15:39:13 -08003380 for (llvm::Function::iterator i = func->begin(), e = func->end(); i != e; ++i) {
buzbeefa57c472012-11-21 12:06:18 -08003381 cu->block_to_label_map.Put(static_cast<llvm::BasicBlock*>(i),
3382 &label_list[next_label++]);
buzbee2cfc6392012-05-07 14:51:40 -07003383 }
buzbeead8f15e2012-06-18 14:49:45 -07003384
3385 /*
buzbeefa57c472012-11-21 12:06:18 -08003386 * Keep honest - clear reg_locations, Value => RegLocation,
buzbeead8f15e2012-06-18 14:49:45 -07003387 * promotion map and VmapTables.
3388 */
buzbeefa57c472012-11-21 12:06:18 -08003389 cu->loc_map.clear(); // Start fresh
3390 cu->reg_location = NULL;
buzbee28c9a832012-11-21 15:39:13 -08003391 for (int i = 0; i < cu->num_dalvik_registers + cu->num_compiler_temps + 1; i++) {
buzbeefa57c472012-11-21 12:06:18 -08003392 cu->promotion_map[i].core_location = kLocDalvikFrame;
3393 cu->promotion_map[i].fp_location = kLocDalvikFrame;
buzbeead8f15e2012-06-18 14:49:45 -07003394 }
buzbeefa57c472012-11-21 12:06:18 -08003395 cu->core_spill_mask = 0;
3396 cu->num_core_spills = 0;
3397 cu->fp_spill_mask = 0;
3398 cu->num_fp_spills = 0;
3399 cu->core_vmap_table.clear();
3400 cu->fp_vmap_table.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003401
3402 /*
3403 * At this point, we've lost all knowledge of register promotion.
3404 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003405 * exists - not required for correctness). Normally, this will
3406 * be the first instruction we encounter, so we won't have to iterate
3407 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003408 */
buzbee28c9a832012-11-21 15:39:13 -08003409 for (llvm::inst_iterator i = llvm::inst_begin(func), e = llvm::inst_end(func); i != e; ++i) {
buzbeefa57c472012-11-21 12:06:18 -08003410 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(&*i);
3411 if (call_inst != NULL) {
3412 llvm::Function* callee = call_inst->getCalledFunction();
buzbeeca7a5e42012-08-20 11:12:18 -07003413 greenland::IntrinsicHelper::IntrinsicId id =
buzbeefa57c472012-11-21 12:06:18 -08003414 cu->intrinsic_helper->GetIntrinsicId(callee);
buzbeeca7a5e42012-08-20 11:12:18 -07003415 if (id == greenland::IntrinsicHelper::MethodInfo) {
buzbeefa57c472012-11-21 12:06:18 -08003416 if (cu->verbose) {
buzbeeca7a5e42012-08-20 11:12:18 -07003417 LOG(INFO) << "Found MethodInfo";
3418 }
buzbeefa57c472012-11-21 12:06:18 -08003419 llvm::MDNode* reg_info_node = call_inst->getMetadata("RegInfo");
3420 if (reg_info_node != NULL) {
3421 llvm::ConstantInt* num_ins_value =
3422 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(0));
3423 llvm::ConstantInt* num_regs_value =
3424 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(1));
3425 llvm::ConstantInt* num_outs_value =
3426 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(2));
3427 llvm::ConstantInt* num_compiler_temps_value =
3428 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(3));
3429 llvm::ConstantInt* num_ssa_regs_value =
3430 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(4));
3431 if (cu->verbose) {
3432 LOG(INFO) << "RegInfo - Ins:" << num_ins_value->getZExtValue()
3433 << ", Regs:" << num_regs_value->getZExtValue()
3434 << ", Outs:" << num_outs_value->getZExtValue()
3435 << ", CTemps:" << num_compiler_temps_value->getZExtValue()
3436 << ", SSARegs:" << num_ssa_regs_value->getZExtValue();
buzbeeca7a5e42012-08-20 11:12:18 -07003437 }
3438 }
buzbeefa57c472012-11-21 12:06:18 -08003439 llvm::MDNode* pmap_info_node = call_inst->getMetadata("PromotionMap");
3440 if (pmap_info_node != NULL) {
3441 int elems = pmap_info_node->getNumOperands();
3442 if (cu->verbose) {
buzbeeca7a5e42012-08-20 11:12:18 -07003443 LOG(INFO) << "PMap size: " << elems;
3444 }
3445 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -08003446 llvm::ConstantInt* raw_map_data =
3447 static_cast<llvm::ConstantInt*>(pmap_info_node->getOperand(i));
3448 uint32_t map_data = raw_map_data->getZExtValue();
3449 PromotionMap* p = &cu->promotion_map[i];
3450 p->first_in_pair = (map_data >> 24) & 0xff;
3451 p->FpReg = (map_data >> 16) & 0xff;
3452 p->core_reg = (map_data >> 8) & 0xff;
3453 p->fp_location = static_cast<RegLocationType>((map_data >> 4) & 0xf);
3454 if (p->fp_location == kLocPhysReg) {
3455 RecordFpPromotion(cu, p->FpReg, i);
buzbeeca7a5e42012-08-20 11:12:18 -07003456 }
buzbeefa57c472012-11-21 12:06:18 -08003457 p->core_location = static_cast<RegLocationType>(map_data & 0xf);
3458 if (p->core_location == kLocPhysReg) {
3459 RecordCorePromotion(cu, p->core_reg, i);
buzbeeca7a5e42012-08-20 11:12:18 -07003460 }
3461 }
buzbeefa57c472012-11-21 12:06:18 -08003462 if (cu->verbose) {
3463 DumpPromotionMap(cu);
buzbeeca7a5e42012-08-20 11:12:18 -07003464 }
3465 }
3466 break;
3467 }
3468 }
3469 }
buzbee02031b12012-11-23 09:41:35 -08003470 cg->AdjustSpillMask(cu);
buzbeefa57c472012-11-21 12:06:18 -08003471 cu->frame_size = ComputeFrameSize(cu);
buzbeead8f15e2012-06-18 14:49:45 -07003472
3473 // Create RegLocations for arguments
buzbeefa57c472012-11-21 12:06:18 -08003474 llvm::Function::arg_iterator it(cu->func->arg_begin());
3475 llvm::Function::arg_iterator it_end(cu->func->arg_end());
buzbeead8f15e2012-06-18 14:49:45 -07003476 for (; it != it_end; ++it) {
3477 llvm::Value* val = it;
buzbeefa57c472012-11-21 12:06:18 -08003478 CreateLocFromValue(cu, val);
buzbeead8f15e2012-06-18 14:49:45 -07003479 }
3480 // Create RegLocations for all non-argument defintions
buzbee28c9a832012-11-21 15:39:13 -08003481 for (llvm::inst_iterator i = llvm::inst_begin(func), e = llvm::inst_end(func); i != e; ++i) {
buzbeead8f15e2012-06-18 14:49:45 -07003482 llvm::Value* val = &*i;
3483 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
buzbeefa57c472012-11-21 12:06:18 -08003484 CreateLocFromValue(cu, val);
buzbeead8f15e2012-06-18 14:49:45 -07003485 }
3486 }
3487
buzbee2cfc6392012-05-07 14:51:40 -07003488 // Walk the blocks, generating code.
buzbee28c9a832012-11-21 15:39:13 -08003489 for (llvm::Function::iterator i = cu->func->begin(), e = cu->func->end(); i != e; ++i) {
buzbeefa57c472012-11-21 12:06:18 -08003490 BitcodeBlockCodeGen(cu, static_cast<llvm::BasicBlock*>(i));
buzbee2cfc6392012-05-07 14:51:40 -07003491 }
3492
buzbee02031b12012-11-23 09:41:35 -08003493 cg->HandleSuspendLaunchPads(cu);
buzbee2cfc6392012-05-07 14:51:40 -07003494
buzbee02031b12012-11-23 09:41:35 -08003495 cg->HandleThrowLaunchPads(cu);
buzbee2cfc6392012-05-07 14:51:40 -07003496
buzbee02031b12012-11-23 09:41:35 -08003497 cg->HandleIntrinsicLaunchPads(cu);
buzbee2cfc6392012-05-07 14:51:40 -07003498
buzbeefa57c472012-11-21 12:06:18 -08003499 cu->func->eraseFromParent();
3500 cu->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003501}
3502
3503
3504} // namespace art