blob: c7ad45d104fb7d47522a334c80d84e7f22206db4 [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:
199 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000200 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000201 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000202 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000203 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
204 SmallVectorImpl<unsigned> &RegArgs,
205 CallingConv::ID CC,
206 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000207 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000208 const Instruction *I, CallingConv::ID CC,
209 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000210 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000211
212 // OptionalDef handling routines.
213 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000214 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000215 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
216 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000217 void AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000218 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000219 unsigned Flags, bool useAM3);
Eric Christopher456144e2010-08-19 00:37:05 +0000220};
Eric Christopherab695882010-07-21 22:26:11 +0000221
222} // end anonymous namespace
223
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000224#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000225
Eric Christopher456144e2010-08-19 00:37:05 +0000226// DefinesOptionalPredicate - This is different from DefinesPredicate in that
227// we don't care about implicit defs here, just places we'll need to add a
228// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
229bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Chenge837dea2011-06-28 19:10:37 +0000230 const MCInstrDesc &MCID = MI->getDesc();
231 if (!MCID.hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000232 return false;
233
234 // Look to see if our OptionalDef is defining CPSR or CCR.
235 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
236 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000237 if (!MO.isReg() || !MO.isDef()) continue;
238 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000239 *CPSR = true;
240 }
241 return true;
242}
243
Eric Christopheraf3dce52011-03-12 01:09:29 +0000244bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000245 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000246
Eric Christopheraf3dce52011-03-12 01:09:29 +0000247 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000248 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000249 AFI->isThumb2Function())
250 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000251
Evan Chenge837dea2011-06-28 19:10:37 +0000252 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
253 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000254 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000255
Eric Christopheraf3dce52011-03-12 01:09:29 +0000256 return false;
257}
258
Eric Christopher456144e2010-08-19 00:37:05 +0000259// If the machine is predicable go ahead and add the predicate operands, if
260// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000261// TODO: If we want to support thumb1 then we'll need to deal with optional
262// CPSR defs that need to be added before the remaining operands. See s_cc_out
263// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000264const MachineInstrBuilder &
265ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
266 MachineInstr *MI = &*MIB;
267
Eric Christopheraf3dce52011-03-12 01:09:29 +0000268 // Do we use a predicate? or...
269 // Are we NEON in ARM mode and have a predicate operand? If so, I know
270 // we're not predicable but add it anyways.
271 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000272 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000273
Eric Christopher456144e2010-08-19 00:37:05 +0000274 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
275 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000276 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000277 if (DefinesOptionalPredicate(MI, &CPSR)) {
278 if (CPSR)
279 AddDefaultT1CC(MIB);
280 else
281 AddDefaultCC(MIB);
282 }
283 return MIB;
284}
285
Eric Christopher0fe7d542010-08-17 01:25:29 +0000286unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
287 const TargetRegisterClass* RC) {
288 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000289 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290
Eric Christopher456144e2010-08-19 00:37:05 +0000291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000292 return ResultReg;
293}
294
295unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
296 const TargetRegisterClass *RC,
297 unsigned Op0, bool Op0IsKill) {
298 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000299 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000300
301 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303 .addReg(Op0, Op0IsKill * RegState::Kill));
304 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308 TII.get(TargetOpcode::COPY), ResultReg)
309 .addReg(II.ImplicitDefs[0]));
310 }
311 return ResultReg;
312}
313
314unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
315 const TargetRegisterClass *RC,
316 unsigned Op0, bool Op0IsKill,
317 unsigned Op1, bool Op1IsKill) {
318 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000319 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000320
321 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323 .addReg(Op0, Op0IsKill * RegState::Kill)
324 .addReg(Op1, Op1IsKill * RegState::Kill));
325 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 TII.get(TargetOpcode::COPY), ResultReg)
331 .addReg(II.ImplicitDefs[0]));
332 }
333 return ResultReg;
334}
335
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000336unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
337 const TargetRegisterClass *RC,
338 unsigned Op0, bool Op0IsKill,
339 unsigned Op1, bool Op1IsKill,
340 unsigned Op2, bool Op2IsKill) {
341 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000342 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000343
344 if (II.getNumDefs() >= 1)
345 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
346 .addReg(Op0, Op0IsKill * RegState::Kill)
347 .addReg(Op1, Op1IsKill * RegState::Kill)
348 .addReg(Op2, Op2IsKill * RegState::Kill));
349 else {
350 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
351 .addReg(Op0, Op0IsKill * RegState::Kill)
352 .addReg(Op1, Op1IsKill * RegState::Kill)
353 .addReg(Op2, Op2IsKill * RegState::Kill));
354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
355 TII.get(TargetOpcode::COPY), ResultReg)
356 .addReg(II.ImplicitDefs[0]));
357 }
358 return ResultReg;
359}
360
Eric Christopher0fe7d542010-08-17 01:25:29 +0000361unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
362 const TargetRegisterClass *RC,
363 unsigned Op0, bool Op0IsKill,
364 uint64_t Imm) {
365 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000366 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000367
368 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000369 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000370 .addReg(Op0, Op0IsKill * RegState::Kill)
371 .addImm(Imm));
372 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000374 .addReg(Op0, Op0IsKill * RegState::Kill)
375 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000377 TII.get(TargetOpcode::COPY), ResultReg)
378 .addReg(II.ImplicitDefs[0]));
379 }
380 return ResultReg;
381}
382
383unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
384 const TargetRegisterClass *RC,
385 unsigned Op0, bool Op0IsKill,
386 const ConstantFP *FPImm) {
387 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000388 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000389
390 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000391 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000392 .addReg(Op0, Op0IsKill * RegState::Kill)
393 .addFPImm(FPImm));
394 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000395 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000396 .addReg(Op0, Op0IsKill * RegState::Kill)
397 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000399 TII.get(TargetOpcode::COPY), ResultReg)
400 .addReg(II.ImplicitDefs[0]));
401 }
402 return ResultReg;
403}
404
405unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
406 const TargetRegisterClass *RC,
407 unsigned Op0, bool Op0IsKill,
408 unsigned Op1, bool Op1IsKill,
409 uint64_t Imm) {
410 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000411 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000412
413 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000414 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000415 .addReg(Op0, Op0IsKill * RegState::Kill)
416 .addReg(Op1, Op1IsKill * RegState::Kill)
417 .addImm(Imm));
418 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000419 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000420 .addReg(Op0, Op0IsKill * RegState::Kill)
421 .addReg(Op1, Op1IsKill * RegState::Kill)
422 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000423 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000424 TII.get(TargetOpcode::COPY), ResultReg)
425 .addReg(II.ImplicitDefs[0]));
426 }
427 return ResultReg;
428}
429
430unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
431 const TargetRegisterClass *RC,
432 uint64_t Imm) {
433 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000434 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000435
Eric Christopher0fe7d542010-08-17 01:25:29 +0000436 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000437 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000438 .addImm(Imm));
439 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000441 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000443 TII.get(TargetOpcode::COPY), ResultReg)
444 .addReg(II.ImplicitDefs[0]));
445 }
446 return ResultReg;
447}
448
Eric Christopherd94bc542011-04-29 22:07:50 +0000449unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
450 const TargetRegisterClass *RC,
451 uint64_t Imm1, uint64_t Imm2) {
452 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000453 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000454
Eric Christopherd94bc542011-04-29 22:07:50 +0000455 if (II.getNumDefs() >= 1)
456 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
457 .addImm(Imm1).addImm(Imm2));
458 else {
459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
460 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000462 TII.get(TargetOpcode::COPY),
463 ResultReg)
464 .addReg(II.ImplicitDefs[0]));
465 }
466 return ResultReg;
467}
468
Eric Christopher0fe7d542010-08-17 01:25:29 +0000469unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
470 unsigned Op0, bool Op0IsKill,
471 uint32_t Idx) {
472 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
473 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
474 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000475 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000476 DL, TII.get(TargetOpcode::COPY), ResultReg)
477 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
478 return ResultReg;
479}
480
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000481// TODO: Don't worry about 64-bit now, but when this is fixed remove the
482// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000483unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000484 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000485
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000486 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
488 TII.get(ARM::VMOVRS), MoveReg)
489 .addReg(SrcReg));
490 return MoveReg;
491}
492
493unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000494 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000495
Eric Christopheraa3ace12010-09-09 20:49:25 +0000496 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
497 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000498 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000499 .addReg(SrcReg));
500 return MoveReg;
501}
502
Eric Christopher9ed58df2010-09-09 00:19:41 +0000503// For double width floating point we need to materialize two constants
504// (the high and the low) into integer registers then use a move to get
505// the combined constant into an FP reg.
506unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
507 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000508 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000509
Eric Christopher9ed58df2010-09-09 00:19:41 +0000510 // This checks to see if we can use VFP3 instructions to materialize
511 // a constant, otherwise we have to go through the constant pool.
512 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000513 int Imm;
514 unsigned Opc;
515 if (is64bit) {
516 Imm = ARM_AM::getFP64Imm(Val);
517 Opc = ARM::FCONSTD;
518 } else {
519 Imm = ARM_AM::getFP32Imm(Val);
520 Opc = ARM::FCONSTS;
521 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000522 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
523 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
524 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000525 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000526 return DestReg;
527 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000528
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000529 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000530 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000531
Eric Christopher238bb162010-09-09 23:50:00 +0000532 // MachineConstantPool wants an explicit alignment.
533 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
534 if (Align == 0) {
535 // TODO: Figure out if this is correct.
536 Align = TD.getTypeAllocSize(CFP->getType());
537 }
538 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
539 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
540 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000541
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000542 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000543 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
544 DestReg)
545 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000546 .addReg(0));
547 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000548}
549
Eric Christopher744c7c82010-09-28 22:47:54 +0000550unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000551
Chad Rosier44e89572011-11-04 22:29:00 +0000552 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
553 return false;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000554
555 // If we can do this in a single instruction without a constant pool entry
556 // do so now.
557 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiera4e07272011-11-04 23:09:49 +0000558 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000559 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier4e89d972011-11-11 00:36:21 +0000560 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000561 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier44e89572011-11-04 22:29:00 +0000562 TII.get(Opc), ImmReg)
Chad Rosier42536af2011-11-05 20:16:15 +0000563 .addImm(CI->getZExtValue()));
Chad Rosier44e89572011-11-04 22:29:00 +0000564 return ImmReg;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000565 }
566
Chad Rosier4e89d972011-11-11 00:36:21 +0000567 // Use MVN to emit negative constants.
568 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
569 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosier1c47de82011-11-11 06:27:41 +0000570 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier4e89d972011-11-11 00:36:21 +0000571 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier1c47de82011-11-11 06:27:41 +0000572 if (UseImm) {
Chad Rosier4e89d972011-11-11 00:36:21 +0000573 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
574 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
576 TII.get(Opc), ImmReg)
577 .addImm(Imm));
578 return ImmReg;
579 }
580 }
581
582 // Load from constant pool. For now 32-bit only.
Chad Rosier44e89572011-11-04 22:29:00 +0000583 if (VT != MVT::i32)
584 return false;
585
586 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
587
Eric Christopher56d2b722010-09-02 23:43:26 +0000588 // MachineConstantPool wants an explicit alignment.
589 unsigned Align = TD.getPrefTypeAlignment(C->getType());
590 if (Align == 0) {
591 // TODO: Figure out if this is correct.
592 Align = TD.getTypeAllocSize(C->getType());
593 }
594 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000595
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000596 if (isThumb2)
Eric Christopher56d2b722010-09-02 23:43:26 +0000597 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000598 TII.get(ARM::t2LDRpci), DestReg)
599 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000600 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000601 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000602 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000603 TII.get(ARM::LDRcp), DestReg)
604 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000605 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000606
Eric Christopher56d2b722010-09-02 23:43:26 +0000607 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000608}
609
Eric Christopherc9932f62010-10-01 23:24:42 +0000610unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000611 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000612 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000613
Eric Christopher890dbbe2010-10-02 00:32:44 +0000614 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000615
Eric Christopher890dbbe2010-10-02 00:32:44 +0000616 // TODO: Need more magic for ARM PIC.
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000617 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000618
Eric Christopher890dbbe2010-10-02 00:32:44 +0000619 // MachineConstantPool wants an explicit alignment.
620 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
621 if (Align == 0) {
622 // TODO: Figure out if this is correct.
623 Align = TD.getTypeAllocSize(GV->getType());
624 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000625
Eric Christopher890dbbe2010-10-02 00:32:44 +0000626 // Grab index.
627 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000628 unsigned Id = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +0000629 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
630 ARMCP::CPValue,
631 PCAdj);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000632 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000633
Eric Christopher890dbbe2010-10-02 00:32:44 +0000634 // Load value.
635 MachineInstrBuilder MIB;
636 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000637 if (isThumb2) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000638 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
639 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
640 .addConstantPoolIndex(Idx);
641 if (RelocM == Reloc::PIC_)
642 MIB.addImm(Id);
643 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000644 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000645 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
646 DestReg)
647 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000648 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000649 }
650 AddOptionalDefs(MIB);
Eli Friedmand6412c92011-06-03 01:13:19 +0000651
652 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
653 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000654 if (isThumb2)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000655 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
656 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000657 .addReg(DestReg)
658 .addImm(0);
659 else
660 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
661 NewDestReg)
662 .addReg(DestReg)
663 .addImm(0);
664 DestReg = NewDestReg;
665 AddOptionalDefs(MIB);
666 }
667
Eric Christopher890dbbe2010-10-02 00:32:44 +0000668 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000669}
670
Eric Christopher9ed58df2010-09-09 00:19:41 +0000671unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
672 EVT VT = TLI.getValueType(C->getType(), true);
673
674 // Only handle simple types.
675 if (!VT.isSimple()) return 0;
676
677 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
678 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000679 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
680 return ARMMaterializeGV(GV, VT);
681 else if (isa<ConstantInt>(C))
682 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000683
Eric Christopherc9932f62010-10-01 23:24:42 +0000684 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000685}
686
Eric Christopherf9764fa2010-09-30 20:49:44 +0000687unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
688 // Don't handle dynamic allocas.
689 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000690
Duncan Sands1440e8b2010-11-03 11:35:31 +0000691 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000692 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000693
Eric Christopherf9764fa2010-09-30 20:49:44 +0000694 DenseMap<const AllocaInst*, int>::iterator SI =
695 FuncInfo.StaticAllocaMap.find(AI);
696
697 // This will get lowered later into the correct offsets and registers
698 // via rewriteXFrameIndex.
699 if (SI != FuncInfo.StaticAllocaMap.end()) {
700 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
701 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000702 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopherf9764fa2010-09-30 20:49:44 +0000703 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
704 TII.get(Opc), ResultReg)
705 .addFrameIndex(SI->second)
706 .addImm(0));
707 return ResultReg;
708 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000709
Eric Christopherf9764fa2010-09-30 20:49:44 +0000710 return 0;
711}
712
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000713bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000714 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000715
Eric Christopherb1cc8482010-08-25 07:23:49 +0000716 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000717 if (evt == MVT::Other || !evt.isSimple()) return false;
718 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000719
Eric Christopherdc908042010-08-31 01:28:42 +0000720 // Handle all legal types, i.e. a register that will directly hold this
721 // value.
722 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000723}
724
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000725bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000726 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000727
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000728 // If this is a type than can be sign or zero-extended to a basic operation
729 // go ahead and accept it now.
Chad Rosierb29b9502011-11-13 02:23:59 +0000730 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000731 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000732
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000733 return false;
734}
735
Eric Christopher88de86b2010-11-19 22:36:41 +0000736// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000737bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000738 // Some boilerplate from the X86 FastISel.
739 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000740 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000741 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000742 // Don't walk into other basic blocks unless the object is an alloca from
743 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000744 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
745 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
746 Opcode = I->getOpcode();
747 U = I;
748 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000749 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000750 Opcode = C->getOpcode();
751 U = C;
752 }
753
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000754 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000755 if (Ty->getAddressSpace() > 255)
756 // Fast instruction selection doesn't support the special
757 // address spaces.
758 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000759
Eric Christopher83007122010-08-23 21:44:12 +0000760 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000761 default:
Eric Christopher83007122010-08-23 21:44:12 +0000762 break;
Eric Christopher55324332010-10-12 00:43:21 +0000763 case Instruction::BitCast: {
764 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000765 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000766 }
767 case Instruction::IntToPtr: {
768 // Look past no-op inttoptrs.
769 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000770 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000771 break;
772 }
773 case Instruction::PtrToInt: {
774 // Look past no-op ptrtoints.
775 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000776 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000777 break;
778 }
Eric Christophereae84392010-10-14 09:29:41 +0000779 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000780 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000781 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000782
Eric Christophereae84392010-10-14 09:29:41 +0000783 // Iterate through the GEP folding the constants into offsets where
784 // we can.
785 gep_type_iterator GTI = gep_type_begin(U);
786 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
787 i != e; ++i, ++GTI) {
788 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000789 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000790 const StructLayout *SL = TD.getStructLayout(STy);
791 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
792 TmpOffset += SL->getElementOffset(Idx);
793 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000794 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000795 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000796 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
797 // Constant-offset addressing.
798 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000799 break;
800 }
801 if (isa<AddOperator>(Op) &&
802 (!isa<Instruction>(Op) ||
803 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
804 == FuncInfo.MBB) &&
805 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000806 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000807 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000808 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000809 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000810 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000811 // Iterate on the other operand.
812 Op = cast<AddOperator>(Op)->getOperand(0);
813 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000814 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000815 // Unsupported
816 goto unsupported_gep;
817 }
Eric Christophereae84392010-10-14 09:29:41 +0000818 }
819 }
Eric Christopher2896df82010-10-15 18:02:07 +0000820
821 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000822 Addr.Offset = TmpOffset;
823 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000824
825 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000826 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000827
Eric Christophereae84392010-10-14 09:29:41 +0000828 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000829 break;
830 }
Eric Christopher83007122010-08-23 21:44:12 +0000831 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000832 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000833 DenseMap<const AllocaInst*, int>::iterator SI =
834 FuncInfo.StaticAllocaMap.find(AI);
835 if (SI != FuncInfo.StaticAllocaMap.end()) {
836 Addr.BaseType = Address::FrameIndexBase;
837 Addr.Base.FI = SI->second;
838 return true;
839 }
840 break;
Eric Christopher83007122010-08-23 21:44:12 +0000841 }
842 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000843
Eric Christophera9c57512010-10-13 21:41:51 +0000844 // Materialize the global variable's address into a reg which can
845 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000846 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000847 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
848 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000849
Eric Christopher0d581222010-11-19 22:30:02 +0000850 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000851 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000852 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000853
Eric Christophercb0b04b2010-08-24 00:07:24 +0000854 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000855 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
856 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000857}
858
Chad Rosierb29b9502011-11-13 02:23:59 +0000859void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000860
Eric Christopher212ae932010-10-21 19:40:30 +0000861 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000862
Eric Christopher212ae932010-10-21 19:40:30 +0000863 bool needsLowering = false;
864 switch (VT.getSimpleVT().SimpleTy) {
865 default:
866 assert(false && "Unhandled load/store type!");
Chad Rosier73463472011-11-09 21:30:12 +0000867 break;
Eric Christopher212ae932010-10-21 19:40:30 +0000868 case MVT::i1:
869 case MVT::i8:
Chad Rosierb29b9502011-11-13 02:23:59 +0000870 case MVT::i16:
Eric Christopher212ae932010-10-21 19:40:30 +0000871 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +0000872 if (!useAM3) {
Chad Rosierb29b9502011-11-13 02:23:59 +0000873 // Integer loads/stores handle 12-bit offsets.
874 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosier57b29972011-11-14 20:22:27 +0000875 // Handle negative offsets.
Chad Rosiere489af82011-11-14 22:34:48 +0000876 if (needsLowering && isThumb2)
877 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
878 Addr.Offset > -256);
Chad Rosier57b29972011-11-14 20:22:27 +0000879 } else {
Chad Rosier5be833d2011-11-13 04:25:02 +0000880 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000881 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosier57b29972011-11-14 20:22:27 +0000882 }
Eric Christopher212ae932010-10-21 19:40:30 +0000883 break;
884 case MVT::f32:
885 case MVT::f64:
886 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000887 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000888 break;
889 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000890
Eric Christopher827656d2010-11-20 22:38:27 +0000891 // If this is a stack pointer and the offset needs to be simplified then
892 // put the alloca address into a register, set the base type back to
893 // register and continue. This should almost never happen.
894 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000895 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher827656d2010-11-20 22:38:27 +0000896 ARM::GPRRegisterClass;
897 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000898 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Eric Christopher827656d2010-11-20 22:38:27 +0000899 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
900 TII.get(Opc), ResultReg)
901 .addFrameIndex(Addr.Base.FI)
902 .addImm(0));
903 Addr.Base.Reg = ResultReg;
904 Addr.BaseType = Address::RegBase;
905 }
906
Eric Christopher212ae932010-10-21 19:40:30 +0000907 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000908 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000909 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000910 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
911 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000912 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000913 }
Eric Christopher83007122010-08-23 21:44:12 +0000914}
915
Eric Christopher564857f2010-12-01 01:40:24 +0000916void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000917 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000918 unsigned Flags, bool useAM3) {
Eric Christopher564857f2010-12-01 01:40:24 +0000919 // addrmode5 output depends on the selection dag addressing dividing the
920 // offset by 4 that it then later multiplies. Do this here as well.
921 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
922 VT.getSimpleVT().SimpleTy == MVT::f64)
923 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000924
Eric Christopher564857f2010-12-01 01:40:24 +0000925 // Frame base works a bit differently. Handle it separately.
926 if (Addr.BaseType == Address::FrameIndexBase) {
927 int FI = Addr.Base.FI;
928 int Offset = Addr.Offset;
929 MachineMemOperand *MMO =
930 FuncInfo.MF->getMachineMemOperand(
931 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000932 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000933 MFI.getObjectSize(FI),
934 MFI.getObjectAlignment(FI));
935 // Now add the rest of the operands.
936 MIB.addFrameIndex(FI);
937
Chad Rosier5be833d2011-11-13 04:25:02 +0000938 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000939 if (useAM3) {
940 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
941 MIB.addReg(0);
942 MIB.addImm(Imm);
943 } else {
944 MIB.addImm(Addr.Offset);
945 }
Eric Christopher564857f2010-12-01 01:40:24 +0000946 MIB.addMemOperand(MMO);
947 } else {
948 // Now add the rest of the operands.
949 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000950
Chad Rosier5be833d2011-11-13 04:25:02 +0000951 // ARM halfword load/stores and signed byte loads need an additional operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000952 if (useAM3) {
953 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
954 MIB.addReg(0);
955 MIB.addImm(Imm);
956 } else {
957 MIB.addImm(Addr.Offset);
958 }
Eric Christopher564857f2010-12-01 01:40:24 +0000959 }
960 AddOptionalDefs(MIB);
961}
962
Chad Rosierb29b9502011-11-13 02:23:59 +0000963bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
964 bool isZExt = true, bool allocReg = true) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000965 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000966 unsigned Opc;
Chad Rosierb29b9502011-11-13 02:23:59 +0000967 bool useAM3 = false;
968 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000969 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000970 // This is mostly going to be Neon/vector support.
971 default: return false;
Chad Rosier646abbf2011-11-11 02:38:59 +0000972 case MVT::i1:
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000973 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +0000974 if (isThumb2) {
975 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
976 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
977 else
978 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierb29b9502011-11-13 02:23:59 +0000979 } else {
Chad Rosier57b29972011-11-14 20:22:27 +0000980 if (isZExt) {
981 Opc = ARM::LDRBi12;
982 } else {
983 Opc = ARM::LDRSB;
984 useAM3 = true;
985 }
Chad Rosierb29b9502011-11-13 02:23:59 +0000986 }
Eric Christopher7a56f332010-10-08 01:13:17 +0000987 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000988 break;
Chad Rosier73463472011-11-09 21:30:12 +0000989 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +0000990 if (isThumb2) {
991 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
992 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
993 else
994 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
995 } else {
996 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
997 useAM3 = true;
998 }
Chad Rosier73463472011-11-09 21:30:12 +0000999 RC = ARM::GPRRegisterClass;
1000 break;
Eric Christopherdc908042010-08-31 01:28:42 +00001001 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001002 if (isThumb2) {
1003 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1004 Opc = ARM::t2LDRi8;
1005 else
1006 Opc = ARM::t2LDRi12;
1007 } else {
1008 Opc = ARM::LDRi12;
1009 }
Eric Christopher7a56f332010-10-08 01:13:17 +00001010 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +00001011 break;
Eric Christopher6dab1372010-09-18 01:59:37 +00001012 case MVT::f32:
1013 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +00001014 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001015 break;
1016 case MVT::f64:
1017 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +00001018 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001019 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001020 }
Eric Christopher564857f2010-12-01 01:40:24 +00001021 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001022 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001023
Eric Christopher564857f2010-12-01 01:40:24 +00001024 // Create the base instruction, then add the operands.
Chad Rosierb29b9502011-11-13 02:23:59 +00001025 if (allocReg)
1026 ResultReg = createResultReg(RC);
1027 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher564857f2010-12-01 01:40:24 +00001028 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1029 TII.get(Opc), ResultReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001030 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Eric Christopherdc908042010-08-31 01:28:42 +00001031 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001032}
1033
Eric Christopher43b62be2010-09-27 06:02:23 +00001034bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +00001035 // Atomic loads need special handling.
1036 if (cast<LoadInst>(I)->isAtomic())
1037 return false;
1038
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001039 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001040 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001041 if (!isLoadTypeLegal(I->getType(), VT))
1042 return false;
1043
Eric Christopher564857f2010-12-01 01:40:24 +00001044 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001045 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +00001046 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001047
1048 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +00001049 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001050 UpdateValueMap(I, ResultReg);
1051 return true;
1052}
1053
Eric Christopher0d581222010-11-19 22:30:02 +00001054bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001055 unsigned StrOpc;
Chad Rosierb29b9502011-11-13 02:23:59 +00001056 bool useAM3 = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001057 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001058 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +00001059 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +00001060 case MVT::i1: {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001061 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
Eric Christopher4c914122010-11-02 23:59:09 +00001062 ARM::GPRRegisterClass);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001063 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eric Christopher4c914122010-11-02 23:59:09 +00001064 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1065 TII.get(Opc), Res)
1066 .addReg(SrcReg).addImm(1));
1067 SrcReg = Res;
1068 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +00001069 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +00001070 if (isThumb2) {
1071 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1072 StrOpc = ARM::t2STRBi8;
1073 else
1074 StrOpc = ARM::t2STRBi12;
1075 } else {
1076 StrOpc = ARM::STRBi12;
1077 }
Eric Christopher15418772010-10-12 05:39:06 +00001078 break;
1079 case MVT::i16:
Chad Rosier57b29972011-11-14 20:22:27 +00001080 if (isThumb2) {
1081 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1082 StrOpc = ARM::t2STRHi8;
1083 else
1084 StrOpc = ARM::t2STRHi12;
1085 } else {
1086 StrOpc = ARM::STRH;
1087 useAM3 = true;
1088 }
Eric Christopher15418772010-10-12 05:39:06 +00001089 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001090 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +00001091 if (isThumb2) {
1092 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1093 StrOpc = ARM::t2STRi8;
1094 else
1095 StrOpc = ARM::t2STRi12;
1096 } else {
1097 StrOpc = ARM::STRi12;
1098 }
Eric Christopher47650ec2010-10-16 01:10:35 +00001099 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001100 case MVT::f32:
1101 if (!Subtarget->hasVFP2()) return false;
1102 StrOpc = ARM::VSTRS;
1103 break;
1104 case MVT::f64:
1105 if (!Subtarget->hasVFP2()) return false;
1106 StrOpc = ARM::VSTRD;
1107 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001108 }
Eric Christopher564857f2010-12-01 01:40:24 +00001109 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001110 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001111
Eric Christopher564857f2010-12-01 01:40:24 +00001112 // Create the base instruction, then add the operands.
1113 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1114 TII.get(StrOpc))
Chad Rosier3bdb3c92011-11-17 01:16:53 +00001115 .addReg(SrcReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001116 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001117 return true;
1118}
1119
Eric Christopher43b62be2010-09-27 06:02:23 +00001120bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001121 Value *Op0 = I->getOperand(0);
1122 unsigned SrcReg = 0;
1123
Eli Friedman4136d232011-09-02 22:33:24 +00001124 // Atomic stores need special handling.
1125 if (cast<StoreInst>(I)->isAtomic())
1126 return false;
1127
Eric Christopher564857f2010-12-01 01:40:24 +00001128 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001129 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001130 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001131 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001132
Eric Christopher1b61ef42010-09-02 01:48:11 +00001133 // Get the value to be stored into a register.
1134 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001135 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001136
Eric Christopher564857f2010-12-01 01:40:24 +00001137 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001138 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001139 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001140 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001141
Eric Christopher0d581222010-11-19 22:30:02 +00001142 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001143 return true;
1144}
1145
1146static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1147 switch (Pred) {
1148 // Needs two compares...
1149 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001150 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001151 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001152 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001153 return ARMCC::AL;
1154 case CmpInst::ICMP_EQ:
1155 case CmpInst::FCMP_OEQ:
1156 return ARMCC::EQ;
1157 case CmpInst::ICMP_SGT:
1158 case CmpInst::FCMP_OGT:
1159 return ARMCC::GT;
1160 case CmpInst::ICMP_SGE:
1161 case CmpInst::FCMP_OGE:
1162 return ARMCC::GE;
1163 case CmpInst::ICMP_UGT:
1164 case CmpInst::FCMP_UGT:
1165 return ARMCC::HI;
1166 case CmpInst::FCMP_OLT:
1167 return ARMCC::MI;
1168 case CmpInst::ICMP_ULE:
1169 case CmpInst::FCMP_OLE:
1170 return ARMCC::LS;
1171 case CmpInst::FCMP_ORD:
1172 return ARMCC::VC;
1173 case CmpInst::FCMP_UNO:
1174 return ARMCC::VS;
1175 case CmpInst::FCMP_UGE:
1176 return ARMCC::PL;
1177 case CmpInst::ICMP_SLT:
1178 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001179 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001180 case CmpInst::ICMP_SLE:
1181 case CmpInst::FCMP_ULE:
1182 return ARMCC::LE;
1183 case CmpInst::FCMP_UNE:
1184 case CmpInst::ICMP_NE:
1185 return ARMCC::NE;
1186 case CmpInst::ICMP_UGE:
1187 return ARMCC::HS;
1188 case CmpInst::ICMP_ULT:
1189 return ARMCC::LO;
1190 }
Eric Christopher543cf052010-09-01 22:16:27 +00001191}
1192
Eric Christopher43b62be2010-09-27 06:02:23 +00001193bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001194 const BranchInst *BI = cast<BranchInst>(I);
1195 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1196 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001197
Eric Christophere5734102010-09-03 00:35:47 +00001198 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001199
Eric Christopher0e6233b2010-10-29 21:08:19 +00001200 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1201 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001202 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001203 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001204
1205 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001206 // Try to take advantage of fallthrough opportunities.
1207 CmpInst::Predicate Predicate = CI->getPredicate();
1208 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1209 std::swap(TBB, FBB);
1210 Predicate = CmpInst::getInversePredicate(Predicate);
1211 }
1212
1213 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001214
1215 // We may not handle every CC for now.
1216 if (ARMPred == ARMCC::AL) return false;
1217
Chad Rosier75698f32011-10-26 23:17:28 +00001218 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001219 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier75698f32011-10-26 23:17:28 +00001220 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001221
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001222 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1224 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1225 FastEmitBranch(FBB, DL);
1226 FuncInfo.MBB->addSuccessor(TBB);
1227 return true;
1228 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001229 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1230 MVT SourceVT;
1231 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001232 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001233 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001234 unsigned OpReg = getRegForValue(TI->getOperand(0));
1235 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1236 TII.get(TstOpc))
1237 .addReg(OpReg).addImm(1));
1238
1239 unsigned CCMode = ARMCC::NE;
1240 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1241 std::swap(TBB, FBB);
1242 CCMode = ARMCC::EQ;
1243 }
1244
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001245 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1247 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1248
1249 FastEmitBranch(FBB, DL);
1250 FuncInfo.MBB->addSuccessor(TBB);
1251 return true;
1252 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001253 } else if (const ConstantInt *CI =
1254 dyn_cast<ConstantInt>(BI->getCondition())) {
1255 uint64_t Imm = CI->getZExtValue();
1256 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1257 FastEmitBranch(Target, DL);
1258 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001259 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001260
Eric Christopher0e6233b2010-10-29 21:08:19 +00001261 unsigned CmpReg = getRegForValue(BI->getCondition());
1262 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001263
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001264 // We've been divorced from our compare! Our block was split, and
1265 // now our compare lives in a predecessor block. We musn't
1266 // re-compare here, as the children of the compare aren't guaranteed
1267 // live across the block boundary (we *could* check for this).
1268 // Regardless, the compare has been done in the predecessor block,
1269 // and it left a value for us in a virtual register. Ergo, we test
1270 // the one-bit value left in the virtual register.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001271 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001272 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1273 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001274
Eric Christopher7a20a372011-04-28 16:52:09 +00001275 unsigned CCMode = ARMCC::NE;
1276 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1277 std::swap(TBB, FBB);
1278 CCMode = ARMCC::EQ;
1279 }
1280
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001281 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001282 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001283 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001284 FastEmitBranch(FBB, DL);
1285 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001286 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001287}
1288
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001289bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1290 bool isZExt) {
Chad Rosierade62002011-10-26 23:25:44 +00001291 Type *Ty = Src1Value->getType();
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001292 EVT SrcVT = TLI.getValueType(Ty, true);
1293 if (!SrcVT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001294
Chad Rosierade62002011-10-26 23:25:44 +00001295 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1296 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001297 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001298
Chad Rosier2f2fe412011-11-09 03:22:02 +00001299 // Check to see if the 2nd operand is a constant that we can encode directly
1300 // in the compare.
Chad Rosier1c47de82011-11-11 06:27:41 +00001301 int Imm = 0;
1302 bool UseImm = false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001303 bool isNegativeImm = false;
Chad Rosierf56c60b2011-11-16 00:32:20 +00001304 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1305 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier2f2fe412011-11-09 03:22:02 +00001306 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 Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001672 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001673 switch (ISDOpcode) {
1674 default: return false;
1675 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001676 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001677 break;
1678 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001679 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001680 break;
1681 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001682 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001683 break;
1684 }
Chad Rosier508a1f42011-11-16 18:39:44 +00001685 unsigned Op1 = getRegForValue(I->getOperand(0));
1686 if (Op1 == 0) return false;
1687
1688 unsigned Op2 = getRegForValue(I->getOperand(1));
1689 if (Op2 == 0) return false;
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
1701// This is largely taken directly from CCAssignFnForNode - we don't support
1702// varargs in FastISel so that part has been removed.
1703// TODO: We may not support all of this.
1704CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1705 switch (CC) {
1706 default:
1707 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001708 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001709 // Ignore fastcc. Silence compiler warnings.
1710 (void)RetFastCC_ARM_APCS;
1711 (void)FastCC_ARM_APCS;
1712 // Fallthrough
1713 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001714 // Use target triple & subtarget features to do actual dispatch.
1715 if (Subtarget->isAAPCS_ABI()) {
1716 if (Subtarget->hasVFP2() &&
1717 FloatABIType == FloatABI::Hard)
1718 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1719 else
1720 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1721 } else
1722 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1723 case CallingConv::ARM_AAPCS_VFP:
1724 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1725 case CallingConv::ARM_AAPCS:
1726 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1727 case CallingConv::ARM_APCS:
1728 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1729 }
1730}
1731
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001732bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1733 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001734 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001735 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1736 SmallVectorImpl<unsigned> &RegArgs,
1737 CallingConv::ID CC,
1738 unsigned &NumBytes) {
1739 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001740 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001741 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1742
1743 // Get a count of how many bytes are to be pushed on the stack.
1744 NumBytes = CCInfo.getNextStackOffset();
1745
1746 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001747 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001748 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1749 TII.get(AdjStackDown))
1750 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001751
1752 // Process the args.
1753 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1754 CCValAssign &VA = ArgLocs[i];
1755 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001756 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001757
Eric Christopher4a2b3162011-01-27 05:44:56 +00001758 // We don't handle NEON/vector parameters yet.
1759 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001760 return false;
1761
Eric Christopherf9764fa2010-09-30 20:49:44 +00001762 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001763 switch (VA.getLocInfo()) {
1764 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001765 case CCValAssign::SExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001766 EVT DestVT = VA.getLocVT();
1767 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1768 /*isZExt*/false);
1769 assert (ResultReg != 0 && "Failed to emit a sext");
1770 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001771 break;
1772 }
Chad Rosier42536af2011-11-05 20:16:15 +00001773 case CCValAssign::AExt:
1774 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherfa87d662010-10-18 02:17:53 +00001775 case CCValAssign::ZExt: {
Chad Rosier42536af2011-11-05 20:16:15 +00001776 EVT DestVT = VA.getLocVT();
1777 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1778 /*isZExt*/true);
1779 assert (ResultReg != 0 && "Failed to emit a sext");
1780 Arg = ResultReg;
Eric Christopherfa87d662010-10-18 02:17:53 +00001781 break;
1782 }
1783 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001784 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001785 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001786 assert(BC != 0 && "Failed to emit a bitcast!");
1787 Arg = BC;
1788 ArgVT = VA.getLocVT();
1789 break;
1790 }
1791 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001792 }
1793
1794 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001795 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001796 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001797 VA.getLocReg())
Chad Rosier42536af2011-11-05 20:16:15 +00001798 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001799 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001800 } else if (VA.needsCustom()) {
1801 // TODO: We need custom lowering for vector (v2f64) args.
1802 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001803
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001804 CCValAssign &NextVA = ArgLocs[++i];
1805
1806 // TODO: Only handle register args for now.
1807 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1808
1809 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1810 TII.get(ARM::VMOVRRD), VA.getLocReg())
1811 .addReg(NextVA.getLocReg(), RegState::Define)
1812 .addReg(Arg));
1813 RegArgs.push_back(VA.getLocReg());
1814 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001815 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001816 assert(VA.isMemLoc());
1817 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001818 Address Addr;
1819 Addr.BaseType = Address::RegBase;
1820 Addr.Base.Reg = ARM::SP;
1821 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001822
Eric Christopher0d581222010-11-19 22:30:02 +00001823 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001824 }
1825 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001826 return true;
1827}
1828
Duncan Sands1440e8b2010-11-03 11:35:31 +00001829bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001830 const Instruction *I, CallingConv::ID CC,
1831 unsigned &NumBytes) {
1832 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001833 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001834 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1835 TII.get(AdjStackUp))
1836 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001837
1838 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001839 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001840 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001841 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001842 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1843
1844 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001845 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001846 // For this move we copy into two registers and then move into the
1847 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001848 EVT DestVT = RVLocs[0].getValVT();
1849 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1850 unsigned ResultReg = createResultReg(DstRC);
1851 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1852 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001853 .addReg(RVLocs[0].getLocReg())
1854 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001855
Eric Christopher3659ac22010-10-20 08:02:24 +00001856 UsedRegs.push_back(RVLocs[0].getLocReg());
1857 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001858
Eric Christopherdccd2c32010-10-11 08:38:55 +00001859 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001860 UpdateValueMap(I, ResultReg);
1861 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001862 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001863 EVT CopyVT = RVLocs[0].getValVT();
Chad Rosier0eff39f2011-11-08 00:03:32 +00001864
1865 // Special handling for extended integers.
1866 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1867 CopyVT = MVT::i32;
1868
Eric Christopher14df8822010-10-01 00:00:11 +00001869 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001870
Eric Christopher14df8822010-10-01 00:00:11 +00001871 unsigned ResultReg = createResultReg(DstRC);
1872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1873 ResultReg).addReg(RVLocs[0].getLocReg());
1874 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001875
Eric Christopherdccd2c32010-10-11 08:38:55 +00001876 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001877 UpdateValueMap(I, ResultReg);
1878 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001879 }
1880
Eric Christopherdccd2c32010-10-11 08:38:55 +00001881 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001882}
1883
Eric Christopher4f512ef2010-10-22 01:28:00 +00001884bool ARMFastISel::SelectRet(const Instruction *I) {
1885 const ReturnInst *Ret = cast<ReturnInst>(I);
1886 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001887
Eric Christopher4f512ef2010-10-22 01:28:00 +00001888 if (!FuncInfo.CanLowerReturn)
1889 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001890
Eric Christopher4f512ef2010-10-22 01:28:00 +00001891 if (F.isVarArg())
1892 return false;
1893
1894 CallingConv::ID CC = F.getCallingConv();
1895 if (Ret->getNumOperands() > 0) {
1896 SmallVector<ISD::OutputArg, 4> Outs;
1897 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1898 Outs, TLI);
1899
1900 // Analyze operands of the call, assigning locations to each operand.
1901 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00001902 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Eric Christopher4f512ef2010-10-22 01:28:00 +00001903 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1904
1905 const Value *RV = Ret->getOperand(0);
1906 unsigned Reg = getRegForValue(RV);
1907 if (Reg == 0)
1908 return false;
1909
1910 // Only handle a single return value for now.
1911 if (ValLocs.size() != 1)
1912 return false;
1913
1914 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001915
Eric Christopher4f512ef2010-10-22 01:28:00 +00001916 // Don't bother handling odd stuff for now.
1917 if (VA.getLocInfo() != CCValAssign::Full)
1918 return false;
1919 // Only handle register returns for now.
1920 if (!VA.isRegLoc())
1921 return false;
Chad Rosierf470cbb2011-11-04 00:50:21 +00001922
1923 unsigned SrcReg = Reg + VA.getValNo();
1924 EVT RVVT = TLI.getValueType(RV->getType());
1925 EVT DestVT = VA.getValVT();
1926 // Special handling for extended integers.
1927 if (RVVT != DestVT) {
1928 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1929 return false;
1930
1931 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1932 return false;
1933
1934 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1935
1936 bool isZExt = Outs[0].Flags.isZExt();
1937 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1938 if (ResultReg == 0) return false;
1939 SrcReg = ResultReg;
1940 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001941
Eric Christopher4f512ef2010-10-22 01:28:00 +00001942 // Make the copy.
Eric Christopher4f512ef2010-10-22 01:28:00 +00001943 unsigned DstReg = VA.getLocReg();
1944 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1945 // Avoid a cross-class copy. This is very unlikely.
1946 if (!SrcRC->contains(DstReg))
1947 return false;
1948 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1949 DstReg).addReg(SrcReg);
1950
1951 // Mark the register as live out of the function.
1952 MRI.addLiveOut(VA.getLocReg());
1953 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001954
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001955 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Eric Christopher4f512ef2010-10-22 01:28:00 +00001956 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1957 TII.get(RetOpc)));
1958 return true;
1959}
1960
Eric Christopher872f4a22011-02-22 01:37:10 +00001961unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1962
Eric Christopher872f4a22011-02-22 01:37:10 +00001963 // Darwin needs the r9 versions of the opcodes.
1964 bool isDarwin = Subtarget->isTargetDarwin();
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001965 if (isThumb2) {
Eric Christopher872f4a22011-02-22 01:37:10 +00001966 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1967 } else {
1968 return isDarwin ? ARM::BLr9 : ARM::BL;
1969 }
1970}
1971
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001972// A quick function that will emit a call for a named libcall in F with the
1973// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001974// can emit a call for any libcall we can produce. This is an abridged version
1975// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001976// like computed function pointers or strange arguments at call sites.
1977// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1978// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001979bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1980 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001981
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001982 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001983 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001984 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001985 if (RetTy->isVoidTy())
1986 RetVT = MVT::isVoid;
1987 else if (!isTypeLegal(RetTy, RetVT))
1988 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001989
Eric Christopher836c6242010-12-15 23:47:29 +00001990 // TODO: For now if we have long calls specified we don't handle the call.
1991 if (EnableARMLongCalls) return false;
1992
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001993 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001994 SmallVector<Value*, 8> Args;
1995 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001996 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001997 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1998 Args.reserve(I->getNumOperands());
1999 ArgRegs.reserve(I->getNumOperands());
2000 ArgVTs.reserve(I->getNumOperands());
2001 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00002002 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002003 Value *Op = I->getOperand(i);
2004 unsigned Arg = getRegForValue(Op);
2005 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002006
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002007 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002008 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002009 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002010
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002011 ISD::ArgFlagsTy Flags;
2012 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2013 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002014
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002015 Args.push_back(Op);
2016 ArgRegs.push_back(Arg);
2017 ArgVTs.push_back(ArgVT);
2018 ArgFlags.push_back(Flags);
2019 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002020
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002021 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002022 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002023 unsigned NumBytes;
2024 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2025 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002026
Eric Christopher6344a5f2011-04-29 00:07:20 +00002027 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002028 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002029 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002030 unsigned CallOpc = ARMSelectCallOp(NULL);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002031 if(isThumb2)
Eric Christopherc19aadb2010-12-21 03:50:43 +00002032 // Explicitly adding the predicate here.
2033 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2034 TII.get(CallOpc)))
2035 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00002036 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00002037 // Explicitly adding the predicate here.
2038 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2039 TII.get(CallOpc))
2040 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00002041
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002042 // Add implicit physical register uses to the call.
2043 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2044 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002045
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002046 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002047 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002048 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002049
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002050 // Set all unused physreg defs as dead.
2051 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002052
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002053 return true;
2054}
2055
Chad Rosier11add262011-11-11 23:31:03 +00002056bool ARMFastISel::SelectCall(const Instruction *I,
2057 const char *IntrMemName = 0) {
Eric Christopherf9764fa2010-09-30 20:49:44 +00002058 const CallInst *CI = cast<CallInst>(I);
2059 const Value *Callee = CI->getCalledValue();
2060
Chad Rosier11add262011-11-11 23:31:03 +00002061 // Can't handle inline asm.
2062 if (isa<InlineAsm>(Callee)) return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002063
Eric Christopher52f6c032011-05-02 20:16:33 +00002064 // Only handle global variable Callees.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002065 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christopher52f6c032011-05-02 20:16:33 +00002066 if (!GV)
Eric Christophere6ca6772010-10-01 21:33:12 +00002067 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002068
Eric Christopherf9764fa2010-09-30 20:49:44 +00002069 // Check the calling convention.
2070 ImmutableCallSite CS(CI);
2071 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00002072
Eric Christopherf9764fa2010-09-30 20:49:44 +00002073 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00002074
Eric Christopherf9764fa2010-09-30 20:49:44 +00002075 // Let SDISel handle vararg functions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002076 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2077 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eric Christopherf9764fa2010-09-30 20:49:44 +00002078 if (FTy->isVarArg())
2079 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002080
Eric Christopherf9764fa2010-09-30 20:49:44 +00002081 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002082 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002083 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002084 if (RetTy->isVoidTy())
2085 RetVT = MVT::isVoid;
Chad Rosier0eff39f2011-11-08 00:03:32 +00002086 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2087 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002088 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002089
Eric Christopher836c6242010-12-15 23:47:29 +00002090 // TODO: For now if we have long calls specified we don't handle the call.
2091 if (EnableARMLongCalls) return false;
Eric Christopher299bbb22011-04-29 00:03:10 +00002092
Eric Christopherf9764fa2010-09-30 20:49:44 +00002093 // Set up the argument vectors.
2094 SmallVector<Value*, 8> Args;
2095 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002096 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002097 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2098 Args.reserve(CS.arg_size());
2099 ArgRegs.reserve(CS.arg_size());
2100 ArgVTs.reserve(CS.arg_size());
2101 ArgFlags.reserve(CS.arg_size());
2102 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2103 i != e; ++i) {
Chad Rosier11add262011-11-11 23:31:03 +00002104 // If we're lowering a memory intrinsic instead of a regular call, skip the
2105 // last two arguments, which shouldn't be passed to the underlying function.
2106 if (IntrMemName && e-i <= 2)
2107 break;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002108
Chad Rosier11add262011-11-11 23:31:03 +00002109 unsigned Arg = getRegForValue(*i);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002110 if (Arg == 0)
2111 return false;
2112 ISD::ArgFlagsTy Flags;
2113 unsigned AttrInd = i - CS.arg_begin() + 1;
2114 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2115 Flags.setSExt();
2116 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2117 Flags.setZExt();
2118
Chad Rosier8e4a2e42011-11-04 00:58:10 +00002119 // FIXME: Only handle *easy* calls for now.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002120 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2121 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2122 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2123 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2124 return false;
2125
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002126 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002127 MVT ArgVT;
Chad Rosier42536af2011-11-05 20:16:15 +00002128 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2129 ArgVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002130 return false;
2131 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2132 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002133
Eric Christopherf9764fa2010-09-30 20:49:44 +00002134 Args.push_back(*i);
2135 ArgRegs.push_back(Arg);
2136 ArgVTs.push_back(ArgVT);
2137 ArgFlags.push_back(Flags);
2138 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002139
Eric Christopherf9764fa2010-09-30 20:49:44 +00002140 // Handle the arguments now that we've gotten them.
2141 SmallVector<unsigned, 4> RegArgs;
2142 unsigned NumBytes;
2143 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2144 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002145
Eric Christopher6344a5f2011-04-29 00:07:20 +00002146 // Issue the call, BLr9 for darwin, BL otherwise.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002147 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00002148 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00002149 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00002150 // Explicitly adding the predicate here.
Chad Rosier9eb67482011-11-13 09:44:21 +00002151 if(isThumb2) {
Eric Christopherc19aadb2010-12-21 03:50:43 +00002152 // Explicitly adding the predicate here.
2153 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier11add262011-11-11 23:31:03 +00002154 TII.get(CallOpc)));
Chad Rosier9eb67482011-11-13 09:44:21 +00002155 if (!IntrMemName)
2156 MIB.addGlobalAddress(GV, 0, 0);
2157 else
2158 MIB.addExternalSymbol(IntrMemName, 0);
2159 } else {
2160 if (!IntrMemName)
2161 // Explicitly adding the predicate here.
2162 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2163 TII.get(CallOpc))
2164 .addGlobalAddress(GV, 0, 0));
2165 else
2166 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2167 TII.get(CallOpc))
2168 .addExternalSymbol(IntrMemName, 0));
2169 }
Chad Rosier11add262011-11-11 23:31:03 +00002170
Eric Christopherf9764fa2010-09-30 20:49:44 +00002171 // Add implicit physical register uses to the call.
2172 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2173 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002174
Eric Christopherf9764fa2010-09-30 20:49:44 +00002175 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002176 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002177 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002178
Eric Christopherf9764fa2010-09-30 20:49:44 +00002179 // Set all unused physreg defs as dead.
2180 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002181
Eric Christopherf9764fa2010-09-30 20:49:44 +00002182 return true;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002183}
2184
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002185bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002186 return Len <= 16;
2187}
2188
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002189bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002190 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002191 if (!ARMIsMemCpySmall(Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002192 return false;
2193
2194 // We don't care about alignment here since we just emit integer accesses.
2195 while (Len) {
2196 MVT VT;
2197 if (Len >= 4)
2198 VT = MVT::i32;
2199 else if (Len >= 2)
2200 VT = MVT::i16;
2201 else {
2202 assert(Len == 1);
2203 VT = MVT::i8;
2204 }
2205
2206 bool RV;
2207 unsigned ResultReg;
2208 RV = ARMEmitLoad(VT, ResultReg, Src);
2209 assert (RV = true && "Should be able to handle this load.");
2210 RV = ARMEmitStore(VT, ResultReg, Dest);
2211 assert (RV = true && "Should be able to handle this store.");
2212
2213 unsigned Size = VT.getSizeInBits()/8;
2214 Len -= Size;
2215 Dest.Offset += Size;
2216 Src.Offset += Size;
2217 }
2218
2219 return true;
2220}
2221
Chad Rosier11add262011-11-11 23:31:03 +00002222bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2223 // FIXME: Handle more intrinsics.
2224 switch (I.getIntrinsicID()) {
2225 default: return false;
2226 case Intrinsic::memcpy:
2227 case Intrinsic::memmove: {
Chad Rosier11add262011-11-11 23:31:03 +00002228 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2229 // Don't handle volatile.
2230 if (MTI.isVolatile())
2231 return false;
Chad Rosier909cb4f2011-11-14 22:46:17 +00002232
2233 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2234 // we would emit dead code because we don't currently handle memmoves.
2235 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2236 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002237 // Small memcpy's are common enough that we want to do them without a call
2238 // if possible.
Chad Rosier909cb4f2011-11-14 22:46:17 +00002239 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002240 if (ARMIsMemCpySmall(Len)) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002241 Address Dest, Src;
2242 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2243 !ARMComputeAddress(MTI.getRawSource(), Src))
2244 return false;
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002245 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002246 return true;
2247 }
2248 }
Chad Rosier11add262011-11-11 23:31:03 +00002249
2250 if (!MTI.getLength()->getType()->isIntegerTy(32))
2251 return false;
2252
2253 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2254 return false;
2255
2256 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2257 return SelectCall(&I, IntrMemName);
2258 }
2259 case Intrinsic::memset: {
2260 const MemSetInst &MSI = cast<MemSetInst>(I);
2261 // Don't handle volatile.
2262 if (MSI.isVolatile())
2263 return false;
2264
2265 if (!MSI.getLength()->getType()->isIntegerTy(32))
2266 return false;
2267
2268 if (MSI.getDestAddressSpace() > 255)
2269 return false;
2270
2271 return SelectCall(&I, "memset");
2272 }
2273 }
2274 return false;
2275}
2276
Chad Rosier0d7b2312011-11-02 00:18:48 +00002277bool ARMFastISel::SelectTrunc(const Instruction *I) {
2278 // The high bits for a type smaller than the register size are assumed to be
2279 // undefined.
2280 Value *Op = I->getOperand(0);
2281
2282 EVT SrcVT, DestVT;
2283 SrcVT = TLI.getValueType(Op->getType(), true);
2284 DestVT = TLI.getValueType(I->getType(), true);
2285
2286 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2287 return false;
2288 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2289 return false;
2290
2291 unsigned SrcReg = getRegForValue(Op);
2292 if (!SrcReg) return false;
2293
2294 // Because the high bits are undefined, a truncate doesn't generate
2295 // any code.
2296 UpdateValueMap(I, SrcReg);
2297 return true;
2298}
2299
Chad Rosier87633022011-11-02 17:20:24 +00002300unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2301 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002302 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00002303 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002304
2305 unsigned Opc;
Eli Friedman76927d732011-05-25 23:49:02 +00002306 bool isBoolZext = false;
Chad Rosier87633022011-11-02 17:20:24 +00002307 if (!SrcVT.isSimple()) return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002308 switch (SrcVT.getSimpleVT().SimpleTy) {
Chad Rosier87633022011-11-02 17:20:24 +00002309 default: return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002310 case MVT::i16:
Chad Rosier87633022011-11-02 17:20:24 +00002311 if (!Subtarget->hasV6Ops()) return 0;
2312 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002313 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002314 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002315 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Eli Friedman76927d732011-05-25 23:49:02 +00002316 break;
2317 case MVT::i8:
Chad Rosier87633022011-11-02 17:20:24 +00002318 if (!Subtarget->hasV6Ops()) return 0;
2319 if (isZExt)
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002320 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002321 else
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002322 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Eli Friedman76927d732011-05-25 23:49:02 +00002323 break;
2324 case MVT::i1:
Chad Rosier87633022011-11-02 17:20:24 +00002325 if (isZExt) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002326 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eli Friedman76927d732011-05-25 23:49:02 +00002327 isBoolZext = true;
2328 break;
2329 }
Chad Rosier87633022011-11-02 17:20:24 +00002330 return 0;
Eli Friedman76927d732011-05-25 23:49:02 +00002331 }
2332
Chad Rosier87633022011-11-02 17:20:24 +00002333 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eli Friedman76927d732011-05-25 23:49:02 +00002334 MachineInstrBuilder MIB;
Chad Rosier87633022011-11-02 17:20:24 +00002335 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
Eli Friedman76927d732011-05-25 23:49:02 +00002336 .addReg(SrcReg);
2337 if (isBoolZext)
2338 MIB.addImm(1);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00002339 else
2340 MIB.addImm(0);
Eli Friedman76927d732011-05-25 23:49:02 +00002341 AddOptionalDefs(MIB);
Chad Rosier87633022011-11-02 17:20:24 +00002342 return ResultReg;
2343}
2344
2345bool ARMFastISel::SelectIntExt(const Instruction *I) {
2346 // On ARM, in general, integer casts don't involve legal types; this code
2347 // handles promotable integers.
Chad Rosier87633022011-11-02 17:20:24 +00002348 Type *DestTy = I->getType();
2349 Value *Src = I->getOperand(0);
2350 Type *SrcTy = Src->getType();
2351
2352 EVT SrcVT, DestVT;
2353 SrcVT = TLI.getValueType(SrcTy, true);
2354 DestVT = TLI.getValueType(DestTy, true);
2355
2356 bool isZExt = isa<ZExtInst>(I);
2357 unsigned SrcReg = getRegForValue(Src);
2358 if (!SrcReg) return false;
2359
2360 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2361 if (ResultReg == 0) return false;
2362 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002363 return true;
2364}
2365
Eric Christopher56d2b722010-09-02 23:43:26 +00002366// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002367bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002368
Eric Christopherab695882010-07-21 22:26:11 +00002369 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002370 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002371 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002372 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002373 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002374 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002375 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002376 case Instruction::ICmp:
2377 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002378 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002379 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002380 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002381 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002382 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002383 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00002384 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002385 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00002386 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00002387 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00002388 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002389 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00002390 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002391 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00002392 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002393 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00002394 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00002395 case Instruction::SRem:
2396 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002397 case Instruction::Call:
Chad Rosier11add262011-11-11 23:31:03 +00002398 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2399 return SelectIntrinsicCall(*II);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002400 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002401 case Instruction::Select:
2402 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002403 case Instruction::Ret:
2404 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002405 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002406 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002407 case Instruction::ZExt:
2408 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002409 return SelectIntExt(I);
Eric Christopherab695882010-07-21 22:26:11 +00002410 default: break;
2411 }
2412 return false;
2413}
2414
Chad Rosierb29b9502011-11-13 02:23:59 +00002415/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2416/// vreg is being provided by the specified load instruction. If possible,
2417/// try to fold the load as an operand to the instruction, returning true if
2418/// successful.
2419bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2420 const LoadInst *LI) {
2421 // Verify we have a legal type before going any further.
2422 MVT VT;
2423 if (!isLoadTypeLegal(LI->getType(), VT))
2424 return false;
2425
2426 // Combine load followed by zero- or sign-extend.
2427 // ldrb r1, [r0] ldrb r1, [r0]
2428 // uxtb r2, r1 =>
2429 // mov r3, r2 mov r3, r1
2430 bool isZExt = true;
2431 switch(MI->getOpcode()) {
2432 default: return false;
2433 case ARM::SXTH:
2434 case ARM::t2SXTH:
2435 isZExt = false;
2436 case ARM::UXTH:
2437 case ARM::t2UXTH:
2438 if (VT != MVT::i16)
2439 return false;
2440 break;
2441 case ARM::SXTB:
2442 case ARM::t2SXTB:
2443 isZExt = false;
2444 case ARM::UXTB:
2445 case ARM::t2UXTB:
2446 if (VT != MVT::i8)
2447 return false;
2448 break;
2449 }
2450 // See if we can handle this address.
2451 Address Addr;
2452 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2453
2454 unsigned ResultReg = MI->getOperand(0).getReg();
2455 if (!ARMEmitLoad(VT, ResultReg, Addr, isZExt, false))
2456 return false;
2457 MI->eraseFromParent();
2458 return true;
2459}
2460
Eric Christopherab695882010-07-21 22:26:11 +00002461namespace llvm {
2462 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00002463 // Completely untested on non-darwin.
2464 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002465
Eric Christopheraaa8df42010-11-02 01:21:28 +00002466 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00002467 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00002468 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00002469 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00002470 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00002471 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00002472 }
2473}