blob: 68fcee7d240795cc9b7ce18efdaf48d737e74eab [file] [log] [blame]
Eric Christopher84bdfd82010-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 Christopher0d274a02010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopher72497e52010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopher83a5ec82010-10-01 23:24:42 +000019#include "ARMConstantPoolValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Evan Chenga20cde32011-07-20 23:34:39 +000022#include "MCTargetDesc/ARMAddressingModes.h"
JF Bastien3c6bb8e2013-06-11 22:13:46 +000023#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-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 Carruth9fb823b2013-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 Christopher84bdfd82010-07-21 22:26:11 +000041#include "llvm/Support/CallSite.h"
Eric Christopher663f4992010-08-17 00:46:57 +000042#include "llvm/Support/CommandLine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher09f757d2010-08-17 01:25:29 +000045#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000048#include "llvm/Target/TargetOptions.h"
49using namespace llvm;
50
Eric Christopher347f4c32010-12-15 23:47:29 +000051extern cl::opt<bool> EnableARMLongCalls;
52
Eric Christopher84bdfd82010-07-21 22:26:11 +000053namespace {
Eric Christopher0a3c28b2010-11-20 22:38:27 +000054
Eric Christopherfef5f312010-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 Christopher0a3c28b2010-11-20 22:38:27 +000061
Eric Christopherfef5f312010-11-19 22:30:02 +000062 union {
63 unsigned Reg;
64 int FI;
65 } Base;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000066
Eric Christopherfef5f312010-11-19 22:30:02 +000067 int Offset;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000068
Eric Christopherfef5f312010-11-19 22:30:02 +000069 // Innocuous defaults for our address.
70 Address()
Jim Grosbach4e983162011-05-16 22:24:07 +000071 : BaseType(RegBase), Offset(0) {
Eric Christopherfef5f312010-11-19 22:30:02 +000072 Base.Reg = 0;
73 }
74 } Address;
Eric Christopher84bdfd82010-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;
Bill Wendling6c1d9592013-12-30 05:17:29 +000081 Module &M;
Eric Christopher09f757d2010-08-17 01:25:29 +000082 const TargetMachine &TM;
83 const TargetInstrInfo &TII;
84 const TargetLowering &TLI;
Eric Christopher83a5ec82010-10-01 23:24:42 +000085 ARMFunctionInfo *AFI;
Eric Christopher84bdfd82010-07-21 22:26:11 +000086
Eric Christopherb024be32010-09-29 22:24:45 +000087 // Convenience variables to avoid some queries.
Chad Rosier0439cfc2011-11-08 21:12:00 +000088 bool isThumb2;
Eric Christopherb024be32010-09-29 22:24:45 +000089 LLVMContext *Context;
Eric Christopher6a0333c2010-09-02 01:39:14 +000090
Eric Christopher84bdfd82010-07-21 22:26:11 +000091 public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000092 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
93 const TargetLibraryInfo *libInfo)
94 : FastISel(funcInfo, libInfo),
Bill Wendling76cce192013-12-29 08:00:04 +000095 M(const_cast<Module&>(*funcInfo.Fn->getParent())),
Eric Christopher09f757d2010-08-17 01:25:29 +000096 TM(funcInfo.MF->getTarget()),
97 TII(*TM.getInstrInfo()),
98 TLI(*TM.getTargetLowering()) {
Eric Christopher84bdfd82010-07-21 22:26:11 +000099 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8d03b8a2010-08-23 22:32:45 +0000100 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier0439cfc2011-11-08 21:12:00 +0000101 isThumb2 = AFI->isThumbFunction();
Eric Christopherb024be32010-09-29 22:24:45 +0000102 Context = &funcInfo.Fn->getContext();
Eric Christopher84bdfd82010-07-21 22:26:11 +0000103 }
104
Eric Christopherd8e8a292010-08-20 00:20:31 +0000105 // Code from FastISel.cpp.
Craig Topperfd1c9252012-08-18 21:38:45 +0000106 private:
107 unsigned FastEmitInst_(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC);
109 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill);
112 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill,
115 unsigned Op1, bool Op1IsKill);
116 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill,
119 unsigned Op1, bool Op1IsKill,
120 unsigned Op2, bool Op2IsKill);
121 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
122 const TargetRegisterClass *RC,
123 unsigned Op0, bool Op0IsKill,
124 uint64_t Imm);
125 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
126 const TargetRegisterClass *RC,
127 unsigned Op0, bool Op0IsKill,
128 const ConstantFP *FPImm);
129 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
131 unsigned Op0, bool Op0IsKill,
132 unsigned Op1, bool Op1IsKill,
133 uint64_t Imm);
134 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
135 const TargetRegisterClass *RC,
136 uint64_t Imm);
137 unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
138 const TargetRegisterClass *RC,
139 uint64_t Imm1, uint64_t Imm2);
Eric Christopher174d8722011-03-12 01:09:29 +0000140
Craig Topperfd1c9252012-08-18 21:38:45 +0000141 unsigned FastEmitInst_extractsubreg(MVT RetVT,
142 unsigned Op0, bool Op0IsKill,
143 uint32_t Idx);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000144
Eric Christopherd8e8a292010-08-20 00:20:31 +0000145 // Backend specific FastISel code.
Craig Topperfd1c9252012-08-18 21:38:45 +0000146 private:
Eric Christopher84bdfd82010-07-21 22:26:11 +0000147 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher92db2012010-09-02 01:48:11 +0000148 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000149 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eli Bendersky90dd3e72013-04-19 22:29:18 +0000150 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
151 const LoadInst *LI);
Evan Cheng615620c2013-02-11 01:27:15 +0000152 virtual bool FastLowerArguments();
Craig Topperfd1c9252012-08-18 21:38:45 +0000153 private:
Eric Christopher84bdfd82010-07-21 22:26:11 +0000154 #include "ARMGenFastISel.inc"
Eric Christopher2ff757d2010-09-09 01:06:51 +0000155
Eric Christopher00202ee2010-08-23 21:44:12 +0000156 // Instruction selection routines.
Eric Christophercc766a22010-09-10 23:10:30 +0000157 private:
Eric Christopher2f8637d2010-10-21 21:47:51 +0000158 bool SelectLoad(const Instruction *I);
159 bool SelectStore(const Instruction *I);
160 bool SelectBranch(const Instruction *I);
Chad Rosierded4c992012-02-07 23:56:08 +0000161 bool SelectIndirectBr(const Instruction *I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000162 bool SelectCmp(const Instruction *I);
163 bool SelectFPExt(const Instruction *I);
164 bool SelectFPTrunc(const Instruction *I);
Chad Rosier685b20c2012-02-06 23:50:07 +0000165 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
166 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosiere023d5d2012-02-03 21:14:11 +0000167 bool SelectIToFP(const Instruction *I, bool isSigned);
168 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosieraaa55a82012-02-03 21:07:27 +0000169 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosierb84a4b42012-02-03 21:23:45 +0000170 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosiera7ebc562011-11-11 23:31:03 +0000171 bool SelectCall(const Instruction *I, const char *IntrMemName);
172 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000173 bool SelectSelect(const Instruction *I);
Eric Christopher93bbe652010-10-22 01:28:00 +0000174 bool SelectRet(const Instruction *I);
Chad Rosieree7e4522011-11-02 00:18:48 +0000175 bool SelectTrunc(const Instruction *I);
176 bool SelectIntExt(const Instruction *I);
Jush Lu4705da92012-08-03 02:37:48 +0000177 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopher84bdfd82010-07-21 22:26:11 +0000178
Eric Christopher00202ee2010-08-23 21:44:12 +0000179 // Utility routines.
Eric Christopher0d274a02010-08-19 00:37:05 +0000180 private:
Jim Grosbach06c2a682013-08-16 23:37:31 +0000181 unsigned constrainOperandRegClass(const MCInstrDesc &II, unsigned OpNum,
182 unsigned Op);
Chris Lattner229907c2011-07-18 04:54:35 +0000183 bool isTypeLegal(Type *Ty, MVT &VT);
184 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosier9cf803c2011-11-02 18:08:25 +0000185 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
186 bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000187 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosiera26979b2011-12-14 17:26:05 +0000188 unsigned Alignment = 0, bool isZExt = true,
189 bool allocReg = true);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000190 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +0000191 unsigned Alignment = 0);
Eric Christopherfef5f312010-11-19 22:30:02 +0000192 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier150d35b2012-12-17 22:35:29 +0000193 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier057b6d32011-11-14 23:04:09 +0000194 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosier9f5c68a2012-12-06 01:34:31 +0000195 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
196 unsigned Alignment);
Chad Rosier62a144f2012-12-17 19:59:43 +0000197 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000198 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
199 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
200 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
201 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
202 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000203 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000204 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000205
Eric Christopher72497e52010-09-10 23:18:12 +0000206 // Call handling routines.
207 private:
Jush Lue67e07b2012-07-19 09:49:00 +0000208 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
209 bool Return,
210 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000211 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000212 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000213 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000214 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
215 SmallVectorImpl<unsigned> &RegArgs,
216 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000217 unsigned &NumBytes,
218 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000219 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000220 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000221 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000222 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000223 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000224
225 // OptionalDef handling routines.
226 private:
Eric Christopher174d8722011-03-12 01:09:29 +0000227 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000228 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
229 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000230 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000231 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000232 unsigned Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000233};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000234
235} // end anonymous namespace
236
Eric Christopher72497e52010-09-10 23:18:12 +0000237#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000238
Eric Christopher0d274a02010-08-19 00:37:05 +0000239// DefinesOptionalPredicate - This is different from DefinesPredicate in that
240// we don't care about implicit defs here, just places we'll need to add a
241// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
242bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000243 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000244 return false;
245
246 // Look to see if our OptionalDef is defining CPSR or CCR.
247 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
248 const MachineOperand &MO = MI->getOperand(i);
Eric Christopher985d9e42010-08-20 00:36:24 +0000249 if (!MO.isReg() || !MO.isDef()) continue;
250 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000251 *CPSR = true;
252 }
253 return true;
254}
255
Eric Christopher174d8722011-03-12 01:09:29 +0000256bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000257 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000258
Joey Goulya5153cb2013-09-09 14:21:49 +0000259 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000260 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000261 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000262 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000263
Evan Cheng6cc775f2011-06-28 19:10:37 +0000264 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
265 if (MCID.OpInfo[i].isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000266 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000267
Eric Christopher174d8722011-03-12 01:09:29 +0000268 return false;
269}
270
Eric Christopher0d274a02010-08-19 00:37:05 +0000271// If the machine is predicable go ahead and add the predicate operands, if
272// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000273// TODO: If we want to support thumb1 then we'll need to deal with optional
274// CPSR defs that need to be added before the remaining operands. See s_cc_out
275// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000276const MachineInstrBuilder &
277ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
278 MachineInstr *MI = &*MIB;
279
Eric Christopher174d8722011-03-12 01:09:29 +0000280 // Do we use a predicate? or...
281 // Are we NEON in ARM mode and have a predicate operand? If so, I know
282 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000283 if (isARMNEONPred(MI))
Eric Christopher0d274a02010-08-19 00:37:05 +0000284 AddDefaultPred(MIB);
Eric Christopher501d2e22011-04-29 00:03:10 +0000285
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000286 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000287 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000288 bool CPSR = false;
Eric Christopher0d274a02010-08-19 00:37:05 +0000289 if (DefinesOptionalPredicate(MI, &CPSR)) {
290 if (CPSR)
291 AddDefaultT1CC(MIB);
292 else
293 AddDefaultCC(MIB);
294 }
295 return MIB;
296}
297
Jim Grosbach06c2a682013-08-16 23:37:31 +0000298unsigned ARMFastISel::constrainOperandRegClass(const MCInstrDesc &II,
299 unsigned Op, unsigned OpNum) {
300 if (TargetRegisterInfo::isVirtualRegister(Op)) {
301 const TargetRegisterClass *RegClass =
302 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
303 if (!MRI.constrainRegClass(Op, RegClass)) {
304 // If it's not legal to COPY between the register classes, something
305 // has gone very wrong before we got here.
306 unsigned NewOp = createResultReg(RegClass);
307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
308 TII.get(TargetOpcode::COPY), NewOp).addReg(Op));
309 return NewOp;
310 }
311 }
312 return Op;
313}
314
Eric Christopher09f757d2010-08-17 01:25:29 +0000315unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
316 const TargetRegisterClass* RC) {
317 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000318 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000319
Eric Christopher0d274a02010-08-19 00:37:05 +0000320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher09f757d2010-08-17 01:25:29 +0000321 return ResultReg;
322}
323
324unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
325 const TargetRegisterClass *RC,
326 unsigned Op0, bool Op0IsKill) {
327 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000328 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000329
Jim Grosbach06c2a682013-08-16 23:37:31 +0000330 // Make sure the input operand is sufficiently constrained to be legal
331 // for this instruction.
332 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000333 if (II.getNumDefs() >= 1) {
Eric Christopher0d274a02010-08-19 00:37:05 +0000334 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher09f757d2010-08-17 01:25:29 +0000335 .addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000336 } else {
Eric Christopher0d274a02010-08-19 00:37:05 +0000337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000338 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher0d274a02010-08-19 00:37:05 +0000339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher09f757d2010-08-17 01:25:29 +0000340 TII.get(TargetOpcode::COPY), ResultReg)
341 .addReg(II.ImplicitDefs[0]));
342 }
343 return ResultReg;
344}
345
346unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
347 const TargetRegisterClass *RC,
348 unsigned Op0, bool Op0IsKill,
349 unsigned Op1, bool Op1IsKill) {
350 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000351 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000352
Jim Grosbach06c2a682013-08-16 23:37:31 +0000353 // Make sure the input operands are sufficiently constrained to be legal
354 // for this instruction.
355 Op0 = constrainOperandRegClass(II, Op0, 1);
356 Op1 = constrainOperandRegClass(II, Op1, 2);
357
Chad Rosier0bc51322012-02-15 17:36:21 +0000358 if (II.getNumDefs() >= 1) {
Eric Christopher0d274a02010-08-19 00:37:05 +0000359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher09f757d2010-08-17 01:25:29 +0000360 .addReg(Op0, Op0IsKill * RegState::Kill)
361 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000362 } else {
Eric Christopher0d274a02010-08-19 00:37:05 +0000363 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000364 .addReg(Op0, Op0IsKill * RegState::Kill)
365 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher0d274a02010-08-19 00:37:05 +0000366 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher09f757d2010-08-17 01:25:29 +0000367 TII.get(TargetOpcode::COPY), ResultReg)
368 .addReg(II.ImplicitDefs[0]));
369 }
370 return ResultReg;
371}
372
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000373unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
374 const TargetRegisterClass *RC,
375 unsigned Op0, bool Op0IsKill,
376 unsigned Op1, bool Op1IsKill,
377 unsigned Op2, bool Op2IsKill) {
378 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000379 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000380
Jim Grosbach06c2a682013-08-16 23:37:31 +0000381 // Make sure the input operands are sufficiently constrained to be legal
382 // for this instruction.
383 Op0 = constrainOperandRegClass(II, Op0, 1);
384 Op1 = constrainOperandRegClass(II, Op1, 2);
385 Op2 = constrainOperandRegClass(II, Op1, 3);
386
Chad Rosier0bc51322012-02-15 17:36:21 +0000387 if (II.getNumDefs() >= 1) {
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
389 .addReg(Op0, Op0IsKill * RegState::Kill)
390 .addReg(Op1, Op1IsKill * RegState::Kill)
391 .addReg(Op2, Op2IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000392 } else {
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000393 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
394 .addReg(Op0, Op0IsKill * RegState::Kill)
395 .addReg(Op1, Op1IsKill * RegState::Kill)
396 .addReg(Op2, Op2IsKill * RegState::Kill));
397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
398 TII.get(TargetOpcode::COPY), ResultReg)
399 .addReg(II.ImplicitDefs[0]));
400 }
401 return ResultReg;
402}
403
Eric Christopher09f757d2010-08-17 01:25:29 +0000404unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
405 const TargetRegisterClass *RC,
406 unsigned Op0, bool Op0IsKill,
407 uint64_t Imm) {
408 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000409 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000410
Jim Grosbach06c2a682013-08-16 23:37:31 +0000411 // Make sure the input operand is sufficiently constrained to be legal
412 // for this instruction.
413 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000414 if (II.getNumDefs() >= 1) {
Eric Christopher0d274a02010-08-19 00:37:05 +0000415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher09f757d2010-08-17 01:25:29 +0000416 .addReg(Op0, Op0IsKill * RegState::Kill)
417 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000418 } else {
Eric Christopher0d274a02010-08-19 00:37:05 +0000419 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000420 .addReg(Op0, Op0IsKill * RegState::Kill)
421 .addImm(Imm));
Eric Christopher0d274a02010-08-19 00:37:05 +0000422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher09f757d2010-08-17 01:25:29 +0000423 TII.get(TargetOpcode::COPY), ResultReg)
424 .addReg(II.ImplicitDefs[0]));
425 }
426 return ResultReg;
427}
428
429unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
430 const TargetRegisterClass *RC,
431 unsigned Op0, bool Op0IsKill,
432 const ConstantFP *FPImm) {
433 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000434 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000435
Jim Grosbach06c2a682013-08-16 23:37:31 +0000436 // Make sure the input operand is sufficiently constrained to be legal
437 // for this instruction.
438 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000439 if (II.getNumDefs() >= 1) {
Eric Christopher0d274a02010-08-19 00:37:05 +0000440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher09f757d2010-08-17 01:25:29 +0000441 .addReg(Op0, Op0IsKill * RegState::Kill)
442 .addFPImm(FPImm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000443 } else {
Eric Christopher0d274a02010-08-19 00:37:05 +0000444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000445 .addReg(Op0, Op0IsKill * RegState::Kill)
446 .addFPImm(FPImm));
Eric Christopher0d274a02010-08-19 00:37:05 +0000447 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher09f757d2010-08-17 01:25:29 +0000448 TII.get(TargetOpcode::COPY), ResultReg)
449 .addReg(II.ImplicitDefs[0]));
450 }
451 return ResultReg;
452}
453
454unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
455 const TargetRegisterClass *RC,
456 unsigned Op0, bool Op0IsKill,
457 unsigned Op1, bool Op1IsKill,
458 uint64_t Imm) {
459 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000460 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000461
Jim Grosbach06c2a682013-08-16 23:37:31 +0000462 // Make sure the input operands are sufficiently constrained to be legal
463 // for this instruction.
464 Op0 = constrainOperandRegClass(II, Op0, 1);
465 Op1 = constrainOperandRegClass(II, Op1, 2);
Chad Rosier0bc51322012-02-15 17:36:21 +0000466 if (II.getNumDefs() >= 1) {
Eric Christopher0d274a02010-08-19 00:37:05 +0000467 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher09f757d2010-08-17 01:25:29 +0000468 .addReg(Op0, Op0IsKill * RegState::Kill)
469 .addReg(Op1, Op1IsKill * RegState::Kill)
470 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000471 } else {
Eric Christopher0d274a02010-08-19 00:37:05 +0000472 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000473 .addReg(Op0, Op0IsKill * RegState::Kill)
474 .addReg(Op1, Op1IsKill * RegState::Kill)
475 .addImm(Imm));
Eric Christopher0d274a02010-08-19 00:37:05 +0000476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher09f757d2010-08-17 01:25:29 +0000477 TII.get(TargetOpcode::COPY), ResultReg)
478 .addReg(II.ImplicitDefs[0]));
479 }
480 return ResultReg;
481}
482
483unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
484 const TargetRegisterClass *RC,
485 uint64_t Imm) {
486 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000487 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000488
Chad Rosier0bc51322012-02-15 17:36:21 +0000489 if (II.getNumDefs() >= 1) {
Eric Christopher0d274a02010-08-19 00:37:05 +0000490 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher09f757d2010-08-17 01:25:29 +0000491 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000492 } else {
Eric Christopher0d274a02010-08-19 00:37:05 +0000493 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000494 .addImm(Imm));
Eric Christopher0d274a02010-08-19 00:37:05 +0000495 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher09f757d2010-08-17 01:25:29 +0000496 TII.get(TargetOpcode::COPY), ResultReg)
497 .addReg(II.ImplicitDefs[0]));
498 }
499 return ResultReg;
500}
501
Eric Christopher77087462011-04-29 22:07:50 +0000502unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
503 const TargetRegisterClass *RC,
504 uint64_t Imm1, uint64_t Imm2) {
505 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000506 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0713a9d2011-06-08 23:55:35 +0000507
Chad Rosier0bc51322012-02-15 17:36:21 +0000508 if (II.getNumDefs() >= 1) {
Eric Christopher77087462011-04-29 22:07:50 +0000509 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
510 .addImm(Imm1).addImm(Imm2));
Chad Rosier0bc51322012-02-15 17:36:21 +0000511 } else {
Eric Christopher77087462011-04-29 22:07:50 +0000512 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
513 .addImm(Imm1).addImm(Imm2));
Eric Christopher0713a9d2011-06-08 23:55:35 +0000514 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher77087462011-04-29 22:07:50 +0000515 TII.get(TargetOpcode::COPY),
516 ResultReg)
517 .addReg(II.ImplicitDefs[0]));
518 }
519 return ResultReg;
520}
521
Eric Christopher09f757d2010-08-17 01:25:29 +0000522unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
523 unsigned Op0, bool Op0IsKill,
524 uint32_t Idx) {
525 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
526 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
527 "Cannot yet extract from physregs");
Chad Rosier0bc51322012-02-15 17:36:21 +0000528
Eric Christopher0d274a02010-08-19 00:37:05 +0000529 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Chad Rosier0bc51322012-02-15 17:36:21 +0000530 DL, TII.get(TargetOpcode::COPY), ResultReg)
531 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
Eric Christopher09f757d2010-08-17 01:25:29 +0000532 return ResultReg;
533}
534
Eric Christopher860fc932010-09-10 00:34:35 +0000535// TODO: Don't worry about 64-bit now, but when this is fixed remove the
536// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000537unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000538 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000539
Eric Christopher4bd70472010-09-09 21:44:45 +0000540 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
541 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000542 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000543 .addReg(SrcReg));
544 return MoveReg;
545}
546
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000547unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000548 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000549
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000550 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
551 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000552 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000553 .addReg(SrcReg));
554 return MoveReg;
555}
556
Eric Christopher3cf63f12010-09-09 00:19:41 +0000557// For double width floating point we need to materialize two constants
558// (the high and the low) into integer registers then use a move to get
559// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000560unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000561 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000562 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000563
Eric Christopher3cf63f12010-09-09 00:19:41 +0000564 // This checks to see if we can use VFP3 instructions to materialize
565 // a constant, otherwise we have to go through the constant pool.
566 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000567 int Imm;
568 unsigned Opc;
569 if (is64bit) {
570 Imm = ARM_AM::getFP64Imm(Val);
571 Opc = ARM::FCONSTD;
572 } else {
573 Imm = ARM_AM::getFP32Imm(Val);
574 Opc = ARM::FCONSTS;
575 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000576 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
577 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
578 DestReg)
Jim Grosbachefc761a2011-09-30 00:50:06 +0000579 .addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000580 return DestReg;
581 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000582
Eric Christopher860fc932010-09-10 00:34:35 +0000583 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000584 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000585
Eric Christopher22fd29a2010-09-09 23:50:00 +0000586 // MachineConstantPool wants an explicit alignment.
587 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
588 if (Align == 0) {
589 // TODO: Figure out if this is correct.
590 Align = TD.getTypeAllocSize(CFP->getType());
591 }
592 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
593 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
594 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000595
Eric Christopher860fc932010-09-10 00:34:35 +0000596 // The extra reg is for addrmode5.
Eric Christopher6f98bfd2010-09-28 00:35:09 +0000597 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
598 DestReg)
599 .addConstantPoolIndex(Idx)
Eric Christopher22fd29a2010-09-09 23:50:00 +0000600 .addReg(0));
601 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000602}
603
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000604unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopher7ac602b2010-10-11 08:38:55 +0000605
Chad Rosier67f96882011-11-04 22:29:00 +0000606 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
607 return false;
Eric Christophere4dd7372010-11-03 20:21:17 +0000608
609 // If we can do this in a single instruction without a constant pool entry
610 // do so now.
611 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000612 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000613 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000614 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
615 &ARM::GPRRegClass;
616 unsigned ImmReg = createResultReg(RC);
Eric Christophere4dd7372010-11-03 20:21:17 +0000617 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Chad Rosier67f96882011-11-04 22:29:00 +0000618 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000619 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000620 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000621 }
622
Chad Rosier2a3503e2011-11-11 00:36:21 +0000623 // Use MVN to emit negative constants.
624 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
625 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000626 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000627 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000628 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000629 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
630 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
631 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
632 TII.get(Opc), ImmReg)
633 .addImm(Imm));
634 return ImmReg;
635 }
636 }
637
638 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000639 if (VT != MVT::i32)
640 return false;
641
642 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
643
Eric Christopherc3e118e2010-09-02 23:43:26 +0000644 // MachineConstantPool wants an explicit alignment.
645 unsigned Align = TD.getPrefTypeAlignment(C->getType());
646 if (Align == 0) {
647 // TODO: Figure out if this is correct.
648 Align = TD.getTypeAllocSize(C->getType());
649 }
650 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000651
Chad Rosier0439cfc2011-11-08 21:12:00 +0000652 if (isThumb2)
Eric Christopherc3e118e2010-09-02 23:43:26 +0000653 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher953b1af2010-09-28 21:55:34 +0000654 TII.get(ARM::t2LDRpci), DestReg)
655 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000656 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000657 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000658 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Eric Christopherc3e118e2010-09-02 23:43:26 +0000659 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher953b1af2010-09-28 21:55:34 +0000660 TII.get(ARM::LDRcp), DestReg)
661 .addConstantPoolIndex(Idx)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000662 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000663 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000664
Eric Christopherc3e118e2010-09-02 23:43:26 +0000665 return DestReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000666}
667
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000668unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000669 // For now 32-bit only.
Duncan Sands14627772010-11-03 12:17:33 +0000670 if (VT != MVT::i32) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000671
Eric Christopher7787f792010-10-02 00:32:44 +0000672 Reloc::Model RelocM = TM.getRelocationModel();
Jush Lue87e5592012-08-29 02:41:21 +0000673 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier65710a72012-11-07 00:13:01 +0000674 const TargetRegisterClass *RC = isThumb2 ?
675 (const TargetRegisterClass*)&ARM::rGPRRegClass :
676 (const TargetRegisterClass*)&ARM::GPRRegClass;
677 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000678
Tim Northoverd6a729b2014-01-06 14:28:05 +0000679 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000680 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
681 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000682 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000683
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000684 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000685 // Non-darwin targets only support static movt relocations in FastISel.
Jakob Stoklund Olesen083dbdc2012-01-07 20:49:15 +0000686 if (Subtarget->useMovt() &&
Tim Northoverd6a729b2014-01-06 14:28:05 +0000687 (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000688 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000689 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000690 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000691 TF = ARMII::MO_NONLAZY;
692
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000693 switch (RelocM) {
694 case Reloc::PIC_:
695 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
696 break;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000697 default:
698 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
699 break;
700 }
701 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000702 DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000703 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000704 // MachineConstantPool wants an explicit alignment.
705 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
706 if (Align == 0) {
707 // TODO: Figure out if this is correct.
708 Align = TD.getTypeAllocSize(GV->getType());
709 }
710
Jush Lu47172a02012-09-27 05:21:41 +0000711 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
712 return ARMLowerPICELF(GV, Align, VT);
713
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000714 // Grab index.
715 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
716 (Subtarget->isThumb() ? 4 : 8);
717 unsigned Id = AFI->createPICLabelUId();
718 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
719 ARMCP::CPValue,
720 PCAdj);
721 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
722
723 // Load value.
724 MachineInstrBuilder MIB;
725 if (isThumb2) {
726 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
727 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
728 .addConstantPoolIndex(Idx);
729 if (RelocM == Reloc::PIC_)
730 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000731 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000732 } else {
733 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000734 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000735 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
736 DestReg)
737 .addConstantPoolIndex(Idx)
738 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000739 AddOptionalDefs(MIB);
740
741 if (RelocM == Reloc::PIC_) {
742 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
743 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
744
745 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
746 DL, TII.get(Opc), NewDestReg)
747 .addReg(DestReg)
748 .addImm(Id);
749 AddOptionalDefs(MIB);
750 return NewDestReg;
751 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000752 }
Eric Christopher7787f792010-10-02 00:32:44 +0000753 }
Eli Friedman86585792011-06-03 01:13:19 +0000754
Jush Lue87e5592012-08-29 02:41:21 +0000755 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000756 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000757 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000758 if (isThumb2)
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000759 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
760 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000761 .addReg(DestReg)
762 .addImm(0);
763 else
764 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
765 NewDestReg)
766 .addReg(DestReg)
767 .addImm(0);
768 DestReg = NewDestReg;
769 AddOptionalDefs(MIB);
770 }
771
Eric Christopher7787f792010-10-02 00:32:44 +0000772 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000773}
774
Eric Christopher3cf63f12010-09-09 00:19:41 +0000775unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglundc494d242012-12-17 14:30:06 +0000776 EVT CEVT = TLI.getValueType(C->getType(), true);
777
778 // Only handle simple types.
779 if (!CEVT.isSimple()) return 0;
780 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000781
782 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
783 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000784 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
785 return ARMMaterializeGV(GV, VT);
786 else if (isa<ConstantInt>(C))
787 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000788
Eric Christopher83a5ec82010-10-01 23:24:42 +0000789 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000790}
791
Chad Rosier0eff3e52011-11-17 21:46:13 +0000792// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
793
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000794unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
795 // Don't handle dynamic allocas.
796 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000797
Duncan Sandsf5dda012010-11-03 11:35:31 +0000798 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000799 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000800
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000801 DenseMap<const AllocaInst*, int>::iterator SI =
802 FuncInfo.StaticAllocaMap.find(AI);
803
804 // This will get lowered later into the correct offsets and registers
805 // via rewriteXFrameIndex.
806 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000807 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000808 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000809 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000810 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
811
Evan Cheng7fae11b2011-12-14 02:11:42 +0000812 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000813 TII.get(Opc), ResultReg)
814 .addFrameIndex(SI->second)
815 .addImm(0));
816 return ResultReg;
817 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000818
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000819 return 0;
820}
821
Chris Lattner229907c2011-07-18 04:54:35 +0000822bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000823 EVT evt = TLI.getValueType(Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000824
Eric Christopher761e7fb2010-08-25 07:23:49 +0000825 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000826 if (evt == MVT::Other || !evt.isSimple()) return false;
827 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000828
Eric Christopher901176a2010-08-31 01:28:42 +0000829 // Handle all legal types, i.e. a register that will directly hold this
830 // value.
831 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000832}
833
Chris Lattner229907c2011-07-18 04:54:35 +0000834bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000835 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000836
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000837 // If this is a type than can be sign or zero-extended to a basic operation
838 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000839 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000840 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000841
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000842 return false;
843}
844
Eric Christopher558b61e2010-11-19 22:36:41 +0000845// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000846bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000847 // Some boilerplate from the X86 FastISel.
848 const User *U = NULL;
Eric Christopher00202ee2010-08-23 21:44:12 +0000849 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000850 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000851 // Don't walk into other basic blocks unless the object is an alloca from
852 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000853 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
854 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
855 Opcode = I->getOpcode();
856 U = I;
857 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000858 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000859 Opcode = C->getOpcode();
860 U = C;
861 }
862
Chris Lattner229907c2011-07-18 04:54:35 +0000863 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000864 if (Ty->getAddressSpace() > 255)
865 // Fast instruction selection doesn't support the special
866 // address spaces.
867 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000868
Eric Christopher00202ee2010-08-23 21:44:12 +0000869 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000870 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000871 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000872 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000873 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000874 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000875 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000876 // Look past no-op inttoptrs.
877 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000878 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000879 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000880 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000881 // Look past no-op ptrtoints.
882 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000883 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000884 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000885 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000886 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000887 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000888
Eric Christopher21d0c172010-10-14 09:29:41 +0000889 // Iterate through the GEP folding the constants into offsets where
890 // we can.
891 gep_type_iterator GTI = gep_type_begin(U);
892 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
893 i != e; ++i, ++GTI) {
894 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000895 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christopher21d0c172010-10-14 09:29:41 +0000896 const StructLayout *SL = TD.getStructLayout(STy);
897 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
898 TmpOffset += SL->getElementOffset(Idx);
899 } else {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000900 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000901 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000902 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
903 // Constant-offset addressing.
904 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000905 break;
906 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000907 if (canFoldAddIntoGEP(U, Op)) {
908 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000909 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000910 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000911 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000912 // Iterate on the other operand.
913 Op = cast<AddOperator>(Op)->getOperand(0);
914 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000915 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000916 // Unsupported
917 goto unsupported_gep;
918 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000919 }
920 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000921
922 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000923 Addr.Offset = TmpOffset;
924 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000925
926 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000927 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000928
Eric Christopher21d0c172010-10-14 09:29:41 +0000929 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000930 break;
931 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000932 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000933 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000934 DenseMap<const AllocaInst*, int>::iterator SI =
935 FuncInfo.StaticAllocaMap.find(AI);
936 if (SI != FuncInfo.StaticAllocaMap.end()) {
937 Addr.BaseType = Address::FrameIndexBase;
938 Addr.Base.FI = SI->second;
939 return true;
940 }
941 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000942 }
943 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000944
Eric Christopher9d4e4712010-08-24 00:07:24 +0000945 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000946 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
947 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000948}
949
Chad Rosier150d35b2012-12-17 22:35:29 +0000950void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000951 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000952 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000953 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000954 case MVT::i1:
955 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000956 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000957 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000958 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000959 // Integer loads/stores handle 12-bit offsets.
960 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000961 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000962 if (needsLowering && isThumb2)
963 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
964 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000965 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000966 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000967 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000968 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000969 break;
970 case MVT::f32:
971 case MVT::f64:
972 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000973 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000974 break;
975 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000976
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000977 // If this is a stack pointer and the offset needs to be simplified then
978 // put the alloca address into a register, set the base type back to
979 // register and continue. This should almost never happen.
980 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topperc7242e02012-04-20 07:30:17 +0000981 const TargetRegisterClass *RC = isThumb2 ?
982 (const TargetRegisterClass*)&ARM::tGPRRegClass :
983 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000984 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000985 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000986 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000987 TII.get(Opc), ResultReg)
988 .addFrameIndex(Addr.Base.FI)
989 .addImm(0));
990 Addr.Base.Reg = ResultReg;
991 Addr.BaseType = Address::RegBase;
992 }
993
Eric Christopher73bc5b02010-10-21 19:40:30 +0000994 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +0000995 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +0000996 if (needsLowering) {
Eli Friedman86caced2011-04-29 21:22:56 +0000997 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
998 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +0000999 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +00001000 }
Eric Christopher00202ee2010-08-23 21:44:12 +00001001}
1002
Chad Rosier150d35b2012-12-17 22:35:29 +00001003void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +00001004 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001005 unsigned Flags, bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001006 // addrmode5 output depends on the selection dag addressing dividing the
1007 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +00001008 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +00001009 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +00001010
Eric Christopher119ff7f2010-12-01 01:40:24 +00001011 // Frame base works a bit differently. Handle it separately.
1012 if (Addr.BaseType == Address::FrameIndexBase) {
1013 int FI = Addr.Base.FI;
1014 int Offset = Addr.Offset;
1015 MachineMemOperand *MMO =
1016 FuncInfo.MF->getMachineMemOperand(
1017 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarich6528a542011-05-28 20:34:49 +00001018 Flags,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001019 MFI.getObjectSize(FI),
1020 MFI.getObjectAlignment(FI));
1021 // Now add the rest of the operands.
1022 MIB.addFrameIndex(FI);
1023
Bob Wilson80381f62011-12-04 00:52:23 +00001024 // ARM halfword load/stores and signed byte loads need an additional
1025 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001026 if (useAM3) {
1027 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1028 MIB.addReg(0);
1029 MIB.addImm(Imm);
1030 } else {
1031 MIB.addImm(Addr.Offset);
1032 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001033 MIB.addMemOperand(MMO);
1034 } else {
1035 // Now add the rest of the operands.
1036 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +00001037
Bob Wilson80381f62011-12-04 00:52:23 +00001038 // ARM halfword load/stores and signed byte loads need an additional
1039 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001040 if (useAM3) {
1041 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1042 MIB.addReg(0);
1043 MIB.addImm(Imm);
1044 } else {
1045 MIB.addImm(Addr.Offset);
1046 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001047 }
1048 AddOptionalDefs(MIB);
1049}
1050
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001051bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +00001052 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +00001053 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001054 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +00001055 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +00001056 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001057 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001058 // This is mostly going to be Neon/vector support.
1059 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +00001060 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001061 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001062 if (isThumb2) {
1063 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1064 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1065 else
1066 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001067 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +00001068 if (isZExt) {
1069 Opc = ARM::LDRBi12;
1070 } else {
1071 Opc = ARM::LDRSB;
1072 useAM3 = true;
1073 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001074 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001075 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001076 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001077 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001078 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001079 return false;
1080
Chad Rosieradfd2002011-11-14 20:22:27 +00001081 if (isThumb2) {
1082 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1083 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1084 else
1085 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1086 } else {
1087 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1088 useAM3 = true;
1089 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001090 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001091 break;
Eric Christopher901176a2010-08-31 01:28:42 +00001092 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001093 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001094 return false;
1095
Chad Rosieradfd2002011-11-14 20:22:27 +00001096 if (isThumb2) {
1097 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1098 Opc = ARM::t2LDRi8;
1099 else
1100 Opc = ARM::t2LDRi12;
1101 } else {
1102 Opc = ARM::LDRi12;
1103 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001104 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +00001105 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +00001106 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +00001107 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +00001108 // Unaligned loads need special handling. Floats require word-alignment.
1109 if (Alignment && Alignment < 4) {
1110 needVMOV = true;
1111 VT = MVT::i32;
1112 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +00001113 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +00001114 } else {
1115 Opc = ARM::VLDRS;
1116 RC = TLI.getRegClassFor(VT);
1117 }
Eric Christopheraef6499b2010-09-18 01:59:37 +00001118 break;
1119 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +00001120 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001121 // FIXME: Unaligned loads need special handling. Doublewords require
1122 // word-alignment.
1123 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +00001124 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001125
Eric Christopheraef6499b2010-09-18 01:59:37 +00001126 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +00001127 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +00001128 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001129 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001130 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001131 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001132
Eric Christopher119ff7f2010-12-01 01:40:24 +00001133 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001134 if (allocReg)
1135 ResultReg = createResultReg(RC);
1136 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher119ff7f2010-12-01 01:40:24 +00001137 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1138 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001139 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001140
1141 // If we had an unaligned load of a float we've converted it to an regular
1142 // load. Now we must move from the GRP to the FP register.
1143 if (needVMOV) {
1144 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1145 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1146 TII.get(ARM::VMOVSR), MoveReg)
1147 .addReg(ResultReg));
1148 ResultReg = MoveReg;
1149 }
Eric Christopher901176a2010-08-31 01:28:42 +00001150 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001151}
1152
Eric Christopher29ab6d12010-09-27 06:02:23 +00001153bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001154 // Atomic loads need special handling.
1155 if (cast<LoadInst>(I)->isAtomic())
1156 return false;
1157
Eric Christopher860fc932010-09-10 00:34:35 +00001158 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001159 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001160 if (!isLoadTypeLegal(I->getType(), VT))
1161 return false;
1162
Eric Christopher119ff7f2010-12-01 01:40:24 +00001163 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001164 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001165 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001166
1167 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001168 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1169 return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001170 UpdateValueMap(I, ResultReg);
1171 return true;
1172}
1173
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001174bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001175 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001176 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001177 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001178 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001179 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001180 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001181 case MVT::i1: {
Craig Topperc7242e02012-04-20 07:30:17 +00001182 unsigned Res = createResultReg(isThumb2 ?
1183 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1184 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001185 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001186 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Eric Christopher1e43892e2010-11-02 23:59:09 +00001187 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1188 TII.get(Opc), Res)
1189 .addReg(SrcReg).addImm(1));
1190 SrcReg = Res;
1191 } // Fallthrough here.
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001192 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001193 if (isThumb2) {
1194 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1195 StrOpc = ARM::t2STRBi8;
1196 else
1197 StrOpc = ARM::t2STRBi12;
1198 } else {
1199 StrOpc = ARM::STRBi12;
1200 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001201 break;
1202 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001203 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001204 return false;
1205
Chad Rosieradfd2002011-11-14 20:22:27 +00001206 if (isThumb2) {
1207 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1208 StrOpc = ARM::t2STRHi8;
1209 else
1210 StrOpc = ARM::t2STRHi12;
1211 } else {
1212 StrOpc = ARM::STRH;
1213 useAM3 = true;
1214 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001215 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001216 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001217 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001218 return false;
1219
Chad Rosieradfd2002011-11-14 20:22:27 +00001220 if (isThumb2) {
1221 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1222 StrOpc = ARM::t2STRi8;
1223 else
1224 StrOpc = ARM::t2STRi12;
1225 } else {
1226 StrOpc = ARM::STRi12;
1227 }
Eric Christopherc918d552010-10-16 01:10:35 +00001228 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001229 case MVT::f32:
1230 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001231 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001232 if (Alignment && Alignment < 4) {
1233 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1235 TII.get(ARM::VMOVRS), MoveReg)
1236 .addReg(SrcReg));
1237 SrcReg = MoveReg;
1238 VT = MVT::i32;
1239 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001240 } else {
1241 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001242 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001243 break;
1244 case MVT::f64:
1245 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001246 // FIXME: Unaligned stores need special handling. Doublewords require
1247 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001248 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001249 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001250
Eric Christopherc3e118e2010-09-02 23:43:26 +00001251 StrOpc = ARM::VSTRD;
1252 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001253 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001254 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001255 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001256
Eric Christopher119ff7f2010-12-01 01:40:24 +00001257 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001258 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001259 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1260 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001261 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001262 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001263 return true;
1264}
1265
Eric Christopher29ab6d12010-09-27 06:02:23 +00001266bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001267 Value *Op0 = I->getOperand(0);
1268 unsigned SrcReg = 0;
1269
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001270 // Atomic stores need special handling.
1271 if (cast<StoreInst>(I)->isAtomic())
1272 return false;
1273
Eric Christopher119ff7f2010-12-01 01:40:24 +00001274 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001275 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001276 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001277 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001278
Eric Christopher92db2012010-09-02 01:48:11 +00001279 // Get the value to be stored into a register.
1280 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001281 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001282
Eric Christopher119ff7f2010-12-01 01:40:24 +00001283 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001284 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001285 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001286 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001287
Chad Rosierec3b77e2011-12-03 02:21:57 +00001288 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1289 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001290 return true;
1291}
1292
1293static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1294 switch (Pred) {
1295 // Needs two compares...
1296 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001297 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001298 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001299 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001300 return ARMCC::AL;
1301 case CmpInst::ICMP_EQ:
1302 case CmpInst::FCMP_OEQ:
1303 return ARMCC::EQ;
1304 case CmpInst::ICMP_SGT:
1305 case CmpInst::FCMP_OGT:
1306 return ARMCC::GT;
1307 case CmpInst::ICMP_SGE:
1308 case CmpInst::FCMP_OGE:
1309 return ARMCC::GE;
1310 case CmpInst::ICMP_UGT:
1311 case CmpInst::FCMP_UGT:
1312 return ARMCC::HI;
1313 case CmpInst::FCMP_OLT:
1314 return ARMCC::MI;
1315 case CmpInst::ICMP_ULE:
1316 case CmpInst::FCMP_OLE:
1317 return ARMCC::LS;
1318 case CmpInst::FCMP_ORD:
1319 return ARMCC::VC;
1320 case CmpInst::FCMP_UNO:
1321 return ARMCC::VS;
1322 case CmpInst::FCMP_UGE:
1323 return ARMCC::PL;
1324 case CmpInst::ICMP_SLT:
1325 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001326 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001327 case CmpInst::ICMP_SLE:
1328 case CmpInst::FCMP_ULE:
1329 return ARMCC::LE;
1330 case CmpInst::FCMP_UNE:
1331 case CmpInst::ICMP_NE:
1332 return ARMCC::NE;
1333 case CmpInst::ICMP_UGE:
1334 return ARMCC::HS;
1335 case CmpInst::ICMP_ULT:
1336 return ARMCC::LO;
1337 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001338}
1339
Eric Christopher29ab6d12010-09-27 06:02:23 +00001340bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001341 const BranchInst *BI = cast<BranchInst>(I);
1342 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1343 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001344
Eric Christopher6aaed722010-09-03 00:35:47 +00001345 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001346
Eric Christopher5c308f82010-10-29 21:08:19 +00001347 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1348 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001349 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001350 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001351
1352 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001353 // Try to take advantage of fallthrough opportunities.
1354 CmpInst::Predicate Predicate = CI->getPredicate();
1355 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1356 std::swap(TBB, FBB);
1357 Predicate = CmpInst::getInversePredicate(Predicate);
1358 }
1359
1360 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001361
1362 // We may not handle every CC for now.
1363 if (ARMPred == ARMCC::AL) return false;
1364
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001365 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001366 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001367 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001368
Chad Rosier0439cfc2011-11-08 21:12:00 +00001369 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher5c308f82010-10-29 21:08:19 +00001370 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1371 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1372 FastEmitBranch(FBB, DL);
1373 FuncInfo.MBB->addSuccessor(TBB);
1374 return true;
1375 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001376 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1377 MVT SourceVT;
1378 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001379 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001380 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001381 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001382 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Eric Christopher8d46b472011-04-29 20:02:39 +00001383 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1384 TII.get(TstOpc))
1385 .addReg(OpReg).addImm(1));
1386
1387 unsigned CCMode = ARMCC::NE;
1388 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1389 std::swap(TBB, FBB);
1390 CCMode = ARMCC::EQ;
1391 }
1392
Chad Rosier0439cfc2011-11-08 21:12:00 +00001393 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher8d46b472011-04-29 20:02:39 +00001394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1395 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1396
1397 FastEmitBranch(FBB, DL);
1398 FuncInfo.MBB->addSuccessor(TBB);
1399 return true;
1400 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001401 } else if (const ConstantInt *CI =
1402 dyn_cast<ConstantInt>(BI->getCondition())) {
1403 uint64_t Imm = CI->getZExtValue();
1404 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1405 FastEmitBranch(Target, DL);
1406 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001407 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001408
Eric Christopher5c308f82010-10-29 21:08:19 +00001409 unsigned CmpReg = getRegForValue(BI->getCondition());
1410 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001411
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001412 // We've been divorced from our compare! Our block was split, and
1413 // now our compare lives in a predecessor block. We musn't
1414 // re-compare here, as the children of the compare aren't guaranteed
1415 // live across the block boundary (we *could* check for this).
1416 // Regardless, the compare has been done in the predecessor block,
1417 // and it left a value for us in a virtual register. Ergo, we test
1418 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001419 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001420 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001421 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1422 .addReg(CmpReg).addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001423
Eric Christopher4f012fd2011-04-28 16:52:09 +00001424 unsigned CCMode = ARMCC::NE;
1425 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1426 std::swap(TBB, FBB);
1427 CCMode = ARMCC::EQ;
1428 }
1429
Chad Rosier0439cfc2011-11-08 21:12:00 +00001430 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher6aaed722010-09-03 00:35:47 +00001431 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001432 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christopher6aaed722010-09-03 00:35:47 +00001433 FastEmitBranch(FBB, DL);
1434 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001435 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001436}
1437
Chad Rosierded4c992012-02-07 23:56:08 +00001438bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1439 unsigned AddrReg = getRegForValue(I->getOperand(0));
1440 if (AddrReg == 0) return false;
1441
1442 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
1444 .addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001445
1446 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1447 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1448 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1449
Jush Luac96b762012-06-14 06:08:19 +00001450 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001451}
1452
Chad Rosier9cf803c2011-11-02 18:08:25 +00001453bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1454 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001455 Type *Ty = Src1Value->getType();
Patrik Hagglundc494d242012-12-17 14:30:06 +00001456 EVT SrcEVT = TLI.getValueType(Ty, true);
1457 if (!SrcEVT.isSimple()) return false;
1458 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001459
Chad Rosier78127d32011-10-26 23:25:44 +00001460 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1461 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001462 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001463
Chad Rosier595d4192011-11-09 03:22:02 +00001464 // Check to see if the 2nd operand is a constant that we can encode directly
1465 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001466 int Imm = 0;
1467 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001468 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001469 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1470 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001471 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1472 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1473 SrcVT == MVT::i1) {
1474 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001475 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001476 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1477 // then a cmn, because there is no way to represent 2147483648 as a
1478 // signed 32-bit int.
1479 if (Imm < 0 && Imm != (int)0x80000000) {
1480 isNegativeImm = true;
1481 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001482 }
Chad Rosier26d05882012-03-15 22:54:20 +00001483 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1484 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001485 }
1486 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1487 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1488 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001489 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001490 }
1491
Eric Christopherc3e9c402010-09-08 23:13:45 +00001492 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001493 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001494 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001495 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001496 default: return false;
1497 // TODO: Verify compares.
1498 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001499 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001500 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001501 break;
1502 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001503 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001504 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001505 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001506 case MVT::i1:
1507 case MVT::i8:
1508 case MVT::i16:
1509 needsExt = true;
1510 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001511 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001512 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001513 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001514 CmpOpc = ARM::t2CMPrr;
1515 else
Bill Wendling4b796472012-06-11 08:07:26 +00001516 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001517 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001518 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001519 CmpOpc = ARM::CMPrr;
1520 else
Bill Wendling4b796472012-06-11 08:07:26 +00001521 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001522 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001523 break;
1524 }
1525
Chad Rosier9cf803c2011-11-02 18:08:25 +00001526 unsigned SrcReg1 = getRegForValue(Src1Value);
1527 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001528
Duncan Sands12330652011-11-28 10:31:27 +00001529 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001530 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001531 SrcReg2 = getRegForValue(Src2Value);
1532 if (SrcReg2 == 0) return false;
1533 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001534
1535 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1536 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001537 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1538 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001539 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001540 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1541 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001542 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001543 }
Chad Rosier59a20192011-10-26 22:47:55 +00001544
Jim Grosbachd7866792013-08-16 23:37:40 +00001545 const MCInstrDesc &II = TII.get(CmpOpc);
1546 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001547 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001548 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
1549 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001550 .addReg(SrcReg1).addReg(SrcReg2));
1551 } else {
1552 MachineInstrBuilder MIB;
Jim Grosbachd7866792013-08-16 23:37:40 +00001553 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001554 .addReg(SrcReg1);
1555
1556 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1557 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001558 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001559 AddOptionalDefs(MIB);
1560 }
Chad Rosier78127d32011-10-26 23:25:44 +00001561
1562 // For floating point we need to move the result to a comparison register
1563 // that we can then use for branches.
1564 if (Ty->isFloatTy() || Ty->isDoubleTy())
1565 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1566 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001567 return true;
1568}
1569
1570bool ARMFastISel::SelectCmp(const Instruction *I) {
1571 const CmpInst *CI = cast<CmpInst>(I);
1572
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001573 // Get the compare predicate.
1574 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001575
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001576 // We may not handle every CC for now.
1577 if (ARMPred == ARMCC::AL) return false;
1578
Chad Rosier59a20192011-10-26 22:47:55 +00001579 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001580 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001581 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001582
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001583 // Now set a register based on the comparison. Explicitly set the predicates
1584 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001585 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topperc7242e02012-04-20 07:30:17 +00001586 const TargetRegisterClass *RC = isThumb2 ?
1587 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1588 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001589 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001590 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001591 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001592 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001593 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1594 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001595 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001596
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001597 UpdateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001598 return true;
1599}
1600
Eric Christopher29ab6d12010-09-27 06:02:23 +00001601bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001602 // Make sure we have VFP and that we're extending float to double.
1603 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001604
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001605 Value *V = I->getOperand(0);
1606 if (!I->getType()->isDoubleTy() ||
1607 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001608
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001609 unsigned Op = getRegForValue(V);
1610 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001611
Craig Topperc7242e02012-04-20 07:30:17 +00001612 unsigned Result = createResultReg(&ARM::DPRRegClass);
Eric Christopher2ff757d2010-09-09 01:06:51 +00001613 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher82b05d72010-09-09 20:36:19 +00001614 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001615 .addReg(Op));
1616 UpdateValueMap(I, Result);
1617 return true;
1618}
1619
Eric Christopher29ab6d12010-09-27 06:02:23 +00001620bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001621 // Make sure we have VFP and that we're truncating double to float.
1622 if (!Subtarget->hasVFP2()) return false;
1623
1624 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001625 if (!(I->getType()->isFloatTy() &&
1626 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001627
1628 unsigned Op = getRegForValue(V);
1629 if (Op == 0) return false;
1630
Craig Topperc7242e02012-04-20 07:30:17 +00001631 unsigned Result = createResultReg(&ARM::SPRRegClass);
Eric Christopher5903c0b2010-09-09 20:26:31 +00001632 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher82b05d72010-09-09 20:36:19 +00001633 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001634 .addReg(Op));
1635 UpdateValueMap(I, Result);
1636 return true;
1637}
1638
Chad Rosiere023d5d2012-02-03 21:14:11 +00001639bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001640 // Make sure we have VFP.
1641 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001642
Duncan Sandsf5dda012010-11-03 11:35:31 +00001643 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001644 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001645 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001646 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001647
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001648 Value *Src = I->getOperand(0);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001649 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1650 if (!SrcEVT.isSimple())
1651 return false;
1652 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001653 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001654 return false;
1655
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001656 unsigned SrcReg = getRegForValue(Src);
1657 if (SrcReg == 0) return false;
1658
1659 // Handle sign-extension.
1660 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001661 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001662 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001663 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001664 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001665
Eric Christopher860fc932010-09-10 00:34:35 +00001666 // The conversion routine works on fp-reg to fp-reg and the operand above
1667 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001668 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001669 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001670
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001671 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001672 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1673 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001674 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001675
Eric Christopher4bd70472010-09-09 21:44:45 +00001676 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001677 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1678 ResultReg)
Eric Christopher4bd70472010-09-09 21:44:45 +00001679 .addReg(FP));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001680 UpdateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001681 return true;
1682}
1683
Chad Rosiere023d5d2012-02-03 21:14:11 +00001684bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001685 // Make sure we have VFP.
1686 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001687
Duncan Sandsf5dda012010-11-03 11:35:31 +00001688 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001689 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001690 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001691 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001692
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001693 unsigned Op = getRegForValue(I->getOperand(0));
1694 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001695
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001696 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001697 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001698 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1699 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001700 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001701
Chad Rosier41f0e782012-02-03 20:27:51 +00001702 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001703 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001704 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1705 ResultReg)
1706 .addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001707
Eric Christopher4bd70472010-09-09 21:44:45 +00001708 // This result needs to be in an integer register, but the conversion only
1709 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001710 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001711 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001712
Eric Christopher4bd70472010-09-09 21:44:45 +00001713 UpdateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001714 return true;
1715}
1716
Eric Christopher511aa312010-10-11 08:27:59 +00001717bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001718 MVT VT;
1719 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001720 return false;
1721
1722 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001723 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001724
1725 unsigned CondReg = getRegForValue(I->getOperand(0));
1726 if (CondReg == 0) return false;
1727 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1728 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001729
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001730 // Check to see if we can use an immediate in the conditional move.
1731 int Imm = 0;
1732 bool UseImm = false;
1733 bool isNegativeImm = false;
1734 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1735 assert (VT == MVT::i32 && "Expecting an i32.");
1736 Imm = (int)ConstInt->getValue().getZExtValue();
1737 if (Imm < 0) {
1738 isNegativeImm = true;
1739 Imm = ~Imm;
1740 }
1741 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1742 (ARM_AM::getSOImmVal(Imm) != -1);
1743 }
1744
Duncan Sands12330652011-11-28 10:31:27 +00001745 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001746 if (!UseImm) {
1747 Op2Reg = getRegForValue(I->getOperand(2));
1748 if (Op2Reg == 0) return false;
1749 }
1750
1751 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Jim Grosbachd7866792013-08-16 23:37:40 +00001752 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
Eric Christopher511aa312010-10-11 08:27:59 +00001753 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001754 .addReg(CondReg).addImm(0));
1755
1756 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001757 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001758 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001759 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001760 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1761 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001762 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1763 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001764 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001765 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001766 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001767 }
Eric Christopher511aa312010-10-11 08:27:59 +00001768 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001769 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001770 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001771 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001772 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1773 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001774 } else {
1775 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001776 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1777 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001778 }
Eric Christopher511aa312010-10-11 08:27:59 +00001779 UpdateValueMap(I, ResultReg);
1780 return true;
1781}
1782
Chad Rosieraaa55a82012-02-03 21:07:27 +00001783bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001784 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001785 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001786 if (!isTypeLegal(Ty, VT))
1787 return false;
1788
1789 // If we have integer div support we should have selected this automagically.
1790 // In case we have a real miss go ahead and return false and we'll pick
1791 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001792 if (Subtarget->hasDivide()) return false;
1793
Eric Christopher56094ff2010-09-30 22:34:19 +00001794 // Otherwise emit a libcall.
1795 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001796 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001797 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001798 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001799 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001800 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001801 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001802 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001803 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001804 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001805 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001806 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001807
Eric Christopher56094ff2010-09-30 22:34:19 +00001808 return ARMEmitLibcall(I, LC);
1809}
1810
Chad Rosierb84a4b42012-02-03 21:23:45 +00001811bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001812 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001813 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001814 if (!isTypeLegal(Ty, VT))
1815 return false;
1816
1817 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1818 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001819 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001820 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001821 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001822 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001823 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001824 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001825 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001826 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001827 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001828 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001829
Eric Christophereae1b382010-10-11 08:37:26 +00001830 return ARMEmitLibcall(I, LC);
1831}
1832
Chad Rosier685b20c2012-02-06 23:50:07 +00001833bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier685b20c2012-02-06 23:50:07 +00001834 EVT DestVT = TLI.getValueType(I->getType(), true);
1835
1836 // We can get here in the case when we have a binary operation on a non-legal
1837 // type and the target independent selector doesn't know how to handle it.
1838 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1839 return false;
Jush Luac96b762012-06-14 06:08:19 +00001840
Chad Rosierbd471252012-02-08 02:29:21 +00001841 unsigned Opc;
1842 switch (ISDOpcode) {
1843 default: return false;
1844 case ISD::ADD:
1845 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1846 break;
1847 case ISD::OR:
1848 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1849 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001850 case ISD::SUB:
1851 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1852 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001853 }
1854
Chad Rosier685b20c2012-02-06 23:50:07 +00001855 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1856 if (SrcReg1 == 0) return false;
1857
1858 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1859 // in the instruction, rather then materializing the value in a register.
1860 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1861 if (SrcReg2 == 0) return false;
1862
JF Bastien13969d02013-05-29 15:45:47 +00001863 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001864 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1865 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Chad Rosier685b20c2012-02-06 23:50:07 +00001866 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1867 TII.get(Opc), ResultReg)
1868 .addReg(SrcReg1).addReg(SrcReg2));
1869 UpdateValueMap(I, ResultReg);
1870 return true;
1871}
1872
1873bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001874 EVT FPVT = TLI.getValueType(I->getType(), true);
1875 if (!FPVT.isSimple()) return false;
1876 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001877
Eric Christopher24dc27f2010-09-09 00:53:57 +00001878 // We can get here in the case when we want to use NEON for our fp
1879 // operations, but can't figure out how to. Just use the vfp instructions
1880 // if we have them.
1881 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001882 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001883 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1884 if (isFloat && !Subtarget->hasVFP2())
1885 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001886
Eric Christopher24dc27f2010-09-09 00:53:57 +00001887 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001888 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001889 switch (ISDOpcode) {
1890 default: return false;
1891 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001892 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001893 break;
1894 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001895 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001896 break;
1897 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001898 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001899 break;
1900 }
Chad Rosier80979b62011-11-16 18:39:44 +00001901 unsigned Op1 = getRegForValue(I->getOperand(0));
1902 if (Op1 == 0) return false;
1903
1904 unsigned Op2 = getRegForValue(I->getOperand(1));
1905 if (Op2 == 0) return false;
1906
Chad Rosier62a144f2012-12-17 19:59:43 +00001907 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Eric Christopher24dc27f2010-09-09 00:53:57 +00001908 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1909 TII.get(Opc), ResultReg)
1910 .addReg(Op1).addReg(Op2));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001911 UpdateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001912 return true;
1913}
1914
Eric Christopher72497e52010-09-10 23:18:12 +00001915// Call Handling Code
1916
Jush Lue67e07b2012-07-19 09:49:00 +00001917// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001918// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001919CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1920 bool Return,
1921 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001922 switch (CC) {
1923 default:
1924 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001925 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001926 if (Subtarget->hasVFP2() && !isVarArg) {
1927 if (!Subtarget->isAAPCS_ABI())
1928 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1929 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1930 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1931 }
Evan Cheng21abfc92010-10-22 18:57:05 +00001932 // Fallthrough
1933 case CallingConv::C:
Eric Christopher72497e52010-09-10 23:18:12 +00001934 // Use target triple & subtarget features to do actual dispatch.
1935 if (Subtarget->isAAPCS_ABI()) {
1936 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001937 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001938 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1939 else
1940 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1941 } else
1942 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1943 case CallingConv::ARM_AAPCS_VFP:
Jush Lue67e07b2012-07-19 09:49:00 +00001944 if (!isVarArg)
1945 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1946 // Fall through to soft float variant, variadic functions don't
1947 // use hard floating point ABI.
Eric Christopher72497e52010-09-10 23:18:12 +00001948 case CallingConv::ARM_AAPCS:
1949 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1950 case CallingConv::ARM_APCS:
1951 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001952 case CallingConv::GHC:
1953 if (Return)
1954 llvm_unreachable("Can't return in GHC call convention");
1955 else
1956 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001957 }
1958}
1959
Eric Christopher79398062010-09-29 23:11:09 +00001960bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1961 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001962 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001963 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1964 SmallVectorImpl<unsigned> &RegArgs,
1965 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001966 unsigned &NumBytes,
1967 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001968 SmallVector<CCValAssign, 16> ArgLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00001969 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1970 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1971 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001972
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001973 // Check that we can handle all of the arguments. If we can't, then bail out
1974 // now before we add code to the MBB.
1975 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1976 CCValAssign &VA = ArgLocs[i];
1977 MVT ArgVT = ArgVTs[VA.getValNo()];
1978
1979 // We don't handle NEON/vector parameters yet.
1980 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1981 return false;
1982
1983 // Now copy/store arg to correct locations.
1984 if (VA.isRegLoc() && !VA.needsCustom()) {
1985 continue;
1986 } else if (VA.needsCustom()) {
1987 // TODO: We need custom lowering for vector (v2f64) args.
1988 if (VA.getLocVT() != MVT::f64 ||
1989 // TODO: Only handle register args for now.
1990 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1991 return false;
1992 } else {
Craig Topper56710102013-08-15 02:33:50 +00001993 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001994 default:
1995 return false;
1996 case MVT::i1:
1997 case MVT::i8:
1998 case MVT::i16:
1999 case MVT::i32:
2000 break;
2001 case MVT::f32:
2002 if (!Subtarget->hasVFP2())
2003 return false;
2004 break;
2005 case MVT::f64:
2006 if (!Subtarget->hasVFP2())
2007 return false;
2008 break;
2009 }
2010 }
2011 }
2012
2013 // At the point, we are able to handle the call's arguments in fast isel.
2014
Eric Christopher79398062010-09-29 23:11:09 +00002015 // Get a count of how many bytes are to be pushed on the stack.
2016 NumBytes = CCInfo.getNextStackOffset();
2017
2018 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002019 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopher71ef1af2010-10-11 21:20:02 +00002020 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2021 TII.get(AdjStackDown))
2022 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00002023
2024 // Process the args.
2025 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2026 CCValAssign &VA = ArgLocs[i];
2027 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00002028 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00002029
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002030 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
2031 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00002032
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002033 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00002034 switch (VA.getLocInfo()) {
2035 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00002036 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002037 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002038 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
2039 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002040 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002041 break;
2042 }
Chad Rosierd0191a52011-11-05 20:16:15 +00002043 case CCValAssign::AExt:
2044 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00002045 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002046 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002047 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00002048 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002049 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002050 break;
2051 }
2052 case CCValAssign::BCvt: {
Wesley Peck527da1b2010-11-23 03:31:01 +00002053 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00002054 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00002055 assert(BC != 0 && "Failed to emit a bitcast!");
2056 Arg = BC;
2057 ArgVT = VA.getLocVT();
2058 break;
2059 }
2060 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00002061 }
2062
2063 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00002064 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christopher79398062010-09-29 23:11:09 +00002065 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002066 VA.getLocReg())
Chad Rosierd0191a52011-11-05 20:16:15 +00002067 .addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00002068 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002069 } else if (VA.needsCustom()) {
2070 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002071 assert(VA.getLocVT() == MVT::f64 &&
2072 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00002073
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002074 CCValAssign &NextVA = ArgLocs[++i];
2075
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002076 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2077 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002078
2079 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2080 TII.get(ARM::VMOVRRD), VA.getLocReg())
2081 .addReg(NextVA.getLocReg(), RegState::Define)
2082 .addReg(Arg));
2083 RegArgs.push_back(VA.getLocReg());
2084 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002085 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002086 assert(VA.isMemLoc());
2087 // Need to store on the stack.
Eric Christopherfef5f312010-11-19 22:30:02 +00002088 Address Addr;
2089 Addr.BaseType = Address::RegBase;
2090 Addr.Base.Reg = ARM::SP;
2091 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002092
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002093 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2094 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002095 }
2096 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002097
Eric Christopher79398062010-09-29 23:11:09 +00002098 return true;
2099}
2100
Duncan Sandsf5dda012010-11-03 11:35:31 +00002101bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002102 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002103 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002104 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002105 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopher71ef1af2010-10-11 21:20:02 +00002106 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2107 TII.get(AdjStackUp))
2108 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002109
2110 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002111 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002112 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002113 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2114 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002115
2116 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002117 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002118 // For this move we copy into two registers and then move into the
2119 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002120 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002121 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002122 unsigned ResultReg = createResultReg(DstRC);
2123 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2124 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002125 .addReg(RVLocs[0].getLocReg())
2126 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002127
Eric Christopheraf719ef2010-10-20 08:02:24 +00002128 UsedRegs.push_back(RVLocs[0].getLocReg());
2129 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002130
Eric Christopher7ac602b2010-10-11 08:38:55 +00002131 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002132 UpdateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002133 } else {
2134 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002135 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002136
2137 // Special handling for extended integers.
2138 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2139 CopyVT = MVT::i32;
2140
Craig Topper760b1342012-02-22 05:59:10 +00002141 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002142
Eric Christopherc1e209d2010-10-01 00:00:11 +00002143 unsigned ResultReg = createResultReg(DstRC);
2144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2145 ResultReg).addReg(RVLocs[0].getLocReg());
2146 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002147
Eric Christopher7ac602b2010-10-11 08:38:55 +00002148 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002149 UpdateValueMap(I, ResultReg);
2150 }
Eric Christopher79398062010-09-29 23:11:09 +00002151 }
2152
Eric Christopher7ac602b2010-10-11 08:38:55 +00002153 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002154}
2155
Eric Christopher93bbe652010-10-22 01:28:00 +00002156bool ARMFastISel::SelectRet(const Instruction *I) {
2157 const ReturnInst *Ret = cast<ReturnInst>(I);
2158 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002159
Eric Christopher93bbe652010-10-22 01:28:00 +00002160 if (!FuncInfo.CanLowerReturn)
2161 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002162
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002163 // Build a list of return value registers.
2164 SmallVector<unsigned, 4> RetRegs;
2165
Eric Christopher93bbe652010-10-22 01:28:00 +00002166 CallingConv::ID CC = F.getCallingConv();
2167 if (Ret->getNumOperands() > 0) {
2168 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002169 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher93bbe652010-10-22 01:28:00 +00002170
2171 // Analyze operands of the call, assigning locations to each operand.
2172 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbache7e2aca2011-09-13 20:30:37 +00002173 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002174 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2175 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002176
2177 const Value *RV = Ret->getOperand(0);
2178 unsigned Reg = getRegForValue(RV);
2179 if (Reg == 0)
2180 return false;
2181
2182 // Only handle a single return value for now.
2183 if (ValLocs.size() != 1)
2184 return false;
2185
2186 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002187
Eric Christopher93bbe652010-10-22 01:28:00 +00002188 // Don't bother handling odd stuff for now.
2189 if (VA.getLocInfo() != CCValAssign::Full)
2190 return false;
2191 // Only handle register returns for now.
2192 if (!VA.isRegLoc())
2193 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002194
2195 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier62a144f2012-12-17 19:59:43 +00002196 EVT RVEVT = TLI.getValueType(RV->getType());
2197 if (!RVEVT.isSimple()) return false;
2198 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002199 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002200 // Special handling for extended integers.
2201 if (RVVT != DestVT) {
2202 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2203 return false;
2204
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002205 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2206
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002207 // Perform extension if flagged as either zext or sext. Otherwise, do
2208 // nothing.
2209 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2210 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2211 if (SrcReg == 0) return false;
2212 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002213 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002214
Eric Christopher93bbe652010-10-22 01:28:00 +00002215 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002216 unsigned DstReg = VA.getLocReg();
2217 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2218 // Avoid a cross-class copy. This is very unlikely.
2219 if (!SrcRC->contains(DstReg))
2220 return false;
2221 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2222 DstReg).addReg(SrcReg);
2223
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002224 // Add register to return instruction.
2225 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002226 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002227
Chad Rosier0439cfc2011-11-08 21:12:00 +00002228 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002229 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2230 TII.get(RetOpc));
2231 AddOptionalDefs(MIB);
2232 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2233 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002234 return true;
2235}
2236
Chad Rosierc6916f82012-06-12 19:25:13 +00002237unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2238 if (UseReg)
2239 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2240 else
2241 return isThumb2 ? ARM::tBL : ARM::BL;
2242}
2243
2244unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002245 // Manually compute the global's type to avoid building it when unnecessary.
2246 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2247 EVT LCREVT = TLI.getValueType(GVTy);
2248 if (!LCREVT.isSimple()) return 0;
2249
Bill Wendling76cce192013-12-29 08:00:04 +00002250 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Chad Rosierc6916f82012-06-12 19:25:13 +00002251 GlobalValue::ExternalLinkage, 0, Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002252 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002253 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002254}
2255
Eric Christopher8b912662010-09-14 23:03:37 +00002256// A quick function that will emit a call for a named libcall in F with the
2257// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002258// can emit a call for any libcall we can produce. This is an abridged version
2259// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002260// like computed function pointers or strange arguments at call sites.
2261// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2262// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002263bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2264 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002265
Eric Christopher8b912662010-09-14 23:03:37 +00002266 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002267 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002268 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002269 if (RetTy->isVoidTy())
2270 RetVT = MVT::isVoid;
2271 else if (!isTypeLegal(RetTy, RetVT))
2272 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002273
Chad Rosier90f9afe2012-05-11 18:51:55 +00002274 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002275 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002276 SmallVector<CCValAssign, 16> RVLocs;
2277 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002278 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002279 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2280 return false;
2281 }
2282
Eric Christopher79398062010-09-29 23:11:09 +00002283 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002284 SmallVector<Value*, 8> Args;
2285 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002286 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002287 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2288 Args.reserve(I->getNumOperands());
2289 ArgRegs.reserve(I->getNumOperands());
2290 ArgVTs.reserve(I->getNumOperands());
2291 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002292 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002293 Value *Op = I->getOperand(i);
2294 unsigned Arg = getRegForValue(Op);
2295 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002296
Chris Lattner229907c2011-07-18 04:54:35 +00002297 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002298 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002299 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002300
Eric Christopher8b912662010-09-14 23:03:37 +00002301 ISD::ArgFlagsTy Flags;
2302 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2303 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002304
Eric Christopher8b912662010-09-14 23:03:37 +00002305 Args.push_back(Op);
2306 ArgRegs.push_back(Arg);
2307 ArgVTs.push_back(ArgVT);
2308 ArgFlags.push_back(Flags);
2309 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002310
Eric Christopher79398062010-09-29 23:11:09 +00002311 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002312 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002313 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002314 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2315 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002316 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002317
Chad Rosierc6916f82012-06-12 19:25:13 +00002318 unsigned CalleeReg = 0;
2319 if (EnableARMLongCalls) {
2320 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2321 if (CalleeReg == 0) return false;
2322 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002323
Chad Rosierc6916f82012-06-12 19:25:13 +00002324 // Issue the call.
2325 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2326 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2327 DL, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002328 // BL / BLX don't take a predicate, but tBL / tBLX do.
2329 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002330 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002331 if (EnableARMLongCalls)
2332 MIB.addReg(CalleeReg);
2333 else
2334 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002335
Eric Christopher8b912662010-09-14 23:03:37 +00002336 // Add implicit physical register uses to the call.
2337 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002338 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002339
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002340 // Add a register mask with the call-preserved registers.
2341 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2342 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2343
Eric Christopher79398062010-09-29 23:11:09 +00002344 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002345 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002346 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002347
Eric Christopher8b912662010-09-14 23:03:37 +00002348 // Set all unused physreg defs as dead.
2349 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002350
Eric Christopher8b912662010-09-14 23:03:37 +00002351 return true;
2352}
2353
Chad Rosiera7ebc562011-11-11 23:31:03 +00002354bool ARMFastISel::SelectCall(const Instruction *I,
2355 const char *IntrMemName = 0) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002356 const CallInst *CI = cast<CallInst>(I);
2357 const Value *Callee = CI->getCalledValue();
2358
Chad Rosiera7ebc562011-11-11 23:31:03 +00002359 // Can't handle inline asm.
2360 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002361
Chad Rosierdf42cf32012-12-11 00:18:02 +00002362 // Allow SelectionDAG isel to handle tail calls.
2363 if (CI->isTailCall()) return false;
2364
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002365 // Check the calling convention.
2366 ImmutableCallSite CS(CI);
2367 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002368
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002369 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002370
Chris Lattner229907c2011-07-18 04:54:35 +00002371 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2372 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Lue67e07b2012-07-19 09:49:00 +00002373 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002374
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002375 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002376 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002377 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002378 if (RetTy->isVoidTy())
2379 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002380 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2381 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002382 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002383
Chad Rosier90f9afe2012-05-11 18:51:55 +00002384 // Can't handle non-double multi-reg retvals.
2385 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2386 RetVT != MVT::i16 && RetVT != MVT::i32) {
2387 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002388 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2389 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002390 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2391 return false;
2392 }
2393
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002394 // Set up the argument vectors.
2395 SmallVector<Value*, 8> Args;
2396 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002397 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002398 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002399 unsigned arg_size = CS.arg_size();
2400 Args.reserve(arg_size);
2401 ArgRegs.reserve(arg_size);
2402 ArgVTs.reserve(arg_size);
2403 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002404 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2405 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002406 // If we're lowering a memory intrinsic instead of a regular call, skip the
2407 // last two arguments, which shouldn't be passed to the underlying function.
2408 if (IntrMemName && e-i <= 2)
2409 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002410
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002411 ISD::ArgFlagsTy Flags;
2412 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002413 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002414 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002415 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002416 Flags.setZExt();
2417
Chad Rosier8a98ec42011-11-04 00:58:10 +00002418 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002419 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2420 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2421 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2422 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002423 return false;
2424
Chris Lattner229907c2011-07-18 04:54:35 +00002425 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002426 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002427 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2428 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002429 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002430
2431 unsigned Arg = getRegForValue(*i);
2432 if (Arg == 0)
2433 return false;
2434
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002435 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2436 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002437
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002438 Args.push_back(*i);
2439 ArgRegs.push_back(Arg);
2440 ArgVTs.push_back(ArgVT);
2441 ArgFlags.push_back(Flags);
2442 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002443
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002444 // Handle the arguments now that we've gotten them.
2445 SmallVector<unsigned, 4> RegArgs;
2446 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002447 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2448 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002449 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002450
Chad Rosierc6916f82012-06-12 19:25:13 +00002451 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002452 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosierc6916f82012-06-12 19:25:13 +00002453 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002454
Chad Rosierc6916f82012-06-12 19:25:13 +00002455 unsigned CalleeReg = 0;
2456 if (UseReg) {
2457 if (IntrMemName)
2458 CalleeReg = getLibcallReg(IntrMemName);
2459 else
2460 CalleeReg = getRegForValue(Callee);
2461
Chad Rosier223faf72012-05-23 18:38:57 +00002462 if (CalleeReg == 0) return false;
2463 }
2464
Chad Rosierc6916f82012-06-12 19:25:13 +00002465 // Issue the call.
2466 unsigned CallOpc = ARMSelectCallOp(UseReg);
2467 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2468 DL, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002469
Logan Chien2361f512013-08-22 12:08:04 +00002470 unsigned char OpFlags = 0;
2471
2472 // Add MO_PLT for global address or external symbol in the PIC relocation
2473 // model.
2474 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2475 OpFlags = ARMII::MO_PLT;
2476
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002477 // ARM calls don't take a predicate, but tBL / tBLX do.
2478 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002479 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002480 if (UseReg)
2481 MIB.addReg(CalleeReg);
2482 else if (!IntrMemName)
Logan Chien2361f512013-08-22 12:08:04 +00002483 MIB.addGlobalAddress(GV, 0, OpFlags);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002484 else
Logan Chien2361f512013-08-22 12:08:04 +00002485 MIB.addExternalSymbol(IntrMemName, OpFlags);
Jush Luac96b762012-06-14 06:08:19 +00002486
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002487 // Add implicit physical register uses to the call.
2488 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002489 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002490
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002491 // Add a register mask with the call-preserved registers.
2492 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2493 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2494
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002495 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002496 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002497 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2498 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002499
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002500 // Set all unused physreg defs as dead.
2501 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002502
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002503 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002504}
2505
Chad Rosier057b6d32011-11-14 23:04:09 +00002506bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002507 return Len <= 16;
2508}
2509
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002510bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002511 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002512 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002513 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002514 return false;
2515
Chad Rosierab7223e2011-11-14 22:46:17 +00002516 while (Len) {
2517 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002518 if (!Alignment || Alignment >= 4) {
2519 if (Len >= 4)
2520 VT = MVT::i32;
2521 else if (Len >= 2)
2522 VT = MVT::i16;
2523 else {
2524 assert (Len == 1 && "Expected a length of 1!");
2525 VT = MVT::i8;
2526 }
2527 } else {
2528 // Bound based on alignment.
2529 if (Len >= 2 && Alignment == 2)
2530 VT = MVT::i16;
2531 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002532 VT = MVT::i8;
2533 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002534 }
2535
2536 bool RV;
2537 unsigned ResultReg;
2538 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002539 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002540 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002541 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002542 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002543
2544 unsigned Size = VT.getSizeInBits()/8;
2545 Len -= Size;
2546 Dest.Offset += Size;
2547 Src.Offset += Size;
2548 }
2549
2550 return true;
2551}
2552
Chad Rosiera7ebc562011-11-11 23:31:03 +00002553bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2554 // FIXME: Handle more intrinsics.
2555 switch (I.getIntrinsicID()) {
2556 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002557 case Intrinsic::frameaddress: {
2558 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2559 MFI->setFrameAddressIsTaken(true);
2560
2561 unsigned LdrOpc;
2562 const TargetRegisterClass *RC;
2563 if (isThumb2) {
2564 LdrOpc = ARM::t2LDRi12;
2565 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2566 } else {
2567 LdrOpc = ARM::LDRi12;
2568 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2569 }
2570
2571 const ARMBaseRegisterInfo *RegInfo =
2572 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2573 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2574 unsigned SrcReg = FramePtr;
2575
2576 // Recursively load frame address
2577 // ldr r0 [fp]
2578 // ldr r0 [r0]
2579 // ldr r0 [r0]
2580 // ...
2581 unsigned DestReg;
2582 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2583 while (Depth--) {
2584 DestReg = createResultReg(RC);
2585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2586 TII.get(LdrOpc), DestReg)
2587 .addReg(SrcReg).addImm(0));
2588 SrcReg = DestReg;
2589 }
Chad Rosierf3193242012-06-01 21:12:31 +00002590 UpdateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002591 return true;
2592 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002593 case Intrinsic::memcpy:
2594 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002595 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2596 // Don't handle volatile.
2597 if (MTI.isVolatile())
2598 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002599
2600 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2601 // we would emit dead code because we don't currently handle memmoves.
2602 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2603 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002604 // Small memcpy's are common enough that we want to do them without a call
2605 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002606 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002607 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002608 Address Dest, Src;
2609 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2610 !ARMComputeAddress(MTI.getRawSource(), Src))
2611 return false;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002612 unsigned Alignment = MTI.getAlignment();
2613 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002614 return true;
2615 }
2616 }
Jush Luac96b762012-06-14 06:08:19 +00002617
Chad Rosiera7ebc562011-11-11 23:31:03 +00002618 if (!MTI.getLength()->getType()->isIntegerTy(32))
2619 return false;
Jush Luac96b762012-06-14 06:08:19 +00002620
Chad Rosiera7ebc562011-11-11 23:31:03 +00002621 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2622 return false;
2623
2624 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2625 return SelectCall(&I, IntrMemName);
2626 }
2627 case Intrinsic::memset: {
2628 const MemSetInst &MSI = cast<MemSetInst>(I);
2629 // Don't handle volatile.
2630 if (MSI.isVolatile())
2631 return false;
Jush Luac96b762012-06-14 06:08:19 +00002632
Chad Rosiera7ebc562011-11-11 23:31:03 +00002633 if (!MSI.getLength()->getType()->isIntegerTy(32))
2634 return false;
Jush Luac96b762012-06-14 06:08:19 +00002635
Chad Rosiera7ebc562011-11-11 23:31:03 +00002636 if (MSI.getDestAddressSpace() > 255)
2637 return false;
Jush Luac96b762012-06-14 06:08:19 +00002638
Chad Rosiera7ebc562011-11-11 23:31:03 +00002639 return SelectCall(&I, "memset");
2640 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002641 case Intrinsic::trap: {
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002642 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(
2643 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002644 return true;
2645 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002646 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002647}
2648
Chad Rosieree7e4522011-11-02 00:18:48 +00002649bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002650 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002651 // undefined.
2652 Value *Op = I->getOperand(0);
2653
2654 EVT SrcVT, DestVT;
2655 SrcVT = TLI.getValueType(Op->getType(), true);
2656 DestVT = TLI.getValueType(I->getType(), true);
2657
2658 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2659 return false;
2660 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2661 return false;
2662
2663 unsigned SrcReg = getRegForValue(Op);
2664 if (!SrcReg) return false;
2665
2666 // Because the high bits are undefined, a truncate doesn't generate
2667 // any code.
2668 UpdateValueMap(I, SrcReg);
2669 return true;
2670}
2671
Chad Rosier62a144f2012-12-17 19:59:43 +00002672unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002673 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002674 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002675 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002676 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002677 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002678
2679 // Table of which combinations can be emitted as a single instruction,
2680 // and which will require two.
2681 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2682 // ARM Thumb
2683 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2684 // ext: s z s z s z s z
2685 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2686 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2687 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2688 };
2689
2690 // Target registers for:
2691 // - For ARM can never be PC.
2692 // - For 16-bit Thumb are restricted to lower 8 registers.
2693 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2694 static const TargetRegisterClass *RCTbl[2][2] = {
2695 // Instructions: Two Single
2696 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2697 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2698 };
2699
2700 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002701 static const struct InstructionTable {
2702 uint32_t Opc : 16;
2703 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2704 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2705 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2706 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002707 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002708 { // ARM Opc S Shift Imm
2709 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2710 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2711 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2712 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2713 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2714 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002715 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002716 { // Thumb Opc S Shift Imm
2717 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2718 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2719 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2720 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2721 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2722 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002723 }
2724 },
2725 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002726 { // ARM Opc S Shift Imm
2727 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2728 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2729 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2730 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2731 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2732 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002733 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002734 { // Thumb Opc S Shift Imm
2735 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2736 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2737 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2738 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2739 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2740 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002741 }
2742 }
2743 };
2744
2745 unsigned SrcBits = SrcVT.getSizeInBits();
2746 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002747 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002748 assert((SrcBits < DestBits) && "can only extend to larger types");
2749 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2750 "other sizes unimplemented");
2751 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2752 "other sizes unimplemented");
2753
2754 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002755 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002756 assert((Bitness < 3) && "sanity-check table bounds");
2757
2758 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2759 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002760 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2761 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002762 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002763 unsigned hasS = ITP->hasS;
2764 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2765 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2766 "only MOVsi has shift operand addressing mode");
2767 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002768
2769 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2770 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002771 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002772 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002773 // MOVsi encodes shift and immediate in shift operand addressing mode.
2774 // The following condition has the same value when emitting two
2775 // instruction sequences: both are shifts.
2776 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002777
2778 // Either one or two instructions are emitted.
2779 // They're always of the form:
2780 // dst = in OP imm
2781 // CPSR is set only by 16-bit Thumb instructions.
2782 // Predicate, if any, is AL.
2783 // S bit, if available, is always 0.
2784 // When two are emitted the first's result will feed as the second's input,
2785 // that value is then dead.
2786 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2787 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2788 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002789 bool isLsl = (0 == Instr) && !isSingleInstr;
2790 unsigned Opcode = isLsl ? LSLOpc : Opc;
2791 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2792 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002793 bool isKill = 1 == Instr;
2794 MachineInstrBuilder MIB = BuildMI(
2795 *FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opcode), ResultReg);
2796 if (setsCPSR)
2797 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002798 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002799 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002800 if (hasS)
2801 AddDefaultCC(MIB);
2802 // Second instruction consumes the first's result.
2803 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002804 }
2805
Chad Rosier4489f942011-11-02 17:20:24 +00002806 return ResultReg;
2807}
2808
2809bool ARMFastISel::SelectIntExt(const Instruction *I) {
2810 // On ARM, in general, integer casts don't involve legal types; this code
2811 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002812 Type *DestTy = I->getType();
2813 Value *Src = I->getOperand(0);
2814 Type *SrcTy = Src->getType();
2815
Chad Rosier4489f942011-11-02 17:20:24 +00002816 bool isZExt = isa<ZExtInst>(I);
2817 unsigned SrcReg = getRegForValue(Src);
2818 if (!SrcReg) return false;
2819
Chad Rosier62a144f2012-12-17 19:59:43 +00002820 EVT SrcEVT, DestEVT;
2821 SrcEVT = TLI.getValueType(SrcTy, true);
2822 DestEVT = TLI.getValueType(DestTy, true);
2823 if (!SrcEVT.isSimple()) return false;
2824 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002825
Chad Rosier62a144f2012-12-17 19:59:43 +00002826 MVT SrcVT = SrcEVT.getSimpleVT();
2827 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002828 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2829 if (ResultReg == 0) return false;
2830 UpdateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002831 return true;
2832}
2833
Jush Lu4705da92012-08-03 02:37:48 +00002834bool ARMFastISel::SelectShift(const Instruction *I,
2835 ARM_AM::ShiftOpc ShiftTy) {
2836 // We handle thumb2 mode by target independent selector
2837 // or SelectionDAG ISel.
2838 if (isThumb2)
2839 return false;
2840
2841 // Only handle i32 now.
2842 EVT DestVT = TLI.getValueType(I->getType(), true);
2843 if (DestVT != MVT::i32)
2844 return false;
2845
2846 unsigned Opc = ARM::MOVsr;
2847 unsigned ShiftImm;
2848 Value *Src2Value = I->getOperand(1);
2849 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2850 ShiftImm = CI->getZExtValue();
2851
2852 // Fall back to selection DAG isel if the shift amount
2853 // is zero or greater than the width of the value type.
2854 if (ShiftImm == 0 || ShiftImm >=32)
2855 return false;
2856
2857 Opc = ARM::MOVsi;
2858 }
2859
2860 Value *Src1Value = I->getOperand(0);
2861 unsigned Reg1 = getRegForValue(Src1Value);
2862 if (Reg1 == 0) return false;
2863
Nadav Rotema8e15b02012-09-06 11:13:55 +00002864 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002865 if (Opc == ARM::MOVsr) {
2866 Reg2 = getRegForValue(Src2Value);
2867 if (Reg2 == 0) return false;
2868 }
2869
JF Bastien13969d02013-05-29 15:45:47 +00002870 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002871 if(ResultReg == 0) return false;
2872
2873 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2874 TII.get(Opc), ResultReg)
2875 .addReg(Reg1);
2876
2877 if (Opc == ARM::MOVsi)
2878 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2879 else if (Opc == ARM::MOVsr) {
2880 MIB.addReg(Reg2);
2881 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2882 }
2883
2884 AddOptionalDefs(MIB);
2885 UpdateValueMap(I, ResultReg);
2886 return true;
2887}
2888
Eric Christopherc3e118e2010-09-02 23:43:26 +00002889// TODO: SoftFP support.
Eric Christopher84bdfd82010-07-21 22:26:11 +00002890bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002891
Eric Christopher84bdfd82010-07-21 22:26:11 +00002892 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002893 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002894 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002895 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002896 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002897 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002898 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002899 case Instruction::IndirectBr:
2900 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002901 case Instruction::ICmp:
2902 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002903 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002904 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002905 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002906 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002907 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002908 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002909 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002910 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002911 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002912 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002913 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002914 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002915 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002916 case Instruction::Add:
2917 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002918 case Instruction::Or:
2919 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002920 case Instruction::Sub:
2921 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002922 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002923 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002924 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002925 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002926 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002927 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002928 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002929 return SelectDiv(I, /*isSigned*/ true);
2930 case Instruction::UDiv:
2931 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002932 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002933 return SelectRem(I, /*isSigned*/ true);
2934 case Instruction::URem:
2935 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002936 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002937 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2938 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002939 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002940 case Instruction::Select:
2941 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002942 case Instruction::Ret:
2943 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002944 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002945 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002946 case Instruction::ZExt:
2947 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002948 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002949 case Instruction::Shl:
2950 return SelectShift(I, ARM_AM::lsl);
2951 case Instruction::LShr:
2952 return SelectShift(I, ARM_AM::lsr);
2953 case Instruction::AShr:
2954 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002955 default: break;
2956 }
2957 return false;
2958}
2959
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002960namespace {
2961// This table describes sign- and zero-extend instructions which can be
2962// folded into a preceding load. All of these extends have an immediate
2963// (sometimes a mask and sometimes a shift) that's applied after
2964// extension.
2965const struct FoldableLoadExtendsStruct {
2966 uint16_t Opc[2]; // ARM, Thumb.
2967 uint8_t ExpectedImm;
2968 uint8_t isZExt : 1;
2969 uint8_t ExpectedVT : 7;
2970} FoldableLoadExtends[] = {
2971 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2972 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2973 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2974 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2975 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2976};
2977}
2978
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002979/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002980/// vreg is being provided by the specified load instruction. If possible,
2981/// try to fold the load as an operand to the instruction, returning true if
2982/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002983bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2984 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002985 // Verify we have a legal type before going any further.
2986 MVT VT;
2987 if (!isLoadTypeLegal(LI->getType(), VT))
2988 return false;
2989
2990 // Combine load followed by zero- or sign-extend.
2991 // ldrb r1, [r0] ldrb r1, [r0]
2992 // uxtb r2, r1 =>
2993 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002994 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2995 return false;
2996 const uint64_t Imm = MI->getOperand(2).getImm();
2997
2998 bool Found = false;
2999 bool isZExt;
3000 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
3001 i != e; ++i) {
3002 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
3003 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
3004 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
3005 Found = true;
3006 isZExt = FoldableLoadExtends[i].isZExt;
3007 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003008 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00003009 if (!Found) return false;
3010
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003011 // See if we can handle this address.
3012 Address Addr;
3013 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00003014
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003015 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00003016 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003017 return false;
3018 MI->eraseFromParent();
3019 return true;
3020}
3021
Jush Lu47172a02012-09-27 05:21:41 +00003022unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003023 unsigned Align, MVT VT) {
Jush Lu47172a02012-09-27 05:21:41 +00003024 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
3025 ARMConstantPoolConstant *CPV =
3026 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
3027 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
3028
3029 unsigned Opc;
3030 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
3031 // Load value.
3032 if (isThumb2) {
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003033 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00003034 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
3035 TII.get(ARM::t2LDRpci), DestReg1)
3036 .addConstantPoolIndex(Idx));
3037 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
3038 } else {
3039 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003040 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00003041 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
3042 DL, TII.get(ARM::LDRcp), DestReg1)
3043 .addConstantPoolIndex(Idx).addImm(0));
3044 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
3045 }
3046
3047 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
3048 if (GlobalBaseReg == 0) {
3049 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
3050 AFI->setGlobalBaseReg(GlobalBaseReg);
3051 }
3052
3053 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003054 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
3055 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
3056 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
Jush Lu47172a02012-09-27 05:21:41 +00003057 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
3058 DL, TII.get(Opc), DestReg2)
3059 .addReg(DestReg1)
3060 .addReg(GlobalBaseReg);
3061 if (!UseGOTOFF)
3062 MIB.addImm(0);
3063 AddOptionalDefs(MIB);
3064
3065 return DestReg2;
3066}
3067
Evan Cheng615620c2013-02-11 01:27:15 +00003068bool ARMFastISel::FastLowerArguments() {
3069 if (!FuncInfo.CanLowerReturn)
3070 return false;
3071
3072 const Function *F = FuncInfo.Fn;
3073 if (F->isVarArg())
3074 return false;
3075
3076 CallingConv::ID CC = F->getCallingConv();
3077 switch (CC) {
3078 default:
3079 return false;
3080 case CallingConv::Fast:
3081 case CallingConv::C:
3082 case CallingConv::ARM_AAPCS_VFP:
3083 case CallingConv::ARM_AAPCS:
3084 case CallingConv::ARM_APCS:
3085 break;
3086 }
3087
3088 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3089 // which are passed in r0 - r3.
3090 unsigned Idx = 1;
3091 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3092 I != E; ++I, ++Idx) {
3093 if (Idx > 4)
3094 return false;
3095
3096 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3097 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3098 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3099 return false;
3100
3101 Type *ArgTy = I->getType();
3102 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3103 return false;
3104
3105 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003106 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003107 switch (ArgVT.getSimpleVT().SimpleTy) {
3108 case MVT::i8:
3109 case MVT::i16:
3110 case MVT::i32:
3111 break;
3112 default:
3113 return false;
3114 }
3115 }
3116
3117
3118 static const uint16_t GPRArgRegs[] = {
3119 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3120 };
3121
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003122 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003123 Idx = 0;
3124 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3125 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003126 unsigned SrcReg = GPRArgRegs[Idx];
3127 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3128 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3129 // Without this, EmitLiveInCopies may eliminate the livein if its only
3130 // use is a bitcast (which isn't turned into an instruction).
3131 unsigned ResultReg = createResultReg(RC);
3132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
3133 ResultReg).addReg(DstReg, getKillRegState(true));
3134 UpdateValueMap(I, ResultReg);
3135 }
3136
3137 return true;
3138}
3139
Eric Christopher84bdfd82010-07-21 22:26:11 +00003140namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003141 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3142 const TargetLibraryInfo *libInfo) {
Eric Christopher5501b7e2010-10-11 20:05:22 +00003143 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach68147ee2010-11-09 19:22:26 +00003144
Eric Christopher5501b7e2010-10-11 20:05:22 +00003145 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastien18db1f22013-06-14 02:49:43 +00003146 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3147 bool UseFastISel = false;
Tim Northoverd6a729b2014-01-06 14:28:05 +00003148 UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
JF Bastien18db1f22013-06-14 02:49:43 +00003149 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3150 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3151
3152 if (UseFastISel) {
3153 // iOS always has a FP for backtracking, force other targets
3154 // to keep their FP when doing FastISel. The emitted code is
3155 // currently superior, and in cases like test-suite's lencod
3156 // FastISel isn't quite correct when FP is eliminated.
3157 TM.Options.NoFramePointerElim = true;
Bob Wilson3e6fa462012-08-03 04:06:28 +00003158 return new ARMFastISel(funcInfo, libInfo);
JF Bastien18db1f22013-06-14 02:49:43 +00003159 }
Evan Cheng23b05d12010-07-26 18:32:55 +00003160 return 0;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003161 }
3162}