blob: 4df084f34bd94cb7ab54b5c757fb2ff2b35d740e [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"
Evan Chengee04a6d2011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Eric Christopherab695882010-07-21 22:26:11 +000024#include "llvm/CallingConv.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/Instructions.h"
28#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000029#include "llvm/Module.h"
Jay Foad562b84b2011-04-11 09:35:34 +000030#include "llvm/Operator.h"
Eric Christopherab695882010-07-21 22:26:11 +000031#include "llvm/CodeGen/Analysis.h"
32#include "llvm/CodeGen/FastISel.h"
33#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000036#include "llvm/CodeGen/MachineConstantPool.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000042#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000045#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000049#include "llvm/Target/TargetOptions.h"
50using namespace llvm;
51
Eric Christopher038fea52010-08-17 00:46:57 +000052static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000053DisableARMFastISel("disable-arm-fast-isel",
54 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000055 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000056
Eric Christopher836c6242010-12-15 23:47:29 +000057extern cl::opt<bool> EnableARMLongCalls;
58
Eric Christopherab695882010-07-21 22:26:11 +000059namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000060
Eric Christopher0d581222010-11-19 22:30:02 +000061 // All possible address modes, plus some.
62 typedef struct Address {
63 enum {
64 RegBase,
65 FrameIndexBase
66 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000067
Eric Christopher0d581222010-11-19 22:30:02 +000068 union {
69 unsigned Reg;
70 int FI;
71 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000072
Eric Christopher0d581222010-11-19 22:30:02 +000073 int Offset;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
Jim Grosbach0c720762011-05-16 22:24:07 +000077 : BaseType(RegBase), Offset(0) {
Eric Christopher0d581222010-11-19 22:30:02 +000078 Base.Reg = 0;
79 }
80 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000081
82class ARMFastISel : public FastISel {
83
84 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
85 /// make the right decision when generating code for different targets.
86 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 const TargetMachine &TM;
88 const TargetInstrInfo &TII;
89 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000090 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000091
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 // Convenience variables to avoid some queries.
Chad Rosier66dc8ca2011-11-08 21:12:00 +000093 bool isThumb2;
Eric Christopher8cf6c602010-09-29 22:24:45 +000094 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000095
Eric Christopherab695882010-07-21 22:26:11 +000096 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000097 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000098 : FastISel(funcInfo),
99 TM(funcInfo.MF->getTarget()),
100 TII(*TM.getInstrInfo()),
101 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000102 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000103 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000104 isThumb2 = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000105 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000106 }
107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000109 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC);
111 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill);
114 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000118 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 unsigned Op1, bool Op1IsKill,
122 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000123 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
124 const TargetRegisterClass *RC,
125 unsigned Op0, bool Op0IsKill,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000131 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 unsigned Op0, bool Op0IsKill,
134 unsigned Op1, bool Op1IsKill,
135 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000136 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
137 const TargetRegisterClass *RC,
138 uint64_t Imm);
Eric Christopherd94bc542011-04-29 22:07:50 +0000139 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
140 const TargetRegisterClass *RC,
141 uint64_t Imm1, uint64_t Imm2);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000142
Eric Christopher0fe7d542010-08-17 01:25:29 +0000143 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
144 unsigned Op0, bool Op0IsKill,
145 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000146
Eric Christophercb592292010-08-20 00:20:31 +0000147 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000148 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000149 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000150 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Chad Rosierb29b9502011-11-13 02:23:59 +0000151 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
152 const LoadInst *LI);
Eric Christopherab695882010-07-21 22:26:11 +0000153
154 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000155
Eric Christopher83007122010-08-23 21:44:12 +0000156 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000157 private:
Eric Christopher17787722010-10-21 21:47:51 +0000158 bool SelectLoad(const Instruction *I);
159 bool SelectStore(const Instruction *I);
160 bool SelectBranch(const Instruction *I);
161 bool SelectCmp(const Instruction *I);
162 bool SelectFPExt(const Instruction *I);
163 bool SelectFPTrunc(const Instruction *I);
164 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
165 bool SelectSIToFP(const Instruction *I);
166 bool SelectFPToSI(const Instruction *I);
167 bool SelectSDiv(const Instruction *I);
168 bool SelectSRem(const Instruction *I);
Chad Rosier11add262011-11-11 23:31:03 +0000169 bool SelectCall(const Instruction *I, const char *IntrMemName);
170 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher17787722010-10-21 21:47:51 +0000171 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000172 bool SelectRet(const Instruction *I);
Chad Rosier0d7b2312011-11-02 00:18:48 +0000173 bool SelectTrunc(const Instruction *I);
174 bool SelectIntExt(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000175
Eric Christopher83007122010-08-23 21:44:12 +0000176 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000177 private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000178 bool isTypeLegal(Type *Ty, MVT &VT);
179 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosiere07cd5e2011-11-02 18:08:25 +0000180 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
181 bool isZExt);
Chad Rosierb29b9502011-11-13 02:23:59 +0000182 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, bool isZExt,
183 bool allocReg);
184
Eric Christopher0d581222010-11-19 22:30:02 +0000185 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
186 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosierb29b9502011-11-13 02:23:59 +0000187 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
Chad Rosier2c42b8c2011-11-14 23:04:09 +0000188 bool ARMIsMemCpySmall(uint64_t Len);
189 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
Chad Rosier87633022011-11-02 17:20:24 +0000190 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000191 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000192 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000193 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000194 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000195 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000196 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000197
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000198 // Call handling routines.
199 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000200 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
201 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000202 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000203 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000204 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000205 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000206 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
207 SmallVectorImpl<unsigned> &RegArgs,
208 CallingConv::ID CC,
209 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000210 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000211 const Instruction *I, CallingConv::ID CC,
212 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000213 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000214
215 // OptionalDef handling routines.
216 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000217 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000218 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
219 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000220 void AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000221 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000222 unsigned Flags, bool useAM3);
Eric Christopher456144e2010-08-19 00:37:05 +0000223};
Eric Christopherab695882010-07-21 22:26:11 +0000224
225} // end anonymous namespace
226
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000227#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000228
Eric Christopher456144e2010-08-19 00:37:05 +0000229// DefinesOptionalPredicate - This is different from DefinesPredicate in that
230// we don't care about implicit defs here, just places we'll need to add a
231// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
232bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Chenge837dea2011-06-28 19:10:37 +0000233 const MCInstrDesc &MCID = MI->getDesc();
234 if (!MCID.hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000235 return false;
236
237 // Look to see if our OptionalDef is defining CPSR or CCR.
238 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
239 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000240 if (!MO.isReg() || !MO.isDef()) continue;
241 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000242 *CPSR = true;
243 }
244 return true;
245}
246
Eric Christopheraf3dce52011-03-12 01:09:29 +0000247bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000248 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000249
Eric Christopheraf3dce52011-03-12 01:09:29 +0000250 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000251 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000252 AFI->isThumb2Function())
253 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000254
Evan Chenge837dea2011-06-28 19:10:37 +0000255 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
256 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000257 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000258
Eric Christopheraf3dce52011-03-12 01:09:29 +0000259 return false;
260}
261
Eric Christopher456144e2010-08-19 00:37:05 +0000262// If the machine is predicable go ahead and add the predicate operands, if
263// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000264// TODO: If we want to support thumb1 then we'll need to deal with optional
265// CPSR defs that need to be added before the remaining operands. See s_cc_out
266// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000267const MachineInstrBuilder &
268ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
269 MachineInstr *MI = &*MIB;
270
Eric Christopheraf3dce52011-03-12 01:09:29 +0000271 // Do we use a predicate? or...
272 // Are we NEON in ARM mode and have a predicate operand? If so, I know
273 // we're not predicable but add it anyways.
274 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000275 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000276
Eric Christopher456144e2010-08-19 00:37:05 +0000277 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
278 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000279 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000280 if (DefinesOptionalPredicate(MI, &CPSR)) {
281 if (CPSR)
282 AddDefaultT1CC(MIB);
283 else
284 AddDefaultCC(MIB);
285 }
286 return MIB;
287}
288
Eric Christopher0fe7d542010-08-17 01:25:29 +0000289unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
290 const TargetRegisterClass* RC) {
291 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000292 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293
Eric Christopher456144e2010-08-19 00:37:05 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295 return ResultReg;
296}
297
298unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
299 const TargetRegisterClass *RC,
300 unsigned Op0, bool Op0IsKill) {
301 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000302 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303
304 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 .addReg(Op0, Op0IsKill * RegState::Kill));
307 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 TII.get(TargetOpcode::COPY), ResultReg)
312 .addReg(II.ImplicitDefs[0]));
313 }
314 return ResultReg;
315}
316
317unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
318 const TargetRegisterClass *RC,
319 unsigned Op0, bool Op0IsKill,
320 unsigned Op1, bool Op1IsKill) {
321 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000322 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323
324 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000326 .addReg(Op0, Op0IsKill * RegState::Kill)
327 .addReg(Op1, Op1IsKill * RegState::Kill));
328 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 .addReg(Op0, Op0IsKill * RegState::Kill)
331 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000332 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000333 TII.get(TargetOpcode::COPY), ResultReg)
334 .addReg(II.ImplicitDefs[0]));
335 }
336 return ResultReg;
337}
338
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000339unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
340 const TargetRegisterClass *RC,
341 unsigned Op0, bool Op0IsKill,
342 unsigned Op1, bool Op1IsKill,
343 unsigned Op2, bool Op2IsKill) {
344 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000345 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000346
347 if (II.getNumDefs() >= 1)
348 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
349 .addReg(Op0, Op0IsKill * RegState::Kill)
350 .addReg(Op1, Op1IsKill * RegState::Kill)
351 .addReg(Op2, Op2IsKill * RegState::Kill));
352 else {
353 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
354 .addReg(Op0, Op0IsKill * RegState::Kill)
355 .addReg(Op1, Op1IsKill * RegState::Kill)
356 .addReg(Op2, Op2IsKill * RegState::Kill));
357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
358 TII.get(TargetOpcode::COPY), ResultReg)
359 .addReg(II.ImplicitDefs[0]));
360 }
361 return ResultReg;
362}
363
Eric Christopher0fe7d542010-08-17 01:25:29 +0000364unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
365 const TargetRegisterClass *RC,
366 unsigned Op0, bool Op0IsKill,
367 uint64_t Imm) {
368 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000369 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000370
371 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000372 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000373 .addReg(Op0, Op0IsKill * RegState::Kill)
374 .addImm(Imm));
375 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000377 .addReg(Op0, Op0IsKill * RegState::Kill)
378 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000380 TII.get(TargetOpcode::COPY), ResultReg)
381 .addReg(II.ImplicitDefs[0]));
382 }
383 return ResultReg;
384}
385
386unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
387 const TargetRegisterClass *RC,
388 unsigned Op0, bool Op0IsKill,
389 const ConstantFP *FPImm) {
390 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000391 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000392
393 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000394 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000395 .addReg(Op0, Op0IsKill * RegState::Kill)
396 .addFPImm(FPImm));
397 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000399 .addReg(Op0, Op0IsKill * RegState::Kill)
400 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000401 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000402 TII.get(TargetOpcode::COPY), ResultReg)
403 .addReg(II.ImplicitDefs[0]));
404 }
405 return ResultReg;
406}
407
408unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
409 const TargetRegisterClass *RC,
410 unsigned Op0, bool Op0IsKill,
411 unsigned Op1, bool Op1IsKill,
412 uint64_t Imm) {
413 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000414 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000415
416 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000417 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000418 .addReg(Op0, Op0IsKill * RegState::Kill)
419 .addReg(Op1, Op1IsKill * RegState::Kill)
420 .addImm(Imm));
421 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000423 .addReg(Op0, Op0IsKill * RegState::Kill)
424 .addReg(Op1, Op1IsKill * RegState::Kill)
425 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000426 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000427 TII.get(TargetOpcode::COPY), ResultReg)
428 .addReg(II.ImplicitDefs[0]));
429 }
430 return ResultReg;
431}
432
433unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
434 const TargetRegisterClass *RC,
435 uint64_t Imm) {
436 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000437 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000438
Eric Christopher0fe7d542010-08-17 01:25:29 +0000439 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000441 .addImm(Imm));
442 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000444 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000445 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000446 TII.get(TargetOpcode::COPY), ResultReg)
447 .addReg(II.ImplicitDefs[0]));
448 }
449 return ResultReg;
450}
451
Eric Christopherd94bc542011-04-29 22:07:50 +0000452unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
453 const TargetRegisterClass *RC,
454 uint64_t Imm1, uint64_t Imm2) {
455 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000456 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000457
Eric Christopherd94bc542011-04-29 22:07:50 +0000458 if (II.getNumDefs() >= 1)
459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
460 .addImm(Imm1).addImm(Imm2));
461 else {
462 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
463 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000465 TII.get(TargetOpcode::COPY),
466 ResultReg)
467 .addReg(II.ImplicitDefs[0]));
468 }
469 return ResultReg;
470}
471
Eric Christopher0fe7d542010-08-17 01:25:29 +0000472unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
473 unsigned Op0, bool Op0IsKill,
474 uint32_t Idx) {
475 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
476 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
477 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000478 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000479 DL, TII.get(TargetOpcode::COPY), ResultReg)
480 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
481 return ResultReg;
482}
483
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000484// TODO: Don't worry about 64-bit now, but when this is fixed remove the
485// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000486unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000487 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000488
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000489 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
490 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
491 TII.get(ARM::VMOVRS), MoveReg)
492 .addReg(SrcReg));
493 return MoveReg;
494}
495
496unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000497 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000498
Eric Christopheraa3ace12010-09-09 20:49:25 +0000499 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
500 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000501 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000502 .addReg(SrcReg));
503 return MoveReg;
504}
505
Eric Christopher9ed58df2010-09-09 00:19:41 +0000506// For double width floating point we need to materialize two constants
507// (the high and the low) into integer registers then use a move to get
508// the combined constant into an FP reg.
509unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
510 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000511 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000512
Eric Christopher9ed58df2010-09-09 00:19:41 +0000513 // This checks to see if we can use VFP3 instructions to materialize
514 // a constant, otherwise we have to go through the constant pool.
515 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000516 int Imm;
517 unsigned Opc;
518 if (is64bit) {
519 Imm = ARM_AM::getFP64Imm(Val);
520 Opc = ARM::FCONSTD;
521 } else {
522 Imm = ARM_AM::getFP32Imm(Val);
523 Opc = ARM::FCONSTS;
524 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000525 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
526 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
527 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000528 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000529 return DestReg;
530 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000531
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000532 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000533 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000534
Eric Christopher238bb162010-09-09 23:50:00 +0000535 // MachineConstantPool wants an explicit alignment.
536 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
537 if (Align == 0) {
538 // TODO: Figure out if this is correct.
539 Align = TD.getTypeAllocSize(CFP->getType());
540 }
541 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
542 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
543 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000544
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000545 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000546 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
547 DestReg)
548 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000549 .addReg(0));
550 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000551}
552
Eric Christopher744c7c82010-09-28 22:47:54 +0000553unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000554
Chad Rosier44e89572011-11-04 22:29:00 +0000555 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
556 return false;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000557
558 // If we can do this in a single instruction without a constant pool entry
559 // do so now.
560 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiera4e07272011-11-04 23:09:49 +0000561 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000562 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier4e89d972011-11-11 00:36:21 +0000563 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000564 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier44e89572011-11-04 22:29:00 +0000565 TII.get(Opc), ImmReg)
Chad Rosier42536af2011-11-05 20:16:15 +0000566 .addImm(CI->getZExtValue()));
Chad Rosier44e89572011-11-04 22:29:00 +0000567 return ImmReg;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000568 }
569
Chad Rosier4e89d972011-11-11 00:36:21 +0000570 // Use MVN to emit negative constants.
571 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
572 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosier1c47de82011-11-11 06:27:41 +0000573 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier4e89d972011-11-11 00:36:21 +0000574 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier1c47de82011-11-11 06:27:41 +0000575 if (UseImm) {
Chad Rosier4e89d972011-11-11 00:36:21 +0000576 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
577 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
578 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
579 TII.get(Opc), ImmReg)
580 .addImm(Imm));
581 return ImmReg;
582 }
583 }
584
585 // Load from constant pool. For now 32-bit only.
Chad Rosier44e89572011-11-04 22:29:00 +0000586 if (VT != MVT::i32)
587 return false;
588
589 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
590
Eric Christopher56d2b722010-09-02 23:43:26 +0000591 // MachineConstantPool wants an explicit alignment.
592 unsigned Align = TD.getPrefTypeAlignment(C->getType());
593 if (Align == 0) {
594 // TODO: Figure out if this is correct.
595 Align = TD.getTypeAllocSize(C->getType());
596 }
597 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000598
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000599 if (isThumb2)
Eric Christopher56d2b722010-09-02 23:43:26 +0000600 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000601 TII.get(ARM::t2LDRpci), DestReg)
602 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000603 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000604 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000605 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000606 TII.get(ARM::LDRcp), DestReg)
607 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000608 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000609
Eric Christopher56d2b722010-09-02 23:43:26 +0000610 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000611}
612
Eric Christopherc9932f62010-10-01 23:24:42 +0000613unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000614 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000615 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000616
Eric Christopher890dbbe2010-10-02 00:32:44 +0000617 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000618
Eric Christopher890dbbe2010-10-02 00:32:44 +0000619 // TODO: Need more magic for ARM PIC.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000620 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000621
Eric Christopher890dbbe2010-10-02 00:32:44 +0000622 // MachineConstantPool wants an explicit alignment.
623 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
624 if (Align == 0) {
625 // TODO: Figure out if this is correct.
626 Align = TD.getTypeAllocSize(GV->getType());
627 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000628
Eric Christopher890dbbe2010-10-02 00:32:44 +0000629 // Grab index.
630 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000631 unsigned Id = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +0000632 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
633 ARMCP::CPValue,
634 PCAdj);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000635 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000636
Eric Christopher890dbbe2010-10-02 00:32:44 +0000637 // Load value.
638 MachineInstrBuilder MIB;
639 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000640 if (isThumb2) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000641 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
642 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
643 .addConstantPoolIndex(Idx);
644 if (RelocM == Reloc::PIC_)
645 MIB.addImm(Id);
646 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000647 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000648 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
649 DestReg)
650 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000651 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000652 }
653 AddOptionalDefs(MIB);
Eli Friedmand6412c92011-06-03 01:13:19 +0000654
655 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
656 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000657 if (isThumb2)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000658 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
659 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000660 .addReg(DestReg)
661 .addImm(0);
662 else
663 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
664 NewDestReg)
665 .addReg(DestReg)
666 .addImm(0);
667 DestReg = NewDestReg;
668 AddOptionalDefs(MIB);
669 }
670
Eric Christopher890dbbe2010-10-02 00:32:44 +0000671 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000672}
673
Eric Christopher9ed58df2010-09-09 00:19:41 +0000674unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
675 EVT VT = TLI.getValueType(C->getType(), true);
676
677 // Only handle simple types.
678 if (!VT.isSimple()) return 0;
679
680 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
681 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000682 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
683 return ARMMaterializeGV(GV, VT);
684 else if (isa<ConstantInt>(C))
685 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000686
Eric Christopherc9932f62010-10-01 23:24:42 +0000687 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000688}
689
Eric Christopherf9764fa2010-09-30 20:49:44 +0000690unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
691 // Don't handle dynamic allocas.
692 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000693
Duncan Sands1440e8b2010-11-03 11:35:31 +0000694 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000695 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000696
Eric Christopherf9764fa2010-09-30 20:49:44 +0000697 DenseMap<const AllocaInst*, int>::iterator SI =
698 FuncInfo.StaticAllocaMap.find(AI);
699
700 // This will get lowered later into the correct offsets and registers
701 // via rewriteXFrameIndex.
702 if (SI != FuncInfo.StaticAllocaMap.end()) {
703 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
704 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000705 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopherf9764fa2010-09-30 20:49:44 +0000706 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
707 TII.get(Opc), ResultReg)
708 .addFrameIndex(SI->second)
709 .addImm(0));
710 return ResultReg;
711 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000712
Eric Christopherf9764fa2010-09-30 20:49:44 +0000713 return 0;
714}
715
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000716bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000717 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000718
Eric Christopherb1cc8482010-08-25 07:23:49 +0000719 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000720 if (evt == MVT::Other || !evt.isSimple()) return false;
721 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000722
Eric Christopherdc908042010-08-31 01:28:42 +0000723 // Handle all legal types, i.e. a register that will directly hold this
724 // value.
725 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000726}
727
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000728bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000729 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000730
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000731 // If this is a type than can be sign or zero-extended to a basic operation
732 // go ahead and accept it now.
Chad Rosierb29b9502011-11-13 02:23:59 +0000733 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000734 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000735
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000736 return false;
737}
738
Eric Christopher88de86b2010-11-19 22:36:41 +0000739// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000740bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000741 // Some boilerplate from the X86 FastISel.
742 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000743 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000744 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000745 // Don't walk into other basic blocks unless the object is an alloca from
746 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000747 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
748 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
749 Opcode = I->getOpcode();
750 U = I;
751 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000752 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000753 Opcode = C->getOpcode();
754 U = C;
755 }
756
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000757 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000758 if (Ty->getAddressSpace() > 255)
759 // Fast instruction selection doesn't support the special
760 // address spaces.
761 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000762
Eric Christopher83007122010-08-23 21:44:12 +0000763 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000764 default:
Eric Christopher83007122010-08-23 21:44:12 +0000765 break;
Eric Christopher55324332010-10-12 00:43:21 +0000766 case Instruction::BitCast: {
767 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000768 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000769 }
770 case Instruction::IntToPtr: {
771 // Look past no-op inttoptrs.
772 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000773 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000774 break;
775 }
776 case Instruction::PtrToInt: {
777 // Look past no-op ptrtoints.
778 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000779 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000780 break;
781 }
Eric Christophereae84392010-10-14 09:29:41 +0000782 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000783 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000784 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000785
Eric Christophereae84392010-10-14 09:29:41 +0000786 // Iterate through the GEP folding the constants into offsets where
787 // we can.
788 gep_type_iterator GTI = gep_type_begin(U);
789 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
790 i != e; ++i, ++GTI) {
791 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000792 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000793 const StructLayout *SL = TD.getStructLayout(STy);
794 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
795 TmpOffset += SL->getElementOffset(Idx);
796 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000797 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000798 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000799 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
800 // Constant-offset addressing.
801 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000802 break;
803 }
804 if (isa<AddOperator>(Op) &&
805 (!isa<Instruction>(Op) ||
806 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
807 == FuncInfo.MBB) &&
808 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000809 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000810 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000811 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000812 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000813 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000814 // Iterate on the other operand.
815 Op = cast<AddOperator>(Op)->getOperand(0);
816 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000817 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000818 // Unsupported
819 goto unsupported_gep;
820 }
Eric Christophereae84392010-10-14 09:29:41 +0000821 }
822 }
Eric Christopher2896df82010-10-15 18:02:07 +0000823
824 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000825 Addr.Offset = TmpOffset;
826 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000827
828 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000829 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000830
Eric Christophereae84392010-10-14 09:29:41 +0000831 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000832 break;
833 }
Eric Christopher83007122010-08-23 21:44:12 +0000834 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000835 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000836 DenseMap<const AllocaInst*, int>::iterator SI =
837 FuncInfo.StaticAllocaMap.find(AI);
838 if (SI != FuncInfo.StaticAllocaMap.end()) {
839 Addr.BaseType = Address::FrameIndexBase;
840 Addr.Base.FI = SI->second;
841 return true;
842 }
843 break;
Eric Christopher83007122010-08-23 21:44:12 +0000844 }
845 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000846
Eric Christophera9c57512010-10-13 21:41:51 +0000847 // Materialize the global variable's address into a reg which can
848 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000849 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000850 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
851 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000852
Eric Christopher0d581222010-11-19 22:30:02 +0000853 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000854 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000855 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000856
Eric Christophercb0b04b2010-08-24 00:07:24 +0000857 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000858 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
859 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000860}
861
Chad Rosierb29b9502011-11-13 02:23:59 +0000862void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000863
Eric Christopher212ae932010-10-21 19:40:30 +0000864 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000865
Eric Christopher212ae932010-10-21 19:40:30 +0000866 bool needsLowering = false;
867 switch (VT.getSimpleVT().SimpleTy) {
868 default:
869 assert(false && "Unhandled load/store type!");
Chad Rosier73463472011-11-09 21:30:12 +0000870 break;
Eric Christopher212ae932010-10-21 19:40:30 +0000871 case MVT::i1:
872 case MVT::i8:
Chad Rosierb29b9502011-11-13 02:23:59 +0000873 case MVT::i16:
Eric Christopher212ae932010-10-21 19:40:30 +0000874 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +0000875 if (!useAM3) {
Chad Rosierb29b9502011-11-13 02:23:59 +0000876 // Integer loads/stores handle 12-bit offsets.
877 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosier57b29972011-11-14 20:22:27 +0000878 // Handle negative offsets.
Chad Rosiere489af82011-11-14 22:34:48 +0000879 if (needsLowering && isThumb2)
880 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
881 Addr.Offset > -256);
Chad Rosier57b29972011-11-14 20:22:27 +0000882 } else {
Chad Rosier5be833d2011-11-13 04:25:02 +0000883 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000884 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosier57b29972011-11-14 20:22:27 +0000885 }
Eric Christopher212ae932010-10-21 19:40:30 +0000886 break;
887 case MVT::f32:
888 case MVT::f64:
889 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000890 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000891 break;
892 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000893
Eric Christopher827656d2010-11-20 22:38:27 +0000894 // If this is a stack pointer and the offset needs to be simplified then
895 // put the alloca address into a register, set the base type back to
896 // register and continue. This should almost never happen.
897 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000898 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher827656d2010-11-20 22:38:27 +0000899 ARM::GPRRegisterClass;
900 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000901 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopher827656d2010-11-20 22:38:27 +0000902 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
903 TII.get(Opc), ResultReg)
904 .addFrameIndex(Addr.Base.FI)
905 .addImm(0));
906 Addr.Base.Reg = ResultReg;
907 Addr.BaseType = Address::RegBase;
908 }
909
Eric Christopher212ae932010-10-21 19:40:30 +0000910 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000911 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000912 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000913 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
914 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000915 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000916 }
Eric Christopher83007122010-08-23 21:44:12 +0000917}
918
Eric Christopher564857f2010-12-01 01:40:24 +0000919void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000920 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000921 unsigned Flags, bool useAM3) {
Eric Christopher564857f2010-12-01 01:40:24 +0000922 // addrmode5 output depends on the selection dag addressing dividing the
923 // offset by 4 that it then later multiplies. Do this here as well.
924 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
925 VT.getSimpleVT().SimpleTy == MVT::f64)
926 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000927
Eric Christopher564857f2010-12-01 01:40:24 +0000928 // Frame base works a bit differently. Handle it separately.
929 if (Addr.BaseType == Address::FrameIndexBase) {
930 int FI = Addr.Base.FI;
931 int Offset = Addr.Offset;
932 MachineMemOperand *MMO =
933 FuncInfo.MF->getMachineMemOperand(
934 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000935 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000936 MFI.getObjectSize(FI),
937 MFI.getObjectAlignment(FI));
938 // Now add the rest of the operands.
939 MIB.addFrameIndex(FI);
940
Chad Rosier5be833d2011-11-13 04:25:02 +0000941 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000942 if (useAM3) {
943 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
944 MIB.addReg(0);
945 MIB.addImm(Imm);
946 } else {
947 MIB.addImm(Addr.Offset);
948 }
Eric Christopher564857f2010-12-01 01:40:24 +0000949 MIB.addMemOperand(MMO);
950 } else {
951 // Now add the rest of the operands.
952 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000953
Chad Rosier5be833d2011-11-13 04:25:02 +0000954 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000955 if (useAM3) {
956 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
957 MIB.addReg(0);
958 MIB.addImm(Imm);
959 } else {
960 MIB.addImm(Addr.Offset);
961 }
Eric Christopher564857f2010-12-01 01:40:24 +0000962 }
963 AddOptionalDefs(MIB);
964}
965
Chad Rosierb29b9502011-11-13 02:23:59 +0000966bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
967 bool isZExt = true, bool allocReg = true) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000968 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000969 unsigned Opc;
Chad Rosierb29b9502011-11-13 02:23:59 +0000970 bool useAM3 = false;
971 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000972 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000973 // This is mostly going to be Neon/vector support.
974 default: return false;
Chad Rosier646abbf2011-11-11 02:38:59 +0000975 case MVT::i1:
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000976 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +0000977 if (isThumb2) {
978 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
979 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
980 else
981 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierb29b9502011-11-13 02:23:59 +0000982 } else {
Chad Rosier57b29972011-11-14 20:22:27 +0000983 if (isZExt) {
984 Opc = ARM::LDRBi12;
985 } else {
986 Opc = ARM::LDRSB;
987 useAM3 = true;
988 }
Chad Rosierb29b9502011-11-13 02:23:59 +0000989 }
Eric Christopher7a56f332010-10-08 01:13:17 +0000990 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000991 break;
Chad Rosier73463472011-11-09 21:30:12 +0000992 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +0000993 if (isThumb2) {
994 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
995 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
996 else
997 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
998 } else {
999 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1000 useAM3 = true;
1001 }
Chad Rosier73463472011-11-09 21:30:12 +00001002 RC = ARM::GPRRegisterClass;
1003 break;
Eric Christopherdc908042010-08-31 01:28:42 +00001004 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001005 if (isThumb2) {
1006 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1007 Opc = ARM::t2LDRi8;
1008 else
1009 Opc = ARM::t2LDRi12;
1010 } else {
1011 Opc = ARM::LDRi12;
1012 }
Eric Christopher7a56f332010-10-08 01:13:17 +00001013 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +00001014 break;
Eric Christopher6dab1372010-09-18 01:59:37 +00001015 case MVT::f32:
1016 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +00001017 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001018 break;
1019 case MVT::f64:
1020 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +00001021 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001022 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001023 }
Eric Christopher564857f2010-12-01 01:40:24 +00001024 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001025 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001026
Eric Christopher564857f2010-12-01 01:40:24 +00001027 // Create the base instruction, then add the operands.
Chad Rosierb29b9502011-11-13 02:23:59 +00001028 if (allocReg)
1029 ResultReg = createResultReg(RC);
1030 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher564857f2010-12-01 01:40:24 +00001031 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1032 TII.get(Opc), ResultReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001033 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Eric Christopherdc908042010-08-31 01:28:42 +00001034 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001035}
1036
Eric Christopher43b62be2010-09-27 06:02:23 +00001037bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +00001038 // Atomic loads need special handling.
1039 if (cast<LoadInst>(I)->isAtomic())
1040 return false;
1041
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001042 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001043 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001044 if (!isLoadTypeLegal(I->getType(), VT))
1045 return false;
1046
Eric Christopher564857f2010-12-01 01:40:24 +00001047 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001048 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +00001049 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001050
1051 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +00001052 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001053 UpdateValueMap(I, ResultReg);
1054 return true;
1055}
1056
Eric Christopher0d581222010-11-19 22:30:02 +00001057bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001058 unsigned StrOpc;
Chad Rosierb29b9502011-11-13 02:23:59 +00001059 bool useAM3 = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001060 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001061 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +00001062 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +00001063 case MVT::i1: {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001064 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher4c914122010-11-02 23:59:09 +00001065 ARM::GPRRegisterClass);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001066 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eric Christopher4c914122010-11-02 23:59:09 +00001067 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1068 TII.get(Opc), Res)
1069 .addReg(SrcReg).addImm(1));
1070 SrcReg = Res;
1071 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +00001072 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +00001073 if (isThumb2) {
1074 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1075 StrOpc = ARM::t2STRBi8;
1076 else
1077 StrOpc = ARM::t2STRBi12;
1078 } else {
1079 StrOpc = ARM::STRBi12;
1080 }
Eric Christopher15418772010-10-12 05:39:06 +00001081 break;
1082 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +00001083 if (isThumb2) {
1084 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1085 StrOpc = ARM::t2STRHi8;
1086 else
1087 StrOpc = ARM::t2STRHi12;
1088 } else {
1089 StrOpc = ARM::STRH;
1090 useAM3 = true;
1091 }
Eric Christopher15418772010-10-12 05:39:06 +00001092 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001093 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001094 if (isThumb2) {
1095 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1096 StrOpc = ARM::t2STRi8;
1097 else
1098 StrOpc = ARM::t2STRi12;
1099 } else {
1100 StrOpc = ARM::STRi12;
1101 }
Eric Christopher47650ec2010-10-16 01:10:35 +00001102 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001103 case MVT::f32:
1104 if (!Subtarget->hasVFP2()) return false;
1105 StrOpc = ARM::VSTRS;
1106 break;
1107 case MVT::f64:
1108 if (!Subtarget->hasVFP2()) return false;
1109 StrOpc = ARM::VSTRD;
1110 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001111 }
Eric Christopher564857f2010-12-01 01:40:24 +00001112 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001113 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001114
Eric Christopher564857f2010-12-01 01:40:24 +00001115 // Create the base instruction, then add the operands.
1116 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1117 TII.get(StrOpc))
1118 .addReg(SrcReg, getKillRegState(true));
Chad Rosierb29b9502011-11-13 02:23:59 +00001119 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001120 return true;
1121}
1122
Eric Christopher43b62be2010-09-27 06:02:23 +00001123bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001124 Value *Op0 = I->getOperand(0);
1125 unsigned SrcReg = 0;
1126
Eli Friedman4136d232011-09-02 22:33:24 +00001127 // Atomic stores need special handling.
1128 if (cast<StoreInst>(I)->isAtomic())
1129 return false;
1130
Eric Christopher564857f2010-12-01 01:40:24 +00001131 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001132 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001133 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001134 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001135
Eric Christopher1b61ef42010-09-02 01:48:11 +00001136 // Get the value to be stored into a register.
1137 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001138 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001139
Eric Christopher564857f2010-12-01 01:40:24 +00001140 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001141 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001142 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001143 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001144
Eric Christopher0d581222010-11-19 22:30:02 +00001145 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001146 return true;
1147}
1148
1149static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1150 switch (Pred) {
1151 // Needs two compares...
1152 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001153 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001154 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001155 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001156 return ARMCC::AL;
1157 case CmpInst::ICMP_EQ:
1158 case CmpInst::FCMP_OEQ:
1159 return ARMCC::EQ;
1160 case CmpInst::ICMP_SGT:
1161 case CmpInst::FCMP_OGT:
1162 return ARMCC::GT;
1163 case CmpInst::ICMP_SGE:
1164 case CmpInst::FCMP_OGE:
1165 return ARMCC::GE;
1166 case CmpInst::ICMP_UGT:
1167 case CmpInst::FCMP_UGT:
1168 return ARMCC::HI;
1169 case CmpInst::FCMP_OLT:
1170 return ARMCC::MI;
1171 case CmpInst::ICMP_ULE:
1172 case CmpInst::FCMP_OLE:
1173 return ARMCC::LS;
1174 case CmpInst::FCMP_ORD:
1175 return ARMCC::VC;
1176 case CmpInst::FCMP_UNO:
1177 return ARMCC::VS;
1178 case CmpInst::FCMP_UGE:
1179 return ARMCC::PL;
1180 case CmpInst::ICMP_SLT:
1181 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001182 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001183 case CmpInst::ICMP_SLE:
1184 case CmpInst::FCMP_ULE:
1185 return ARMCC::LE;
1186 case CmpInst::FCMP_UNE:
1187 case CmpInst::ICMP_NE:
1188 return ARMCC::NE;
1189 case CmpInst::ICMP_UGE:
1190 return ARMCC::HS;
1191 case CmpInst::ICMP_ULT:
1192 return ARMCC::LO;
1193 }
Eric Christopher543cf052010-09-01 22:16:27 +00001194}
1195
Eric Christopher43b62be2010-09-27 06:02:23 +00001196bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001197 const BranchInst *BI = cast<BranchInst>(I);
1198 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1199 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001200
Eric Christophere5734102010-09-03 00:35:47 +00001201 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001202
Eric Christopher0e6233b2010-10-29 21:08:19 +00001203 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1204 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001205 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001206 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001207
1208 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001209 // Try to take advantage of fallthrough opportunities.
1210 CmpInst::Predicate Predicate = CI->getPredicate();
1211 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1212 std::swap(TBB, FBB);
1213 Predicate = CmpInst::getInversePredicate(Predicate);
1214 }
1215
1216 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001217
1218 // We may not handle every CC for now.
1219 if (ARMPred == ARMCC::AL) return false;
1220
Chad Rosier75698f32011-10-26 23:17:28 +00001221 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001222 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier75698f32011-10-26 23:17:28 +00001223 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001224
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001225 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001226 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1227 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1228 FastEmitBranch(FBB, DL);
1229 FuncInfo.MBB->addSuccessor(TBB);
1230 return true;
1231 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001232 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1233 MVT SourceVT;
1234 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001235 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001236 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001237 unsigned OpReg = getRegForValue(TI->getOperand(0));
1238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1239 TII.get(TstOpc))
1240 .addReg(OpReg).addImm(1));
1241
1242 unsigned CCMode = ARMCC::NE;
1243 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1244 std::swap(TBB, FBB);
1245 CCMode = ARMCC::EQ;
1246 }
1247
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001248 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1250 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1251
1252 FastEmitBranch(FBB, DL);
1253 FuncInfo.MBB->addSuccessor(TBB);
1254 return true;
1255 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001256 } else if (const ConstantInt *CI =
1257 dyn_cast<ConstantInt>(BI->getCondition())) {
1258 uint64_t Imm = CI->getZExtValue();
1259 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1260 FastEmitBranch(Target, DL);
1261 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001262 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001263
Eric Christopher0e6233b2010-10-29 21:08:19 +00001264 unsigned CmpReg = getRegForValue(BI->getCondition());
1265 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001266
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001267 // We've been divorced from our compare! Our block was split, and
1268 // now our compare lives in a predecessor block. We musn't
1269 // re-compare here, as the children of the compare aren't guaranteed
1270 // live across the block boundary (we *could* check for this).
1271 // Regardless, the compare has been done in the predecessor block,
1272 // and it left a value for us in a virtual register. Ergo, we test
1273 // the one-bit value left in the virtual register.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001274 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001275 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1276 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001277
Eric Christopher7a20a372011-04-28 16:52:09 +00001278 unsigned CCMode = ARMCC::NE;
1279 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1280 std::swap(TBB, FBB);
1281 CCMode = ARMCC::EQ;
1282 }
1283
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001284 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001286 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001287 FastEmitBranch(FBB, DL);
1288 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001289 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001290}
1291
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001292bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1293 bool isZExt) {
Chad Rosierade62002011-10-26 23:25:44 +00001294 Type *Ty = Src1Value->getType();
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001295 EVT SrcVT = TLI.getValueType(Ty, true);
1296 if (!SrcVT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001297
Chad Rosierade62002011-10-26 23:25:44 +00001298 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1299 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001300 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001301
Chad Rosier2f2fe412011-11-09 03:22:02 +00001302 // Check to see if the 2nd operand is a constant that we can encode directly
1303 // in the compare.
Chad Rosier1c47de82011-11-11 06:27:41 +00001304 int Imm = 0;
1305 bool UseImm = false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001306 bool isNegativeImm = false;
1307 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1308 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1309 SrcVT == MVT::i1) {
1310 const APInt &CIVal = ConstInt->getValue();
Chad Rosier1c47de82011-11-11 06:27:41 +00001311 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1312 if (Imm < 0) {
Chad Rosier6cba97c2011-11-10 01:30:39 +00001313 isNegativeImm = true;
Chad Rosier1c47de82011-11-11 06:27:41 +00001314 Imm = -Imm;
Chad Rosier6cba97c2011-11-10 01:30:39 +00001315 }
Chad Rosier1c47de82011-11-11 06:27:41 +00001316 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1317 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001318 }
1319 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1320 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1321 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosier1c47de82011-11-11 06:27:41 +00001322 UseImm = true;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001323 }
1324
Eric Christopherd43393a2010-09-08 23:13:45 +00001325 unsigned CmpOpc;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001326 bool isICmp = true;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001327 bool needsExt = false;
1328 switch (SrcVT.getSimpleVT().SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001329 default: return false;
1330 // TODO: Verify compares.
1331 case MVT::f32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001332 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001333 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherd43393a2010-09-08 23:13:45 +00001334 break;
1335 case MVT::f64:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001336 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001337 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherd43393a2010-09-08 23:13:45 +00001338 break;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001339 case MVT::i1:
1340 case MVT::i8:
1341 case MVT::i16:
1342 needsExt = true;
1343 // Intentional fall-through.
Eric Christopherd43393a2010-09-08 23:13:45 +00001344 case MVT::i32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001345 if (isThumb2) {
Chad Rosier1c47de82011-11-11 06:27:41 +00001346 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001347 CmpOpc = ARM::t2CMPrr;
1348 else
1349 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1350 } else {
Chad Rosier1c47de82011-11-11 06:27:41 +00001351 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001352 CmpOpc = ARM::CMPrr;
1353 else
1354 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1355 }
Eric Christopherd43393a2010-09-08 23:13:45 +00001356 break;
1357 }
1358
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001359 unsigned SrcReg1 = getRegForValue(Src1Value);
1360 if (SrcReg1 == 0) return false;
Chad Rosier530f7ce2011-10-26 22:47:55 +00001361
Chad Rosier2f2fe412011-11-09 03:22:02 +00001362 unsigned SrcReg2;
Chad Rosier1c47de82011-11-11 06:27:41 +00001363 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001364 SrcReg2 = getRegForValue(Src2Value);
1365 if (SrcReg2 == 0) return false;
1366 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001367
1368 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1369 if (needsExt) {
1370 unsigned ResultReg;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001371 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001372 if (ResultReg == 0) return false;
1373 SrcReg1 = ResultReg;
Chad Rosier1c47de82011-11-11 06:27:41 +00001374 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001375 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1376 if (ResultReg == 0) return false;
1377 SrcReg2 = ResultReg;
1378 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001379 }
Chad Rosier530f7ce2011-10-26 22:47:55 +00001380
Chad Rosier1c47de82011-11-11 06:27:41 +00001381 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1383 TII.get(CmpOpc))
1384 .addReg(SrcReg1).addReg(SrcReg2));
1385 } else {
1386 MachineInstrBuilder MIB;
1387 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1388 .addReg(SrcReg1);
1389
1390 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1391 if (isICmp)
Chad Rosier1c47de82011-11-11 06:27:41 +00001392 MIB.addImm(Imm);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001393 AddOptionalDefs(MIB);
1394 }
Chad Rosierade62002011-10-26 23:25:44 +00001395
1396 // For floating point we need to move the result to a comparison register
1397 // that we can then use for branches.
1398 if (Ty->isFloatTy() || Ty->isDoubleTy())
1399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1400 TII.get(ARM::FMSTAT)));
Chad Rosier530f7ce2011-10-26 22:47:55 +00001401 return true;
1402}
1403
1404bool ARMFastISel::SelectCmp(const Instruction *I) {
1405 const CmpInst *CI = cast<CmpInst>(I);
Chad Rosierade62002011-10-26 23:25:44 +00001406 Type *Ty = CI->getOperand(0)->getType();
Chad Rosier530f7ce2011-10-26 22:47:55 +00001407
Eric Christopher229207a2010-09-29 01:14:47 +00001408 // Get the compare predicate.
1409 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001410
Eric Christopher229207a2010-09-29 01:14:47 +00001411 // We may not handle every CC for now.
1412 if (ARMPred == ARMCC::AL) return false;
1413
Chad Rosier530f7ce2011-10-26 22:47:55 +00001414 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001415 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier530f7ce2011-10-26 22:47:55 +00001416 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001417
Eric Christopher229207a2010-09-29 01:14:47 +00001418 // Now set a register based on the comparison. Explicitly set the predicates
1419 // here.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001420 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1421 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001422 : ARM::GPRRegisterClass;
1423 unsigned DestReg = createResultReg(RC);
Chad Rosierade62002011-10-26 23:25:44 +00001424 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001425 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosierade62002011-10-26 23:25:44 +00001426 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
Chad Rosier530f7ce2011-10-26 22:47:55 +00001427 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
Eric Christopher229207a2010-09-29 01:14:47 +00001428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1429 .addReg(ZeroReg).addImm(1)
1430 .addImm(ARMPred).addReg(CondReg);
1431
Eric Christophera5b1e682010-09-17 22:28:18 +00001432 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001433 return true;
1434}
1435
Eric Christopher43b62be2010-09-27 06:02:23 +00001436bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001437 // Make sure we have VFP and that we're extending float to double.
1438 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001439
Eric Christopher46203602010-09-09 00:26:48 +00001440 Value *V = I->getOperand(0);
1441 if (!I->getType()->isDoubleTy() ||
1442 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001443
Eric Christopher46203602010-09-09 00:26:48 +00001444 unsigned Op = getRegForValue(V);
1445 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001446
Eric Christopher46203602010-09-09 00:26:48 +00001447 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001448 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001449 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001450 .addReg(Op));
1451 UpdateValueMap(I, Result);
1452 return true;
1453}
1454
Eric Christopher43b62be2010-09-27 06:02:23 +00001455bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001456 // Make sure we have VFP and that we're truncating double to float.
1457 if (!Subtarget->hasVFP2()) return false;
1458
1459 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001460 if (!(I->getType()->isFloatTy() &&
1461 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001462
1463 unsigned Op = getRegForValue(V);
1464 if (Op == 0) return false;
1465
1466 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001467 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001468 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001469 .addReg(Op));
1470 UpdateValueMap(I, Result);
1471 return true;
1472}
1473
Eric Christopher43b62be2010-09-27 06:02:23 +00001474bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001475 // Make sure we have VFP.
1476 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001477
Duncan Sands1440e8b2010-11-03 11:35:31 +00001478 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001479 Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001480 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001481 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001482
Chad Rosier463fe242011-11-03 02:04:59 +00001483 Value *Src = I->getOperand(0);
1484 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1485 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman783c6642011-05-25 19:09:45 +00001486 return false;
1487
Chad Rosier463fe242011-11-03 02:04:59 +00001488 unsigned SrcReg = getRegForValue(Src);
1489 if (SrcReg == 0) return false;
1490
1491 // Handle sign-extension.
1492 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1493 EVT DestVT = MVT::i32;
1494 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false);
1495 if (ResultReg == 0) return false;
1496 SrcReg = ResultReg;
1497 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001498
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001499 // The conversion routine works on fp-reg to fp-reg and the operand above
1500 // was an integer, move it to the fp registers if possible.
Chad Rosier463fe242011-11-03 02:04:59 +00001501 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001502 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001503
Eric Christopher9a040492010-09-09 18:54:59 +00001504 unsigned Opc;
1505 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1506 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001507 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001508
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001509 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001510 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1511 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001512 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001513 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001514 return true;
1515}
1516
Eric Christopher43b62be2010-09-27 06:02:23 +00001517bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001518 // Make sure we have VFP.
1519 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001520
Duncan Sands1440e8b2010-11-03 11:35:31 +00001521 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001522 Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001523 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001524 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001525
Eric Christopher9a040492010-09-09 18:54:59 +00001526 unsigned Op = getRegForValue(I->getOperand(0));
1527 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001528
Eric Christopher9a040492010-09-09 18:54:59 +00001529 unsigned Opc;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001530 Type *OpTy = I->getOperand(0)->getType();
Eric Christopher9a040492010-09-09 18:54:59 +00001531 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1532 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001533 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001534
Eric Christopher022b7fb2010-10-05 23:13:24 +00001535 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1536 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001537 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1538 ResultReg)
1539 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001540
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001541 // This result needs to be in an integer register, but the conversion only
1542 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001543 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001544 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001545
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001546 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001547 return true;
1548}
1549
Eric Christopher3bbd3962010-10-11 08:27:59 +00001550bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001551 MVT VT;
1552 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001553 return false;
1554
1555 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001556 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001557 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1558
1559 unsigned CondReg = getRegForValue(I->getOperand(0));
1560 if (CondReg == 0) return false;
1561 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1562 if (Op1Reg == 0) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001563
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001564 // Check to see if we can use an immediate in the conditional move.
1565 int Imm = 0;
1566 bool UseImm = false;
1567 bool isNegativeImm = false;
1568 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1569 assert (VT == MVT::i32 && "Expecting an i32.");
1570 Imm = (int)ConstInt->getValue().getZExtValue();
1571 if (Imm < 0) {
1572 isNegativeImm = true;
1573 Imm = ~Imm;
1574 }
1575 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1576 (ARM_AM::getSOImmVal(Imm) != -1);
1577 }
1578
1579 unsigned Op2Reg;
1580 if (!UseImm) {
1581 Op2Reg = getRegForValue(I->getOperand(2));
1582 if (Op2Reg == 0) return false;
1583 }
1584
1585 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001586 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001587 .addReg(CondReg).addImm(0));
1588
1589 unsigned MovCCOpc;
1590 if (!UseImm) {
1591 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1592 } else {
1593 if (!isNegativeImm) {
1594 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1595 } else {
1596 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1597 }
1598 }
Eric Christopher3bbd3962010-10-11 08:27:59 +00001599 unsigned ResultReg = createResultReg(RC);
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001600 if (!UseImm)
1601 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1602 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1603 else
1604 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1605 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001606 UpdateValueMap(I, ResultReg);
1607 return true;
1608}
1609
Eric Christopher08637852010-09-30 22:34:19 +00001610bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001611 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001612 Type *Ty = I->getType();
Eric Christopher08637852010-09-30 22:34:19 +00001613 if (!isTypeLegal(Ty, VT))
1614 return false;
1615
1616 // If we have integer div support we should have selected this automagically.
1617 // In case we have a real miss go ahead and return false and we'll pick
1618 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001619 if (Subtarget->hasDivide()) return false;
1620
Eric Christopher08637852010-09-30 22:34:19 +00001621 // Otherwise emit a libcall.
1622 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001623 if (VT == MVT::i8)
1624 LC = RTLIB::SDIV_I8;
1625 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001626 LC = RTLIB::SDIV_I16;
1627 else if (VT == MVT::i32)
1628 LC = RTLIB::SDIV_I32;
1629 else if (VT == MVT::i64)
1630 LC = RTLIB::SDIV_I64;
1631 else if (VT == MVT::i128)
1632 LC = RTLIB::SDIV_I128;
1633 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001634
Eric Christopher08637852010-09-30 22:34:19 +00001635 return ARMEmitLibcall(I, LC);
1636}
1637
Eric Christopher6a880d62010-10-11 08:37:26 +00001638bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001639 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001640 Type *Ty = I->getType();
Eric Christopher6a880d62010-10-11 08:37:26 +00001641 if (!isTypeLegal(Ty, VT))
1642 return false;
1643
1644 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1645 if (VT == MVT::i8)
1646 LC = RTLIB::SREM_I8;
1647 else if (VT == MVT::i16)
1648 LC = RTLIB::SREM_I16;
1649 else if (VT == MVT::i32)
1650 LC = RTLIB::SREM_I32;
1651 else if (VT == MVT::i64)
1652 LC = RTLIB::SREM_I64;
1653 else if (VT == MVT::i128)
1654 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001655 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001656
Eric Christopher6a880d62010-10-11 08:37:26 +00001657 return ARMEmitLibcall(I, LC);
1658}
1659
Eric Christopher43b62be2010-09-27 06:02:23 +00001660bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001661 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001662
Eric Christopherbc39b822010-09-09 00:53:57 +00001663 // We can get here in the case when we want to use NEON for our fp
1664 // operations, but can't figure out how to. Just use the vfp instructions
1665 // if we have them.
1666 // FIXME: It'd be nice to use NEON instructions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001667 Type *Ty = I->getType();
Eric Christopherbd6bf082010-09-09 01:02:03 +00001668 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1669 if (isFloat && !Subtarget->hasVFP2())
1670 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001671
Eric Christopherbc39b822010-09-09 00:53:57 +00001672 unsigned Op1 = getRegForValue(I->getOperand(0));
1673 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001674
Eric Christopherbc39b822010-09-09 00:53:57 +00001675 unsigned Op2 = getRegForValue(I->getOperand(1));
1676 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001677
Eric Christopherbc39b822010-09-09 00:53:57 +00001678 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001679 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001680 switch (ISDOpcode) {
1681 default: return false;
1682 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001683 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001684 break;
1685 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001686 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001687 break;
1688 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001689 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001690 break;
1691 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001692 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001693 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1694 TII.get(Opc), ResultReg)
1695 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001696 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001697 return true;
1698}
1699
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001700// Call Handling Code
1701
Eric Christopherfa87d662010-10-18 02:17:53 +00001702bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1703 EVT SrcVT, unsigned &ResultReg) {
1704 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1705 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001706
Eric Christopherfa87d662010-10-18 02:17:53 +00001707 if (RR != 0) {
1708 ResultReg = RR;
1709 return true;
1710 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001711 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001712}
1713
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001714// This is largely taken directly from CCAssignFnForNode - we don't support
1715// varargs in FastISel so that part has been removed.
1716// TODO: We may not support all of this.
1717CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1718 switch (CC) {
1719 default:
1720 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001721 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001722 // Ignore fastcc. Silence compiler warnings.
1723 (void)RetFastCC_ARM_APCS;
1724 (void)FastCC_ARM_APCS;
1725 // Fallthrough
1726 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001727 // Use target triple & subtarget features to do actual dispatch.
1728 if (Subtarget->isAAPCS_ABI()) {
1729 if (Subtarget->hasVFP2() &&
1730 FloatABIType == FloatABI::Hard)
1731 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1732 else
1733 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1734 } else
1735 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1736 case CallingConv::ARM_AAPCS_VFP:
1737 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1738 case CallingConv::ARM_AAPCS:
1739 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1740 case CallingConv::ARM_APCS:
1741 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1742 }
1743}
1744
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001745bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1746 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001747 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001748 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1749 SmallVectorImpl<unsigned> &RegArgs,
1750 CallingConv::ID CC,
1751 unsigned &NumBytes) {
1752 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001753 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001754 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1755
1756 // Get a count of how many bytes are to be pushed on the stack.
1757 NumBytes = CCInfo.getNextStackOffset();
1758
1759 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001760 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001761 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1762 TII.get(AdjStackDown))
1763 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001764
1765 // Process the args.
1766 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1767 CCValAssign &VA = ArgLocs[i];
1768 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001769 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001770
Eric Christopher4a2b3162011-01-27 05:44:56 +00001771 // We don't handle NEON/vector parameters yet.
1772 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001773 return false;
1774
Eric Christopherf9764fa2010-09-30 20:49:44 +00001775 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001776 switch (VA.getLocInfo()) {
1777 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001778 case CCValAssign::SExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001779 EVT DestVT = VA.getLocVT();
1780 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1781 /*isZExt*/false);
1782 assert (ResultReg != 0 && "Failed to emit a sext");
1783 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001784 break;
1785 }
Chad Rosier42536af2011-11-05 20:16:15 +00001786 case CCValAssign::AExt:
1787 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherfa87d662010-10-18 02:17:53 +00001788 case CCValAssign::ZExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001789 EVT DestVT = VA.getLocVT();
1790 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1791 /*isZExt*/true);
1792 assert (ResultReg != 0 && "Failed to emit a sext");
1793 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001794 break;
1795 }
1796 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001797 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001798 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001799 assert(BC != 0 && "Failed to emit a bitcast!");
1800 Arg = BC;
1801 ArgVT = VA.getLocVT();
1802 break;
1803 }
1804 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001805 }
1806
1807 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001808 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001809 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001810 VA.getLocReg())
Chad Rosier42536af2011-11-05 20:16:15 +00001811 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001812 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001813 } else if (VA.needsCustom()) {
1814 // TODO: We need custom lowering for vector (v2f64) args.
1815 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001816
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001817 CCValAssign &NextVA = ArgLocs[++i];
1818
1819 // TODO: Only handle register args for now.
1820 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1821
1822 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1823 TII.get(ARM::VMOVRRD), VA.getLocReg())
1824 .addReg(NextVA.getLocReg(), RegState::Define)
1825 .addReg(Arg));
1826 RegArgs.push_back(VA.getLocReg());
1827 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001828 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001829 assert(VA.isMemLoc());
1830 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001831 Address Addr;
1832 Addr.BaseType = Address::RegBase;
1833 Addr.Base.Reg = ARM::SP;
1834 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001835
Eric Christopher0d581222010-11-19 22:30:02 +00001836 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001837 }
1838 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001839 return true;
1840}
1841
Duncan Sands1440e8b2010-11-03 11:35:31 +00001842bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001843 const Instruction *I, CallingConv::ID CC,
1844 unsigned &NumBytes) {
1845 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001846 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001847 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1848 TII.get(AdjStackUp))
1849 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001850
1851 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001852 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001853 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001854 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001855 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1856
1857 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001858 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001859 // For this move we copy into two registers and then move into the
1860 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001861 EVT DestVT = RVLocs[0].getValVT();
1862 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1863 unsigned ResultReg = createResultReg(DstRC);
1864 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1865 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001866 .addReg(RVLocs[0].getLocReg())
1867 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001868
Eric Christopher3659ac22010-10-20 08:02:24 +00001869 UsedRegs.push_back(RVLocs[0].getLocReg());
1870 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001871
Eric Christopherdccd2c32010-10-11 08:38:55 +00001872 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001873 UpdateValueMap(I, ResultReg);
1874 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001875 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001876 EVT CopyVT = RVLocs[0].getValVT();
Chad Rosier0eff39f2011-11-08 00:03:32 +00001877
1878 // Special handling for extended integers.
1879 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1880 CopyVT = MVT::i32;
1881
Eric Christopher14df8822010-10-01 00:00:11 +00001882 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001883
Eric Christopher14df8822010-10-01 00:00:11 +00001884 unsigned ResultReg = createResultReg(DstRC);
1885 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1886 ResultReg).addReg(RVLocs[0].getLocReg());
1887 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001888
Eric Christopherdccd2c32010-10-11 08:38:55 +00001889 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001890 UpdateValueMap(I, ResultReg);
1891 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001892 }
1893
Eric Christopherdccd2c32010-10-11 08:38:55 +00001894 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001895}
1896
Eric Christopher4f512ef2010-10-22 01:28:00 +00001897bool ARMFastISel::SelectRet(const Instruction *I) {
1898 const ReturnInst *Ret = cast<ReturnInst>(I);
1899 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001900
Eric Christopher4f512ef2010-10-22 01:28:00 +00001901 if (!FuncInfo.CanLowerReturn)
1902 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001903
Eric Christopher4f512ef2010-10-22 01:28:00 +00001904 if (F.isVarArg())
1905 return false;
1906
1907 CallingConv::ID CC = F.getCallingConv();
1908 if (Ret->getNumOperands() > 0) {
1909 SmallVector<ISD::OutputArg, 4> Outs;
1910 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1911 Outs, TLI);
1912
1913 // Analyze operands of the call, assigning locations to each operand.
1914 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00001915 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Eric Christopher4f512ef2010-10-22 01:28:00 +00001916 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1917
1918 const Value *RV = Ret->getOperand(0);
1919 unsigned Reg = getRegForValue(RV);
1920 if (Reg == 0)
1921 return false;
1922
1923 // Only handle a single return value for now.
1924 if (ValLocs.size() != 1)
1925 return false;
1926
1927 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001928
Eric Christopher4f512ef2010-10-22 01:28:00 +00001929 // Don't bother handling odd stuff for now.
1930 if (VA.getLocInfo() != CCValAssign::Full)
1931 return false;
1932 // Only handle register returns for now.
1933 if (!VA.isRegLoc())
1934 return false;
Chad Rosierf470cbb2011-11-04 00:50:21 +00001935
1936 unsigned SrcReg = Reg + VA.getValNo();
1937 EVT RVVT = TLI.getValueType(RV->getType());
1938 EVT DestVT = VA.getValVT();
1939 // Special handling for extended integers.
1940 if (RVVT != DestVT) {
1941 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1942 return false;
1943
1944 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1945 return false;
1946
1947 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1948
1949 bool isZExt = Outs[0].Flags.isZExt();
1950 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1951 if (ResultReg == 0) return false;
1952 SrcReg = ResultReg;
1953 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001954
Eric Christopher4f512ef2010-10-22 01:28:00 +00001955 // Make the copy.
Eric Christopher4f512ef2010-10-22 01:28:00 +00001956 unsigned DstReg = VA.getLocReg();
1957 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1958 // Avoid a cross-class copy. This is very unlikely.
1959 if (!SrcRC->contains(DstReg))
1960 return false;
1961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1962 DstReg).addReg(SrcReg);
1963
1964 // Mark the register as live out of the function.
1965 MRI.addLiveOut(VA.getLocReg());
1966 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001967
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001968 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Eric Christopher4f512ef2010-10-22 01:28:00 +00001969 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1970 TII.get(RetOpc)));
1971 return true;
1972}
1973
Eric Christopher872f4a22011-02-22 01:37:10 +00001974unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1975
Eric Christopher872f4a22011-02-22 01:37:10 +00001976 // Darwin needs the r9 versions of the opcodes.
1977 bool isDarwin = Subtarget->isTargetDarwin();
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001978 if (isThumb2) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001979 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1980 } else {
1981 return isDarwin ? ARM::BLr9 : ARM::BL;
1982 }
1983}
1984
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001985// A quick function that will emit a call for a named libcall in F with the
1986// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001987// can emit a call for any libcall we can produce. This is an abridged version
1988// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001989// like computed function pointers or strange arguments at call sites.
1990// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1991// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001992bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1993 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001994
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001995 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001996 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001997 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001998 if (RetTy->isVoidTy())
1999 RetVT = MVT::isVoid;
2000 else if (!isTypeLegal(RetTy, RetVT))
2001 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002002
Eric Christopher836c6242010-12-15 23:47:29 +00002003 // TODO: For now if we have long calls specified we don't handle the call.
2004 if (EnableARMLongCalls) return false;
2005
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002006 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002007 SmallVector<Value*, 8> Args;
2008 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002009 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002010 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2011 Args.reserve(I->getNumOperands());
2012 ArgRegs.reserve(I->getNumOperands());
2013 ArgVTs.reserve(I->getNumOperands());
2014 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00002015 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002016 Value *Op = I->getOperand(i);
2017 unsigned Arg = getRegForValue(Op);
2018 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002019
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002020 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002021 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002022 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002023
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002024 ISD::ArgFlagsTy Flags;
2025 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2026 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002027
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002028 Args.push_back(Op);
2029 ArgRegs.push_back(Arg);
2030 ArgVTs.push_back(ArgVT);
2031 ArgFlags.push_back(Flags);
2032 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002033
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002034 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002035 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002036 unsigned NumBytes;
2037 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2038 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002039
Eric Christopher6344a5f2011-04-29 00:07:20 +00002040 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002041 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002042 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002043 unsigned CallOpc = ARMSelectCallOp(NULL);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002044 if(isThumb2)
Eric Christopherc19aadb2010-12-21 03:50:43 +00002045 // Explicitly adding the predicate here.
2046 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2047 TII.get(CallOpc)))
2048 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00002049 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00002050 // Explicitly adding the predicate here.
2051 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2052 TII.get(CallOpc))
2053 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00002054
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002055 // Add implicit physical register uses to the call.
2056 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2057 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002058
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002059 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002060 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002061 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002062
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002063 // Set all unused physreg defs as dead.
2064 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002065
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002066 return true;
2067}
2068
Chad Rosier11add262011-11-11 23:31:03 +00002069bool ARMFastISel::SelectCall(const Instruction *I,
2070 const char *IntrMemName = 0) {
Eric Christopherf9764fa2010-09-30 20:49:44 +00002071 const CallInst *CI = cast<CallInst>(I);
2072 const Value *Callee = CI->getCalledValue();
2073
Chad Rosier11add262011-11-11 23:31:03 +00002074 // Can't handle inline asm.
2075 if (isa<InlineAsm>(Callee)) return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002076
Eric Christopher52f6c032011-05-02 20:16:33 +00002077 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002078 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00002079 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00002080 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002081
Eric Christopherf9764fa2010-09-30 20:49:44 +00002082 // Check the calling convention.
2083 ImmutableCallSite CS(CI);
2084 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00002085
Eric Christopherf9764fa2010-09-30 20:49:44 +00002086 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00002087
Eric Christopherf9764fa2010-09-30 20:49:44 +00002088 // Let SDISel handle vararg functions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002089 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2090 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eric Christopherf9764fa2010-09-30 20:49:44 +00002091 if (FTy->isVarArg())
2092 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002093
Eric Christopherf9764fa2010-09-30 20:49:44 +00002094 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002095 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002096 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002097 if (RetTy->isVoidTy())
2098 RetVT = MVT::isVoid;
Chad Rosier0eff39f2011-11-08 00:03:32 +00002099 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2100 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002101 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002102
Eric Christopher836c6242010-12-15 23:47:29 +00002103 // TODO: For now if we have long calls specified we don't handle the call.
2104 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00002105
Eric Christopherf9764fa2010-09-30 20:49:44 +00002106 // Set up the argument vectors.
2107 SmallVector<Value*, 8> Args;
2108 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002109 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002110 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2111 Args.reserve(CS.arg_size());
2112 ArgRegs.reserve(CS.arg_size());
2113 ArgVTs.reserve(CS.arg_size());
2114 ArgFlags.reserve(CS.arg_size());
2115 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2116 i != e; ++i) {
Chad Rosier11add262011-11-11 23:31:03 +00002117 // If we're lowering a memory intrinsic instead of a regular call, skip the
2118 // last two arguments, which shouldn't be passed to the underlying function.
2119 if (IntrMemName && e-i <= 2)
2120 break;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002121
Chad Rosier11add262011-11-11 23:31:03 +00002122 unsigned Arg = getRegForValue(*i);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002123 if (Arg == 0)
2124 return false;
2125 ISD::ArgFlagsTy Flags;
2126 unsigned AttrInd = i - CS.arg_begin() + 1;
2127 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2128 Flags.setSExt();
2129 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2130 Flags.setZExt();
2131
Chad Rosier8e4a2e42011-11-04 00:58:10 +00002132 // FIXME: Only handle *easy* calls for now.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002133 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2134 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2135 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2136 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2137 return false;
2138
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002139 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002140 MVT ArgVT;
Chad Rosier42536af2011-11-05 20:16:15 +00002141 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2142 ArgVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002143 return false;
2144 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2145 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002146
Eric Christopherf9764fa2010-09-30 20:49:44 +00002147 Args.push_back(*i);
2148 ArgRegs.push_back(Arg);
2149 ArgVTs.push_back(ArgVT);
2150 ArgFlags.push_back(Flags);
2151 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002152
Eric Christopherf9764fa2010-09-30 20:49:44 +00002153 // Handle the arguments now that we've gotten them.
2154 SmallVector<unsigned, 4> RegArgs;
2155 unsigned NumBytes;
2156 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2157 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002158
Eric Christopher6344a5f2011-04-29 00:07:20 +00002159 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002160 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002161 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002162 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00002163 // Explicitly adding the predicate here.
Chad Rosier9eb67482011-11-13 09:44:21 +00002164 if(isThumb2) {
Eric Christopherc19aadb2010-12-21 03:50:43 +00002165 // Explicitly adding the predicate here.
2166 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier11add262011-11-11 23:31:03 +00002167 TII.get(CallOpc)));
Chad Rosier9eb67482011-11-13 09:44:21 +00002168 if (!IntrMemName)
2169 MIB.addGlobalAddress(GV, 0, 0);
2170 else
2171 MIB.addExternalSymbol(IntrMemName, 0);
2172 } else {
2173 if (!IntrMemName)
2174 // Explicitly adding the predicate here.
2175 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2176 TII.get(CallOpc))
2177 .addGlobalAddress(GV, 0, 0));
2178 else
2179 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2180 TII.get(CallOpc))
2181 .addExternalSymbol(IntrMemName, 0));
2182 }
Chad Rosier11add262011-11-11 23:31:03 +00002183
Eric Christopherf9764fa2010-09-30 20:49:44 +00002184 // Add implicit physical register uses to the call.
2185 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2186 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002187
Eric Christopherf9764fa2010-09-30 20:49:44 +00002188 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002189 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002190 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002191
Eric Christopherf9764fa2010-09-30 20:49:44 +00002192 // Set all unused physreg defs as dead.
2193 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002194
Eric Christopherf9764fa2010-09-30 20:49:44 +00002195 return true;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002196}
2197
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002198bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002199 return Len <= 16;
2200}
2201
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002202bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002203 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002204 if (!ARMIsMemCpySmall(Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002205 return false;
2206
2207 // We don't care about alignment here since we just emit integer accesses.
2208 while (Len) {
2209 MVT VT;
2210 if (Len >= 4)
2211 VT = MVT::i32;
2212 else if (Len >= 2)
2213 VT = MVT::i16;
2214 else {
2215 assert(Len == 1);
2216 VT = MVT::i8;
2217 }
2218
2219 bool RV;
2220 unsigned ResultReg;
2221 RV = ARMEmitLoad(VT, ResultReg, Src);
2222 assert (RV = true && "Should be able to handle this load.");
2223 RV = ARMEmitStore(VT, ResultReg, Dest);
2224 assert (RV = true && "Should be able to handle this store.");
2225
2226 unsigned Size = VT.getSizeInBits()/8;
2227 Len -= Size;
2228 Dest.Offset += Size;
2229 Src.Offset += Size;
2230 }
2231
2232 return true;
2233}
2234
Chad Rosier11add262011-11-11 23:31:03 +00002235bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2236 // FIXME: Handle more intrinsics.
2237 switch (I.getIntrinsicID()) {
2238 default: return false;
2239 case Intrinsic::memcpy:
2240 case Intrinsic::memmove: {
Chad Rosier11add262011-11-11 23:31:03 +00002241 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2242 // Don't handle volatile.
2243 if (MTI.isVolatile())
2244 return false;
Chad Rosier909cb4f2011-11-14 22:46:17 +00002245
2246 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2247 // we would emit dead code because we don't currently handle memmoves.
2248 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2249 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002250 // Small memcpy's are common enough that we want to do them without a call
2251 // if possible.
Chad Rosier909cb4f2011-11-14 22:46:17 +00002252 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002253 if (ARMIsMemCpySmall(Len)) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002254 Address Dest, Src;
2255 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2256 !ARMComputeAddress(MTI.getRawSource(), Src))
2257 return false;
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002258 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002259 return true;
2260 }
2261 }
Chad Rosier11add262011-11-11 23:31:03 +00002262
2263 if (!MTI.getLength()->getType()->isIntegerTy(32))
2264 return false;
2265
2266 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2267 return false;
2268
2269 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2270 return SelectCall(&I, IntrMemName);
2271 }
2272 case Intrinsic::memset: {
2273 const MemSetInst &MSI = cast<MemSetInst>(I);
2274 // Don't handle volatile.
2275 if (MSI.isVolatile())
2276 return false;
2277
2278 if (!MSI.getLength()->getType()->isIntegerTy(32))
2279 return false;
2280
2281 if (MSI.getDestAddressSpace() > 255)
2282 return false;
2283
2284 return SelectCall(&I, "memset");
2285 }
2286 }
2287 return false;
2288}
2289
Chad Rosier0d7b2312011-11-02 00:18:48 +00002290bool ARMFastISel::SelectTrunc(const Instruction *I) {
2291 // The high bits for a type smaller than the register size are assumed to be
2292 // undefined.
2293 Value *Op = I->getOperand(0);
2294
2295 EVT SrcVT, DestVT;
2296 SrcVT = TLI.getValueType(Op->getType(), true);
2297 DestVT = TLI.getValueType(I->getType(), true);
2298
2299 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2300 return false;
2301 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2302 return false;
2303
2304 unsigned SrcReg = getRegForValue(Op);
2305 if (!SrcReg) return false;
2306
2307 // Because the high bits are undefined, a truncate doesn't generate
2308 // any code.
2309 UpdateValueMap(I, SrcReg);
2310 return true;
2311}
2312
Chad Rosier87633022011-11-02 17:20:24 +00002313unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2314 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002315 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00002316 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002317
2318 unsigned Opc;
Eli Friedman76927d732011-05-25 23:49:02 +00002319 bool isBoolZext = false;
Chad Rosier87633022011-11-02 17:20:24 +00002320 if (!SrcVT.isSimple()) return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002321 switch (SrcVT.getSimpleVT().SimpleTy) {
Chad Rosier87633022011-11-02 17:20:24 +00002322 default: return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002323 case MVT::i16:
Chad Rosier87633022011-11-02 17:20:24 +00002324 if (!Subtarget->hasV6Ops()) return 0;
2325 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002326 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002327 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002328 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002329 break;
2330 case MVT::i8:
Chad Rosier87633022011-11-02 17:20:24 +00002331 if (!Subtarget->hasV6Ops()) return 0;
2332 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002333 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002334 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002335 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002336 break;
2337 case MVT::i1:
Chad Rosier87633022011-11-02 17:20:24 +00002338 if (isZExt) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002339 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eli Friedman76927d732011-05-25 23:49:02 +00002340 isBoolZext = true;
2341 break;
2342 }
Chad Rosier87633022011-11-02 17:20:24 +00002343 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002344 }
2345
Chad Rosier87633022011-11-02 17:20:24 +00002346 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eli Friedman76927d732011-05-25 23:49:02 +00002347 MachineInstrBuilder MIB;
Chad Rosier87633022011-11-02 17:20:24 +00002348 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
Eli Friedman76927d732011-05-25 23:49:02 +00002349 .addReg(SrcReg);
2350 if (isBoolZext)
2351 MIB.addImm(1);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002352 else
2353 MIB.addImm(0);
Eli Friedman76927d732011-05-25 23:49:02 +00002354 AddOptionalDefs(MIB);
Chad Rosier87633022011-11-02 17:20:24 +00002355 return ResultReg;
2356}
2357
2358bool ARMFastISel::SelectIntExt(const Instruction *I) {
2359 // On ARM, in general, integer casts don't involve legal types; this code
2360 // handles promotable integers.
Chad Rosier87633022011-11-02 17:20:24 +00002361 Type *DestTy = I->getType();
2362 Value *Src = I->getOperand(0);
2363 Type *SrcTy = Src->getType();
2364
2365 EVT SrcVT, DestVT;
2366 SrcVT = TLI.getValueType(SrcTy, true);
2367 DestVT = TLI.getValueType(DestTy, true);
2368
2369 bool isZExt = isa<ZExtInst>(I);
2370 unsigned SrcReg = getRegForValue(Src);
2371 if (!SrcReg) return false;
2372
2373 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2374 if (ResultReg == 0) return false;
2375 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002376 return true;
2377}
2378
Eric Christopher56d2b722010-09-02 23:43:26 +00002379// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002380bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002381
Eric Christopherab695882010-07-21 22:26:11 +00002382 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002383 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002384 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002385 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002386 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002387 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002388 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002389 case Instruction::ICmp:
2390 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002391 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002392 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002393 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002394 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002395 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002396 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00002397 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002398 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00002399 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00002400 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00002401 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002402 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00002403 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002404 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00002405 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002406 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00002407 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00002408 case Instruction::SRem:
2409 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002410 case Instruction::Call:
Chad Rosier11add262011-11-11 23:31:03 +00002411 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2412 return SelectIntrinsicCall(*II);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002413 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002414 case Instruction::Select:
2415 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002416 case Instruction::Ret:
2417 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002418 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002419 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002420 case Instruction::ZExt:
2421 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002422 return SelectIntExt(I);
Eric Christopherab695882010-07-21 22:26:11 +00002423 default: break;
2424 }
2425 return false;
2426}
2427
Chad Rosierb29b9502011-11-13 02:23:59 +00002428/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2429/// vreg is being provided by the specified load instruction. If possible,
2430/// try to fold the load as an operand to the instruction, returning true if
2431/// successful.
2432bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2433 const LoadInst *LI) {
2434 // Verify we have a legal type before going any further.
2435 MVT VT;
2436 if (!isLoadTypeLegal(LI->getType(), VT))
2437 return false;
2438
2439 // Combine load followed by zero- or sign-extend.
2440 // ldrb r1, [r0] ldrb r1, [r0]
2441 // uxtb r2, r1 =>
2442 // mov r3, r2 mov r3, r1
2443 bool isZExt = true;
2444 switch(MI->getOpcode()) {
2445 default: return false;
2446 case ARM::SXTH:
2447 case ARM::t2SXTH:
2448 isZExt = false;
2449 case ARM::UXTH:
2450 case ARM::t2UXTH:
2451 if (VT != MVT::i16)
2452 return false;
2453 break;
2454 case ARM::SXTB:
2455 case ARM::t2SXTB:
2456 isZExt = false;
2457 case ARM::UXTB:
2458 case ARM::t2UXTB:
2459 if (VT != MVT::i8)
2460 return false;
2461 break;
2462 }
2463 // See if we can handle this address.
2464 Address Addr;
2465 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2466
2467 unsigned ResultReg = MI->getOperand(0).getReg();
2468 if (!ARMEmitLoad(VT, ResultReg, Addr, isZExt, false))
2469 return false;
2470 MI->eraseFromParent();
2471 return true;
2472}
2473
Eric Christopherab695882010-07-21 22:26:11 +00002474namespace llvm {
2475 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00002476 // Completely untested on non-darwin.
2477 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002478
Eric Christopheraaa8df42010-11-02 01:21:28 +00002479 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00002480 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002481 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00002482 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00002483 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00002484 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002485 }
2486}