blob: 4cae511bb72d06f1cf765f90f559133c808df4b9 [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
buzbee395116c2013-02-27 14:30:25 -080030#include "compiler/dex/compiler_internals.h"
31
32//TODO: move gbc_to_lir code into quick directory (if necessary).
33#include "compiler/dex/quick/codegen_util.h"
34#include "compiler/dex/quick/local_optimizations.h"
35#include "compiler/dex/quick/ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080036
buzbee8320f382012-09-11 16:29:42 -070037static const char* kLabelFormat = "%c0x%x_%d";
buzbee951c0a12012-10-03 16:31:39 -070038static const char kInvalidBlock = 0xff;
buzbee8320f382012-09-11 16:29:42 -070039static const char kNormalBlock = 'L';
40static const char kCatchBlock = 'C';
buzbee2cfc6392012-05-07 14:51:40 -070041
42namespace art {
buzbeefa57c472012-11-21 12:06:18 -080043static RegLocation GetLoc(CompilationUnit* cu, llvm::Value* val);
buzbee2cfc6392012-05-07 14:51:40 -070044
buzbeefa57c472012-11-21 12:06:18 -080045static llvm::BasicBlock* GetLLVMBlock(CompilationUnit* cu, int id)
buzbee2cfc6392012-05-07 14:51:40 -070046{
buzbeefa57c472012-11-21 12:06:18 -080047 return cu->id_to_block_map.Get(id);
buzbee2cfc6392012-05-07 14:51:40 -070048}
49
buzbeefa57c472012-11-21 12:06:18 -080050static llvm::Value* GetLLVMValue(CompilationUnit* cu, int s_reg)
buzbee2cfc6392012-05-07 14:51:40 -070051{
buzbeefa57c472012-11-21 12:06:18 -080052 return reinterpret_cast<llvm::Value*>(GrowableListGetElement(&cu->llvm_values, s_reg));
buzbee2cfc6392012-05-07 14:51:40 -070053}
54
buzbee26f10ee2012-12-21 11:16:29 -080055static void SetVregOnValue(CompilationUnit* cu, llvm::Value* val, int s_reg)
56{
57 // Set vreg for debugging
Ian Rogers76ae4fe2013-02-27 16:03:41 -080058 compiler_llvm::IntrinsicHelper::IntrinsicId id =
59 compiler_llvm::IntrinsicHelper::SetVReg;
TDYa127dc5daa02013-01-09 21:31:37 +080060 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(id);
61 int v_reg = SRegToVReg(cu, s_reg);
62 llvm::Value* table_slot = cu->irb->getInt32(v_reg);
63 llvm::Value* args[] = { table_slot, val };
64 cu->irb->CreateCall(func, args);
buzbee26f10ee2012-12-21 11:16:29 -080065}
66
buzbee2cfc6392012-05-07 14:51:40 -070067// Replace the placeholder value with the real definition
buzbee26f10ee2012-12-21 11:16:29 -080068static void DefineValueOnly(CompilationUnit* cu, llvm::Value* val, int s_reg)
buzbee2cfc6392012-05-07 14:51:40 -070069{
buzbeefa57c472012-11-21 12:06:18 -080070 llvm::Value* placeholder = GetLLVMValue(cu, s_reg);
buzbee9a2487f2012-07-26 14:01:13 -070071 if (placeholder == NULL) {
72 // This can happen on instruction rewrite on verification failure
Bill Buzbeec9f40dd2012-08-15 11:35:25 -070073 LOG(WARNING) << "Null placeholder";
buzbee9a2487f2012-07-26 14:01:13 -070074 return;
75 }
buzbee2cfc6392012-05-07 14:51:40 -070076 placeholder->replaceAllUsesWith(val);
77 val->takeName(placeholder);
buzbeefa57c472012-11-21 12:06:18 -080078 cu->llvm_values.elem_list[s_reg] = reinterpret_cast<uintptr_t>(val);
buzbee4be777b2012-07-12 14:38:18 -070079 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(placeholder);
80 DCHECK(inst != NULL);
81 inst->eraseFromParent();
TDYa1278e950c12012-11-02 09:58:19 -070082
buzbee26f10ee2012-12-21 11:16:29 -080083}
84
85static void DefineValue(CompilationUnit* cu, llvm::Value* val, int s_reg)
86{
87 DefineValueOnly(cu, val, s_reg);
88 SetVregOnValue(cu, val, s_reg);
buzbee2cfc6392012-05-07 14:51:40 -070089}
90
buzbeefa57c472012-11-21 12:06:18 -080091static llvm::Type* LlvmTypeFromLocRec(CompilationUnit* cu, RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -070092{
93 llvm::Type* res = NULL;
94 if (loc.wide) {
95 if (loc.fp)
buzbeefa57c472012-11-21 12:06:18 -080096 res = cu->irb->getDoubleTy();
buzbee2cfc6392012-05-07 14:51:40 -070097 else
buzbeefa57c472012-11-21 12:06:18 -080098 res = cu->irb->getInt64Ty();
buzbee2cfc6392012-05-07 14:51:40 -070099 } else {
100 if (loc.fp) {
buzbeefa57c472012-11-21 12:06:18 -0800101 res = cu->irb->getFloatTy();
buzbee2cfc6392012-05-07 14:51:40 -0700102 } else {
103 if (loc.ref)
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800104 res = cu->irb->getJObjectTy();
buzbee2cfc6392012-05-07 14:51:40 -0700105 else
buzbeefa57c472012-11-21 12:06:18 -0800106 res = cu->irb->getInt32Ty();
buzbee2cfc6392012-05-07 14:51:40 -0700107 }
108 }
109 return res;
110}
111
buzbeead8f15e2012-06-18 14:49:45 -0700112/* Create an in-memory RegLocation from an llvm Value. */
buzbeefa57c472012-11-21 12:06:18 -0800113static void CreateLocFromValue(CompilationUnit* cu, llvm::Value* val)
buzbeead8f15e2012-06-18 14:49:45 -0700114{
115 // NOTE: llvm takes shortcuts with c_str() - get to std::string firstt
116 std::string s(val->getName().str());
buzbeefa57c472012-11-21 12:06:18 -0800117 const char* val_name = s.c_str();
118 SafeMap<llvm::Value*, RegLocation>::iterator it = cu->loc_map.find(val);
119 DCHECK(it == cu->loc_map.end()) << " - already defined: " << val_name;
120 int base_sreg = INVALID_SREG;
buzbeead8f15e2012-06-18 14:49:45 -0700121 int subscript = -1;
buzbeefa57c472012-11-21 12:06:18 -0800122 sscanf(val_name, "v%d_%d", &base_sreg, &subscript);
123 if ((base_sreg == INVALID_SREG) && (!strcmp(val_name, "method"))) {
124 base_sreg = SSA_METHOD_BASEREG;
buzbeead8f15e2012-06-18 14:49:45 -0700125 subscript = 0;
126 }
buzbeefa57c472012-11-21 12:06:18 -0800127 DCHECK_NE(base_sreg, INVALID_SREG);
buzbeead8f15e2012-06-18 14:49:45 -0700128 DCHECK_NE(subscript, -1);
129 // TODO: redo during C++'ification
130 RegLocation loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, INVALID_REG,
131 INVALID_REG, INVALID_SREG, INVALID_SREG};
132 llvm::Type* ty = val->getType();
buzbeefa57c472012-11-21 12:06:18 -0800133 loc.wide = ((ty == cu->irb->getInt64Ty()) ||
134 (ty == cu->irb->getDoubleTy()));
buzbeead8f15e2012-06-18 14:49:45 -0700135 loc.defined = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700136 loc.home = false; // May change during promotion
buzbeefa57c472012-11-21 12:06:18 -0800137 loc.s_reg_low = base_sreg;
138 loc.orig_sreg = cu->loc_map.size();
139 PromotionMap p_map = cu->promotion_map[base_sreg];
140 if (ty == cu->irb->getFloatTy()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700141 loc.fp = true;
buzbeefa57c472012-11-21 12:06:18 -0800142 if (p_map.fp_location == kLocPhysReg) {
143 loc.low_reg = p_map.FpReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700144 loc.location = kLocPhysReg;
145 loc.home = true;
146 }
buzbeefa57c472012-11-21 12:06:18 -0800147 } else if (ty == cu->irb->getDoubleTy()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700148 loc.fp = true;
buzbeefa57c472012-11-21 12:06:18 -0800149 PromotionMap p_map_high = cu->promotion_map[base_sreg + 1];
150 if ((p_map.fp_location == kLocPhysReg) &&
151 (p_map_high.fp_location == kLocPhysReg) &&
152 ((p_map.FpReg & 0x1) == 0) &&
153 (p_map.FpReg + 1 == p_map_high.FpReg)) {
154 loc.low_reg = p_map.FpReg;
155 loc.high_reg = p_map_high.FpReg;
buzbeeca7a5e42012-08-20 11:12:18 -0700156 loc.location = kLocPhysReg;
157 loc.home = true;
158 }
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800159 } else if (ty == cu->irb->getJObjectTy()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700160 loc.ref = true;
buzbeefa57c472012-11-21 12:06:18 -0800161 if (p_map.core_location == kLocPhysReg) {
162 loc.low_reg = p_map.core_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700163 loc.location = kLocPhysReg;
164 loc.home = true;
165 }
buzbeefa57c472012-11-21 12:06:18 -0800166 } else if (ty == cu->irb->getInt64Ty()) {
buzbeeca7a5e42012-08-20 11:12:18 -0700167 loc.core = true;
buzbeefa57c472012-11-21 12:06:18 -0800168 PromotionMap p_map_high = cu->promotion_map[base_sreg + 1];
169 if ((p_map.core_location == kLocPhysReg) &&
170 (p_map_high.core_location == kLocPhysReg)) {
171 loc.low_reg = p_map.core_reg;
172 loc.high_reg = p_map_high.core_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700173 loc.location = kLocPhysReg;
174 loc.home = true;
175 }
176 } else {
177 loc.core = true;
buzbeefa57c472012-11-21 12:06:18 -0800178 if (p_map.core_location == kLocPhysReg) {
179 loc.low_reg = p_map.core_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700180 loc.location = kLocPhysReg;
181 loc.home = true;
182 }
183 }
184
buzbeefa57c472012-11-21 12:06:18 -0800185 if (cu->verbose && loc.home) {
buzbeeca7a5e42012-08-20 11:12:18 -0700186 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -0800187 LOG(INFO) << "Promoted wide " << s << " to regs " << loc.low_reg << "/" << loc.high_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700188 } else {
buzbeefa57c472012-11-21 12:06:18 -0800189 LOG(INFO) << "Promoted " << s << " to reg " << loc.low_reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700190 }
191 }
buzbeefa57c472012-11-21 12:06:18 -0800192 cu->loc_map.Put(val, loc);
buzbeead8f15e2012-06-18 14:49:45 -0700193}
buzbeeaad94382012-11-21 07:40:50 -0800194
buzbeefa57c472012-11-21 12:06:18 -0800195static void InitIR(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -0700196{
buzbeefa57c472012-11-21 12:06:18 -0800197 LLVMInfo* llvm_info = cu->llvm_info;
198 if (llvm_info == NULL) {
199 CompilerTls* tls = cu->compiler->GetTls();
buzbee4df2bbd2012-10-11 14:46:06 -0700200 CHECK(tls != NULL);
buzbeefa57c472012-11-21 12:06:18 -0800201 llvm_info = static_cast<LLVMInfo*>(tls->GetLLVMInfo());
202 if (llvm_info == NULL) {
203 llvm_info = new LLVMInfo();
204 tls->SetLLVMInfo(llvm_info);
buzbee4df2bbd2012-10-11 14:46:06 -0700205 }
206 }
buzbeefa57c472012-11-21 12:06:18 -0800207 cu->context = llvm_info->GetLLVMContext();
208 cu->module = llvm_info->GetLLVMModule();
209 cu->intrinsic_helper = llvm_info->GetIntrinsicHelper();
210 cu->irb = llvm_info->GetIRBuilder();
buzbee2cfc6392012-05-07 14:51:40 -0700211}
212
buzbeefa57c472012-11-21 12:06:18 -0800213static const char* LlvmSSAName(CompilationUnit* cu, int ssa_reg) {
214 return GET_ELEM_N(cu->ssa_strings, char*, ssa_reg);
buzbee2cfc6392012-05-07 14:51:40 -0700215}
216
buzbeefa57c472012-11-21 12:06:18 -0800217llvm::BasicBlock* FindCaseTarget(CompilationUnit* cu, uint32_t vaddr)
buzbeef58c12c2012-07-03 15:06:29 -0700218{
buzbeefa57c472012-11-21 12:06:18 -0800219 BasicBlock* bb = FindBlock(cu, vaddr);
buzbeef58c12c2012-07-03 15:06:29 -0700220 DCHECK(bb != NULL);
buzbeefa57c472012-11-21 12:06:18 -0800221 return GetLLVMBlock(cu, bb->id);
buzbeef58c12c2012-07-03 15:06:29 -0700222}
223
buzbeefa57c472012-11-21 12:06:18 -0800224static void ConvertPackedSwitch(CompilationUnit* cu, BasicBlock* bb,
225 int32_t table_offset, RegLocation rl_src)
buzbeef58c12c2012-07-03 15:06:29 -0700226{
227 const Instruction::PackedSwitchPayload* payload =
228 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
buzbeefa57c472012-11-21 12:06:18 -0800229 cu->insns + cu->current_dalvik_offset + table_offset);
buzbeef58c12c2012-07-03 15:06:29 -0700230
buzbeefa57c472012-11-21 12:06:18 -0800231 llvm::Value* value = GetLLVMValue(cu, rl_src.orig_sreg);
buzbeef58c12c2012-07-03 15:06:29 -0700232
233 llvm::SwitchInst* sw =
buzbeefa57c472012-11-21 12:06:18 -0800234 cu->irb->CreateSwitch(value, GetLLVMBlock(cu, bb->fall_through->id),
buzbeef58c12c2012-07-03 15:06:29 -0700235 payload->case_count);
236
237 for (uint16_t i = 0; i < payload->case_count; ++i) {
buzbeefa57c472012-11-21 12:06:18 -0800238 llvm::BasicBlock* llvm_bb =
239 FindCaseTarget(cu, cu->current_dalvik_offset + payload->targets[i]);
240 sw->addCase(cu->irb->getInt32(payload->first_key + i), llvm_bb);
buzbeef58c12c2012-07-03 15:06:29 -0700241 }
buzbeefa57c472012-11-21 12:06:18 -0800242 llvm::MDNode* switch_node =
243 llvm::MDNode::get(*cu->context, cu->irb->getInt32(table_offset));
244 sw->setMetadata("SwitchTable", switch_node);
buzbeef58c12c2012-07-03 15:06:29 -0700245 bb->taken = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800246 bb->fall_through = NULL;
buzbeef58c12c2012-07-03 15:06:29 -0700247}
248
buzbeefa57c472012-11-21 12:06:18 -0800249static void ConvertSparseSwitch(CompilationUnit* cu, BasicBlock* bb,
250 int32_t table_offset, RegLocation rl_src)
buzbeea1da8a52012-07-09 14:00:21 -0700251{
252 const Instruction::SparseSwitchPayload* payload =
253 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
buzbeefa57c472012-11-21 12:06:18 -0800254 cu->insns + cu->current_dalvik_offset + table_offset);
buzbeea1da8a52012-07-09 14:00:21 -0700255
256 const int32_t* keys = payload->GetKeys();
257 const int32_t* targets = payload->GetTargets();
258
buzbeefa57c472012-11-21 12:06:18 -0800259 llvm::Value* value = GetLLVMValue(cu, rl_src.orig_sreg);
buzbeea1da8a52012-07-09 14:00:21 -0700260
261 llvm::SwitchInst* sw =
buzbeefa57c472012-11-21 12:06:18 -0800262 cu->irb->CreateSwitch(value, GetLLVMBlock(cu, bb->fall_through->id),
buzbeea1da8a52012-07-09 14:00:21 -0700263 payload->case_count);
264
265 for (size_t i = 0; i < payload->case_count; ++i) {
buzbeefa57c472012-11-21 12:06:18 -0800266 llvm::BasicBlock* llvm_bb =
267 FindCaseTarget(cu, cu->current_dalvik_offset + targets[i]);
268 sw->addCase(cu->irb->getInt32(keys[i]), llvm_bb);
buzbeea1da8a52012-07-09 14:00:21 -0700269 }
buzbeefa57c472012-11-21 12:06:18 -0800270 llvm::MDNode* switch_node =
271 llvm::MDNode::get(*cu->context, cu->irb->getInt32(table_offset));
272 sw->setMetadata("SwitchTable", switch_node);
buzbeea1da8a52012-07-09 14:00:21 -0700273 bb->taken = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800274 bb->fall_through = NULL;
buzbeea1da8a52012-07-09 14:00:21 -0700275}
276
buzbeefa57c472012-11-21 12:06:18 -0800277static void ConvertSget(CompilationUnit* cu, int32_t field_index,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800278 compiler_llvm::IntrinsicHelper::IntrinsicId id, RegLocation rl_dest)
buzbee4f1181f2012-06-22 13:52:12 -0700279{
buzbeefa57c472012-11-21 12:06:18 -0800280 llvm::Constant* field_idx = cu->irb->getInt32(field_index);
281 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
282 llvm::Value* res = cu->irb->CreateCall(intr, field_idx);
283 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700284}
285
buzbeefa57c472012-11-21 12:06:18 -0800286static void ConvertSput(CompilationUnit* cu, int32_t field_index,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800287 compiler_llvm::IntrinsicHelper::IntrinsicId id, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700288{
289 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800290 args.push_back(cu->irb->getInt32(field_index));
291 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
292 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
293 cu->irb->CreateCall(intr, args);
buzbee4f1181f2012-06-22 13:52:12 -0700294}
295
buzbeefa57c472012-11-21 12:06:18 -0800296static void ConvertFillArrayData(CompilationUnit* cu, int32_t offset, RegLocation rl_array)
buzbee101305f2012-06-28 18:00:56 -0700297{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800298 compiler_llvm::IntrinsicHelper::IntrinsicId id;
299 id = compiler_llvm::IntrinsicHelper::HLFillArrayData;
buzbee101305f2012-06-28 18:00:56 -0700300 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800301 args.push_back(cu->irb->getInt32(offset));
302 args.push_back(GetLLVMValue(cu, rl_array.orig_sreg));
303 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
304 cu->irb->CreateCall(intr, args);
buzbee101305f2012-06-28 18:00:56 -0700305}
306
buzbeefa57c472012-11-21 12:06:18 -0800307static llvm::Value* EmitConst(CompilationUnit* cu, llvm::ArrayRef<llvm::Value*> src,
buzbeeaad94382012-11-21 07:40:50 -0800308 RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -0700309{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800310 compiler_llvm::IntrinsicHelper::IntrinsicId id;
buzbee2cfc6392012-05-07 14:51:40 -0700311 if (loc.wide) {
312 if (loc.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800313 id = compiler_llvm::IntrinsicHelper::ConstDouble;
buzbee2cfc6392012-05-07 14:51:40 -0700314 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800315 id = compiler_llvm::IntrinsicHelper::ConstLong;
buzbee2cfc6392012-05-07 14:51:40 -0700316 }
317 } else {
318 if (loc.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800319 id = compiler_llvm::IntrinsicHelper::ConstFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700320 } else if (loc.ref) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800321 id = compiler_llvm::IntrinsicHelper::ConstObj;
buzbee2cfc6392012-05-07 14:51:40 -0700322 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800323 id = compiler_llvm::IntrinsicHelper::ConstInt;
buzbee2cfc6392012-05-07 14:51:40 -0700324 }
325 }
buzbeefa57c472012-11-21 12:06:18 -0800326 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
327 return cu->irb->CreateCall(intr, src);
buzbee2cfc6392012-05-07 14:51:40 -0700328}
buzbeeb03f4872012-06-11 15:22:11 -0700329
buzbeefa57c472012-11-21 12:06:18 -0800330static void EmitPopShadowFrame(CompilationUnit* cu)
buzbeeb03f4872012-06-11 15:22:11 -0700331{
buzbeefa57c472012-11-21 12:06:18 -0800332 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800333 compiler_llvm::IntrinsicHelper::PopShadowFrame);
buzbeefa57c472012-11-21 12:06:18 -0800334 cu->irb->CreateCall(intr);
buzbeeb03f4872012-06-11 15:22:11 -0700335}
336
buzbeefa57c472012-11-21 12:06:18 -0800337static llvm::Value* EmitCopy(CompilationUnit* cu, llvm::ArrayRef<llvm::Value*> src,
buzbeeaad94382012-11-21 07:40:50 -0800338 RegLocation loc)
buzbee2cfc6392012-05-07 14:51:40 -0700339{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800340 compiler_llvm::IntrinsicHelper::IntrinsicId id;
buzbee2cfc6392012-05-07 14:51:40 -0700341 if (loc.wide) {
342 if (loc.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800343 id = compiler_llvm::IntrinsicHelper::CopyDouble;
buzbee2cfc6392012-05-07 14:51:40 -0700344 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800345 id = compiler_llvm::IntrinsicHelper::CopyLong;
buzbee2cfc6392012-05-07 14:51:40 -0700346 }
347 } else {
348 if (loc.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800349 id = compiler_llvm::IntrinsicHelper::CopyFloat;
buzbee4f1181f2012-06-22 13:52:12 -0700350 } else if (loc.ref) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800351 id = compiler_llvm::IntrinsicHelper::CopyObj;
buzbee2cfc6392012-05-07 14:51:40 -0700352 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800353 id = compiler_llvm::IntrinsicHelper::CopyInt;
buzbee2cfc6392012-05-07 14:51:40 -0700354 }
355 }
buzbeefa57c472012-11-21 12:06:18 -0800356 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
357 return cu->irb->CreateCall(intr, src);
buzbee2cfc6392012-05-07 14:51:40 -0700358}
359
buzbeefa57c472012-11-21 12:06:18 -0800360static void ConvertMoveException(CompilationUnit* cu, RegLocation rl_dest)
buzbee32412962012-06-26 16:27:56 -0700361{
buzbeefa57c472012-11-21 12:06:18 -0800362 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800363 compiler_llvm::IntrinsicHelper::GetException);
buzbeefa57c472012-11-21 12:06:18 -0800364 llvm::Value* res = cu->irb->CreateCall(func);
365 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee32412962012-06-26 16:27:56 -0700366}
367
buzbeefa57c472012-11-21 12:06:18 -0800368static void ConvertThrow(CompilationUnit* cu, RegLocation rl_src)
buzbee32412962012-06-26 16:27:56 -0700369{
buzbeefa57c472012-11-21 12:06:18 -0800370 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
371 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800372 compiler_llvm::IntrinsicHelper::HLThrowException);
buzbeefa57c472012-11-21 12:06:18 -0800373 cu->irb->CreateCall(func, src);
buzbee32412962012-06-26 16:27:56 -0700374}
375
buzbeefa57c472012-11-21 12:06:18 -0800376static void ConvertMonitorEnterExit(CompilationUnit* cu, int opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800377 compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800378 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(id);
384 cu->irb->CreateCall(func, args);
buzbee8fa0fda2012-06-27 15:44:52 -0700385}
386
buzbeefa57c472012-11-21 12:06:18 -0800387static void ConvertArrayLength(CompilationUnit* cu, int opt_flags,
388 RegLocation rl_dest, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700389{
390 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800391 args.push_back(cu->irb->getInt32(opt_flags));
392 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
393 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800394 compiler_llvm::IntrinsicHelper::OptArrayLength);
buzbeefa57c472012-11-21 12:06:18 -0800395 llvm::Value* res = cu->irb->CreateCall(func, args);
396 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700397}
398
buzbeefa57c472012-11-21 12:06:18 -0800399static void EmitSuspendCheck(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -0700400{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800401 compiler_llvm::IntrinsicHelper::IntrinsicId id =
402 compiler_llvm::IntrinsicHelper::CheckSuspend;
buzbeefa57c472012-11-21 12:06:18 -0800403 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
404 cu->irb->CreateCall(intr);
buzbee2cfc6392012-05-07 14:51:40 -0700405}
406
buzbeefa57c472012-11-21 12:06:18 -0800407static llvm::Value* ConvertCompare(CompilationUnit* cu, ConditionCode cc,
buzbeeaad94382012-11-21 07:40:50 -0800408 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700409{
410 llvm::Value* res = NULL;
buzbee76592632012-06-29 15:18:35 -0700411 DCHECK_EQ(src1->getType(), src2->getType());
buzbee2cfc6392012-05-07 14:51:40 -0700412 switch(cc) {
buzbeefa57c472012-11-21 12:06:18 -0800413 case kCondEq: res = cu->irb->CreateICmpEQ(src1, src2); break;
414 case kCondNe: res = cu->irb->CreateICmpNE(src1, src2); break;
415 case kCondLt: res = cu->irb->CreateICmpSLT(src1, src2); break;
416 case kCondGe: res = cu->irb->CreateICmpSGE(src1, src2); break;
417 case kCondGt: res = cu->irb->CreateICmpSGT(src1, src2); break;
418 case kCondLe: res = cu->irb->CreateICmpSLE(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700419 default: LOG(FATAL) << "Unexpected cc value " << cc;
420 }
421 return res;
422}
423
buzbeefa57c472012-11-21 12:06:18 -0800424static void ConvertCompareAndBranch(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
425 ConditionCode cc, RegLocation rl_src1, RegLocation rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -0700426{
buzbeefa57c472012-11-21 12:06:18 -0800427 if (bb->taken->start_offset <= mir->offset) {
428 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -0700429 }
buzbeefa57c472012-11-21 12:06:18 -0800430 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
431 llvm::Value* src2 = GetLLVMValue(cu, rl_src2.orig_sreg);
432 llvm::Value* cond_value = ConvertCompare(cu, cc, src1, src2);
433 cond_value->setName(StringPrintf("t%d", cu->temp_name++));
434 cu->irb->CreateCondBr(cond_value, GetLLVMBlock(cu, bb->taken->id),
435 GetLLVMBlock(cu, bb->fall_through->id));
buzbee6969d502012-06-15 16:40:31 -0700436 // Don't redo the fallthrough branch in the BB driver
buzbeefa57c472012-11-21 12:06:18 -0800437 bb->fall_through = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700438}
439
buzbeefa57c472012-11-21 12:06:18 -0800440static void ConvertCompareZeroAndBranch(CompilationUnit* cu, BasicBlock* bb,
441 MIR* mir, ConditionCode cc, RegLocation rl_src1)
buzbee2cfc6392012-05-07 14:51:40 -0700442{
buzbeefa57c472012-11-21 12:06:18 -0800443 if (bb->taken->start_offset <= mir->offset) {
444 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -0700445 }
buzbeefa57c472012-11-21 12:06:18 -0800446 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700447 llvm::Value* src2;
buzbeefa57c472012-11-21 12:06:18 -0800448 if (rl_src1.ref) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800449 src2 = cu->irb->getJNull();
buzbee2cfc6392012-05-07 14:51:40 -0700450 } else {
buzbeefa57c472012-11-21 12:06:18 -0800451 src2 = cu->irb->getInt32(0);
buzbee2cfc6392012-05-07 14:51:40 -0700452 }
buzbeefa57c472012-11-21 12:06:18 -0800453 llvm::Value* cond_value = ConvertCompare(cu, cc, src1, src2);
454 cu->irb->CreateCondBr(cond_value, GetLLVMBlock(cu, bb->taken->id),
455 GetLLVMBlock(cu, bb->fall_through->id));
buzbee6969d502012-06-15 16:40:31 -0700456 // Don't redo the fallthrough branch in the BB driver
buzbeefa57c472012-11-21 12:06:18 -0800457 bb->fall_through = NULL;
buzbee2cfc6392012-05-07 14:51:40 -0700458}
459
buzbeefa57c472012-11-21 12:06:18 -0800460static llvm::Value* GenDivModOp(CompilationUnit* cu, bool is_div, bool is_long,
buzbeeaad94382012-11-21 07:40:50 -0800461 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700462{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800463 compiler_llvm::IntrinsicHelper::IntrinsicId id;
buzbeefa57c472012-11-21 12:06:18 -0800464 if (is_long) {
465 if (is_div) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800466 id = compiler_llvm::IntrinsicHelper::DivLong;
buzbee2cfc6392012-05-07 14:51:40 -0700467 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800468 id = compiler_llvm::IntrinsicHelper::RemLong;
buzbee2cfc6392012-05-07 14:51:40 -0700469 }
Logan Chien554e6072012-07-23 20:00:01 -0700470 } else {
buzbeefa57c472012-11-21 12:06:18 -0800471 if (is_div) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800472 id = compiler_llvm::IntrinsicHelper::DivInt;
buzbee2cfc6392012-05-07 14:51:40 -0700473 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800474 id = compiler_llvm::IntrinsicHelper::RemInt;
Logan Chien554e6072012-07-23 20:00:01 -0700475 }
buzbee2cfc6392012-05-07 14:51:40 -0700476 }
buzbeefa57c472012-11-21 12:06:18 -0800477 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee2cfc6392012-05-07 14:51:40 -0700478 llvm::SmallVector<llvm::Value*, 2>args;
479 args.push_back(src1);
480 args.push_back(src2);
buzbeefa57c472012-11-21 12:06:18 -0800481 return cu->irb->CreateCall(intr, args);
buzbee2cfc6392012-05-07 14:51:40 -0700482}
483
buzbeefa57c472012-11-21 12:06:18 -0800484static llvm::Value* GenArithOp(CompilationUnit* cu, OpKind op, bool is_long,
buzbeeaad94382012-11-21 07:40:50 -0800485 llvm::Value* src1, llvm::Value* src2)
buzbee2cfc6392012-05-07 14:51:40 -0700486{
487 llvm::Value* res = NULL;
488 switch(op) {
buzbeefa57c472012-11-21 12:06:18 -0800489 case kOpAdd: res = cu->irb->CreateAdd(src1, src2); break;
490 case kOpSub: res = cu->irb->CreateSub(src1, src2); break;
491 case kOpRsub: res = cu->irb->CreateSub(src2, src1); break;
492 case kOpMul: res = cu->irb->CreateMul(src1, src2); break;
493 case kOpOr: res = cu->irb->CreateOr(src1, src2); break;
494 case kOpAnd: res = cu->irb->CreateAnd(src1, src2); break;
495 case kOpXor: res = cu->irb->CreateXor(src1, src2); break;
496 case kOpDiv: res = GenDivModOp(cu, true, is_long, src1, src2); break;
497 case kOpRem: res = GenDivModOp(cu, false, is_long, src1, src2); break;
498 case kOpLsl: res = cu->irb->CreateShl(src1, src2); break;
499 case kOpLsr: res = cu->irb->CreateLShr(src1, src2); break;
500 case kOpAsr: res = cu->irb->CreateAShr(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700501 default:
502 LOG(FATAL) << "Invalid op " << op;
503 }
504 return res;
505}
506
buzbeefa57c472012-11-21 12:06:18 -0800507static void ConvertFPArithOp(CompilationUnit* cu, OpKind op, RegLocation rl_dest,
508 RegLocation rl_src1, RegLocation rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -0700509{
buzbeefa57c472012-11-21 12:06:18 -0800510 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
511 llvm::Value* src2 = GetLLVMValue(cu, rl_src2.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700512 llvm::Value* res = NULL;
513 switch(op) {
buzbeefa57c472012-11-21 12:06:18 -0800514 case kOpAdd: res = cu->irb->CreateFAdd(src1, src2); break;
515 case kOpSub: res = cu->irb->CreateFSub(src1, src2); break;
516 case kOpMul: res = cu->irb->CreateFMul(src1, src2); break;
517 case kOpDiv: res = cu->irb->CreateFDiv(src1, src2); break;
518 case kOpRem: res = cu->irb->CreateFRem(src1, src2); break;
buzbee2cfc6392012-05-07 14:51:40 -0700519 default:
520 LOG(FATAL) << "Invalid op " << op;
521 }
buzbeefa57c472012-11-21 12:06:18 -0800522 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700523}
524
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800525static void ConvertShift(CompilationUnit* cu, compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800526 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee4f1181f2012-06-22 13:52:12 -0700527{
buzbeefa57c472012-11-21 12:06:18 -0800528 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee2a83e8f2012-07-13 16:42:30 -0700529 llvm::SmallVector<llvm::Value*, 2>args;
buzbeefa57c472012-11-21 12:06:18 -0800530 args.push_back(GetLLVMValue(cu, rl_src1.orig_sreg));
531 args.push_back(GetLLVMValue(cu, rl_src2.orig_sreg));
532 llvm::Value* res = cu->irb->CreateCall(intr, args);
533 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2a83e8f2012-07-13 16:42:30 -0700534}
535
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800536static void ConvertShiftLit(CompilationUnit* cu, compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800537 RegLocation rl_dest, RegLocation rl_src, int shift_amount)
buzbee2a83e8f2012-07-13 16:42:30 -0700538{
buzbeefa57c472012-11-21 12:06:18 -0800539 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee2a83e8f2012-07-13 16:42:30 -0700540 llvm::SmallVector<llvm::Value*, 2>args;
buzbeefa57c472012-11-21 12:06:18 -0800541 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
542 args.push_back(cu->irb->getInt32(shift_amount));
543 llvm::Value* res = cu->irb->CreateCall(intr, args);
544 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700545}
546
buzbeefa57c472012-11-21 12:06:18 -0800547static void ConvertArithOp(CompilationUnit* cu, OpKind op, RegLocation rl_dest,
548 RegLocation rl_src1, RegLocation rl_src2)
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 = GetLLVMValue(cu, rl_src2.orig_sreg);
buzbee4f4dfc72012-07-02 14:54:44 -0700552 DCHECK_EQ(src1->getType(), src2->getType());
buzbeefa57c472012-11-21 12:06:18 -0800553 llvm::Value* res = GenArithOp(cu, op, rl_dest.wide, src1, src2);
554 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700555}
556
buzbeefa57c472012-11-21 12:06:18 -0800557static void ConvertArithOpLit(CompilationUnit* cu, OpKind op, RegLocation rl_dest,
558 RegLocation rl_src1, int32_t imm)
buzbee2cfc6392012-05-07 14:51:40 -0700559{
buzbeefa57c472012-11-21 12:06:18 -0800560 llvm::Value* src1 = GetLLVMValue(cu, rl_src1.orig_sreg);
561 llvm::Value* src2 = cu->irb->getInt32(imm);
562 llvm::Value* res = GenArithOp(cu, op, rl_dest.wide, src1, src2);
563 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700564}
565
buzbee101305f2012-06-28 18:00:56 -0700566/*
567 * Process arguments for invoke. Note: this code is also used to
568 * collect and process arguments for NEW_FILLED_ARRAY and NEW_FILLED_ARRAY_RANGE.
569 * The requirements are similar.
570 */
buzbeefa57c472012-11-21 12:06:18 -0800571static void ConvertInvoke(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
572 InvokeType invoke_type, bool is_range, bool is_filled_new_array)
buzbee6969d502012-06-15 16:40:31 -0700573{
buzbee02031b12012-11-23 09:41:35 -0800574 Codegen* cg = cu->cg.get();
575 CallInfo* info = cg->NewMemCallInfo(cu, bb, mir, invoke_type, is_range);
buzbee6969d502012-06-15 16:40:31 -0700576 llvm::SmallVector<llvm::Value*, 10> args;
buzbeefa57c472012-11-21 12:06:18 -0800577 // Insert the invoke_type
578 args.push_back(cu->irb->getInt32(static_cast<int>(invoke_type)));
buzbee6969d502012-06-15 16:40:31 -0700579 // Insert the method_idx
buzbeefa57c472012-11-21 12:06:18 -0800580 args.push_back(cu->irb->getInt32(info->index));
buzbee6969d502012-06-15 16:40:31 -0700581 // Insert the optimization flags
buzbeefa57c472012-11-21 12:06:18 -0800582 args.push_back(cu->irb->getInt32(info->opt_flags));
buzbee6969d502012-06-15 16:40:31 -0700583 // Now, insert the actual arguments
buzbeefa57c472012-11-21 12:06:18 -0800584 for (int i = 0; i < info->num_arg_words;) {
585 llvm::Value* val = GetLLVMValue(cu, info->args[i].orig_sreg);
buzbee6969d502012-06-15 16:40:31 -0700586 args.push_back(val);
587 i += info->args[i].wide ? 2 : 1;
588 }
589 /*
590 * Choose the invoke return type based on actual usage. Note: may
591 * be different than shorty. For example, if a function return value
592 * is not used, we'll treat this as a void invoke.
593 */
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800594 compiler_llvm::IntrinsicHelper::IntrinsicId id;
buzbeefa57c472012-11-21 12:06:18 -0800595 if (is_filled_new_array) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800596 id = compiler_llvm::IntrinsicHelper::HLFilledNewArray;
buzbee101305f2012-06-28 18:00:56 -0700597 } else if (info->result.location == kLocInvalid) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800598 id = compiler_llvm::IntrinsicHelper::HLInvokeVoid;
buzbee6969d502012-06-15 16:40:31 -0700599 } else {
600 if (info->result.wide) {
601 if (info->result.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800602 id = compiler_llvm::IntrinsicHelper::HLInvokeDouble;
buzbee6969d502012-06-15 16:40:31 -0700603 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800604 id = compiler_llvm::IntrinsicHelper::HLInvokeLong;
buzbee6969d502012-06-15 16:40:31 -0700605 }
606 } else if (info->result.ref) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800607 id = compiler_llvm::IntrinsicHelper::HLInvokeObj;
buzbee6969d502012-06-15 16:40:31 -0700608 } else if (info->result.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800609 id = compiler_llvm::IntrinsicHelper::HLInvokeFloat;
buzbee6969d502012-06-15 16:40:31 -0700610 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800611 id = compiler_llvm::IntrinsicHelper::HLInvokeInt;
buzbee6969d502012-06-15 16:40:31 -0700612 }
613 }
buzbeefa57c472012-11-21 12:06:18 -0800614 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
615 llvm::Value* res = cu->irb->CreateCall(intr, args);
buzbee6969d502012-06-15 16:40:31 -0700616 if (info->result.location != kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800617 DefineValue(cu, res, info->result.orig_sreg);
buzbee6969d502012-06-15 16:40:31 -0700618 }
619}
620
buzbeefa57c472012-11-21 12:06:18 -0800621static void ConvertConstObject(CompilationUnit* cu, uint32_t idx,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800622 compiler_llvm::IntrinsicHelper::IntrinsicId id, RegLocation rl_dest)
buzbee6969d502012-06-15 16:40:31 -0700623{
buzbeefa57c472012-11-21 12:06:18 -0800624 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
625 llvm::Value* index = cu->irb->getInt32(idx);
626 llvm::Value* res = cu->irb->CreateCall(intr, index);
627 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee6969d502012-06-15 16:40:31 -0700628}
629
buzbeefa57c472012-11-21 12:06:18 -0800630static void ConvertCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee101305f2012-06-28 18:00:56 -0700631{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800632 compiler_llvm::IntrinsicHelper::IntrinsicId id;
633 id = compiler_llvm::IntrinsicHelper::HLCheckCast;
buzbeefa57c472012-11-21 12:06:18 -0800634 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee101305f2012-06-28 18:00:56 -0700635 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800636 args.push_back(cu->irb->getInt32(type_idx));
637 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
638 cu->irb->CreateCall(intr, args);
buzbee101305f2012-06-28 18:00:56 -0700639}
640
buzbeefa57c472012-11-21 12:06:18 -0800641static void ConvertNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee4f1181f2012-06-22 13:52:12 -0700642{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800643 compiler_llvm::IntrinsicHelper::IntrinsicId id;
644 id = compiler_llvm::IntrinsicHelper::NewInstance;
buzbeefa57c472012-11-21 12:06:18 -0800645 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
646 llvm::Value* index = cu->irb->getInt32(type_idx);
647 llvm::Value* res = cu->irb->CreateCall(intr, index);
648 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700649}
650
buzbeefa57c472012-11-21 12:06:18 -0800651static void ConvertNewArray(CompilationUnit* cu, uint32_t type_idx,
652 RegLocation rl_dest, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700653{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800654 compiler_llvm::IntrinsicHelper::IntrinsicId id;
655 id = compiler_llvm::IntrinsicHelper::NewArray;
buzbeefa57c472012-11-21 12:06:18 -0800656 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700657 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800658 args.push_back(cu->irb->getInt32(type_idx));
659 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
660 llvm::Value* res = cu->irb->CreateCall(intr, args);
661 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700662}
663
buzbeefa57c472012-11-21 12:06:18 -0800664static void ConvertAget(CompilationUnit* cu, int opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800665 compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800666 RegLocation rl_dest, RegLocation rl_array, RegLocation rl_index)
buzbee8fa0fda2012-06-27 15:44:52 -0700667{
668 llvm::SmallVector<llvm::Value*, 3> args;
buzbeefa57c472012-11-21 12:06:18 -0800669 args.push_back(cu->irb->getInt32(opt_flags));
670 args.push_back(GetLLVMValue(cu, rl_array.orig_sreg));
671 args.push_back(GetLLVMValue(cu, rl_index.orig_sreg));
672 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
673 llvm::Value* res = cu->irb->CreateCall(intr, args);
674 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700675}
676
buzbeefa57c472012-11-21 12:06:18 -0800677static void ConvertAput(CompilationUnit* cu, int opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800678 compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800679 RegLocation rl_src, RegLocation rl_array, RegLocation rl_index)
buzbee8fa0fda2012-06-27 15:44:52 -0700680{
681 llvm::SmallVector<llvm::Value*, 4> args;
buzbeefa57c472012-11-21 12:06:18 -0800682 args.push_back(cu->irb->getInt32(opt_flags));
683 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
684 args.push_back(GetLLVMValue(cu, rl_array.orig_sreg));
685 args.push_back(GetLLVMValue(cu, rl_index.orig_sreg));
686 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
687 cu->irb->CreateCall(intr, args);
buzbee8fa0fda2012-06-27 15:44:52 -0700688}
689
buzbeefa57c472012-11-21 12:06:18 -0800690static void ConvertIget(CompilationUnit* cu, int opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800691 compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800692 RegLocation rl_dest, RegLocation rl_obj, int field_index)
buzbee101305f2012-06-28 18:00:56 -0700693{
694 llvm::SmallVector<llvm::Value*, 3> args;
buzbeefa57c472012-11-21 12:06:18 -0800695 args.push_back(cu->irb->getInt32(opt_flags));
696 args.push_back(GetLLVMValue(cu, rl_obj.orig_sreg));
697 args.push_back(cu->irb->getInt32(field_index));
698 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
699 llvm::Value* res = cu->irb->CreateCall(intr, args);
700 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee101305f2012-06-28 18:00:56 -0700701}
702
buzbeefa57c472012-11-21 12:06:18 -0800703static void ConvertIput(CompilationUnit* cu, int opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800704 compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800705 RegLocation rl_src, RegLocation rl_obj, int field_index)
buzbee101305f2012-06-28 18:00:56 -0700706{
707 llvm::SmallVector<llvm::Value*, 4> args;
buzbeefa57c472012-11-21 12:06:18 -0800708 args.push_back(cu->irb->getInt32(opt_flags));
709 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
710 args.push_back(GetLLVMValue(cu, rl_obj.orig_sreg));
711 args.push_back(cu->irb->getInt32(field_index));
712 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
713 cu->irb->CreateCall(intr, args);
buzbee101305f2012-06-28 18:00:56 -0700714}
715
buzbeefa57c472012-11-21 12:06:18 -0800716static void ConvertInstanceOf(CompilationUnit* cu, uint32_t type_idx,
717 RegLocation rl_dest, RegLocation rl_src)
buzbee8fa0fda2012-06-27 15:44:52 -0700718{
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800719 compiler_llvm::IntrinsicHelper::IntrinsicId id;
720 id = compiler_llvm::IntrinsicHelper::InstanceOf;
buzbeefa57c472012-11-21 12:06:18 -0800721 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee8fa0fda2012-06-27 15:44:52 -0700722 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800723 args.push_back(cu->irb->getInt32(type_idx));
724 args.push_back(GetLLVMValue(cu, rl_src.orig_sreg));
725 llvm::Value* res = cu->irb->CreateCall(intr, args);
726 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee8fa0fda2012-06-27 15:44:52 -0700727}
728
buzbeefa57c472012-11-21 12:06:18 -0800729static void ConvertIntToLong(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee101305f2012-06-28 18:00:56 -0700730{
buzbeefa57c472012-11-21 12:06:18 -0800731 llvm::Value* res = cu->irb->CreateSExt(GetLLVMValue(cu, rl_src.orig_sreg),
732 cu->irb->getInt64Ty());
733 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee101305f2012-06-28 18:00:56 -0700734}
735
buzbeefa57c472012-11-21 12:06:18 -0800736static void ConvertLongToInt(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700737{
buzbeefa57c472012-11-21 12:06:18 -0800738 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
739 llvm::Value* res = cu->irb->CreateTrunc(src, cu->irb->getInt32Ty());
740 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700741}
742
buzbeefa57c472012-11-21 12:06:18 -0800743static void ConvertFloatToDouble(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700744{
buzbeefa57c472012-11-21 12:06:18 -0800745 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
746 llvm::Value* res = cu->irb->CreateFPExt(src, cu->irb->getDoubleTy());
747 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700748}
749
buzbeefa57c472012-11-21 12:06:18 -0800750static void ConvertDoubleToFloat(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700751{
buzbeefa57c472012-11-21 12:06:18 -0800752 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
753 llvm::Value* res = cu->irb->CreateFPTrunc(src, cu->irb->getFloatTy());
754 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700755}
756
buzbeefa57c472012-11-21 12:06:18 -0800757static void ConvertWideComparison(CompilationUnit* cu,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800758 compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800759 RegLocation rl_dest, RegLocation rl_src1,
760 RegLocation rl_src2)
buzbee76592632012-06-29 15:18:35 -0700761{
buzbeefa57c472012-11-21 12:06:18 -0800762 DCHECK_EQ(rl_src1.fp, rl_src2.fp);
763 DCHECK_EQ(rl_src1.wide, rl_src2.wide);
764 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700765 llvm::SmallVector<llvm::Value*, 2> args;
buzbeefa57c472012-11-21 12:06:18 -0800766 args.push_back(GetLLVMValue(cu, rl_src1.orig_sreg));
767 args.push_back(GetLLVMValue(cu, rl_src2.orig_sreg));
768 llvm::Value* res = cu->irb->CreateCall(intr, args);
769 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700770}
771
buzbeefa57c472012-11-21 12:06:18 -0800772static void ConvertIntNarrowing(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800773 compiler_llvm::IntrinsicHelper::IntrinsicId id)
buzbee101305f2012-06-28 18:00:56 -0700774{
buzbeefa57c472012-11-21 12:06:18 -0800775 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
buzbee76592632012-06-29 15:18:35 -0700776 llvm::Value* res =
buzbeefa57c472012-11-21 12:06:18 -0800777 cu->irb->CreateCall(intr, GetLLVMValue(cu, rl_src.orig_sreg));
778 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700779}
780
buzbeefa57c472012-11-21 12:06:18 -0800781static void ConvertNeg(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700782{
buzbeefa57c472012-11-21 12:06:18 -0800783 llvm::Value* res = cu->irb->CreateNeg(GetLLVMValue(cu, rl_src.orig_sreg));
784 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700785}
786
buzbeefa57c472012-11-21 12:06:18 -0800787static void ConvertIntToFP(CompilationUnit* cu, llvm::Type* ty, RegLocation rl_dest,
788 RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700789{
790 llvm::Value* res =
buzbeefa57c472012-11-21 12:06:18 -0800791 cu->irb->CreateSIToFP(GetLLVMValue(cu, rl_src.orig_sreg), ty);
792 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700793}
794
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800795static void ConvertFPToInt(CompilationUnit* cu, compiler_llvm::IntrinsicHelper::IntrinsicId id,
buzbeefa57c472012-11-21 12:06:18 -0800796 RegLocation rl_dest,
797 RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700798{
buzbeefa57c472012-11-21 12:06:18 -0800799 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
800 llvm::Value* res = cu->irb->CreateCall(intr, GetLLVMValue(cu, rl_src.orig_sreg));
801 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700802}
803
804
buzbeefa57c472012-11-21 12:06:18 -0800805static void ConvertNegFP(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700806{
807 llvm::Value* res =
buzbeefa57c472012-11-21 12:06:18 -0800808 cu->irb->CreateFNeg(GetLLVMValue(cu, rl_src.orig_sreg));
809 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee76592632012-06-29 15:18:35 -0700810}
811
buzbeefa57c472012-11-21 12:06:18 -0800812static void ConvertNot(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee76592632012-06-29 15:18:35 -0700813{
buzbeefa57c472012-11-21 12:06:18 -0800814 llvm::Value* src = GetLLVMValue(cu, rl_src.orig_sreg);
815 llvm::Value* res = cu->irb->CreateXor(src, static_cast<uint64_t>(-1));
816 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee101305f2012-06-28 18:00:56 -0700817}
818
buzbee2cfc6392012-05-07 14:51:40 -0700819/*
820 * Target-independent code generation. Use only high-level
821 * load/store utilities here, or target-dependent genXX() handlers
822 * when necessary.
823 */
buzbeefa57c472012-11-21 12:06:18 -0800824static bool ConvertMIRNode(CompilationUnit* cu, MIR* mir, BasicBlock* bb,
825 llvm::BasicBlock* llvm_bb, LIR* label_list)
buzbee2cfc6392012-05-07 14:51:40 -0700826{
827 bool res = false; // Assume success
buzbeefa57c472012-11-21 12:06:18 -0800828 RegLocation rl_src[3];
buzbee02031b12012-11-23 09:41:35 -0800829 RegLocation rl_dest = GetBadLoc();
buzbee2cfc6392012-05-07 14:51:40 -0700830 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800831 int op_val = opcode;
buzbee6969d502012-06-15 16:40:31 -0700832 uint32_t vB = mir->dalvikInsn.vB;
833 uint32_t vC = mir->dalvikInsn.vC;
buzbeefa57c472012-11-21 12:06:18 -0800834 int opt_flags = mir->optimization_flags;
buzbee6969d502012-06-15 16:40:31 -0700835
buzbeefa57c472012-11-21 12:06:18 -0800836 if (cu->verbose) {
837 if (op_val < kMirOpFirst) {
838 LOG(INFO) << ".. " << Instruction::Name(opcode) << " 0x" << std::hex << op_val;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700839 } else {
buzbeefa57c472012-11-21 12:06:18 -0800840 LOG(INFO) << extended_mir_op_names[op_val - kMirOpFirst] << " 0x" << std::hex << op_val;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700841 }
842 }
843
buzbee2cfc6392012-05-07 14:51:40 -0700844 /* Prep Src and Dest locations */
buzbeefa57c472012-11-21 12:06:18 -0800845 int next_sreg = 0;
846 int next_loc = 0;
847 int attrs = oat_data_flow_attributes[opcode];
buzbee02031b12012-11-23 09:41:35 -0800848 rl_src[0] = rl_src[1] = rl_src[2] = GetBadLoc();
buzbee2cfc6392012-05-07 14:51:40 -0700849 if (attrs & DF_UA) {
850 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800851 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
852 next_sreg+= 2;
buzbee2cfc6392012-05-07 14:51:40 -0700853 } else {
buzbeefa57c472012-11-21 12:06:18 -0800854 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
855 next_sreg++;
buzbee2cfc6392012-05-07 14:51:40 -0700856 }
857 }
858 if (attrs & DF_UB) {
859 if (attrs & DF_B_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800860 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
861 next_sreg+= 2;
buzbee2cfc6392012-05-07 14:51:40 -0700862 } else {
buzbeefa57c472012-11-21 12:06:18 -0800863 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
864 next_sreg++;
buzbee2cfc6392012-05-07 14:51:40 -0700865 }
866 }
867 if (attrs & DF_UC) {
868 if (attrs & DF_C_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800869 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700870 } else {
buzbeefa57c472012-11-21 12:06:18 -0800871 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700872 }
873 }
874 if (attrs & DF_DA) {
875 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800876 rl_dest = GetDestWide(cu, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700877 } else {
buzbeefa57c472012-11-21 12:06:18 -0800878 rl_dest = GetDest(cu, mir);
buzbee2cfc6392012-05-07 14:51:40 -0700879 }
880 }
881
882 switch (opcode) {
883 case Instruction::NOP:
884 break;
885
886 case Instruction::MOVE:
887 case Instruction::MOVE_OBJECT:
888 case Instruction::MOVE_16:
889 case Instruction::MOVE_OBJECT_16:
buzbee76592632012-06-29 15:18:35 -0700890 case Instruction::MOVE_OBJECT_FROM16:
buzbee2cfc6392012-05-07 14:51:40 -0700891 case Instruction::MOVE_FROM16:
892 case Instruction::MOVE_WIDE:
893 case Instruction::MOVE_WIDE_16:
894 case Instruction::MOVE_WIDE_FROM16: {
895 /*
896 * Moves/copies are meaningless in pure SSA register form,
897 * but we need to preserve them for the conversion back into
898 * MIR (at least until we stop using the Dalvik register maps).
899 * Insert a dummy intrinsic copy call, which will be recognized
900 * by the quick path and removed by the portable path.
901 */
buzbeefa57c472012-11-21 12:06:18 -0800902 llvm::Value* src = GetLLVMValue(cu, rl_src[0].orig_sreg);
903 llvm::Value* res = EmitCopy(cu, src, rl_dest);
904 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700905 }
906 break;
907
908 case Instruction::CONST:
909 case Instruction::CONST_4:
910 case Instruction::CONST_16: {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800911 llvm::Constant* imm_value = cu->irb->getJInt(vB);
buzbeefa57c472012-11-21 12:06:18 -0800912 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_WIDE_16:
918 case Instruction::CONST_WIDE_32: {
buzbee76592632012-06-29 15:18:35 -0700919 // Sign extend to 64 bits
920 int64_t imm = static_cast<int32_t>(vB);
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800921 llvm::Constant* imm_value = cu->irb->getJLong(imm);
buzbeefa57c472012-11-21 12:06:18 -0800922 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
923 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700924 }
925 break;
926
927 case Instruction::CONST_HIGH16: {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800928 llvm::Constant* imm_value = cu->irb->getJInt(vB << 16);
buzbeefa57c472012-11-21 12:06:18 -0800929 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
930 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee2cfc6392012-05-07 14:51:40 -0700931 }
932 break;
933
934 case Instruction::CONST_WIDE: {
buzbeefa57c472012-11-21 12:06:18 -0800935 llvm::Constant* imm_value =
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800936 cu->irb->getJLong(mir->dalvikInsn.vB_wide);
buzbeefa57c472012-11-21 12:06:18 -0800937 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
938 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700939 }
940 break;
buzbee2cfc6392012-05-07 14:51:40 -0700941 case Instruction::CONST_WIDE_HIGH16: {
buzbee6969d502012-06-15 16:40:31 -0700942 int64_t imm = static_cast<int64_t>(vB) << 48;
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800943 llvm::Constant* imm_value = cu->irb->getJLong(imm);
buzbeefa57c472012-11-21 12:06:18 -0800944 llvm::Value* res = EmitConst(cu, imm_value, rl_dest);
945 DefineValue(cu, res, rl_dest.orig_sreg);
buzbee4f1181f2012-06-22 13:52:12 -0700946 }
947 break;
948
buzbee8fa0fda2012-06-27 15:44:52 -0700949 case Instruction::SPUT_OBJECT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800950 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputObject,
buzbeefa57c472012-11-21 12:06:18 -0800951 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700952 break;
953 case Instruction::SPUT:
buzbeefa57c472012-11-21 12:06:18 -0800954 if (rl_src[0].fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800955 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputFloat,
buzbeefa57c472012-11-21 12:06:18 -0800956 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700957 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800958 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSput, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700959 }
960 break;
961 case Instruction::SPUT_BOOLEAN:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800962 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputBoolean,
buzbeefa57c472012-11-21 12:06:18 -0800963 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700964 break;
965 case Instruction::SPUT_BYTE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800966 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputByte, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700967 break;
968 case Instruction::SPUT_CHAR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800969 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputChar, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700970 break;
971 case Instruction::SPUT_SHORT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800972 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputShort, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700973 break;
974 case Instruction::SPUT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800975 if (rl_src[0].fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800976 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputDouble,
buzbeefa57c472012-11-21 12:06:18 -0800977 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700978 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800979 ConvertSput(cu, vB, compiler_llvm::IntrinsicHelper::HLSputWide,
buzbeefa57c472012-11-21 12:06:18 -0800980 rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -0700981 }
982 break;
983
984 case Instruction::SGET_OBJECT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800985 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetObject, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700986 break;
987 case Instruction::SGET:
buzbeefa57c472012-11-21 12:06:18 -0800988 if (rl_dest.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800989 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetFloat, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700990 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800991 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSget, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700992 }
993 break;
994 case Instruction::SGET_BOOLEAN:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800995 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetBoolean, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700996 break;
997 case Instruction::SGET_BYTE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800998 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetByte, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -0700999 break;
1000 case Instruction::SGET_CHAR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001001 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetChar, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -07001002 break;
1003 case Instruction::SGET_SHORT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001004 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetShort, rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -07001005 break;
1006 case Instruction::SGET_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001007 if (rl_dest.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001008 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetDouble,
buzbeefa57c472012-11-21 12:06:18 -08001009 rl_dest);
buzbee8fa0fda2012-06-27 15:44:52 -07001010 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001011 ConvertSget(cu, vB, compiler_llvm::IntrinsicHelper::HLSgetWide, rl_dest);
buzbee4f1181f2012-06-22 13:52:12 -07001012 }
1013 break;
buzbee2cfc6392012-05-07 14:51:40 -07001014
1015 case Instruction::RETURN_WIDE:
1016 case Instruction::RETURN:
1017 case Instruction::RETURN_OBJECT: {
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->CreateRet(GetLLVMValue(cu, rl_src[0].orig_sreg));
buzbeebbdd0532013-02-07 09:33:02 -08001023 DCHECK(bb->terminated_by_return);
buzbee2cfc6392012-05-07 14:51:40 -07001024 }
1025 break;
1026
1027 case Instruction::RETURN_VOID: {
buzbeefa57c472012-11-21 12:06:18 -08001028 if (!(cu->attrs & METHOD_IS_LEAF)) {
1029 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001030 }
buzbeefa57c472012-11-21 12:06:18 -08001031 EmitPopShadowFrame(cu);
1032 cu->irb->CreateRetVoid();
buzbeebbdd0532013-02-07 09:33:02 -08001033 DCHECK(bb->terminated_by_return);
buzbee2cfc6392012-05-07 14:51:40 -07001034 }
1035 break;
1036
1037 case Instruction::IF_EQ:
buzbeefa57c472012-11-21 12:06:18 -08001038 ConvertCompareAndBranch(cu, bb, mir, kCondEq, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001039 break;
1040 case Instruction::IF_NE:
buzbeefa57c472012-11-21 12:06:18 -08001041 ConvertCompareAndBranch(cu, bb, mir, kCondNe, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001042 break;
1043 case Instruction::IF_LT:
buzbeefa57c472012-11-21 12:06:18 -08001044 ConvertCompareAndBranch(cu, bb, mir, kCondLt, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001045 break;
1046 case Instruction::IF_GE:
buzbeefa57c472012-11-21 12:06:18 -08001047 ConvertCompareAndBranch(cu, bb, mir, kCondGe, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001048 break;
1049 case Instruction::IF_GT:
buzbeefa57c472012-11-21 12:06:18 -08001050 ConvertCompareAndBranch(cu, bb, mir, kCondGt, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001051 break;
1052 case Instruction::IF_LE:
buzbeefa57c472012-11-21 12:06:18 -08001053 ConvertCompareAndBranch(cu, bb, mir, kCondLe, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001054 break;
1055 case Instruction::IF_EQZ:
buzbeefa57c472012-11-21 12:06:18 -08001056 ConvertCompareZeroAndBranch(cu, bb, mir, kCondEq, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001057 break;
1058 case Instruction::IF_NEZ:
buzbeefa57c472012-11-21 12:06:18 -08001059 ConvertCompareZeroAndBranch(cu, bb, mir, kCondNe, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001060 break;
1061 case Instruction::IF_LTZ:
buzbeefa57c472012-11-21 12:06:18 -08001062 ConvertCompareZeroAndBranch(cu, bb, mir, kCondLt, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001063 break;
1064 case Instruction::IF_GEZ:
buzbeefa57c472012-11-21 12:06:18 -08001065 ConvertCompareZeroAndBranch(cu, bb, mir, kCondGe, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001066 break;
1067 case Instruction::IF_GTZ:
buzbeefa57c472012-11-21 12:06:18 -08001068 ConvertCompareZeroAndBranch(cu, bb, mir, kCondGt, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001069 break;
1070 case Instruction::IF_LEZ:
buzbeefa57c472012-11-21 12:06:18 -08001071 ConvertCompareZeroAndBranch(cu, bb, mir, kCondLe, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001072 break;
1073
1074 case Instruction::GOTO:
1075 case Instruction::GOTO_16:
1076 case Instruction::GOTO_32: {
buzbeefa57c472012-11-21 12:06:18 -08001077 if (bb->taken->start_offset <= bb->start_offset) {
1078 EmitSuspendCheck(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001079 }
buzbeefa57c472012-11-21 12:06:18 -08001080 cu->irb->CreateBr(GetLLVMBlock(cu, bb->taken->id));
buzbee2cfc6392012-05-07 14:51:40 -07001081 }
1082 break;
1083
1084 case Instruction::ADD_LONG:
1085 case Instruction::ADD_LONG_2ADDR:
1086 case Instruction::ADD_INT:
1087 case Instruction::ADD_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001088 ConvertArithOp(cu, kOpAdd, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001089 break;
1090 case Instruction::SUB_LONG:
1091 case Instruction::SUB_LONG_2ADDR:
1092 case Instruction::SUB_INT:
1093 case Instruction::SUB_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001094 ConvertArithOp(cu, kOpSub, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001095 break;
1096 case Instruction::MUL_LONG:
1097 case Instruction::MUL_LONG_2ADDR:
1098 case Instruction::MUL_INT:
1099 case Instruction::MUL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001100 ConvertArithOp(cu, kOpMul, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001101 break;
1102 case Instruction::DIV_LONG:
1103 case Instruction::DIV_LONG_2ADDR:
1104 case Instruction::DIV_INT:
1105 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001106 ConvertArithOp(cu, kOpDiv, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001107 break;
1108 case Instruction::REM_LONG:
1109 case Instruction::REM_LONG_2ADDR:
1110 case Instruction::REM_INT:
1111 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001112 ConvertArithOp(cu, kOpRem, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001113 break;
1114 case Instruction::AND_LONG:
1115 case Instruction::AND_LONG_2ADDR:
1116 case Instruction::AND_INT:
1117 case Instruction::AND_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001118 ConvertArithOp(cu, kOpAnd, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001119 break;
1120 case Instruction::OR_LONG:
1121 case Instruction::OR_LONG_2ADDR:
1122 case Instruction::OR_INT:
1123 case Instruction::OR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001124 ConvertArithOp(cu, kOpOr, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001125 break;
1126 case Instruction::XOR_LONG:
1127 case Instruction::XOR_LONG_2ADDR:
1128 case Instruction::XOR_INT:
1129 case Instruction::XOR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001130 ConvertArithOp(cu, kOpXor, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001131 break;
1132 case Instruction::SHL_LONG:
1133 case Instruction::SHL_LONG_2ADDR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001134 ConvertShift(cu, compiler_llvm::IntrinsicHelper::SHLLong,
buzbeefa57c472012-11-21 12:06:18 -08001135 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::SHL_INT:
1138 case Instruction::SHL_INT_2ADDR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001139 ConvertShift(cu, compiler_llvm::IntrinsicHelper::SHLInt,
buzbeefa57c472012-11-21 12:06:18 -08001140 rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001141 break;
1142 case Instruction::SHR_LONG:
1143 case Instruction::SHR_LONG_2ADDR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001144 ConvertShift(cu, compiler_llvm::IntrinsicHelper::SHRLong,
buzbeefa57c472012-11-21 12:06:18 -08001145 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::SHR_INT:
1148 case Instruction::SHR_INT_2ADDR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001149 ConvertShift(cu, compiler_llvm::IntrinsicHelper::SHRInt,
buzbeefa57c472012-11-21 12:06:18 -08001150 rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001151 break;
1152 case Instruction::USHR_LONG:
1153 case Instruction::USHR_LONG_2ADDR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001154 ConvertShift(cu, compiler_llvm::IntrinsicHelper::USHRLong,
buzbeefa57c472012-11-21 12:06:18 -08001155 rl_dest, rl_src[0], rl_src[1]);
buzbee4f1181f2012-06-22 13:52:12 -07001156 break;
buzbee2cfc6392012-05-07 14:51:40 -07001157 case Instruction::USHR_INT:
1158 case Instruction::USHR_INT_2ADDR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001159 ConvertShift(cu, compiler_llvm::IntrinsicHelper::USHRInt,
buzbeefa57c472012-11-21 12:06:18 -08001160 rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001161 break;
1162
1163 case Instruction::ADD_INT_LIT16:
1164 case Instruction::ADD_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001165 ConvertArithOpLit(cu, kOpAdd, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001166 break;
1167 case Instruction::RSUB_INT:
1168 case Instruction::RSUB_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001169 ConvertArithOpLit(cu, kOpRsub, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001170 break;
1171 case Instruction::MUL_INT_LIT16:
1172 case Instruction::MUL_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001173 ConvertArithOpLit(cu, kOpMul, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001174 break;
1175 case Instruction::DIV_INT_LIT16:
1176 case Instruction::DIV_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001177 ConvertArithOpLit(cu, kOpDiv, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001178 break;
1179 case Instruction::REM_INT_LIT16:
1180 case Instruction::REM_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001181 ConvertArithOpLit(cu, kOpRem, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001182 break;
1183 case Instruction::AND_INT_LIT16:
1184 case Instruction::AND_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001185 ConvertArithOpLit(cu, kOpAnd, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001186 break;
1187 case Instruction::OR_INT_LIT16:
1188 case Instruction::OR_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001189 ConvertArithOpLit(cu, kOpOr, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001190 break;
1191 case Instruction::XOR_INT_LIT16:
1192 case Instruction::XOR_INT_LIT8:
buzbeefa57c472012-11-21 12:06:18 -08001193 ConvertArithOpLit(cu, kOpXor, rl_dest, rl_src[0], vC);
buzbee2cfc6392012-05-07 14:51:40 -07001194 break;
1195 case Instruction::SHL_INT_LIT8:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001196 ConvertShiftLit(cu, compiler_llvm::IntrinsicHelper::SHLInt,
buzbeefa57c472012-11-21 12:06:18 -08001197 rl_dest, rl_src[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001198 break;
1199 case Instruction::SHR_INT_LIT8:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001200 ConvertShiftLit(cu, compiler_llvm::IntrinsicHelper::SHRInt,
buzbeefa57c472012-11-21 12:06:18 -08001201 rl_dest, rl_src[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001202 break;
1203 case Instruction::USHR_INT_LIT8:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001204 ConvertShiftLit(cu, compiler_llvm::IntrinsicHelper::USHRInt,
buzbeefa57c472012-11-21 12:06:18 -08001205 rl_dest, rl_src[0], vC & 0x1f);
buzbee2cfc6392012-05-07 14:51:40 -07001206 break;
1207
1208 case Instruction::ADD_FLOAT:
1209 case Instruction::ADD_FLOAT_2ADDR:
1210 case Instruction::ADD_DOUBLE:
1211 case Instruction::ADD_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001212 ConvertFPArithOp(cu, kOpAdd, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001213 break;
1214
1215 case Instruction::SUB_FLOAT:
1216 case Instruction::SUB_FLOAT_2ADDR:
1217 case Instruction::SUB_DOUBLE:
1218 case Instruction::SUB_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001219 ConvertFPArithOp(cu, kOpSub, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001220 break;
1221
1222 case Instruction::MUL_FLOAT:
1223 case Instruction::MUL_FLOAT_2ADDR:
1224 case Instruction::MUL_DOUBLE:
1225 case Instruction::MUL_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001226 ConvertFPArithOp(cu, kOpMul, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001227 break;
1228
1229 case Instruction::DIV_FLOAT:
1230 case Instruction::DIV_FLOAT_2ADDR:
1231 case Instruction::DIV_DOUBLE:
1232 case Instruction::DIV_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001233 ConvertFPArithOp(cu, kOpDiv, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001234 break;
1235
1236 case Instruction::REM_FLOAT:
1237 case Instruction::REM_FLOAT_2ADDR:
1238 case Instruction::REM_DOUBLE:
1239 case Instruction::REM_DOUBLE_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001240 ConvertFPArithOp(cu, kOpRem, rl_dest, rl_src[0], rl_src[1]);
buzbee2cfc6392012-05-07 14:51:40 -07001241 break;
1242
buzbee6969d502012-06-15 16:40:31 -07001243 case Instruction::INVOKE_STATIC:
buzbeefa57c472012-11-21 12:06:18 -08001244 ConvertInvoke(cu, bb, mir, kStatic, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001245 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001246 break;
1247 case Instruction::INVOKE_STATIC_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001248 ConvertInvoke(cu, bb, mir, kStatic, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001249 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001250 break;
1251
1252 case Instruction::INVOKE_DIRECT:
buzbeefa57c472012-11-21 12:06:18 -08001253 ConvertInvoke(cu, bb, mir, kDirect, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001254 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001255 break;
1256 case Instruction::INVOKE_DIRECT_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001257 ConvertInvoke(cu, bb, mir, kDirect, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001258 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001259 break;
1260
1261 case Instruction::INVOKE_VIRTUAL:
buzbeefa57c472012-11-21 12:06:18 -08001262 ConvertInvoke(cu, bb, mir, kVirtual, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001263 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001264 break;
1265 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001266 ConvertInvoke(cu, bb, mir, kVirtual, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001267 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001268 break;
1269
1270 case Instruction::INVOKE_SUPER:
buzbeefa57c472012-11-21 12:06:18 -08001271 ConvertInvoke(cu, bb, mir, kSuper, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001272 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001273 break;
1274 case Instruction::INVOKE_SUPER_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001275 ConvertInvoke(cu, bb, mir, kSuper, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001276 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001277 break;
1278
1279 case Instruction::INVOKE_INTERFACE:
buzbeefa57c472012-11-21 12:06:18 -08001280 ConvertInvoke(cu, bb, mir, kInterface, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001281 false /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001282 break;
1283 case Instruction::INVOKE_INTERFACE_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001284 ConvertInvoke(cu, bb, mir, kInterface, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001285 false /* NewFilledArray */);
1286 break;
1287 case Instruction::FILLED_NEW_ARRAY:
buzbeefa57c472012-11-21 12:06:18 -08001288 ConvertInvoke(cu, bb, mir, kInterface, false /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001289 true /* NewFilledArray */);
1290 break;
1291 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbeefa57c472012-11-21 12:06:18 -08001292 ConvertInvoke(cu, bb, mir, kInterface, true /*range*/,
buzbee101305f2012-06-28 18:00:56 -07001293 true /* NewFilledArray */);
buzbee6969d502012-06-15 16:40:31 -07001294 break;
1295
1296 case Instruction::CONST_STRING:
1297 case Instruction::CONST_STRING_JUMBO:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001298 ConvertConstObject(cu, vB, compiler_llvm::IntrinsicHelper::ConstString,
buzbeefa57c472012-11-21 12:06:18 -08001299 rl_dest);
buzbee101305f2012-06-28 18:00:56 -07001300 break;
1301
1302 case Instruction::CONST_CLASS:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001303 ConvertConstObject(cu, vB, compiler_llvm::IntrinsicHelper::ConstClass,
buzbeefa57c472012-11-21 12:06:18 -08001304 rl_dest);
buzbee101305f2012-06-28 18:00:56 -07001305 break;
1306
1307 case Instruction::CHECK_CAST:
buzbeefa57c472012-11-21 12:06:18 -08001308 ConvertCheckCast(cu, vB, rl_src[0]);
buzbee6969d502012-06-15 16:40:31 -07001309 break;
1310
buzbee4f1181f2012-06-22 13:52:12 -07001311 case Instruction::NEW_INSTANCE:
buzbeefa57c472012-11-21 12:06:18 -08001312 ConvertNewInstance(cu, vB, rl_dest);
buzbee4f1181f2012-06-22 13:52:12 -07001313 break;
1314
buzbee32412962012-06-26 16:27:56 -07001315 case Instruction::MOVE_EXCEPTION:
buzbeefa57c472012-11-21 12:06:18 -08001316 ConvertMoveException(cu, rl_dest);
buzbee32412962012-06-26 16:27:56 -07001317 break;
1318
1319 case Instruction::THROW:
buzbeefa57c472012-11-21 12:06:18 -08001320 ConvertThrow(cu, rl_src[0]);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001321 /*
1322 * If this throw is standalone, terminate.
1323 * If it might rethrow, force termination
1324 * of the following block.
1325 */
buzbeefa57c472012-11-21 12:06:18 -08001326 if (bb->fall_through == NULL) {
1327 cu->irb->CreateUnreachable();
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001328 } else {
buzbeefa57c472012-11-21 12:06:18 -08001329 bb->fall_through->fall_through = NULL;
1330 bb->fall_through->taken = NULL;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001331 }
buzbee32412962012-06-26 16:27:56 -07001332 break;
1333
buzbee2cfc6392012-05-07 14:51:40 -07001334 case Instruction::MOVE_RESULT_WIDE:
buzbee2cfc6392012-05-07 14:51:40 -07001335 case Instruction::MOVE_RESULT:
1336 case Instruction::MOVE_RESULT_OBJECT:
buzbee9a2487f2012-07-26 14:01:13 -07001337 /*
jeffhao9a4f0032012-08-30 16:17:40 -07001338 * All move_results should have been folded into the preceeding invoke.
buzbee9a2487f2012-07-26 14:01:13 -07001339 */
jeffhao9a4f0032012-08-30 16:17:40 -07001340 LOG(FATAL) << "Unexpected move_result";
buzbee2cfc6392012-05-07 14:51:40 -07001341 break;
1342
1343 case Instruction::MONITOR_ENTER:
buzbeefa57c472012-11-21 12:06:18 -08001344 ConvertMonitorEnterExit(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001345 compiler_llvm::IntrinsicHelper::MonitorEnter,
buzbeefa57c472012-11-21 12:06:18 -08001346 rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001347 break;
1348
1349 case Instruction::MONITOR_EXIT:
buzbeefa57c472012-11-21 12:06:18 -08001350 ConvertMonitorEnterExit(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001351 compiler_llvm::IntrinsicHelper::MonitorExit,
buzbeefa57c472012-11-21 12:06:18 -08001352 rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001353 break;
1354
1355 case Instruction::ARRAY_LENGTH:
buzbeefa57c472012-11-21 12:06:18 -08001356 ConvertArrayLength(cu, opt_flags, rl_dest, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001357 break;
1358
1359 case Instruction::NEW_ARRAY:
buzbeefa57c472012-11-21 12:06:18 -08001360 ConvertNewArray(cu, vC, rl_dest, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001361 break;
1362
1363 case Instruction::INSTANCE_OF:
buzbeefa57c472012-11-21 12:06:18 -08001364 ConvertInstanceOf(cu, vC, rl_dest, rl_src[0]);
buzbee8fa0fda2012-06-27 15:44:52 -07001365 break;
1366
1367 case Instruction::AGET:
buzbeefa57c472012-11-21 12:06:18 -08001368 if (rl_dest.fp) {
1369 ConvertAget(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001370 compiler_llvm::IntrinsicHelper::HLArrayGetFloat,
buzbeefa57c472012-11-21 12:06:18 -08001371 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001372 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001373 ConvertAget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayGet,
buzbeefa57c472012-11-21 12:06:18 -08001374 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001375 }
1376 break;
1377 case Instruction::AGET_OBJECT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001378 ConvertAget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayGetObject,
buzbeefa57c472012-11-21 12:06:18 -08001379 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001380 break;
1381 case Instruction::AGET_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -08001382 ConvertAget(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001383 compiler_llvm::IntrinsicHelper::HLArrayGetBoolean,
buzbeefa57c472012-11-21 12:06:18 -08001384 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001385 break;
1386 case Instruction::AGET_BYTE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001387 ConvertAget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayGetByte,
buzbeefa57c472012-11-21 12:06:18 -08001388 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001389 break;
1390 case Instruction::AGET_CHAR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001391 ConvertAget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayGetChar,
buzbeefa57c472012-11-21 12:06:18 -08001392 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001393 break;
1394 case Instruction::AGET_SHORT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001395 ConvertAget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayGetShort,
buzbeefa57c472012-11-21 12:06:18 -08001396 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001397 break;
1398 case Instruction::AGET_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001399 if (rl_dest.fp) {
1400 ConvertAget(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001401 compiler_llvm::IntrinsicHelper::HLArrayGetDouble,
buzbeefa57c472012-11-21 12:06:18 -08001402 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001403 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001404 ConvertAget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayGetWide,
buzbeefa57c472012-11-21 12:06:18 -08001405 rl_dest, rl_src[0], rl_src[1]);
buzbee8fa0fda2012-06-27 15:44:52 -07001406 }
1407 break;
1408
1409 case Instruction::APUT:
buzbeefa57c472012-11-21 12:06:18 -08001410 if (rl_src[0].fp) {
1411 ConvertAput(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001412 compiler_llvm::IntrinsicHelper::HLArrayPutFloat,
buzbeefa57c472012-11-21 12:06:18 -08001413 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001414 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001415 ConvertAput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayPut,
buzbeefa57c472012-11-21 12:06:18 -08001416 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001417 }
1418 break;
1419 case Instruction::APUT_OBJECT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001420 ConvertAput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayPutObject,
buzbeefa57c472012-11-21 12:06:18 -08001421 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001422 break;
1423 case Instruction::APUT_BOOLEAN:
buzbeefa57c472012-11-21 12:06:18 -08001424 ConvertAput(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001425 compiler_llvm::IntrinsicHelper::HLArrayPutBoolean,
buzbeefa57c472012-11-21 12:06:18 -08001426 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001427 break;
1428 case Instruction::APUT_BYTE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001429 ConvertAput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayPutByte,
buzbeefa57c472012-11-21 12:06:18 -08001430 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001431 break;
1432 case Instruction::APUT_CHAR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001433 ConvertAput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayPutChar,
buzbeefa57c472012-11-21 12:06:18 -08001434 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001435 break;
1436 case Instruction::APUT_SHORT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001437 ConvertAput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayPutShort,
buzbeefa57c472012-11-21 12:06:18 -08001438 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001439 break;
1440 case Instruction::APUT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001441 if (rl_src[0].fp) {
1442 ConvertAput(cu, opt_flags,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001443 compiler_llvm::IntrinsicHelper::HLArrayPutDouble,
buzbeefa57c472012-11-21 12:06:18 -08001444 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001445 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001446 ConvertAput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLArrayPutWide,
buzbeefa57c472012-11-21 12:06:18 -08001447 rl_src[0], rl_src[1], rl_src[2]);
buzbee8fa0fda2012-06-27 15:44:52 -07001448 }
1449 break;
1450
buzbee101305f2012-06-28 18:00:56 -07001451 case Instruction::IGET:
buzbeefa57c472012-11-21 12:06:18 -08001452 if (rl_dest.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001453 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetFloat,
buzbeefa57c472012-11-21 12:06:18 -08001454 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001455 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001456 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGet,
buzbeefa57c472012-11-21 12:06:18 -08001457 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001458 }
buzbee2cfc6392012-05-07 14:51:40 -07001459 break;
buzbee101305f2012-06-28 18:00:56 -07001460 case Instruction::IGET_OBJECT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001461 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetObject,
buzbeefa57c472012-11-21 12:06:18 -08001462 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001463 break;
1464 case Instruction::IGET_BOOLEAN:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001465 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetBoolean,
buzbeefa57c472012-11-21 12:06:18 -08001466 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001467 break;
1468 case Instruction::IGET_BYTE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001469 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetByte,
buzbeefa57c472012-11-21 12:06:18 -08001470 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001471 break;
1472 case Instruction::IGET_CHAR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001473 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetChar,
buzbeefa57c472012-11-21 12:06:18 -08001474 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001475 break;
1476 case Instruction::IGET_SHORT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001477 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetShort,
buzbeefa57c472012-11-21 12:06:18 -08001478 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001479 break;
1480 case Instruction::IGET_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001481 if (rl_dest.fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001482 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetDouble,
buzbeefa57c472012-11-21 12:06:18 -08001483 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001484 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001485 ConvertIget(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIGetWide,
buzbeefa57c472012-11-21 12:06:18 -08001486 rl_dest, rl_src[0], vC);
buzbee101305f2012-06-28 18:00:56 -07001487 }
1488 break;
1489 case Instruction::IPUT:
buzbeefa57c472012-11-21 12:06:18 -08001490 if (rl_src[0].fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001491 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutFloat,
buzbeefa57c472012-11-21 12:06:18 -08001492 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001493 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001494 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPut,
buzbeefa57c472012-11-21 12:06:18 -08001495 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001496 }
1497 break;
1498 case Instruction::IPUT_OBJECT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001499 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutObject,
buzbeefa57c472012-11-21 12:06:18 -08001500 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001501 break;
1502 case Instruction::IPUT_BOOLEAN:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001503 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutBoolean,
buzbeefa57c472012-11-21 12:06:18 -08001504 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001505 break;
1506 case Instruction::IPUT_BYTE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001507 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutByte,
buzbeefa57c472012-11-21 12:06:18 -08001508 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001509 break;
1510 case Instruction::IPUT_CHAR:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001511 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutChar,
buzbeefa57c472012-11-21 12:06:18 -08001512 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001513 break;
1514 case Instruction::IPUT_SHORT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001515 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutShort,
buzbeefa57c472012-11-21 12:06:18 -08001516 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001517 break;
1518 case Instruction::IPUT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -08001519 if (rl_src[0].fp) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001520 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutDouble,
buzbeefa57c472012-11-21 12:06:18 -08001521 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001522 } else {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001523 ConvertIput(cu, opt_flags, compiler_llvm::IntrinsicHelper::HLIPutWide,
buzbeefa57c472012-11-21 12:06:18 -08001524 rl_src[0], rl_src[1], vC);
buzbee101305f2012-06-28 18:00:56 -07001525 }
buzbee2cfc6392012-05-07 14:51:40 -07001526 break;
1527
1528 case Instruction::FILL_ARRAY_DATA:
buzbeefa57c472012-11-21 12:06:18 -08001529 ConvertFillArrayData(cu, vB, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001530 break;
1531
buzbee76592632012-06-29 15:18:35 -07001532 case Instruction::LONG_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -08001533 ConvertLongToInt(cu, rl_dest, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001534 break;
1535
buzbee101305f2012-06-28 18:00:56 -07001536 case Instruction::INT_TO_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001537 ConvertIntToLong(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001538 break;
1539
buzbee101305f2012-06-28 18:00:56 -07001540 case Instruction::INT_TO_CHAR:
buzbeefa57c472012-11-21 12:06:18 -08001541 ConvertIntNarrowing(cu, rl_dest, rl_src[0],
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001542 compiler_llvm::IntrinsicHelper::IntToChar);
buzbee101305f2012-06-28 18:00:56 -07001543 break;
1544 case Instruction::INT_TO_BYTE:
buzbeefa57c472012-11-21 12:06:18 -08001545 ConvertIntNarrowing(cu, rl_dest, rl_src[0],
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001546 compiler_llvm::IntrinsicHelper::IntToByte);
buzbee101305f2012-06-28 18:00:56 -07001547 break;
1548 case Instruction::INT_TO_SHORT:
buzbeefa57c472012-11-21 12:06:18 -08001549 ConvertIntNarrowing(cu, rl_dest, rl_src[0],
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001550 compiler_llvm::IntrinsicHelper::IntToShort);
buzbee101305f2012-06-28 18:00:56 -07001551 break;
1552
buzbee76592632012-06-29 15:18:35 -07001553 case Instruction::INT_TO_FLOAT:
1554 case Instruction::LONG_TO_FLOAT:
buzbeefa57c472012-11-21 12:06:18 -08001555 ConvertIntToFP(cu, cu->irb->getFloatTy(), rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001556 break;
1557
buzbee76592632012-06-29 15:18:35 -07001558 case Instruction::INT_TO_DOUBLE:
1559 case Instruction::LONG_TO_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001560 ConvertIntToFP(cu, cu->irb->getDoubleTy(), rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001561 break;
1562
buzbee76592632012-06-29 15:18:35 -07001563 case Instruction::FLOAT_TO_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001564 ConvertFloatToDouble(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001565 break;
1566
buzbee76592632012-06-29 15:18:35 -07001567 case Instruction::DOUBLE_TO_FLOAT:
buzbeefa57c472012-11-21 12:06:18 -08001568 ConvertDoubleToFloat(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001569 break;
1570
1571 case Instruction::NEG_LONG:
buzbee76592632012-06-29 15:18:35 -07001572 case Instruction::NEG_INT:
buzbeefa57c472012-11-21 12:06:18 -08001573 ConvertNeg(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001574 break;
1575
1576 case Instruction::NEG_FLOAT:
buzbee2cfc6392012-05-07 14:51:40 -07001577 case Instruction::NEG_DOUBLE:
buzbeefa57c472012-11-21 12:06:18 -08001578 ConvertNegFP(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001579 break;
1580
buzbee76592632012-06-29 15:18:35 -07001581 case Instruction::NOT_LONG:
1582 case Instruction::NOT_INT:
buzbeefa57c472012-11-21 12:06:18 -08001583 ConvertNot(cu, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001584 break;
1585
buzbee2cfc6392012-05-07 14:51:40 -07001586 case Instruction::FLOAT_TO_INT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001587 ConvertFPToInt(cu, compiler_llvm::IntrinsicHelper::F2I, rl_dest, rl_src[0]);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001588 break;
1589
buzbee2cfc6392012-05-07 14:51:40 -07001590 case Instruction::DOUBLE_TO_INT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001591 ConvertFPToInt(cu, compiler_llvm::IntrinsicHelper::D2I, rl_dest, rl_src[0]);
buzbee2cfc6392012-05-07 14:51:40 -07001592 break;
1593
buzbee76592632012-06-29 15:18:35 -07001594 case Instruction::FLOAT_TO_LONG:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001595 ConvertFPToInt(cu, compiler_llvm::IntrinsicHelper::F2L, rl_dest, rl_src[0]);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001596 break;
1597
buzbee76592632012-06-29 15:18:35 -07001598 case Instruction::DOUBLE_TO_LONG:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001599 ConvertFPToInt(cu, compiler_llvm::IntrinsicHelper::D2L, rl_dest, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001600 break;
1601
1602 case Instruction::CMPL_FLOAT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001603 ConvertWideComparison(cu, compiler_llvm::IntrinsicHelper::CmplFloat,
buzbeefa57c472012-11-21 12:06:18 -08001604 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001605 break;
1606 case Instruction::CMPG_FLOAT:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001607 ConvertWideComparison(cu, compiler_llvm::IntrinsicHelper::CmpgFloat,
buzbeefa57c472012-11-21 12:06:18 -08001608 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001609 break;
1610 case Instruction::CMPL_DOUBLE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001611 ConvertWideComparison(cu, compiler_llvm::IntrinsicHelper::CmplDouble,
buzbeefa57c472012-11-21 12:06:18 -08001612 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001613 break;
1614 case Instruction::CMPG_DOUBLE:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001615 ConvertWideComparison(cu, compiler_llvm::IntrinsicHelper::CmpgDouble,
buzbeefa57c472012-11-21 12:06:18 -08001616 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001617 break;
1618 case Instruction::CMP_LONG:
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001619 ConvertWideComparison(cu, compiler_llvm::IntrinsicHelper::CmpLong,
buzbeefa57c472012-11-21 12:06:18 -08001620 rl_dest, rl_src[0], rl_src[1]);
buzbee76592632012-06-29 15:18:35 -07001621 break;
1622
buzbee76592632012-06-29 15:18:35 -07001623 case Instruction::PACKED_SWITCH:
buzbeefa57c472012-11-21 12:06:18 -08001624 ConvertPackedSwitch(cu, bb, vB, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001625 break;
1626
1627 case Instruction::SPARSE_SWITCH:
buzbeefa57c472012-11-21 12:06:18 -08001628 ConvertSparseSwitch(cu, bb, vB, rl_src[0]);
buzbee76592632012-06-29 15:18:35 -07001629 break;
buzbee2cfc6392012-05-07 14:51:40 -07001630
1631 default:
buzbee32412962012-06-26 16:27:56 -07001632 UNIMPLEMENTED(FATAL) << "Unsupported Dex opcode 0x" << std::hex << opcode;
buzbee2cfc6392012-05-07 14:51:40 -07001633 res = true;
1634 }
1635 return res;
1636}
1637
buzbeefa57c472012-11-21 12:06:18 -08001638static void SetDexOffset(CompilationUnit* cu, int32_t offset)
buzbee2cfc6392012-05-07 14:51:40 -07001639{
buzbeefa57c472012-11-21 12:06:18 -08001640 cu->current_dalvik_offset = offset;
1641 llvm::SmallVector<llvm::Value*, 1> array_ref;
1642 array_ref.push_back(cu->irb->getInt32(offset));
1643 llvm::MDNode* node = llvm::MDNode::get(*cu->context, array_ref);
1644 cu->irb->SetDexOffset(node);
buzbee2cfc6392012-05-07 14:51:40 -07001645}
1646
1647// Attach method info as metadata to special intrinsic
buzbeefa57c472012-11-21 12:06:18 -08001648static void SetMethodInfo(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -07001649{
1650 // We don't want dex offset on this
buzbeefa57c472012-11-21 12:06:18 -08001651 cu->irb->SetDexOffset(NULL);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001652 compiler_llvm::IntrinsicHelper::IntrinsicId id;
1653 id = compiler_llvm::IntrinsicHelper::MethodInfo;
buzbeefa57c472012-11-21 12:06:18 -08001654 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(id);
1655 llvm::Instruction* inst = cu->irb->CreateCall(intr);
1656 llvm::SmallVector<llvm::Value*, 2> reg_info;
1657 reg_info.push_back(cu->irb->getInt32(cu->num_ins));
1658 reg_info.push_back(cu->irb->getInt32(cu->num_regs));
1659 reg_info.push_back(cu->irb->getInt32(cu->num_outs));
1660 reg_info.push_back(cu->irb->getInt32(cu->num_compiler_temps));
1661 reg_info.push_back(cu->irb->getInt32(cu->num_ssa_regs));
1662 llvm::MDNode* reg_info_node = llvm::MDNode::get(*cu->context, reg_info);
1663 inst->setMetadata("RegInfo", reg_info_node);
1664 int promo_size = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
buzbee2cfc6392012-05-07 14:51:40 -07001665 llvm::SmallVector<llvm::Value*, 50> pmap;
buzbeefa57c472012-11-21 12:06:18 -08001666 for (int i = 0; i < promo_size; i++) {
1667 PromotionMap* p = &cu->promotion_map[i];
1668 int32_t map_data = ((p->first_in_pair & 0xff) << 24) |
buzbee52a77fc2012-11-20 19:50:46 -08001669 ((p->FpReg & 0xff) << 16) |
buzbeefa57c472012-11-21 12:06:18 -08001670 ((p->core_reg & 0xff) << 8) |
1671 ((p->fp_location & 0xf) << 4) |
1672 (p->core_location & 0xf);
1673 pmap.push_back(cu->irb->getInt32(map_data));
buzbee2cfc6392012-05-07 14:51:40 -07001674 }
buzbeefa57c472012-11-21 12:06:18 -08001675 llvm::MDNode* map_node = llvm::MDNode::get(*cu->context, pmap);
1676 inst->setMetadata("PromotionMap", map_node);
1677 SetDexOffset(cu, cu->current_dalvik_offset);
buzbee2cfc6392012-05-07 14:51:40 -07001678}
1679
buzbee26f10ee2012-12-21 11:16:29 -08001680static void HandlePhiNodes(CompilationUnit* cu, BasicBlock* bb, llvm::BasicBlock* llvm_bb)
1681{
1682 SetDexOffset(cu, bb->start_offset);
1683 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
1684 int opcode = mir->dalvikInsn.opcode;
1685 if (opcode < kMirOpFirst) {
1686 // Stop after first non-pseudo MIR op.
1687 continue;
1688 }
1689 if (opcode != kMirOpPhi) {
1690 // Skip other mir Pseudos.
1691 continue;
1692 }
1693 RegLocation rl_dest = cu->reg_location[mir->ssa_rep->defs[0]];
1694 /*
1695 * The Art compiler's Phi nodes only handle 32-bit operands,
1696 * representing wide values using a matched set of Phi nodes
1697 * for the lower and upper halves. In the llvm world, we only
1698 * want a single Phi for wides. Here we will simply discard
1699 * the Phi node representing the high word.
1700 */
1701 if (rl_dest.high_word) {
1702 continue; // No Phi node - handled via low word
1703 }
1704 int* incoming = reinterpret_cast<int*>(mir->dalvikInsn.vB);
1705 llvm::Type* phi_type =
1706 LlvmTypeFromLocRec(cu, rl_dest);
1707 llvm::PHINode* phi = cu->irb->CreatePHI(phi_type, mir->ssa_rep->num_uses);
1708 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
1709 RegLocation loc;
1710 // Don't check width here.
1711 loc = GetRawSrc(cu, mir, i);
1712 DCHECK_EQ(rl_dest.wide, loc.wide);
1713 DCHECK_EQ(rl_dest.wide & rl_dest.high_word, loc.wide & loc.high_word);
1714 DCHECK_EQ(rl_dest.fp, loc.fp);
1715 DCHECK_EQ(rl_dest.core, loc.core);
1716 DCHECK_EQ(rl_dest.ref, loc.ref);
1717 SafeMap<unsigned int, unsigned int>::iterator it;
1718 it = cu->block_id_map.find(incoming[i]);
1719 DCHECK(it != cu->block_id_map.end());
1720 DCHECK(GetLLVMValue(cu, loc.orig_sreg) != NULL);
1721 DCHECK(GetLLVMBlock(cu, it->second) != NULL);
1722 phi->addIncoming(GetLLVMValue(cu, loc.orig_sreg),
1723 GetLLVMBlock(cu, it->second));
1724 }
1725 DefineValueOnly(cu, phi, rl_dest.orig_sreg);
1726 }
1727}
1728
1729/* Extended MIR instructions like PHI */
1730static void ConvertExtendedMIR(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
1731 llvm::BasicBlock* llvm_bb)
1732{
1733
1734 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1735 case kMirOpPhi: {
1736 // The llvm Phi node already emitted - just DefineValue() here.
1737 RegLocation rl_dest = cu->reg_location[mir->ssa_rep->defs[0]];
1738 if (!rl_dest.high_word) {
1739 // Only consider low word of pairs.
1740 DCHECK(GetLLVMValue(cu, rl_dest.orig_sreg) != NULL);
1741 llvm::Value* phi = GetLLVMValue(cu, rl_dest.orig_sreg);
1742 if (1) SetVregOnValue(cu, phi, rl_dest.orig_sreg);
1743 }
1744 break;
1745 }
1746 case kMirOpCopy: {
1747 UNIMPLEMENTED(WARNING) << "unimp kMirOpPhi";
1748 break;
1749 }
1750 case kMirOpNop:
1751 if ((mir == bb->last_mir_insn) && (bb->taken == NULL) &&
1752 (bb->fall_through == NULL)) {
1753 cu->irb->CreateUnreachable();
1754 }
1755 break;
1756
1757 // TODO: need GBC intrinsic to take advantage of fused operations
1758 case kMirOpFusedCmplFloat:
1759 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpFloat unsupported";
1760 break;
1761 case kMirOpFusedCmpgFloat:
1762 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmgFloat unsupported";
1763 break;
1764 case kMirOpFusedCmplDouble:
1765 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmplDouble unsupported";
1766 break;
1767 case kMirOpFusedCmpgDouble:
1768 UNIMPLEMENTED(FATAL) << "kMirOpFusedCmpgDouble unsupported";
1769 break;
1770 case kMirOpFusedCmpLong:
1771 UNIMPLEMENTED(FATAL) << "kMirOpLongCmpBranch unsupported";
1772 break;
1773 default:
1774 break;
1775 }
1776}
1777
buzbee2cfc6392012-05-07 14:51:40 -07001778/* Handle the content in each basic block */
buzbeefa57c472012-11-21 12:06:18 -08001779static bool BlockBitcodeConversion(CompilationUnit* cu, BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07001780{
buzbeefa57c472012-11-21 12:06:18 -08001781 if (bb->block_type == kDead) return false;
1782 llvm::BasicBlock* llvm_bb = GetLLVMBlock(cu, bb->id);
1783 if (llvm_bb == NULL) {
1784 CHECK(bb->block_type == kExitBlock);
buzbeef5f5a122012-09-21 13:57:36 -07001785 } else {
buzbeefa57c472012-11-21 12:06:18 -08001786 cu->irb->SetInsertPoint(llvm_bb);
1787 SetDexOffset(cu, bb->start_offset);
buzbeef5f5a122012-09-21 13:57:36 -07001788 }
buzbee2cfc6392012-05-07 14:51:40 -07001789
buzbeefa57c472012-11-21 12:06:18 -08001790 if (cu->verbose) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001791 LOG(INFO) << "................................";
1792 LOG(INFO) << "Block id " << bb->id;
buzbeefa57c472012-11-21 12:06:18 -08001793 if (llvm_bb != NULL) {
1794 LOG(INFO) << "label " << llvm_bb->getName().str().c_str();
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001795 } else {
buzbeefa57c472012-11-21 12:06:18 -08001796 LOG(INFO) << "llvm_bb is NULL";
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001797 }
1798 }
1799
buzbeefa57c472012-11-21 12:06:18 -08001800 if (bb->block_type == kEntryBlock) {
1801 SetMethodInfo(cu);
TDYa127dc5daa02013-01-09 21:31:37 +08001802
1803 { // Allocate shadowframe.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001804 compiler_llvm::IntrinsicHelper::IntrinsicId id =
1805 compiler_llvm::IntrinsicHelper::AllocaShadowFrame;
TDYa127dc5daa02013-01-09 21:31:37 +08001806 llvm::Function* func = cu->intrinsic_helper->GetIntrinsicFunction(id);
1807 llvm::Value* entries = cu->irb->getInt32(cu->num_dalvik_registers);
1808 cu->irb->CreateCall(func, entries);
buzbeeb03f4872012-06-11 15:22:11 -07001809 }
TDYa127dc5daa02013-01-09 21:31:37 +08001810
1811 { // Store arguments to vregs.
1812 uint16_t arg_reg = cu->num_regs;
1813
1814 llvm::Function::arg_iterator arg_iter(cu->func->arg_begin());
1815 llvm::Function::arg_iterator arg_end(cu->func->arg_end());
1816
1817 const char* shorty = cu->shorty;
1818 uint32_t shorty_size = strlen(shorty);
1819 CHECK_GE(shorty_size, 1u);
1820
1821 ++arg_iter; // skip method object
1822
1823 if ((cu->access_flags & kAccStatic) == 0) {
1824 SetVregOnValue(cu, arg_iter, arg_reg);
1825 ++arg_iter;
1826 ++arg_reg;
buzbeeb03f4872012-06-11 15:22:11 -07001827 }
TDYa127dc5daa02013-01-09 21:31:37 +08001828
1829 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
1830 SetVregOnValue(cu, arg_iter, arg_reg);
1831
1832 ++arg_reg;
1833 if (shorty[i] == 'J' || shorty[i] == 'D') {
1834 // Wide types, such as long and double, are using a pair of registers
1835 // to store the value, so we have to increase arg_reg again.
1836 ++arg_reg;
buzbeeb03f4872012-06-11 15:22:11 -07001837 }
1838 }
buzbeeb03f4872012-06-11 15:22:11 -07001839 }
buzbeefa57c472012-11-21 12:06:18 -08001840 } else if (bb->block_type == kExitBlock) {
buzbee2cfc6392012-05-07 14:51:40 -07001841 /*
1842 * Because of the differences between how MIR/LIR and llvm handle exit
1843 * blocks, we won't explicitly covert them. On the llvm-to-lir
1844 * path, it will need to be regenereated.
1845 */
1846 return false;
buzbeefa57c472012-11-21 12:06:18 -08001847 } else if (bb->block_type == kExceptionHandling) {
buzbee6969d502012-06-15 16:40:31 -07001848 /*
1849 * Because we're deferring null checking, delete the associated empty
1850 * exception block.
buzbee6969d502012-06-15 16:40:31 -07001851 */
buzbeefa57c472012-11-21 12:06:18 -08001852 llvm_bb->eraseFromParent();
buzbee6969d502012-06-15 16:40:31 -07001853 return false;
buzbee2cfc6392012-05-07 14:51:40 -07001854 }
1855
buzbee26f10ee2012-12-21 11:16:29 -08001856 HandlePhiNodes(cu, bb, llvm_bb);
1857
buzbee28c9a832012-11-21 15:39:13 -08001858 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbee2cfc6392012-05-07 14:51:40 -07001859
buzbeefa57c472012-11-21 12:06:18 -08001860 SetDexOffset(cu, mir->offset);
buzbee2cfc6392012-05-07 14:51:40 -07001861
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001862 int opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -08001863 Instruction::Format dalvik_format =
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001864 Instruction::FormatOf(mir->dalvikInsn.opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001865
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001866 if (opcode == kMirOpCheck) {
1867 // Combine check and work halves of throwing instruction.
buzbeefa57c472012-11-21 12:06:18 -08001868 MIR* work_half = mir->meta.throw_insn;
1869 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001870 opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -08001871 SSARepresentation* ssa_rep = work_half->ssa_rep;
1872 work_half->ssa_rep = mir->ssa_rep;
1873 mir->ssa_rep = ssa_rep;
buzbeea169e1d2012-12-05 14:26:44 -08001874 work_half->meta.original_opcode = work_half->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -08001875 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1876 if (bb->successor_block_list.block_list_type == kCatch) {
1877 llvm::Function* intr = cu->intrinsic_helper->GetIntrinsicFunction(
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001878 compiler_llvm::IntrinsicHelper::CatchTargets);
buzbeefa57c472012-11-21 12:06:18 -08001879 llvm::Value* switch_key =
1880 cu->irb->CreateCall(intr, cu->irb->getInt32(mir->offset));
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001881 GrowableListIterator iter;
buzbeefa57c472012-11-21 12:06:18 -08001882 GrowableListIteratorInit(&bb->successor_block_list.blocks, &iter);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001883 // New basic block to use for work half
buzbeefa57c472012-11-21 12:06:18 -08001884 llvm::BasicBlock* work_bb =
1885 llvm::BasicBlock::Create(*cu->context, "", cu->func);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001886 llvm::SwitchInst* sw =
buzbeefa57c472012-11-21 12:06:18 -08001887 cu->irb->CreateSwitch(switch_key, work_bb,
1888 bb->successor_block_list.blocks.num_used);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001889 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001890 SuccessorBlockInfo *successor_block_info =
buzbee52a77fc2012-11-20 19:50:46 -08001891 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iter));
buzbeefa57c472012-11-21 12:06:18 -08001892 if (successor_block_info == NULL) break;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001893 llvm::BasicBlock *target =
buzbeefa57c472012-11-21 12:06:18 -08001894 GetLLVMBlock(cu, successor_block_info->block->id);
1895 int type_index = successor_block_info->key;
1896 sw->addCase(cu->irb->getInt32(type_index), target);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001897 }
buzbeefa57c472012-11-21 12:06:18 -08001898 llvm_bb = work_bb;
1899 cu->irb->SetInsertPoint(llvm_bb);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001900 }
1901 }
1902
1903 if (opcode >= kMirOpFirst) {
buzbeefa57c472012-11-21 12:06:18 -08001904 ConvertExtendedMIR(cu, bb, mir, llvm_bb);
buzbee2cfc6392012-05-07 14:51:40 -07001905 continue;
1906 }
1907
buzbeefa57c472012-11-21 12:06:18 -08001908 bool not_handled = ConvertMIRNode(cu, mir, bb, llvm_bb,
1909 NULL /* label_list */);
1910 if (not_handled) {
1911 Instruction::Code dalvik_opcode = static_cast<Instruction::Code>(opcode);
buzbee2cfc6392012-05-07 14:51:40 -07001912 LOG(WARNING) << StringPrintf("%#06x: Op %#x (%s) / Fmt %d not handled",
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07001913 mir->offset, opcode,
buzbeefa57c472012-11-21 12:06:18 -08001914 Instruction::Name(dalvik_opcode),
1915 dalvik_format);
buzbee2cfc6392012-05-07 14:51:40 -07001916 }
1917 }
1918
buzbeefa57c472012-11-21 12:06:18 -08001919 if (bb->block_type == kEntryBlock) {
1920 cu->entryTarget_bb = GetLLVMBlock(cu, bb->fall_through->id);
buzbeebbdd0532013-02-07 09:33:02 -08001921 } else if ((bb->fall_through != NULL) && !bb->terminated_by_return) {
buzbeefa57c472012-11-21 12:06:18 -08001922 cu->irb->CreateBr(GetLLVMBlock(cu, bb->fall_through->id));
buzbee2cfc6392012-05-07 14:51:40 -07001923 }
1924
1925 return false;
1926}
1927
buzbeefa57c472012-11-21 12:06:18 -08001928char RemapShorty(char shorty_type) {
buzbee4f4dfc72012-07-02 14:54:44 -07001929 /*
1930 * TODO: might want to revisit this. Dalvik registers are 32-bits wide,
1931 * and longs/doubles are represented as a pair of registers. When sub-word
1932 * arguments (and method results) are passed, they are extended to Dalvik
1933 * virtual register containers. Because llvm is picky about type consistency,
1934 * we must either cast the "real" type to 32-bit container multiple Dalvik
1935 * register types, or always use the expanded values.
1936 * Here, we're doing the latter. We map the shorty signature to container
1937 * types (which is valid so long as we always do a real expansion of passed
1938 * arguments and field loads).
1939 */
buzbeefa57c472012-11-21 12:06:18 -08001940 switch(shorty_type) {
1941 case 'Z' : shorty_type = 'I'; break;
1942 case 'B' : shorty_type = 'I'; break;
1943 case 'S' : shorty_type = 'I'; break;
1944 case 'C' : shorty_type = 'I'; break;
buzbee4f4dfc72012-07-02 14:54:44 -07001945 default: break;
1946 }
buzbeefa57c472012-11-21 12:06:18 -08001947 return shorty_type;
buzbee4f4dfc72012-07-02 14:54:44 -07001948}
1949
buzbeefa57c472012-11-21 12:06:18 -08001950static llvm::FunctionType* GetFunctionType(CompilationUnit* cu) {
buzbee2cfc6392012-05-07 14:51:40 -07001951
1952 // Get return type
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001953 llvm::Type* ret_type = cu->irb->getJType(RemapShorty(cu->shorty[0]));
buzbee2cfc6392012-05-07 14:51:40 -07001954
1955 // Get argument type
1956 std::vector<llvm::Type*> args_type;
1957
1958 // method object
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001959 args_type.push_back(cu->irb->getJMethodTy());
buzbee2cfc6392012-05-07 14:51:40 -07001960
1961 // Do we have a "this"?
buzbeefa57c472012-11-21 12:06:18 -08001962 if ((cu->access_flags & kAccStatic) == 0) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001963 args_type.push_back(cu->irb->getJObjectTy());
buzbee2cfc6392012-05-07 14:51:40 -07001964 }
1965
buzbeefa57c472012-11-21 12:06:18 -08001966 for (uint32_t i = 1; i < strlen(cu->shorty); ++i) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001967 args_type.push_back(cu->irb->getJType(RemapShorty(cu->shorty[i])));
buzbee2cfc6392012-05-07 14:51:40 -07001968 }
1969
1970 return llvm::FunctionType::get(ret_type, args_type, false);
1971}
1972
buzbeefa57c472012-11-21 12:06:18 -08001973static bool CreateFunction(CompilationUnit* cu) {
1974 std::string func_name(PrettyMethod(cu->method_idx, *cu->dex_file,
buzbee2cfc6392012-05-07 14:51:40 -07001975 /* with_signature */ false));
buzbeefa57c472012-11-21 12:06:18 -08001976 llvm::FunctionType* func_type = GetFunctionType(cu);
buzbee2cfc6392012-05-07 14:51:40 -07001977
1978 if (func_type == NULL) {
1979 return false;
1980 }
1981
buzbeefa57c472012-11-21 12:06:18 -08001982 cu->func = llvm::Function::Create(func_type,
buzbee2cfc6392012-05-07 14:51:40 -07001983 llvm::Function::ExternalLinkage,
buzbeefa57c472012-11-21 12:06:18 -08001984 func_name, cu->module);
buzbee2cfc6392012-05-07 14:51:40 -07001985
buzbeefa57c472012-11-21 12:06:18 -08001986 llvm::Function::arg_iterator arg_iter(cu->func->arg_begin());
1987 llvm::Function::arg_iterator arg_end(cu->func->arg_end());
buzbee2cfc6392012-05-07 14:51:40 -07001988
1989 arg_iter->setName("method");
1990 ++arg_iter;
1991
buzbeefa57c472012-11-21 12:06:18 -08001992 int start_sreg = cu->num_regs;
buzbee2cfc6392012-05-07 14:51:40 -07001993
1994 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
buzbeefa57c472012-11-21 12:06:18 -08001995 arg_iter->setName(StringPrintf("v%i_0", start_sreg));
1996 start_sreg += cu->reg_location[start_sreg].wide ? 2 : 1;
buzbee2cfc6392012-05-07 14:51:40 -07001997 }
1998
1999 return true;
2000}
2001
buzbeefa57c472012-11-21 12:06:18 -08002002static bool CreateLLVMBasicBlock(CompilationUnit* cu, BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07002003{
2004 // Skip the exit block
buzbeefa57c472012-11-21 12:06:18 -08002005 if ((bb->block_type == kDead) ||(bb->block_type == kExitBlock)) {
2006 cu->id_to_block_map.Put(bb->id, NULL);
buzbee2cfc6392012-05-07 14:51:40 -07002007 } else {
buzbeefa57c472012-11-21 12:06:18 -08002008 int offset = bb->start_offset;
2009 bool entry_block = (bb->block_type == kEntryBlock);
2010 llvm::BasicBlock* llvm_bb =
2011 llvm::BasicBlock::Create(*cu->context, entry_block ? "entry" :
2012 StringPrintf(kLabelFormat, bb->catch_entry ? kCatchBlock :
2013 kNormalBlock, offset, bb->id), cu->func);
2014 if (entry_block) {
2015 cu->entry_bb = llvm_bb;
2016 cu->placeholder_bb =
2017 llvm::BasicBlock::Create(*cu->context, "placeholder",
2018 cu->func);
buzbee2cfc6392012-05-07 14:51:40 -07002019 }
buzbeefa57c472012-11-21 12:06:18 -08002020 cu->id_to_block_map.Put(bb->id, llvm_bb);
buzbee2cfc6392012-05-07 14:51:40 -07002021 }
2022 return false;
2023}
2024
2025
2026/*
2027 * Convert MIR to LLVM_IR
2028 * o For each ssa name, create LLVM named value. Type these
2029 * appropriately, and ignore high half of wide and double operands.
2030 * o For each MIR basic block, create an LLVM basic block.
2031 * o Iterate through the MIR a basic block at a time, setting arguments
2032 * to recovered ssa name.
2033 */
buzbeefa57c472012-11-21 12:06:18 -08002034void MethodMIR2Bitcode(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -07002035{
buzbeefa57c472012-11-21 12:06:18 -08002036 InitIR(cu);
2037 CompilerInitGrowableList(cu, &cu->llvm_values, cu->num_ssa_regs);
buzbee2cfc6392012-05-07 14:51:40 -07002038
2039 // Create the function
buzbeefa57c472012-11-21 12:06:18 -08002040 CreateFunction(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002041
2042 // Create an LLVM basic block for each MIR block in dfs preorder
buzbeefa57c472012-11-21 12:06:18 -08002043 DataFlowAnalysisDispatcher(cu, CreateLLVMBasicBlock,
2044 kPreOrderDFSTraversal, false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -07002045 /*
2046 * Create an llvm named value for each MIR SSA name. Note: we'll use
2047 * placeholders for all non-argument values (because we haven't seen
2048 * the definition yet).
2049 */
buzbeefa57c472012-11-21 12:06:18 -08002050 cu->irb->SetInsertPoint(cu->placeholder_bb);
2051 llvm::Function::arg_iterator arg_iter(cu->func->arg_begin());
buzbee2cfc6392012-05-07 14:51:40 -07002052 arg_iter++; /* Skip path method */
buzbeefa57c472012-11-21 12:06:18 -08002053 for (int i = 0; i < cu->num_ssa_regs; i++) {
buzbee2cfc6392012-05-07 14:51:40 -07002054 llvm::Value* val;
buzbeefa57c472012-11-21 12:06:18 -08002055 RegLocation rl_temp = cu->reg_location[i];
2056 if ((SRegToVReg(cu, i) < 0) || rl_temp.high_word) {
2057 InsertGrowableList(cu, &cu->llvm_values, 0);
2058 } else if ((i < cu->num_regs) ||
2059 (i >= (cu->num_regs + cu->num_ins))) {
2060 llvm::Constant* imm_value = cu->reg_location[i].wide ?
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002061 cu->irb->getJLong(0) : cu->irb->getJInt(0);
buzbeefa57c472012-11-21 12:06:18 -08002062 val = EmitConst(cu, imm_value, cu->reg_location[i]);
2063 val->setName(LlvmSSAName(cu, i));
2064 InsertGrowableList(cu, &cu->llvm_values, reinterpret_cast<uintptr_t>(val));
buzbee2cfc6392012-05-07 14:51:40 -07002065 } else {
2066 // Recover previously-created argument values
buzbeefa57c472012-11-21 12:06:18 -08002067 llvm::Value* arg_val = arg_iter++;
2068 InsertGrowableList(cu, &cu->llvm_values, reinterpret_cast<uintptr_t>(arg_val));
buzbee2cfc6392012-05-07 14:51:40 -07002069 }
2070 }
buzbee2cfc6392012-05-07 14:51:40 -07002071
buzbeefa57c472012-11-21 12:06:18 -08002072 DataFlowAnalysisDispatcher(cu, BlockBitcodeConversion,
buzbee2cfc6392012-05-07 14:51:40 -07002073 kPreOrderDFSTraversal, false /* Iterative */);
2074
buzbee4be777b2012-07-12 14:38:18 -07002075 /*
2076 * In a few rare cases of verification failure, the verifier will
2077 * replace one or more Dalvik opcodes with the special
2078 * throw-verification-failure opcode. This can leave the SSA graph
2079 * in an invalid state, as definitions may be lost, while uses retained.
2080 * To work around this problem, we insert placeholder definitions for
2081 * all Dalvik SSA regs in the "placeholder" block. Here, after
2082 * bitcode conversion is complete, we examine those placeholder definitions
2083 * and delete any with no references (which normally is all of them).
2084 *
2085 * If any definitions remain, we link the placeholder block into the
2086 * CFG. Otherwise, it is deleted.
2087 */
buzbeefa57c472012-11-21 12:06:18 -08002088 for (llvm::BasicBlock::iterator it = cu->placeholder_bb->begin(),
2089 it_end = cu->placeholder_bb->end(); it != it_end;) {
buzbee4be777b2012-07-12 14:38:18 -07002090 llvm::Instruction* inst = llvm::dyn_cast<llvm::Instruction>(it++);
2091 DCHECK(inst != NULL);
2092 llvm::Value* val = llvm::dyn_cast<llvm::Value>(inst);
2093 DCHECK(val != NULL);
2094 if (val->getNumUses() == 0) {
2095 inst->eraseFromParent();
2096 }
2097 }
buzbeefa57c472012-11-21 12:06:18 -08002098 SetDexOffset(cu, 0);
2099 if (cu->placeholder_bb->empty()) {
2100 cu->placeholder_bb->eraseFromParent();
buzbee4be777b2012-07-12 14:38:18 -07002101 } else {
buzbeefa57c472012-11-21 12:06:18 -08002102 cu->irb->SetInsertPoint(cu->placeholder_bb);
2103 cu->irb->CreateBr(cu->entryTarget_bb);
2104 cu->entryTarget_bb = cu->placeholder_bb;
buzbee4be777b2012-07-12 14:38:18 -07002105 }
buzbeefa57c472012-11-21 12:06:18 -08002106 cu->irb->SetInsertPoint(cu->entry_bb);
2107 cu->irb->CreateBr(cu->entryTarget_bb);
buzbee2cfc6392012-05-07 14:51:40 -07002108
buzbeefa57c472012-11-21 12:06:18 -08002109 if (cu->enable_debug & (1 << kDebugVerifyBitcode)) {
2110 if (llvm::verifyFunction(*cu->func, llvm::PrintMessageAction)) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002111 LOG(INFO) << "Bitcode verification FAILED for "
buzbeefa57c472012-11-21 12:06:18 -08002112 << PrettyMethod(cu->method_idx, *cu->dex_file)
2113 << " of size " << cu->insns_size;
2114 cu->enable_debug |= (1 << kDebugDumpBitcodeFile);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -07002115 }
2116 }
buzbee2cfc6392012-05-07 14:51:40 -07002117
buzbeefa57c472012-11-21 12:06:18 -08002118 if (cu->enable_debug & (1 << kDebugDumpBitcodeFile)) {
buzbeead8f15e2012-06-18 14:49:45 -07002119 // Write bitcode to file
2120 std::string errmsg;
buzbeefa57c472012-11-21 12:06:18 -08002121 std::string fname(PrettyMethod(cu->method_idx, *cu->dex_file));
buzbee52a77fc2012-11-20 19:50:46 -08002122 ReplaceSpecialChars(fname);
buzbee6459e7c2012-10-02 14:42:41 -07002123 // TODO: make configurable change naming mechanism to avoid fname length issues.
buzbee4f1181f2012-06-22 13:52:12 -07002124 fname = StringPrintf("/sdcard/Bitcode/%s.bc", fname.c_str());
buzbee2cfc6392012-05-07 14:51:40 -07002125
buzbee6459e7c2012-10-02 14:42:41 -07002126 if (fname.size() > 240) {
2127 LOG(INFO) << "Warning: bitcode filename too long. Truncated.";
2128 fname.resize(240);
2129 }
2130
buzbeead8f15e2012-06-18 14:49:45 -07002131 llvm::OwningPtr<llvm::tool_output_file> out_file(
2132 new llvm::tool_output_file(fname.c_str(), errmsg,
2133 llvm::raw_fd_ostream::F_Binary));
buzbee2cfc6392012-05-07 14:51:40 -07002134
buzbeead8f15e2012-06-18 14:49:45 -07002135 if (!errmsg.empty()) {
2136 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
2137 }
2138
buzbeefa57c472012-11-21 12:06:18 -08002139 llvm::WriteBitcodeToFile(cu->module, out_file->os());
buzbeead8f15e2012-06-18 14:49:45 -07002140 out_file->keep();
buzbee6969d502012-06-15 16:40:31 -07002141 }
buzbee2cfc6392012-05-07 14:51:40 -07002142}
2143
buzbeefa57c472012-11-21 12:06:18 -08002144static RegLocation GetLoc(CompilationUnit* cu, llvm::Value* val) {
buzbee2cfc6392012-05-07 14:51:40 -07002145 RegLocation res;
buzbeeb03f4872012-06-11 15:22:11 -07002146 DCHECK(val != NULL);
buzbeefa57c472012-11-21 12:06:18 -08002147 SafeMap<llvm::Value*, RegLocation>::iterator it = cu->loc_map.find(val);
2148 if (it == cu->loc_map.end()) {
2149 std::string val_name = val->getName().str();
2150 if (val_name.empty()) {
buzbee101305f2012-06-28 18:00:56 -07002151 // FIXME: need to be more robust, handle FP and be in a position to
2152 // manage unnamed temps whose lifetimes span basic block boundaries
buzbee4f1181f2012-06-22 13:52:12 -07002153 UNIMPLEMENTED(WARNING) << "Need to handle unnamed llvm temps";
2154 memset(&res, 0, sizeof(res));
2155 res.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08002156 res.low_reg = AllocTemp(cu);
buzbee4f1181f2012-06-22 13:52:12 -07002157 res.home = true;
buzbeefa57c472012-11-21 12:06:18 -08002158 res.s_reg_low = INVALID_SREG;
2159 res.orig_sreg = INVALID_SREG;
buzbee101305f2012-06-28 18:00:56 -07002160 llvm::Type* ty = val->getType();
buzbeefa57c472012-11-21 12:06:18 -08002161 res.wide = ((ty == cu->irb->getInt64Ty()) ||
2162 (ty == cu->irb->getDoubleTy()));
buzbee101305f2012-06-28 18:00:56 -07002163 if (res.wide) {
buzbeefa57c472012-11-21 12:06:18 -08002164 res.high_reg = AllocTemp(cu);
buzbee101305f2012-06-28 18:00:56 -07002165 }
buzbeefa57c472012-11-21 12:06:18 -08002166 cu->loc_map.Put(val, res);
buzbee32412962012-06-26 16:27:56 -07002167 } else {
buzbeefa57c472012-11-21 12:06:18 -08002168 DCHECK_EQ(val_name[0], 'v');
2169 int base_sreg = INVALID_SREG;
2170 sscanf(val_name.c_str(), "v%d_", &base_sreg);
2171 res = cu->reg_location[base_sreg];
2172 cu->loc_map.Put(val, res);
buzbee2cfc6392012-05-07 14:51:40 -07002173 }
2174 } else {
2175 res = it->second;
2176 }
2177 return res;
2178}
2179
buzbeefa57c472012-11-21 12:06:18 -08002180static Instruction::Code GetDalvikOpcode(OpKind op, bool is_const, bool is_wide)
buzbee2cfc6392012-05-07 14:51:40 -07002181{
2182 Instruction::Code res = Instruction::NOP;
buzbeefa57c472012-11-21 12:06:18 -08002183 if (is_wide) {
buzbee2cfc6392012-05-07 14:51:40 -07002184 switch(op) {
2185 case kOpAdd: res = Instruction::ADD_LONG; break;
2186 case kOpSub: res = Instruction::SUB_LONG; break;
2187 case kOpMul: res = Instruction::MUL_LONG; break;
2188 case kOpDiv: res = Instruction::DIV_LONG; break;
2189 case kOpRem: res = Instruction::REM_LONG; break;
2190 case kOpAnd: res = Instruction::AND_LONG; break;
2191 case kOpOr: res = Instruction::OR_LONG; break;
2192 case kOpXor: res = Instruction::XOR_LONG; break;
2193 case kOpLsl: res = Instruction::SHL_LONG; break;
2194 case kOpLsr: res = Instruction::USHR_LONG; break;
2195 case kOpAsr: res = Instruction::SHR_LONG; break;
2196 default: LOG(FATAL) << "Unexpected OpKind " << op;
2197 }
buzbeefa57c472012-11-21 12:06:18 -08002198 } else if (is_const){
buzbee2cfc6392012-05-07 14:51:40 -07002199 switch(op) {
2200 case kOpAdd: res = Instruction::ADD_INT_LIT16; break;
2201 case kOpSub: res = Instruction::RSUB_INT_LIT8; break;
2202 case kOpMul: res = Instruction::MUL_INT_LIT16; break;
2203 case kOpDiv: res = Instruction::DIV_INT_LIT16; break;
2204 case kOpRem: res = Instruction::REM_INT_LIT16; break;
2205 case kOpAnd: res = Instruction::AND_INT_LIT16; break;
2206 case kOpOr: res = Instruction::OR_INT_LIT16; break;
2207 case kOpXor: res = Instruction::XOR_INT_LIT16; break;
2208 case kOpLsl: res = Instruction::SHL_INT_LIT8; break;
2209 case kOpLsr: res = Instruction::USHR_INT_LIT8; break;
2210 case kOpAsr: res = Instruction::SHR_INT_LIT8; break;
2211 default: LOG(FATAL) << "Unexpected OpKind " << op;
2212 }
2213 } else {
2214 switch(op) {
2215 case kOpAdd: res = Instruction::ADD_INT; break;
2216 case kOpSub: res = Instruction::SUB_INT; break;
2217 case kOpMul: res = Instruction::MUL_INT; break;
2218 case kOpDiv: res = Instruction::DIV_INT; break;
2219 case kOpRem: res = Instruction::REM_INT; break;
2220 case kOpAnd: res = Instruction::AND_INT; break;
2221 case kOpOr: res = Instruction::OR_INT; break;
2222 case kOpXor: res = Instruction::XOR_INT; break;
2223 case kOpLsl: res = Instruction::SHL_INT; break;
2224 case kOpLsr: res = Instruction::USHR_INT; break;
2225 case kOpAsr: res = Instruction::SHR_INT; break;
2226 default: LOG(FATAL) << "Unexpected OpKind " << op;
2227 }
2228 }
2229 return res;
2230}
2231
buzbeefa57c472012-11-21 12:06:18 -08002232static Instruction::Code GetDalvikFPOpcode(OpKind op, bool is_const, bool is_wide)
buzbee4f1181f2012-06-22 13:52:12 -07002233{
2234 Instruction::Code res = Instruction::NOP;
buzbeefa57c472012-11-21 12:06:18 -08002235 if (is_wide) {
buzbee4f1181f2012-06-22 13:52:12 -07002236 switch(op) {
2237 case kOpAdd: res = Instruction::ADD_DOUBLE; break;
2238 case kOpSub: res = Instruction::SUB_DOUBLE; break;
2239 case kOpMul: res = Instruction::MUL_DOUBLE; break;
2240 case kOpDiv: res = Instruction::DIV_DOUBLE; break;
2241 case kOpRem: res = Instruction::REM_DOUBLE; break;
2242 default: LOG(FATAL) << "Unexpected OpKind " << op;
2243 }
2244 } else {
2245 switch(op) {
2246 case kOpAdd: res = Instruction::ADD_FLOAT; break;
2247 case kOpSub: res = Instruction::SUB_FLOAT; break;
2248 case kOpMul: res = Instruction::MUL_FLOAT; break;
2249 case kOpDiv: res = Instruction::DIV_FLOAT; break;
2250 case kOpRem: res = Instruction::REM_FLOAT; break;
2251 default: LOG(FATAL) << "Unexpected OpKind " << op;
2252 }
2253 }
2254 return res;
2255}
2256
buzbeefa57c472012-11-21 12:06:18 -08002257static void CvtBinFPOp(CompilationUnit* cu, OpKind op, llvm::Instruction* inst)
buzbee4f1181f2012-06-22 13:52:12 -07002258{
buzbee02031b12012-11-23 09:41:35 -08002259 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002260 RegLocation rl_dest = GetLoc(cu, inst);
buzbee4f4dfc72012-07-02 14:54:44 -07002261 /*
2262 * Normally, we won't ever generate an FP operation with an immediate
2263 * operand (not supported in Dex instruction set). However, the IR builder
buzbeefa57c472012-11-21 12:06:18 -08002264 * may insert them - in particular for create_neg_fp. Recognize this case
buzbee4f4dfc72012-07-02 14:54:44 -07002265 * and deal with it.
2266 */
2267 llvm::ConstantFP* op1C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(0));
2268 llvm::ConstantFP* op2C = llvm::dyn_cast<llvm::ConstantFP>(inst->getOperand(1));
2269 DCHECK(op2C == NULL);
2270 if ((op1C != NULL) && (op == kOpSub)) {
buzbeefa57c472012-11-21 12:06:18 -08002271 RegLocation rl_src = GetLoc(cu, inst->getOperand(1));
2272 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002273 cg->GenArithOpDouble(cu, Instruction::NEG_DOUBLE, rl_dest, rl_src, rl_src);
buzbee4f4dfc72012-07-02 14:54:44 -07002274 } else {
buzbee02031b12012-11-23 09:41:35 -08002275 cg->GenArithOpFloat(cu, Instruction::NEG_FLOAT, rl_dest, rl_src, rl_src);
buzbee4f4dfc72012-07-02 14:54:44 -07002276 }
buzbee4f1181f2012-06-22 13:52:12 -07002277 } else {
buzbee4f4dfc72012-07-02 14:54:44 -07002278 DCHECK(op1C == NULL);
buzbeefa57c472012-11-21 12:06:18 -08002279 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(0));
2280 RegLocation rl_src2 = GetLoc(cu, inst->getOperand(1));
2281 Instruction::Code dalvik_op = GetDalvikFPOpcode(op, false, rl_dest.wide);
2282 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002283 cg->GenArithOpDouble(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee4f4dfc72012-07-02 14:54:44 -07002284 } else {
buzbee02031b12012-11-23 09:41:35 -08002285 cg->GenArithOpFloat(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee4f4dfc72012-07-02 14:54:44 -07002286 }
buzbee4f1181f2012-06-22 13:52:12 -07002287 }
2288}
2289
buzbeefa57c472012-11-21 12:06:18 -08002290static void CvtIntNarrowing(CompilationUnit* cu, llvm::Instruction* inst,
buzbee101305f2012-06-28 18:00:56 -07002291 Instruction::Code opcode)
2292{
buzbee02031b12012-11-23 09:41:35 -08002293 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002294 RegLocation rl_dest = GetLoc(cu, inst);
2295 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002296 cg->GenIntNarrowing(cu, opcode, rl_dest, rl_src);
buzbee101305f2012-06-28 18:00:56 -07002297}
2298
buzbeefa57c472012-11-21 12:06:18 -08002299static void CvtIntToFP(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002300{
buzbee02031b12012-11-23 09:41:35 -08002301 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002302 RegLocation rl_dest = GetLoc(cu, inst);
2303 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002304 Instruction::Code opcode;
buzbeefa57c472012-11-21 12:06:18 -08002305 if (rl_dest.wide) {
2306 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002307 opcode = Instruction::LONG_TO_DOUBLE;
2308 } else {
2309 opcode = Instruction::INT_TO_DOUBLE;
2310 }
2311 } else {
buzbeefa57c472012-11-21 12:06:18 -08002312 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002313 opcode = Instruction::LONG_TO_FLOAT;
2314 } else {
2315 opcode = Instruction::INT_TO_FLOAT;
2316 }
2317 }
buzbee02031b12012-11-23 09:41:35 -08002318 cg->GenConversion(cu, opcode, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002319}
2320
buzbeefa57c472012-11-21 12:06:18 -08002321static void CvtFPToInt(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002322{
buzbee02031b12012-11-23 09:41:35 -08002323 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002324 RegLocation rl_dest = GetLoc(cu, call_inst);
2325 RegLocation rl_src = GetLoc(cu, call_inst->getOperand(0));
buzbee76592632012-06-29 15:18:35 -07002326 Instruction::Code opcode;
buzbeefa57c472012-11-21 12:06:18 -08002327 if (rl_dest.wide) {
2328 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002329 opcode = Instruction::DOUBLE_TO_LONG;
2330 } else {
2331 opcode = Instruction::FLOAT_TO_LONG;
2332 }
2333 } else {
buzbeefa57c472012-11-21 12:06:18 -08002334 if (rl_src.wide) {
buzbee76592632012-06-29 15:18:35 -07002335 opcode = Instruction::DOUBLE_TO_INT;
2336 } else {
2337 opcode = Instruction::FLOAT_TO_INT;
2338 }
2339 }
buzbee02031b12012-11-23 09:41:35 -08002340 cg->GenConversion(cu, opcode, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002341}
2342
buzbeefa57c472012-11-21 12:06:18 -08002343static void CvtFloatToDouble(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002344{
buzbee02031b12012-11-23 09:41:35 -08002345 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002346 RegLocation rl_dest = GetLoc(cu, inst);
2347 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002348 cg->GenConversion(cu, Instruction::FLOAT_TO_DOUBLE, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002349}
2350
buzbeefa57c472012-11-21 12:06:18 -08002351static void CvtTrunc(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002352{
buzbee02031b12012-11-23 09:41:35 -08002353 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002354 RegLocation rl_dest = GetLoc(cu, inst);
2355 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
2356 rl_src = UpdateLocWide(cu, rl_src);
2357 rl_src = WideToNarrow(cu, rl_src);
buzbee02031b12012-11-23 09:41:35 -08002358 cg->StoreValue(cu, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002359}
2360
buzbeefa57c472012-11-21 12:06:18 -08002361static void CvtDoubleToFloat(CompilationUnit* cu, llvm::Instruction* inst)
buzbee76592632012-06-29 15:18:35 -07002362{
buzbee02031b12012-11-23 09:41:35 -08002363 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002364 RegLocation rl_dest = GetLoc(cu, inst);
2365 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002366 cg->GenConversion(cu, Instruction::DOUBLE_TO_FLOAT, rl_dest, rl_src);
buzbee76592632012-06-29 15:18:35 -07002367}
2368
2369
buzbeefa57c472012-11-21 12:06:18 -08002370static void CvtIntExt(CompilationUnit* cu, llvm::Instruction* inst, bool is_signed)
buzbee101305f2012-06-28 18:00:56 -07002371{
buzbee02031b12012-11-23 09:41:35 -08002372 Codegen* cg = cu->cg.get();
buzbee101305f2012-06-28 18:00:56 -07002373 // TODO: evaluate src/tgt types and add general support for more than int to long
buzbeefa57c472012-11-21 12:06:18 -08002374 RegLocation rl_dest = GetLoc(cu, inst);
2375 RegLocation rl_src = GetLoc(cu, inst->getOperand(0));
2376 DCHECK(rl_dest.wide);
2377 DCHECK(!rl_src.wide);
2378 DCHECK(!rl_dest.fp);
2379 DCHECK(!rl_src.fp);
2380 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
2381 if (rl_src.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -08002382 cg->OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
buzbee101305f2012-06-28 18:00:56 -07002383 } else {
buzbee02031b12012-11-23 09:41:35 -08002384 cg->LoadValueDirect(cu, rl_src, rl_result.low_reg);
buzbee101305f2012-06-28 18:00:56 -07002385 }
buzbeefa57c472012-11-21 12:06:18 -08002386 if (is_signed) {
buzbee02031b12012-11-23 09:41:35 -08002387 cg->OpRegRegImm(cu, kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
buzbee101305f2012-06-28 18:00:56 -07002388 } else {
buzbee02031b12012-11-23 09:41:35 -08002389 cg->LoadConstant(cu, rl_result.high_reg, 0);
buzbee101305f2012-06-28 18:00:56 -07002390 }
buzbee02031b12012-11-23 09:41:35 -08002391 cg->StoreValueWide(cu, rl_dest, rl_result);
buzbee101305f2012-06-28 18:00:56 -07002392}
2393
buzbeefa57c472012-11-21 12:06:18 -08002394static void CvtBinOp(CompilationUnit* cu, OpKind op, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002395{
buzbee02031b12012-11-23 09:41:35 -08002396 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002397 RegLocation rl_dest = GetLoc(cu, inst);
buzbee2cfc6392012-05-07 14:51:40 -07002398 llvm::Value* lhs = inst->getOperand(0);
buzbeef58c12c2012-07-03 15:06:29 -07002399 // Special-case RSUB/NEG
buzbeefa57c472012-11-21 12:06:18 -08002400 llvm::ConstantInt* lhs_imm = llvm::dyn_cast<llvm::ConstantInt>(lhs);
2401 if ((op == kOpSub) && (lhs_imm != NULL)) {
2402 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(1));
2403 if (rl_src1.wide) {
2404 DCHECK_EQ(lhs_imm->getSExtValue(), 0);
buzbee02031b12012-11-23 09:41:35 -08002405 cg->GenArithOpLong(cu, Instruction::NEG_LONG, rl_dest, rl_src1, rl_src1);
buzbeef58c12c2012-07-03 15:06:29 -07002406 } else {
buzbee02031b12012-11-23 09:41:35 -08002407 cg->GenArithOpIntLit(cu, Instruction::RSUB_INT, rl_dest, rl_src1,
buzbeefa57c472012-11-21 12:06:18 -08002408 lhs_imm->getSExtValue());
buzbeef58c12c2012-07-03 15:06:29 -07002409 }
buzbee4f1181f2012-06-22 13:52:12 -07002410 return;
2411 }
buzbeefa57c472012-11-21 12:06:18 -08002412 DCHECK(lhs_imm == NULL);
2413 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(0));
buzbee2cfc6392012-05-07 14:51:40 -07002414 llvm::Value* rhs = inst->getOperand(1);
buzbeefa57c472012-11-21 12:06:18 -08002415 llvm::ConstantInt* const_rhs = llvm::dyn_cast<llvm::ConstantInt>(rhs);
2416 if (!rl_dest.wide && (const_rhs != NULL)) {
2417 Instruction::Code dalvik_op = GetDalvikOpcode(op, true, false);
buzbee02031b12012-11-23 09:41:35 -08002418 cg->GenArithOpIntLit(cu, dalvik_op, rl_dest, rl_src1, const_rhs->getSExtValue());
buzbee2cfc6392012-05-07 14:51:40 -07002419 } else {
buzbeefa57c472012-11-21 12:06:18 -08002420 Instruction::Code dalvik_op = GetDalvikOpcode(op, false, rl_dest.wide);
2421 RegLocation rl_src2;
2422 if (const_rhs != NULL) {
buzbee63ebbb62012-08-03 14:05:41 -07002423 // ir_builder converts NOT_LONG to xor src, -1. Restore
buzbeefa57c472012-11-21 12:06:18 -08002424 DCHECK_EQ(dalvik_op, Instruction::XOR_LONG);
2425 DCHECK_EQ(-1L, const_rhs->getSExtValue());
2426 dalvik_op = Instruction::NOT_LONG;
2427 rl_src2 = rl_src1;
buzbee9a2487f2012-07-26 14:01:13 -07002428 } else {
buzbeefa57c472012-11-21 12:06:18 -08002429 rl_src2 = GetLoc(cu, rhs);
buzbee9a2487f2012-07-26 14:01:13 -07002430 }
buzbeefa57c472012-11-21 12:06:18 -08002431 if (rl_dest.wide) {
buzbee02031b12012-11-23 09:41:35 -08002432 cg->GenArithOpLong(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee2cfc6392012-05-07 14:51:40 -07002433 } else {
buzbee02031b12012-11-23 09:41:35 -08002434 cg->GenArithOpInt(cu, dalvik_op, rl_dest, rl_src1, rl_src2);
buzbee2cfc6392012-05-07 14:51:40 -07002435 }
2436 }
2437}
2438
buzbeefa57c472012-11-21 12:06:18 -08002439static void CvtShiftOp(CompilationUnit* cu, Instruction::Code opcode, llvm::CallInst* call_inst)
buzbee101305f2012-06-28 18:00:56 -07002440{
buzbee02031b12012-11-23 09:41:35 -08002441 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002442 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2443 RegLocation rl_dest = GetLoc(cu, call_inst);
2444 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(0));
2445 llvm::Value* rhs = call_inst->getArgOperand(1);
buzbee2a83e8f2012-07-13 16:42:30 -07002446 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
buzbeefa57c472012-11-21 12:06:18 -08002447 DCHECK(!rl_dest.wide);
buzbee02031b12012-11-23 09:41:35 -08002448 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src, src2->getSExtValue());
buzbee101305f2012-06-28 18:00:56 -07002449 } else {
buzbeefa57c472012-11-21 12:06:18 -08002450 RegLocation rl_shift = GetLoc(cu, rhs);
2451 if (call_inst->getType() == cu->irb->getInt64Ty()) {
buzbee02031b12012-11-23 09:41:35 -08002452 cg->GenShiftOpLong(cu, opcode, rl_dest, rl_src, rl_shift);
buzbee2a83e8f2012-07-13 16:42:30 -07002453 } else {
buzbee02031b12012-11-23 09:41:35 -08002454 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src, rl_shift);
buzbee2a83e8f2012-07-13 16:42:30 -07002455 }
buzbee101305f2012-06-28 18:00:56 -07002456 }
2457}
2458
buzbeefa57c472012-11-21 12:06:18 -08002459static void CvtBr(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002460{
buzbee02031b12012-11-23 09:41:35 -08002461 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002462 llvm::BranchInst* br_inst = llvm::dyn_cast<llvm::BranchInst>(inst);
2463 DCHECK(br_inst != NULL);
2464 DCHECK(br_inst->isUnconditional()); // May change - but this is all we use now
2465 llvm::BasicBlock* target_bb = br_inst->getSuccessor(0);
buzbee02031b12012-11-23 09:41:35 -08002466 cg->OpUnconditionalBranch(cu, cu->block_to_label_map.Get(target_bb));
buzbee2cfc6392012-05-07 14:51:40 -07002467}
2468
buzbeefa57c472012-11-21 12:06:18 -08002469static void CvtPhi(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002470{
2471 // Nop - these have already been processed
2472}
2473
buzbeefa57c472012-11-21 12:06:18 -08002474static void CvtRet(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002475{
buzbee02031b12012-11-23 09:41:35 -08002476 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002477 llvm::ReturnInst* ret_inst = llvm::dyn_cast<llvm::ReturnInst>(inst);
2478 llvm::Value* ret_val = ret_inst->getReturnValue();
2479 if (ret_val != NULL) {
2480 RegLocation rl_src = GetLoc(cu, ret_val);
2481 if (rl_src.wide) {
buzbee02031b12012-11-23 09:41:35 -08002482 cg->StoreValueWide(cu, GetReturnWide(cu, rl_src.fp), rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002483 } else {
buzbee02031b12012-11-23 09:41:35 -08002484 cg->StoreValue(cu, GetReturn(cu, rl_src.fp), rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002485 }
2486 }
buzbee02031b12012-11-23 09:41:35 -08002487 cg->GenExitSequence(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002488}
2489
buzbeefa57c472012-11-21 12:06:18 -08002490static ConditionCode GetCond(llvm::ICmpInst::Predicate llvm_cond)
buzbee2cfc6392012-05-07 14:51:40 -07002491{
2492 ConditionCode res = kCondAl;
buzbeefa57c472012-11-21 12:06:18 -08002493 switch(llvm_cond) {
buzbee6969d502012-06-15 16:40:31 -07002494 case llvm::ICmpInst::ICMP_EQ: res = kCondEq; break;
buzbee4f1181f2012-06-22 13:52:12 -07002495 case llvm::ICmpInst::ICMP_NE: res = kCondNe; break;
2496 case llvm::ICmpInst::ICMP_SLT: res = kCondLt; break;
2497 case llvm::ICmpInst::ICMP_SGE: res = kCondGe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002498 case llvm::ICmpInst::ICMP_SGT: res = kCondGt; break;
buzbee4f1181f2012-06-22 13:52:12 -07002499 case llvm::ICmpInst::ICMP_SLE: res = kCondLe; break;
buzbee2cfc6392012-05-07 14:51:40 -07002500 default: LOG(FATAL) << "Unexpected llvm condition";
2501 }
2502 return res;
2503}
2504
buzbeefa57c472012-11-21 12:06:18 -08002505static void CvtICmp(CompilationUnit* cu, llvm::Instruction* inst)
buzbee2cfc6392012-05-07 14:51:40 -07002506{
buzbee02031b12012-11-23 09:41:35 -08002507 // cg->GenCmpLong(cu, rl_dest, rl_src1, rl_src2)
buzbee2cfc6392012-05-07 14:51:40 -07002508 UNIMPLEMENTED(FATAL);
2509}
2510
buzbeefa57c472012-11-21 12:06:18 -08002511static void CvtICmpBr(CompilationUnit* cu, llvm::Instruction* inst,
2512 llvm::BranchInst* br_inst)
buzbee2cfc6392012-05-07 14:51:40 -07002513{
buzbee02031b12012-11-23 09:41:35 -08002514 Codegen* cg = cu->cg.get();
buzbee2cfc6392012-05-07 14:51:40 -07002515 // Get targets
buzbeefa57c472012-11-21 12:06:18 -08002516 llvm::BasicBlock* taken_bb = br_inst->getSuccessor(0);
2517 LIR* taken = cu->block_to_label_map.Get(taken_bb);
2518 llvm::BasicBlock* fallthrough_bb = br_inst->getSuccessor(1);
2519 LIR* fall_through = cu->block_to_label_map.Get(fallthrough_bb);
buzbee2cfc6392012-05-07 14:51:40 -07002520 // Get comparison operands
buzbeefa57c472012-11-21 12:06:18 -08002521 llvm::ICmpInst* i_cmp_inst = llvm::dyn_cast<llvm::ICmpInst>(inst);
2522 ConditionCode cond = GetCond(i_cmp_inst->getPredicate());
2523 llvm::Value* lhs = i_cmp_inst->getOperand(0);
buzbee2cfc6392012-05-07 14:51:40 -07002524 // Not expecting a constant as 1st operand
2525 DCHECK(llvm::dyn_cast<llvm::ConstantInt>(lhs) == NULL);
buzbeefa57c472012-11-21 12:06:18 -08002526 RegLocation rl_src1 = GetLoc(cu, inst->getOperand(0));
buzbee02031b12012-11-23 09:41:35 -08002527 rl_src1 = cg->LoadValue(cu, rl_src1, kCoreReg);
buzbee2cfc6392012-05-07 14:51:40 -07002528 llvm::Value* rhs = inst->getOperand(1);
buzbeefa57c472012-11-21 12:06:18 -08002529 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -07002530 // Compare and branch in one shot
2531 UNIMPLEMENTED(FATAL);
2532 }
buzbee2cfc6392012-05-07 14:51:40 -07002533 //Compare, then branch
2534 // TODO: handle fused CMP_LONG/IF_xxZ case
2535 if (llvm::ConstantInt* src2 = llvm::dyn_cast<llvm::ConstantInt>(rhs)) {
buzbee02031b12012-11-23 09:41:35 -08002536 cg->OpRegImm(cu, kOpCmp, rl_src1.low_reg, src2->getSExtValue());
buzbeed5018892012-07-11 14:23:40 -07002537 } else if (llvm::dyn_cast<llvm::ConstantPointerNull>(rhs) != NULL) {
buzbee02031b12012-11-23 09:41:35 -08002538 cg->OpRegImm(cu, kOpCmp, rl_src1.low_reg, 0);
buzbee2cfc6392012-05-07 14:51:40 -07002539 } else {
buzbeefa57c472012-11-21 12:06:18 -08002540 RegLocation rl_src2 = GetLoc(cu, rhs);
buzbee02031b12012-11-23 09:41:35 -08002541 rl_src2 = cg->LoadValue(cu, rl_src2, kCoreReg);
2542 cg->OpRegReg(cu, kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
buzbee2cfc6392012-05-07 14:51:40 -07002543 }
buzbee02031b12012-11-23 09:41:35 -08002544 cg->OpCondBranch(cu, cond, taken);
buzbee2cfc6392012-05-07 14:51:40 -07002545 // Fallthrough
buzbee02031b12012-11-23 09:41:35 -08002546 cg->OpUnconditionalBranch(cu, fall_through);
buzbee2cfc6392012-05-07 14:51:40 -07002547}
2548
buzbeefa57c472012-11-21 12:06:18 -08002549static void CvtCopy(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee2cfc6392012-05-07 14:51:40 -07002550{
buzbee02031b12012-11-23 09:41:35 -08002551 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002552 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2553 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(0));
2554 RegLocation rl_dest = GetLoc(cu, call_inst);
2555 DCHECK_EQ(rl_src.wide, rl_dest.wide);
2556 DCHECK_EQ(rl_src.fp, rl_dest.fp);
2557 if (rl_src.wide) {
buzbee02031b12012-11-23 09:41:35 -08002558 cg->StoreValueWide(cu, rl_dest, rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002559 } else {
buzbee02031b12012-11-23 09:41:35 -08002560 cg->StoreValue(cu, rl_dest, rl_src);
buzbee2cfc6392012-05-07 14:51:40 -07002561 }
2562}
2563
2564// Note: Immediate arg is a ConstantInt regardless of result type
buzbeefa57c472012-11-21 12:06:18 -08002565static void CvtConst(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee2cfc6392012-05-07 14:51:40 -07002566{
buzbee02031b12012-11-23 09:41:35 -08002567 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002568 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
buzbee2cfc6392012-05-07 14:51:40 -07002569 llvm::ConstantInt* src =
buzbeefa57c472012-11-21 12:06:18 -08002570 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
buzbee2cfc6392012-05-07 14:51:40 -07002571 uint64_t immval = src->getZExtValue();
buzbeefa57c472012-11-21 12:06:18 -08002572 RegLocation rl_dest = GetLoc(cu, call_inst);
2573 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
2574 if (rl_dest.wide) {
buzbee4ef3e452012-12-14 13:35:28 -08002575 cg->LoadConstantWide(cu, rl_result.low_reg, rl_result.high_reg, immval);
buzbee02031b12012-11-23 09:41:35 -08002576 cg->StoreValueWide(cu, rl_dest, rl_result);
buzbee2cfc6392012-05-07 14:51:40 -07002577 } else {
buzbee7da142f2012-11-29 16:33:42 -08002578 int immediate = immval & 0xffffffff;
2579 cg->LoadConstantNoClobber(cu, rl_result.low_reg, immediate);
buzbee02031b12012-11-23 09:41:35 -08002580 cg->StoreValue(cu, rl_dest, rl_result);
buzbee7da142f2012-11-29 16:33:42 -08002581 if (immediate == 0) {
2582 cg->Workaround7250540(cu, rl_dest, rl_result.low_reg);
2583 }
buzbee2cfc6392012-05-07 14:51:40 -07002584 }
2585}
2586
buzbeefa57c472012-11-21 12:06:18 -08002587static void CvtConstObject(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_string)
buzbee6969d502012-06-15 16:40:31 -07002588{
buzbee02031b12012-11-23 09:41:35 -08002589 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002590 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2591 llvm::ConstantInt* idx_val =
2592 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2593 uint32_t index = idx_val->getZExtValue();
2594 RegLocation rl_dest = GetLoc(cu, call_inst);
2595 if (is_string) {
buzbee02031b12012-11-23 09:41:35 -08002596 cg->GenConstString(cu, index, rl_dest);
buzbee101305f2012-06-28 18:00:56 -07002597 } else {
buzbee02031b12012-11-23 09:41:35 -08002598 cg->GenConstClass(cu, index, rl_dest);
buzbee101305f2012-06-28 18:00:56 -07002599 }
2600}
2601
buzbeefa57c472012-11-21 12:06:18 -08002602static void CvtFillArrayData(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee101305f2012-06-28 18:00:56 -07002603{
buzbee02031b12012-11-23 09:41:35 -08002604 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002605 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2606 llvm::ConstantInt* offset_val =
2607 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2608 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
buzbee02031b12012-11-23 09:41:35 -08002609 cg->GenFillArrayData(cu, offset_val->getSExtValue(), rl_src);
buzbee6969d502012-06-15 16:40:31 -07002610}
2611
buzbeefa57c472012-11-21 12:06:18 -08002612static void CvtNewInstance(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee4f1181f2012-06-22 13:52:12 -07002613{
buzbee02031b12012-11-23 09:41:35 -08002614 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002615 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2616 llvm::ConstantInt* type_idx_val =
2617 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2618 uint32_t type_idx = type_idx_val->getZExtValue();
2619 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002620 cg->GenNewInstance(cu, type_idx, rl_dest);
buzbee4f1181f2012-06-22 13:52:12 -07002621}
2622
buzbeefa57c472012-11-21 12:06:18 -08002623static void CvtNewArray(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002624{
buzbee02031b12012-11-23 09:41:35 -08002625 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002626 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2627 llvm::ConstantInt* type_idx_val =
2628 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2629 uint32_t type_idx = type_idx_val->getZExtValue();
2630 llvm::Value* len = call_inst->getArgOperand(1);
2631 RegLocation rl_len = GetLoc(cu, len);
2632 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002633 cg->GenNewArray(cu, type_idx, rl_dest, rl_len);
buzbee8fa0fda2012-06-27 15:44:52 -07002634}
2635
buzbeefa57c472012-11-21 12:06:18 -08002636static void CvtInstanceOf(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002637{
buzbee02031b12012-11-23 09:41:35 -08002638 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002639 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2640 llvm::ConstantInt* type_idx_val =
2641 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2642 uint32_t type_idx = type_idx_val->getZExtValue();
2643 llvm::Value* src = call_inst->getArgOperand(1);
2644 RegLocation rl_src = GetLoc(cu, src);
2645 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002646 cg->GenInstanceof(cu, type_idx, rl_dest, rl_src);
buzbee8fa0fda2012-06-27 15:44:52 -07002647}
2648
buzbeefa57c472012-11-21 12:06:18 -08002649static void CvtThrow(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee32412962012-06-26 16:27:56 -07002650{
buzbee02031b12012-11-23 09:41:35 -08002651 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002652 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2653 llvm::Value* src = call_inst->getArgOperand(0);
2654 RegLocation rl_src = GetLoc(cu, src);
buzbee02031b12012-11-23 09:41:35 -08002655 cg->GenThrow(cu, rl_src);
buzbee32412962012-06-26 16:27:56 -07002656}
2657
buzbeefa57c472012-11-21 12:06:18 -08002658static void CvtMonitorEnterExit(CompilationUnit* cu, bool is_enter,
2659 llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002660{
buzbee02031b12012-11-23 09:41:35 -08002661 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002662 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2663 llvm::ConstantInt* opt_flags =
2664 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2665 llvm::Value* src = call_inst->getArgOperand(1);
2666 RegLocation rl_src = GetLoc(cu, src);
2667 if (is_enter) {
buzbee02031b12012-11-23 09:41:35 -08002668 cg->GenMonitorEnter(cu, opt_flags->getZExtValue(), rl_src);
buzbee8fa0fda2012-06-27 15:44:52 -07002669 } else {
buzbee02031b12012-11-23 09:41:35 -08002670 cg->GenMonitorExit(cu, opt_flags->getZExtValue(), rl_src);
buzbee8fa0fda2012-06-27 15:44:52 -07002671 }
2672}
2673
buzbeefa57c472012-11-21 12:06:18 -08002674static void CvtArrayLength(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee8fa0fda2012-06-27 15:44:52 -07002675{
buzbee02031b12012-11-23 09:41:35 -08002676 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002677 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2678 llvm::ConstantInt* opt_flags =
2679 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2680 llvm::Value* src = call_inst->getArgOperand(1);
2681 RegLocation rl_src = GetLoc(cu, src);
buzbee02031b12012-11-23 09:41:35 -08002682 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
2683 cg->GenNullCheck(cu, rl_src.s_reg_low, rl_src.low_reg, opt_flags->getZExtValue());
buzbeefa57c472012-11-21 12:06:18 -08002684 RegLocation rl_dest = GetLoc(cu, call_inst);
2685 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002686 int len_offset = mirror::Array::LengthOffset().Int32Value();
buzbee02031b12012-11-23 09:41:35 -08002687 cg->LoadWordDisp(cu, rl_src.low_reg, len_offset, rl_result.low_reg);
2688 cg->StoreValue(cu, rl_dest, rl_result);
buzbee8fa0fda2012-06-27 15:44:52 -07002689}
2690
buzbeefa57c472012-11-21 12:06:18 -08002691static void CvtMoveException(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee32412962012-06-26 16:27:56 -07002692{
buzbee02031b12012-11-23 09:41:35 -08002693 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002694 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002695 cg->GenMoveException(cu, rl_dest);
buzbee32412962012-06-26 16:27:56 -07002696}
2697
buzbeefa57c472012-11-21 12:06:18 -08002698static void CvtSget(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_wide, bool is_object)
buzbee4f1181f2012-06-22 13:52:12 -07002699{
buzbee02031b12012-11-23 09:41:35 -08002700 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002701 DCHECK_EQ(call_inst->getNumArgOperands(), 1U);
2702 llvm::ConstantInt* type_idx_val =
2703 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2704 uint32_t type_idx = type_idx_val->getZExtValue();
2705 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002706 cg->GenSget(cu, type_idx, rl_dest, is_wide, is_object);
buzbee4f1181f2012-06-22 13:52:12 -07002707}
2708
buzbeefa57c472012-11-21 12:06:18 -08002709static void CvtSput(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_wide, bool is_object)
buzbee8fa0fda2012-06-27 15:44:52 -07002710{
buzbee02031b12012-11-23 09:41:35 -08002711 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002712 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2713 llvm::ConstantInt* type_idx_val =
2714 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2715 uint32_t type_idx = type_idx_val->getZExtValue();
2716 llvm::Value* src = call_inst->getArgOperand(1);
2717 RegLocation rl_src = GetLoc(cu, src);
buzbee02031b12012-11-23 09:41:35 -08002718 cg->GenSput(cu, type_idx, rl_src, is_wide, is_object);
buzbee8fa0fda2012-06-27 15:44:52 -07002719}
2720
buzbeefa57c472012-11-21 12:06:18 -08002721static void CvtAget(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size, int scale)
buzbee8fa0fda2012-06-27 15:44:52 -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_array = GetLoc(cu, call_inst->getArgOperand(1));
2728 RegLocation rl_index = GetLoc(cu, call_inst->getArgOperand(2));
2729 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002730 cg->GenArrayGet(cu, opt_flags->getZExtValue(), size, rl_array, rl_index,
buzbeefa57c472012-11-21 12:06:18 -08002731 rl_dest, scale);
buzbee8fa0fda2012-06-27 15:44:52 -07002732}
2733
buzbeefa57c472012-11-21 12:06:18 -08002734static void CvtAput(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size,
2735 int scale, bool is_object)
buzbee8fa0fda2012-06-27 15:44:52 -07002736{
buzbee02031b12012-11-23 09:41:35 -08002737 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002738 DCHECK_EQ(call_inst->getNumArgOperands(), 4U);
2739 llvm::ConstantInt* opt_flags =
2740 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2741 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
2742 RegLocation rl_array = GetLoc(cu, call_inst->getArgOperand(2));
2743 RegLocation rl_index = GetLoc(cu, call_inst->getArgOperand(3));
2744 if (is_object) {
buzbee02031b12012-11-23 09:41:35 -08002745 cg->GenArrayObjPut(cu, opt_flags->getZExtValue(), rl_array, rl_index,
buzbeefa57c472012-11-21 12:06:18 -08002746 rl_src, scale);
buzbeef1f86362012-07-10 15:18:31 -07002747 } else {
buzbee02031b12012-11-23 09:41:35 -08002748 cg->GenArrayPut(cu, opt_flags->getZExtValue(), size, rl_array, rl_index,
buzbeefa57c472012-11-21 12:06:18 -08002749 rl_src, scale);
buzbeef1f86362012-07-10 15:18:31 -07002750 }
2751}
2752
buzbeefa57c472012-11-21 12:06:18 -08002753static void CvtAputObj(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbeef1f86362012-07-10 15:18:31 -07002754{
buzbeefa57c472012-11-21 12:06:18 -08002755 CvtAput(cu, call_inst, kWord, 2, true /* is_object */);
buzbeef1f86362012-07-10 15:18:31 -07002756}
2757
buzbeefa57c472012-11-21 12:06:18 -08002758static void CvtAputPrimitive(CompilationUnit* cu, llvm::CallInst* call_inst,
buzbeef1f86362012-07-10 15:18:31 -07002759 OpSize size, int scale)
2760{
buzbeefa57c472012-11-21 12:06:18 -08002761 CvtAput(cu, call_inst, size, scale, false /* is_object */);
buzbee8fa0fda2012-06-27 15:44:52 -07002762}
2763
buzbeefa57c472012-11-21 12:06:18 -08002764static void CvtIget(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size,
2765 bool is_wide, bool is_obj)
buzbee101305f2012-06-28 18:00:56 -07002766{
buzbee02031b12012-11-23 09:41:35 -08002767 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002768 DCHECK_EQ(call_inst->getNumArgOperands(), 3U);
2769 llvm::ConstantInt* opt_flags =
2770 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2771 RegLocation rl_obj = GetLoc(cu, call_inst->getArgOperand(1));
2772 llvm::ConstantInt* field_idx =
2773 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(2));
2774 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002775 cg->GenIGet(cu, field_idx->getZExtValue(), opt_flags->getZExtValue(),
buzbeefa57c472012-11-21 12:06:18 -08002776 size, rl_dest, rl_obj, is_wide, is_obj);
buzbee101305f2012-06-28 18:00:56 -07002777}
2778
buzbeefa57c472012-11-21 12:06:18 -08002779static void CvtIput(CompilationUnit* cu, llvm::CallInst* call_inst, OpSize size,
2780 bool is_wide, bool is_obj)
buzbee101305f2012-06-28 18:00:56 -07002781{
buzbee02031b12012-11-23 09:41:35 -08002782 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002783 DCHECK_EQ(call_inst->getNumArgOperands(), 4U);
2784 llvm::ConstantInt* opt_flags =
2785 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2786 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
2787 RegLocation rl_obj = GetLoc(cu, call_inst->getArgOperand(2));
2788 llvm::ConstantInt* field_idx =
2789 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(3));
buzbee02031b12012-11-23 09:41:35 -08002790 cg->GenIPut(cu, field_idx->getZExtValue(), opt_flags->getZExtValue(),
buzbeefa57c472012-11-21 12:06:18 -08002791 size, rl_src, rl_obj, is_wide, is_obj);
buzbee101305f2012-06-28 18:00:56 -07002792}
2793
buzbeefa57c472012-11-21 12:06:18 -08002794static void CvtCheckCast(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee101305f2012-06-28 18:00:56 -07002795{
buzbee02031b12012-11-23 09:41:35 -08002796 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002797 DCHECK_EQ(call_inst->getNumArgOperands(), 2U);
2798 llvm::ConstantInt* type_idx =
2799 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2800 RegLocation rl_src = GetLoc(cu, call_inst->getArgOperand(1));
buzbee02031b12012-11-23 09:41:35 -08002801 cg->GenCheckCast(cu, type_idx->getZExtValue(), rl_src);
buzbee101305f2012-06-28 18:00:56 -07002802}
2803
buzbeefa57c472012-11-21 12:06:18 -08002804static void CvtFPCompare(CompilationUnit* cu, llvm::CallInst* call_inst,
buzbeeaad94382012-11-21 07:40:50 -08002805 Instruction::Code opcode)
buzbee76592632012-06-29 15:18:35 -07002806{
buzbee02031b12012-11-23 09:41:35 -08002807 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002808 RegLocation rl_src1 = GetLoc(cu, call_inst->getArgOperand(0));
2809 RegLocation rl_src2 = GetLoc(cu, call_inst->getArgOperand(1));
2810 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002811 cg->GenCmpFP(cu, opcode, rl_dest, rl_src1, rl_src2);
buzbee76592632012-06-29 15:18:35 -07002812}
2813
buzbeefa57c472012-11-21 12:06:18 -08002814static void CvtLongCompare(CompilationUnit* cu, llvm::CallInst* call_inst)
buzbee76592632012-06-29 15:18:35 -07002815{
buzbee02031b12012-11-23 09:41:35 -08002816 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002817 RegLocation rl_src1 = GetLoc(cu, call_inst->getArgOperand(0));
2818 RegLocation rl_src2 = GetLoc(cu, call_inst->getArgOperand(1));
2819 RegLocation rl_dest = GetLoc(cu, call_inst);
buzbee02031b12012-11-23 09:41:35 -08002820 cg->GenCmpLong(cu, rl_dest, rl_src1, rl_src2);
buzbee76592632012-06-29 15:18:35 -07002821}
2822
buzbeefa57c472012-11-21 12:06:18 -08002823static void CvtSwitch(CompilationUnit* cu, llvm::Instruction* inst)
buzbeef58c12c2012-07-03 15:06:29 -07002824{
buzbee02031b12012-11-23 09:41:35 -08002825 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002826 llvm::SwitchInst* sw_inst = llvm::dyn_cast<llvm::SwitchInst>(inst);
2827 DCHECK(sw_inst != NULL);
2828 llvm::Value* test_val = sw_inst->getCondition();
2829 llvm::MDNode* table_offset_node = sw_inst->getMetadata("SwitchTable");
2830 DCHECK(table_offset_node != NULL);
2831 llvm::ConstantInt* table_offset_value =
2832 static_cast<llvm::ConstantInt*>(table_offset_node->getOperand(0));
2833 int32_t table_offset = table_offset_value->getSExtValue();
2834 RegLocation rl_src = GetLoc(cu, test_val);
2835 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
2836 uint16_t table_magic = *table;
2837 if (table_magic == 0x100) {
buzbee02031b12012-11-23 09:41:35 -08002838 cg->GenPackedSwitch(cu, table_offset, rl_src);
buzbeea1da8a52012-07-09 14:00:21 -07002839 } else {
buzbeefa57c472012-11-21 12:06:18 -08002840 DCHECK_EQ(table_magic, 0x200);
buzbee02031b12012-11-23 09:41:35 -08002841 cg->GenSparseSwitch(cu, table_offset, rl_src);
buzbeea1da8a52012-07-09 14:00:21 -07002842 }
buzbeef58c12c2012-07-03 15:06:29 -07002843}
2844
buzbeefa57c472012-11-21 12:06:18 -08002845static void CvtInvoke(CompilationUnit* cu, llvm::CallInst* call_inst, bool is_void,
2846 bool is_filled_new_array)
buzbee6969d502012-06-15 16:40:31 -07002847{
buzbee02031b12012-11-23 09:41:35 -08002848 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002849 CallInfo* info = static_cast<CallInfo*>(NewMem(cu, sizeof(CallInfo), true, kAllocMisc));
2850 if (is_void) {
buzbee6969d502012-06-15 16:40:31 -07002851 info->result.location = kLocInvalid;
2852 } else {
buzbeefa57c472012-11-21 12:06:18 -08002853 info->result = GetLoc(cu, call_inst);
buzbee6969d502012-06-15 16:40:31 -07002854 }
buzbeefa57c472012-11-21 12:06:18 -08002855 llvm::ConstantInt* invoke_type_val =
2856 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(0));
2857 llvm::ConstantInt* method_index_val =
2858 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(1));
2859 llvm::ConstantInt* opt_flags_val =
2860 llvm::dyn_cast<llvm::ConstantInt>(call_inst->getArgOperand(2));
2861 info->type = static_cast<InvokeType>(invoke_type_val->getZExtValue());
2862 info->index = method_index_val->getZExtValue();
2863 info->opt_flags = opt_flags_val->getZExtValue();
2864 info->offset = cu->current_dalvik_offset;
buzbee6969d502012-06-15 16:40:31 -07002865
buzbee6969d502012-06-15 16:40:31 -07002866 // Count the argument words, and then build argument array.
buzbeefa57c472012-11-21 12:06:18 -08002867 info->num_arg_words = 0;
2868 for (unsigned int i = 3; i < call_inst->getNumArgOperands(); i++) {
2869 RegLocation t_loc = GetLoc(cu, call_inst->getArgOperand(i));
2870 info->num_arg_words += t_loc.wide ? 2 : 1;
buzbee6969d502012-06-15 16:40:31 -07002871 }
buzbeefa57c472012-11-21 12:06:18 -08002872 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
2873 (NewMem(cu, sizeof(RegLocation) * info->num_arg_words, false, kAllocMisc));
buzbee6969d502012-06-15 16:40:31 -07002874 // Now, fill in the location records, synthesizing high loc of wide vals
buzbeefa57c472012-11-21 12:06:18 -08002875 for (int i = 3, next = 0; next < info->num_arg_words;) {
2876 info->args[next] = GetLoc(cu, call_inst->getArgOperand(i++));
buzbee6969d502012-06-15 16:40:31 -07002877 if (info->args[next].wide) {
2878 next++;
2879 // TODO: Might make sense to mark this as an invalid loc
buzbeefa57c472012-11-21 12:06:18 -08002880 info->args[next].orig_sreg = info->args[next-1].orig_sreg+1;
2881 info->args[next].s_reg_low = info->args[next-1].s_reg_low+1;
buzbee6969d502012-06-15 16:40:31 -07002882 }
2883 next++;
2884 }
buzbeefa57c472012-11-21 12:06:18 -08002885 // TODO - rework such that we no longer need is_range
2886 info->is_range = (info->num_arg_words > 5);
buzbee4f4dfc72012-07-02 14:54:44 -07002887
buzbeefa57c472012-11-21 12:06:18 -08002888 if (is_filled_new_array) {
buzbee02031b12012-11-23 09:41:35 -08002889 cg->GenFilledNewArray(cu, info);
buzbee101305f2012-06-28 18:00:56 -07002890 } else {
buzbee02031b12012-11-23 09:41:35 -08002891 cg->GenInvoke(cu, info);
buzbee101305f2012-06-28 18:00:56 -07002892 }
buzbee6969d502012-06-15 16:40:31 -07002893}
2894
buzbeead8f15e2012-06-18 14:49:45 -07002895/* Look up the RegLocation associated with a Value. Must already be defined */
buzbeefa57c472012-11-21 12:06:18 -08002896static RegLocation ValToLoc(CompilationUnit* cu, llvm::Value* val)
buzbeead8f15e2012-06-18 14:49:45 -07002897{
buzbeefa57c472012-11-21 12:06:18 -08002898 SafeMap<llvm::Value*, RegLocation>::iterator it = cu->loc_map.find(val);
2899 DCHECK(it != cu->loc_map.end()) << "Missing definition";
buzbeead8f15e2012-06-18 14:49:45 -07002900 return it->second;
2901}
2902
buzbeefa57c472012-11-21 12:06:18 -08002903static bool BitcodeBlockCodeGen(CompilationUnit* cu, llvm::BasicBlock* bb)
buzbee2cfc6392012-05-07 14:51:40 -07002904{
buzbee02031b12012-11-23 09:41:35 -08002905 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08002906 while (cu->llvm_blocks.find(bb) == cu->llvm_blocks.end()) {
2907 llvm::BasicBlock* next_bb = NULL;
2908 cu->llvm_blocks.insert(bb);
2909 bool is_entry = (bb == &cu->func->getEntryBlock());
buzbee0967a252012-09-14 10:43:54 -07002910 // Define the starting label
buzbeefa57c472012-11-21 12:06:18 -08002911 LIR* block_label = cu->block_to_label_map.Get(bb);
buzbee0967a252012-09-14 10:43:54 -07002912 // Extract the type and starting offset from the block's name
buzbeefa57c472012-11-21 12:06:18 -08002913 char block_type = kInvalidBlock;
2914 if (is_entry) {
2915 block_type = kNormalBlock;
2916 block_label->operands[0] = 0;
buzbee951c0a12012-10-03 16:31:39 -07002917 } else if (!bb->hasName()) {
buzbeefa57c472012-11-21 12:06:18 -08002918 block_type = kNormalBlock;
2919 block_label->operands[0] = DexFile::kDexNoIndex;
buzbee0967a252012-09-14 10:43:54 -07002920 } else {
buzbeefa57c472012-11-21 12:06:18 -08002921 std::string block_name = bb->getName().str();
buzbee951c0a12012-10-03 16:31:39 -07002922 int dummy;
buzbeefa57c472012-11-21 12:06:18 -08002923 sscanf(block_name.c_str(), kLabelFormat, &block_type, &block_label->operands[0], &dummy);
2924 cu->current_dalvik_offset = block_label->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002925 }
buzbeefa57c472012-11-21 12:06:18 -08002926 DCHECK((block_type == kNormalBlock) || (block_type == kCatchBlock));
2927 cu->current_dalvik_offset = block_label->operands[0];
buzbee0967a252012-09-14 10:43:54 -07002928 // Set the label kind
buzbeefa57c472012-11-21 12:06:18 -08002929 block_label->opcode = kPseudoNormalBlockLabel;
buzbee0967a252012-09-14 10:43:54 -07002930 // Insert the label
buzbeefa57c472012-11-21 12:06:18 -08002931 AppendLIR(cu, block_label);
buzbee2cfc6392012-05-07 14:51:40 -07002932
buzbeefa57c472012-11-21 12:06:18 -08002933 LIR* head_lir = NULL;
buzbee8320f382012-09-11 16:29:42 -07002934
buzbeefa57c472012-11-21 12:06:18 -08002935 if (block_type == kCatchBlock) {
2936 head_lir = NewLIR0(cu, kPseudoExportedPC);
buzbee0967a252012-09-14 10:43:54 -07002937 }
buzbee8320f382012-09-11 16:29:42 -07002938
buzbee0967a252012-09-14 10:43:54 -07002939 // Free temp registers and reset redundant store tracking */
buzbeefa57c472012-11-21 12:06:18 -08002940 ResetRegPool(cu);
2941 ResetDefTracking(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002942
buzbee0967a252012-09-14 10:43:54 -07002943 //TODO: restore oat incoming liveness optimization
buzbeefa57c472012-11-21 12:06:18 -08002944 ClobberAllRegs(cu);
buzbee2cfc6392012-05-07 14:51:40 -07002945
buzbeefa57c472012-11-21 12:06:18 -08002946 if (is_entry) {
buzbee52a77fc2012-11-20 19:50:46 -08002947 RegLocation* ArgLocs = static_cast<RegLocation*>
buzbeefa57c472012-11-21 12:06:18 -08002948 (NewMem(cu, sizeof(RegLocation) * cu->num_ins, true, kAllocMisc));
2949 llvm::Function::arg_iterator it(cu->func->arg_begin());
2950 llvm::Function::arg_iterator it_end(cu->func->arg_end());
buzbee0967a252012-09-14 10:43:54 -07002951 // Skip past Method*
2952 it++;
2953 for (unsigned i = 0; it != it_end; ++it) {
2954 llvm::Value* val = it;
buzbeefa57c472012-11-21 12:06:18 -08002955 ArgLocs[i++] = ValToLoc(cu, val);
buzbee0967a252012-09-14 10:43:54 -07002956 llvm::Type* ty = val->getType();
buzbeefa57c472012-11-21 12:06:18 -08002957 if ((ty == cu->irb->getInt64Ty()) || (ty == cu->irb->getDoubleTy())) {
buzbee52a77fc2012-11-20 19:50:46 -08002958 ArgLocs[i] = ArgLocs[i-1];
buzbeefa57c472012-11-21 12:06:18 -08002959 ArgLocs[i].low_reg = ArgLocs[i].high_reg;
2960 ArgLocs[i].orig_sreg++;
2961 ArgLocs[i].s_reg_low = INVALID_SREG;
2962 ArgLocs[i].high_word = true;
buzbee0967a252012-09-14 10:43:54 -07002963 i++;
2964 }
2965 }
buzbee02031b12012-11-23 09:41:35 -08002966 cg->GenEntrySequence(cu, ArgLocs, cu->method_loc);
buzbee0967a252012-09-14 10:43:54 -07002967 }
2968
2969 // Visit all of the instructions in the block
2970 for (llvm::BasicBlock::iterator it = bb->begin(), e = bb->end(); it != e;) {
2971 llvm::Instruction* inst = it;
buzbeefa57c472012-11-21 12:06:18 -08002972 llvm::BasicBlock::iterator next_it = ++it;
buzbee0967a252012-09-14 10:43:54 -07002973 // Extract the Dalvik offset from the instruction
2974 uint32_t opcode = inst->getOpcode();
buzbeefa57c472012-11-21 12:06:18 -08002975 llvm::MDNode* dex_offset_node = inst->getMetadata("DexOff");
2976 if (dex_offset_node != NULL) {
2977 llvm::ConstantInt* dex_offset_value =
2978 static_cast<llvm::ConstantInt*>(dex_offset_node->getOperand(0));
2979 cu->current_dalvik_offset = dex_offset_value->getZExtValue();
buzbee0967a252012-09-14 10:43:54 -07002980 }
2981
buzbeefa57c472012-11-21 12:06:18 -08002982 ResetRegPool(cu);
2983 if (cu->disable_opt & (1 << kTrackLiveTemps)) {
2984 ClobberAllRegs(cu);
buzbee0967a252012-09-14 10:43:54 -07002985 }
2986
buzbeefa57c472012-11-21 12:06:18 -08002987 if (cu->disable_opt & (1 << kSuppressLoads)) {
2988 ResetDefTracking(cu);
buzbee0967a252012-09-14 10:43:54 -07002989 }
2990
2991 #ifndef NDEBUG
2992 /* Reset temp tracking sanity check */
buzbeefa57c472012-11-21 12:06:18 -08002993 cu->live_sreg = INVALID_SREG;
buzbee0967a252012-09-14 10:43:54 -07002994 #endif
2995
2996 // TODO: use llvm opcode name here instead of "boundary" if verbose
buzbeefa57c472012-11-21 12:06:18 -08002997 LIR* boundary_lir = MarkBoundary(cu, cu->current_dalvik_offset, "boundary");
buzbee0967a252012-09-14 10:43:54 -07002998
2999 /* Remember the first LIR for thisl block*/
buzbeefa57c472012-11-21 12:06:18 -08003000 if (head_lir == NULL) {
3001 head_lir = boundary_lir;
3002 head_lir->def_mask = ENCODE_ALL;
buzbee0967a252012-09-14 10:43:54 -07003003 }
3004
3005 switch(opcode) {
3006
3007 case llvm::Instruction::ICmp: {
buzbeefa57c472012-11-21 12:06:18 -08003008 llvm::Instruction* next_inst = next_it;
3009 llvm::BranchInst* br_inst = llvm::dyn_cast<llvm::BranchInst>(next_inst);
3010 if (br_inst != NULL /* and... */) {
3011 CvtICmpBr(cu, inst, br_inst);
buzbee0967a252012-09-14 10:43:54 -07003012 ++it;
3013 } else {
buzbeefa57c472012-11-21 12:06:18 -08003014 CvtICmp(cu, inst);
buzbee0967a252012-09-14 10:43:54 -07003015 }
3016 }
3017 break;
3018
3019 case llvm::Instruction::Call: {
buzbeefa57c472012-11-21 12:06:18 -08003020 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst);
3021 llvm::Function* callee = call_inst->getCalledFunction();
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003022 compiler_llvm::IntrinsicHelper::IntrinsicId id =
buzbeefa57c472012-11-21 12:06:18 -08003023 cu->intrinsic_helper->GetIntrinsicId(callee);
buzbee0967a252012-09-14 10:43:54 -07003024 switch (id) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003025 case compiler_llvm::IntrinsicHelper::AllocaShadowFrame:
3026 case compiler_llvm::IntrinsicHelper::PopShadowFrame:
3027 case compiler_llvm::IntrinsicHelper::SetVReg:
buzbee0967a252012-09-14 10:43:54 -07003028 // Ignore shadow frame stuff for quick compiler
3029 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003030 case compiler_llvm::IntrinsicHelper::CopyInt:
3031 case compiler_llvm::IntrinsicHelper::CopyObj:
3032 case compiler_llvm::IntrinsicHelper::CopyFloat:
3033 case compiler_llvm::IntrinsicHelper::CopyLong:
3034 case compiler_llvm::IntrinsicHelper::CopyDouble:
buzbeefa57c472012-11-21 12:06:18 -08003035 CvtCopy(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003036 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003037 case compiler_llvm::IntrinsicHelper::ConstInt:
3038 case compiler_llvm::IntrinsicHelper::ConstObj:
3039 case compiler_llvm::IntrinsicHelper::ConstLong:
3040 case compiler_llvm::IntrinsicHelper::ConstFloat:
3041 case compiler_llvm::IntrinsicHelper::ConstDouble:
buzbeefa57c472012-11-21 12:06:18 -08003042 CvtConst(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003043 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003044 case compiler_llvm::IntrinsicHelper::DivInt:
3045 case compiler_llvm::IntrinsicHelper::DivLong:
buzbeefa57c472012-11-21 12:06:18 -08003046 CvtBinOp(cu, kOpDiv, inst);
buzbee0967a252012-09-14 10:43:54 -07003047 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003048 case compiler_llvm::IntrinsicHelper::RemInt:
3049 case compiler_llvm::IntrinsicHelper::RemLong:
buzbeefa57c472012-11-21 12:06:18 -08003050 CvtBinOp(cu, kOpRem, inst);
buzbee0967a252012-09-14 10:43:54 -07003051 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003052 case compiler_llvm::IntrinsicHelper::MethodInfo:
buzbee0967a252012-09-14 10:43:54 -07003053 // Already dealt with - just ignore it here.
3054 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003055 case compiler_llvm::IntrinsicHelper::CheckSuspend:
buzbee02031b12012-11-23 09:41:35 -08003056 cg->GenSuspendTest(cu, 0 /* opt_flags already applied */);
buzbee0967a252012-09-14 10:43:54 -07003057 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003058 case compiler_llvm::IntrinsicHelper::HLInvokeObj:
3059 case compiler_llvm::IntrinsicHelper::HLInvokeFloat:
3060 case compiler_llvm::IntrinsicHelper::HLInvokeDouble:
3061 case compiler_llvm::IntrinsicHelper::HLInvokeLong:
3062 case compiler_llvm::IntrinsicHelper::HLInvokeInt:
buzbeefa57c472012-11-21 12:06:18 -08003063 CvtInvoke(cu, call_inst, false /* is_void */, false /* new_array */);
buzbee0967a252012-09-14 10:43:54 -07003064 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003065 case compiler_llvm::IntrinsicHelper::HLInvokeVoid:
buzbeefa57c472012-11-21 12:06:18 -08003066 CvtInvoke(cu, call_inst, true /* is_void */, false /* new_array */);
buzbee0967a252012-09-14 10:43:54 -07003067 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003068 case compiler_llvm::IntrinsicHelper::HLFilledNewArray:
buzbeefa57c472012-11-21 12:06:18 -08003069 CvtInvoke(cu, call_inst, false /* is_void */, true /* new_array */);
buzbee0967a252012-09-14 10:43:54 -07003070 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003071 case compiler_llvm::IntrinsicHelper::HLFillArrayData:
buzbeefa57c472012-11-21 12:06:18 -08003072 CvtFillArrayData(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003073 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003074 case compiler_llvm::IntrinsicHelper::ConstString:
buzbeefa57c472012-11-21 12:06:18 -08003075 CvtConstObject(cu, call_inst, true /* is_string */);
buzbee0967a252012-09-14 10:43:54 -07003076 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003077 case compiler_llvm::IntrinsicHelper::ConstClass:
buzbeefa57c472012-11-21 12:06:18 -08003078 CvtConstObject(cu, call_inst, false /* is_string */);
buzbee0967a252012-09-14 10:43:54 -07003079 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003080 case compiler_llvm::IntrinsicHelper::HLCheckCast:
buzbeefa57c472012-11-21 12:06:18 -08003081 CvtCheckCast(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003082 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003083 case compiler_llvm::IntrinsicHelper::NewInstance:
buzbeefa57c472012-11-21 12:06:18 -08003084 CvtNewInstance(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003085 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003086 case compiler_llvm::IntrinsicHelper::HLSgetObject:
buzbeefa57c472012-11-21 12:06:18 -08003087 CvtSget(cu, call_inst, false /* wide */, true /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003088 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003089 case compiler_llvm::IntrinsicHelper::HLSget:
3090 case compiler_llvm::IntrinsicHelper::HLSgetFloat:
3091 case compiler_llvm::IntrinsicHelper::HLSgetBoolean:
3092 case compiler_llvm::IntrinsicHelper::HLSgetByte:
3093 case compiler_llvm::IntrinsicHelper::HLSgetChar:
3094 case compiler_llvm::IntrinsicHelper::HLSgetShort:
buzbeefa57c472012-11-21 12:06:18 -08003095 CvtSget(cu, call_inst, false /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003096 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003097 case compiler_llvm::IntrinsicHelper::HLSgetWide:
3098 case compiler_llvm::IntrinsicHelper::HLSgetDouble:
buzbeefa57c472012-11-21 12:06:18 -08003099 CvtSget(cu, call_inst, true /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003100 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003101 case compiler_llvm::IntrinsicHelper::HLSput:
3102 case compiler_llvm::IntrinsicHelper::HLSputFloat:
3103 case compiler_llvm::IntrinsicHelper::HLSputBoolean:
3104 case compiler_llvm::IntrinsicHelper::HLSputByte:
3105 case compiler_llvm::IntrinsicHelper::HLSputChar:
3106 case compiler_llvm::IntrinsicHelper::HLSputShort:
buzbeefa57c472012-11-21 12:06:18 -08003107 CvtSput(cu, call_inst, false /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003108 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003109 case compiler_llvm::IntrinsicHelper::HLSputWide:
3110 case compiler_llvm::IntrinsicHelper::HLSputDouble:
buzbeefa57c472012-11-21 12:06:18 -08003111 CvtSput(cu, call_inst, true /* wide */, false /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003112 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003113 case compiler_llvm::IntrinsicHelper::HLSputObject:
buzbeefa57c472012-11-21 12:06:18 -08003114 CvtSput(cu, call_inst, false /* wide */, true /* Object */);
buzbee0967a252012-09-14 10:43:54 -07003115 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003116 case compiler_llvm::IntrinsicHelper::GetException:
buzbeefa57c472012-11-21 12:06:18 -08003117 CvtMoveException(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003118 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003119 case compiler_llvm::IntrinsicHelper::HLThrowException:
buzbeefa57c472012-11-21 12:06:18 -08003120 CvtThrow(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003121 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003122 case compiler_llvm::IntrinsicHelper::MonitorEnter:
buzbeefa57c472012-11-21 12:06:18 -08003123 CvtMonitorEnterExit(cu, true /* is_enter */, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003124 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003125 case compiler_llvm::IntrinsicHelper::MonitorExit:
buzbeefa57c472012-11-21 12:06:18 -08003126 CvtMonitorEnterExit(cu, false /* is_enter */, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003127 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003128 case compiler_llvm::IntrinsicHelper::OptArrayLength:
buzbeefa57c472012-11-21 12:06:18 -08003129 CvtArrayLength(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003130 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003131 case compiler_llvm::IntrinsicHelper::NewArray:
buzbeefa57c472012-11-21 12:06:18 -08003132 CvtNewArray(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003133 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003134 case compiler_llvm::IntrinsicHelper::InstanceOf:
buzbeefa57c472012-11-21 12:06:18 -08003135 CvtInstanceOf(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003136 break;
3137
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003138 case compiler_llvm::IntrinsicHelper::HLArrayGet:
3139 case compiler_llvm::IntrinsicHelper::HLArrayGetObject:
3140 case compiler_llvm::IntrinsicHelper::HLArrayGetFloat:
buzbeefa57c472012-11-21 12:06:18 -08003141 CvtAget(cu, call_inst, kWord, 2);
buzbee0967a252012-09-14 10:43:54 -07003142 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003143 case compiler_llvm::IntrinsicHelper::HLArrayGetWide:
3144 case compiler_llvm::IntrinsicHelper::HLArrayGetDouble:
buzbeefa57c472012-11-21 12:06:18 -08003145 CvtAget(cu, call_inst, kLong, 3);
buzbee0967a252012-09-14 10:43:54 -07003146 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003147 case compiler_llvm::IntrinsicHelper::HLArrayGetBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003148 CvtAget(cu, call_inst, kUnsignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003149 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003150 case compiler_llvm::IntrinsicHelper::HLArrayGetByte:
buzbeefa57c472012-11-21 12:06:18 -08003151 CvtAget(cu, call_inst, kSignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003152 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003153 case compiler_llvm::IntrinsicHelper::HLArrayGetChar:
buzbeefa57c472012-11-21 12:06:18 -08003154 CvtAget(cu, call_inst, kUnsignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003155 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003156 case compiler_llvm::IntrinsicHelper::HLArrayGetShort:
buzbeefa57c472012-11-21 12:06:18 -08003157 CvtAget(cu, call_inst, kSignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003158 break;
3159
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003160 case compiler_llvm::IntrinsicHelper::HLArrayPut:
3161 case compiler_llvm::IntrinsicHelper::HLArrayPutFloat:
buzbeefa57c472012-11-21 12:06:18 -08003162 CvtAputPrimitive(cu, call_inst, kWord, 2);
buzbee0967a252012-09-14 10:43:54 -07003163 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003164 case compiler_llvm::IntrinsicHelper::HLArrayPutObject:
buzbeefa57c472012-11-21 12:06:18 -08003165 CvtAputObj(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003166 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003167 case compiler_llvm::IntrinsicHelper::HLArrayPutWide:
3168 case compiler_llvm::IntrinsicHelper::HLArrayPutDouble:
buzbeefa57c472012-11-21 12:06:18 -08003169 CvtAputPrimitive(cu, call_inst, kLong, 3);
buzbee0967a252012-09-14 10:43:54 -07003170 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003171 case compiler_llvm::IntrinsicHelper::HLArrayPutBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003172 CvtAputPrimitive(cu, call_inst, kUnsignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003173 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003174 case compiler_llvm::IntrinsicHelper::HLArrayPutByte:
buzbeefa57c472012-11-21 12:06:18 -08003175 CvtAputPrimitive(cu, call_inst, kSignedByte, 0);
buzbee0967a252012-09-14 10:43:54 -07003176 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003177 case compiler_llvm::IntrinsicHelper::HLArrayPutChar:
buzbeefa57c472012-11-21 12:06:18 -08003178 CvtAputPrimitive(cu, call_inst, kUnsignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003179 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003180 case compiler_llvm::IntrinsicHelper::HLArrayPutShort:
buzbeefa57c472012-11-21 12:06:18 -08003181 CvtAputPrimitive(cu, call_inst, kSignedHalf, 1);
buzbee0967a252012-09-14 10:43:54 -07003182 break;
3183
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003184 case compiler_llvm::IntrinsicHelper::HLIGet:
3185 case compiler_llvm::IntrinsicHelper::HLIGetFloat:
buzbeefa57c472012-11-21 12:06:18 -08003186 CvtIget(cu, call_inst, kWord, false /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003187 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003188 case compiler_llvm::IntrinsicHelper::HLIGetObject:
buzbeefa57c472012-11-21 12:06:18 -08003189 CvtIget(cu, call_inst, kWord, false /* is_wide */, true /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003190 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003191 case compiler_llvm::IntrinsicHelper::HLIGetWide:
3192 case compiler_llvm::IntrinsicHelper::HLIGetDouble:
buzbeefa57c472012-11-21 12:06:18 -08003193 CvtIget(cu, call_inst, kLong, true /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003194 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003195 case compiler_llvm::IntrinsicHelper::HLIGetBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003196 CvtIget(cu, call_inst, kUnsignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003197 false /* obj */);
3198 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003199 case compiler_llvm::IntrinsicHelper::HLIGetByte:
buzbeefa57c472012-11-21 12:06:18 -08003200 CvtIget(cu, call_inst, kSignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003201 false /* obj */);
3202 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003203 case compiler_llvm::IntrinsicHelper::HLIGetChar:
buzbeefa57c472012-11-21 12:06:18 -08003204 CvtIget(cu, call_inst, kUnsignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003205 false /* obj */);
3206 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003207 case compiler_llvm::IntrinsicHelper::HLIGetShort:
buzbeefa57c472012-11-21 12:06:18 -08003208 CvtIget(cu, call_inst, kSignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003209 false /* obj */);
3210 break;
3211
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003212 case compiler_llvm::IntrinsicHelper::HLIPut:
3213 case compiler_llvm::IntrinsicHelper::HLIPutFloat:
buzbeefa57c472012-11-21 12:06:18 -08003214 CvtIput(cu, call_inst, kWord, false /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003215 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003216 case compiler_llvm::IntrinsicHelper::HLIPutObject:
buzbeefa57c472012-11-21 12:06:18 -08003217 CvtIput(cu, call_inst, kWord, false /* is_wide */, true /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003218 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003219 case compiler_llvm::IntrinsicHelper::HLIPutWide:
3220 case compiler_llvm::IntrinsicHelper::HLIPutDouble:
buzbeefa57c472012-11-21 12:06:18 -08003221 CvtIput(cu, call_inst, kLong, true /* is_wide */, false /* obj */);
buzbee0967a252012-09-14 10:43:54 -07003222 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003223 case compiler_llvm::IntrinsicHelper::HLIPutBoolean:
buzbeefa57c472012-11-21 12:06:18 -08003224 CvtIput(cu, call_inst, kUnsignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003225 false /* obj */);
3226 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003227 case compiler_llvm::IntrinsicHelper::HLIPutByte:
buzbeefa57c472012-11-21 12:06:18 -08003228 CvtIput(cu, call_inst, kSignedByte, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003229 false /* obj */);
3230 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003231 case compiler_llvm::IntrinsicHelper::HLIPutChar:
buzbeefa57c472012-11-21 12:06:18 -08003232 CvtIput(cu, call_inst, kUnsignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003233 false /* obj */);
3234 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003235 case compiler_llvm::IntrinsicHelper::HLIPutShort:
buzbeefa57c472012-11-21 12:06:18 -08003236 CvtIput(cu, call_inst, kSignedHalf, false /* is_wide */,
buzbee0967a252012-09-14 10:43:54 -07003237 false /* obj */);
3238 break;
3239
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003240 case compiler_llvm::IntrinsicHelper::IntToChar:
buzbeefa57c472012-11-21 12:06:18 -08003241 CvtIntNarrowing(cu, call_inst, Instruction::INT_TO_CHAR);
buzbee0967a252012-09-14 10:43:54 -07003242 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003243 case compiler_llvm::IntrinsicHelper::IntToShort:
buzbeefa57c472012-11-21 12:06:18 -08003244 CvtIntNarrowing(cu, call_inst, Instruction::INT_TO_SHORT);
buzbee0967a252012-09-14 10:43:54 -07003245 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003246 case compiler_llvm::IntrinsicHelper::IntToByte:
buzbeefa57c472012-11-21 12:06:18 -08003247 CvtIntNarrowing(cu, call_inst, Instruction::INT_TO_BYTE);
buzbee0967a252012-09-14 10:43:54 -07003248 break;
3249
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003250 case compiler_llvm::IntrinsicHelper::F2I:
3251 case compiler_llvm::IntrinsicHelper::D2I:
3252 case compiler_llvm::IntrinsicHelper::F2L:
3253 case compiler_llvm::IntrinsicHelper::D2L:
buzbeefa57c472012-11-21 12:06:18 -08003254 CvtFPToInt(cu, call_inst);
TDYa1274ec8ccd2012-08-11 07:04:57 -07003255 break;
3256
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003257 case compiler_llvm::IntrinsicHelper::CmplFloat:
buzbeefa57c472012-11-21 12:06:18 -08003258 CvtFPCompare(cu, call_inst, Instruction::CMPL_FLOAT);
buzbee0967a252012-09-14 10:43:54 -07003259 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003260 case compiler_llvm::IntrinsicHelper::CmpgFloat:
buzbeefa57c472012-11-21 12:06:18 -08003261 CvtFPCompare(cu, call_inst, Instruction::CMPG_FLOAT);
buzbee0967a252012-09-14 10:43:54 -07003262 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003263 case compiler_llvm::IntrinsicHelper::CmplDouble:
buzbeefa57c472012-11-21 12:06:18 -08003264 CvtFPCompare(cu, call_inst, Instruction::CMPL_DOUBLE);
buzbee0967a252012-09-14 10:43:54 -07003265 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003266 case compiler_llvm::IntrinsicHelper::CmpgDouble:
buzbeefa57c472012-11-21 12:06:18 -08003267 CvtFPCompare(cu, call_inst, Instruction::CMPG_DOUBLE);
buzbee0967a252012-09-14 10:43:54 -07003268 break;
3269
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003270 case compiler_llvm::IntrinsicHelper::CmpLong:
buzbeefa57c472012-11-21 12:06:18 -08003271 CvtLongCompare(cu, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003272 break;
3273
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003274 case compiler_llvm::IntrinsicHelper::SHLLong:
buzbeefa57c472012-11-21 12:06:18 -08003275 CvtShiftOp(cu, Instruction::SHL_LONG, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003276 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003277 case compiler_llvm::IntrinsicHelper::SHRLong:
buzbeefa57c472012-11-21 12:06:18 -08003278 CvtShiftOp(cu, Instruction::SHR_LONG, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003279 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003280 case compiler_llvm::IntrinsicHelper::USHRLong:
buzbeefa57c472012-11-21 12:06:18 -08003281 CvtShiftOp(cu, Instruction::USHR_LONG, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003282 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003283 case compiler_llvm::IntrinsicHelper::SHLInt:
buzbeefa57c472012-11-21 12:06:18 -08003284 CvtShiftOp(cu, Instruction::SHL_INT, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003285 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003286 case compiler_llvm::IntrinsicHelper::SHRInt:
buzbeefa57c472012-11-21 12:06:18 -08003287 CvtShiftOp(cu, Instruction::SHR_INT, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003288 break;
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003289 case compiler_llvm::IntrinsicHelper::USHRInt:
buzbeefa57c472012-11-21 12:06:18 -08003290 CvtShiftOp(cu, Instruction::USHR_INT, call_inst);
buzbee0967a252012-09-14 10:43:54 -07003291 break;
3292
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003293 case compiler_llvm::IntrinsicHelper::CatchTargets: {
buzbeefa57c472012-11-21 12:06:18 -08003294 llvm::SwitchInst* sw_inst =
3295 llvm::dyn_cast<llvm::SwitchInst>(next_it);
3296 DCHECK(sw_inst != NULL);
buzbee0967a252012-09-14 10:43:54 -07003297 /*
3298 * Discard the edges and the following conditional branch.
3299 * Do a direct branch to the default target (which is the
3300 * "work" portion of the pair.
3301 * TODO: awful code layout - rework
3302 */
buzbeefa57c472012-11-21 12:06:18 -08003303 llvm::BasicBlock* target_bb = sw_inst->getDefaultDest();
3304 DCHECK(target_bb != NULL);
buzbee02031b12012-11-23 09:41:35 -08003305 cg->OpUnconditionalBranch(cu, cu->block_to_label_map.Get(target_bb));
buzbee0967a252012-09-14 10:43:54 -07003306 ++it;
3307 // Set next bb to default target - improves code layout
buzbeefa57c472012-11-21 12:06:18 -08003308 next_bb = target_bb;
buzbee0967a252012-09-14 10:43:54 -07003309 }
3310 break;
3311
3312 default:
buzbeefa57c472012-11-21 12:06:18 -08003313 LOG(FATAL) << "Unexpected intrinsic " << cu->intrinsic_helper->GetName(id);
buzbee0967a252012-09-14 10:43:54 -07003314 }
3315 }
3316 break;
3317
buzbeefa57c472012-11-21 12:06:18 -08003318 case llvm::Instruction::Br: CvtBr(cu, inst); break;
3319 case llvm::Instruction::Add: CvtBinOp(cu, kOpAdd, inst); break;
3320 case llvm::Instruction::Sub: CvtBinOp(cu, kOpSub, inst); break;
3321 case llvm::Instruction::Mul: CvtBinOp(cu, kOpMul, inst); break;
3322 case llvm::Instruction::SDiv: CvtBinOp(cu, kOpDiv, inst); break;
3323 case llvm::Instruction::SRem: CvtBinOp(cu, kOpRem, inst); break;
3324 case llvm::Instruction::And: CvtBinOp(cu, kOpAnd, inst); break;
3325 case llvm::Instruction::Or: CvtBinOp(cu, kOpOr, inst); break;
3326 case llvm::Instruction::Xor: CvtBinOp(cu, kOpXor, inst); break;
3327 case llvm::Instruction::PHI: CvtPhi(cu, inst); break;
3328 case llvm::Instruction::Ret: CvtRet(cu, inst); break;
3329 case llvm::Instruction::FAdd: CvtBinFPOp(cu, kOpAdd, inst); break;
3330 case llvm::Instruction::FSub: CvtBinFPOp(cu, kOpSub, inst); break;
3331 case llvm::Instruction::FMul: CvtBinFPOp(cu, kOpMul, inst); break;
3332 case llvm::Instruction::FDiv: CvtBinFPOp(cu, kOpDiv, inst); break;
3333 case llvm::Instruction::FRem: CvtBinFPOp(cu, kOpRem, inst); break;
3334 case llvm::Instruction::SIToFP: CvtIntToFP(cu, inst); break;
3335 case llvm::Instruction::FPTrunc: CvtDoubleToFloat(cu, inst); break;
3336 case llvm::Instruction::FPExt: CvtFloatToDouble(cu, inst); break;
3337 case llvm::Instruction::Trunc: CvtTrunc(cu, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003338
buzbeefa57c472012-11-21 12:06:18 -08003339 case llvm::Instruction::ZExt: CvtIntExt(cu, inst, false /* signed */);
buzbee0967a252012-09-14 10:43:54 -07003340 break;
buzbeefa57c472012-11-21 12:06:18 -08003341 case llvm::Instruction::SExt: CvtIntExt(cu, inst, true /* signed */);
buzbee0967a252012-09-14 10:43:54 -07003342 break;
3343
buzbeefa57c472012-11-21 12:06:18 -08003344 case llvm::Instruction::Switch: CvtSwitch(cu, inst); break;
buzbee0967a252012-09-14 10:43:54 -07003345
3346 case llvm::Instruction::Unreachable:
3347 break; // FIXME: can we really ignore these?
3348
3349 case llvm::Instruction::Shl:
3350 case llvm::Instruction::LShr:
3351 case llvm::Instruction::AShr:
3352 case llvm::Instruction::Invoke:
3353 case llvm::Instruction::FPToUI:
TDYa1274ec8ccd2012-08-11 07:04:57 -07003354 case llvm::Instruction::FPToSI:
buzbee0967a252012-09-14 10:43:54 -07003355 case llvm::Instruction::UIToFP:
3356 case llvm::Instruction::PtrToInt:
3357 case llvm::Instruction::IntToPtr:
3358 case llvm::Instruction::FCmp:
3359 case llvm::Instruction::URem:
3360 case llvm::Instruction::UDiv:
3361 case llvm::Instruction::Resume:
3362 case llvm::Instruction::Alloca:
3363 case llvm::Instruction::GetElementPtr:
3364 case llvm::Instruction::Fence:
3365 case llvm::Instruction::AtomicCmpXchg:
3366 case llvm::Instruction::AtomicRMW:
3367 case llvm::Instruction::BitCast:
3368 case llvm::Instruction::VAArg:
3369 case llvm::Instruction::Select:
3370 case llvm::Instruction::UserOp1:
3371 case llvm::Instruction::UserOp2:
3372 case llvm::Instruction::ExtractElement:
3373 case llvm::Instruction::InsertElement:
3374 case llvm::Instruction::ShuffleVector:
3375 case llvm::Instruction::ExtractValue:
3376 case llvm::Instruction::InsertValue:
3377 case llvm::Instruction::LandingPad:
3378 case llvm::Instruction::IndirectBr:
3379 case llvm::Instruction::Load:
3380 case llvm::Instruction::Store:
3381 LOG(FATAL) << "Unexpected llvm opcode: " << opcode; break;
3382
3383 default:
3384 LOG(FATAL) << "Unknown llvm opcode: " << inst->getOpcodeName();
3385 break;
buzbeead8f15e2012-06-18 14:49:45 -07003386 }
3387 }
buzbee2cfc6392012-05-07 14:51:40 -07003388
buzbeefa57c472012-11-21 12:06:18 -08003389 if (head_lir != NULL) {
3390 ApplyLocalOptimizations(cu, head_lir, cu->last_lir_insn);
buzbee2cfc6392012-05-07 14:51:40 -07003391 }
buzbeefa57c472012-11-21 12:06:18 -08003392 if (next_bb != NULL) {
3393 bb = next_bb;
3394 next_bb = NULL;
buzbee6969d502012-06-15 16:40:31 -07003395 }
buzbee6969d502012-06-15 16:40:31 -07003396 }
buzbee2cfc6392012-05-07 14:51:40 -07003397 return false;
3398}
3399
3400/*
3401 * Convert LLVM_IR to MIR:
3402 * o Iterate through the LLVM_IR and construct a graph using
3403 * standard MIR building blocks.
3404 * o Perform a basic-block optimization pass to remove unnecessary
3405 * store/load sequences.
3406 * o Convert the LLVM Value operands into RegLocations where applicable.
buzbeefa57c472012-11-21 12:06:18 -08003407 * o Create ssa_rep def/use operand arrays for each converted LLVM opcode
buzbee2cfc6392012-05-07 14:51:40 -07003408 * o Perform register promotion
3409 * o Iterate through the graph a basic block at a time, generating
3410 * LIR.
3411 * o Assemble LIR as usual.
3412 * o Profit.
3413 */
buzbeefa57c472012-11-21 12:06:18 -08003414void MethodBitcode2LIR(CompilationUnit* cu)
buzbee2cfc6392012-05-07 14:51:40 -07003415{
buzbee02031b12012-11-23 09:41:35 -08003416 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08003417 llvm::Function* func = cu->func;
3418 int num_basic_blocks = func->getBasicBlockList().size();
buzbee2cfc6392012-05-07 14:51:40 -07003419 // Allocate a list for LIR basic block labels
buzbeefa57c472012-11-21 12:06:18 -08003420 cu->block_label_list =
3421 static_cast<LIR*>(NewMem(cu, sizeof(LIR) * num_basic_blocks, true, kAllocLIR));
3422 LIR* label_list = cu->block_label_list;
3423 int next_label = 0;
buzbee28c9a832012-11-21 15:39:13 -08003424 for (llvm::Function::iterator i = func->begin(), e = func->end(); i != e; ++i) {
buzbeefa57c472012-11-21 12:06:18 -08003425 cu->block_to_label_map.Put(static_cast<llvm::BasicBlock*>(i),
3426 &label_list[next_label++]);
buzbee2cfc6392012-05-07 14:51:40 -07003427 }
buzbeead8f15e2012-06-18 14:49:45 -07003428
3429 /*
buzbeefa57c472012-11-21 12:06:18 -08003430 * Keep honest - clear reg_locations, Value => RegLocation,
buzbeead8f15e2012-06-18 14:49:45 -07003431 * promotion map and VmapTables.
3432 */
buzbeefa57c472012-11-21 12:06:18 -08003433 cu->loc_map.clear(); // Start fresh
3434 cu->reg_location = NULL;
buzbee28c9a832012-11-21 15:39:13 -08003435 for (int i = 0; i < cu->num_dalvik_registers + cu->num_compiler_temps + 1; i++) {
buzbeefa57c472012-11-21 12:06:18 -08003436 cu->promotion_map[i].core_location = kLocDalvikFrame;
3437 cu->promotion_map[i].fp_location = kLocDalvikFrame;
buzbeead8f15e2012-06-18 14:49:45 -07003438 }
buzbeefa57c472012-11-21 12:06:18 -08003439 cu->core_spill_mask = 0;
3440 cu->num_core_spills = 0;
3441 cu->fp_spill_mask = 0;
3442 cu->num_fp_spills = 0;
3443 cu->core_vmap_table.clear();
3444 cu->fp_vmap_table.clear();
buzbeead8f15e2012-06-18 14:49:45 -07003445
3446 /*
3447 * At this point, we've lost all knowledge of register promotion.
3448 * Rebuild that info from the MethodInfo intrinsic (if it
buzbeeca7a5e42012-08-20 11:12:18 -07003449 * exists - not required for correctness). Normally, this will
3450 * be the first instruction we encounter, so we won't have to iterate
3451 * through everything.
buzbeead8f15e2012-06-18 14:49:45 -07003452 */
buzbee28c9a832012-11-21 15:39:13 -08003453 for (llvm::inst_iterator i = llvm::inst_begin(func), e = llvm::inst_end(func); i != e; ++i) {
buzbeefa57c472012-11-21 12:06:18 -08003454 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(&*i);
3455 if (call_inst != NULL) {
3456 llvm::Function* callee = call_inst->getCalledFunction();
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003457 compiler_llvm::IntrinsicHelper::IntrinsicId id =
buzbeefa57c472012-11-21 12:06:18 -08003458 cu->intrinsic_helper->GetIntrinsicId(callee);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08003459 if (id == compiler_llvm::IntrinsicHelper::MethodInfo) {
buzbeefa57c472012-11-21 12:06:18 -08003460 if (cu->verbose) {
buzbeeca7a5e42012-08-20 11:12:18 -07003461 LOG(INFO) << "Found MethodInfo";
3462 }
buzbeefa57c472012-11-21 12:06:18 -08003463 llvm::MDNode* reg_info_node = call_inst->getMetadata("RegInfo");
3464 if (reg_info_node != NULL) {
3465 llvm::ConstantInt* num_ins_value =
3466 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(0));
3467 llvm::ConstantInt* num_regs_value =
3468 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(1));
3469 llvm::ConstantInt* num_outs_value =
3470 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(2));
3471 llvm::ConstantInt* num_compiler_temps_value =
3472 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(3));
3473 llvm::ConstantInt* num_ssa_regs_value =
3474 static_cast<llvm::ConstantInt*>(reg_info_node->getOperand(4));
3475 if (cu->verbose) {
3476 LOG(INFO) << "RegInfo - Ins:" << num_ins_value->getZExtValue()
3477 << ", Regs:" << num_regs_value->getZExtValue()
3478 << ", Outs:" << num_outs_value->getZExtValue()
3479 << ", CTemps:" << num_compiler_temps_value->getZExtValue()
3480 << ", SSARegs:" << num_ssa_regs_value->getZExtValue();
buzbeeca7a5e42012-08-20 11:12:18 -07003481 }
3482 }
buzbeefa57c472012-11-21 12:06:18 -08003483 llvm::MDNode* pmap_info_node = call_inst->getMetadata("PromotionMap");
3484 if (pmap_info_node != NULL) {
3485 int elems = pmap_info_node->getNumOperands();
3486 if (cu->verbose) {
buzbeeca7a5e42012-08-20 11:12:18 -07003487 LOG(INFO) << "PMap size: " << elems;
3488 }
3489 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -08003490 llvm::ConstantInt* raw_map_data =
3491 static_cast<llvm::ConstantInt*>(pmap_info_node->getOperand(i));
3492 uint32_t map_data = raw_map_data->getZExtValue();
3493 PromotionMap* p = &cu->promotion_map[i];
3494 p->first_in_pair = (map_data >> 24) & 0xff;
3495 p->FpReg = (map_data >> 16) & 0xff;
3496 p->core_reg = (map_data >> 8) & 0xff;
3497 p->fp_location = static_cast<RegLocationType>((map_data >> 4) & 0xf);
3498 if (p->fp_location == kLocPhysReg) {
3499 RecordFpPromotion(cu, p->FpReg, i);
buzbeeca7a5e42012-08-20 11:12:18 -07003500 }
buzbeefa57c472012-11-21 12:06:18 -08003501 p->core_location = static_cast<RegLocationType>(map_data & 0xf);
3502 if (p->core_location == kLocPhysReg) {
3503 RecordCorePromotion(cu, p->core_reg, i);
buzbeeca7a5e42012-08-20 11:12:18 -07003504 }
3505 }
buzbeefa57c472012-11-21 12:06:18 -08003506 if (cu->verbose) {
3507 DumpPromotionMap(cu);
buzbeeca7a5e42012-08-20 11:12:18 -07003508 }
3509 }
3510 break;
3511 }
3512 }
3513 }
buzbee02031b12012-11-23 09:41:35 -08003514 cg->AdjustSpillMask(cu);
buzbeefa57c472012-11-21 12:06:18 -08003515 cu->frame_size = ComputeFrameSize(cu);
buzbeead8f15e2012-06-18 14:49:45 -07003516
3517 // Create RegLocations for arguments
buzbeefa57c472012-11-21 12:06:18 -08003518 llvm::Function::arg_iterator it(cu->func->arg_begin());
3519 llvm::Function::arg_iterator it_end(cu->func->arg_end());
buzbeead8f15e2012-06-18 14:49:45 -07003520 for (; it != it_end; ++it) {
3521 llvm::Value* val = it;
buzbeefa57c472012-11-21 12:06:18 -08003522 CreateLocFromValue(cu, val);
buzbeead8f15e2012-06-18 14:49:45 -07003523 }
3524 // Create RegLocations for all non-argument defintions
buzbee28c9a832012-11-21 15:39:13 -08003525 for (llvm::inst_iterator i = llvm::inst_begin(func), e = llvm::inst_end(func); i != e; ++i) {
buzbeead8f15e2012-06-18 14:49:45 -07003526 llvm::Value* val = &*i;
3527 if (val->hasName() && (val->getName().str().c_str()[0] == 'v')) {
buzbeefa57c472012-11-21 12:06:18 -08003528 CreateLocFromValue(cu, val);
buzbeead8f15e2012-06-18 14:49:45 -07003529 }
3530 }
3531
buzbee2cfc6392012-05-07 14:51:40 -07003532 // Walk the blocks, generating code.
buzbee28c9a832012-11-21 15:39:13 -08003533 for (llvm::Function::iterator i = cu->func->begin(), e = cu->func->end(); i != e; ++i) {
buzbeefa57c472012-11-21 12:06:18 -08003534 BitcodeBlockCodeGen(cu, static_cast<llvm::BasicBlock*>(i));
buzbee2cfc6392012-05-07 14:51:40 -07003535 }
3536
buzbee02031b12012-11-23 09:41:35 -08003537 cg->HandleSuspendLaunchPads(cu);
buzbee2cfc6392012-05-07 14:51:40 -07003538
buzbee02031b12012-11-23 09:41:35 -08003539 cg->HandleThrowLaunchPads(cu);
buzbee2cfc6392012-05-07 14:51:40 -07003540
buzbee02031b12012-11-23 09:41:35 -08003541 cg->HandleIntrinsicLaunchPads(cu);
buzbee2cfc6392012-05-07 14:51:40 -07003542
buzbeefa57c472012-11-21 12:06:18 -08003543 cu->func->eraseFromParent();
3544 cu->func = NULL;
buzbee2cfc6392012-05-07 14:51:40 -07003545}
3546
3547
3548} // namespace art