blob: 5cb6d9a0a779dd4ada5eb0953b183cb84cecec52 [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)
Eric Christopherd9134482014-08-04 21:25:23 +000095 : FastISel(funcInfo, libInfo),
96 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
97 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getSubtargetImpl()->getInstrInfo()),
99 TLI(*TM.getSubtargetImpl()->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:
Craig Topperfd1c9252012-08-18 21:38:45 +0000108 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
109 const TargetRegisterClass *RC,
110 unsigned Op0, bool Op0IsKill);
111 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill,
114 unsigned Op1, bool Op1IsKill);
115 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
116 const TargetRegisterClass *RC,
117 unsigned Op0, bool Op0IsKill,
118 unsigned Op1, bool Op1IsKill,
119 unsigned Op2, bool Op2IsKill);
120 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
121 const TargetRegisterClass *RC,
122 unsigned Op0, bool Op0IsKill,
123 uint64_t Imm);
Craig Topperfd1c9252012-08-18 21:38:45 +0000124 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
126 unsigned Op0, bool Op0IsKill,
127 unsigned Op1, bool Op1IsKill,
128 uint64_t Imm);
129 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
131 uint64_t Imm);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000132
Eric Christopherd8e8a292010-08-20 00:20:31 +0000133 // Backend specific FastISel code.
Craig Topperfd1c9252012-08-18 21:38:45 +0000134 private:
Craig Topper6bc27bf2014-03-10 02:09:33 +0000135 bool TargetSelectInstruction(const Instruction *I) override;
136 unsigned TargetMaterializeConstant(const Constant *C) override;
137 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
138 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
139 const LoadInst *LI) override;
140 bool FastLowerArguments() override;
Craig Topperfd1c9252012-08-18 21:38:45 +0000141 private:
Eric Christopher84bdfd82010-07-21 22:26:11 +0000142 #include "ARMGenFastISel.inc"
Eric Christopher2ff757d2010-09-09 01:06:51 +0000143
Eric Christopher00202ee2010-08-23 21:44:12 +0000144 // Instruction selection routines.
Eric Christophercc766a22010-09-10 23:10:30 +0000145 private:
Eric Christopher2f8637d2010-10-21 21:47:51 +0000146 bool SelectLoad(const Instruction *I);
147 bool SelectStore(const Instruction *I);
148 bool SelectBranch(const Instruction *I);
Chad Rosierded4c992012-02-07 23:56:08 +0000149 bool SelectIndirectBr(const Instruction *I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000150 bool SelectCmp(const Instruction *I);
151 bool SelectFPExt(const Instruction *I);
152 bool SelectFPTrunc(const Instruction *I);
Chad Rosier685b20c2012-02-06 23:50:07 +0000153 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
154 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosiere023d5d2012-02-03 21:14:11 +0000155 bool SelectIToFP(const Instruction *I, bool isSigned);
156 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosieraaa55a82012-02-03 21:07:27 +0000157 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosierb84a4b42012-02-03 21:23:45 +0000158 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosiera7ebc562011-11-11 23:31:03 +0000159 bool SelectCall(const Instruction *I, const char *IntrMemName);
160 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000161 bool SelectSelect(const Instruction *I);
Eric Christopher93bbe652010-10-22 01:28:00 +0000162 bool SelectRet(const Instruction *I);
Chad Rosieree7e4522011-11-02 00:18:48 +0000163 bool SelectTrunc(const Instruction *I);
164 bool SelectIntExt(const Instruction *I);
Jush Lu4705da92012-08-03 02:37:48 +0000165 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopher84bdfd82010-07-21 22:26:11 +0000166
Eric Christopher00202ee2010-08-23 21:44:12 +0000167 // Utility routines.
Eric Christopher0d274a02010-08-19 00:37:05 +0000168 private:
Chris Lattner229907c2011-07-18 04:54:35 +0000169 bool isTypeLegal(Type *Ty, MVT &VT);
170 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosier9cf803c2011-11-02 18:08:25 +0000171 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
172 bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000173 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosiera26979b2011-12-14 17:26:05 +0000174 unsigned Alignment = 0, bool isZExt = true,
175 bool allocReg = true);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000176 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +0000177 unsigned Alignment = 0);
Eric Christopherfef5f312010-11-19 22:30:02 +0000178 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier150d35b2012-12-17 22:35:29 +0000179 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier057b6d32011-11-14 23:04:09 +0000180 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosier9f5c68a2012-12-06 01:34:31 +0000181 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
182 unsigned Alignment);
Chad Rosier62a144f2012-12-17 19:59:43 +0000183 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000184 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
185 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
186 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
187 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
188 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000189 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000190 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000191
Eric Christopherd9134482014-08-04 21:25:23 +0000192 const TargetLowering *getTargetLowering() {
193 return TM.getSubtargetImpl()->getTargetLowering();
194 }
Christian Pirker238c7c12014-05-12 11:19:20 +0000195
Eric Christopher72497e52010-09-10 23:18:12 +0000196 // Call handling routines.
197 private:
Jush Lue67e07b2012-07-19 09:49:00 +0000198 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
199 bool Return,
200 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000201 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000202 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000203 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000204 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
205 SmallVectorImpl<unsigned> &RegArgs,
206 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000207 unsigned &NumBytes,
208 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000209 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000210 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000211 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000212 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000213 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000214
215 // OptionalDef handling routines.
216 private:
Eric Christopher174d8722011-03-12 01:09:29 +0000217 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000218 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
219 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000220 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000221 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000222 unsigned Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000223};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000224
225} // end anonymous namespace
226
Eric Christopher72497e52010-09-10 23:18:12 +0000227#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000228
Eric Christopher0d274a02010-08-19 00:37:05 +0000229// DefinesOptionalPredicate - This is different from DefinesPredicate in that
230// we don't care about implicit defs here, just places we'll need to add a
231// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
232bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000233 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000234 return false;
235
236 // Look to see if our OptionalDef is defining CPSR or CCR.
237 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
238 const MachineOperand &MO = MI->getOperand(i);
Eric Christopher985d9e42010-08-20 00:36:24 +0000239 if (!MO.isReg() || !MO.isDef()) continue;
240 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000241 *CPSR = true;
242 }
243 return true;
244}
245
Eric Christopher174d8722011-03-12 01:09:29 +0000246bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000247 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000248
Joey Goulya5153cb2013-09-09 14:21:49 +0000249 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000250 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000251 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000252 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000253
Evan Cheng6cc775f2011-06-28 19:10:37 +0000254 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
255 if (MCID.OpInfo[i].isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000256 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000257
Eric Christopher174d8722011-03-12 01:09:29 +0000258 return false;
259}
260
Eric Christopher0d274a02010-08-19 00:37:05 +0000261// If the machine is predicable go ahead and add the predicate operands, if
262// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000263// TODO: If we want to support thumb1 then we'll need to deal with optional
264// CPSR defs that need to be added before the remaining operands. See s_cc_out
265// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000266const MachineInstrBuilder &
267ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
268 MachineInstr *MI = &*MIB;
269
Eric Christopher174d8722011-03-12 01:09:29 +0000270 // Do we use a predicate? or...
271 // Are we NEON in ARM mode and have a predicate operand? If so, I know
272 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000273 if (isARMNEONPred(MI))
Eric Christopher0d274a02010-08-19 00:37:05 +0000274 AddDefaultPred(MIB);
Eric Christopher501d2e22011-04-29 00:03:10 +0000275
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000276 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000277 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000278 bool CPSR = false;
Eric Christopher0d274a02010-08-19 00:37:05 +0000279 if (DefinesOptionalPredicate(MI, &CPSR)) {
280 if (CPSR)
281 AddDefaultT1CC(MIB);
282 else
283 AddDefaultCC(MIB);
284 }
285 return MIB;
286}
287
Eric Christopher09f757d2010-08-17 01:25:29 +0000288unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
289 const TargetRegisterClass *RC,
290 unsigned Op0, bool Op0IsKill) {
291 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000292 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000293
Jim Grosbach06c2a682013-08-16 23:37:31 +0000294 // Make sure the input operand is sufficiently constrained to be legal
295 // for this instruction.
296 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000297 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
299 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000300 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000302 .addReg(Op0, Op0IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000303 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000304 TII.get(TargetOpcode::COPY), ResultReg)
305 .addReg(II.ImplicitDefs[0]));
306 }
307 return ResultReg;
308}
309
310unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
311 const TargetRegisterClass *RC,
312 unsigned Op0, bool Op0IsKill,
313 unsigned Op1, bool Op1IsKill) {
314 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000315 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000316
Jim Grosbach06c2a682013-08-16 23:37:31 +0000317 // Make sure the input operands are sufficiently constrained to be legal
318 // for this instruction.
319 Op0 = constrainOperandRegClass(II, Op0, 1);
320 Op1 = constrainOperandRegClass(II, Op1, 2);
321
Chad Rosier0bc51322012-02-15 17:36:21 +0000322 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000323 AddOptionalDefs(
324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000327 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addReg(Op1, Op1IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000331 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000332 TII.get(TargetOpcode::COPY), ResultReg)
333 .addReg(II.ImplicitDefs[0]));
334 }
335 return ResultReg;
336}
337
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000338unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
339 const TargetRegisterClass *RC,
340 unsigned Op0, bool Op0IsKill,
341 unsigned Op1, bool Op1IsKill,
342 unsigned Op2, bool Op2IsKill) {
343 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000344 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000345
Jim Grosbach06c2a682013-08-16 23:37:31 +0000346 // Make sure the input operands are sufficiently constrained to be legal
347 // for this instruction.
348 Op0 = constrainOperandRegClass(II, Op0, 1);
349 Op1 = constrainOperandRegClass(II, Op1, 2);
350 Op2 = constrainOperandRegClass(II, Op1, 3);
351
Chad Rosier0bc51322012-02-15 17:36:21 +0000352 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000353 AddOptionalDefs(
354 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
355 .addReg(Op0, Op0IsKill * RegState::Kill)
356 .addReg(Op1, Op1IsKill * RegState::Kill)
357 .addReg(Op2, Op2IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000358 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000360 .addReg(Op0, Op0IsKill * RegState::Kill)
361 .addReg(Op1, Op1IsKill * RegState::Kill)
362 .addReg(Op2, Op2IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000363 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000364 TII.get(TargetOpcode::COPY), ResultReg)
365 .addReg(II.ImplicitDefs[0]));
366 }
367 return ResultReg;
368}
369
Eric Christopher09f757d2010-08-17 01:25:29 +0000370unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
371 const TargetRegisterClass *RC,
372 unsigned Op0, bool Op0IsKill,
373 uint64_t Imm) {
374 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000375 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000376
Jim Grosbach06c2a682013-08-16 23:37:31 +0000377 // Make sure the input operand is sufficiently constrained to be legal
378 // for this instruction.
379 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000380 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000381 AddOptionalDefs(
382 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
383 .addReg(Op0, Op0IsKill * RegState::Kill)
384 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000385 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000386 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000387 .addReg(Op0, Op0IsKill * RegState::Kill)
388 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000389 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000390 TII.get(TargetOpcode::COPY), ResultReg)
391 .addReg(II.ImplicitDefs[0]));
392 }
393 return ResultReg;
394}
395
Eric Christopher09f757d2010-08-17 01:25:29 +0000396unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
397 const TargetRegisterClass *RC,
398 unsigned Op0, bool Op0IsKill,
399 unsigned Op1, bool Op1IsKill,
400 uint64_t Imm) {
401 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000402 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000403
Jim Grosbach06c2a682013-08-16 23:37:31 +0000404 // Make sure the input operands are sufficiently constrained to be legal
405 // for this instruction.
406 Op0 = constrainOperandRegClass(II, Op0, 1);
407 Op1 = constrainOperandRegClass(II, Op1, 2);
Chad Rosier0bc51322012-02-15 17:36:21 +0000408 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000409 AddOptionalDefs(
410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
411 .addReg(Op0, Op0IsKill * RegState::Kill)
412 .addReg(Op1, Op1IsKill * RegState::Kill)
413 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000414 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000416 .addReg(Op0, Op0IsKill * RegState::Kill)
417 .addReg(Op1, Op1IsKill * RegState::Kill)
418 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000419 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000420 TII.get(TargetOpcode::COPY), ResultReg)
421 .addReg(II.ImplicitDefs[0]));
422 }
423 return ResultReg;
424}
425
426unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
427 const TargetRegisterClass *RC,
428 uint64_t Imm) {
429 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000430 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000431
Chad Rosier0bc51322012-02-15 17:36:21 +0000432 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000433 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
434 ResultReg).addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000435 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000436 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000437 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000439 TII.get(TargetOpcode::COPY), ResultReg)
440 .addReg(II.ImplicitDefs[0]));
441 }
442 return ResultReg;
443}
444
Eric Christopher860fc932010-09-10 00:34:35 +0000445// TODO: Don't worry about 64-bit now, but when this is fixed remove the
446// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000447unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000448 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000449
Eric Christopher4bd70472010-09-09 21:44:45 +0000450 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000451 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000452 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000453 .addReg(SrcReg));
454 return MoveReg;
455}
456
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000457unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000458 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000459
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000460 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000462 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000463 .addReg(SrcReg));
464 return MoveReg;
465}
466
Eric Christopher3cf63f12010-09-09 00:19:41 +0000467// For double width floating point we need to materialize two constants
468// (the high and the low) into integer registers then use a move to get
469// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000470unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000471 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000472 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000473
Eric Christopher3cf63f12010-09-09 00:19:41 +0000474 // This checks to see if we can use VFP3 instructions to materialize
475 // a constant, otherwise we have to go through the constant pool.
476 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000477 int Imm;
478 unsigned Opc;
479 if (is64bit) {
480 Imm = ARM_AM::getFP64Imm(Val);
481 Opc = ARM::FCONSTD;
482 } else {
483 Imm = ARM_AM::getFP32Imm(Val);
484 Opc = ARM::FCONSTS;
485 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000486 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
488 TII.get(Opc), DestReg).addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000489 return DestReg;
490 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000491
Eric Christopher860fc932010-09-10 00:34:35 +0000492 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000493 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000494
Eric Christopher22fd29a2010-09-09 23:50:00 +0000495 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000496 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000497 if (Align == 0) {
498 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000499 Align = DL.getTypeAllocSize(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000500 }
501 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
502 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
503 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000504
Eric Christopher860fc932010-09-10 00:34:35 +0000505 // The extra reg is for addrmode5.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000506 AddOptionalDefs(
507 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
508 .addConstantPoolIndex(Idx)
509 .addReg(0));
Eric Christopher22fd29a2010-09-09 23:50:00 +0000510 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000511}
512
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000513unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopher7ac602b2010-10-11 08:38:55 +0000514
Chad Rosier67f96882011-11-04 22:29:00 +0000515 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
516 return false;
Eric Christophere4dd7372010-11-03 20:21:17 +0000517
518 // If we can do this in a single instruction without a constant pool entry
519 // do so now.
520 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000521 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000522 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000523 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
524 &ARM::GPRRegClass;
525 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000526 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier67f96882011-11-04 22:29:00 +0000527 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000528 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000529 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000530 }
531
Chad Rosier2a3503e2011-11-11 00:36:21 +0000532 // Use MVN to emit negative constants.
533 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
534 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000535 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000536 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000537 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000538 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
Juergen Ributzka2cbcf7a2014-08-13 21:39:18 +0000539 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
540 &ARM::GPRRegClass;
541 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000542 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier2a3503e2011-11-11 00:36:21 +0000543 TII.get(Opc), ImmReg)
544 .addImm(Imm));
545 return ImmReg;
546 }
547 }
548
Juergen Ributzkaa5b08382014-08-13 21:42:19 +0000549 if (Subtarget->useMovt(*FuncInfo.MF))
550 return FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
551
Chad Rosier2a3503e2011-11-11 00:36:21 +0000552 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000553 if (VT != MVT::i32)
554 return false;
555
556 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
557
Eric Christopherc3e118e2010-09-02 23:43:26 +0000558 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000559 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000560 if (Align == 0) {
561 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000562 Align = DL.getTypeAllocSize(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000563 }
564 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000565
Chad Rosier0439cfc2011-11-08 21:12:00 +0000566 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000567 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000568 TII.get(ARM::t2LDRpci), DestReg)
569 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000570 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000571 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000572 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000573 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000574 TII.get(ARM::LDRcp), DestReg)
575 .addConstantPoolIndex(Idx)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000576 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000577 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000578
Eric Christopherc3e118e2010-09-02 23:43:26 +0000579 return DestReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000580}
581
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000582unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000583 // For now 32-bit only.
Duncan Sands14627772010-11-03 12:17:33 +0000584 if (VT != MVT::i32) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000585
Eric Christopher7787f792010-10-02 00:32:44 +0000586 Reloc::Model RelocM = TM.getRelocationModel();
Jush Lue87e5592012-08-29 02:41:21 +0000587 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier65710a72012-11-07 00:13:01 +0000588 const TargetRegisterClass *RC = isThumb2 ?
589 (const TargetRegisterClass*)&ARM::rGPRRegClass :
590 (const TargetRegisterClass*)&ARM::GPRRegClass;
591 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000592
Tim Northoverd6a729b2014-01-06 14:28:05 +0000593 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000594 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
595 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000596 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000597
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000598 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000599 // Non-darwin targets only support static movt relocations in FastISel.
Eric Christopherc1058df2014-07-04 01:55:26 +0000600 if (Subtarget->useMovt(*FuncInfo.MF) &&
Tim Northoverd6a729b2014-01-06 14:28:05 +0000601 (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000602 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000603 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000604 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000605 TF = ARMII::MO_NONLAZY;
606
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000607 switch (RelocM) {
608 case Reloc::PIC_:
609 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
610 break;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000611 default:
612 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
613 break;
614 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000615 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
616 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000617 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000618 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000619 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000620 if (Align == 0) {
621 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000622 Align = DL.getTypeAllocSize(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000623 }
624
Jush Lu47172a02012-09-27 05:21:41 +0000625 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
626 return ARMLowerPICELF(GV, Align, VT);
627
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000628 // Grab index.
629 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
630 (Subtarget->isThumb() ? 4 : 8);
631 unsigned Id = AFI->createPICLabelUId();
632 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
633 ARMCP::CPValue,
634 PCAdj);
635 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
636
637 // Load value.
638 MachineInstrBuilder MIB;
639 if (isThumb2) {
640 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000641 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
642 DestReg).addConstantPoolIndex(Idx);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000643 if (RelocM == Reloc::PIC_)
644 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000645 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000646 } else {
647 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000648 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000649 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
650 TII.get(ARM::LDRcp), DestReg)
651 .addConstantPoolIndex(Idx)
652 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000653 AddOptionalDefs(MIB);
654
655 if (RelocM == Reloc::PIC_) {
656 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
657 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
658
659 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000660 DbgLoc, TII.get(Opc), NewDestReg)
Jush Lue87e5592012-08-29 02:41:21 +0000661 .addReg(DestReg)
662 .addImm(Id);
663 AddOptionalDefs(MIB);
664 return NewDestReg;
665 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000666 }
Eric Christopher7787f792010-10-02 00:32:44 +0000667 }
Eli Friedman86585792011-06-03 01:13:19 +0000668
Jush Lue87e5592012-08-29 02:41:21 +0000669 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000670 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000671 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000672 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000673 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000674 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000675 .addReg(DestReg)
676 .addImm(0);
677 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000678 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
679 TII.get(ARM::LDRi12), NewDestReg)
680 .addReg(DestReg)
681 .addImm(0);
Eli Friedman86585792011-06-03 01:13:19 +0000682 DestReg = NewDestReg;
683 AddOptionalDefs(MIB);
684 }
685
Eric Christopher7787f792010-10-02 00:32:44 +0000686 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000687}
688
Eric Christopher3cf63f12010-09-09 00:19:41 +0000689unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglundc494d242012-12-17 14:30:06 +0000690 EVT CEVT = TLI.getValueType(C->getType(), true);
691
692 // Only handle simple types.
693 if (!CEVT.isSimple()) return 0;
694 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000695
696 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
697 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000698 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
699 return ARMMaterializeGV(GV, VT);
700 else if (isa<ConstantInt>(C))
701 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000702
Eric Christopher83a5ec82010-10-01 23:24:42 +0000703 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000704}
705
Chad Rosier0eff3e52011-11-17 21:46:13 +0000706// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
707
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000708unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
709 // Don't handle dynamic allocas.
710 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000711
Duncan Sandsf5dda012010-11-03 11:35:31 +0000712 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000713 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000714
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000715 DenseMap<const AllocaInst*, int>::iterator SI =
716 FuncInfo.StaticAllocaMap.find(AI);
717
718 // This will get lowered later into the correct offsets and registers
719 // via rewriteXFrameIndex.
720 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000721 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000722 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000723 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000724 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
725
Rafael Espindolaea09c592014-02-18 22:05:46 +0000726 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000727 TII.get(Opc), ResultReg)
728 .addFrameIndex(SI->second)
729 .addImm(0));
730 return ResultReg;
731 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000732
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000733 return 0;
734}
735
Chris Lattner229907c2011-07-18 04:54:35 +0000736bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000737 EVT evt = TLI.getValueType(Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000738
Eric Christopher761e7fb2010-08-25 07:23:49 +0000739 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000740 if (evt == MVT::Other || !evt.isSimple()) return false;
741 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000742
Eric Christopher901176a2010-08-31 01:28:42 +0000743 // Handle all legal types, i.e. a register that will directly hold this
744 // value.
745 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000746}
747
Chris Lattner229907c2011-07-18 04:54:35 +0000748bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000749 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000750
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000751 // If this is a type than can be sign or zero-extended to a basic operation
752 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000753 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000754 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000755
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000756 return false;
757}
758
Eric Christopher558b61e2010-11-19 22:36:41 +0000759// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000760bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000761 // Some boilerplate from the X86 FastISel.
Craig Topper062a2ba2014-04-25 05:30:21 +0000762 const User *U = nullptr;
Eric Christopher00202ee2010-08-23 21:44:12 +0000763 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000764 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000765 // Don't walk into other basic blocks unless the object is an alloca from
766 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000767 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
768 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
769 Opcode = I->getOpcode();
770 U = I;
771 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000772 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000773 Opcode = C->getOpcode();
774 U = C;
775 }
776
Chris Lattner229907c2011-07-18 04:54:35 +0000777 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000778 if (Ty->getAddressSpace() > 255)
779 // Fast instruction selection doesn't support the special
780 // address spaces.
781 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000782
Eric Christopher00202ee2010-08-23 21:44:12 +0000783 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000784 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000785 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000786 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000787 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000788 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000789 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000790 // Look past no-op inttoptrs.
791 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000792 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000793 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000794 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000795 // Look past no-op ptrtoints.
796 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000797 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000798 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000799 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000800 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000801 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000802
Eric Christopher21d0c172010-10-14 09:29:41 +0000803 // Iterate through the GEP folding the constants into offsets where
804 // we can.
805 gep_type_iterator GTI = gep_type_begin(U);
806 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
807 i != e; ++i, ++GTI) {
808 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000809 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000810 const StructLayout *SL = DL.getStructLayout(STy);
Eric Christopher21d0c172010-10-14 09:29:41 +0000811 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
812 TmpOffset += SL->getElementOffset(Idx);
813 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000814 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000815 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000816 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
817 // Constant-offset addressing.
818 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000819 break;
820 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000821 if (canFoldAddIntoGEP(U, Op)) {
822 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000823 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000824 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000825 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000826 // Iterate on the other operand.
827 Op = cast<AddOperator>(Op)->getOperand(0);
828 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000829 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000830 // Unsupported
831 goto unsupported_gep;
832 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000833 }
834 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000835
836 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000837 Addr.Offset = TmpOffset;
838 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000839
840 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000841 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000842
Eric Christopher21d0c172010-10-14 09:29:41 +0000843 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000844 break;
845 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000846 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000847 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000848 DenseMap<const AllocaInst*, int>::iterator SI =
849 FuncInfo.StaticAllocaMap.find(AI);
850 if (SI != FuncInfo.StaticAllocaMap.end()) {
851 Addr.BaseType = Address::FrameIndexBase;
852 Addr.Base.FI = SI->second;
853 return true;
854 }
855 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000856 }
857 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000858
Eric Christopher9d4e4712010-08-24 00:07:24 +0000859 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000860 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
861 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000862}
863
Chad Rosier150d35b2012-12-17 22:35:29 +0000864void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000865 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000866 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000867 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000868 case MVT::i1:
869 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000870 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000871 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000872 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000873 // Integer loads/stores handle 12-bit offsets.
874 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000875 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000876 if (needsLowering && isThumb2)
877 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
878 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000879 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000880 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000881 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000882 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000883 break;
884 case MVT::f32:
885 case MVT::f64:
886 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000887 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000888 break;
889 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000890
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000891 // If this is a stack pointer and the offset needs to be simplified then
892 // put the alloca address into a register, set the base type back to
893 // register and continue. This should almost never happen.
894 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topperc7242e02012-04-20 07:30:17 +0000895 const TargetRegisterClass *RC = isThumb2 ?
896 (const TargetRegisterClass*)&ARM::tGPRRegClass :
897 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000898 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000899 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000900 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000901 TII.get(Opc), ResultReg)
902 .addFrameIndex(Addr.Base.FI)
903 .addImm(0));
904 Addr.Base.Reg = ResultReg;
905 Addr.BaseType = Address::RegBase;
906 }
907
Eric Christopher73bc5b02010-10-21 19:40:30 +0000908 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +0000909 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +0000910 if (needsLowering) {
Eli Friedman86caced2011-04-29 21:22:56 +0000911 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
912 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +0000913 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +0000914 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000915}
916
Chad Rosier150d35b2012-12-17 22:35:29 +0000917void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000918 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000919 unsigned Flags, bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000920 // addrmode5 output depends on the selection dag addressing dividing the
921 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +0000922 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +0000923 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +0000924
Eric Christopher119ff7f2010-12-01 01:40:24 +0000925 // Frame base works a bit differently. Handle it separately.
926 if (Addr.BaseType == Address::FrameIndexBase) {
927 int FI = Addr.Base.FI;
928 int Offset = Addr.Offset;
929 MachineMemOperand *MMO =
930 FuncInfo.MF->getMachineMemOperand(
931 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarich6528a542011-05-28 20:34:49 +0000932 Flags,
Eric Christopher119ff7f2010-12-01 01:40:24 +0000933 MFI.getObjectSize(FI),
934 MFI.getObjectAlignment(FI));
935 // Now add the rest of the operands.
936 MIB.addFrameIndex(FI);
937
Bob Wilson80381f62011-12-04 00:52:23 +0000938 // ARM halfword load/stores and signed byte loads need an additional
939 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000940 if (useAM3) {
941 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
942 MIB.addReg(0);
943 MIB.addImm(Imm);
944 } else {
945 MIB.addImm(Addr.Offset);
946 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000947 MIB.addMemOperand(MMO);
948 } else {
949 // Now add the rest of the operands.
950 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +0000951
Bob Wilson80381f62011-12-04 00:52:23 +0000952 // ARM halfword load/stores and signed byte loads need an additional
953 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000954 if (useAM3) {
955 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
956 MIB.addReg(0);
957 MIB.addImm(Imm);
958 } else {
959 MIB.addImm(Addr.Offset);
960 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000961 }
962 AddOptionalDefs(MIB);
963}
964
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000965bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +0000966 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +0000967 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000968 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +0000969 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +0000970 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000971 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000972 // This is mostly going to be Neon/vector support.
973 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +0000974 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000975 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +0000976 if (isThumb2) {
977 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
978 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
979 else
980 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000981 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +0000982 if (isZExt) {
983 Opc = ARM::LDRBi12;
984 } else {
985 Opc = ARM::LDRSB;
986 useAM3 = true;
987 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000988 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000989 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000990 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000991 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +0000992 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +0000993 return false;
994
Chad Rosieradfd2002011-11-14 20:22:27 +0000995 if (isThumb2) {
996 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
997 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
998 else
999 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1000 } else {
1001 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1002 useAM3 = true;
1003 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001004 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +00001005 break;
Eric Christopher901176a2010-08-31 01:28:42 +00001006 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001007 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001008 return false;
1009
Chad Rosieradfd2002011-11-14 20:22:27 +00001010 if (isThumb2) {
1011 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1012 Opc = ARM::t2LDRi8;
1013 else
1014 Opc = ARM::t2LDRi12;
1015 } else {
1016 Opc = ARM::LDRi12;
1017 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001018 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +00001019 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +00001020 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +00001021 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +00001022 // Unaligned loads need special handling. Floats require word-alignment.
1023 if (Alignment && Alignment < 4) {
1024 needVMOV = true;
1025 VT = MVT::i32;
1026 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +00001027 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +00001028 } else {
1029 Opc = ARM::VLDRS;
1030 RC = TLI.getRegClassFor(VT);
1031 }
Eric Christopheraef6499b2010-09-18 01:59:37 +00001032 break;
1033 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +00001034 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001035 // FIXME: Unaligned loads need special handling. Doublewords require
1036 // word-alignment.
1037 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +00001038 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001039
Eric Christopheraef6499b2010-09-18 01:59:37 +00001040 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +00001041 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +00001042 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001043 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001044 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001045 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001046
Eric Christopher119ff7f2010-12-01 01:40:24 +00001047 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001048 if (allocReg)
1049 ResultReg = createResultReg(RC);
1050 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Rafael Espindolaea09c592014-02-18 22:05:46 +00001051 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001052 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001053 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001054
1055 // If we had an unaligned load of a float we've converted it to an regular
1056 // load. Now we must move from the GRP to the FP register.
1057 if (needVMOV) {
1058 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001059 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier563de602011-12-13 19:22:14 +00001060 TII.get(ARM::VMOVSR), MoveReg)
1061 .addReg(ResultReg));
1062 ResultReg = MoveReg;
1063 }
Eric Christopher901176a2010-08-31 01:28:42 +00001064 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001065}
1066
Eric Christopher29ab6d12010-09-27 06:02:23 +00001067bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001068 // Atomic loads need special handling.
1069 if (cast<LoadInst>(I)->isAtomic())
1070 return false;
1071
Eric Christopher860fc932010-09-10 00:34:35 +00001072 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001073 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001074 if (!isLoadTypeLegal(I->getType(), VT))
1075 return false;
1076
Eric Christopher119ff7f2010-12-01 01:40:24 +00001077 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001078 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001079 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001080
1081 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001082 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1083 return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001084 UpdateValueMap(I, ResultReg);
1085 return true;
1086}
1087
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001088bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001089 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001090 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001091 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001092 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001093 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001094 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001095 case MVT::i1: {
Craig Topperc7242e02012-04-20 07:30:17 +00001096 unsigned Res = createResultReg(isThumb2 ?
1097 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1098 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001099 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001100 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001101 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher1e43892e2010-11-02 23:59:09 +00001102 TII.get(Opc), Res)
1103 .addReg(SrcReg).addImm(1));
1104 SrcReg = Res;
1105 } // Fallthrough here.
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001106 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001107 if (isThumb2) {
1108 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1109 StrOpc = ARM::t2STRBi8;
1110 else
1111 StrOpc = ARM::t2STRBi12;
1112 } else {
1113 StrOpc = ARM::STRBi12;
1114 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001115 break;
1116 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001117 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001118 return false;
1119
Chad Rosieradfd2002011-11-14 20:22:27 +00001120 if (isThumb2) {
1121 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1122 StrOpc = ARM::t2STRHi8;
1123 else
1124 StrOpc = ARM::t2STRHi12;
1125 } else {
1126 StrOpc = ARM::STRH;
1127 useAM3 = true;
1128 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001129 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001130 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001131 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001132 return false;
1133
Chad Rosieradfd2002011-11-14 20:22:27 +00001134 if (isThumb2) {
1135 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1136 StrOpc = ARM::t2STRi8;
1137 else
1138 StrOpc = ARM::t2STRi12;
1139 } else {
1140 StrOpc = ARM::STRi12;
1141 }
Eric Christopherc918d552010-10-16 01:10:35 +00001142 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001143 case MVT::f32:
1144 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001145 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001146 if (Alignment && Alignment < 4) {
1147 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001148 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosierec3b77e2011-12-03 02:21:57 +00001149 TII.get(ARM::VMOVRS), MoveReg)
1150 .addReg(SrcReg));
1151 SrcReg = MoveReg;
1152 VT = MVT::i32;
1153 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001154 } else {
1155 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001156 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001157 break;
1158 case MVT::f64:
1159 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001160 // FIXME: Unaligned stores need special handling. Doublewords require
1161 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001162 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001163 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001164
Eric Christopherc3e118e2010-09-02 23:43:26 +00001165 StrOpc = ARM::VSTRD;
1166 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001167 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001168 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001169 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001170
Eric Christopher119ff7f2010-12-01 01:40:24 +00001171 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001172 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001173 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001174 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001175 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001176 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001177 return true;
1178}
1179
Eric Christopher29ab6d12010-09-27 06:02:23 +00001180bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001181 Value *Op0 = I->getOperand(0);
1182 unsigned SrcReg = 0;
1183
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001184 // Atomic stores need special handling.
1185 if (cast<StoreInst>(I)->isAtomic())
1186 return false;
1187
Eric Christopher119ff7f2010-12-01 01:40:24 +00001188 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001189 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001190 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001191 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001192
Eric Christopher92db2012010-09-02 01:48:11 +00001193 // Get the value to be stored into a register.
1194 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001195 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001196
Eric Christopher119ff7f2010-12-01 01:40:24 +00001197 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001198 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001199 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001200 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001201
Chad Rosierec3b77e2011-12-03 02:21:57 +00001202 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1203 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001204 return true;
1205}
1206
1207static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1208 switch (Pred) {
1209 // Needs two compares...
1210 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001211 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001212 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001213 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001214 return ARMCC::AL;
1215 case CmpInst::ICMP_EQ:
1216 case CmpInst::FCMP_OEQ:
1217 return ARMCC::EQ;
1218 case CmpInst::ICMP_SGT:
1219 case CmpInst::FCMP_OGT:
1220 return ARMCC::GT;
1221 case CmpInst::ICMP_SGE:
1222 case CmpInst::FCMP_OGE:
1223 return ARMCC::GE;
1224 case CmpInst::ICMP_UGT:
1225 case CmpInst::FCMP_UGT:
1226 return ARMCC::HI;
1227 case CmpInst::FCMP_OLT:
1228 return ARMCC::MI;
1229 case CmpInst::ICMP_ULE:
1230 case CmpInst::FCMP_OLE:
1231 return ARMCC::LS;
1232 case CmpInst::FCMP_ORD:
1233 return ARMCC::VC;
1234 case CmpInst::FCMP_UNO:
1235 return ARMCC::VS;
1236 case CmpInst::FCMP_UGE:
1237 return ARMCC::PL;
1238 case CmpInst::ICMP_SLT:
1239 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001240 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001241 case CmpInst::ICMP_SLE:
1242 case CmpInst::FCMP_ULE:
1243 return ARMCC::LE;
1244 case CmpInst::FCMP_UNE:
1245 case CmpInst::ICMP_NE:
1246 return ARMCC::NE;
1247 case CmpInst::ICMP_UGE:
1248 return ARMCC::HS;
1249 case CmpInst::ICMP_ULT:
1250 return ARMCC::LO;
1251 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001252}
1253
Eric Christopher29ab6d12010-09-27 06:02:23 +00001254bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001255 const BranchInst *BI = cast<BranchInst>(I);
1256 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1257 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001258
Eric Christopher6aaed722010-09-03 00:35:47 +00001259 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001260
Eric Christopher5c308f82010-10-29 21:08:19 +00001261 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1262 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001263 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001264 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001265
1266 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001267 // Try to take advantage of fallthrough opportunities.
1268 CmpInst::Predicate Predicate = CI->getPredicate();
1269 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1270 std::swap(TBB, FBB);
1271 Predicate = CmpInst::getInversePredicate(Predicate);
1272 }
1273
1274 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001275
1276 // We may not handle every CC for now.
1277 if (ARMPred == ARMCC::AL) return false;
1278
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001279 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001280 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001281 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001282
Chad Rosier0439cfc2011-11-08 21:12:00 +00001283 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher5c308f82010-10-29 21:08:19 +00001285 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001286 FastEmitBranch(FBB, DbgLoc);
Eric Christopher5c308f82010-10-29 21:08:19 +00001287 FuncInfo.MBB->addSuccessor(TBB);
1288 return true;
1289 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001290 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1291 MVT SourceVT;
1292 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001293 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001294 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001295 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001296 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8d46b472011-04-29 20:02:39 +00001298 TII.get(TstOpc))
1299 .addReg(OpReg).addImm(1));
1300
1301 unsigned CCMode = ARMCC::NE;
1302 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1303 std::swap(TBB, FBB);
1304 CCMode = ARMCC::EQ;
1305 }
1306
Chad Rosier0439cfc2011-11-08 21:12:00 +00001307 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher8d46b472011-04-29 20:02:39 +00001309 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1310
Rafael Espindolaea09c592014-02-18 22:05:46 +00001311 FastEmitBranch(FBB, DbgLoc);
Eric Christopher8d46b472011-04-29 20:02:39 +00001312 FuncInfo.MBB->addSuccessor(TBB);
1313 return true;
1314 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001315 } else if (const ConstantInt *CI =
1316 dyn_cast<ConstantInt>(BI->getCondition())) {
1317 uint64_t Imm = CI->getZExtValue();
1318 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001319 FastEmitBranch(Target, DbgLoc);
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001320 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001321 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001322
Eric Christopher5c308f82010-10-29 21:08:19 +00001323 unsigned CmpReg = getRegForValue(BI->getCondition());
1324 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001325
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001326 // We've been divorced from our compare! Our block was split, and
1327 // now our compare lives in a predecessor block. We musn't
1328 // re-compare here, as the children of the compare aren't guaranteed
1329 // live across the block boundary (we *could* check for this).
1330 // Regardless, the compare has been done in the predecessor block,
1331 // and it left a value for us in a virtual register. Ergo, we test
1332 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001333 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001334 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001335 AddOptionalDefs(
1336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1337 .addReg(CmpReg)
1338 .addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001339
Eric Christopher4f012fd2011-04-28 16:52:09 +00001340 unsigned CCMode = ARMCC::NE;
1341 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1342 std::swap(TBB, FBB);
1343 CCMode = ARMCC::EQ;
1344 }
1345
Chad Rosier0439cfc2011-11-08 21:12:00 +00001346 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001348 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001349 FastEmitBranch(FBB, DbgLoc);
Eric Christopher6aaed722010-09-03 00:35:47 +00001350 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001351 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001352}
1353
Chad Rosierded4c992012-02-07 23:56:08 +00001354bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1355 unsigned AddrReg = getRegForValue(I->getOperand(0));
1356 if (AddrReg == 0) return false;
1357
1358 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1360 TII.get(Opc)).addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001361
1362 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1363 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1364 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1365
Jush Luac96b762012-06-14 06:08:19 +00001366 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001367}
1368
Chad Rosier9cf803c2011-11-02 18:08:25 +00001369bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1370 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001371 Type *Ty = Src1Value->getType();
Patrik Hagglundc494d242012-12-17 14:30:06 +00001372 EVT SrcEVT = TLI.getValueType(Ty, true);
1373 if (!SrcEVT.isSimple()) return false;
1374 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001375
Chad Rosier78127d32011-10-26 23:25:44 +00001376 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1377 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001378 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001379
Chad Rosier595d4192011-11-09 03:22:02 +00001380 // Check to see if the 2nd operand is a constant that we can encode directly
1381 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001382 int Imm = 0;
1383 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001384 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001385 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1386 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001387 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1388 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1389 SrcVT == MVT::i1) {
1390 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001391 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001392 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
Jim Grosbach1a597112014-04-03 23:43:18 +00001393 // then a cmn, because there is no way to represent 2147483648 as a
Chad Rosier26d05882012-03-15 22:54:20 +00001394 // signed 32-bit int.
1395 if (Imm < 0 && Imm != (int)0x80000000) {
1396 isNegativeImm = true;
1397 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001398 }
Chad Rosier26d05882012-03-15 22:54:20 +00001399 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1400 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001401 }
1402 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1403 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1404 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001405 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001406 }
1407
Eric Christopherc3e9c402010-09-08 23:13:45 +00001408 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001409 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001410 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001411 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001412 default: return false;
1413 // TODO: Verify compares.
1414 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001415 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001416 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001417 break;
1418 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001419 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001420 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001421 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001422 case MVT::i1:
1423 case MVT::i8:
1424 case MVT::i16:
1425 needsExt = true;
1426 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001427 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001428 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001429 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001430 CmpOpc = ARM::t2CMPrr;
1431 else
Bill Wendling4b796472012-06-11 08:07:26 +00001432 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001433 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001434 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001435 CmpOpc = ARM::CMPrr;
1436 else
Bill Wendling4b796472012-06-11 08:07:26 +00001437 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001438 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001439 break;
1440 }
1441
Chad Rosier9cf803c2011-11-02 18:08:25 +00001442 unsigned SrcReg1 = getRegForValue(Src1Value);
1443 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001444
Duncan Sands12330652011-11-28 10:31:27 +00001445 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001446 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001447 SrcReg2 = getRegForValue(Src2Value);
1448 if (SrcReg2 == 0) return false;
1449 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001450
1451 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1452 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001453 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1454 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001455 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001456 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1457 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001458 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001459 }
Chad Rosier59a20192011-10-26 22:47:55 +00001460
Jim Grosbachd7866792013-08-16 23:37:40 +00001461 const MCInstrDesc &II = TII.get(CmpOpc);
1462 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001463 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001464 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001466 .addReg(SrcReg1).addReg(SrcReg2));
1467 } else {
1468 MachineInstrBuilder MIB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001469 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001470 .addReg(SrcReg1);
1471
1472 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1473 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001474 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001475 AddOptionalDefs(MIB);
1476 }
Chad Rosier78127d32011-10-26 23:25:44 +00001477
1478 // For floating point we need to move the result to a comparison register
1479 // that we can then use for branches.
1480 if (Ty->isFloatTy() || Ty->isDoubleTy())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001481 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier78127d32011-10-26 23:25:44 +00001482 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001483 return true;
1484}
1485
1486bool ARMFastISel::SelectCmp(const Instruction *I) {
1487 const CmpInst *CI = cast<CmpInst>(I);
1488
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001489 // Get the compare predicate.
1490 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001491
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001492 // We may not handle every CC for now.
1493 if (ARMPred == ARMCC::AL) return false;
1494
Chad Rosier59a20192011-10-26 22:47:55 +00001495 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001496 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001497 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001498
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001499 // Now set a register based on the comparison. Explicitly set the predicates
1500 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001501 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topperc7242e02012-04-20 07:30:17 +00001502 const TargetRegisterClass *RC = isThumb2 ?
1503 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1504 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001505 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001506 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001507 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001508 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001509 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001510 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001511 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001512
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001513 UpdateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001514 return true;
1515}
1516
Eric Christopher29ab6d12010-09-27 06:02:23 +00001517bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001518 // Make sure we have VFP and that we're extending float to double.
1519 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001520
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001521 Value *V = I->getOperand(0);
1522 if (!I->getType()->isDoubleTy() ||
1523 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001524
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001525 unsigned Op = getRegForValue(V);
1526 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001527
Craig Topperc7242e02012-04-20 07:30:17 +00001528 unsigned Result = createResultReg(&ARM::DPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001529 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001530 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001531 .addReg(Op));
1532 UpdateValueMap(I, Result);
1533 return true;
1534}
1535
Eric Christopher29ab6d12010-09-27 06:02:23 +00001536bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001537 // Make sure we have VFP and that we're truncating double to float.
1538 if (!Subtarget->hasVFP2()) return false;
1539
1540 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001541 if (!(I->getType()->isFloatTy() &&
1542 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001543
1544 unsigned Op = getRegForValue(V);
1545 if (Op == 0) return false;
1546
Craig Topperc7242e02012-04-20 07:30:17 +00001547 unsigned Result = createResultReg(&ARM::SPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001548 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001549 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001550 .addReg(Op));
1551 UpdateValueMap(I, Result);
1552 return true;
1553}
1554
Chad Rosiere023d5d2012-02-03 21:14:11 +00001555bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001556 // Make sure we have VFP.
1557 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001558
Duncan Sandsf5dda012010-11-03 11:35:31 +00001559 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001560 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001561 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001562 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001563
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001564 Value *Src = I->getOperand(0);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001565 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1566 if (!SrcEVT.isSimple())
1567 return false;
1568 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001569 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001570 return false;
1571
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001572 unsigned SrcReg = getRegForValue(Src);
1573 if (SrcReg == 0) return false;
1574
1575 // Handle sign-extension.
1576 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001577 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001578 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001579 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001580 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001581
Eric Christopher860fc932010-09-10 00:34:35 +00001582 // The conversion routine works on fp-reg to fp-reg and the operand above
1583 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001584 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001585 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001586
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001587 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001588 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1589 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001590 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001591
Eric Christopher4bd70472010-09-09 21:44:45 +00001592 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001593 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1594 TII.get(Opc), ResultReg).addReg(FP));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001595 UpdateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001596 return true;
1597}
1598
Chad Rosiere023d5d2012-02-03 21:14:11 +00001599bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001600 // Make sure we have VFP.
1601 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001602
Duncan Sandsf5dda012010-11-03 11:35:31 +00001603 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001604 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001605 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001606 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001607
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001608 unsigned Op = getRegForValue(I->getOperand(0));
1609 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001610
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001611 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001612 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001613 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1614 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001615 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001616
Chad Rosier41f0e782012-02-03 20:27:51 +00001617 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001618 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001619 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1620 TII.get(Opc), ResultReg).addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001621
Eric Christopher4bd70472010-09-09 21:44:45 +00001622 // This result needs to be in an integer register, but the conversion only
1623 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001624 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001625 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001626
Eric Christopher4bd70472010-09-09 21:44:45 +00001627 UpdateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001628 return true;
1629}
1630
Eric Christopher511aa312010-10-11 08:27:59 +00001631bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001632 MVT VT;
1633 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001634 return false;
1635
1636 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001637 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001638
1639 unsigned CondReg = getRegForValue(I->getOperand(0));
1640 if (CondReg == 0) return false;
1641 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1642 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001643
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001644 // Check to see if we can use an immediate in the conditional move.
1645 int Imm = 0;
1646 bool UseImm = false;
1647 bool isNegativeImm = false;
1648 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1649 assert (VT == MVT::i32 && "Expecting an i32.");
1650 Imm = (int)ConstInt->getValue().getZExtValue();
1651 if (Imm < 0) {
1652 isNegativeImm = true;
1653 Imm = ~Imm;
1654 }
1655 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1656 (ARM_AM::getSOImmVal(Imm) != -1);
1657 }
1658
Duncan Sands12330652011-11-28 10:31:27 +00001659 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001660 if (!UseImm) {
1661 Op2Reg = getRegForValue(I->getOperand(2));
1662 if (Op2Reg == 0) return false;
1663 }
1664
1665 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Jim Grosbachd7866792013-08-16 23:37:40 +00001666 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001667 AddOptionalDefs(
1668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1669 .addReg(CondReg)
1670 .addImm(0));
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001671
1672 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001673 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001674 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001675 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001676 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1677 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001678 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1679 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001680 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001681 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001682 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001683 }
Eric Christopher511aa312010-10-11 08:27:59 +00001684 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001685 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001686 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001687 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1689 ResultReg)
1690 .addReg(Op2Reg)
1691 .addReg(Op1Reg)
1692 .addImm(ARMCC::NE)
1693 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001694 } else {
1695 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001696 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1697 ResultReg)
1698 .addReg(Op1Reg)
1699 .addImm(Imm)
1700 .addImm(ARMCC::EQ)
1701 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001702 }
Eric Christopher511aa312010-10-11 08:27:59 +00001703 UpdateValueMap(I, ResultReg);
1704 return true;
1705}
1706
Chad Rosieraaa55a82012-02-03 21:07:27 +00001707bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001708 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001709 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001710 if (!isTypeLegal(Ty, VT))
1711 return false;
1712
1713 // If we have integer div support we should have selected this automagically.
1714 // In case we have a real miss go ahead and return false and we'll pick
1715 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001716 if (Subtarget->hasDivide()) return false;
1717
Eric Christopher56094ff2010-09-30 22:34:19 +00001718 // Otherwise emit a libcall.
1719 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001720 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001721 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001722 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001723 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001724 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001725 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001726 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001727 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001728 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001729 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001730 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001731
Eric Christopher56094ff2010-09-30 22:34:19 +00001732 return ARMEmitLibcall(I, LC);
1733}
1734
Chad Rosierb84a4b42012-02-03 21:23:45 +00001735bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001736 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001737 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001738 if (!isTypeLegal(Ty, VT))
1739 return false;
1740
1741 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1742 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001743 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001744 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001745 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001746 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001747 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001748 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001749 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001750 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001751 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001752 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001753
Eric Christophereae1b382010-10-11 08:37:26 +00001754 return ARMEmitLibcall(I, LC);
1755}
1756
Chad Rosier685b20c2012-02-06 23:50:07 +00001757bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier685b20c2012-02-06 23:50:07 +00001758 EVT DestVT = TLI.getValueType(I->getType(), true);
1759
1760 // We can get here in the case when we have a binary operation on a non-legal
1761 // type and the target independent selector doesn't know how to handle it.
1762 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1763 return false;
Jush Luac96b762012-06-14 06:08:19 +00001764
Chad Rosierbd471252012-02-08 02:29:21 +00001765 unsigned Opc;
1766 switch (ISDOpcode) {
1767 default: return false;
1768 case ISD::ADD:
1769 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1770 break;
1771 case ISD::OR:
1772 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1773 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001774 case ISD::SUB:
1775 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1776 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001777 }
1778
Chad Rosier685b20c2012-02-06 23:50:07 +00001779 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1780 if (SrcReg1 == 0) return false;
1781
1782 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1783 // in the instruction, rather then materializing the value in a register.
1784 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1785 if (SrcReg2 == 0) return false;
1786
JF Bastien13969d02013-05-29 15:45:47 +00001787 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001788 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1789 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001790 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier685b20c2012-02-06 23:50:07 +00001791 TII.get(Opc), ResultReg)
1792 .addReg(SrcReg1).addReg(SrcReg2));
1793 UpdateValueMap(I, ResultReg);
1794 return true;
1795}
1796
1797bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001798 EVT FPVT = TLI.getValueType(I->getType(), true);
1799 if (!FPVT.isSimple()) return false;
1800 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001801
Eric Christopher24dc27f2010-09-09 00:53:57 +00001802 // We can get here in the case when we want to use NEON for our fp
1803 // operations, but can't figure out how to. Just use the vfp instructions
1804 // if we have them.
1805 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001806 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001807 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1808 if (isFloat && !Subtarget->hasVFP2())
1809 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001810
Eric Christopher24dc27f2010-09-09 00:53:57 +00001811 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001812 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001813 switch (ISDOpcode) {
1814 default: return false;
1815 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001816 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001817 break;
1818 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001819 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001820 break;
1821 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001822 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001823 break;
1824 }
Chad Rosier80979b62011-11-16 18:39:44 +00001825 unsigned Op1 = getRegForValue(I->getOperand(0));
1826 if (Op1 == 0) return false;
1827
1828 unsigned Op2 = getRegForValue(I->getOperand(1));
1829 if (Op2 == 0) return false;
1830
Chad Rosier62a144f2012-12-17 19:59:43 +00001831 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001832 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher24dc27f2010-09-09 00:53:57 +00001833 TII.get(Opc), ResultReg)
1834 .addReg(Op1).addReg(Op2));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001835 UpdateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001836 return true;
1837}
1838
Eric Christopher72497e52010-09-10 23:18:12 +00001839// Call Handling Code
1840
Jush Lue67e07b2012-07-19 09:49:00 +00001841// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001842// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001843CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1844 bool Return,
1845 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001846 switch (CC) {
1847 default:
1848 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001849 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001850 if (Subtarget->hasVFP2() && !isVarArg) {
1851 if (!Subtarget->isAAPCS_ABI())
1852 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1853 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1854 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1855 }
Evan Cheng21abfc92010-10-22 18:57:05 +00001856 // Fallthrough
1857 case CallingConv::C:
Eric Christopher72497e52010-09-10 23:18:12 +00001858 // Use target triple & subtarget features to do actual dispatch.
1859 if (Subtarget->isAAPCS_ABI()) {
1860 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001861 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001862 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1863 else
1864 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1865 } else
1866 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1867 case CallingConv::ARM_AAPCS_VFP:
Jush Lue67e07b2012-07-19 09:49:00 +00001868 if (!isVarArg)
1869 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1870 // Fall through to soft float variant, variadic functions don't
1871 // use hard floating point ABI.
Eric Christopher72497e52010-09-10 23:18:12 +00001872 case CallingConv::ARM_AAPCS:
1873 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1874 case CallingConv::ARM_APCS:
1875 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001876 case CallingConv::GHC:
1877 if (Return)
1878 llvm_unreachable("Can't return in GHC call convention");
1879 else
1880 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001881 }
1882}
1883
Eric Christopher79398062010-09-29 23:11:09 +00001884bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1885 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001886 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001887 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1888 SmallVectorImpl<unsigned> &RegArgs,
1889 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001890 unsigned &NumBytes,
1891 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001892 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001893 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00001894 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1895 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001896
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001897 // Check that we can handle all of the arguments. If we can't, then bail out
1898 // now before we add code to the MBB.
1899 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1900 CCValAssign &VA = ArgLocs[i];
1901 MVT ArgVT = ArgVTs[VA.getValNo()];
1902
1903 // We don't handle NEON/vector parameters yet.
1904 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1905 return false;
1906
1907 // Now copy/store arg to correct locations.
1908 if (VA.isRegLoc() && !VA.needsCustom()) {
1909 continue;
1910 } else if (VA.needsCustom()) {
1911 // TODO: We need custom lowering for vector (v2f64) args.
1912 if (VA.getLocVT() != MVT::f64 ||
1913 // TODO: Only handle register args for now.
1914 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1915 return false;
1916 } else {
Craig Topper56710102013-08-15 02:33:50 +00001917 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001918 default:
1919 return false;
1920 case MVT::i1:
1921 case MVT::i8:
1922 case MVT::i16:
1923 case MVT::i32:
1924 break;
1925 case MVT::f32:
1926 if (!Subtarget->hasVFP2())
1927 return false;
1928 break;
1929 case MVT::f64:
1930 if (!Subtarget->hasVFP2())
1931 return false;
1932 break;
1933 }
1934 }
1935 }
1936
1937 // At the point, we are able to handle the call's arguments in fast isel.
1938
Eric Christopher79398062010-09-29 23:11:09 +00001939 // Get a count of how many bytes are to be pushed on the stack.
1940 NumBytes = CCInfo.getNextStackOffset();
1941
1942 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00001943 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001944 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00001945 TII.get(AdjStackDown))
1946 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00001947
1948 // Process the args.
1949 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1950 CCValAssign &VA = ArgLocs[i];
Juergen Ributzka4c018a12014-08-01 18:04:14 +00001951 const Value *ArgVal = Args[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001952 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00001953 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001954
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001955 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1956 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00001957
Eric Christopher78f8d4e2010-09-30 20:49:44 +00001958 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00001959 switch (VA.getLocInfo()) {
1960 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00001961 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001962 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001963 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1964 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001965 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001966 break;
1967 }
Chad Rosierd0191a52011-11-05 20:16:15 +00001968 case CCValAssign::AExt:
1969 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00001970 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001971 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001972 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00001973 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001974 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001975 break;
1976 }
1977 case CCValAssign::BCvt: {
Wesley Peck527da1b2010-11-23 03:31:01 +00001978 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001979 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00001980 assert(BC != 0 && "Failed to emit a bitcast!");
1981 Arg = BC;
1982 ArgVT = VA.getLocVT();
1983 break;
1984 }
1985 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00001986 }
1987
1988 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00001989 if (VA.isRegLoc() && !VA.needsCustom()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001990 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1991 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00001992 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001993 } else if (VA.needsCustom()) {
1994 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001995 assert(VA.getLocVT() == MVT::f64 &&
1996 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00001997
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001998 CCValAssign &NextVA = ArgLocs[++i];
1999
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002000 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2001 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002002
Rafael Espindolaea09c592014-02-18 22:05:46 +00002003 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002004 TII.get(ARM::VMOVRRD), VA.getLocReg())
2005 .addReg(NextVA.getLocReg(), RegState::Define)
2006 .addReg(Arg));
2007 RegArgs.push_back(VA.getLocReg());
2008 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002009 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002010 assert(VA.isMemLoc());
2011 // Need to store on the stack.
Juergen Ributzka4c018a12014-08-01 18:04:14 +00002012
2013 // Don't emit stores for undef values.
2014 if (isa<UndefValue>(ArgVal))
2015 continue;
2016
Eric Christopherfef5f312010-11-19 22:30:02 +00002017 Address Addr;
2018 Addr.BaseType = Address::RegBase;
2019 Addr.Base.Reg = ARM::SP;
2020 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002021
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002022 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2023 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002024 }
2025 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002026
Eric Christopher79398062010-09-29 23:11:09 +00002027 return true;
2028}
2029
Duncan Sandsf5dda012010-11-03 11:35:31 +00002030bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002031 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002032 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002033 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002034 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002035 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002036 TII.get(AdjStackUp))
2037 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002038
2039 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002040 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002041 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002042 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002043 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002044
2045 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002046 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002047 // For this move we copy into two registers and then move into the
2048 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002049 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002050 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002051 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002052 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopherc1e209d2010-10-01 00:00:11 +00002053 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002054 .addReg(RVLocs[0].getLocReg())
2055 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002056
Eric Christopheraf719ef2010-10-20 08:02:24 +00002057 UsedRegs.push_back(RVLocs[0].getLocReg());
2058 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002059
Eric Christopher7ac602b2010-10-11 08:38:55 +00002060 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002061 UpdateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002062 } else {
2063 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002064 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002065
2066 // Special handling for extended integers.
2067 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2068 CopyVT = MVT::i32;
2069
Craig Topper760b1342012-02-22 05:59:10 +00002070 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002071
Eric Christopherc1e209d2010-10-01 00:00:11 +00002072 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002073 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2074 TII.get(TargetOpcode::COPY),
Eric Christopherc1e209d2010-10-01 00:00:11 +00002075 ResultReg).addReg(RVLocs[0].getLocReg());
2076 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002077
Eric Christopher7ac602b2010-10-11 08:38:55 +00002078 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002079 UpdateValueMap(I, ResultReg);
2080 }
Eric Christopher79398062010-09-29 23:11:09 +00002081 }
2082
Eric Christopher7ac602b2010-10-11 08:38:55 +00002083 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002084}
2085
Eric Christopher93bbe652010-10-22 01:28:00 +00002086bool ARMFastISel::SelectRet(const Instruction *I) {
2087 const ReturnInst *Ret = cast<ReturnInst>(I);
2088 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002089
Eric Christopher93bbe652010-10-22 01:28:00 +00002090 if (!FuncInfo.CanLowerReturn)
2091 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002092
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002093 // Build a list of return value registers.
2094 SmallVector<unsigned, 4> RetRegs;
2095
Eric Christopher93bbe652010-10-22 01:28:00 +00002096 CallingConv::ID CC = F.getCallingConv();
2097 if (Ret->getNumOperands() > 0) {
2098 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002099 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher93bbe652010-10-22 01:28:00 +00002100
2101 // Analyze operands of the call, assigning locations to each operand.
2102 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002103 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002104 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2105 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002106
2107 const Value *RV = Ret->getOperand(0);
2108 unsigned Reg = getRegForValue(RV);
2109 if (Reg == 0)
2110 return false;
2111
2112 // Only handle a single return value for now.
2113 if (ValLocs.size() != 1)
2114 return false;
2115
2116 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002117
Eric Christopher93bbe652010-10-22 01:28:00 +00002118 // Don't bother handling odd stuff for now.
2119 if (VA.getLocInfo() != CCValAssign::Full)
2120 return false;
2121 // Only handle register returns for now.
2122 if (!VA.isRegLoc())
2123 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002124
2125 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier62a144f2012-12-17 19:59:43 +00002126 EVT RVEVT = TLI.getValueType(RV->getType());
2127 if (!RVEVT.isSimple()) return false;
2128 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002129 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002130 // Special handling for extended integers.
2131 if (RVVT != DestVT) {
2132 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2133 return false;
2134
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002135 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2136
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002137 // Perform extension if flagged as either zext or sext. Otherwise, do
2138 // nothing.
2139 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2140 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2141 if (SrcReg == 0) return false;
2142 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002143 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002144
Eric Christopher93bbe652010-10-22 01:28:00 +00002145 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002146 unsigned DstReg = VA.getLocReg();
2147 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2148 // Avoid a cross-class copy. This is very unlikely.
2149 if (!SrcRC->contains(DstReg))
2150 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002151 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2152 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
Eric Christopher93bbe652010-10-22 01:28:00 +00002153
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002154 // Add register to return instruction.
2155 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002156 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002157
Chad Rosier0439cfc2011-11-08 21:12:00 +00002158 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002159 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002160 TII.get(RetOpc));
2161 AddOptionalDefs(MIB);
2162 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2163 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002164 return true;
2165}
2166
Chad Rosierc6916f82012-06-12 19:25:13 +00002167unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2168 if (UseReg)
2169 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2170 else
2171 return isThumb2 ? ARM::tBL : ARM::BL;
2172}
2173
2174unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002175 // Manually compute the global's type to avoid building it when unnecessary.
2176 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2177 EVT LCREVT = TLI.getValueType(GVTy);
2178 if (!LCREVT.isSimple()) return 0;
2179
Bill Wendling76cce192013-12-29 08:00:04 +00002180 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Craig Topper062a2ba2014-04-25 05:30:21 +00002181 GlobalValue::ExternalLinkage, nullptr,
2182 Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002183 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002184 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002185}
2186
Eric Christopher8b912662010-09-14 23:03:37 +00002187// A quick function that will emit a call for a named libcall in F with the
2188// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002189// can emit a call for any libcall we can produce. This is an abridged version
2190// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002191// like computed function pointers or strange arguments at call sites.
2192// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2193// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002194bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2195 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002196
Eric Christopher8b912662010-09-14 23:03:37 +00002197 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002198 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002199 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002200 if (RetTy->isVoidTy())
2201 RetVT = MVT::isVoid;
2202 else if (!isTypeLegal(RetTy, RetVT))
2203 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002204
Chad Rosier90f9afe2012-05-11 18:51:55 +00002205 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002206 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002207 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002208 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002209 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002210 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2211 return false;
2212 }
2213
Eric Christopher79398062010-09-29 23:11:09 +00002214 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002215 SmallVector<Value*, 8> Args;
2216 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002217 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002218 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2219 Args.reserve(I->getNumOperands());
2220 ArgRegs.reserve(I->getNumOperands());
2221 ArgVTs.reserve(I->getNumOperands());
2222 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002223 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002224 Value *Op = I->getOperand(i);
2225 unsigned Arg = getRegForValue(Op);
2226 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002227
Chris Lattner229907c2011-07-18 04:54:35 +00002228 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002229 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002230 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002231
Eric Christopher8b912662010-09-14 23:03:37 +00002232 ISD::ArgFlagsTy Flags;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002233 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher8b912662010-09-14 23:03:37 +00002234 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002235
Eric Christopher8b912662010-09-14 23:03:37 +00002236 Args.push_back(Op);
2237 ArgRegs.push_back(Arg);
2238 ArgVTs.push_back(ArgVT);
2239 ArgFlags.push_back(Flags);
2240 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002241
Eric Christopher79398062010-09-29 23:11:09 +00002242 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002243 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002244 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002245 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2246 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002247 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002248
Chad Rosierc6916f82012-06-12 19:25:13 +00002249 unsigned CalleeReg = 0;
2250 if (EnableARMLongCalls) {
2251 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2252 if (CalleeReg == 0) return false;
2253 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002254
Chad Rosierc6916f82012-06-12 19:25:13 +00002255 // Issue the call.
2256 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2257 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002258 DbgLoc, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002259 // BL / BLX don't take a predicate, but tBL / tBLX do.
2260 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002261 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002262 if (EnableARMLongCalls)
2263 MIB.addReg(CalleeReg);
2264 else
2265 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002266
Eric Christopher8b912662010-09-14 23:03:37 +00002267 // Add implicit physical register uses to the call.
2268 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002269 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002270
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002271 // Add a register mask with the call-preserved registers.
2272 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2273 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2274
Eric Christopher79398062010-09-29 23:11:09 +00002275 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002276 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002277 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002278
Eric Christopher8b912662010-09-14 23:03:37 +00002279 // Set all unused physreg defs as dead.
2280 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002281
Eric Christopher8b912662010-09-14 23:03:37 +00002282 return true;
2283}
2284
Chad Rosiera7ebc562011-11-11 23:31:03 +00002285bool ARMFastISel::SelectCall(const Instruction *I,
Craig Topper062a2ba2014-04-25 05:30:21 +00002286 const char *IntrMemName = nullptr) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002287 const CallInst *CI = cast<CallInst>(I);
2288 const Value *Callee = CI->getCalledValue();
2289
Chad Rosiera7ebc562011-11-11 23:31:03 +00002290 // Can't handle inline asm.
2291 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002292
Chad Rosierdf42cf32012-12-11 00:18:02 +00002293 // Allow SelectionDAG isel to handle tail calls.
2294 if (CI->isTailCall()) return false;
2295
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002296 // Check the calling convention.
2297 ImmutableCallSite CS(CI);
2298 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002299
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002300 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002301
Chris Lattner229907c2011-07-18 04:54:35 +00002302 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2303 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Lue67e07b2012-07-19 09:49:00 +00002304 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002305
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002306 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002307 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002308 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002309 if (RetTy->isVoidTy())
2310 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002311 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2312 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002313 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002314
Chad Rosier90f9afe2012-05-11 18:51:55 +00002315 // Can't handle non-double multi-reg retvals.
2316 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2317 RetVT != MVT::i16 && RetVT != MVT::i32) {
2318 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002319 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002320 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002321 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2322 return false;
2323 }
2324
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002325 // Set up the argument vectors.
2326 SmallVector<Value*, 8> Args;
2327 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002328 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002329 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002330 unsigned arg_size = CS.arg_size();
2331 Args.reserve(arg_size);
2332 ArgRegs.reserve(arg_size);
2333 ArgVTs.reserve(arg_size);
2334 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002335 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2336 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002337 // If we're lowering a memory intrinsic instead of a regular call, skip the
2338 // last two arguments, which shouldn't be passed to the underlying function.
2339 if (IntrMemName && e-i <= 2)
2340 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002341
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002342 ISD::ArgFlagsTy Flags;
2343 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002344 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002345 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002346 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002347 Flags.setZExt();
2348
Chad Rosier8a98ec42011-11-04 00:58:10 +00002349 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002350 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2351 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2352 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2353 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002354 return false;
2355
Chris Lattner229907c2011-07-18 04:54:35 +00002356 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002357 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002358 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2359 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002360 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002361
2362 unsigned Arg = getRegForValue(*i);
2363 if (Arg == 0)
2364 return false;
2365
Rafael Espindolaea09c592014-02-18 22:05:46 +00002366 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002367 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002368
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002369 Args.push_back(*i);
2370 ArgRegs.push_back(Arg);
2371 ArgVTs.push_back(ArgVT);
2372 ArgFlags.push_back(Flags);
2373 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002374
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002375 // Handle the arguments now that we've gotten them.
2376 SmallVector<unsigned, 4> RegArgs;
2377 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002378 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2379 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002380 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002381
Chad Rosierc6916f82012-06-12 19:25:13 +00002382 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002383 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosierc6916f82012-06-12 19:25:13 +00002384 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002385
Chad Rosierc6916f82012-06-12 19:25:13 +00002386 unsigned CalleeReg = 0;
2387 if (UseReg) {
2388 if (IntrMemName)
2389 CalleeReg = getLibcallReg(IntrMemName);
2390 else
2391 CalleeReg = getRegForValue(Callee);
2392
Chad Rosier223faf72012-05-23 18:38:57 +00002393 if (CalleeReg == 0) return false;
2394 }
2395
Chad Rosierc6916f82012-06-12 19:25:13 +00002396 // Issue the call.
2397 unsigned CallOpc = ARMSelectCallOp(UseReg);
2398 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002399 DbgLoc, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002400
Logan Chien2361f512013-08-22 12:08:04 +00002401 unsigned char OpFlags = 0;
2402
2403 // Add MO_PLT for global address or external symbol in the PIC relocation
2404 // model.
2405 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2406 OpFlags = ARMII::MO_PLT;
2407
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002408 // ARM calls don't take a predicate, but tBL / tBLX do.
2409 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002410 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002411 if (UseReg)
2412 MIB.addReg(CalleeReg);
2413 else if (!IntrMemName)
Logan Chien2361f512013-08-22 12:08:04 +00002414 MIB.addGlobalAddress(GV, 0, OpFlags);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002415 else
Logan Chien2361f512013-08-22 12:08:04 +00002416 MIB.addExternalSymbol(IntrMemName, OpFlags);
Jush Luac96b762012-06-14 06:08:19 +00002417
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002418 // Add implicit physical register uses to the call.
2419 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002420 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002421
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002422 // Add a register mask with the call-preserved registers.
2423 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2424 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2425
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002426 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002427 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002428 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2429 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002430
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002431 // Set all unused physreg defs as dead.
2432 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002433
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002434 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002435}
2436
Chad Rosier057b6d32011-11-14 23:04:09 +00002437bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002438 return Len <= 16;
2439}
2440
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002441bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002442 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002443 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002444 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002445 return false;
2446
Chad Rosierab7223e2011-11-14 22:46:17 +00002447 while (Len) {
2448 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002449 if (!Alignment || Alignment >= 4) {
2450 if (Len >= 4)
2451 VT = MVT::i32;
2452 else if (Len >= 2)
2453 VT = MVT::i16;
2454 else {
2455 assert (Len == 1 && "Expected a length of 1!");
2456 VT = MVT::i8;
2457 }
2458 } else {
2459 // Bound based on alignment.
2460 if (Len >= 2 && Alignment == 2)
2461 VT = MVT::i16;
2462 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002463 VT = MVT::i8;
2464 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002465 }
2466
2467 bool RV;
2468 unsigned ResultReg;
2469 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002470 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002471 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002472 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002473 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002474
2475 unsigned Size = VT.getSizeInBits()/8;
2476 Len -= Size;
2477 Dest.Offset += Size;
2478 Src.Offset += Size;
2479 }
2480
2481 return true;
2482}
2483
Chad Rosiera7ebc562011-11-11 23:31:03 +00002484bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2485 // FIXME: Handle more intrinsics.
2486 switch (I.getIntrinsicID()) {
2487 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002488 case Intrinsic::frameaddress: {
2489 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2490 MFI->setFrameAddressIsTaken(true);
2491
2492 unsigned LdrOpc;
2493 const TargetRegisterClass *RC;
2494 if (isThumb2) {
2495 LdrOpc = ARM::t2LDRi12;
2496 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2497 } else {
2498 LdrOpc = ARM::LDRi12;
2499 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2500 }
2501
2502 const ARMBaseRegisterInfo *RegInfo =
Eric Christopherd9134482014-08-04 21:25:23 +00002503 static_cast<const ARMBaseRegisterInfo *>(
2504 TM.getSubtargetImpl()->getRegisterInfo());
Chad Rosier820d248c2012-05-30 17:23:22 +00002505 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2506 unsigned SrcReg = FramePtr;
2507
2508 // Recursively load frame address
2509 // ldr r0 [fp]
2510 // ldr r0 [r0]
2511 // ldr r0 [r0]
2512 // ...
2513 unsigned DestReg;
2514 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2515 while (Depth--) {
2516 DestReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002517 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier820d248c2012-05-30 17:23:22 +00002518 TII.get(LdrOpc), DestReg)
2519 .addReg(SrcReg).addImm(0));
2520 SrcReg = DestReg;
2521 }
Chad Rosierf3193242012-06-01 21:12:31 +00002522 UpdateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002523 return true;
2524 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002525 case Intrinsic::memcpy:
2526 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002527 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2528 // Don't handle volatile.
2529 if (MTI.isVolatile())
2530 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002531
2532 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2533 // we would emit dead code because we don't currently handle memmoves.
2534 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2535 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002536 // Small memcpy's are common enough that we want to do them without a call
2537 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002538 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002539 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002540 Address Dest, Src;
2541 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2542 !ARMComputeAddress(MTI.getRawSource(), Src))
2543 return false;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002544 unsigned Alignment = MTI.getAlignment();
2545 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002546 return true;
2547 }
2548 }
Jush Luac96b762012-06-14 06:08:19 +00002549
Chad Rosiera7ebc562011-11-11 23:31:03 +00002550 if (!MTI.getLength()->getType()->isIntegerTy(32))
2551 return false;
Jush Luac96b762012-06-14 06:08:19 +00002552
Chad Rosiera7ebc562011-11-11 23:31:03 +00002553 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2554 return false;
2555
2556 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2557 return SelectCall(&I, IntrMemName);
2558 }
2559 case Intrinsic::memset: {
2560 const MemSetInst &MSI = cast<MemSetInst>(I);
2561 // Don't handle volatile.
2562 if (MSI.isVolatile())
2563 return false;
Jush Luac96b762012-06-14 06:08:19 +00002564
Chad Rosiera7ebc562011-11-11 23:31:03 +00002565 if (!MSI.getLength()->getType()->isIntegerTy(32))
2566 return false;
Jush Luac96b762012-06-14 06:08:19 +00002567
Chad Rosiera7ebc562011-11-11 23:31:03 +00002568 if (MSI.getDestAddressSpace() > 255)
2569 return false;
Jush Luac96b762012-06-14 06:08:19 +00002570
Chad Rosiera7ebc562011-11-11 23:31:03 +00002571 return SelectCall(&I, "memset");
2572 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002573 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002575 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002576 return true;
2577 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002578 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002579}
2580
Chad Rosieree7e4522011-11-02 00:18:48 +00002581bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002582 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002583 // undefined.
2584 Value *Op = I->getOperand(0);
2585
2586 EVT SrcVT, DestVT;
2587 SrcVT = TLI.getValueType(Op->getType(), true);
2588 DestVT = TLI.getValueType(I->getType(), true);
2589
2590 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2591 return false;
2592 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2593 return false;
2594
2595 unsigned SrcReg = getRegForValue(Op);
2596 if (!SrcReg) return false;
2597
2598 // Because the high bits are undefined, a truncate doesn't generate
2599 // any code.
2600 UpdateValueMap(I, SrcReg);
2601 return true;
2602}
2603
Chad Rosier62a144f2012-12-17 19:59:43 +00002604unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002605 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002606 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002607 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002608 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002609 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002610
2611 // Table of which combinations can be emitted as a single instruction,
2612 // and which will require two.
2613 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2614 // ARM Thumb
2615 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2616 // ext: s z s z s z s z
2617 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2618 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2619 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2620 };
2621
2622 // Target registers for:
2623 // - For ARM can never be PC.
2624 // - For 16-bit Thumb are restricted to lower 8 registers.
2625 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2626 static const TargetRegisterClass *RCTbl[2][2] = {
2627 // Instructions: Two Single
2628 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2629 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2630 };
2631
2632 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002633 static const struct InstructionTable {
2634 uint32_t Opc : 16;
2635 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2636 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2637 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2638 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002639 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002640 { // ARM Opc S Shift Imm
2641 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2642 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2643 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2644 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2645 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2646 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002647 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002648 { // Thumb Opc S Shift Imm
2649 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2650 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2651 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2652 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2653 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2654 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002655 }
2656 },
2657 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002658 { // ARM Opc S Shift Imm
2659 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2660 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2661 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2662 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2663 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2664 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002665 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002666 { // Thumb Opc S Shift Imm
2667 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2668 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2669 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2670 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2671 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2672 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002673 }
2674 }
2675 };
2676
2677 unsigned SrcBits = SrcVT.getSizeInBits();
2678 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002679 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002680 assert((SrcBits < DestBits) && "can only extend to larger types");
2681 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2682 "other sizes unimplemented");
2683 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2684 "other sizes unimplemented");
2685
2686 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002687 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002688 assert((Bitness < 3) && "sanity-check table bounds");
2689
2690 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2691 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002692 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2693 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002694 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002695 unsigned hasS = ITP->hasS;
2696 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2697 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2698 "only MOVsi has shift operand addressing mode");
2699 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002700
2701 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2702 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002703 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002704 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002705 // MOVsi encodes shift and immediate in shift operand addressing mode.
2706 // The following condition has the same value when emitting two
2707 // instruction sequences: both are shifts.
2708 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002709
2710 // Either one or two instructions are emitted.
2711 // They're always of the form:
2712 // dst = in OP imm
2713 // CPSR is set only by 16-bit Thumb instructions.
2714 // Predicate, if any, is AL.
2715 // S bit, if available, is always 0.
2716 // When two are emitted the first's result will feed as the second's input,
2717 // that value is then dead.
2718 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2719 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2720 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002721 bool isLsl = (0 == Instr) && !isSingleInstr;
2722 unsigned Opcode = isLsl ? LSLOpc : Opc;
2723 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2724 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002725 bool isKill = 1 == Instr;
2726 MachineInstrBuilder MIB = BuildMI(
Rafael Espindolaea09c592014-02-18 22:05:46 +00002727 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
JF Bastien06ce03d2013-06-07 20:10:37 +00002728 if (setsCPSR)
2729 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002730 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002731 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002732 if (hasS)
2733 AddDefaultCC(MIB);
2734 // Second instruction consumes the first's result.
2735 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002736 }
2737
Chad Rosier4489f942011-11-02 17:20:24 +00002738 return ResultReg;
2739}
2740
2741bool ARMFastISel::SelectIntExt(const Instruction *I) {
2742 // On ARM, in general, integer casts don't involve legal types; this code
2743 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002744 Type *DestTy = I->getType();
2745 Value *Src = I->getOperand(0);
2746 Type *SrcTy = Src->getType();
2747
Chad Rosier4489f942011-11-02 17:20:24 +00002748 bool isZExt = isa<ZExtInst>(I);
2749 unsigned SrcReg = getRegForValue(Src);
2750 if (!SrcReg) return false;
2751
Chad Rosier62a144f2012-12-17 19:59:43 +00002752 EVT SrcEVT, DestEVT;
2753 SrcEVT = TLI.getValueType(SrcTy, true);
2754 DestEVT = TLI.getValueType(DestTy, true);
2755 if (!SrcEVT.isSimple()) return false;
2756 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002757
Chad Rosier62a144f2012-12-17 19:59:43 +00002758 MVT SrcVT = SrcEVT.getSimpleVT();
2759 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002760 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2761 if (ResultReg == 0) return false;
2762 UpdateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002763 return true;
2764}
2765
Jush Lu4705da92012-08-03 02:37:48 +00002766bool ARMFastISel::SelectShift(const Instruction *I,
2767 ARM_AM::ShiftOpc ShiftTy) {
2768 // We handle thumb2 mode by target independent selector
2769 // or SelectionDAG ISel.
2770 if (isThumb2)
2771 return false;
2772
2773 // Only handle i32 now.
2774 EVT DestVT = TLI.getValueType(I->getType(), true);
2775 if (DestVT != MVT::i32)
2776 return false;
2777
2778 unsigned Opc = ARM::MOVsr;
2779 unsigned ShiftImm;
2780 Value *Src2Value = I->getOperand(1);
2781 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2782 ShiftImm = CI->getZExtValue();
2783
2784 // Fall back to selection DAG isel if the shift amount
2785 // is zero or greater than the width of the value type.
2786 if (ShiftImm == 0 || ShiftImm >=32)
2787 return false;
2788
2789 Opc = ARM::MOVsi;
2790 }
2791
2792 Value *Src1Value = I->getOperand(0);
2793 unsigned Reg1 = getRegForValue(Src1Value);
2794 if (Reg1 == 0) return false;
2795
Nadav Rotema8e15b02012-09-06 11:13:55 +00002796 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002797 if (Opc == ARM::MOVsr) {
2798 Reg2 = getRegForValue(Src2Value);
2799 if (Reg2 == 0) return false;
2800 }
2801
JF Bastien13969d02013-05-29 15:45:47 +00002802 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002803 if(ResultReg == 0) return false;
2804
Rafael Espindolaea09c592014-02-18 22:05:46 +00002805 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu4705da92012-08-03 02:37:48 +00002806 TII.get(Opc), ResultReg)
2807 .addReg(Reg1);
2808
2809 if (Opc == ARM::MOVsi)
2810 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2811 else if (Opc == ARM::MOVsr) {
2812 MIB.addReg(Reg2);
2813 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2814 }
2815
2816 AddOptionalDefs(MIB);
2817 UpdateValueMap(I, ResultReg);
2818 return true;
2819}
2820
Eric Christopherc3e118e2010-09-02 23:43:26 +00002821// TODO: SoftFP support.
Eric Christopher84bdfd82010-07-21 22:26:11 +00002822bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002823
Eric Christopher84bdfd82010-07-21 22:26:11 +00002824 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002825 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002826 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002827 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002828 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002829 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002830 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002831 case Instruction::IndirectBr:
2832 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002833 case Instruction::ICmp:
2834 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002835 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002836 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002837 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002838 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002839 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002840 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002841 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002842 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002843 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002844 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002845 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002846 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002847 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002848 case Instruction::Add:
2849 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002850 case Instruction::Or:
2851 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002852 case Instruction::Sub:
2853 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002854 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002855 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002856 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002857 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002858 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002859 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002860 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002861 return SelectDiv(I, /*isSigned*/ true);
2862 case Instruction::UDiv:
2863 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002864 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002865 return SelectRem(I, /*isSigned*/ true);
2866 case Instruction::URem:
2867 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002868 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002869 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2870 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002871 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002872 case Instruction::Select:
2873 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002874 case Instruction::Ret:
2875 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002876 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002877 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002878 case Instruction::ZExt:
2879 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002880 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002881 case Instruction::Shl:
2882 return SelectShift(I, ARM_AM::lsl);
2883 case Instruction::LShr:
2884 return SelectShift(I, ARM_AM::lsr);
2885 case Instruction::AShr:
2886 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002887 default: break;
2888 }
2889 return false;
2890}
2891
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002892namespace {
2893// This table describes sign- and zero-extend instructions which can be
2894// folded into a preceding load. All of these extends have an immediate
2895// (sometimes a mask and sometimes a shift) that's applied after
2896// extension.
2897const struct FoldableLoadExtendsStruct {
2898 uint16_t Opc[2]; // ARM, Thumb.
2899 uint8_t ExpectedImm;
2900 uint8_t isZExt : 1;
2901 uint8_t ExpectedVT : 7;
2902} FoldableLoadExtends[] = {
2903 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2904 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2905 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2906 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2907 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2908};
2909}
2910
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002911/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002912/// vreg is being provided by the specified load instruction. If possible,
2913/// try to fold the load as an operand to the instruction, returning true if
2914/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002915bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2916 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002917 // Verify we have a legal type before going any further.
2918 MVT VT;
2919 if (!isLoadTypeLegal(LI->getType(), VT))
2920 return false;
2921
2922 // Combine load followed by zero- or sign-extend.
2923 // ldrb r1, [r0] ldrb r1, [r0]
2924 // uxtb r2, r1 =>
2925 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002926 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2927 return false;
2928 const uint64_t Imm = MI->getOperand(2).getImm();
2929
2930 bool Found = false;
2931 bool isZExt;
2932 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
2933 i != e; ++i) {
2934 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
2935 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
2936 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
2937 Found = true;
2938 isZExt = FoldableLoadExtends[i].isZExt;
2939 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002940 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002941 if (!Found) return false;
2942
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002943 // See if we can handle this address.
2944 Address Addr;
2945 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00002946
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002947 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00002948 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002949 return false;
2950 MI->eraseFromParent();
2951 return true;
2952}
2953
Jush Lu47172a02012-09-27 05:21:41 +00002954unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002955 unsigned Align, MVT VT) {
Jush Lu47172a02012-09-27 05:21:41 +00002956 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
2957 ARMConstantPoolConstant *CPV =
2958 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
2959 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
2960
2961 unsigned Opc;
2962 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
2963 // Load value.
2964 if (isThumb2) {
Jim Grosbach5f71aab2013-08-26 20:07:29 +00002965 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002966 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu47172a02012-09-27 05:21:41 +00002967 TII.get(ARM::t2LDRpci), DestReg1)
2968 .addConstantPoolIndex(Idx));
2969 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
2970 } else {
2971 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +00002972 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00002973 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002974 DbgLoc, TII.get(ARM::LDRcp), DestReg1)
Jush Lu47172a02012-09-27 05:21:41 +00002975 .addConstantPoolIndex(Idx).addImm(0));
2976 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
2977 }
2978
2979 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
2980 if (GlobalBaseReg == 0) {
2981 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
2982 AFI->setGlobalBaseReg(GlobalBaseReg);
2983 }
2984
2985 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
Jim Grosbach5f71aab2013-08-26 20:07:29 +00002986 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
2987 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
2988 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
Jush Lu47172a02012-09-27 05:21:41 +00002989 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002990 DbgLoc, TII.get(Opc), DestReg2)
Jush Lu47172a02012-09-27 05:21:41 +00002991 .addReg(DestReg1)
2992 .addReg(GlobalBaseReg);
2993 if (!UseGOTOFF)
2994 MIB.addImm(0);
2995 AddOptionalDefs(MIB);
2996
2997 return DestReg2;
2998}
2999
Evan Cheng615620c2013-02-11 01:27:15 +00003000bool ARMFastISel::FastLowerArguments() {
3001 if (!FuncInfo.CanLowerReturn)
3002 return false;
3003
3004 const Function *F = FuncInfo.Fn;
3005 if (F->isVarArg())
3006 return false;
3007
3008 CallingConv::ID CC = F->getCallingConv();
3009 switch (CC) {
3010 default:
3011 return false;
3012 case CallingConv::Fast:
3013 case CallingConv::C:
3014 case CallingConv::ARM_AAPCS_VFP:
3015 case CallingConv::ARM_AAPCS:
3016 case CallingConv::ARM_APCS:
3017 break;
3018 }
3019
3020 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3021 // which are passed in r0 - r3.
3022 unsigned Idx = 1;
3023 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3024 I != E; ++I, ++Idx) {
3025 if (Idx > 4)
3026 return false;
3027
3028 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3029 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3030 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3031 return false;
3032
3033 Type *ArgTy = I->getType();
3034 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3035 return false;
3036
3037 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003038 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003039 switch (ArgVT.getSimpleVT().SimpleTy) {
3040 case MVT::i8:
3041 case MVT::i16:
3042 case MVT::i32:
3043 break;
3044 default:
3045 return false;
3046 }
3047 }
3048
3049
3050 static const uint16_t GPRArgRegs[] = {
3051 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3052 };
3053
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003054 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003055 Idx = 0;
3056 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3057 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003058 unsigned SrcReg = GPRArgRegs[Idx];
3059 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3060 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3061 // Without this, EmitLiveInCopies may eliminate the livein if its only
3062 // use is a bitcast (which isn't turned into an instruction).
3063 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3065 TII.get(TargetOpcode::COPY),
Evan Cheng615620c2013-02-11 01:27:15 +00003066 ResultReg).addReg(DstReg, getKillRegState(true));
3067 UpdateValueMap(I, ResultReg);
3068 }
3069
3070 return true;
3071}
3072
Eric Christopher84bdfd82010-07-21 22:26:11 +00003073namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003074 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3075 const TargetLibraryInfo *libInfo) {
Eric Christopher5501b7e2010-10-11 20:05:22 +00003076 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach68147ee2010-11-09 19:22:26 +00003077
Eric Christopher5501b7e2010-10-11 20:05:22 +00003078 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastien18db1f22013-06-14 02:49:43 +00003079 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3080 bool UseFastISel = false;
Tim Northoverd6a729b2014-01-06 14:28:05 +00003081 UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
JF Bastien18db1f22013-06-14 02:49:43 +00003082 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3083 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3084
3085 if (UseFastISel) {
3086 // iOS always has a FP for backtracking, force other targets
3087 // to keep their FP when doing FastISel. The emitted code is
3088 // currently superior, and in cases like test-suite's lencod
3089 // FastISel isn't quite correct when FP is eliminated.
3090 TM.Options.NoFramePointerElim = true;
Bob Wilson3e6fa462012-08-03 04:06:28 +00003091 return new ARMFastISel(funcInfo, libInfo);
JF Bastien18db1f22013-06-14 02:49:43 +00003092 }
Craig Topper062a2ba2014-04-25 05:30:21 +00003093 return nullptr;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003094 }
3095}