blob: d29b075ff8f19f22cdbb91d123bc0d57719b4b78 [file] [log] [blame]
Eric Christopherab695882010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ARM-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// ARMGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Eric Christopher456144e2010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopherd10cd7b2010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopherab695882010-07-21 22:26:11 +000019#include "ARMRegisterInfo.h"
20#include "ARMTargetMachine.h"
21#include "ARMSubtarget.h"
Eric Christopherc9932f62010-10-01 23:24:42 +000022#include "ARMConstantPoolValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000023#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/Instructions.h"
27#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000028#include "llvm/Module.h"
Eric Christopherab695882010-07-21 22:26:11 +000029#include "llvm/CodeGen/Analysis.h"
30#include "llvm/CodeGen/FastISel.h"
31#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000034#include "llvm/CodeGen/MachineConstantPool.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000038#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000041#include "llvm/Target/TargetData.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetLowering.h"
44#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000045#include "llvm/Target/TargetOptions.h"
46using namespace llvm;
47
Eric Christopher038fea52010-08-17 00:46:57 +000048static cl::opt<bool>
Eric Christopher8ff9a9d2010-10-11 20:26:21 +000049EnableARMFastISel("arm-fast-isel",
50 cl::desc("Turn on experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000051 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000052
Eric Christopherab695882010-07-21 22:26:11 +000053namespace {
54
55class ARMFastISel : public FastISel {
56
57 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
58 /// make the right decision when generating code for different targets.
59 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000060 const TargetMachine &TM;
61 const TargetInstrInfo &TII;
62 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000063 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000064
Eric Christopher8cf6c602010-09-29 22:24:45 +000065 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000066 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000067 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000068
Eric Christopherab695882010-07-21 22:26:11 +000069 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000070 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000071 : FastISel(funcInfo),
72 TM(funcInfo.MF->getTarget()),
73 TII(*TM.getInstrInfo()),
74 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000075 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000076 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000077 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000078 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000079 }
80
Eric Christophercb592292010-08-20 00:20:31 +000081 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000082 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC);
84 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC,
86 unsigned Op0, bool Op0IsKill);
87 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
88 const TargetRegisterClass *RC,
89 unsigned Op0, bool Op0IsKill,
90 unsigned Op1, bool Op1IsKill);
91 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
92 const TargetRegisterClass *RC,
93 unsigned Op0, bool Op0IsKill,
94 uint64_t Imm);
95 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
96 const TargetRegisterClass *RC,
97 unsigned Op0, bool Op0IsKill,
98 const ConstantFP *FPImm);
99 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
100 const TargetRegisterClass *RC,
101 uint64_t Imm);
102 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
103 const TargetRegisterClass *RC,
104 unsigned Op0, bool Op0IsKill,
105 unsigned Op1, bool Op1IsKill,
106 uint64_t Imm);
107 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
108 unsigned Op0, bool Op0IsKill,
109 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000110
Eric Christophercb592292010-08-20 00:20:31 +0000111 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000112 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000113 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000114 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000115
116 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000117
Eric Christopher83007122010-08-23 21:44:12 +0000118 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000119 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000120 virtual bool SelectLoad(const Instruction *I);
121 virtual bool SelectStore(const Instruction *I);
122 virtual bool SelectBranch(const Instruction *I);
123 virtual bool SelectCmp(const Instruction *I);
124 virtual bool SelectFPExt(const Instruction *I);
125 virtual bool SelectFPTrunc(const Instruction *I);
126 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
127 virtual bool SelectSIToFP(const Instruction *I);
128 virtual bool SelectFPToSI(const Instruction *I);
129 virtual bool SelectSDiv(const Instruction *I);
Eric Christopher6a880d62010-10-11 08:37:26 +0000130 virtual bool SelectSRem(const Instruction *I);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000131 virtual bool SelectCall(const Instruction *I);
Eric Christopher3bbd3962010-10-11 08:27:59 +0000132 virtual bool SelectSelect(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000133
Eric Christopher83007122010-08-23 21:44:12 +0000134 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000135 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000136 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000137 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000138 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000139 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000140 bool ARMLoadAlloca(const Instruction *I, EVT VT);
141 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000142 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000143 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000144 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000145 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000146 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000147 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000148
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000149 // Call handling routines.
150 private:
151 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000152 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000153 SmallVectorImpl<unsigned> &ArgRegs,
154 SmallVectorImpl<EVT> &ArgVTs,
155 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
156 SmallVectorImpl<unsigned> &RegArgs,
157 CallingConv::ID CC,
158 unsigned &NumBytes);
159 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
160 const Instruction *I, CallingConv::ID CC,
161 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000162 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000163
164 // OptionalDef handling routines.
165 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000166 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
167 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
168};
Eric Christopherab695882010-07-21 22:26:11 +0000169
170} // end anonymous namespace
171
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000172#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000173
Eric Christopher456144e2010-08-19 00:37:05 +0000174// DefinesOptionalPredicate - This is different from DefinesPredicate in that
175// we don't care about implicit defs here, just places we'll need to add a
176// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
177bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
178 const TargetInstrDesc &TID = MI->getDesc();
179 if (!TID.hasOptionalDef())
180 return false;
181
182 // Look to see if our OptionalDef is defining CPSR or CCR.
183 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
184 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000185 if (!MO.isReg() || !MO.isDef()) continue;
186 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000187 *CPSR = true;
188 }
189 return true;
190}
191
192// If the machine is predicable go ahead and add the predicate operands, if
193// it needs default CC operands add those.
194const MachineInstrBuilder &
195ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
196 MachineInstr *MI = &*MIB;
197
198 // Do we use a predicate?
199 if (TII.isPredicable(MI))
200 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000201
Eric Christopher456144e2010-08-19 00:37:05 +0000202 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
203 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000204 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000205 if (DefinesOptionalPredicate(MI, &CPSR)) {
206 if (CPSR)
207 AddDefaultT1CC(MIB);
208 else
209 AddDefaultCC(MIB);
210 }
211 return MIB;
212}
213
Eric Christopher0fe7d542010-08-17 01:25:29 +0000214unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
215 const TargetRegisterClass* RC) {
216 unsigned ResultReg = createResultReg(RC);
217 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
218
Eric Christopher456144e2010-08-19 00:37:05 +0000219 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000220 return ResultReg;
221}
222
223unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
224 const TargetRegisterClass *RC,
225 unsigned Op0, bool Op0IsKill) {
226 unsigned ResultReg = createResultReg(RC);
227 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
228
229 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000230 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000231 .addReg(Op0, Op0IsKill * RegState::Kill));
232 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000234 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000235 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000236 TII.get(TargetOpcode::COPY), ResultReg)
237 .addReg(II.ImplicitDefs[0]));
238 }
239 return ResultReg;
240}
241
242unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
243 const TargetRegisterClass *RC,
244 unsigned Op0, bool Op0IsKill,
245 unsigned Op1, bool Op1IsKill) {
246 unsigned ResultReg = createResultReg(RC);
247 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
248
249 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000250 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000251 .addReg(Op0, Op0IsKill * RegState::Kill)
252 .addReg(Op1, Op1IsKill * RegState::Kill));
253 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000255 .addReg(Op0, Op0IsKill * RegState::Kill)
256 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000257 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000258 TII.get(TargetOpcode::COPY), ResultReg)
259 .addReg(II.ImplicitDefs[0]));
260 }
261 return ResultReg;
262}
263
264unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
265 const TargetRegisterClass *RC,
266 unsigned Op0, bool Op0IsKill,
267 uint64_t Imm) {
268 unsigned ResultReg = createResultReg(RC);
269 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
270
271 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000272 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000273 .addReg(Op0, Op0IsKill * RegState::Kill)
274 .addImm(Imm));
275 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000276 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000277 .addReg(Op0, Op0IsKill * RegState::Kill)
278 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000280 TII.get(TargetOpcode::COPY), ResultReg)
281 .addReg(II.ImplicitDefs[0]));
282 }
283 return ResultReg;
284}
285
286unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
287 const TargetRegisterClass *RC,
288 unsigned Op0, bool Op0IsKill,
289 const ConstantFP *FPImm) {
290 unsigned ResultReg = createResultReg(RC);
291 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
292
293 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295 .addReg(Op0, Op0IsKill * RegState::Kill)
296 .addFPImm(FPImm));
297 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000299 .addReg(Op0, Op0IsKill * RegState::Kill)
300 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 TII.get(TargetOpcode::COPY), ResultReg)
303 .addReg(II.ImplicitDefs[0]));
304 }
305 return ResultReg;
306}
307
308unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
309 const TargetRegisterClass *RC,
310 unsigned Op0, bool Op0IsKill,
311 unsigned Op1, bool Op1IsKill,
312 uint64_t Imm) {
313 unsigned ResultReg = createResultReg(RC);
314 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
315
316 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000317 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000318 .addReg(Op0, Op0IsKill * RegState::Kill)
319 .addReg(Op1, Op1IsKill * RegState::Kill)
320 .addImm(Imm));
321 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323 .addReg(Op0, Op0IsKill * RegState::Kill)
324 .addReg(Op1, Op1IsKill * RegState::Kill)
325 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 TII.get(TargetOpcode::COPY), ResultReg)
328 .addReg(II.ImplicitDefs[0]));
329 }
330 return ResultReg;
331}
332
333unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
334 const TargetRegisterClass *RC,
335 uint64_t Imm) {
336 unsigned ResultReg = createResultReg(RC);
337 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000338
Eric Christopher0fe7d542010-08-17 01:25:29 +0000339 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000341 .addImm(Imm));
342 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000343 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000344 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000345 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000346 TII.get(TargetOpcode::COPY), ResultReg)
347 .addReg(II.ImplicitDefs[0]));
348 }
349 return ResultReg;
350}
351
352unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
353 unsigned Op0, bool Op0IsKill,
354 uint32_t Idx) {
355 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
356 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
357 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000358 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000359 DL, TII.get(TargetOpcode::COPY), ResultReg)
360 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
361 return ResultReg;
362}
363
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000364// TODO: Don't worry about 64-bit now, but when this is fixed remove the
365// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000366unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000367 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000368
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000369 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
371 TII.get(ARM::VMOVRS), MoveReg)
372 .addReg(SrcReg));
373 return MoveReg;
374}
375
376unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000377 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000378
Eric Christopheraa3ace12010-09-09 20:49:25 +0000379 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000381 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000382 .addReg(SrcReg));
383 return MoveReg;
384}
385
Eric Christopher9ed58df2010-09-09 00:19:41 +0000386// For double width floating point we need to materialize two constants
387// (the high and the low) into integer registers then use a move to get
388// the combined constant into an FP reg.
389unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
390 const APFloat Val = CFP->getValueAPF();
391 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000392
Eric Christopher9ed58df2010-09-09 00:19:41 +0000393 // This checks to see if we can use VFP3 instructions to materialize
394 // a constant, otherwise we have to go through the constant pool.
395 if (TLI.isFPImmLegal(Val, VT)) {
396 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
397 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
399 DestReg)
400 .addFPImm(CFP));
401 return DestReg;
402 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000403
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000404 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000405 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000406
Eric Christopher238bb162010-09-09 23:50:00 +0000407 // MachineConstantPool wants an explicit alignment.
408 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
409 if (Align == 0) {
410 // TODO: Figure out if this is correct.
411 Align = TD.getTypeAllocSize(CFP->getType());
412 }
413 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
414 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
415 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000416
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000417 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000418 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
419 DestReg)
420 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000421 .addReg(0));
422 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000423}
424
Eric Christopher744c7c82010-09-28 22:47:54 +0000425unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000426
Eric Christopher744c7c82010-09-28 22:47:54 +0000427 // For now 32-bit only.
428 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000429
Eric Christopher56d2b722010-09-02 23:43:26 +0000430 // MachineConstantPool wants an explicit alignment.
431 unsigned Align = TD.getPrefTypeAlignment(C->getType());
432 if (Align == 0) {
433 // TODO: Figure out if this is correct.
434 Align = TD.getTypeAllocSize(C->getType());
435 }
436 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000437 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000438
Eric Christopher56d2b722010-09-02 23:43:26 +0000439 if (isThumb)
440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000441 TII.get(ARM::t2LDRpci), DestReg)
442 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000443 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000444 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000445 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000446 TII.get(ARM::LDRcp), DestReg)
447 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000448 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000449
Eric Christopher56d2b722010-09-02 23:43:26 +0000450 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000451}
452
Eric Christopherc9932f62010-10-01 23:24:42 +0000453unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000454 // For now 32-bit only.
455 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000456
Eric Christopher890dbbe2010-10-02 00:32:44 +0000457 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000458
Eric Christopher890dbbe2010-10-02 00:32:44 +0000459 // TODO: No external globals for now.
460 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000461
Eric Christopher890dbbe2010-10-02 00:32:44 +0000462 // TODO: Need more magic for ARM PIC.
463 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000464
Eric Christopher890dbbe2010-10-02 00:32:44 +0000465 // MachineConstantPool wants an explicit alignment.
466 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
467 if (Align == 0) {
468 // TODO: Figure out if this is correct.
469 Align = TD.getTypeAllocSize(GV->getType());
470 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000471
Eric Christopher890dbbe2010-10-02 00:32:44 +0000472 // Grab index.
473 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
474 unsigned Id = AFI->createConstPoolEntryUId();
475 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
476 ARMCP::CPValue, PCAdj);
477 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000478
Eric Christopher890dbbe2010-10-02 00:32:44 +0000479 // Load value.
480 MachineInstrBuilder MIB;
481 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
482 if (isThumb) {
483 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
484 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
485 .addConstantPoolIndex(Idx);
486 if (RelocM == Reloc::PIC_)
487 MIB.addImm(Id);
488 } else {
489 // The extra reg and immediate are for addrmode2.
490 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
491 DestReg)
492 .addConstantPoolIndex(Idx)
493 .addReg(0).addImm(0);
494 }
495 AddOptionalDefs(MIB);
496 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000497}
498
Eric Christopher9ed58df2010-09-09 00:19:41 +0000499unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
500 EVT VT = TLI.getValueType(C->getType(), true);
501
502 // Only handle simple types.
503 if (!VT.isSimple()) return 0;
504
505 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
506 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000507 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
508 return ARMMaterializeGV(GV, VT);
509 else if (isa<ConstantInt>(C))
510 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000511
Eric Christopherc9932f62010-10-01 23:24:42 +0000512 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000513}
514
Eric Christopherf9764fa2010-09-30 20:49:44 +0000515unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
516 // Don't handle dynamic allocas.
517 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000518
Eric Christopherf9764fa2010-09-30 20:49:44 +0000519 EVT VT;
520 if (!isTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000521
Eric Christopherf9764fa2010-09-30 20:49:44 +0000522 DenseMap<const AllocaInst*, int>::iterator SI =
523 FuncInfo.StaticAllocaMap.find(AI);
524
525 // This will get lowered later into the correct offsets and registers
526 // via rewriteXFrameIndex.
527 if (SI != FuncInfo.StaticAllocaMap.end()) {
528 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
529 unsigned ResultReg = createResultReg(RC);
530 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
531 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
532 TII.get(Opc), ResultReg)
533 .addFrameIndex(SI->second)
534 .addImm(0));
535 return ResultReg;
536 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000537
Eric Christopherf9764fa2010-09-30 20:49:44 +0000538 return 0;
539}
540
Eric Christopherb1cc8482010-08-25 07:23:49 +0000541bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
542 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000543
Eric Christopherb1cc8482010-08-25 07:23:49 +0000544 // Only handle simple types.
545 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000546
Eric Christopherdc908042010-08-31 01:28:42 +0000547 // Handle all legal types, i.e. a register that will directly hold this
548 // value.
549 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000550}
551
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000552bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
553 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000554
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000555 // If this is a type than can be sign or zero-extended to a basic operation
556 // go ahead and accept it now.
557 if (VT == MVT::i8 || VT == MVT::i16)
558 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000559
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000560 return false;
561}
562
Eric Christophercb0b04b2010-08-24 00:07:24 +0000563// Computes the Reg+Offset to get to an object.
564bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000565 int &Offset) {
566 // Some boilerplate from the X86 FastISel.
567 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000568 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000569 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000570 // Don't walk into other basic blocks; it's possible we haven't
571 // visited them yet, so the instructions may not yet be assigned
572 // virtual registers.
573 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
574 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000575 Opcode = I->getOpcode();
576 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000577 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000578 Opcode = C->getOpcode();
579 U = C;
580 }
581
Eric Christophercb0b04b2010-08-24 00:07:24 +0000582 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000583 if (Ty->getAddressSpace() > 255)
584 // Fast instruction selection doesn't support the special
585 // address spaces.
586 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000587
Eric Christopher83007122010-08-23 21:44:12 +0000588 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000589 default:
Eric Christopher83007122010-08-23 21:44:12 +0000590 break;
591 case Instruction::Alloca: {
Eric Christopher050d16c2010-10-11 21:37:35 +0000592 // Don't handle dynamic allocas.
Eric Christopher5f9e8b92010-10-11 22:01:22 +0000593 assert(!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Obj)) &&
594 "Alloca should have been handled earlier!");
Eric Christopherf06f3092010-08-24 00:50:47 +0000595 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000596 }
597 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000598
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000599 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000600 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000601 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000602 return false;
603 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000604
Eric Christophercb0b04b2010-08-24 00:07:24 +0000605 // Try to get this in a register if nothing else has worked.
606 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000607 if (Reg == 0) return false;
608
609 // Since the offset may be too large for the load instruction
610 // get the reg+offset into a register.
611 // TODO: Verify the additions work, otherwise we'll need to add the
612 // offset instead of 0 to the instructions and do all sorts of operand
613 // munging.
614 // TODO: Optimize this somewhat.
615 if (Offset != 0) {
616 ARMCC::CondCodes Pred = ARMCC::AL;
617 unsigned PredReg = 0;
618
Eric Christophereaa204b2010-09-02 01:39:14 +0000619 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000620 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
621 Reg, Reg, Offset, Pred, PredReg,
622 static_cast<const ARMBaseInstrInfo&>(TII));
623 else {
624 assert(AFI->isThumb2Function());
625 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
626 Reg, Reg, Offset, Pred, PredReg,
627 static_cast<const ARMBaseInstrInfo&>(TII));
628 }
629 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000630 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000631}
632
Eric Christopher30b66332010-09-08 21:49:50 +0000633bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000634 Value *Op0 = I->getOperand(0);
635
Eric Christopherdf1f5a92010-10-07 21:40:18 +0000636 // Promote load/store types.
637 if (VT == MVT::i8 || VT == MVT::i16) VT = MVT::i32;
638
Eric Christopherf06f3092010-08-24 00:50:47 +0000639 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000640 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
641 DenseMap<const AllocaInst*, int>::iterator SI =
642 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000643
Eric Christophere24d66f2010-08-24 22:07:27 +0000644 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000645 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000646 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000647 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000648 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000649 TM.getRegisterInfo());
650 UpdateValueMap(I, ResultReg);
651 return true;
652 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000653 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000654 return false;
655}
656
Eric Christopherb1cc8482010-08-25 07:23:49 +0000657bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
658 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000659
Eric Christopherb1cc8482010-08-25 07:23:49 +0000660 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000661 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000662 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000663 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000664 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000665 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000666 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000667 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000668 case MVT::i16:
Eric Christopher7a56f332010-10-08 01:13:17 +0000669 Opc = isThumb ? ARM::t2LDRHi8 : ARM::LDRH;
670 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000671 VT = MVT::i32;
672 break;
673 case MVT::i8:
Eric Christopher7a56f332010-10-08 01:13:17 +0000674 Opc = isThumb ? ARM::t2LDRBi8 : ARM::LDRB;
675 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000676 VT = MVT::i32;
677 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000678 case MVT::i32:
Eric Christopher7a56f332010-10-08 01:13:17 +0000679 Opc = isThumb ? ARM::t2LDRi8 : ARM::LDR;
680 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000681 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000682 case MVT::f32:
683 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000684 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000685 isFloat = true;
686 break;
687 case MVT::f64:
688 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000689 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000690 isFloat = true;
691 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000692 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000693
Eric Christopheree56ea62010-10-07 05:50:44 +0000694 ResultReg = createResultReg(RC);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000695
Eric Christopher7a56f332010-10-08 01:13:17 +0000696 // For now with the additions above the offset should be zero - thus we
697 // can always fit into an i8.
698 assert(Offset == 0 && "Offset not zero!");
Eric Christopherdccd2c32010-10-11 08:38:55 +0000699
Eric Christopher7a56f332010-10-08 01:13:17 +0000700 // The thumb and floating point instructions both take 2 operands, ARM takes
701 // another register.
702 if (isFloat || isThumb)
Eric Christopher6dab1372010-09-18 01:59:37 +0000703 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
704 TII.get(Opc), ResultReg)
705 .addReg(Reg).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000706 else
707 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
708 TII.get(Opc), ResultReg)
709 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000710 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000711}
712
Eric Christopher43b62be2010-09-27 06:02:23 +0000713bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000714 // Verify we have a legal type before going any further.
715 EVT VT;
716 if (!isLoadTypeLegal(I->getType(), VT))
717 return false;
718
719 // If we're an alloca we know we have a frame index and can emit the load
720 // directly in short order.
721 if (ARMLoadAlloca(I, VT))
722 return true;
723
724 // Our register and offset with innocuous defaults.
725 unsigned Reg = 0;
726 int Offset = 0;
727
728 // See if we can handle this as Reg + Offset
729 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
730 return false;
731
732 unsigned ResultReg;
733 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
734
735 UpdateValueMap(I, ResultReg);
736 return true;
737}
738
Eric Christopher30b66332010-09-08 21:49:50 +0000739bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000740 Value *Op1 = I->getOperand(1);
741
Eric Christopherdf1f5a92010-10-07 21:40:18 +0000742 // Promote load/store types.
743 if (VT == MVT::i8 || VT == MVT::i16) VT = MVT::i32;
744
Eric Christopher543cf052010-09-01 22:16:27 +0000745 // Verify it's an alloca.
746 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
747 DenseMap<const AllocaInst*, int>::iterator SI =
748 FuncInfo.StaticAllocaMap.find(AI);
749
750 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000751 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000752 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000753 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000754 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000755 TM.getRegisterInfo());
756 return true;
757 }
758 }
759 return false;
760}
761
Eric Christopher318b6ee2010-09-02 00:53:56 +0000762bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
763 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000764 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000765 bool isFloat = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000766 switch (VT.getSimpleVT().SimpleTy) {
767 default: return false;
768 case MVT::i1:
Eric Christophere93417b2010-10-08 23:52:16 +0000769 case MVT::i8: StrOpc = isThumb ? ARM::t2STRBi8 : ARM::STRB; break;
770 case MVT::i16: StrOpc = isThumb ? ARM::t2STRHi8 : ARM::STRH; break;
771 case MVT::i32: StrOpc = isThumb ? ARM::t2STRi8 : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000772 case MVT::f32:
773 if (!Subtarget->hasVFP2()) return false;
774 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000775 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000776 break;
777 case MVT::f64:
778 if (!Subtarget->hasVFP2()) return false;
779 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000780 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000781 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000782 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000783
Eric Christopherb74558a2010-09-18 01:23:38 +0000784 // The thumb addressing mode has operands swapped from the arm addressing
785 // mode, the floating point one only has two operands.
Eric Christophere93417b2010-10-08 23:52:16 +0000786 if (isFloat || isThumb)
Eric Christopherb74558a2010-09-18 01:23:38 +0000787 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000788 TII.get(StrOpc))
789 .addReg(SrcReg).addReg(DstReg).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000790 else
791 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000792 TII.get(StrOpc))
793 .addReg(SrcReg).addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000794
Eric Christopher318b6ee2010-09-02 00:53:56 +0000795 return true;
796}
797
Eric Christopher43b62be2010-09-27 06:02:23 +0000798bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000799 Value *Op0 = I->getOperand(0);
800 unsigned SrcReg = 0;
801
Eric Christopher543cf052010-09-01 22:16:27 +0000802 // Yay type legalization
803 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000804 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000805 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000806
Eric Christopher1b61ef42010-09-02 01:48:11 +0000807 // Get the value to be stored into a register.
808 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000809 if (SrcReg == 0)
810 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000811
Eric Christopher318b6ee2010-09-02 00:53:56 +0000812 // If we're an alloca we know we have a frame index and can emit the store
813 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000814 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000815 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000816
Eric Christopher318b6ee2010-09-02 00:53:56 +0000817 // Our register and offset with innocuous defaults.
818 unsigned Reg = 0;
819 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000820
Eric Christopher318b6ee2010-09-02 00:53:56 +0000821 // See if we can handle this as Reg + Offset
822 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
823 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000824
Eric Christopher318b6ee2010-09-02 00:53:56 +0000825 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000826
Eric Christophera5b1e682010-09-17 22:28:18 +0000827 return true;
828}
829
830static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
831 switch (Pred) {
832 // Needs two compares...
833 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000834 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000835 default:
836 assert(false && "Unhandled CmpInst::Predicate!");
837 return ARMCC::AL;
838 case CmpInst::ICMP_EQ:
839 case CmpInst::FCMP_OEQ:
840 return ARMCC::EQ;
841 case CmpInst::ICMP_SGT:
842 case CmpInst::FCMP_OGT:
843 return ARMCC::GT;
844 case CmpInst::ICMP_SGE:
845 case CmpInst::FCMP_OGE:
846 return ARMCC::GE;
847 case CmpInst::ICMP_UGT:
848 case CmpInst::FCMP_UGT:
849 return ARMCC::HI;
850 case CmpInst::FCMP_OLT:
851 return ARMCC::MI;
852 case CmpInst::ICMP_ULE:
853 case CmpInst::FCMP_OLE:
854 return ARMCC::LS;
855 case CmpInst::FCMP_ORD:
856 return ARMCC::VC;
857 case CmpInst::FCMP_UNO:
858 return ARMCC::VS;
859 case CmpInst::FCMP_UGE:
860 return ARMCC::PL;
861 case CmpInst::ICMP_SLT:
862 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000863 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000864 case CmpInst::ICMP_SLE:
865 case CmpInst::FCMP_ULE:
866 return ARMCC::LE;
867 case CmpInst::FCMP_UNE:
868 case CmpInst::ICMP_NE:
869 return ARMCC::NE;
870 case CmpInst::ICMP_UGE:
871 return ARMCC::HS;
872 case CmpInst::ICMP_ULT:
873 return ARMCC::LO;
874 }
Eric Christopher543cf052010-09-01 22:16:27 +0000875}
876
Eric Christopher43b62be2010-09-27 06:02:23 +0000877bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000878 const BranchInst *BI = cast<BranchInst>(I);
879 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
880 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000881
Eric Christophere5734102010-09-03 00:35:47 +0000882 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000883 // TODO: Try to avoid the re-computation in some places.
884 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000885 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000886
Eric Christopher229207a2010-09-29 01:14:47 +0000887 // Re-set the flags just in case.
888 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
889 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
890 .addReg(CondReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000891
Eric Christophere5734102010-09-03 00:35:47 +0000892 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000893 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000894 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000895 FastEmitBranch(FBB, DL);
896 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000897 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000898}
899
Eric Christopher43b62be2010-09-27 06:02:23 +0000900bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000901 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000902
Eric Christopherd43393a2010-09-08 23:13:45 +0000903 EVT VT;
904 const Type *Ty = CI->getOperand(0)->getType();
905 if (!isTypeLegal(Ty, VT))
906 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000907
Eric Christopherd43393a2010-09-08 23:13:45 +0000908 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
909 if (isFloat && !Subtarget->hasVFP2())
910 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000911
Eric Christopherd43393a2010-09-08 23:13:45 +0000912 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000913 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000914 switch (VT.getSimpleVT().SimpleTy) {
915 default: return false;
916 // TODO: Verify compares.
917 case MVT::f32:
918 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000919 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000920 break;
921 case MVT::f64:
922 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000923 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000924 break;
925 case MVT::i32:
926 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000927 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000928 break;
929 }
930
Eric Christopher229207a2010-09-29 01:14:47 +0000931 // Get the compare predicate.
932 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +0000933
Eric Christopher229207a2010-09-29 01:14:47 +0000934 // We may not handle every CC for now.
935 if (ARMPred == ARMCC::AL) return false;
936
Eric Christopherd43393a2010-09-08 23:13:45 +0000937 unsigned Arg1 = getRegForValue(CI->getOperand(0));
938 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000939
Eric Christopherd43393a2010-09-08 23:13:45 +0000940 unsigned Arg2 = getRegForValue(CI->getOperand(1));
941 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000942
Eric Christopherd43393a2010-09-08 23:13:45 +0000943 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
944 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000945
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000946 // For floating point we need to move the result to a comparison register
947 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000948 if (isFloat)
949 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
950 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000951
Eric Christopher229207a2010-09-29 01:14:47 +0000952 // Now set a register based on the comparison. Explicitly set the predicates
953 // here.
Eric Christopher338c2532010-10-07 05:31:49 +0000954 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000955 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +0000956 : ARM::GPRRegisterClass;
957 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000958 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +0000959 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +0000960 unsigned ZeroReg = TargetMaterializeConstant(Zero);
961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
962 .addReg(ZeroReg).addImm(1)
963 .addImm(ARMPred).addReg(CondReg);
964
Eric Christophera5b1e682010-09-17 22:28:18 +0000965 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +0000966 return true;
967}
968
Eric Christopher43b62be2010-09-27 06:02:23 +0000969bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +0000970 // Make sure we have VFP and that we're extending float to double.
971 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000972
Eric Christopher46203602010-09-09 00:26:48 +0000973 Value *V = I->getOperand(0);
974 if (!I->getType()->isDoubleTy() ||
975 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000976
Eric Christopher46203602010-09-09 00:26:48 +0000977 unsigned Op = getRegForValue(V);
978 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000979
Eric Christopher46203602010-09-09 00:26:48 +0000980 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000981 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000982 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000983 .addReg(Op));
984 UpdateValueMap(I, Result);
985 return true;
986}
987
Eric Christopher43b62be2010-09-27 06:02:23 +0000988bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +0000989 // Make sure we have VFP and that we're truncating double to float.
990 if (!Subtarget->hasVFP2()) return false;
991
992 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +0000993 if (!(I->getType()->isFloatTy() &&
994 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +0000995
996 unsigned Op = getRegForValue(V);
997 if (Op == 0) return false;
998
999 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001000 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001001 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001002 .addReg(Op));
1003 UpdateValueMap(I, Result);
1004 return true;
1005}
1006
Eric Christopher43b62be2010-09-27 06:02:23 +00001007bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001008 // Make sure we have VFP.
1009 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001010
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001011 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001012 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001013 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001014 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001015
Eric Christopher9a040492010-09-09 18:54:59 +00001016 unsigned Op = getRegForValue(I->getOperand(0));
1017 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001018
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001019 // The conversion routine works on fp-reg to fp-reg and the operand above
1020 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001021 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001022 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001023
Eric Christopher9a040492010-09-09 18:54:59 +00001024 unsigned Opc;
1025 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1026 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1027 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001028
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001029 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001030 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1031 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001032 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001033 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001034 return true;
1035}
1036
Eric Christopher43b62be2010-09-27 06:02:23 +00001037bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001038 // Make sure we have VFP.
1039 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001040
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001041 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001042 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001043 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001044 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001045
Eric Christopher9a040492010-09-09 18:54:59 +00001046 unsigned Op = getRegForValue(I->getOperand(0));
1047 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001048
Eric Christopher9a040492010-09-09 18:54:59 +00001049 unsigned Opc;
1050 const Type *OpTy = I->getOperand(0)->getType();
1051 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1052 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1053 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001054
Eric Christopher022b7fb2010-10-05 23:13:24 +00001055 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1056 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001057 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1058 ResultReg)
1059 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001060
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001061 // This result needs to be in an integer register, but the conversion only
1062 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001063 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001064 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001065
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001066 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001067 return true;
1068}
1069
Eric Christopher3bbd3962010-10-11 08:27:59 +00001070bool ARMFastISel::SelectSelect(const Instruction *I) {
1071 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1072 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1073 return false;
1074
1075 // Things need to be register sized for register moves.
1076 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1077 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1078
1079 unsigned CondReg = getRegForValue(I->getOperand(0));
1080 if (CondReg == 0) return false;
1081 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1082 if (Op1Reg == 0) return false;
1083 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1084 if (Op2Reg == 0) return false;
1085
1086 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1087 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1088 .addReg(CondReg).addImm(1));
1089 unsigned ResultReg = createResultReg(RC);
1090 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1092 .addReg(Op1Reg).addReg(Op2Reg)
1093 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1094 UpdateValueMap(I, ResultReg);
1095 return true;
1096}
1097
Eric Christopher08637852010-09-30 22:34:19 +00001098bool ARMFastISel::SelectSDiv(const Instruction *I) {
1099 EVT VT;
1100 const Type *Ty = I->getType();
1101 if (!isTypeLegal(Ty, VT))
1102 return false;
1103
1104 // If we have integer div support we should have selected this automagically.
1105 // In case we have a real miss go ahead and return false and we'll pick
1106 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001107 if (Subtarget->hasDivide()) return false;
1108
Eric Christopher08637852010-09-30 22:34:19 +00001109 // Otherwise emit a libcall.
1110 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001111 if (VT == MVT::i8)
1112 LC = RTLIB::SDIV_I8;
1113 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001114 LC = RTLIB::SDIV_I16;
1115 else if (VT == MVT::i32)
1116 LC = RTLIB::SDIV_I32;
1117 else if (VT == MVT::i64)
1118 LC = RTLIB::SDIV_I64;
1119 else if (VT == MVT::i128)
1120 LC = RTLIB::SDIV_I128;
1121 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001122
Eric Christopher08637852010-09-30 22:34:19 +00001123 return ARMEmitLibcall(I, LC);
1124}
1125
Eric Christopher6a880d62010-10-11 08:37:26 +00001126bool ARMFastISel::SelectSRem(const Instruction *I) {
1127 EVT VT;
1128 const Type *Ty = I->getType();
1129 if (!isTypeLegal(Ty, VT))
1130 return false;
1131
1132 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1133 if (VT == MVT::i8)
1134 LC = RTLIB::SREM_I8;
1135 else if (VT == MVT::i16)
1136 LC = RTLIB::SREM_I16;
1137 else if (VT == MVT::i32)
1138 LC = RTLIB::SREM_I32;
1139 else if (VT == MVT::i64)
1140 LC = RTLIB::SREM_I64;
1141 else if (VT == MVT::i128)
1142 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001143 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher6a880d62010-10-11 08:37:26 +00001144
1145 return ARMEmitLibcall(I, LC);
1146}
1147
Eric Christopher43b62be2010-09-27 06:02:23 +00001148bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001149 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001150
Eric Christopherbc39b822010-09-09 00:53:57 +00001151 // We can get here in the case when we want to use NEON for our fp
1152 // operations, but can't figure out how to. Just use the vfp instructions
1153 // if we have them.
1154 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001155 const Type *Ty = I->getType();
1156 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1157 if (isFloat && !Subtarget->hasVFP2())
1158 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001159
Eric Christopherbc39b822010-09-09 00:53:57 +00001160 unsigned Op1 = getRegForValue(I->getOperand(0));
1161 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001162
Eric Christopherbc39b822010-09-09 00:53:57 +00001163 unsigned Op2 = getRegForValue(I->getOperand(1));
1164 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001165
Eric Christopherbc39b822010-09-09 00:53:57 +00001166 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001167 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1168 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001169 switch (ISDOpcode) {
1170 default: return false;
1171 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001172 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001173 break;
1174 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001175 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001176 break;
1177 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001178 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001179 break;
1180 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001181 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001182 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1183 TII.get(Opc), ResultReg)
1184 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001185 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001186 return true;
1187}
1188
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001189// Call Handling Code
1190
1191// This is largely taken directly from CCAssignFnForNode - we don't support
1192// varargs in FastISel so that part has been removed.
1193// TODO: We may not support all of this.
1194CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1195 switch (CC) {
1196 default:
1197 llvm_unreachable("Unsupported calling convention");
1198 case CallingConv::C:
1199 case CallingConv::Fast:
1200 // Use target triple & subtarget features to do actual dispatch.
1201 if (Subtarget->isAAPCS_ABI()) {
1202 if (Subtarget->hasVFP2() &&
1203 FloatABIType == FloatABI::Hard)
1204 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1205 else
1206 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1207 } else
1208 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1209 case CallingConv::ARM_AAPCS_VFP:
1210 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1211 case CallingConv::ARM_AAPCS:
1212 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1213 case CallingConv::ARM_APCS:
1214 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1215 }
1216}
1217
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001218bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1219 SmallVectorImpl<unsigned> &ArgRegs,
1220 SmallVectorImpl<EVT> &ArgVTs,
1221 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1222 SmallVectorImpl<unsigned> &RegArgs,
1223 CallingConv::ID CC,
1224 unsigned &NumBytes) {
1225 SmallVector<CCValAssign, 16> ArgLocs;
1226 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1227 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1228
1229 // Get a count of how many bytes are to be pushed on the stack.
1230 NumBytes = CCInfo.getNextStackOffset();
1231
1232 // Issue CALLSEQ_START
1233 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1235 TII.get(AdjStackDown))
1236 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001237
1238 // Process the args.
1239 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1240 CCValAssign &VA = ArgLocs[i];
1241 unsigned Arg = ArgRegs[VA.getValNo()];
1242 EVT ArgVT = ArgVTs[VA.getValNo()];
1243
Eric Christopherf9764fa2010-09-30 20:49:44 +00001244 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001245 switch (VA.getLocInfo()) {
1246 case CCValAssign::Full: break;
1247 default:
Eric Christopher11077342010-10-07 05:14:08 +00001248 // TODO: Handle arg promotion.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001249 return false;
1250 }
1251
1252 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001253 // TODO: We need custom lowering for f64 args.
1254 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001255 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001256 VA.getLocReg())
1257 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001258 RegArgs.push_back(VA.getLocReg());
1259 } else {
1260 // Need to store
1261 return false;
1262 }
1263 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001264
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001265 return true;
1266}
1267
1268bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1269 const Instruction *I, CallingConv::ID CC,
1270 unsigned &NumBytes) {
1271 // Issue CALLSEQ_END
1272 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1274 TII.get(AdjStackUp))
1275 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001276
1277 // Now the return value.
1278 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1279 SmallVector<CCValAssign, 16> RVLocs;
1280 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1281 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1282
1283 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001284 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1285 // For this move we copy into two registers and then move into the
1286 // double fp reg we want.
1287 // TODO: Are the copies necessary?
1288 TargetRegisterClass *CopyRC = TLI.getRegClassFor(MVT::i32);
1289 unsigned Copy1 = createResultReg(CopyRC);
1290 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1291 Copy1).addReg(RVLocs[0].getLocReg());
1292 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001293
Eric Christopher14df8822010-10-01 00:00:11 +00001294 unsigned Copy2 = createResultReg(CopyRC);
1295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1296 Copy2).addReg(RVLocs[1].getLocReg());
1297 UsedRegs.push_back(RVLocs[1].getLocReg());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001298
Eric Christopher14df8822010-10-01 00:00:11 +00001299 EVT DestVT = RVLocs[0].getValVT();
1300 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1301 unsigned ResultReg = createResultReg(DstRC);
1302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1303 TII.get(ARM::VMOVDRR), ResultReg)
1304 .addReg(Copy1).addReg(Copy2));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001305
1306 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001307 UpdateValueMap(I, ResultReg);
1308 } else {
1309 assert(RVLocs.size() == 1 && "Can't handle non-double multi-reg retvals!");
1310 EVT CopyVT = RVLocs[0].getValVT();
1311 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001312
Eric Christopher14df8822010-10-01 00:00:11 +00001313 unsigned ResultReg = createResultReg(DstRC);
1314 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1315 ResultReg).addReg(RVLocs[0].getLocReg());
1316 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001317
Eric Christopherdccd2c32010-10-11 08:38:55 +00001318 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001319 UpdateValueMap(I, ResultReg);
1320 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001321 }
1322
Eric Christopherdccd2c32010-10-11 08:38:55 +00001323 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001324}
1325
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001326// A quick function that will emit a call for a named libcall in F with the
1327// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001328// can emit a call for any libcall we can produce. This is an abridged version
1329// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001330// like computed function pointers or strange arguments at call sites.
1331// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1332// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001333bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1334 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001335
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001336 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001337 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001338 EVT RetVT;
1339 if (RetTy->isVoidTy())
1340 RetVT = MVT::isVoid;
1341 else if (!isTypeLegal(RetTy, RetVT))
1342 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001343
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001344 // For now we're using BLX etc on the assumption that we have v5t ops.
1345 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001346
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001347 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001348 SmallVector<Value*, 8> Args;
1349 SmallVector<unsigned, 8> ArgRegs;
1350 SmallVector<EVT, 8> ArgVTs;
1351 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1352 Args.reserve(I->getNumOperands());
1353 ArgRegs.reserve(I->getNumOperands());
1354 ArgVTs.reserve(I->getNumOperands());
1355 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001356 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001357 Value *Op = I->getOperand(i);
1358 unsigned Arg = getRegForValue(Op);
1359 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001360
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001361 const Type *ArgTy = Op->getType();
1362 EVT ArgVT;
1363 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001364
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001365 ISD::ArgFlagsTy Flags;
1366 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1367 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001368
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001369 Args.push_back(Op);
1370 ArgRegs.push_back(Arg);
1371 ArgVTs.push_back(ArgVT);
1372 ArgFlags.push_back(Flags);
1373 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001374
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001375 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001376 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001377 unsigned NumBytes;
1378 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1379 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001380
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001381 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001382 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001383 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001384 unsigned CallOpc;
1385 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001386 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001387 else
1388 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001389 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001390 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001391
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001392 // Add implicit physical register uses to the call.
1393 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1394 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001395
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001396 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001397 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001398 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001399
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001400 // Set all unused physreg defs as dead.
1401 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001402
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001403 return true;
1404}
1405
Eric Christopherf9764fa2010-09-30 20:49:44 +00001406bool ARMFastISel::SelectCall(const Instruction *I) {
1407 const CallInst *CI = cast<CallInst>(I);
1408 const Value *Callee = CI->getCalledValue();
1409
1410 // Can't handle inline asm or worry about intrinsics yet.
1411 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1412
Eric Christophere6ca6772010-10-01 21:33:12 +00001413 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001414 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001415 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1416 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001417
Eric Christopherf9764fa2010-09-30 20:49:44 +00001418 // Check the calling convention.
1419 ImmutableCallSite CS(CI);
1420 CallingConv::ID CC = CS.getCallingConv();
1421 // TODO: Avoid some calling conventions?
1422 if (CC != CallingConv::C) {
Eric Christophere540a6f2010-10-05 23:50:58 +00001423 // errs() << "Can't handle calling convention: " << CC << "\n";
Eric Christopherf9764fa2010-09-30 20:49:44 +00001424 return false;
1425 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001426
Eric Christopherf9764fa2010-09-30 20:49:44 +00001427 // Let SDISel handle vararg functions.
1428 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1429 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1430 if (FTy->isVarArg())
1431 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001432
Eric Christopherf9764fa2010-09-30 20:49:44 +00001433 // Handle *simple* calls for now.
1434 const Type *RetTy = I->getType();
1435 EVT RetVT;
1436 if (RetTy->isVoidTy())
1437 RetVT = MVT::isVoid;
1438 else if (!isTypeLegal(RetTy, RetVT))
1439 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001440
Eric Christopherf9764fa2010-09-30 20:49:44 +00001441 // For now we're using BLX etc on the assumption that we have v5t ops.
1442 // TODO: Maybe?
1443 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001444
Eric Christopherf9764fa2010-09-30 20:49:44 +00001445 // Set up the argument vectors.
1446 SmallVector<Value*, 8> Args;
1447 SmallVector<unsigned, 8> ArgRegs;
1448 SmallVector<EVT, 8> ArgVTs;
1449 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1450 Args.reserve(CS.arg_size());
1451 ArgRegs.reserve(CS.arg_size());
1452 ArgVTs.reserve(CS.arg_size());
1453 ArgFlags.reserve(CS.arg_size());
1454 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1455 i != e; ++i) {
1456 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001457
Eric Christopherf9764fa2010-09-30 20:49:44 +00001458 if (Arg == 0)
1459 return false;
1460 ISD::ArgFlagsTy Flags;
1461 unsigned AttrInd = i - CS.arg_begin() + 1;
1462 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1463 Flags.setSExt();
1464 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1465 Flags.setZExt();
1466
1467 // FIXME: Only handle *easy* calls for now.
1468 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1469 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1470 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1471 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1472 return false;
1473
1474 const Type *ArgTy = (*i)->getType();
1475 EVT ArgVT;
1476 if (!isTypeLegal(ArgTy, ArgVT))
1477 return false;
1478 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1479 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001480
Eric Christopherf9764fa2010-09-30 20:49:44 +00001481 Args.push_back(*i);
1482 ArgRegs.push_back(Arg);
1483 ArgVTs.push_back(ArgVT);
1484 ArgFlags.push_back(Flags);
1485 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001486
Eric Christopherf9764fa2010-09-30 20:49:44 +00001487 // Handle the arguments now that we've gotten them.
1488 SmallVector<unsigned, 4> RegArgs;
1489 unsigned NumBytes;
1490 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1491 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001492
Eric Christopherf9764fa2010-09-30 20:49:44 +00001493 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001494 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001495 MachineInstrBuilder MIB;
1496 unsigned CallOpc;
1497 if(isThumb)
1498 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1499 else
1500 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1501 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1502 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001503
Eric Christopherf9764fa2010-09-30 20:49:44 +00001504 // Add implicit physical register uses to the call.
1505 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1506 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001507
Eric Christopherf9764fa2010-09-30 20:49:44 +00001508 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001509 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001510 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001511
Eric Christopherf9764fa2010-09-30 20:49:44 +00001512 // Set all unused physreg defs as dead.
1513 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001514
Eric Christopherf9764fa2010-09-30 20:49:44 +00001515 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001516
Eric Christopherf9764fa2010-09-30 20:49:44 +00001517}
1518
Eric Christopher56d2b722010-09-02 23:43:26 +00001519// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001520bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001521 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001522 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001523
Eric Christopherab695882010-07-21 22:26:11 +00001524 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001525 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001526 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001527 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001528 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001529 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001530 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001531 case Instruction::ICmp:
1532 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001533 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001534 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001535 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001536 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001537 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001538 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001539 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001540 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001541 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001542 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001543 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001544 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001545 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001546 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001547 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001548 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001549 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001550 case Instruction::SRem:
1551 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001552 case Instruction::Call:
1553 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001554 case Instruction::Select:
1555 return SelectSelect(I);
Eric Christopherab695882010-07-21 22:26:11 +00001556 default: break;
1557 }
1558 return false;
1559}
1560
1561namespace llvm {
1562 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001563 // Completely untested on non-darwin.
1564 const TargetMachine &TM = funcInfo.MF->getTarget();
1565 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8ff9a9d2010-10-11 20:26:21 +00001566 if (Subtarget->isTargetDarwin() && EnableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001567 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001568 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001569 }
1570}