blob: c17631b51199d918f39b36f039d2cfbd93c2bcd5 [file] [log] [blame]
Eric Christopher84bdfd82010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ARM-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// ARMGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Craig Toppera9253262014-03-22 23:51:00 +000017#include "ARMBaseRegisterInfo.h"
Eric Christopher72497e52010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopher83a5ec82010-10-01 23:24:42 +000019#include "ARMConstantPoolValue.h"
Craig Toppera9253262014-03-22 23:51:00 +000020#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
JF Bastien3c6bb8e2013-06-11 22:13:46 +000024#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/Analysis.h"
26#include "llvm/CodeGen/FastISel.h"
27#include "llvm/CodeGen/FunctionLoweringInfo.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineMemOperand.h"
32#include "llvm/CodeGen/MachineModuleInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000034#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000038#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/GlobalVariable.h"
40#include "llvm/IR/Instructions.h"
41#include "llvm/IR/IntrinsicInst.h"
42#include "llvm/IR/Module.h"
43#include "llvm/IR/Operator.h"
Eric Christopher663f4992010-08-17 00:46:57 +000044#include "llvm/Support/CommandLine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000045#include "llvm/Support/ErrorHandling.h"
Eric Christopher09f757d2010-08-17 01:25:29 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000049#include "llvm/Target/TargetOptions.h"
50using namespace llvm;
51
Eric Christopher347f4c32010-12-15 23:47:29 +000052extern cl::opt<bool> EnableARMLongCalls;
53
Eric Christopher84bdfd82010-07-21 22:26:11 +000054namespace {
Eric Christopher0a3c28b2010-11-20 22:38:27 +000055
Eric Christopherfef5f312010-11-19 22:30:02 +000056 // All possible address modes, plus some.
57 typedef struct Address {
58 enum {
59 RegBase,
60 FrameIndexBase
61 } BaseType;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000062
Eric Christopherfef5f312010-11-19 22:30:02 +000063 union {
64 unsigned Reg;
65 int FI;
66 } Base;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000067
Eric Christopherfef5f312010-11-19 22:30:02 +000068 int Offset;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000069
Eric Christopherfef5f312010-11-19 22:30:02 +000070 // Innocuous defaults for our address.
71 Address()
Jim Grosbach4e983162011-05-16 22:24:07 +000072 : BaseType(RegBase), Offset(0) {
Eric Christopherfef5f312010-11-19 22:30:02 +000073 Base.Reg = 0;
74 }
75 } Address;
Eric Christopher84bdfd82010-07-21 22:26:11 +000076
Craig Topper26696312014-03-18 07:27:13 +000077class ARMFastISel final : public FastISel {
Eric Christopher84bdfd82010-07-21 22:26:11 +000078
79 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
80 /// make the right decision when generating code for different targets.
81 const ARMSubtarget *Subtarget;
Bill Wendling6c1d9592013-12-30 05:17:29 +000082 Module &M;
Eric Christopher09f757d2010-08-17 01:25:29 +000083 const TargetMachine &TM;
84 const TargetInstrInfo &TII;
85 const TargetLowering &TLI;
Eric Christopher83a5ec82010-10-01 23:24:42 +000086 ARMFunctionInfo *AFI;
Eric Christopher84bdfd82010-07-21 22:26:11 +000087
Eric Christopherb024be32010-09-29 22:24:45 +000088 // Convenience variables to avoid some queries.
Chad Rosier0439cfc2011-11-08 21:12:00 +000089 bool isThumb2;
Eric Christopherb024be32010-09-29 22:24:45 +000090 LLVMContext *Context;
Eric Christopher6a0333c2010-09-02 01:39:14 +000091
Eric Christopher84bdfd82010-07-21 22:26:11 +000092 public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000093 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
94 const TargetLibraryInfo *libInfo)
95 : FastISel(funcInfo, libInfo),
Bill Wendling76cce192013-12-29 08:00:04 +000096 M(const_cast<Module&>(*funcInfo.Fn->getParent())),
Eric Christopher09f757d2010-08-17 01:25:29 +000097 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getInstrInfo()),
99 TLI(*TM.getTargetLowering()) {
Eric Christopher84bdfd82010-07-21 22:26:11 +0000100 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher8d03b8a2010-08-23 22:32:45 +0000101 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier0439cfc2011-11-08 21:12:00 +0000102 isThumb2 = AFI->isThumbFunction();
Eric Christopherb024be32010-09-29 22:24:45 +0000103 Context = &funcInfo.Fn->getContext();
Eric Christopher84bdfd82010-07-21 22:26:11 +0000104 }
105
Eric Christopherd8e8a292010-08-20 00:20:31 +0000106 // Code from FastISel.cpp.
Craig Topperfd1c9252012-08-18 21:38:45 +0000107 private:
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 Christopher72497e52010-09-10 23:18:12 +0000192 // Call handling routines.
193 private:
Jush Lue67e07b2012-07-19 09:49:00 +0000194 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
195 bool Return,
196 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000197 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000198 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000199 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000200 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
201 SmallVectorImpl<unsigned> &RegArgs,
202 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000203 unsigned &NumBytes,
204 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000205 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000206 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000207 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000208 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000209 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000210
211 // OptionalDef handling routines.
212 private:
Eric Christopher174d8722011-03-12 01:09:29 +0000213 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000214 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
215 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000216 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000217 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000218 unsigned Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000219};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000220
221} // end anonymous namespace
222
Eric Christopher72497e52010-09-10 23:18:12 +0000223#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000224
Eric Christopher0d274a02010-08-19 00:37:05 +0000225// DefinesOptionalPredicate - This is different from DefinesPredicate in that
226// we don't care about implicit defs here, just places we'll need to add a
227// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
228bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000229 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000230 return false;
231
232 // Look to see if our OptionalDef is defining CPSR or CCR.
233 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
234 const MachineOperand &MO = MI->getOperand(i);
Eric Christopher985d9e42010-08-20 00:36:24 +0000235 if (!MO.isReg() || !MO.isDef()) continue;
236 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000237 *CPSR = true;
238 }
239 return true;
240}
241
Eric Christopher174d8722011-03-12 01:09:29 +0000242bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000243 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000244
Joey Goulya5153cb2013-09-09 14:21:49 +0000245 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000246 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000247 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000248 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000249
Evan Cheng6cc775f2011-06-28 19:10:37 +0000250 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
251 if (MCID.OpInfo[i].isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000252 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000253
Eric Christopher174d8722011-03-12 01:09:29 +0000254 return false;
255}
256
Eric Christopher0d274a02010-08-19 00:37:05 +0000257// If the machine is predicable go ahead and add the predicate operands, if
258// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000259// TODO: If we want to support thumb1 then we'll need to deal with optional
260// CPSR defs that need to be added before the remaining operands. See s_cc_out
261// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000262const MachineInstrBuilder &
263ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
264 MachineInstr *MI = &*MIB;
265
Eric Christopher174d8722011-03-12 01:09:29 +0000266 // Do we use a predicate? or...
267 // Are we NEON in ARM mode and have a predicate operand? If so, I know
268 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000269 if (isARMNEONPred(MI))
Eric Christopher0d274a02010-08-19 00:37:05 +0000270 AddDefaultPred(MIB);
Eric Christopher501d2e22011-04-29 00:03:10 +0000271
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000272 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000273 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000274 bool CPSR = false;
Eric Christopher0d274a02010-08-19 00:37:05 +0000275 if (DefinesOptionalPredicate(MI, &CPSR)) {
276 if (CPSR)
277 AddDefaultT1CC(MIB);
278 else
279 AddDefaultCC(MIB);
280 }
281 return MIB;
282}
283
Eric Christopher09f757d2010-08-17 01:25:29 +0000284unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
285 const TargetRegisterClass *RC,
286 unsigned Op0, bool Op0IsKill) {
287 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000288 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000289
Jim Grosbach06c2a682013-08-16 23:37:31 +0000290 // Make sure the input operand is sufficiently constrained to be legal
291 // for this instruction.
292 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000293 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
295 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000296 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000298 .addReg(Op0, Op0IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000299 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000300 TII.get(TargetOpcode::COPY), ResultReg)
301 .addReg(II.ImplicitDefs[0]));
302 }
303 return ResultReg;
304}
305
306unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
307 const TargetRegisterClass *RC,
308 unsigned Op0, bool Op0IsKill,
309 unsigned Op1, bool Op1IsKill) {
310 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000311 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000312
Jim Grosbach06c2a682013-08-16 23:37:31 +0000313 // Make sure the input operands are sufficiently constrained to be legal
314 // for this instruction.
315 Op0 = constrainOperandRegClass(II, Op0, 1);
316 Op1 = constrainOperandRegClass(II, Op1, 2);
317
Chad Rosier0bc51322012-02-15 17:36:21 +0000318 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000319 AddOptionalDefs(
320 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
321 .addReg(Op0, Op0IsKill * RegState::Kill)
322 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000323 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000328 TII.get(TargetOpcode::COPY), ResultReg)
329 .addReg(II.ImplicitDefs[0]));
330 }
331 return ResultReg;
332}
333
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000334unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
335 const TargetRegisterClass *RC,
336 unsigned Op0, bool Op0IsKill,
337 unsigned Op1, bool Op1IsKill,
338 unsigned Op2, bool Op2IsKill) {
339 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000340 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000341
Jim Grosbach06c2a682013-08-16 23:37:31 +0000342 // Make sure the input operands are sufficiently constrained to be legal
343 // for this instruction.
344 Op0 = constrainOperandRegClass(II, Op0, 1);
345 Op1 = constrainOperandRegClass(II, Op1, 2);
346 Op2 = constrainOperandRegClass(II, Op1, 3);
347
Chad Rosier0bc51322012-02-15 17:36:21 +0000348 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000349 AddOptionalDefs(
350 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
351 .addReg(Op0, Op0IsKill * RegState::Kill)
352 .addReg(Op1, Op1IsKill * RegState::Kill)
353 .addReg(Op2, Op2IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000354 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000356 .addReg(Op0, Op0IsKill * RegState::Kill)
357 .addReg(Op1, Op1IsKill * RegState::Kill)
358 .addReg(Op2, Op2IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Cameron Zwarich53dd03d2011-03-30 23:01:21 +0000360 TII.get(TargetOpcode::COPY), ResultReg)
361 .addReg(II.ImplicitDefs[0]));
362 }
363 return ResultReg;
364}
365
Eric Christopher09f757d2010-08-17 01:25:29 +0000366unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
367 const TargetRegisterClass *RC,
368 unsigned Op0, bool Op0IsKill,
369 uint64_t Imm) {
370 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000371 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000372
Jim Grosbach06c2a682013-08-16 23:37:31 +0000373 // Make sure the input operand is sufficiently constrained to be legal
374 // for this instruction.
375 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000376 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000377 AddOptionalDefs(
378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
379 .addReg(Op0, Op0IsKill * RegState::Kill)
380 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000381 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000383 .addReg(Op0, Op0IsKill * RegState::Kill)
384 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000385 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000386 TII.get(TargetOpcode::COPY), ResultReg)
387 .addReg(II.ImplicitDefs[0]));
388 }
389 return ResultReg;
390}
391
Eric Christopher09f757d2010-08-17 01:25:29 +0000392unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
393 const TargetRegisterClass *RC,
394 unsigned Op0, bool Op0IsKill,
395 unsigned Op1, bool Op1IsKill,
396 uint64_t Imm) {
397 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000398 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000399
Jim Grosbach06c2a682013-08-16 23:37:31 +0000400 // Make sure the input operands are sufficiently constrained to be legal
401 // for this instruction.
402 Op0 = constrainOperandRegClass(II, Op0, 1);
403 Op1 = constrainOperandRegClass(II, Op1, 2);
Chad Rosier0bc51322012-02-15 17:36:21 +0000404 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000405 AddOptionalDefs(
406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
407 .addReg(Op0, Op0IsKill * RegState::Kill)
408 .addReg(Op1, Op1IsKill * RegState::Kill)
409 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000410 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000411 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000412 .addReg(Op0, Op0IsKill * RegState::Kill)
413 .addReg(Op1, Op1IsKill * RegState::Kill)
414 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000416 TII.get(TargetOpcode::COPY), ResultReg)
417 .addReg(II.ImplicitDefs[0]));
418 }
419 return ResultReg;
420}
421
422unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
423 const TargetRegisterClass *RC,
424 uint64_t Imm) {
425 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000426 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000427
Chad Rosier0bc51322012-02-15 17:36:21 +0000428 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000429 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
430 ResultReg).addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000431 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000433 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000434 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000435 TII.get(TargetOpcode::COPY), ResultReg)
436 .addReg(II.ImplicitDefs[0]));
437 }
438 return ResultReg;
439}
440
Eric Christopher860fc932010-09-10 00:34:35 +0000441// TODO: Don't worry about 64-bit now, but when this is fixed remove the
442// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000443unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000444 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000445
Eric Christopher4bd70472010-09-09 21:44:45 +0000446 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000447 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000448 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000449 .addReg(SrcReg));
450 return MoveReg;
451}
452
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000453unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000454 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000455
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000456 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000457 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000458 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000459 .addReg(SrcReg));
460 return MoveReg;
461}
462
Eric Christopher3cf63f12010-09-09 00:19:41 +0000463// For double width floating point we need to materialize two constants
464// (the high and the low) into integer registers then use a move to get
465// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000466unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000467 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000468 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000469
Eric Christopher3cf63f12010-09-09 00:19:41 +0000470 // This checks to see if we can use VFP3 instructions to materialize
471 // a constant, otherwise we have to go through the constant pool.
472 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000473 int Imm;
474 unsigned Opc;
475 if (is64bit) {
476 Imm = ARM_AM::getFP64Imm(Val);
477 Opc = ARM::FCONSTD;
478 } else {
479 Imm = ARM_AM::getFP32Imm(Val);
480 Opc = ARM::FCONSTS;
481 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000482 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000483 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
484 TII.get(Opc), DestReg).addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000485 return DestReg;
486 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000487
Eric Christopher860fc932010-09-10 00:34:35 +0000488 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000489 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000490
Eric Christopher22fd29a2010-09-09 23:50:00 +0000491 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000492 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000493 if (Align == 0) {
494 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000495 Align = DL.getTypeAllocSize(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000496 }
497 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
498 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
499 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000500
Eric Christopher860fc932010-09-10 00:34:35 +0000501 // The extra reg is for addrmode5.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000502 AddOptionalDefs(
503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
504 .addConstantPoolIndex(Idx)
505 .addReg(0));
Eric Christopher22fd29a2010-09-09 23:50:00 +0000506 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000507}
508
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000509unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopher7ac602b2010-10-11 08:38:55 +0000510
Chad Rosier67f96882011-11-04 22:29:00 +0000511 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
512 return false;
Eric Christophere4dd7372010-11-03 20:21:17 +0000513
514 // If we can do this in a single instruction without a constant pool entry
515 // do so now.
516 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000517 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000518 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000519 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
520 &ARM::GPRRegClass;
521 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000522 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier67f96882011-11-04 22:29:00 +0000523 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000524 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000525 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000526 }
527
Chad Rosier2a3503e2011-11-11 00:36:21 +0000528 // Use MVN to emit negative constants.
529 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
530 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000531 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000532 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000533 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000534 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
535 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000536 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier2a3503e2011-11-11 00:36:21 +0000537 TII.get(Opc), ImmReg)
538 .addImm(Imm));
539 return ImmReg;
540 }
541 }
542
543 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000544 if (VT != MVT::i32)
545 return false;
546
547 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
548
Eric Christopherc3e118e2010-09-02 23:43:26 +0000549 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000550 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000551 if (Align == 0) {
552 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000553 Align = DL.getTypeAllocSize(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000554 }
555 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000556
Chad Rosier0439cfc2011-11-08 21:12:00 +0000557 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000558 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000559 TII.get(ARM::t2LDRpci), DestReg)
560 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000561 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000562 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000563 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000564 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher953b1af2010-09-28 21:55:34 +0000565 TII.get(ARM::LDRcp), DestReg)
566 .addConstantPoolIndex(Idx)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000567 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000568 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000569
Eric Christopherc3e118e2010-09-02 23:43:26 +0000570 return DestReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000571}
572
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000573unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000574 // For now 32-bit only.
Duncan Sands14627772010-11-03 12:17:33 +0000575 if (VT != MVT::i32) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000576
Eric Christopher7787f792010-10-02 00:32:44 +0000577 Reloc::Model RelocM = TM.getRelocationModel();
Jush Lue87e5592012-08-29 02:41:21 +0000578 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
Chad Rosier65710a72012-11-07 00:13:01 +0000579 const TargetRegisterClass *RC = isThumb2 ?
580 (const TargetRegisterClass*)&ARM::rGPRRegClass :
581 (const TargetRegisterClass*)&ARM::GPRRegClass;
582 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000583
Tim Northoverd6a729b2014-01-06 14:28:05 +0000584 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000585 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
586 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000587 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000588
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000589 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000590 // Non-darwin targets only support static movt relocations in FastISel.
Jakob Stoklund Olesen083dbdc2012-01-07 20:49:15 +0000591 if (Subtarget->useMovt() &&
Tim Northoverd6a729b2014-01-06 14:28:05 +0000592 (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000593 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000594 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000595 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000596 TF = ARMII::MO_NONLAZY;
597
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000598 switch (RelocM) {
599 case Reloc::PIC_:
600 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
601 break;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000602 default:
603 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
604 break;
605 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000606 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
607 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000608 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000609 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000610 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000611 if (Align == 0) {
612 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000613 Align = DL.getTypeAllocSize(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000614 }
615
Jush Lu47172a02012-09-27 05:21:41 +0000616 if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
617 return ARMLowerPICELF(GV, Align, VT);
618
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000619 // Grab index.
620 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
621 (Subtarget->isThumb() ? 4 : 8);
622 unsigned Id = AFI->createPICLabelUId();
623 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
624 ARMCP::CPValue,
625 PCAdj);
626 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
627
628 // Load value.
629 MachineInstrBuilder MIB;
630 if (isThumb2) {
631 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000632 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
633 DestReg).addConstantPoolIndex(Idx);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000634 if (RelocM == Reloc::PIC_)
635 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000636 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000637 } else {
638 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000639 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000640 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
641 TII.get(ARM::LDRcp), DestReg)
642 .addConstantPoolIndex(Idx)
643 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000644 AddOptionalDefs(MIB);
645
646 if (RelocM == Reloc::PIC_) {
647 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
648 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
649
650 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000651 DbgLoc, TII.get(Opc), NewDestReg)
Jush Lue87e5592012-08-29 02:41:21 +0000652 .addReg(DestReg)
653 .addImm(Id);
654 AddOptionalDefs(MIB);
655 return NewDestReg;
656 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000657 }
Eric Christopher7787f792010-10-02 00:32:44 +0000658 }
Eli Friedman86585792011-06-03 01:13:19 +0000659
Jush Lue87e5592012-08-29 02:41:21 +0000660 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000661 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000662 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000663 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000664 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000665 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000666 .addReg(DestReg)
667 .addImm(0);
668 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000669 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
670 TII.get(ARM::LDRi12), NewDestReg)
671 .addReg(DestReg)
672 .addImm(0);
Eli Friedman86585792011-06-03 01:13:19 +0000673 DestReg = NewDestReg;
674 AddOptionalDefs(MIB);
675 }
676
Eric Christopher7787f792010-10-02 00:32:44 +0000677 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000678}
679
Eric Christopher3cf63f12010-09-09 00:19:41 +0000680unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
Patrik Hagglundc494d242012-12-17 14:30:06 +0000681 EVT CEVT = TLI.getValueType(C->getType(), true);
682
683 // Only handle simple types.
684 if (!CEVT.isSimple()) return 0;
685 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000686
687 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
688 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000689 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
690 return ARMMaterializeGV(GV, VT);
691 else if (isa<ConstantInt>(C))
692 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000693
Eric Christopher83a5ec82010-10-01 23:24:42 +0000694 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000695}
696
Chad Rosier0eff3e52011-11-17 21:46:13 +0000697// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
698
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000699unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
700 // Don't handle dynamic allocas.
701 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000702
Duncan Sandsf5dda012010-11-03 11:35:31 +0000703 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000704 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000705
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000706 DenseMap<const AllocaInst*, int>::iterator SI =
707 FuncInfo.StaticAllocaMap.find(AI);
708
709 // This will get lowered later into the correct offsets and registers
710 // via rewriteXFrameIndex.
711 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000712 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000713 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000714 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000715 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
716
Rafael Espindolaea09c592014-02-18 22:05:46 +0000717 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000718 TII.get(Opc), ResultReg)
719 .addFrameIndex(SI->second)
720 .addImm(0));
721 return ResultReg;
722 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000723
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000724 return 0;
725}
726
Chris Lattner229907c2011-07-18 04:54:35 +0000727bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000728 EVT evt = TLI.getValueType(Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000729
Eric Christopher761e7fb2010-08-25 07:23:49 +0000730 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000731 if (evt == MVT::Other || !evt.isSimple()) return false;
732 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000733
Eric Christopher901176a2010-08-31 01:28:42 +0000734 // Handle all legal types, i.e. a register that will directly hold this
735 // value.
736 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000737}
738
Chris Lattner229907c2011-07-18 04:54:35 +0000739bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000740 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000741
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000742 // If this is a type than can be sign or zero-extended to a basic operation
743 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000744 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000745 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000746
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000747 return false;
748}
749
Eric Christopher558b61e2010-11-19 22:36:41 +0000750// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000751bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000752 // Some boilerplate from the X86 FastISel.
Craig Topper062a2ba2014-04-25 05:30:21 +0000753 const User *U = nullptr;
Eric Christopher00202ee2010-08-23 21:44:12 +0000754 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000755 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000756 // Don't walk into other basic blocks unless the object is an alloca from
757 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000758 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
759 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
760 Opcode = I->getOpcode();
761 U = I;
762 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000763 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000764 Opcode = C->getOpcode();
765 U = C;
766 }
767
Chris Lattner229907c2011-07-18 04:54:35 +0000768 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000769 if (Ty->getAddressSpace() > 255)
770 // Fast instruction selection doesn't support the special
771 // address spaces.
772 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000773
Eric Christopher00202ee2010-08-23 21:44:12 +0000774 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000775 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000776 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000777 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000778 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000779 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000780 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000781 // Look past no-op inttoptrs.
782 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000783 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000784 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000785 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000786 // Look past no-op ptrtoints.
787 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopherfef5f312010-11-19 22:30:02 +0000788 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000789 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000790 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000791 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000792 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000793
Eric Christopher21d0c172010-10-14 09:29:41 +0000794 // Iterate through the GEP folding the constants into offsets where
795 // we can.
796 gep_type_iterator GTI = gep_type_begin(U);
797 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
798 i != e; ++i, ++GTI) {
799 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000800 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000801 const StructLayout *SL = DL.getStructLayout(STy);
Eric Christopher21d0c172010-10-14 09:29:41 +0000802 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
803 TmpOffset += SL->getElementOffset(Idx);
804 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000805 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000806 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000807 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
808 // Constant-offset addressing.
809 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000810 break;
811 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000812 if (canFoldAddIntoGEP(U, Op)) {
813 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000814 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000815 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000816 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000817 // Iterate on the other operand.
818 Op = cast<AddOperator>(Op)->getOperand(0);
819 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000820 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000821 // Unsupported
822 goto unsupported_gep;
823 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000824 }
825 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000826
827 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000828 Addr.Offset = TmpOffset;
829 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000830
831 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000832 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000833
Eric Christopher21d0c172010-10-14 09:29:41 +0000834 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000835 break;
836 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000837 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000838 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000839 DenseMap<const AllocaInst*, int>::iterator SI =
840 FuncInfo.StaticAllocaMap.find(AI);
841 if (SI != FuncInfo.StaticAllocaMap.end()) {
842 Addr.BaseType = Address::FrameIndexBase;
843 Addr.Base.FI = SI->second;
844 return true;
845 }
846 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000847 }
848 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000849
Eric Christopher9d4e4712010-08-24 00:07:24 +0000850 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000851 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
852 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000853}
854
Chad Rosier150d35b2012-12-17 22:35:29 +0000855void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000856 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000857 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000858 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000859 case MVT::i1:
860 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000861 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000862 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000863 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000864 // Integer loads/stores handle 12-bit offsets.
865 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000866 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000867 if (needsLowering && isThumb2)
868 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
869 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000870 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000871 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000872 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000873 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000874 break;
875 case MVT::f32:
876 case MVT::f64:
877 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000878 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000879 break;
880 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000881
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000882 // If this is a stack pointer and the offset needs to be simplified then
883 // put the alloca address into a register, set the base type back to
884 // register and continue. This should almost never happen.
885 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topperc7242e02012-04-20 07:30:17 +0000886 const TargetRegisterClass *RC = isThumb2 ?
887 (const TargetRegisterClass*)&ARM::tGPRRegClass :
888 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000889 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000890 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000891 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000892 TII.get(Opc), ResultReg)
893 .addFrameIndex(Addr.Base.FI)
894 .addImm(0));
895 Addr.Base.Reg = ResultReg;
896 Addr.BaseType = Address::RegBase;
897 }
898
Eric Christopher73bc5b02010-10-21 19:40:30 +0000899 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +0000900 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +0000901 if (needsLowering) {
Eli Friedman86caced2011-04-29 21:22:56 +0000902 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
903 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +0000904 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +0000905 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000906}
907
Chad Rosier150d35b2012-12-17 22:35:29 +0000908void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000909 const MachineInstrBuilder &MIB,
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000910 unsigned Flags, bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000911 // addrmode5 output depends on the selection dag addressing dividing the
912 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +0000913 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +0000914 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +0000915
Eric Christopher119ff7f2010-12-01 01:40:24 +0000916 // Frame base works a bit differently. Handle it separately.
917 if (Addr.BaseType == Address::FrameIndexBase) {
918 int FI = Addr.Base.FI;
919 int Offset = Addr.Offset;
920 MachineMemOperand *MMO =
921 FuncInfo.MF->getMachineMemOperand(
922 MachinePointerInfo::getFixedStack(FI, Offset),
Cameron Zwarich6528a542011-05-28 20:34:49 +0000923 Flags,
Eric Christopher119ff7f2010-12-01 01:40:24 +0000924 MFI.getObjectSize(FI),
925 MFI.getObjectAlignment(FI));
926 // Now add the rest of the operands.
927 MIB.addFrameIndex(FI);
928
Bob Wilson80381f62011-12-04 00:52:23 +0000929 // ARM halfword load/stores and signed byte loads need an additional
930 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000931 if (useAM3) {
932 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
933 MIB.addReg(0);
934 MIB.addImm(Imm);
935 } else {
936 MIB.addImm(Addr.Offset);
937 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000938 MIB.addMemOperand(MMO);
939 } else {
940 // Now add the rest of the operands.
941 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +0000942
Bob Wilson80381f62011-12-04 00:52:23 +0000943 // ARM halfword load/stores and signed byte loads need an additional
944 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000945 if (useAM3) {
946 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
947 MIB.addReg(0);
948 MIB.addImm(Imm);
949 } else {
950 MIB.addImm(Addr.Offset);
951 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000952 }
953 AddOptionalDefs(MIB);
954}
955
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000956bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +0000957 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +0000958 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000959 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +0000960 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +0000961 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000962 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000963 // This is mostly going to be Neon/vector support.
964 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +0000965 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000966 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +0000967 if (isThumb2) {
968 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
969 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
970 else
971 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000972 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +0000973 if (isZExt) {
974 Opc = ARM::LDRBi12;
975 } else {
976 Opc = ARM::LDRSB;
977 useAM3 = true;
978 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000979 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000980 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000981 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000982 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +0000983 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +0000984 return false;
985
Chad Rosieradfd2002011-11-14 20:22:27 +0000986 if (isThumb2) {
987 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
988 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
989 else
990 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
991 } else {
992 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
993 useAM3 = true;
994 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000995 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000996 break;
Eric Christopher901176a2010-08-31 01:28:42 +0000997 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +0000998 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +0000999 return false;
1000
Chad Rosieradfd2002011-11-14 20:22:27 +00001001 if (isThumb2) {
1002 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1003 Opc = ARM::t2LDRi8;
1004 else
1005 Opc = ARM::t2LDRi12;
1006 } else {
1007 Opc = ARM::LDRi12;
1008 }
JF Bastien652fa6a2013-06-09 00:20:24 +00001009 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +00001010 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +00001011 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +00001012 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +00001013 // Unaligned loads need special handling. Floats require word-alignment.
1014 if (Alignment && Alignment < 4) {
1015 needVMOV = true;
1016 VT = MVT::i32;
1017 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +00001018 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +00001019 } else {
1020 Opc = ARM::VLDRS;
1021 RC = TLI.getRegClassFor(VT);
1022 }
Eric Christopheraef6499b2010-09-18 01:59:37 +00001023 break;
1024 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +00001025 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001026 // FIXME: Unaligned loads need special handling. Doublewords require
1027 // word-alignment.
1028 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +00001029 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001030
Eric Christopheraef6499b2010-09-18 01:59:37 +00001031 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +00001032 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +00001033 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001034 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001035 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001036 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001037
Eric Christopher119ff7f2010-12-01 01:40:24 +00001038 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001039 if (allocReg)
1040 ResultReg = createResultReg(RC);
1041 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Rafael Espindolaea09c592014-02-18 22:05:46 +00001042 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001043 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001044 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001045
1046 // If we had an unaligned load of a float we've converted it to an regular
1047 // load. Now we must move from the GRP to the FP register.
1048 if (needVMOV) {
1049 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001050 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier563de602011-12-13 19:22:14 +00001051 TII.get(ARM::VMOVSR), MoveReg)
1052 .addReg(ResultReg));
1053 ResultReg = MoveReg;
1054 }
Eric Christopher901176a2010-08-31 01:28:42 +00001055 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001056}
1057
Eric Christopher29ab6d12010-09-27 06:02:23 +00001058bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001059 // Atomic loads need special handling.
1060 if (cast<LoadInst>(I)->isAtomic())
1061 return false;
1062
Eric Christopher860fc932010-09-10 00:34:35 +00001063 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001064 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001065 if (!isLoadTypeLegal(I->getType(), VT))
1066 return false;
1067
Eric Christopher119ff7f2010-12-01 01:40:24 +00001068 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001069 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001070 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001071
1072 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001073 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1074 return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001075 UpdateValueMap(I, ResultReg);
1076 return true;
1077}
1078
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001079bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001080 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001081 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001082 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001083 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001084 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001085 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001086 case MVT::i1: {
Craig Topperc7242e02012-04-20 07:30:17 +00001087 unsigned Res = createResultReg(isThumb2 ?
1088 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1089 (const TargetRegisterClass*)&ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001090 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001091 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001092 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher1e43892e2010-11-02 23:59:09 +00001093 TII.get(Opc), Res)
1094 .addReg(SrcReg).addImm(1));
1095 SrcReg = Res;
1096 } // Fallthrough here.
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001097 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001098 if (isThumb2) {
1099 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1100 StrOpc = ARM::t2STRBi8;
1101 else
1102 StrOpc = ARM::t2STRBi12;
1103 } else {
1104 StrOpc = ARM::STRBi12;
1105 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001106 break;
1107 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001108 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001109 return false;
1110
Chad Rosieradfd2002011-11-14 20:22:27 +00001111 if (isThumb2) {
1112 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1113 StrOpc = ARM::t2STRHi8;
1114 else
1115 StrOpc = ARM::t2STRHi12;
1116 } else {
1117 StrOpc = ARM::STRH;
1118 useAM3 = true;
1119 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001120 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001121 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001122 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001123 return false;
1124
Chad Rosieradfd2002011-11-14 20:22:27 +00001125 if (isThumb2) {
1126 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1127 StrOpc = ARM::t2STRi8;
1128 else
1129 StrOpc = ARM::t2STRi12;
1130 } else {
1131 StrOpc = ARM::STRi12;
1132 }
Eric Christopherc918d552010-10-16 01:10:35 +00001133 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001134 case MVT::f32:
1135 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001136 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001137 if (Alignment && Alignment < 4) {
1138 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001139 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosierec3b77e2011-12-03 02:21:57 +00001140 TII.get(ARM::VMOVRS), MoveReg)
1141 .addReg(SrcReg));
1142 SrcReg = MoveReg;
1143 VT = MVT::i32;
1144 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001145 } else {
1146 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001147 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001148 break;
1149 case MVT::f64:
1150 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001151 // FIXME: Unaligned stores need special handling. Doublewords require
1152 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001153 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001154 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001155
Eric Christopherc3e118e2010-09-02 23:43:26 +00001156 StrOpc = ARM::VSTRD;
1157 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001158 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001159 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001160 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001161
Eric Christopher119ff7f2010-12-01 01:40:24 +00001162 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001163 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001164 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001165 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001166 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001167 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001168 return true;
1169}
1170
Eric Christopher29ab6d12010-09-27 06:02:23 +00001171bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001172 Value *Op0 = I->getOperand(0);
1173 unsigned SrcReg = 0;
1174
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001175 // Atomic stores need special handling.
1176 if (cast<StoreInst>(I)->isAtomic())
1177 return false;
1178
Eric Christopher119ff7f2010-12-01 01:40:24 +00001179 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001180 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001181 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001182 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001183
Eric Christopher92db2012010-09-02 01:48:11 +00001184 // Get the value to be stored into a register.
1185 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001186 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001187
Eric Christopher119ff7f2010-12-01 01:40:24 +00001188 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001189 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001190 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001191 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001192
Chad Rosierec3b77e2011-12-03 02:21:57 +00001193 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1194 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001195 return true;
1196}
1197
1198static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1199 switch (Pred) {
1200 // Needs two compares...
1201 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001202 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001203 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001204 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001205 return ARMCC::AL;
1206 case CmpInst::ICMP_EQ:
1207 case CmpInst::FCMP_OEQ:
1208 return ARMCC::EQ;
1209 case CmpInst::ICMP_SGT:
1210 case CmpInst::FCMP_OGT:
1211 return ARMCC::GT;
1212 case CmpInst::ICMP_SGE:
1213 case CmpInst::FCMP_OGE:
1214 return ARMCC::GE;
1215 case CmpInst::ICMP_UGT:
1216 case CmpInst::FCMP_UGT:
1217 return ARMCC::HI;
1218 case CmpInst::FCMP_OLT:
1219 return ARMCC::MI;
1220 case CmpInst::ICMP_ULE:
1221 case CmpInst::FCMP_OLE:
1222 return ARMCC::LS;
1223 case CmpInst::FCMP_ORD:
1224 return ARMCC::VC;
1225 case CmpInst::FCMP_UNO:
1226 return ARMCC::VS;
1227 case CmpInst::FCMP_UGE:
1228 return ARMCC::PL;
1229 case CmpInst::ICMP_SLT:
1230 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001231 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001232 case CmpInst::ICMP_SLE:
1233 case CmpInst::FCMP_ULE:
1234 return ARMCC::LE;
1235 case CmpInst::FCMP_UNE:
1236 case CmpInst::ICMP_NE:
1237 return ARMCC::NE;
1238 case CmpInst::ICMP_UGE:
1239 return ARMCC::HS;
1240 case CmpInst::ICMP_ULT:
1241 return ARMCC::LO;
1242 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001243}
1244
Eric Christopher29ab6d12010-09-27 06:02:23 +00001245bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001246 const BranchInst *BI = cast<BranchInst>(I);
1247 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1248 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001249
Eric Christopher6aaed722010-09-03 00:35:47 +00001250 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001251
Eric Christopher5c308f82010-10-29 21:08:19 +00001252 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1253 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001254 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001255 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001256
1257 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001258 // Try to take advantage of fallthrough opportunities.
1259 CmpInst::Predicate Predicate = CI->getPredicate();
1260 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1261 std::swap(TBB, FBB);
1262 Predicate = CmpInst::getInversePredicate(Predicate);
1263 }
1264
1265 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001266
1267 // We may not handle every CC for now.
1268 if (ARMPred == ARMCC::AL) return false;
1269
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001270 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001271 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001272 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001273
Chad Rosier0439cfc2011-11-08 21:12:00 +00001274 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher5c308f82010-10-29 21:08:19 +00001276 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001277 FastEmitBranch(FBB, DbgLoc);
Eric Christopher5c308f82010-10-29 21:08:19 +00001278 FuncInfo.MBB->addSuccessor(TBB);
1279 return true;
1280 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001281 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1282 MVT SourceVT;
1283 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001284 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001285 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001286 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001287 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8d46b472011-04-29 20:02:39 +00001289 TII.get(TstOpc))
1290 .addReg(OpReg).addImm(1));
1291
1292 unsigned CCMode = ARMCC::NE;
1293 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1294 std::swap(TBB, FBB);
1295 CCMode = ARMCC::EQ;
1296 }
1297
Chad Rosier0439cfc2011-11-08 21:12:00 +00001298 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher8d46b472011-04-29 20:02:39 +00001300 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1301
Rafael Espindolaea09c592014-02-18 22:05:46 +00001302 FastEmitBranch(FBB, DbgLoc);
Eric Christopher8d46b472011-04-29 20:02:39 +00001303 FuncInfo.MBB->addSuccessor(TBB);
1304 return true;
1305 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001306 } else if (const ConstantInt *CI =
1307 dyn_cast<ConstantInt>(BI->getCondition())) {
1308 uint64_t Imm = CI->getZExtValue();
1309 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001310 FastEmitBranch(Target, DbgLoc);
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001311 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001312 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001313
Eric Christopher5c308f82010-10-29 21:08:19 +00001314 unsigned CmpReg = getRegForValue(BI->getCondition());
1315 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001316
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001317 // We've been divorced from our compare! Our block was split, and
1318 // now our compare lives in a predecessor block. We musn't
1319 // re-compare here, as the children of the compare aren't guaranteed
1320 // live across the block boundary (we *could* check for this).
1321 // Regardless, the compare has been done in the predecessor block,
1322 // and it left a value for us in a virtual register. Ergo, we test
1323 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001324 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001325 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001326 AddOptionalDefs(
1327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1328 .addReg(CmpReg)
1329 .addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001330
Eric Christopher4f012fd2011-04-28 16:52:09 +00001331 unsigned CCMode = ARMCC::NE;
1332 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1333 std::swap(TBB, FBB);
1334 CCMode = ARMCC::EQ;
1335 }
1336
Chad Rosier0439cfc2011-11-08 21:12:00 +00001337 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001338 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001339 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001340 FastEmitBranch(FBB, DbgLoc);
Eric Christopher6aaed722010-09-03 00:35:47 +00001341 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001342 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001343}
1344
Chad Rosierded4c992012-02-07 23:56:08 +00001345bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1346 unsigned AddrReg = getRegForValue(I->getOperand(0));
1347 if (AddrReg == 0) return false;
1348
1349 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001350 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1351 TII.get(Opc)).addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001352
1353 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1354 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1355 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1356
Jush Luac96b762012-06-14 06:08:19 +00001357 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001358}
1359
Chad Rosier9cf803c2011-11-02 18:08:25 +00001360bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1361 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001362 Type *Ty = Src1Value->getType();
Patrik Hagglundc494d242012-12-17 14:30:06 +00001363 EVT SrcEVT = TLI.getValueType(Ty, true);
1364 if (!SrcEVT.isSimple()) return false;
1365 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001366
Chad Rosier78127d32011-10-26 23:25:44 +00001367 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1368 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001369 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001370
Chad Rosier595d4192011-11-09 03:22:02 +00001371 // Check to see if the 2nd operand is a constant that we can encode directly
1372 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001373 int Imm = 0;
1374 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001375 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001376 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1377 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001378 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1379 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1380 SrcVT == MVT::i1) {
1381 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001382 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001383 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
Jim Grosbach1a597112014-04-03 23:43:18 +00001384 // then a cmn, because there is no way to represent 2147483648 as a
Chad Rosier26d05882012-03-15 22:54:20 +00001385 // signed 32-bit int.
1386 if (Imm < 0 && Imm != (int)0x80000000) {
1387 isNegativeImm = true;
1388 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001389 }
Chad Rosier26d05882012-03-15 22:54:20 +00001390 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1391 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001392 }
1393 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1394 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1395 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001396 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001397 }
1398
Eric Christopherc3e9c402010-09-08 23:13:45 +00001399 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001400 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001401 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001402 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001403 default: return false;
1404 // TODO: Verify compares.
1405 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001406 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001407 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001408 break;
1409 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001410 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001411 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001412 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001413 case MVT::i1:
1414 case MVT::i8:
1415 case MVT::i16:
1416 needsExt = true;
1417 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001418 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001419 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001420 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001421 CmpOpc = ARM::t2CMPrr;
1422 else
Bill Wendling4b796472012-06-11 08:07:26 +00001423 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001424 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001425 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001426 CmpOpc = ARM::CMPrr;
1427 else
Bill Wendling4b796472012-06-11 08:07:26 +00001428 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001429 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001430 break;
1431 }
1432
Chad Rosier9cf803c2011-11-02 18:08:25 +00001433 unsigned SrcReg1 = getRegForValue(Src1Value);
1434 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001435
Duncan Sands12330652011-11-28 10:31:27 +00001436 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001437 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001438 SrcReg2 = getRegForValue(Src2Value);
1439 if (SrcReg2 == 0) return false;
1440 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001441
1442 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1443 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001444 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1445 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001446 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001447 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1448 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001449 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001450 }
Chad Rosier59a20192011-10-26 22:47:55 +00001451
Jim Grosbachd7866792013-08-16 23:37:40 +00001452 const MCInstrDesc &II = TII.get(CmpOpc);
1453 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001454 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001455 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001456 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001457 .addReg(SrcReg1).addReg(SrcReg2));
1458 } else {
1459 MachineInstrBuilder MIB;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001460 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001461 .addReg(SrcReg1);
1462
1463 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1464 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001465 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001466 AddOptionalDefs(MIB);
1467 }
Chad Rosier78127d32011-10-26 23:25:44 +00001468
1469 // For floating point we need to move the result to a comparison register
1470 // that we can then use for branches.
1471 if (Ty->isFloatTy() || Ty->isDoubleTy())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001472 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier78127d32011-10-26 23:25:44 +00001473 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001474 return true;
1475}
1476
1477bool ARMFastISel::SelectCmp(const Instruction *I) {
1478 const CmpInst *CI = cast<CmpInst>(I);
1479
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001480 // Get the compare predicate.
1481 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001482
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001483 // We may not handle every CC for now.
1484 if (ARMPred == ARMCC::AL) return false;
1485
Chad Rosier59a20192011-10-26 22:47:55 +00001486 // Emit the compare.
Chad Rosier9cf803c2011-11-02 18:08:25 +00001487 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001488 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001489
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001490 // Now set a register based on the comparison. Explicitly set the predicates
1491 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001492 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topperc7242e02012-04-20 07:30:17 +00001493 const TargetRegisterClass *RC = isThumb2 ?
1494 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1495 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001496 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001497 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001498 unsigned ZeroReg = TargetMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001499 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001501 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001502 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001503
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001504 UpdateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001505 return true;
1506}
1507
Eric Christopher29ab6d12010-09-27 06:02:23 +00001508bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001509 // Make sure we have VFP and that we're extending float to double.
1510 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001511
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001512 Value *V = I->getOperand(0);
1513 if (!I->getType()->isDoubleTy() ||
1514 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001515
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001516 unsigned Op = getRegForValue(V);
1517 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001518
Craig Topperc7242e02012-04-20 07:30:17 +00001519 unsigned Result = createResultReg(&ARM::DPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001520 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001521 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001522 .addReg(Op));
1523 UpdateValueMap(I, Result);
1524 return true;
1525}
1526
Eric Christopher29ab6d12010-09-27 06:02:23 +00001527bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001528 // Make sure we have VFP and that we're truncating double to float.
1529 if (!Subtarget->hasVFP2()) return false;
1530
1531 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001532 if (!(I->getType()->isFloatTy() &&
1533 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001534
1535 unsigned Op = getRegForValue(V);
1536 if (Op == 0) return false;
1537
Craig Topperc7242e02012-04-20 07:30:17 +00001538 unsigned Result = createResultReg(&ARM::SPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001539 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001540 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001541 .addReg(Op));
1542 UpdateValueMap(I, Result);
1543 return true;
1544}
1545
Chad Rosiere023d5d2012-02-03 21:14:11 +00001546bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001547 // Make sure we have VFP.
1548 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001549
Duncan Sandsf5dda012010-11-03 11:35:31 +00001550 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001551 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001552 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001553 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001554
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001555 Value *Src = I->getOperand(0);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001556 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
1557 if (!SrcEVT.isSimple())
1558 return false;
1559 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001560 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001561 return false;
1562
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001563 unsigned SrcReg = getRegForValue(Src);
1564 if (SrcReg == 0) return false;
1565
1566 // Handle sign-extension.
1567 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001568 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001569 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001570 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001571 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001572
Eric Christopher860fc932010-09-10 00:34:35 +00001573 // The conversion routine works on fp-reg to fp-reg and the operand above
1574 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001575 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001576 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001577
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001578 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001579 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1580 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001581 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001582
Eric Christopher4bd70472010-09-09 21:44:45 +00001583 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001584 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1585 TII.get(Opc), ResultReg).addReg(FP));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001586 UpdateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001587 return true;
1588}
1589
Chad Rosiere023d5d2012-02-03 21:14:11 +00001590bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001591 // Make sure we have VFP.
1592 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001593
Duncan Sandsf5dda012010-11-03 11:35:31 +00001594 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001595 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001596 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001597 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001598
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001599 unsigned Op = getRegForValue(I->getOperand(0));
1600 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001601
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001602 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001603 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001604 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1605 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001606 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001607
Chad Rosier41f0e782012-02-03 20:27:51 +00001608 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001609 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001610 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1611 TII.get(Opc), ResultReg).addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001612
Eric Christopher4bd70472010-09-09 21:44:45 +00001613 // This result needs to be in an integer register, but the conversion only
1614 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001615 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001616 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001617
Eric Christopher4bd70472010-09-09 21:44:45 +00001618 UpdateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001619 return true;
1620}
1621
Eric Christopher511aa312010-10-11 08:27:59 +00001622bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001623 MVT VT;
1624 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001625 return false;
1626
1627 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001628 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001629
1630 unsigned CondReg = getRegForValue(I->getOperand(0));
1631 if (CondReg == 0) return false;
1632 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1633 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001634
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001635 // Check to see if we can use an immediate in the conditional move.
1636 int Imm = 0;
1637 bool UseImm = false;
1638 bool isNegativeImm = false;
1639 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1640 assert (VT == MVT::i32 && "Expecting an i32.");
1641 Imm = (int)ConstInt->getValue().getZExtValue();
1642 if (Imm < 0) {
1643 isNegativeImm = true;
1644 Imm = ~Imm;
1645 }
1646 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1647 (ARM_AM::getSOImmVal(Imm) != -1);
1648 }
1649
Duncan Sands12330652011-11-28 10:31:27 +00001650 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001651 if (!UseImm) {
1652 Op2Reg = getRegForValue(I->getOperand(2));
1653 if (Op2Reg == 0) return false;
1654 }
1655
1656 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
Jim Grosbachd7866792013-08-16 23:37:40 +00001657 CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001658 AddOptionalDefs(
1659 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1660 .addReg(CondReg)
1661 .addImm(0));
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001662
1663 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001664 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001665 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001666 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001667 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1668 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001669 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1670 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001671 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001672 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001673 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001674 }
Eric Christopher511aa312010-10-11 08:27:59 +00001675 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001676 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001677 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001678 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1680 ResultReg)
1681 .addReg(Op2Reg)
1682 .addReg(Op1Reg)
1683 .addImm(ARMCC::NE)
1684 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001685 } else {
1686 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001687 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1688 ResultReg)
1689 .addReg(Op1Reg)
1690 .addImm(Imm)
1691 .addImm(ARMCC::EQ)
1692 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001693 }
Eric Christopher511aa312010-10-11 08:27:59 +00001694 UpdateValueMap(I, ResultReg);
1695 return true;
1696}
1697
Chad Rosieraaa55a82012-02-03 21:07:27 +00001698bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001699 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001700 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001701 if (!isTypeLegal(Ty, VT))
1702 return false;
1703
1704 // If we have integer div support we should have selected this automagically.
1705 // In case we have a real miss go ahead and return false and we'll pick
1706 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001707 if (Subtarget->hasDivide()) return false;
1708
Eric Christopher56094ff2010-09-30 22:34:19 +00001709 // Otherwise emit a libcall.
1710 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001711 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001712 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001713 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001714 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001715 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001716 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001717 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001718 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001719 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001720 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001721 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001722
Eric Christopher56094ff2010-09-30 22:34:19 +00001723 return ARMEmitLibcall(I, LC);
1724}
1725
Chad Rosierb84a4b42012-02-03 21:23:45 +00001726bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001727 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001728 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001729 if (!isTypeLegal(Ty, VT))
1730 return false;
1731
1732 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1733 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001734 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001735 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001736 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001737 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001738 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001739 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001740 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001741 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001742 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001743 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001744
Eric Christophereae1b382010-10-11 08:37:26 +00001745 return ARMEmitLibcall(I, LC);
1746}
1747
Chad Rosier685b20c2012-02-06 23:50:07 +00001748bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier685b20c2012-02-06 23:50:07 +00001749 EVT DestVT = TLI.getValueType(I->getType(), true);
1750
1751 // We can get here in the case when we have a binary operation on a non-legal
1752 // type and the target independent selector doesn't know how to handle it.
1753 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1754 return false;
Jush Luac96b762012-06-14 06:08:19 +00001755
Chad Rosierbd471252012-02-08 02:29:21 +00001756 unsigned Opc;
1757 switch (ISDOpcode) {
1758 default: return false;
1759 case ISD::ADD:
1760 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1761 break;
1762 case ISD::OR:
1763 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1764 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001765 case ISD::SUB:
1766 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1767 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001768 }
1769
Chad Rosier685b20c2012-02-06 23:50:07 +00001770 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1771 if (SrcReg1 == 0) return false;
1772
1773 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1774 // in the instruction, rather then materializing the value in a register.
1775 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1776 if (SrcReg2 == 0) return false;
1777
JF Bastien13969d02013-05-29 15:45:47 +00001778 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001779 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1780 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001781 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier685b20c2012-02-06 23:50:07 +00001782 TII.get(Opc), ResultReg)
1783 .addReg(SrcReg1).addReg(SrcReg2));
1784 UpdateValueMap(I, ResultReg);
1785 return true;
1786}
1787
1788bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001789 EVT FPVT = TLI.getValueType(I->getType(), true);
1790 if (!FPVT.isSimple()) return false;
1791 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001792
Eric Christopher24dc27f2010-09-09 00:53:57 +00001793 // We can get here in the case when we want to use NEON for our fp
1794 // operations, but can't figure out how to. Just use the vfp instructions
1795 // if we have them.
1796 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001797 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001798 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1799 if (isFloat && !Subtarget->hasVFP2())
1800 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001801
Eric Christopher24dc27f2010-09-09 00:53:57 +00001802 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001803 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001804 switch (ISDOpcode) {
1805 default: return false;
1806 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001807 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001808 break;
1809 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001810 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001811 break;
1812 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001813 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001814 break;
1815 }
Chad Rosier80979b62011-11-16 18:39:44 +00001816 unsigned Op1 = getRegForValue(I->getOperand(0));
1817 if (Op1 == 0) return false;
1818
1819 unsigned Op2 = getRegForValue(I->getOperand(1));
1820 if (Op2 == 0) return false;
1821
Chad Rosier62a144f2012-12-17 19:59:43 +00001822 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001823 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher24dc27f2010-09-09 00:53:57 +00001824 TII.get(Opc), ResultReg)
1825 .addReg(Op1).addReg(Op2));
Eric Christopher5903c0b2010-09-09 20:26:31 +00001826 UpdateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001827 return true;
1828}
1829
Eric Christopher72497e52010-09-10 23:18:12 +00001830// Call Handling Code
1831
Jush Lue67e07b2012-07-19 09:49:00 +00001832// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001833// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001834CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1835 bool Return,
1836 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001837 switch (CC) {
1838 default:
1839 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001840 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001841 if (Subtarget->hasVFP2() && !isVarArg) {
1842 if (!Subtarget->isAAPCS_ABI())
1843 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1844 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1845 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1846 }
Evan Cheng21abfc92010-10-22 18:57:05 +00001847 // Fallthrough
1848 case CallingConv::C:
Eric Christopher72497e52010-09-10 23:18:12 +00001849 // Use target triple & subtarget features to do actual dispatch.
1850 if (Subtarget->isAAPCS_ABI()) {
1851 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001852 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001853 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1854 else
1855 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1856 } else
1857 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1858 case CallingConv::ARM_AAPCS_VFP:
Jush Lue67e07b2012-07-19 09:49:00 +00001859 if (!isVarArg)
1860 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1861 // Fall through to soft float variant, variadic functions don't
1862 // use hard floating point ABI.
Eric Christopher72497e52010-09-10 23:18:12 +00001863 case CallingConv::ARM_AAPCS:
1864 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1865 case CallingConv::ARM_APCS:
1866 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001867 case CallingConv::GHC:
1868 if (Return)
1869 llvm_unreachable("Can't return in GHC call convention");
1870 else
1871 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001872 }
1873}
1874
Eric Christopher79398062010-09-29 23:11:09 +00001875bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1876 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001877 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001878 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1879 SmallVectorImpl<unsigned> &RegArgs,
1880 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001881 unsigned &NumBytes,
1882 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001883 SmallVector<CCValAssign, 16> ArgLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00001884 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1885 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1886 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001887
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001888 // Check that we can handle all of the arguments. If we can't, then bail out
1889 // now before we add code to the MBB.
1890 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1891 CCValAssign &VA = ArgLocs[i];
1892 MVT ArgVT = ArgVTs[VA.getValNo()];
1893
1894 // We don't handle NEON/vector parameters yet.
1895 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1896 return false;
1897
1898 // Now copy/store arg to correct locations.
1899 if (VA.isRegLoc() && !VA.needsCustom()) {
1900 continue;
1901 } else if (VA.needsCustom()) {
1902 // TODO: We need custom lowering for vector (v2f64) args.
1903 if (VA.getLocVT() != MVT::f64 ||
1904 // TODO: Only handle register args for now.
1905 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1906 return false;
1907 } else {
Craig Topper56710102013-08-15 02:33:50 +00001908 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001909 default:
1910 return false;
1911 case MVT::i1:
1912 case MVT::i8:
1913 case MVT::i16:
1914 case MVT::i32:
1915 break;
1916 case MVT::f32:
1917 if (!Subtarget->hasVFP2())
1918 return false;
1919 break;
1920 case MVT::f64:
1921 if (!Subtarget->hasVFP2())
1922 return false;
1923 break;
1924 }
1925 }
1926 }
1927
1928 // At the point, we are able to handle the call's arguments in fast isel.
1929
Eric Christopher79398062010-09-29 23:11:09 +00001930 // Get a count of how many bytes are to be pushed on the stack.
1931 NumBytes = CCInfo.getNextStackOffset();
1932
1933 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00001934 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001935 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00001936 TII.get(AdjStackDown))
1937 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00001938
1939 // Process the args.
1940 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1941 CCValAssign &VA = ArgLocs[i];
1942 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00001943 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001944
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001945 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1946 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00001947
Eric Christopher78f8d4e2010-09-30 20:49:44 +00001948 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00001949 switch (VA.getLocInfo()) {
1950 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00001951 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001952 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001953 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1954 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001955 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001956 break;
1957 }
Chad Rosierd0191a52011-11-05 20:16:15 +00001958 case CCValAssign::AExt:
1959 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00001960 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001961 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001962 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00001963 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001964 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001965 break;
1966 }
1967 case CCValAssign::BCvt: {
Wesley Peck527da1b2010-11-23 03:31:01 +00001968 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001969 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00001970 assert(BC != 0 && "Failed to emit a bitcast!");
1971 Arg = BC;
1972 ArgVT = VA.getLocVT();
1973 break;
1974 }
1975 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00001976 }
1977
1978 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00001979 if (VA.isRegLoc() && !VA.needsCustom()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001980 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1981 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00001982 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001983 } else if (VA.needsCustom()) {
1984 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001985 assert(VA.getLocVT() == MVT::f64 &&
1986 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00001987
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001988 CCValAssign &NextVA = ArgLocs[++i];
1989
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001990 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
1991 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001992
Rafael Espindolaea09c592014-02-18 22:05:46 +00001993 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001994 TII.get(ARM::VMOVRRD), VA.getLocReg())
1995 .addReg(NextVA.getLocReg(), RegState::Define)
1996 .addReg(Arg));
1997 RegArgs.push_back(VA.getLocReg());
1998 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00001999 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002000 assert(VA.isMemLoc());
2001 // Need to store on the stack.
Eric Christopherfef5f312010-11-19 22:30:02 +00002002 Address Addr;
2003 Addr.BaseType = Address::RegBase;
2004 Addr.Base.Reg = ARM::SP;
2005 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002006
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002007 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2008 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002009 }
2010 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002011
Eric Christopher79398062010-09-29 23:11:09 +00002012 return true;
2013}
2014
Duncan Sandsf5dda012010-11-03 11:35:31 +00002015bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002016 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002017 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002018 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002019 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002020 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002021 TII.get(AdjStackUp))
2022 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002023
2024 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002025 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002026 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002027 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2028 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002029
2030 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002031 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002032 // For this move we copy into two registers and then move into the
2033 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002034 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002035 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002036 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002037 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopherc1e209d2010-10-01 00:00:11 +00002038 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002039 .addReg(RVLocs[0].getLocReg())
2040 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002041
Eric Christopheraf719ef2010-10-20 08:02:24 +00002042 UsedRegs.push_back(RVLocs[0].getLocReg());
2043 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002044
Eric Christopher7ac602b2010-10-11 08:38:55 +00002045 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002046 UpdateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002047 } else {
2048 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002049 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002050
2051 // Special handling for extended integers.
2052 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2053 CopyVT = MVT::i32;
2054
Craig Topper760b1342012-02-22 05:59:10 +00002055 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002056
Eric Christopherc1e209d2010-10-01 00:00:11 +00002057 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2059 TII.get(TargetOpcode::COPY),
Eric Christopherc1e209d2010-10-01 00:00:11 +00002060 ResultReg).addReg(RVLocs[0].getLocReg());
2061 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002062
Eric Christopher7ac602b2010-10-11 08:38:55 +00002063 // Finally update the result.
Eric Christopherc1e209d2010-10-01 00:00:11 +00002064 UpdateValueMap(I, ResultReg);
2065 }
Eric Christopher79398062010-09-29 23:11:09 +00002066 }
2067
Eric Christopher7ac602b2010-10-11 08:38:55 +00002068 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002069}
2070
Eric Christopher93bbe652010-10-22 01:28:00 +00002071bool ARMFastISel::SelectRet(const Instruction *I) {
2072 const ReturnInst *Ret = cast<ReturnInst>(I);
2073 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002074
Eric Christopher93bbe652010-10-22 01:28:00 +00002075 if (!FuncInfo.CanLowerReturn)
2076 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002077
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002078 // Build a list of return value registers.
2079 SmallVector<unsigned, 4> RetRegs;
2080
Eric Christopher93bbe652010-10-22 01:28:00 +00002081 CallingConv::ID CC = F.getCallingConv();
2082 if (Ret->getNumOperands() > 0) {
2083 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002084 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Eric Christopher93bbe652010-10-22 01:28:00 +00002085
2086 // Analyze operands of the call, assigning locations to each operand.
2087 SmallVector<CCValAssign, 16> ValLocs;
Jim Grosbache7e2aca2011-09-13 20:30:37 +00002088 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002089 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2090 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002091
2092 const Value *RV = Ret->getOperand(0);
2093 unsigned Reg = getRegForValue(RV);
2094 if (Reg == 0)
2095 return false;
2096
2097 // Only handle a single return value for now.
2098 if (ValLocs.size() != 1)
2099 return false;
2100
2101 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002102
Eric Christopher93bbe652010-10-22 01:28:00 +00002103 // Don't bother handling odd stuff for now.
2104 if (VA.getLocInfo() != CCValAssign::Full)
2105 return false;
2106 // Only handle register returns for now.
2107 if (!VA.isRegLoc())
2108 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002109
2110 unsigned SrcReg = Reg + VA.getValNo();
Chad Rosier62a144f2012-12-17 19:59:43 +00002111 EVT RVEVT = TLI.getValueType(RV->getType());
2112 if (!RVEVT.isSimple()) return false;
2113 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002114 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002115 // Special handling for extended integers.
2116 if (RVVT != DestVT) {
2117 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2118 return false;
2119
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002120 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2121
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002122 // Perform extension if flagged as either zext or sext. Otherwise, do
2123 // nothing.
2124 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2125 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2126 if (SrcReg == 0) return false;
2127 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002128 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002129
Eric Christopher93bbe652010-10-22 01:28:00 +00002130 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002131 unsigned DstReg = VA.getLocReg();
2132 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2133 // Avoid a cross-class copy. This is very unlikely.
2134 if (!SrcRC->contains(DstReg))
2135 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2137 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
Eric Christopher93bbe652010-10-22 01:28:00 +00002138
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002139 // Add register to return instruction.
2140 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002141 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002142
Chad Rosier0439cfc2011-11-08 21:12:00 +00002143 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002144 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002145 TII.get(RetOpc));
2146 AddOptionalDefs(MIB);
2147 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2148 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002149 return true;
2150}
2151
Chad Rosierc6916f82012-06-12 19:25:13 +00002152unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2153 if (UseReg)
2154 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2155 else
2156 return isThumb2 ? ARM::tBL : ARM::BL;
2157}
2158
2159unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002160 // Manually compute the global's type to avoid building it when unnecessary.
2161 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
2162 EVT LCREVT = TLI.getValueType(GVTy);
2163 if (!LCREVT.isSimple()) return 0;
2164
Bill Wendling76cce192013-12-29 08:00:04 +00002165 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Craig Topper062a2ba2014-04-25 05:30:21 +00002166 GlobalValue::ExternalLinkage, nullptr,
2167 Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002168 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002169 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002170}
2171
Eric Christopher8b912662010-09-14 23:03:37 +00002172// A quick function that will emit a call for a named libcall in F with the
2173// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002174// can emit a call for any libcall we can produce. This is an abridged version
2175// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002176// like computed function pointers or strange arguments at call sites.
2177// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2178// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002179bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2180 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002181
Eric Christopher8b912662010-09-14 23:03:37 +00002182 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002183 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002184 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002185 if (RetTy->isVoidTy())
2186 RetVT = MVT::isVoid;
2187 else if (!isTypeLegal(RetTy, RetVT))
2188 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002189
Chad Rosier90f9afe2012-05-11 18:51:55 +00002190 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002191 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002192 SmallVector<CCValAssign, 16> RVLocs;
2193 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002194 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002195 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2196 return false;
2197 }
2198
Eric Christopher79398062010-09-29 23:11:09 +00002199 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002200 SmallVector<Value*, 8> Args;
2201 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002202 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002203 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2204 Args.reserve(I->getNumOperands());
2205 ArgRegs.reserve(I->getNumOperands());
2206 ArgVTs.reserve(I->getNumOperands());
2207 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002208 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002209 Value *Op = I->getOperand(i);
2210 unsigned Arg = getRegForValue(Op);
2211 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002212
Chris Lattner229907c2011-07-18 04:54:35 +00002213 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002214 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002215 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002216
Eric Christopher8b912662010-09-14 23:03:37 +00002217 ISD::ArgFlagsTy Flags;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002218 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher8b912662010-09-14 23:03:37 +00002219 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002220
Eric Christopher8b912662010-09-14 23:03:37 +00002221 Args.push_back(Op);
2222 ArgRegs.push_back(Arg);
2223 ArgVTs.push_back(ArgVT);
2224 ArgFlags.push_back(Flags);
2225 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002226
Eric Christopher79398062010-09-29 23:11:09 +00002227 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002228 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002229 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002230 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2231 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002232 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002233
Chad Rosierc6916f82012-06-12 19:25:13 +00002234 unsigned CalleeReg = 0;
2235 if (EnableARMLongCalls) {
2236 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2237 if (CalleeReg == 0) return false;
2238 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002239
Chad Rosierc6916f82012-06-12 19:25:13 +00002240 // Issue the call.
2241 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2242 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002243 DbgLoc, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002244 // BL / BLX don't take a predicate, but tBL / tBLX do.
2245 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002246 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002247 if (EnableARMLongCalls)
2248 MIB.addReg(CalleeReg);
2249 else
2250 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002251
Eric Christopher8b912662010-09-14 23:03:37 +00002252 // Add implicit physical register uses to the call.
2253 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002254 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002255
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002256 // Add a register mask with the call-preserved registers.
2257 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2258 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2259
Eric Christopher79398062010-09-29 23:11:09 +00002260 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002261 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002262 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002263
Eric Christopher8b912662010-09-14 23:03:37 +00002264 // Set all unused physreg defs as dead.
2265 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002266
Eric Christopher8b912662010-09-14 23:03:37 +00002267 return true;
2268}
2269
Chad Rosiera7ebc562011-11-11 23:31:03 +00002270bool ARMFastISel::SelectCall(const Instruction *I,
Craig Topper062a2ba2014-04-25 05:30:21 +00002271 const char *IntrMemName = nullptr) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002272 const CallInst *CI = cast<CallInst>(I);
2273 const Value *Callee = CI->getCalledValue();
2274
Chad Rosiera7ebc562011-11-11 23:31:03 +00002275 // Can't handle inline asm.
2276 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002277
Chad Rosierdf42cf32012-12-11 00:18:02 +00002278 // Allow SelectionDAG isel to handle tail calls.
2279 if (CI->isTailCall()) return false;
2280
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002281 // Check the calling convention.
2282 ImmutableCallSite CS(CI);
2283 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002284
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002285 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002286
Chris Lattner229907c2011-07-18 04:54:35 +00002287 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2288 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Jush Lue67e07b2012-07-19 09:49:00 +00002289 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002290
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002291 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002292 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002293 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002294 if (RetTy->isVoidTy())
2295 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002296 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2297 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002298 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002299
Chad Rosier90f9afe2012-05-11 18:51:55 +00002300 // Can't handle non-double multi-reg retvals.
2301 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2302 RetVT != MVT::i16 && RetVT != MVT::i32) {
2303 SmallVector<CCValAssign, 16> RVLocs;
Jush Lue67e07b2012-07-19 09:49:00 +00002304 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2305 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002306 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2307 return false;
2308 }
2309
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002310 // Set up the argument vectors.
2311 SmallVector<Value*, 8> Args;
2312 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002313 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002314 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002315 unsigned arg_size = CS.arg_size();
2316 Args.reserve(arg_size);
2317 ArgRegs.reserve(arg_size);
2318 ArgVTs.reserve(arg_size);
2319 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002320 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2321 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002322 // If we're lowering a memory intrinsic instead of a regular call, skip the
2323 // last two arguments, which shouldn't be passed to the underlying function.
2324 if (IntrMemName && e-i <= 2)
2325 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002326
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002327 ISD::ArgFlagsTy Flags;
2328 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002329 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002330 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002331 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002332 Flags.setZExt();
2333
Chad Rosier8a98ec42011-11-04 00:58:10 +00002334 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002335 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2336 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2337 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2338 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002339 return false;
2340
Chris Lattner229907c2011-07-18 04:54:35 +00002341 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002342 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002343 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2344 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002345 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002346
2347 unsigned Arg = getRegForValue(*i);
2348 if (Arg == 0)
2349 return false;
2350
Rafael Espindolaea09c592014-02-18 22:05:46 +00002351 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002352 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002353
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002354 Args.push_back(*i);
2355 ArgRegs.push_back(Arg);
2356 ArgVTs.push_back(ArgVT);
2357 ArgFlags.push_back(Flags);
2358 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002359
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002360 // Handle the arguments now that we've gotten them.
2361 SmallVector<unsigned, 4> RegArgs;
2362 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002363 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2364 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002365 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002366
Chad Rosierc6916f82012-06-12 19:25:13 +00002367 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002368 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Chad Rosierc6916f82012-06-12 19:25:13 +00002369 if (!GV || EnableARMLongCalls) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002370
Chad Rosierc6916f82012-06-12 19:25:13 +00002371 unsigned CalleeReg = 0;
2372 if (UseReg) {
2373 if (IntrMemName)
2374 CalleeReg = getLibcallReg(IntrMemName);
2375 else
2376 CalleeReg = getRegForValue(Callee);
2377
Chad Rosier223faf72012-05-23 18:38:57 +00002378 if (CalleeReg == 0) return false;
2379 }
2380
Chad Rosierc6916f82012-06-12 19:25:13 +00002381 // Issue the call.
2382 unsigned CallOpc = ARMSelectCallOp(UseReg);
2383 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002384 DbgLoc, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002385
Logan Chien2361f512013-08-22 12:08:04 +00002386 unsigned char OpFlags = 0;
2387
2388 // Add MO_PLT for global address or external symbol in the PIC relocation
2389 // model.
2390 if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
2391 OpFlags = ARMII::MO_PLT;
2392
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002393 // ARM calls don't take a predicate, but tBL / tBLX do.
2394 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002395 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002396 if (UseReg)
2397 MIB.addReg(CalleeReg);
2398 else if (!IntrMemName)
Logan Chien2361f512013-08-22 12:08:04 +00002399 MIB.addGlobalAddress(GV, 0, OpFlags);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002400 else
Logan Chien2361f512013-08-22 12:08:04 +00002401 MIB.addExternalSymbol(IntrMemName, OpFlags);
Jush Luac96b762012-06-14 06:08:19 +00002402
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002403 // Add implicit physical register uses to the call.
2404 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002405 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002406
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002407 // Add a register mask with the call-preserved registers.
2408 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2409 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2410
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002411 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002412 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002413 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2414 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002415
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002416 // Set all unused physreg defs as dead.
2417 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002418
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002419 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002420}
2421
Chad Rosier057b6d32011-11-14 23:04:09 +00002422bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002423 return Len <= 16;
2424}
2425
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002426bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002427 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002428 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002429 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002430 return false;
2431
Chad Rosierab7223e2011-11-14 22:46:17 +00002432 while (Len) {
2433 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002434 if (!Alignment || Alignment >= 4) {
2435 if (Len >= 4)
2436 VT = MVT::i32;
2437 else if (Len >= 2)
2438 VT = MVT::i16;
2439 else {
2440 assert (Len == 1 && "Expected a length of 1!");
2441 VT = MVT::i8;
2442 }
2443 } else {
2444 // Bound based on alignment.
2445 if (Len >= 2 && Alignment == 2)
2446 VT = MVT::i16;
2447 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002448 VT = MVT::i8;
2449 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002450 }
2451
2452 bool RV;
2453 unsigned ResultReg;
2454 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002455 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002456 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002457 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002458 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002459
2460 unsigned Size = VT.getSizeInBits()/8;
2461 Len -= Size;
2462 Dest.Offset += Size;
2463 Src.Offset += Size;
2464 }
2465
2466 return true;
2467}
2468
Chad Rosiera7ebc562011-11-11 23:31:03 +00002469bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2470 // FIXME: Handle more intrinsics.
2471 switch (I.getIntrinsicID()) {
2472 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002473 case Intrinsic::frameaddress: {
2474 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2475 MFI->setFrameAddressIsTaken(true);
2476
2477 unsigned LdrOpc;
2478 const TargetRegisterClass *RC;
2479 if (isThumb2) {
2480 LdrOpc = ARM::t2LDRi12;
2481 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2482 } else {
2483 LdrOpc = ARM::LDRi12;
2484 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2485 }
2486
2487 const ARMBaseRegisterInfo *RegInfo =
2488 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2489 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2490 unsigned SrcReg = FramePtr;
2491
2492 // Recursively load frame address
2493 // ldr r0 [fp]
2494 // ldr r0 [r0]
2495 // ldr r0 [r0]
2496 // ...
2497 unsigned DestReg;
2498 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2499 while (Depth--) {
2500 DestReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002501 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier820d248c2012-05-30 17:23:22 +00002502 TII.get(LdrOpc), DestReg)
2503 .addReg(SrcReg).addImm(0));
2504 SrcReg = DestReg;
2505 }
Chad Rosierf3193242012-06-01 21:12:31 +00002506 UpdateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002507 return true;
2508 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002509 case Intrinsic::memcpy:
2510 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002511 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2512 // Don't handle volatile.
2513 if (MTI.isVolatile())
2514 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002515
2516 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2517 // we would emit dead code because we don't currently handle memmoves.
2518 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2519 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002520 // Small memcpy's are common enough that we want to do them without a call
2521 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002522 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002523 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002524 Address Dest, Src;
2525 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2526 !ARMComputeAddress(MTI.getRawSource(), Src))
2527 return false;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002528 unsigned Alignment = MTI.getAlignment();
2529 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002530 return true;
2531 }
2532 }
Jush Luac96b762012-06-14 06:08:19 +00002533
Chad Rosiera7ebc562011-11-11 23:31:03 +00002534 if (!MTI.getLength()->getType()->isIntegerTy(32))
2535 return false;
Jush Luac96b762012-06-14 06:08:19 +00002536
Chad Rosiera7ebc562011-11-11 23:31:03 +00002537 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2538 return false;
2539
2540 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2541 return SelectCall(&I, IntrMemName);
2542 }
2543 case Intrinsic::memset: {
2544 const MemSetInst &MSI = cast<MemSetInst>(I);
2545 // Don't handle volatile.
2546 if (MSI.isVolatile())
2547 return false;
Jush Luac96b762012-06-14 06:08:19 +00002548
Chad Rosiera7ebc562011-11-11 23:31:03 +00002549 if (!MSI.getLength()->getType()->isIntegerTy(32))
2550 return false;
Jush Luac96b762012-06-14 06:08:19 +00002551
Chad Rosiera7ebc562011-11-11 23:31:03 +00002552 if (MSI.getDestAddressSpace() > 255)
2553 return false;
Jush Luac96b762012-06-14 06:08:19 +00002554
Chad Rosiera7ebc562011-11-11 23:31:03 +00002555 return SelectCall(&I, "memset");
2556 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002557 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002559 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002560 return true;
2561 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002562 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002563}
2564
Chad Rosieree7e4522011-11-02 00:18:48 +00002565bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002566 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002567 // undefined.
2568 Value *Op = I->getOperand(0);
2569
2570 EVT SrcVT, DestVT;
2571 SrcVT = TLI.getValueType(Op->getType(), true);
2572 DestVT = TLI.getValueType(I->getType(), true);
2573
2574 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2575 return false;
2576 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2577 return false;
2578
2579 unsigned SrcReg = getRegForValue(Op);
2580 if (!SrcReg) return false;
2581
2582 // Because the high bits are undefined, a truncate doesn't generate
2583 // any code.
2584 UpdateValueMap(I, SrcReg);
2585 return true;
2586}
2587
Chad Rosier62a144f2012-12-17 19:59:43 +00002588unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002589 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002590 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002591 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002592 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002593 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002594
2595 // Table of which combinations can be emitted as a single instruction,
2596 // and which will require two.
2597 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2598 // ARM Thumb
2599 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2600 // ext: s z s z s z s z
2601 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2602 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2603 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2604 };
2605
2606 // Target registers for:
2607 // - For ARM can never be PC.
2608 // - For 16-bit Thumb are restricted to lower 8 registers.
2609 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2610 static const TargetRegisterClass *RCTbl[2][2] = {
2611 // Instructions: Two Single
2612 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2613 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2614 };
2615
2616 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002617 static const struct InstructionTable {
2618 uint32_t Opc : 16;
2619 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2620 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2621 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2622 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002623 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002624 { // ARM Opc S Shift Imm
2625 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2626 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2627 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2628 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2629 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2630 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002631 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002632 { // Thumb Opc S Shift Imm
2633 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2634 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2635 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2636 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2637 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2638 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002639 }
2640 },
2641 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002642 { // ARM Opc S Shift Imm
2643 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2644 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2645 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2646 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2647 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2648 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002649 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002650 { // Thumb Opc S Shift Imm
2651 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2652 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2653 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2654 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2655 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2656 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002657 }
2658 }
2659 };
2660
2661 unsigned SrcBits = SrcVT.getSizeInBits();
2662 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002663 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002664 assert((SrcBits < DestBits) && "can only extend to larger types");
2665 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2666 "other sizes unimplemented");
2667 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2668 "other sizes unimplemented");
2669
2670 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002671 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002672 assert((Bitness < 3) && "sanity-check table bounds");
2673
2674 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2675 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002676 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2677 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002678 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002679 unsigned hasS = ITP->hasS;
2680 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2681 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2682 "only MOVsi has shift operand addressing mode");
2683 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002684
2685 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2686 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002687 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002688 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002689 // MOVsi encodes shift and immediate in shift operand addressing mode.
2690 // The following condition has the same value when emitting two
2691 // instruction sequences: both are shifts.
2692 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002693
2694 // Either one or two instructions are emitted.
2695 // They're always of the form:
2696 // dst = in OP imm
2697 // CPSR is set only by 16-bit Thumb instructions.
2698 // Predicate, if any, is AL.
2699 // S bit, if available, is always 0.
2700 // When two are emitted the first's result will feed as the second's input,
2701 // that value is then dead.
2702 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2703 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2704 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002705 bool isLsl = (0 == Instr) && !isSingleInstr;
2706 unsigned Opcode = isLsl ? LSLOpc : Opc;
2707 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2708 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002709 bool isKill = 1 == Instr;
2710 MachineInstrBuilder MIB = BuildMI(
Rafael Espindolaea09c592014-02-18 22:05:46 +00002711 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
JF Bastien06ce03d2013-06-07 20:10:37 +00002712 if (setsCPSR)
2713 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002714 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002715 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002716 if (hasS)
2717 AddDefaultCC(MIB);
2718 // Second instruction consumes the first's result.
2719 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002720 }
2721
Chad Rosier4489f942011-11-02 17:20:24 +00002722 return ResultReg;
2723}
2724
2725bool ARMFastISel::SelectIntExt(const Instruction *I) {
2726 // On ARM, in general, integer casts don't involve legal types; this code
2727 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002728 Type *DestTy = I->getType();
2729 Value *Src = I->getOperand(0);
2730 Type *SrcTy = Src->getType();
2731
Chad Rosier4489f942011-11-02 17:20:24 +00002732 bool isZExt = isa<ZExtInst>(I);
2733 unsigned SrcReg = getRegForValue(Src);
2734 if (!SrcReg) return false;
2735
Chad Rosier62a144f2012-12-17 19:59:43 +00002736 EVT SrcEVT, DestEVT;
2737 SrcEVT = TLI.getValueType(SrcTy, true);
2738 DestEVT = TLI.getValueType(DestTy, true);
2739 if (!SrcEVT.isSimple()) return false;
2740 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002741
Chad Rosier62a144f2012-12-17 19:59:43 +00002742 MVT SrcVT = SrcEVT.getSimpleVT();
2743 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002744 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2745 if (ResultReg == 0) return false;
2746 UpdateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002747 return true;
2748}
2749
Jush Lu4705da92012-08-03 02:37:48 +00002750bool ARMFastISel::SelectShift(const Instruction *I,
2751 ARM_AM::ShiftOpc ShiftTy) {
2752 // We handle thumb2 mode by target independent selector
2753 // or SelectionDAG ISel.
2754 if (isThumb2)
2755 return false;
2756
2757 // Only handle i32 now.
2758 EVT DestVT = TLI.getValueType(I->getType(), true);
2759 if (DestVT != MVT::i32)
2760 return false;
2761
2762 unsigned Opc = ARM::MOVsr;
2763 unsigned ShiftImm;
2764 Value *Src2Value = I->getOperand(1);
2765 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2766 ShiftImm = CI->getZExtValue();
2767
2768 // Fall back to selection DAG isel if the shift amount
2769 // is zero or greater than the width of the value type.
2770 if (ShiftImm == 0 || ShiftImm >=32)
2771 return false;
2772
2773 Opc = ARM::MOVsi;
2774 }
2775
2776 Value *Src1Value = I->getOperand(0);
2777 unsigned Reg1 = getRegForValue(Src1Value);
2778 if (Reg1 == 0) return false;
2779
Nadav Rotema8e15b02012-09-06 11:13:55 +00002780 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002781 if (Opc == ARM::MOVsr) {
2782 Reg2 = getRegForValue(Src2Value);
2783 if (Reg2 == 0) return false;
2784 }
2785
JF Bastien13969d02013-05-29 15:45:47 +00002786 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002787 if(ResultReg == 0) return false;
2788
Rafael Espindolaea09c592014-02-18 22:05:46 +00002789 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu4705da92012-08-03 02:37:48 +00002790 TII.get(Opc), ResultReg)
2791 .addReg(Reg1);
2792
2793 if (Opc == ARM::MOVsi)
2794 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2795 else if (Opc == ARM::MOVsr) {
2796 MIB.addReg(Reg2);
2797 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2798 }
2799
2800 AddOptionalDefs(MIB);
2801 UpdateValueMap(I, ResultReg);
2802 return true;
2803}
2804
Eric Christopherc3e118e2010-09-02 23:43:26 +00002805// TODO: SoftFP support.
Eric Christopher84bdfd82010-07-21 22:26:11 +00002806bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002807
Eric Christopher84bdfd82010-07-21 22:26:11 +00002808 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002809 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002810 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002811 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002812 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002813 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002814 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002815 case Instruction::IndirectBr:
2816 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002817 case Instruction::ICmp:
2818 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002819 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002820 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002821 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002822 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002823 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002824 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002825 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002826 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002827 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002828 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002829 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002830 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002831 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002832 case Instruction::Add:
2833 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002834 case Instruction::Or:
2835 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002836 case Instruction::Sub:
2837 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002838 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002839 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002840 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002841 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002842 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002843 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002844 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002845 return SelectDiv(I, /*isSigned*/ true);
2846 case Instruction::UDiv:
2847 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002848 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002849 return SelectRem(I, /*isSigned*/ true);
2850 case Instruction::URem:
2851 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002852 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002853 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2854 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002855 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002856 case Instruction::Select:
2857 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002858 case Instruction::Ret:
2859 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002860 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002861 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002862 case Instruction::ZExt:
2863 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002864 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002865 case Instruction::Shl:
2866 return SelectShift(I, ARM_AM::lsl);
2867 case Instruction::LShr:
2868 return SelectShift(I, ARM_AM::lsr);
2869 case Instruction::AShr:
2870 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002871 default: break;
2872 }
2873 return false;
2874}
2875
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002876namespace {
2877// This table describes sign- and zero-extend instructions which can be
2878// folded into a preceding load. All of these extends have an immediate
2879// (sometimes a mask and sometimes a shift) that's applied after
2880// extension.
2881const struct FoldableLoadExtendsStruct {
2882 uint16_t Opc[2]; // ARM, Thumb.
2883 uint8_t ExpectedImm;
2884 uint8_t isZExt : 1;
2885 uint8_t ExpectedVT : 7;
2886} FoldableLoadExtends[] = {
2887 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2888 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2889 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2890 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2891 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2892};
2893}
2894
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002895/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002896/// vreg is being provided by the specified load instruction. If possible,
2897/// try to fold the load as an operand to the instruction, returning true if
2898/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002899bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2900 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002901 // Verify we have a legal type before going any further.
2902 MVT VT;
2903 if (!isLoadTypeLegal(LI->getType(), VT))
2904 return false;
2905
2906 // Combine load followed by zero- or sign-extend.
2907 // ldrb r1, [r0] ldrb r1, [r0]
2908 // uxtb r2, r1 =>
2909 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002910 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2911 return false;
2912 const uint64_t Imm = MI->getOperand(2).getImm();
2913
2914 bool Found = false;
2915 bool isZExt;
2916 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
2917 i != e; ++i) {
2918 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
2919 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
2920 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
2921 Found = true;
2922 isZExt = FoldableLoadExtends[i].isZExt;
2923 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002924 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002925 if (!Found) return false;
2926
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002927 // See if we can handle this address.
2928 Address Addr;
2929 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00002930
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002931 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00002932 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002933 return false;
2934 MI->eraseFromParent();
2935 return true;
2936}
2937
Jush Lu47172a02012-09-27 05:21:41 +00002938unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002939 unsigned Align, MVT VT) {
Jush Lu47172a02012-09-27 05:21:41 +00002940 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
2941 ARMConstantPoolConstant *CPV =
2942 ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
2943 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
2944
2945 unsigned Opc;
2946 unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
2947 // Load value.
2948 if (isThumb2) {
Jim Grosbach5f71aab2013-08-26 20:07:29 +00002949 DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002950 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu47172a02012-09-27 05:21:41 +00002951 TII.get(ARM::t2LDRpci), DestReg1)
2952 .addConstantPoolIndex(Idx));
2953 Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
2954 } else {
2955 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +00002956 DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
Jush Lu47172a02012-09-27 05:21:41 +00002957 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002958 DbgLoc, TII.get(ARM::LDRcp), DestReg1)
Jush Lu47172a02012-09-27 05:21:41 +00002959 .addConstantPoolIndex(Idx).addImm(0));
2960 Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
2961 }
2962
2963 unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
2964 if (GlobalBaseReg == 0) {
2965 GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
2966 AFI->setGlobalBaseReg(GlobalBaseReg);
2967 }
2968
2969 unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
Jim Grosbach5f71aab2013-08-26 20:07:29 +00002970 DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
2971 DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
2972 GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
Jush Lu47172a02012-09-27 05:21:41 +00002973 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002974 DbgLoc, TII.get(Opc), DestReg2)
Jush Lu47172a02012-09-27 05:21:41 +00002975 .addReg(DestReg1)
2976 .addReg(GlobalBaseReg);
2977 if (!UseGOTOFF)
2978 MIB.addImm(0);
2979 AddOptionalDefs(MIB);
2980
2981 return DestReg2;
2982}
2983
Evan Cheng615620c2013-02-11 01:27:15 +00002984bool ARMFastISel::FastLowerArguments() {
2985 if (!FuncInfo.CanLowerReturn)
2986 return false;
2987
2988 const Function *F = FuncInfo.Fn;
2989 if (F->isVarArg())
2990 return false;
2991
2992 CallingConv::ID CC = F->getCallingConv();
2993 switch (CC) {
2994 default:
2995 return false;
2996 case CallingConv::Fast:
2997 case CallingConv::C:
2998 case CallingConv::ARM_AAPCS_VFP:
2999 case CallingConv::ARM_AAPCS:
3000 case CallingConv::ARM_APCS:
3001 break;
3002 }
3003
3004 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3005 // which are passed in r0 - r3.
3006 unsigned Idx = 1;
3007 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3008 I != E; ++I, ++Idx) {
3009 if (Idx > 4)
3010 return false;
3011
3012 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
3013 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
3014 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
3015 return false;
3016
3017 Type *ArgTy = I->getType();
3018 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3019 return false;
3020
3021 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003022 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003023 switch (ArgVT.getSimpleVT().SimpleTy) {
3024 case MVT::i8:
3025 case MVT::i16:
3026 case MVT::i32:
3027 break;
3028 default:
3029 return false;
3030 }
3031 }
3032
3033
3034 static const uint16_t GPRArgRegs[] = {
3035 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3036 };
3037
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003038 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003039 Idx = 0;
3040 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3041 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003042 unsigned SrcReg = GPRArgRegs[Idx];
3043 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3044 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3045 // Without this, EmitLiveInCopies may eliminate the livein if its only
3046 // use is a bitcast (which isn't turned into an instruction).
3047 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003048 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3049 TII.get(TargetOpcode::COPY),
Evan Cheng615620c2013-02-11 01:27:15 +00003050 ResultReg).addReg(DstReg, getKillRegState(true));
3051 UpdateValueMap(I, ResultReg);
3052 }
3053
3054 return true;
3055}
3056
Eric Christopher84bdfd82010-07-21 22:26:11 +00003057namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003058 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3059 const TargetLibraryInfo *libInfo) {
Eric Christopher5501b7e2010-10-11 20:05:22 +00003060 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach68147ee2010-11-09 19:22:26 +00003061
Eric Christopher5501b7e2010-10-11 20:05:22 +00003062 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
JF Bastien18db1f22013-06-14 02:49:43 +00003063 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3064 bool UseFastISel = false;
Tim Northoverd6a729b2014-01-06 14:28:05 +00003065 UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
JF Bastien18db1f22013-06-14 02:49:43 +00003066 UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
3067 UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
3068
3069 if (UseFastISel) {
3070 // iOS always has a FP for backtracking, force other targets
3071 // to keep their FP when doing FastISel. The emitted code is
3072 // currently superior, and in cases like test-suite's lencod
3073 // FastISel isn't quite correct when FP is eliminated.
3074 TM.Options.NoFramePointerElim = true;
Bob Wilson3e6fa462012-08-03 04:06:28 +00003075 return new ARMFastISel(funcInfo, libInfo);
JF Bastien18db1f22013-06-14 02:49:43 +00003076 }
Craig Topper062a2ba2014-04-25 05:30:21 +00003077 return nullptr;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003078 }
3079}