blob: cc6a3d4c4d59ef0897c4a8dd59ee89b7b0fbcebc [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"
40#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000041#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000042#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000044#include "llvm/Target/TargetData.h"
45#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000048#include "llvm/Target/TargetOptions.h"
49using namespace llvm;
50
Eric Christopher038fea52010-08-17 00:46:57 +000051static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000052DisableARMFastISel("disable-arm-fast-isel",
53 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000054 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000055
Eric Christopher836c6242010-12-15 23:47:29 +000056extern cl::opt<bool> EnableARMLongCalls;
57
Eric Christopherab695882010-07-21 22:26:11 +000058namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000059
Eric Christopher0d581222010-11-19 22:30:02 +000060 // All possible address modes, plus some.
61 typedef struct Address {
62 enum {
63 RegBase,
64 FrameIndexBase
65 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000066
Eric Christopher0d581222010-11-19 22:30:02 +000067 union {
68 unsigned Reg;
69 int FI;
70 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000071
Eric Christopher0d581222010-11-19 22:30:02 +000072 int Offset;
Eric Christopher827656d2010-11-20 22:38:27 +000073
Eric Christopher0d581222010-11-19 22:30:02 +000074 // Innocuous defaults for our address.
75 Address()
Jim Grosbach0c720762011-05-16 22:24:07 +000076 : BaseType(RegBase), Offset(0) {
Eric Christopher0d581222010-11-19 22:30:02 +000077 Base.Reg = 0;
78 }
79 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000080
81class ARMFastISel : public FastISel {
82
83 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
84 /// make the right decision when generating code for different targets.
85 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000086 const TargetMachine &TM;
87 const TargetInstrInfo &TII;
88 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000089 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000090
Eric Christopher8cf6c602010-09-29 22:24:45 +000091 // Convenience variables to avoid some queries.
Chad Rosier66dc8ca2011-11-08 21:12:00 +000092 bool isThumb2;
Eric Christopher8cf6c602010-09-29 22:24:45 +000093 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000094
Eric Christopherab695882010-07-21 22:26:11 +000095 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000096 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000097 : FastISel(funcInfo),
98 TM(funcInfo.MF->getTarget()),
99 TII(*TM.getInstrInfo()),
100 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000101 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000102 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000103 isThumb2 = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000104 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000105 }
106
Eric Christophercb592292010-08-20 00:20:31 +0000107 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000108 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
109 const TargetRegisterClass *RC);
110 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC,
112 unsigned Op0, bool Op0IsKill);
113 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
114 const TargetRegisterClass *RC,
115 unsigned Op0, bool Op0IsKill,
116 unsigned Op1, bool Op1IsKill);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000117 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
118 const TargetRegisterClass *RC,
119 unsigned Op0, bool Op0IsKill,
120 unsigned Op1, bool Op1IsKill,
121 unsigned Op2, bool Op2IsKill);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000122 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 uint64_t Imm);
126 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000130 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
131 const TargetRegisterClass *RC,
132 unsigned Op0, bool Op0IsKill,
133 unsigned Op1, bool Op1IsKill,
134 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000135 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
136 const TargetRegisterClass *RC,
137 uint64_t Imm);
Eric Christopherd94bc542011-04-29 22:07:50 +0000138 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
139 const TargetRegisterClass *RC,
140 uint64_t Imm1, uint64_t Imm2);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000141
Eric Christopher0fe7d542010-08-17 01:25:29 +0000142 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
143 unsigned Op0, bool Op0IsKill,
144 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000145
Eric Christophercb592292010-08-20 00:20:31 +0000146 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000147 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000148 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000149 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Chad Rosierb29b9502011-11-13 02:23:59 +0000150 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
151 const LoadInst *LI);
Eric Christopherab695882010-07-21 22:26:11 +0000152
153 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000154
Eric Christopher83007122010-08-23 21:44:12 +0000155 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000156 private:
Eric Christopher17787722010-10-21 21:47:51 +0000157 bool SelectLoad(const Instruction *I);
158 bool SelectStore(const Instruction *I);
159 bool SelectBranch(const Instruction *I);
160 bool SelectCmp(const Instruction *I);
161 bool SelectFPExt(const Instruction *I);
162 bool SelectFPTrunc(const Instruction *I);
163 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
164 bool SelectSIToFP(const Instruction *I);
165 bool SelectFPToSI(const Instruction *I);
166 bool SelectSDiv(const Instruction *I);
167 bool SelectSRem(const Instruction *I);
Chad Rosier11add262011-11-11 23:31:03 +0000168 bool SelectCall(const Instruction *I, const char *IntrMemName);
169 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher17787722010-10-21 21:47:51 +0000170 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000171 bool SelectRet(const Instruction *I);
Chad Rosier0d7b2312011-11-02 00:18:48 +0000172 bool SelectTrunc(const Instruction *I);
173 bool SelectIntExt(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000174
Eric Christopher83007122010-08-23 21:44:12 +0000175 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000176 private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000177 bool isTypeLegal(Type *Ty, MVT &VT);
178 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosiere07cd5e2011-11-02 18:08:25 +0000179 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
180 bool isZExt);
Chad Rosierb29b9502011-11-13 02:23:59 +0000181 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, bool isZExt,
182 bool allocReg);
183
Eric Christopher0d581222010-11-19 22:30:02 +0000184 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
185 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosierb29b9502011-11-13 02:23:59 +0000186 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
Chad Rosier2c42b8c2011-11-14 23:04:09 +0000187 bool ARMIsMemCpySmall(uint64_t Len);
188 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
Chad Rosier87633022011-11-02 17:20:24 +0000189 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000190 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000191 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000192 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000193 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000194 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000195 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000196
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000197 // Call handling routines.
198 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000199 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
200 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000201 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000202 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000203 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000204 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000205 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
206 SmallVectorImpl<unsigned> &RegArgs,
207 CallingConv::ID CC,
208 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000209 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000210 const Instruction *I, CallingConv::ID CC,
211 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000212 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000213
214 // OptionalDef handling routines.
215 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000216 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000217 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
218 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000219 void AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000220 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000221 unsigned Flags, bool useAM3);
Eric Christopher456144e2010-08-19 00:37:05 +0000222};
Eric Christopherab695882010-07-21 22:26:11 +0000223
224} // end anonymous namespace
225
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000226#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000227
Eric Christopher456144e2010-08-19 00:37:05 +0000228// DefinesOptionalPredicate - This is different from DefinesPredicate in that
229// we don't care about implicit defs here, just places we'll need to add a
230// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
231bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Chenge837dea2011-06-28 19:10:37 +0000232 const MCInstrDesc &MCID = MI->getDesc();
233 if (!MCID.hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000234 return false;
235
236 // Look to see if our OptionalDef is defining CPSR or CCR.
237 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
238 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000239 if (!MO.isReg() || !MO.isDef()) continue;
240 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000241 *CPSR = true;
242 }
243 return true;
244}
245
Eric Christopheraf3dce52011-03-12 01:09:29 +0000246bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000247 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000248
Eric Christopheraf3dce52011-03-12 01:09:29 +0000249 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000250 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000251 AFI->isThumb2Function())
252 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000253
Evan Chenge837dea2011-06-28 19:10:37 +0000254 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
255 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000256 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000257
Eric Christopheraf3dce52011-03-12 01:09:29 +0000258 return false;
259}
260
Eric Christopher456144e2010-08-19 00:37:05 +0000261// If the machine is predicable go ahead and add the predicate operands, if
262// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000263// TODO: If we want to support thumb1 then we'll need to deal with optional
264// CPSR defs that need to be added before the remaining operands. See s_cc_out
265// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000266const MachineInstrBuilder &
267ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
268 MachineInstr *MI = &*MIB;
269
Eric Christopheraf3dce52011-03-12 01:09:29 +0000270 // Do we use a predicate? or...
271 // Are we NEON in ARM mode and have a predicate operand? If so, I know
272 // we're not predicable but add it anyways.
273 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000274 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000275
Eric Christopher456144e2010-08-19 00:37:05 +0000276 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
277 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000278 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000279 if (DefinesOptionalPredicate(MI, &CPSR)) {
280 if (CPSR)
281 AddDefaultT1CC(MIB);
282 else
283 AddDefaultCC(MIB);
284 }
285 return MIB;
286}
287
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
289 const TargetRegisterClass* RC) {
290 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000291 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000292
Eric Christopher456144e2010-08-19 00:37:05 +0000293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000294 return ResultReg;
295}
296
297unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
298 const TargetRegisterClass *RC,
299 unsigned Op0, bool Op0IsKill) {
300 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000301 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302
303 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addReg(Op0, Op0IsKill * RegState::Kill));
306 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 TII.get(TargetOpcode::COPY), ResultReg)
311 .addReg(II.ImplicitDefs[0]));
312 }
313 return ResultReg;
314}
315
316unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
317 const TargetRegisterClass *RC,
318 unsigned Op0, bool Op0IsKill,
319 unsigned Op1, bool Op1IsKill) {
320 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000321 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000322
323 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill));
327 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000331 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000332 TII.get(TargetOpcode::COPY), ResultReg)
333 .addReg(II.ImplicitDefs[0]));
334 }
335 return ResultReg;
336}
337
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000338unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
339 const TargetRegisterClass *RC,
340 unsigned Op0, bool Op0IsKill,
341 unsigned Op1, bool Op1IsKill,
342 unsigned Op2, bool Op2IsKill) {
343 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000344 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000345
346 if (II.getNumDefs() >= 1)
347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
348 .addReg(Op0, Op0IsKill * RegState::Kill)
349 .addReg(Op1, Op1IsKill * RegState::Kill)
350 .addReg(Op2, Op2IsKill * RegState::Kill));
351 else {
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
353 .addReg(Op0, Op0IsKill * RegState::Kill)
354 .addReg(Op1, Op1IsKill * RegState::Kill)
355 .addReg(Op2, Op2IsKill * RegState::Kill));
356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
357 TII.get(TargetOpcode::COPY), ResultReg)
358 .addReg(II.ImplicitDefs[0]));
359 }
360 return ResultReg;
361}
362
Eric Christopher0fe7d542010-08-17 01:25:29 +0000363unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
364 const TargetRegisterClass *RC,
365 unsigned Op0, bool Op0IsKill,
366 uint64_t Imm) {
367 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000368 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000369
370 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000372 .addReg(Op0, Op0IsKill * RegState::Kill)
373 .addImm(Imm));
374 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000375 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000376 .addReg(Op0, Op0IsKill * RegState::Kill)
377 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000378 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000379 TII.get(TargetOpcode::COPY), ResultReg)
380 .addReg(II.ImplicitDefs[0]));
381 }
382 return ResultReg;
383}
384
385unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
386 const TargetRegisterClass *RC,
387 unsigned Op0, bool Op0IsKill,
388 const ConstantFP *FPImm) {
389 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000390 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000391
392 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000393 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000394 .addReg(Op0, Op0IsKill * RegState::Kill)
395 .addFPImm(FPImm));
396 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000398 .addReg(Op0, Op0IsKill * RegState::Kill)
399 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000401 TII.get(TargetOpcode::COPY), ResultReg)
402 .addReg(II.ImplicitDefs[0]));
403 }
404 return ResultReg;
405}
406
407unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
408 const TargetRegisterClass *RC,
409 unsigned Op0, bool Op0IsKill,
410 unsigned Op1, bool Op1IsKill,
411 uint64_t Imm) {
412 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000413 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000414
415 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000416 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000417 .addReg(Op0, Op0IsKill * RegState::Kill)
418 .addReg(Op1, Op1IsKill * RegState::Kill)
419 .addImm(Imm));
420 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000421 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000422 .addReg(Op0, Op0IsKill * RegState::Kill)
423 .addReg(Op1, Op1IsKill * RegState::Kill)
424 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000425 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000426 TII.get(TargetOpcode::COPY), ResultReg)
427 .addReg(II.ImplicitDefs[0]));
428 }
429 return ResultReg;
430}
431
432unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
433 const TargetRegisterClass *RC,
434 uint64_t Imm) {
435 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000436 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000437
Eric Christopher0fe7d542010-08-17 01:25:29 +0000438 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000440 .addImm(Imm));
441 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000443 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000445 TII.get(TargetOpcode::COPY), ResultReg)
446 .addReg(II.ImplicitDefs[0]));
447 }
448 return ResultReg;
449}
450
Eric Christopherd94bc542011-04-29 22:07:50 +0000451unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
452 const TargetRegisterClass *RC,
453 uint64_t Imm1, uint64_t Imm2) {
454 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000455 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000456
Eric Christopherd94bc542011-04-29 22:07:50 +0000457 if (II.getNumDefs() >= 1)
458 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
459 .addImm(Imm1).addImm(Imm2));
460 else {
461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
462 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000463 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000464 TII.get(TargetOpcode::COPY),
465 ResultReg)
466 .addReg(II.ImplicitDefs[0]));
467 }
468 return ResultReg;
469}
470
Eric Christopher0fe7d542010-08-17 01:25:29 +0000471unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
472 unsigned Op0, bool Op0IsKill,
473 uint32_t Idx) {
474 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
475 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
476 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000477 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000478 DL, TII.get(TargetOpcode::COPY), ResultReg)
479 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
480 return ResultReg;
481}
482
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000483// TODO: Don't worry about 64-bit now, but when this is fixed remove the
484// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000485unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000486 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000487
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000488 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
489 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
490 TII.get(ARM::VMOVRS), MoveReg)
491 .addReg(SrcReg));
492 return MoveReg;
493}
494
495unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000496 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000497
Eric Christopheraa3ace12010-09-09 20:49:25 +0000498 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
499 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000500 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000501 .addReg(SrcReg));
502 return MoveReg;
503}
504
Eric Christopher9ed58df2010-09-09 00:19:41 +0000505// For double width floating point we need to materialize two constants
506// (the high and the low) into integer registers then use a move to get
507// the combined constant into an FP reg.
508unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
509 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000510 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000511
Eric Christopher9ed58df2010-09-09 00:19:41 +0000512 // This checks to see if we can use VFP3 instructions to materialize
513 // a constant, otherwise we have to go through the constant pool.
514 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000515 int Imm;
516 unsigned Opc;
517 if (is64bit) {
518 Imm = ARM_AM::getFP64Imm(Val);
519 Opc = ARM::FCONSTD;
520 } else {
521 Imm = ARM_AM::getFP32Imm(Val);
522 Opc = ARM::FCONSTS;
523 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000524 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
525 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
526 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000527 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000528 return DestReg;
529 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000530
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000531 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000532 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000533
Eric Christopher238bb162010-09-09 23:50:00 +0000534 // MachineConstantPool wants an explicit alignment.
535 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
536 if (Align == 0) {
537 // TODO: Figure out if this is correct.
538 Align = TD.getTypeAllocSize(CFP->getType());
539 }
540 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
541 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
542 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000543
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000544 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000545 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
546 DestReg)
547 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000548 .addReg(0));
549 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000550}
551
Eric Christopher744c7c82010-09-28 22:47:54 +0000552unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000553
Chad Rosier44e89572011-11-04 22:29:00 +0000554 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
555 return false;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000556
557 // If we can do this in a single instruction without a constant pool entry
558 // do so now.
559 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiera4e07272011-11-04 23:09:49 +0000560 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000561 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier4e89d972011-11-11 00:36:21 +0000562 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000563 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier44e89572011-11-04 22:29:00 +0000564 TII.get(Opc), ImmReg)
Chad Rosier42536af2011-11-05 20:16:15 +0000565 .addImm(CI->getZExtValue()));
Chad Rosier44e89572011-11-04 22:29:00 +0000566 return ImmReg;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000567 }
568
Chad Rosier4e89d972011-11-11 00:36:21 +0000569 // Use MVN to emit negative constants.
570 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
571 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosier1c47de82011-11-11 06:27:41 +0000572 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier4e89d972011-11-11 00:36:21 +0000573 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier1c47de82011-11-11 06:27:41 +0000574 if (UseImm) {
Chad Rosier4e89d972011-11-11 00:36:21 +0000575 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
576 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
577 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
578 TII.get(Opc), ImmReg)
579 .addImm(Imm));
580 return ImmReg;
581 }
582 }
583
584 // Load from constant pool. For now 32-bit only.
Chad Rosier44e89572011-11-04 22:29:00 +0000585 if (VT != MVT::i32)
586 return false;
587
588 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
589
Eric Christopher56d2b722010-09-02 23:43:26 +0000590 // MachineConstantPool wants an explicit alignment.
591 unsigned Align = TD.getPrefTypeAlignment(C->getType());
592 if (Align == 0) {
593 // TODO: Figure out if this is correct.
594 Align = TD.getTypeAllocSize(C->getType());
595 }
596 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000597
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000598 if (isThumb2)
Eric Christopher56d2b722010-09-02 23:43:26 +0000599 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000600 TII.get(ARM::t2LDRpci), DestReg)
601 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000602 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000603 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000604 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000605 TII.get(ARM::LDRcp), DestReg)
606 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000607 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000608
Eric Christopher56d2b722010-09-02 23:43:26 +0000609 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000610}
611
Eric Christopherc9932f62010-10-01 23:24:42 +0000612unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000613 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000614 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000615
Eric Christopher890dbbe2010-10-02 00:32:44 +0000616 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000617
Eric Christopher890dbbe2010-10-02 00:32:44 +0000618 // TODO: Need more magic for ARM PIC.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000619 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000620
Eric Christopher890dbbe2010-10-02 00:32:44 +0000621 // MachineConstantPool wants an explicit alignment.
622 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
623 if (Align == 0) {
624 // TODO: Figure out if this is correct.
625 Align = TD.getTypeAllocSize(GV->getType());
626 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000627
Eric Christopher890dbbe2010-10-02 00:32:44 +0000628 // Grab index.
629 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000630 unsigned Id = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +0000631 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
632 ARMCP::CPValue,
633 PCAdj);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000634 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000635
Eric Christopher890dbbe2010-10-02 00:32:44 +0000636 // Load value.
637 MachineInstrBuilder MIB;
638 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000639 if (isThumb2) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000640 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
641 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
642 .addConstantPoolIndex(Idx);
643 if (RelocM == Reloc::PIC_)
644 MIB.addImm(Id);
645 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000646 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000647 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
648 DestReg)
649 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000650 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000651 }
652 AddOptionalDefs(MIB);
Eli Friedmand6412c92011-06-03 01:13:19 +0000653
654 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
655 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000656 if (isThumb2)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000657 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
658 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000659 .addReg(DestReg)
660 .addImm(0);
661 else
662 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
663 NewDestReg)
664 .addReg(DestReg)
665 .addImm(0);
666 DestReg = NewDestReg;
667 AddOptionalDefs(MIB);
668 }
669
Eric Christopher890dbbe2010-10-02 00:32:44 +0000670 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000671}
672
Eric Christopher9ed58df2010-09-09 00:19:41 +0000673unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
674 EVT VT = TLI.getValueType(C->getType(), true);
675
676 // Only handle simple types.
677 if (!VT.isSimple()) return 0;
678
679 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
680 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000681 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
682 return ARMMaterializeGV(GV, VT);
683 else if (isa<ConstantInt>(C))
684 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000685
Eric Christopherc9932f62010-10-01 23:24:42 +0000686 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000687}
688
Eric Christopherf9764fa2010-09-30 20:49:44 +0000689unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
690 // Don't handle dynamic allocas.
691 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000692
Duncan Sands1440e8b2010-11-03 11:35:31 +0000693 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000694 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000695
Eric Christopherf9764fa2010-09-30 20:49:44 +0000696 DenseMap<const AllocaInst*, int>::iterator SI =
697 FuncInfo.StaticAllocaMap.find(AI);
698
699 // This will get lowered later into the correct offsets and registers
700 // via rewriteXFrameIndex.
701 if (SI != FuncInfo.StaticAllocaMap.end()) {
702 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
703 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000704 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopherf9764fa2010-09-30 20:49:44 +0000705 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
706 TII.get(Opc), ResultReg)
707 .addFrameIndex(SI->second)
708 .addImm(0));
709 return ResultReg;
710 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000711
Eric Christopherf9764fa2010-09-30 20:49:44 +0000712 return 0;
713}
714
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000715bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000716 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000717
Eric Christopherb1cc8482010-08-25 07:23:49 +0000718 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000719 if (evt == MVT::Other || !evt.isSimple()) return false;
720 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000721
Eric Christopherdc908042010-08-31 01:28:42 +0000722 // Handle all legal types, i.e. a register that will directly hold this
723 // value.
724 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000725}
726
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000727bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000728 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000729
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000730 // If this is a type than can be sign or zero-extended to a basic operation
731 // go ahead and accept it now.
Chad Rosierb29b9502011-11-13 02:23:59 +0000732 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000733 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000734
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000735 return false;
736}
737
Eric Christopher88de86b2010-11-19 22:36:41 +0000738// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000739bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000740 // Some boilerplate from the X86 FastISel.
741 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000742 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000743 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000744 // Don't walk into other basic blocks unless the object is an alloca from
745 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000746 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
747 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
748 Opcode = I->getOpcode();
749 U = I;
750 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000751 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000752 Opcode = C->getOpcode();
753 U = C;
754 }
755
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000756 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000757 if (Ty->getAddressSpace() > 255)
758 // Fast instruction selection doesn't support the special
759 // address spaces.
760 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000761
Eric Christopher83007122010-08-23 21:44:12 +0000762 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000763 default:
Eric Christopher83007122010-08-23 21:44:12 +0000764 break;
Eric Christopher55324332010-10-12 00:43:21 +0000765 case Instruction::BitCast: {
766 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000767 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000768 }
769 case Instruction::IntToPtr: {
770 // Look past no-op inttoptrs.
771 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000772 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000773 break;
774 }
775 case Instruction::PtrToInt: {
776 // Look past no-op ptrtoints.
777 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000778 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000779 break;
780 }
Eric Christophereae84392010-10-14 09:29:41 +0000781 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000782 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000783 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000784
Eric Christophereae84392010-10-14 09:29:41 +0000785 // Iterate through the GEP folding the constants into offsets where
786 // we can.
787 gep_type_iterator GTI = gep_type_begin(U);
788 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
789 i != e; ++i, ++GTI) {
790 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000791 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000792 const StructLayout *SL = TD.getStructLayout(STy);
793 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
794 TmpOffset += SL->getElementOffset(Idx);
795 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000796 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000797 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000798 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
799 // Constant-offset addressing.
800 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000801 break;
802 }
803 if (isa<AddOperator>(Op) &&
804 (!isa<Instruction>(Op) ||
805 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
806 == FuncInfo.MBB) &&
807 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000808 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000809 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000810 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000811 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000812 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000813 // Iterate on the other operand.
814 Op = cast<AddOperator>(Op)->getOperand(0);
815 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000816 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000817 // Unsupported
818 goto unsupported_gep;
819 }
Eric Christophereae84392010-10-14 09:29:41 +0000820 }
821 }
Eric Christopher2896df82010-10-15 18:02:07 +0000822
823 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000824 Addr.Offset = TmpOffset;
825 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000826
827 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000828 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000829
Eric Christophereae84392010-10-14 09:29:41 +0000830 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000831 break;
832 }
Eric Christopher83007122010-08-23 21:44:12 +0000833 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000834 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000835 DenseMap<const AllocaInst*, int>::iterator SI =
836 FuncInfo.StaticAllocaMap.find(AI);
837 if (SI != FuncInfo.StaticAllocaMap.end()) {
838 Addr.BaseType = Address::FrameIndexBase;
839 Addr.Base.FI = SI->second;
840 return true;
841 }
842 break;
Eric Christopher83007122010-08-23 21:44:12 +0000843 }
844 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000845
Eric Christophera9c57512010-10-13 21:41:51 +0000846 // Materialize the global variable's address into a reg which can
847 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000848 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000849 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
850 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000851
Eric Christopher0d581222010-11-19 22:30:02 +0000852 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000853 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000854 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000855
Eric Christophercb0b04b2010-08-24 00:07:24 +0000856 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000857 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
858 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000859}
860
Chad Rosierb29b9502011-11-13 02:23:59 +0000861void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000862
Eric Christopher212ae932010-10-21 19:40:30 +0000863 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000864
Eric Christopher212ae932010-10-21 19:40:30 +0000865 bool needsLowering = false;
866 switch (VT.getSimpleVT().SimpleTy) {
867 default:
868 assert(false && "Unhandled load/store type!");
Chad Rosier73463472011-11-09 21:30:12 +0000869 break;
Eric Christopher212ae932010-10-21 19:40:30 +0000870 case MVT::i1:
871 case MVT::i8:
Chad Rosierb29b9502011-11-13 02:23:59 +0000872 case MVT::i16:
Eric Christopher212ae932010-10-21 19:40:30 +0000873 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +0000874 if (!useAM3) {
Chad Rosierb29b9502011-11-13 02:23:59 +0000875 // Integer loads/stores handle 12-bit offsets.
876 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosier57b29972011-11-14 20:22:27 +0000877 // Handle negative offsets.
Chad Rosiere489af82011-11-14 22:34:48 +0000878 if (needsLowering && isThumb2)
879 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
880 Addr.Offset > -256);
Chad Rosier57b29972011-11-14 20:22:27 +0000881 } else {
Chad Rosier5be833d2011-11-13 04:25:02 +0000882 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000883 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosier57b29972011-11-14 20:22:27 +0000884 }
Eric Christopher212ae932010-10-21 19:40:30 +0000885 break;
886 case MVT::f32:
887 case MVT::f64:
888 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000889 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000890 break;
891 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000892
Eric Christopher827656d2010-11-20 22:38:27 +0000893 // If this is a stack pointer and the offset needs to be simplified then
894 // put the alloca address into a register, set the base type back to
895 // register and continue. This should almost never happen.
896 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000897 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher827656d2010-11-20 22:38:27 +0000898 ARM::GPRRegisterClass;
899 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000900 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopher827656d2010-11-20 22:38:27 +0000901 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
902 TII.get(Opc), ResultReg)
903 .addFrameIndex(Addr.Base.FI)
904 .addImm(0));
905 Addr.Base.Reg = ResultReg;
906 Addr.BaseType = Address::RegBase;
907 }
908
Eric Christopher212ae932010-10-21 19:40:30 +0000909 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000910 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000911 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000912 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
913 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000914 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000915 }
Eric Christopher83007122010-08-23 21:44:12 +0000916}
917
Eric Christopher564857f2010-12-01 01:40:24 +0000918void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000919 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000920 unsigned Flags, bool useAM3) {
Eric Christopher564857f2010-12-01 01:40:24 +0000921 // addrmode5 output depends on the selection dag addressing dividing the
922 // offset by 4 that it then later multiplies. Do this here as well.
923 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
924 VT.getSimpleVT().SimpleTy == MVT::f64)
925 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000926
Eric Christopher564857f2010-12-01 01:40:24 +0000927 // Frame base works a bit differently. Handle it separately.
928 if (Addr.BaseType == Address::FrameIndexBase) {
929 int FI = Addr.Base.FI;
930 int Offset = Addr.Offset;
931 MachineMemOperand *MMO =
932 FuncInfo.MF->getMachineMemOperand(
933 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000934 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000935 MFI.getObjectSize(FI),
936 MFI.getObjectAlignment(FI));
937 // Now add the rest of the operands.
938 MIB.addFrameIndex(FI);
939
Chad Rosier5be833d2011-11-13 04:25:02 +0000940 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000941 if (useAM3) {
942 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
943 MIB.addReg(0);
944 MIB.addImm(Imm);
945 } else {
946 MIB.addImm(Addr.Offset);
947 }
Eric Christopher564857f2010-12-01 01:40:24 +0000948 MIB.addMemOperand(MMO);
949 } else {
950 // Now add the rest of the operands.
951 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000952
Chad Rosier5be833d2011-11-13 04:25:02 +0000953 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000954 if (useAM3) {
955 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
956 MIB.addReg(0);
957 MIB.addImm(Imm);
958 } else {
959 MIB.addImm(Addr.Offset);
960 }
Eric Christopher564857f2010-12-01 01:40:24 +0000961 }
962 AddOptionalDefs(MIB);
963}
964
Chad Rosierb29b9502011-11-13 02:23:59 +0000965bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
966 bool isZExt = true, bool allocReg = true) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000967 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000968 unsigned Opc;
Chad Rosierb29b9502011-11-13 02:23:59 +0000969 bool useAM3 = false;
970 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000971 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000972 // This is mostly going to be Neon/vector support.
973 default: return false;
Chad Rosier646abbf2011-11-11 02:38:59 +0000974 case MVT::i1:
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000975 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +0000976 if (isThumb2) {
977 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
978 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
979 else
980 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierb29b9502011-11-13 02:23:59 +0000981 } else {
Chad Rosier57b29972011-11-14 20:22:27 +0000982 if (isZExt) {
983 Opc = ARM::LDRBi12;
984 } else {
985 Opc = ARM::LDRSB;
986 useAM3 = true;
987 }
Chad Rosierb29b9502011-11-13 02:23:59 +0000988 }
Eric Christopher7a56f332010-10-08 01:13:17 +0000989 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000990 break;
Chad Rosier73463472011-11-09 21:30:12 +0000991 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +0000992 if (isThumb2) {
993 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
994 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
995 else
996 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
997 } else {
998 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
999 useAM3 = true;
1000 }
Chad Rosier73463472011-11-09 21:30:12 +00001001 RC = ARM::GPRRegisterClass;
1002 break;
Eric Christopherdc908042010-08-31 01:28:42 +00001003 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001004 if (isThumb2) {
1005 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1006 Opc = ARM::t2LDRi8;
1007 else
1008 Opc = ARM::t2LDRi12;
1009 } else {
1010 Opc = ARM::LDRi12;
1011 }
Eric Christopher7a56f332010-10-08 01:13:17 +00001012 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +00001013 break;
Eric Christopher6dab1372010-09-18 01:59:37 +00001014 case MVT::f32:
1015 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +00001016 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001017 break;
1018 case MVT::f64:
1019 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +00001020 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001021 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001022 }
Eric Christopher564857f2010-12-01 01:40:24 +00001023 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001024 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001025
Eric Christopher564857f2010-12-01 01:40:24 +00001026 // Create the base instruction, then add the operands.
Chad Rosierb29b9502011-11-13 02:23:59 +00001027 if (allocReg)
1028 ResultReg = createResultReg(RC);
1029 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher564857f2010-12-01 01:40:24 +00001030 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1031 TII.get(Opc), ResultReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001032 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Eric Christopherdc908042010-08-31 01:28:42 +00001033 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001034}
1035
Eric Christopher43b62be2010-09-27 06:02:23 +00001036bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +00001037 // Atomic loads need special handling.
1038 if (cast<LoadInst>(I)->isAtomic())
1039 return false;
1040
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001041 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001042 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001043 if (!isLoadTypeLegal(I->getType(), VT))
1044 return false;
1045
Eric Christopher564857f2010-12-01 01:40:24 +00001046 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001047 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +00001048 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001049
1050 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +00001051 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001052 UpdateValueMap(I, ResultReg);
1053 return true;
1054}
1055
Eric Christopher0d581222010-11-19 22:30:02 +00001056bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001057 unsigned StrOpc;
Chad Rosierb29b9502011-11-13 02:23:59 +00001058 bool useAM3 = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001059 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001060 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +00001061 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +00001062 case MVT::i1: {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001063 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher4c914122010-11-02 23:59:09 +00001064 ARM::GPRRegisterClass);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001065 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eric Christopher4c914122010-11-02 23:59:09 +00001066 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1067 TII.get(Opc), Res)
1068 .addReg(SrcReg).addImm(1));
1069 SrcReg = Res;
1070 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +00001071 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +00001072 if (isThumb2) {
1073 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1074 StrOpc = ARM::t2STRBi8;
1075 else
1076 StrOpc = ARM::t2STRBi12;
1077 } else {
1078 StrOpc = ARM::STRBi12;
1079 }
Eric Christopher15418772010-10-12 05:39:06 +00001080 break;
1081 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +00001082 if (isThumb2) {
1083 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1084 StrOpc = ARM::t2STRHi8;
1085 else
1086 StrOpc = ARM::t2STRHi12;
1087 } else {
1088 StrOpc = ARM::STRH;
1089 useAM3 = true;
1090 }
Eric Christopher15418772010-10-12 05:39:06 +00001091 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001092 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001093 if (isThumb2) {
1094 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1095 StrOpc = ARM::t2STRi8;
1096 else
1097 StrOpc = ARM::t2STRi12;
1098 } else {
1099 StrOpc = ARM::STRi12;
1100 }
Eric Christopher47650ec2010-10-16 01:10:35 +00001101 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001102 case MVT::f32:
1103 if (!Subtarget->hasVFP2()) return false;
1104 StrOpc = ARM::VSTRS;
1105 break;
1106 case MVT::f64:
1107 if (!Subtarget->hasVFP2()) return false;
1108 StrOpc = ARM::VSTRD;
1109 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001110 }
Eric Christopher564857f2010-12-01 01:40:24 +00001111 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001112 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001113
Eric Christopher564857f2010-12-01 01:40:24 +00001114 // Create the base instruction, then add the operands.
1115 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1116 TII.get(StrOpc))
1117 .addReg(SrcReg, getKillRegState(true));
Chad Rosierb29b9502011-11-13 02:23:59 +00001118 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001119 return true;
1120}
1121
Eric Christopher43b62be2010-09-27 06:02:23 +00001122bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001123 Value *Op0 = I->getOperand(0);
1124 unsigned SrcReg = 0;
1125
Eli Friedman4136d232011-09-02 22:33:24 +00001126 // Atomic stores need special handling.
1127 if (cast<StoreInst>(I)->isAtomic())
1128 return false;
1129
Eric Christopher564857f2010-12-01 01:40:24 +00001130 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001131 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001132 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001133 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001134
Eric Christopher1b61ef42010-09-02 01:48:11 +00001135 // Get the value to be stored into a register.
1136 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001137 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001138
Eric Christopher564857f2010-12-01 01:40:24 +00001139 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001140 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001141 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001142 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001143
Eric Christopher0d581222010-11-19 22:30:02 +00001144 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001145 return true;
1146}
1147
1148static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1149 switch (Pred) {
1150 // Needs two compares...
1151 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001152 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001153 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001154 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001155 return ARMCC::AL;
1156 case CmpInst::ICMP_EQ:
1157 case CmpInst::FCMP_OEQ:
1158 return ARMCC::EQ;
1159 case CmpInst::ICMP_SGT:
1160 case CmpInst::FCMP_OGT:
1161 return ARMCC::GT;
1162 case CmpInst::ICMP_SGE:
1163 case CmpInst::FCMP_OGE:
1164 return ARMCC::GE;
1165 case CmpInst::ICMP_UGT:
1166 case CmpInst::FCMP_UGT:
1167 return ARMCC::HI;
1168 case CmpInst::FCMP_OLT:
1169 return ARMCC::MI;
1170 case CmpInst::ICMP_ULE:
1171 case CmpInst::FCMP_OLE:
1172 return ARMCC::LS;
1173 case CmpInst::FCMP_ORD:
1174 return ARMCC::VC;
1175 case CmpInst::FCMP_UNO:
1176 return ARMCC::VS;
1177 case CmpInst::FCMP_UGE:
1178 return ARMCC::PL;
1179 case CmpInst::ICMP_SLT:
1180 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001181 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001182 case CmpInst::ICMP_SLE:
1183 case CmpInst::FCMP_ULE:
1184 return ARMCC::LE;
1185 case CmpInst::FCMP_UNE:
1186 case CmpInst::ICMP_NE:
1187 return ARMCC::NE;
1188 case CmpInst::ICMP_UGE:
1189 return ARMCC::HS;
1190 case CmpInst::ICMP_ULT:
1191 return ARMCC::LO;
1192 }
Eric Christopher543cf052010-09-01 22:16:27 +00001193}
1194
Eric Christopher43b62be2010-09-27 06:02:23 +00001195bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001196 const BranchInst *BI = cast<BranchInst>(I);
1197 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1198 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001199
Eric Christophere5734102010-09-03 00:35:47 +00001200 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001201
Eric Christopher0e6233b2010-10-29 21:08:19 +00001202 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1203 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001204 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001205 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001206
1207 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001208 // Try to take advantage of fallthrough opportunities.
1209 CmpInst::Predicate Predicate = CI->getPredicate();
1210 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1211 std::swap(TBB, FBB);
1212 Predicate = CmpInst::getInversePredicate(Predicate);
1213 }
1214
1215 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001216
1217 // We may not handle every CC for now.
1218 if (ARMPred == ARMCC::AL) return false;
1219
Chad Rosier75698f32011-10-26 23:17:28 +00001220 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001221 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier75698f32011-10-26 23:17:28 +00001222 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001223
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001224 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1226 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1227 FastEmitBranch(FBB, DL);
1228 FuncInfo.MBB->addSuccessor(TBB);
1229 return true;
1230 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001231 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1232 MVT SourceVT;
1233 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001234 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001235 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001236 unsigned OpReg = getRegForValue(TI->getOperand(0));
1237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1238 TII.get(TstOpc))
1239 .addReg(OpReg).addImm(1));
1240
1241 unsigned CCMode = ARMCC::NE;
1242 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1243 std::swap(TBB, FBB);
1244 CCMode = ARMCC::EQ;
1245 }
1246
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001247 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1249 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1250
1251 FastEmitBranch(FBB, DL);
1252 FuncInfo.MBB->addSuccessor(TBB);
1253 return true;
1254 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001255 } else if (const ConstantInt *CI =
1256 dyn_cast<ConstantInt>(BI->getCondition())) {
1257 uint64_t Imm = CI->getZExtValue();
1258 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1259 FastEmitBranch(Target, DL);
1260 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001261 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001262
Eric Christopher0e6233b2010-10-29 21:08:19 +00001263 unsigned CmpReg = getRegForValue(BI->getCondition());
1264 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001265
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001266 // We've been divorced from our compare! Our block was split, and
1267 // now our compare lives in a predecessor block. We musn't
1268 // re-compare here, as the children of the compare aren't guaranteed
1269 // live across the block boundary (we *could* check for this).
1270 // Regardless, the compare has been done in the predecessor block,
1271 // and it left a value for us in a virtual register. Ergo, we test
1272 // the one-bit value left in the virtual register.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001273 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001274 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1275 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001276
Eric Christopher7a20a372011-04-28 16:52:09 +00001277 unsigned CCMode = ARMCC::NE;
1278 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1279 std::swap(TBB, FBB);
1280 CCMode = ARMCC::EQ;
1281 }
1282
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001283 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001285 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001286 FastEmitBranch(FBB, DL);
1287 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001288 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001289}
1290
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001291bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1292 bool isZExt) {
Chad Rosierade62002011-10-26 23:25:44 +00001293 Type *Ty = Src1Value->getType();
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001294 EVT SrcVT = TLI.getValueType(Ty, true);
1295 if (!SrcVT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001296
Chad Rosierade62002011-10-26 23:25:44 +00001297 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1298 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001299 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001300
Chad Rosier2f2fe412011-11-09 03:22:02 +00001301 // Check to see if the 2nd operand is a constant that we can encode directly
1302 // in the compare.
Chad Rosier1c47de82011-11-11 06:27:41 +00001303 int Imm = 0;
1304 bool UseImm = false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001305 bool isNegativeImm = false;
1306 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1307 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1308 SrcVT == MVT::i1) {
1309 const APInt &CIVal = ConstInt->getValue();
Chad Rosier1c47de82011-11-11 06:27:41 +00001310 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1311 if (Imm < 0) {
Chad Rosier6cba97c2011-11-10 01:30:39 +00001312 isNegativeImm = true;
Chad Rosier1c47de82011-11-11 06:27:41 +00001313 Imm = -Imm;
Chad Rosier6cba97c2011-11-10 01:30:39 +00001314 }
Chad Rosier1c47de82011-11-11 06:27:41 +00001315 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1316 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001317 }
1318 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1319 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1320 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosier1c47de82011-11-11 06:27:41 +00001321 UseImm = true;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001322 }
1323
Eric Christopherd43393a2010-09-08 23:13:45 +00001324 unsigned CmpOpc;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001325 bool isICmp = true;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001326 bool needsExt = false;
1327 switch (SrcVT.getSimpleVT().SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001328 default: return false;
1329 // TODO: Verify compares.
1330 case MVT::f32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001331 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001332 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherd43393a2010-09-08 23:13:45 +00001333 break;
1334 case MVT::f64:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001335 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001336 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherd43393a2010-09-08 23:13:45 +00001337 break;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001338 case MVT::i1:
1339 case MVT::i8:
1340 case MVT::i16:
1341 needsExt = true;
1342 // Intentional fall-through.
Eric Christopherd43393a2010-09-08 23:13:45 +00001343 case MVT::i32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001344 if (isThumb2) {
Chad Rosier1c47de82011-11-11 06:27:41 +00001345 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001346 CmpOpc = ARM::t2CMPrr;
1347 else
1348 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1349 } else {
Chad Rosier1c47de82011-11-11 06:27:41 +00001350 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001351 CmpOpc = ARM::CMPrr;
1352 else
1353 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1354 }
Eric Christopherd43393a2010-09-08 23:13:45 +00001355 break;
1356 }
1357
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001358 unsigned SrcReg1 = getRegForValue(Src1Value);
1359 if (SrcReg1 == 0) return false;
Chad Rosier530f7ce2011-10-26 22:47:55 +00001360
Chad Rosier2f2fe412011-11-09 03:22:02 +00001361 unsigned SrcReg2;
Chad Rosier1c47de82011-11-11 06:27:41 +00001362 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001363 SrcReg2 = getRegForValue(Src2Value);
1364 if (SrcReg2 == 0) return false;
1365 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001366
1367 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1368 if (needsExt) {
1369 unsigned ResultReg;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001370 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001371 if (ResultReg == 0) return false;
1372 SrcReg1 = ResultReg;
Chad Rosier1c47de82011-11-11 06:27:41 +00001373 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001374 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1375 if (ResultReg == 0) return false;
1376 SrcReg2 = ResultReg;
1377 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001378 }
Chad Rosier530f7ce2011-10-26 22:47:55 +00001379
Chad Rosier1c47de82011-11-11 06:27:41 +00001380 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001381 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1382 TII.get(CmpOpc))
1383 .addReg(SrcReg1).addReg(SrcReg2));
1384 } else {
1385 MachineInstrBuilder MIB;
1386 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1387 .addReg(SrcReg1);
1388
1389 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1390 if (isICmp)
Chad Rosier1c47de82011-11-11 06:27:41 +00001391 MIB.addImm(Imm);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001392 AddOptionalDefs(MIB);
1393 }
Chad Rosierade62002011-10-26 23:25:44 +00001394
1395 // For floating point we need to move the result to a comparison register
1396 // that we can then use for branches.
1397 if (Ty->isFloatTy() || Ty->isDoubleTy())
1398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1399 TII.get(ARM::FMSTAT)));
Chad Rosier530f7ce2011-10-26 22:47:55 +00001400 return true;
1401}
1402
1403bool ARMFastISel::SelectCmp(const Instruction *I) {
1404 const CmpInst *CI = cast<CmpInst>(I);
Chad Rosierade62002011-10-26 23:25:44 +00001405 Type *Ty = CI->getOperand(0)->getType();
Chad Rosier530f7ce2011-10-26 22:47:55 +00001406
Eric Christopher229207a2010-09-29 01:14:47 +00001407 // Get the compare predicate.
1408 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001409
Eric Christopher229207a2010-09-29 01:14:47 +00001410 // We may not handle every CC for now.
1411 if (ARMPred == ARMCC::AL) return false;
1412
Chad Rosier530f7ce2011-10-26 22:47:55 +00001413 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001414 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier530f7ce2011-10-26 22:47:55 +00001415 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001416
Eric Christopher229207a2010-09-29 01:14:47 +00001417 // Now set a register based on the comparison. Explicitly set the predicates
1418 // here.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001419 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1420 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001421 : ARM::GPRRegisterClass;
1422 unsigned DestReg = createResultReg(RC);
Chad Rosierade62002011-10-26 23:25:44 +00001423 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001424 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosierade62002011-10-26 23:25:44 +00001425 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
Chad Rosier530f7ce2011-10-26 22:47:55 +00001426 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
Eric Christopher229207a2010-09-29 01:14:47 +00001427 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1428 .addReg(ZeroReg).addImm(1)
1429 .addImm(ARMPred).addReg(CondReg);
1430
Eric Christophera5b1e682010-09-17 22:28:18 +00001431 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001432 return true;
1433}
1434
Eric Christopher43b62be2010-09-27 06:02:23 +00001435bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001436 // Make sure we have VFP and that we're extending float to double.
1437 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001438
Eric Christopher46203602010-09-09 00:26:48 +00001439 Value *V = I->getOperand(0);
1440 if (!I->getType()->isDoubleTy() ||
1441 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001442
Eric Christopher46203602010-09-09 00:26:48 +00001443 unsigned Op = getRegForValue(V);
1444 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001445
Eric Christopher46203602010-09-09 00:26:48 +00001446 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001447 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001448 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001449 .addReg(Op));
1450 UpdateValueMap(I, Result);
1451 return true;
1452}
1453
Eric Christopher43b62be2010-09-27 06:02:23 +00001454bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001455 // Make sure we have VFP and that we're truncating double to float.
1456 if (!Subtarget->hasVFP2()) return false;
1457
1458 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001459 if (!(I->getType()->isFloatTy() &&
1460 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001461
1462 unsigned Op = getRegForValue(V);
1463 if (Op == 0) return false;
1464
1465 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001466 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001467 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001468 .addReg(Op));
1469 UpdateValueMap(I, Result);
1470 return true;
1471}
1472
Eric Christopher43b62be2010-09-27 06:02:23 +00001473bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001474 // Make sure we have VFP.
1475 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001476
Duncan Sands1440e8b2010-11-03 11:35:31 +00001477 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001478 Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001479 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001480 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001481
Chad Rosier463fe242011-11-03 02:04:59 +00001482 Value *Src = I->getOperand(0);
1483 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1484 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman783c6642011-05-25 19:09:45 +00001485 return false;
1486
Chad Rosier463fe242011-11-03 02:04:59 +00001487 unsigned SrcReg = getRegForValue(Src);
1488 if (SrcReg == 0) return false;
1489
1490 // Handle sign-extension.
1491 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1492 EVT DestVT = MVT::i32;
1493 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false);
1494 if (ResultReg == 0) return false;
1495 SrcReg = ResultReg;
1496 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001497
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001498 // The conversion routine works on fp-reg to fp-reg and the operand above
1499 // was an integer, move it to the fp registers if possible.
Chad Rosier463fe242011-11-03 02:04:59 +00001500 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001501 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001502
Eric Christopher9a040492010-09-09 18:54:59 +00001503 unsigned Opc;
1504 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1505 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001506 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001507
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001508 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001509 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1510 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001511 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001512 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001513 return true;
1514}
1515
Eric Christopher43b62be2010-09-27 06:02:23 +00001516bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001517 // Make sure we have VFP.
1518 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001519
Duncan Sands1440e8b2010-11-03 11:35:31 +00001520 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001521 Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001522 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001523 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001524
Eric Christopher9a040492010-09-09 18:54:59 +00001525 unsigned Op = getRegForValue(I->getOperand(0));
1526 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001527
Eric Christopher9a040492010-09-09 18:54:59 +00001528 unsigned Opc;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001529 Type *OpTy = I->getOperand(0)->getType();
Eric Christopher9a040492010-09-09 18:54:59 +00001530 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1531 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001532 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001533
Eric Christopher022b7fb2010-10-05 23:13:24 +00001534 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1535 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001536 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1537 ResultReg)
1538 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001539
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001540 // This result needs to be in an integer register, but the conversion only
1541 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001542 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001543 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001544
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001545 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001546 return true;
1547}
1548
Eric Christopher3bbd3962010-10-11 08:27:59 +00001549bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001550 MVT VT;
1551 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001552 return false;
1553
1554 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001555 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001556 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1557
1558 unsigned CondReg = getRegForValue(I->getOperand(0));
1559 if (CondReg == 0) return false;
1560 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1561 if (Op1Reg == 0) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001562
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001563 // Check to see if we can use an immediate in the conditional move.
1564 int Imm = 0;
1565 bool UseImm = false;
1566 bool isNegativeImm = false;
1567 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1568 assert (VT == MVT::i32 && "Expecting an i32.");
1569 Imm = (int)ConstInt->getValue().getZExtValue();
1570 if (Imm < 0) {
1571 isNegativeImm = true;
1572 Imm = ~Imm;
1573 }
1574 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1575 (ARM_AM::getSOImmVal(Imm) != -1);
1576 }
1577
1578 unsigned Op2Reg;
1579 if (!UseImm) {
1580 Op2Reg = getRegForValue(I->getOperand(2));
1581 if (Op2Reg == 0) return false;
1582 }
1583
1584 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001586 .addReg(CondReg).addImm(0));
1587
1588 unsigned MovCCOpc;
1589 if (!UseImm) {
1590 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1591 } else {
1592 if (!isNegativeImm) {
1593 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1594 } else {
1595 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1596 }
1597 }
Eric Christopher3bbd3962010-10-11 08:27:59 +00001598 unsigned ResultReg = createResultReg(RC);
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001599 if (!UseImm)
1600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1601 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1602 else
1603 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1604 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001605 UpdateValueMap(I, ResultReg);
1606 return true;
1607}
1608
Eric Christopher08637852010-09-30 22:34:19 +00001609bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001610 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001611 Type *Ty = I->getType();
Eric Christopher08637852010-09-30 22:34:19 +00001612 if (!isTypeLegal(Ty, VT))
1613 return false;
1614
1615 // If we have integer div support we should have selected this automagically.
1616 // In case we have a real miss go ahead and return false and we'll pick
1617 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001618 if (Subtarget->hasDivide()) return false;
1619
Eric Christopher08637852010-09-30 22:34:19 +00001620 // Otherwise emit a libcall.
1621 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001622 if (VT == MVT::i8)
1623 LC = RTLIB::SDIV_I8;
1624 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001625 LC = RTLIB::SDIV_I16;
1626 else if (VT == MVT::i32)
1627 LC = RTLIB::SDIV_I32;
1628 else if (VT == MVT::i64)
1629 LC = RTLIB::SDIV_I64;
1630 else if (VT == MVT::i128)
1631 LC = RTLIB::SDIV_I128;
1632 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001633
Eric Christopher08637852010-09-30 22:34:19 +00001634 return ARMEmitLibcall(I, LC);
1635}
1636
Eric Christopher6a880d62010-10-11 08:37:26 +00001637bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001638 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001639 Type *Ty = I->getType();
Eric Christopher6a880d62010-10-11 08:37:26 +00001640 if (!isTypeLegal(Ty, VT))
1641 return false;
1642
1643 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1644 if (VT == MVT::i8)
1645 LC = RTLIB::SREM_I8;
1646 else if (VT == MVT::i16)
1647 LC = RTLIB::SREM_I16;
1648 else if (VT == MVT::i32)
1649 LC = RTLIB::SREM_I32;
1650 else if (VT == MVT::i64)
1651 LC = RTLIB::SREM_I64;
1652 else if (VT == MVT::i128)
1653 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001654 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001655
Eric Christopher6a880d62010-10-11 08:37:26 +00001656 return ARMEmitLibcall(I, LC);
1657}
1658
Eric Christopher43b62be2010-09-27 06:02:23 +00001659bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001660 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001661
Eric Christopherbc39b822010-09-09 00:53:57 +00001662 // We can get here in the case when we want to use NEON for our fp
1663 // operations, but can't figure out how to. Just use the vfp instructions
1664 // if we have them.
1665 // FIXME: It'd be nice to use NEON instructions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001666 Type *Ty = I->getType();
Eric Christopherbd6bf082010-09-09 01:02:03 +00001667 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1668 if (isFloat && !Subtarget->hasVFP2())
1669 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001670
Eric Christopherbc39b822010-09-09 00:53:57 +00001671 unsigned Op1 = getRegForValue(I->getOperand(0));
1672 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001673
Eric Christopherbc39b822010-09-09 00:53:57 +00001674 unsigned Op2 = getRegForValue(I->getOperand(1));
1675 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001676
Eric Christopherbc39b822010-09-09 00:53:57 +00001677 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001678 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001679 switch (ISDOpcode) {
1680 default: return false;
1681 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001682 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001683 break;
1684 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001685 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001686 break;
1687 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001688 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001689 break;
1690 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001691 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001692 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1693 TII.get(Opc), ResultReg)
1694 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001695 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001696 return true;
1697}
1698
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001699// Call Handling Code
1700
Eric Christopherfa87d662010-10-18 02:17:53 +00001701bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1702 EVT SrcVT, unsigned &ResultReg) {
1703 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1704 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001705
Eric Christopherfa87d662010-10-18 02:17:53 +00001706 if (RR != 0) {
1707 ResultReg = RR;
1708 return true;
1709 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001710 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001711}
1712
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001713// This is largely taken directly from CCAssignFnForNode - we don't support
1714// varargs in FastISel so that part has been removed.
1715// TODO: We may not support all of this.
1716CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1717 switch (CC) {
1718 default:
1719 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001720 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001721 // Ignore fastcc. Silence compiler warnings.
1722 (void)RetFastCC_ARM_APCS;
1723 (void)FastCC_ARM_APCS;
1724 // Fallthrough
1725 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001726 // Use target triple & subtarget features to do actual dispatch.
1727 if (Subtarget->isAAPCS_ABI()) {
1728 if (Subtarget->hasVFP2() &&
1729 FloatABIType == FloatABI::Hard)
1730 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1731 else
1732 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1733 } else
1734 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1735 case CallingConv::ARM_AAPCS_VFP:
1736 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1737 case CallingConv::ARM_AAPCS:
1738 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1739 case CallingConv::ARM_APCS:
1740 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1741 }
1742}
1743
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001744bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1745 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001746 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001747 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1748 SmallVectorImpl<unsigned> &RegArgs,
1749 CallingConv::ID CC,
1750 unsigned &NumBytes) {
1751 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001752 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001753 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1754
1755 // Get a count of how many bytes are to be pushed on the stack.
1756 NumBytes = CCInfo.getNextStackOffset();
1757
1758 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001759 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001760 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1761 TII.get(AdjStackDown))
1762 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001763
1764 // Process the args.
1765 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1766 CCValAssign &VA = ArgLocs[i];
1767 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001768 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001769
Eric Christopher4a2b3162011-01-27 05:44:56 +00001770 // We don't handle NEON/vector parameters yet.
1771 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001772 return false;
1773
Eric Christopherf9764fa2010-09-30 20:49:44 +00001774 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001775 switch (VA.getLocInfo()) {
1776 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001777 case CCValAssign::SExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001778 EVT DestVT = VA.getLocVT();
1779 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1780 /*isZExt*/false);
1781 assert (ResultReg != 0 && "Failed to emit a sext");
1782 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001783 break;
1784 }
Chad Rosier42536af2011-11-05 20:16:15 +00001785 case CCValAssign::AExt:
1786 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherfa87d662010-10-18 02:17:53 +00001787 case CCValAssign::ZExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001788 EVT DestVT = VA.getLocVT();
1789 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1790 /*isZExt*/true);
1791 assert (ResultReg != 0 && "Failed to emit a sext");
1792 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001793 break;
1794 }
1795 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001796 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001797 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001798 assert(BC != 0 && "Failed to emit a bitcast!");
1799 Arg = BC;
1800 ArgVT = VA.getLocVT();
1801 break;
1802 }
1803 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001804 }
1805
1806 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001807 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001808 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001809 VA.getLocReg())
Chad Rosier42536af2011-11-05 20:16:15 +00001810 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001811 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001812 } else if (VA.needsCustom()) {
1813 // TODO: We need custom lowering for vector (v2f64) args.
1814 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001815
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001816 CCValAssign &NextVA = ArgLocs[++i];
1817
1818 // TODO: Only handle register args for now.
1819 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1820
1821 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1822 TII.get(ARM::VMOVRRD), VA.getLocReg())
1823 .addReg(NextVA.getLocReg(), RegState::Define)
1824 .addReg(Arg));
1825 RegArgs.push_back(VA.getLocReg());
1826 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001827 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001828 assert(VA.isMemLoc());
1829 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001830 Address Addr;
1831 Addr.BaseType = Address::RegBase;
1832 Addr.Base.Reg = ARM::SP;
1833 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001834
Eric Christopher0d581222010-11-19 22:30:02 +00001835 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001836 }
1837 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001838 return true;
1839}
1840
Duncan Sands1440e8b2010-11-03 11:35:31 +00001841bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001842 const Instruction *I, CallingConv::ID CC,
1843 unsigned &NumBytes) {
1844 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001845 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001846 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1847 TII.get(AdjStackUp))
1848 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001849
1850 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001851 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001852 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001853 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001854 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1855
1856 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001857 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001858 // For this move we copy into two registers and then move into the
1859 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001860 EVT DestVT = RVLocs[0].getValVT();
1861 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1862 unsigned ResultReg = createResultReg(DstRC);
1863 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1864 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001865 .addReg(RVLocs[0].getLocReg())
1866 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001867
Eric Christopher3659ac22010-10-20 08:02:24 +00001868 UsedRegs.push_back(RVLocs[0].getLocReg());
1869 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001870
Eric Christopherdccd2c32010-10-11 08:38:55 +00001871 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001872 UpdateValueMap(I, ResultReg);
1873 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001874 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001875 EVT CopyVT = RVLocs[0].getValVT();
Chad Rosier0eff39f2011-11-08 00:03:32 +00001876
1877 // Special handling for extended integers.
1878 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1879 CopyVT = MVT::i32;
1880
Eric Christopher14df8822010-10-01 00:00:11 +00001881 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001882
Eric Christopher14df8822010-10-01 00:00:11 +00001883 unsigned ResultReg = createResultReg(DstRC);
1884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1885 ResultReg).addReg(RVLocs[0].getLocReg());
1886 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001887
Eric Christopherdccd2c32010-10-11 08:38:55 +00001888 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001889 UpdateValueMap(I, ResultReg);
1890 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001891 }
1892
Eric Christopherdccd2c32010-10-11 08:38:55 +00001893 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001894}
1895
Eric Christopher4f512ef2010-10-22 01:28:00 +00001896bool ARMFastISel::SelectRet(const Instruction *I) {
1897 const ReturnInst *Ret = cast<ReturnInst>(I);
1898 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001899
Eric Christopher4f512ef2010-10-22 01:28:00 +00001900 if (!FuncInfo.CanLowerReturn)
1901 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001902
Eric Christopher4f512ef2010-10-22 01:28:00 +00001903 if (F.isVarArg())
1904 return false;
1905
1906 CallingConv::ID CC = F.getCallingConv();
1907 if (Ret->getNumOperands() > 0) {
1908 SmallVector<ISD::OutputArg, 4> Outs;
1909 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1910 Outs, TLI);
1911
1912 // Analyze operands of the call, assigning locations to each operand.
1913 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00001914 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Eric Christopher4f512ef2010-10-22 01:28:00 +00001915 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1916
1917 const Value *RV = Ret->getOperand(0);
1918 unsigned Reg = getRegForValue(RV);
1919 if (Reg == 0)
1920 return false;
1921
1922 // Only handle a single return value for now.
1923 if (ValLocs.size() != 1)
1924 return false;
1925
1926 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001927
Eric Christopher4f512ef2010-10-22 01:28:00 +00001928 // Don't bother handling odd stuff for now.
1929 if (VA.getLocInfo() != CCValAssign::Full)
1930 return false;
1931 // Only handle register returns for now.
1932 if (!VA.isRegLoc())
1933 return false;
Chad Rosierf470cbb2011-11-04 00:50:21 +00001934
1935 unsigned SrcReg = Reg + VA.getValNo();
1936 EVT RVVT = TLI.getValueType(RV->getType());
1937 EVT DestVT = VA.getValVT();
1938 // Special handling for extended integers.
1939 if (RVVT != DestVT) {
1940 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1941 return false;
1942
1943 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1944 return false;
1945
1946 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1947
1948 bool isZExt = Outs[0].Flags.isZExt();
1949 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1950 if (ResultReg == 0) return false;
1951 SrcReg = ResultReg;
1952 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001953
Eric Christopher4f512ef2010-10-22 01:28:00 +00001954 // Make the copy.
Eric Christopher4f512ef2010-10-22 01:28:00 +00001955 unsigned DstReg = VA.getLocReg();
1956 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1957 // Avoid a cross-class copy. This is very unlikely.
1958 if (!SrcRC->contains(DstReg))
1959 return false;
1960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1961 DstReg).addReg(SrcReg);
1962
1963 // Mark the register as live out of the function.
1964 MRI.addLiveOut(VA.getLocReg());
1965 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001966
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001967 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Eric Christopher4f512ef2010-10-22 01:28:00 +00001968 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1969 TII.get(RetOpc)));
1970 return true;
1971}
1972
Eric Christopher872f4a22011-02-22 01:37:10 +00001973unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1974
Eric Christopher872f4a22011-02-22 01:37:10 +00001975 // Darwin needs the r9 versions of the opcodes.
1976 bool isDarwin = Subtarget->isTargetDarwin();
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001977 if (isThumb2) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001978 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1979 } else {
1980 return isDarwin ? ARM::BLr9 : ARM::BL;
1981 }
1982}
1983
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001984// A quick function that will emit a call for a named libcall in F with the
1985// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001986// can emit a call for any libcall we can produce. This is an abridged version
1987// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001988// like computed function pointers or strange arguments at call sites.
1989// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1990// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001991bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1992 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001993
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001994 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001995 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001996 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001997 if (RetTy->isVoidTy())
1998 RetVT = MVT::isVoid;
1999 else if (!isTypeLegal(RetTy, RetVT))
2000 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002001
Eric Christopher836c6242010-12-15 23:47:29 +00002002 // TODO: For now if we have long calls specified we don't handle the call.
2003 if (EnableARMLongCalls) return false;
2004
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002005 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002006 SmallVector<Value*, 8> Args;
2007 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002008 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002009 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2010 Args.reserve(I->getNumOperands());
2011 ArgRegs.reserve(I->getNumOperands());
2012 ArgVTs.reserve(I->getNumOperands());
2013 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00002014 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002015 Value *Op = I->getOperand(i);
2016 unsigned Arg = getRegForValue(Op);
2017 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002018
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002019 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002020 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002021 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002022
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002023 ISD::ArgFlagsTy Flags;
2024 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2025 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002026
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002027 Args.push_back(Op);
2028 ArgRegs.push_back(Arg);
2029 ArgVTs.push_back(ArgVT);
2030 ArgFlags.push_back(Flags);
2031 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002032
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002033 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002034 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002035 unsigned NumBytes;
2036 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2037 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002038
Eric Christopher6344a5f2011-04-29 00:07:20 +00002039 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002040 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002041 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002042 unsigned CallOpc = ARMSelectCallOp(NULL);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002043 if(isThumb2)
Eric Christopherc19aadb2010-12-21 03:50:43 +00002044 // Explicitly adding the predicate here.
2045 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2046 TII.get(CallOpc)))
2047 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00002048 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00002049 // Explicitly adding the predicate here.
2050 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2051 TII.get(CallOpc))
2052 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00002053
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002054 // Add implicit physical register uses to the call.
2055 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2056 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002057
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002058 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002059 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002060 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002061
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002062 // Set all unused physreg defs as dead.
2063 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002064
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002065 return true;
2066}
2067
Chad Rosier11add262011-11-11 23:31:03 +00002068bool ARMFastISel::SelectCall(const Instruction *I,
2069 const char *IntrMemName = 0) {
Eric Christopherf9764fa2010-09-30 20:49:44 +00002070 const CallInst *CI = cast<CallInst>(I);
2071 const Value *Callee = CI->getCalledValue();
2072
Chad Rosier11add262011-11-11 23:31:03 +00002073 // Can't handle inline asm.
2074 if (isa<InlineAsm>(Callee)) return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002075
Eric Christopher52f6c032011-05-02 20:16:33 +00002076 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002077 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00002078 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00002079 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002080
Eric Christopherf9764fa2010-09-30 20:49:44 +00002081 // Check the calling convention.
2082 ImmutableCallSite CS(CI);
2083 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00002084
Eric Christopherf9764fa2010-09-30 20:49:44 +00002085 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00002086
Eric Christopherf9764fa2010-09-30 20:49:44 +00002087 // Let SDISel handle vararg functions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002088 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2089 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eric Christopherf9764fa2010-09-30 20:49:44 +00002090 if (FTy->isVarArg())
2091 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002092
Eric Christopherf9764fa2010-09-30 20:49:44 +00002093 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002094 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002095 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002096 if (RetTy->isVoidTy())
2097 RetVT = MVT::isVoid;
Chad Rosier0eff39f2011-11-08 00:03:32 +00002098 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2099 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002100 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002101
Eric Christopher836c6242010-12-15 23:47:29 +00002102 // TODO: For now if we have long calls specified we don't handle the call.
2103 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00002104
Eric Christopherf9764fa2010-09-30 20:49:44 +00002105 // Set up the argument vectors.
2106 SmallVector<Value*, 8> Args;
2107 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002108 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002109 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2110 Args.reserve(CS.arg_size());
2111 ArgRegs.reserve(CS.arg_size());
2112 ArgVTs.reserve(CS.arg_size());
2113 ArgFlags.reserve(CS.arg_size());
2114 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2115 i != e; ++i) {
Chad Rosier11add262011-11-11 23:31:03 +00002116 // If we're lowering a memory intrinsic instead of a regular call, skip the
2117 // last two arguments, which shouldn't be passed to the underlying function.
2118 if (IntrMemName && e-i <= 2)
2119 break;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002120
Chad Rosier11add262011-11-11 23:31:03 +00002121 unsigned Arg = getRegForValue(*i);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002122 if (Arg == 0)
2123 return false;
2124 ISD::ArgFlagsTy Flags;
2125 unsigned AttrInd = i - CS.arg_begin() + 1;
2126 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2127 Flags.setSExt();
2128 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2129 Flags.setZExt();
2130
Chad Rosier8e4a2e42011-11-04 00:58:10 +00002131 // FIXME: Only handle *easy* calls for now.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002132 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2133 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2134 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2135 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2136 return false;
2137
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002138 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002139 MVT ArgVT;
Chad Rosier42536af2011-11-05 20:16:15 +00002140 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2141 ArgVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002142 return false;
2143 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2144 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002145
Eric Christopherf9764fa2010-09-30 20:49:44 +00002146 Args.push_back(*i);
2147 ArgRegs.push_back(Arg);
2148 ArgVTs.push_back(ArgVT);
2149 ArgFlags.push_back(Flags);
2150 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002151
Eric Christopherf9764fa2010-09-30 20:49:44 +00002152 // Handle the arguments now that we've gotten them.
2153 SmallVector<unsigned, 4> RegArgs;
2154 unsigned NumBytes;
2155 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2156 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002157
Eric Christopher6344a5f2011-04-29 00:07:20 +00002158 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002159 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002160 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002161 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00002162 // Explicitly adding the predicate here.
Chad Rosier9eb67482011-11-13 09:44:21 +00002163 if(isThumb2) {
Eric Christopherc19aadb2010-12-21 03:50:43 +00002164 // Explicitly adding the predicate here.
2165 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier11add262011-11-11 23:31:03 +00002166 TII.get(CallOpc)));
Chad Rosier9eb67482011-11-13 09:44:21 +00002167 if (!IntrMemName)
2168 MIB.addGlobalAddress(GV, 0, 0);
2169 else
2170 MIB.addExternalSymbol(IntrMemName, 0);
2171 } else {
2172 if (!IntrMemName)
2173 // Explicitly adding the predicate here.
2174 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2175 TII.get(CallOpc))
2176 .addGlobalAddress(GV, 0, 0));
2177 else
2178 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2179 TII.get(CallOpc))
2180 .addExternalSymbol(IntrMemName, 0));
2181 }
Chad Rosier11add262011-11-11 23:31:03 +00002182
Eric Christopherf9764fa2010-09-30 20:49:44 +00002183 // Add implicit physical register uses to the call.
2184 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2185 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002186
Eric Christopherf9764fa2010-09-30 20:49:44 +00002187 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002188 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002189 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002190
Eric Christopherf9764fa2010-09-30 20:49:44 +00002191 // Set all unused physreg defs as dead.
2192 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002193
Eric Christopherf9764fa2010-09-30 20:49:44 +00002194 return true;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002195}
2196
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002197bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002198 return Len <= 16;
2199}
2200
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002201bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002202 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002203 if (!ARMIsMemCpySmall(Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002204 return false;
2205
2206 // We don't care about alignment here since we just emit integer accesses.
2207 while (Len) {
2208 MVT VT;
2209 if (Len >= 4)
2210 VT = MVT::i32;
2211 else if (Len >= 2)
2212 VT = MVT::i16;
2213 else {
2214 assert(Len == 1);
2215 VT = MVT::i8;
2216 }
2217
2218 bool RV;
2219 unsigned ResultReg;
2220 RV = ARMEmitLoad(VT, ResultReg, Src);
2221 assert (RV = true && "Should be able to handle this load.");
2222 RV = ARMEmitStore(VT, ResultReg, Dest);
2223 assert (RV = true && "Should be able to handle this store.");
2224
2225 unsigned Size = VT.getSizeInBits()/8;
2226 Len -= Size;
2227 Dest.Offset += Size;
2228 Src.Offset += Size;
2229 }
2230
2231 return true;
2232}
2233
Chad Rosier11add262011-11-11 23:31:03 +00002234bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2235 // FIXME: Handle more intrinsics.
2236 switch (I.getIntrinsicID()) {
2237 default: return false;
2238 case Intrinsic::memcpy:
2239 case Intrinsic::memmove: {
Chad Rosier11add262011-11-11 23:31:03 +00002240 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2241 // Don't handle volatile.
2242 if (MTI.isVolatile())
2243 return false;
Chad Rosier909cb4f2011-11-14 22:46:17 +00002244
2245 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2246 // we would emit dead code because we don't currently handle memmoves.
2247 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2248 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002249 // Small memcpy's are common enough that we want to do them without a call
2250 // if possible.
Chad Rosier909cb4f2011-11-14 22:46:17 +00002251 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002252 if (ARMIsMemCpySmall(Len)) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002253 Address Dest, Src;
2254 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2255 !ARMComputeAddress(MTI.getRawSource(), Src))
2256 return false;
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002257 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002258 return true;
2259 }
2260 }
Chad Rosier11add262011-11-11 23:31:03 +00002261
2262 if (!MTI.getLength()->getType()->isIntegerTy(32))
2263 return false;
2264
2265 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2266 return false;
2267
2268 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2269 return SelectCall(&I, IntrMemName);
2270 }
2271 case Intrinsic::memset: {
2272 const MemSetInst &MSI = cast<MemSetInst>(I);
2273 // Don't handle volatile.
2274 if (MSI.isVolatile())
2275 return false;
2276
2277 if (!MSI.getLength()->getType()->isIntegerTy(32))
2278 return false;
2279
2280 if (MSI.getDestAddressSpace() > 255)
2281 return false;
2282
2283 return SelectCall(&I, "memset");
2284 }
2285 }
2286 return false;
2287}
2288
Chad Rosier0d7b2312011-11-02 00:18:48 +00002289bool ARMFastISel::SelectTrunc(const Instruction *I) {
2290 // The high bits for a type smaller than the register size are assumed to be
2291 // undefined.
2292 Value *Op = I->getOperand(0);
2293
2294 EVT SrcVT, DestVT;
2295 SrcVT = TLI.getValueType(Op->getType(), true);
2296 DestVT = TLI.getValueType(I->getType(), true);
2297
2298 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2299 return false;
2300 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2301 return false;
2302
2303 unsigned SrcReg = getRegForValue(Op);
2304 if (!SrcReg) return false;
2305
2306 // Because the high bits are undefined, a truncate doesn't generate
2307 // any code.
2308 UpdateValueMap(I, SrcReg);
2309 return true;
2310}
2311
Chad Rosier87633022011-11-02 17:20:24 +00002312unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2313 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002314 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00002315 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002316
2317 unsigned Opc;
Eli Friedman76927d732011-05-25 23:49:02 +00002318 bool isBoolZext = false;
Chad Rosier87633022011-11-02 17:20:24 +00002319 if (!SrcVT.isSimple()) return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002320 switch (SrcVT.getSimpleVT().SimpleTy) {
Chad Rosier87633022011-11-02 17:20:24 +00002321 default: return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002322 case MVT::i16:
Chad Rosier87633022011-11-02 17:20:24 +00002323 if (!Subtarget->hasV6Ops()) return 0;
2324 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002325 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002326 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002327 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002328 break;
2329 case MVT::i8:
Chad Rosier87633022011-11-02 17:20:24 +00002330 if (!Subtarget->hasV6Ops()) return 0;
2331 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002332 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002333 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002334 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002335 break;
2336 case MVT::i1:
Chad Rosier87633022011-11-02 17:20:24 +00002337 if (isZExt) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002338 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eli Friedman76927d732011-05-25 23:49:02 +00002339 isBoolZext = true;
2340 break;
2341 }
Chad Rosier87633022011-11-02 17:20:24 +00002342 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002343 }
2344
Chad Rosier87633022011-11-02 17:20:24 +00002345 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eli Friedman76927d732011-05-25 23:49:02 +00002346 MachineInstrBuilder MIB;
Chad Rosier87633022011-11-02 17:20:24 +00002347 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
Eli Friedman76927d732011-05-25 23:49:02 +00002348 .addReg(SrcReg);
2349 if (isBoolZext)
2350 MIB.addImm(1);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002351 else
2352 MIB.addImm(0);
Eli Friedman76927d732011-05-25 23:49:02 +00002353 AddOptionalDefs(MIB);
Chad Rosier87633022011-11-02 17:20:24 +00002354 return ResultReg;
2355}
2356
2357bool ARMFastISel::SelectIntExt(const Instruction *I) {
2358 // On ARM, in general, integer casts don't involve legal types; this code
2359 // handles promotable integers.
Chad Rosier87633022011-11-02 17:20:24 +00002360 Type *DestTy = I->getType();
2361 Value *Src = I->getOperand(0);
2362 Type *SrcTy = Src->getType();
2363
2364 EVT SrcVT, DestVT;
2365 SrcVT = TLI.getValueType(SrcTy, true);
2366 DestVT = TLI.getValueType(DestTy, true);
2367
2368 bool isZExt = isa<ZExtInst>(I);
2369 unsigned SrcReg = getRegForValue(Src);
2370 if (!SrcReg) return false;
2371
2372 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2373 if (ResultReg == 0) return false;
2374 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002375 return true;
2376}
2377
Eric Christopher56d2b722010-09-02 23:43:26 +00002378// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002379bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002380
Eric Christopherab695882010-07-21 22:26:11 +00002381 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002382 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002383 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002384 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002385 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002386 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002387 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002388 case Instruction::ICmp:
2389 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002390 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002391 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002392 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002393 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002394 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002395 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00002396 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002397 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00002398 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00002399 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00002400 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002401 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00002402 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002403 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00002404 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002405 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00002406 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00002407 case Instruction::SRem:
2408 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002409 case Instruction::Call:
Chad Rosier11add262011-11-11 23:31:03 +00002410 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2411 return SelectIntrinsicCall(*II);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002412 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002413 case Instruction::Select:
2414 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002415 case Instruction::Ret:
2416 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002417 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002418 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002419 case Instruction::ZExt:
2420 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002421 return SelectIntExt(I);
Eric Christopherab695882010-07-21 22:26:11 +00002422 default: break;
2423 }
2424 return false;
2425}
2426
Chad Rosierb29b9502011-11-13 02:23:59 +00002427/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2428/// vreg is being provided by the specified load instruction. If possible,
2429/// try to fold the load as an operand to the instruction, returning true if
2430/// successful.
2431bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2432 const LoadInst *LI) {
2433 // Verify we have a legal type before going any further.
2434 MVT VT;
2435 if (!isLoadTypeLegal(LI->getType(), VT))
2436 return false;
2437
2438 // Combine load followed by zero- or sign-extend.
2439 // ldrb r1, [r0] ldrb r1, [r0]
2440 // uxtb r2, r1 =>
2441 // mov r3, r2 mov r3, r1
2442 bool isZExt = true;
2443 switch(MI->getOpcode()) {
2444 default: return false;
2445 case ARM::SXTH:
2446 case ARM::t2SXTH:
2447 isZExt = false;
2448 case ARM::UXTH:
2449 case ARM::t2UXTH:
2450 if (VT != MVT::i16)
2451 return false;
2452 break;
2453 case ARM::SXTB:
2454 case ARM::t2SXTB:
2455 isZExt = false;
2456 case ARM::UXTB:
2457 case ARM::t2UXTB:
2458 if (VT != MVT::i8)
2459 return false;
2460 break;
2461 }
2462 // See if we can handle this address.
2463 Address Addr;
2464 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2465
2466 unsigned ResultReg = MI->getOperand(0).getReg();
2467 if (!ARMEmitLoad(VT, ResultReg, Addr, isZExt, false))
2468 return false;
2469 MI->eraseFromParent();
2470 return true;
2471}
2472
Eric Christopherab695882010-07-21 22:26:11 +00002473namespace llvm {
2474 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00002475 // Completely untested on non-darwin.
2476 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002477
Eric Christopheraaa8df42010-11-02 01:21:28 +00002478 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00002479 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002480 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00002481 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00002482 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00002483 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002484 }
2485}