blob: fc847b8b1f800264d61c5601497e9bccddd80667 [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 Christopherc9932f62010-10-01 23:24:42 +000019#include "ARMConstantPoolValue.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000022#include "MCTargetDesc/ARMAddressingModes.h"
JF Bastien5ab77042013-06-11 22:13:46 +000023#include "llvm/ADT/STLExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/Analysis.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineMemOperand.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/GlobalVariable.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/IntrinsicInst.h"
39#include "llvm/IR/Module.h"
40#include "llvm/IR/Operator.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000042#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000045#include "llvm/Target/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 Christopher836c6242010-12-15 23:47:29 +000051extern cl::opt<bool> EnableARMLongCalls;
52
Eric Christopherab695882010-07-21 22:26:11 +000053namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000054
Eric Christopher0d581222010-11-19 22:30:02 +000055 // All possible address modes, plus some.
56 typedef struct Address {
57 enum {
58 RegBase,
59 FrameIndexBase
60 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000061
Eric Christopher0d581222010-11-19 22:30:02 +000062 union {
63 unsigned Reg;
64 int FI;
65 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000066
Eric Christopher0d581222010-11-19 22:30:02 +000067 int Offset;
Eric Christopher827656d2010-11-20 22:38:27 +000068
Eric Christopher0d581222010-11-19 22:30:02 +000069 // Innocuous defaults for our address.
70 Address()
Jim Grosbach0c720762011-05-16 22:24:07 +000071 : BaseType(RegBase), Offset(0) {
Eric Christopher0d581222010-11-19 22:30:02 +000072 Base.Reg = 0;
73 }
74 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000075
76class ARMFastISel : public FastISel {
77
78 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
79 /// make the right decision when generating code for different targets.
80 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000081 const TargetMachine &TM;
82 const TargetInstrInfo &TII;
83 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000084 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000085
Eric Christopher8cf6c602010-09-29 22:24:45 +000086 // Convenience variables to avoid some queries.
Chad Rosier66dc8ca2011-11-08 21:12:00 +000087 bool isThumb2;
Eric Christopher8cf6c602010-09-29 22:24:45 +000088 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000089
Eric Christopherab695882010-07-21 22:26:11 +000090 public:
Bob Wilsond49edb72012-08-03 04:06:28 +000091 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
92 const TargetLibraryInfo *libInfo)
93 : FastISel(funcInfo, libInfo),
Eric Christopher0fe7d542010-08-17 01:25:29 +000094 TM(funcInfo.MF->getTarget()),
95 TII(*TM.getInstrInfo()),
96 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000097 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000098 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier66dc8ca2011-11-08 21:12:00 +000099 isThumb2 = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000100 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000101 }
102
Eric Christophercb592292010-08-20 00:20:31 +0000103 // Code from FastISel.cpp.
Craig Topper35fc62b2012-08-18 21:38:45 +0000104 private:
105 unsigned FastEmitInst_(unsigned MachineInstOpcode,
106 const TargetRegisterClass *RC);
107 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC,
109 unsigned Op0, bool Op0IsKill);
110 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC,
112 unsigned Op0, bool Op0IsKill,
113 unsigned Op1, bool Op1IsKill);
114 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill,
118 unsigned Op2, bool Op2IsKill);
119 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
120 const TargetRegisterClass *RC,
121 unsigned Op0, bool Op0IsKill,
122 uint64_t Imm);
123 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
124 const TargetRegisterClass *RC,
125 unsigned Op0, bool Op0IsKill,
126 const ConstantFP *FPImm);
127 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 unsigned Op1, bool Op1IsKill,
131 uint64_t Imm);
132 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
133 const TargetRegisterClass *RC,
134 uint64_t Imm);
135 unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
136 const TargetRegisterClass *RC,
137 uint64_t Imm1, uint64_t Imm2);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000138
Craig Topper35fc62b2012-08-18 21:38:45 +0000139 unsigned FastEmitInst_extractsubreg(MVT RetVT,
140 unsigned Op0, bool Op0IsKill,
141 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000142
Eric Christophercb592292010-08-20 00:20:31 +0000143 // Backend specific FastISel code.
Craig Topper35fc62b2012-08-18 21:38:45 +0000144 private:
Eric Christopherab695882010-07-21 22:26:11 +0000145 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000146 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000147 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eli Bendersky75299e32013-04-19 22:29:18 +0000148 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
149 const LoadInst *LI);
Evan Cheng092e5e72013-02-11 01:27:15 +0000150 virtual bool FastLowerArguments();
Craig Topper35fc62b2012-08-18 21:38:45 +0000151 private:
Eric Christopherab695882010-07-21 22:26:11 +0000152 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000153
Eric Christopher83007122010-08-23 21:44:12 +0000154 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000155 private:
Eric Christopher17787722010-10-21 21:47:51 +0000156 bool SelectLoad(const Instruction *I);
157 bool SelectStore(const Instruction *I);
158 bool SelectBranch(const Instruction *I);
Chad Rosier60c8fa62012-02-07 23:56:08 +0000159 bool SelectIndirectBr(const Instruction *I);
Eric Christopher17787722010-10-21 21:47:51 +0000160 bool SelectCmp(const Instruction *I);
161 bool SelectFPExt(const Instruction *I);
162 bool SelectFPTrunc(const Instruction *I);
Chad Rosier3901c3e2012-02-06 23:50:07 +0000163 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
164 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosierae46a332012-02-03 21:14:11 +0000165 bool SelectIToFP(const Instruction *I, bool isSigned);
166 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosier7ccb30b2012-02-03 21:07:27 +0000167 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosier769422f2012-02-03 21:23:45 +0000168 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosier11add262011-11-11 23:31:03 +0000169 bool SelectCall(const Instruction *I, const char *IntrMemName);
170 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher17787722010-10-21 21:47:51 +0000171 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000172 bool SelectRet(const Instruction *I);
Chad Rosier0d7b2312011-11-02 00:18:48 +0000173 bool SelectTrunc(const Instruction *I);
174 bool SelectIntExt(const Instruction *I);
Jush Lu29465492012-08-03 02:37:48 +0000175 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopherab695882010-07-21 22:26:11 +0000176
Eric Christopher83007122010-08-23 21:44:12 +0000177 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000178 private:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000179 bool isTypeLegal(Type *Ty, MVT &VT);
180 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosiere07cd5e2011-11-02 18:08:25 +0000181 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
182 bool isZExt);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000183 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier404ed3c2011-12-14 17:26:05 +0000184 unsigned Alignment = 0, bool isZExt = true,
185 bool allocReg = true);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000186 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson6ce2dea2011-12-04 00:52:23 +0000187 unsigned Alignment = 0);
Eric Christopher0d581222010-11-19 22:30:02 +0000188 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier6290b932012-12-17 22:35:29 +0000189 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier2c42b8c2011-11-14 23:04:09 +0000190 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosierc9758b12012-12-06 01:34:31 +0000191 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
192 unsigned Alignment);
Chad Rosier316a5aa2012-12-17 19:59:43 +0000193 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000194 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
195 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
196 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
197 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
198 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosier49d6fc02012-06-12 19:25:13 +0000199 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000200 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000201
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000202 // Call handling routines.
203 private:
Jush Luee649832012-07-19 09:49:00 +0000204 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
205 bool Return,
206 bool isVarArg);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000207 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000208 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000209 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000210 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
211 SmallVectorImpl<unsigned> &RegArgs,
212 CallingConv::ID CC,
Jush Luee649832012-07-19 09:49:00 +0000213 unsigned &NumBytes,
214 bool isVarArg);
Chad Rosier49d6fc02012-06-12 19:25:13 +0000215 unsigned getLibcallReg(const Twine &Name);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000216 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000217 const Instruction *I, CallingConv::ID CC,
Jush Luee649832012-07-19 09:49:00 +0000218 unsigned &NumBytes, bool isVarArg);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000219 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000220
221 // OptionalDef handling routines.
222 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000223 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000224 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
225 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier6290b932012-12-17 22:35:29 +0000226 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000227 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000228 unsigned Flags, bool useAM3);
Eric Christopher456144e2010-08-19 00:37:05 +0000229};
Eric Christopherab695882010-07-21 22:26:11 +0000230
231} // end anonymous namespace
232
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000233#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000234
Eric Christopher456144e2010-08-19 00:37:05 +0000235// DefinesOptionalPredicate - This is different from DefinesPredicate in that
236// we don't care about implicit defs here, just places we'll need to add a
237// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
238bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000239 if (!MI->hasOptionalDef())
Eric Christopher456144e2010-08-19 00:37:05 +0000240 return false;
241
242 // Look to see if our OptionalDef is defining CPSR or CCR.
243 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
244 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000245 if (!MO.isReg() || !MO.isDef()) continue;
246 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000247 *CPSR = true;
248 }
249 return true;
250}
251
Eric Christopheraf3dce52011-03-12 01:09:29 +0000252bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000253 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher299bbb22011-04-29 00:03:10 +0000254
Eric Christopheraf3dce52011-03-12 01:09:29 +0000255 // If we're a thumb2 or not NEON function we were handled via isPredicable.
Evan Chenge837dea2011-06-28 19:10:37 +0000256 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopheraf3dce52011-03-12 01:09:29 +0000257 AFI->isThumb2Function())
258 return false;
Eric Christopher299bbb22011-04-29 00:03:10 +0000259
Evan Chenge837dea2011-06-28 19:10:37 +0000260 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
261 if (MCID.OpInfo[i].isPredicate())
Eric Christopheraf3dce52011-03-12 01:09:29 +0000262 return true;
Eric Christopher299bbb22011-04-29 00:03:10 +0000263
Eric Christopheraf3dce52011-03-12 01:09:29 +0000264 return false;
265}
266
Eric Christopher456144e2010-08-19 00:37:05 +0000267// If the machine is predicable go ahead and add the predicate operands, if
268// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000269// TODO: If we want to support thumb1 then we'll need to deal with optional
270// CPSR defs that need to be added before the remaining operands. See s_cc_out
271// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000272const MachineInstrBuilder &
273ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
274 MachineInstr *MI = &*MIB;
275
Eric Christopheraf3dce52011-03-12 01:09:29 +0000276 // Do we use a predicate? or...
277 // Are we NEON in ARM mode and have a predicate operand? If so, I know
278 // we're not predicable but add it anyways.
279 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000280 AddDefaultPred(MIB);
Eric Christopher299bbb22011-04-29 00:03:10 +0000281
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000282 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher456144e2010-08-19 00:37:05 +0000283 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000284 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000285 if (DefinesOptionalPredicate(MI, &CPSR)) {
286 if (CPSR)
287 AddDefaultT1CC(MIB);
288 else
289 AddDefaultCC(MIB);
290 }
291 return MIB;
292}
293
Eric Christopher0fe7d542010-08-17 01:25:29 +0000294unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
295 const TargetRegisterClass* RC) {
296 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000297 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298
Eric Christopher456144e2010-08-19 00:37:05 +0000299 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000300 return ResultReg;
301}
302
303unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
304 const TargetRegisterClass *RC,
305 unsigned Op0, bool Op0IsKill) {
306 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000307 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308
Chad Rosier40d552e2012-02-15 17:36:21 +0000309 if (II.getNumDefs() >= 1) {
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 .addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier40d552e2012-02-15 17:36:21 +0000312 } else {
Eric Christopher456144e2010-08-19 00:37:05 +0000313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000314 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000315 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000316 TII.get(TargetOpcode::COPY), ResultReg)
317 .addReg(II.ImplicitDefs[0]));
318 }
319 return ResultReg;
320}
321
322unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
323 const TargetRegisterClass *RC,
324 unsigned Op0, bool Op0IsKill,
325 unsigned Op1, bool Op1IsKill) {
326 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000327 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000328
Chad Rosier40d552e2012-02-15 17:36:21 +0000329 if (II.getNumDefs() >= 1) {
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 .addReg(Op0, Op0IsKill * RegState::Kill)
332 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier40d552e2012-02-15 17:36:21 +0000333 } else {
Eric Christopher456144e2010-08-19 00:37:05 +0000334 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000335 .addReg(Op0, Op0IsKill * RegState::Kill)
336 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000338 TII.get(TargetOpcode::COPY), ResultReg)
339 .addReg(II.ImplicitDefs[0]));
340 }
341 return ResultReg;
342}
343
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000344unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
345 const TargetRegisterClass *RC,
346 unsigned Op0, bool Op0IsKill,
347 unsigned Op1, bool Op1IsKill,
348 unsigned Op2, bool Op2IsKill) {
349 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000350 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000351
Chad Rosier40d552e2012-02-15 17:36:21 +0000352 if (II.getNumDefs() >= 1) {
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000353 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
354 .addReg(Op0, Op0IsKill * RegState::Kill)
355 .addReg(Op1, Op1IsKill * RegState::Kill)
356 .addReg(Op2, Op2IsKill * RegState::Kill));
Chad Rosier40d552e2012-02-15 17:36:21 +0000357 } else {
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000358 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
359 .addReg(Op0, Op0IsKill * RegState::Kill)
360 .addReg(Op1, Op1IsKill * RegState::Kill)
361 .addReg(Op2, Op2IsKill * RegState::Kill));
362 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
363 TII.get(TargetOpcode::COPY), ResultReg)
364 .addReg(II.ImplicitDefs[0]));
365 }
366 return ResultReg;
367}
368
Eric Christopher0fe7d542010-08-17 01:25:29 +0000369unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
370 const TargetRegisterClass *RC,
371 unsigned Op0, bool Op0IsKill,
372 uint64_t Imm) {
373 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000374 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000375
Chad Rosier40d552e2012-02-15 17:36:21 +0000376 if (II.getNumDefs() >= 1) {
Eric Christopher456144e2010-08-19 00:37:05 +0000377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000378 .addReg(Op0, Op0IsKill * RegState::Kill)
379 .addImm(Imm));
Chad Rosier40d552e2012-02-15 17:36:21 +0000380 } else {
Eric Christopher456144e2010-08-19 00:37:05 +0000381 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000382 .addReg(Op0, Op0IsKill * RegState::Kill)
383 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000385 TII.get(TargetOpcode::COPY), ResultReg)
386 .addReg(II.ImplicitDefs[0]));
387 }
388 return ResultReg;
389}
390
391unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
392 const TargetRegisterClass *RC,
393 unsigned Op0, bool Op0IsKill,
394 const ConstantFP *FPImm) {
395 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000396 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000397
Chad Rosier40d552e2012-02-15 17:36:21 +0000398 if (II.getNumDefs() >= 1) {
Eric Christopher456144e2010-08-19 00:37:05 +0000399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000400 .addReg(Op0, Op0IsKill * RegState::Kill)
401 .addFPImm(FPImm));
Chad Rosier40d552e2012-02-15 17:36:21 +0000402 } else {
Eric Christopher456144e2010-08-19 00:37:05 +0000403 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000404 .addReg(Op0, Op0IsKill * RegState::Kill)
405 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000406 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000407 TII.get(TargetOpcode::COPY), ResultReg)
408 .addReg(II.ImplicitDefs[0]));
409 }
410 return ResultReg;
411}
412
413unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
414 const TargetRegisterClass *RC,
415 unsigned Op0, bool Op0IsKill,
416 unsigned Op1, bool Op1IsKill,
417 uint64_t Imm) {
418 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000419 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000420
Chad Rosier40d552e2012-02-15 17:36:21 +0000421 if (II.getNumDefs() >= 1) {
Eric Christopher456144e2010-08-19 00:37:05 +0000422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000423 .addReg(Op0, Op0IsKill * RegState::Kill)
424 .addReg(Op1, Op1IsKill * RegState::Kill)
425 .addImm(Imm));
Chad Rosier40d552e2012-02-15 17:36:21 +0000426 } else {
Eric Christopher456144e2010-08-19 00:37:05 +0000427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000428 .addReg(Op0, Op0IsKill * RegState::Kill)
429 .addReg(Op1, Op1IsKill * RegState::Kill)
430 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000431 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000432 TII.get(TargetOpcode::COPY), ResultReg)
433 .addReg(II.ImplicitDefs[0]));
434 }
435 return ResultReg;
436}
437
438unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
439 const TargetRegisterClass *RC,
440 uint64_t Imm) {
441 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000442 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000443
Chad Rosier40d552e2012-02-15 17:36:21 +0000444 if (II.getNumDefs() >= 1) {
Eric Christopher456144e2010-08-19 00:37:05 +0000445 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000446 .addImm(Imm));
Chad Rosier40d552e2012-02-15 17:36:21 +0000447 } else {
Eric Christopher456144e2010-08-19 00:37:05 +0000448 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000449 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000450 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000451 TII.get(TargetOpcode::COPY), ResultReg)
452 .addReg(II.ImplicitDefs[0]));
453 }
454 return ResultReg;
455}
456
Eric Christopherd94bc542011-04-29 22:07:50 +0000457unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
458 const TargetRegisterClass *RC,
459 uint64_t Imm1, uint64_t Imm2) {
460 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +0000461 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher471e4222011-06-08 23:55:35 +0000462
Chad Rosier40d552e2012-02-15 17:36:21 +0000463 if (II.getNumDefs() >= 1) {
Eric Christopherd94bc542011-04-29 22:07:50 +0000464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
465 .addImm(Imm1).addImm(Imm2));
Chad Rosier40d552e2012-02-15 17:36:21 +0000466 } else {
Eric Christopherd94bc542011-04-29 22:07:50 +0000467 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
468 .addImm(Imm1).addImm(Imm2));
Eric Christopher471e4222011-06-08 23:55:35 +0000469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherd94bc542011-04-29 22:07:50 +0000470 TII.get(TargetOpcode::COPY),
471 ResultReg)
472 .addReg(II.ImplicitDefs[0]));
473 }
474 return ResultReg;
475}
476
Eric Christopher0fe7d542010-08-17 01:25:29 +0000477unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
478 unsigned Op0, bool Op0IsKill,
479 uint32_t Idx) {
480 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
481 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
482 "Cannot yet extract from physregs");
Chad Rosier40d552e2012-02-15 17:36:21 +0000483
Eric Christopher456144e2010-08-19 00:37:05 +0000484 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Chad Rosier40d552e2012-02-15 17:36:21 +0000485 DL, TII.get(TargetOpcode::COPY), ResultReg)
486 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000487 return ResultReg;
488}
489
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000490// TODO: Don't worry about 64-bit now, but when this is fixed remove the
491// checks from the various callers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000492unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000493 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000494
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000495 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
496 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbache751c002012-03-01 22:47:09 +0000497 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000498 .addReg(SrcReg));
499 return MoveReg;
500}
501
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000502unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000503 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000504
Eric Christopheraa3ace12010-09-09 20:49:25 +0000505 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
506 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbache751c002012-03-01 22:47:09 +0000507 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000508 .addReg(SrcReg));
509 return MoveReg;
510}
511
Eric Christopher9ed58df2010-09-09 00:19:41 +0000512// For double width floating point we need to materialize two constants
513// (the high and the low) into integer registers then use a move to get
514// the combined constant into an FP reg.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000515unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher9ed58df2010-09-09 00:19:41 +0000516 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000517 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000518
Eric Christopher9ed58df2010-09-09 00:19:41 +0000519 // This checks to see if we can use VFP3 instructions to materialize
520 // a constant, otherwise we have to go through the constant pool.
521 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000522 int Imm;
523 unsigned Opc;
524 if (is64bit) {
525 Imm = ARM_AM::getFP64Imm(Val);
526 Opc = ARM::FCONSTD;
527 } else {
528 Imm = ARM_AM::getFP32Imm(Val);
529 Opc = ARM::FCONSTS;
530 }
Eric Christopher9ed58df2010-09-09 00:19:41 +0000531 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
532 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
533 DestReg)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +0000534 .addImm(Imm));
Eric Christopher9ed58df2010-09-09 00:19:41 +0000535 return DestReg;
536 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000537
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000538 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000539 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000540
Eric Christopher238bb162010-09-09 23:50:00 +0000541 // MachineConstantPool wants an explicit alignment.
542 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
543 if (Align == 0) {
544 // TODO: Figure out if this is correct.
545 Align = TD.getTypeAllocSize(CFP->getType());
546 }
547 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
548 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
549 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000550
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000551 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000552 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
553 DestReg)
554 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000555 .addReg(0));
556 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000557}
558
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000559unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000560
Chad Rosier44e89572011-11-04 22:29:00 +0000561 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
562 return false;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000563
564 // If we can do this in a single instruction without a constant pool entry
565 // do so now.
566 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiera4e07272011-11-04 23:09:49 +0000567 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000568 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosierfc17ddd2012-11-27 01:06:49 +0000569 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
570 &ARM::GPRRegClass;
571 unsigned ImmReg = createResultReg(RC);
Eric Christophere5b13cf2010-11-03 20:21:17 +0000572 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier44e89572011-11-04 22:29:00 +0000573 TII.get(Opc), ImmReg)
Chad Rosier42536af2011-11-05 20:16:15 +0000574 .addImm(CI->getZExtValue()));
Chad Rosier44e89572011-11-04 22:29:00 +0000575 return ImmReg;
Eric Christophere5b13cf2010-11-03 20:21:17 +0000576 }
577
Chad Rosier4e89d972011-11-11 00:36:21 +0000578 // Use MVN to emit negative constants.
579 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
580 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosier1c47de82011-11-11 06:27:41 +0000581 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier4e89d972011-11-11 00:36:21 +0000582 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier1c47de82011-11-11 06:27:41 +0000583 if (UseImm) {
Chad Rosier4e89d972011-11-11 00:36:21 +0000584 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
585 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
586 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
587 TII.get(Opc), ImmReg)
588 .addImm(Imm));
589 return ImmReg;
590 }
591 }
592
593 // Load from constant pool. For now 32-bit only.
Chad Rosier44e89572011-11-04 22:29:00 +0000594 if (VT != MVT::i32)
595 return false;
596
597 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
598
Eric Christopher56d2b722010-09-02 23:43:26 +0000599 // MachineConstantPool wants an explicit alignment.
600 unsigned Align = TD.getPrefTypeAlignment(C->getType());
601 if (Align == 0) {
602 // TODO: Figure out if this is correct.
603 Align = TD.getTypeAllocSize(C->getType());
604 }
605 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000606
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000607 if (isThumb2)
Eric Christopher56d2b722010-09-02 23:43:26 +0000608 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000609 TII.get(ARM::t2LDRpci), DestReg)
610 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000611 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000612 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000613 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000614 TII.get(ARM::LDRcp), DestReg)
615 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000616 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000617
Eric Christopher56d2b722010-09-02 23:43:26 +0000618 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000619}
620
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000621unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000622 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000623 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000624
Eric Christopher890dbbe2010-10-02 00:32:44 +0000625 Reloc::Model RelocM = TM.getRelocationModel();
Jush Luc4dc2492012-08-29 02:41:21 +0000626 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier6aa6e5a2012-11-07 00:13:01 +0000627 const TargetRegisterClass *RC = isThumb2 ?
628 (const TargetRegisterClass*)&ARM::rGPRRegClass :
629 (const TargetRegisterClass*)&ARM::GPRRegClass;
630 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000631
JF Bastienfe532ad2013-06-14 02:49:43 +0000632 // FastISel TLS support on non-Darwin is broken, punt to SelectionDAG.
633 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
634 bool IsThreadLocal = GVar && GVar->isThreadLocal();
635 if (!Subtarget->isTargetDarwin() && IsThreadLocal) return 0;
636
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000637 // Use movw+movt when possible, it avoids constant pool entries.
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +0000638 // Darwin targets don't support movt with Reloc::Static, see
639 // ARMTargetLowering::LowerGlobalAddressDarwin. Other targets only support
640 // static movt relocations.
641 if (Subtarget->useMovt() &&
642 Subtarget->isTargetDarwin() == (RelocM != Reloc::Static)) {
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000643 unsigned Opc;
644 switch (RelocM) {
645 case Reloc::PIC_:
646 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
647 break;
648 case Reloc::DynamicNoPIC:
649 Opc = isThumb2 ? ARM::t2MOV_ga_dyn : ARM::MOV_ga_dyn;
650 break;
651 default:
652 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
653 break;
654 }
655 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
656 DestReg).addGlobalAddress(GV));
Eric Christopher890dbbe2010-10-02 00:32:44 +0000657 } else {
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000658 // MachineConstantPool wants an explicit alignment.
659 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
660 if (Align == 0) {
661 // TODO: Figure out if this is correct.
662 Align = TD.getTypeAllocSize(GV->getType());
663 }
664
Jush Lu8f506472012-09-27 05:21:41 +0000665 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
666 return ARMLowerPICELF(GV, Align, VT);
667
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000668 // Grab index.
669 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
670 (Subtarget->isThumb() ? 4 : 8);
671 unsigned Id = AFI->createPICLabelUId();
672 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
673 ARMCP::CPValue,
674 PCAdj);
675 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
676
677 // Load value.
678 MachineInstrBuilder MIB;
679 if (isThumb2) {
680 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
681 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
682 .addConstantPoolIndex(Idx);
683 if (RelocM == Reloc::PIC_)
684 MIB.addImm(Id);
Jush Luc4dc2492012-08-29 02:41:21 +0000685 AddOptionalDefs(MIB);
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000686 } else {
687 // The extra immediate is for addrmode2.
688 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
689 DestReg)
690 .addConstantPoolIndex(Idx)
691 .addImm(0);
Jush Luc4dc2492012-08-29 02:41:21 +0000692 AddOptionalDefs(MIB);
693
694 if (RelocM == Reloc::PIC_) {
695 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
696 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
697
698 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
699 DL, TII.get(Opc), NewDestReg)
700 .addReg(DestReg)
701 .addImm(Id);
702 AddOptionalDefs(MIB);
703 return NewDestReg;
704 }
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000705 }
Eric Christopher890dbbe2010-10-02 00:32:44 +0000706 }
Eli Friedmand6412c92011-06-03 01:13:19 +0000707
Jush Luc4dc2492012-08-29 02:41:21 +0000708 if (IsIndirect) {
Jakob Stoklund Olesen45ca7c62012-01-07 01:47:05 +0000709 MachineInstrBuilder MIB;
Eli Friedmand6412c92011-06-03 01:13:19 +0000710 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000711 if (isThumb2)
Jim Grosbachb04546f2011-09-13 20:30:37 +0000712 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
713 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedmand6412c92011-06-03 01:13:19 +0000714 .addReg(DestReg)
715 .addImm(0);
716 else
717 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
718 NewDestReg)
719 .addReg(DestReg)
720 .addImm(0);
721 DestReg = NewDestReg;
722 AddOptionalDefs(MIB);
723 }
724
Eric Christopher890dbbe2010-10-02 00:32:44 +0000725 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000726}
727
Eric Christopher9ed58df2010-09-09 00:19:41 +0000728unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000729 EVT CEVT = TLI.getValueType(C->getType(), true);
730
731 // Only handle simple types.
732 if (!CEVT.isSimple()) return 0;
733 MVT VT = CEVT.getSimpleVT();
Eric Christopher9ed58df2010-09-09 00:19:41 +0000734
735 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
736 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000737 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
738 return ARMMaterializeGV(GV, VT);
739 else if (isa<ConstantInt>(C))
740 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000741
Eric Christopherc9932f62010-10-01 23:24:42 +0000742 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000743}
744
Chad Rosier944d82b2011-11-17 21:46:13 +0000745// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
746
Eric Christopherf9764fa2010-09-30 20:49:44 +0000747unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
748 // Don't handle dynamic allocas.
749 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000750
Duncan Sands1440e8b2010-11-03 11:35:31 +0000751 MVT VT;
Chad Rosierf4bd21c2012-05-11 16:41:38 +0000752 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000753
Eric Christopherf9764fa2010-09-30 20:49:44 +0000754 DenseMap<const AllocaInst*, int>::iterator SI =
755 FuncInfo.StaticAllocaMap.find(AI);
756
757 // This will get lowered later into the correct offsets and registers
758 // via rewriteXFrameIndex.
759 if (SI != FuncInfo.StaticAllocaMap.end()) {
Craig Topper44d23822012-02-22 05:59:10 +0000760 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000761 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000762 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Evan Chengddfd1372011-12-14 02:11:42 +0000763 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherf9764fa2010-09-30 20:49:44 +0000764 TII.get(Opc), ResultReg)
765 .addFrameIndex(SI->second)
766 .addImm(0));
767 return ResultReg;
768 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000769
Eric Christopherf9764fa2010-09-30 20:49:44 +0000770 return 0;
771}
772
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000773bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000774 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000775
Eric Christopherb1cc8482010-08-25 07:23:49 +0000776 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000777 if (evt == MVT::Other || !evt.isSimple()) return false;
778 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000779
Eric Christopherdc908042010-08-31 01:28:42 +0000780 // Handle all legal types, i.e. a register that will directly hold this
781 // value.
782 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000783}
784
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000785bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000786 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000787
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000788 // If this is a type than can be sign or zero-extended to a basic operation
789 // go ahead and accept it now.
Chad Rosierb29b9502011-11-13 02:23:59 +0000790 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000791 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000792
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000793 return false;
794}
795
Eric Christopher88de86b2010-11-19 22:36:41 +0000796// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000797bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000798 // Some boilerplate from the X86 FastISel.
799 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000800 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000801 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000802 // Don't walk into other basic blocks unless the object is an alloca from
803 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000804 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
805 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
806 Opcode = I->getOpcode();
807 U = I;
808 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000809 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000810 Opcode = C->getOpcode();
811 U = C;
812 }
813
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000814 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000815 if (Ty->getAddressSpace() > 255)
816 // Fast instruction selection doesn't support the special
817 // address spaces.
818 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000819
Eric Christopher83007122010-08-23 21:44:12 +0000820 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000821 default:
Eric Christopher83007122010-08-23 21:44:12 +0000822 break;
Eric Christopheradde9da2013-07-12 22:08:24 +0000823 case Instruction::BitCast:
Eric Christopher55324332010-10-12 00:43:21 +0000824 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000825 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopheradde9da2013-07-12 22:08:24 +0000826 case Instruction::IntToPtr:
Eric Christopher55324332010-10-12 00:43:21 +0000827 // Look past no-op inttoptrs.
828 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000829 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000830 break;
Eric Christopheradde9da2013-07-12 22:08:24 +0000831 case Instruction::PtrToInt:
Eric Christopher55324332010-10-12 00:43:21 +0000832 // Look past no-op ptrtoints.
833 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000834 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000835 break;
Eric Christophereae84392010-10-14 09:29:41 +0000836 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000837 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000838 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000839
Eric Christophereae84392010-10-14 09:29:41 +0000840 // Iterate through the GEP folding the constants into offsets where
841 // we can.
842 gep_type_iterator GTI = gep_type_begin(U);
843 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
844 i != e; ++i, ++GTI) {
845 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000846 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christophereae84392010-10-14 09:29:41 +0000847 const StructLayout *SL = TD.getStructLayout(STy);
848 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
849 TmpOffset += SL->getElementOffset(Idx);
850 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000851 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christopher7244d7c2011-03-22 19:39:17 +0000852 for (;;) {
Eric Christopher2896df82010-10-15 18:02:07 +0000853 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
854 // Constant-offset addressing.
855 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000856 break;
857 }
858 if (isa<AddOperator>(Op) &&
859 (!isa<Instruction>(Op) ||
860 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
861 == FuncInfo.MBB) &&
862 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
Eric Christopher299bbb22011-04-29 00:03:10 +0000863 // An add (in the same block) with a constant operand. Fold the
Eric Christopher7244d7c2011-03-22 19:39:17 +0000864 // constant.
Eric Christopher2896df82010-10-15 18:02:07 +0000865 ConstantInt *CI =
Eric Christopher7244d7c2011-03-22 19:39:17 +0000866 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christopher2896df82010-10-15 18:02:07 +0000867 TmpOffset += CI->getSExtValue() * S;
Eric Christopher7244d7c2011-03-22 19:39:17 +0000868 // Iterate on the other operand.
869 Op = cast<AddOperator>(Op)->getOperand(0);
870 continue;
Eric Christopher299bbb22011-04-29 00:03:10 +0000871 }
Eric Christopher7244d7c2011-03-22 19:39:17 +0000872 // Unsupported
873 goto unsupported_gep;
874 }
Eric Christophereae84392010-10-14 09:29:41 +0000875 }
876 }
Eric Christopher2896df82010-10-15 18:02:07 +0000877
878 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000879 Addr.Offset = TmpOffset;
880 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000881
882 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000883 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000884
Eric Christophereae84392010-10-14 09:29:41 +0000885 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000886 break;
887 }
Eric Christopher83007122010-08-23 21:44:12 +0000888 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000889 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000890 DenseMap<const AllocaInst*, int>::iterator SI =
891 FuncInfo.StaticAllocaMap.find(AI);
892 if (SI != FuncInfo.StaticAllocaMap.end()) {
893 Addr.BaseType = Address::FrameIndexBase;
894 Addr.Base.FI = SI->second;
895 return true;
896 }
897 break;
Eric Christopher83007122010-08-23 21:44:12 +0000898 }
899 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000900
Eric Christophercb0b04b2010-08-24 00:07:24 +0000901 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000902 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
903 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000904}
905
Chad Rosier6290b932012-12-17 22:35:29 +0000906void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher212ae932010-10-21 19:40:30 +0000907 bool needsLowering = false;
Chad Rosier6290b932012-12-17 22:35:29 +0000908 switch (VT.SimpleTy) {
Craig Topperbc219812012-02-07 02:50:20 +0000909 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher212ae932010-10-21 19:40:30 +0000910 case MVT::i1:
911 case MVT::i8:
Chad Rosierb29b9502011-11-13 02:23:59 +0000912 case MVT::i16:
Eric Christopher212ae932010-10-21 19:40:30 +0000913 case MVT::i32:
Chad Rosier57b29972011-11-14 20:22:27 +0000914 if (!useAM3) {
Chad Rosierb29b9502011-11-13 02:23:59 +0000915 // Integer loads/stores handle 12-bit offsets.
916 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosier57b29972011-11-14 20:22:27 +0000917 // Handle negative offsets.
Chad Rosiere489af82011-11-14 22:34:48 +0000918 if (needsLowering && isThumb2)
919 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
920 Addr.Offset > -256);
Chad Rosier57b29972011-11-14 20:22:27 +0000921 } else {
Chad Rosier5be833d2011-11-13 04:25:02 +0000922 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000923 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosier57b29972011-11-14 20:22:27 +0000924 }
Eric Christopher212ae932010-10-21 19:40:30 +0000925 break;
926 case MVT::f32:
927 case MVT::f64:
928 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000929 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000930 break;
931 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000932
Eric Christopher827656d2010-11-20 22:38:27 +0000933 // If this is a stack pointer and the offset needs to be simplified then
934 // put the alloca address into a register, set the base type back to
935 // register and continue. This should almost never happen.
936 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topper420761a2012-04-20 07:30:17 +0000937 const TargetRegisterClass *RC = isThumb2 ?
938 (const TargetRegisterClass*)&ARM::tGPRRegClass :
939 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher827656d2010-11-20 22:38:27 +0000940 unsigned ResultReg = createResultReg(RC);
Chad Rosier66dc8ca2011-11-08 21:12:00 +0000941 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Evan Chengddfd1372011-12-14 02:11:42 +0000942 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher827656d2010-11-20 22:38:27 +0000943 TII.get(Opc), ResultReg)
944 .addFrameIndex(Addr.Base.FI)
945 .addImm(0));
946 Addr.Base.Reg = ResultReg;
947 Addr.BaseType = Address::RegBase;
948 }
949
Eric Christopher212ae932010-10-21 19:40:30 +0000950 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000951 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000952 if (needsLowering) {
Eli Friedman9ebf57a2011-04-29 21:22:56 +0000953 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
954 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopher0d581222010-11-19 22:30:02 +0000955 Addr.Offset = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000956 }
Eric Christopher83007122010-08-23 21:44:12 +0000957}
958
Chad Rosier6290b932012-12-17 22:35:29 +0000959void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000960 const MachineInstrBuilder &MIB,
Chad Rosierb29b9502011-11-13 02:23:59 +0000961 unsigned Flags, bool useAM3) {
Eric Christopher564857f2010-12-01 01:40:24 +0000962 // addrmode5 output depends on the selection dag addressing dividing the
963 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier6290b932012-12-17 22:35:29 +0000964 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher564857f2010-12-01 01:40:24 +0000965 Addr.Offset /= 4;
Eric Christopher299bbb22011-04-29 00:03:10 +0000966
Eric Christopher564857f2010-12-01 01:40:24 +0000967 // Frame base works a bit differently. Handle it separately.
968 if (Addr.BaseType == Address::FrameIndexBase) {
969 int FI = Addr.Base.FI;
970 int Offset = Addr.Offset;
971 MachineMemOperand *MMO =
972 FuncInfo.MF->getMachineMemOperand(
973 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarichc152aa62011-05-28 20:34:49 +0000974 Flags,
Eric Christopher564857f2010-12-01 01:40:24 +0000975 MFI.getObjectSize(FI),
976 MFI.getObjectAlignment(FI));
977 // Now add the rest of the operands.
978 MIB.addFrameIndex(FI);
979
Bob Wilson6ce2dea2011-12-04 00:52:23 +0000980 // ARM halfword load/stores and signed byte loads need an additional
981 // operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000982 if (useAM3) {
983 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
984 MIB.addReg(0);
985 MIB.addImm(Imm);
986 } else {
987 MIB.addImm(Addr.Offset);
988 }
Eric Christopher564857f2010-12-01 01:40:24 +0000989 MIB.addMemOperand(MMO);
990 } else {
991 // Now add the rest of the operands.
992 MIB.addReg(Addr.Base.Reg);
Eric Christopher299bbb22011-04-29 00:03:10 +0000993
Bob Wilson6ce2dea2011-12-04 00:52:23 +0000994 // ARM halfword load/stores and signed byte loads need an additional
995 // operand.
Chad Rosierdc9205d2011-11-14 04:09:28 +0000996 if (useAM3) {
997 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
998 MIB.addReg(0);
999 MIB.addImm(Imm);
1000 } else {
1001 MIB.addImm(Addr.Offset);
1002 }
Eric Christopher564857f2010-12-01 01:40:24 +00001003 }
1004 AddOptionalDefs(MIB);
1005}
1006
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001007bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier8a9bce92011-12-13 19:22:14 +00001008 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopherdc908042010-08-31 01:28:42 +00001009 unsigned Opc;
Chad Rosierb29b9502011-11-13 02:23:59 +00001010 bool useAM3 = false;
Chad Rosier8a9bce92011-12-13 19:22:14 +00001011 bool needVMOV = false;
Craig Topper44d23822012-02-22 05:59:10 +00001012 const TargetRegisterClass *RC;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001013 switch (VT.SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001014 // This is mostly going to be Neon/vector support.
1015 default: return false;
Chad Rosier646abbf2011-11-11 02:38:59 +00001016 case MVT::i1:
Eric Christopher4e68c7c2010-09-01 18:01:32 +00001017 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +00001018 if (isThumb2) {
1019 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1020 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1021 else
1022 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierb29b9502011-11-13 02:23:59 +00001023 } else {
Chad Rosier57b29972011-11-14 20:22:27 +00001024 if (isZExt) {
1025 Opc = ARM::LDRBi12;
1026 } else {
1027 Opc = ARM::LDRSB;
1028 useAM3 = true;
1029 }
Chad Rosierb29b9502011-11-13 02:23:59 +00001030 }
JF Bastien1fe907e2013-06-09 00:20:24 +00001031 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +00001032 break;
Chad Rosier73463472011-11-09 21:30:12 +00001033 case MVT::i16:
Chad Rosierb3235b12012-11-09 18:25:27 +00001034 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosierd70c98e2012-09-21 00:41:42 +00001035 return false;
1036
Chad Rosier57b29972011-11-14 20:22:27 +00001037 if (isThumb2) {
1038 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1039 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1040 else
1041 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1042 } else {
1043 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1044 useAM3 = true;
1045 }
JF Bastien1fe907e2013-06-09 00:20:24 +00001046 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier73463472011-11-09 21:30:12 +00001047 break;
Eric Christopherdc908042010-08-31 01:28:42 +00001048 case MVT::i32:
Chad Rosierb3235b12012-11-09 18:25:27 +00001049 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosiere5e674b2012-09-21 16:58:35 +00001050 return false;
1051
Chad Rosier57b29972011-11-14 20:22:27 +00001052 if (isThumb2) {
1053 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1054 Opc = ARM::t2LDRi8;
1055 else
1056 Opc = ARM::t2LDRi12;
1057 } else {
1058 Opc = ARM::LDRi12;
1059 }
JF Bastien1fe907e2013-06-09 00:20:24 +00001060 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopherdc908042010-08-31 01:28:42 +00001061 break;
Eric Christopher6dab1372010-09-18 01:59:37 +00001062 case MVT::f32:
Chad Rosier6762f8f2011-12-14 17:55:03 +00001063 if (!Subtarget->hasVFP2()) return false;
Chad Rosier8a9bce92011-12-13 19:22:14 +00001064 // Unaligned loads need special handling. Floats require word-alignment.
1065 if (Alignment && Alignment < 4) {
1066 needVMOV = true;
1067 VT = MVT::i32;
1068 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien1fe907e2013-06-09 00:20:24 +00001069 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier8a9bce92011-12-13 19:22:14 +00001070 } else {
1071 Opc = ARM::VLDRS;
1072 RC = TLI.getRegClassFor(VT);
1073 }
Eric Christopher6dab1372010-09-18 01:59:37 +00001074 break;
1075 case MVT::f64:
Chad Rosier6762f8f2011-12-14 17:55:03 +00001076 if (!Subtarget->hasVFP2()) return false;
Chad Rosier404ed3c2011-12-14 17:26:05 +00001077 // FIXME: Unaligned loads need special handling. Doublewords require
1078 // word-alignment.
1079 if (Alignment && Alignment < 4)
Chad Rosier8a9bce92011-12-13 19:22:14 +00001080 return false;
Chad Rosier404ed3c2011-12-14 17:26:05 +00001081
Eric Christopher6dab1372010-09-18 01:59:37 +00001082 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +00001083 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +00001084 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001085 }
Eric Christopher564857f2010-12-01 01:40:24 +00001086 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001087 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001088
Eric Christopher564857f2010-12-01 01:40:24 +00001089 // Create the base instruction, then add the operands.
Chad Rosierb29b9502011-11-13 02:23:59 +00001090 if (allocReg)
1091 ResultReg = createResultReg(RC);
1092 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher564857f2010-12-01 01:40:24 +00001093 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1094 TII.get(Opc), ResultReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001095 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier8a9bce92011-12-13 19:22:14 +00001096
1097 // If we had an unaligned load of a float we've converted it to an regular
1098 // load. Now we must move from the GRP to the FP register.
1099 if (needVMOV) {
1100 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1101 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1102 TII.get(ARM::VMOVSR), MoveReg)
1103 .addReg(ResultReg));
1104 ResultReg = MoveReg;
1105 }
Eric Christopherdc908042010-08-31 01:28:42 +00001106 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +00001107}
1108
Eric Christopher43b62be2010-09-27 06:02:23 +00001109bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +00001110 // Atomic loads need special handling.
1111 if (cast<LoadInst>(I)->isAtomic())
1112 return false;
1113
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001114 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001115 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001116 if (!isLoadTypeLegal(I->getType(), VT))
1117 return false;
1118
Eric Christopher564857f2010-12-01 01:40:24 +00001119 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001120 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +00001121 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001122
1123 unsigned ResultReg;
Chad Rosier8a9bce92011-12-13 19:22:14 +00001124 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1125 return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001126 UpdateValueMap(I, ResultReg);
1127 return true;
1128}
1129
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001130bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson6ce2dea2011-12-04 00:52:23 +00001131 unsigned Alignment) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001132 unsigned StrOpc;
Chad Rosierb29b9502011-11-13 02:23:59 +00001133 bool useAM3 = false;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001134 switch (VT.SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +00001135 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +00001136 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +00001137 case MVT::i1: {
Craig Topper420761a2012-04-20 07:30:17 +00001138 unsigned Res = createResultReg(isThumb2 ?
1139 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1140 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001141 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Eric Christopher4c914122010-11-02 23:59:09 +00001142 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1143 TII.get(Opc), Res)
1144 .addReg(SrcReg).addImm(1));
1145 SrcReg = Res;
1146 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +00001147 case MVT::i8:
Chad Rosier57b29972011-11-14 20:22:27 +00001148 if (isThumb2) {
1149 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1150 StrOpc = ARM::t2STRBi8;
1151 else
1152 StrOpc = ARM::t2STRBi12;
1153 } else {
1154 StrOpc = ARM::STRBi12;
1155 }
Eric Christopher15418772010-10-12 05:39:06 +00001156 break;
1157 case MVT::i16:
Chad Rosierb3235b12012-11-09 18:25:27 +00001158 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosierd70c98e2012-09-21 00:41:42 +00001159 return false;
1160
Chad Rosier57b29972011-11-14 20:22:27 +00001161 if (isThumb2) {
1162 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1163 StrOpc = ARM::t2STRHi8;
1164 else
1165 StrOpc = ARM::t2STRHi12;
1166 } else {
1167 StrOpc = ARM::STRH;
1168 useAM3 = true;
1169 }
Eric Christopher15418772010-10-12 05:39:06 +00001170 break;
Eric Christopher47650ec2010-10-16 01:10:35 +00001171 case MVT::i32:
Chad Rosierb3235b12012-11-09 18:25:27 +00001172 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosiere5e674b2012-09-21 16:58:35 +00001173 return false;
1174
Chad Rosier57b29972011-11-14 20:22:27 +00001175 if (isThumb2) {
1176 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1177 StrOpc = ARM::t2STRi8;
1178 else
1179 StrOpc = ARM::t2STRi12;
1180 } else {
1181 StrOpc = ARM::STRi12;
1182 }
Eric Christopher47650ec2010-10-16 01:10:35 +00001183 break;
Eric Christopher56d2b722010-09-02 23:43:26 +00001184 case MVT::f32:
1185 if (!Subtarget->hasVFP2()) return false;
Chad Rosiered42c5f2011-12-06 01:44:17 +00001186 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosier9eff1e32011-12-03 02:21:57 +00001187 if (Alignment && Alignment < 4) {
1188 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1189 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1190 TII.get(ARM::VMOVRS), MoveReg)
1191 .addReg(SrcReg));
1192 SrcReg = MoveReg;
1193 VT = MVT::i32;
1194 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosier64ac91b2011-12-14 17:32:02 +00001195 } else {
1196 StrOpc = ARM::VSTRS;
Chad Rosier9eff1e32011-12-03 02:21:57 +00001197 }
Eric Christopher56d2b722010-09-02 23:43:26 +00001198 break;
1199 case MVT::f64:
1200 if (!Subtarget->hasVFP2()) return false;
Chad Rosiered42c5f2011-12-06 01:44:17 +00001201 // FIXME: Unaligned stores need special handling. Doublewords require
1202 // word-alignment.
Chad Rosier404ed3c2011-12-14 17:26:05 +00001203 if (Alignment && Alignment < 4)
Chad Rosier9eff1e32011-12-03 02:21:57 +00001204 return false;
Chad Rosier404ed3c2011-12-14 17:26:05 +00001205
Eric Christopher56d2b722010-09-02 23:43:26 +00001206 StrOpc = ARM::VSTRD;
1207 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001208 }
Eric Christopher564857f2010-12-01 01:40:24 +00001209 // Simplify this down to something we can handle.
Chad Rosierb29b9502011-11-13 02:23:59 +00001210 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach6b156392010-10-27 21:39:08 +00001211
Eric Christopher564857f2010-12-01 01:40:24 +00001212 // Create the base instruction, then add the operands.
1213 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1214 TII.get(StrOpc))
Chad Rosier3bdb3c92011-11-17 01:16:53 +00001215 .addReg(SrcReg);
Chad Rosierb29b9502011-11-13 02:23:59 +00001216 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher318b6ee2010-09-02 00:53:56 +00001217 return true;
1218}
1219
Eric Christopher43b62be2010-09-27 06:02:23 +00001220bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +00001221 Value *Op0 = I->getOperand(0);
1222 unsigned SrcReg = 0;
1223
Eli Friedman4136d232011-09-02 22:33:24 +00001224 // Atomic stores need special handling.
1225 if (cast<StoreInst>(I)->isAtomic())
1226 return false;
1227
Eric Christopher564857f2010-12-01 01:40:24 +00001228 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001229 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001230 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +00001231 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +00001232
Eric Christopher1b61ef42010-09-02 01:48:11 +00001233 // Get the value to be stored into a register.
1234 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +00001235 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001236
Eric Christopher564857f2010-12-01 01:40:24 +00001237 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +00001238 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +00001239 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +00001240 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001241
Chad Rosier9eff1e32011-12-03 02:21:57 +00001242 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1243 return false;
Eric Christophera5b1e682010-09-17 22:28:18 +00001244 return true;
1245}
1246
1247static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1248 switch (Pred) {
1249 // Needs two compares...
1250 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001251 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +00001252 default:
Eric Christopher4053e632010-11-02 01:24:49 +00001253 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +00001254 return ARMCC::AL;
1255 case CmpInst::ICMP_EQ:
1256 case CmpInst::FCMP_OEQ:
1257 return ARMCC::EQ;
1258 case CmpInst::ICMP_SGT:
1259 case CmpInst::FCMP_OGT:
1260 return ARMCC::GT;
1261 case CmpInst::ICMP_SGE:
1262 case CmpInst::FCMP_OGE:
1263 return ARMCC::GE;
1264 case CmpInst::ICMP_UGT:
1265 case CmpInst::FCMP_UGT:
1266 return ARMCC::HI;
1267 case CmpInst::FCMP_OLT:
1268 return ARMCC::MI;
1269 case CmpInst::ICMP_ULE:
1270 case CmpInst::FCMP_OLE:
1271 return ARMCC::LS;
1272 case CmpInst::FCMP_ORD:
1273 return ARMCC::VC;
1274 case CmpInst::FCMP_UNO:
1275 return ARMCC::VS;
1276 case CmpInst::FCMP_UGE:
1277 return ARMCC::PL;
1278 case CmpInst::ICMP_SLT:
1279 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001280 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001281 case CmpInst::ICMP_SLE:
1282 case CmpInst::FCMP_ULE:
1283 return ARMCC::LE;
1284 case CmpInst::FCMP_UNE:
1285 case CmpInst::ICMP_NE:
1286 return ARMCC::NE;
1287 case CmpInst::ICMP_UGE:
1288 return ARMCC::HS;
1289 case CmpInst::ICMP_ULT:
1290 return ARMCC::LO;
1291 }
Eric Christopher543cf052010-09-01 22:16:27 +00001292}
1293
Eric Christopher43b62be2010-09-27 06:02:23 +00001294bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001295 const BranchInst *BI = cast<BranchInst>(I);
1296 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1297 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001298
Eric Christophere5734102010-09-03 00:35:47 +00001299 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001300
Eric Christopher0e6233b2010-10-29 21:08:19 +00001301 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1302 // behavior.
Eric Christopher0e6233b2010-10-29 21:08:19 +00001303 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosier75698f32011-10-26 23:17:28 +00001304 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001305
1306 // Get the compare predicate.
Eric Christopher632ae892011-04-29 21:56:31 +00001307 // Try to take advantage of fallthrough opportunities.
1308 CmpInst::Predicate Predicate = CI->getPredicate();
1309 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1310 std::swap(TBB, FBB);
1311 Predicate = CmpInst::getInversePredicate(Predicate);
1312 }
1313
1314 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher0e6233b2010-10-29 21:08:19 +00001315
1316 // We may not handle every CC for now.
1317 if (ARMPred == ARMCC::AL) return false;
1318
Chad Rosier75698f32011-10-26 23:17:28 +00001319 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001320 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier75698f32011-10-26 23:17:28 +00001321 return false;
Jim Grosbach16cb3762010-11-09 19:22:26 +00001322
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001323 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1325 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1326 FastEmitBranch(FBB, DL);
1327 FuncInfo.MBB->addSuccessor(TBB);
1328 return true;
1329 }
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001330 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1331 MVT SourceVT;
1332 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedman76927d732011-05-25 23:49:02 +00001333 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001334 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001335 unsigned OpReg = getRegForValue(TI->getOperand(0));
1336 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1337 TII.get(TstOpc))
1338 .addReg(OpReg).addImm(1));
1339
1340 unsigned CCMode = ARMCC::NE;
1341 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1342 std::swap(TBB, FBB);
1343 CCMode = ARMCC::EQ;
1344 }
1345
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001346 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopherbcf26ae2011-04-29 20:02:39 +00001347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1348 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1349
1350 FastEmitBranch(FBB, DL);
1351 FuncInfo.MBB->addSuccessor(TBB);
1352 return true;
1353 }
Chad Rosier6d64b3a2011-10-27 00:21:16 +00001354 } else if (const ConstantInt *CI =
1355 dyn_cast<ConstantInt>(BI->getCondition())) {
1356 uint64_t Imm = CI->getZExtValue();
1357 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1358 FastEmitBranch(Target, DL);
1359 return true;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001360 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001361
Eric Christopher0e6233b2010-10-29 21:08:19 +00001362 unsigned CmpReg = getRegForValue(BI->getCondition());
1363 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001364
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001365 // We've been divorced from our compare! Our block was split, and
1366 // now our compare lives in a predecessor block. We musn't
1367 // re-compare here, as the children of the compare aren't guaranteed
1368 // live across the block boundary (we *could* check for this).
1369 // Regardless, the compare has been done in the predecessor block,
1370 // and it left a value for us in a virtual register. Ergo, we test
1371 // the one-bit value left in the virtual register.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001372 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Stuart Hastingsc5eecbc2011-04-16 03:31:26 +00001373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1374 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001375
Eric Christopher7a20a372011-04-28 16:52:09 +00001376 unsigned CCMode = ARMCC::NE;
1377 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1378 std::swap(TBB, FBB);
1379 CCMode = ARMCC::EQ;
1380 }
1381
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001382 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher7a20a372011-04-28 16:52:09 +00001384 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001385 FastEmitBranch(FBB, DL);
1386 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001387 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001388}
1389
Chad Rosier60c8fa62012-02-07 23:56:08 +00001390bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1391 unsigned AddrReg = getRegForValue(I->getOperand(0));
1392 if (AddrReg == 0) return false;
1393
1394 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1395 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
1396 .addReg(AddrReg));
Bill Wendling8f47fc82012-10-22 23:30:04 +00001397
1398 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1399 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1400 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1401
Jush Luefc967e2012-06-14 06:08:19 +00001402 return true;
Chad Rosier60c8fa62012-02-07 23:56:08 +00001403}
1404
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001405bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1406 bool isZExt) {
Chad Rosierade62002011-10-26 23:25:44 +00001407 Type *Ty = Src1Value->getType();
Patrik Hagglund3d170e62012-12-17 14:30:06 +00001408 EVT SrcEVT = TLI.getValueType(Ty, true);
1409 if (!SrcEVT.isSimple()) return false;
1410 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +00001411
Chad Rosierade62002011-10-26 23:25:44 +00001412 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1413 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherd43393a2010-09-08 23:13:45 +00001414 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001415
Chad Rosier2f2fe412011-11-09 03:22:02 +00001416 // Check to see if the 2nd operand is a constant that we can encode directly
1417 // in the compare.
Chad Rosier1c47de82011-11-11 06:27:41 +00001418 int Imm = 0;
1419 bool UseImm = false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001420 bool isNegativeImm = false;
Chad Rosierf56c60b2011-11-16 00:32:20 +00001421 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1422 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier2f2fe412011-11-09 03:22:02 +00001423 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1424 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1425 SrcVT == MVT::i1) {
1426 const APInt &CIVal = ConstInt->getValue();
Chad Rosier1c47de82011-11-11 06:27:41 +00001427 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier0ac754f2012-03-15 22:54:20 +00001428 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1429 // then a cmn, because there is no way to represent 2147483648 as a
1430 // signed 32-bit int.
1431 if (Imm < 0 && Imm != (int)0x80000000) {
1432 isNegativeImm = true;
1433 Imm = -Imm;
Chad Rosier6cba97c2011-11-10 01:30:39 +00001434 }
Chad Rosier0ac754f2012-03-15 22:54:20 +00001435 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1436 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001437 }
1438 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1439 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1440 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosier1c47de82011-11-11 06:27:41 +00001441 UseImm = true;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001442 }
1443
Eric Christopherd43393a2010-09-08 23:13:45 +00001444 unsigned CmpOpc;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001445 bool isICmp = true;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001446 bool needsExt = false;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001447 switch (SrcVT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001448 default: return false;
1449 // TODO: Verify compares.
1450 case MVT::f32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001451 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001452 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherd43393a2010-09-08 23:13:45 +00001453 break;
1454 case MVT::f64:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001455 isICmp = false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001456 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherd43393a2010-09-08 23:13:45 +00001457 break;
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001458 case MVT::i1:
1459 case MVT::i8:
1460 case MVT::i16:
1461 needsExt = true;
1462 // Intentional fall-through.
Eric Christopherd43393a2010-09-08 23:13:45 +00001463 case MVT::i32:
Chad Rosier2f2fe412011-11-09 03:22:02 +00001464 if (isThumb2) {
Chad Rosier1c47de82011-11-11 06:27:41 +00001465 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001466 CmpOpc = ARM::t2CMPrr;
1467 else
Bill Wendlingad5c8802012-06-11 08:07:26 +00001468 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001469 } else {
Chad Rosier1c47de82011-11-11 06:27:41 +00001470 if (!UseImm)
Chad Rosier2f2fe412011-11-09 03:22:02 +00001471 CmpOpc = ARM::CMPrr;
1472 else
Bill Wendlingad5c8802012-06-11 08:07:26 +00001473 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001474 }
Eric Christopherd43393a2010-09-08 23:13:45 +00001475 break;
1476 }
1477
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001478 unsigned SrcReg1 = getRegForValue(Src1Value);
1479 if (SrcReg1 == 0) return false;
Chad Rosier530f7ce2011-10-26 22:47:55 +00001480
Duncan Sands4c0c5452011-11-28 10:31:27 +00001481 unsigned SrcReg2 = 0;
Chad Rosier1c47de82011-11-11 06:27:41 +00001482 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001483 SrcReg2 = getRegForValue(Src2Value);
1484 if (SrcReg2 == 0) return false;
1485 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001486
1487 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1488 if (needsExt) {
Chad Rosiera69feb02012-02-16 22:45:33 +00001489 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1490 if (SrcReg1 == 0) return false;
Chad Rosier1c47de82011-11-11 06:27:41 +00001491 if (!UseImm) {
Chad Rosiera69feb02012-02-16 22:45:33 +00001492 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1493 if (SrcReg2 == 0) return false;
Chad Rosier2f2fe412011-11-09 03:22:02 +00001494 }
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001495 }
Chad Rosier530f7ce2011-10-26 22:47:55 +00001496
Chad Rosier1c47de82011-11-11 06:27:41 +00001497 if (!UseImm) {
Chad Rosier2f2fe412011-11-09 03:22:02 +00001498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1499 TII.get(CmpOpc))
1500 .addReg(SrcReg1).addReg(SrcReg2));
1501 } else {
1502 MachineInstrBuilder MIB;
1503 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1504 .addReg(SrcReg1);
1505
1506 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1507 if (isICmp)
Chad Rosier1c47de82011-11-11 06:27:41 +00001508 MIB.addImm(Imm);
Chad Rosier2f2fe412011-11-09 03:22:02 +00001509 AddOptionalDefs(MIB);
1510 }
Chad Rosierade62002011-10-26 23:25:44 +00001511
1512 // For floating point we need to move the result to a comparison register
1513 // that we can then use for branches.
1514 if (Ty->isFloatTy() || Ty->isDoubleTy())
1515 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1516 TII.get(ARM::FMSTAT)));
Chad Rosier530f7ce2011-10-26 22:47:55 +00001517 return true;
1518}
1519
1520bool ARMFastISel::SelectCmp(const Instruction *I) {
1521 const CmpInst *CI = cast<CmpInst>(I);
1522
Eric Christopher229207a2010-09-29 01:14:47 +00001523 // Get the compare predicate.
1524 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001525
Eric Christopher229207a2010-09-29 01:14:47 +00001526 // We may not handle every CC for now.
1527 if (ARMPred == ARMCC::AL) return false;
1528
Chad Rosier530f7ce2011-10-26 22:47:55 +00001529 // Emit the compare.
Chad Rosiere07cd5e2011-11-02 18:08:25 +00001530 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier530f7ce2011-10-26 22:47:55 +00001531 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001532
Eric Christopher229207a2010-09-29 01:14:47 +00001533 // Now set a register based on the comparison. Explicitly set the predicates
1534 // here.
Chad Rosier66dc8ca2011-11-08 21:12:00 +00001535 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topper420761a2012-04-20 07:30:17 +00001536 const TargetRegisterClass *RC = isThumb2 ?
1537 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1538 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher5d18d922010-10-07 05:39:19 +00001539 unsigned DestReg = createResultReg(RC);
Chad Rosierade62002011-10-26 23:25:44 +00001540 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001541 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier44c98b72012-03-07 20:59:26 +00001542 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Eric Christopher229207a2010-09-29 01:14:47 +00001543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1544 .addReg(ZeroReg).addImm(1)
Chad Rosier44c98b72012-03-07 20:59:26 +00001545 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher229207a2010-09-29 01:14:47 +00001546
Eric Christophera5b1e682010-09-17 22:28:18 +00001547 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001548 return true;
1549}
1550
Eric Christopher43b62be2010-09-27 06:02:23 +00001551bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001552 // Make sure we have VFP and that we're extending float to double.
1553 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001554
Eric Christopher46203602010-09-09 00:26:48 +00001555 Value *V = I->getOperand(0);
1556 if (!I->getType()->isDoubleTy() ||
1557 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001558
Eric Christopher46203602010-09-09 00:26:48 +00001559 unsigned Op = getRegForValue(V);
1560 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001561
Craig Topper420761a2012-04-20 07:30:17 +00001562 unsigned Result = createResultReg(&ARM::DPRRegClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001563 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001564 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001565 .addReg(Op));
1566 UpdateValueMap(I, Result);
1567 return true;
1568}
1569
Eric Christopher43b62be2010-09-27 06:02:23 +00001570bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001571 // Make sure we have VFP and that we're truncating double to float.
1572 if (!Subtarget->hasVFP2()) return false;
1573
1574 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001575 if (!(I->getType()->isFloatTy() &&
1576 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001577
1578 unsigned Op = getRegForValue(V);
1579 if (Op == 0) return false;
1580
Craig Topper420761a2012-04-20 07:30:17 +00001581 unsigned Result = createResultReg(&ARM::SPRRegClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001582 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001583 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001584 .addReg(Op));
1585 UpdateValueMap(I, Result);
1586 return true;
1587}
1588
Chad Rosierae46a332012-02-03 21:14:11 +00001589bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher9a040492010-09-09 18:54:59 +00001590 // Make sure we have VFP.
1591 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001592
Duncan Sands1440e8b2010-11-03 11:35:31 +00001593 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001594 Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001595 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001596 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001597
Chad Rosier463fe242011-11-03 02:04:59 +00001598 Value *Src = I->getOperand(0);
Patrik Hagglund3d170e62012-12-17 14:30:06 +00001599 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1600 if (!SrcEVT.isSimple())
1601 return false;
1602 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosier463fe242011-11-03 02:04:59 +00001603 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman783c6642011-05-25 19:09:45 +00001604 return false;
1605
Chad Rosier463fe242011-11-03 02:04:59 +00001606 unsigned SrcReg = getRegForValue(Src);
1607 if (SrcReg == 0) return false;
1608
1609 // Handle sign-extension.
1610 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier316a5aa2012-12-17 19:59:43 +00001611 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosierae46a332012-02-03 21:14:11 +00001612 /*isZExt*/!isSigned);
Chad Rosiera69feb02012-02-16 22:45:33 +00001613 if (SrcReg == 0) return false;
Chad Rosier463fe242011-11-03 02:04:59 +00001614 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001615
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001616 // The conversion routine works on fp-reg to fp-reg and the operand above
1617 // was an integer, move it to the fp registers if possible.
Chad Rosier463fe242011-11-03 02:04:59 +00001618 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001619 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001620
Eric Christopher9a040492010-09-09 18:54:59 +00001621 unsigned Opc;
Chad Rosierae46a332012-02-03 21:14:11 +00001622 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1623 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001624 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001625
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001626 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001627 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1628 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001629 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001630 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001631 return true;
1632}
1633
Chad Rosierae46a332012-02-03 21:14:11 +00001634bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher9a040492010-09-09 18:54:59 +00001635 // Make sure we have VFP.
1636 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001637
Duncan Sands1440e8b2010-11-03 11:35:31 +00001638 MVT DstVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001639 Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001640 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001641 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001642
Eric Christopher9a040492010-09-09 18:54:59 +00001643 unsigned Op = getRegForValue(I->getOperand(0));
1644 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001645
Eric Christopher9a040492010-09-09 18:54:59 +00001646 unsigned Opc;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001647 Type *OpTy = I->getOperand(0)->getType();
Chad Rosierae46a332012-02-03 21:14:11 +00001648 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1649 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosierdd1e7512011-08-31 23:49:05 +00001650 else return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001651
Chad Rosieree8901c2012-02-03 20:27:51 +00001652 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001653 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001654 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1655 ResultReg)
1656 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001657
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001658 // This result needs to be in an integer register, but the conversion only
1659 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001660 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001661 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001662
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001663 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001664 return true;
1665}
1666
Eric Christopher3bbd3962010-10-11 08:27:59 +00001667bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001668 MVT VT;
1669 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001670 return false;
1671
1672 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001673 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001674
1675 unsigned CondReg = getRegForValue(I->getOperand(0));
1676 if (CondReg == 0) return false;
1677 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1678 if (Op1Reg == 0) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001679
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001680 // Check to see if we can use an immediate in the conditional move.
1681 int Imm = 0;
1682 bool UseImm = false;
1683 bool isNegativeImm = false;
1684 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1685 assert (VT == MVT::i32 && "Expecting an i32.");
1686 Imm = (int)ConstInt->getValue().getZExtValue();
1687 if (Imm < 0) {
1688 isNegativeImm = true;
1689 Imm = ~Imm;
1690 }
1691 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1692 (ARM_AM::getSOImmVal(Imm) != -1);
1693 }
1694
Duncan Sands4c0c5452011-11-28 10:31:27 +00001695 unsigned Op2Reg = 0;
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001696 if (!UseImm) {
1697 Op2Reg = getRegForValue(I->getOperand(2));
1698 if (Op2Reg == 0) return false;
1699 }
1700
1701 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001702 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001703 .addReg(CondReg).addImm(0));
1704
1705 unsigned MovCCOpc;
Chad Rosierac3158b2012-11-27 21:46:46 +00001706 const TargetRegisterClass *RC;
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001707 if (!UseImm) {
Chad Rosierac3158b2012-11-27 21:46:46 +00001708 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001709 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1710 } else {
Chad Rosierac3158b2012-11-27 21:46:46 +00001711 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1712 if (!isNegativeImm)
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001713 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosierac3158b2012-11-27 21:46:46 +00001714 else
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001715 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001716 }
Eric Christopher3bbd3962010-10-11 08:27:59 +00001717 unsigned ResultReg = createResultReg(RC);
Chad Rosiera07d3fc2011-11-11 06:20:39 +00001718 if (!UseImm)
1719 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1720 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1721 else
1722 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1723 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001724 UpdateValueMap(I, ResultReg);
1725 return true;
1726}
1727
Chad Rosier7ccb30b2012-02-03 21:07:27 +00001728bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001729 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001730 Type *Ty = I->getType();
Eric Christopher08637852010-09-30 22:34:19 +00001731 if (!isTypeLegal(Ty, VT))
1732 return false;
1733
1734 // If we have integer div support we should have selected this automagically.
1735 // In case we have a real miss go ahead and return false and we'll pick
1736 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001737 if (Subtarget->hasDivide()) return false;
1738
Eric Christopher08637852010-09-30 22:34:19 +00001739 // Otherwise emit a libcall.
1740 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001741 if (VT == MVT::i8)
Chad Rosier7ccb30b2012-02-03 21:07:27 +00001742 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001743 else if (VT == MVT::i16)
Chad Rosier7ccb30b2012-02-03 21:07:27 +00001744 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher08637852010-09-30 22:34:19 +00001745 else if (VT == MVT::i32)
Chad Rosier7ccb30b2012-02-03 21:07:27 +00001746 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher08637852010-09-30 22:34:19 +00001747 else if (VT == MVT::i64)
Chad Rosier7ccb30b2012-02-03 21:07:27 +00001748 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher08637852010-09-30 22:34:19 +00001749 else if (VT == MVT::i128)
Chad Rosier7ccb30b2012-02-03 21:07:27 +00001750 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher08637852010-09-30 22:34:19 +00001751 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001752
Eric Christopher08637852010-09-30 22:34:19 +00001753 return ARMEmitLibcall(I, LC);
1754}
1755
Chad Rosier769422f2012-02-03 21:23:45 +00001756bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001757 MVT VT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001758 Type *Ty = I->getType();
Eric Christopher6a880d62010-10-11 08:37:26 +00001759 if (!isTypeLegal(Ty, VT))
1760 return false;
1761
1762 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1763 if (VT == MVT::i8)
Chad Rosier769422f2012-02-03 21:23:45 +00001764 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christopher6a880d62010-10-11 08:37:26 +00001765 else if (VT == MVT::i16)
Chad Rosier769422f2012-02-03 21:23:45 +00001766 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christopher6a880d62010-10-11 08:37:26 +00001767 else if (VT == MVT::i32)
Chad Rosier769422f2012-02-03 21:23:45 +00001768 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christopher6a880d62010-10-11 08:37:26 +00001769 else if (VT == MVT::i64)
Chad Rosier769422f2012-02-03 21:23:45 +00001770 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christopher6a880d62010-10-11 08:37:26 +00001771 else if (VT == MVT::i128)
Chad Rosier769422f2012-02-03 21:23:45 +00001772 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001773 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001774
Eric Christopher6a880d62010-10-11 08:37:26 +00001775 return ARMEmitLibcall(I, LC);
1776}
1777
Chad Rosier3901c3e2012-02-06 23:50:07 +00001778bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier3901c3e2012-02-06 23:50:07 +00001779 EVT DestVT = TLI.getValueType(I->getType(), true);
1780
1781 // We can get here in the case when we have a binary operation on a non-legal
1782 // type and the target independent selector doesn't know how to handle it.
1783 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1784 return false;
Jush Luefc967e2012-06-14 06:08:19 +00001785
Chad Rosier6fde8752012-02-08 02:29:21 +00001786 unsigned Opc;
1787 switch (ISDOpcode) {
1788 default: return false;
1789 case ISD::ADD:
1790 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1791 break;
1792 case ISD::OR:
1793 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1794 break;
Chad Rosier743e1992012-02-08 02:45:44 +00001795 case ISD::SUB:
1796 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1797 break;
Chad Rosier6fde8752012-02-08 02:29:21 +00001798 }
1799
Chad Rosier3901c3e2012-02-06 23:50:07 +00001800 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1801 if (SrcReg1 == 0) return false;
1802
1803 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1804 // in the instruction, rather then materializing the value in a register.
1805 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1806 if (SrcReg2 == 0) return false;
1807
JF Bastiena9a8a122013-05-29 15:45:47 +00001808 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Chad Rosier3901c3e2012-02-06 23:50:07 +00001809 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1810 TII.get(Opc), ResultReg)
1811 .addReg(SrcReg1).addReg(SrcReg2));
1812 UpdateValueMap(I, ResultReg);
1813 return true;
1814}
1815
1816bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier316a5aa2012-12-17 19:59:43 +00001817 EVT FPVT = TLI.getValueType(I->getType(), true);
1818 if (!FPVT.isSimple()) return false;
1819 MVT VT = FPVT.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +00001820
Eric Christopherbc39b822010-09-09 00:53:57 +00001821 // We can get here in the case when we want to use NEON for our fp
1822 // operations, but can't figure out how to. Just use the vfp instructions
1823 // if we have them.
1824 // FIXME: It'd be nice to use NEON instructions.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001825 Type *Ty = I->getType();
Eric Christopherbd6bf082010-09-09 01:02:03 +00001826 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1827 if (isFloat && !Subtarget->hasVFP2())
1828 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001829
Eric Christopherbc39b822010-09-09 00:53:57 +00001830 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001831 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001832 switch (ISDOpcode) {
1833 default: return false;
1834 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001835 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001836 break;
1837 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001838 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001839 break;
1840 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001841 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001842 break;
1843 }
Chad Rosier508a1f42011-11-16 18:39:44 +00001844 unsigned Op1 = getRegForValue(I->getOperand(0));
1845 if (Op1 == 0) return false;
1846
1847 unsigned Op2 = getRegForValue(I->getOperand(1));
1848 if (Op2 == 0) return false;
1849
Chad Rosier316a5aa2012-12-17 19:59:43 +00001850 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Eric Christopherbc39b822010-09-09 00:53:57 +00001851 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1852 TII.get(Opc), ResultReg)
1853 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001854 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001855 return true;
1856}
1857
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001858// Call Handling Code
1859
Jush Luee649832012-07-19 09:49:00 +00001860// This is largely taken directly from CCAssignFnForNode
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001861// TODO: We may not support all of this.
Jush Luee649832012-07-19 09:49:00 +00001862CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1863 bool Return,
1864 bool isVarArg) {
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001865 switch (CC) {
1866 default:
1867 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001868 case CallingConv::Fast:
Jush Lu2ff4e9d2012-08-16 05:15:53 +00001869 if (Subtarget->hasVFP2() && !isVarArg) {
1870 if (!Subtarget->isAAPCS_ABI())
1871 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1872 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1873 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1874 }
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001875 // Fallthrough
1876 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001877 // Use target triple & subtarget features to do actual dispatch.
1878 if (Subtarget->isAAPCS_ABI()) {
1879 if (Subtarget->hasVFP2() &&
Jush Luee649832012-07-19 09:49:00 +00001880 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001881 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1882 else
1883 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1884 } else
1885 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1886 case CallingConv::ARM_AAPCS_VFP:
Jush Luee649832012-07-19 09:49:00 +00001887 if (!isVarArg)
1888 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1889 // Fall through to soft float variant, variadic functions don't
1890 // use hard floating point ABI.
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001891 case CallingConv::ARM_AAPCS:
1892 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1893 case CallingConv::ARM_APCS:
1894 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001895 case CallingConv::GHC:
1896 if (Return)
1897 llvm_unreachable("Can't return in GHC call convention");
1898 else
1899 return CC_ARM_APCS_GHC;
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001900 }
1901}
1902
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001903bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1904 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001905 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001906 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1907 SmallVectorImpl<unsigned> &RegArgs,
1908 CallingConv::ID CC,
Jush Luee649832012-07-19 09:49:00 +00001909 unsigned &NumBytes,
1910 bool isVarArg) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001911 SmallVector<CCValAssign, 16> ArgLocs;
Jush Luee649832012-07-19 09:49:00 +00001912 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1913 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1914 CCAssignFnForCall(CC, false, isVarArg));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001915
Bill Wendling5aeff312012-03-16 23:11:07 +00001916 // Check that we can handle all of the arguments. If we can't, then bail out
1917 // now before we add code to the MBB.
1918 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1919 CCValAssign &VA = ArgLocs[i];
1920 MVT ArgVT = ArgVTs[VA.getValNo()];
1921
1922 // We don't handle NEON/vector parameters yet.
1923 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1924 return false;
1925
1926 // Now copy/store arg to correct locations.
1927 if (VA.isRegLoc() && !VA.needsCustom()) {
1928 continue;
1929 } else if (VA.needsCustom()) {
1930 // TODO: We need custom lowering for vector (v2f64) args.
1931 if (VA.getLocVT() != MVT::f64 ||
1932 // TODO: Only handle register args for now.
1933 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1934 return false;
1935 } else {
Craig Topper5a0910b2013-08-15 02:33:50 +00001936 switch (ArgVT.SimpleTy) {
Bill Wendling5aeff312012-03-16 23:11:07 +00001937 default:
1938 return false;
1939 case MVT::i1:
1940 case MVT::i8:
1941 case MVT::i16:
1942 case MVT::i32:
1943 break;
1944 case MVT::f32:
1945 if (!Subtarget->hasVFP2())
1946 return false;
1947 break;
1948 case MVT::f64:
1949 if (!Subtarget->hasVFP2())
1950 return false;
1951 break;
1952 }
1953 }
1954 }
1955
1956 // At the point, we are able to handle the call's arguments in fast isel.
1957
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001958 // Get a count of how many bytes are to be pushed on the stack.
1959 NumBytes = CCInfo.getNextStackOffset();
1960
1961 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001962 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001963 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1964 TII.get(AdjStackDown))
1965 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001966
1967 // Process the args.
1968 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1969 CCValAssign &VA = ArgLocs[i];
1970 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001971 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001972
Bill Wendling5aeff312012-03-16 23:11:07 +00001973 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1974 "We don't handle NEON/vector parameters yet.");
Eric Christophera4633f52010-10-23 09:37:17 +00001975
Eric Christopherf9764fa2010-09-30 20:49:44 +00001976 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001977 switch (VA.getLocInfo()) {
1978 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001979 case CCValAssign::SExt: {
Chad Rosierb74c8652011-12-02 20:25:18 +00001980 MVT DestVT = VA.getLocVT();
Chad Rosier5793a652012-02-14 22:29:48 +00001981 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1982 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosierb74c8652011-12-02 20:25:18 +00001983 ArgVT = DestVT;
Eric Christopherfa87d662010-10-18 02:17:53 +00001984 break;
1985 }
Chad Rosier42536af2011-11-05 20:16:15 +00001986 case CCValAssign::AExt:
1987 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherfa87d662010-10-18 02:17:53 +00001988 case CCValAssign::ZExt: {
Chad Rosierb74c8652011-12-02 20:25:18 +00001989 MVT DestVT = VA.getLocVT();
Chad Rosier5793a652012-02-14 22:29:48 +00001990 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien8fc760c2013-06-07 20:10:37 +00001991 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosierb74c8652011-12-02 20:25:18 +00001992 ArgVT = DestVT;
Eric Christopherfa87d662010-10-18 02:17:53 +00001993 break;
1994 }
1995 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001996 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001997 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001998 assert(BC != 0 && "Failed to emit a bitcast!");
1999 Arg = BC;
2000 ArgVT = VA.getLocVT();
2001 break;
2002 }
2003 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002004 }
2005
2006 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00002007 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00002009 VA.getLocReg())
Chad Rosier42536af2011-11-05 20:16:15 +00002010 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002011 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00002012 } else if (VA.needsCustom()) {
2013 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling5aeff312012-03-16 23:11:07 +00002014 assert(VA.getLocVT() == MVT::f64 &&
2015 "Custom lowering for v2f64 args not available");
Jim Grosbach6b156392010-10-27 21:39:08 +00002016
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00002017 CCValAssign &NextVA = ArgLocs[++i];
2018
Bill Wendling5aeff312012-03-16 23:11:07 +00002019 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2020 "We only handle register args!");
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00002021
2022 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2023 TII.get(ARM::VMOVRRD), VA.getLocReg())
2024 .addReg(NextVA.getLocReg(), RegState::Define)
2025 .addReg(Arg));
2026 RegArgs.push_back(VA.getLocReg());
2027 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002028 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00002029 assert(VA.isMemLoc());
2030 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00002031 Address Addr;
2032 Addr.BaseType = Address::RegBase;
2033 Addr.Base.Reg = ARM::SP;
2034 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00002035
Bill Wendling5aeff312012-03-16 23:11:07 +00002036 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2037 assert(EmitRet && "Could not emit a store for argument!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002038 }
2039 }
Bill Wendling5aeff312012-03-16 23:11:07 +00002040
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002041 return true;
2042}
2043
Duncan Sands1440e8b2010-11-03 11:35:31 +00002044bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002045 const Instruction *I, CallingConv::ID CC,
Jush Luee649832012-07-19 09:49:00 +00002046 unsigned &NumBytes, bool isVarArg) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002047 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00002048 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00002049 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2050 TII.get(AdjStackUp))
2051 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002052
2053 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00002054 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002055 SmallVector<CCValAssign, 16> RVLocs;
Jush Luee649832012-07-19 09:49:00 +00002056 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2057 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002058
2059 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00002060 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00002061 // For this move we copy into two registers and then move into the
2062 // double fp reg we want.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00002063 MVT DestVT = RVLocs[0].getValVT();
Craig Topper44d23822012-02-22 05:59:10 +00002064 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopher14df8822010-10-01 00:00:11 +00002065 unsigned ResultReg = createResultReg(DstRC);
2066 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2067 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00002068 .addReg(RVLocs[0].getLocReg())
2069 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00002070
Eric Christopher3659ac22010-10-20 08:02:24 +00002071 UsedRegs.push_back(RVLocs[0].getLocReg());
2072 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00002073
Eric Christopherdccd2c32010-10-11 08:38:55 +00002074 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00002075 UpdateValueMap(I, ResultReg);
Chad Rosier2a2e9d52012-05-11 18:51:55 +00002076 } else {
2077 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00002078 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier0eff39f2011-11-08 00:03:32 +00002079
2080 // Special handling for extended integers.
2081 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2082 CopyVT = MVT::i32;
2083
Craig Topper44d23822012-02-22 05:59:10 +00002084 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002085
Eric Christopher14df8822010-10-01 00:00:11 +00002086 unsigned ResultReg = createResultReg(DstRC);
2087 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2088 ResultReg).addReg(RVLocs[0].getLocReg());
2089 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002090
Eric Christopherdccd2c32010-10-11 08:38:55 +00002091 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00002092 UpdateValueMap(I, ResultReg);
2093 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002094 }
2095
Eric Christopherdccd2c32010-10-11 08:38:55 +00002096 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002097}
2098
Eric Christopher4f512ef2010-10-22 01:28:00 +00002099bool ARMFastISel::SelectRet(const Instruction *I) {
2100 const ReturnInst *Ret = cast<ReturnInst>(I);
2101 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00002102
Eric Christopher4f512ef2010-10-22 01:28:00 +00002103 if (!FuncInfo.CanLowerReturn)
2104 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00002105
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002106 // Build a list of return value registers.
2107 SmallVector<unsigned, 4> RetRegs;
2108
Eric Christopher4f512ef2010-10-22 01:28:00 +00002109 CallingConv::ID CC = F.getCallingConv();
2110 if (Ret->getNumOperands() > 0) {
2111 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00002112 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002113
2114 // Analyze operands of the call, assigning locations to each operand.
2115 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbachb04546f2011-09-13 20:30:37 +00002116 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Jush Luee649832012-07-19 09:49:00 +00002117 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2118 F.isVarArg()));
Eric Christopher4f512ef2010-10-22 01:28:00 +00002119
2120 const Value *RV = Ret->getOperand(0);
2121 unsigned Reg = getRegForValue(RV);
2122 if (Reg == 0)
2123 return false;
2124
2125 // Only handle a single return value for now.
2126 if (ValLocs.size() != 1)
2127 return false;
2128
2129 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00002130
Eric Christopher4f512ef2010-10-22 01:28:00 +00002131 // Don't bother handling odd stuff for now.
2132 if (VA.getLocInfo() != CCValAssign::Full)
2133 return false;
2134 // Only handle register returns for now.
2135 if (!VA.isRegLoc())
2136 return false;
Chad Rosierf470cbb2011-11-04 00:50:21 +00002137
2138 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier316a5aa2012-12-17 19:59:43 +00002139 EVT RVEVT = TLI.getValueType(RV->getType());
2140 if (!RVEVT.isSimple()) return false;
2141 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00002142 MVT DestVT = VA.getValVT();
Chad Rosierf470cbb2011-11-04 00:50:21 +00002143 // Special handling for extended integers.
2144 if (RVVT != DestVT) {
2145 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2146 return false;
2147
Chad Rosierf470cbb2011-11-04 00:50:21 +00002148 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2149
Chad Rosierb8703fe2012-02-17 01:21:28 +00002150 // Perform extension if flagged as either zext or sext. Otherwise, do
2151 // nothing.
2152 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2153 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2154 if (SrcReg == 0) return false;
2155 }
Chad Rosierf470cbb2011-11-04 00:50:21 +00002156 }
Jim Grosbach6b156392010-10-27 21:39:08 +00002157
Eric Christopher4f512ef2010-10-22 01:28:00 +00002158 // Make the copy.
Eric Christopher4f512ef2010-10-22 01:28:00 +00002159 unsigned DstReg = VA.getLocReg();
2160 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2161 // Avoid a cross-class copy. This is very unlikely.
2162 if (!SrcRC->contains(DstReg))
2163 return false;
2164 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2165 DstReg).addReg(SrcReg);
2166
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002167 // Add register to return instruction.
2168 RetRegs.push_back(VA.getLocReg());
Eric Christopher4f512ef2010-10-22 01:28:00 +00002169 }
Jim Grosbach6b156392010-10-27 21:39:08 +00002170
Chad Rosier66dc8ca2011-11-08 21:12:00 +00002171 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002172 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2173 TII.get(RetOpc));
2174 AddOptionalDefs(MIB);
2175 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2176 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002177 return true;
2178}
2179
Chad Rosier49d6fc02012-06-12 19:25:13 +00002180unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2181 if (UseReg)
2182 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2183 else
2184 return isThumb2 ? ARM::tBL : ARM::BL;
2185}
2186
2187unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth6c54b3d2013-07-27 11:23:08 +00002188 // Manually compute the global's type to avoid building it when unnecessary.
2189 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2190 EVT LCREVT = TLI.getValueType(GVTy);
2191 if (!LCREVT.isSimple()) return 0;
2192
Chad Rosier49d6fc02012-06-12 19:25:13 +00002193 GlobalValue *GV = new GlobalVariable(Type::getInt32Ty(*Context), false,
2194 GlobalValue::ExternalLinkage, 0, Name);
Chandler Carruth6c54b3d2013-07-27 11:23:08 +00002195 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier316a5aa2012-12-17 19:59:43 +00002196 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher872f4a22011-02-22 01:37:10 +00002197}
2198
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002199// A quick function that will emit a call for a named libcall in F with the
2200// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00002201// can emit a call for any libcall we can produce. This is an abridged version
2202// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002203// like computed function pointers or strange arguments at call sites.
2204// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2205// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00002206bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2207 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002208
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002209 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002210 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002211 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002212 if (RetTy->isVoidTy())
2213 RetVT = MVT::isVoid;
2214 else if (!isTypeLegal(RetTy, RetVT))
2215 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002216
Chad Rosier2a2e9d52012-05-11 18:51:55 +00002217 // Can't handle non-double multi-reg retvals.
Jush Luefc967e2012-06-14 06:08:19 +00002218 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier2a2e9d52012-05-11 18:51:55 +00002219 SmallVector<CCValAssign, 16> RVLocs;
2220 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Jush Luee649832012-07-19 09:49:00 +00002221 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier2a2e9d52012-05-11 18:51:55 +00002222 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2223 return false;
2224 }
2225
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002226 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002227 SmallVector<Value*, 8> Args;
2228 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002229 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002230 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2231 Args.reserve(I->getNumOperands());
2232 ArgRegs.reserve(I->getNumOperands());
2233 ArgVTs.reserve(I->getNumOperands());
2234 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00002235 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002236 Value *Op = I->getOperand(i);
2237 unsigned Arg = getRegForValue(Op);
2238 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002239
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002240 Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002241 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002242 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002243
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002244 ISD::ArgFlagsTy Flags;
2245 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2246 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002247
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002248 Args.push_back(Op);
2249 ArgRegs.push_back(Arg);
2250 ArgVTs.push_back(ArgVT);
2251 ArgFlags.push_back(Flags);
2252 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002253
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002254 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002255 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002256 unsigned NumBytes;
Jush Luee649832012-07-19 09:49:00 +00002257 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2258 RegArgs, CC, NumBytes, false))
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002259 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002260
Chad Rosier49d6fc02012-06-12 19:25:13 +00002261 unsigned CalleeReg = 0;
2262 if (EnableARMLongCalls) {
2263 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2264 if (CalleeReg == 0) return false;
2265 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002266
Chad Rosier49d6fc02012-06-12 19:25:13 +00002267 // Issue the call.
2268 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2269 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2270 DL, TII.get(CallOpc));
Jakob Stoklund Olesen0745b642012-08-24 20:52:46 +00002271 // BL / BLX don't take a predicate, but tBL / tBLX do.
2272 if (isThumb2)
Chad Rosier49d6fc02012-06-12 19:25:13 +00002273 AddDefaultPred(MIB);
Jakob Stoklund Olesen0745b642012-08-24 20:52:46 +00002274 if (EnableARMLongCalls)
2275 MIB.addReg(CalleeReg);
2276 else
2277 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosier49d6fc02012-06-12 19:25:13 +00002278
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002279 // Add implicit physical register uses to the call.
2280 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesen0745b642012-08-24 20:52:46 +00002281 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002282
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00002283 // Add a register mask with the call-preserved registers.
2284 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2285 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2286
Eric Christophera9a7a1a2010-09-29 23:11:09 +00002287 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002288 SmallVector<unsigned, 4> UsedRegs;
Jush Luee649832012-07-19 09:49:00 +00002289 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002290
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002291 // Set all unused physreg defs as dead.
2292 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002293
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002294 return true;
2295}
2296
Chad Rosier11add262011-11-11 23:31:03 +00002297bool ARMFastISel::SelectCall(const Instruction *I,
2298 const char *IntrMemName = 0) {
Eric Christopherf9764fa2010-09-30 20:49:44 +00002299 const CallInst *CI = cast<CallInst>(I);
2300 const Value *Callee = CI->getCalledValue();
2301
Chad Rosier11add262011-11-11 23:31:03 +00002302 // Can't handle inline asm.
2303 if (isa<InlineAsm>(Callee)) return false;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002304
Chad Rosier425e9512012-12-11 00:18:02 +00002305 // Allow SelectionDAG isel to handle tail calls.
2306 if (CI->isTailCall()) return false;
2307
Eric Christopherf9764fa2010-09-30 20:49:44 +00002308 // Check the calling convention.
2309 ImmutableCallSite CS(CI);
2310 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00002311
Eric Christopherf9764fa2010-09-30 20:49:44 +00002312 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00002313
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002314 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2315 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Luee649832012-07-19 09:49:00 +00002316 bool isVarArg = FTy->isVarArg();
Eric Christopherdccd2c32010-10-11 08:38:55 +00002317
Eric Christopherf9764fa2010-09-30 20:49:44 +00002318 // Handle *simple* calls for now.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002319 Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002320 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002321 if (RetTy->isVoidTy())
2322 RetVT = MVT::isVoid;
Chad Rosier0eff39f2011-11-08 00:03:32 +00002323 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2324 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002325 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002326
Chad Rosier2a2e9d52012-05-11 18:51:55 +00002327 // Can't handle non-double multi-reg retvals.
2328 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2329 RetVT != MVT::i16 && RetVT != MVT::i32) {
2330 SmallVector<CCValAssign, 16> RVLocs;
Jush Luee649832012-07-19 09:49:00 +00002331 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2332 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier2a2e9d52012-05-11 18:51:55 +00002333 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2334 return false;
2335 }
2336
Eric Christopherf9764fa2010-09-30 20:49:44 +00002337 // Set up the argument vectors.
2338 SmallVector<Value*, 8> Args;
2339 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002340 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002341 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier92fd0172012-02-15 00:23:55 +00002342 unsigned arg_size = CS.arg_size();
2343 Args.reserve(arg_size);
2344 ArgRegs.reserve(arg_size);
2345 ArgVTs.reserve(arg_size);
2346 ArgFlags.reserve(arg_size);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002347 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2348 i != e; ++i) {
Chad Rosier11add262011-11-11 23:31:03 +00002349 // If we're lowering a memory intrinsic instead of a regular call, skip the
2350 // last two arguments, which shouldn't be passed to the underlying function.
2351 if (IntrMemName && e-i <= 2)
2352 break;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002353
Eric Christopherf9764fa2010-09-30 20:49:44 +00002354 ISD::ArgFlagsTy Flags;
2355 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00002356 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopherf9764fa2010-09-30 20:49:44 +00002357 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002358 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopherf9764fa2010-09-30 20:49:44 +00002359 Flags.setZExt();
2360
Chad Rosier8e4a2e42011-11-04 00:58:10 +00002361 // FIXME: Only handle *easy* calls for now.
Bill Wendling034b94b2012-12-19 07:18:57 +00002362 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2363 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2364 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2365 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopherf9764fa2010-09-30 20:49:44 +00002366 return false;
2367
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002368 Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00002369 MVT ArgVT;
Chad Rosier42536af2011-11-05 20:16:15 +00002370 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2371 ArgVT != MVT::i1)
Eric Christopherf9764fa2010-09-30 20:49:44 +00002372 return false;
Chad Rosier424fe0e2011-11-18 01:17:34 +00002373
2374 unsigned Arg = getRegForValue(*i);
2375 if (Arg == 0)
2376 return false;
2377
Eric Christopherf9764fa2010-09-30 20:49:44 +00002378 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2379 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002380
Eric Christopherf9764fa2010-09-30 20:49:44 +00002381 Args.push_back(*i);
2382 ArgRegs.push_back(Arg);
2383 ArgVTs.push_back(ArgVT);
2384 ArgFlags.push_back(Flags);
2385 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00002386
Eric Christopherf9764fa2010-09-30 20:49:44 +00002387 // Handle the arguments now that we've gotten them.
2388 SmallVector<unsigned, 4> RegArgs;
2389 unsigned NumBytes;
Jush Luee649832012-07-19 09:49:00 +00002390 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2391 RegArgs, CC, NumBytes, isVarArg))
Eric Christopherf9764fa2010-09-30 20:49:44 +00002392 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002393
Chad Rosier49d6fc02012-06-12 19:25:13 +00002394 bool UseReg = false;
Chad Rosier1c8fccb2012-05-23 18:38:57 +00002395 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosier49d6fc02012-06-12 19:25:13 +00002396 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier1c8fccb2012-05-23 18:38:57 +00002397
Chad Rosier49d6fc02012-06-12 19:25:13 +00002398 unsigned CalleeReg = 0;
2399 if (UseReg) {
2400 if (IntrMemName)
2401 CalleeReg = getLibcallReg(IntrMemName);
2402 else
2403 CalleeReg = getRegForValue(Callee);
2404
Chad Rosier1c8fccb2012-05-23 18:38:57 +00002405 if (CalleeReg == 0) return false;
2406 }
2407
Chad Rosier49d6fc02012-06-12 19:25:13 +00002408 // Issue the call.
2409 unsigned CallOpc = ARMSelectCallOp(UseReg);
2410 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2411 DL, TII.get(CallOpc));
Chad Rosier49d6fc02012-06-12 19:25:13 +00002412
Jakob Stoklund Olesen0745b642012-08-24 20:52:46 +00002413 // ARM calls don't take a predicate, but tBL / tBLX do.
2414 if(isThumb2)
Chad Rosier49d6fc02012-06-12 19:25:13 +00002415 AddDefaultPred(MIB);
Jakob Stoklund Olesen0745b642012-08-24 20:52:46 +00002416 if (UseReg)
2417 MIB.addReg(CalleeReg);
2418 else if (!IntrMemName)
2419 MIB.addGlobalAddress(GV, 0, 0);
2420 else
2421 MIB.addExternalSymbol(IntrMemName, 0);
Jush Luefc967e2012-06-14 06:08:19 +00002422
Eric Christopherf9764fa2010-09-30 20:49:44 +00002423 // Add implicit physical register uses to the call.
2424 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesen0745b642012-08-24 20:52:46 +00002425 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002426
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00002427 // Add a register mask with the call-preserved registers.
2428 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2429 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2430
Eric Christopherf9764fa2010-09-30 20:49:44 +00002431 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00002432 SmallVector<unsigned, 4> UsedRegs;
Jush Luee649832012-07-19 09:49:00 +00002433 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2434 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00002435
Eric Christopherf9764fa2010-09-30 20:49:44 +00002436 // Set all unused physreg defs as dead.
2437 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00002438
Eric Christopherf9764fa2010-09-30 20:49:44 +00002439 return true;
Eric Christopherf9764fa2010-09-30 20:49:44 +00002440}
2441
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002442bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002443 return Len <= 16;
2444}
2445
Jim Grosbachd4f020a2012-04-06 23:43:50 +00002446bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosierc9758b12012-12-06 01:34:31 +00002447 uint64_t Len, unsigned Alignment) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002448 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002449 if (!ARMIsMemCpySmall(Len))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002450 return false;
2451
Chad Rosier909cb4f2011-11-14 22:46:17 +00002452 while (Len) {
2453 MVT VT;
Chad Rosierc9758b12012-12-06 01:34:31 +00002454 if (!Alignment || Alignment >= 4) {
2455 if (Len >= 4)
2456 VT = MVT::i32;
2457 else if (Len >= 2)
2458 VT = MVT::i16;
2459 else {
2460 assert (Len == 1 && "Expected a length of 1!");
2461 VT = MVT::i8;
2462 }
2463 } else {
2464 // Bound based on alignment.
2465 if (Len >= 2 && Alignment == 2)
2466 VT = MVT::i16;
2467 else {
Chad Rosierc9758b12012-12-06 01:34:31 +00002468 VT = MVT::i8;
2469 }
Chad Rosier909cb4f2011-11-14 22:46:17 +00002470 }
2471
2472 bool RV;
2473 unsigned ResultReg;
2474 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherfae699a2012-01-11 20:55:27 +00002475 assert (RV == true && "Should be able to handle this load.");
Chad Rosier909cb4f2011-11-14 22:46:17 +00002476 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherfae699a2012-01-11 20:55:27 +00002477 assert (RV == true && "Should be able to handle this store.");
Duncan Sands5b8a1db2012-02-05 14:20:11 +00002478 (void)RV;
Chad Rosier909cb4f2011-11-14 22:46:17 +00002479
2480 unsigned Size = VT.getSizeInBits()/8;
2481 Len -= Size;
2482 Dest.Offset += Size;
2483 Src.Offset += Size;
2484 }
2485
2486 return true;
2487}
2488
Chad Rosier11add262011-11-11 23:31:03 +00002489bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2490 // FIXME: Handle more intrinsics.
2491 switch (I.getIntrinsicID()) {
2492 default: return false;
Chad Rosierada759d2012-05-30 17:23:22 +00002493 case Intrinsic::frameaddress: {
2494 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2495 MFI->setFrameAddressIsTaken(true);
2496
2497 unsigned LdrOpc;
2498 const TargetRegisterClass *RC;
2499 if (isThumb2) {
2500 LdrOpc = ARM::t2LDRi12;
2501 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2502 } else {
2503 LdrOpc = ARM::LDRi12;
2504 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2505 }
2506
2507 const ARMBaseRegisterInfo *RegInfo =
2508 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2509 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2510 unsigned SrcReg = FramePtr;
2511
2512 // Recursively load frame address
2513 // ldr r0 [fp]
2514 // ldr r0 [r0]
2515 // ldr r0 [r0]
2516 // ...
2517 unsigned DestReg;
2518 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2519 while (Depth--) {
2520 DestReg = createResultReg(RC);
2521 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2522 TII.get(LdrOpc), DestReg)
2523 .addReg(SrcReg).addImm(0));
2524 SrcReg = DestReg;
2525 }
Chad Rosierbbff4ee2012-06-01 21:12:31 +00002526 UpdateValueMap(&I, SrcReg);
Chad Rosierada759d2012-05-30 17:23:22 +00002527 return true;
2528 }
Chad Rosier11add262011-11-11 23:31:03 +00002529 case Intrinsic::memcpy:
2530 case Intrinsic::memmove: {
Chad Rosier11add262011-11-11 23:31:03 +00002531 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2532 // Don't handle volatile.
2533 if (MTI.isVolatile())
2534 return false;
Chad Rosier909cb4f2011-11-14 22:46:17 +00002535
2536 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2537 // we would emit dead code because we don't currently handle memmoves.
2538 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2539 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002540 // Small memcpy's are common enough that we want to do them without a call
2541 // if possible.
Chad Rosier909cb4f2011-11-14 22:46:17 +00002542 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier2c42b8c2011-11-14 23:04:09 +00002543 if (ARMIsMemCpySmall(Len)) {
Chad Rosier909cb4f2011-11-14 22:46:17 +00002544 Address Dest, Src;
2545 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2546 !ARMComputeAddress(MTI.getRawSource(), Src))
2547 return false;
Chad Rosierc9758b12012-12-06 01:34:31 +00002548 unsigned Alignment = MTI.getAlignment();
2549 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosier909cb4f2011-11-14 22:46:17 +00002550 return true;
2551 }
2552 }
Jush Luefc967e2012-06-14 06:08:19 +00002553
Chad Rosier11add262011-11-11 23:31:03 +00002554 if (!MTI.getLength()->getType()->isIntegerTy(32))
2555 return false;
Jush Luefc967e2012-06-14 06:08:19 +00002556
Chad Rosier11add262011-11-11 23:31:03 +00002557 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2558 return false;
2559
2560 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2561 return SelectCall(&I, IntrMemName);
2562 }
2563 case Intrinsic::memset: {
2564 const MemSetInst &MSI = cast<MemSetInst>(I);
2565 // Don't handle volatile.
2566 if (MSI.isVolatile())
2567 return false;
Jush Luefc967e2012-06-14 06:08:19 +00002568
Chad Rosier11add262011-11-11 23:31:03 +00002569 if (!MSI.getLength()->getType()->isIntegerTy(32))
2570 return false;
Jush Luefc967e2012-06-14 06:08:19 +00002571
Chad Rosier11add262011-11-11 23:31:03 +00002572 if (MSI.getDestAddressSpace() > 255)
2573 return false;
Jush Luefc967e2012-06-14 06:08:19 +00002574
Chad Rosier11add262011-11-11 23:31:03 +00002575 return SelectCall(&I, "memset");
2576 }
Chad Rosier226ddf52012-05-11 21:33:49 +00002577 case Intrinsic::trap: {
Eli Bendersky0f156af2013-01-30 16:30:19 +00002578 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(
2579 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosier226ddf52012-05-11 21:33:49 +00002580 return true;
2581 }
Chad Rosier11add262011-11-11 23:31:03 +00002582 }
Chad Rosier11add262011-11-11 23:31:03 +00002583}
2584
Chad Rosier0d7b2312011-11-02 00:18:48 +00002585bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luefc967e2012-06-14 06:08:19 +00002586 // The high bits for a type smaller than the register size are assumed to be
Chad Rosier0d7b2312011-11-02 00:18:48 +00002587 // undefined.
2588 Value *Op = I->getOperand(0);
2589
2590 EVT SrcVT, DestVT;
2591 SrcVT = TLI.getValueType(Op->getType(), true);
2592 DestVT = TLI.getValueType(I->getType(), true);
2593
2594 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2595 return false;
2596 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2597 return false;
2598
2599 unsigned SrcReg = getRegForValue(Op);
2600 if (!SrcReg) return false;
2601
2602 // Because the high bits are undefined, a truncate doesn't generate
2603 // any code.
2604 UpdateValueMap(I, SrcReg);
2605 return true;
2606}
2607
Chad Rosier316a5aa2012-12-17 19:59:43 +00002608unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier87633022011-11-02 17:20:24 +00002609 bool isZExt) {
Eli Friedman76927d732011-05-25 23:49:02 +00002610 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier87633022011-11-02 17:20:24 +00002611 return 0;
JF Bastien8fc760c2013-06-07 20:10:37 +00002612 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier87633022011-11-02 17:20:24 +00002613 return 0;
JF Bastien8fc760c2013-06-07 20:10:37 +00002614
2615 // Table of which combinations can be emitted as a single instruction,
2616 // and which will require two.
2617 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2618 // ARM Thumb
2619 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2620 // ext: s z s z s z s z
2621 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2622 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2623 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2624 };
2625
2626 // Target registers for:
2627 // - For ARM can never be PC.
2628 // - For 16-bit Thumb are restricted to lower 8 registers.
2629 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2630 static const TargetRegisterClass *RCTbl[2][2] = {
2631 // Instructions: Two Single
2632 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2633 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2634 };
2635
2636 // Table governing the instruction(s) to be emitted.
JF Bastiend055c592013-07-17 05:46:46 +00002637 static const struct InstructionTable {
2638 uint32_t Opc : 16;
2639 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2640 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2641 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2642 } IT[2][2][3][2] = {
JF Bastien8fc760c2013-06-07 20:10:37 +00002643 { // Two instructions (first is left shift, second is in this table).
JF Bastiend055c592013-07-17 05:46:46 +00002644 { // ARM Opc S Shift Imm
2645 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2646 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2647 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2648 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2649 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2650 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien8fc760c2013-06-07 20:10:37 +00002651 },
JF Bastiend055c592013-07-17 05:46:46 +00002652 { // Thumb Opc S Shift Imm
2653 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2654 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2655 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2656 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2657 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2658 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien8fc760c2013-06-07 20:10:37 +00002659 }
2660 },
2661 { // Single instruction.
JF Bastiend055c592013-07-17 05:46:46 +00002662 { // ARM Opc S Shift Imm
2663 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2664 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2665 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2666 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2667 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2668 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien8fc760c2013-06-07 20:10:37 +00002669 },
JF Bastiend055c592013-07-17 05:46:46 +00002670 { // Thumb Opc S Shift Imm
2671 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2672 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2673 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2674 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2675 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2676 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien8fc760c2013-06-07 20:10:37 +00002677 }
2678 }
2679 };
2680
2681 unsigned SrcBits = SrcVT.getSizeInBits();
2682 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien2c69e902013-06-08 00:51:51 +00002683 (void) DestBits;
JF Bastien8fc760c2013-06-07 20:10:37 +00002684 assert((SrcBits < DestBits) && "can only extend to larger types");
2685 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2686 "other sizes unimplemented");
2687 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2688 "other sizes unimplemented");
2689
2690 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiend055c592013-07-17 05:46:46 +00002691 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien8fc760c2013-06-07 20:10:37 +00002692 assert((Bitness < 3) && "sanity-check table bounds");
2693
2694 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2695 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiend055c592013-07-17 05:46:46 +00002696 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2697 unsigned Opc = ITP->Opc;
JF Bastien8fc760c2013-06-07 20:10:37 +00002698 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiend055c592013-07-17 05:46:46 +00002699 unsigned hasS = ITP->hasS;
2700 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2701 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2702 "only MOVsi has shift operand addressing mode");
2703 unsigned Imm = ITP->Imm;
JF Bastien8fc760c2013-06-07 20:10:37 +00002704
2705 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2706 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiend055c592013-07-17 05:46:46 +00002707 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien8fc760c2013-06-07 20:10:37 +00002708 unsigned ResultReg;
JF Bastiend055c592013-07-17 05:46:46 +00002709 // MOVsi encodes shift and immediate in shift operand addressing mode.
2710 // The following condition has the same value when emitting two
2711 // instruction sequences: both are shifts.
2712 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien8fc760c2013-06-07 20:10:37 +00002713
2714 // Either one or two instructions are emitted.
2715 // They're always of the form:
2716 // dst = in OP imm
2717 // CPSR is set only by 16-bit Thumb instructions.
2718 // Predicate, if any, is AL.
2719 // S bit, if available, is always 0.
2720 // When two are emitted the first's result will feed as the second's input,
2721 // that value is then dead.
2722 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2723 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2724 ResultReg = createResultReg(RC);
JF Bastiend055c592013-07-17 05:46:46 +00002725 bool isLsl = (0 == Instr) && !isSingleInstr;
2726 unsigned Opcode = isLsl ? LSLOpc : Opc;
2727 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2728 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien8fc760c2013-06-07 20:10:37 +00002729 bool isKill = 1 == Instr;
2730 MachineInstrBuilder MIB = BuildMI(
2731 *FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opcode), ResultReg);
2732 if (setsCPSR)
2733 MIB.addReg(ARM::CPSR, RegState::Define);
JF Bastiend055c592013-07-17 05:46:46 +00002734 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien8fc760c2013-06-07 20:10:37 +00002735 if (hasS)
2736 AddDefaultCC(MIB);
2737 // Second instruction consumes the first's result.
2738 SrcReg = ResultReg;
Eli Friedman76927d732011-05-25 23:49:02 +00002739 }
2740
Chad Rosier87633022011-11-02 17:20:24 +00002741 return ResultReg;
2742}
2743
2744bool ARMFastISel::SelectIntExt(const Instruction *I) {
2745 // On ARM, in general, integer casts don't involve legal types; this code
2746 // handles promotable integers.
Chad Rosier87633022011-11-02 17:20:24 +00002747 Type *DestTy = I->getType();
2748 Value *Src = I->getOperand(0);
2749 Type *SrcTy = Src->getType();
2750
Chad Rosier87633022011-11-02 17:20:24 +00002751 bool isZExt = isa<ZExtInst>(I);
2752 unsigned SrcReg = getRegForValue(Src);
2753 if (!SrcReg) return false;
2754
Chad Rosier316a5aa2012-12-17 19:59:43 +00002755 EVT SrcEVT, DestEVT;
2756 SrcEVT = TLI.getValueType(SrcTy, true);
2757 DestEVT = TLI.getValueType(DestTy, true);
2758 if (!SrcEVT.isSimple()) return false;
2759 if (!DestEVT.isSimple()) return false;
Patrik Hagglund3d170e62012-12-17 14:30:06 +00002760
Chad Rosier316a5aa2012-12-17 19:59:43 +00002761 MVT SrcVT = SrcEVT.getSimpleVT();
2762 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier87633022011-11-02 17:20:24 +00002763 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2764 if (ResultReg == 0) return false;
2765 UpdateValueMap(I, ResultReg);
Eli Friedman76927d732011-05-25 23:49:02 +00002766 return true;
2767}
2768
Jush Lu29465492012-08-03 02:37:48 +00002769bool ARMFastISel::SelectShift(const Instruction *I,
2770 ARM_AM::ShiftOpc ShiftTy) {
2771 // We handle thumb2 mode by target independent selector
2772 // or SelectionDAG ISel.
2773 if (isThumb2)
2774 return false;
2775
2776 // Only handle i32 now.
2777 EVT DestVT = TLI.getValueType(I->getType(), true);
2778 if (DestVT != MVT::i32)
2779 return false;
2780
2781 unsigned Opc = ARM::MOVsr;
2782 unsigned ShiftImm;
2783 Value *Src2Value = I->getOperand(1);
2784 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2785 ShiftImm = CI->getZExtValue();
2786
2787 // Fall back to selection DAG isel if the shift amount
2788 // is zero or greater than the width of the value type.
2789 if (ShiftImm == 0 || ShiftImm >=32)
2790 return false;
2791
2792 Opc = ARM::MOVsi;
2793 }
2794
2795 Value *Src1Value = I->getOperand(0);
2796 unsigned Reg1 = getRegForValue(Src1Value);
2797 if (Reg1 == 0) return false;
2798
Nadav Roteme7576402012-09-06 11:13:55 +00002799 unsigned Reg2 = 0;
Jush Lu29465492012-08-03 02:37:48 +00002800 if (Opc == ARM::MOVsr) {
2801 Reg2 = getRegForValue(Src2Value);
2802 if (Reg2 == 0) return false;
2803 }
2804
JF Bastiena9a8a122013-05-29 15:45:47 +00002805 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu29465492012-08-03 02:37:48 +00002806 if(ResultReg == 0) return false;
2807
2808 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2809 TII.get(Opc), ResultReg)
2810 .addReg(Reg1);
2811
2812 if (Opc == ARM::MOVsi)
2813 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2814 else if (Opc == ARM::MOVsr) {
2815 MIB.addReg(Reg2);
2816 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2817 }
2818
2819 AddOptionalDefs(MIB);
2820 UpdateValueMap(I, ResultReg);
2821 return true;
2822}
2823
Eric Christopher56d2b722010-09-02 23:43:26 +00002824// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00002825bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00002826
Eric Christopherab695882010-07-21 22:26:11 +00002827 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00002828 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00002829 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00002830 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00002831 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00002832 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00002833 return SelectBranch(I);
Chad Rosier60c8fa62012-02-07 23:56:08 +00002834 case Instruction::IndirectBr:
2835 return SelectIndirectBr(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00002836 case Instruction::ICmp:
2837 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00002838 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00002839 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00002840 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00002841 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00002842 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00002843 case Instruction::SIToFP:
Chad Rosierae46a332012-02-03 21:14:11 +00002844 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosier36b7beb2012-02-03 19:42:52 +00002845 case Instruction::UIToFP:
Chad Rosierae46a332012-02-03 21:14:11 +00002846 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher9a040492010-09-09 18:54:59 +00002847 case Instruction::FPToSI:
Chad Rosierae46a332012-02-03 21:14:11 +00002848 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosieree8901c2012-02-03 20:27:51 +00002849 case Instruction::FPToUI:
Chad Rosierae46a332012-02-03 21:14:11 +00002850 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier3901c3e2012-02-06 23:50:07 +00002851 case Instruction::Add:
2852 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosier6fde8752012-02-08 02:29:21 +00002853 case Instruction::Or:
2854 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier743e1992012-02-08 02:45:44 +00002855 case Instruction::Sub:
2856 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002857 case Instruction::FAdd:
Chad Rosier3901c3e2012-02-06 23:50:07 +00002858 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00002859 case Instruction::FSub:
Chad Rosier3901c3e2012-02-06 23:50:07 +00002860 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00002861 case Instruction::FMul:
Chad Rosier3901c3e2012-02-06 23:50:07 +00002862 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00002863 case Instruction::SDiv:
Chad Rosier7ccb30b2012-02-03 21:07:27 +00002864 return SelectDiv(I, /*isSigned*/ true);
2865 case Instruction::UDiv:
2866 return SelectDiv(I, /*isSigned*/ false);
Eric Christopher6a880d62010-10-11 08:37:26 +00002867 case Instruction::SRem:
Chad Rosier769422f2012-02-03 21:23:45 +00002868 return SelectRem(I, /*isSigned*/ true);
2869 case Instruction::URem:
2870 return SelectRem(I, /*isSigned*/ false);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002871 case Instruction::Call:
Chad Rosier11add262011-11-11 23:31:03 +00002872 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2873 return SelectIntrinsicCall(*II);
Eric Christopherf9764fa2010-09-30 20:49:44 +00002874 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00002875 case Instruction::Select:
2876 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00002877 case Instruction::Ret:
2878 return SelectRet(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002879 case Instruction::Trunc:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002880 return SelectTrunc(I);
Eli Friedman76927d732011-05-25 23:49:02 +00002881 case Instruction::ZExt:
2882 case Instruction::SExt:
Chad Rosier0d7b2312011-11-02 00:18:48 +00002883 return SelectIntExt(I);
Jush Lu29465492012-08-03 02:37:48 +00002884 case Instruction::Shl:
2885 return SelectShift(I, ARM_AM::lsl);
2886 case Instruction::LShr:
2887 return SelectShift(I, ARM_AM::lsr);
2888 case Instruction::AShr:
2889 return SelectShift(I, ARM_AM::asr);
Eric Christopherab695882010-07-21 22:26:11 +00002890 default: break;
2891 }
2892 return false;
2893}
2894
JF Bastien5ab77042013-06-11 22:13:46 +00002895namespace {
2896// This table describes sign- and zero-extend instructions which can be
2897// folded into a preceding load. All of these extends have an immediate
2898// (sometimes a mask and sometimes a shift) that's applied after
2899// extension.
2900const struct FoldableLoadExtendsStruct {
2901 uint16_t Opc[2]; // ARM, Thumb.
2902 uint8_t ExpectedImm;
2903 uint8_t isZExt : 1;
2904 uint8_t ExpectedVT : 7;
2905} FoldableLoadExtends[] = {
2906 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2907 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2908 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2909 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2910 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2911};
2912}
2913
Eli Bendersky75299e32013-04-19 22:29:18 +00002914/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierb29b9502011-11-13 02:23:59 +00002915/// vreg is being provided by the specified load instruction. If possible,
2916/// try to fold the load as an operand to the instruction, returning true if
2917/// successful.
Eli Bendersky75299e32013-04-19 22:29:18 +00002918bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2919 const LoadInst *LI) {
Chad Rosierb29b9502011-11-13 02:23:59 +00002920 // Verify we have a legal type before going any further.
2921 MVT VT;
2922 if (!isLoadTypeLegal(LI->getType(), VT))
2923 return false;
2924
2925 // Combine load followed by zero- or sign-extend.
2926 // ldrb r1, [r0] ldrb r1, [r0]
2927 // uxtb r2, r1 =>
2928 // mov r3, r2 mov r3, r1
JF Bastien5ab77042013-06-11 22:13:46 +00002929 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2930 return false;
2931 const uint64_t Imm = MI->getOperand(2).getImm();
2932
2933 bool Found = false;
2934 bool isZExt;
2935 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
2936 i != e; ++i) {
2937 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
2938 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
2939 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
2940 Found = true;
2941 isZExt = FoldableLoadExtends[i].isZExt;
2942 }
Chad Rosierb29b9502011-11-13 02:23:59 +00002943 }
JF Bastien5ab77042013-06-11 22:13:46 +00002944 if (!Found) return false;
2945
Chad Rosierb29b9502011-11-13 02:23:59 +00002946 // See if we can handle this address.
2947 Address Addr;
2948 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luefc967e2012-06-14 06:08:19 +00002949
Chad Rosierb29b9502011-11-13 02:23:59 +00002950 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier8a9bce92011-12-13 19:22:14 +00002951 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierb29b9502011-11-13 02:23:59 +00002952 return false;
2953 MI->eraseFromParent();
2954 return true;
2955}
2956
Jush Lu8f506472012-09-27 05:21:41 +00002957unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00002958 unsigned Align, MVT VT) {
Jush Lu8f506472012-09-27 05:21:41 +00002959 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
2960 ARMConstantPoolConstant *CPV =
2961 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
2962 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
2963
2964 unsigned Opc;
2965 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
2966 // Load value.
2967 if (isThumb2) {
2968 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2969 TII.get(ARM::t2LDRpci), DestReg1)
2970 .addConstantPoolIndex(Idx));
2971 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
2972 } else {
2973 // The extra immediate is for addrmode2.
2974 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2975 DL, TII.get(ARM::LDRcp), DestReg1)
2976 .addConstantPoolIndex(Idx).addImm(0));
2977 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
2978 }
2979
2980 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
2981 if (GlobalBaseReg == 0) {
2982 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
2983 AFI->setGlobalBaseReg(GlobalBaseReg);
2984 }
2985
2986 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
2987 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2988 DL, TII.get(Opc), DestReg2)
2989 .addReg(DestReg1)
2990 .addReg(GlobalBaseReg);
2991 if (!UseGOTOFF)
2992 MIB.addImm(0);
2993 AddOptionalDefs(MIB);
2994
2995 return DestReg2;
2996}
2997
Evan Cheng092e5e72013-02-11 01:27:15 +00002998bool ARMFastISel::FastLowerArguments() {
2999 if (!FuncInfo.CanLowerReturn)
3000 return false;
3001
3002 const Function *F = FuncInfo.Fn;
3003 if (F->isVarArg())
3004 return false;
3005
3006 CallingConv::ID CC = F->getCallingConv();
3007 switch (CC) {
3008 default:
3009 return false;
3010 case CallingConv::Fast:
3011 case CallingConv::C:
3012 case CallingConv::ARM_AAPCS_VFP:
3013 case CallingConv::ARM_AAPCS:
3014 case CallingConv::ARM_APCS:
3015 break;
3016 }
3017
3018 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3019 // which are passed in r0 - r3.
3020 unsigned Idx = 1;
3021 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3022 I != E; ++I, ++Idx) {
3023 if (Idx > 4)
3024 return false;
3025
3026 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3027 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3028 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3029 return false;
3030
3031 Type *ArgTy = I->getType();
3032 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3033 return false;
3034
3035 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00003036 if (!ArgVT.isSimple()) return false;
Evan Cheng092e5e72013-02-11 01:27:15 +00003037 switch (ArgVT.getSimpleVT().SimpleTy) {
3038 case MVT::i8:
3039 case MVT::i16:
3040 case MVT::i32:
3041 break;
3042 default:
3043 return false;
3044 }
3045 }
3046
3047
3048 static const uint16_t GPRArgRegs[] = {
3049 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3050 };
3051
3052 const TargetRegisterClass *RC = TLI.getRegClassFor(MVT::i32);
3053 Idx = 0;
3054 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3055 I != E; ++I, ++Idx) {
Evan Cheng092e5e72013-02-11 01:27:15 +00003056 unsigned SrcReg = GPRArgRegs[Idx];
3057 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3058 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3059 // Without this, EmitLiveInCopies may eliminate the livein if its only
3060 // use is a bitcast (which isn't turned into an instruction).
3061 unsigned ResultReg = createResultReg(RC);
3062 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
3063 ResultReg).addReg(DstReg, getKillRegState(true));
3064 UpdateValueMap(I, ResultReg);
3065 }
3066
3067 return true;
3068}
3069
Eric Christopherab695882010-07-21 22:26:11 +00003070namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00003071 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3072 const TargetLibraryInfo *libInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00003073 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00003074
Eric Christopherfeadddd2010-10-11 20:05:22 +00003075 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastienfe532ad2013-06-14 02:49:43 +00003076 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3077 bool UseFastISel = false;
3078 UseFastISel |= Subtarget->isTargetIOS() && !Subtarget->isThumb1Only();
3079 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3080 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3081
3082 if (UseFastISel) {
3083 // iOS always has a FP for backtracking, force other targets
3084 // to keep their FP when doing FastISel. The emitted code is
3085 // currently superior, and in cases like test-suite's lencod
3086 // FastISel isn't quite correct when FP is eliminated.
3087 TM.Options.NoFramePointerElim = true;
Bob Wilsond49edb72012-08-03 04:06:28 +00003088 return new ARMFastISel(funcInfo, libInfo);
JF Bastienfe532ad2013-06-14 02:49:43 +00003089 }
Evan Cheng09447952010-07-26 18:32:55 +00003090 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00003091 }
3092}