blob: 5bb4ba9bd2d20c686d68e1c162ae6436b136a8cd [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));
Eric Christopherc3e118e2010-09-02 23:43:26 +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));
Eric Christopher2ff757d2010-09-09 01:06:51 +0000663
Eric Christopherc3e118e2010-09-02 23:43:26 +0000664 return DestReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000665}
666
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000667unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000668 // For now 32-bit only.
Duncan Sands14627772010-11-03 12:17:33 +0000669 if (VT != MVT::i32) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000670
Eric Christopher7787f792010-10-02 00:32:44 +0000671 Reloc::Model RelocM = TM.getRelocationModel();
Jush Lue87e5592012-08-29 02:41:21 +0000672 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier65710a72012-11-07 00:13:01 +0000673 const TargetRegisterClass *RC = isThumb2 ?
674 (const TargetRegisterClass*)&ARM::rGPRRegClass :
675 (const TargetRegisterClass*)&ARM::GPRRegClass;
676 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000677
JF Bastien18db1f22013-06-14 02:49:43 +0000678 // FastISel TLS support on non-Darwin is broken, punt to SelectionDAG.
679 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
680 bool IsThreadLocal = GVar && GVar->isThreadLocal();
681 if (!Subtarget->isTargetDarwin() && IsThreadLocal) return 0;
682
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000683 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000684 // Non-darwin targets only support static movt relocations in FastISel.
Jakob Stoklund Olesen083dbdc2012-01-07 20:49:15 +0000685 if (Subtarget->useMovt() &&
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000686 (Subtarget->isTargetDarwin() || RelocM == Reloc::Static)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000687 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000688 unsigned char TF = 0;
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000689 if (Subtarget->isTargetDarwin())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000690 TF = ARMII::MO_NONLAZY;
691
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000692 switch (RelocM) {
693 case Reloc::PIC_:
694 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
695 break;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000696 default:
697 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
698 break;
699 }
700 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000701 DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000702 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000703 // MachineConstantPool wants an explicit alignment.
704 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
705 if (Align == 0) {
706 // TODO: Figure out if this is correct.
707 Align = TD.getTypeAllocSize(GV->getType());
708 }
709
Jush Lu47172a02012-09-27 05:21:41 +0000710 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
711 return ARMLowerPICELF(GV, Align, VT);
712
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000713 // Grab index.
714 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
715 (Subtarget->isThumb() ? 4 : 8);
716 unsigned Id = AFI->createPICLabelUId();
717 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
718 ARMCP::CPValue,
719 PCAdj);
720 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
721
722 // Load value.
723 MachineInstrBuilder MIB;
724 if (isThumb2) {
725 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
726 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
727 .addConstantPoolIndex(Idx);
728 if (RelocM == Reloc::PIC_)
729 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000730 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000731 } else {
732 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000733 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000734 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
735 DestReg)
736 .addConstantPoolIndex(Idx)
737 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000738 AddOptionalDefs(MIB);
739
740 if (RelocM == Reloc::PIC_) {
741 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
742 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
743
744 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
745 DL, TII.get(Opc), NewDestReg)
746 .addReg(DestReg)
747 .addImm(Id);
748 AddOptionalDefs(MIB);
749 return NewDestReg;
750 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000751 }
Eric Christopher7787f792010-10-02 00:32:44 +0000752 }
Eli Friedman86585792011-06-03 01:13:19 +0000753
Jush Lue87e5592012-08-29 02:41:21 +0000754 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000755 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000756 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000757 if (isThumb2)
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000758 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
759 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000760 .addReg(DestReg)
761 .addImm(0);
762 else
763 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
764 NewDestReg)
765 .addReg(DestReg)
766 .addImm(0);
767 DestReg = NewDestReg;
768 AddOptionalDefs(MIB);
769 }
770
Eric Christopher7787f792010-10-02 00:32:44 +0000771 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000772}
773
Eric Christopher3cf63f12010-09-09 00:19:41 +0000774unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglundc494d242012-12-17 14:30:06 +0000775 EVT CEVT = TLI.getValueType(C->getType(), true);
776
777 // Only handle simple types.
778 if (!CEVT.isSimple()) return 0;
779 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000780
781 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
782 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000783 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
784 return ARMMaterializeGV(GV, VT);
785 else if (isa<ConstantInt>(C))
786 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000787
Eric Christopher83a5ec82010-10-01 23:24:42 +0000788 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000789}
790
Chad Rosier0eff3e52011-11-17 21:46:13 +0000791// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
792
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000793unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
794 // Don't handle dynamic allocas.
795 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000796
Duncan Sandsf5dda012010-11-03 11:35:31 +0000797 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000798 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000799
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000800 DenseMap<const AllocaInst*, int>::iterator SI =
801 FuncInfo.StaticAllocaMap.find(AI);
802
803 // This will get lowered later into the correct offsets and registers
804 // via rewriteXFrameIndex.
805 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000806 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000807 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000808 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000809 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
810
Evan Cheng7fae11b2011-12-14 02:11:42 +0000811 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000812 TII.get(Opc), ResultReg)
813 .addFrameIndex(SI->second)
814 .addImm(0));
815 return ResultReg;
816 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000817
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000818 return 0;
819}
820
Chris Lattner229907c2011-07-18 04:54:35 +0000821bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000822 EVT evt = TLI.getValueType(Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000823
Eric Christopher761e7fb2010-08-25 07:23:49 +0000824 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000825 if (evt == MVT::Other || !evt.isSimple()) return false;
826 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000827
Eric Christopher901176a2010-08-31 01:28:42 +0000828 // Handle all legal types, i.e. a register that will directly hold this
829 // value.
830 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000831}
832
Chris Lattner229907c2011-07-18 04:54:35 +0000833bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000834 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000835
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000836 // If this is a type than can be sign or zero-extended to a basic operation
837 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000838 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000839 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000840
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000841 return false;
842}
843
Eric Christopher558b61e2010-11-19 22:36:41 +0000844// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000845bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000846 // Some boilerplate from the X86 FastISel.
847 const User *U = NULL;
Eric Christopher00202ee2010-08-23 21:44:12 +0000848 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000849 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000850 // Don't walk into other basic blocks unless the object is an alloca from
851 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000852 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
853 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
854 Opcode = I->getOpcode();
855 U = I;
856 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000857 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000858 Opcode = C->getOpcode();
859 U = C;
860 }
861
Chris Lattner229907c2011-07-18 04:54:35 +0000862 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000863 if (Ty->getAddressSpace() > 255)
864 // Fast instruction selection doesn't support the special
865 // address spaces.
866 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000867
Eric Christopher00202ee2010-08-23 21:44:12 +0000868 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000869 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000870 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000871 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000872 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000873 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000874 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000875 // Look past no-op inttoptrs.
876 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000877 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000878 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000879 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000880 // Look past no-op ptrtoints.
881 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000882 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000883 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000884 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000885 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000886 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000887
Eric Christopher21d0c172010-10-14 09:29:41 +0000888 // Iterate through the GEP folding the constants into offsets where
889 // we can.
890 gep_type_iterator GTI = gep_type_begin(U);
891 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
892 i != e; ++i, ++GTI) {
893 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000894 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Eric Christopher21d0c172010-10-14 09:29:41 +0000895 const StructLayout *SL = TD.getStructLayout(STy);
896 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
897 TmpOffset += SL->getElementOffset(Idx);
898 } else {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000899 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000900 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000901 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
902 // Constant-offset addressing.
903 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000904 break;
905 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000906 if (canFoldAddIntoGEP(U, Op)) {
907 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000908 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000909 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000910 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000911 // Iterate on the other operand.
912 Op = cast<AddOperator>(Op)->getOperand(0);
913 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000914 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000915 // Unsupported
916 goto unsupported_gep;
917 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000918 }
919 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000920
921 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000922 Addr.Offset = TmpOffset;
923 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000924
925 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000926 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000927
Eric Christopher21d0c172010-10-14 09:29:41 +0000928 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000929 break;
930 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000931 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000932 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000933 DenseMap<const AllocaInst*, int>::iterator SI =
934 FuncInfo.StaticAllocaMap.find(AI);
935 if (SI != FuncInfo.StaticAllocaMap.end()) {
936 Addr.BaseType = Address::FrameIndexBase;
937 Addr.Base.FI = SI->second;
938 return true;
939 }
940 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000941 }
942 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000943
Eric Christopher9d4e4712010-08-24 00:07:24 +0000944 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000945 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
946 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000947}
948
Chad Rosier150d35b2012-12-17 22:35:29 +0000949void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000950 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000951 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000952 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000953 case MVT::i1:
954 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000955 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000956 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000957 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000958 // Integer loads/stores handle 12-bit offsets.
959 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000960 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000961 if (needsLowering && isThumb2)
962 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
963 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000964 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000965 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000966 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000967 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000968 break;
969 case MVT::f32:
970 case MVT::f64:
971 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000972 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000973 break;
974 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000975
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000976 // If this is a stack pointer and the offset needs to be simplified then
977 // put the alloca address into a register, set the base type back to
978 // register and continue. This should almost never happen.
979 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topperc7242e02012-04-20 07:30:17 +0000980 const TargetRegisterClass *RC = isThumb2 ?
981 (const TargetRegisterClass*)&ARM::tGPRRegClass :
982 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000983 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000984 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000985 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000986 TII.get(Opc), ResultReg)
987 .addFrameIndex(Addr.Base.FI)
988 .addImm(0));
989 Addr.Base.Reg = ResultReg;
990 Addr.BaseType = Address::RegBase;
991 }
992
Eric Christopher73bc5b02010-10-21 19:40:30 +0000993 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +0000994 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +0000995 if (needsLowering) {
Eli Friedman86caced2011-04-29 21:22:56 +0000996 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
997 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +0000998 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +0000999 }
Eric Christopher00202ee2010-08-23 21:44:12 +00001000}
1001
Chad Rosier150d35b2012-12-17 22:35:29 +00001002void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +00001003 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001004 unsigned Flags, bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001005 // addrmode5 output depends on the selection dag addressing dividing the
1006 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +00001007 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +00001008 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +00001009
Eric Christopher119ff7f2010-12-01 01:40:24 +00001010 // Frame base works a bit differently. Handle it separately.
1011 if (Addr.BaseType == Address::FrameIndexBase) {
1012 int FI = Addr.Base.FI;
1013 int Offset = Addr.Offset;
1014 MachineMemOperand *MMO =
1015 FuncInfo.MF->getMachineMemOperand(
1016 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarich6528a542011-05-28 20:34:49 +00001017 Flags,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001018 MFI.getObjectSize(FI),
1019 MFI.getObjectAlignment(FI));
1020 // Now add the rest of the operands.
1021 MIB.addFrameIndex(FI);
1022
Bob Wilson80381f62011-12-04 00:52:23 +00001023 // ARM halfword load/stores and signed byte loads need an additional
1024 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001025 if (useAM3) {
1026 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1027 MIB.addReg(0);
1028 MIB.addImm(Imm);
1029 } else {
1030 MIB.addImm(Addr.Offset);
1031 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001032 MIB.addMemOperand(MMO);
1033 } else {
1034 // Now add the rest of the operands.
1035 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +00001036
Bob Wilson80381f62011-12-04 00:52:23 +00001037 // ARM halfword load/stores and signed byte loads need an additional
1038 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001039 if (useAM3) {
1040 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1041 MIB.addReg(0);
1042 MIB.addImm(Imm);
1043 } else {
1044 MIB.addImm(Addr.Offset);
1045 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001046 }
1047 AddOptionalDefs(MIB);
1048}
1049
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001050bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +00001051 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +00001052 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001053 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +00001054 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +00001055 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001056 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001057 // This is mostly going to be Neon/vector support.
1058 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +00001059 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001060 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001061 if (isThumb2) {
1062 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1063 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1064 else
1065 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001066 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +00001067 if (isZExt) {
1068 Opc = ARM::LDRBi12;
1069 } else {
1070 Opc = ARM::LDRSB;
1071 useAM3 = true;
1072 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001073 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001074 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001075 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001076 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001077 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001078 return false;
1079
Chad Rosieradfd2002011-11-14 20:22:27 +00001080 if (isThumb2) {
1081 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1082 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1083 else
1084 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1085 } else {
1086 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1087 useAM3 = true;
1088 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001089 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001090 break;
Eric Christopher901176a2010-08-31 01:28:42 +00001091 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001092 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001093 return false;
1094
Chad Rosieradfd2002011-11-14 20:22:27 +00001095 if (isThumb2) {
1096 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1097 Opc = ARM::t2LDRi8;
1098 else
1099 Opc = ARM::t2LDRi12;
1100 } else {
1101 Opc = ARM::LDRi12;
1102 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001103 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +00001104 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +00001105 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +00001106 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +00001107 // Unaligned loads need special handling. Floats require word-alignment.
1108 if (Alignment && Alignment < 4) {
1109 needVMOV = true;
1110 VT = MVT::i32;
1111 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +00001112 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +00001113 } else {
1114 Opc = ARM::VLDRS;
1115 RC = TLI.getRegClassFor(VT);
1116 }
Eric Christopheraef6499b2010-09-18 01:59:37 +00001117 break;
1118 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +00001119 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001120 // FIXME: Unaligned loads need special handling. Doublewords require
1121 // word-alignment.
1122 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +00001123 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001124
Eric Christopheraef6499b2010-09-18 01:59:37 +00001125 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +00001126 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +00001127 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001128 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001129 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001130 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001131
Eric Christopher119ff7f2010-12-01 01:40:24 +00001132 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001133 if (allocReg)
1134 ResultReg = createResultReg(RC);
1135 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Eric Christopher119ff7f2010-12-01 01:40:24 +00001136 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1137 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001138 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001139
1140 // If we had an unaligned load of a float we've converted it to an regular
1141 // load. Now we must move from the GRP to the FP register.
1142 if (needVMOV) {
1143 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1144 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1145 TII.get(ARM::VMOVSR), MoveReg)
1146 .addReg(ResultReg));
1147 ResultReg = MoveReg;
1148 }
Eric Christopher901176a2010-08-31 01:28:42 +00001149 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001150}
1151
Eric Christopher29ab6d12010-09-27 06:02:23 +00001152bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001153 // Atomic loads need special handling.
1154 if (cast<LoadInst>(I)->isAtomic())
1155 return false;
1156
Eric Christopher860fc932010-09-10 00:34:35 +00001157 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001158 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001159 if (!isLoadTypeLegal(I->getType(), VT))
1160 return false;
1161
Eric Christopher119ff7f2010-12-01 01:40:24 +00001162 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001163 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001164 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001165
1166 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001167 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1168 return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001169 UpdateValueMap(I, ResultReg);
1170 return true;
1171}
1172
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001173bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001174 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001175 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001176 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001177 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001178 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001179 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001180 case MVT::i1: {
Craig Topperc7242e02012-04-20 07:30:17 +00001181 unsigned Res = createResultReg(isThumb2 ?
1182 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1183 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001184 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001185 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Eric Christopher1e43892e2010-11-02 23:59:09 +00001186 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1187 TII.get(Opc), Res)
1188 .addReg(SrcReg).addImm(1));
1189 SrcReg = Res;
1190 } // Fallthrough here.
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001191 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001192 if (isThumb2) {
1193 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1194 StrOpc = ARM::t2STRBi8;
1195 else
1196 StrOpc = ARM::t2STRBi12;
1197 } else {
1198 StrOpc = ARM::STRBi12;
1199 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001200 break;
1201 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001202 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001203 return false;
1204
Chad Rosieradfd2002011-11-14 20:22:27 +00001205 if (isThumb2) {
1206 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1207 StrOpc = ARM::t2STRHi8;
1208 else
1209 StrOpc = ARM::t2STRHi12;
1210 } else {
1211 StrOpc = ARM::STRH;
1212 useAM3 = true;
1213 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001214 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001215 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001216 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001217 return false;
1218
Chad Rosieradfd2002011-11-14 20:22:27 +00001219 if (isThumb2) {
1220 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1221 StrOpc = ARM::t2STRi8;
1222 else
1223 StrOpc = ARM::t2STRi12;
1224 } else {
1225 StrOpc = ARM::STRi12;
1226 }
Eric Christopherc918d552010-10-16 01:10:35 +00001227 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001228 case MVT::f32:
1229 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001230 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001231 if (Alignment && Alignment < 4) {
1232 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1234 TII.get(ARM::VMOVRS), MoveReg)
1235 .addReg(SrcReg));
1236 SrcReg = MoveReg;
1237 VT = MVT::i32;
1238 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001239 } else {
1240 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001241 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001242 break;
1243 case MVT::f64:
1244 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001245 // FIXME: Unaligned stores need special handling. Doublewords require
1246 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001247 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001248 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001249
Eric Christopherc3e118e2010-09-02 23:43:26 +00001250 StrOpc = ARM::VSTRD;
1251 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001252 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001253 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001254 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001255
Eric Christopher119ff7f2010-12-01 01:40:24 +00001256 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001257 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001258 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1259 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001260 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001261 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001262 return true;
1263}
1264
Eric Christopher29ab6d12010-09-27 06:02:23 +00001265bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001266 Value *Op0 = I->getOperand(0);
1267 unsigned SrcReg = 0;
1268
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001269 // Atomic stores need special handling.
1270 if (cast<StoreInst>(I)->isAtomic())
1271 return false;
1272
Eric Christopher119ff7f2010-12-01 01:40:24 +00001273 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001274 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001275 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001276 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001277
Eric Christopher92db2012010-09-02 01:48:11 +00001278 // Get the value to be stored into a register.
1279 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001280 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001281
Eric Christopher119ff7f2010-12-01 01:40:24 +00001282 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001283 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001284 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001285 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001286
Chad Rosierec3b77e2011-12-03 02:21:57 +00001287 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1288 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001289 return true;
1290}
1291
1292static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1293 switch (Pred) {
1294 // Needs two compares...
1295 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001296 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001297 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001298 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001299 return ARMCC::AL;
1300 case CmpInst::ICMP_EQ:
1301 case CmpInst::FCMP_OEQ:
1302 return ARMCC::EQ;
1303 case CmpInst::ICMP_SGT:
1304 case CmpInst::FCMP_OGT:
1305 return ARMCC::GT;
1306 case CmpInst::ICMP_SGE:
1307 case CmpInst::FCMP_OGE:
1308 return ARMCC::GE;
1309 case CmpInst::ICMP_UGT:
1310 case CmpInst::FCMP_UGT:
1311 return ARMCC::HI;
1312 case CmpInst::FCMP_OLT:
1313 return ARMCC::MI;
1314 case CmpInst::ICMP_ULE:
1315 case CmpInst::FCMP_OLE:
1316 return ARMCC::LS;
1317 case CmpInst::FCMP_ORD:
1318 return ARMCC::VC;
1319 case CmpInst::FCMP_UNO:
1320 return ARMCC::VS;
1321 case CmpInst::FCMP_UGE:
1322 return ARMCC::PL;
1323 case CmpInst::ICMP_SLT:
1324 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001325 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001326 case CmpInst::ICMP_SLE:
1327 case CmpInst::FCMP_ULE:
1328 return ARMCC::LE;
1329 case CmpInst::FCMP_UNE:
1330 case CmpInst::ICMP_NE:
1331 return ARMCC::NE;
1332 case CmpInst::ICMP_UGE:
1333 return ARMCC::HS;
1334 case CmpInst::ICMP_ULT:
1335 return ARMCC::LO;
1336 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001337}
1338
Eric Christopher29ab6d12010-09-27 06:02:23 +00001339bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001340 const BranchInst *BI = cast<BranchInst>(I);
1341 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1342 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001343
Eric Christopher6aaed722010-09-03 00:35:47 +00001344 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001345
Eric Christopher5c308f82010-10-29 21:08:19 +00001346 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1347 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001348 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001349 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001350
1351 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001352 // Try to take advantage of fallthrough opportunities.
1353 CmpInst::Predicate Predicate = CI->getPredicate();
1354 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1355 std::swap(TBB, FBB);
1356 Predicate = CmpInst::getInversePredicate(Predicate);
1357 }
1358
1359 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001360
1361 // We may not handle every CC for now.
1362 if (ARMPred == ARMCC::AL) return false;
1363
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001364 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001365 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001366 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001367
Chad Rosier0439cfc2011-11-08 21:12:00 +00001368 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher5c308f82010-10-29 21:08:19 +00001369 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1370 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1371 FastEmitBranch(FBB, DL);
1372 FuncInfo.MBB->addSuccessor(TBB);
1373 return true;
1374 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001375 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1376 MVT SourceVT;
1377 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001378 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001379 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001380 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001381 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Eric Christopher8d46b472011-04-29 20:02:39 +00001382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1383 TII.get(TstOpc))
1384 .addReg(OpReg).addImm(1));
1385
1386 unsigned CCMode = ARMCC::NE;
1387 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1388 std::swap(TBB, FBB);
1389 CCMode = ARMCC::EQ;
1390 }
1391
Chad Rosier0439cfc2011-11-08 21:12:00 +00001392 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher8d46b472011-04-29 20:02:39 +00001393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1394 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1395
1396 FastEmitBranch(FBB, DL);
1397 FuncInfo.MBB->addSuccessor(TBB);
1398 return true;
1399 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001400 } else if (const ConstantInt *CI =
1401 dyn_cast<ConstantInt>(BI->getCondition())) {
1402 uint64_t Imm = CI->getZExtValue();
1403 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1404 FastEmitBranch(Target, DL);
1405 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001406 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001407
Eric Christopher5c308f82010-10-29 21:08:19 +00001408 unsigned CmpReg = getRegForValue(BI->getCondition());
1409 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001410
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001411 // We've been divorced from our compare! Our block was split, and
1412 // now our compare lives in a predecessor block. We musn't
1413 // re-compare here, as the children of the compare aren't guaranteed
1414 // live across the block boundary (we *could* check for this).
1415 // Regardless, the compare has been done in the predecessor block,
1416 // and it left a value for us in a virtual register. Ergo, we test
1417 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001418 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001419 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1421 .addReg(CmpReg).addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001422
Eric Christopher4f012fd2011-04-28 16:52:09 +00001423 unsigned CCMode = ARMCC::NE;
1424 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1425 std::swap(TBB, FBB);
1426 CCMode = ARMCC::EQ;
1427 }
1428
Chad Rosier0439cfc2011-11-08 21:12:00 +00001429 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Eric Christopher6aaed722010-09-03 00:35:47 +00001430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001431 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Eric Christopher6aaed722010-09-03 00:35:47 +00001432 FastEmitBranch(FBB, DL);
1433 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001434 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001435}
1436
Chad Rosierded4c992012-02-07 23:56:08 +00001437bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1438 unsigned AddrReg = getRegForValue(I->getOperand(0));
1439 if (AddrReg == 0) return false;
1440
1441 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
1443 .addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001444
1445 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1446 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1447 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1448
Jush Luac96b762012-06-14 06:08:19 +00001449 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001450}
1451
Chad Rosier9cf803c2011-11-02 18:08:25 +00001452bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1453 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001454 Type *Ty = Src1Value->getType();
Patrik Hagglundc494d242012-12-17 14:30:06 +00001455 EVT SrcEVT = TLI.getValueType(Ty, true);
1456 if (!SrcEVT.isSimple()) return false;
1457 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001458
Chad Rosier78127d32011-10-26 23:25:44 +00001459 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1460 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001461 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001462
Chad Rosier595d4192011-11-09 03:22:02 +00001463 // Check to see if the 2nd operand is a constant that we can encode directly
1464 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001465 int Imm = 0;
1466 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001467 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001468 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1469 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001470 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1471 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1472 SrcVT == MVT::i1) {
1473 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001474 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001475 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1476 // then a cmn, because there is no way to represent 2147483648 as a
1477 // signed 32-bit int.
1478 if (Imm < 0 && Imm != (int)0x80000000) {
1479 isNegativeImm = true;
1480 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001481 }
Chad Rosier26d05882012-03-15 22:54:20 +00001482 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1483 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001484 }
1485 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1486 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1487 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001488 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001489 }
1490
Eric Christopherc3e9c402010-09-08 23:13:45 +00001491 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001492 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001493 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001494 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001495 default: return false;
1496 // TODO: Verify compares.
1497 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001498 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001499 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001500 break;
1501 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001502 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001503 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001504 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001505 case MVT::i1:
1506 case MVT::i8:
1507 case MVT::i16:
1508 needsExt = true;
1509 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001510 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001511 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001512 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001513 CmpOpc = ARM::t2CMPrr;
1514 else
Bill Wendling4b796472012-06-11 08:07:26 +00001515 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001516 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001517 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001518 CmpOpc = ARM::CMPrr;
1519 else
Bill Wendling4b796472012-06-11 08:07:26 +00001520 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001521 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001522 break;
1523 }
1524
Chad Rosier9cf803c2011-11-02 18:08:25 +00001525 unsigned SrcReg1 = getRegForValue(Src1Value);
1526 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001527
Duncan Sands12330652011-11-28 10:31:27 +00001528 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001529 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001530 SrcReg2 = getRegForValue(Src2Value);
1531 if (SrcReg2 == 0) return false;
1532 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001533
1534 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1535 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001536 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1537 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001538 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001539 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1540 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001541 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001542 }
Chad Rosier59a20192011-10-26 22:47:55 +00001543
Jim Grosbachd7866792013-08-16 23:37:40 +00001544 const MCInstrDesc &II = TII.get(CmpOpc);
1545 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001546 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001547 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
1548 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001549 .addReg(SrcReg1).addReg(SrcReg2));
1550 } else {
1551 MachineInstrBuilder MIB;
Jim Grosbachd7866792013-08-16 23:37:40 +00001552 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001553 .addReg(SrcReg1);
1554
1555 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1556 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001557 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001558 AddOptionalDefs(MIB);
1559 }
Chad Rosier78127d32011-10-26 23:25:44 +00001560
1561 // For floating point we need to move the result to a comparison register
1562 // that we can then use for branches.
1563 if (Ty->isFloatTy() || Ty->isDoubleTy())
1564 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1565 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001566 return true;
1567}
1568
1569bool ARMFastISel::SelectCmp(const Instruction *I) {
1570 const CmpInst *CI = cast<CmpInst>(I);
1571
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001572 // Get the compare predicate.
1573 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001574
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001575 // We may not handle every CC for now.
1576 if (ARMPred == ARMCC::AL) return false;
1577
Chad Rosier59a20192011-10-26 22:47:55 +00001578 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001579 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001580 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001581
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001582 // Now set a register based on the comparison. Explicitly set the predicates
1583 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001584 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topperc7242e02012-04-20 07:30:17 +00001585 const TargetRegisterClass *RC = isThumb2 ?
1586 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1587 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001588 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001589 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001590 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001591 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1593 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001594 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001595
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001596 UpdateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001597 return true;
1598}
1599
Eric Christopher29ab6d12010-09-27 06:02:23 +00001600bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001601 // Make sure we have VFP and that we're extending float to double.
1602 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001603
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001604 Value *V = I->getOperand(0);
1605 if (!I->getType()->isDoubleTy() ||
1606 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001607
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001608 unsigned Op = getRegForValue(V);
1609 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001610
Craig Topperc7242e02012-04-20 07:30:17 +00001611 unsigned Result = createResultReg(&ARM::DPRRegClass);
Eric Christopher2ff757d2010-09-09 01:06:51 +00001612 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher82b05d72010-09-09 20:36:19 +00001613 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001614 .addReg(Op));
1615 UpdateValueMap(I, Result);
1616 return true;
1617}
1618
Eric Christopher29ab6d12010-09-27 06:02:23 +00001619bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001620 // Make sure we have VFP and that we're truncating double to float.
1621 if (!Subtarget->hasVFP2()) return false;
1622
1623 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001624 if (!(I->getType()->isFloatTy() &&
1625 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001626
1627 unsigned Op = getRegForValue(V);
1628 if (Op == 0) return false;
1629
Craig Topperc7242e02012-04-20 07:30:17 +00001630 unsigned Result = createResultReg(&ARM::SPRRegClass);
Eric Christopher5903c0b2010-09-09 20:26:31 +00001631 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher82b05d72010-09-09 20:36:19 +00001632 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001633 .addReg(Op));
1634 UpdateValueMap(I, Result);
1635 return true;
1636}
1637
Chad Rosiere023d5d2012-02-03 21:14:11 +00001638bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001639 // Make sure we have VFP.
1640 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001641
Duncan Sandsf5dda012010-11-03 11:35:31 +00001642 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001643 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001644 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001645 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001646
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001647 Value *Src = I->getOperand(0);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001648 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1649 if (!SrcEVT.isSimple())
1650 return false;
1651 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001652 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001653 return false;
1654
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001655 unsigned SrcReg = getRegForValue(Src);
1656 if (SrcReg == 0) return false;
1657
1658 // Handle sign-extension.
1659 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001660 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001661 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001662 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001663 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001664
Eric Christopher860fc932010-09-10 00:34:35 +00001665 // The conversion routine works on fp-reg to fp-reg and the operand above
1666 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001667 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001668 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001669
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001670 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001671 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1672 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001673 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001674
Eric Christopher4bd70472010-09-09 21:44:45 +00001675 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001676 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1677 ResultReg)
Eric Christopher4bd70472010-09-09 21:44:45 +00001678 .addReg(FP));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001679 UpdateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001680 return true;
1681}
1682
Chad Rosiere023d5d2012-02-03 21:14:11 +00001683bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001684 // Make sure we have VFP.
1685 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001686
Duncan Sandsf5dda012010-11-03 11:35:31 +00001687 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001688 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001689 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001690 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001691
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001692 unsigned Op = getRegForValue(I->getOperand(0));
1693 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001694
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001695 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001696 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001697 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1698 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001699 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001700
Chad Rosier41f0e782012-02-03 20:27:51 +00001701 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001702 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001703 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1704 ResultReg)
1705 .addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001706
Eric Christopher4bd70472010-09-09 21:44:45 +00001707 // This result needs to be in an integer register, but the conversion only
1708 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001709 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001710 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001711
Eric Christopher4bd70472010-09-09 21:44:45 +00001712 UpdateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001713 return true;
1714}
1715
Eric Christopher511aa312010-10-11 08:27:59 +00001716bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001717 MVT VT;
1718 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001719 return false;
1720
1721 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001722 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001723
1724 unsigned CondReg = getRegForValue(I->getOperand(0));
1725 if (CondReg == 0) return false;
1726 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1727 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001728
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001729 // Check to see if we can use an immediate in the conditional move.
1730 int Imm = 0;
1731 bool UseImm = false;
1732 bool isNegativeImm = false;
1733 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1734 assert (VT == MVT::i32 && "Expecting an i32.");
1735 Imm = (int)ConstInt->getValue().getZExtValue();
1736 if (Imm < 0) {
1737 isNegativeImm = true;
1738 Imm = ~Imm;
1739 }
1740 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1741 (ARM_AM::getSOImmVal(Imm) != -1);
1742 }
1743
Duncan Sands12330652011-11-28 10:31:27 +00001744 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001745 if (!UseImm) {
1746 Op2Reg = getRegForValue(I->getOperand(2));
1747 if (Op2Reg == 0) return false;
1748 }
1749
1750 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Jim Grosbachd7866792013-08-16 23:37:40 +00001751 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
Eric Christopher511aa312010-10-11 08:27:59 +00001752 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001753 .addReg(CondReg).addImm(0));
1754
1755 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001756 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001757 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001758 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001759 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1760 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001761 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1762 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001763 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001764 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001765 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001766 }
Eric Christopher511aa312010-10-11 08:27:59 +00001767 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001768 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001769 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001770 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001771 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1772 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001773 } else {
1774 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001775 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1776 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001777 }
Eric Christopher511aa312010-10-11 08:27:59 +00001778 UpdateValueMap(I, ResultReg);
1779 return true;
1780}
1781
Chad Rosieraaa55a82012-02-03 21:07:27 +00001782bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001783 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001784 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001785 if (!isTypeLegal(Ty, VT))
1786 return false;
1787
1788 // If we have integer div support we should have selected this automagically.
1789 // In case we have a real miss go ahead and return false and we'll pick
1790 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001791 if (Subtarget->hasDivide()) return false;
1792
Eric Christopher56094ff2010-09-30 22:34:19 +00001793 // Otherwise emit a libcall.
1794 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001795 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001796 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001797 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001798 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001799 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001800 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001801 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001802 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001803 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001804 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001805 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001806
Eric Christopher56094ff2010-09-30 22:34:19 +00001807 return ARMEmitLibcall(I, LC);
1808}
1809
Chad Rosierb84a4b42012-02-03 21:23:45 +00001810bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001811 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001812 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001813 if (!isTypeLegal(Ty, VT))
1814 return false;
1815
1816 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1817 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001818 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001819 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001820 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001821 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001822 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001823 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001824 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001825 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001826 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001827 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001828
Eric Christophereae1b382010-10-11 08:37:26 +00001829 return ARMEmitLibcall(I, LC);
1830}
1831
Chad Rosier685b20c2012-02-06 23:50:07 +00001832bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier685b20c2012-02-06 23:50:07 +00001833 EVT DestVT = TLI.getValueType(I->getType(), true);
1834
1835 // We can get here in the case when we have a binary operation on a non-legal
1836 // type and the target independent selector doesn't know how to handle it.
1837 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1838 return false;
Jush Luac96b762012-06-14 06:08:19 +00001839
Chad Rosierbd471252012-02-08 02:29:21 +00001840 unsigned Opc;
1841 switch (ISDOpcode) {
1842 default: return false;
1843 case ISD::ADD:
1844 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1845 break;
1846 case ISD::OR:
1847 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1848 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001849 case ISD::SUB:
1850 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1851 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001852 }
1853
Chad Rosier685b20c2012-02-06 23:50:07 +00001854 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1855 if (SrcReg1 == 0) return false;
1856
1857 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1858 // in the instruction, rather then materializing the value in a register.
1859 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1860 if (SrcReg2 == 0) return false;
1861
JF Bastien13969d02013-05-29 15:45:47 +00001862 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001863 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1864 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Chad Rosier685b20c2012-02-06 23:50:07 +00001865 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1866 TII.get(Opc), ResultReg)
1867 .addReg(SrcReg1).addReg(SrcReg2));
1868 UpdateValueMap(I, ResultReg);
1869 return true;
1870}
1871
1872bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001873 EVT FPVT = TLI.getValueType(I->getType(), true);
1874 if (!FPVT.isSimple()) return false;
1875 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001876
Eric Christopher24dc27f2010-09-09 00:53:57 +00001877 // We can get here in the case when we want to use NEON for our fp
1878 // operations, but can't figure out how to. Just use the vfp instructions
1879 // if we have them.
1880 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001881 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001882 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1883 if (isFloat && !Subtarget->hasVFP2())
1884 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001885
Eric Christopher24dc27f2010-09-09 00:53:57 +00001886 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001887 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001888 switch (ISDOpcode) {
1889 default: return false;
1890 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001891 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001892 break;
1893 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001894 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001895 break;
1896 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001897 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001898 break;
1899 }
Chad Rosier80979b62011-11-16 18:39:44 +00001900 unsigned Op1 = getRegForValue(I->getOperand(0));
1901 if (Op1 == 0) return false;
1902
1903 unsigned Op2 = getRegForValue(I->getOperand(1));
1904 if (Op2 == 0) return false;
1905
Chad Rosier62a144f2012-12-17 19:59:43 +00001906 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Eric Christopher24dc27f2010-09-09 00:53:57 +00001907 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1908 TII.get(Opc), ResultReg)
1909 .addReg(Op1).addReg(Op2));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001910 UpdateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001911 return true;
1912}
1913
Eric Christopher72497e52010-09-10 23:18:12 +00001914// Call Handling Code
1915
Jush Lue67e07b2012-07-19 09:49:00 +00001916// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001917// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001918CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1919 bool Return,
1920 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001921 switch (CC) {
1922 default:
1923 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001924 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001925 if (Subtarget->hasVFP2() && !isVarArg) {
1926 if (!Subtarget->isAAPCS_ABI())
1927 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1928 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1929 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1930 }
Evan Cheng21abfc92010-10-22 18:57:05 +00001931 // Fallthrough
1932 case CallingConv::C:
Eric Christopher72497e52010-09-10 23:18:12 +00001933 // Use target triple & subtarget features to do actual dispatch.
1934 if (Subtarget->isAAPCS_ABI()) {
1935 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001936 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001937 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1938 else
1939 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1940 } else
1941 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1942 case CallingConv::ARM_AAPCS_VFP:
Jush Lue67e07b2012-07-19 09:49:00 +00001943 if (!isVarArg)
1944 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1945 // Fall through to soft float variant, variadic functions don't
1946 // use hard floating point ABI.
Eric Christopher72497e52010-09-10 23:18:12 +00001947 case CallingConv::ARM_AAPCS:
1948 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1949 case CallingConv::ARM_APCS:
1950 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001951 case CallingConv::GHC:
1952 if (Return)
1953 llvm_unreachable("Can't return in GHC call convention");
1954 else
1955 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001956 }
1957}
1958
Eric Christopher79398062010-09-29 23:11:09 +00001959bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1960 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001961 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001962 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1963 SmallVectorImpl<unsigned> &RegArgs,
1964 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001965 unsigned &NumBytes,
1966 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001967 SmallVector<CCValAssign, 16> ArgLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00001968 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1969 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1970 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001971
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001972 // Check that we can handle all of the arguments. If we can't, then bail out
1973 // now before we add code to the MBB.
1974 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1975 CCValAssign &VA = ArgLocs[i];
1976 MVT ArgVT = ArgVTs[VA.getValNo()];
1977
1978 // We don't handle NEON/vector parameters yet.
1979 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1980 return false;
1981
1982 // Now copy/store arg to correct locations.
1983 if (VA.isRegLoc() && !VA.needsCustom()) {
1984 continue;
1985 } else if (VA.needsCustom()) {
1986 // TODO: We need custom lowering for vector (v2f64) args.
1987 if (VA.getLocVT() != MVT::f64 ||
1988 // TODO: Only handle register args for now.
1989 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1990 return false;
1991 } else {
Craig Topper56710102013-08-15 02:33:50 +00001992 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001993 default:
1994 return false;
1995 case MVT::i1:
1996 case MVT::i8:
1997 case MVT::i16:
1998 case MVT::i32:
1999 break;
2000 case MVT::f32:
2001 if (!Subtarget->hasVFP2())
2002 return false;
2003 break;
2004 case MVT::f64:
2005 if (!Subtarget->hasVFP2())
2006 return false;
2007 break;
2008 }
2009 }
2010 }
2011
2012 // At the point, we are able to handle the call's arguments in fast isel.
2013
Eric Christopher79398062010-09-29 23:11:09 +00002014 // Get a count of how many bytes are to be pushed on the stack.
2015 NumBytes = CCInfo.getNextStackOffset();
2016
2017 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002018 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Eric Christopher71ef1af2010-10-11 21:20:02 +00002019 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2020 TII.get(AdjStackDown))
2021 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00002022
2023 // Process the args.
2024 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2025 CCValAssign &VA = ArgLocs[i];
2026 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00002027 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00002028
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002029 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
2030 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00002031
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002032 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00002033 switch (VA.getLocInfo()) {
2034 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00002035 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002036 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002037 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
2038 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002039 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002040 break;
2041 }
Chad Rosierd0191a52011-11-05 20:16:15 +00002042 case CCValAssign::AExt:
2043 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00002044 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002045 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002046 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00002047 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002048 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002049 break;
2050 }
2051 case CCValAssign::BCvt: {
Wesley Peck527da1b2010-11-23 03:31:01 +00002052 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00002053 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00002054 assert(BC != 0 && "Failed to emit a bitcast!");
2055 Arg = BC;
2056 ArgVT = VA.getLocVT();
2057 break;
2058 }
2059 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00002060 }
2061
2062 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00002063 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christopher79398062010-09-29 23:11:09 +00002064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002065 VA.getLocReg())
Chad Rosierd0191a52011-11-05 20:16:15 +00002066 .addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00002067 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002068 } else if (VA.needsCustom()) {
2069 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002070 assert(VA.getLocVT() == MVT::f64 &&
2071 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00002072
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002073 CCValAssign &NextVA = ArgLocs[++i];
2074
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002075 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2076 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002077
2078 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2079 TII.get(ARM::VMOVRRD), VA.getLocReg())
2080 .addReg(NextVA.getLocReg(), RegState::Define)
2081 .addReg(Arg));
2082 RegArgs.push_back(VA.getLocReg());
2083 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002084 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002085 assert(VA.isMemLoc());
2086 // Need to store on the stack.
Eric Christopherfef5f312010-11-19 22:30:02 +00002087 Address Addr;
2088 Addr.BaseType = Address::RegBase;
2089 Addr.Base.Reg = ARM::SP;
2090 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002091
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002092 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2093 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002094 }
2095 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002096
Eric Christopher79398062010-09-29 23:11:09 +00002097 return true;
2098}
2099
Duncan Sandsf5dda012010-11-03 11:35:31 +00002100bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002101 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002102 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002103 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002104 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eric Christopher71ef1af2010-10-11 21:20:02 +00002105 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2106 TII.get(AdjStackUp))
2107 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002108
2109 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002110 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002111 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002112 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2113 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002114
2115 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002116 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002117 // For this move we copy into two registers and then move into the
2118 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002119 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002120 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002121 unsigned ResultReg = createResultReg(DstRC);
2122 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2123 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002124 .addReg(RVLocs[0].getLocReg())
2125 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002126
Eric Christopheraf719ef2010-10-20 08:02:24 +00002127 UsedRegs.push_back(RVLocs[0].getLocReg());
2128 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002129
Eric Christopher7ac602b2010-10-11 08:38:55 +00002130 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002131 UpdateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002132 } else {
2133 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002134 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002135
2136 // Special handling for extended integers.
2137 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2138 CopyVT = MVT::i32;
2139
Craig Topper760b1342012-02-22 05:59:10 +00002140 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002141
Eric Christopherc1e209d2010-10-01 00:00:11 +00002142 unsigned ResultReg = createResultReg(DstRC);
2143 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2144 ResultReg).addReg(RVLocs[0].getLocReg());
2145 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002146
Eric Christopher7ac602b2010-10-11 08:38:55 +00002147 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002148 UpdateValueMap(I, ResultReg);
2149 }
Eric Christopher79398062010-09-29 23:11:09 +00002150 }
2151
Eric Christopher7ac602b2010-10-11 08:38:55 +00002152 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002153}
2154
Eric Christopher93bbe652010-10-22 01:28:00 +00002155bool ARMFastISel::SelectRet(const Instruction *I) {
2156 const ReturnInst *Ret = cast<ReturnInst>(I);
2157 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002158
Eric Christopher93bbe652010-10-22 01:28:00 +00002159 if (!FuncInfo.CanLowerReturn)
2160 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002161
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002162 // Build a list of return value registers.
2163 SmallVector<unsigned, 4> RetRegs;
2164
Eric Christopher93bbe652010-10-22 01:28:00 +00002165 CallingConv::ID CC = F.getCallingConv();
2166 if (Ret->getNumOperands() > 0) {
2167 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002168 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher93bbe652010-10-22 01:28:00 +00002169
2170 // Analyze operands of the call, assigning locations to each operand.
2171 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbache7e2aca2011-09-13 20:30:37 +00002172 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002173 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2174 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002175
2176 const Value *RV = Ret->getOperand(0);
2177 unsigned Reg = getRegForValue(RV);
2178 if (Reg == 0)
2179 return false;
2180
2181 // Only handle a single return value for now.
2182 if (ValLocs.size() != 1)
2183 return false;
2184
2185 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002186
Eric Christopher93bbe652010-10-22 01:28:00 +00002187 // Don't bother handling odd stuff for now.
2188 if (VA.getLocInfo() != CCValAssign::Full)
2189 return false;
2190 // Only handle register returns for now.
2191 if (!VA.isRegLoc())
2192 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002193
2194 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier62a144f2012-12-17 19:59:43 +00002195 EVT RVEVT = TLI.getValueType(RV->getType());
2196 if (!RVEVT.isSimple()) return false;
2197 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002198 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002199 // Special handling for extended integers.
2200 if (RVVT != DestVT) {
2201 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2202 return false;
2203
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002204 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2205
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002206 // Perform extension if flagged as either zext or sext. Otherwise, do
2207 // nothing.
2208 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2209 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2210 if (SrcReg == 0) return false;
2211 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002212 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002213
Eric Christopher93bbe652010-10-22 01:28:00 +00002214 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002215 unsigned DstReg = VA.getLocReg();
2216 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2217 // Avoid a cross-class copy. This is very unlikely.
2218 if (!SrcRC->contains(DstReg))
2219 return false;
2220 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2221 DstReg).addReg(SrcReg);
2222
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002223 // Add register to return instruction.
2224 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002225 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002226
Chad Rosier0439cfc2011-11-08 21:12:00 +00002227 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002228 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2229 TII.get(RetOpc));
2230 AddOptionalDefs(MIB);
2231 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2232 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002233 return true;
2234}
2235
Chad Rosierc6916f82012-06-12 19:25:13 +00002236unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2237 if (UseReg)
2238 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2239 else
2240 return isThumb2 ? ARM::tBL : ARM::BL;
2241}
2242
2243unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002244 // Manually compute the global's type to avoid building it when unnecessary.
2245 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2246 EVT LCREVT = TLI.getValueType(GVTy);
2247 if (!LCREVT.isSimple()) return 0;
2248
Bill Wendling76cce192013-12-29 08:00:04 +00002249 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Chad Rosierc6916f82012-06-12 19:25:13 +00002250 GlobalValue::ExternalLinkage, 0, Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002251 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002252 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002253}
2254
Eric Christopher8b912662010-09-14 23:03:37 +00002255// A quick function that will emit a call for a named libcall in F with the
2256// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002257// can emit a call for any libcall we can produce. This is an abridged version
2258// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002259// like computed function pointers or strange arguments at call sites.
2260// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2261// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002262bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2263 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002264
Eric Christopher8b912662010-09-14 23:03:37 +00002265 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002266 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002267 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002268 if (RetTy->isVoidTy())
2269 RetVT = MVT::isVoid;
2270 else if (!isTypeLegal(RetTy, RetVT))
2271 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002272
Chad Rosier90f9afe2012-05-11 18:51:55 +00002273 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002274 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002275 SmallVector<CCValAssign, 16> RVLocs;
2276 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002277 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002278 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2279 return false;
2280 }
2281
Eric Christopher79398062010-09-29 23:11:09 +00002282 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002283 SmallVector<Value*, 8> Args;
2284 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002285 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002286 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2287 Args.reserve(I->getNumOperands());
2288 ArgRegs.reserve(I->getNumOperands());
2289 ArgVTs.reserve(I->getNumOperands());
2290 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002291 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002292 Value *Op = I->getOperand(i);
2293 unsigned Arg = getRegForValue(Op);
2294 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002295
Chris Lattner229907c2011-07-18 04:54:35 +00002296 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002297 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002298 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002299
Eric Christopher8b912662010-09-14 23:03:37 +00002300 ISD::ArgFlagsTy Flags;
2301 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2302 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002303
Eric Christopher8b912662010-09-14 23:03:37 +00002304 Args.push_back(Op);
2305 ArgRegs.push_back(Arg);
2306 ArgVTs.push_back(ArgVT);
2307 ArgFlags.push_back(Flags);
2308 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002309
Eric Christopher79398062010-09-29 23:11:09 +00002310 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002311 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002312 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002313 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2314 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002315 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002316
Chad Rosierc6916f82012-06-12 19:25:13 +00002317 unsigned CalleeReg = 0;
2318 if (EnableARMLongCalls) {
2319 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2320 if (CalleeReg == 0) return false;
2321 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002322
Chad Rosierc6916f82012-06-12 19:25:13 +00002323 // Issue the call.
2324 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2325 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2326 DL, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002327 // BL / BLX don't take a predicate, but tBL / tBLX do.
2328 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002329 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002330 if (EnableARMLongCalls)
2331 MIB.addReg(CalleeReg);
2332 else
2333 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002334
Eric Christopher8b912662010-09-14 23:03:37 +00002335 // Add implicit physical register uses to the call.
2336 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002337 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002338
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002339 // Add a register mask with the call-preserved registers.
2340 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2341 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2342
Eric Christopher79398062010-09-29 23:11:09 +00002343 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002344 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002345 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002346
Eric Christopher8b912662010-09-14 23:03:37 +00002347 // Set all unused physreg defs as dead.
2348 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002349
Eric Christopher8b912662010-09-14 23:03:37 +00002350 return true;
2351}
2352
Chad Rosiera7ebc562011-11-11 23:31:03 +00002353bool ARMFastISel::SelectCall(const Instruction *I,
2354 const char *IntrMemName = 0) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002355 const CallInst *CI = cast<CallInst>(I);
2356 const Value *Callee = CI->getCalledValue();
2357
Chad Rosiera7ebc562011-11-11 23:31:03 +00002358 // Can't handle inline asm.
2359 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002360
Chad Rosierdf42cf32012-12-11 00:18:02 +00002361 // Allow SelectionDAG isel to handle tail calls.
2362 if (CI->isTailCall()) return false;
2363
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002364 // Check the calling convention.
2365 ImmutableCallSite CS(CI);
2366 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002367
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002368 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002369
Chris Lattner229907c2011-07-18 04:54:35 +00002370 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2371 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Lue67e07b2012-07-19 09:49:00 +00002372 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002373
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002374 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002375 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002376 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002377 if (RetTy->isVoidTy())
2378 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002379 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2380 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002381 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002382
Chad Rosier90f9afe2012-05-11 18:51:55 +00002383 // Can't handle non-double multi-reg retvals.
2384 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2385 RetVT != MVT::i16 && RetVT != MVT::i32) {
2386 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002387 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2388 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002389 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2390 return false;
2391 }
2392
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002393 // Set up the argument vectors.
2394 SmallVector<Value*, 8> Args;
2395 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002396 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002397 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002398 unsigned arg_size = CS.arg_size();
2399 Args.reserve(arg_size);
2400 ArgRegs.reserve(arg_size);
2401 ArgVTs.reserve(arg_size);
2402 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002403 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2404 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002405 // If we're lowering a memory intrinsic instead of a regular call, skip the
2406 // last two arguments, which shouldn't be passed to the underlying function.
2407 if (IntrMemName && e-i <= 2)
2408 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002409
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002410 ISD::ArgFlagsTy Flags;
2411 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002412 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002413 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002414 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002415 Flags.setZExt();
2416
Chad Rosier8a98ec42011-11-04 00:58:10 +00002417 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002418 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2419 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2420 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2421 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002422 return false;
2423
Chris Lattner229907c2011-07-18 04:54:35 +00002424 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002425 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002426 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2427 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002428 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002429
2430 unsigned Arg = getRegForValue(*i);
2431 if (Arg == 0)
2432 return false;
2433
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002434 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2435 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002436
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002437 Args.push_back(*i);
2438 ArgRegs.push_back(Arg);
2439 ArgVTs.push_back(ArgVT);
2440 ArgFlags.push_back(Flags);
2441 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002442
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002443 // Handle the arguments now that we've gotten them.
2444 SmallVector<unsigned, 4> RegArgs;
2445 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002446 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2447 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002448 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002449
Chad Rosierc6916f82012-06-12 19:25:13 +00002450 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002451 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosierc6916f82012-06-12 19:25:13 +00002452 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002453
Chad Rosierc6916f82012-06-12 19:25:13 +00002454 unsigned CalleeReg = 0;
2455 if (UseReg) {
2456 if (IntrMemName)
2457 CalleeReg = getLibcallReg(IntrMemName);
2458 else
2459 CalleeReg = getRegForValue(Callee);
2460
Chad Rosier223faf72012-05-23 18:38:57 +00002461 if (CalleeReg == 0) return false;
2462 }
2463
Chad Rosierc6916f82012-06-12 19:25:13 +00002464 // Issue the call.
2465 unsigned CallOpc = ARMSelectCallOp(UseReg);
2466 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2467 DL, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002468
Logan Chien2361f512013-08-22 12:08:04 +00002469 unsigned char OpFlags = 0;
2470
2471 // Add MO_PLT for global address or external symbol in the PIC relocation
2472 // model.
2473 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2474 OpFlags = ARMII::MO_PLT;
2475
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002476 // ARM calls don't take a predicate, but tBL / tBLX do.
2477 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002478 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002479 if (UseReg)
2480 MIB.addReg(CalleeReg);
2481 else if (!IntrMemName)
Logan Chien2361f512013-08-22 12:08:04 +00002482 MIB.addGlobalAddress(GV, 0, OpFlags);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002483 else
Logan Chien2361f512013-08-22 12:08:04 +00002484 MIB.addExternalSymbol(IntrMemName, OpFlags);
Jush Luac96b762012-06-14 06:08:19 +00002485
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002486 // Add implicit physical register uses to the call.
2487 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002488 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002489
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002490 // Add a register mask with the call-preserved registers.
2491 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2492 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2493
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002494 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002495 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002496 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2497 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002498
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002499 // Set all unused physreg defs as dead.
2500 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002501
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002502 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002503}
2504
Chad Rosier057b6d32011-11-14 23:04:09 +00002505bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002506 return Len <= 16;
2507}
2508
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002509bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002510 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002511 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002512 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002513 return false;
2514
Chad Rosierab7223e2011-11-14 22:46:17 +00002515 while (Len) {
2516 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002517 if (!Alignment || Alignment >= 4) {
2518 if (Len >= 4)
2519 VT = MVT::i32;
2520 else if (Len >= 2)
2521 VT = MVT::i16;
2522 else {
2523 assert (Len == 1 && "Expected a length of 1!");
2524 VT = MVT::i8;
2525 }
2526 } else {
2527 // Bound based on alignment.
2528 if (Len >= 2 && Alignment == 2)
2529 VT = MVT::i16;
2530 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002531 VT = MVT::i8;
2532 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002533 }
2534
2535 bool RV;
2536 unsigned ResultReg;
2537 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002538 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002539 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002540 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002541 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002542
2543 unsigned Size = VT.getSizeInBits()/8;
2544 Len -= Size;
2545 Dest.Offset += Size;
2546 Src.Offset += Size;
2547 }
2548
2549 return true;
2550}
2551
Chad Rosiera7ebc562011-11-11 23:31:03 +00002552bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2553 // FIXME: Handle more intrinsics.
2554 switch (I.getIntrinsicID()) {
2555 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002556 case Intrinsic::frameaddress: {
2557 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2558 MFI->setFrameAddressIsTaken(true);
2559
2560 unsigned LdrOpc;
2561 const TargetRegisterClass *RC;
2562 if (isThumb2) {
2563 LdrOpc = ARM::t2LDRi12;
2564 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2565 } else {
2566 LdrOpc = ARM::LDRi12;
2567 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2568 }
2569
2570 const ARMBaseRegisterInfo *RegInfo =
2571 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2572 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2573 unsigned SrcReg = FramePtr;
2574
2575 // Recursively load frame address
2576 // ldr r0 [fp]
2577 // ldr r0 [r0]
2578 // ldr r0 [r0]
2579 // ...
2580 unsigned DestReg;
2581 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2582 while (Depth--) {
2583 DestReg = createResultReg(RC);
2584 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2585 TII.get(LdrOpc), DestReg)
2586 .addReg(SrcReg).addImm(0));
2587 SrcReg = DestReg;
2588 }
Chad Rosierf3193242012-06-01 21:12:31 +00002589 UpdateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002590 return true;
2591 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002592 case Intrinsic::memcpy:
2593 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002594 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2595 // Don't handle volatile.
2596 if (MTI.isVolatile())
2597 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002598
2599 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2600 // we would emit dead code because we don't currently handle memmoves.
2601 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2602 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002603 // Small memcpy's are common enough that we want to do them without a call
2604 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002605 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002606 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002607 Address Dest, Src;
2608 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2609 !ARMComputeAddress(MTI.getRawSource(), Src))
2610 return false;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002611 unsigned Alignment = MTI.getAlignment();
2612 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002613 return true;
2614 }
2615 }
Jush Luac96b762012-06-14 06:08:19 +00002616
Chad Rosiera7ebc562011-11-11 23:31:03 +00002617 if (!MTI.getLength()->getType()->isIntegerTy(32))
2618 return false;
Jush Luac96b762012-06-14 06:08:19 +00002619
Chad Rosiera7ebc562011-11-11 23:31:03 +00002620 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2621 return false;
2622
2623 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2624 return SelectCall(&I, IntrMemName);
2625 }
2626 case Intrinsic::memset: {
2627 const MemSetInst &MSI = cast<MemSetInst>(I);
2628 // Don't handle volatile.
2629 if (MSI.isVolatile())
2630 return false;
Jush Luac96b762012-06-14 06:08:19 +00002631
Chad Rosiera7ebc562011-11-11 23:31:03 +00002632 if (!MSI.getLength()->getType()->isIntegerTy(32))
2633 return false;
Jush Luac96b762012-06-14 06:08:19 +00002634
Chad Rosiera7ebc562011-11-11 23:31:03 +00002635 if (MSI.getDestAddressSpace() > 255)
2636 return false;
Jush Luac96b762012-06-14 06:08:19 +00002637
Chad Rosiera7ebc562011-11-11 23:31:03 +00002638 return SelectCall(&I, "memset");
2639 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002640 case Intrinsic::trap: {
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002641 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(
2642 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002643 return true;
2644 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002645 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002646}
2647
Chad Rosieree7e4522011-11-02 00:18:48 +00002648bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002649 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002650 // undefined.
2651 Value *Op = I->getOperand(0);
2652
2653 EVT SrcVT, DestVT;
2654 SrcVT = TLI.getValueType(Op->getType(), true);
2655 DestVT = TLI.getValueType(I->getType(), true);
2656
2657 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2658 return false;
2659 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2660 return false;
2661
2662 unsigned SrcReg = getRegForValue(Op);
2663 if (!SrcReg) return false;
2664
2665 // Because the high bits are undefined, a truncate doesn't generate
2666 // any code.
2667 UpdateValueMap(I, SrcReg);
2668 return true;
2669}
2670
Chad Rosier62a144f2012-12-17 19:59:43 +00002671unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002672 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002673 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002674 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002675 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002676 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002677
2678 // Table of which combinations can be emitted as a single instruction,
2679 // and which will require two.
2680 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2681 // ARM Thumb
2682 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2683 // ext: s z s z s z s z
2684 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2685 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2686 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2687 };
2688
2689 // Target registers for:
2690 // - For ARM can never be PC.
2691 // - For 16-bit Thumb are restricted to lower 8 registers.
2692 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2693 static const TargetRegisterClass *RCTbl[2][2] = {
2694 // Instructions: Two Single
2695 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2696 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2697 };
2698
2699 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002700 static const struct InstructionTable {
2701 uint32_t Opc : 16;
2702 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2703 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2704 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2705 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002706 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002707 { // ARM Opc S Shift Imm
2708 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2709 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2710 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2711 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2712 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2713 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002714 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002715 { // Thumb Opc S Shift Imm
2716 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2717 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2718 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2719 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2720 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2721 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002722 }
2723 },
2724 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002725 { // ARM Opc S Shift Imm
2726 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2727 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2728 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2729 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2730 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2731 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002732 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002733 { // Thumb Opc S Shift Imm
2734 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2735 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2736 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2737 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2738 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2739 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002740 }
2741 }
2742 };
2743
2744 unsigned SrcBits = SrcVT.getSizeInBits();
2745 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002746 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002747 assert((SrcBits < DestBits) && "can only extend to larger types");
2748 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2749 "other sizes unimplemented");
2750 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2751 "other sizes unimplemented");
2752
2753 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002754 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002755 assert((Bitness < 3) && "sanity-check table bounds");
2756
2757 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2758 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002759 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2760 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002761 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002762 unsigned hasS = ITP->hasS;
2763 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2764 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2765 "only MOVsi has shift operand addressing mode");
2766 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002767
2768 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2769 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002770 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002771 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002772 // MOVsi encodes shift and immediate in shift operand addressing mode.
2773 // The following condition has the same value when emitting two
2774 // instruction sequences: both are shifts.
2775 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002776
2777 // Either one or two instructions are emitted.
2778 // They're always of the form:
2779 // dst = in OP imm
2780 // CPSR is set only by 16-bit Thumb instructions.
2781 // Predicate, if any, is AL.
2782 // S bit, if available, is always 0.
2783 // When two are emitted the first's result will feed as the second's input,
2784 // that value is then dead.
2785 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2786 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2787 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002788 bool isLsl = (0 == Instr) && !isSingleInstr;
2789 unsigned Opcode = isLsl ? LSLOpc : Opc;
2790 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2791 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002792 bool isKill = 1 == Instr;
2793 MachineInstrBuilder MIB = BuildMI(
2794 *FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opcode), ResultReg);
2795 if (setsCPSR)
2796 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002797 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002798 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002799 if (hasS)
2800 AddDefaultCC(MIB);
2801 // Second instruction consumes the first's result.
2802 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002803 }
2804
Chad Rosier4489f942011-11-02 17:20:24 +00002805 return ResultReg;
2806}
2807
2808bool ARMFastISel::SelectIntExt(const Instruction *I) {
2809 // On ARM, in general, integer casts don't involve legal types; this code
2810 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002811 Type *DestTy = I->getType();
2812 Value *Src = I->getOperand(0);
2813 Type *SrcTy = Src->getType();
2814
Chad Rosier4489f942011-11-02 17:20:24 +00002815 bool isZExt = isa<ZExtInst>(I);
2816 unsigned SrcReg = getRegForValue(Src);
2817 if (!SrcReg) return false;
2818
Chad Rosier62a144f2012-12-17 19:59:43 +00002819 EVT SrcEVT, DestEVT;
2820 SrcEVT = TLI.getValueType(SrcTy, true);
2821 DestEVT = TLI.getValueType(DestTy, true);
2822 if (!SrcEVT.isSimple()) return false;
2823 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002824
Chad Rosier62a144f2012-12-17 19:59:43 +00002825 MVT SrcVT = SrcEVT.getSimpleVT();
2826 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002827 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2828 if (ResultReg == 0) return false;
2829 UpdateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002830 return true;
2831}
2832
Jush Lu4705da92012-08-03 02:37:48 +00002833bool ARMFastISel::SelectShift(const Instruction *I,
2834 ARM_AM::ShiftOpc ShiftTy) {
2835 // We handle thumb2 mode by target independent selector
2836 // or SelectionDAG ISel.
2837 if (isThumb2)
2838 return false;
2839
2840 // Only handle i32 now.
2841 EVT DestVT = TLI.getValueType(I->getType(), true);
2842 if (DestVT != MVT::i32)
2843 return false;
2844
2845 unsigned Opc = ARM::MOVsr;
2846 unsigned ShiftImm;
2847 Value *Src2Value = I->getOperand(1);
2848 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2849 ShiftImm = CI->getZExtValue();
2850
2851 // Fall back to selection DAG isel if the shift amount
2852 // is zero or greater than the width of the value type.
2853 if (ShiftImm == 0 || ShiftImm >=32)
2854 return false;
2855
2856 Opc = ARM::MOVsi;
2857 }
2858
2859 Value *Src1Value = I->getOperand(0);
2860 unsigned Reg1 = getRegForValue(Src1Value);
2861 if (Reg1 == 0) return false;
2862
Nadav Rotema8e15b02012-09-06 11:13:55 +00002863 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002864 if (Opc == ARM::MOVsr) {
2865 Reg2 = getRegForValue(Src2Value);
2866 if (Reg2 == 0) return false;
2867 }
2868
JF Bastien13969d02013-05-29 15:45:47 +00002869 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002870 if(ResultReg == 0) return false;
2871
2872 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2873 TII.get(Opc), ResultReg)
2874 .addReg(Reg1);
2875
2876 if (Opc == ARM::MOVsi)
2877 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2878 else if (Opc == ARM::MOVsr) {
2879 MIB.addReg(Reg2);
2880 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2881 }
2882
2883 AddOptionalDefs(MIB);
2884 UpdateValueMap(I, ResultReg);
2885 return true;
2886}
2887
Eric Christopherc3e118e2010-09-02 23:43:26 +00002888// TODO: SoftFP support.
Eric Christopher84bdfd82010-07-21 22:26:11 +00002889bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002890
Eric Christopher84bdfd82010-07-21 22:26:11 +00002891 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002892 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002893 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002894 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002895 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002896 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002897 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002898 case Instruction::IndirectBr:
2899 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002900 case Instruction::ICmp:
2901 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002902 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002903 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002904 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002905 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002906 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002907 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002908 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002909 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002910 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002911 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002912 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002913 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002914 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002915 case Instruction::Add:
2916 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002917 case Instruction::Or:
2918 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002919 case Instruction::Sub:
2920 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002921 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002922 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002923 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002924 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002925 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002926 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002927 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002928 return SelectDiv(I, /*isSigned*/ true);
2929 case Instruction::UDiv:
2930 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002931 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002932 return SelectRem(I, /*isSigned*/ true);
2933 case Instruction::URem:
2934 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002935 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002936 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2937 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002938 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002939 case Instruction::Select:
2940 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002941 case Instruction::Ret:
2942 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002943 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002944 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002945 case Instruction::ZExt:
2946 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002947 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002948 case Instruction::Shl:
2949 return SelectShift(I, ARM_AM::lsl);
2950 case Instruction::LShr:
2951 return SelectShift(I, ARM_AM::lsr);
2952 case Instruction::AShr:
2953 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002954 default: break;
2955 }
2956 return false;
2957}
2958
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002959namespace {
2960// This table describes sign- and zero-extend instructions which can be
2961// folded into a preceding load. All of these extends have an immediate
2962// (sometimes a mask and sometimes a shift) that's applied after
2963// extension.
2964const struct FoldableLoadExtendsStruct {
2965 uint16_t Opc[2]; // ARM, Thumb.
2966 uint8_t ExpectedImm;
2967 uint8_t isZExt : 1;
2968 uint8_t ExpectedVT : 7;
2969} FoldableLoadExtends[] = {
2970 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2971 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2972 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2973 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2974 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2975};
2976}
2977
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002978/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002979/// vreg is being provided by the specified load instruction. If possible,
2980/// try to fold the load as an operand to the instruction, returning true if
2981/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002982bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2983 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002984 // Verify we have a legal type before going any further.
2985 MVT VT;
2986 if (!isLoadTypeLegal(LI->getType(), VT))
2987 return false;
2988
2989 // Combine load followed by zero- or sign-extend.
2990 // ldrb r1, [r0] ldrb r1, [r0]
2991 // uxtb r2, r1 =>
2992 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002993 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2994 return false;
2995 const uint64_t Imm = MI->getOperand(2).getImm();
2996
2997 bool Found = false;
2998 bool isZExt;
2999 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
3000 i != e; ++i) {
3001 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
3002 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
3003 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
3004 Found = true;
3005 isZExt = FoldableLoadExtends[i].isZExt;
3006 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003007 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00003008 if (!Found) return false;
3009
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003010 // See if we can handle this address.
3011 Address Addr;
3012 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00003013
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003014 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00003015 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003016 return false;
3017 MI->eraseFromParent();
3018 return true;
3019}
3020
Jush Lu47172a02012-09-27 05:21:41 +00003021unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003022 unsigned Align, MVT VT) {
Jush Lu47172a02012-09-27 05:21:41 +00003023 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
3024 ARMConstantPoolConstant *CPV =
3025 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
3026 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
3027
3028 unsigned Opc;
3029 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
3030 // Load value.
3031 if (isThumb2) {
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003032 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00003033 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
3034 TII.get(ARM::t2LDRpci), DestReg1)
3035 .addConstantPoolIndex(Idx));
3036 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
3037 } else {
3038 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003039 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00003040 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
3041 DL, TII.get(ARM::LDRcp), DestReg1)
3042 .addConstantPoolIndex(Idx).addImm(0));
3043 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
3044 }
3045
3046 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
3047 if (GlobalBaseReg == 0) {
3048 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
3049 AFI->setGlobalBaseReg(GlobalBaseReg);
3050 }
3051
3052 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003053 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
3054 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
3055 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
Jush Lu47172a02012-09-27 05:21:41 +00003056 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
3057 DL, TII.get(Opc), DestReg2)
3058 .addReg(DestReg1)
3059 .addReg(GlobalBaseReg);
3060 if (!UseGOTOFF)
3061 MIB.addImm(0);
3062 AddOptionalDefs(MIB);
3063
3064 return DestReg2;
3065}
3066
Evan Cheng615620c2013-02-11 01:27:15 +00003067bool ARMFastISel::FastLowerArguments() {
3068 if (!FuncInfo.CanLowerReturn)
3069 return false;
3070
3071 const Function *F = FuncInfo.Fn;
3072 if (F->isVarArg())
3073 return false;
3074
3075 CallingConv::ID CC = F->getCallingConv();
3076 switch (CC) {
3077 default:
3078 return false;
3079 case CallingConv::Fast:
3080 case CallingConv::C:
3081 case CallingConv::ARM_AAPCS_VFP:
3082 case CallingConv::ARM_AAPCS:
3083 case CallingConv::ARM_APCS:
3084 break;
3085 }
3086
3087 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3088 // which are passed in r0 - r3.
3089 unsigned Idx = 1;
3090 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3091 I != E; ++I, ++Idx) {
3092 if (Idx > 4)
3093 return false;
3094
3095 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3096 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3097 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3098 return false;
3099
3100 Type *ArgTy = I->getType();
3101 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3102 return false;
3103
3104 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003105 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003106 switch (ArgVT.getSimpleVT().SimpleTy) {
3107 case MVT::i8:
3108 case MVT::i16:
3109 case MVT::i32:
3110 break;
3111 default:
3112 return false;
3113 }
3114 }
3115
3116
3117 static const uint16_t GPRArgRegs[] = {
3118 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3119 };
3120
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003121 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003122 Idx = 0;
3123 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3124 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003125 unsigned SrcReg = GPRArgRegs[Idx];
3126 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3127 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3128 // Without this, EmitLiveInCopies may eliminate the livein if its only
3129 // use is a bitcast (which isn't turned into an instruction).
3130 unsigned ResultReg = createResultReg(RC);
3131 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
3132 ResultReg).addReg(DstReg, getKillRegState(true));
3133 UpdateValueMap(I, ResultReg);
3134 }
3135
3136 return true;
3137}
3138
Eric Christopher84bdfd82010-07-21 22:26:11 +00003139namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003140 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3141 const TargetLibraryInfo *libInfo) {
Eric Christopher5501b7e2010-10-11 20:05:22 +00003142 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach68147ee2010-11-09 19:22:26 +00003143
Eric Christopher5501b7e2010-10-11 20:05:22 +00003144 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastien18db1f22013-06-14 02:49:43 +00003145 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3146 bool UseFastISel = false;
3147 UseFastISel |= Subtarget->isTargetIOS() && !Subtarget->isThumb1Only();
3148 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3149 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3150
3151 if (UseFastISel) {
3152 // iOS always has a FP for backtracking, force other targets
3153 // to keep their FP when doing FastISel. The emitted code is
3154 // currently superior, and in cases like test-suite's lencod
3155 // FastISel isn't quite correct when FP is eliminated.
3156 TM.Options.NoFramePointerElim = true;
Bob Wilson3e6fa462012-08-03 04:06:28 +00003157 return new ARMFastISel(funcInfo, libInfo);
JF Bastien18db1f22013-06-14 02:49:43 +00003158 }
Evan Cheng23b05d12010-07-26 18:32:55 +00003159 return 0;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003160 }
3161}