blob: 9b50458dd9bbdc3ee1db986e4ab5aa2f472f8176 [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/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineMemOperand.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000033#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/CallingConv.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000037#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/IntrinsicInst.h"
41#include "llvm/IR/Module.h"
42#include "llvm/IR/Operator.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000043#include "llvm/Support/ErrorHandling.h"
Eric Christopher09f757d2010-08-17 01:25:29 +000044#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000047#include "llvm/Target/TargetOptions.h"
48using namespace llvm;
49
50namespace {
Eric Christopher0a3c28b2010-11-20 22:38:27 +000051
Eric Christopherfef5f312010-11-19 22:30:02 +000052 // All possible address modes, plus some.
53 typedef struct Address {
54 enum {
55 RegBase,
56 FrameIndexBase
57 } BaseType;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000058
Eric Christopherfef5f312010-11-19 22:30:02 +000059 union {
60 unsigned Reg;
61 int FI;
62 } Base;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000063
Eric Christopherfef5f312010-11-19 22:30:02 +000064 int Offset;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000065
Eric Christopherfef5f312010-11-19 22:30:02 +000066 // Innocuous defaults for our address.
67 Address()
Jim Grosbach4e983162011-05-16 22:24:07 +000068 : BaseType(RegBase), Offset(0) {
Eric Christopherfef5f312010-11-19 22:30:02 +000069 Base.Reg = 0;
70 }
71 } Address;
Eric Christopher84bdfd82010-07-21 22:26:11 +000072
Craig Topper26696312014-03-18 07:27:13 +000073class ARMFastISel final : public FastISel {
Eric Christopher84bdfd82010-07-21 22:26:11 +000074
75 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
76 /// make the right decision when generating code for different targets.
77 const ARMSubtarget *Subtarget;
Bill Wendling6c1d9592013-12-30 05:17:29 +000078 Module &M;
Eric Christopher09f757d2010-08-17 01:25:29 +000079 const TargetMachine &TM;
80 const TargetInstrInfo &TII;
81 const TargetLowering &TLI;
Eric Christopher83a5ec82010-10-01 23:24:42 +000082 ARMFunctionInfo *AFI;
Eric Christopher84bdfd82010-07-21 22:26:11 +000083
Eric Christopherb024be32010-09-29 22:24:45 +000084 // Convenience variables to avoid some queries.
Chad Rosier0439cfc2011-11-08 21:12:00 +000085 bool isThumb2;
Eric Christopherb024be32010-09-29 22:24:45 +000086 LLVMContext *Context;
Eric Christopher6a0333c2010-09-02 01:39:14 +000087
Eric Christopher84bdfd82010-07-21 22:26:11 +000088 public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000089 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
90 const TargetLibraryInfo *libInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000091 : FastISel(funcInfo, libInfo),
Eric Christopherc125e122015-01-29 00:19:37 +000092 Subtarget(
93 &static_cast<const ARMSubtarget &>(funcInfo.MF->getSubtarget())),
Eric Christopherd9134482014-08-04 21:25:23 +000094 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
Eric Christopherc125e122015-01-29 00:19:37 +000095 TM(funcInfo.MF->getTarget()), TII(*Subtarget->getInstrInfo()),
96 TLI(*Subtarget->getTargetLowering()) {
Eric Christopher8d03b8a2010-08-23 22:32:45 +000097 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier0439cfc2011-11-08 21:12:00 +000098 isThumb2 = AFI->isThumbFunction();
Eric Christopherb024be32010-09-29 22:24:45 +000099 Context = &funcInfo.Fn->getContext();
Eric Christopher84bdfd82010-07-21 22:26:11 +0000100 }
101
Eric Christopherd8e8a292010-08-20 00:20:31 +0000102 // Code from FastISel.cpp.
Craig Topperfd1c9252012-08-18 21:38:45 +0000103 private:
Juergen Ributzka88e32512014-09-03 20:56:59 +0000104 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000107 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000108 const TargetRegisterClass *RC,
109 unsigned Op0, bool Op0IsKill,
110 unsigned Op1, bool Op1IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000111 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill,
114 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000115 unsigned fastEmitInst_i(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000116 const TargetRegisterClass *RC,
117 uint64_t Imm);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000118
Eric Christopherd8e8a292010-08-20 00:20:31 +0000119 // Backend specific FastISel code.
Craig Topperfd1c9252012-08-18 21:38:45 +0000120 private:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000121 bool fastSelectInstruction(const Instruction *I) override;
122 unsigned fastMaterializeConstant(const Constant *C) override;
123 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000124 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
125 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000126 bool fastLowerArguments() override;
Craig Topperfd1c9252012-08-18 21:38:45 +0000127 private:
Eric Christopher84bdfd82010-07-21 22:26:11 +0000128 #include "ARMGenFastISel.inc"
Eric Christopher2ff757d2010-09-09 01:06:51 +0000129
Eric Christopher00202ee2010-08-23 21:44:12 +0000130 // Instruction selection routines.
Eric Christophercc766a22010-09-10 23:10:30 +0000131 private:
Eric Christopher2f8637d2010-10-21 21:47:51 +0000132 bool SelectLoad(const Instruction *I);
133 bool SelectStore(const Instruction *I);
134 bool SelectBranch(const Instruction *I);
Chad Rosierded4c992012-02-07 23:56:08 +0000135 bool SelectIndirectBr(const Instruction *I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000136 bool SelectCmp(const Instruction *I);
137 bool SelectFPExt(const Instruction *I);
138 bool SelectFPTrunc(const Instruction *I);
Chad Rosier685b20c2012-02-06 23:50:07 +0000139 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
140 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosiere023d5d2012-02-03 21:14:11 +0000141 bool SelectIToFP(const Instruction *I, bool isSigned);
142 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosieraaa55a82012-02-03 21:07:27 +0000143 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosierb84a4b42012-02-03 21:23:45 +0000144 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosiera7ebc562011-11-11 23:31:03 +0000145 bool SelectCall(const Instruction *I, const char *IntrMemName);
146 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000147 bool SelectSelect(const Instruction *I);
Eric Christopher93bbe652010-10-22 01:28:00 +0000148 bool SelectRet(const Instruction *I);
Chad Rosieree7e4522011-11-02 00:18:48 +0000149 bool SelectTrunc(const Instruction *I);
150 bool SelectIntExt(const Instruction *I);
Jush Lu4705da92012-08-03 02:37:48 +0000151 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopher84bdfd82010-07-21 22:26:11 +0000152
Eric Christopher00202ee2010-08-23 21:44:12 +0000153 // Utility routines.
Eric Christopher0d274a02010-08-19 00:37:05 +0000154 private:
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000155 bool isPositionIndependent() const;
Chris Lattner229907c2011-07-18 04:54:35 +0000156 bool isTypeLegal(Type *Ty, MVT &VT);
157 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosier9cf803c2011-11-02 18:08:25 +0000158 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
David Blaikie3ef249c92015-01-30 23:04:39 +0000159 bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000160 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosiera26979b2011-12-14 17:26:05 +0000161 unsigned Alignment = 0, bool isZExt = true,
162 bool allocReg = true);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000163 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +0000164 unsigned Alignment = 0);
Eric Christopherfef5f312010-11-19 22:30:02 +0000165 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier150d35b2012-12-17 22:35:29 +0000166 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier057b6d32011-11-14 23:04:09 +0000167 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosier9f5c68a2012-12-06 01:34:31 +0000168 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
169 unsigned Alignment);
Chad Rosier62a144f2012-12-17 19:59:43 +0000170 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000171 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
172 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
173 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
174 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
175 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000176 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000177 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000178
Eric Christopher1b21f002015-01-29 00:19:33 +0000179 const TargetLowering *getTargetLowering() { return &TLI; }
Christian Pirker238c7c12014-05-12 11:19:20 +0000180
Eric Christopher72497e52010-09-10 23:18:12 +0000181 // Call handling routines.
182 private:
Jush Lue67e07b2012-07-19 09:49:00 +0000183 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
184 bool Return,
185 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000186 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000187 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000188 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000189 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
190 SmallVectorImpl<unsigned> &RegArgs,
191 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000192 unsigned &NumBytes,
193 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000194 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000195 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000196 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000197 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000198 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000199
200 // OptionalDef handling routines.
201 private:
Eric Christopher174d8722011-03-12 01:09:29 +0000202 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000203 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
204 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000205 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000206 const MachineInstrBuilder &MIB,
Justin Lebar0af80cd2016-07-15 18:26:59 +0000207 MachineMemOperand::Flags Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000208};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000209
210} // end anonymous namespace
211
Eric Christopher72497e52010-09-10 23:18:12 +0000212#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000213
Eric Christopher0d274a02010-08-19 00:37:05 +0000214// DefinesOptionalPredicate - This is different from DefinesPredicate in that
215// we don't care about implicit defs here, just places we'll need to add a
216// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
217bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000218 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000219 return false;
220
221 // Look to see if our OptionalDef is defining CPSR or CCR.
222 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
223 const MachineOperand &MO = MI->getOperand(i);
Eric Christopher985d9e42010-08-20 00:36:24 +0000224 if (!MO.isReg() || !MO.isDef()) continue;
225 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000226 *CPSR = true;
227 }
228 return true;
229}
230
Eric Christopher174d8722011-03-12 01:09:29 +0000231bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000232 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000233
Joey Goulya5153cb2013-09-09 14:21:49 +0000234 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000235 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000236 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000237 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000238
Evan Cheng6cc775f2011-06-28 19:10:37 +0000239 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
240 if (MCID.OpInfo[i].isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000241 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000242
Eric Christopher174d8722011-03-12 01:09:29 +0000243 return false;
244}
245
Eric Christopher0d274a02010-08-19 00:37:05 +0000246// If the machine is predicable go ahead and add the predicate operands, if
247// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000248// TODO: If we want to support thumb1 then we'll need to deal with optional
249// CPSR defs that need to be added before the remaining operands. See s_cc_out
250// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000251const MachineInstrBuilder &
252ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
253 MachineInstr *MI = &*MIB;
254
Eric Christopher174d8722011-03-12 01:09:29 +0000255 // Do we use a predicate? or...
256 // Are we NEON in ARM mode and have a predicate operand? If so, I know
257 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000258 if (isARMNEONPred(MI))
Eric Christopher0d274a02010-08-19 00:37:05 +0000259 AddDefaultPred(MIB);
Eric Christopher501d2e22011-04-29 00:03:10 +0000260
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000261 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000262 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000263 bool CPSR = false;
Eric Christopher0d274a02010-08-19 00:37:05 +0000264 if (DefinesOptionalPredicate(MI, &CPSR)) {
265 if (CPSR)
266 AddDefaultT1CC(MIB);
267 else
268 AddDefaultCC(MIB);
269 }
270 return MIB;
271}
272
Juergen Ributzka88e32512014-09-03 20:56:59 +0000273unsigned ARMFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000274 const TargetRegisterClass *RC,
275 unsigned Op0, bool Op0IsKill) {
276 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000277 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000278
Jim Grosbach06c2a682013-08-16 23:37:31 +0000279 // Make sure the input operand is sufficiently constrained to be legal
280 // for this instruction.
281 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000282 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
284 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000285 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000287 .addReg(Op0, Op0IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000289 TII.get(TargetOpcode::COPY), ResultReg)
290 .addReg(II.ImplicitDefs[0]));
291 }
292 return ResultReg;
293}
294
Juergen Ributzka88e32512014-09-03 20:56:59 +0000295unsigned ARMFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000296 const TargetRegisterClass *RC,
297 unsigned Op0, bool Op0IsKill,
298 unsigned Op1, bool Op1IsKill) {
299 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000300 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000301
Jim Grosbach06c2a682013-08-16 23:37:31 +0000302 // Make sure the input operands are sufficiently constrained to be legal
303 // for this instruction.
304 Op0 = constrainOperandRegClass(II, Op0, 1);
305 Op1 = constrainOperandRegClass(II, Op1, 2);
306
Chad Rosier0bc51322012-02-15 17:36:21 +0000307 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000308 AddOptionalDefs(
309 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
310 .addReg(Op0, Op0IsKill * RegState::Kill)
311 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000312 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000314 .addReg(Op0, Op0IsKill * RegState::Kill)
315 .addReg(Op1, Op1IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000317 TII.get(TargetOpcode::COPY), ResultReg)
318 .addReg(II.ImplicitDefs[0]));
319 }
320 return ResultReg;
321}
322
Juergen Ributzka88e32512014-09-03 20:56:59 +0000323unsigned ARMFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000324 const TargetRegisterClass *RC,
325 unsigned Op0, bool Op0IsKill,
326 uint64_t Imm) {
327 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000328 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000329
Jim Grosbach06c2a682013-08-16 23:37:31 +0000330 // Make sure the input operand is sufficiently constrained to be legal
331 // for this instruction.
332 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000333 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000334 AddOptionalDefs(
335 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
336 .addReg(Op0, Op0IsKill * RegState::Kill)
337 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000338 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000340 .addReg(Op0, Op0IsKill * RegState::Kill)
341 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000343 TII.get(TargetOpcode::COPY), ResultReg)
344 .addReg(II.ImplicitDefs[0]));
345 }
346 return ResultReg;
347}
348
Juergen Ributzka88e32512014-09-03 20:56:59 +0000349unsigned ARMFastISel::fastEmitInst_i(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000350 const TargetRegisterClass *RC,
351 uint64_t Imm) {
352 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000353 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000354
Chad Rosier0bc51322012-02-15 17:36:21 +0000355 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
357 ResultReg).addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000358 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000360 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000361 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000362 TII.get(TargetOpcode::COPY), ResultReg)
363 .addReg(II.ImplicitDefs[0]));
364 }
365 return ResultReg;
366}
367
Eric Christopher860fc932010-09-10 00:34:35 +0000368// TODO: Don't worry about 64-bit now, but when this is fixed remove the
369// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000370unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000371 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000372
Eric Christopher4bd70472010-09-09 21:44:45 +0000373 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000375 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000376 .addReg(SrcReg));
377 return MoveReg;
378}
379
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000380unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000381 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000382
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000383 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000385 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000386 .addReg(SrcReg));
387 return MoveReg;
388}
389
Eric Christopher3cf63f12010-09-09 00:19:41 +0000390// For double width floating point we need to materialize two constants
391// (the high and the low) into integer registers then use a move to get
392// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000393unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000394 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000395 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000396
Eric Christopher3cf63f12010-09-09 00:19:41 +0000397 // This checks to see if we can use VFP3 instructions to materialize
398 // a constant, otherwise we have to go through the constant pool.
399 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000400 int Imm;
401 unsigned Opc;
402 if (is64bit) {
403 Imm = ARM_AM::getFP64Imm(Val);
404 Opc = ARM::FCONSTD;
405 } else {
406 Imm = ARM_AM::getFP32Imm(Val);
407 Opc = ARM::FCONSTS;
408 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000409 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000410 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
411 TII.get(Opc), DestReg).addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000412 return DestReg;
413 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000414
Eric Christopher860fc932010-09-10 00:34:35 +0000415 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000416 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000417
Eric Christopher22fd29a2010-09-09 23:50:00 +0000418 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000419 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000420 if (Align == 0) {
421 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000422 Align = DL.getTypeAllocSize(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000423 }
424 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
425 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
426 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000427
Eric Christopher860fc932010-09-10 00:34:35 +0000428 // The extra reg is for addrmode5.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000429 AddOptionalDefs(
430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
431 .addConstantPoolIndex(Idx)
432 .addReg(0));
Eric Christopher22fd29a2010-09-09 23:50:00 +0000433 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000434}
435
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000436unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Eric Christopher7ac602b2010-10-11 08:38:55 +0000437
Chad Rosier67f96882011-11-04 22:29:00 +0000438 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000439 return 0;
Eric Christophere4dd7372010-11-03 20:21:17 +0000440
441 // If we can do this in a single instruction without a constant pool entry
442 // do so now.
443 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000444 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000445 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000446 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
447 &ARM::GPRRegClass;
448 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000449 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier67f96882011-11-04 22:29:00 +0000450 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000451 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000452 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000453 }
454
Chad Rosier2a3503e2011-11-11 00:36:21 +0000455 // Use MVN to emit negative constants.
456 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
457 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000458 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000459 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000460 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000461 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
Juergen Ributzka2cbcf7a2014-08-13 21:39:18 +0000462 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
463 &ARM::GPRRegClass;
464 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier2a3503e2011-11-11 00:36:21 +0000466 TII.get(Opc), ImmReg)
467 .addImm(Imm));
468 return ImmReg;
469 }
470 }
471
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000472 unsigned ResultReg = 0;
Juergen Ributzkaa5b08382014-08-13 21:42:19 +0000473 if (Subtarget->useMovt(*FuncInfo.MF))
Juergen Ributzka88e32512014-09-03 20:56:59 +0000474 ResultReg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000475
476 if (ResultReg)
477 return ResultReg;
Juergen Ributzkaa5b08382014-08-13 21:42:19 +0000478
Chad Rosier2a3503e2011-11-11 00:36:21 +0000479 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000480 if (VT != MVT::i32)
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000481 return 0;
Chad Rosier67f96882011-11-04 22:29:00 +0000482
Eric Christopherc3e118e2010-09-02 23:43:26 +0000483 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000484 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000485 if (Align == 0) {
486 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000487 Align = DL.getTypeAllocSize(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000488 }
489 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000490 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000491 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000493 TII.get(ARM::t2LDRpci), ResultReg)
494 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000495 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000496 // The extra immediate is for addrmode2.
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000497 ResultReg = constrainOperandRegClass(TII.get(ARM::LDRcp), ResultReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000499 TII.get(ARM::LDRcp), ResultReg)
500 .addConstantPoolIndex(Idx)
501 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000502 }
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000503 return ResultReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000504}
505
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000506bool ARMFastISel::isPositionIndependent() const {
Rafael Espindolae7151722016-06-26 22:32:53 +0000507 return TLI.isPositionIndependent();
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000508}
509
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000510unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000511 // For now 32-bit only.
Tim Northoverbd41cf82016-01-07 09:03:03 +0000512 if (VT != MVT::i32 || GV->isThreadLocal()) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000513
Oliver Stannard8331aae2016-08-08 15:28:31 +0000514 // ROPI/RWPI not currently supported.
515 if (Subtarget->isROPI() || Subtarget->isRWPI())
516 return 0;
517
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +0000518 bool IsIndirect = Subtarget->isGVIndirectSymbol(GV);
Craig Topper61e88f42014-11-21 05:58:21 +0000519 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass
520 : &ARM::GPRRegClass;
Chad Rosier65710a72012-11-07 00:13:01 +0000521 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000522
Tim Northoverd6a729b2014-01-06 14:28:05 +0000523 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000524 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
525 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000526 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000527
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000528 bool IsPositionIndependent = isPositionIndependent();
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000529 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000530 // Non-darwin targets only support static movt relocations in FastISel.
Eric Christopherc1058df2014-07-04 01:55:26 +0000531 if (Subtarget->useMovt(*FuncInfo.MF) &&
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000532 (Subtarget->isTargetMachO() || !IsPositionIndependent)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000533 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000534 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000535 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000536 TF = ARMII::MO_NONLAZY;
537
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000538 if (IsPositionIndependent)
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000539 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
Rafael Espindola99357662016-06-20 17:00:13 +0000540 else
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000541 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000542 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
543 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000544 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000545 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000546 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000547 if (Align == 0) {
548 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000549 Align = DL.getTypeAllocSize(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000550 }
551
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000552 if (Subtarget->isTargetELF() && IsPositionIndependent)
Jush Lu47172a02012-09-27 05:21:41 +0000553 return ARMLowerPICELF(GV, Align, VT);
554
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000555 // Grab index.
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000556 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000557 unsigned Id = AFI->createPICLabelUId();
558 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
559 ARMCP::CPValue,
560 PCAdj);
561 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
562
563 // Load value.
564 MachineInstrBuilder MIB;
565 if (isThumb2) {
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000566 unsigned Opc = IsPositionIndependent ? ARM::t2LDRpci_pic : ARM::t2LDRpci;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000567 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
568 DestReg).addConstantPoolIndex(Idx);
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000569 if (IsPositionIndependent)
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000570 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000571 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000572 } else {
573 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000574 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000575 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
576 TII.get(ARM::LDRcp), DestReg)
577 .addConstantPoolIndex(Idx)
578 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000579 AddOptionalDefs(MIB);
580
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000581 if (IsPositionIndependent) {
Jush Lue87e5592012-08-29 02:41:21 +0000582 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
583 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
584
585 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000586 DbgLoc, TII.get(Opc), NewDestReg)
Jush Lue87e5592012-08-29 02:41:21 +0000587 .addReg(DestReg)
588 .addImm(Id);
589 AddOptionalDefs(MIB);
590 return NewDestReg;
591 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000592 }
Eric Christopher7787f792010-10-02 00:32:44 +0000593 }
Eli Friedman86585792011-06-03 01:13:19 +0000594
Jush Lue87e5592012-08-29 02:41:21 +0000595 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000596 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000597 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000598 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000599 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000600 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000601 .addReg(DestReg)
602 .addImm(0);
603 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000604 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
605 TII.get(ARM::LDRi12), NewDestReg)
606 .addReg(DestReg)
607 .addImm(0);
Eli Friedman86585792011-06-03 01:13:19 +0000608 DestReg = NewDestReg;
609 AddOptionalDefs(MIB);
610 }
611
Eric Christopher7787f792010-10-02 00:32:44 +0000612 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000613}
614
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000615unsigned ARMFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000616 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Patrik Hagglundc494d242012-12-17 14:30:06 +0000617
618 // Only handle simple types.
619 if (!CEVT.isSimple()) return 0;
620 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000621
622 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
623 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000624 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
625 return ARMMaterializeGV(GV, VT);
626 else if (isa<ConstantInt>(C))
627 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000628
Eric Christopher83a5ec82010-10-01 23:24:42 +0000629 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000630}
631
Chad Rosier0eff3e52011-11-17 21:46:13 +0000632// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
633
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000634unsigned ARMFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000635 // Don't handle dynamic allocas.
636 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000637
Duncan Sandsf5dda012010-11-03 11:35:31 +0000638 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000639 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000640
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000641 DenseMap<const AllocaInst*, int>::iterator SI =
642 FuncInfo.StaticAllocaMap.find(AI);
643
644 // This will get lowered later into the correct offsets and registers
645 // via rewriteXFrameIndex.
646 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000647 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000648 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000649 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000650 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
651
Rafael Espindolaea09c592014-02-18 22:05:46 +0000652 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000653 TII.get(Opc), ResultReg)
654 .addFrameIndex(SI->second)
655 .addImm(0));
656 return ResultReg;
657 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000658
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000659 return 0;
660}
661
Chris Lattner229907c2011-07-18 04:54:35 +0000662bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000663 EVT evt = TLI.getValueType(DL, Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000664
Eric Christopher761e7fb2010-08-25 07:23:49 +0000665 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000666 if (evt == MVT::Other || !evt.isSimple()) return false;
667 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000668
Eric Christopher901176a2010-08-31 01:28:42 +0000669 // Handle all legal types, i.e. a register that will directly hold this
670 // value.
671 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000672}
673
Chris Lattner229907c2011-07-18 04:54:35 +0000674bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000675 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000676
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000677 // If this is a type than can be sign or zero-extended to a basic operation
678 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000679 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000680 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000681
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000682 return false;
683}
684
Eric Christopher558b61e2010-11-19 22:36:41 +0000685// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000686bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000687 // Some boilerplate from the X86 FastISel.
Craig Topper062a2ba2014-04-25 05:30:21 +0000688 const User *U = nullptr;
Eric Christopher00202ee2010-08-23 21:44:12 +0000689 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000690 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000691 // Don't walk into other basic blocks unless the object is an alloca from
692 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000693 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
694 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
695 Opcode = I->getOpcode();
696 U = I;
697 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000698 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000699 Opcode = C->getOpcode();
700 U = C;
701 }
702
Chris Lattner229907c2011-07-18 04:54:35 +0000703 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000704 if (Ty->getAddressSpace() > 255)
705 // Fast instruction selection doesn't support the special
706 // address spaces.
707 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000708
Eric Christopher00202ee2010-08-23 21:44:12 +0000709 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000710 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000711 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000712 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000713 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000714 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000715 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000716 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000717 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
718 TLI.getPointerTy(DL))
Eric Christopherfef5f312010-11-19 22:30:02 +0000719 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000720 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000721 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000722 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000723 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Eric Christopherfef5f312010-11-19 22:30:02 +0000724 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000725 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000726 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000727 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000728 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000729
Eric Christopher21d0c172010-10-14 09:29:41 +0000730 // Iterate through the GEP folding the constants into offsets where
731 // we can.
732 gep_type_iterator GTI = gep_type_begin(U);
733 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
734 i != e; ++i, ++GTI) {
735 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000736 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000737 const StructLayout *SL = DL.getStructLayout(STy);
Eric Christopher21d0c172010-10-14 09:29:41 +0000738 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
739 TmpOffset += SL->getElementOffset(Idx);
740 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000741 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eric Christophera5a779e2011-03-22 19:39:17 +0000742 for (;;) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000743 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
744 // Constant-offset addressing.
745 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000746 break;
747 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000748 if (canFoldAddIntoGEP(U, Op)) {
749 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000750 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000751 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000752 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000753 // Iterate on the other operand.
754 Op = cast<AddOperator>(Op)->getOperand(0);
755 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000756 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000757 // Unsupported
758 goto unsupported_gep;
759 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000760 }
761 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000762
763 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000764 Addr.Offset = TmpOffset;
765 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000766
767 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000768 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000769
Eric Christopher21d0c172010-10-14 09:29:41 +0000770 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000771 break;
772 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000773 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000774 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000775 DenseMap<const AllocaInst*, int>::iterator SI =
776 FuncInfo.StaticAllocaMap.find(AI);
777 if (SI != FuncInfo.StaticAllocaMap.end()) {
778 Addr.BaseType = Address::FrameIndexBase;
779 Addr.Base.FI = SI->second;
780 return true;
781 }
782 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000783 }
784 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000785
Eric Christopher9d4e4712010-08-24 00:07:24 +0000786 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000787 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
788 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000789}
790
Chad Rosier150d35b2012-12-17 22:35:29 +0000791void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000792 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000793 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000794 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000795 case MVT::i1:
796 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000797 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000798 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000799 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000800 // Integer loads/stores handle 12-bit offsets.
801 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000802 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000803 if (needsLowering && isThumb2)
804 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
805 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000806 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000807 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000808 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000809 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000810 break;
811 case MVT::f32:
812 case MVT::f64:
813 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000814 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000815 break;
816 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000817
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000818 // If this is a stack pointer and the offset needs to be simplified then
819 // put the alloca address into a register, set the base type back to
820 // register and continue. This should almost never happen.
821 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topper61e88f42014-11-21 05:58:21 +0000822 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass
823 : &ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000824 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000825 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000826 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000827 TII.get(Opc), ResultReg)
828 .addFrameIndex(Addr.Base.FI)
829 .addImm(0));
830 Addr.Base.Reg = ResultReg;
831 Addr.BaseType = Address::RegBase;
832 }
833
Eric Christopher73bc5b02010-10-21 19:40:30 +0000834 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +0000835 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +0000836 if (needsLowering) {
Juergen Ributzka88e32512014-09-03 20:56:59 +0000837 Addr.Base.Reg = fastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
Eli Friedman86caced2011-04-29 21:22:56 +0000838 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +0000839 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +0000840 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000841}
842
Chad Rosier150d35b2012-12-17 22:35:29 +0000843void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000844 const MachineInstrBuilder &MIB,
Justin Lebar0af80cd2016-07-15 18:26:59 +0000845 MachineMemOperand::Flags Flags,
846 bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000847 // addrmode5 output depends on the selection dag addressing dividing the
848 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +0000849 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +0000850 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +0000851
Eric Christopher119ff7f2010-12-01 01:40:24 +0000852 // Frame base works a bit differently. Handle it separately.
853 if (Addr.BaseType == Address::FrameIndexBase) {
854 int FI = Addr.Base.FI;
855 int Offset = Addr.Offset;
Alex Lorenze40c8a22015-08-11 23:09:45 +0000856 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
857 MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
858 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
Eric Christopher119ff7f2010-12-01 01:40:24 +0000859 // Now add the rest of the operands.
860 MIB.addFrameIndex(FI);
861
Bob Wilson80381f62011-12-04 00:52:23 +0000862 // ARM halfword load/stores and signed byte loads need an additional
863 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000864 if (useAM3) {
David Majnemere61e4bf2016-06-21 05:10:24 +0000865 int Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
Chad Rosier2a1df882011-11-14 04:09:28 +0000866 MIB.addReg(0);
867 MIB.addImm(Imm);
868 } else {
869 MIB.addImm(Addr.Offset);
870 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000871 MIB.addMemOperand(MMO);
872 } else {
873 // Now add the rest of the operands.
874 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +0000875
Bob Wilson80381f62011-12-04 00:52:23 +0000876 // ARM halfword load/stores and signed byte loads need an additional
877 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000878 if (useAM3) {
David Majnemere61e4bf2016-06-21 05:10:24 +0000879 int Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
Chad Rosier2a1df882011-11-14 04:09:28 +0000880 MIB.addReg(0);
881 MIB.addImm(Imm);
882 } else {
883 MIB.addImm(Addr.Offset);
884 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000885 }
886 AddOptionalDefs(MIB);
887}
888
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000889bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +0000890 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +0000891 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000892 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +0000893 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +0000894 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000895 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000896 // This is mostly going to be Neon/vector support.
897 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +0000898 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000899 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +0000900 if (isThumb2) {
901 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
902 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
903 else
904 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000905 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +0000906 if (isZExt) {
907 Opc = ARM::LDRBi12;
908 } else {
909 Opc = ARM::LDRSB;
910 useAM3 = true;
911 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000912 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000913 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000914 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000915 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +0000916 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +0000917 return false;
918
Chad Rosieradfd2002011-11-14 20:22:27 +0000919 if (isThumb2) {
920 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
921 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
922 else
923 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
924 } else {
925 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
926 useAM3 = true;
927 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000928 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000929 break;
Eric Christopher901176a2010-08-31 01:28:42 +0000930 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +0000931 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +0000932 return false;
933
Chad Rosieradfd2002011-11-14 20:22:27 +0000934 if (isThumb2) {
935 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
936 Opc = ARM::t2LDRi8;
937 else
938 Opc = ARM::t2LDRi12;
939 } else {
940 Opc = ARM::LDRi12;
941 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000942 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +0000943 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +0000944 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +0000945 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +0000946 // Unaligned loads need special handling. Floats require word-alignment.
947 if (Alignment && Alignment < 4) {
948 needVMOV = true;
949 VT = MVT::i32;
950 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +0000951 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +0000952 } else {
953 Opc = ARM::VLDRS;
954 RC = TLI.getRegClassFor(VT);
955 }
Eric Christopheraef6499b2010-09-18 01:59:37 +0000956 break;
957 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +0000958 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +0000959 // FIXME: Unaligned loads need special handling. Doublewords require
960 // word-alignment.
961 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +0000962 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +0000963
Eric Christopheraef6499b2010-09-18 01:59:37 +0000964 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +0000965 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +0000966 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +0000967 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000968 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000969 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +0000970
Eric Christopher119ff7f2010-12-01 01:40:24 +0000971 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000972 if (allocReg)
973 ResultReg = createResultReg(RC);
974 assert (ResultReg > 255 && "Expected an allocated virtual register.");
Rafael Espindolaea09c592014-02-18 22:05:46 +0000975 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +0000976 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000977 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +0000978
979 // If we had an unaligned load of a float we've converted it to an regular
980 // load. Now we must move from the GRP to the FP register.
981 if (needVMOV) {
982 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000983 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier563de602011-12-13 19:22:14 +0000984 TII.get(ARM::VMOVSR), MoveReg)
985 .addReg(ResultReg));
986 ResultReg = MoveReg;
987 }
Eric Christopher901176a2010-08-31 01:28:42 +0000988 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +0000989}
990
Eric Christopher29ab6d12010-09-27 06:02:23 +0000991bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000992 // Atomic loads need special handling.
993 if (cast<LoadInst>(I)->isAtomic())
994 return false;
995
Manman Ren57518142016-04-11 21:08:06 +0000996 const Value *SV = I->getOperand(0);
997 if (TLI.supportSwiftError()) {
998 // Swifterror values can come from either a function parameter with
999 // swifterror attribute or an alloca with swifterror attribute.
1000 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1001 if (Arg->hasSwiftErrorAttr())
1002 return false;
1003 }
1004
1005 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1006 if (Alloca->isSwiftError())
1007 return false;
1008 }
1009 }
1010
Eric Christopher860fc932010-09-10 00:34:35 +00001011 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001012 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001013 if (!isLoadTypeLegal(I->getType(), VT))
1014 return false;
1015
Eric Christopher119ff7f2010-12-01 01:40:24 +00001016 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001017 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001018 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001019
1020 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001021 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1022 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001023 updateValueMap(I, ResultReg);
Eric Christopher860fc932010-09-10 00:34:35 +00001024 return true;
1025}
1026
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001027bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001028 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001029 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001030 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001031 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001032 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001033 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001034 case MVT::i1: {
Craig Topper61e88f42014-11-21 05:58:21 +00001035 unsigned Res = createResultReg(isThumb2 ? &ARM::tGPRRegClass
1036 : &ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001037 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001038 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001039 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher1e43892e2010-11-02 23:59:09 +00001040 TII.get(Opc), Res)
1041 .addReg(SrcReg).addImm(1));
1042 SrcReg = Res;
Justin Bognerb03fd122016-08-17 05:10:15 +00001043 LLVM_FALLTHROUGH;
1044 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001045 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001046 if (isThumb2) {
1047 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1048 StrOpc = ARM::t2STRBi8;
1049 else
1050 StrOpc = ARM::t2STRBi12;
1051 } else {
1052 StrOpc = ARM::STRBi12;
1053 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001054 break;
1055 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001056 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001057 return false;
1058
Chad Rosieradfd2002011-11-14 20:22:27 +00001059 if (isThumb2) {
1060 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1061 StrOpc = ARM::t2STRHi8;
1062 else
1063 StrOpc = ARM::t2STRHi12;
1064 } else {
1065 StrOpc = ARM::STRH;
1066 useAM3 = true;
1067 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001068 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001069 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001070 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001071 return false;
1072
Chad Rosieradfd2002011-11-14 20:22:27 +00001073 if (isThumb2) {
1074 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1075 StrOpc = ARM::t2STRi8;
1076 else
1077 StrOpc = ARM::t2STRi12;
1078 } else {
1079 StrOpc = ARM::STRi12;
1080 }
Eric Christopherc918d552010-10-16 01:10:35 +00001081 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001082 case MVT::f32:
1083 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001084 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001085 if (Alignment && Alignment < 4) {
1086 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001087 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosierec3b77e2011-12-03 02:21:57 +00001088 TII.get(ARM::VMOVRS), MoveReg)
1089 .addReg(SrcReg));
1090 SrcReg = MoveReg;
1091 VT = MVT::i32;
1092 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001093 } else {
1094 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001095 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001096 break;
1097 case MVT::f64:
1098 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001099 // FIXME: Unaligned stores need special handling. Doublewords require
1100 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001101 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001102 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001103
Eric Christopherc3e118e2010-09-02 23:43:26 +00001104 StrOpc = ARM::VSTRD;
1105 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001106 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001107 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001108 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001109
Eric Christopher119ff7f2010-12-01 01:40:24 +00001110 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001111 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001112 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001113 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001114 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001115 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001116 return true;
1117}
1118
Eric Christopher29ab6d12010-09-27 06:02:23 +00001119bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001120 Value *Op0 = I->getOperand(0);
1121 unsigned SrcReg = 0;
1122
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001123 // Atomic stores need special handling.
1124 if (cast<StoreInst>(I)->isAtomic())
1125 return false;
1126
Manman Ren57518142016-04-11 21:08:06 +00001127 const Value *PtrV = I->getOperand(1);
1128 if (TLI.supportSwiftError()) {
1129 // Swifterror values can come from either a function parameter with
1130 // swifterror attribute or an alloca with swifterror attribute.
1131 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1132 if (Arg->hasSwiftErrorAttr())
1133 return false;
1134 }
1135
1136 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1137 if (Alloca->isSwiftError())
1138 return false;
1139 }
1140 }
1141
Eric Christopher119ff7f2010-12-01 01:40:24 +00001142 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001143 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001144 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001145 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001146
Eric Christopher92db2012010-09-02 01:48:11 +00001147 // Get the value to be stored into a register.
1148 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001149 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001150
Eric Christopher119ff7f2010-12-01 01:40:24 +00001151 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001152 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001153 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001154 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001155
Chad Rosierec3b77e2011-12-03 02:21:57 +00001156 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1157 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001158 return true;
1159}
1160
1161static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1162 switch (Pred) {
1163 // Needs two compares...
1164 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001165 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001166 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001167 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001168 return ARMCC::AL;
1169 case CmpInst::ICMP_EQ:
1170 case CmpInst::FCMP_OEQ:
1171 return ARMCC::EQ;
1172 case CmpInst::ICMP_SGT:
1173 case CmpInst::FCMP_OGT:
1174 return ARMCC::GT;
1175 case CmpInst::ICMP_SGE:
1176 case CmpInst::FCMP_OGE:
1177 return ARMCC::GE;
1178 case CmpInst::ICMP_UGT:
1179 case CmpInst::FCMP_UGT:
1180 return ARMCC::HI;
1181 case CmpInst::FCMP_OLT:
1182 return ARMCC::MI;
1183 case CmpInst::ICMP_ULE:
1184 case CmpInst::FCMP_OLE:
1185 return ARMCC::LS;
1186 case CmpInst::FCMP_ORD:
1187 return ARMCC::VC;
1188 case CmpInst::FCMP_UNO:
1189 return ARMCC::VS;
1190 case CmpInst::FCMP_UGE:
1191 return ARMCC::PL;
1192 case CmpInst::ICMP_SLT:
1193 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001194 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001195 case CmpInst::ICMP_SLE:
1196 case CmpInst::FCMP_ULE:
1197 return ARMCC::LE;
1198 case CmpInst::FCMP_UNE:
1199 case CmpInst::ICMP_NE:
1200 return ARMCC::NE;
1201 case CmpInst::ICMP_UGE:
1202 return ARMCC::HS;
1203 case CmpInst::ICMP_ULT:
1204 return ARMCC::LO;
1205 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001206}
1207
Eric Christopher29ab6d12010-09-27 06:02:23 +00001208bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001209 const BranchInst *BI = cast<BranchInst>(I);
1210 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1211 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001212
Eric Christopher6aaed722010-09-03 00:35:47 +00001213 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001214
Eric Christopher5c308f82010-10-29 21:08:19 +00001215 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1216 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001217 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001218 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001219
1220 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001221 // Try to take advantage of fallthrough opportunities.
1222 CmpInst::Predicate Predicate = CI->getPredicate();
1223 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1224 std::swap(TBB, FBB);
1225 Predicate = CmpInst::getInversePredicate(Predicate);
1226 }
1227
1228 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001229
1230 // We may not handle every CC for now.
1231 if (ARMPred == ARMCC::AL) return false;
1232
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001233 // Emit the compare.
David Blaikie3ef249c92015-01-30 23:04:39 +00001234 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001235 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001236
Chad Rosier0439cfc2011-11-08 21:12:00 +00001237 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher5c308f82010-10-29 21:08:19 +00001239 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
Matthias Braunccfc9c82015-08-26 01:55:47 +00001240 finishCondBranch(BI->getParent(), TBB, FBB);
Eric Christopher5c308f82010-10-29 21:08:19 +00001241 return true;
1242 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001243 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1244 MVT SourceVT;
1245 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001246 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001247 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001248 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001249 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001250 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8d46b472011-04-29 20:02:39 +00001251 TII.get(TstOpc))
1252 .addReg(OpReg).addImm(1));
1253
1254 unsigned CCMode = ARMCC::NE;
1255 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1256 std::swap(TBB, FBB);
1257 CCMode = ARMCC::EQ;
1258 }
1259
Chad Rosier0439cfc2011-11-08 21:12:00 +00001260 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001261 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher8d46b472011-04-29 20:02:39 +00001262 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1263
Matthias Braunccfc9c82015-08-26 01:55:47 +00001264 finishCondBranch(BI->getParent(), TBB, FBB);
Eric Christopher8d46b472011-04-29 20:02:39 +00001265 return true;
1266 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001267 } else if (const ConstantInt *CI =
1268 dyn_cast<ConstantInt>(BI->getCondition())) {
1269 uint64_t Imm = CI->getZExtValue();
1270 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001271 fastEmitBranch(Target, DbgLoc);
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001272 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001273 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001274
Eric Christopher5c308f82010-10-29 21:08:19 +00001275 unsigned CmpReg = getRegForValue(BI->getCondition());
1276 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001277
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001278 // We've been divorced from our compare! Our block was split, and
1279 // now our compare lives in a predecessor block. We musn't
1280 // re-compare here, as the children of the compare aren't guaranteed
1281 // live across the block boundary (we *could* check for this).
1282 // Regardless, the compare has been done in the predecessor block,
1283 // and it left a value for us in a virtual register. Ergo, we test
1284 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001285 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001286 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001287 AddOptionalDefs(
1288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1289 .addReg(CmpReg)
1290 .addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001291
Eric Christopher4f012fd2011-04-28 16:52:09 +00001292 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 Christopher4f012fd2011-04-28 16:52:09 +00001300 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Matthias Braunccfc9c82015-08-26 01:55:47 +00001301 finishCondBranch(BI->getParent(), TBB, FBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001302 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001303}
1304
Chad Rosierded4c992012-02-07 23:56:08 +00001305bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1306 unsigned AddrReg = getRegForValue(I->getOperand(0));
1307 if (AddrReg == 0) return false;
1308
1309 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1311 TII.get(Opc)).addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001312
1313 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001314 for (const BasicBlock *SuccBB : IB->successors())
1315 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Wendling12cda502012-10-22 23:30:04 +00001316
Jush Luac96b762012-06-14 06:08:19 +00001317 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001318}
1319
Chad Rosier9cf803c2011-11-02 18:08:25 +00001320bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
David Blaikie3ef249c92015-01-30 23:04:39 +00001321 bool isZExt) {
Chad Rosier78127d32011-10-26 23:25:44 +00001322 Type *Ty = Src1Value->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +00001323 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001324 if (!SrcEVT.isSimple()) return false;
1325 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001326
Chad Rosier78127d32011-10-26 23:25:44 +00001327 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1328 if (isFloat && !Subtarget->hasVFP2())
Eric Christopherc3e9c402010-09-08 23:13:45 +00001329 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001330
Chad Rosier595d4192011-11-09 03:22:02 +00001331 // Check to see if the 2nd operand is a constant that we can encode directly
1332 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001333 int Imm = 0;
1334 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001335 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001336 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1337 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001338 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1339 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1340 SrcVT == MVT::i1) {
1341 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001342 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001343 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
Jim Grosbach1a597112014-04-03 23:43:18 +00001344 // then a cmn, because there is no way to represent 2147483648 as a
Chad Rosier26d05882012-03-15 22:54:20 +00001345 // signed 32-bit int.
1346 if (Imm < 0 && Imm != (int)0x80000000) {
1347 isNegativeImm = true;
1348 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001349 }
Chad Rosier26d05882012-03-15 22:54:20 +00001350 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1351 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001352 }
1353 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1354 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1355 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001356 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001357 }
1358
Eric Christopherc3e9c402010-09-08 23:13:45 +00001359 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001360 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001361 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001362 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001363 default: return false;
1364 // TODO: Verify compares.
1365 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001366 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001367 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001368 break;
1369 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001370 isICmp = false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001371 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001372 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001373 case MVT::i1:
1374 case MVT::i8:
1375 case MVT::i16:
1376 needsExt = true;
1377 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001378 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001379 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001380 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001381 CmpOpc = ARM::t2CMPrr;
1382 else
Bill Wendling4b796472012-06-11 08:07:26 +00001383 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001384 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001385 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001386 CmpOpc = ARM::CMPrr;
1387 else
Bill Wendling4b796472012-06-11 08:07:26 +00001388 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001389 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001390 break;
1391 }
1392
Chad Rosier9cf803c2011-11-02 18:08:25 +00001393 unsigned SrcReg1 = getRegForValue(Src1Value);
1394 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001395
Duncan Sands12330652011-11-28 10:31:27 +00001396 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001397 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001398 SrcReg2 = getRegForValue(Src2Value);
1399 if (SrcReg2 == 0) return false;
1400 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001401
1402 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1403 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001404 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1405 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001406 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001407 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1408 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001409 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001410 }
Chad Rosier59a20192011-10-26 22:47:55 +00001411
Jim Grosbachd7866792013-08-16 23:37:40 +00001412 const MCInstrDesc &II = TII.get(CmpOpc);
1413 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001414 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001415 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
David Blaikie3ef249c92015-01-30 23:04:39 +00001416 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001417 .addReg(SrcReg1).addReg(SrcReg2));
1418 } else {
1419 MachineInstrBuilder MIB;
David Blaikie3ef249c92015-01-30 23:04:39 +00001420 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001421 .addReg(SrcReg1);
1422
1423 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1424 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001425 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001426 AddOptionalDefs(MIB);
1427 }
Chad Rosier78127d32011-10-26 23:25:44 +00001428
1429 // For floating point we need to move the result to a comparison register
1430 // that we can then use for branches.
1431 if (Ty->isFloatTy() || Ty->isDoubleTy())
David Blaikie3ef249c92015-01-30 23:04:39 +00001432 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier78127d32011-10-26 23:25:44 +00001433 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001434 return true;
1435}
1436
1437bool ARMFastISel::SelectCmp(const Instruction *I) {
1438 const CmpInst *CI = cast<CmpInst>(I);
1439
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001440 // Get the compare predicate.
1441 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001442
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001443 // We may not handle every CC for now.
1444 if (ARMPred == ARMCC::AL) return false;
1445
Chad Rosier59a20192011-10-26 22:47:55 +00001446 // Emit the compare.
David Blaikie3ef249c92015-01-30 23:04:39 +00001447 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
Chad Rosier59a20192011-10-26 22:47:55 +00001448 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001449
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001450 // Now set a register based on the comparison. Explicitly set the predicates
1451 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001452 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topper61e88f42014-11-21 05:58:21 +00001453 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass
1454 : &ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001455 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001456 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001457 unsigned ZeroReg = fastMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001458 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001460 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001461 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001462
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001463 updateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001464 return true;
1465}
1466
Eric Christopher29ab6d12010-09-27 06:02:23 +00001467bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001468 // Make sure we have VFP and that we're extending float to double.
1469 if (!Subtarget->hasVFP2()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001470
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001471 Value *V = I->getOperand(0);
1472 if (!I->getType()->isDoubleTy() ||
1473 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001474
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001475 unsigned Op = getRegForValue(V);
1476 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001477
Craig Topperc7242e02012-04-20 07:30:17 +00001478 unsigned Result = createResultReg(&ARM::DPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001479 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001480 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001481 .addReg(Op));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001482 updateValueMap(I, Result);
Eric Christopher5903c0b2010-09-09 20:26:31 +00001483 return true;
1484}
1485
Eric Christopher29ab6d12010-09-27 06:02:23 +00001486bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001487 // Make sure we have VFP and that we're truncating double to float.
1488 if (!Subtarget->hasVFP2()) return false;
1489
1490 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001491 if (!(I->getType()->isFloatTy() &&
1492 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001493
1494 unsigned Op = getRegForValue(V);
1495 if (Op == 0) return false;
1496
Craig Topperc7242e02012-04-20 07:30:17 +00001497 unsigned Result = createResultReg(&ARM::SPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001499 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001500 .addReg(Op));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001501 updateValueMap(I, Result);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001502 return true;
1503}
1504
Chad Rosiere023d5d2012-02-03 21:14:11 +00001505bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001506 // Make sure we have VFP.
1507 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001508
Duncan Sandsf5dda012010-11-03 11:35:31 +00001509 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001510 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001511 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001512 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001513
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001514 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001515 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001516 if (!SrcEVT.isSimple())
1517 return false;
1518 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001519 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001520 return false;
1521
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001522 unsigned SrcReg = getRegForValue(Src);
1523 if (SrcReg == 0) return false;
1524
1525 // Handle sign-extension.
1526 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001527 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001528 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001529 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001530 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001531
Eric Christopher860fc932010-09-10 00:34:35 +00001532 // The conversion routine works on fp-reg to fp-reg and the operand above
1533 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001534 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001535 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001536
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001537 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001538 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1539 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001540 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001541
Eric Christopher4bd70472010-09-09 21:44:45 +00001542 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001543 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1544 TII.get(Opc), ResultReg).addReg(FP));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001545 updateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001546 return true;
1547}
1548
Chad Rosiere023d5d2012-02-03 21:14:11 +00001549bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001550 // Make sure we have VFP.
1551 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001552
Duncan Sandsf5dda012010-11-03 11:35:31 +00001553 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001554 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001555 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001556 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001557
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001558 unsigned Op = getRegForValue(I->getOperand(0));
1559 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001560
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001561 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001562 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001563 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1564 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001565 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001566
Chad Rosier41f0e782012-02-03 20:27:51 +00001567 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001568 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001569 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1570 TII.get(Opc), ResultReg).addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001571
Eric Christopher4bd70472010-09-09 21:44:45 +00001572 // This result needs to be in an integer register, but the conversion only
1573 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001574 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001575 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001576
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001577 updateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001578 return true;
1579}
1580
Eric Christopher511aa312010-10-11 08:27:59 +00001581bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001582 MVT VT;
1583 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001584 return false;
1585
1586 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001587 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001588
1589 unsigned CondReg = getRegForValue(I->getOperand(0));
1590 if (CondReg == 0) return false;
1591 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1592 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001593
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001594 // Check to see if we can use an immediate in the conditional move.
1595 int Imm = 0;
1596 bool UseImm = false;
1597 bool isNegativeImm = false;
1598 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1599 assert (VT == MVT::i32 && "Expecting an i32.");
1600 Imm = (int)ConstInt->getValue().getZExtValue();
1601 if (Imm < 0) {
1602 isNegativeImm = true;
1603 Imm = ~Imm;
1604 }
1605 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1606 (ARM_AM::getSOImmVal(Imm) != -1);
1607 }
1608
Duncan Sands12330652011-11-28 10:31:27 +00001609 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001610 if (!UseImm) {
1611 Op2Reg = getRegForValue(I->getOperand(2));
1612 if (Op2Reg == 0) return false;
1613 }
1614
Ahmed Bougachae8d0c4c2015-05-06 04:14:02 +00001615 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1616 CondReg = constrainOperandRegClass(TII.get(TstOpc), CondReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001617 AddOptionalDefs(
Ahmed Bougachae8d0c4c2015-05-06 04:14:02 +00001618 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001619 .addReg(CondReg)
Ahmed Bougachae8d0c4c2015-05-06 04:14:02 +00001620 .addImm(1));
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001621
1622 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001623 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001624 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001625 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001626 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1627 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001628 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1629 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001630 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001631 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001632 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001633 }
Eric Christopher511aa312010-10-11 08:27:59 +00001634 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001635 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001636 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001637 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001638 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1639 ResultReg)
1640 .addReg(Op2Reg)
1641 .addReg(Op1Reg)
1642 .addImm(ARMCC::NE)
1643 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001644 } else {
1645 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001646 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1647 ResultReg)
1648 .addReg(Op1Reg)
1649 .addImm(Imm)
1650 .addImm(ARMCC::EQ)
1651 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001652 }
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001653 updateValueMap(I, ResultReg);
Eric Christopher511aa312010-10-11 08:27:59 +00001654 return true;
1655}
1656
Chad Rosieraaa55a82012-02-03 21:07:27 +00001657bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001658 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001659 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001660 if (!isTypeLegal(Ty, VT))
1661 return false;
1662
1663 // If we have integer div support we should have selected this automagically.
1664 // In case we have a real miss go ahead and return false and we'll pick
1665 // it up later.
Eric Christopher7ac602b2010-10-11 08:38:55 +00001666 if (Subtarget->hasDivide()) return false;
1667
Eric Christopher56094ff2010-09-30 22:34:19 +00001668 // Otherwise emit a libcall.
1669 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001670 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001671 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001672 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001673 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001674 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001675 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001676 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001677 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001678 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001679 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001680 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001681
Eric Christopher56094ff2010-09-30 22:34:19 +00001682 return ARMEmitLibcall(I, LC);
1683}
1684
Chad Rosierb84a4b42012-02-03 21:23:45 +00001685bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001686 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001687 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001688 if (!isTypeLegal(Ty, VT))
1689 return false;
1690
Diana Picus774d1572016-07-18 06:48:25 +00001691 // Many ABIs do not provide a libcall for standalone remainder, so we need to
1692 // use divrem (see the RTABI 4.3.1). Since FastISel can't handle non-double
1693 // multi-reg returns, we'll have to bail out.
1694 if (!TLI.hasStandaloneRem(VT)) {
1695 return false;
1696 }
1697
Eric Christophereae1b382010-10-11 08:37:26 +00001698 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1699 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001700 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001701 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001702 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001703 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001704 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001705 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001706 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001707 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001708 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001709 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001710
Eric Christophereae1b382010-10-11 08:37:26 +00001711 return ARMEmitLibcall(I, LC);
1712}
1713
Chad Rosier685b20c2012-02-06 23:50:07 +00001714bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001715 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Chad Rosier685b20c2012-02-06 23:50:07 +00001716
1717 // We can get here in the case when we have a binary operation on a non-legal
1718 // type and the target independent selector doesn't know how to handle it.
1719 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1720 return false;
Jush Luac96b762012-06-14 06:08:19 +00001721
Chad Rosierbd471252012-02-08 02:29:21 +00001722 unsigned Opc;
1723 switch (ISDOpcode) {
1724 default: return false;
1725 case ISD::ADD:
1726 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1727 break;
1728 case ISD::OR:
1729 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1730 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001731 case ISD::SUB:
1732 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1733 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001734 }
1735
Chad Rosier685b20c2012-02-06 23:50:07 +00001736 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1737 if (SrcReg1 == 0) return false;
1738
1739 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1740 // in the instruction, rather then materializing the value in a register.
1741 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1742 if (SrcReg2 == 0) return false;
1743
JF Bastien13969d02013-05-29 15:45:47 +00001744 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001745 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1746 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001747 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier685b20c2012-02-06 23:50:07 +00001748 TII.get(Opc), ResultReg)
1749 .addReg(SrcReg1).addReg(SrcReg2));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001750 updateValueMap(I, ResultReg);
Chad Rosier685b20c2012-02-06 23:50:07 +00001751 return true;
1752}
1753
1754bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001755 EVT FPVT = TLI.getValueType(DL, I->getType(), true);
Chad Rosier62a144f2012-12-17 19:59:43 +00001756 if (!FPVT.isSimple()) return false;
1757 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001758
Pete Cooperd927c6e2015-05-06 16:39:17 +00001759 // FIXME: Support vector types where possible.
1760 if (VT.isVector())
1761 return false;
1762
Eric Christopher24dc27f2010-09-09 00:53:57 +00001763 // We can get here in the case when we want to use NEON for our fp
1764 // operations, but can't figure out how to. Just use the vfp instructions
1765 // if we have them.
1766 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001767 Type *Ty = I->getType();
Eric Christopherbd3d1212010-09-09 01:02:03 +00001768 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1769 if (isFloat && !Subtarget->hasVFP2())
1770 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001771
Eric Christopher24dc27f2010-09-09 00:53:57 +00001772 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001773 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001774 switch (ISDOpcode) {
1775 default: return false;
1776 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001777 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001778 break;
1779 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001780 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001781 break;
1782 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001783 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001784 break;
1785 }
Chad Rosier80979b62011-11-16 18:39:44 +00001786 unsigned Op1 = getRegForValue(I->getOperand(0));
1787 if (Op1 == 0) return false;
1788
1789 unsigned Op2 = getRegForValue(I->getOperand(1));
1790 if (Op2 == 0) return false;
1791
Chad Rosier62a144f2012-12-17 19:59:43 +00001792 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001793 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher24dc27f2010-09-09 00:53:57 +00001794 TII.get(Opc), ResultReg)
1795 .addReg(Op1).addReg(Op2));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001796 updateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001797 return true;
1798}
1799
Eric Christopher72497e52010-09-10 23:18:12 +00001800// Call Handling Code
1801
Jush Lue67e07b2012-07-19 09:49:00 +00001802// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001803// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001804CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1805 bool Return,
1806 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001807 switch (CC) {
1808 default:
1809 llvm_unreachable("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001810 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001811 if (Subtarget->hasVFP2() && !isVarArg) {
1812 if (!Subtarget->isAAPCS_ABI())
1813 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1814 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1815 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1816 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001817 LLVM_FALLTHROUGH;
Evan Cheng21abfc92010-10-22 18:57:05 +00001818 case CallingConv::C:
Manman Ren2828c572016-03-18 23:38:49 +00001819 case CallingConv::CXX_FAST_TLS:
Eric Christopher72497e52010-09-10 23:18:12 +00001820 // Use target triple & subtarget features to do actual dispatch.
1821 if (Subtarget->isAAPCS_ABI()) {
1822 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001823 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001824 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1825 else
1826 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Bob Wilson8823b842015-09-19 06:20:59 +00001827 } else {
1828 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1829 }
Eric Christopher72497e52010-09-10 23:18:12 +00001830 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001831 case CallingConv::Swift:
Jush Lue67e07b2012-07-19 09:49:00 +00001832 if (!isVarArg)
1833 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1834 // Fall through to soft float variant, variadic functions don't
1835 // use hard floating point ABI.
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001836 LLVM_FALLTHROUGH;
Eric Christopher72497e52010-09-10 23:18:12 +00001837 case CallingConv::ARM_AAPCS:
1838 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1839 case CallingConv::ARM_APCS:
1840 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001841 case CallingConv::GHC:
1842 if (Return)
1843 llvm_unreachable("Can't return in GHC call convention");
1844 else
1845 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001846 }
1847}
1848
Eric Christopher79398062010-09-29 23:11:09 +00001849bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1850 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001851 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001852 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1853 SmallVectorImpl<unsigned> &RegArgs,
1854 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001855 unsigned &NumBytes,
1856 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001857 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001858 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00001859 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1860 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001861
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001862 // Check that we can handle all of the arguments. If we can't, then bail out
1863 // now before we add code to the MBB.
1864 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1865 CCValAssign &VA = ArgLocs[i];
1866 MVT ArgVT = ArgVTs[VA.getValNo()];
1867
1868 // We don't handle NEON/vector parameters yet.
1869 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1870 return false;
1871
1872 // Now copy/store arg to correct locations.
1873 if (VA.isRegLoc() && !VA.needsCustom()) {
1874 continue;
1875 } else if (VA.needsCustom()) {
1876 // TODO: We need custom lowering for vector (v2f64) args.
1877 if (VA.getLocVT() != MVT::f64 ||
1878 // TODO: Only handle register args for now.
1879 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1880 return false;
1881 } else {
Craig Topper56710102013-08-15 02:33:50 +00001882 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001883 default:
1884 return false;
1885 case MVT::i1:
1886 case MVT::i8:
1887 case MVT::i16:
1888 case MVT::i32:
1889 break;
1890 case MVT::f32:
1891 if (!Subtarget->hasVFP2())
1892 return false;
1893 break;
1894 case MVT::f64:
1895 if (!Subtarget->hasVFP2())
1896 return false;
1897 break;
1898 }
1899 }
1900 }
1901
1902 // At the point, we are able to handle the call's arguments in fast isel.
1903
Eric Christopher79398062010-09-29 23:11:09 +00001904 // Get a count of how many bytes are to be pushed on the stack.
1905 NumBytes = CCInfo.getNextStackOffset();
1906
1907 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00001908 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001909 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00001910 TII.get(AdjStackDown))
1911 .addImm(NumBytes));
Eric Christopher79398062010-09-29 23:11:09 +00001912
1913 // Process the args.
1914 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1915 CCValAssign &VA = ArgLocs[i];
Juergen Ributzka4c018a12014-08-01 18:04:14 +00001916 const Value *ArgVal = Args[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001917 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00001918 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001919
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001920 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1921 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00001922
Eric Christopher78f8d4e2010-09-30 20:49:44 +00001923 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00001924 switch (VA.getLocInfo()) {
1925 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00001926 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001927 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001928 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1929 assert (Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001930 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001931 break;
1932 }
Chad Rosierd0191a52011-11-05 20:16:15 +00001933 case CCValAssign::AExt:
1934 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00001935 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001936 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001937 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
JF Bastien06ce03d2013-06-07 20:10:37 +00001938 assert (Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001939 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001940 break;
1941 }
1942 case CCValAssign::BCvt: {
Juergen Ributzka88e32512014-09-03 20:56:59 +00001943 unsigned BC = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001944 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00001945 assert(BC != 0 && "Failed to emit a bitcast!");
1946 Arg = BC;
1947 ArgVT = VA.getLocVT();
1948 break;
1949 }
1950 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00001951 }
1952
1953 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00001954 if (VA.isRegLoc() && !VA.needsCustom()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001955 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1956 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00001957 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001958 } else if (VA.needsCustom()) {
1959 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001960 assert(VA.getLocVT() == MVT::f64 &&
1961 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00001962
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001963 CCValAssign &NextVA = ArgLocs[++i];
1964
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001965 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
1966 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001967
Rafael Espindolaea09c592014-02-18 22:05:46 +00001968 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher4ac3ed02010-10-21 00:01:47 +00001969 TII.get(ARM::VMOVRRD), VA.getLocReg())
1970 .addReg(NextVA.getLocReg(), RegState::Define)
1971 .addReg(Arg));
1972 RegArgs.push_back(VA.getLocReg());
1973 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00001974 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00001975 assert(VA.isMemLoc());
1976 // Need to store on the stack.
Juergen Ributzka4c018a12014-08-01 18:04:14 +00001977
1978 // Don't emit stores for undef values.
1979 if (isa<UndefValue>(ArgVal))
1980 continue;
1981
Eric Christopherfef5f312010-11-19 22:30:02 +00001982 Address Addr;
1983 Addr.BaseType = Address::RegBase;
1984 Addr.Base.Reg = ARM::SP;
1985 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00001986
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001987 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
1988 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00001989 }
1990 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001991
Eric Christopher79398062010-09-29 23:11:09 +00001992 return true;
1993}
1994
Duncan Sandsf5dda012010-11-03 11:35:31 +00001995bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00001996 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001997 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001998 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00001999 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002000 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002001 TII.get(AdjStackUp))
2002 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002003
2004 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002005 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002006 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002007 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002008 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002009
2010 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002011 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002012 // For this move we copy into two registers and then move into the
2013 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002014 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002015 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002016 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002017 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopherc1e209d2010-10-01 00:00:11 +00002018 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002019 .addReg(RVLocs[0].getLocReg())
2020 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002021
Eric Christopheraf719ef2010-10-20 08:02:24 +00002022 UsedRegs.push_back(RVLocs[0].getLocReg());
2023 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002024
Eric Christopher7ac602b2010-10-11 08:38:55 +00002025 // Finally update the result.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002026 updateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002027 } else {
2028 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002029 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002030
2031 // Special handling for extended integers.
2032 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2033 CopyVT = MVT::i32;
2034
Craig Topper760b1342012-02-22 05:59:10 +00002035 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002036
Eric Christopherc1e209d2010-10-01 00:00:11 +00002037 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2039 TII.get(TargetOpcode::COPY),
Eric Christopherc1e209d2010-10-01 00:00:11 +00002040 ResultReg).addReg(RVLocs[0].getLocReg());
2041 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002042
Eric Christopher7ac602b2010-10-11 08:38:55 +00002043 // Finally update the result.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002044 updateValueMap(I, ResultReg);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002045 }
Eric Christopher79398062010-09-29 23:11:09 +00002046 }
2047
Eric Christopher7ac602b2010-10-11 08:38:55 +00002048 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002049}
2050
Eric Christopher93bbe652010-10-22 01:28:00 +00002051bool ARMFastISel::SelectRet(const Instruction *I) {
2052 const ReturnInst *Ret = cast<ReturnInst>(I);
2053 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002054
Eric Christopher93bbe652010-10-22 01:28:00 +00002055 if (!FuncInfo.CanLowerReturn)
2056 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002057
Manman Ren57518142016-04-11 21:08:06 +00002058 if (TLI.supportSwiftError() &&
2059 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2060 return false;
2061
Manman Ren5e9e65e2016-01-12 00:47:18 +00002062 if (TLI.supportSplitCSR(FuncInfo.MF))
2063 return false;
2064
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002065 // Build a list of return value registers.
2066 SmallVector<unsigned, 4> RetRegs;
2067
Eric Christopher93bbe652010-10-22 01:28:00 +00002068 CallingConv::ID CC = F.getCallingConv();
2069 if (Ret->getNumOperands() > 0) {
2070 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00002071 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Eric Christopher93bbe652010-10-22 01:28:00 +00002072
2073 // Analyze operands of the call, assigning locations to each operand.
2074 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002075 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002076 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2077 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002078
2079 const Value *RV = Ret->getOperand(0);
2080 unsigned Reg = getRegForValue(RV);
2081 if (Reg == 0)
2082 return false;
2083
2084 // Only handle a single return value for now.
2085 if (ValLocs.size() != 1)
2086 return false;
2087
2088 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002089
Eric Christopher93bbe652010-10-22 01:28:00 +00002090 // Don't bother handling odd stuff for now.
2091 if (VA.getLocInfo() != CCValAssign::Full)
2092 return false;
2093 // Only handle register returns for now.
2094 if (!VA.isRegLoc())
2095 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002096
2097 unsigned SrcReg = Reg + VA.getValNo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002098 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Chad Rosier62a144f2012-12-17 19:59:43 +00002099 if (!RVEVT.isSimple()) return false;
2100 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002101 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002102 // Special handling for extended integers.
2103 if (RVVT != DestVT) {
2104 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2105 return false;
2106
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002107 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2108
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002109 // Perform extension if flagged as either zext or sext. Otherwise, do
2110 // nothing.
2111 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2112 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2113 if (SrcReg == 0) return false;
2114 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002115 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002116
Eric Christopher93bbe652010-10-22 01:28:00 +00002117 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002118 unsigned DstReg = VA.getLocReg();
2119 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2120 // Avoid a cross-class copy. This is very unlikely.
2121 if (!SrcRC->contains(DstReg))
2122 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002123 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2124 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
Eric Christopher93bbe652010-10-22 01:28:00 +00002125
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002126 // Add register to return instruction.
2127 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002128 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002129
Chad Rosier0439cfc2011-11-08 21:12:00 +00002130 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002131 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002132 TII.get(RetOpc));
2133 AddOptionalDefs(MIB);
2134 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2135 MIB.addReg(RetRegs[i], RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002136 return true;
2137}
2138
Chad Rosierc6916f82012-06-12 19:25:13 +00002139unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2140 if (UseReg)
2141 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2142 else
2143 return isThumb2 ? ARM::tBL : ARM::BL;
2144}
2145
2146unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002147 // Manually compute the global's type to avoid building it when unnecessary.
2148 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
Mehdi Amini44ede332015-07-09 02:09:04 +00002149 EVT LCREVT = TLI.getValueType(DL, GVTy);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002150 if (!LCREVT.isSimple()) return 0;
2151
Bill Wendling76cce192013-12-29 08:00:04 +00002152 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Craig Topper062a2ba2014-04-25 05:30:21 +00002153 GlobalValue::ExternalLinkage, nullptr,
2154 Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002155 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002156 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002157}
2158
Eric Christopher8b912662010-09-14 23:03:37 +00002159// A quick function that will emit a call for a named libcall in F with the
2160// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002161// can emit a call for any libcall we can produce. This is an abridged version
2162// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002163// like computed function pointers or strange arguments at call sites.
2164// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2165// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002166bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2167 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002168
Eric Christopher8b912662010-09-14 23:03:37 +00002169 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002170 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002171 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002172 if (RetTy->isVoidTy())
2173 RetVT = MVT::isVoid;
2174 else if (!isTypeLegal(RetTy, RetVT))
2175 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002176
Chad Rosier90f9afe2012-05-11 18:51:55 +00002177 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002178 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002179 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002180 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002181 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002182 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2183 return false;
2184 }
2185
Eric Christopher79398062010-09-29 23:11:09 +00002186 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002187 SmallVector<Value*, 8> Args;
2188 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002189 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002190 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2191 Args.reserve(I->getNumOperands());
2192 ArgRegs.reserve(I->getNumOperands());
2193 ArgVTs.reserve(I->getNumOperands());
2194 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7990df12010-09-28 01:21:42 +00002195 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopher8b912662010-09-14 23:03:37 +00002196 Value *Op = I->getOperand(i);
2197 unsigned Arg = getRegForValue(Op);
2198 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002199
Chris Lattner229907c2011-07-18 04:54:35 +00002200 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002201 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002202 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002203
Eric Christopher8b912662010-09-14 23:03:37 +00002204 ISD::ArgFlagsTy Flags;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002205 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher8b912662010-09-14 23:03:37 +00002206 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002207
Eric Christopher8b912662010-09-14 23:03:37 +00002208 Args.push_back(Op);
2209 ArgRegs.push_back(Arg);
2210 ArgVTs.push_back(ArgVT);
2211 ArgFlags.push_back(Flags);
2212 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002213
Eric Christopher79398062010-09-29 23:11:09 +00002214 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002215 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002216 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002217 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2218 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002219 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002220
Chad Rosierc6916f82012-06-12 19:25:13 +00002221 unsigned CalleeReg = 0;
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002222 if (Subtarget->genLongCalls()) {
Chad Rosierc6916f82012-06-12 19:25:13 +00002223 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2224 if (CalleeReg == 0) return false;
2225 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002226
Chad Rosierc6916f82012-06-12 19:25:13 +00002227 // Issue the call.
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002228 unsigned CallOpc = ARMSelectCallOp(Subtarget->genLongCalls());
Chad Rosierc6916f82012-06-12 19:25:13 +00002229 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002230 DbgLoc, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002231 // BL / BLX don't take a predicate, but tBL / tBLX do.
2232 if (isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002233 AddDefaultPred(MIB);
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002234 if (Subtarget->genLongCalls())
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002235 MIB.addReg(CalleeReg);
2236 else
2237 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002238
Eric Christopher8b912662010-09-14 23:03:37 +00002239 // Add implicit physical register uses to the call.
2240 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002241 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002242
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002243 // Add a register mask with the call-preserved registers.
2244 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00002245 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002246
Eric Christopher79398062010-09-29 23:11:09 +00002247 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002248 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002249 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002250
Eric Christopher8b912662010-09-14 23:03:37 +00002251 // Set all unused physreg defs as dead.
2252 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002253
Eric Christopher8b912662010-09-14 23:03:37 +00002254 return true;
2255}
2256
Chad Rosiera7ebc562011-11-11 23:31:03 +00002257bool ARMFastISel::SelectCall(const Instruction *I,
Craig Topper062a2ba2014-04-25 05:30:21 +00002258 const char *IntrMemName = nullptr) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002259 const CallInst *CI = cast<CallInst>(I);
2260 const Value *Callee = CI->getCalledValue();
2261
Chad Rosiera7ebc562011-11-11 23:31:03 +00002262 // Can't handle inline asm.
2263 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002264
Chad Rosierdf42cf32012-12-11 00:18:02 +00002265 // Allow SelectionDAG isel to handle tail calls.
2266 if (CI->isTailCall()) return false;
2267
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002268 // Check the calling convention.
2269 ImmutableCallSite CS(CI);
2270 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002271
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002272 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002273
Manuel Jacob190577a2016-01-17 22:37:39 +00002274 FunctionType *FTy = CS.getFunctionType();
Jush Lue67e07b2012-07-19 09:49:00 +00002275 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002276
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002277 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002278 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002279 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002280 if (RetTy->isVoidTy())
2281 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002282 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2283 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002284 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002285
Chad Rosier90f9afe2012-05-11 18:51:55 +00002286 // Can't handle non-double multi-reg retvals.
2287 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2288 RetVT != MVT::i16 && RetVT != MVT::i32) {
2289 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002290 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002291 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002292 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2293 return false;
2294 }
2295
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002296 // Set up the argument vectors.
2297 SmallVector<Value*, 8> Args;
2298 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002299 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002300 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002301 unsigned arg_size = CS.arg_size();
2302 Args.reserve(arg_size);
2303 ArgRegs.reserve(arg_size);
2304 ArgVTs.reserve(arg_size);
2305 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002306 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2307 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002308 // If we're lowering a memory intrinsic instead of a regular call, skip the
Pete Cooper67cf9a72015-11-19 05:56:52 +00002309 // last two arguments, which shouldn't be passed to the underlying function.
2310 if (IntrMemName && e-i <= 2)
Chad Rosiera7ebc562011-11-11 23:31:03 +00002311 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002312
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002313 ISD::ArgFlagsTy Flags;
2314 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002315 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002316 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002317 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002318 Flags.setZExt();
2319
Chad Rosier8a98ec42011-11-04 00:58:10 +00002320 // FIXME: Only handle *easy* calls for now.
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002321 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2322 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
Manman Renf46262e2016-03-29 17:37:21 +00002323 CS.paramHasAttr(AttrInd, Attribute::SwiftSelf) ||
Manman Ren57518142016-04-11 21:08:06 +00002324 CS.paramHasAttr(AttrInd, Attribute::SwiftError) ||
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002325 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2326 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002327 return false;
2328
Chris Lattner229907c2011-07-18 04:54:35 +00002329 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002330 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002331 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2332 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002333 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002334
2335 unsigned Arg = getRegForValue(*i);
2336 if (Arg == 0)
2337 return false;
2338
Rafael Espindolaea09c592014-02-18 22:05:46 +00002339 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002340 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002341
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002342 Args.push_back(*i);
2343 ArgRegs.push_back(Arg);
2344 ArgVTs.push_back(ArgVT);
2345 ArgFlags.push_back(Flags);
2346 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002347
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002348 // Handle the arguments now that we've gotten them.
2349 SmallVector<unsigned, 4> RegArgs;
2350 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002351 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2352 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002353 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002354
Chad Rosierc6916f82012-06-12 19:25:13 +00002355 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002356 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002357 if (!GV || Subtarget->genLongCalls()) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002358
Chad Rosierc6916f82012-06-12 19:25:13 +00002359 unsigned CalleeReg = 0;
2360 if (UseReg) {
2361 if (IntrMemName)
2362 CalleeReg = getLibcallReg(IntrMemName);
2363 else
2364 CalleeReg = getRegForValue(Callee);
2365
Chad Rosier223faf72012-05-23 18:38:57 +00002366 if (CalleeReg == 0) return false;
2367 }
2368
Chad Rosierc6916f82012-06-12 19:25:13 +00002369 // Issue the call.
2370 unsigned CallOpc = ARMSelectCallOp(UseReg);
2371 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002372 DbgLoc, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002373
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002374 // ARM calls don't take a predicate, but tBL / tBLX do.
2375 if(isThumb2)
Chad Rosierc6916f82012-06-12 19:25:13 +00002376 AddDefaultPred(MIB);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002377 if (UseReg)
2378 MIB.addReg(CalleeReg);
2379 else if (!IntrMemName)
Rafael Espindolaafade352016-06-16 16:09:53 +00002380 MIB.addGlobalAddress(GV, 0, 0);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002381 else
Rafael Espindolaafade352016-06-16 16:09:53 +00002382 MIB.addExternalSymbol(IntrMemName, 0);
Jush Luac96b762012-06-14 06:08:19 +00002383
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002384 // Add implicit physical register uses to the call.
2385 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002386 MIB.addReg(RegArgs[i], RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002387
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002388 // Add a register mask with the call-preserved registers.
2389 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00002390 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002391
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002392 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002393 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002394 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2395 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002396
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002397 // Set all unused physreg defs as dead.
2398 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002399
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002400 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002401}
2402
Chad Rosier057b6d32011-11-14 23:04:09 +00002403bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002404 return Len <= 16;
2405}
2406
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002407bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002408 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002409 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002410 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002411 return false;
2412
Chad Rosierab7223e2011-11-14 22:46:17 +00002413 while (Len) {
2414 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002415 if (!Alignment || Alignment >= 4) {
2416 if (Len >= 4)
2417 VT = MVT::i32;
2418 else if (Len >= 2)
2419 VT = MVT::i16;
2420 else {
2421 assert (Len == 1 && "Expected a length of 1!");
2422 VT = MVT::i8;
2423 }
2424 } else {
2425 // Bound based on alignment.
2426 if (Len >= 2 && Alignment == 2)
2427 VT = MVT::i16;
2428 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002429 VT = MVT::i8;
2430 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002431 }
2432
2433 bool RV;
2434 unsigned ResultReg;
2435 RV = ARMEmitLoad(VT, ResultReg, Src);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002436 assert (RV == true && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002437 RV = ARMEmitStore(VT, ResultReg, Dest);
Eric Christopherd284c1d2012-01-11 20:55:27 +00002438 assert (RV == true && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002439 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002440
2441 unsigned Size = VT.getSizeInBits()/8;
2442 Len -= Size;
2443 Dest.Offset += Size;
2444 Src.Offset += Size;
2445 }
2446
2447 return true;
2448}
2449
Chad Rosiera7ebc562011-11-11 23:31:03 +00002450bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2451 // FIXME: Handle more intrinsics.
2452 switch (I.getIntrinsicID()) {
2453 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002454 case Intrinsic::frameaddress: {
Matthias Braun941a7052016-07-28 18:40:00 +00002455 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
2456 MFI.setFrameAddressIsTaken(true);
Chad Rosier820d248c2012-05-30 17:23:22 +00002457
Craig Topper61e88f42014-11-21 05:58:21 +00002458 unsigned LdrOpc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
2459 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass
2460 : &ARM::GPRRegClass;
Chad Rosier820d248c2012-05-30 17:23:22 +00002461
2462 const ARMBaseRegisterInfo *RegInfo =
Eric Christopher1b21f002015-01-29 00:19:33 +00002463 static_cast<const ARMBaseRegisterInfo *>(Subtarget->getRegisterInfo());
Chad Rosier820d248c2012-05-30 17:23:22 +00002464 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2465 unsigned SrcReg = FramePtr;
2466
2467 // Recursively load frame address
2468 // ldr r0 [fp]
2469 // ldr r0 [r0]
2470 // ldr r0 [r0]
2471 // ...
2472 unsigned DestReg;
2473 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2474 while (Depth--) {
2475 DestReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier820d248c2012-05-30 17:23:22 +00002477 TII.get(LdrOpc), DestReg)
2478 .addReg(SrcReg).addImm(0));
2479 SrcReg = DestReg;
2480 }
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002481 updateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002482 return true;
2483 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002484 case Intrinsic::memcpy:
2485 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002486 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2487 // Don't handle volatile.
2488 if (MTI.isVolatile())
2489 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002490
2491 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2492 // we would emit dead code because we don't currently handle memmoves.
2493 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2494 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002495 // Small memcpy's are common enough that we want to do them without a call
2496 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002497 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002498 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002499 Address Dest, Src;
2500 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2501 !ARMComputeAddress(MTI.getRawSource(), Src))
2502 return false;
Pete Cooper67cf9a72015-11-19 05:56:52 +00002503 unsigned Alignment = MTI.getAlignment();
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002504 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002505 return true;
2506 }
2507 }
Jush Luac96b762012-06-14 06:08:19 +00002508
Chad Rosiera7ebc562011-11-11 23:31:03 +00002509 if (!MTI.getLength()->getType()->isIntegerTy(32))
2510 return false;
Jush Luac96b762012-06-14 06:08:19 +00002511
Chad Rosiera7ebc562011-11-11 23:31:03 +00002512 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2513 return false;
2514
2515 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2516 return SelectCall(&I, IntrMemName);
2517 }
2518 case Intrinsic::memset: {
2519 const MemSetInst &MSI = cast<MemSetInst>(I);
2520 // Don't handle volatile.
2521 if (MSI.isVolatile())
2522 return false;
Jush Luac96b762012-06-14 06:08:19 +00002523
Chad Rosiera7ebc562011-11-11 23:31:03 +00002524 if (!MSI.getLength()->getType()->isIntegerTy(32))
2525 return false;
Jush Luac96b762012-06-14 06:08:19 +00002526
Chad Rosiera7ebc562011-11-11 23:31:03 +00002527 if (MSI.getDestAddressSpace() > 255)
2528 return false;
Jush Luac96b762012-06-14 06:08:19 +00002529
Chad Rosiera7ebc562011-11-11 23:31:03 +00002530 return SelectCall(&I, "memset");
2531 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002532 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002533 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002534 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002535 return true;
2536 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002537 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002538}
2539
Chad Rosieree7e4522011-11-02 00:18:48 +00002540bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002541 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002542 // undefined.
2543 Value *Op = I->getOperand(0);
2544
2545 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00002546 SrcVT = TLI.getValueType(DL, Op->getType(), true);
2547 DestVT = TLI.getValueType(DL, I->getType(), true);
Chad Rosieree7e4522011-11-02 00:18:48 +00002548
2549 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2550 return false;
2551 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2552 return false;
2553
2554 unsigned SrcReg = getRegForValue(Op);
2555 if (!SrcReg) return false;
2556
2557 // Because the high bits are undefined, a truncate doesn't generate
2558 // any code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002559 updateValueMap(I, SrcReg);
Chad Rosieree7e4522011-11-02 00:18:48 +00002560 return true;
2561}
2562
Chad Rosier62a144f2012-12-17 19:59:43 +00002563unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002564 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002565 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002566 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002567 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002568 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002569
2570 // Table of which combinations can be emitted as a single instruction,
2571 // and which will require two.
2572 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2573 // ARM Thumb
2574 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2575 // ext: s z s z s z s z
2576 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2577 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2578 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2579 };
2580
2581 // Target registers for:
2582 // - For ARM can never be PC.
2583 // - For 16-bit Thumb are restricted to lower 8 registers.
2584 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2585 static const TargetRegisterClass *RCTbl[2][2] = {
2586 // Instructions: Two Single
2587 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2588 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2589 };
2590
2591 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002592 static const struct InstructionTable {
2593 uint32_t Opc : 16;
2594 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2595 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2596 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2597 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002598 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002599 { // ARM Opc S Shift Imm
2600 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2601 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2602 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2603 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2604 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2605 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002606 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002607 { // Thumb Opc S Shift Imm
2608 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2609 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2610 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2611 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2612 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2613 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002614 }
2615 },
2616 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002617 { // ARM Opc S Shift Imm
2618 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2619 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2620 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2621 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2622 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2623 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002624 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002625 { // Thumb Opc S Shift Imm
2626 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2627 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2628 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2629 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2630 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2631 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002632 }
2633 }
2634 };
2635
2636 unsigned SrcBits = SrcVT.getSizeInBits();
2637 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002638 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002639 assert((SrcBits < DestBits) && "can only extend to larger types");
2640 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2641 "other sizes unimplemented");
2642 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2643 "other sizes unimplemented");
2644
2645 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002646 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002647 assert((Bitness < 3) && "sanity-check table bounds");
2648
2649 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2650 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002651 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2652 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002653 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002654 unsigned hasS = ITP->hasS;
2655 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2656 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2657 "only MOVsi has shift operand addressing mode");
2658 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002659
2660 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2661 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002662 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002663 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002664 // MOVsi encodes shift and immediate in shift operand addressing mode.
2665 // The following condition has the same value when emitting two
2666 // instruction sequences: both are shifts.
2667 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002668
2669 // Either one or two instructions are emitted.
2670 // They're always of the form:
2671 // dst = in OP imm
2672 // CPSR is set only by 16-bit Thumb instructions.
2673 // Predicate, if any, is AL.
2674 // S bit, if available, is always 0.
2675 // When two are emitted the first's result will feed as the second's input,
2676 // that value is then dead.
2677 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2678 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2679 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002680 bool isLsl = (0 == Instr) && !isSingleInstr;
2681 unsigned Opcode = isLsl ? LSLOpc : Opc;
2682 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2683 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002684 bool isKill = 1 == Instr;
2685 MachineInstrBuilder MIB = BuildMI(
Rafael Espindolaea09c592014-02-18 22:05:46 +00002686 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
JF Bastien06ce03d2013-06-07 20:10:37 +00002687 if (setsCPSR)
2688 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002689 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002690 AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
JF Bastien06ce03d2013-06-07 20:10:37 +00002691 if (hasS)
2692 AddDefaultCC(MIB);
2693 // Second instruction consumes the first's result.
2694 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002695 }
2696
Chad Rosier4489f942011-11-02 17:20:24 +00002697 return ResultReg;
2698}
2699
2700bool ARMFastISel::SelectIntExt(const Instruction *I) {
2701 // On ARM, in general, integer casts don't involve legal types; this code
2702 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002703 Type *DestTy = I->getType();
2704 Value *Src = I->getOperand(0);
2705 Type *SrcTy = Src->getType();
2706
Chad Rosier4489f942011-11-02 17:20:24 +00002707 bool isZExt = isa<ZExtInst>(I);
2708 unsigned SrcReg = getRegForValue(Src);
2709 if (!SrcReg) return false;
2710
Chad Rosier62a144f2012-12-17 19:59:43 +00002711 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00002712 SrcEVT = TLI.getValueType(DL, SrcTy, true);
2713 DestEVT = TLI.getValueType(DL, DestTy, true);
Chad Rosier62a144f2012-12-17 19:59:43 +00002714 if (!SrcEVT.isSimple()) return false;
2715 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002716
Chad Rosier62a144f2012-12-17 19:59:43 +00002717 MVT SrcVT = SrcEVT.getSimpleVT();
2718 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002719 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2720 if (ResultReg == 0) return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002721 updateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002722 return true;
2723}
2724
Jush Lu4705da92012-08-03 02:37:48 +00002725bool ARMFastISel::SelectShift(const Instruction *I,
2726 ARM_AM::ShiftOpc ShiftTy) {
2727 // We handle thumb2 mode by target independent selector
2728 // or SelectionDAG ISel.
2729 if (isThumb2)
2730 return false;
2731
2732 // Only handle i32 now.
Mehdi Amini44ede332015-07-09 02:09:04 +00002733 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Jush Lu4705da92012-08-03 02:37:48 +00002734 if (DestVT != MVT::i32)
2735 return false;
2736
2737 unsigned Opc = ARM::MOVsr;
2738 unsigned ShiftImm;
2739 Value *Src2Value = I->getOperand(1);
2740 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2741 ShiftImm = CI->getZExtValue();
2742
2743 // Fall back to selection DAG isel if the shift amount
2744 // is zero or greater than the width of the value type.
2745 if (ShiftImm == 0 || ShiftImm >=32)
2746 return false;
2747
2748 Opc = ARM::MOVsi;
2749 }
2750
2751 Value *Src1Value = I->getOperand(0);
2752 unsigned Reg1 = getRegForValue(Src1Value);
2753 if (Reg1 == 0) return false;
2754
Nadav Rotema8e15b02012-09-06 11:13:55 +00002755 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002756 if (Opc == ARM::MOVsr) {
2757 Reg2 = getRegForValue(Src2Value);
2758 if (Reg2 == 0) return false;
2759 }
2760
JF Bastien13969d02013-05-29 15:45:47 +00002761 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002762 if(ResultReg == 0) return false;
2763
Rafael Espindolaea09c592014-02-18 22:05:46 +00002764 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu4705da92012-08-03 02:37:48 +00002765 TII.get(Opc), ResultReg)
2766 .addReg(Reg1);
2767
2768 if (Opc == ARM::MOVsi)
2769 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2770 else if (Opc == ARM::MOVsr) {
2771 MIB.addReg(Reg2);
2772 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2773 }
2774
2775 AddOptionalDefs(MIB);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002776 updateValueMap(I, ResultReg);
Jush Lu4705da92012-08-03 02:37:48 +00002777 return true;
2778}
2779
Eric Christopherc3e118e2010-09-02 23:43:26 +00002780// TODO: SoftFP support.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002781bool ARMFastISel::fastSelectInstruction(const Instruction *I) {
Eric Christopher2ff757d2010-09-09 01:06:51 +00002782
Eric Christopher84bdfd82010-07-21 22:26:11 +00002783 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002784 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002785 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002786 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002787 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002788 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002789 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002790 case Instruction::IndirectBr:
2791 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002792 case Instruction::ICmp:
2793 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002794 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002795 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002796 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002797 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002798 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002799 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002800 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002801 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002802 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002803 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002804 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002805 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002806 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002807 case Instruction::Add:
2808 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002809 case Instruction::Or:
2810 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002811 case Instruction::Sub:
2812 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002813 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002814 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002815 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002816 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002817 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002818 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002819 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002820 return SelectDiv(I, /*isSigned*/ true);
2821 case Instruction::UDiv:
2822 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002823 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002824 return SelectRem(I, /*isSigned*/ true);
2825 case Instruction::URem:
2826 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002827 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002828 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2829 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002830 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002831 case Instruction::Select:
2832 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002833 case Instruction::Ret:
2834 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002835 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002836 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002837 case Instruction::ZExt:
2838 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002839 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002840 case Instruction::Shl:
2841 return SelectShift(I, ARM_AM::lsl);
2842 case Instruction::LShr:
2843 return SelectShift(I, ARM_AM::lsr);
2844 case Instruction::AShr:
2845 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002846 default: break;
2847 }
2848 return false;
2849}
2850
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002851namespace {
2852// This table describes sign- and zero-extend instructions which can be
2853// folded into a preceding load. All of these extends have an immediate
2854// (sometimes a mask and sometimes a shift) that's applied after
2855// extension.
2856const struct FoldableLoadExtendsStruct {
2857 uint16_t Opc[2]; // ARM, Thumb.
2858 uint8_t ExpectedImm;
2859 uint8_t isZExt : 1;
2860 uint8_t ExpectedVT : 7;
2861} FoldableLoadExtends[] = {
2862 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2863 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2864 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2865 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2866 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2867};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002868}
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002869
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002870/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002871/// vreg is being provided by the specified load instruction. If possible,
2872/// try to fold the load as an operand to the instruction, returning true if
2873/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002874bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2875 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002876 // Verify we have a legal type before going any further.
2877 MVT VT;
2878 if (!isLoadTypeLegal(LI->getType(), VT))
2879 return false;
2880
2881 // Combine load followed by zero- or sign-extend.
2882 // ldrb r1, [r0] ldrb r1, [r0]
2883 // uxtb r2, r1 =>
2884 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002885 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2886 return false;
2887 const uint64_t Imm = MI->getOperand(2).getImm();
2888
2889 bool Found = false;
2890 bool isZExt;
2891 for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
2892 i != e; ++i) {
2893 if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
2894 (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
2895 MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
2896 Found = true;
2897 isZExt = FoldableLoadExtends[i].isZExt;
2898 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002899 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002900 if (!Found) return false;
2901
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002902 // See if we can handle this address.
2903 Address Addr;
2904 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00002905
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002906 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00002907 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002908 return false;
2909 MI->eraseFromParent();
2910 return true;
2911}
2912
Jush Lu47172a02012-09-27 05:21:41 +00002913unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002914 unsigned Align, MVT VT) {
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002915 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Jush Lu47172a02012-09-27 05:21:41 +00002916
Peter Collingbourne97aae402015-10-26 18:23:16 +00002917 LLVMContext *Context = &MF->getFunction()->getContext();
2918 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2919 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2920 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2921 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2922 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2923 /*AddCurrentAddress=*/UseGOT_PREL);
Jush Lu47172a02012-09-27 05:21:41 +00002924
Peter Collingbourne97aae402015-10-26 18:23:16 +00002925 unsigned ConstAlign =
2926 MF->getDataLayout().getPrefTypeAlignment(Type::getInt32PtrTy(*Context));
2927 unsigned Idx = MF->getConstantPool()->getConstantPoolIndex(CPV, ConstAlign);
Jush Lu47172a02012-09-27 05:21:41 +00002928
Peter Collingbourne97aae402015-10-26 18:23:16 +00002929 unsigned TempReg = MF->getRegInfo().createVirtualRegister(&ARM::rGPRRegClass);
2930 unsigned Opc = isThumb2 ? ARM::t2LDRpci : ARM::LDRcp;
2931 MachineInstrBuilder MIB =
2932 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), TempReg)
2933 .addConstantPoolIndex(Idx);
2934 if (Opc == ARM::LDRcp)
Jush Lu47172a02012-09-27 05:21:41 +00002935 MIB.addImm(0);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002936 AddDefaultPred(MIB);
Jush Lu47172a02012-09-27 05:21:41 +00002937
Peter Collingbourne97aae402015-10-26 18:23:16 +00002938 // Fix the address by adding pc.
2939 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
2940 Opc = Subtarget->isThumb() ? ARM::tPICADD : UseGOT_PREL ? ARM::PICLDR
2941 : ARM::PICADD;
2942 DestReg = constrainOperandRegClass(TII.get(Opc), DestReg, 0);
2943 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2944 .addReg(TempReg)
2945 .addImm(ARMPCLabelIndex);
2946 if (!Subtarget->isThumb())
2947 AddDefaultPred(MIB);
2948
2949 if (UseGOT_PREL && Subtarget->isThumb()) {
2950 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
2951 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2952 TII.get(ARM::t2LDRi12), NewDestReg)
2953 .addReg(DestReg)
2954 .addImm(0);
2955 DestReg = NewDestReg;
2956 AddOptionalDefs(MIB);
2957 }
2958 return DestReg;
Jush Lu47172a02012-09-27 05:21:41 +00002959}
2960
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002961bool ARMFastISel::fastLowerArguments() {
Evan Cheng615620c2013-02-11 01:27:15 +00002962 if (!FuncInfo.CanLowerReturn)
2963 return false;
2964
2965 const Function *F = FuncInfo.Fn;
2966 if (F->isVarArg())
2967 return false;
2968
2969 CallingConv::ID CC = F->getCallingConv();
2970 switch (CC) {
2971 default:
2972 return false;
2973 case CallingConv::Fast:
2974 case CallingConv::C:
2975 case CallingConv::ARM_AAPCS_VFP:
2976 case CallingConv::ARM_AAPCS:
2977 case CallingConv::ARM_APCS:
Manman Ren802cd6f2016-04-05 22:44:44 +00002978 case CallingConv::Swift:
Evan Cheng615620c2013-02-11 01:27:15 +00002979 break;
2980 }
2981
2982 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
2983 // which are passed in r0 - r3.
2984 unsigned Idx = 1;
2985 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
2986 I != E; ++I, ++Idx) {
2987 if (Idx > 4)
2988 return false;
2989
2990 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2991 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
Manman Renf46262e2016-03-29 17:37:21 +00002992 F->getAttributes().hasAttribute(Idx, Attribute::SwiftSelf) ||
Manman Ren57518142016-04-11 21:08:06 +00002993 F->getAttributes().hasAttribute(Idx, Attribute::SwiftError) ||
Evan Cheng615620c2013-02-11 01:27:15 +00002994 F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
2995 return false;
2996
2997 Type *ArgTy = I->getType();
2998 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2999 return false;
3000
Mehdi Amini44ede332015-07-09 02:09:04 +00003001 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003002 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003003 switch (ArgVT.getSimpleVT().SimpleTy) {
3004 case MVT::i8:
3005 case MVT::i16:
3006 case MVT::i32:
3007 break;
3008 default:
3009 return false;
3010 }
3011 }
3012
3013
Craig Toppere5e035a32015-12-05 07:13:35 +00003014 static const MCPhysReg GPRArgRegs[] = {
Evan Cheng615620c2013-02-11 01:27:15 +00003015 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3016 };
3017
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003018 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Evan Cheng615620c2013-02-11 01:27:15 +00003019 Idx = 0;
3020 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
3021 I != E; ++I, ++Idx) {
Evan Cheng615620c2013-02-11 01:27:15 +00003022 unsigned SrcReg = GPRArgRegs[Idx];
3023 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3024 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3025 // Without this, EmitLiveInCopies may eliminate the livein if its only
3026 // use is a bitcast (which isn't turned into an instruction).
3027 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003028 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3029 TII.get(TargetOpcode::COPY),
Evan Cheng615620c2013-02-11 01:27:15 +00003030 ResultReg).addReg(DstReg, getKillRegState(true));
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003031 updateValueMap(&*I, ResultReg);
Evan Cheng615620c2013-02-11 01:27:15 +00003032 }
3033
3034 return true;
3035}
3036
Eric Christopher84bdfd82010-07-21 22:26:11 +00003037namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003038 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3039 const TargetLibraryInfo *libInfo) {
Akira Hatanakaddf76aa2015-05-23 01:14:08 +00003040 if (funcInfo.MF->getSubtarget<ARMSubtarget>().useFastISel())
Bob Wilson3e6fa462012-08-03 04:06:28 +00003041 return new ARMFastISel(funcInfo, libInfo);
Akira Hatanakaddf76aa2015-05-23 01:14:08 +00003042
Craig Topper062a2ba2014-04-25 05:30:21 +00003043 return nullptr;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003044 }
3045}