blob: 0f3e6cf2c6427fe045011f3d97c860cec16a90cd [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"
Eric Christopherd56d61a2010-10-17 01:51:42 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000040#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000047#include "llvm/Target/TargetOptions.h"
48using namespace llvm;
49
Eric Christopher038fea52010-08-17 00:46:57 +000050static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000051DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000053 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000054
Eric Christopher836c6242010-12-15 23:47:29 +000055extern cl::opt<bool> EnableARMLongCalls;
56
Eric Christopherab695882010-07-21 22:26:11 +000057namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000058
Eric Christopher0d581222010-11-19 22:30:02 +000059 // All possible address modes, plus some.
60 typedef struct Address {
61 enum {
62 RegBase,
63 FrameIndexBase
64 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000065
Eric Christopher0d581222010-11-19 22:30:02 +000066 union {
67 unsigned Reg;
68 int FI;
69 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000070
Eric Christopher0d581222010-11-19 22:30:02 +000071 int Offset;
72 unsigned Scale;
73 unsigned PlusReg;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
77 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
78 Base.Reg = 0;
79 }
80 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000081
82class ARMFastISel : public FastISel {
83
84 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
85 /// make the right decision when generating code for different targets.
86 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 const TargetMachine &TM;
88 const TargetInstrInfo &TII;
89 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000090 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000091
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000093 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000094 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000095
Eric Christopherab695882010-07-21 22:26:11 +000096 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000097 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000098 : FastISel(funcInfo),
99 TM(funcInfo.MF->getTarget()),
100 TII(*TM.getInstrInfo()),
101 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000102 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000103 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000104 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000105 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000106 }
107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000109 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC);
111 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill);
114 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000118 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 unsigned Op1, bool Op1IsKill,
122 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000123 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
124 const TargetRegisterClass *RC,
125 unsigned Op0, bool Op0IsKill,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000131 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 unsigned Op0, bool Op0IsKill,
134 unsigned Op1, bool Op1IsKill,
135 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000136 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
137 const TargetRegisterClass *RC,
138 uint64_t Imm);
139
Eric Christopher0fe7d542010-08-17 01:25:29 +0000140 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
141 unsigned Op0, bool Op0IsKill,
142 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000143
Eric Christophercb592292010-08-20 00:20:31 +0000144 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000145 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000146 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000147 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000148
149 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000150
Eric Christopher83007122010-08-23 21:44:12 +0000151 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000152 private:
Eric Christopher17787722010-10-21 21:47:51 +0000153 bool SelectLoad(const Instruction *I);
154 bool SelectStore(const Instruction *I);
155 bool SelectBranch(const Instruction *I);
156 bool SelectCmp(const Instruction *I);
157 bool SelectFPExt(const Instruction *I);
158 bool SelectFPTrunc(const Instruction *I);
159 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
160 bool SelectSIToFP(const Instruction *I);
161 bool SelectFPToSI(const Instruction *I);
162 bool SelectSDiv(const Instruction *I);
163 bool SelectSRem(const Instruction *I);
164 bool SelectCall(const Instruction *I);
165 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000166 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000167
Eric Christopher83007122010-08-23 21:44:12 +0000168 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000169 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000170 bool isTypeLegal(const Type *Ty, MVT &VT);
171 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000172 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
173 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
174 bool ARMComputeAddress(const Value *Obj, Address &Addr);
175 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000176 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000177 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000178 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000179 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000180 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000181 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000182
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000183 // Call handling routines.
184 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000185 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
186 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000187 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000188 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000189 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000190 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000191 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
192 SmallVectorImpl<unsigned> &RegArgs,
193 CallingConv::ID CC,
194 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000195 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000196 const Instruction *I, CallingConv::ID CC,
197 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000198 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000199
200 // OptionalDef handling routines.
201 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000202 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000203 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
204 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000205 void AddLoadStoreOperands(EVT VT, Address &Addr,
206 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000207};
Eric Christopherab695882010-07-21 22:26:11 +0000208
209} // end anonymous namespace
210
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000211#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000212
Eric Christopher456144e2010-08-19 00:37:05 +0000213// DefinesOptionalPredicate - This is different from DefinesPredicate in that
214// we don't care about implicit defs here, just places we'll need to add a
215// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
216bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
217 const TargetInstrDesc &TID = MI->getDesc();
218 if (!TID.hasOptionalDef())
219 return false;
220
221 // Look to see if our OptionalDef is defining CPSR or CCR.
222 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
223 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000224 if (!MO.isReg() || !MO.isDef()) continue;
225 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000226 *CPSR = true;
227 }
228 return true;
229}
230
Eric Christopheraf3dce52011-03-12 01:09:29 +0000231bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
232 const TargetInstrDesc &TID = MI->getDesc();
233
234 // If we're a thumb2 or not NEON function we were handled via isPredicable.
235 if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
236 AFI->isThumb2Function())
237 return false;
238
239 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i)
240 if (TID.OpInfo[i].isPredicate())
241 return true;
242
243 return false;
244}
245
Eric Christopher456144e2010-08-19 00:37:05 +0000246// If the machine is predicable go ahead and add the predicate operands, if
247// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000248// TODO: If we want to support thumb1 then we'll need to deal with optional
249// CPSR defs that need to be added before the remaining operands. See s_cc_out
250// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000251const MachineInstrBuilder &
252ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
253 MachineInstr *MI = &*MIB;
254
Eric Christopheraf3dce52011-03-12 01:09:29 +0000255 // Do we use a predicate? or...
256 // Are we NEON in ARM mode and have a predicate operand? If so, I know
257 // we're not predicable but add it anyways.
258 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000259 AddDefaultPred(MIB);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000260
Eric Christopher456144e2010-08-19 00:37:05 +0000261 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
262 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000263 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000264 if (DefinesOptionalPredicate(MI, &CPSR)) {
265 if (CPSR)
266 AddDefaultT1CC(MIB);
267 else
268 AddDefaultCC(MIB);
269 }
270 return MIB;
271}
272
Eric Christopher0fe7d542010-08-17 01:25:29 +0000273unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
274 const TargetRegisterClass* RC) {
275 unsigned ResultReg = createResultReg(RC);
276 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
277
Eric Christopher456144e2010-08-19 00:37:05 +0000278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000279 return ResultReg;
280}
281
282unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
283 const TargetRegisterClass *RC,
284 unsigned Op0, bool Op0IsKill) {
285 unsigned ResultReg = createResultReg(RC);
286 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
287
288 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290 .addReg(Op0, Op0IsKill * RegState::Kill));
291 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295 TII.get(TargetOpcode::COPY), ResultReg)
296 .addReg(II.ImplicitDefs[0]));
297 }
298 return ResultReg;
299}
300
301unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
302 const TargetRegisterClass *RC,
303 unsigned Op0, bool Op0IsKill,
304 unsigned Op1, bool Op1IsKill) {
305 unsigned ResultReg = createResultReg(RC);
306 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
307
308 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 .addReg(Op0, Op0IsKill * RegState::Kill)
311 .addReg(Op1, Op1IsKill * RegState::Kill));
312 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000314 .addReg(Op0, Op0IsKill * RegState::Kill)
315 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000317 TII.get(TargetOpcode::COPY), ResultReg)
318 .addReg(II.ImplicitDefs[0]));
319 }
320 return ResultReg;
321}
322
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000323unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
324 const TargetRegisterClass *RC,
325 unsigned Op0, bool Op0IsKill,
326 unsigned Op1, bool Op1IsKill,
327 unsigned Op2, bool Op2IsKill) {
328 unsigned ResultReg = createResultReg(RC);
329 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
330
331 if (II.getNumDefs() >= 1)
332 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
333 .addReg(Op0, Op0IsKill * RegState::Kill)
334 .addReg(Op1, Op1IsKill * RegState::Kill)
335 .addReg(Op2, Op2IsKill * RegState::Kill));
336 else {
337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
338 .addReg(Op0, Op0IsKill * RegState::Kill)
339 .addReg(Op1, Op1IsKill * RegState::Kill)
340 .addReg(Op2, Op2IsKill * RegState::Kill));
341 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
342 TII.get(TargetOpcode::COPY), ResultReg)
343 .addReg(II.ImplicitDefs[0]));
344 }
345 return ResultReg;
346}
347
Eric Christopher0fe7d542010-08-17 01:25:29 +0000348unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
349 const TargetRegisterClass *RC,
350 unsigned Op0, bool Op0IsKill,
351 uint64_t Imm) {
352 unsigned ResultReg = createResultReg(RC);
353 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
354
355 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000357 .addReg(Op0, Op0IsKill * RegState::Kill)
358 .addImm(Imm));
359 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000360 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000361 .addReg(Op0, Op0IsKill * RegState::Kill)
362 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000363 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000364 TII.get(TargetOpcode::COPY), ResultReg)
365 .addReg(II.ImplicitDefs[0]));
366 }
367 return ResultReg;
368}
369
370unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
371 const TargetRegisterClass *RC,
372 unsigned Op0, bool Op0IsKill,
373 const ConstantFP *FPImm) {
374 unsigned ResultReg = createResultReg(RC);
375 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
376
377 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000378 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000379 .addReg(Op0, Op0IsKill * RegState::Kill)
380 .addFPImm(FPImm));
381 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000383 .addReg(Op0, Op0IsKill * RegState::Kill)
384 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000385 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000386 TII.get(TargetOpcode::COPY), ResultReg)
387 .addReg(II.ImplicitDefs[0]));
388 }
389 return ResultReg;
390}
391
392unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
393 const TargetRegisterClass *RC,
394 unsigned Op0, bool Op0IsKill,
395 unsigned Op1, bool Op1IsKill,
396 uint64_t Imm) {
397 unsigned ResultReg = createResultReg(RC);
398 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
399
400 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000401 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000402 .addReg(Op0, Op0IsKill * RegState::Kill)
403 .addReg(Op1, Op1IsKill * RegState::Kill)
404 .addImm(Imm));
405 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000406 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000407 .addReg(Op0, Op0IsKill * RegState::Kill)
408 .addReg(Op1, Op1IsKill * RegState::Kill)
409 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000410 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000411 TII.get(TargetOpcode::COPY), ResultReg)
412 .addReg(II.ImplicitDefs[0]));
413 }
414 return ResultReg;
415}
416
417unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
418 const TargetRegisterClass *RC,
419 uint64_t Imm) {
420 unsigned ResultReg = createResultReg(RC);
421 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000422
Eric Christopher0fe7d542010-08-17 01:25:29 +0000423 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000425 .addImm(Imm));
426 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000428 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000429 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000430 TII.get(TargetOpcode::COPY), ResultReg)
431 .addReg(II.ImplicitDefs[0]));
432 }
433 return ResultReg;
434}
435
436unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
437 unsigned Op0, bool Op0IsKill,
438 uint32_t Idx) {
439 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
440 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
441 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000443 DL, TII.get(TargetOpcode::COPY), ResultReg)
444 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
445 return ResultReg;
446}
447
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000448// TODO: Don't worry about 64-bit now, but when this is fixed remove the
449// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000450unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000451 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000452
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000453 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
454 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
455 TII.get(ARM::VMOVRS), MoveReg)
456 .addReg(SrcReg));
457 return MoveReg;
458}
459
460unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000461 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000462
Eric Christopheraa3ace12010-09-09 20:49:25 +0000463 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000465 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000466 .addReg(SrcReg));
467 return MoveReg;
468}
469
Eric Christopher9ed58df2010-09-09 00:19:41 +0000470// For double width floating point we need to materialize two constants
471// (the high and the low) into integer registers then use a move to get
472// the combined constant into an FP reg.
473unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
474 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000475 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000476
Eric Christopher9ed58df2010-09-09 00:19:41 +0000477 // This checks to see if we can use VFP3 instructions to materialize
478 // a constant, otherwise we have to go through the constant pool.
479 if (TLI.isFPImmLegal(Val, VT)) {
480 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
481 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
482 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
483 DestReg)
484 .addFPImm(CFP));
485 return DestReg;
486 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000487
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000488 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000489 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000490
Eric Christopher238bb162010-09-09 23:50:00 +0000491 // MachineConstantPool wants an explicit alignment.
492 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
493 if (Align == 0) {
494 // TODO: Figure out if this is correct.
495 Align = TD.getTypeAllocSize(CFP->getType());
496 }
497 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
498 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
499 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000500
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000501 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000502 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
503 DestReg)
504 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000505 .addReg(0));
506 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000507}
508
Eric Christopher744c7c82010-09-28 22:47:54 +0000509unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000510
Eric Christopher744c7c82010-09-28 22:47:54 +0000511 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000512 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000513
Eric Christophere5b13cf2010-11-03 20:21:17 +0000514 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
515
516 // If we can do this in a single instruction without a constant pool entry
517 // do so now.
518 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000519 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000520 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
521 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000522 TII.get(Opc), DestReg)
523 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000524 return DestReg;
525 }
526
Eric Christopher56d2b722010-09-02 23:43:26 +0000527 // MachineConstantPool wants an explicit alignment.
528 unsigned Align = TD.getPrefTypeAlignment(C->getType());
529 if (Align == 0) {
530 // TODO: Figure out if this is correct.
531 Align = TD.getTypeAllocSize(C->getType());
532 }
533 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000534
Eric Christopher56d2b722010-09-02 23:43:26 +0000535 if (isThumb)
536 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000537 TII.get(ARM::t2LDRpci), DestReg)
538 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000539 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000540 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000541 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000542 TII.get(ARM::LDRcp), DestReg)
543 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000544 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000545
Eric Christopher56d2b722010-09-02 23:43:26 +0000546 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000547}
548
Eric Christopherc9932f62010-10-01 23:24:42 +0000549unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000550 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000551 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000552
Eric Christopher890dbbe2010-10-02 00:32:44 +0000553 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000554
Eric Christopher890dbbe2010-10-02 00:32:44 +0000555 // TODO: No external globals for now.
556 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000557
Eric Christopher890dbbe2010-10-02 00:32:44 +0000558 // TODO: Need more magic for ARM PIC.
559 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000560
Eric Christopher890dbbe2010-10-02 00:32:44 +0000561 // MachineConstantPool wants an explicit alignment.
562 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
563 if (Align == 0) {
564 // TODO: Figure out if this is correct.
565 Align = TD.getTypeAllocSize(GV->getType());
566 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000567
Eric Christopher890dbbe2010-10-02 00:32:44 +0000568 // Grab index.
569 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000570 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000571 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
572 ARMCP::CPValue, PCAdj);
573 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000574
Eric Christopher890dbbe2010-10-02 00:32:44 +0000575 // Load value.
576 MachineInstrBuilder MIB;
577 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
578 if (isThumb) {
579 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
580 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
581 .addConstantPoolIndex(Idx);
582 if (RelocM == Reloc::PIC_)
583 MIB.addImm(Id);
584 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000585 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000586 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
587 DestReg)
588 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000589 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000590 }
591 AddOptionalDefs(MIB);
592 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000593}
594
Eric Christopher9ed58df2010-09-09 00:19:41 +0000595unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
596 EVT VT = TLI.getValueType(C->getType(), true);
597
598 // Only handle simple types.
599 if (!VT.isSimple()) return 0;
600
601 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
602 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000603 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
604 return ARMMaterializeGV(GV, VT);
605 else if (isa<ConstantInt>(C))
606 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000607
Eric Christopherc9932f62010-10-01 23:24:42 +0000608 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000609}
610
Eric Christopherf9764fa2010-09-30 20:49:44 +0000611unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
612 // Don't handle dynamic allocas.
613 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000614
Duncan Sands1440e8b2010-11-03 11:35:31 +0000615 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000616 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000617
Eric Christopherf9764fa2010-09-30 20:49:44 +0000618 DenseMap<const AllocaInst*, int>::iterator SI =
619 FuncInfo.StaticAllocaMap.find(AI);
620
621 // This will get lowered later into the correct offsets and registers
622 // via rewriteXFrameIndex.
623 if (SI != FuncInfo.StaticAllocaMap.end()) {
624 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
625 unsigned ResultReg = createResultReg(RC);
626 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
627 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
628 TII.get(Opc), ResultReg)
629 .addFrameIndex(SI->second)
630 .addImm(0));
631 return ResultReg;
632 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000633
Eric Christopherf9764fa2010-09-30 20:49:44 +0000634 return 0;
635}
636
Duncan Sands1440e8b2010-11-03 11:35:31 +0000637bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
638 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000639
Eric Christopherb1cc8482010-08-25 07:23:49 +0000640 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000641 if (evt == MVT::Other || !evt.isSimple()) return false;
642 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000643
Eric Christopherdc908042010-08-31 01:28:42 +0000644 // Handle all legal types, i.e. a register that will directly hold this
645 // value.
646 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000647}
648
Duncan Sands1440e8b2010-11-03 11:35:31 +0000649bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000650 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000651
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000652 // If this is a type than can be sign or zero-extended to a basic operation
653 // go ahead and accept it now.
654 if (VT == MVT::i8 || VT == MVT::i16)
655 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000656
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000657 return false;
658}
659
Eric Christopher88de86b2010-11-19 22:36:41 +0000660// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000661bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000662 // Some boilerplate from the X86 FastISel.
663 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000664 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000665 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000666 // Don't walk into other basic blocks unless the object is an alloca from
667 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000668 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
669 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
670 Opcode = I->getOpcode();
671 U = I;
672 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000673 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000674 Opcode = C->getOpcode();
675 U = C;
676 }
677
Eric Christophercb0b04b2010-08-24 00:07:24 +0000678 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000679 if (Ty->getAddressSpace() > 255)
680 // Fast instruction selection doesn't support the special
681 // address spaces.
682 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000683
Eric Christopher83007122010-08-23 21:44:12 +0000684 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000685 default:
Eric Christopher83007122010-08-23 21:44:12 +0000686 break;
Eric Christopher55324332010-10-12 00:43:21 +0000687 case Instruction::BitCast: {
688 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000689 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000690 }
691 case Instruction::IntToPtr: {
692 // Look past no-op inttoptrs.
693 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000694 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000695 break;
696 }
697 case Instruction::PtrToInt: {
698 // Look past no-op ptrtoints.
699 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000700 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000701 break;
702 }
Eric Christophereae84392010-10-14 09:29:41 +0000703 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000704 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000705 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000706
Eric Christophereae84392010-10-14 09:29:41 +0000707 // Iterate through the GEP folding the constants into offsets where
708 // we can.
709 gep_type_iterator GTI = gep_type_begin(U);
710 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
711 i != e; ++i, ++GTI) {
712 const Value *Op = *i;
713 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
714 const StructLayout *SL = TD.getStructLayout(STy);
715 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
716 TmpOffset += SL->getElementOffset(Idx);
717 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000718 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000719 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000720 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
721 // Constant-offset addressing.
722 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000723 break;
724 }
725 if (isa<AddOperator>(Op) &&
726 (!isa<Instruction>(Op) ||
727 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
728 == FuncInfo.MBB) &&
729 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
730 // An add (in the same block) with a constant operand. Fold the
731 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000732 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000733 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000734 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000735 // Iterate on the other operand.
736 Op = cast<AddOperator>(Op)->getOperand(0);
737 continue;
738 }
739 // Unsupported
740 goto unsupported_gep;
741 }
Eric Christophereae84392010-10-14 09:29:41 +0000742 }
743 }
Eric Christopher2896df82010-10-15 18:02:07 +0000744
745 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000746 Addr.Offset = TmpOffset;
747 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000748
749 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000750 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000751
Eric Christophereae84392010-10-14 09:29:41 +0000752 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000753 break;
754 }
Eric Christopher83007122010-08-23 21:44:12 +0000755 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000756 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000757 DenseMap<const AllocaInst*, int>::iterator SI =
758 FuncInfo.StaticAllocaMap.find(AI);
759 if (SI != FuncInfo.StaticAllocaMap.end()) {
760 Addr.BaseType = Address::FrameIndexBase;
761 Addr.Base.FI = SI->second;
762 return true;
763 }
764 break;
Eric Christopher83007122010-08-23 21:44:12 +0000765 }
766 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000767
Eric Christophera9c57512010-10-13 21:41:51 +0000768 // Materialize the global variable's address into a reg which can
769 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000770 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000771 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
772 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000773
Eric Christopher0d581222010-11-19 22:30:02 +0000774 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000775 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000776 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000777
Eric Christophercb0b04b2010-08-24 00:07:24 +0000778 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000779 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
780 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000781}
782
Eric Christopher0d581222010-11-19 22:30:02 +0000783void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000784
Eric Christopher212ae932010-10-21 19:40:30 +0000785 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000786
Eric Christopher212ae932010-10-21 19:40:30 +0000787 bool needsLowering = false;
788 switch (VT.getSimpleVT().SimpleTy) {
789 default:
790 assert(false && "Unhandled load/store type!");
791 case MVT::i1:
792 case MVT::i8:
793 case MVT::i16:
794 case MVT::i32:
795 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000796 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000797 break;
798 case MVT::f32:
799 case MVT::f64:
800 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000801 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000802 break;
803 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000804
Eric Christopher827656d2010-11-20 22:38:27 +0000805 // If this is a stack pointer and the offset needs to be simplified then
806 // put the alloca address into a register, set the base type back to
807 // register and continue. This should almost never happen.
808 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
809 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
810 ARM::GPRRegisterClass;
811 unsigned ResultReg = createResultReg(RC);
812 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
813 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
814 TII.get(Opc), ResultReg)
815 .addFrameIndex(Addr.Base.FI)
816 .addImm(0));
817 Addr.Base.Reg = ResultReg;
818 Addr.BaseType = Address::RegBase;
819 }
820
Eric Christopher212ae932010-10-21 19:40:30 +0000821 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000822 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000823 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000824 ARMCC::CondCodes Pred = ARMCC::AL;
825 unsigned PredReg = 0;
826
Eric Christopher2896df82010-10-15 18:02:07 +0000827 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
828 ARM::GPRRegisterClass;
829 unsigned BaseReg = createResultReg(RC);
830
Eric Christophereaa204b2010-09-02 01:39:14 +0000831 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000832 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000833 BaseReg, Addr.Base.Reg, Addr.Offset,
834 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000835 static_cast<const ARMBaseInstrInfo&>(TII));
836 else {
837 assert(AFI->isThumb2Function());
838 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000839 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000840 static_cast<const ARMBaseInstrInfo&>(TII));
841 }
Eric Christopher0d581222010-11-19 22:30:02 +0000842 Addr.Offset = 0;
843 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000844 }
Eric Christopher83007122010-08-23 21:44:12 +0000845}
846
Eric Christopher564857f2010-12-01 01:40:24 +0000847void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
848 const MachineInstrBuilder &MIB) {
849 // addrmode5 output depends on the selection dag addressing dividing the
850 // offset by 4 that it then later multiplies. Do this here as well.
851 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
852 VT.getSimpleVT().SimpleTy == MVT::f64)
853 Addr.Offset /= 4;
854
855 // Frame base works a bit differently. Handle it separately.
856 if (Addr.BaseType == Address::FrameIndexBase) {
857 int FI = Addr.Base.FI;
858 int Offset = Addr.Offset;
859 MachineMemOperand *MMO =
860 FuncInfo.MF->getMachineMemOperand(
861 MachinePointerInfo::getFixedStack(FI, Offset),
862 MachineMemOperand::MOLoad,
863 MFI.getObjectSize(FI),
864 MFI.getObjectAlignment(FI));
865 // Now add the rest of the operands.
866 MIB.addFrameIndex(FI);
867
868 // ARM halfword load/stores need an additional operand.
869 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
870
871 MIB.addImm(Addr.Offset);
872 MIB.addMemOperand(MMO);
873 } else {
874 // Now add the rest of the operands.
875 MIB.addReg(Addr.Base.Reg);
876
877 // ARM halfword load/stores need an additional operand.
878 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
879
880 MIB.addImm(Addr.Offset);
881 }
882 AddOptionalDefs(MIB);
883}
884
Eric Christopher0d581222010-11-19 22:30:02 +0000885bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000886
Eric Christopherb1cc8482010-08-25 07:23:49 +0000887 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000888 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000889 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000890 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000891 // This is mostly going to be Neon/vector support.
892 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000893 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000894 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000895 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000896 break;
897 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000898 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000899 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000900 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000901 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000902 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000903 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000904 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000905 case MVT::f32:
906 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000907 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000908 break;
909 case MVT::f64:
910 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000911 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000912 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000913 }
Eric Christopher564857f2010-12-01 01:40:24 +0000914 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000915 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000916
Eric Christopher564857f2010-12-01 01:40:24 +0000917 // Create the base instruction, then add the operands.
918 ResultReg = createResultReg(RC);
919 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
920 TII.get(Opc), ResultReg);
921 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000922 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000923}
924
Eric Christopher43b62be2010-09-27 06:02:23 +0000925bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000926 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000927 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000928 if (!isLoadTypeLegal(I->getType(), VT))
929 return false;
930
Eric Christopher564857f2010-12-01 01:40:24 +0000931 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000932 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000933 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000934
935 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000936 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000937 UpdateValueMap(I, ResultReg);
938 return true;
939}
940
Eric Christopher0d581222010-11-19 22:30:02 +0000941bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000942 unsigned StrOpc;
943 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000944 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000945 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000946 case MVT::i1: {
947 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
948 ARM::GPRRegisterClass);
949 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
950 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
951 TII.get(Opc), Res)
952 .addReg(SrcReg).addImm(1));
953 SrcReg = Res;
954 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000955 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000956 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000957 break;
958 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000959 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000960 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000961 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000962 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000963 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000964 case MVT::f32:
965 if (!Subtarget->hasVFP2()) return false;
966 StrOpc = ARM::VSTRS;
967 break;
968 case MVT::f64:
969 if (!Subtarget->hasVFP2()) return false;
970 StrOpc = ARM::VSTRD;
971 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000972 }
Eric Christopher564857f2010-12-01 01:40:24 +0000973 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000974 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000975
Eric Christopher564857f2010-12-01 01:40:24 +0000976 // Create the base instruction, then add the operands.
977 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
978 TII.get(StrOpc))
979 .addReg(SrcReg, getKillRegState(true));
980 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000981 return true;
982}
983
Eric Christopher43b62be2010-09-27 06:02:23 +0000984bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000985 Value *Op0 = I->getOperand(0);
986 unsigned SrcReg = 0;
987
Eric Christopher564857f2010-12-01 01:40:24 +0000988 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000989 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000990 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000991 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000992
Eric Christopher1b61ef42010-09-02 01:48:11 +0000993 // Get the value to be stored into a register.
994 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000995 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000996
Eric Christopher564857f2010-12-01 01:40:24 +0000997 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000998 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000999 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001000 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001001
Eric Christopher0d581222010-11-19 22:30:02 +00001002 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001003 return true;
1004}
1005
1006static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1007 switch (Pred) {
1008 // Needs two compares...
1009 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001010 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001011 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001012 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001013 return ARMCC::AL;
1014 case CmpInst::ICMP_EQ:
1015 case CmpInst::FCMP_OEQ:
1016 return ARMCC::EQ;
1017 case CmpInst::ICMP_SGT:
1018 case CmpInst::FCMP_OGT:
1019 return ARMCC::GT;
1020 case CmpInst::ICMP_SGE:
1021 case CmpInst::FCMP_OGE:
1022 return ARMCC::GE;
1023 case CmpInst::ICMP_UGT:
1024 case CmpInst::FCMP_UGT:
1025 return ARMCC::HI;
1026 case CmpInst::FCMP_OLT:
1027 return ARMCC::MI;
1028 case CmpInst::ICMP_ULE:
1029 case CmpInst::FCMP_OLE:
1030 return ARMCC::LS;
1031 case CmpInst::FCMP_ORD:
1032 return ARMCC::VC;
1033 case CmpInst::FCMP_UNO:
1034 return ARMCC::VS;
1035 case CmpInst::FCMP_UGE:
1036 return ARMCC::PL;
1037 case CmpInst::ICMP_SLT:
1038 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001039 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001040 case CmpInst::ICMP_SLE:
1041 case CmpInst::FCMP_ULE:
1042 return ARMCC::LE;
1043 case CmpInst::FCMP_UNE:
1044 case CmpInst::ICMP_NE:
1045 return ARMCC::NE;
1046 case CmpInst::ICMP_UGE:
1047 return ARMCC::HS;
1048 case CmpInst::ICMP_ULT:
1049 return ARMCC::LO;
1050 }
Eric Christopher543cf052010-09-01 22:16:27 +00001051}
1052
Eric Christopher43b62be2010-09-27 06:02:23 +00001053bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001054 const BranchInst *BI = cast<BranchInst>(I);
1055 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1056 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001057
Eric Christophere5734102010-09-03 00:35:47 +00001058 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001059
Eric Christopher0e6233b2010-10-29 21:08:19 +00001060 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1061 // behavior.
1062 // TODO: Factor this out.
1063 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1064 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001065 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001066 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +00001067 if (!isTypeLegal(Ty, VT))
1068 return false;
1069
Eric Christopher0e6233b2010-10-29 21:08:19 +00001070 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1071 if (isFloat && !Subtarget->hasVFP2())
1072 return false;
1073
1074 unsigned CmpOpc;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001075 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001076 default: return false;
1077 // TODO: Verify compares.
1078 case MVT::f32:
1079 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001080 break;
1081 case MVT::f64:
1082 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001083 break;
1084 case MVT::i32:
1085 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001086 break;
1087 }
1088
1089 // Get the compare predicate.
1090 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1091
1092 // We may not handle every CC for now.
1093 if (ARMPred == ARMCC::AL) return false;
1094
1095 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1096 if (Arg1 == 0) return false;
1097
1098 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1099 if (Arg2 == 0) return false;
1100
1101 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1102 TII.get(CmpOpc))
1103 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001104
Eric Christopher0e6233b2010-10-29 21:08:19 +00001105 // For floating point we need to move the result to a comparison register
1106 // that we can then use for branches.
1107 if (isFloat)
1108 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1109 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001110
Eric Christopher0e6233b2010-10-29 21:08:19 +00001111 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1113 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1114 FastEmitBranch(FBB, DL);
1115 FuncInfo.MBB->addSuccessor(TBB);
1116 return true;
1117 }
1118 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001119
Eric Christopher0e6233b2010-10-29 21:08:19 +00001120 unsigned CmpReg = getRegForValue(BI->getCondition());
1121 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001122
Eric Christopher229207a2010-09-29 01:14:47 +00001123 // Re-set the flags just in case.
1124 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1125 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001126 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001127
Eric Christophere5734102010-09-03 00:35:47 +00001128 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001129 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001130 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001131 FastEmitBranch(FBB, DL);
1132 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001133 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001134}
1135
Eric Christopher43b62be2010-09-27 06:02:23 +00001136bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001137 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001138
Duncan Sands1440e8b2010-11-03 11:35:31 +00001139 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001140 const Type *Ty = CI->getOperand(0)->getType();
1141 if (!isTypeLegal(Ty, VT))
1142 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001143
Eric Christopherd43393a2010-09-08 23:13:45 +00001144 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1145 if (isFloat && !Subtarget->hasVFP2())
1146 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001147
Eric Christopherd43393a2010-09-08 23:13:45 +00001148 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001149 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001150 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001151 default: return false;
1152 // TODO: Verify compares.
1153 case MVT::f32:
1154 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001155 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001156 break;
1157 case MVT::f64:
1158 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001159 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001160 break;
1161 case MVT::i32:
1162 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001163 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001164 break;
1165 }
1166
Eric Christopher229207a2010-09-29 01:14:47 +00001167 // Get the compare predicate.
1168 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001169
Eric Christopher229207a2010-09-29 01:14:47 +00001170 // We may not handle every CC for now.
1171 if (ARMPred == ARMCC::AL) return false;
1172
Eric Christopherd43393a2010-09-08 23:13:45 +00001173 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1174 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001175
Eric Christopherd43393a2010-09-08 23:13:45 +00001176 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1177 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001178
Eric Christopherd43393a2010-09-08 23:13:45 +00001179 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1180 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001181
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001182 // For floating point we need to move the result to a comparison register
1183 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001184 if (isFloat)
1185 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1186 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001187
Eric Christopher229207a2010-09-29 01:14:47 +00001188 // Now set a register based on the comparison. Explicitly set the predicates
1189 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001190 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001191 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001192 : ARM::GPRRegisterClass;
1193 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001194 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001195 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001196 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1197 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1198 .addReg(ZeroReg).addImm(1)
1199 .addImm(ARMPred).addReg(CondReg);
1200
Eric Christophera5b1e682010-09-17 22:28:18 +00001201 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001202 return true;
1203}
1204
Eric Christopher43b62be2010-09-27 06:02:23 +00001205bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001206 // Make sure we have VFP and that we're extending float to double.
1207 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001208
Eric Christopher46203602010-09-09 00:26:48 +00001209 Value *V = I->getOperand(0);
1210 if (!I->getType()->isDoubleTy() ||
1211 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001212
Eric Christopher46203602010-09-09 00:26:48 +00001213 unsigned Op = getRegForValue(V);
1214 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001215
Eric Christopher46203602010-09-09 00:26:48 +00001216 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001217 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001218 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001219 .addReg(Op));
1220 UpdateValueMap(I, Result);
1221 return true;
1222}
1223
Eric Christopher43b62be2010-09-27 06:02:23 +00001224bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001225 // Make sure we have VFP and that we're truncating double to float.
1226 if (!Subtarget->hasVFP2()) return false;
1227
1228 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001229 if (!(I->getType()->isFloatTy() &&
1230 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001231
1232 unsigned Op = getRegForValue(V);
1233 if (Op == 0) return false;
1234
1235 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001237 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001238 .addReg(Op));
1239 UpdateValueMap(I, Result);
1240 return true;
1241}
1242
Eric Christopher43b62be2010-09-27 06:02:23 +00001243bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001244 // Make sure we have VFP.
1245 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001246
Duncan Sands1440e8b2010-11-03 11:35:31 +00001247 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001248 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001249 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001250 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001251
Eric Christopher9a040492010-09-09 18:54:59 +00001252 unsigned Op = getRegForValue(I->getOperand(0));
1253 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001254
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001255 // The conversion routine works on fp-reg to fp-reg and the operand above
1256 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001257 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001258 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001259
Eric Christopher9a040492010-09-09 18:54:59 +00001260 unsigned Opc;
1261 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1262 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1263 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001264
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001265 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1267 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001268 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001269 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001270 return true;
1271}
1272
Eric Christopher43b62be2010-09-27 06:02:23 +00001273bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001274 // Make sure we have VFP.
1275 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001276
Duncan Sands1440e8b2010-11-03 11:35:31 +00001277 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001278 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001279 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001280 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001281
Eric Christopher9a040492010-09-09 18:54:59 +00001282 unsigned Op = getRegForValue(I->getOperand(0));
1283 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001284
Eric Christopher9a040492010-09-09 18:54:59 +00001285 unsigned Opc;
1286 const Type *OpTy = I->getOperand(0)->getType();
1287 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1288 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1289 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001290
Eric Christopher022b7fb2010-10-05 23:13:24 +00001291 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1292 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1294 ResultReg)
1295 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001296
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001297 // This result needs to be in an integer register, but the conversion only
1298 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001299 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001300 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001301
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001302 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001303 return true;
1304}
1305
Eric Christopher3bbd3962010-10-11 08:27:59 +00001306bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001307 MVT VT;
1308 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001309 return false;
1310
1311 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001312 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001313 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1314
1315 unsigned CondReg = getRegForValue(I->getOperand(0));
1316 if (CondReg == 0) return false;
1317 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1318 if (Op1Reg == 0) return false;
1319 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1320 if (Op2Reg == 0) return false;
1321
1322 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1324 .addReg(CondReg).addImm(1));
1325 unsigned ResultReg = createResultReg(RC);
1326 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1328 .addReg(Op1Reg).addReg(Op2Reg)
1329 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1330 UpdateValueMap(I, ResultReg);
1331 return true;
1332}
1333
Eric Christopher08637852010-09-30 22:34:19 +00001334bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001335 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001336 const Type *Ty = I->getType();
1337 if (!isTypeLegal(Ty, VT))
1338 return false;
1339
1340 // If we have integer div support we should have selected this automagically.
1341 // In case we have a real miss go ahead and return false and we'll pick
1342 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001343 if (Subtarget->hasDivide()) return false;
1344
Eric Christopher08637852010-09-30 22:34:19 +00001345 // Otherwise emit a libcall.
1346 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001347 if (VT == MVT::i8)
1348 LC = RTLIB::SDIV_I8;
1349 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001350 LC = RTLIB::SDIV_I16;
1351 else if (VT == MVT::i32)
1352 LC = RTLIB::SDIV_I32;
1353 else if (VT == MVT::i64)
1354 LC = RTLIB::SDIV_I64;
1355 else if (VT == MVT::i128)
1356 LC = RTLIB::SDIV_I128;
1357 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001358
Eric Christopher08637852010-09-30 22:34:19 +00001359 return ARMEmitLibcall(I, LC);
1360}
1361
Eric Christopher6a880d62010-10-11 08:37:26 +00001362bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001363 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001364 const Type *Ty = I->getType();
1365 if (!isTypeLegal(Ty, VT))
1366 return false;
1367
1368 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1369 if (VT == MVT::i8)
1370 LC = RTLIB::SREM_I8;
1371 else if (VT == MVT::i16)
1372 LC = RTLIB::SREM_I16;
1373 else if (VT == MVT::i32)
1374 LC = RTLIB::SREM_I32;
1375 else if (VT == MVT::i64)
1376 LC = RTLIB::SREM_I64;
1377 else if (VT == MVT::i128)
1378 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001379 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001380
Eric Christopher6a880d62010-10-11 08:37:26 +00001381 return ARMEmitLibcall(I, LC);
1382}
1383
Eric Christopher43b62be2010-09-27 06:02:23 +00001384bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001385 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001386
Eric Christopherbc39b822010-09-09 00:53:57 +00001387 // We can get here in the case when we want to use NEON for our fp
1388 // operations, but can't figure out how to. Just use the vfp instructions
1389 // if we have them.
1390 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001391 const Type *Ty = I->getType();
1392 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1393 if (isFloat && !Subtarget->hasVFP2())
1394 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001395
Eric Christopherbc39b822010-09-09 00:53:57 +00001396 unsigned Op1 = getRegForValue(I->getOperand(0));
1397 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001398
Eric Christopherbc39b822010-09-09 00:53:57 +00001399 unsigned Op2 = getRegForValue(I->getOperand(1));
1400 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001401
Eric Christopherbc39b822010-09-09 00:53:57 +00001402 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001403 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001404 switch (ISDOpcode) {
1405 default: return false;
1406 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001407 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001408 break;
1409 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001410 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001411 break;
1412 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001413 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001414 break;
1415 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001416 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001417 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1418 TII.get(Opc), ResultReg)
1419 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001420 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001421 return true;
1422}
1423
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001424// Call Handling Code
1425
Eric Christopherfa87d662010-10-18 02:17:53 +00001426bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1427 EVT SrcVT, unsigned &ResultReg) {
1428 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1429 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001430
Eric Christopherfa87d662010-10-18 02:17:53 +00001431 if (RR != 0) {
1432 ResultReg = RR;
1433 return true;
1434 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001435 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001436}
1437
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001438// This is largely taken directly from CCAssignFnForNode - we don't support
1439// varargs in FastISel so that part has been removed.
1440// TODO: We may not support all of this.
1441CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1442 switch (CC) {
1443 default:
1444 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001445 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001446 // Ignore fastcc. Silence compiler warnings.
1447 (void)RetFastCC_ARM_APCS;
1448 (void)FastCC_ARM_APCS;
1449 // Fallthrough
1450 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001451 // Use target triple & subtarget features to do actual dispatch.
1452 if (Subtarget->isAAPCS_ABI()) {
1453 if (Subtarget->hasVFP2() &&
1454 FloatABIType == FloatABI::Hard)
1455 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1456 else
1457 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1458 } else
1459 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1460 case CallingConv::ARM_AAPCS_VFP:
1461 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1462 case CallingConv::ARM_AAPCS:
1463 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1464 case CallingConv::ARM_APCS:
1465 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1466 }
1467}
1468
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001469bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1470 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001471 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001472 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1473 SmallVectorImpl<unsigned> &RegArgs,
1474 CallingConv::ID CC,
1475 unsigned &NumBytes) {
1476 SmallVector<CCValAssign, 16> ArgLocs;
1477 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1478 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1479
1480 // Get a count of how many bytes are to be pushed on the stack.
1481 NumBytes = CCInfo.getNextStackOffset();
1482
1483 // Issue CALLSEQ_START
1484 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001485 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1486 TII.get(AdjStackDown))
1487 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001488
1489 // Process the args.
1490 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1491 CCValAssign &VA = ArgLocs[i];
1492 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001493 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001494
Eric Christopher4a2b3162011-01-27 05:44:56 +00001495 // We don't handle NEON/vector parameters yet.
1496 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001497 return false;
1498
Eric Christopherf9764fa2010-09-30 20:49:44 +00001499 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001500 switch (VA.getLocInfo()) {
1501 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001502 case CCValAssign::SExt: {
1503 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1504 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001505 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001506 Emitted = true;
1507 ArgVT = VA.getLocVT();
1508 break;
1509 }
1510 case CCValAssign::ZExt: {
1511 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1512 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001513 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001514 Emitted = true;
1515 ArgVT = VA.getLocVT();
1516 break;
1517 }
1518 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001519 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1520 Arg, ArgVT, Arg);
1521 if (!Emitted)
1522 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1523 Arg, ArgVT, Arg);
1524 if (!Emitted)
1525 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1526 Arg, ArgVT, Arg);
1527
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001528 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001529 ArgVT = VA.getLocVT();
1530 break;
1531 }
1532 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001533 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001534 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001535 assert(BC != 0 && "Failed to emit a bitcast!");
1536 Arg = BC;
1537 ArgVT = VA.getLocVT();
1538 break;
1539 }
1540 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001541 }
1542
1543 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001544 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001546 VA.getLocReg())
1547 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001548 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001549 } else if (VA.needsCustom()) {
1550 // TODO: We need custom lowering for vector (v2f64) args.
1551 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001552
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001553 CCValAssign &NextVA = ArgLocs[++i];
1554
1555 // TODO: Only handle register args for now.
1556 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1557
1558 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1559 TII.get(ARM::VMOVRRD), VA.getLocReg())
1560 .addReg(NextVA.getLocReg(), RegState::Define)
1561 .addReg(Arg));
1562 RegArgs.push_back(VA.getLocReg());
1563 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001564 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001565 assert(VA.isMemLoc());
1566 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001567 Address Addr;
1568 Addr.BaseType = Address::RegBase;
1569 Addr.Base.Reg = ARM::SP;
1570 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001571
Eric Christopher0d581222010-11-19 22:30:02 +00001572 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001573 }
1574 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001575 return true;
1576}
1577
Duncan Sands1440e8b2010-11-03 11:35:31 +00001578bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001579 const Instruction *I, CallingConv::ID CC,
1580 unsigned &NumBytes) {
1581 // Issue CALLSEQ_END
1582 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001583 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1584 TII.get(AdjStackUp))
1585 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001586
1587 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001588 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001589 SmallVector<CCValAssign, 16> RVLocs;
1590 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1591 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1592
1593 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001594 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001595 // For this move we copy into two registers and then move into the
1596 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001597 EVT DestVT = RVLocs[0].getValVT();
1598 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1599 unsigned ResultReg = createResultReg(DstRC);
1600 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1601 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001602 .addReg(RVLocs[0].getLocReg())
1603 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001604
Eric Christopher3659ac22010-10-20 08:02:24 +00001605 UsedRegs.push_back(RVLocs[0].getLocReg());
1606 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001607
Eric Christopherdccd2c32010-10-11 08:38:55 +00001608 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001609 UpdateValueMap(I, ResultReg);
1610 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001611 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001612 EVT CopyVT = RVLocs[0].getValVT();
1613 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001614
Eric Christopher14df8822010-10-01 00:00:11 +00001615 unsigned ResultReg = createResultReg(DstRC);
1616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1617 ResultReg).addReg(RVLocs[0].getLocReg());
1618 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001619
Eric Christopherdccd2c32010-10-11 08:38:55 +00001620 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001621 UpdateValueMap(I, ResultReg);
1622 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001623 }
1624
Eric Christopherdccd2c32010-10-11 08:38:55 +00001625 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001626}
1627
Eric Christopher4f512ef2010-10-22 01:28:00 +00001628bool ARMFastISel::SelectRet(const Instruction *I) {
1629 const ReturnInst *Ret = cast<ReturnInst>(I);
1630 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001631
Eric Christopher4f512ef2010-10-22 01:28:00 +00001632 if (!FuncInfo.CanLowerReturn)
1633 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001634
Eric Christopher4f512ef2010-10-22 01:28:00 +00001635 if (F.isVarArg())
1636 return false;
1637
1638 CallingConv::ID CC = F.getCallingConv();
1639 if (Ret->getNumOperands() > 0) {
1640 SmallVector<ISD::OutputArg, 4> Outs;
1641 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1642 Outs, TLI);
1643
1644 // Analyze operands of the call, assigning locations to each operand.
1645 SmallVector<CCValAssign, 16> ValLocs;
1646 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1647 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1648
1649 const Value *RV = Ret->getOperand(0);
1650 unsigned Reg = getRegForValue(RV);
1651 if (Reg == 0)
1652 return false;
1653
1654 // Only handle a single return value for now.
1655 if (ValLocs.size() != 1)
1656 return false;
1657
1658 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001659
Eric Christopher4f512ef2010-10-22 01:28:00 +00001660 // Don't bother handling odd stuff for now.
1661 if (VA.getLocInfo() != CCValAssign::Full)
1662 return false;
1663 // Only handle register returns for now.
1664 if (!VA.isRegLoc())
1665 return false;
1666 // TODO: For now, don't try to handle cases where getLocInfo()
1667 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001668 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001669 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001670
Eric Christopher4f512ef2010-10-22 01:28:00 +00001671 // Make the copy.
1672 unsigned SrcReg = Reg + VA.getValNo();
1673 unsigned DstReg = VA.getLocReg();
1674 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1675 // Avoid a cross-class copy. This is very unlikely.
1676 if (!SrcRC->contains(DstReg))
1677 return false;
1678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1679 DstReg).addReg(SrcReg);
1680
1681 // Mark the register as live out of the function.
1682 MRI.addLiveOut(VA.getLocReg());
1683 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001684
Eric Christopher4f512ef2010-10-22 01:28:00 +00001685 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1686 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1687 TII.get(RetOpc)));
1688 return true;
1689}
1690
Eric Christopher872f4a22011-02-22 01:37:10 +00001691unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1692
1693 // Depend our opcode for thumb on whether or not we're targeting an
1694 // externally callable function. For libcalls we'll just pass a NULL GV
1695 // in here.
1696 bool isExternal = false;
1697 if (!GV || GV->hasExternalLinkage()) isExternal = true;
1698
1699 // Darwin needs the r9 versions of the opcodes.
1700 bool isDarwin = Subtarget->isTargetDarwin();
1701 if (isThumb && isExternal) {
1702 return isDarwin ? ARM::tBLXi_r9 : ARM::tBLXi;
1703 } else if (isThumb) {
1704 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1705 } else {
1706 return isDarwin ? ARM::BLr9 : ARM::BL;
1707 }
1708}
1709
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001710// A quick function that will emit a call for a named libcall in F with the
1711// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001712// can emit a call for any libcall we can produce. This is an abridged version
1713// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001714// like computed function pointers or strange arguments at call sites.
1715// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1716// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001717bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1718 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001719
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001720 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001721 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001722 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001723 if (RetTy->isVoidTy())
1724 RetVT = MVT::isVoid;
1725 else if (!isTypeLegal(RetTy, RetVT))
1726 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001727
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001728 // For now we're using BLX etc on the assumption that we have v5t ops.
1729 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001730
Eric Christopher836c6242010-12-15 23:47:29 +00001731 // TODO: For now if we have long calls specified we don't handle the call.
1732 if (EnableARMLongCalls) return false;
1733
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001734 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001735 SmallVector<Value*, 8> Args;
1736 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001737 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001738 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1739 Args.reserve(I->getNumOperands());
1740 ArgRegs.reserve(I->getNumOperands());
1741 ArgVTs.reserve(I->getNumOperands());
1742 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001743 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001744 Value *Op = I->getOperand(i);
1745 unsigned Arg = getRegForValue(Op);
1746 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001747
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001748 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001749 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001750 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001751
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001752 ISD::ArgFlagsTy Flags;
1753 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1754 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001755
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001756 Args.push_back(Op);
1757 ArgRegs.push_back(Arg);
1758 ArgVTs.push_back(ArgVT);
1759 ArgFlags.push_back(Flags);
1760 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001761
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001762 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001763 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001764 unsigned NumBytes;
1765 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1766 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001767
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001768 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001769 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001770 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001771 unsigned CallOpc = ARMSelectCallOp(NULL);
1772 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001773 // Explicitly adding the predicate here.
1774 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1775 TII.get(CallOpc)))
1776 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001777 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001778 // Explicitly adding the predicate here.
1779 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1780 TII.get(CallOpc))
1781 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001782
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001783 // Add implicit physical register uses to the call.
1784 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1785 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001786
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001787 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001788 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001789 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001790
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001791 // Set all unused physreg defs as dead.
1792 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001793
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001794 return true;
1795}
1796
Eric Christopherf9764fa2010-09-30 20:49:44 +00001797bool ARMFastISel::SelectCall(const Instruction *I) {
1798 const CallInst *CI = cast<CallInst>(I);
1799 const Value *Callee = CI->getCalledValue();
1800
1801 // Can't handle inline asm or worry about intrinsics yet.
1802 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1803
Eric Christophere6ca6772010-10-01 21:33:12 +00001804 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001805 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001806 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1807 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001808
Eric Christopherf9764fa2010-09-30 20:49:44 +00001809 // Check the calling convention.
1810 ImmutableCallSite CS(CI);
1811 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001812
Eric Christopherf9764fa2010-09-30 20:49:44 +00001813 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001814
Eric Christopherf9764fa2010-09-30 20:49:44 +00001815 // Let SDISel handle vararg functions.
1816 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1817 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1818 if (FTy->isVarArg())
1819 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001820
Eric Christopherf9764fa2010-09-30 20:49:44 +00001821 // Handle *simple* calls for now.
1822 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001823 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001824 if (RetTy->isVoidTy())
1825 RetVT = MVT::isVoid;
1826 else if (!isTypeLegal(RetTy, RetVT))
1827 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001828
Eric Christopherf9764fa2010-09-30 20:49:44 +00001829 // For now we're using BLX etc on the assumption that we have v5t ops.
1830 // TODO: Maybe?
1831 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001832
Eric Christopher836c6242010-12-15 23:47:29 +00001833 // TODO: For now if we have long calls specified we don't handle the call.
1834 if (EnableARMLongCalls) return false;
1835
Eric Christopherf9764fa2010-09-30 20:49:44 +00001836 // Set up the argument vectors.
1837 SmallVector<Value*, 8> Args;
1838 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001839 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001840 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1841 Args.reserve(CS.arg_size());
1842 ArgRegs.reserve(CS.arg_size());
1843 ArgVTs.reserve(CS.arg_size());
1844 ArgFlags.reserve(CS.arg_size());
1845 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1846 i != e; ++i) {
1847 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001848
Eric Christopherf9764fa2010-09-30 20:49:44 +00001849 if (Arg == 0)
1850 return false;
1851 ISD::ArgFlagsTy Flags;
1852 unsigned AttrInd = i - CS.arg_begin() + 1;
1853 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1854 Flags.setSExt();
1855 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1856 Flags.setZExt();
1857
1858 // FIXME: Only handle *easy* calls for now.
1859 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1860 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1861 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1862 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1863 return false;
1864
1865 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001866 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001867 if (!isTypeLegal(ArgTy, ArgVT))
1868 return false;
1869 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1870 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001871
Eric Christopherf9764fa2010-09-30 20:49:44 +00001872 Args.push_back(*i);
1873 ArgRegs.push_back(Arg);
1874 ArgVTs.push_back(ArgVT);
1875 ArgFlags.push_back(Flags);
1876 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001877
Eric Christopherf9764fa2010-09-30 20:49:44 +00001878 // Handle the arguments now that we've gotten them.
1879 SmallVector<unsigned, 4> RegArgs;
1880 unsigned NumBytes;
1881 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1882 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001883
Eric Christopherf9764fa2010-09-30 20:49:44 +00001884 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001885 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001886 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001887 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001888 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001889 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001890 // Explicitly adding the predicate here.
1891 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1892 TII.get(CallOpc)))
1893 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001894 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001895 // Explicitly adding the predicate here.
1896 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1897 TII.get(CallOpc))
1898 .addGlobalAddress(GV, 0, 0));
Eric Christopherc19aadb2010-12-21 03:50:43 +00001899
Eric Christopherf9764fa2010-09-30 20:49:44 +00001900 // Add implicit physical register uses to the call.
1901 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1902 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001903
Eric Christopherf9764fa2010-09-30 20:49:44 +00001904 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001905 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001906 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001907
Eric Christopherf9764fa2010-09-30 20:49:44 +00001908 // Set all unused physreg defs as dead.
1909 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001910
Eric Christopherf9764fa2010-09-30 20:49:44 +00001911 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001912
Eric Christopherf9764fa2010-09-30 20:49:44 +00001913}
1914
Eric Christopher56d2b722010-09-02 23:43:26 +00001915// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001916bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001917
Eric Christopherab695882010-07-21 22:26:11 +00001918 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001919 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001920 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001921 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001922 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001923 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001924 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001925 case Instruction::ICmp:
1926 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001927 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001928 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001929 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001930 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001931 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001932 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001933 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001934 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001935 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001936 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001937 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001938 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001939 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001940 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001941 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001942 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001943 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001944 case Instruction::SRem:
1945 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001946 case Instruction::Call:
1947 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001948 case Instruction::Select:
1949 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001950 case Instruction::Ret:
1951 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001952 default: break;
1953 }
1954 return false;
1955}
1956
1957namespace llvm {
1958 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001959 // Completely untested on non-darwin.
1960 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001961
Eric Christopheraaa8df42010-11-02 01:21:28 +00001962 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001963 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001964 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001965 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001966 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001967 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001968 }
1969}