blob: d2d5b9fe2a93ee7ec5e552a69c0931b84ad62736 [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"
Craig Toppera9253262014-03-22 23:51:00 +000017#include "ARMBaseRegisterInfo.h"
Eric Christopher72497e52010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopher83a5ec82010-10-01 23:24:42 +000019#include "ARMConstantPoolValue.h"
Craig Toppera9253262014-03-22 23:51:00 +000020#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
JF Bastien3c6bb8e2013-06-11 22:13:46 +000024#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/Analysis.h"
26#include "llvm/CodeGen/FastISel.h"
27#include "llvm/CodeGen/FunctionLoweringInfo.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineMemOperand.h"
32#include "llvm/CodeGen/MachineModuleInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000034#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000038#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/GlobalVariable.h"
40#include "llvm/IR/Instructions.h"
41#include "llvm/IR/IntrinsicInst.h"
42#include "llvm/IR/Module.h"
43#include "llvm/IR/Operator.h"
Eric Christopher663f4992010-08-17 00:46:57 +000044#include "llvm/Support/CommandLine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000045#include "llvm/Support/ErrorHandling.h"
Eric Christopher09f757d2010-08-17 01:25:29 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000049#include "llvm/Target/TargetOptions.h"
50using namespace llvm;
51
Eric Christopher347f4c32010-12-15 23:47:29 +000052extern cl::opt<bool> EnableARMLongCalls;
53
Eric Christopher84bdfd82010-07-21 22:26:11 +000054namespace {
Eric Christopher0a3c28b2010-11-20 22:38:27 +000055
Eric Christopherfef5f312010-11-19 22:30:02 +000056 // All possible address modes, plus some.
57 typedef struct Address {
58 enum {
59 RegBase,
60 FrameIndexBase
61 } BaseType;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000062
Eric Christopherfef5f312010-11-19 22:30:02 +000063 union {
64 unsigned Reg;
65 int FI;
66 } Base;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000067
Eric Christopherfef5f312010-11-19 22:30:02 +000068 int Offset;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000069
Eric Christopherfef5f312010-11-19 22:30:02 +000070 // Innocuous defaults for our address.
71 Address()
Jim Grosbach4e983162011-05-16 22:24:07 +000072 : BaseType(RegBase), Offset(0) {
Eric Christopherfef5f312010-11-19 22:30:02 +000073 Base.Reg = 0;
74 }
75 } Address;
Eric Christopher84bdfd82010-07-21 22:26:11 +000076
Craig Topper26696312014-03-18 07:27:13 +000077class ARMFastISel final : public FastISel {
Eric Christopher84bdfd82010-07-21 22:26:11 +000078
79 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
80 /// make the right decision when generating code for different targets.
81 const ARMSubtarget *Subtarget;
Bill Wendling6c1d9592013-12-30 05:17:29 +000082 Module &M;
Eric Christopher09f757d2010-08-17 01:25:29 +000083 const TargetMachine &TM;
84 const TargetInstrInfo &TII;
85 const TargetLowering &TLI;
Eric Christopher83a5ec82010-10-01 23:24:42 +000086 ARMFunctionInfo *AFI;
Eric Christopher84bdfd82010-07-21 22:26:11 +000087
Eric Christopherb024be32010-09-29 22:24:45 +000088 // Convenience variables to avoid some queries.
Chad Rosier0439cfc2011-11-08 21:12:00 +000089 bool isThumb2;
Eric Christopherb024be32010-09-29 22:24:45 +000090 LLVMContext *Context;
Eric Christopher6a0333c2010-09-02 01:39:14 +000091
Eric Christopher84bdfd82010-07-21 22:26:11 +000092 public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000093 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
94 const TargetLibraryInfo *libInfo)
95 : FastISel(funcInfo, libInfo),
Bill Wendling76cce192013-12-29 08:00:04 +000096 M(const_cast<Module&>(*funcInfo.Fn->getParent())),
Eric Christopher09f757d2010-08-17 01:25:29 +000097 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getInstrInfo()),
99 TLI(*TM.getTargetLowering()) {
Eric Christopher84bdfd82010-07-21 22:26:11 +0000100 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8d03b8a2010-08-23 22:32:45 +0000101 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier0439cfc2011-11-08 21:12:00 +0000102 isThumb2 = AFI->isThumbFunction();
Eric Christopherb024be32010-09-29 22:24:45 +0000103 Context = &funcInfo.Fn->getContext();
Eric Christopher84bdfd82010-07-21 22:26:11 +0000104 }
105
Eric Christopherd8e8a292010-08-20 00:20:31 +0000106 // Code from FastISel.cpp.
Craig Topperfd1c9252012-08-18 21:38:45 +0000107 private:
108 unsigned FastEmitInst_(unsigned MachineInstOpcode,
109 const TargetRegisterClass *RC);
110 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC,
112 unsigned Op0, bool Op0IsKill);
113 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
114 const TargetRegisterClass *RC,
115 unsigned Op0, bool Op0IsKill,
116 unsigned Op1, bool Op1IsKill);
117 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
118 const TargetRegisterClass *RC,
119 unsigned Op0, bool Op0IsKill,
120 unsigned Op1, bool Op1IsKill,
121 unsigned Op2, bool Op2IsKill);
122 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 uint64_t Imm);
126 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 const ConstantFP *FPImm);
130 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
131 const TargetRegisterClass *RC,
132 unsigned Op0, bool Op0IsKill,
133 unsigned Op1, bool Op1IsKill,
134 uint64_t Imm);
135 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
136 const TargetRegisterClass *RC,
137 uint64_t Imm);
138 unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
139 const TargetRegisterClass *RC,
140 uint64_t Imm1, uint64_t Imm2);
Eric Christopher174d8722011-03-12 01:09:29 +0000141
Craig Topperfd1c9252012-08-18 21:38:45 +0000142 unsigned FastEmitInst_extractsubreg(MVT RetVT,
143 unsigned Op0, bool Op0IsKill,
144 uint32_t Idx);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000145
Eric Christopherd8e8a292010-08-20 00:20:31 +0000146 // Backend specific FastISel code.
Craig Topperfd1c9252012-08-18 21:38:45 +0000147 private:
Craig Topper6bc27bf2014-03-10 02:09:33 +0000148 bool TargetSelectInstruction(const Instruction *I) override;
149 unsigned TargetMaterializeConstant(const Constant *C) override;
150 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
151 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
152 const LoadInst *LI) override;
153 bool FastLowerArguments() override;
Craig Topperfd1c9252012-08-18 21:38:45 +0000154 private:
Eric Christopher84bdfd82010-07-21 22:26:11 +0000155 #include "ARMGenFastISel.inc"
Eric Christopher2ff757d2010-09-09 01:06:51 +0000156
Eric Christopher00202ee2010-08-23 21:44:12 +0000157 // Instruction selection routines.
Eric Christophercc766a22010-09-10 23:10:30 +0000158 private:
Eric Christopher2f8637d2010-10-21 21:47:51 +0000159 bool SelectLoad(const Instruction *I);
160 bool SelectStore(const Instruction *I);
161 bool SelectBranch(const Instruction *I);
Chad Rosierded4c992012-02-07 23:56:08 +0000162 bool SelectIndirectBr(const Instruction *I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000163 bool SelectCmp(const Instruction *I);
164 bool SelectFPExt(const Instruction *I);
165 bool SelectFPTrunc(const Instruction *I);
Chad Rosier685b20c2012-02-06 23:50:07 +0000166 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
167 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosiere023d5d2012-02-03 21:14:11 +0000168 bool SelectIToFP(const Instruction *I, bool isSigned);
169 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosieraaa55a82012-02-03 21:07:27 +0000170 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosierb84a4b42012-02-03 21:23:45 +0000171 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosiera7ebc562011-11-11 23:31:03 +0000172 bool SelectCall(const Instruction *I, const char *IntrMemName);
173 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000174 bool SelectSelect(const Instruction *I);
Eric Christopher93bbe652010-10-22 01:28:00 +0000175 bool SelectRet(const Instruction *I);
Chad Rosieree7e4522011-11-02 00:18:48 +0000176 bool SelectTrunc(const Instruction *I);
177 bool SelectIntExt(const Instruction *I);
Jush Lu4705da92012-08-03 02:37:48 +0000178 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopher84bdfd82010-07-21 22:26:11 +0000179
Eric Christopher00202ee2010-08-23 21:44:12 +0000180 // Utility routines.
Eric Christopher0d274a02010-08-19 00:37:05 +0000181 private:
Jim Grosbach06c2a682013-08-16 23:37:31 +0000182 unsigned constrainOperandRegClass(const MCInstrDesc &II, unsigned OpNum,
183 unsigned Op);
Chris Lattner229907c2011-07-18 04:54:35 +0000184 bool isTypeLegal(Type *Ty, MVT &VT);
185 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosier9cf803c2011-11-02 18:08:25 +0000186 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
187 bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000188 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosiera26979b2011-12-14 17:26:05 +0000189 unsigned Alignment = 0, bool isZExt = true,
190 bool allocReg = true);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000191 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +0000192 unsigned Alignment = 0);
Eric Christopherfef5f312010-11-19 22:30:02 +0000193 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier150d35b2012-12-17 22:35:29 +0000194 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier057b6d32011-11-14 23:04:09 +0000195 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosier9f5c68a2012-12-06 01:34:31 +0000196 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
197 unsigned Alignment);
Chad Rosier62a144f2012-12-17 19:59:43 +0000198 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000199 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
200 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
201 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
202 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
203 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000204 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000205 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000206
Eric Christopher72497e52010-09-10 23:18:12 +0000207 // Call handling routines.
208 private:
Jush Lue67e07b2012-07-19 09:49:00 +0000209 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
210 bool Return,
211 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000212 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000213 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000214 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000215 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
216 SmallVectorImpl<unsigned> &RegArgs,
217 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000218 unsigned &NumBytes,
219 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000220 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000221 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000222 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000223 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000224 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000225
226 // OptionalDef handling routines.
227 private:
Eric Christopher174d8722011-03-12 01:09:29 +0000228 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000229 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
230 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000231 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000232 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000233 unsigned Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000234};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000235
236} // end anonymous namespace
237
Eric Christopher72497e52010-09-10 23:18:12 +0000238#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000239
Eric Christopher0d274a02010-08-19 00:37:05 +0000240// DefinesOptionalPredicate - This is different from DefinesPredicate in that
241// we don't care about implicit defs here, just places we'll need to add a
242// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
243bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000244 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000245 return false;
246
247 // Look to see if our OptionalDef is defining CPSR or CCR.
248 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
249 const MachineOperand &MO = MI->getOperand(i);
Eric Christopher985d9e42010-08-20 00:36:24 +0000250 if (!MO.isReg() || !MO.isDef()) continue;
251 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000252 *CPSR = true;
253 }
254 return true;
255}
256
Eric Christopher174d8722011-03-12 01:09:29 +0000257bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000258 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000259
Joey Goulya5153cb2013-09-09 14:21:49 +0000260 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000261 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000262 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000263 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000264
Evan Cheng6cc775f2011-06-28 19:10:37 +0000265 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
266 if (MCID.OpInfo[i].isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000267 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000268
Eric Christopher174d8722011-03-12 01:09:29 +0000269 return false;
270}
271
Eric Christopher0d274a02010-08-19 00:37:05 +0000272// If the machine is predicable go ahead and add the predicate operands, if
273// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000274// TODO: If we want to support thumb1 then we'll need to deal with optional
275// CPSR defs that need to be added before the remaining operands. See s_cc_out
276// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000277const MachineInstrBuilder &
278ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
279 MachineInstr *MI = &*MIB;
280
Eric Christopher174d8722011-03-12 01:09:29 +0000281 // Do we use a predicate? or...
282 // Are we NEON in ARM mode and have a predicate operand? If so, I know
283 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000284 if (isARMNEONPred(MI))
Eric Christopher0d274a02010-08-19 00:37:05 +0000285 AddDefaultPred(MIB);
Eric Christopher501d2e22011-04-29 00:03:10 +0000286
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000287 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000288 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000289 bool CPSR = false;
Eric Christopher0d274a02010-08-19 00:37:05 +0000290 if (DefinesOptionalPredicate(MI, &CPSR)) {
291 if (CPSR)
292 AddDefaultT1CC(MIB);
293 else
294 AddDefaultCC(MIB);
295 }
296 return MIB;
297}
298
Jim Grosbach06c2a682013-08-16 23:37:31 +0000299unsigned ARMFastISel::constrainOperandRegClass(const MCInstrDesc &II,
300 unsigned Op, unsigned OpNum) {
301 if (TargetRegisterInfo::isVirtualRegister(Op)) {
302 const TargetRegisterClass *RegClass =
303 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
304 if (!MRI.constrainRegClass(Op, RegClass)) {
305 // If it's not legal to COPY between the register classes, something
306 // has gone very wrong before we got here.
307 unsigned NewOp = createResultReg(RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach06c2a682013-08-16 23:37:31 +0000309 TII.get(TargetOpcode::COPY), NewOp).addReg(Op));
310 return NewOp;
311 }
312 }
313 return Op;
314}
315
Eric Christopher09f757d2010-08-17 01:25:29 +0000316unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
317 const TargetRegisterClass* RC) {
318 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000319 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000320
Rafael Espindolaea09c592014-02-18 22:05:46 +0000321 AddOptionalDefs(
322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg));
Eric Christopher09f757d2010-08-17 01:25:29 +0000323 return ResultReg;
324}
325
326unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
327 const TargetRegisterClass *RC,
328 unsigned Op0, bool Op0IsKill) {
329 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000330 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000331
Jim Grosbach06c2a682013-08-16 23:37:31 +0000332 // Make sure the input operand is sufficiently constrained to be legal
333 // for this instruction.
334 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000335 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000336 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
337 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000338 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000340 .addReg(Op0, Op0IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000341 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000342 TII.get(TargetOpcode::COPY), ResultReg)
343 .addReg(II.ImplicitDefs[0]));
344 }
345 return ResultReg;
346}
347
348unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
349 const TargetRegisterClass *RC,
350 unsigned Op0, bool Op0IsKill,
351 unsigned Op1, bool Op1IsKill) {
352 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000353 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000354
Jim Grosbach06c2a682013-08-16 23:37:31 +0000355 // Make sure the input operands are sufficiently constrained to be legal
356 // for this instruction.
357 Op0 = constrainOperandRegClass(II, Op0, 1);
358 Op1 = constrainOperandRegClass(II, Op1, 2);
359
Chad Rosier0bc51322012-02-15 17:36:21 +0000360 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000361 AddOptionalDefs(
362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
363 .addReg(Op0, Op0IsKill * RegState::Kill)
364 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000365 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000366 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000367 .addReg(Op0, Op0IsKill * RegState::Kill)
368 .addReg(Op1, Op1IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000369 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000370 TII.get(TargetOpcode::COPY), ResultReg)
371 .addReg(II.ImplicitDefs[0]));
372 }
373 return ResultReg;
374}
375
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000376unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
377 const TargetRegisterClass *RC,
378 unsigned Op0, bool Op0IsKill,
379 unsigned Op1, bool Op1IsKill,
380 unsigned Op2, bool Op2IsKill) {
381 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000382 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000383
Jim Grosbach06c2a682013-08-16 23:37:31 +0000384 // Make sure the input operands are sufficiently constrained to be legal
385 // for this instruction.
386 Op0 = constrainOperandRegClass(II, Op0, 1);
387 Op1 = constrainOperandRegClass(II, Op1, 2);
388 Op2 = constrainOperandRegClass(II, Op1, 3);
389
Chad Rosier0bc51322012-02-15 17:36:21 +0000390 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000391 AddOptionalDefs(
392 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
393 .addReg(Op0, Op0IsKill * RegState::Kill)
394 .addReg(Op1, Op1IsKill * RegState::Kill)
395 .addReg(Op2, Op2IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000396 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000398 .addReg(Op0, Op0IsKill * RegState::Kill)
399 .addReg(Op1, Op1IsKill * RegState::Kill)
400 .addReg(Op2, Op2IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000401 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000402 TII.get(TargetOpcode::COPY), ResultReg)
403 .addReg(II.ImplicitDefs[0]));
404 }
405 return ResultReg;
406}
407
Eric Christopher09f757d2010-08-17 01:25:29 +0000408unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
409 const TargetRegisterClass *RC,
410 unsigned Op0, bool Op0IsKill,
411 uint64_t Imm) {
412 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000413 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000414
Jim Grosbach06c2a682013-08-16 23:37:31 +0000415 // Make sure the input operand is sufficiently constrained to be legal
416 // for this instruction.
417 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000418 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000419 AddOptionalDefs(
420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
421 .addReg(Op0, Op0IsKill * RegState::Kill)
422 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000423 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000425 .addReg(Op0, Op0IsKill * RegState::Kill)
426 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000428 TII.get(TargetOpcode::COPY), ResultReg)
429 .addReg(II.ImplicitDefs[0]));
430 }
431 return ResultReg;
432}
433
434unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
435 const TargetRegisterClass *RC,
436 unsigned Op0, bool Op0IsKill,
437 const ConstantFP *FPImm) {
438 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000439 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000440
Jim Grosbach06c2a682013-08-16 23:37:31 +0000441 // Make sure the input operand is sufficiently constrained to be legal
442 // for this instruction.
443 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000444 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000445 AddOptionalDefs(
446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
447 .addReg(Op0, Op0IsKill * RegState::Kill)
448 .addFPImm(FPImm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000449 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000450 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000451 .addReg(Op0, Op0IsKill * RegState::Kill)
452 .addFPImm(FPImm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000453 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000454 TII.get(TargetOpcode::COPY), ResultReg)
455 .addReg(II.ImplicitDefs[0]));
456 }
457 return ResultReg;
458}
459
460unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
461 const TargetRegisterClass *RC,
462 unsigned Op0, bool Op0IsKill,
463 unsigned Op1, bool Op1IsKill,
464 uint64_t Imm) {
465 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000466 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000467
Jim Grosbach06c2a682013-08-16 23:37:31 +0000468 // Make sure the input operands are sufficiently constrained to be legal
469 // for this instruction.
470 Op0 = constrainOperandRegClass(II, Op0, 1);
471 Op1 = constrainOperandRegClass(II, Op1, 2);
Chad Rosier0bc51322012-02-15 17:36:21 +0000472 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000473 AddOptionalDefs(
474 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
475 .addReg(Op0, Op0IsKill * RegState::Kill)
476 .addReg(Op1, Op1IsKill * RegState::Kill)
477 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000478 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000479 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000480 .addReg(Op0, Op0IsKill * RegState::Kill)
481 .addReg(Op1, Op1IsKill * RegState::Kill)
482 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000483 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000484 TII.get(TargetOpcode::COPY), ResultReg)
485 .addReg(II.ImplicitDefs[0]));
486 }
487 return ResultReg;
488}
489
490unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
491 const TargetRegisterClass *RC,
492 uint64_t Imm) {
493 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000494 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000495
Chad Rosier0bc51322012-02-15 17:36:21 +0000496 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000497 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
498 ResultReg).addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000499 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000500 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000501 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000502 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000503 TII.get(TargetOpcode::COPY), ResultReg)
504 .addReg(II.ImplicitDefs[0]));
505 }
506 return ResultReg;
507}
508
Eric Christopher77087462011-04-29 22:07:50 +0000509unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
510 const TargetRegisterClass *RC,
511 uint64_t Imm1, uint64_t Imm2) {
512 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000513 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher0713a9d2011-06-08 23:55:35 +0000514
Chad Rosier0bc51322012-02-15 17:36:21 +0000515 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000516 AddOptionalDefs(
517 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
518 .addImm(Imm1)
519 .addImm(Imm2));
Chad Rosier0bc51322012-02-15 17:36:21 +0000520 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000521 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher77087462011-04-29 22:07:50 +0000522 .addImm(Imm1).addImm(Imm2));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000523 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher77087462011-04-29 22:07:50 +0000524 TII.get(TargetOpcode::COPY),
525 ResultReg)
526 .addReg(II.ImplicitDefs[0]));
527 }
528 return ResultReg;
529}
530
Eric Christopher09f757d2010-08-17 01:25:29 +0000531unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
532 unsigned Op0, bool Op0IsKill,
533 uint32_t Idx) {
534 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
535 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
536 "Cannot yet extract from physregs");
Chad Rosier0bc51322012-02-15 17:36:21 +0000537
Eric Christopher0d274a02010-08-19 00:37:05 +0000538 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000539 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
Chad Rosier0bc51322012-02-15 17:36:21 +0000540 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
Eric Christopher09f757d2010-08-17 01:25:29 +0000541 return ResultReg;
542}
543
Eric Christopher860fc932010-09-10 00:34:35 +0000544// TODO: Don't worry about 64-bit now, but when this is fixed remove the
545// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000546unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000547 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000548
Eric Christopher4bd70472010-09-09 21:44:45 +0000549 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000550 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000551 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000552 .addReg(SrcReg));
553 return MoveReg;
554}
555
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000556unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000557 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000558
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000559 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000560 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000561 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000562 .addReg(SrcReg));
563 return MoveReg;
564}
565
Eric Christopher3cf63f12010-09-09 00:19:41 +0000566// For double width floating point we need to materialize two constants
567// (the high and the low) into integer registers then use a move to get
568// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000569unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000570 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000571 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000572
Eric Christopher3cf63f12010-09-09 00:19:41 +0000573 // This checks to see if we can use VFP3 instructions to materialize
574 // a constant, otherwise we have to go through the constant pool.
575 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000576 int Imm;
577 unsigned Opc;
578 if (is64bit) {
579 Imm = ARM_AM::getFP64Imm(Val);
580 Opc = ARM::FCONSTD;
581 } else {
582 Imm = ARM_AM::getFP32Imm(Val);
583 Opc = ARM::FCONSTS;
584 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000585 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000586 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
587 TII.get(Opc), DestReg).addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000588 return DestReg;
589 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000590
Eric Christopher860fc932010-09-10 00:34:35 +0000591 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000592 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000593
Eric Christopher22fd29a2010-09-09 23:50:00 +0000594 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000595 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000596 if (Align == 0) {
597 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000598 Align = DL.getTypeAllocSize(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000599 }
600 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
601 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
602 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000603
Eric Christopher860fc932010-09-10 00:34:35 +0000604 // The extra reg is for addrmode5.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000605 AddOptionalDefs(
606 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
607 .addConstantPoolIndex(Idx)
608 .addReg(0));
Eric Christopher22fd29a2010-09-09 23:50:00 +0000609 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000610}
611
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000612unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopher7ac602b2010-10-11 08:38:55 +0000613
Chad Rosier67f96882011-11-04 22:29:00 +0000614 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
615 return false;
Eric Christophere4dd7372010-11-03 20:21:17 +0000616
617 // If we can do this in a single instruction without a constant pool entry
618 // do so now.
619 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000620 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000621 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000622 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
623 &ARM::GPRRegClass;
624 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000625 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier67f96882011-11-04 22:29:00 +0000626 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000627 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000628 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000629 }
630
Chad Rosier2a3503e2011-11-11 00:36:21 +0000631 // Use MVN to emit negative constants.
632 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
633 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000634 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000635 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000636 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000637 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
638 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000639 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier2a3503e2011-11-11 00:36:21 +0000640 TII.get(Opc), ImmReg)
641 .addImm(Imm));
642 return ImmReg;
643 }
644 }
645
646 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000647 if (VT != MVT::i32)
648 return false;
649
650 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
651
Eric Christopherc3e118e2010-09-02 23:43:26 +0000652 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000653 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000654 if (Align == 0) {
655 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000656 Align = DL.getTypeAllocSize(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000657 }
658 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000659
Chad Rosier0439cfc2011-11-08 21:12:00 +0000660 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000661 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000662 TII.get(ARM::t2LDRpci), DestReg)
663 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000664 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000665 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000666 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000667 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000668 TII.get(ARM::LDRcp), DestReg)
669 .addConstantPoolIndex(Idx)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000670 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000671 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000672
Eric Christopherc3e118e2010-09-02 23:43:26 +0000673 return DestReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000674}
675
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000676unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000677 // For now 32-bit only.
Duncan Sands14627772010-11-03 12:17:33 +0000678 if (VT != MVT::i32) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000679
Eric Christopher7787f792010-10-02 00:32:44 +0000680 Reloc::Model RelocM = TM.getRelocationModel();
Jush Lue87e5592012-08-29 02:41:21 +0000681 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier65710a72012-11-07 00:13:01 +0000682 const TargetRegisterClass *RC = isThumb2 ?
683 (const TargetRegisterClass*)&ARM::rGPRRegClass :
684 (const TargetRegisterClass*)&ARM::GPRRegClass;
685 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000686
Tim Northoverd6a729b2014-01-06 14:28:05 +0000687 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000688 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
689 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000690 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000691
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000692 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000693 // Non-darwin targets only support static movt relocations in FastISel.
Jakob Stoklund Olesen083dbdc2012-01-07 20:49:15 +0000694 if (Subtarget->useMovt() &&
Tim Northoverd6a729b2014-01-06 14:28:05 +0000695 (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000696 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000697 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000698 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000699 TF = ARMII::MO_NONLAZY;
700
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000701 switch (RelocM) {
702 case Reloc::PIC_:
703 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
704 break;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000705 default:
706 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
707 break;
708 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000709 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
710 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000711 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000712 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000713 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000714 if (Align == 0) {
715 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000716 Align = DL.getTypeAllocSize(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000717 }
718
Jush Lu47172a02012-09-27 05:21:41 +0000719 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
720 return ARMLowerPICELF(GV, Align, VT);
721
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000722 // Grab index.
723 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
724 (Subtarget->isThumb() ? 4 : 8);
725 unsigned Id = AFI->createPICLabelUId();
726 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
727 ARMCP::CPValue,
728 PCAdj);
729 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
730
731 // Load value.
732 MachineInstrBuilder MIB;
733 if (isThumb2) {
734 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000735 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
736 DestReg).addConstantPoolIndex(Idx);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000737 if (RelocM == Reloc::PIC_)
738 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000739 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000740 } else {
741 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000742 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000743 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
744 TII.get(ARM::LDRcp), DestReg)
745 .addConstantPoolIndex(Idx)
746 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000747 AddOptionalDefs(MIB);
748
749 if (RelocM == Reloc::PIC_) {
750 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
751 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
752
753 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000754 DbgLoc, TII.get(Opc), NewDestReg)
Jush Lue87e5592012-08-29 02:41:21 +0000755 .addReg(DestReg)
756 .addImm(Id);
757 AddOptionalDefs(MIB);
758 return NewDestReg;
759 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000760 }
Eric Christopher7787f792010-10-02 00:32:44 +0000761 }
Eli Friedman86585792011-06-03 01:13:19 +0000762
Jush Lue87e5592012-08-29 02:41:21 +0000763 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000764 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000765 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000766 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000767 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000768 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000769 .addReg(DestReg)
770 .addImm(0);
771 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000772 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
773 TII.get(ARM::LDRi12), NewDestReg)
774 .addReg(DestReg)
775 .addImm(0);
Eli Friedman86585792011-06-03 01:13:19 +0000776 DestReg = NewDestReg;
777 AddOptionalDefs(MIB);
778 }
779
Eric Christopher7787f792010-10-02 00:32:44 +0000780 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000781}
782
Eric Christopher3cf63f12010-09-09 00:19:41 +0000783unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglundc494d242012-12-17 14:30:06 +0000784 EVT CEVT = TLI.getValueType(C->getType(), true);
785
786 // Only handle simple types.
787 if (!CEVT.isSimple()) return 0;
788 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000789
790 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
791 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000792 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
793 return ARMMaterializeGV(GV, VT);
794 else if (isa<ConstantInt>(C))
795 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000796
Eric Christopher83a5ec82010-10-01 23:24:42 +0000797 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000798}
799
Chad Rosier0eff3e52011-11-17 21:46:13 +0000800// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
801
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000802unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
803 // Don't handle dynamic allocas.
804 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000805
Duncan Sandsf5dda012010-11-03 11:35:31 +0000806 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000807 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000808
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000809 DenseMap<const AllocaInst*, int>::iterator SI =
810 FuncInfo.StaticAllocaMap.find(AI);
811
812 // This will get lowered later into the correct offsets and registers
813 // via rewriteXFrameIndex.
814 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000815 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000816 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000817 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000818 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
819
Rafael Espindolaea09c592014-02-18 22:05:46 +0000820 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000821 TII.get(Opc), ResultReg)
822 .addFrameIndex(SI->second)
823 .addImm(0));
824 return ResultReg;
825 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000826
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000827 return 0;
828}
829
Chris Lattner229907c2011-07-18 04:54:35 +0000830bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000831 EVT evt = TLI.getValueType(Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000832
Eric Christopher761e7fb2010-08-25 07:23:49 +0000833 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000834 if (evt == MVT::Other || !evt.isSimple()) return false;
835 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000836
Eric Christopher901176a2010-08-31 01:28:42 +0000837 // Handle all legal types, i.e. a register that will directly hold this
838 // value.
839 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000840}
841
Chris Lattner229907c2011-07-18 04:54:35 +0000842bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000843 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000844
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000845 // If this is a type than can be sign or zero-extended to a basic operation
846 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000847 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000848 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000849
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000850 return false;
851}
852
Eric Christopher558b61e2010-11-19 22:36:41 +0000853// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000854bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000855 // Some boilerplate from the X86 FastISel.
856 const User *U = NULL;
Eric Christopher00202ee2010-08-23 21:44:12 +0000857 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000858 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000859 // Don't walk into other basic blocks unless the object is an alloca from
860 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000861 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
862 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
863 Opcode = I->getOpcode();
864 U = I;
865 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000866 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000867 Opcode = C->getOpcode();
868 U = C;
869 }
870
Chris Lattner229907c2011-07-18 04:54:35 +0000871 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000872 if (Ty->getAddressSpace() > 255)
873 // Fast instruction selection doesn't support the special
874 // address spaces.
875 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000876
Eric Christopher00202ee2010-08-23 21:44:12 +0000877 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000878 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000879 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000880 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000881 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000882 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000883 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000884 // Look past no-op inttoptrs.
885 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000886 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000887 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000888 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000889 // Look past no-op ptrtoints.
890 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000891 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000892 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000893 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000894 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000895 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000896
Eric Christopher21d0c172010-10-14 09:29:41 +0000897 // Iterate through the GEP folding the constants into offsets where
898 // we can.
899 gep_type_iterator GTI = gep_type_begin(U);
900 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
901 i != e; ++i, ++GTI) {
902 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000903 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000904 const StructLayout *SL = DL.getStructLayout(STy);
Eric Christopher21d0c172010-10-14 09:29:41 +0000905 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
906 TmpOffset += SL->getElementOffset(Idx);
907 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000908 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000909 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000910 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
911 // Constant-offset addressing.
912 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000913 break;
914 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000915 if (canFoldAddIntoGEP(U, Op)) {
916 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000917 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000918 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000919 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000920 // Iterate on the other operand.
921 Op = cast<AddOperator>(Op)->getOperand(0);
922 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000923 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000924 // Unsupported
925 goto unsupported_gep;
926 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000927 }
928 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000929
930 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000931 Addr.Offset = TmpOffset;
932 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000933
934 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000935 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000936
Eric Christopher21d0c172010-10-14 09:29:41 +0000937 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000938 break;
939 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000940 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000941 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000942 DenseMap<const AllocaInst*, int>::iterator SI =
943 FuncInfo.StaticAllocaMap.find(AI);
944 if (SI != FuncInfo.StaticAllocaMap.end()) {
945 Addr.BaseType = Address::FrameIndexBase;
946 Addr.Base.FI = SI->second;
947 return true;
948 }
949 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000950 }
951 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000952
Eric Christopher9d4e4712010-08-24 00:07:24 +0000953 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000954 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
955 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000956}
957
Chad Rosier150d35b2012-12-17 22:35:29 +0000958void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000959 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000960 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000961 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000962 case MVT::i1:
963 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000964 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000965 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000966 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000967 // Integer loads/stores handle 12-bit offsets.
968 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000969 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000970 if (needsLowering && isThumb2)
971 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
972 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000973 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000974 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000975 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000976 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000977 break;
978 case MVT::f32:
979 case MVT::f64:
980 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000981 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000982 break;
983 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000984
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000985 // If this is a stack pointer and the offset needs to be simplified then
986 // put the alloca address into a register, set the base type back to
987 // register and continue. This should almost never happen.
988 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topperc7242e02012-04-20 07:30:17 +0000989 const TargetRegisterClass *RC = isThumb2 ?
990 (const TargetRegisterClass*)&ARM::tGPRRegClass :
991 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000992 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000993 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000994 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000995 TII.get(Opc), ResultReg)
996 .addFrameIndex(Addr.Base.FI)
997 .addImm(0));
998 Addr.Base.Reg = ResultReg;
999 Addr.BaseType = Address::RegBase;
1000 }
1001
Eric Christopher73bc5b02010-10-21 19:40:30 +00001002 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +00001003 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +00001004 if (needsLowering) {
Eli Friedman86caced2011-04-29 21:22:56 +00001005 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
1006 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +00001007 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +00001008 }
Eric Christopher00202ee2010-08-23 21:44:12 +00001009}
1010
Chad Rosier150d35b2012-12-17 22:35:29 +00001011void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +00001012 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001013 unsigned Flags, bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001014 // addrmode5 output depends on the selection dag addressing dividing the
1015 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +00001016 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +00001017 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +00001018
Eric Christopher119ff7f2010-12-01 01:40:24 +00001019 // Frame base works a bit differently. Handle it separately.
1020 if (Addr.BaseType == Address::FrameIndexBase) {
1021 int FI = Addr.Base.FI;
1022 int Offset = Addr.Offset;
1023 MachineMemOperand *MMO =
1024 FuncInfo.MF->getMachineMemOperand(
1025 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarich6528a542011-05-28 20:34:49 +00001026 Flags,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001027 MFI.getObjectSize(FI),
1028 MFI.getObjectAlignment(FI));
1029 // Now add the rest of the operands.
1030 MIB.addFrameIndex(FI);
1031
Bob Wilson80381f62011-12-04 00:52:23 +00001032 // ARM halfword load/stores and signed byte loads need an additional
1033 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001034 if (useAM3) {
1035 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1036 MIB.addReg(0);
1037 MIB.addImm(Imm);
1038 } else {
1039 MIB.addImm(Addr.Offset);
1040 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001041 MIB.addMemOperand(MMO);
1042 } else {
1043 // Now add the rest of the operands.
1044 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +00001045
Bob Wilson80381f62011-12-04 00:52:23 +00001046 // ARM halfword load/stores and signed byte loads need an additional
1047 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +00001048 if (useAM3) {
1049 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
1050 MIB.addReg(0);
1051 MIB.addImm(Imm);
1052 } else {
1053 MIB.addImm(Addr.Offset);
1054 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001055 }
1056 AddOptionalDefs(MIB);
1057}
1058
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001059bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +00001060 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +00001061 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001062 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +00001063 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +00001064 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001065 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001066 // This is mostly going to be Neon/vector support.
1067 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +00001068 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001069 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001070 if (isThumb2) {
1071 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1072 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1073 else
1074 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001075 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +00001076 if (isZExt) {
1077 Opc = ARM::LDRBi12;
1078 } else {
1079 Opc = ARM::LDRSB;
1080 useAM3 = true;
1081 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001082 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001083 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +00001084 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001085 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001086 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001087 return false;
1088
Chad Rosieradfd2002011-11-14 20:22:27 +00001089 if (isThumb2) {
1090 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1091 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1092 else
1093 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1094 } else {
1095 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1096 useAM3 = true;
1097 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001098 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001099 break;
Eric Christopher901176a2010-08-31 01:28:42 +00001100 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001101 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001102 return false;
1103
Chad Rosieradfd2002011-11-14 20:22:27 +00001104 if (isThumb2) {
1105 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1106 Opc = ARM::t2LDRi8;
1107 else
1108 Opc = ARM::t2LDRi12;
1109 } else {
1110 Opc = ARM::LDRi12;
1111 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001112 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +00001113 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +00001114 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +00001115 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +00001116 // Unaligned loads need special handling. Floats require word-alignment.
1117 if (Alignment && Alignment < 4) {
1118 needVMOV = true;
1119 VT = MVT::i32;
1120 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +00001121 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +00001122 } else {
1123 Opc = ARM::VLDRS;
1124 RC = TLI.getRegClassFor(VT);
1125 }
Eric Christopheraef6499b2010-09-18 01:59:37 +00001126 break;
1127 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +00001128 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001129 // FIXME: Unaligned loads need special handling. Doublewords require
1130 // word-alignment.
1131 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +00001132 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001133
Eric Christopheraef6499b2010-09-18 01:59:37 +00001134 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +00001135 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +00001136 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001137 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001138 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001139 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001140
Eric Christopher119ff7f2010-12-01 01:40:24 +00001141 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001142 if (allocReg)
1143 ResultReg = createResultReg(RC);
1144 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Rafael Espindolaea09c592014-02-18 22:05:46 +00001145 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001146 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001147 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001148
1149 // If we had an unaligned load of a float we've converted it to an regular
1150 // load. Now we must move from the GRP to the FP register.
1151 if (needVMOV) {
1152 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001153 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier563de602011-12-13 19:22:14 +00001154 TII.get(ARM::VMOVSR), MoveReg)
1155 .addReg(ResultReg));
1156 ResultReg = MoveReg;
1157 }
Eric Christopher901176a2010-08-31 01:28:42 +00001158 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001159}
1160
Eric Christopher29ab6d12010-09-27 06:02:23 +00001161bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001162 // Atomic loads need special handling.
1163 if (cast<LoadInst>(I)->isAtomic())
1164 return false;
1165
Eric Christopher860fc932010-09-10 00:34:35 +00001166 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001167 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001168 if (!isLoadTypeLegal(I->getType(), VT))
1169 return false;
1170
Eric Christopher119ff7f2010-12-01 01:40:24 +00001171 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001172 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001173 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001174
1175 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001176 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1177 return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001178 UpdateValueMap(I, ResultReg);
1179 return true;
1180}
1181
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001182bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001183 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001184 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001185 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001186 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001187 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001188 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001189 case MVT::i1: {
Craig Topperc7242e02012-04-20 07:30:17 +00001190 unsigned Res = createResultReg(isThumb2 ?
1191 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1192 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001193 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001194 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001195 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher1e43892e2010-11-02 23:59:09 +00001196 TII.get(Opc), Res)
1197 .addReg(SrcReg).addImm(1));
1198 SrcReg = Res;
1199 } // Fallthrough here.
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001200 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001201 if (isThumb2) {
1202 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1203 StrOpc = ARM::t2STRBi8;
1204 else
1205 StrOpc = ARM::t2STRBi12;
1206 } else {
1207 StrOpc = ARM::STRBi12;
1208 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001209 break;
1210 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001211 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001212 return false;
1213
Chad Rosieradfd2002011-11-14 20:22:27 +00001214 if (isThumb2) {
1215 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1216 StrOpc = ARM::t2STRHi8;
1217 else
1218 StrOpc = ARM::t2STRHi12;
1219 } else {
1220 StrOpc = ARM::STRH;
1221 useAM3 = true;
1222 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001223 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001224 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001225 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001226 return false;
1227
Chad Rosieradfd2002011-11-14 20:22:27 +00001228 if (isThumb2) {
1229 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1230 StrOpc = ARM::t2STRi8;
1231 else
1232 StrOpc = ARM::t2STRi12;
1233 } else {
1234 StrOpc = ARM::STRi12;
1235 }
Eric Christopherc918d552010-10-16 01:10:35 +00001236 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001237 case MVT::f32:
1238 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001239 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001240 if (Alignment && Alignment < 4) {
1241 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosierec3b77e2011-12-03 02:21:57 +00001243 TII.get(ARM::VMOVRS), MoveReg)
1244 .addReg(SrcReg));
1245 SrcReg = MoveReg;
1246 VT = MVT::i32;
1247 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001248 } else {
1249 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001250 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001251 break;
1252 case MVT::f64:
1253 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001254 // FIXME: Unaligned stores need special handling. Doublewords require
1255 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001256 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001257 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001258
Eric Christopherc3e118e2010-09-02 23:43:26 +00001259 StrOpc = ARM::VSTRD;
1260 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001261 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001262 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001263 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001264
Eric Christopher119ff7f2010-12-01 01:40:24 +00001265 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001266 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001267 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001268 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001269 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001270 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001271 return true;
1272}
1273
Eric Christopher29ab6d12010-09-27 06:02:23 +00001274bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001275 Value *Op0 = I->getOperand(0);
1276 unsigned SrcReg = 0;
1277
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001278 // Atomic stores need special handling.
1279 if (cast<StoreInst>(I)->isAtomic())
1280 return false;
1281
Eric Christopher119ff7f2010-12-01 01:40:24 +00001282 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001283 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001284 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001285 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001286
Eric Christopher92db2012010-09-02 01:48:11 +00001287 // Get the value to be stored into a register.
1288 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001289 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001290
Eric Christopher119ff7f2010-12-01 01:40:24 +00001291 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001292 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001293 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001294 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001295
Chad Rosierec3b77e2011-12-03 02:21:57 +00001296 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1297 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001298 return true;
1299}
1300
1301static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1302 switch (Pred) {
1303 // Needs two compares...
1304 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001305 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001306 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001307 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001308 return ARMCC::AL;
1309 case CmpInst::ICMP_EQ:
1310 case CmpInst::FCMP_OEQ:
1311 return ARMCC::EQ;
1312 case CmpInst::ICMP_SGT:
1313 case CmpInst::FCMP_OGT:
1314 return ARMCC::GT;
1315 case CmpInst::ICMP_SGE:
1316 case CmpInst::FCMP_OGE:
1317 return ARMCC::GE;
1318 case CmpInst::ICMP_UGT:
1319 case CmpInst::FCMP_UGT:
1320 return ARMCC::HI;
1321 case CmpInst::FCMP_OLT:
1322 return ARMCC::MI;
1323 case CmpInst::ICMP_ULE:
1324 case CmpInst::FCMP_OLE:
1325 return ARMCC::LS;
1326 case CmpInst::FCMP_ORD:
1327 return ARMCC::VC;
1328 case CmpInst::FCMP_UNO:
1329 return ARMCC::VS;
1330 case CmpInst::FCMP_UGE:
1331 return ARMCC::PL;
1332 case CmpInst::ICMP_SLT:
1333 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001334 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001335 case CmpInst::ICMP_SLE:
1336 case CmpInst::FCMP_ULE:
1337 return ARMCC::LE;
1338 case CmpInst::FCMP_UNE:
1339 case CmpInst::ICMP_NE:
1340 return ARMCC::NE;
1341 case CmpInst::ICMP_UGE:
1342 return ARMCC::HS;
1343 case CmpInst::ICMP_ULT:
1344 return ARMCC::LO;
1345 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001346}
1347
Eric Christopher29ab6d12010-09-27 06:02:23 +00001348bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001349 const BranchInst *BI = cast<BranchInst>(I);
1350 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1351 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001352
Eric Christopher6aaed722010-09-03 00:35:47 +00001353 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001354
Eric Christopher5c308f82010-10-29 21:08:19 +00001355 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1356 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001357 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001358 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001359
1360 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001361 // Try to take advantage of fallthrough opportunities.
1362 CmpInst::Predicate Predicate = CI->getPredicate();
1363 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1364 std::swap(TBB, FBB);
1365 Predicate = CmpInst::getInversePredicate(Predicate);
1366 }
1367
1368 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001369
1370 // We may not handle every CC for now.
1371 if (ARMPred == ARMCC::AL) return false;
1372
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001373 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001374 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001375 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001376
Chad Rosier0439cfc2011-11-08 21:12:00 +00001377 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher5c308f82010-10-29 21:08:19 +00001379 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001380 FastEmitBranch(FBB, DbgLoc);
Eric Christopher5c308f82010-10-29 21:08:19 +00001381 FuncInfo.MBB->addSuccessor(TBB);
1382 return true;
1383 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001384 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1385 MVT SourceVT;
1386 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001387 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001388 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001389 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001390 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001391 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8d46b472011-04-29 20:02:39 +00001392 TII.get(TstOpc))
1393 .addReg(OpReg).addImm(1));
1394
1395 unsigned CCMode = ARMCC::NE;
1396 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1397 std::swap(TBB, FBB);
1398 CCMode = ARMCC::EQ;
1399 }
1400
Chad Rosier0439cfc2011-11-08 21:12:00 +00001401 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001402 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher8d46b472011-04-29 20:02:39 +00001403 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1404
Rafael Espindolaea09c592014-02-18 22:05:46 +00001405 FastEmitBranch(FBB, DbgLoc);
Eric Christopher8d46b472011-04-29 20:02:39 +00001406 FuncInfo.MBB->addSuccessor(TBB);
1407 return true;
1408 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001409 } else if (const ConstantInt *CI =
1410 dyn_cast<ConstantInt>(BI->getCondition())) {
1411 uint64_t Imm = CI->getZExtValue();
1412 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001413 FastEmitBranch(Target, DbgLoc);
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001414 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001415 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001416
Eric Christopher5c308f82010-10-29 21:08:19 +00001417 unsigned CmpReg = getRegForValue(BI->getCondition());
1418 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001419
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001420 // We've been divorced from our compare! Our block was split, and
1421 // now our compare lives in a predecessor block. We musn't
1422 // re-compare here, as the children of the compare aren't guaranteed
1423 // live across the block boundary (we *could* check for this).
1424 // Regardless, the compare has been done in the predecessor block,
1425 // and it left a value for us in a virtual register. Ergo, we test
1426 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001427 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001428 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001429 AddOptionalDefs(
1430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1431 .addReg(CmpReg)
1432 .addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001433
Eric Christopher4f012fd2011-04-28 16:52:09 +00001434 unsigned CCMode = ARMCC::NE;
1435 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1436 std::swap(TBB, FBB);
1437 CCMode = ARMCC::EQ;
1438 }
1439
Chad Rosier0439cfc2011-11-08 21:12:00 +00001440 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001441 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001442 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001443 FastEmitBranch(FBB, DbgLoc);
Eric Christopher6aaed722010-09-03 00:35:47 +00001444 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001445 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001446}
1447
Chad Rosierded4c992012-02-07 23:56:08 +00001448bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1449 unsigned AddrReg = getRegForValue(I->getOperand(0));
1450 if (AddrReg == 0) return false;
1451
1452 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001453 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1454 TII.get(Opc)).addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001455
1456 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1457 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1458 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1459
Jush Luac96b762012-06-14 06:08:19 +00001460 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001461}
1462
Chad Rosier9cf803c2011-11-02 18:08:25 +00001463bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1464 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001465 Type *Ty = Src1Value->getType();
Patrik Hagglundc494d242012-12-17 14:30:06 +00001466 EVT SrcEVT = TLI.getValueType(Ty, true);
1467 if (!SrcEVT.isSimple()) return false;
1468 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001469
Chad Rosier78127d32011-10-26 23:25:44 +00001470 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1471 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001472 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001473
Chad Rosier595d4192011-11-09 03:22:02 +00001474 // Check to see if the 2nd operand is a constant that we can encode directly
1475 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001476 int Imm = 0;
1477 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001478 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001479 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1480 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001481 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1482 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1483 SrcVT == MVT::i1) {
1484 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001485 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001486 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1487 // then a cmn, because there is no way to represent 2147483648 as a
1488 // signed 32-bit int.
1489 if (Imm < 0 && Imm != (int)0x80000000) {
1490 isNegativeImm = true;
1491 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001492 }
Chad Rosier26d05882012-03-15 22:54:20 +00001493 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1494 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001495 }
1496 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1497 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1498 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001499 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001500 }
1501
Eric Christopherc3e9c402010-09-08 23:13:45 +00001502 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001503 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001504 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001505 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001506 default: return false;
1507 // TODO: Verify compares.
1508 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001509 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001510 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001511 break;
1512 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001513 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001514 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001515 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001516 case MVT::i1:
1517 case MVT::i8:
1518 case MVT::i16:
1519 needsExt = true;
1520 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001521 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001522 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001523 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001524 CmpOpc = ARM::t2CMPrr;
1525 else
Bill Wendling4b796472012-06-11 08:07:26 +00001526 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001527 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001528 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001529 CmpOpc = ARM::CMPrr;
1530 else
Bill Wendling4b796472012-06-11 08:07:26 +00001531 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001532 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001533 break;
1534 }
1535
Chad Rosier9cf803c2011-11-02 18:08:25 +00001536 unsigned SrcReg1 = getRegForValue(Src1Value);
1537 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001538
Duncan Sands12330652011-11-28 10:31:27 +00001539 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001540 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001541 SrcReg2 = getRegForValue(Src2Value);
1542 if (SrcReg2 == 0) return false;
1543 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001544
1545 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1546 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001547 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1548 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001549 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001550 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1551 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001552 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001553 }
Chad Rosier59a20192011-10-26 22:47:55 +00001554
Jim Grosbachd7866792013-08-16 23:37:40 +00001555 const MCInstrDesc &II = TII.get(CmpOpc);
1556 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001557 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001558 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001559 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001560 .addReg(SrcReg1).addReg(SrcReg2));
1561 } else {
1562 MachineInstrBuilder MIB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001563 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001564 .addReg(SrcReg1);
1565
1566 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1567 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001568 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001569 AddOptionalDefs(MIB);
1570 }
Chad Rosier78127d32011-10-26 23:25:44 +00001571
1572 // For floating point we need to move the result to a comparison register
1573 // that we can then use for branches.
1574 if (Ty->isFloatTy() || Ty->isDoubleTy())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier78127d32011-10-26 23:25:44 +00001576 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001577 return true;
1578}
1579
1580bool ARMFastISel::SelectCmp(const Instruction *I) {
1581 const CmpInst *CI = cast<CmpInst>(I);
1582
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001583 // Get the compare predicate.
1584 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001585
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001586 // We may not handle every CC for now.
1587 if (ARMPred == ARMCC::AL) return false;
1588
Chad Rosier59a20192011-10-26 22:47:55 +00001589 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001590 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001591 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001592
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001593 // Now set a register based on the comparison. Explicitly set the predicates
1594 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001595 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topperc7242e02012-04-20 07:30:17 +00001596 const TargetRegisterClass *RC = isThumb2 ?
1597 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1598 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001599 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001600 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001601 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001602 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001603 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001604 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001605 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001606
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001607 UpdateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001608 return true;
1609}
1610
Eric Christopher29ab6d12010-09-27 06:02:23 +00001611bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001612 // Make sure we have VFP and that we're extending float to double.
1613 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001614
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001615 Value *V = I->getOperand(0);
1616 if (!I->getType()->isDoubleTy() ||
1617 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001618
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001619 unsigned Op = getRegForValue(V);
1620 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001621
Craig Topperc7242e02012-04-20 07:30:17 +00001622 unsigned Result = createResultReg(&ARM::DPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001623 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001624 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001625 .addReg(Op));
1626 UpdateValueMap(I, Result);
1627 return true;
1628}
1629
Eric Christopher29ab6d12010-09-27 06:02:23 +00001630bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001631 // Make sure we have VFP and that we're truncating double to float.
1632 if (!Subtarget->hasVFP2()) return false;
1633
1634 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001635 if (!(I->getType()->isFloatTy() &&
1636 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001637
1638 unsigned Op = getRegForValue(V);
1639 if (Op == 0) return false;
1640
Craig Topperc7242e02012-04-20 07:30:17 +00001641 unsigned Result = createResultReg(&ARM::SPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001642 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001643 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001644 .addReg(Op));
1645 UpdateValueMap(I, Result);
1646 return true;
1647}
1648
Chad Rosiere023d5d2012-02-03 21:14:11 +00001649bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001650 // Make sure we have VFP.
1651 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001652
Duncan Sandsf5dda012010-11-03 11:35:31 +00001653 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001654 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001655 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001656 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001657
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001658 Value *Src = I->getOperand(0);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001659 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1660 if (!SrcEVT.isSimple())
1661 return false;
1662 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001663 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001664 return false;
1665
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001666 unsigned SrcReg = getRegForValue(Src);
1667 if (SrcReg == 0) return false;
1668
1669 // Handle sign-extension.
1670 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001671 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001672 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001673 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001674 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001675
Eric Christopher860fc932010-09-10 00:34:35 +00001676 // The conversion routine works on fp-reg to fp-reg and the operand above
1677 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001678 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001679 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001680
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001681 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001682 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1683 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001684 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001685
Eric Christopher4bd70472010-09-09 21:44:45 +00001686 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001687 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1688 TII.get(Opc), ResultReg).addReg(FP));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001689 UpdateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001690 return true;
1691}
1692
Chad Rosiere023d5d2012-02-03 21:14:11 +00001693bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001694 // Make sure we have VFP.
1695 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001696
Duncan Sandsf5dda012010-11-03 11:35:31 +00001697 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001698 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001699 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001700 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001701
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001702 unsigned Op = getRegForValue(I->getOperand(0));
1703 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001704
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001705 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001706 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001707 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1708 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001709 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001710
Chad Rosier41f0e782012-02-03 20:27:51 +00001711 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001712 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001713 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1714 TII.get(Opc), ResultReg).addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001715
Eric Christopher4bd70472010-09-09 21:44:45 +00001716 // This result needs to be in an integer register, but the conversion only
1717 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001718 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001719 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001720
Eric Christopher4bd70472010-09-09 21:44:45 +00001721 UpdateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001722 return true;
1723}
1724
Eric Christopher511aa312010-10-11 08:27:59 +00001725bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001726 MVT VT;
1727 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001728 return false;
1729
1730 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001731 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001732
1733 unsigned CondReg = getRegForValue(I->getOperand(0));
1734 if (CondReg == 0) return false;
1735 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1736 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001737
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001738 // Check to see if we can use an immediate in the conditional move.
1739 int Imm = 0;
1740 bool UseImm = false;
1741 bool isNegativeImm = false;
1742 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1743 assert (VT == MVT::i32 && "Expecting an i32.");
1744 Imm = (int)ConstInt->getValue().getZExtValue();
1745 if (Imm < 0) {
1746 isNegativeImm = true;
1747 Imm = ~Imm;
1748 }
1749 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1750 (ARM_AM::getSOImmVal(Imm) != -1);
1751 }
1752
Duncan Sands12330652011-11-28 10:31:27 +00001753 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001754 if (!UseImm) {
1755 Op2Reg = getRegForValue(I->getOperand(2));
1756 if (Op2Reg == 0) return false;
1757 }
1758
1759 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Jim Grosbachd7866792013-08-16 23:37:40 +00001760 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001761 AddOptionalDefs(
1762 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1763 .addReg(CondReg)
1764 .addImm(0));
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001765
1766 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001767 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001768 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001769 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001770 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1771 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001772 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1773 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001774 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001775 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001776 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001777 }
Eric Christopher511aa312010-10-11 08:27:59 +00001778 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001779 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001780 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001781 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001782 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1783 ResultReg)
1784 .addReg(Op2Reg)
1785 .addReg(Op1Reg)
1786 .addImm(ARMCC::NE)
1787 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001788 } else {
1789 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001790 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1791 ResultReg)
1792 .addReg(Op1Reg)
1793 .addImm(Imm)
1794 .addImm(ARMCC::EQ)
1795 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001796 }
Eric Christopher511aa312010-10-11 08:27:59 +00001797 UpdateValueMap(I, ResultReg);
1798 return true;
1799}
1800
Chad Rosieraaa55a82012-02-03 21:07:27 +00001801bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001802 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001803 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001804 if (!isTypeLegal(Ty, VT))
1805 return false;
1806
1807 // If we have integer div support we should have selected this automagically.
1808 // In case we have a real miss go ahead and return false and we'll pick
1809 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001810 if (Subtarget->hasDivide()) return false;
1811
Eric Christopher56094ff2010-09-30 22:34:19 +00001812 // Otherwise emit a libcall.
1813 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001814 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001815 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001816 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001817 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001818 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001819 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001820 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001821 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001822 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001823 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001824 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001825
Eric Christopher56094ff2010-09-30 22:34:19 +00001826 return ARMEmitLibcall(I, LC);
1827}
1828
Chad Rosierb84a4b42012-02-03 21:23:45 +00001829bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001830 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001831 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001832 if (!isTypeLegal(Ty, VT))
1833 return false;
1834
1835 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1836 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001837 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001838 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001839 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001840 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001841 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001842 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001843 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001844 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001845 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001846 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001847
Eric Christophereae1b382010-10-11 08:37:26 +00001848 return ARMEmitLibcall(I, LC);
1849}
1850
Chad Rosier685b20c2012-02-06 23:50:07 +00001851bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier685b20c2012-02-06 23:50:07 +00001852 EVT DestVT = TLI.getValueType(I->getType(), true);
1853
1854 // We can get here in the case when we have a binary operation on a non-legal
1855 // type and the target independent selector doesn't know how to handle it.
1856 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1857 return false;
Jush Luac96b762012-06-14 06:08:19 +00001858
Chad Rosierbd471252012-02-08 02:29:21 +00001859 unsigned Opc;
1860 switch (ISDOpcode) {
1861 default: return false;
1862 case ISD::ADD:
1863 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1864 break;
1865 case ISD::OR:
1866 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1867 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001868 case ISD::SUB:
1869 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1870 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001871 }
1872
Chad Rosier685b20c2012-02-06 23:50:07 +00001873 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1874 if (SrcReg1 == 0) return false;
1875
1876 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1877 // in the instruction, rather then materializing the value in a register.
1878 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1879 if (SrcReg2 == 0) return false;
1880
JF Bastien13969d02013-05-29 15:45:47 +00001881 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001882 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1883 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001884 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier685b20c2012-02-06 23:50:07 +00001885 TII.get(Opc), ResultReg)
1886 .addReg(SrcReg1).addReg(SrcReg2));
1887 UpdateValueMap(I, ResultReg);
1888 return true;
1889}
1890
1891bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001892 EVT FPVT = TLI.getValueType(I->getType(), true);
1893 if (!FPVT.isSimple()) return false;
1894 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001895
Eric Christopher24dc27f2010-09-09 00:53:57 +00001896 // We can get here in the case when we want to use NEON for our fp
1897 // operations, but can't figure out how to. Just use the vfp instructions
1898 // if we have them.
1899 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001900 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001901 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1902 if (isFloat && !Subtarget->hasVFP2())
1903 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001904
Eric Christopher24dc27f2010-09-09 00:53:57 +00001905 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001906 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001907 switch (ISDOpcode) {
1908 default: return false;
1909 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001910 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001911 break;
1912 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001913 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001914 break;
1915 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001916 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001917 break;
1918 }
Chad Rosier80979b62011-11-16 18:39:44 +00001919 unsigned Op1 = getRegForValue(I->getOperand(0));
1920 if (Op1 == 0) return false;
1921
1922 unsigned Op2 = getRegForValue(I->getOperand(1));
1923 if (Op2 == 0) return false;
1924
Chad Rosier62a144f2012-12-17 19:59:43 +00001925 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001926 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher24dc27f2010-09-09 00:53:57 +00001927 TII.get(Opc), ResultReg)
1928 .addReg(Op1).addReg(Op2));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001929 UpdateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001930 return true;
1931}
1932
Eric Christopher72497e52010-09-10 23:18:12 +00001933// Call Handling Code
1934
Jush Lue67e07b2012-07-19 09:49:00 +00001935// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001936// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001937CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1938 bool Return,
1939 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001940 switch (CC) {
1941 default:
1942 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001943 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001944 if (Subtarget->hasVFP2() && !isVarArg) {
1945 if (!Subtarget->isAAPCS_ABI())
1946 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1947 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1948 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1949 }
Evan Cheng21abfc92010-10-22 18:57:05 +00001950 // Fallthrough
1951 case CallingConv::C:
Eric Christopher72497e52010-09-10 23:18:12 +00001952 // Use target triple & subtarget features to do actual dispatch.
1953 if (Subtarget->isAAPCS_ABI()) {
1954 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001955 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001956 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1957 else
1958 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1959 } else
1960 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1961 case CallingConv::ARM_AAPCS_VFP:
Jush Lue67e07b2012-07-19 09:49:00 +00001962 if (!isVarArg)
1963 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1964 // Fall through to soft float variant, variadic functions don't
1965 // use hard floating point ABI.
Eric Christopher72497e52010-09-10 23:18:12 +00001966 case CallingConv::ARM_AAPCS:
1967 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1968 case CallingConv::ARM_APCS:
1969 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001970 case CallingConv::GHC:
1971 if (Return)
1972 llvm_unreachable("Can't return in GHC call convention");
1973 else
1974 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001975 }
1976}
1977
Eric Christopher79398062010-09-29 23:11:09 +00001978bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1979 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001980 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001981 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1982 SmallVectorImpl<unsigned> &RegArgs,
1983 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001984 unsigned &NumBytes,
1985 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001986 SmallVector<CCValAssign, 16> ArgLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00001987 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1988 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1989 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001990
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001991 // Check that we can handle all of the arguments. If we can't, then bail out
1992 // now before we add code to the MBB.
1993 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1994 CCValAssign &VA = ArgLocs[i];
1995 MVT ArgVT = ArgVTs[VA.getValNo()];
1996
1997 // We don't handle NEON/vector parameters yet.
1998 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1999 return false;
2000
2001 // Now copy/store arg to correct locations.
2002 if (VA.isRegLoc() && !VA.needsCustom()) {
2003 continue;
2004 } else if (VA.needsCustom()) {
2005 // TODO: We need custom lowering for vector (v2f64) args.
2006 if (VA.getLocVT() != MVT::f64 ||
2007 // TODO: Only handle register args for now.
2008 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
2009 return false;
2010 } else {
Craig Topper56710102013-08-15 02:33:50 +00002011 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002012 default:
2013 return false;
2014 case MVT::i1:
2015 case MVT::i8:
2016 case MVT::i16:
2017 case MVT::i32:
2018 break;
2019 case MVT::f32:
2020 if (!Subtarget->hasVFP2())
2021 return false;
2022 break;
2023 case MVT::f64:
2024 if (!Subtarget->hasVFP2())
2025 return false;
2026 break;
2027 }
2028 }
2029 }
2030
2031 // At the point, we are able to handle the call's arguments in fast isel.
2032
Eric Christopher79398062010-09-29 23:11:09 +00002033 // Get a count of how many bytes are to be pushed on the stack.
2034 NumBytes = CCInfo.getNextStackOffset();
2035
2036 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002037 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002038 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002039 TII.get(AdjStackDown))
2040 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00002041
2042 // Process the args.
2043 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2044 CCValAssign &VA = ArgLocs[i];
2045 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00002046 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00002047
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002048 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
2049 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00002050
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002051 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00002052 switch (VA.getLocInfo()) {
2053 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00002054 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002055 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002056 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
2057 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002058 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002059 break;
2060 }
Chad Rosierd0191a52011-11-05 20:16:15 +00002061 case CCValAssign::AExt:
2062 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00002063 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00002064 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00002065 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00002066 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00002067 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00002068 break;
2069 }
2070 case CCValAssign::BCvt: {
Wesley Peck527da1b2010-11-23 03:31:01 +00002071 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00002072 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00002073 assert(BC != 0 && "Failed to emit a bitcast!");
2074 Arg = BC;
2075 ArgVT = VA.getLocVT();
2076 break;
2077 }
2078 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00002079 }
2080
2081 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00002082 if (VA.isRegLoc() && !VA.needsCustom()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2084 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00002085 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002086 } else if (VA.needsCustom()) {
2087 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002088 assert(VA.getLocVT() == MVT::f64 &&
2089 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00002090
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002091 CCValAssign &NextVA = ArgLocs[++i];
2092
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002093 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2094 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002095
Rafael Espindolaea09c592014-02-18 22:05:46 +00002096 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002097 TII.get(ARM::VMOVRRD), VA.getLocReg())
2098 .addReg(NextVA.getLocReg(), RegState::Define)
2099 .addReg(Arg));
2100 RegArgs.push_back(VA.getLocReg());
2101 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002102 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002103 assert(VA.isMemLoc());
2104 // Need to store on the stack.
Eric Christopherfef5f312010-11-19 22:30:02 +00002105 Address Addr;
2106 Addr.BaseType = Address::RegBase;
2107 Addr.Base.Reg = ARM::SP;
2108 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002109
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002110 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2111 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002112 }
2113 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002114
Eric Christopher79398062010-09-29 23:11:09 +00002115 return true;
2116}
2117
Duncan Sandsf5dda012010-11-03 11:35:31 +00002118bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002119 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002120 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002121 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002122 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002123 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002124 TII.get(AdjStackUp))
2125 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002126
2127 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002128 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002129 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002130 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2131 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002132
2133 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002134 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002135 // For this move we copy into two registers and then move into the
2136 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002137 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002138 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002139 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002140 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopherc1e209d2010-10-01 00:00:11 +00002141 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002142 .addReg(RVLocs[0].getLocReg())
2143 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002144
Eric Christopheraf719ef2010-10-20 08:02:24 +00002145 UsedRegs.push_back(RVLocs[0].getLocReg());
2146 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002147
Eric Christopher7ac602b2010-10-11 08:38:55 +00002148 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002149 UpdateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002150 } else {
2151 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002152 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002153
2154 // Special handling for extended integers.
2155 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2156 CopyVT = MVT::i32;
2157
Craig Topper760b1342012-02-22 05:59:10 +00002158 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002159
Eric Christopherc1e209d2010-10-01 00:00:11 +00002160 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2162 TII.get(TargetOpcode::COPY),
Eric Christopherc1e209d2010-10-01 00:00:11 +00002163 ResultReg).addReg(RVLocs[0].getLocReg());
2164 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002165
Eric Christopher7ac602b2010-10-11 08:38:55 +00002166 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002167 UpdateValueMap(I, ResultReg);
2168 }
Eric Christopher79398062010-09-29 23:11:09 +00002169 }
2170
Eric Christopher7ac602b2010-10-11 08:38:55 +00002171 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002172}
2173
Eric Christopher93bbe652010-10-22 01:28:00 +00002174bool ARMFastISel::SelectRet(const Instruction *I) {
2175 const ReturnInst *Ret = cast<ReturnInst>(I);
2176 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002177
Eric Christopher93bbe652010-10-22 01:28:00 +00002178 if (!FuncInfo.CanLowerReturn)
2179 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002180
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002181 // Build a list of return value registers.
2182 SmallVector<unsigned, 4> RetRegs;
2183
Eric Christopher93bbe652010-10-22 01:28:00 +00002184 CallingConv::ID CC = F.getCallingConv();
2185 if (Ret->getNumOperands() > 0) {
2186 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002187 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher93bbe652010-10-22 01:28:00 +00002188
2189 // Analyze operands of the call, assigning locations to each operand.
2190 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbache7e2aca2011-09-13 20:30:37 +00002191 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002192 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2193 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002194
2195 const Value *RV = Ret->getOperand(0);
2196 unsigned Reg = getRegForValue(RV);
2197 if (Reg == 0)
2198 return false;
2199
2200 // Only handle a single return value for now.
2201 if (ValLocs.size() != 1)
2202 return false;
2203
2204 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002205
Eric Christopher93bbe652010-10-22 01:28:00 +00002206 // Don't bother handling odd stuff for now.
2207 if (VA.getLocInfo() != CCValAssign::Full)
2208 return false;
2209 // Only handle register returns for now.
2210 if (!VA.isRegLoc())
2211 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002212
2213 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier62a144f2012-12-17 19:59:43 +00002214 EVT RVEVT = TLI.getValueType(RV->getType());
2215 if (!RVEVT.isSimple()) return false;
2216 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002217 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002218 // Special handling for extended integers.
2219 if (RVVT != DestVT) {
2220 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2221 return false;
2222
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002223 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2224
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002225 // Perform extension if flagged as either zext or sext. Otherwise, do
2226 // nothing.
2227 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2228 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2229 if (SrcReg == 0) return false;
2230 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002231 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002232
Eric Christopher93bbe652010-10-22 01:28:00 +00002233 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002234 unsigned DstReg = VA.getLocReg();
2235 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2236 // Avoid a cross-class copy. This is very unlikely.
2237 if (!SrcRC->contains(DstReg))
2238 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002239 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2240 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
Eric Christopher93bbe652010-10-22 01:28:00 +00002241
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002242 // Add register to return instruction.
2243 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002244 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002245
Chad Rosier0439cfc2011-11-08 21:12:00 +00002246 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002247 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002248 TII.get(RetOpc));
2249 AddOptionalDefs(MIB);
2250 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2251 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002252 return true;
2253}
2254
Chad Rosierc6916f82012-06-12 19:25:13 +00002255unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2256 if (UseReg)
2257 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2258 else
2259 return isThumb2 ? ARM::tBL : ARM::BL;
2260}
2261
2262unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002263 // Manually compute the global's type to avoid building it when unnecessary.
2264 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2265 EVT LCREVT = TLI.getValueType(GVTy);
2266 if (!LCREVT.isSimple()) return 0;
2267
Bill Wendling76cce192013-12-29 08:00:04 +00002268 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Chad Rosierc6916f82012-06-12 19:25:13 +00002269 GlobalValue::ExternalLinkage, 0, Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002270 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002271 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002272}
2273
Eric Christopher8b912662010-09-14 23:03:37 +00002274// A quick function that will emit a call for a named libcall in F with the
2275// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002276// can emit a call for any libcall we can produce. This is an abridged version
2277// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002278// like computed function pointers or strange arguments at call sites.
2279// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2280// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002281bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2282 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002283
Eric Christopher8b912662010-09-14 23:03:37 +00002284 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002285 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002286 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002287 if (RetTy->isVoidTy())
2288 RetVT = MVT::isVoid;
2289 else if (!isTypeLegal(RetTy, RetVT))
2290 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002291
Chad Rosier90f9afe2012-05-11 18:51:55 +00002292 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002293 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002294 SmallVector<CCValAssign, 16> RVLocs;
2295 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002296 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002297 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2298 return false;
2299 }
2300
Eric Christopher79398062010-09-29 23:11:09 +00002301 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002302 SmallVector<Value*, 8> Args;
2303 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002304 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002305 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2306 Args.reserve(I->getNumOperands());
2307 ArgRegs.reserve(I->getNumOperands());
2308 ArgVTs.reserve(I->getNumOperands());
2309 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002310 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002311 Value *Op = I->getOperand(i);
2312 unsigned Arg = getRegForValue(Op);
2313 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002314
Chris Lattner229907c2011-07-18 04:54:35 +00002315 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002316 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002317 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002318
Eric Christopher8b912662010-09-14 23:03:37 +00002319 ISD::ArgFlagsTy Flags;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002320 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher8b912662010-09-14 23:03:37 +00002321 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002322
Eric Christopher8b912662010-09-14 23:03:37 +00002323 Args.push_back(Op);
2324 ArgRegs.push_back(Arg);
2325 ArgVTs.push_back(ArgVT);
2326 ArgFlags.push_back(Flags);
2327 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002328
Eric Christopher79398062010-09-29 23:11:09 +00002329 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002330 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002331 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002332 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2333 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002334 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002335
Chad Rosierc6916f82012-06-12 19:25:13 +00002336 unsigned CalleeReg = 0;
2337 if (EnableARMLongCalls) {
2338 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2339 if (CalleeReg == 0) return false;
2340 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002341
Chad Rosierc6916f82012-06-12 19:25:13 +00002342 // Issue the call.
2343 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2344 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002345 DbgLoc, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002346 // BL / BLX don't take a predicate, but tBL / tBLX do.
2347 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002348 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002349 if (EnableARMLongCalls)
2350 MIB.addReg(CalleeReg);
2351 else
2352 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002353
Eric Christopher8b912662010-09-14 23:03:37 +00002354 // Add implicit physical register uses to the call.
2355 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002356 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002357
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002358 // Add a register mask with the call-preserved registers.
2359 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2360 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2361
Eric Christopher79398062010-09-29 23:11:09 +00002362 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002363 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002364 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002365
Eric Christopher8b912662010-09-14 23:03:37 +00002366 // Set all unused physreg defs as dead.
2367 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002368
Eric Christopher8b912662010-09-14 23:03:37 +00002369 return true;
2370}
2371
Chad Rosiera7ebc562011-11-11 23:31:03 +00002372bool ARMFastISel::SelectCall(const Instruction *I,
2373 const char *IntrMemName = 0) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002374 const CallInst *CI = cast<CallInst>(I);
2375 const Value *Callee = CI->getCalledValue();
2376
Chad Rosiera7ebc562011-11-11 23:31:03 +00002377 // Can't handle inline asm.
2378 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002379
Chad Rosierdf42cf32012-12-11 00:18:02 +00002380 // Allow SelectionDAG isel to handle tail calls.
2381 if (CI->isTailCall()) return false;
2382
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002383 // Check the calling convention.
2384 ImmutableCallSite CS(CI);
2385 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002386
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002387 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002388
Chris Lattner229907c2011-07-18 04:54:35 +00002389 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2390 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Lue67e07b2012-07-19 09:49:00 +00002391 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002392
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002393 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002394 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002395 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002396 if (RetTy->isVoidTy())
2397 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002398 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2399 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002400 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002401
Chad Rosier90f9afe2012-05-11 18:51:55 +00002402 // Can't handle non-double multi-reg retvals.
2403 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2404 RetVT != MVT::i16 && RetVT != MVT::i32) {
2405 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002406 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2407 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002408 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2409 return false;
2410 }
2411
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002412 // Set up the argument vectors.
2413 SmallVector<Value*, 8> Args;
2414 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002415 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002416 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002417 unsigned arg_size = CS.arg_size();
2418 Args.reserve(arg_size);
2419 ArgRegs.reserve(arg_size);
2420 ArgVTs.reserve(arg_size);
2421 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002422 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2423 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002424 // If we're lowering a memory intrinsic instead of a regular call, skip the
2425 // last two arguments, which shouldn't be passed to the underlying function.
2426 if (IntrMemName && e-i <= 2)
2427 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002428
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002429 ISD::ArgFlagsTy Flags;
2430 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002431 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002432 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002433 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002434 Flags.setZExt();
2435
Chad Rosier8a98ec42011-11-04 00:58:10 +00002436 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002437 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2438 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2439 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2440 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002441 return false;
2442
Chris Lattner229907c2011-07-18 04:54:35 +00002443 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002444 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002445 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2446 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002447 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002448
2449 unsigned Arg = getRegForValue(*i);
2450 if (Arg == 0)
2451 return false;
2452
Rafael Espindolaea09c592014-02-18 22:05:46 +00002453 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002454 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002455
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002456 Args.push_back(*i);
2457 ArgRegs.push_back(Arg);
2458 ArgVTs.push_back(ArgVT);
2459 ArgFlags.push_back(Flags);
2460 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002461
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002462 // Handle the arguments now that we've gotten them.
2463 SmallVector<unsigned, 4> RegArgs;
2464 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002465 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2466 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002467 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002468
Chad Rosierc6916f82012-06-12 19:25:13 +00002469 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002470 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosierc6916f82012-06-12 19:25:13 +00002471 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002472
Chad Rosierc6916f82012-06-12 19:25:13 +00002473 unsigned CalleeReg = 0;
2474 if (UseReg) {
2475 if (IntrMemName)
2476 CalleeReg = getLibcallReg(IntrMemName);
2477 else
2478 CalleeReg = getRegForValue(Callee);
2479
Chad Rosier223faf72012-05-23 18:38:57 +00002480 if (CalleeReg == 0) return false;
2481 }
2482
Chad Rosierc6916f82012-06-12 19:25:13 +00002483 // Issue the call.
2484 unsigned CallOpc = ARMSelectCallOp(UseReg);
2485 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002486 DbgLoc, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002487
Logan Chien2361f512013-08-22 12:08:04 +00002488 unsigned char OpFlags = 0;
2489
2490 // Add MO_PLT for global address or external symbol in the PIC relocation
2491 // model.
2492 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2493 OpFlags = ARMII::MO_PLT;
2494
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002495 // ARM calls don't take a predicate, but tBL / tBLX do.
2496 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002497 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002498 if (UseReg)
2499 MIB.addReg(CalleeReg);
2500 else if (!IntrMemName)
Logan Chien2361f512013-08-22 12:08:04 +00002501 MIB.addGlobalAddress(GV, 0, OpFlags);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002502 else
Logan Chien2361f512013-08-22 12:08:04 +00002503 MIB.addExternalSymbol(IntrMemName, OpFlags);
Jush Luac96b762012-06-14 06:08:19 +00002504
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002505 // Add implicit physical register uses to the call.
2506 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002507 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002508
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002509 // Add a register mask with the call-preserved registers.
2510 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2511 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2512
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002513 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002514 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002515 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2516 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002517
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002518 // Set all unused physreg defs as dead.
2519 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002520
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002521 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002522}
2523
Chad Rosier057b6d32011-11-14 23:04:09 +00002524bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002525 return Len <= 16;
2526}
2527
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002528bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002529 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002530 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002531 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002532 return false;
2533
Chad Rosierab7223e2011-11-14 22:46:17 +00002534 while (Len) {
2535 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002536 if (!Alignment || Alignment >= 4) {
2537 if (Len >= 4)
2538 VT = MVT::i32;
2539 else if (Len >= 2)
2540 VT = MVT::i16;
2541 else {
2542 assert (Len == 1 && "Expected a length of 1!");
2543 VT = MVT::i8;
2544 }
2545 } else {
2546 // Bound based on alignment.
2547 if (Len >= 2 && Alignment == 2)
2548 VT = MVT::i16;
2549 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002550 VT = MVT::i8;
2551 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002552 }
2553
2554 bool RV;
2555 unsigned ResultReg;
2556 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002557 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002558 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002559 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002560 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002561
2562 unsigned Size = VT.getSizeInBits()/8;
2563 Len -= Size;
2564 Dest.Offset += Size;
2565 Src.Offset += Size;
2566 }
2567
2568 return true;
2569}
2570
Chad Rosiera7ebc562011-11-11 23:31:03 +00002571bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2572 // FIXME: Handle more intrinsics.
2573 switch (I.getIntrinsicID()) {
2574 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002575 case Intrinsic::frameaddress: {
2576 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2577 MFI->setFrameAddressIsTaken(true);
2578
2579 unsigned LdrOpc;
2580 const TargetRegisterClass *RC;
2581 if (isThumb2) {
2582 LdrOpc = ARM::t2LDRi12;
2583 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2584 } else {
2585 LdrOpc = ARM::LDRi12;
2586 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2587 }
2588
2589 const ARMBaseRegisterInfo *RegInfo =
2590 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2591 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2592 unsigned SrcReg = FramePtr;
2593
2594 // Recursively load frame address
2595 // ldr r0 [fp]
2596 // ldr r0 [r0]
2597 // ldr r0 [r0]
2598 // ...
2599 unsigned DestReg;
2600 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2601 while (Depth--) {
2602 DestReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier820d248c2012-05-30 17:23:22 +00002604 TII.get(LdrOpc), DestReg)
2605 .addReg(SrcReg).addImm(0));
2606 SrcReg = DestReg;
2607 }
Chad Rosierf3193242012-06-01 21:12:31 +00002608 UpdateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002609 return true;
2610 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002611 case Intrinsic::memcpy:
2612 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002613 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2614 // Don't handle volatile.
2615 if (MTI.isVolatile())
2616 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002617
2618 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2619 // we would emit dead code because we don't currently handle memmoves.
2620 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2621 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002622 // Small memcpy's are common enough that we want to do them without a call
2623 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002624 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002625 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002626 Address Dest, Src;
2627 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2628 !ARMComputeAddress(MTI.getRawSource(), Src))
2629 return false;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002630 unsigned Alignment = MTI.getAlignment();
2631 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002632 return true;
2633 }
2634 }
Jush Luac96b762012-06-14 06:08:19 +00002635
Chad Rosiera7ebc562011-11-11 23:31:03 +00002636 if (!MTI.getLength()->getType()->isIntegerTy(32))
2637 return false;
Jush Luac96b762012-06-14 06:08:19 +00002638
Chad Rosiera7ebc562011-11-11 23:31:03 +00002639 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2640 return false;
2641
2642 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2643 return SelectCall(&I, IntrMemName);
2644 }
2645 case Intrinsic::memset: {
2646 const MemSetInst &MSI = cast<MemSetInst>(I);
2647 // Don't handle volatile.
2648 if (MSI.isVolatile())
2649 return false;
Jush Luac96b762012-06-14 06:08:19 +00002650
Chad Rosiera7ebc562011-11-11 23:31:03 +00002651 if (!MSI.getLength()->getType()->isIntegerTy(32))
2652 return false;
Jush Luac96b762012-06-14 06:08:19 +00002653
Chad Rosiera7ebc562011-11-11 23:31:03 +00002654 if (MSI.getDestAddressSpace() > 255)
2655 return false;
Jush Luac96b762012-06-14 06:08:19 +00002656
Chad Rosiera7ebc562011-11-11 23:31:03 +00002657 return SelectCall(&I, "memset");
2658 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002659 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002660 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002661 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002662 return true;
2663 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002664 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002665}
2666
Chad Rosieree7e4522011-11-02 00:18:48 +00002667bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002668 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002669 // undefined.
2670 Value *Op = I->getOperand(0);
2671
2672 EVT SrcVT, DestVT;
2673 SrcVT = TLI.getValueType(Op->getType(), true);
2674 DestVT = TLI.getValueType(I->getType(), true);
2675
2676 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2677 return false;
2678 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2679 return false;
2680
2681 unsigned SrcReg = getRegForValue(Op);
2682 if (!SrcReg) return false;
2683
2684 // Because the high bits are undefined, a truncate doesn't generate
2685 // any code.
2686 UpdateValueMap(I, SrcReg);
2687 return true;
2688}
2689
Chad Rosier62a144f2012-12-17 19:59:43 +00002690unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002691 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002692 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002693 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002694 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002695 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002696
2697 // Table of which combinations can be emitted as a single instruction,
2698 // and which will require two.
2699 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2700 // ARM Thumb
2701 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2702 // ext: s z s z s z s z
2703 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2704 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2705 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2706 };
2707
2708 // Target registers for:
2709 // - For ARM can never be PC.
2710 // - For 16-bit Thumb are restricted to lower 8 registers.
2711 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2712 static const TargetRegisterClass *RCTbl[2][2] = {
2713 // Instructions: Two Single
2714 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2715 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2716 };
2717
2718 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002719 static const struct InstructionTable {
2720 uint32_t Opc : 16;
2721 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2722 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2723 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2724 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002725 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002726 { // ARM Opc S Shift Imm
2727 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2728 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2729 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2730 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2731 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2732 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002733 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002734 { // Thumb Opc S Shift Imm
2735 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2736 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2737 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2738 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2739 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2740 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002741 }
2742 },
2743 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002744 { // ARM Opc S Shift Imm
2745 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2746 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2747 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2748 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2749 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2750 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002751 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002752 { // Thumb Opc S Shift Imm
2753 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2754 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2755 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2756 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2757 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2758 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002759 }
2760 }
2761 };
2762
2763 unsigned SrcBits = SrcVT.getSizeInBits();
2764 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002765 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002766 assert((SrcBits < DestBits) && "can only extend to larger types");
2767 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2768 "other sizes unimplemented");
2769 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2770 "other sizes unimplemented");
2771
2772 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002773 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002774 assert((Bitness < 3) && "sanity-check table bounds");
2775
2776 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2777 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002778 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2779 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002780 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002781 unsigned hasS = ITP->hasS;
2782 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2783 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2784 "only MOVsi has shift operand addressing mode");
2785 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002786
2787 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2788 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002789 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002790 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002791 // MOVsi encodes shift and immediate in shift operand addressing mode.
2792 // The following condition has the same value when emitting two
2793 // instruction sequences: both are shifts.
2794 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002795
2796 // Either one or two instructions are emitted.
2797 // They're always of the form:
2798 // dst = in OP imm
2799 // CPSR is set only by 16-bit Thumb instructions.
2800 // Predicate, if any, is AL.
2801 // S bit, if available, is always 0.
2802 // When two are emitted the first's result will feed as the second's input,
2803 // that value is then dead.
2804 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2805 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2806 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002807 bool isLsl = (0 == Instr) && !isSingleInstr;
2808 unsigned Opcode = isLsl ? LSLOpc : Opc;
2809 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2810 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002811 bool isKill = 1 == Instr;
2812 MachineInstrBuilder MIB = BuildMI(
Rafael Espindolaea09c592014-02-18 22:05:46 +00002813 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
JF Bastien06ce03d2013-06-07 20:10:37 +00002814 if (setsCPSR)
2815 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002816 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002817 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002818 if (hasS)
2819 AddDefaultCC(MIB);
2820 // Second instruction consumes the first's result.
2821 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002822 }
2823
Chad Rosier4489f942011-11-02 17:20:24 +00002824 return ResultReg;
2825}
2826
2827bool ARMFastISel::SelectIntExt(const Instruction *I) {
2828 // On ARM, in general, integer casts don't involve legal types; this code
2829 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002830 Type *DestTy = I->getType();
2831 Value *Src = I->getOperand(0);
2832 Type *SrcTy = Src->getType();
2833
Chad Rosier4489f942011-11-02 17:20:24 +00002834 bool isZExt = isa<ZExtInst>(I);
2835 unsigned SrcReg = getRegForValue(Src);
2836 if (!SrcReg) return false;
2837
Chad Rosier62a144f2012-12-17 19:59:43 +00002838 EVT SrcEVT, DestEVT;
2839 SrcEVT = TLI.getValueType(SrcTy, true);
2840 DestEVT = TLI.getValueType(DestTy, true);
2841 if (!SrcEVT.isSimple()) return false;
2842 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002843
Chad Rosier62a144f2012-12-17 19:59:43 +00002844 MVT SrcVT = SrcEVT.getSimpleVT();
2845 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002846 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2847 if (ResultReg == 0) return false;
2848 UpdateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002849 return true;
2850}
2851
Jush Lu4705da92012-08-03 02:37:48 +00002852bool ARMFastISel::SelectShift(const Instruction *I,
2853 ARM_AM::ShiftOpc ShiftTy) {
2854 // We handle thumb2 mode by target independent selector
2855 // or SelectionDAG ISel.
2856 if (isThumb2)
2857 return false;
2858
2859 // Only handle i32 now.
2860 EVT DestVT = TLI.getValueType(I->getType(), true);
2861 if (DestVT != MVT::i32)
2862 return false;
2863
2864 unsigned Opc = ARM::MOVsr;
2865 unsigned ShiftImm;
2866 Value *Src2Value = I->getOperand(1);
2867 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2868 ShiftImm = CI->getZExtValue();
2869
2870 // Fall back to selection DAG isel if the shift amount
2871 // is zero or greater than the width of the value type.
2872 if (ShiftImm == 0 || ShiftImm >=32)
2873 return false;
2874
2875 Opc = ARM::MOVsi;
2876 }
2877
2878 Value *Src1Value = I->getOperand(0);
2879 unsigned Reg1 = getRegForValue(Src1Value);
2880 if (Reg1 == 0) return false;
2881
Nadav Rotema8e15b02012-09-06 11:13:55 +00002882 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002883 if (Opc == ARM::MOVsr) {
2884 Reg2 = getRegForValue(Src2Value);
2885 if (Reg2 == 0) return false;
2886 }
2887
JF Bastien13969d02013-05-29 15:45:47 +00002888 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002889 if(ResultReg == 0) return false;
2890
Rafael Espindolaea09c592014-02-18 22:05:46 +00002891 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu4705da92012-08-03 02:37:48 +00002892 TII.get(Opc), ResultReg)
2893 .addReg(Reg1);
2894
2895 if (Opc == ARM::MOVsi)
2896 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2897 else if (Opc == ARM::MOVsr) {
2898 MIB.addReg(Reg2);
2899 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2900 }
2901
2902 AddOptionalDefs(MIB);
2903 UpdateValueMap(I, ResultReg);
2904 return true;
2905}
2906
Eric Christopherc3e118e2010-09-02 23:43:26 +00002907// TODO: SoftFP support.
Eric Christopher84bdfd82010-07-21 22:26:11 +00002908bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002909
Eric Christopher84bdfd82010-07-21 22:26:11 +00002910 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002911 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002912 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002913 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002914 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002915 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002916 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002917 case Instruction::IndirectBr:
2918 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002919 case Instruction::ICmp:
2920 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002921 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002922 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002923 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002924 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002925 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002926 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002927 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002928 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002929 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002930 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002931 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002932 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002933 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002934 case Instruction::Add:
2935 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002936 case Instruction::Or:
2937 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002938 case Instruction::Sub:
2939 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002940 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002941 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002942 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002943 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002944 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002945 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002946 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002947 return SelectDiv(I, /*isSigned*/ true);
2948 case Instruction::UDiv:
2949 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002950 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002951 return SelectRem(I, /*isSigned*/ true);
2952 case Instruction::URem:
2953 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002954 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002955 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2956 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002957 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002958 case Instruction::Select:
2959 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002960 case Instruction::Ret:
2961 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002962 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002963 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002964 case Instruction::ZExt:
2965 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002966 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002967 case Instruction::Shl:
2968 return SelectShift(I, ARM_AM::lsl);
2969 case Instruction::LShr:
2970 return SelectShift(I, ARM_AM::lsr);
2971 case Instruction::AShr:
2972 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002973 default: break;
2974 }
2975 return false;
2976}
2977
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002978namespace {
2979// This table describes sign- and zero-extend instructions which can be
2980// folded into a preceding load. All of these extends have an immediate
2981// (sometimes a mask and sometimes a shift) that's applied after
2982// extension.
2983const struct FoldableLoadExtendsStruct {
2984 uint16_t Opc[2]; // ARM, Thumb.
2985 uint8_t ExpectedImm;
2986 uint8_t isZExt : 1;
2987 uint8_t ExpectedVT : 7;
2988} FoldableLoadExtends[] = {
2989 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2990 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2991 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2992 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2993 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2994};
2995}
2996
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002997/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002998/// vreg is being provided by the specified load instruction. If possible,
2999/// try to fold the load as an operand to the instruction, returning true if
3000/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003001bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3002 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003003 // Verify we have a legal type before going any further.
3004 MVT VT;
3005 if (!isLoadTypeLegal(LI->getType(), VT))
3006 return false;
3007
3008 // Combine load followed by zero- or sign-extend.
3009 // ldrb r1, [r0] ldrb r1, [r0]
3010 // uxtb r2, r1 =>
3011 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00003012 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
3013 return false;
3014 const uint64_t Imm = MI->getOperand(2).getImm();
3015
3016 bool Found = false;
3017 bool isZExt;
3018 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
3019 i != e; ++i) {
3020 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
3021 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
3022 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
3023 Found = true;
3024 isZExt = FoldableLoadExtends[i].isZExt;
3025 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003026 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00003027 if (!Found) return false;
3028
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003029 // See if we can handle this address.
3030 Address Addr;
3031 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00003032
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003033 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00003034 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00003035 return false;
3036 MI->eraseFromParent();
3037 return true;
3038}
3039
Jush Lu47172a02012-09-27 05:21:41 +00003040unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003041 unsigned Align, MVT VT) {
Jush Lu47172a02012-09-27 05:21:41 +00003042 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
3043 ARMConstantPoolConstant *CPV =
3044 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
3045 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
3046
3047 unsigned Opc;
3048 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
3049 // Load value.
3050 if (isThumb2) {
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003051 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003052 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu47172a02012-09-27 05:21:41 +00003053 TII.get(ARM::t2LDRpci), DestReg1)
3054 .addConstantPoolIndex(Idx));
3055 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
3056 } else {
3057 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003058 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00003059 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00003060 DbgLoc, TII.get(ARM::LDRcp), DestReg1)
Jush Lu47172a02012-09-27 05:21:41 +00003061 .addConstantPoolIndex(Idx).addImm(0));
3062 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
3063 }
3064
3065 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
3066 if (GlobalBaseReg == 0) {
3067 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
3068 AFI->setGlobalBaseReg(GlobalBaseReg);
3069 }
3070
3071 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
Jim Grosbach5f71aab2013-08-26 20:07:29 +00003072 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
3073 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
3074 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
Jush Lu47172a02012-09-27 05:21:41 +00003075 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00003076 DbgLoc, TII.get(Opc), DestReg2)
Jush Lu47172a02012-09-27 05:21:41 +00003077 .addReg(DestReg1)
3078 .addReg(GlobalBaseReg);
3079 if (!UseGOTOFF)
3080 MIB.addImm(0);
3081 AddOptionalDefs(MIB);
3082
3083 return DestReg2;
3084}
3085
Evan Cheng615620c2013-02-11 01:27:15 +00003086bool ARMFastISel::FastLowerArguments() {
3087 if (!FuncInfo.CanLowerReturn)
3088 return false;
3089
3090 const Function *F = FuncInfo.Fn;
3091 if (F->isVarArg())
3092 return false;
3093
3094 CallingConv::ID CC = F->getCallingConv();
3095 switch (CC) {
3096 default:
3097 return false;
3098 case CallingConv::Fast:
3099 case CallingConv::C:
3100 case CallingConv::ARM_AAPCS_VFP:
3101 case CallingConv::ARM_AAPCS:
3102 case CallingConv::ARM_APCS:
3103 break;
3104 }
3105
3106 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3107 // which are passed in r0 - r3.
3108 unsigned Idx = 1;
3109 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3110 I != E; ++I, ++Idx) {
3111 if (Idx > 4)
3112 return false;
3113
3114 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3115 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3116 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3117 return false;
3118
3119 Type *ArgTy = I->getType();
3120 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3121 return false;
3122
3123 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003124 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003125 switch (ArgVT.getSimpleVT().SimpleTy) {
3126 case MVT::i8:
3127 case MVT::i16:
3128 case MVT::i32:
3129 break;
3130 default:
3131 return false;
3132 }
3133 }
3134
3135
3136 static const uint16_t GPRArgRegs[] = {
3137 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3138 };
3139
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003140 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003141 Idx = 0;
3142 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3143 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003144 unsigned SrcReg = GPRArgRegs[Idx];
3145 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3146 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3147 // Without this, EmitLiveInCopies may eliminate the livein if its only
3148 // use is a bitcast (which isn't turned into an instruction).
3149 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3151 TII.get(TargetOpcode::COPY),
Evan Cheng615620c2013-02-11 01:27:15 +00003152 ResultReg).addReg(DstReg, getKillRegState(true));
3153 UpdateValueMap(I, ResultReg);
3154 }
3155
3156 return true;
3157}
3158
Eric Christopher84bdfd82010-07-21 22:26:11 +00003159namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003160 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3161 const TargetLibraryInfo *libInfo) {
Eric Christopher5501b7e2010-10-11 20:05:22 +00003162 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach68147ee2010-11-09 19:22:26 +00003163
Eric Christopher5501b7e2010-10-11 20:05:22 +00003164 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastien18db1f22013-06-14 02:49:43 +00003165 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3166 bool UseFastISel = false;
Tim Northoverd6a729b2014-01-06 14:28:05 +00003167 UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
JF Bastien18db1f22013-06-14 02:49:43 +00003168 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3169 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3170
3171 if (UseFastISel) {
3172 // iOS always has a FP for backtracking, force other targets
3173 // to keep their FP when doing FastISel. The emitted code is
3174 // currently superior, and in cases like test-suite's lencod
3175 // FastISel isn't quite correct when FP is eliminated.
3176 TM.Options.NoFramePointerElim = true;
Bob Wilson3e6fa462012-08-03 04:06:28 +00003177 return new ARMFastISel(funcInfo, libInfo);
JF Bastien18db1f22013-06-14 02:49:43 +00003178 }
Evan Cheng23b05d12010-07-26 18:32:55 +00003179 return 0;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003180 }
3181}