blob: 345c46b4aa8e3ec790495b886440de953097ee7f [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"
Jay Foad562b84b2011-04-11 09:35:34 +000029#include "llvm/Operator.h"
Eric Christopherab695882010-07-21 22:26:11 +000030#include "llvm/CodeGen/Analysis.h"
31#include "llvm/CodeGen/FastISel.h"
32#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
34#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000035#include "llvm/CodeGen/MachineConstantPool.h"
36#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000037#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000040#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000041#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000042#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000044#include "llvm/Target/TargetData.h"
45#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000048#include "llvm/Target/TargetOptions.h"
49using namespace llvm;
50
Eric Christopher038fea52010-08-17 00:46:57 +000051static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000052DisableARMFastISel("disable-arm-fast-isel",
53 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000054 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000055
Eric Christopher836c6242010-12-15 23:47:29 +000056extern cl::opt<bool> EnableARMLongCalls;
57
Eric Christopherab695882010-07-21 22:26:11 +000058namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000059
Eric Christopher0d581222010-11-19 22:30:02 +000060 // All possible address modes, plus some.
61 typedef struct Address {
62 enum {
63 RegBase,
64 FrameIndexBase
65 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000066
Eric Christopher0d581222010-11-19 22:30:02 +000067 union {
68 unsigned Reg;
69 int FI;
70 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000071
Eric Christopher0d581222010-11-19 22:30:02 +000072 int Offset;
73 unsigned Scale;
74 unsigned PlusReg;
Eric Christopher827656d2010-11-20 22:38:27 +000075
Eric Christopher0d581222010-11-19 22:30:02 +000076 // Innocuous defaults for our address.
77 Address()
78 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
79 Base.Reg = 0;
80 }
81 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000082
83class ARMFastISel : public FastISel {
84
85 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
86 /// make the right decision when generating code for different targets.
87 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000088 const TargetMachine &TM;
89 const TargetInstrInfo &TII;
90 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000091 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000092
Eric Christopher8cf6c602010-09-29 22:24:45 +000093 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000094 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000095 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000096
Eric Christopherab695882010-07-21 22:26:11 +000097 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000098 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000099 : FastISel(funcInfo),
100 TM(funcInfo.MF->getTarget()),
101 TII(*TM.getInstrInfo()),
102 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000103 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000104 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000105 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000106 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000107 }
108
Eric Christophercb592292010-08-20 00:20:31 +0000109 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000110 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC);
112 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill);
115 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
116 const TargetRegisterClass *RC,
117 unsigned Op0, bool Op0IsKill,
118 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000119 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
120 const TargetRegisterClass *RC,
121 unsigned Op0, bool Op0IsKill,
122 unsigned Op1, bool Op1IsKill,
123 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000124 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
126 unsigned Op0, bool Op0IsKill,
127 uint64_t Imm);
128 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
129 const TargetRegisterClass *RC,
130 unsigned Op0, bool Op0IsKill,
131 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000132 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
133 const TargetRegisterClass *RC,
134 unsigned Op0, bool Op0IsKill,
135 unsigned Op1, bool Op1IsKill,
136 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000137 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
138 const TargetRegisterClass *RC,
139 uint64_t Imm);
140
Eric Christopher0fe7d542010-08-17 01:25:29 +0000141 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
142 unsigned Op0, bool Op0IsKill,
143 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000144
Eric Christophercb592292010-08-20 00:20:31 +0000145 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000146 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000147 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000148 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000149
150 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000151
Eric Christopher83007122010-08-23 21:44:12 +0000152 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000153 private:
Eric Christopher17787722010-10-21 21:47:51 +0000154 bool SelectLoad(const Instruction *I);
155 bool SelectStore(const Instruction *I);
156 bool SelectBranch(const Instruction *I);
157 bool SelectCmp(const Instruction *I);
158 bool SelectFPExt(const Instruction *I);
159 bool SelectFPTrunc(const Instruction *I);
160 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
161 bool SelectSIToFP(const Instruction *I);
162 bool SelectFPToSI(const Instruction *I);
163 bool SelectSDiv(const Instruction *I);
164 bool SelectSRem(const Instruction *I);
165 bool SelectCall(const Instruction *I);
166 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000167 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000168
Eric Christopher83007122010-08-23 21:44:12 +0000169 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000170 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000171 bool isTypeLegal(const Type *Ty, MVT &VT);
172 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000173 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
174 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
175 bool ARMComputeAddress(const Value *Obj, Address &Addr);
176 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000177 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000178 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000179 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000180 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000181 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000182 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000183
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000184 // Call handling routines.
185 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000186 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
187 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000188 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000189 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000190 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000191 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000192 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
193 SmallVectorImpl<unsigned> &RegArgs,
194 CallingConv::ID CC,
195 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000196 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000197 const Instruction *I, CallingConv::ID CC,
198 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000199 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000200
201 // OptionalDef handling routines.
202 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000203 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000204 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
205 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000206 void AddLoadStoreOperands(EVT VT, Address &Addr,
207 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000208};
Eric Christopherab695882010-07-21 22:26:11 +0000209
210} // end anonymous namespace
211
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000212#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000213
Eric Christopher456144e2010-08-19 00:37:05 +0000214// DefinesOptionalPredicate - This is different from DefinesPredicate in that
215// we don't care about implicit defs here, just places we'll need to add a
216// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
217bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
218 const TargetInstrDesc &TID = MI->getDesc();
219 if (!TID.hasOptionalDef())
220 return false;
221
222 // Look to see if our OptionalDef is defining CPSR or CCR.
223 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
224 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000225 if (!MO.isReg() || !MO.isDef()) continue;
226 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000227 *CPSR = true;
228 }
229 return true;
230}
231
Eric Christopheraf3dce52011-03-12 01:09:29 +0000232bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
233 const TargetInstrDesc &TID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000234
Eric Christopheraf3dce52011-03-12 01:09:29 +0000235 // If we're a thumb2 or not NEON function we were handled via isPredicable.
236 if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
237 AFI->isThumb2Function())
238 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000239
Eric Christopheraf3dce52011-03-12 01:09:29 +0000240 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i)
241 if (TID.OpInfo[i].isPredicate())
242 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000243
Eric Christopheraf3dce52011-03-12 01:09:29 +0000244 return false;
245}
246
Eric Christopher456144e2010-08-19 00:37:05 +0000247// If the machine is predicable go ahead and add the predicate operands, if
248// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000249// TODO: If we want to support thumb1 then we'll need to deal with optional
250// CPSR defs that need to be added before the remaining operands. See s_cc_out
251// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000252const MachineInstrBuilder &
253ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
254 MachineInstr *MI = &*MIB;
255
Eric Christopheraf3dce52011-03-12 01:09:29 +0000256 // Do we use a predicate? or...
257 // Are we NEON in ARM mode and have a predicate operand? If so, I know
258 // we're not predicable but add it anyways.
259 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000260 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000261
Eric Christopher456144e2010-08-19 00:37:05 +0000262 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
263 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000264 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000265 if (DefinesOptionalPredicate(MI, &CPSR)) {
266 if (CPSR)
267 AddDefaultT1CC(MIB);
268 else
269 AddDefaultCC(MIB);
270 }
271 return MIB;
272}
273
Eric Christopher0fe7d542010-08-17 01:25:29 +0000274unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
275 const TargetRegisterClass* RC) {
276 unsigned ResultReg = createResultReg(RC);
277 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
278
Eric Christopher456144e2010-08-19 00:37:05 +0000279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000280 return ResultReg;
281}
282
283unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
284 const TargetRegisterClass *RC,
285 unsigned Op0, bool Op0IsKill) {
286 unsigned ResultReg = createResultReg(RC);
287 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
288
289 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000290 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000291 .addReg(Op0, Op0IsKill * RegState::Kill));
292 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000294 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000296 TII.get(TargetOpcode::COPY), ResultReg)
297 .addReg(II.ImplicitDefs[0]));
298 }
299 return ResultReg;
300}
301
302unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
303 const TargetRegisterClass *RC,
304 unsigned Op0, bool Op0IsKill,
305 unsigned Op1, bool Op1IsKill) {
306 unsigned ResultReg = createResultReg(RC);
307 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
308
309 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 .addReg(Op0, Op0IsKill * RegState::Kill)
312 .addReg(Op1, Op1IsKill * RegState::Kill));
313 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000315 .addReg(Op0, Op0IsKill * RegState::Kill)
316 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000317 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000318 TII.get(TargetOpcode::COPY), ResultReg)
319 .addReg(II.ImplicitDefs[0]));
320 }
321 return ResultReg;
322}
323
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000324unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
325 const TargetRegisterClass *RC,
326 unsigned Op0, bool Op0IsKill,
327 unsigned Op1, bool Op1IsKill,
328 unsigned Op2, bool Op2IsKill) {
329 unsigned ResultReg = createResultReg(RC);
330 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
331
332 if (II.getNumDefs() >= 1)
333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
334 .addReg(Op0, Op0IsKill * RegState::Kill)
335 .addReg(Op1, Op1IsKill * RegState::Kill)
336 .addReg(Op2, Op2IsKill * RegState::Kill));
337 else {
338 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
339 .addReg(Op0, Op0IsKill * RegState::Kill)
340 .addReg(Op1, Op1IsKill * RegState::Kill)
341 .addReg(Op2, Op2IsKill * RegState::Kill));
342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
343 TII.get(TargetOpcode::COPY), ResultReg)
344 .addReg(II.ImplicitDefs[0]));
345 }
346 return ResultReg;
347}
348
Eric Christopher0fe7d542010-08-17 01:25:29 +0000349unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
350 const TargetRegisterClass *RC,
351 unsigned Op0, bool Op0IsKill,
352 uint64_t Imm) {
353 unsigned ResultReg = createResultReg(RC);
354 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
355
356 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000358 .addReg(Op0, Op0IsKill * RegState::Kill)
359 .addImm(Imm));
360 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000361 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000362 .addReg(Op0, Op0IsKill * RegState::Kill)
363 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000365 TII.get(TargetOpcode::COPY), ResultReg)
366 .addReg(II.ImplicitDefs[0]));
367 }
368 return ResultReg;
369}
370
371unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
372 const TargetRegisterClass *RC,
373 unsigned Op0, bool Op0IsKill,
374 const ConstantFP *FPImm) {
375 unsigned ResultReg = createResultReg(RC);
376 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
377
378 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000380 .addReg(Op0, Op0IsKill * RegState::Kill)
381 .addFPImm(FPImm));
382 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000383 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000384 .addReg(Op0, Op0IsKill * RegState::Kill)
385 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000386 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000387 TII.get(TargetOpcode::COPY), ResultReg)
388 .addReg(II.ImplicitDefs[0]));
389 }
390 return ResultReg;
391}
392
393unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
394 const TargetRegisterClass *RC,
395 unsigned Op0, bool Op0IsKill,
396 unsigned Op1, bool Op1IsKill,
397 uint64_t Imm) {
398 unsigned ResultReg = createResultReg(RC);
399 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
400
401 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000403 .addReg(Op0, Op0IsKill * RegState::Kill)
404 .addReg(Op1, Op1IsKill * RegState::Kill)
405 .addImm(Imm));
406 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000407 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000408 .addReg(Op0, Op0IsKill * RegState::Kill)
409 .addReg(Op1, Op1IsKill * RegState::Kill)
410 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000411 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000412 TII.get(TargetOpcode::COPY), ResultReg)
413 .addReg(II.ImplicitDefs[0]));
414 }
415 return ResultReg;
416}
417
418unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
419 const TargetRegisterClass *RC,
420 uint64_t Imm) {
421 unsigned ResultReg = createResultReg(RC);
422 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000423
Eric Christopher0fe7d542010-08-17 01:25:29 +0000424 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000425 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000426 .addImm(Imm));
427 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000428 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000429 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000430 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000431 TII.get(TargetOpcode::COPY), ResultReg)
432 .addReg(II.ImplicitDefs[0]));
433 }
434 return ResultReg;
435}
436
437unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
438 unsigned Op0, bool Op0IsKill,
439 uint32_t Idx) {
440 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
441 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
442 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000444 DL, TII.get(TargetOpcode::COPY), ResultReg)
445 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
446 return ResultReg;
447}
448
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000449// TODO: Don't worry about 64-bit now, but when this is fixed remove the
450// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000451unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000452 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000453
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000454 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
455 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
456 TII.get(ARM::VMOVRS), MoveReg)
457 .addReg(SrcReg));
458 return MoveReg;
459}
460
461unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000462 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000463
Eric Christopheraa3ace12010-09-09 20:49:25 +0000464 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000466 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000467 .addReg(SrcReg));
468 return MoveReg;
469}
470
Eric Christopher9ed58df2010-09-09 00:19:41 +0000471// For double width floating point we need to materialize two constants
472// (the high and the low) into integer registers then use a move to get
473// the combined constant into an FP reg.
474unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
475 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000476 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000477
Eric Christopher9ed58df2010-09-09 00:19:41 +0000478 // This checks to see if we can use VFP3 instructions to materialize
479 // a constant, otherwise we have to go through the constant pool.
480 if (TLI.isFPImmLegal(Val, VT)) {
481 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
482 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
483 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
484 DestReg)
485 .addFPImm(CFP));
486 return DestReg;
487 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000488
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000489 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000490 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000491
Eric Christopher238bb162010-09-09 23:50:00 +0000492 // MachineConstantPool wants an explicit alignment.
493 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
494 if (Align == 0) {
495 // TODO: Figure out if this is correct.
496 Align = TD.getTypeAllocSize(CFP->getType());
497 }
498 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
499 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
500 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000501
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000502 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000503 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
504 DestReg)
505 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000506 .addReg(0));
507 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000508}
509
Eric Christopher744c7c82010-09-28 22:47:54 +0000510unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000511
Eric Christopher744c7c82010-09-28 22:47:54 +0000512 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000513 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000514
Eric Christophere5b13cf2010-11-03 20:21:17 +0000515 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
516
517 // If we can do this in a single instruction without a constant pool entry
518 // do so now.
519 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000520 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000521 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
522 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000523 TII.get(Opc), DestReg)
524 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000525 return DestReg;
526 }
527
Eric Christopher56d2b722010-09-02 23:43:26 +0000528 // MachineConstantPool wants an explicit alignment.
529 unsigned Align = TD.getPrefTypeAlignment(C->getType());
530 if (Align == 0) {
531 // TODO: Figure out if this is correct.
532 Align = TD.getTypeAllocSize(C->getType());
533 }
534 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000535
Eric Christopher56d2b722010-09-02 23:43:26 +0000536 if (isThumb)
537 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000538 TII.get(ARM::t2LDRpci), DestReg)
539 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000540 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000541 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000542 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000543 TII.get(ARM::LDRcp), DestReg)
544 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000545 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000546
Eric Christopher56d2b722010-09-02 23:43:26 +0000547 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000548}
549
Eric Christopherc9932f62010-10-01 23:24:42 +0000550unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000551 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000552 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000553
Eric Christopher890dbbe2010-10-02 00:32:44 +0000554 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000555
Eric Christopher890dbbe2010-10-02 00:32:44 +0000556 // TODO: No external globals for now.
557 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000558
Eric Christopher890dbbe2010-10-02 00:32:44 +0000559 // TODO: Need more magic for ARM PIC.
560 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000561
Eric Christopher890dbbe2010-10-02 00:32:44 +0000562 // MachineConstantPool wants an explicit alignment.
563 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
564 if (Align == 0) {
565 // TODO: Figure out if this is correct.
566 Align = TD.getTypeAllocSize(GV->getType());
567 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000568
Eric Christopher890dbbe2010-10-02 00:32:44 +0000569 // Grab index.
570 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000571 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000572 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
573 ARMCP::CPValue, PCAdj);
574 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000575
Eric Christopher890dbbe2010-10-02 00:32:44 +0000576 // Load value.
577 MachineInstrBuilder MIB;
578 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
579 if (isThumb) {
580 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
581 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
582 .addConstantPoolIndex(Idx);
583 if (RelocM == Reloc::PIC_)
584 MIB.addImm(Id);
585 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000586 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000587 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
588 DestReg)
589 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000590 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000591 }
592 AddOptionalDefs(MIB);
593 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000594}
595
Eric Christopher9ed58df2010-09-09 00:19:41 +0000596unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
597 EVT VT = TLI.getValueType(C->getType(), true);
598
599 // Only handle simple types.
600 if (!VT.isSimple()) return 0;
601
602 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
603 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000604 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
605 return ARMMaterializeGV(GV, VT);
606 else if (isa<ConstantInt>(C))
607 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000608
Eric Christopherc9932f62010-10-01 23:24:42 +0000609 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000610}
611
Eric Christopherf9764fa2010-09-30 20:49:44 +0000612unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
613 // Don't handle dynamic allocas.
614 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000615
Duncan Sands1440e8b2010-11-03 11:35:31 +0000616 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000617 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000618
Eric Christopherf9764fa2010-09-30 20:49:44 +0000619 DenseMap<const AllocaInst*, int>::iterator SI =
620 FuncInfo.StaticAllocaMap.find(AI);
621
622 // This will get lowered later into the correct offsets and registers
623 // via rewriteXFrameIndex.
624 if (SI != FuncInfo.StaticAllocaMap.end()) {
625 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
626 unsigned ResultReg = createResultReg(RC);
627 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
628 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
629 TII.get(Opc), ResultReg)
630 .addFrameIndex(SI->second)
631 .addImm(0));
632 return ResultReg;
633 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000634
Eric Christopherf9764fa2010-09-30 20:49:44 +0000635 return 0;
636}
637
Duncan Sands1440e8b2010-11-03 11:35:31 +0000638bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
639 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000640
Eric Christopherb1cc8482010-08-25 07:23:49 +0000641 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000642 if (evt == MVT::Other || !evt.isSimple()) return false;
643 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000644
Eric Christopherdc908042010-08-31 01:28:42 +0000645 // Handle all legal types, i.e. a register that will directly hold this
646 // value.
647 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000648}
649
Duncan Sands1440e8b2010-11-03 11:35:31 +0000650bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000651 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000652
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000653 // If this is a type than can be sign or zero-extended to a basic operation
654 // go ahead and accept it now.
655 if (VT == MVT::i8 || VT == MVT::i16)
656 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000657
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000658 return false;
659}
660
Eric Christopher88de86b2010-11-19 22:36:41 +0000661// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000662bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000663 // Some boilerplate from the X86 FastISel.
664 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000665 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000666 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000667 // Don't walk into other basic blocks unless the object is an alloca from
668 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000669 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
670 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
671 Opcode = I->getOpcode();
672 U = I;
673 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000674 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000675 Opcode = C->getOpcode();
676 U = C;
677 }
678
Eric Christophercb0b04b2010-08-24 00:07:24 +0000679 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000680 if (Ty->getAddressSpace() > 255)
681 // Fast instruction selection doesn't support the special
682 // address spaces.
683 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000684
Eric Christopher83007122010-08-23 21:44:12 +0000685 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000686 default:
Eric Christopher83007122010-08-23 21:44:12 +0000687 break;
Eric Christopher55324332010-10-12 00:43:21 +0000688 case Instruction::BitCast: {
689 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000690 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000691 }
692 case Instruction::IntToPtr: {
693 // Look past no-op inttoptrs.
694 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000695 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000696 break;
697 }
698 case Instruction::PtrToInt: {
699 // Look past no-op ptrtoints.
700 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000701 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000702 break;
703 }
Eric Christophereae84392010-10-14 09:29:41 +0000704 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000705 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000706 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000707
Eric Christophereae84392010-10-14 09:29:41 +0000708 // Iterate through the GEP folding the constants into offsets where
709 // we can.
710 gep_type_iterator GTI = gep_type_begin(U);
711 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
712 i != e; ++i, ++GTI) {
713 const Value *Op = *i;
714 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
715 const StructLayout *SL = TD.getStructLayout(STy);
716 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
717 TmpOffset += SL->getElementOffset(Idx);
718 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000719 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000720 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000721 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
722 // Constant-offset addressing.
723 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000724 break;
725 }
726 if (isa<AddOperator>(Op) &&
727 (!isa<Instruction>(Op) ||
728 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
729 == FuncInfo.MBB) &&
730 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000731 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000732 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000733 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000734 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000735 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000736 // Iterate on the other operand.
737 Op = cast<AddOperator>(Op)->getOperand(0);
738 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000739 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000740 // Unsupported
741 goto unsupported_gep;
742 }
Eric Christophereae84392010-10-14 09:29:41 +0000743 }
744 }
Eric Christopher2896df82010-10-15 18:02:07 +0000745
746 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000747 Addr.Offset = TmpOffset;
748 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000749
750 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000751 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000752
Eric Christophereae84392010-10-14 09:29:41 +0000753 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000754 break;
755 }
Eric Christopher83007122010-08-23 21:44:12 +0000756 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000757 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000758 DenseMap<const AllocaInst*, int>::iterator SI =
759 FuncInfo.StaticAllocaMap.find(AI);
760 if (SI != FuncInfo.StaticAllocaMap.end()) {
761 Addr.BaseType = Address::FrameIndexBase;
762 Addr.Base.FI = SI->second;
763 return true;
764 }
765 break;
Eric Christopher83007122010-08-23 21:44:12 +0000766 }
767 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000768
Eric Christophera9c57512010-10-13 21:41:51 +0000769 // Materialize the global variable's address into a reg which can
770 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000771 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000772 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
773 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000774
Eric Christopher0d581222010-11-19 22:30:02 +0000775 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000776 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000777 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000778
Eric Christophercb0b04b2010-08-24 00:07:24 +0000779 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000780 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
781 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000782}
783
Eric Christopher0d581222010-11-19 22:30:02 +0000784void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000785
Eric Christopher212ae932010-10-21 19:40:30 +0000786 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000787
Eric Christopher212ae932010-10-21 19:40:30 +0000788 bool needsLowering = false;
789 switch (VT.getSimpleVT().SimpleTy) {
790 default:
791 assert(false && "Unhandled load/store type!");
792 case MVT::i1:
793 case MVT::i8:
794 case MVT::i16:
795 case MVT::i32:
796 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000797 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000798 break;
799 case MVT::f32:
800 case MVT::f64:
801 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000802 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000803 break;
804 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000805
Eric Christopher827656d2010-11-20 22:38:27 +0000806 // If this is a stack pointer and the offset needs to be simplified then
807 // put the alloca address into a register, set the base type back to
808 // register and continue. This should almost never happen.
809 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
810 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
811 ARM::GPRRegisterClass;
812 unsigned ResultReg = createResultReg(RC);
813 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
814 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
815 TII.get(Opc), ResultReg)
816 .addFrameIndex(Addr.Base.FI)
817 .addImm(0));
818 Addr.Base.Reg = ResultReg;
819 Addr.BaseType = Address::RegBase;
820 }
821
Eric Christopher212ae932010-10-21 19:40:30 +0000822 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000823 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000824 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000825 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
826 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000827 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000828 }
Eric Christopher83007122010-08-23 21:44:12 +0000829}
830
Eric Christopher564857f2010-12-01 01:40:24 +0000831void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
832 const MachineInstrBuilder &MIB) {
833 // addrmode5 output depends on the selection dag addressing dividing the
834 // offset by 4 that it then later multiplies. Do this here as well.
835 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
836 VT.getSimpleVT().SimpleTy == MVT::f64)
837 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000838
Eric Christopher564857f2010-12-01 01:40:24 +0000839 // Frame base works a bit differently. Handle it separately.
840 if (Addr.BaseType == Address::FrameIndexBase) {
841 int FI = Addr.Base.FI;
842 int Offset = Addr.Offset;
843 MachineMemOperand *MMO =
844 FuncInfo.MF->getMachineMemOperand(
845 MachinePointerInfo::getFixedStack(FI, Offset),
846 MachineMemOperand::MOLoad,
847 MFI.getObjectSize(FI),
848 MFI.getObjectAlignment(FI));
849 // Now add the rest of the operands.
850 MIB.addFrameIndex(FI);
851
852 // ARM halfword load/stores need an additional operand.
853 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
854
855 MIB.addImm(Addr.Offset);
856 MIB.addMemOperand(MMO);
857 } else {
858 // Now add the rest of the operands.
859 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000860
Eric Christopher564857f2010-12-01 01:40:24 +0000861 // ARM halfword load/stores need an additional operand.
862 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
863
864 MIB.addImm(Addr.Offset);
865 }
866 AddOptionalDefs(MIB);
867}
868
Eric Christopher0d581222010-11-19 22:30:02 +0000869bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000870
Eric Christopherb1cc8482010-08-25 07:23:49 +0000871 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000872 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000873 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000874 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000875 // This is mostly going to be Neon/vector support.
876 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000877 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000878 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000879 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000880 break;
881 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000882 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000883 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000884 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000885 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000886 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000887 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000888 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000889 case MVT::f32:
890 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000891 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000892 break;
893 case MVT::f64:
894 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000895 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000896 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000897 }
Eric Christopher564857f2010-12-01 01:40:24 +0000898 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000899 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000900
Eric Christopher564857f2010-12-01 01:40:24 +0000901 // Create the base instruction, then add the operands.
902 ResultReg = createResultReg(RC);
903 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
904 TII.get(Opc), ResultReg);
905 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000906 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000907}
908
Eric Christopher43b62be2010-09-27 06:02:23 +0000909bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000910 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000911 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000912 if (!isLoadTypeLegal(I->getType(), VT))
913 return false;
914
Eric Christopher564857f2010-12-01 01:40:24 +0000915 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000916 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000917 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000918
919 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000920 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000921 UpdateValueMap(I, ResultReg);
922 return true;
923}
924
Eric Christopher0d581222010-11-19 22:30:02 +0000925bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000926 unsigned StrOpc;
927 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000928 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000929 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000930 case MVT::i1: {
931 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
932 ARM::GPRRegisterClass);
933 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
934 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
935 TII.get(Opc), Res)
936 .addReg(SrcReg).addImm(1));
937 SrcReg = Res;
938 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000939 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000940 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000941 break;
942 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000943 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000944 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000945 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000946 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000947 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000948 case MVT::f32:
949 if (!Subtarget->hasVFP2()) return false;
950 StrOpc = ARM::VSTRS;
951 break;
952 case MVT::f64:
953 if (!Subtarget->hasVFP2()) return false;
954 StrOpc = ARM::VSTRD;
955 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000956 }
Eric Christopher564857f2010-12-01 01:40:24 +0000957 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000958 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000959
Eric Christopher564857f2010-12-01 01:40:24 +0000960 // Create the base instruction, then add the operands.
961 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
962 TII.get(StrOpc))
963 .addReg(SrcReg, getKillRegState(true));
964 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000965 return true;
966}
967
Eric Christopher43b62be2010-09-27 06:02:23 +0000968bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000969 Value *Op0 = I->getOperand(0);
970 unsigned SrcReg = 0;
971
Eric Christopher564857f2010-12-01 01:40:24 +0000972 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000973 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000974 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000975 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000976
Eric Christopher1b61ef42010-09-02 01:48:11 +0000977 // Get the value to be stored into a register.
978 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000979 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000980
Eric Christopher564857f2010-12-01 01:40:24 +0000981 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000982 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000983 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000984 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000985
Eric Christopher0d581222010-11-19 22:30:02 +0000986 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +0000987 return true;
988}
989
990static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
991 switch (Pred) {
992 // Needs two compares...
993 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000994 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000995 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000996 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000997 return ARMCC::AL;
998 case CmpInst::ICMP_EQ:
999 case CmpInst::FCMP_OEQ:
1000 return ARMCC::EQ;
1001 case CmpInst::ICMP_SGT:
1002 case CmpInst::FCMP_OGT:
1003 return ARMCC::GT;
1004 case CmpInst::ICMP_SGE:
1005 case CmpInst::FCMP_OGE:
1006 return ARMCC::GE;
1007 case CmpInst::ICMP_UGT:
1008 case CmpInst::FCMP_UGT:
1009 return ARMCC::HI;
1010 case CmpInst::FCMP_OLT:
1011 return ARMCC::MI;
1012 case CmpInst::ICMP_ULE:
1013 case CmpInst::FCMP_OLE:
1014 return ARMCC::LS;
1015 case CmpInst::FCMP_ORD:
1016 return ARMCC::VC;
1017 case CmpInst::FCMP_UNO:
1018 return ARMCC::VS;
1019 case CmpInst::FCMP_UGE:
1020 return ARMCC::PL;
1021 case CmpInst::ICMP_SLT:
1022 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001023 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001024 case CmpInst::ICMP_SLE:
1025 case CmpInst::FCMP_ULE:
1026 return ARMCC::LE;
1027 case CmpInst::FCMP_UNE:
1028 case CmpInst::ICMP_NE:
1029 return ARMCC::NE;
1030 case CmpInst::ICMP_UGE:
1031 return ARMCC::HS;
1032 case CmpInst::ICMP_ULT:
1033 return ARMCC::LO;
1034 }
Eric Christopher543cf052010-09-01 22:16:27 +00001035}
1036
Eric Christopher43b62be2010-09-27 06:02:23 +00001037bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001038 const BranchInst *BI = cast<BranchInst>(I);
1039 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1040 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001041
Eric Christophere5734102010-09-03 00:35:47 +00001042 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001043
Eric Christopher0e6233b2010-10-29 21:08:19 +00001044 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1045 // behavior.
1046 // TODO: Factor this out.
1047 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Eric Christopher632ae892011-04-29 21:56:31 +00001048 MVT SourceVT;
1049 const Type *Ty = CI->getOperand(0)->getType();
1050 if (CI->hasOneUse() && (CI->getParent() == I->getParent())
1051 && isTypeLegal(Ty, SourceVT)) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001052 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1053 if (isFloat && !Subtarget->hasVFP2())
1054 return false;
1055
1056 unsigned CmpOpc;
Eric Christopher632ae892011-04-29 21:56:31 +00001057 switch (SourceVT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001058 default: return false;
1059 // TODO: Verify compares.
1060 case MVT::f32:
1061 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001062 break;
1063 case MVT::f64:
1064 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001065 break;
1066 case MVT::i32:
1067 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001068 break;
1069 }
1070
1071 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001072 // Try to take advantage of fallthrough opportunities.
1073 CmpInst::Predicate Predicate = CI->getPredicate();
1074 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1075 std::swap(TBB, FBB);
1076 Predicate = CmpInst::getInversePredicate(Predicate);
1077 }
1078
1079 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001080
1081 // We may not handle every CC for now.
1082 if (ARMPred == ARMCC::AL) return false;
1083
1084 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1085 if (Arg1 == 0) return false;
1086
1087 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1088 if (Arg2 == 0) return false;
1089
1090 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1091 TII.get(CmpOpc))
1092 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001093
Eric Christopher0e6233b2010-10-29 21:08:19 +00001094 // For floating point we need to move the result to a comparison register
1095 // that we can then use for branches.
1096 if (isFloat)
1097 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1098 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001099
Eric Christopher0e6233b2010-10-29 21:08:19 +00001100 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1102 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1103 FastEmitBranch(FBB, DL);
1104 FuncInfo.MBB->addSuccessor(TBB);
1105 return true;
1106 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001107 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1108 MVT SourceVT;
1109 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1110 (isTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1111 unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1112 unsigned OpReg = getRegForValue(TI->getOperand(0));
1113 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1114 TII.get(TstOpc))
1115 .addReg(OpReg).addImm(1));
1116
1117 unsigned CCMode = ARMCC::NE;
1118 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1119 std::swap(TBB, FBB);
1120 CCMode = ARMCC::EQ;
1121 }
1122
1123 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1124 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1125 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1126
1127 FastEmitBranch(FBB, DL);
1128 FuncInfo.MBB->addSuccessor(TBB);
1129 return true;
1130 }
Eric Christopher0e6233b2010-10-29 21:08:19 +00001131 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001132
Eric Christopher0e6233b2010-10-29 21:08:19 +00001133 unsigned CmpReg = getRegForValue(BI->getCondition());
1134 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001135
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001136 // We've been divorced from our compare! Our block was split, and
1137 // now our compare lives in a predecessor block. We musn't
1138 // re-compare here, as the children of the compare aren't guaranteed
1139 // live across the block boundary (we *could* check for this).
1140 // Regardless, the compare has been done in the predecessor block,
1141 // and it left a value for us in a virtual register. Ergo, we test
1142 // the one-bit value left in the virtual register.
1143 unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1144 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1145 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001146
Eric Christopher7a20a372011-04-28 16:52:09 +00001147 unsigned CCMode = ARMCC::NE;
1148 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1149 std::swap(TBB, FBB);
1150 CCMode = ARMCC::EQ;
1151 }
1152
Eric Christophere5734102010-09-03 00:35:47 +00001153 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001155 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001156 FastEmitBranch(FBB, DL);
1157 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001158 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001159}
1160
Eric Christopher43b62be2010-09-27 06:02:23 +00001161bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001162 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001163
Duncan Sands1440e8b2010-11-03 11:35:31 +00001164 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001165 const Type *Ty = CI->getOperand(0)->getType();
1166 if (!isTypeLegal(Ty, VT))
1167 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001168
Eric Christopherd43393a2010-09-08 23:13:45 +00001169 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1170 if (isFloat && !Subtarget->hasVFP2())
1171 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001172
Eric Christopherd43393a2010-09-08 23:13:45 +00001173 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001174 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001175 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001176 default: return false;
1177 // TODO: Verify compares.
1178 case MVT::f32:
1179 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001180 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001181 break;
1182 case MVT::f64:
1183 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001184 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001185 break;
1186 case MVT::i32:
1187 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001188 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001189 break;
1190 }
1191
Eric Christopher229207a2010-09-29 01:14:47 +00001192 // Get the compare predicate.
1193 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001194
Eric Christopher229207a2010-09-29 01:14:47 +00001195 // We may not handle every CC for now.
1196 if (ARMPred == ARMCC::AL) return false;
1197
Eric Christopherd43393a2010-09-08 23:13:45 +00001198 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1199 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001200
Eric Christopherd43393a2010-09-08 23:13:45 +00001201 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1202 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001203
Eric Christopherd43393a2010-09-08 23:13:45 +00001204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1205 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001206
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001207 // For floating point we need to move the result to a comparison register
1208 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001209 if (isFloat)
1210 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1211 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001212
Eric Christopher229207a2010-09-29 01:14:47 +00001213 // Now set a register based on the comparison. Explicitly set the predicates
1214 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001215 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001216 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001217 : ARM::GPRRegisterClass;
1218 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001219 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001220 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001221 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1223 .addReg(ZeroReg).addImm(1)
1224 .addImm(ARMPred).addReg(CondReg);
1225
Eric Christophera5b1e682010-09-17 22:28:18 +00001226 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001227 return true;
1228}
1229
Eric Christopher43b62be2010-09-27 06:02:23 +00001230bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001231 // Make sure we have VFP and that we're extending float to double.
1232 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001233
Eric Christopher46203602010-09-09 00:26:48 +00001234 Value *V = I->getOperand(0);
1235 if (!I->getType()->isDoubleTy() ||
1236 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001237
Eric Christopher46203602010-09-09 00:26:48 +00001238 unsigned Op = getRegForValue(V);
1239 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001240
Eric Christopher46203602010-09-09 00:26:48 +00001241 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001243 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001244 .addReg(Op));
1245 UpdateValueMap(I, Result);
1246 return true;
1247}
1248
Eric Christopher43b62be2010-09-27 06:02:23 +00001249bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001250 // Make sure we have VFP and that we're truncating double to float.
1251 if (!Subtarget->hasVFP2()) return false;
1252
1253 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001254 if (!(I->getType()->isFloatTy() &&
1255 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001256
1257 unsigned Op = getRegForValue(V);
1258 if (Op == 0) return false;
1259
1260 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001262 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001263 .addReg(Op));
1264 UpdateValueMap(I, Result);
1265 return true;
1266}
1267
Eric Christopher43b62be2010-09-27 06:02:23 +00001268bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001269 // Make sure we have VFP.
1270 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001271
Duncan Sands1440e8b2010-11-03 11:35:31 +00001272 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001273 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001274 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001275 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001276
Eric Christopher9a040492010-09-09 18:54:59 +00001277 unsigned Op = getRegForValue(I->getOperand(0));
1278 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001279
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001280 // The conversion routine works on fp-reg to fp-reg and the operand above
1281 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001282 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001283 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001284
Eric Christopher9a040492010-09-09 18:54:59 +00001285 unsigned Opc;
1286 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1287 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1288 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001289
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001290 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1292 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001293 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001294 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001295 return true;
1296}
1297
Eric Christopher43b62be2010-09-27 06:02:23 +00001298bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001299 // Make sure we have VFP.
1300 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001301
Duncan Sands1440e8b2010-11-03 11:35:31 +00001302 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001303 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001304 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001305 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001306
Eric Christopher9a040492010-09-09 18:54:59 +00001307 unsigned Op = getRegForValue(I->getOperand(0));
1308 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001309
Eric Christopher9a040492010-09-09 18:54:59 +00001310 unsigned Opc;
1311 const Type *OpTy = I->getOperand(0)->getType();
1312 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1313 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1314 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001315
Eric Christopher022b7fb2010-10-05 23:13:24 +00001316 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1317 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001318 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1319 ResultReg)
1320 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001321
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001322 // This result needs to be in an integer register, but the conversion only
1323 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001324 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001325 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001326
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001327 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001328 return true;
1329}
1330
Eric Christopher3bbd3962010-10-11 08:27:59 +00001331bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001332 MVT VT;
1333 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001334 return false;
1335
1336 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001337 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001338 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1339
1340 unsigned CondReg = getRegForValue(I->getOperand(0));
1341 if (CondReg == 0) return false;
1342 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1343 if (Op1Reg == 0) return false;
1344 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1345 if (Op2Reg == 0) return false;
1346
1347 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1348 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1349 .addReg(CondReg).addImm(1));
1350 unsigned ResultReg = createResultReg(RC);
1351 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1352 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1353 .addReg(Op1Reg).addReg(Op2Reg)
1354 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1355 UpdateValueMap(I, ResultReg);
1356 return true;
1357}
1358
Eric Christopher08637852010-09-30 22:34:19 +00001359bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001360 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001361 const Type *Ty = I->getType();
1362 if (!isTypeLegal(Ty, VT))
1363 return false;
1364
1365 // If we have integer div support we should have selected this automagically.
1366 // In case we have a real miss go ahead and return false and we'll pick
1367 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001368 if (Subtarget->hasDivide()) return false;
1369
Eric Christopher08637852010-09-30 22:34:19 +00001370 // Otherwise emit a libcall.
1371 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001372 if (VT == MVT::i8)
1373 LC = RTLIB::SDIV_I8;
1374 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001375 LC = RTLIB::SDIV_I16;
1376 else if (VT == MVT::i32)
1377 LC = RTLIB::SDIV_I32;
1378 else if (VT == MVT::i64)
1379 LC = RTLIB::SDIV_I64;
1380 else if (VT == MVT::i128)
1381 LC = RTLIB::SDIV_I128;
1382 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001383
Eric Christopher08637852010-09-30 22:34:19 +00001384 return ARMEmitLibcall(I, LC);
1385}
1386
Eric Christopher6a880d62010-10-11 08:37:26 +00001387bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001388 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001389 const Type *Ty = I->getType();
1390 if (!isTypeLegal(Ty, VT))
1391 return false;
1392
1393 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1394 if (VT == MVT::i8)
1395 LC = RTLIB::SREM_I8;
1396 else if (VT == MVT::i16)
1397 LC = RTLIB::SREM_I16;
1398 else if (VT == MVT::i32)
1399 LC = RTLIB::SREM_I32;
1400 else if (VT == MVT::i64)
1401 LC = RTLIB::SREM_I64;
1402 else if (VT == MVT::i128)
1403 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001404 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001405
Eric Christopher6a880d62010-10-11 08:37:26 +00001406 return ARMEmitLibcall(I, LC);
1407}
1408
Eric Christopher43b62be2010-09-27 06:02:23 +00001409bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001410 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001411
Eric Christopherbc39b822010-09-09 00:53:57 +00001412 // We can get here in the case when we want to use NEON for our fp
1413 // operations, but can't figure out how to. Just use the vfp instructions
1414 // if we have them.
1415 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001416 const Type *Ty = I->getType();
1417 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1418 if (isFloat && !Subtarget->hasVFP2())
1419 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001420
Eric Christopherbc39b822010-09-09 00:53:57 +00001421 unsigned Op1 = getRegForValue(I->getOperand(0));
1422 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001423
Eric Christopherbc39b822010-09-09 00:53:57 +00001424 unsigned Op2 = getRegForValue(I->getOperand(1));
1425 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001426
Eric Christopherbc39b822010-09-09 00:53:57 +00001427 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001428 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001429 switch (ISDOpcode) {
1430 default: return false;
1431 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001432 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001433 break;
1434 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001435 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001436 break;
1437 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001438 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001439 break;
1440 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001441 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1443 TII.get(Opc), ResultReg)
1444 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001445 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001446 return true;
1447}
1448
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001449// Call Handling Code
1450
Eric Christopherfa87d662010-10-18 02:17:53 +00001451bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1452 EVT SrcVT, unsigned &ResultReg) {
1453 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1454 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001455
Eric Christopherfa87d662010-10-18 02:17:53 +00001456 if (RR != 0) {
1457 ResultReg = RR;
1458 return true;
1459 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001460 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001461}
1462
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001463// This is largely taken directly from CCAssignFnForNode - we don't support
1464// varargs in FastISel so that part has been removed.
1465// TODO: We may not support all of this.
1466CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1467 switch (CC) {
1468 default:
1469 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001470 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001471 // Ignore fastcc. Silence compiler warnings.
1472 (void)RetFastCC_ARM_APCS;
1473 (void)FastCC_ARM_APCS;
1474 // Fallthrough
1475 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001476 // Use target triple & subtarget features to do actual dispatch.
1477 if (Subtarget->isAAPCS_ABI()) {
1478 if (Subtarget->hasVFP2() &&
1479 FloatABIType == FloatABI::Hard)
1480 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1481 else
1482 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1483 } else
1484 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1485 case CallingConv::ARM_AAPCS_VFP:
1486 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1487 case CallingConv::ARM_AAPCS:
1488 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1489 case CallingConv::ARM_APCS:
1490 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1491 }
1492}
1493
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001494bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1495 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001496 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001497 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1498 SmallVectorImpl<unsigned> &RegArgs,
1499 CallingConv::ID CC,
1500 unsigned &NumBytes) {
1501 SmallVector<CCValAssign, 16> ArgLocs;
1502 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1503 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1504
1505 // Get a count of how many bytes are to be pushed on the stack.
1506 NumBytes = CCInfo.getNextStackOffset();
1507
1508 // Issue CALLSEQ_START
1509 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001510 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1511 TII.get(AdjStackDown))
1512 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001513
1514 // Process the args.
1515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1516 CCValAssign &VA = ArgLocs[i];
1517 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001518 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001519
Eric Christopher4a2b3162011-01-27 05:44:56 +00001520 // We don't handle NEON/vector parameters yet.
1521 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001522 return false;
1523
Eric Christopherf9764fa2010-09-30 20:49:44 +00001524 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001525 switch (VA.getLocInfo()) {
1526 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001527 case CCValAssign::SExt: {
1528 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1529 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001530 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001531 Emitted = true;
1532 ArgVT = VA.getLocVT();
1533 break;
1534 }
1535 case CCValAssign::ZExt: {
1536 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1537 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001538 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001539 Emitted = true;
1540 ArgVT = VA.getLocVT();
1541 break;
1542 }
1543 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001544 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1545 Arg, ArgVT, Arg);
1546 if (!Emitted)
1547 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1548 Arg, ArgVT, Arg);
1549 if (!Emitted)
1550 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1551 Arg, ArgVT, Arg);
1552
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001553 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001554 ArgVT = VA.getLocVT();
1555 break;
1556 }
1557 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001558 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001559 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001560 assert(BC != 0 && "Failed to emit a bitcast!");
1561 Arg = BC;
1562 ArgVT = VA.getLocVT();
1563 break;
1564 }
1565 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001566 }
1567
1568 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001569 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001570 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001571 VA.getLocReg())
1572 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001573 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001574 } else if (VA.needsCustom()) {
1575 // TODO: We need custom lowering for vector (v2f64) args.
1576 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001577
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001578 CCValAssign &NextVA = ArgLocs[++i];
1579
1580 // TODO: Only handle register args for now.
1581 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1582
1583 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1584 TII.get(ARM::VMOVRRD), VA.getLocReg())
1585 .addReg(NextVA.getLocReg(), RegState::Define)
1586 .addReg(Arg));
1587 RegArgs.push_back(VA.getLocReg());
1588 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001589 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001590 assert(VA.isMemLoc());
1591 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001592 Address Addr;
1593 Addr.BaseType = Address::RegBase;
1594 Addr.Base.Reg = ARM::SP;
1595 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001596
Eric Christopher0d581222010-11-19 22:30:02 +00001597 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001598 }
1599 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001600 return true;
1601}
1602
Duncan Sands1440e8b2010-11-03 11:35:31 +00001603bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001604 const Instruction *I, CallingConv::ID CC,
1605 unsigned &NumBytes) {
1606 // Issue CALLSEQ_END
1607 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001608 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1609 TII.get(AdjStackUp))
1610 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001611
1612 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001613 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001614 SmallVector<CCValAssign, 16> RVLocs;
1615 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1616 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1617
1618 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001619 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001620 // For this move we copy into two registers and then move into the
1621 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001622 EVT DestVT = RVLocs[0].getValVT();
1623 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1624 unsigned ResultReg = createResultReg(DstRC);
1625 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1626 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001627 .addReg(RVLocs[0].getLocReg())
1628 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001629
Eric Christopher3659ac22010-10-20 08:02:24 +00001630 UsedRegs.push_back(RVLocs[0].getLocReg());
1631 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001632
Eric Christopherdccd2c32010-10-11 08:38:55 +00001633 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001634 UpdateValueMap(I, ResultReg);
1635 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001636 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001637 EVT CopyVT = RVLocs[0].getValVT();
1638 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001639
Eric Christopher14df8822010-10-01 00:00:11 +00001640 unsigned ResultReg = createResultReg(DstRC);
1641 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1642 ResultReg).addReg(RVLocs[0].getLocReg());
1643 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001644
Eric Christopherdccd2c32010-10-11 08:38:55 +00001645 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001646 UpdateValueMap(I, ResultReg);
1647 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001648 }
1649
Eric Christopherdccd2c32010-10-11 08:38:55 +00001650 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001651}
1652
Eric Christopher4f512ef2010-10-22 01:28:00 +00001653bool ARMFastISel::SelectRet(const Instruction *I) {
1654 const ReturnInst *Ret = cast<ReturnInst>(I);
1655 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001656
Eric Christopher4f512ef2010-10-22 01:28:00 +00001657 if (!FuncInfo.CanLowerReturn)
1658 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001659
Eric Christopher4f512ef2010-10-22 01:28:00 +00001660 if (F.isVarArg())
1661 return false;
1662
1663 CallingConv::ID CC = F.getCallingConv();
1664 if (Ret->getNumOperands() > 0) {
1665 SmallVector<ISD::OutputArg, 4> Outs;
1666 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1667 Outs, TLI);
1668
1669 // Analyze operands of the call, assigning locations to each operand.
1670 SmallVector<CCValAssign, 16> ValLocs;
1671 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1672 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1673
1674 const Value *RV = Ret->getOperand(0);
1675 unsigned Reg = getRegForValue(RV);
1676 if (Reg == 0)
1677 return false;
1678
1679 // Only handle a single return value for now.
1680 if (ValLocs.size() != 1)
1681 return false;
1682
1683 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001684
Eric Christopher4f512ef2010-10-22 01:28:00 +00001685 // Don't bother handling odd stuff for now.
1686 if (VA.getLocInfo() != CCValAssign::Full)
1687 return false;
1688 // Only handle register returns for now.
1689 if (!VA.isRegLoc())
1690 return false;
1691 // TODO: For now, don't try to handle cases where getLocInfo()
1692 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001693 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001694 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001695
Eric Christopher4f512ef2010-10-22 01:28:00 +00001696 // Make the copy.
1697 unsigned SrcReg = Reg + VA.getValNo();
1698 unsigned DstReg = VA.getLocReg();
1699 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1700 // Avoid a cross-class copy. This is very unlikely.
1701 if (!SrcRC->contains(DstReg))
1702 return false;
1703 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1704 DstReg).addReg(SrcReg);
1705
1706 // Mark the register as live out of the function.
1707 MRI.addLiveOut(VA.getLocReg());
1708 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001709
Eric Christopher4f512ef2010-10-22 01:28:00 +00001710 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1711 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1712 TII.get(RetOpc)));
1713 return true;
1714}
1715
Eric Christopher872f4a22011-02-22 01:37:10 +00001716unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1717
Eric Christopher872f4a22011-02-22 01:37:10 +00001718 // Darwin needs the r9 versions of the opcodes.
1719 bool isDarwin = Subtarget->isTargetDarwin();
Eric Christopher04356612011-04-05 00:39:26 +00001720 if (isThumb) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001721 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1722 } else {
1723 return isDarwin ? ARM::BLr9 : ARM::BL;
1724 }
1725}
1726
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001727// A quick function that will emit a call for a named libcall in F with the
1728// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001729// can emit a call for any libcall we can produce. This is an abridged version
1730// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001731// like computed function pointers or strange arguments at call sites.
1732// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1733// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001734bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1735 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001736
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001737 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001738 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001739 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001740 if (RetTy->isVoidTy())
1741 RetVT = MVT::isVoid;
1742 else if (!isTypeLegal(RetTy, RetVT))
1743 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001744
Eric Christopher836c6242010-12-15 23:47:29 +00001745 // TODO: For now if we have long calls specified we don't handle the call.
1746 if (EnableARMLongCalls) return false;
1747
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001748 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001749 SmallVector<Value*, 8> Args;
1750 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001751 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001752 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1753 Args.reserve(I->getNumOperands());
1754 ArgRegs.reserve(I->getNumOperands());
1755 ArgVTs.reserve(I->getNumOperands());
1756 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001757 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001758 Value *Op = I->getOperand(i);
1759 unsigned Arg = getRegForValue(Op);
1760 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001761
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001762 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001763 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001764 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001765
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001766 ISD::ArgFlagsTy Flags;
1767 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1768 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001769
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001770 Args.push_back(Op);
1771 ArgRegs.push_back(Arg);
1772 ArgVTs.push_back(ArgVT);
1773 ArgFlags.push_back(Flags);
1774 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001775
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001776 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001777 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001778 unsigned NumBytes;
1779 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1780 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001781
Eric Christopher6344a5f2011-04-29 00:07:20 +00001782 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001783 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001784 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001785 unsigned CallOpc = ARMSelectCallOp(NULL);
1786 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001787 // Explicitly adding the predicate here.
1788 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1789 TII.get(CallOpc)))
1790 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001791 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001792 // Explicitly adding the predicate here.
1793 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1794 TII.get(CallOpc))
1795 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001796
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001797 // Add implicit physical register uses to the call.
1798 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1799 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001800
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001801 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001802 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001803 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001804
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001805 // Set all unused physreg defs as dead.
1806 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001807
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001808 return true;
1809}
1810
Eric Christopherf9764fa2010-09-30 20:49:44 +00001811bool ARMFastISel::SelectCall(const Instruction *I) {
1812 const CallInst *CI = cast<CallInst>(I);
1813 const Value *Callee = CI->getCalledValue();
1814
1815 // Can't handle inline asm or worry about intrinsics yet.
1816 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1817
Eric Christophere6ca6772010-10-01 21:33:12 +00001818 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001819 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001820 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1821 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001822
Eric Christopherf9764fa2010-09-30 20:49:44 +00001823 // Check the calling convention.
1824 ImmutableCallSite CS(CI);
1825 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001826
Eric Christopherf9764fa2010-09-30 20:49:44 +00001827 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001828
Eric Christopherf9764fa2010-09-30 20:49:44 +00001829 // Let SDISel handle vararg functions.
1830 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1831 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1832 if (FTy->isVarArg())
1833 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001834
Eric Christopherf9764fa2010-09-30 20:49:44 +00001835 // Handle *simple* calls for now.
1836 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001837 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001838 if (RetTy->isVoidTy())
1839 RetVT = MVT::isVoid;
1840 else if (!isTypeLegal(RetTy, RetVT))
1841 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001842
Eric Christopher836c6242010-12-15 23:47:29 +00001843 // TODO: For now if we have long calls specified we don't handle the call.
1844 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00001845
Eric Christopherf9764fa2010-09-30 20:49:44 +00001846 // Set up the argument vectors.
1847 SmallVector<Value*, 8> Args;
1848 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001849 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001850 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1851 Args.reserve(CS.arg_size());
1852 ArgRegs.reserve(CS.arg_size());
1853 ArgVTs.reserve(CS.arg_size());
1854 ArgFlags.reserve(CS.arg_size());
1855 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1856 i != e; ++i) {
1857 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001858
Eric Christopherf9764fa2010-09-30 20:49:44 +00001859 if (Arg == 0)
1860 return false;
1861 ISD::ArgFlagsTy Flags;
1862 unsigned AttrInd = i - CS.arg_begin() + 1;
1863 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1864 Flags.setSExt();
1865 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1866 Flags.setZExt();
1867
1868 // FIXME: Only handle *easy* calls for now.
1869 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1870 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1871 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1872 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1873 return false;
1874
1875 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001876 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001877 if (!isTypeLegal(ArgTy, ArgVT))
1878 return false;
1879 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1880 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001881
Eric Christopherf9764fa2010-09-30 20:49:44 +00001882 Args.push_back(*i);
1883 ArgRegs.push_back(Arg);
1884 ArgVTs.push_back(ArgVT);
1885 ArgFlags.push_back(Flags);
1886 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001887
Eric Christopherf9764fa2010-09-30 20:49:44 +00001888 // Handle the arguments now that we've gotten them.
1889 SmallVector<unsigned, 4> RegArgs;
1890 unsigned NumBytes;
1891 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1892 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001893
Eric Christopher6344a5f2011-04-29 00:07:20 +00001894 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001895 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001896 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001897 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001898 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001899 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001900 // Explicitly adding the predicate here.
1901 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1902 TII.get(CallOpc)))
1903 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001904 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001905 // Explicitly adding the predicate here.
1906 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1907 TII.get(CallOpc))
1908 .addGlobalAddress(GV, 0, 0));
Eric Christopher299bbb22011-04-29 00:03:10 +00001909
Eric Christopherf9764fa2010-09-30 20:49:44 +00001910 // Add implicit physical register uses to the call.
1911 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1912 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001913
Eric Christopherf9764fa2010-09-30 20:49:44 +00001914 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001915 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001916 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001917
Eric Christopherf9764fa2010-09-30 20:49:44 +00001918 // Set all unused physreg defs as dead.
1919 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001920
Eric Christopherf9764fa2010-09-30 20:49:44 +00001921 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001922
Eric Christopherf9764fa2010-09-30 20:49:44 +00001923}
1924
Eric Christopher56d2b722010-09-02 23:43:26 +00001925// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001926bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001927
Eric Christopherab695882010-07-21 22:26:11 +00001928 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001929 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001930 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001931 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001932 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001933 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001934 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001935 case Instruction::ICmp:
1936 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001937 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001938 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001939 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001940 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001941 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001942 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001943 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001944 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001945 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001946 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001947 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001948 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001949 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001950 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001951 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001952 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001953 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001954 case Instruction::SRem:
1955 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001956 case Instruction::Call:
1957 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001958 case Instruction::Select:
1959 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001960 case Instruction::Ret:
1961 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001962 default: break;
1963 }
1964 return false;
1965}
1966
1967namespace llvm {
1968 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001969 // Completely untested on non-darwin.
1970 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001971
Eric Christopheraaa8df42010-11-02 01:21:28 +00001972 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001973 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001974 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001975 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001976 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001977 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001978 }
1979}