blob: 069d1b3fd7d75489ef4c4b3dc845fe7aefa342b7 [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"
Eugene Zelenko342257e2017-01-31 00:56:17 +000017#include "ARMBaseInstrInfo.h"
Craig Toppera9253262014-03-22 23:51:00 +000018#include "ARMBaseRegisterInfo.h"
Eric Christopher72497e52010-09-10 23:18:12 +000019#include "ARMCallingConv.h"
Eric Christopher83a5ec82010-10-01 23:24:42 +000020#include "ARMConstantPoolValue.h"
Craig Toppera9253262014-03-22 23:51:00 +000021#include "ARMISelLowering.h"
22#include "ARMMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000024#include "MCTargetDesc/ARMAddressingModes.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000025#include "MCTargetDesc/ARMBaseInfo.h"
26#include "llvm/ADT/APFloat.h"
27#include "llvm/ADT/APInt.h"
28#include "llvm/ADT/DenseMap.h"
JF Bastien3c6bb8e2013-06-11 22:13:46 +000029#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/ADT/SmallVector.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000031#include "llvm/CodeGen/CallingConvLower.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/FastISel.h"
33#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000034#include "llvm/CodeGen/ISDOpcodes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/CodeGen/MachineConstantPool.h"
36#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000037#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/CodeGen/MachineInstrBuilder.h"
39#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000040#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000042#include "llvm/CodeGen/MachineValueType.h"
43#include "llvm/CodeGen/RuntimeLibcalls.h"
44#include "llvm/CodeGen/ValueTypes.h"
45#include "llvm/IR/Argument.h"
46#include "llvm/IR/Attributes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000047#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000048#include "llvm/IR/CallingConv.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000049#include "llvm/IR/Constant.h"
50#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000051#include "llvm/IR/DataLayout.h"
52#include "llvm/IR/DerivedTypes.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000053#include "llvm/IR/Function.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000054#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000055#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000056#include "llvm/IR/GlobalVariable.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000057#include "llvm/IR/InstrTypes.h"
58#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000059#include "llvm/IR/Instructions.h"
60#include "llvm/IR/IntrinsicInst.h"
61#include "llvm/IR/Module.h"
62#include "llvm/IR/Operator.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000063#include "llvm/IR/Type.h"
64#include "llvm/IR/User.h"
65#include "llvm/IR/Value.h"
66#include "llvm/MC/MCInstrDesc.h"
67#include "llvm/MC/MCRegisterInfo.h"
68#include "llvm/Support/Casting.h"
69#include "llvm/Support/Compiler.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000070#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000071#include "llvm/Support/MathExtras.h"
Eric Christopher09f757d2010-08-17 01:25:29 +000072#include "llvm/Target/TargetInstrInfo.h"
73#include "llvm/Target/TargetLowering.h"
74#include "llvm/Target/TargetMachine.h"
Eric Christopher84bdfd82010-07-21 22:26:11 +000075#include "llvm/Target/TargetOptions.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000076#include <cassert>
77#include <cstdint>
78#include <utility>
79
Eric Christopher84bdfd82010-07-21 22:26:11 +000080using namespace llvm;
81
82namespace {
Eric Christopher0a3c28b2010-11-20 22:38:27 +000083
Eric Christopherfef5f312010-11-19 22:30:02 +000084 // All possible address modes, plus some.
85 typedef struct Address {
86 enum {
87 RegBase,
88 FrameIndexBase
Eugene Zelenko342257e2017-01-31 00:56:17 +000089 } BaseType = RegBase;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000090
Eric Christopherfef5f312010-11-19 22:30:02 +000091 union {
92 unsigned Reg;
93 int FI;
94 } Base;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000095
Eugene Zelenko342257e2017-01-31 00:56:17 +000096 int Offset = 0;
Eric Christopher0a3c28b2010-11-20 22:38:27 +000097
Eric Christopherfef5f312010-11-19 22:30:02 +000098 // Innocuous defaults for our address.
Eugene Zelenko342257e2017-01-31 00:56:17 +000099 Address() {
100 Base.Reg = 0;
101 }
Eric Christopherfef5f312010-11-19 22:30:02 +0000102 } Address;
Eric Christopher84bdfd82010-07-21 22:26:11 +0000103
Craig Topper26696312014-03-18 07:27:13 +0000104class ARMFastISel final : public FastISel {
Eric Christopher84bdfd82010-07-21 22:26:11 +0000105 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const ARMSubtarget *Subtarget;
Bill Wendling6c1d9592013-12-30 05:17:29 +0000108 Module &M;
Eric Christopher09f757d2010-08-17 01:25:29 +0000109 const TargetMachine &TM;
110 const TargetInstrInfo &TII;
111 const TargetLowering &TLI;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000112 ARMFunctionInfo *AFI;
Eric Christopher84bdfd82010-07-21 22:26:11 +0000113
Eric Christopherb024be32010-09-29 22:24:45 +0000114 // Convenience variables to avoid some queries.
Chad Rosier0439cfc2011-11-08 21:12:00 +0000115 bool isThumb2;
Eric Christopherb024be32010-09-29 22:24:45 +0000116 LLVMContext *Context;
Eric Christopher6a0333c2010-09-02 01:39:14 +0000117
Eric Christopher84bdfd82010-07-21 22:26:11 +0000118 public:
Bob Wilson3e6fa462012-08-03 04:06:28 +0000119 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
120 const TargetLibraryInfo *libInfo)
Eric Christopherd9134482014-08-04 21:25:23 +0000121 : FastISel(funcInfo, libInfo),
Eric Christopherc125e122015-01-29 00:19:37 +0000122 Subtarget(
123 &static_cast<const ARMSubtarget &>(funcInfo.MF->getSubtarget())),
Eric Christopherd9134482014-08-04 21:25:23 +0000124 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
Eric Christopherc125e122015-01-29 00:19:37 +0000125 TM(funcInfo.MF->getTarget()), TII(*Subtarget->getInstrInfo()),
126 TLI(*Subtarget->getTargetLowering()) {
Eric Christopher8d03b8a2010-08-23 22:32:45 +0000127 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Chad Rosier0439cfc2011-11-08 21:12:00 +0000128 isThumb2 = AFI->isThumbFunction();
Eric Christopherb024be32010-09-29 22:24:45 +0000129 Context = &funcInfo.Fn->getContext();
Eric Christopher84bdfd82010-07-21 22:26:11 +0000130 }
131
Craig Topperfd1c9252012-08-18 21:38:45 +0000132 private:
Eugene Zelenko342257e2017-01-31 00:56:17 +0000133 // Code from FastISel.cpp.
134
Juergen Ributzka88e32512014-09-03 20:56:59 +0000135 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000136 const TargetRegisterClass *RC,
137 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000138 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000139 const TargetRegisterClass *RC,
140 unsigned Op0, bool Op0IsKill,
141 unsigned Op1, bool Op1IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000142 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000143 const TargetRegisterClass *RC,
144 unsigned Op0, bool Op0IsKill,
145 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000146 unsigned fastEmitInst_i(unsigned MachineInstOpcode,
Craig Topperfd1c9252012-08-18 21:38:45 +0000147 const TargetRegisterClass *RC,
148 uint64_t Imm);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000149
Eric Christopherd8e8a292010-08-20 00:20:31 +0000150 // Backend specific FastISel code.
Eugene Zelenko342257e2017-01-31 00:56:17 +0000151
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000152 bool fastSelectInstruction(const Instruction *I) override;
153 unsigned fastMaterializeConstant(const Constant *C) override;
154 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000155 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
156 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000157 bool fastLowerArguments() override;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000158
Eric Christopher84bdfd82010-07-21 22:26:11 +0000159 #include "ARMGenFastISel.inc"
Eric Christopher2ff757d2010-09-09 01:06:51 +0000160
Eric Christopher00202ee2010-08-23 21:44:12 +0000161 // Instruction selection routines.
Eugene Zelenko342257e2017-01-31 00:56:17 +0000162
Eric Christopher2f8637d2010-10-21 21:47:51 +0000163 bool SelectLoad(const Instruction *I);
164 bool SelectStore(const Instruction *I);
165 bool SelectBranch(const Instruction *I);
Chad Rosierded4c992012-02-07 23:56:08 +0000166 bool SelectIndirectBr(const Instruction *I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000167 bool SelectCmp(const Instruction *I);
168 bool SelectFPExt(const Instruction *I);
169 bool SelectFPTrunc(const Instruction *I);
Chad Rosier685b20c2012-02-06 23:50:07 +0000170 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
171 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
Chad Rosiere023d5d2012-02-03 21:14:11 +0000172 bool SelectIToFP(const Instruction *I, bool isSigned);
173 bool SelectFPToI(const Instruction *I, bool isSigned);
Chad Rosieraaa55a82012-02-03 21:07:27 +0000174 bool SelectDiv(const Instruction *I, bool isSigned);
Chad Rosierb84a4b42012-02-03 21:23:45 +0000175 bool SelectRem(const Instruction *I, bool isSigned);
Chad Rosiera7ebc562011-11-11 23:31:03 +0000176 bool SelectCall(const Instruction *I, const char *IntrMemName);
177 bool SelectIntrinsicCall(const IntrinsicInst &I);
Eric Christopher2f8637d2010-10-21 21:47:51 +0000178 bool SelectSelect(const Instruction *I);
Eric Christopher93bbe652010-10-22 01:28:00 +0000179 bool SelectRet(const Instruction *I);
Chad Rosieree7e4522011-11-02 00:18:48 +0000180 bool SelectTrunc(const Instruction *I);
181 bool SelectIntExt(const Instruction *I);
Jush Lu4705da92012-08-03 02:37:48 +0000182 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
Eric Christopher84bdfd82010-07-21 22:26:11 +0000183
Eric Christopher00202ee2010-08-23 21:44:12 +0000184 // Utility routines.
Eugene Zelenko342257e2017-01-31 00:56:17 +0000185
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000186 bool isPositionIndependent() const;
Chris Lattner229907c2011-07-18 04:54:35 +0000187 bool isTypeLegal(Type *Ty, MVT &VT);
188 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Chad Rosier9cf803c2011-11-02 18:08:25 +0000189 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
James Molloyd5087892017-02-13 12:32:47 +0000190 bool isZExt, bool isEquality);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000191 bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosiera26979b2011-12-14 17:26:05 +0000192 unsigned Alignment = 0, bool isZExt = true,
193 bool allocReg = true);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000194 bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +0000195 unsigned Alignment = 0);
Eric Christopherfef5f312010-11-19 22:30:02 +0000196 bool ARMComputeAddress(const Value *Obj, Address &Addr);
Chad Rosier150d35b2012-12-17 22:35:29 +0000197 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
Chad Rosier057b6d32011-11-14 23:04:09 +0000198 bool ARMIsMemCpySmall(uint64_t Len);
Chad Rosier9f5c68a2012-12-06 01:34:31 +0000199 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
200 unsigned Alignment);
Chad Rosier62a144f2012-12-17 19:59:43 +0000201 unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000202 unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
203 unsigned ARMMaterializeInt(const Constant *C, MVT VT);
204 unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
205 unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
206 unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000207 unsigned ARMSelectCallOp(bool UseReg);
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000208 unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000209
Eric Christopher1b21f002015-01-29 00:19:33 +0000210 const TargetLowering *getTargetLowering() { return &TLI; }
Christian Pirker238c7c12014-05-12 11:19:20 +0000211
Eric Christopher72497e52010-09-10 23:18:12 +0000212 // Call handling routines.
Eugene Zelenko342257e2017-01-31 00:56:17 +0000213
Jush Lue67e07b2012-07-19 09:49:00 +0000214 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
215 bool Return,
216 bool isVarArg);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000217 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christopher79398062010-09-29 23:11:09 +0000218 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +0000219 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +0000220 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
221 SmallVectorImpl<unsigned> &RegArgs,
222 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000223 unsigned &NumBytes,
224 bool isVarArg);
Chad Rosierc6916f82012-06-12 19:25:13 +0000225 unsigned getLibcallReg(const Twine &Name);
Duncan Sandsf5dda012010-11-03 11:35:31 +0000226 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +0000227 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +0000228 unsigned &NumBytes, bool isVarArg);
Eric Christopher7990df12010-09-28 01:21:42 +0000229 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopher72497e52010-09-10 23:18:12 +0000230
231 // OptionalDef handling routines.
Eugene Zelenko342257e2017-01-31 00:56:17 +0000232
Eric Christopher174d8722011-03-12 01:09:29 +0000233 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher0d274a02010-08-19 00:37:05 +0000234 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
235 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Chad Rosier150d35b2012-12-17 22:35:29 +0000236 void AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000237 const MachineInstrBuilder &MIB,
Justin Lebar0af80cd2016-07-15 18:26:59 +0000238 MachineMemOperand::Flags Flags, bool useAM3);
Eric Christopher0d274a02010-08-19 00:37:05 +0000239};
Eric Christopher84bdfd82010-07-21 22:26:11 +0000240
241} // end anonymous namespace
242
Eric Christopher72497e52010-09-10 23:18:12 +0000243#include "ARMGenCallingConv.inc"
Eric Christopher84bdfd82010-07-21 22:26:11 +0000244
Eric Christopher0d274a02010-08-19 00:37:05 +0000245// DefinesOptionalPredicate - This is different from DefinesPredicate in that
246// we don't care about implicit defs here, just places we'll need to add a
247// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
248bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000249 if (!MI->hasOptionalDef())
Eric Christopher0d274a02010-08-19 00:37:05 +0000250 return false;
251
252 // Look to see if our OptionalDef is defining CPSR or CCR.
Javed Absar5b8e4872017-07-18 10:19:48 +0000253 for (const MachineOperand &MO : MI->operands()) {
Eric Christopher985d9e42010-08-20 00:36:24 +0000254 if (!MO.isReg() || !MO.isDef()) continue;
255 if (MO.getReg() == ARM::CPSR)
Eric Christopher0d274a02010-08-19 00:37:05 +0000256 *CPSR = true;
257 }
258 return true;
259}
260
Eric Christopher174d8722011-03-12 01:09:29 +0000261bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000262 const MCInstrDesc &MCID = MI->getDesc();
Eric Christopher501d2e22011-04-29 00:03:10 +0000263
Joey Goulya5153cb2013-09-09 14:21:49 +0000264 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000265 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
Eric Christopher174d8722011-03-12 01:09:29 +0000266 AFI->isThumb2Function())
Joey Goulya5153cb2013-09-09 14:21:49 +0000267 return MI->isPredicable();
Eric Christopher501d2e22011-04-29 00:03:10 +0000268
Javed Absar5b8e4872017-07-18 10:19:48 +0000269 for (const MCOperandInfo &opInfo : MCID.operands())
270 if (opInfo.isPredicate())
Eric Christopher174d8722011-03-12 01:09:29 +0000271 return true;
Eric Christopher501d2e22011-04-29 00:03:10 +0000272
Eric Christopher174d8722011-03-12 01:09:29 +0000273 return false;
274}
275
Eric Christopher0d274a02010-08-19 00:37:05 +0000276// If the machine is predicable go ahead and add the predicate operands, if
277// it needs default CC operands add those.
Eric Christophere8fccc82010-11-02 01:21:28 +0000278// TODO: If we want to support thumb1 then we'll need to deal with optional
279// CPSR defs that need to be added before the remaining operands. See s_cc_out
280// for descriptions why.
Eric Christopher0d274a02010-08-19 00:37:05 +0000281const MachineInstrBuilder &
282ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
283 MachineInstr *MI = &*MIB;
284
Eric Christopher174d8722011-03-12 01:09:29 +0000285 // Do we use a predicate? or...
286 // Are we NEON in ARM mode and have a predicate operand? If so, I know
287 // we're not predicable but add it anyways.
Joey Goulya5153cb2013-09-09 14:21:49 +0000288 if (isARMNEONPred(MI))
Diana Picus4f8c3e12017-01-13 09:37:56 +0000289 MIB.add(predOps(ARMCC::AL));
Eric Christopher501d2e22011-04-29 00:03:10 +0000290
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000291 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
Eric Christopher0d274a02010-08-19 00:37:05 +0000292 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christophera5d60c62010-08-19 15:35:27 +0000293 bool CPSR = false;
Diana Picusa2c59142017-01-13 10:37:37 +0000294 if (DefinesOptionalPredicate(MI, &CPSR))
295 MIB.add(CPSR ? t1CondCodeOp() : condCodeOp());
Eric Christopher0d274a02010-08-19 00:37:05 +0000296 return MIB;
297}
298
Juergen Ributzka88e32512014-09-03 20:56:59 +0000299unsigned ARMFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000300 const TargetRegisterClass *RC,
301 unsigned Op0, bool Op0IsKill) {
302 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000303 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000304
Jim Grosbach06c2a682013-08-16 23:37:31 +0000305 // Make sure the input operand is sufficiently constrained to be legal
306 // for this instruction.
307 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000308 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
310 ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000311 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000313 .addReg(Op0, Op0IsKill * RegState::Kill));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000315 TII.get(TargetOpcode::COPY), ResultReg)
316 .addReg(II.ImplicitDefs[0]));
317 }
318 return ResultReg;
319}
320
Juergen Ributzka88e32512014-09-03 20:56:59 +0000321unsigned ARMFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000322 const TargetRegisterClass *RC,
323 unsigned Op0, bool Op0IsKill,
324 unsigned Op1, bool Op1IsKill) {
325 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000326 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000327
Jim Grosbach06c2a682013-08-16 23:37:31 +0000328 // Make sure the input operands are sufficiently constrained to be legal
329 // for this instruction.
330 Op0 = constrainOperandRegClass(II, Op0, 1);
331 Op1 = constrainOperandRegClass(II, Op1, 2);
332
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 .addReg(Op1, Op1IsKill * RegState::Kill));
Chad Rosier0bc51322012-02-15 17:36:21 +0000338 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000339 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000340 .addReg(Op0, Op0IsKill * RegState::Kill)
341 .addReg(Op1, Op1IsKill * RegState::Kill));
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_ri(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000350 const TargetRegisterClass *RC,
351 unsigned Op0, bool Op0IsKill,
352 uint64_t Imm) {
353 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000354 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher09f757d2010-08-17 01:25:29 +0000355
Jim Grosbach06c2a682013-08-16 23:37:31 +0000356 // Make sure the input operand is sufficiently constrained to be legal
357 // for this instruction.
358 Op0 = constrainOperandRegClass(II, Op0, 1);
Chad Rosier0bc51322012-02-15 17:36:21 +0000359 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000360 AddOptionalDefs(
361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
362 .addReg(Op0, Op0IsKill * RegState::Kill)
363 .addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000364 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000365 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000366 .addReg(Op0, Op0IsKill * RegState::Kill)
367 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000368 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000369 TII.get(TargetOpcode::COPY), ResultReg)
370 .addReg(II.ImplicitDefs[0]));
371 }
372 return ResultReg;
373}
374
Juergen Ributzka88e32512014-09-03 20:56:59 +0000375unsigned ARMFastISel::fastEmitInst_i(unsigned MachineInstOpcode,
Eric Christopher09f757d2010-08-17 01:25:29 +0000376 const TargetRegisterClass *RC,
377 uint64_t Imm) {
378 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000379 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000380
Chad Rosier0bc51322012-02-15 17:36:21 +0000381 if (II.getNumDefs() >= 1) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
383 ResultReg).addImm(Imm));
Chad Rosier0bc51322012-02-15 17:36:21 +0000384 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000385 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Eric Christopher09f757d2010-08-17 01:25:29 +0000386 .addImm(Imm));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher09f757d2010-08-17 01:25:29 +0000388 TII.get(TargetOpcode::COPY), ResultReg)
389 .addReg(II.ImplicitDefs[0]));
390 }
391 return ResultReg;
392}
393
Eric Christopher860fc932010-09-10 00:34:35 +0000394// TODO: Don't worry about 64-bit now, but when this is fixed remove the
395// checks from the various callers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000396unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000397 if (VT == MVT::f64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000398
Eric Christopher4bd70472010-09-09 21:44:45 +0000399 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000401 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopher4bd70472010-09-09 21:44:45 +0000402 .addReg(SrcReg));
403 return MoveReg;
404}
405
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000406unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
Duncan Sands14627772010-11-03 12:17:33 +0000407 if (VT == MVT::i64) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000408
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000409 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000410 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbach6990e5f2012-03-01 22:47:09 +0000411 TII.get(ARM::VMOVRS), MoveReg)
Eric Christopher2cbe0fd2010-09-09 20:49:25 +0000412 .addReg(SrcReg));
413 return MoveReg;
414}
415
Eric Christopher3cf63f12010-09-09 00:19:41 +0000416// For double width floating point we need to materialize two constants
417// (the high and the low) into integer registers then use a move to get
418// the combined constant into an FP reg.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000419unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
Eric Christopher3cf63f12010-09-09 00:19:41 +0000420 const APFloat Val = CFP->getValueAPF();
Duncan Sands14627772010-11-03 12:17:33 +0000421 bool is64bit = VT == MVT::f64;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000422
Eric Christopher3cf63f12010-09-09 00:19:41 +0000423 // This checks to see if we can use VFP3 instructions to materialize
424 // a constant, otherwise we have to go through the constant pool.
425 if (TLI.isFPImmLegal(Val, VT)) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000426 int Imm;
427 unsigned Opc;
428 if (is64bit) {
429 Imm = ARM_AM::getFP64Imm(Val);
430 Opc = ARM::FCONSTD;
431 } else {
432 Imm = ARM_AM::getFP32Imm(Val);
433 Opc = ARM::FCONSTS;
434 }
Eric Christopher3cf63f12010-09-09 00:19:41 +0000435 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000436 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
437 TII.get(Opc), DestReg).addImm(Imm));
Eric Christopher3cf63f12010-09-09 00:19:41 +0000438 return DestReg;
439 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000440
Eric Christopher860fc932010-09-10 00:34:35 +0000441 // Require VFP2 for loading fp constants.
Eric Christopher22fd29a2010-09-09 23:50:00 +0000442 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000443
Eric Christopher22fd29a2010-09-09 23:50:00 +0000444 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000445 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000446 if (Align == 0) {
447 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000448 Align = DL.getTypeAllocSize(CFP->getType());
Eric Christopher22fd29a2010-09-09 23:50:00 +0000449 }
450 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
451 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
452 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000453
Eric Christopher860fc932010-09-10 00:34:35 +0000454 // The extra reg is for addrmode5.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000455 AddOptionalDefs(
456 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
457 .addConstantPoolIndex(Idx)
458 .addReg(0));
Eric Christopher22fd29a2010-09-09 23:50:00 +0000459 return DestReg;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000460}
461
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000462unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
Chad Rosier67f96882011-11-04 22:29:00 +0000463 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000464 return 0;
Eric Christophere4dd7372010-11-03 20:21:17 +0000465
466 // If we can do this in a single instruction without a constant pool entry
467 // do so now.
468 const ConstantInt *CI = cast<ConstantInt>(C);
Chad Rosiere8b8b772011-11-04 23:09:49 +0000469 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
Chad Rosier0439cfc2011-11-08 21:12:00 +0000470 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
Chad Rosier2e82ad12012-11-27 01:06:49 +0000471 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
472 &ARM::GPRRegClass;
473 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000474 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier67f96882011-11-04 22:29:00 +0000475 TII.get(Opc), ImmReg)
Chad Rosierd0191a52011-11-05 20:16:15 +0000476 .addImm(CI->getZExtValue()));
Chad Rosier67f96882011-11-04 22:29:00 +0000477 return ImmReg;
Eric Christophere4dd7372010-11-03 20:21:17 +0000478 }
479
Chad Rosier2a3503e2011-11-11 00:36:21 +0000480 // Use MVN to emit negative constants.
481 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
482 unsigned Imm = (unsigned)~(CI->getSExtValue());
Chad Rosiere19b0a92011-11-11 06:27:41 +0000483 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
Chad Rosier2a3503e2011-11-11 00:36:21 +0000484 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosiere19b0a92011-11-11 06:27:41 +0000485 if (UseImm) {
Chad Rosier2a3503e2011-11-11 00:36:21 +0000486 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
Juergen Ributzka2cbcf7a2014-08-13 21:39:18 +0000487 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
488 &ARM::GPRRegClass;
489 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000490 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier2a3503e2011-11-11 00:36:21 +0000491 TII.get(Opc), ImmReg)
492 .addImm(Imm));
493 return ImmReg;
494 }
495 }
496
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000497 unsigned ResultReg = 0;
Juergen Ributzkaa5b08382014-08-13 21:42:19 +0000498 if (Subtarget->useMovt(*FuncInfo.MF))
Juergen Ributzka88e32512014-09-03 20:56:59 +0000499 ResultReg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000500
501 if (ResultReg)
502 return ResultReg;
Juergen Ributzkaa5b08382014-08-13 21:42:19 +0000503
Chad Rosier2a3503e2011-11-11 00:36:21 +0000504 // Load from constant pool. For now 32-bit only.
Chad Rosier67f96882011-11-04 22:29:00 +0000505 if (VT != MVT::i32)
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000506 return 0;
Chad Rosier67f96882011-11-04 22:29:00 +0000507
Eric Christopherc3e118e2010-09-02 23:43:26 +0000508 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000509 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000510 if (Align == 0) {
511 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000512 Align = DL.getTypeAllocSize(C->getType());
Eric Christopherc3e118e2010-09-02 23:43:26 +0000513 }
514 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000515 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000516 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000517 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000518 TII.get(ARM::t2LDRpci), ResultReg)
519 .addConstantPoolIndex(Idx));
Tim Northovere42fb072014-02-04 10:38:46 +0000520 else {
Eric Christopher22d04922010-11-12 09:48:30 +0000521 // The extra immediate is for addrmode2.
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000522 ResultReg = constrainOperandRegClass(TII.get(ARM::LDRcp), ResultReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000523 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000524 TII.get(ARM::LDRcp), ResultReg)
525 .addConstantPoolIndex(Idx)
526 .addImm(0));
Tim Northovere42fb072014-02-04 10:38:46 +0000527 }
Juergen Ributzka5df8603df2014-08-15 16:59:46 +0000528 return ResultReg;
Eric Christopher92db2012010-09-02 01:48:11 +0000529}
530
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000531bool ARMFastISel::isPositionIndependent() const {
Rafael Espindolae7151722016-06-26 22:32:53 +0000532 return TLI.isPositionIndependent();
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000533}
534
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000535unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
Eric Christopher7787f792010-10-02 00:32:44 +0000536 // For now 32-bit only.
Tim Northoverbd41cf82016-01-07 09:03:03 +0000537 if (VT != MVT::i32 || GV->isThreadLocal()) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000538
Oliver Stannard8331aae2016-08-08 15:28:31 +0000539 // ROPI/RWPI not currently supported.
540 if (Subtarget->isROPI() || Subtarget->isRWPI())
541 return 0;
542
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +0000543 bool IsIndirect = Subtarget->isGVIndirectSymbol(GV);
Craig Topper61e88f42014-11-21 05:58:21 +0000544 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass
545 : &ARM::GPRRegClass;
Chad Rosier65710a72012-11-07 00:13:01 +0000546 unsigned DestReg = createResultReg(RC);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000547
Tim Northoverd6a729b2014-01-06 14:28:05 +0000548 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
JF Bastien18db1f22013-06-14 02:49:43 +0000549 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
550 bool IsThreadLocal = GVar && GVar->isThreadLocal();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000551 if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
JF Bastien18db1f22013-06-14 02:49:43 +0000552
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000553 bool IsPositionIndependent = isPositionIndependent();
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000554 // Use movw+movt when possible, it avoids constant pool entries.
Tim Northoverfa36dfe2013-11-26 12:45:05 +0000555 // Non-darwin targets only support static movt relocations in FastISel.
Eric Christopherc1058df2014-07-04 01:55:26 +0000556 if (Subtarget->useMovt(*FuncInfo.MF) &&
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000557 (Subtarget->isTargetMachO() || !IsPositionIndependent)) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000558 unsigned Opc;
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000559 unsigned char TF = 0;
Tim Northoverd6a729b2014-01-06 14:28:05 +0000560 if (Subtarget->isTargetMachO())
Tim Northoverdb962e2c2013-11-25 16:24:52 +0000561 TF = ARMII::MO_NONLAZY;
562
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000563 if (IsPositionIndependent)
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000564 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
Rafael Espindola99357662016-06-20 17:00:13 +0000565 else
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000566 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000567 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
568 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
Eric Christopher7787f792010-10-02 00:32:44 +0000569 } else {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000570 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000571 unsigned Align = DL.getPrefTypeAlignment(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000572 if (Align == 0) {
573 // TODO: Figure out if this is correct.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000574 Align = DL.getTypeAllocSize(GV->getType());
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000575 }
576
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000577 if (Subtarget->isTargetELF() && IsPositionIndependent)
Jush Lu47172a02012-09-27 05:21:41 +0000578 return ARMLowerPICELF(GV, Align, VT);
579
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000580 // Grab index.
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000581 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000582 unsigned Id = AFI->createPICLabelUId();
583 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
584 ARMCP::CPValue,
585 PCAdj);
586 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
587
588 // Load value.
589 MachineInstrBuilder MIB;
590 if (isThumb2) {
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000591 unsigned Opc = IsPositionIndependent ? ARM::t2LDRpci_pic : ARM::t2LDRpci;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000592 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
593 DestReg).addConstantPoolIndex(Idx);
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000594 if (IsPositionIndependent)
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000595 MIB.addImm(Id);
Jush Lue87e5592012-08-29 02:41:21 +0000596 AddOptionalDefs(MIB);
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000597 } else {
598 // The extra immediate is for addrmode2.
Jim Grosbach5f71aab2013-08-26 20:07:29 +0000599 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000600 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
601 TII.get(ARM::LDRcp), DestReg)
602 .addConstantPoolIndex(Idx)
603 .addImm(0);
Jush Lue87e5592012-08-29 02:41:21 +0000604 AddOptionalDefs(MIB);
605
Rafael Espindola524bcbf2016-06-20 19:00:05 +0000606 if (IsPositionIndependent) {
Jush Lue87e5592012-08-29 02:41:21 +0000607 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
608 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
609
610 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000611 DbgLoc, TII.get(Opc), NewDestReg)
Jush Lue87e5592012-08-29 02:41:21 +0000612 .addReg(DestReg)
613 .addImm(Id);
614 AddOptionalDefs(MIB);
615 return NewDestReg;
616 }
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000617 }
Eric Christopher7787f792010-10-02 00:32:44 +0000618 }
Eli Friedman86585792011-06-03 01:13:19 +0000619
Jush Lue87e5592012-08-29 02:41:21 +0000620 if (IsIndirect) {
Jakob Stoklund Olesen68f034e2012-01-07 01:47:05 +0000621 MachineInstrBuilder MIB;
Eli Friedman86585792011-06-03 01:13:19 +0000622 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
Chad Rosier0439cfc2011-11-08 21:12:00 +0000623 if (isThumb2)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000624 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbache7e2aca2011-09-13 20:30:37 +0000625 TII.get(ARM::t2LDRi12), NewDestReg)
Eli Friedman86585792011-06-03 01:13:19 +0000626 .addReg(DestReg)
627 .addImm(0);
628 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000629 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
630 TII.get(ARM::LDRi12), NewDestReg)
631 .addReg(DestReg)
632 .addImm(0);
Eli Friedman86585792011-06-03 01:13:19 +0000633 DestReg = NewDestReg;
634 AddOptionalDefs(MIB);
635 }
636
Eric Christopher7787f792010-10-02 00:32:44 +0000637 return DestReg;
Eric Christopher83a5ec82010-10-01 23:24:42 +0000638}
639
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000640unsigned ARMFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000641 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Patrik Hagglundc494d242012-12-17 14:30:06 +0000642
643 // Only handle simple types.
644 if (!CEVT.isSimple()) return 0;
645 MVT VT = CEVT.getSimpleVT();
Eric Christopher3cf63f12010-09-09 00:19:41 +0000646
647 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
648 return ARMMaterializeFP(CFP, VT);
Eric Christopher83a5ec82010-10-01 23:24:42 +0000649 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
650 return ARMMaterializeGV(GV, VT);
651 else if (isa<ConstantInt>(C))
652 return ARMMaterializeInt(C, VT);
Eric Christopher7ac602b2010-10-11 08:38:55 +0000653
Eric Christopher83a5ec82010-10-01 23:24:42 +0000654 return 0;
Eric Christopher3cf63f12010-09-09 00:19:41 +0000655}
656
Chad Rosier0eff3e52011-11-17 21:46:13 +0000657// TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
658
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000659unsigned ARMFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000660 // Don't handle dynamic allocas.
661 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000662
Duncan Sandsf5dda012010-11-03 11:35:31 +0000663 MVT VT;
Chad Rosier466d3d82012-05-11 16:41:38 +0000664 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
Eric Christopher7ac602b2010-10-11 08:38:55 +0000665
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000666 DenseMap<const AllocaInst*, int>::iterator SI =
667 FuncInfo.StaticAllocaMap.find(AI);
668
669 // This will get lowered later into the correct offsets and registers
670 // via rewriteXFrameIndex.
671 if (SI != FuncInfo.StaticAllocaMap.end()) {
Tim Northover76fc8a42013-12-11 16:04:57 +0000672 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Craig Topper760b1342012-02-22 05:59:10 +0000673 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000674 unsigned ResultReg = createResultReg(RC);
Tim Northover76fc8a42013-12-11 16:04:57 +0000675 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
676
Rafael Espindolaea09c592014-02-18 22:05:46 +0000677 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000678 TII.get(Opc), ResultReg)
679 .addFrameIndex(SI->second)
680 .addImm(0));
681 return ResultReg;
682 }
Eric Christopher7ac602b2010-10-11 08:38:55 +0000683
Eric Christopher78f8d4e2010-09-30 20:49:44 +0000684 return 0;
685}
686
Chris Lattner229907c2011-07-18 04:54:35 +0000687bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000688 EVT evt = TLI.getValueType(DL, Ty, true);
Eric Christopher2ff757d2010-09-09 01:06:51 +0000689
Eric Christopher761e7fb2010-08-25 07:23:49 +0000690 // Only handle simple types.
Duncan Sandsf5dda012010-11-03 11:35:31 +0000691 if (evt == MVT::Other || !evt.isSimple()) return false;
692 VT = evt.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +0000693
Eric Christopher901176a2010-08-31 01:28:42 +0000694 // Handle all legal types, i.e. a register that will directly hold this
695 // value.
696 return TLI.isTypeLegal(VT);
Eric Christopher761e7fb2010-08-25 07:23:49 +0000697}
698
Chris Lattner229907c2011-07-18 04:54:35 +0000699bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000700 if (isTypeLegal(Ty, VT)) return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000701
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000702 // If this is a type than can be sign or zero-extended to a basic operation
703 // go ahead and accept it now.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000704 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000705 return true;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000706
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000707 return false;
708}
709
Eric Christopher558b61e2010-11-19 22:36:41 +0000710// Computes the address to get to an object.
Eric Christopherfef5f312010-11-19 22:30:02 +0000711bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000712 // Some boilerplate from the X86 FastISel.
Craig Topper062a2ba2014-04-25 05:30:21 +0000713 const User *U = nullptr;
Eric Christopher00202ee2010-08-23 21:44:12 +0000714 unsigned Opcode = Instruction::UserOp1;
Eric Christopher9d4e4712010-08-24 00:07:24 +0000715 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christophercee83d62010-11-19 22:37:58 +0000716 // Don't walk into other basic blocks unless the object is an alloca from
717 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher96494372010-11-15 21:11:06 +0000718 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
719 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
720 Opcode = I->getOpcode();
721 U = I;
722 }
Eric Christopher9d4e4712010-08-24 00:07:24 +0000723 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher00202ee2010-08-23 21:44:12 +0000724 Opcode = C->getOpcode();
725 U = C;
726 }
727
Chris Lattner229907c2011-07-18 04:54:35 +0000728 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher00202ee2010-08-23 21:44:12 +0000729 if (Ty->getAddressSpace() > 255)
730 // Fast instruction selection doesn't support the special
731 // address spaces.
732 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +0000733
Eric Christopher00202ee2010-08-23 21:44:12 +0000734 switch (Opcode) {
Eric Christopher2ff757d2010-09-09 01:06:51 +0000735 default:
Eric Christopher00202ee2010-08-23 21:44:12 +0000736 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000737 case Instruction::BitCast:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000738 // Look through bitcasts.
Eric Christopherfef5f312010-11-19 22:30:02 +0000739 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher3931cf92013-07-12 22:08:24 +0000740 case Instruction::IntToPtr:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000741 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000742 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
743 TLI.getPointerTy(DL))
Eric Christopherfef5f312010-11-19 22:30:02 +0000744 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000745 break;
Eric Christopher3931cf92013-07-12 22:08:24 +0000746 case Instruction::PtrToInt:
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000747 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000748 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Eric Christopherfef5f312010-11-19 22:30:02 +0000749 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopherdb3bcc92010-10-12 00:43:21 +0000750 break;
Eric Christopher21d0c172010-10-14 09:29:41 +0000751 case Instruction::GetElementPtr: {
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000752 Address SavedAddr = Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +0000753 int TmpOffset = Addr.Offset;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000754
Eric Christopher21d0c172010-10-14 09:29:41 +0000755 // Iterate through the GEP folding the constants into offsets where
756 // we can.
757 gep_type_iterator GTI = gep_type_begin(U);
758 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
759 i != e; ++i, ++GTI) {
760 const Value *Op = *i;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000761 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000762 const StructLayout *SL = DL.getStructLayout(STy);
Eric Christopher21d0c172010-10-14 09:29:41 +0000763 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
764 TmpOffset += SL->getElementOffset(Idx);
765 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000766 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Eugene Zelenko342257e2017-01-31 00:56:17 +0000767 while (true) {
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000768 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
769 // Constant-offset addressing.
770 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000771 break;
772 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000773 if (canFoldAddIntoGEP(U, Op)) {
774 // A compatible add with a constant operand. Fold the constant.
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000775 ConstantInt *CI =
Eric Christophera5a779e2011-03-22 19:39:17 +0000776 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000777 TmpOffset += CI->getSExtValue() * S;
Eric Christophera5a779e2011-03-22 19:39:17 +0000778 // Iterate on the other operand.
779 Op = cast<AddOperator>(Op)->getOperand(0);
780 continue;
Eric Christopher501d2e22011-04-29 00:03:10 +0000781 }
Eric Christophera5a779e2011-03-22 19:39:17 +0000782 // Unsupported
783 goto unsupported_gep;
784 }
Eric Christopher21d0c172010-10-14 09:29:41 +0000785 }
786 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000787
788 // Try to grab the base operand now.
Eric Christopherfef5f312010-11-19 22:30:02 +0000789 Addr.Offset = TmpOffset;
790 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000791
792 // We failed, restore everything and try the other options.
Eric Christopher35e2d7f2010-11-19 22:39:56 +0000793 Addr = SavedAddr;
Eric Christophere4b3d6b2010-10-15 18:02:07 +0000794
Eric Christopher21d0c172010-10-14 09:29:41 +0000795 unsupported_gep:
Eric Christopher21d0c172010-10-14 09:29:41 +0000796 break;
797 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000798 case Instruction::Alloca: {
Eric Christopher7cd5cda2010-10-12 05:39:06 +0000799 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000800 DenseMap<const AllocaInst*, int>::iterator SI =
801 FuncInfo.StaticAllocaMap.find(AI);
802 if (SI != FuncInfo.StaticAllocaMap.end()) {
803 Addr.BaseType = Address::FrameIndexBase;
804 Addr.Base.FI = SI->second;
805 return true;
806 }
807 break;
Eric Christopher00202ee2010-08-23 21:44:12 +0000808 }
809 }
Eric Christopher2ff757d2010-09-09 01:06:51 +0000810
Eric Christopher9d4e4712010-08-24 00:07:24 +0000811 // Try to get this in a register if nothing else has worked.
Eric Christopherfef5f312010-11-19 22:30:02 +0000812 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
813 return Addr.Base.Reg != 0;
Eric Christopher21d0c172010-10-14 09:29:41 +0000814}
815
Chad Rosier150d35b2012-12-17 22:35:29 +0000816void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
Eric Christopher73bc5b02010-10-21 19:40:30 +0000817 bool needsLowering = false;
Chad Rosier150d35b2012-12-17 22:35:29 +0000818 switch (VT.SimpleTy) {
Craig Toppere55c5562012-02-07 02:50:20 +0000819 default: llvm_unreachable("Unhandled load/store type!");
Eric Christopher73bc5b02010-10-21 19:40:30 +0000820 case MVT::i1:
821 case MVT::i8:
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000822 case MVT::i16:
Eric Christopher73bc5b02010-10-21 19:40:30 +0000823 case MVT::i32:
Chad Rosieradfd2002011-11-14 20:22:27 +0000824 if (!useAM3) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000825 // Integer loads/stores handle 12-bit offsets.
826 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Chad Rosieradfd2002011-11-14 20:22:27 +0000827 // Handle negative offsets.
Chad Rosier45110fd2011-11-14 22:34:48 +0000828 if (needsLowering && isThumb2)
829 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
830 Addr.Offset > -256);
Chad Rosieradfd2002011-11-14 20:22:27 +0000831 } else {
Chad Rosier5196efd2011-11-13 04:25:02 +0000832 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
Chad Rosier2a1df882011-11-14 04:09:28 +0000833 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
Chad Rosieradfd2002011-11-14 20:22:27 +0000834 }
Eric Christopher73bc5b02010-10-21 19:40:30 +0000835 break;
836 case MVT::f32:
837 case MVT::f64:
838 // Floating point operands handle 8-bit offsets.
Eric Christopherfef5f312010-11-19 22:30:02 +0000839 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher73bc5b02010-10-21 19:40:30 +0000840 break;
841 }
Jim Grosbach055de2c2010-10-27 21:39:08 +0000842
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000843 // If this is a stack pointer and the offset needs to be simplified then
844 // put the alloca address into a register, set the base type back to
845 // register and continue. This should almost never happen.
846 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
Craig Topper61e88f42014-11-21 05:58:21 +0000847 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass
848 : &ARM::GPRRegClass;
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000849 unsigned ResultReg = createResultReg(RC);
Chad Rosier0439cfc2011-11-08 21:12:00 +0000850 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000851 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher0a3c28b2010-11-20 22:38:27 +0000852 TII.get(Opc), ResultReg)
853 .addFrameIndex(Addr.Base.FI)
854 .addImm(0));
855 Addr.Base.Reg = ResultReg;
856 Addr.BaseType = Address::RegBase;
857 }
858
Eric Christopher73bc5b02010-10-21 19:40:30 +0000859 // Since the offset is too large for the load/store instruction
Eric Christopher74487fc2010-09-02 00:53:56 +0000860 // get the reg+offset into a register.
Eric Christopher73bc5b02010-10-21 19:40:30 +0000861 if (needsLowering) {
Juergen Ributzka88e32512014-09-03 20:56:59 +0000862 Addr.Base.Reg = fastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
Eli Friedman86caced2011-04-29 21:22:56 +0000863 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
Eric Christopherfef5f312010-11-19 22:30:02 +0000864 Addr.Offset = 0;
Eric Christopher74487fc2010-09-02 00:53:56 +0000865 }
Eric Christopher00202ee2010-08-23 21:44:12 +0000866}
867
Chad Rosier150d35b2012-12-17 22:35:29 +0000868void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
Cameron Zwarich6528a542011-05-28 20:34:49 +0000869 const MachineInstrBuilder &MIB,
Justin Lebar0af80cd2016-07-15 18:26:59 +0000870 MachineMemOperand::Flags Flags,
871 bool useAM3) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000872 // addrmode5 output depends on the selection dag addressing dividing the
873 // offset by 4 that it then later multiplies. Do this here as well.
Chad Rosier150d35b2012-12-17 22:35:29 +0000874 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
Eric Christopher119ff7f2010-12-01 01:40:24 +0000875 Addr.Offset /= 4;
Eric Christopher501d2e22011-04-29 00:03:10 +0000876
Eric Christopher119ff7f2010-12-01 01:40:24 +0000877 // Frame base works a bit differently. Handle it separately.
878 if (Addr.BaseType == Address::FrameIndexBase) {
879 int FI = Addr.Base.FI;
880 int Offset = Addr.Offset;
Alex Lorenze40c8a22015-08-11 23:09:45 +0000881 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
882 MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
883 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
Eric Christopher119ff7f2010-12-01 01:40:24 +0000884 // Now add the rest of the operands.
885 MIB.addFrameIndex(FI);
886
Bob Wilson80381f62011-12-04 00:52:23 +0000887 // ARM halfword load/stores and signed byte loads need an additional
888 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000889 if (useAM3) {
David Majnemere61e4bf2016-06-21 05:10:24 +0000890 int Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
Chad Rosier2a1df882011-11-14 04:09:28 +0000891 MIB.addReg(0);
892 MIB.addImm(Imm);
893 } else {
894 MIB.addImm(Addr.Offset);
895 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000896 MIB.addMemOperand(MMO);
897 } else {
898 // Now add the rest of the operands.
899 MIB.addReg(Addr.Base.Reg);
Eric Christopher501d2e22011-04-29 00:03:10 +0000900
Bob Wilson80381f62011-12-04 00:52:23 +0000901 // ARM halfword load/stores and signed byte loads need an additional
902 // operand.
Chad Rosier2a1df882011-11-14 04:09:28 +0000903 if (useAM3) {
David Majnemere61e4bf2016-06-21 05:10:24 +0000904 int Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
Chad Rosier2a1df882011-11-14 04:09:28 +0000905 MIB.addReg(0);
906 MIB.addImm(Imm);
907 } else {
908 MIB.addImm(Addr.Offset);
909 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000910 }
911 AddOptionalDefs(MIB);
912}
913
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000914bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
Chad Rosier563de602011-12-13 19:22:14 +0000915 unsigned Alignment, bool isZExt, bool allocReg) {
Eric Christopher901176a2010-08-31 01:28:42 +0000916 unsigned Opc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000917 bool useAM3 = false;
Chad Rosier563de602011-12-13 19:22:14 +0000918 bool needVMOV = false;
Craig Topper760b1342012-02-22 05:59:10 +0000919 const TargetRegisterClass *RC;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000920 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +0000921 // This is mostly going to be Neon/vector support.
922 default: return false;
Chad Rosier023ede52011-11-11 02:38:59 +0000923 case MVT::i1:
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000924 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +0000925 if (isThumb2) {
926 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
927 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
928 else
929 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000930 } else {
Chad Rosieradfd2002011-11-14 20:22:27 +0000931 if (isZExt) {
932 Opc = ARM::LDRBi12;
933 } else {
934 Opc = ARM::LDRSB;
935 useAM3 = true;
936 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000937 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000938 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher3ce9c4a2010-09-01 18:01:32 +0000939 break;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000940 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +0000941 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +0000942 return false;
943
Chad Rosieradfd2002011-11-14 20:22:27 +0000944 if (isThumb2) {
945 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
946 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
947 else
948 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
949 } else {
950 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
951 useAM3 = true;
952 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000953 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier2f27fab2011-11-09 21:30:12 +0000954 break;
Eric Christopher901176a2010-08-31 01:28:42 +0000955 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +0000956 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +0000957 return false;
958
Chad Rosieradfd2002011-11-14 20:22:27 +0000959 if (isThumb2) {
960 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
961 Opc = ARM::t2LDRi8;
962 else
963 Opc = ARM::t2LDRi12;
964 } else {
965 Opc = ARM::LDRi12;
966 }
JF Bastien652fa6a2013-06-09 00:20:24 +0000967 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Eric Christopher901176a2010-08-31 01:28:42 +0000968 break;
Eric Christopheraef6499b2010-09-18 01:59:37 +0000969 case MVT::f32:
Chad Rosierded61602011-12-14 17:55:03 +0000970 if (!Subtarget->hasVFP2()) return false;
Chad Rosier563de602011-12-13 19:22:14 +0000971 // Unaligned loads need special handling. Floats require word-alignment.
972 if (Alignment && Alignment < 4) {
973 needVMOV = true;
974 VT = MVT::i32;
975 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
JF Bastien652fa6a2013-06-09 00:20:24 +0000976 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
Chad Rosier563de602011-12-13 19:22:14 +0000977 } else {
978 Opc = ARM::VLDRS;
979 RC = TLI.getRegClassFor(VT);
980 }
Eric Christopheraef6499b2010-09-18 01:59:37 +0000981 break;
982 case MVT::f64:
Chad Rosierded61602011-12-14 17:55:03 +0000983 if (!Subtarget->hasVFP2()) return false;
Chad Rosiera26979b2011-12-14 17:26:05 +0000984 // FIXME: Unaligned loads need special handling. Doublewords require
985 // word-alignment.
986 if (Alignment && Alignment < 4)
Chad Rosier563de602011-12-13 19:22:14 +0000987 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +0000988
Eric Christopheraef6499b2010-09-18 01:59:37 +0000989 Opc = ARM::VLDRD;
Eric Christophera2583ea2010-10-07 05:50:44 +0000990 RC = TLI.getRegClassFor(VT);
Eric Christopheraef6499b2010-09-18 01:59:37 +0000991 break;
Eric Christopher761e7fb2010-08-25 07:23:49 +0000992 }
Eric Christopher119ff7f2010-12-01 01:40:24 +0000993 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000994 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +0000995
Eric Christopher119ff7f2010-12-01 01:40:24 +0000996 // Create the base instruction, then add the operands.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +0000997 if (allocReg)
998 ResultReg = createResultReg(RC);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000999 assert(ResultReg > 255 && "Expected an allocated virtual register.");
Rafael Espindolaea09c592014-02-18 22:05:46 +00001000 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001001 TII.get(Opc), ResultReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001002 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
Chad Rosier563de602011-12-13 19:22:14 +00001003
1004 // If we had an unaligned load of a float we've converted it to an regular
1005 // load. Now we must move from the GRP to the FP register.
1006 if (needVMOV) {
1007 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001008 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier563de602011-12-13 19:22:14 +00001009 TII.get(ARM::VMOVSR), MoveReg)
1010 .addReg(ResultReg));
1011 ResultReg = MoveReg;
1012 }
Eric Christopher901176a2010-08-31 01:28:42 +00001013 return true;
Eric Christopher761e7fb2010-08-25 07:23:49 +00001014}
1015
Eric Christopher29ab6d12010-09-27 06:02:23 +00001016bool ARMFastISel::SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001017 // Atomic loads need special handling.
1018 if (cast<LoadInst>(I)->isAtomic())
1019 return false;
1020
Manman Ren57518142016-04-11 21:08:06 +00001021 const Value *SV = I->getOperand(0);
1022 if (TLI.supportSwiftError()) {
1023 // Swifterror values can come from either a function parameter with
1024 // swifterror attribute or an alloca with swifterror attribute.
1025 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1026 if (Arg->hasSwiftErrorAttr())
1027 return false;
1028 }
1029
1030 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1031 if (Alloca->isSwiftError())
1032 return false;
1033 }
1034 }
1035
Eric Christopher860fc932010-09-10 00:34:35 +00001036 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001037 MVT VT;
Eric Christopher860fc932010-09-10 00:34:35 +00001038 if (!isLoadTypeLegal(I->getType(), VT))
1039 return false;
1040
Eric Christopher119ff7f2010-12-01 01:40:24 +00001041 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001042 Address Addr;
Eric Christopher119ff7f2010-12-01 01:40:24 +00001043 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopher860fc932010-09-10 00:34:35 +00001044
1045 unsigned ResultReg;
Chad Rosier563de602011-12-13 19:22:14 +00001046 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1047 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001048 updateValueMap(I, ResultReg);
Eric Christopher860fc932010-09-10 00:34:35 +00001049 return true;
1050}
1051
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001052bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
Bob Wilson80381f62011-12-04 00:52:23 +00001053 unsigned Alignment) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001054 unsigned StrOpc;
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001055 bool useAM3 = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001056 switch (VT.SimpleTy) {
Eric Christopher119ff7f2010-12-01 01:40:24 +00001057 // This is mostly going to be Neon/vector support.
Eric Christopher74487fc2010-09-02 00:53:56 +00001058 default: return false;
Eric Christopher1e43892e2010-11-02 23:59:09 +00001059 case MVT::i1: {
Craig Topper61e88f42014-11-21 05:58:21 +00001060 unsigned Res = createResultReg(isThumb2 ? &ARM::tGPRRegClass
1061 : &ARM::GPRRegClass);
Chad Rosier0439cfc2011-11-08 21:12:00 +00001062 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001063 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001064 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher1e43892e2010-11-02 23:59:09 +00001065 TII.get(Opc), Res)
1066 .addReg(SrcReg).addImm(1));
1067 SrcReg = Res;
Justin Bognerb03fd122016-08-17 05:10:15 +00001068 LLVM_FALLTHROUGH;
1069 }
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001070 case MVT::i8:
Chad Rosieradfd2002011-11-14 20:22:27 +00001071 if (isThumb2) {
1072 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1073 StrOpc = ARM::t2STRBi8;
1074 else
1075 StrOpc = ARM::t2STRBi12;
1076 } else {
1077 StrOpc = ARM::STRBi12;
1078 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001079 break;
1080 case MVT::i16:
Chad Rosier66bb1782012-11-09 18:25:27 +00001081 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
Chad Rosier2364f582012-09-21 00:41:42 +00001082 return false;
1083
Chad Rosieradfd2002011-11-14 20:22:27 +00001084 if (isThumb2) {
1085 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1086 StrOpc = ARM::t2STRHi8;
1087 else
1088 StrOpc = ARM::t2STRHi12;
1089 } else {
1090 StrOpc = ARM::STRH;
1091 useAM3 = true;
1092 }
Eric Christopher7cd5cda2010-10-12 05:39:06 +00001093 break;
Eric Christopherc918d552010-10-16 01:10:35 +00001094 case MVT::i32:
Chad Rosier66bb1782012-11-09 18:25:27 +00001095 if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
Chad Rosier8bf01fc2012-09-21 16:58:35 +00001096 return false;
1097
Chad Rosieradfd2002011-11-14 20:22:27 +00001098 if (isThumb2) {
1099 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1100 StrOpc = ARM::t2STRi8;
1101 else
1102 StrOpc = ARM::t2STRi12;
1103 } else {
1104 StrOpc = ARM::STRi12;
1105 }
Eric Christopherc918d552010-10-16 01:10:35 +00001106 break;
Eric Christopherc3e118e2010-09-02 23:43:26 +00001107 case MVT::f32:
1108 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001109 // Unaligned stores need special handling. Floats require word-alignment.
Chad Rosierec3b77e2011-12-03 02:21:57 +00001110 if (Alignment && Alignment < 4) {
1111 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001112 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosierec3b77e2011-12-03 02:21:57 +00001113 TII.get(ARM::VMOVRS), MoveReg)
1114 .addReg(SrcReg));
1115 SrcReg = MoveReg;
1116 VT = MVT::i32;
1117 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
Chad Rosierfce28912011-12-14 17:32:02 +00001118 } else {
1119 StrOpc = ARM::VSTRS;
Chad Rosierec3b77e2011-12-03 02:21:57 +00001120 }
Eric Christopherc3e118e2010-09-02 23:43:26 +00001121 break;
1122 case MVT::f64:
1123 if (!Subtarget->hasVFP2()) return false;
Chad Rosierc77830d2011-12-06 01:44:17 +00001124 // FIXME: Unaligned stores need special handling. Doublewords require
1125 // word-alignment.
Chad Rosiera26979b2011-12-14 17:26:05 +00001126 if (Alignment && Alignment < 4)
Chad Rosierec3b77e2011-12-03 02:21:57 +00001127 return false;
Chad Rosiera26979b2011-12-14 17:26:05 +00001128
Eric Christopherc3e118e2010-09-02 23:43:26 +00001129 StrOpc = ARM::VSTRD;
1130 break;
Eric Christopher74487fc2010-09-02 00:53:56 +00001131 }
Eric Christopher119ff7f2010-12-01 01:40:24 +00001132 // Simplify this down to something we can handle.
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001133 ARMSimplifyAddress(Addr, VT, useAM3);
Jim Grosbach055de2c2010-10-27 21:39:08 +00001134
Eric Christopher119ff7f2010-12-01 01:40:24 +00001135 // Create the base instruction, then add the operands.
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001136 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001137 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher119ff7f2010-12-01 01:40:24 +00001138 TII.get(StrOpc))
Chad Rosierce619dd2011-11-17 01:16:53 +00001139 .addReg(SrcReg);
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00001140 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
Eric Christopher74487fc2010-09-02 00:53:56 +00001141 return true;
1142}
1143
Eric Christopher29ab6d12010-09-27 06:02:23 +00001144bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher74487fc2010-09-02 00:53:56 +00001145 Value *Op0 = I->getOperand(0);
1146 unsigned SrcReg = 0;
1147
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001148 // Atomic stores need special handling.
1149 if (cast<StoreInst>(I)->isAtomic())
1150 return false;
1151
Manman Ren57518142016-04-11 21:08:06 +00001152 const Value *PtrV = I->getOperand(1);
1153 if (TLI.supportSwiftError()) {
1154 // Swifterror values can come from either a function parameter with
1155 // swifterror attribute or an alloca with swifterror attribute.
1156 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1157 if (Arg->hasSwiftErrorAttr())
1158 return false;
1159 }
1160
1161 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1162 if (Alloca->isSwiftError())
1163 return false;
1164 }
1165 }
1166
Eric Christopher119ff7f2010-12-01 01:40:24 +00001167 // Verify we have a legal type before going any further.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001168 MVT VT;
Eric Christopher74487fc2010-09-02 00:53:56 +00001169 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001170 return false;
Eric Christopher74487fc2010-09-02 00:53:56 +00001171
Eric Christopher92db2012010-09-02 01:48:11 +00001172 // Get the value to be stored into a register.
1173 SrcReg = getRegForValue(Op0);
Eric Christopher119ff7f2010-12-01 01:40:24 +00001174 if (SrcReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001175
Eric Christopher119ff7f2010-12-01 01:40:24 +00001176 // See if we can handle this address.
Eric Christopherfef5f312010-11-19 22:30:02 +00001177 Address Addr;
Eric Christopherfef5f312010-11-19 22:30:02 +00001178 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher74487fc2010-09-02 00:53:56 +00001179 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001180
Chad Rosierec3b77e2011-12-03 02:21:57 +00001181 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1182 return false;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001183 return true;
1184}
1185
1186static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1187 switch (Pred) {
1188 // Needs two compares...
1189 case CmpInst::FCMP_ONE:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001190 case CmpInst::FCMP_UEQ:
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001191 default:
Eric Christopherb2abb502010-11-02 01:24:49 +00001192 // AL is our "false" for now. The other two need more compares.
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001193 return ARMCC::AL;
1194 case CmpInst::ICMP_EQ:
1195 case CmpInst::FCMP_OEQ:
1196 return ARMCC::EQ;
1197 case CmpInst::ICMP_SGT:
1198 case CmpInst::FCMP_OGT:
1199 return ARMCC::GT;
1200 case CmpInst::ICMP_SGE:
1201 case CmpInst::FCMP_OGE:
1202 return ARMCC::GE;
1203 case CmpInst::ICMP_UGT:
1204 case CmpInst::FCMP_UGT:
1205 return ARMCC::HI;
1206 case CmpInst::FCMP_OLT:
1207 return ARMCC::MI;
1208 case CmpInst::ICMP_ULE:
1209 case CmpInst::FCMP_OLE:
1210 return ARMCC::LS;
1211 case CmpInst::FCMP_ORD:
1212 return ARMCC::VC;
1213 case CmpInst::FCMP_UNO:
1214 return ARMCC::VS;
1215 case CmpInst::FCMP_UGE:
1216 return ARMCC::PL;
1217 case CmpInst::ICMP_SLT:
1218 case CmpInst::FCMP_ULT:
Eric Christopher7ac602b2010-10-11 08:38:55 +00001219 return ARMCC::LT;
Eric Christopher2ccc1aa2010-09-17 22:28:18 +00001220 case CmpInst::ICMP_SLE:
1221 case CmpInst::FCMP_ULE:
1222 return ARMCC::LE;
1223 case CmpInst::FCMP_UNE:
1224 case CmpInst::ICMP_NE:
1225 return ARMCC::NE;
1226 case CmpInst::ICMP_UGE:
1227 return ARMCC::HS;
1228 case CmpInst::ICMP_ULT:
1229 return ARMCC::LO;
1230 }
Eric Christopherfde5a3d2010-09-01 22:16:27 +00001231}
1232
Eric Christopher29ab6d12010-09-27 06:02:23 +00001233bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christopher6aaed722010-09-03 00:35:47 +00001234 const BranchInst *BI = cast<BranchInst>(I);
1235 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1236 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopher2ff757d2010-09-09 01:06:51 +00001237
Eric Christopher6aaed722010-09-03 00:35:47 +00001238 // Simple branch support.
Jim Grosbach68147ee2010-11-09 19:22:26 +00001239
Eric Christopher5c308f82010-10-29 21:08:19 +00001240 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1241 // behavior.
Eric Christopher5c308f82010-10-29 21:08:19 +00001242 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001243 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Eric Christopher5c308f82010-10-29 21:08:19 +00001244 // Get the compare predicate.
Eric Christopher26b8ac42011-04-29 21:56:31 +00001245 // Try to take advantage of fallthrough opportunities.
1246 CmpInst::Predicate Predicate = CI->getPredicate();
1247 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1248 std::swap(TBB, FBB);
1249 Predicate = CmpInst::getInversePredicate(Predicate);
1250 }
1251
1252 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
Eric Christopher5c308f82010-10-29 21:08:19 +00001253
1254 // We may not handle every CC for now.
1255 if (ARMPred == ARMCC::AL) return false;
1256
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001257 // Emit the compare.
James Molloyd5087892017-02-13 12:32:47 +00001258 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
1259 CI->isEquality()))
Chad Rosiereafbf3f2011-10-26 23:17:28 +00001260 return false;
Jim Grosbach68147ee2010-11-09 19:22:26 +00001261
Chad Rosier0439cfc2011-11-08 21:12:00 +00001262 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher5c308f82010-10-29 21:08:19 +00001264 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
Matthias Braunccfc9c82015-08-26 01:55:47 +00001265 finishCondBranch(BI->getParent(), TBB, FBB);
Eric Christopher5c308f82010-10-29 21:08:19 +00001266 return true;
1267 }
Eric Christopher8d46b472011-04-29 20:02:39 +00001268 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1269 MVT SourceVT;
1270 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
Eli Friedmanc7035512011-05-25 23:49:02 +00001271 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
Chad Rosier0439cfc2011-11-08 21:12:00 +00001272 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Eric Christopher8d46b472011-04-29 20:02:39 +00001273 unsigned OpReg = getRegForValue(TI->getOperand(0));
Jim Grosbach667b1472013-08-26 20:22:05 +00001274 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001275 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8d46b472011-04-29 20:02:39 +00001276 TII.get(TstOpc))
1277 .addReg(OpReg).addImm(1));
1278
1279 unsigned CCMode = ARMCC::NE;
1280 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1281 std::swap(TBB, FBB);
1282 CCMode = ARMCC::EQ;
1283 }
1284
Chad Rosier0439cfc2011-11-08 21:12:00 +00001285 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001286 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher8d46b472011-04-29 20:02:39 +00001287 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1288
Matthias Braunccfc9c82015-08-26 01:55:47 +00001289 finishCondBranch(BI->getParent(), TBB, FBB);
Eric Christopher8d46b472011-04-29 20:02:39 +00001290 return true;
1291 }
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001292 } else if (const ConstantInt *CI =
1293 dyn_cast<ConstantInt>(BI->getCondition())) {
1294 uint64_t Imm = CI->getZExtValue();
1295 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001296 fastEmitBranch(Target, DbgLoc);
Chad Rosierd24e7e1d2011-10-27 00:21:16 +00001297 return true;
Eric Christopher5c308f82010-10-29 21:08:19 +00001298 }
Jim Grosbach68147ee2010-11-09 19:22:26 +00001299
Eric Christopher5c308f82010-10-29 21:08:19 +00001300 unsigned CmpReg = getRegForValue(BI->getCondition());
1301 if (CmpReg == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001302
Stuart Hastingsebddfe62011-04-16 03:31:26 +00001303 // We've been divorced from our compare! Our block was split, and
1304 // now our compare lives in a predecessor block. We musn't
1305 // re-compare here, as the children of the compare aren't guaranteed
1306 // live across the block boundary (we *could* check for this).
1307 // Regardless, the compare has been done in the predecessor block,
1308 // and it left a value for us in a virtual register. Ergo, we test
1309 // the one-bit value left in the virtual register.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001310 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
Jim Grosbach667b1472013-08-26 20:22:05 +00001311 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001312 AddOptionalDefs(
1313 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
1314 .addReg(CmpReg)
1315 .addImm(1));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001316
Eric Christopher4f012fd2011-04-28 16:52:09 +00001317 unsigned CCMode = ARMCC::NE;
1318 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1319 std::swap(TBB, FBB);
1320 CCMode = ARMCC::EQ;
1321 }
1322
Chad Rosier0439cfc2011-11-08 21:12:00 +00001323 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
Eric Christopher4f012fd2011-04-28 16:52:09 +00001325 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
Matthias Braunccfc9c82015-08-26 01:55:47 +00001326 finishCondBranch(BI->getParent(), TBB, FBB);
Eric Christopher7ac602b2010-10-11 08:38:55 +00001327 return true;
Eric Christopher6aaed722010-09-03 00:35:47 +00001328}
1329
Chad Rosierded4c992012-02-07 23:56:08 +00001330bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1331 unsigned AddrReg = getRegForValue(I->getOperand(0));
1332 if (AddrReg == 0) return false;
1333
1334 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
Joerg Sonnenberger0f76a352017-08-28 20:20:47 +00001335 assert(isThumb2 || Subtarget->hasV4TOps());
1336
Rafael Espindolaea09c592014-02-18 22:05:46 +00001337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1338 TII.get(Opc)).addReg(AddrReg));
Bill Wendling12cda502012-10-22 23:30:04 +00001339
1340 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001341 for (const BasicBlock *SuccBB : IB->successors())
1342 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Wendling12cda502012-10-22 23:30:04 +00001343
Jush Luac96b762012-06-14 06:08:19 +00001344 return true;
Chad Rosierded4c992012-02-07 23:56:08 +00001345}
1346
Chad Rosier9cf803c2011-11-02 18:08:25 +00001347bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
James Molloyd5087892017-02-13 12:32:47 +00001348 bool isZExt, bool isEquality) {
Chad Rosier78127d32011-10-26 23:25:44 +00001349 Type *Ty = Src1Value->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +00001350 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001351 if (!SrcEVT.isSimple()) return false;
1352 MVT SrcVT = SrcEVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001353
Tim Northover063a56e2017-02-23 22:35:00 +00001354 if (Ty->isFloatTy() && !Subtarget->hasVFP2())
1355 return false;
1356
1357 if (Ty->isDoubleTy() && (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP()))
Eric Christopherc3e9c402010-09-08 23:13:45 +00001358 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001359
Chad Rosier595d4192011-11-09 03:22:02 +00001360 // Check to see if the 2nd operand is a constant that we can encode directly
1361 // in the compare.
Chad Rosiere19b0a92011-11-11 06:27:41 +00001362 int Imm = 0;
1363 bool UseImm = false;
Chad Rosier595d4192011-11-09 03:22:02 +00001364 bool isNegativeImm = false;
Chad Rosieraf13d762011-11-16 00:32:20 +00001365 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1366 // Thus, Src1Value may be a ConstantInt, but we're missing it.
Chad Rosier595d4192011-11-09 03:22:02 +00001367 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1368 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1369 SrcVT == MVT::i1) {
1370 const APInt &CIVal = ConstInt->getValue();
Chad Rosiere19b0a92011-11-11 06:27:41 +00001371 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
Chad Rosier26d05882012-03-15 22:54:20 +00001372 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
Jim Grosbach1a597112014-04-03 23:43:18 +00001373 // then a cmn, because there is no way to represent 2147483648 as a
Chad Rosier26d05882012-03-15 22:54:20 +00001374 // signed 32-bit int.
1375 if (Imm < 0 && Imm != (int)0x80000000) {
1376 isNegativeImm = true;
1377 Imm = -Imm;
Chad Rosier3fbd0942011-11-10 01:30:39 +00001378 }
Chad Rosier26d05882012-03-15 22:54:20 +00001379 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1380 (ARM_AM::getSOImmVal(Imm) != -1);
Chad Rosier595d4192011-11-09 03:22:02 +00001381 }
1382 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1383 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1384 if (ConstFP->isZero() && !ConstFP->isNegative())
Chad Rosiere19b0a92011-11-11 06:27:41 +00001385 UseImm = true;
Chad Rosier595d4192011-11-09 03:22:02 +00001386 }
1387
Eric Christopherc3e9c402010-09-08 23:13:45 +00001388 unsigned CmpOpc;
Chad Rosier595d4192011-11-09 03:22:02 +00001389 bool isICmp = true;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001390 bool needsExt = false;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001391 switch (SrcVT.SimpleTy) {
Eric Christopherc3e9c402010-09-08 23:13:45 +00001392 default: return false;
1393 // TODO: Verify compares.
1394 case MVT::f32:
Chad Rosier595d4192011-11-09 03:22:02 +00001395 isICmp = false;
James Molloyd5087892017-02-13 12:32:47 +00001396 // Equality comparisons shouldn't raise Invalid on uordered inputs.
1397 if (isEquality)
1398 CmpOpc = UseImm ? ARM::VCMPZS : ARM::VCMPS;
1399 else
1400 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001401 break;
1402 case MVT::f64:
Chad Rosier595d4192011-11-09 03:22:02 +00001403 isICmp = false;
James Molloyd5087892017-02-13 12:32:47 +00001404 // Equality comparisons shouldn't raise Invalid on uordered inputs.
1405 if (isEquality)
1406 CmpOpc = UseImm ? ARM::VCMPZD : ARM::VCMPD;
1407 else
Chad Rosiere19b0a92011-11-11 06:27:41 +00001408 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
Eric Christopherc3e9c402010-09-08 23:13:45 +00001409 break;
Chad Rosier9cf803c2011-11-02 18:08:25 +00001410 case MVT::i1:
1411 case MVT::i8:
1412 case MVT::i16:
1413 needsExt = true;
1414 // Intentional fall-through.
Eric Christopherc3e9c402010-09-08 23:13:45 +00001415 case MVT::i32:
Chad Rosier595d4192011-11-09 03:22:02 +00001416 if (isThumb2) {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001417 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001418 CmpOpc = ARM::t2CMPrr;
1419 else
Bill Wendling4b796472012-06-11 08:07:26 +00001420 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001421 } else {
Chad Rosiere19b0a92011-11-11 06:27:41 +00001422 if (!UseImm)
Chad Rosier595d4192011-11-09 03:22:02 +00001423 CmpOpc = ARM::CMPrr;
1424 else
Bill Wendling4b796472012-06-11 08:07:26 +00001425 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
Chad Rosier595d4192011-11-09 03:22:02 +00001426 }
Eric Christopherc3e9c402010-09-08 23:13:45 +00001427 break;
1428 }
1429
Chad Rosier9cf803c2011-11-02 18:08:25 +00001430 unsigned SrcReg1 = getRegForValue(Src1Value);
1431 if (SrcReg1 == 0) return false;
Chad Rosier59a20192011-10-26 22:47:55 +00001432
Duncan Sands12330652011-11-28 10:31:27 +00001433 unsigned SrcReg2 = 0;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001434 if (!UseImm) {
Chad Rosier595d4192011-11-09 03:22:02 +00001435 SrcReg2 = getRegForValue(Src2Value);
1436 if (SrcReg2 == 0) return false;
1437 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001438
1439 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1440 if (needsExt) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001441 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1442 if (SrcReg1 == 0) return false;
Chad Rosiere19b0a92011-11-11 06:27:41 +00001443 if (!UseImm) {
Chad Rosiera0d3c752012-02-16 22:45:33 +00001444 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1445 if (SrcReg2 == 0) return false;
Chad Rosier595d4192011-11-09 03:22:02 +00001446 }
Chad Rosier9cf803c2011-11-02 18:08:25 +00001447 }
Chad Rosier59a20192011-10-26 22:47:55 +00001448
Jim Grosbachd7866792013-08-16 23:37:40 +00001449 const MCInstrDesc &II = TII.get(CmpOpc);
1450 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
Chad Rosiere19b0a92011-11-11 06:27:41 +00001451 if (!UseImm) {
Jim Grosbachd7866792013-08-16 23:37:40 +00001452 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
David Blaikie3ef249c92015-01-30 23:04:39 +00001453 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001454 .addReg(SrcReg1).addReg(SrcReg2));
1455 } else {
1456 MachineInstrBuilder MIB;
David Blaikie3ef249c92015-01-30 23:04:39 +00001457 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Chad Rosier595d4192011-11-09 03:22:02 +00001458 .addReg(SrcReg1);
1459
1460 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1461 if (isICmp)
Chad Rosiere19b0a92011-11-11 06:27:41 +00001462 MIB.addImm(Imm);
Chad Rosier595d4192011-11-09 03:22:02 +00001463 AddOptionalDefs(MIB);
1464 }
Chad Rosier78127d32011-10-26 23:25:44 +00001465
1466 // For floating point we need to move the result to a comparison register
1467 // that we can then use for branches.
1468 if (Ty->isFloatTy() || Ty->isDoubleTy())
David Blaikie3ef249c92015-01-30 23:04:39 +00001469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier78127d32011-10-26 23:25:44 +00001470 TII.get(ARM::FMSTAT)));
Chad Rosier59a20192011-10-26 22:47:55 +00001471 return true;
1472}
1473
1474bool ARMFastISel::SelectCmp(const Instruction *I) {
1475 const CmpInst *CI = cast<CmpInst>(I);
1476
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001477 // Get the compare predicate.
1478 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopher7ac602b2010-10-11 08:38:55 +00001479
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001480 // We may not handle every CC for now.
1481 if (ARMPred == ARMCC::AL) return false;
1482
Chad Rosier59a20192011-10-26 22:47:55 +00001483 // Emit the compare.
James Molloyd5087892017-02-13 12:32:47 +00001484 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
1485 CI->isEquality()))
Chad Rosier59a20192011-10-26 22:47:55 +00001486 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001487
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001488 // Now set a register based on the comparison. Explicitly set the predicates
1489 // here.
Chad Rosier0439cfc2011-11-08 21:12:00 +00001490 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Craig Topper61e88f42014-11-21 05:58:21 +00001491 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass
1492 : &ARM::GPRRegClass;
Eric Christopher76a97522010-10-07 05:39:19 +00001493 unsigned DestReg = createResultReg(RC);
Chad Rosier78127d32011-10-26 23:25:44 +00001494 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001495 unsigned ZeroReg = fastMaterializeConstant(Zero);
Chad Rosier377f1f22012-03-07 20:59:26 +00001496 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001497 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001498 .addReg(ZeroReg).addImm(1)
Chad Rosier377f1f22012-03-07 20:59:26 +00001499 .addImm(ARMPred).addReg(ARM::CPSR);
Eric Christopher3a7e8cd2010-09-29 01:14:47 +00001500
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001501 updateValueMap(I, DestReg);
Eric Christopherc3e9c402010-09-08 23:13:45 +00001502 return true;
1503}
1504
Eric Christopher29ab6d12010-09-27 06:02:23 +00001505bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001506 // Make sure we have VFP and that we're extending float to double.
Tim Northover063a56e2017-02-23 22:35:00 +00001507 if (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001508
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001509 Value *V = I->getOperand(0);
1510 if (!I->getType()->isDoubleTy() ||
1511 !V->getType()->isFloatTy()) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001512
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001513 unsigned Op = getRegForValue(V);
1514 if (Op == 0) return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001515
Craig Topperc7242e02012-04-20 07:30:17 +00001516 unsigned Result = createResultReg(&ARM::DPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001517 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001518 TII.get(ARM::VCVTDS), Result)
Eric Christopher5903c0b2010-09-09 20:26:31 +00001519 .addReg(Op));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001520 updateValueMap(I, Result);
Eric Christopher5903c0b2010-09-09 20:26:31 +00001521 return true;
1522}
1523
Eric Christopher29ab6d12010-09-27 06:02:23 +00001524bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopher5903c0b2010-09-09 20:26:31 +00001525 // Make sure we have VFP and that we're truncating double to float.
Tim Northover063a56e2017-02-23 22:35:00 +00001526 if (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP()) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001527
1528 Value *V = I->getOperand(0);
Eric Christopher8cfc4592010-10-05 23:13:24 +00001529 if (!(I->getType()->isFloatTy() &&
1530 V->getType()->isDoubleTy())) return false;
Eric Christopher5903c0b2010-09-09 20:26:31 +00001531
1532 unsigned Op = getRegForValue(V);
1533 if (Op == 0) return false;
1534
Craig Topperc7242e02012-04-20 07:30:17 +00001535 unsigned Result = createResultReg(&ARM::SPRRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001536 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher82b05d72010-09-09 20:36:19 +00001537 TII.get(ARM::VCVTSD), Result)
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001538 .addReg(Op));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001539 updateValueMap(I, Result);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00001540 return true;
1541}
1542
Chad Rosiere023d5d2012-02-03 21:14:11 +00001543bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001544 // Make sure we have VFP.
1545 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001546
Duncan Sandsf5dda012010-11-03 11:35:31 +00001547 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001548 Type *Ty = I->getType();
Eric Christopher4bd70472010-09-09 21:44:45 +00001549 if (!isTypeLegal(Ty, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001550 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001551
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001552 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001553 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Patrik Hagglundc494d242012-12-17 14:30:06 +00001554 if (!SrcEVT.isSimple())
1555 return false;
1556 MVT SrcVT = SrcEVT.getSimpleVT();
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001557 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
Eli Friedman5bbb7562011-05-25 19:09:45 +00001558 return false;
1559
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001560 unsigned SrcReg = getRegForValue(Src);
1561 if (SrcReg == 0) return false;
1562
1563 // Handle sign-extension.
1564 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
Chad Rosier62a144f2012-12-17 19:59:43 +00001565 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
Chad Rosiere023d5d2012-02-03 21:14:11 +00001566 /*isZExt*/!isSigned);
Chad Rosiera0d3c752012-02-16 22:45:33 +00001567 if (SrcReg == 0) return false;
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001568 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00001569
Eric Christopher860fc932010-09-10 00:34:35 +00001570 // The conversion routine works on fp-reg to fp-reg and the operand above
1571 // was an integer, move it to the fp registers if possible.
Chad Rosierbf5f4be2011-11-03 02:04:59 +00001572 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001573 if (FP == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001574
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001575 unsigned Opc;
Chad Rosiere023d5d2012-02-03 21:14:11 +00001576 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
Tim Northover063a56e2017-02-23 22:35:00 +00001577 else if (Ty->isDoubleTy() && !Subtarget->isFPOnlySP())
1578 Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001579 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001580
Eric Christopher4bd70472010-09-09 21:44:45 +00001581 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001582 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1583 TII.get(Opc), ResultReg).addReg(FP));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001584 updateValueMap(I, ResultReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001585 return true;
1586}
1587
Chad Rosiere023d5d2012-02-03 21:14:11 +00001588bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001589 // Make sure we have VFP.
1590 if (!Subtarget->hasVFP2()) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001591
Duncan Sandsf5dda012010-11-03 11:35:31 +00001592 MVT DstVT;
Chris Lattner229907c2011-07-18 04:54:35 +00001593 Type *RetTy = I->getType();
Eric Christopher712bd0a2010-09-10 00:35:09 +00001594 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001595 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001596
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001597 unsigned Op = getRegForValue(I->getOperand(0));
1598 if (Op == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001599
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001600 unsigned Opc;
Chris Lattner229907c2011-07-18 04:54:35 +00001601 Type *OpTy = I->getOperand(0)->getType();
Chad Rosiere023d5d2012-02-03 21:14:11 +00001602 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
Tim Northover063a56e2017-02-23 22:35:00 +00001603 else if (OpTy->isDoubleTy() && !Subtarget->isFPOnlySP())
1604 Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
Chad Rosier17847ae2011-08-31 23:49:05 +00001605 else return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001606
Chad Rosier41f0e782012-02-03 20:27:51 +00001607 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
Eric Christopher8cfc4592010-10-05 23:13:24 +00001608 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001609 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1610 TII.get(Opc), ResultReg).addReg(Op));
Eric Christopher7ac602b2010-10-11 08:38:55 +00001611
Eric Christopher4bd70472010-09-09 21:44:45 +00001612 // This result needs to be in an integer register, but the conversion only
1613 // takes place in fp-regs.
Eric Christopher860fc932010-09-10 00:34:35 +00001614 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher4bd70472010-09-09 21:44:45 +00001615 if (IntReg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001616
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001617 updateValueMap(I, IntReg);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00001618 return true;
1619}
1620
Eric Christopher511aa312010-10-11 08:27:59 +00001621bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001622 MVT VT;
1623 if (!isTypeLegal(I->getType(), VT))
Eric Christopher511aa312010-10-11 08:27:59 +00001624 return false;
1625
1626 // Things need to be register sized for register moves.
Duncan Sandsf5dda012010-11-03 11:35:31 +00001627 if (VT != MVT::i32) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001628
1629 unsigned CondReg = getRegForValue(I->getOperand(0));
1630 if (CondReg == 0) return false;
1631 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1632 if (Op1Reg == 0) return false;
Eric Christopher511aa312010-10-11 08:27:59 +00001633
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001634 // Check to see if we can use an immediate in the conditional move.
1635 int Imm = 0;
1636 bool UseImm = false;
1637 bool isNegativeImm = false;
1638 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
Eugene Zelenko342257e2017-01-31 00:56:17 +00001639 assert(VT == MVT::i32 && "Expecting an i32.");
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001640 Imm = (int)ConstInt->getValue().getZExtValue();
1641 if (Imm < 0) {
1642 isNegativeImm = true;
1643 Imm = ~Imm;
1644 }
1645 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1646 (ARM_AM::getSOImmVal(Imm) != -1);
1647 }
1648
Duncan Sands12330652011-11-28 10:31:27 +00001649 unsigned Op2Reg = 0;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001650 if (!UseImm) {
1651 Op2Reg = getRegForValue(I->getOperand(2));
1652 if (Op2Reg == 0) return false;
1653 }
1654
Ahmed Bougachae8d0c4c2015-05-06 04:14:02 +00001655 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1656 CondReg = constrainOperandRegClass(TII.get(TstOpc), CondReg, 0);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001657 AddOptionalDefs(
Ahmed Bougachae8d0c4c2015-05-06 04:14:02 +00001658 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001659 .addReg(CondReg)
Ahmed Bougachae8d0c4c2015-05-06 04:14:02 +00001660 .addImm(1));
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001661
1662 unsigned MovCCOpc;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001663 const TargetRegisterClass *RC;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001664 if (!UseImm) {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001665 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001666 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1667 } else {
Chad Rosier2ec7db02012-11-27 21:46:46 +00001668 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1669 if (!isNegativeImm)
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001670 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
Chad Rosier2ec7db02012-11-27 21:46:46 +00001671 else
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001672 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
Chad Rosier7ddd63c2011-11-11 06:20:39 +00001673 }
Eric Christopher511aa312010-10-11 08:27:59 +00001674 unsigned ResultReg = createResultReg(RC);
Jim Grosbachd7866792013-08-16 23:37:40 +00001675 if (!UseImm) {
Jim Grosbach71a78f92013-08-20 19:12:42 +00001676 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
Jim Grosbachd7866792013-08-16 23:37:40 +00001677 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1679 ResultReg)
1680 .addReg(Op2Reg)
1681 .addReg(Op1Reg)
1682 .addImm(ARMCC::NE)
1683 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001684 } else {
1685 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001686 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
1687 ResultReg)
1688 .addReg(Op1Reg)
1689 .addImm(Imm)
1690 .addImm(ARMCC::EQ)
1691 .addReg(ARM::CPSR);
Jim Grosbachd7866792013-08-16 23:37:40 +00001692 }
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001693 updateValueMap(I, ResultReg);
Eric Christopher511aa312010-10-11 08:27:59 +00001694 return true;
1695}
1696
Chad Rosieraaa55a82012-02-03 21:07:27 +00001697bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001698 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001699 Type *Ty = I->getType();
Eric Christopher56094ff2010-09-30 22:34:19 +00001700 if (!isTypeLegal(Ty, VT))
1701 return false;
1702
1703 // If we have integer div support we should have selected this automagically.
1704 // In case we have a real miss go ahead and return false and we'll pick
1705 // it up later.
Diana Picus7c6dee9f2017-04-20 09:38:25 +00001706 if (Subtarget->hasDivideInThumbMode())
1707 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00001708
Eric Christopher56094ff2010-09-30 22:34:19 +00001709 // Otherwise emit a libcall.
1710 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christophere11017c2010-10-11 08:31:54 +00001711 if (VT == MVT::i8)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001712 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
Eric Christophere11017c2010-10-11 08:31:54 +00001713 else if (VT == MVT::i16)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001714 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
Eric Christopher56094ff2010-09-30 22:34:19 +00001715 else if (VT == MVT::i32)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001716 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
Eric Christopher56094ff2010-09-30 22:34:19 +00001717 else if (VT == MVT::i64)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001718 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
Eric Christopher56094ff2010-09-30 22:34:19 +00001719 else if (VT == MVT::i128)
Chad Rosieraaa55a82012-02-03 21:07:27 +00001720 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
Eric Christopher56094ff2010-09-30 22:34:19 +00001721 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ac602b2010-10-11 08:38:55 +00001722
Eric Christopher56094ff2010-09-30 22:34:19 +00001723 return ARMEmitLibcall(I, LC);
1724}
1725
Chad Rosierb84a4b42012-02-03 21:23:45 +00001726bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001727 MVT VT;
Chris Lattner229907c2011-07-18 04:54:35 +00001728 Type *Ty = I->getType();
Eric Christophereae1b382010-10-11 08:37:26 +00001729 if (!isTypeLegal(Ty, VT))
1730 return false;
1731
Diana Picus774d1572016-07-18 06:48:25 +00001732 // Many ABIs do not provide a libcall for standalone remainder, so we need to
1733 // use divrem (see the RTABI 4.3.1). Since FastISel can't handle non-double
1734 // multi-reg returns, we'll have to bail out.
1735 if (!TLI.hasStandaloneRem(VT)) {
1736 return false;
1737 }
1738
Eric Christophereae1b382010-10-11 08:37:26 +00001739 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1740 if (VT == MVT::i8)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001741 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
Eric Christophereae1b382010-10-11 08:37:26 +00001742 else if (VT == MVT::i16)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001743 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
Eric Christophereae1b382010-10-11 08:37:26 +00001744 else if (VT == MVT::i32)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001745 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
Eric Christophereae1b382010-10-11 08:37:26 +00001746 else if (VT == MVT::i64)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001747 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
Eric Christophereae1b382010-10-11 08:37:26 +00001748 else if (VT == MVT::i128)
Chad Rosierb84a4b42012-02-03 21:23:45 +00001749 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
Eric Christophere1bcb432010-10-11 08:40:05 +00001750 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christophere4b3d6b2010-10-15 18:02:07 +00001751
Eric Christophereae1b382010-10-11 08:37:26 +00001752 return ARMEmitLibcall(I, LC);
1753}
1754
Chad Rosier685b20c2012-02-06 23:50:07 +00001755bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001756 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Chad Rosier685b20c2012-02-06 23:50:07 +00001757
1758 // We can get here in the case when we have a binary operation on a non-legal
1759 // type and the target independent selector doesn't know how to handle it.
1760 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1761 return false;
Jush Luac96b762012-06-14 06:08:19 +00001762
Chad Rosierbd471252012-02-08 02:29:21 +00001763 unsigned Opc;
1764 switch (ISDOpcode) {
1765 default: return false;
1766 case ISD::ADD:
1767 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1768 break;
1769 case ISD::OR:
1770 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1771 break;
Chad Rosier0ee8c512012-02-08 02:45:44 +00001772 case ISD::SUB:
1773 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1774 break;
Chad Rosierbd471252012-02-08 02:29:21 +00001775 }
1776
Chad Rosier685b20c2012-02-06 23:50:07 +00001777 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1778 if (SrcReg1 == 0) return false;
1779
1780 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1781 // in the instruction, rather then materializing the value in a register.
1782 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1783 if (SrcReg2 == 0) return false;
1784
JF Bastien13969d02013-05-29 15:45:47 +00001785 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Joey Goulyc7cda1c2013-08-23 15:20:56 +00001786 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1787 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001788 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier685b20c2012-02-06 23:50:07 +00001789 TII.get(Opc), ResultReg)
1790 .addReg(SrcReg1).addReg(SrcReg2));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001791 updateValueMap(I, ResultReg);
Chad Rosier685b20c2012-02-06 23:50:07 +00001792 return true;
1793}
1794
1795bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001796 EVT FPVT = TLI.getValueType(DL, I->getType(), true);
Chad Rosier62a144f2012-12-17 19:59:43 +00001797 if (!FPVT.isSimple()) return false;
1798 MVT VT = FPVT.getSimpleVT();
Eric Christopher2ff757d2010-09-09 01:06:51 +00001799
Pete Cooperd927c6e2015-05-06 16:39:17 +00001800 // FIXME: Support vector types where possible.
1801 if (VT.isVector())
1802 return false;
1803
Eric Christopher24dc27f2010-09-09 00:53:57 +00001804 // We can get here in the case when we want to use NEON for our fp
1805 // operations, but can't figure out how to. Just use the vfp instructions
1806 // if we have them.
1807 // FIXME: It'd be nice to use NEON instructions.
Chris Lattner229907c2011-07-18 04:54:35 +00001808 Type *Ty = I->getType();
Tim Northover063a56e2017-02-23 22:35:00 +00001809 if (Ty->isFloatTy() && !Subtarget->hasVFP2())
1810 return false;
1811 if (Ty->isDoubleTy() && (!Subtarget->hasVFP2() || Subtarget->isFPOnlySP()))
Eric Christopherbd3d1212010-09-09 01:02:03 +00001812 return false;
Eric Christopher2ff757d2010-09-09 01:06:51 +00001813
Eric Christopher24dc27f2010-09-09 00:53:57 +00001814 unsigned Opc;
Duncan Sands14627772010-11-03 12:17:33 +00001815 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001816 switch (ISDOpcode) {
1817 default: return false;
1818 case ISD::FADD:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001819 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001820 break;
1821 case ISD::FSUB:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001822 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001823 break;
1824 case ISD::FMUL:
Eric Christopherbd3d1212010-09-09 01:02:03 +00001825 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopher24dc27f2010-09-09 00:53:57 +00001826 break;
1827 }
Chad Rosier80979b62011-11-16 18:39:44 +00001828 unsigned Op1 = getRegForValue(I->getOperand(0));
1829 if (Op1 == 0) return false;
1830
1831 unsigned Op2 = getRegForValue(I->getOperand(1));
1832 if (Op2 == 0) return false;
1833
Chad Rosier62a144f2012-12-17 19:59:43 +00001834 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001835 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher24dc27f2010-09-09 00:53:57 +00001836 TII.get(Opc), ResultReg)
1837 .addReg(Op1).addReg(Op2));
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001838 updateValueMap(I, ResultReg);
Eric Christopher24dc27f2010-09-09 00:53:57 +00001839 return true;
1840}
1841
Eric Christopher72497e52010-09-10 23:18:12 +00001842// Call Handling Code
1843
Jush Lue67e07b2012-07-19 09:49:00 +00001844// This is largely taken directly from CCAssignFnForNode
Eric Christopher72497e52010-09-10 23:18:12 +00001845// TODO: We may not support all of this.
Jush Lue67e07b2012-07-19 09:49:00 +00001846CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1847 bool Return,
1848 bool isVarArg) {
Eric Christopher72497e52010-09-10 23:18:12 +00001849 switch (CC) {
1850 default:
Alex Bradbury080f6972017-08-22 09:11:41 +00001851 report_fatal_error("Unsupported calling convention");
Eric Christopher72497e52010-09-10 23:18:12 +00001852 case CallingConv::Fast:
Jush Lu26088cb2012-08-16 05:15:53 +00001853 if (Subtarget->hasVFP2() && !isVarArg) {
1854 if (!Subtarget->isAAPCS_ABI())
1855 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1856 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1857 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1858 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001859 LLVM_FALLTHROUGH;
Evan Cheng21abfc92010-10-22 18:57:05 +00001860 case CallingConv::C:
Manman Ren2828c572016-03-18 23:38:49 +00001861 case CallingConv::CXX_FAST_TLS:
Eric Christopher72497e52010-09-10 23:18:12 +00001862 // Use target triple & subtarget features to do actual dispatch.
1863 if (Subtarget->isAAPCS_ABI()) {
1864 if (Subtarget->hasVFP2() &&
Jush Lue67e07b2012-07-19 09:49:00 +00001865 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
Eric Christopher72497e52010-09-10 23:18:12 +00001866 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1867 else
1868 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
Bob Wilson8823b842015-09-19 06:20:59 +00001869 } else {
1870 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1871 }
Eric Christopher72497e52010-09-10 23:18:12 +00001872 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001873 case CallingConv::Swift:
Jush Lue67e07b2012-07-19 09:49:00 +00001874 if (!isVarArg)
1875 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1876 // Fall through to soft float variant, variadic functions don't
1877 // use hard floating point ABI.
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001878 LLVM_FALLTHROUGH;
Eric Christopher72497e52010-09-10 23:18:12 +00001879 case CallingConv::ARM_AAPCS:
1880 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1881 case CallingConv::ARM_APCS:
1882 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001883 case CallingConv::GHC:
1884 if (Return)
Alex Bradbury080f6972017-08-22 09:11:41 +00001885 report_fatal_error("Can't return in GHC call convention");
Eric Christopherb3322362012-08-03 00:05:53 +00001886 else
1887 return CC_ARM_APCS_GHC;
Eric Christopher72497e52010-09-10 23:18:12 +00001888 }
1889}
1890
Eric Christopher79398062010-09-29 23:11:09 +00001891bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1892 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001893 SmallVectorImpl<MVT> &ArgVTs,
Eric Christopher79398062010-09-29 23:11:09 +00001894 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1895 SmallVectorImpl<unsigned> &RegArgs,
1896 CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00001897 unsigned &NumBytes,
1898 bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00001899 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001900 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00001901 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1902 CCAssignFnForCall(CC, false, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00001903
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001904 // Check that we can handle all of the arguments. If we can't, then bail out
1905 // now before we add code to the MBB.
1906 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1907 CCValAssign &VA = ArgLocs[i];
1908 MVT ArgVT = ArgVTs[VA.getValNo()];
1909
1910 // We don't handle NEON/vector parameters yet.
1911 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1912 return false;
1913
1914 // Now copy/store arg to correct locations.
1915 if (VA.isRegLoc() && !VA.needsCustom()) {
1916 continue;
1917 } else if (VA.needsCustom()) {
1918 // TODO: We need custom lowering for vector (v2f64) args.
1919 if (VA.getLocVT() != MVT::f64 ||
1920 // TODO: Only handle register args for now.
1921 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1922 return false;
1923 } else {
Craig Topper56710102013-08-15 02:33:50 +00001924 switch (ArgVT.SimpleTy) {
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001925 default:
1926 return false;
1927 case MVT::i1:
1928 case MVT::i8:
1929 case MVT::i16:
1930 case MVT::i32:
1931 break;
1932 case MVT::f32:
1933 if (!Subtarget->hasVFP2())
1934 return false;
1935 break;
1936 case MVT::f64:
1937 if (!Subtarget->hasVFP2())
1938 return false;
1939 break;
1940 }
1941 }
1942 }
1943
1944 // At the point, we are able to handle the call's arguments in fast isel.
1945
Eric Christopher79398062010-09-29 23:11:09 +00001946 // Get a count of how many bytes are to be pushed on the stack.
1947 NumBytes = CCInfo.getNextStackOffset();
1948
1949 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00001950 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00001951 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00001952 TII.get(AdjStackDown))
Serge Pavlovd526b132017-05-09 13:35:13 +00001953 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00001954
1955 // Process the args.
1956 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1957 CCValAssign &VA = ArgLocs[i];
Juergen Ributzka4c018a12014-08-01 18:04:14 +00001958 const Value *ArgVal = Args[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001959 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sandsf5dda012010-11-03 11:35:31 +00001960 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christopher79398062010-09-29 23:11:09 +00001961
Bill Wendling23f8c4a2012-03-16 23:11:07 +00001962 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1963 "We don't handle NEON/vector parameters yet.");
Eric Christopherc9616f22010-10-23 09:37:17 +00001964
Eric Christopher78f8d4e2010-09-30 20:49:44 +00001965 // Handle arg promotion, etc.
Eric Christopher79398062010-09-29 23:11:09 +00001966 switch (VA.getLocInfo()) {
1967 case CCValAssign::Full: break;
Eric Christopherc103c662010-10-18 02:17:53 +00001968 case CCValAssign::SExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001969 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001970 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
Eugene Zelenko342257e2017-01-31 00:56:17 +00001971 assert(Arg != 0 && "Failed to emit a sext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001972 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001973 break;
1974 }
Chad Rosierd0191a52011-11-05 20:16:15 +00001975 case CCValAssign::AExt:
Javed Absar5b8e4872017-07-18 10:19:48 +00001976 // Intentional fall-through. Handle AExt and ZExt.
Eric Christopherc103c662010-10-18 02:17:53 +00001977 case CCValAssign::ZExt: {
Chad Rosier9fd0e552011-12-02 20:25:18 +00001978 MVT DestVT = VA.getLocVT();
Chad Rosier5b9c3972012-02-14 22:29:48 +00001979 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
Eugene Zelenko342257e2017-01-31 00:56:17 +00001980 assert(Arg != 0 && "Failed to emit a zext");
Chad Rosier9fd0e552011-12-02 20:25:18 +00001981 ArgVT = DestVT;
Eric Christopherc103c662010-10-18 02:17:53 +00001982 break;
1983 }
1984 case CCValAssign::BCvt: {
Juergen Ributzka88e32512014-09-03 20:56:59 +00001985 unsigned BC = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sandsf5dda012010-11-03 11:35:31 +00001986 /*TODO: Kill=*/false);
Eric Christopherc103c662010-10-18 02:17:53 +00001987 assert(BC != 0 && "Failed to emit a bitcast!");
1988 Arg = BC;
1989 ArgVT = VA.getLocVT();
1990 break;
1991 }
1992 default: llvm_unreachable("Unknown arg promotion!");
Eric Christopher79398062010-09-29 23:11:09 +00001993 }
1994
1995 // Now copy/store arg to correct locations.
Eric Christopher71ef1af2010-10-11 21:20:02 +00001996 if (VA.isRegLoc() && !VA.needsCustom()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001997 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1998 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Eric Christopher79398062010-09-29 23:11:09 +00001999 RegArgs.push_back(VA.getLocReg());
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002000 } else if (VA.needsCustom()) {
2001 // TODO: We need custom lowering for vector (v2f64) args.
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002002 assert(VA.getLocVT() == MVT::f64 &&
2003 "Custom lowering for v2f64 args not available");
Jim Grosbach055de2c2010-10-27 21:39:08 +00002004
Javed Absar5b8e4872017-07-18 10:19:48 +00002005 // FIXME: ArgLocs[++i] may extend beyond ArgLocs.size()
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002006 CCValAssign &NextVA = ArgLocs[++i];
2007
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002008 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2009 "We only handle register args!");
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002010
Rafael Espindolaea09c592014-02-18 22:05:46 +00002011 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher4ac3ed02010-10-21 00:01:47 +00002012 TII.get(ARM::VMOVRRD), VA.getLocReg())
2013 .addReg(NextVA.getLocReg(), RegState::Define)
2014 .addReg(Arg));
2015 RegArgs.push_back(VA.getLocReg());
2016 RegArgs.push_back(NextVA.getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002017 } else {
Eric Christopherb353e4f2010-10-21 20:09:54 +00002018 assert(VA.isMemLoc());
2019 // Need to store on the stack.
Juergen Ributzka4c018a12014-08-01 18:04:14 +00002020
2021 // Don't emit stores for undef values.
2022 if (isa<UndefValue>(ArgVal))
2023 continue;
2024
Eric Christopherfef5f312010-11-19 22:30:02 +00002025 Address Addr;
2026 Addr.BaseType = Address::RegBase;
2027 Addr.Base.Reg = ARM::SP;
2028 Addr.Offset = VA.getLocMemOffset();
Eric Christopherb353e4f2010-10-21 20:09:54 +00002029
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002030 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2031 assert(EmitRet && "Could not emit a store for argument!");
Eric Christopher79398062010-09-29 23:11:09 +00002032 }
2033 }
Bill Wendling23f8c4a2012-03-16 23:11:07 +00002034
Eric Christopher79398062010-09-29 23:11:09 +00002035 return true;
2036}
2037
Duncan Sandsf5dda012010-11-03 11:35:31 +00002038bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christopher79398062010-09-29 23:11:09 +00002039 const Instruction *I, CallingConv::ID CC,
Jush Lue67e07b2012-07-19 09:49:00 +00002040 unsigned &NumBytes, bool isVarArg) {
Eric Christopher79398062010-09-29 23:11:09 +00002041 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002042 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002043 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher71ef1af2010-10-11 21:20:02 +00002044 TII.get(AdjStackUp))
2045 .addImm(NumBytes).addImm(0));
Eric Christopher79398062010-09-29 23:11:09 +00002046
2047 // Now the return value.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002048 if (RetVT != MVT::isVoid) {
Eric Christopher79398062010-09-29 23:11:09 +00002049 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002050 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002051 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Eric Christopher79398062010-09-29 23:11:09 +00002052
2053 // Copy all of the result registers out of their specified physreg.
Duncan Sandsf5dda012010-11-03 11:35:31 +00002054 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopherc1e209d2010-10-01 00:00:11 +00002055 // For this move we copy into two registers and then move into the
2056 // double fp reg we want.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002057 MVT DestVT = RVLocs[0].getValVT();
Craig Topper760b1342012-02-22 05:59:10 +00002058 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002059 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002060 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopherc1e209d2010-10-01 00:00:11 +00002061 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopheraf719ef2010-10-20 08:02:24 +00002062 .addReg(RVLocs[0].getLocReg())
2063 .addReg(RVLocs[1].getLocReg()));
Eric Christopher7ac602b2010-10-11 08:38:55 +00002064
Eric Christopheraf719ef2010-10-20 08:02:24 +00002065 UsedRegs.push_back(RVLocs[0].getLocReg());
2066 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach055de2c2010-10-27 21:39:08 +00002067
Eric Christopher7ac602b2010-10-11 08:38:55 +00002068 // Finally update the result.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002069 updateValueMap(I, ResultReg);
Chad Rosier90f9afe2012-05-11 18:51:55 +00002070 } else {
2071 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002072 MVT CopyVT = RVLocs[0].getValVT();
Chad Rosier5de1bea2011-11-08 00:03:32 +00002073
2074 // Special handling for extended integers.
2075 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2076 CopyVT = MVT::i32;
2077
Craig Topper760b1342012-02-22 05:59:10 +00002078 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christopher79398062010-09-29 23:11:09 +00002079
Eric Christopherc1e209d2010-10-01 00:00:11 +00002080 unsigned ResultReg = createResultReg(DstRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002081 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2082 TII.get(TargetOpcode::COPY),
Eric Christopherc1e209d2010-10-01 00:00:11 +00002083 ResultReg).addReg(RVLocs[0].getLocReg());
2084 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christopher79398062010-09-29 23:11:09 +00002085
Eric Christopher7ac602b2010-10-11 08:38:55 +00002086 // Finally update the result.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002087 updateValueMap(I, ResultReg);
Eric Christopherc1e209d2010-10-01 00:00:11 +00002088 }
Eric Christopher79398062010-09-29 23:11:09 +00002089 }
2090
Eric Christopher7ac602b2010-10-11 08:38:55 +00002091 return true;
Eric Christopher79398062010-09-29 23:11:09 +00002092}
2093
Eric Christopher93bbe652010-10-22 01:28:00 +00002094bool ARMFastISel::SelectRet(const Instruction *I) {
2095 const ReturnInst *Ret = cast<ReturnInst>(I);
2096 const Function &F = *I->getParent()->getParent();
Jim Grosbach055de2c2010-10-27 21:39:08 +00002097
Eric Christopher93bbe652010-10-22 01:28:00 +00002098 if (!FuncInfo.CanLowerReturn)
2099 return false;
Jim Grosbach055de2c2010-10-27 21:39:08 +00002100
Manman Ren57518142016-04-11 21:08:06 +00002101 if (TLI.supportSwiftError() &&
2102 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2103 return false;
2104
Manman Ren5e9e65e2016-01-12 00:47:18 +00002105 if (TLI.supportSplitCSR(FuncInfo.MF))
2106 return false;
2107
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002108 // Build a list of return value registers.
2109 SmallVector<unsigned, 4> RetRegs;
2110
Eric Christopher93bbe652010-10-22 01:28:00 +00002111 CallingConv::ID CC = F.getCallingConv();
2112 if (Ret->getNumOperands() > 0) {
2113 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00002114 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Eric Christopher93bbe652010-10-22 01:28:00 +00002115
2116 // Analyze operands of the call, assigning locations to each operand.
2117 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002118 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
Jush Lue67e07b2012-07-19 09:49:00 +00002119 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2120 F.isVarArg()));
Eric Christopher93bbe652010-10-22 01:28:00 +00002121
2122 const Value *RV = Ret->getOperand(0);
2123 unsigned Reg = getRegForValue(RV);
2124 if (Reg == 0)
2125 return false;
2126
2127 // Only handle a single return value for now.
2128 if (ValLocs.size() != 1)
2129 return false;
2130
2131 CCValAssign &VA = ValLocs[0];
Jim Grosbach055de2c2010-10-27 21:39:08 +00002132
Eric Christopher93bbe652010-10-22 01:28:00 +00002133 // Don't bother handling odd stuff for now.
2134 if (VA.getLocInfo() != CCValAssign::Full)
2135 return false;
2136 // Only handle register returns for now.
2137 if (!VA.isRegLoc())
2138 return false;
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002139
2140 unsigned SrcReg = Reg + VA.getValNo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002141 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Chad Rosier62a144f2012-12-17 19:59:43 +00002142 if (!RVEVT.isSimple()) return false;
2143 MVT RVVT = RVEVT.getSimpleVT();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002144 MVT DestVT = VA.getValVT();
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002145 // Special handling for extended integers.
2146 if (RVVT != DestVT) {
2147 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2148 return false;
2149
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002150 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2151
Chad Rosierfcd29ae2012-02-17 01:21:28 +00002152 // Perform extension if flagged as either zext or sext. Otherwise, do
2153 // nothing.
2154 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2155 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2156 if (SrcReg == 0) return false;
2157 }
Chad Rosierf3e73ad2011-11-04 00:50:21 +00002158 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002159
Eric Christopher93bbe652010-10-22 01:28:00 +00002160 // Make the copy.
Eric Christopher93bbe652010-10-22 01:28:00 +00002161 unsigned DstReg = VA.getLocReg();
2162 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2163 // Avoid a cross-class copy. This is very unlikely.
2164 if (!SrcRC->contains(DstReg))
2165 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002166 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2167 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
Eric Christopher93bbe652010-10-22 01:28:00 +00002168
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002169 // Add register to return instruction.
2170 RetRegs.push_back(VA.getLocReg());
Eric Christopher93bbe652010-10-22 01:28:00 +00002171 }
Jim Grosbach055de2c2010-10-27 21:39:08 +00002172
Rafael Espindolaea09c592014-02-18 22:05:46 +00002173 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Joerg Sonnenberger0f76a352017-08-28 20:20:47 +00002174 TII.get(Subtarget->getReturnOpcode()));
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002175 AddOptionalDefs(MIB);
Javed Absar5b8e4872017-07-18 10:19:48 +00002176 for (unsigned R : RetRegs)
2177 MIB.addReg(R, RegState::Implicit);
Eric Christopher93bbe652010-10-22 01:28:00 +00002178 return true;
2179}
2180
Chad Rosierc6916f82012-06-12 19:25:13 +00002181unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2182 if (UseReg)
2183 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2184 else
2185 return isThumb2 ? ARM::tBL : ARM::BL;
2186}
2187
2188unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
Chandler Carruth1c82d332013-07-27 11:23:08 +00002189 // Manually compute the global's type to avoid building it when unnecessary.
2190 Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
Mehdi Amini44ede332015-07-09 02:09:04 +00002191 EVT LCREVT = TLI.getValueType(DL, GVTy);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002192 if (!LCREVT.isSimple()) return 0;
2193
Bill Wendling76cce192013-12-29 08:00:04 +00002194 GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
Craig Topper062a2ba2014-04-25 05:30:21 +00002195 GlobalValue::ExternalLinkage, nullptr,
2196 Name);
Chandler Carruth1c82d332013-07-27 11:23:08 +00002197 assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
Chad Rosier62a144f2012-12-17 19:59:43 +00002198 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
Eric Christopher919772f2011-02-22 01:37:10 +00002199}
2200
Eric Christopher8b912662010-09-14 23:03:37 +00002201// A quick function that will emit a call for a named libcall in F with the
2202// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopher7ac602b2010-10-11 08:38:55 +00002203// can emit a call for any libcall we can produce. This is an abridged version
2204// of the full call infrastructure since we won't need to worry about things
Eric Christopher8b912662010-09-14 23:03:37 +00002205// like computed function pointers or strange arguments at call sites.
2206// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2207// with X86.
Eric Christopher7990df12010-09-28 01:21:42 +00002208bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2209 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002210
Eric Christopher8b912662010-09-14 23:03:37 +00002211 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002212 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002213 MVT RetVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002214 if (RetTy->isVoidTy())
2215 RetVT = MVT::isVoid;
2216 else if (!isTypeLegal(RetTy, RetVT))
2217 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002218
Chad Rosier90f9afe2012-05-11 18:51:55 +00002219 // Can't handle non-double multi-reg retvals.
Jush Luac96b762012-06-14 06:08:19 +00002220 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
Chad Rosier90f9afe2012-05-11 18:51:55 +00002221 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002222 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002223 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002224 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2225 return false;
2226 }
2227
Eric Christopher79398062010-09-29 23:11:09 +00002228 // Set up the argument vectors.
Eric Christopher8b912662010-09-14 23:03:37 +00002229 SmallVector<Value*, 8> Args;
2230 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002231 SmallVector<MVT, 8> ArgVTs;
Eric Christopher8b912662010-09-14 23:03:37 +00002232 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2233 Args.reserve(I->getNumOperands());
2234 ArgRegs.reserve(I->getNumOperands());
2235 ArgVTs.reserve(I->getNumOperands());
2236 ArgFlags.reserve(I->getNumOperands());
Javed Absar5b8e4872017-07-18 10:19:48 +00002237 for (Value *Op : I->operands()) {
Eric Christopher8b912662010-09-14 23:03:37 +00002238 unsigned Arg = getRegForValue(Op);
2239 if (Arg == 0) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002240
Chris Lattner229907c2011-07-18 04:54:35 +00002241 Type *ArgTy = Op->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002242 MVT ArgVT;
Eric Christopher8b912662010-09-14 23:03:37 +00002243 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002244
Eric Christopher8b912662010-09-14 23:03:37 +00002245 ISD::ArgFlagsTy Flags;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002246 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher8b912662010-09-14 23:03:37 +00002247 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002248
Eric Christopher8b912662010-09-14 23:03:37 +00002249 Args.push_back(Op);
2250 ArgRegs.push_back(Arg);
2251 ArgVTs.push_back(ArgVT);
2252 ArgFlags.push_back(Flags);
2253 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002254
Eric Christopher79398062010-09-29 23:11:09 +00002255 // Handle the arguments now that we've gotten them.
Eric Christopher8b912662010-09-14 23:03:37 +00002256 SmallVector<unsigned, 4> RegArgs;
Eric Christopher79398062010-09-29 23:11:09 +00002257 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002258 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2259 RegArgs, CC, NumBytes, false))
Eric Christopher79398062010-09-29 23:11:09 +00002260 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002261
Chad Rosierc6916f82012-06-12 19:25:13 +00002262 unsigned CalleeReg = 0;
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002263 if (Subtarget->genLongCalls()) {
Chad Rosierc6916f82012-06-12 19:25:13 +00002264 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2265 if (CalleeReg == 0) return false;
2266 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002267
Chad Rosierc6916f82012-06-12 19:25:13 +00002268 // Issue the call.
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002269 unsigned CallOpc = ARMSelectCallOp(Subtarget->genLongCalls());
Chad Rosierc6916f82012-06-12 19:25:13 +00002270 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002271 DbgLoc, TII.get(CallOpc));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002272 // BL / BLX don't take a predicate, but tBL / tBLX do.
2273 if (isThumb2)
Diana Picus4f8c3e12017-01-13 09:37:56 +00002274 MIB.add(predOps(ARMCC::AL));
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002275 if (Subtarget->genLongCalls())
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002276 MIB.addReg(CalleeReg);
2277 else
2278 MIB.addExternalSymbol(TLI.getLibcallName(Call));
Chad Rosierc6916f82012-06-12 19:25:13 +00002279
Eric Christopher8b912662010-09-14 23:03:37 +00002280 // Add implicit physical register uses to the call.
Javed Absar5b8e4872017-07-18 10:19:48 +00002281 for (unsigned R : RegArgs)
2282 MIB.addReg(R, RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002283
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002284 // Add a register mask with the call-preserved registers.
2285 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00002286 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002287
Eric Christopher79398062010-09-29 23:11:09 +00002288 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002289 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002290 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002291
Eric Christopher8b912662010-09-14 23:03:37 +00002292 // Set all unused physreg defs as dead.
2293 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002294
Eric Christopher8b912662010-09-14 23:03:37 +00002295 return true;
2296}
2297
Chad Rosiera7ebc562011-11-11 23:31:03 +00002298bool ARMFastISel::SelectCall(const Instruction *I,
Craig Topper062a2ba2014-04-25 05:30:21 +00002299 const char *IntrMemName = nullptr) {
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002300 const CallInst *CI = cast<CallInst>(I);
2301 const Value *Callee = CI->getCalledValue();
2302
Chad Rosiera7ebc562011-11-11 23:31:03 +00002303 // Can't handle inline asm.
2304 if (isa<InlineAsm>(Callee)) return false;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002305
Chad Rosierdf42cf32012-12-11 00:18:02 +00002306 // Allow SelectionDAG isel to handle tail calls.
2307 if (CI->isTailCall()) return false;
2308
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002309 // Check the calling convention.
2310 ImmutableCallSite CS(CI);
2311 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher167a70022010-10-18 06:49:12 +00002312
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002313 // TODO: Avoid some calling conventions?
Eric Christopher7ac602b2010-10-11 08:38:55 +00002314
Manuel Jacob190577a2016-01-17 22:37:39 +00002315 FunctionType *FTy = CS.getFunctionType();
Jush Lue67e07b2012-07-19 09:49:00 +00002316 bool isVarArg = FTy->isVarArg();
Eric Christopher7ac602b2010-10-11 08:38:55 +00002317
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002318 // Handle *simple* calls for now.
Chris Lattner229907c2011-07-18 04:54:35 +00002319 Type *RetTy = I->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002320 MVT RetVT;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002321 if (RetTy->isVoidTy())
2322 RetVT = MVT::isVoid;
Chad Rosier5de1bea2011-11-08 00:03:32 +00002323 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2324 RetVT != MVT::i8 && RetVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002325 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002326
Chad Rosier90f9afe2012-05-11 18:51:55 +00002327 // Can't handle non-double multi-reg retvals.
2328 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2329 RetVT != MVT::i16 && RetVT != MVT::i32) {
2330 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002331 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
Jush Lue67e07b2012-07-19 09:49:00 +00002332 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
Chad Rosier90f9afe2012-05-11 18:51:55 +00002333 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2334 return false;
2335 }
2336
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002337 // Set up the argument vectors.
2338 SmallVector<Value*, 8> Args;
2339 SmallVector<unsigned, 8> ArgRegs;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002340 SmallVector<MVT, 8> ArgVTs;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002341 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierdccc4792012-02-15 00:23:55 +00002342 unsigned arg_size = CS.arg_size();
2343 Args.reserve(arg_size);
2344 ArgRegs.reserve(arg_size);
2345 ArgVTs.reserve(arg_size);
2346 ArgFlags.reserve(arg_size);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002347 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2348 i != e; ++i) {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002349 // If we're lowering a memory intrinsic instead of a regular call, skip the
Pete Cooper67cf9a72015-11-19 05:56:52 +00002350 // last two arguments, which shouldn't be passed to the underlying function.
2351 if (IntrMemName && e-i <= 2)
Chad Rosiera7ebc562011-11-11 23:31:03 +00002352 break;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002353
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002354 ISD::ArgFlagsTy Flags;
Reid Klecknerfb502d22017-04-14 20:19:02 +00002355 unsigned ArgIdx = i - CS.arg_begin();
2356 if (CS.paramHasAttr(ArgIdx, Attribute::SExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002357 Flags.setSExt();
Reid Klecknerfb502d22017-04-14 20:19:02 +00002358 if (CS.paramHasAttr(ArgIdx, Attribute::ZExt))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002359 Flags.setZExt();
2360
Chad Rosier8a98ec42011-11-04 00:58:10 +00002361 // FIXME: Only handle *easy* calls for now.
Reid Klecknerfb502d22017-04-14 20:19:02 +00002362 if (CS.paramHasAttr(ArgIdx, Attribute::InReg) ||
2363 CS.paramHasAttr(ArgIdx, Attribute::StructRet) ||
2364 CS.paramHasAttr(ArgIdx, Attribute::SwiftSelf) ||
2365 CS.paramHasAttr(ArgIdx, Attribute::SwiftError) ||
2366 CS.paramHasAttr(ArgIdx, Attribute::Nest) ||
2367 CS.paramHasAttr(ArgIdx, Attribute::ByVal))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002368 return false;
2369
Chris Lattner229907c2011-07-18 04:54:35 +00002370 Type *ArgTy = (*i)->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002371 MVT ArgVT;
Chad Rosierd0191a52011-11-05 20:16:15 +00002372 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2373 ArgVT != MVT::i1)
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002374 return false;
Chad Rosieree93ff72011-11-18 01:17:34 +00002375
2376 unsigned Arg = getRegForValue(*i);
2377 if (Arg == 0)
2378 return false;
2379
Rafael Espindolaea09c592014-02-18 22:05:46 +00002380 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002381 Flags.setOrigAlign(OriginalAlignment);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002382
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002383 Args.push_back(*i);
2384 ArgRegs.push_back(Arg);
2385 ArgVTs.push_back(ArgVT);
2386 ArgFlags.push_back(Flags);
2387 }
Eric Christopher7ac602b2010-10-11 08:38:55 +00002388
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002389 // Handle the arguments now that we've gotten them.
2390 SmallVector<unsigned, 4> RegArgs;
2391 unsigned NumBytes;
Jush Lue67e07b2012-07-19 09:49:00 +00002392 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2393 RegArgs, CC, NumBytes, isVarArg))
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002394 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002395
Chad Rosierc6916f82012-06-12 19:25:13 +00002396 bool UseReg = false;
Chad Rosier223faf72012-05-23 18:38:57 +00002397 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002398 if (!GV || Subtarget->genLongCalls()) UseReg = true;
Chad Rosier223faf72012-05-23 18:38:57 +00002399
Chad Rosierc6916f82012-06-12 19:25:13 +00002400 unsigned CalleeReg = 0;
2401 if (UseReg) {
2402 if (IntrMemName)
2403 CalleeReg = getLibcallReg(IntrMemName);
2404 else
2405 CalleeReg = getRegForValue(Callee);
2406
Chad Rosier223faf72012-05-23 18:38:57 +00002407 if (CalleeReg == 0) return false;
2408 }
2409
Chad Rosierc6916f82012-06-12 19:25:13 +00002410 // Issue the call.
2411 unsigned CallOpc = ARMSelectCallOp(UseReg);
2412 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00002413 DbgLoc, TII.get(CallOpc));
Chad Rosierc6916f82012-06-12 19:25:13 +00002414
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002415 // ARM calls don't take a predicate, but tBL / tBLX do.
2416 if(isThumb2)
Diana Picus4f8c3e12017-01-13 09:37:56 +00002417 MIB.add(predOps(ARMCC::AL));
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002418 if (UseReg)
2419 MIB.addReg(CalleeReg);
2420 else if (!IntrMemName)
Rafael Espindolaafade352016-06-16 16:09:53 +00002421 MIB.addGlobalAddress(GV, 0, 0);
Jakob Stoklund Olesene6afde52012-08-24 20:52:46 +00002422 else
Rafael Espindolaafade352016-06-16 16:09:53 +00002423 MIB.addExternalSymbol(IntrMemName, 0);
Jush Luac96b762012-06-14 06:08:19 +00002424
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002425 // Add implicit physical register uses to the call.
Javed Absar5b8e4872017-07-18 10:19:48 +00002426 for (unsigned R : RegArgs)
2427 MIB.addReg(R, RegState::Implicit);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002428
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002429 // Add a register mask with the call-preserved registers.
2430 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00002431 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002432
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002433 // Finish off the call including any return values.
Eric Christopher7ac602b2010-10-11 08:38:55 +00002434 SmallVector<unsigned, 4> UsedRegs;
Jush Lue67e07b2012-07-19 09:49:00 +00002435 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2436 return false;
Eric Christopher7ac602b2010-10-11 08:38:55 +00002437
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002438 // Set all unused physreg defs as dead.
2439 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopher7ac602b2010-10-11 08:38:55 +00002440
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002441 return true;
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002442}
2443
Chad Rosier057b6d32011-11-14 23:04:09 +00002444bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002445 return Len <= 16;
2446}
2447
Jim Grosbach0c509fa2012-04-06 23:43:50 +00002448bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002449 uint64_t Len, unsigned Alignment) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002450 // Make sure we don't bloat code by inlining very large memcpy's.
Chad Rosier057b6d32011-11-14 23:04:09 +00002451 if (!ARMIsMemCpySmall(Len))
Chad Rosierab7223e2011-11-14 22:46:17 +00002452 return false;
2453
Chad Rosierab7223e2011-11-14 22:46:17 +00002454 while (Len) {
2455 MVT VT;
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002456 if (!Alignment || Alignment >= 4) {
2457 if (Len >= 4)
2458 VT = MVT::i32;
2459 else if (Len >= 2)
2460 VT = MVT::i16;
2461 else {
Eugene Zelenko342257e2017-01-31 00:56:17 +00002462 assert(Len == 1 && "Expected a length of 1!");
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002463 VT = MVT::i8;
2464 }
2465 } else {
2466 // Bound based on alignment.
2467 if (Len >= 2 && Alignment == 2)
2468 VT = MVT::i16;
2469 else {
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002470 VT = MVT::i8;
2471 }
Chad Rosierab7223e2011-11-14 22:46:17 +00002472 }
2473
2474 bool RV;
2475 unsigned ResultReg;
2476 RV = ARMEmitLoad(VT, ResultReg, Src);
Eugene Zelenko342257e2017-01-31 00:56:17 +00002477 assert(RV && "Should be able to handle this load.");
Chad Rosierab7223e2011-11-14 22:46:17 +00002478 RV = ARMEmitStore(VT, ResultReg, Dest);
Eugene Zelenko342257e2017-01-31 00:56:17 +00002479 assert(RV && "Should be able to handle this store.");
Duncan Sandsae22c602012-02-05 14:20:11 +00002480 (void)RV;
Chad Rosierab7223e2011-11-14 22:46:17 +00002481
2482 unsigned Size = VT.getSizeInBits()/8;
2483 Len -= Size;
2484 Dest.Offset += Size;
2485 Src.Offset += Size;
2486 }
2487
2488 return true;
2489}
2490
Chad Rosiera7ebc562011-11-11 23:31:03 +00002491bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2492 // FIXME: Handle more intrinsics.
2493 switch (I.getIntrinsicID()) {
2494 default: return false;
Chad Rosier820d248c2012-05-30 17:23:22 +00002495 case Intrinsic::frameaddress: {
Matthias Braun941a7052016-07-28 18:40:00 +00002496 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
2497 MFI.setFrameAddressIsTaken(true);
Chad Rosier820d248c2012-05-30 17:23:22 +00002498
Craig Topper61e88f42014-11-21 05:58:21 +00002499 unsigned LdrOpc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
2500 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass
2501 : &ARM::GPRRegClass;
Chad Rosier820d248c2012-05-30 17:23:22 +00002502
2503 const ARMBaseRegisterInfo *RegInfo =
Eric Christopher1b21f002015-01-29 00:19:33 +00002504 static_cast<const ARMBaseRegisterInfo *>(Subtarget->getRegisterInfo());
Chad Rosier820d248c2012-05-30 17:23:22 +00002505 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2506 unsigned SrcReg = FramePtr;
2507
2508 // Recursively load frame address
2509 // ldr r0 [fp]
2510 // ldr r0 [r0]
2511 // ldr r0 [r0]
2512 // ...
2513 unsigned DestReg;
2514 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2515 while (Depth--) {
2516 DestReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002517 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Chad Rosier820d248c2012-05-30 17:23:22 +00002518 TII.get(LdrOpc), DestReg)
2519 .addReg(SrcReg).addImm(0));
2520 SrcReg = DestReg;
2521 }
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002522 updateValueMap(&I, SrcReg);
Chad Rosier820d248c2012-05-30 17:23:22 +00002523 return true;
2524 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002525 case Intrinsic::memcpy:
2526 case Intrinsic::memmove: {
Chad Rosiera7ebc562011-11-11 23:31:03 +00002527 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2528 // Don't handle volatile.
2529 if (MTI.isVolatile())
2530 return false;
Chad Rosierab7223e2011-11-14 22:46:17 +00002531
2532 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2533 // we would emit dead code because we don't currently handle memmoves.
2534 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2535 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
Chad Rosier057b6d32011-11-14 23:04:09 +00002536 // Small memcpy's are common enough that we want to do them without a call
2537 // if possible.
Chad Rosierab7223e2011-11-14 22:46:17 +00002538 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
Chad Rosier057b6d32011-11-14 23:04:09 +00002539 if (ARMIsMemCpySmall(Len)) {
Chad Rosierab7223e2011-11-14 22:46:17 +00002540 Address Dest, Src;
2541 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2542 !ARMComputeAddress(MTI.getRawSource(), Src))
2543 return false;
Pete Cooper67cf9a72015-11-19 05:56:52 +00002544 unsigned Alignment = MTI.getAlignment();
Chad Rosier9f5c68a2012-12-06 01:34:31 +00002545 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
Chad Rosierab7223e2011-11-14 22:46:17 +00002546 return true;
2547 }
2548 }
Jush Luac96b762012-06-14 06:08:19 +00002549
Chad Rosiera7ebc562011-11-11 23:31:03 +00002550 if (!MTI.getLength()->getType()->isIntegerTy(32))
2551 return false;
Jush Luac96b762012-06-14 06:08:19 +00002552
Chad Rosiera7ebc562011-11-11 23:31:03 +00002553 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2554 return false;
2555
2556 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2557 return SelectCall(&I, IntrMemName);
2558 }
2559 case Intrinsic::memset: {
2560 const MemSetInst &MSI = cast<MemSetInst>(I);
2561 // Don't handle volatile.
2562 if (MSI.isVolatile())
2563 return false;
Jush Luac96b762012-06-14 06:08:19 +00002564
Chad Rosiera7ebc562011-11-11 23:31:03 +00002565 if (!MSI.getLength()->getType()->isIntegerTy(32))
2566 return false;
Jush Luac96b762012-06-14 06:08:19 +00002567
Chad Rosiera7ebc562011-11-11 23:31:03 +00002568 if (MSI.getDestAddressSpace() > 255)
2569 return false;
Jush Luac96b762012-06-14 06:08:19 +00002570
Chad Rosiera7ebc562011-11-11 23:31:03 +00002571 return SelectCall(&I, "memset");
2572 }
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002573 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
Eli Bendersky2e2ce492013-01-30 16:30:19 +00002575 Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
Chad Rosieraa9cb9d2012-05-11 21:33:49 +00002576 return true;
2577 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002578 }
Chad Rosiera7ebc562011-11-11 23:31:03 +00002579}
2580
Chad Rosieree7e4522011-11-02 00:18:48 +00002581bool ARMFastISel::SelectTrunc(const Instruction *I) {
Jush Luac96b762012-06-14 06:08:19 +00002582 // The high bits for a type smaller than the register size are assumed to be
Chad Rosieree7e4522011-11-02 00:18:48 +00002583 // undefined.
2584 Value *Op = I->getOperand(0);
2585
2586 EVT SrcVT, DestVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00002587 SrcVT = TLI.getValueType(DL, Op->getType(), true);
2588 DestVT = TLI.getValueType(DL, I->getType(), true);
Chad Rosieree7e4522011-11-02 00:18:48 +00002589
2590 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2591 return false;
2592 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2593 return false;
2594
2595 unsigned SrcReg = getRegForValue(Op);
2596 if (!SrcReg) return false;
2597
2598 // Because the high bits are undefined, a truncate doesn't generate
2599 // any code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002600 updateValueMap(I, SrcReg);
Chad Rosieree7e4522011-11-02 00:18:48 +00002601 return true;
2602}
2603
Chad Rosier62a144f2012-12-17 19:59:43 +00002604unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
Chad Rosier4489f942011-11-02 17:20:24 +00002605 bool isZExt) {
Eli Friedmanc7035512011-05-25 23:49:02 +00002606 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
Chad Rosier4489f942011-11-02 17:20:24 +00002607 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002608 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
Chad Rosier4489f942011-11-02 17:20:24 +00002609 return 0;
JF Bastien06ce03d2013-06-07 20:10:37 +00002610
2611 // Table of which combinations can be emitted as a single instruction,
2612 // and which will require two.
2613 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2614 // ARM Thumb
2615 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2616 // ext: s z s z s z s z
2617 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2618 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2619 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2620 };
2621
2622 // Target registers for:
2623 // - For ARM can never be PC.
2624 // - For 16-bit Thumb are restricted to lower 8 registers.
2625 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2626 static const TargetRegisterClass *RCTbl[2][2] = {
2627 // Instructions: Two Single
2628 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2629 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2630 };
2631
2632 // Table governing the instruction(s) to be emitted.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002633 static const struct InstructionTable {
2634 uint32_t Opc : 16;
2635 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2636 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2637 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2638 } IT[2][2][3][2] = {
JF Bastien06ce03d2013-06-07 20:10:37 +00002639 { // Two instructions (first is left shift, second is in this table).
JF Bastiencd4c64d2013-07-17 05:46:46 +00002640 { // ARM Opc S Shift Imm
2641 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2642 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2643 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2644 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2645 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2646 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002647 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002648 { // Thumb Opc S Shift Imm
2649 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2650 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2651 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2652 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2653 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2654 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002655 }
2656 },
2657 { // Single instruction.
JF Bastiencd4c64d2013-07-17 05:46:46 +00002658 { // ARM Opc S Shift Imm
2659 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2660 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2661 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2662 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2663 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2664 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002665 },
JF Bastiencd4c64d2013-07-17 05:46:46 +00002666 { // Thumb Opc S Shift Imm
2667 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2668 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2669 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2670 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2671 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2672 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
JF Bastien06ce03d2013-06-07 20:10:37 +00002673 }
2674 }
2675 };
2676
2677 unsigned SrcBits = SrcVT.getSizeInBits();
2678 unsigned DestBits = DestVT.getSizeInBits();
JF Bastien60a24422013-06-08 00:51:51 +00002679 (void) DestBits;
JF Bastien06ce03d2013-06-07 20:10:37 +00002680 assert((SrcBits < DestBits) && "can only extend to larger types");
2681 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2682 "other sizes unimplemented");
2683 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2684 "other sizes unimplemented");
2685
2686 bool hasV6Ops = Subtarget->hasV6Ops();
JF Bastiencd4c64d2013-07-17 05:46:46 +00002687 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
JF Bastien06ce03d2013-06-07 20:10:37 +00002688 assert((Bitness < 3) && "sanity-check table bounds");
2689
2690 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2691 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
JF Bastiencd4c64d2013-07-17 05:46:46 +00002692 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2693 unsigned Opc = ITP->Opc;
JF Bastien06ce03d2013-06-07 20:10:37 +00002694 assert(ARM::KILL != Opc && "Invalid table entry");
JF Bastiencd4c64d2013-07-17 05:46:46 +00002695 unsigned hasS = ITP->hasS;
2696 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2697 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2698 "only MOVsi has shift operand addressing mode");
2699 unsigned Imm = ITP->Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002700
2701 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2702 bool setsCPSR = &ARM::tGPRRegClass == RC;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002703 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
JF Bastien06ce03d2013-06-07 20:10:37 +00002704 unsigned ResultReg;
JF Bastiencd4c64d2013-07-17 05:46:46 +00002705 // MOVsi encodes shift and immediate in shift operand addressing mode.
2706 // The following condition has the same value when emitting two
2707 // instruction sequences: both are shifts.
2708 bool ImmIsSO = (Shift != ARM_AM::no_shift);
JF Bastien06ce03d2013-06-07 20:10:37 +00002709
2710 // Either one or two instructions are emitted.
2711 // They're always of the form:
2712 // dst = in OP imm
2713 // CPSR is set only by 16-bit Thumb instructions.
2714 // Predicate, if any, is AL.
2715 // S bit, if available, is always 0.
2716 // When two are emitted the first's result will feed as the second's input,
2717 // that value is then dead.
2718 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2719 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2720 ResultReg = createResultReg(RC);
JF Bastiencd4c64d2013-07-17 05:46:46 +00002721 bool isLsl = (0 == Instr) && !isSingleInstr;
2722 unsigned Opcode = isLsl ? LSLOpc : Opc;
2723 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2724 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
JF Bastien06ce03d2013-06-07 20:10:37 +00002725 bool isKill = 1 == Instr;
2726 MachineInstrBuilder MIB = BuildMI(
Rafael Espindolaea09c592014-02-18 22:05:46 +00002727 *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
JF Bastien06ce03d2013-06-07 20:10:37 +00002728 if (setsCPSR)
2729 MIB.addReg(ARM::CPSR, RegState::Define);
Jim Grosbach3fa74912013-08-16 23:37:36 +00002730 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
Diana Picus4f8c3e12017-01-13 09:37:56 +00002731 MIB.addReg(SrcReg, isKill * RegState::Kill)
2732 .addImm(ImmEnc)
2733 .add(predOps(ARMCC::AL));
JF Bastien06ce03d2013-06-07 20:10:37 +00002734 if (hasS)
Diana Picus8a73f552017-01-13 10:18:01 +00002735 MIB.add(condCodeOp());
JF Bastien06ce03d2013-06-07 20:10:37 +00002736 // Second instruction consumes the first's result.
2737 SrcReg = ResultReg;
Eli Friedmanc7035512011-05-25 23:49:02 +00002738 }
2739
Chad Rosier4489f942011-11-02 17:20:24 +00002740 return ResultReg;
2741}
2742
2743bool ARMFastISel::SelectIntExt(const Instruction *I) {
2744 // On ARM, in general, integer casts don't involve legal types; this code
2745 // handles promotable integers.
Chad Rosier4489f942011-11-02 17:20:24 +00002746 Type *DestTy = I->getType();
2747 Value *Src = I->getOperand(0);
2748 Type *SrcTy = Src->getType();
2749
Chad Rosier4489f942011-11-02 17:20:24 +00002750 bool isZExt = isa<ZExtInst>(I);
2751 unsigned SrcReg = getRegForValue(Src);
2752 if (!SrcReg) return false;
2753
Chad Rosier62a144f2012-12-17 19:59:43 +00002754 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00002755 SrcEVT = TLI.getValueType(DL, SrcTy, true);
2756 DestEVT = TLI.getValueType(DL, DestTy, true);
Chad Rosier62a144f2012-12-17 19:59:43 +00002757 if (!SrcEVT.isSimple()) return false;
2758 if (!DestEVT.isSimple()) return false;
Patrik Hagglundc494d242012-12-17 14:30:06 +00002759
Chad Rosier62a144f2012-12-17 19:59:43 +00002760 MVT SrcVT = SrcEVT.getSimpleVT();
2761 MVT DestVT = DestEVT.getSimpleVT();
Chad Rosier4489f942011-11-02 17:20:24 +00002762 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2763 if (ResultReg == 0) return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002764 updateValueMap(I, ResultReg);
Eli Friedmanc7035512011-05-25 23:49:02 +00002765 return true;
2766}
2767
Jush Lu4705da92012-08-03 02:37:48 +00002768bool ARMFastISel::SelectShift(const Instruction *I,
2769 ARM_AM::ShiftOpc ShiftTy) {
2770 // We handle thumb2 mode by target independent selector
2771 // or SelectionDAG ISel.
2772 if (isThumb2)
2773 return false;
2774
2775 // Only handle i32 now.
Mehdi Amini44ede332015-07-09 02:09:04 +00002776 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Jush Lu4705da92012-08-03 02:37:48 +00002777 if (DestVT != MVT::i32)
2778 return false;
2779
2780 unsigned Opc = ARM::MOVsr;
2781 unsigned ShiftImm;
2782 Value *Src2Value = I->getOperand(1);
2783 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2784 ShiftImm = CI->getZExtValue();
2785
2786 // Fall back to selection DAG isel if the shift amount
2787 // is zero or greater than the width of the value type.
2788 if (ShiftImm == 0 || ShiftImm >=32)
2789 return false;
2790
2791 Opc = ARM::MOVsi;
2792 }
2793
2794 Value *Src1Value = I->getOperand(0);
2795 unsigned Reg1 = getRegForValue(Src1Value);
2796 if (Reg1 == 0) return false;
2797
Nadav Rotema8e15b02012-09-06 11:13:55 +00002798 unsigned Reg2 = 0;
Jush Lu4705da92012-08-03 02:37:48 +00002799 if (Opc == ARM::MOVsr) {
2800 Reg2 = getRegForValue(Src2Value);
2801 if (Reg2 == 0) return false;
2802 }
2803
JF Bastien13969d02013-05-29 15:45:47 +00002804 unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
Jush Lu4705da92012-08-03 02:37:48 +00002805 if(ResultReg == 0) return false;
2806
Rafael Espindolaea09c592014-02-18 22:05:46 +00002807 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jush Lu4705da92012-08-03 02:37:48 +00002808 TII.get(Opc), ResultReg)
2809 .addReg(Reg1);
2810
2811 if (Opc == ARM::MOVsi)
2812 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2813 else if (Opc == ARM::MOVsr) {
2814 MIB.addReg(Reg2);
2815 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2816 }
2817
2818 AddOptionalDefs(MIB);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002819 updateValueMap(I, ResultReg);
Jush Lu4705da92012-08-03 02:37:48 +00002820 return true;
2821}
2822
Eric Christopherc3e118e2010-09-02 23:43:26 +00002823// TODO: SoftFP support.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002824bool ARMFastISel::fastSelectInstruction(const Instruction *I) {
Eric Christopher84bdfd82010-07-21 22:26:11 +00002825 switch (I->getOpcode()) {
Eric Christopher00202ee2010-08-23 21:44:12 +00002826 case Instruction::Load:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002827 return SelectLoad(I);
Eric Christopherfde5a3d2010-09-01 22:16:27 +00002828 case Instruction::Store:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002829 return SelectStore(I);
Eric Christopher6aaed722010-09-03 00:35:47 +00002830 case Instruction::Br:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002831 return SelectBranch(I);
Chad Rosierded4c992012-02-07 23:56:08 +00002832 case Instruction::IndirectBr:
2833 return SelectIndirectBr(I);
Eric Christopherc3e9c402010-09-08 23:13:45 +00002834 case Instruction::ICmp:
2835 case Instruction::FCmp:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002836 return SelectCmp(I);
Eric Christopherf14b9bf2010-09-09 00:26:48 +00002837 case Instruction::FPExt:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002838 return SelectFPExt(I);
Eric Christopher5903c0b2010-09-09 20:26:31 +00002839 case Instruction::FPTrunc:
Eric Christopher29ab6d12010-09-27 06:02:23 +00002840 return SelectFPTrunc(I);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002841 case Instruction::SIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002842 return SelectIToFP(I, /*isSigned*/ true);
Chad Rosiera8a8ac52012-02-03 19:42:52 +00002843 case Instruction::UIToFP:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002844 return SelectIToFP(I, /*isSigned*/ false);
Eric Christopher6e3eeba2010-09-09 18:54:59 +00002845 case Instruction::FPToSI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002846 return SelectFPToI(I, /*isSigned*/ true);
Chad Rosier41f0e782012-02-03 20:27:51 +00002847 case Instruction::FPToUI:
Chad Rosiere023d5d2012-02-03 21:14:11 +00002848 return SelectFPToI(I, /*isSigned*/ false);
Chad Rosier685b20c2012-02-06 23:50:07 +00002849 case Instruction::Add:
2850 return SelectBinaryIntOp(I, ISD::ADD);
Chad Rosierbd471252012-02-08 02:29:21 +00002851 case Instruction::Or:
2852 return SelectBinaryIntOp(I, ISD::OR);
Chad Rosier0ee8c512012-02-08 02:45:44 +00002853 case Instruction::Sub:
2854 return SelectBinaryIntOp(I, ISD::SUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002855 case Instruction::FAdd:
Chad Rosier685b20c2012-02-06 23:50:07 +00002856 return SelectBinaryFPOp(I, ISD::FADD);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002857 case Instruction::FSub:
Chad Rosier685b20c2012-02-06 23:50:07 +00002858 return SelectBinaryFPOp(I, ISD::FSUB);
Eric Christopher24dc27f2010-09-09 00:53:57 +00002859 case Instruction::FMul:
Chad Rosier685b20c2012-02-06 23:50:07 +00002860 return SelectBinaryFPOp(I, ISD::FMUL);
Eric Christopher8b912662010-09-14 23:03:37 +00002861 case Instruction::SDiv:
Chad Rosieraaa55a82012-02-03 21:07:27 +00002862 return SelectDiv(I, /*isSigned*/ true);
2863 case Instruction::UDiv:
2864 return SelectDiv(I, /*isSigned*/ false);
Eric Christophereae1b382010-10-11 08:37:26 +00002865 case Instruction::SRem:
Chad Rosierb84a4b42012-02-03 21:23:45 +00002866 return SelectRem(I, /*isSigned*/ true);
2867 case Instruction::URem:
2868 return SelectRem(I, /*isSigned*/ false);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002869 case Instruction::Call:
Chad Rosiera7ebc562011-11-11 23:31:03 +00002870 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2871 return SelectIntrinsicCall(*II);
Eric Christopher78f8d4e2010-09-30 20:49:44 +00002872 return SelectCall(I);
Eric Christopher511aa312010-10-11 08:27:59 +00002873 case Instruction::Select:
2874 return SelectSelect(I);
Eric Christopher93bbe652010-10-22 01:28:00 +00002875 case Instruction::Ret:
2876 return SelectRet(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002877 case Instruction::Trunc:
Chad Rosieree7e4522011-11-02 00:18:48 +00002878 return SelectTrunc(I);
Eli Friedmanc7035512011-05-25 23:49:02 +00002879 case Instruction::ZExt:
2880 case Instruction::SExt:
Chad Rosieree7e4522011-11-02 00:18:48 +00002881 return SelectIntExt(I);
Jush Lu4705da92012-08-03 02:37:48 +00002882 case Instruction::Shl:
2883 return SelectShift(I, ARM_AM::lsl);
2884 case Instruction::LShr:
2885 return SelectShift(I, ARM_AM::lsr);
2886 case Instruction::AShr:
2887 return SelectShift(I, ARM_AM::asr);
Eric Christopher84bdfd82010-07-21 22:26:11 +00002888 default: break;
2889 }
2890 return false;
2891}
2892
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002893namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +00002894
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002895// This table describes sign- and zero-extend instructions which can be
2896// folded into a preceding load. All of these extends have an immediate
2897// (sometimes a mask and sometimes a shift) that's applied after
2898// extension.
2899const struct FoldableLoadExtendsStruct {
2900 uint16_t Opc[2]; // ARM, Thumb.
2901 uint8_t ExpectedImm;
2902 uint8_t isZExt : 1;
2903 uint8_t ExpectedVT : 7;
2904} FoldableLoadExtends[] = {
2905 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2906 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2907 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2908 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2909 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2910};
Eugene Zelenko342257e2017-01-31 00:56:17 +00002911
2912} // end anonymous namespace
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002913
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002914/// \brief The specified machine instr operand is a vreg, and that
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002915/// vreg is being provided by the specified load instruction. If possible,
2916/// try to fold the load as an operand to the instruction, returning true if
2917/// successful.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002918bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2919 const LoadInst *LI) {
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002920 // Verify we have a legal type before going any further.
2921 MVT VT;
2922 if (!isLoadTypeLegal(LI->getType(), VT))
2923 return false;
2924
2925 // Combine load followed by zero- or sign-extend.
2926 // ldrb r1, [r0] ldrb r1, [r0]
2927 // uxtb r2, r1 =>
2928 // mov r3, r2 mov r3, r1
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002929 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
2930 return false;
2931 const uint64_t Imm = MI->getOperand(2).getImm();
2932
2933 bool Found = false;
2934 bool isZExt;
Javed Absar5b8e4872017-07-18 10:19:48 +00002935 for (const FoldableLoadExtendsStruct &FLE : FoldableLoadExtends) {
2936 if (FLE.Opc[isThumb2] == MI->getOpcode() &&
2937 (uint64_t)FLE.ExpectedImm == Imm &&
2938 MVT((MVT::SimpleValueType)FLE.ExpectedVT) == VT) {
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002939 Found = true;
Javed Absar5b8e4872017-07-18 10:19:48 +00002940 isZExt = FLE.isZExt;
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002941 }
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002942 }
JF Bastien3c6bb8e2013-06-11 22:13:46 +00002943 if (!Found) return false;
2944
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002945 // See if we can handle this address.
2946 Address Addr;
2947 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
Jush Luac96b762012-06-14 06:08:19 +00002948
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002949 unsigned ResultReg = MI->getOperand(0).getReg();
Chad Rosier563de602011-12-13 19:22:14 +00002950 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
Chad Rosierc8cfd3a2011-11-13 02:23:59 +00002951 return false;
2952 MI->eraseFromParent();
2953 return true;
2954}
2955
Jush Lu47172a02012-09-27 05:21:41 +00002956unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002957 unsigned Align, MVT VT) {
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002958 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Jush Lu47172a02012-09-27 05:21:41 +00002959
Peter Collingbourne97aae402015-10-26 18:23:16 +00002960 LLVMContext *Context = &MF->getFunction()->getContext();
2961 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2962 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2963 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2964 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2965 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2966 /*AddCurrentAddress=*/UseGOT_PREL);
Jush Lu47172a02012-09-27 05:21:41 +00002967
Peter Collingbourne97aae402015-10-26 18:23:16 +00002968 unsigned ConstAlign =
2969 MF->getDataLayout().getPrefTypeAlignment(Type::getInt32PtrTy(*Context));
2970 unsigned Idx = MF->getConstantPool()->getConstantPoolIndex(CPV, ConstAlign);
Jush Lu47172a02012-09-27 05:21:41 +00002971
Peter Collingbourne97aae402015-10-26 18:23:16 +00002972 unsigned TempReg = MF->getRegInfo().createVirtualRegister(&ARM::rGPRRegClass);
2973 unsigned Opc = isThumb2 ? ARM::t2LDRpci : ARM::LDRcp;
2974 MachineInstrBuilder MIB =
2975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), TempReg)
2976 .addConstantPoolIndex(Idx);
2977 if (Opc == ARM::LDRcp)
Jush Lu47172a02012-09-27 05:21:41 +00002978 MIB.addImm(0);
Diana Picus4f8c3e12017-01-13 09:37:56 +00002979 MIB.add(predOps(ARMCC::AL));
Jush Lu47172a02012-09-27 05:21:41 +00002980
Peter Collingbourne97aae402015-10-26 18:23:16 +00002981 // Fix the address by adding pc.
2982 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
2983 Opc = Subtarget->isThumb() ? ARM::tPICADD : UseGOT_PREL ? ARM::PICLDR
2984 : ARM::PICADD;
2985 DestReg = constrainOperandRegClass(TII.get(Opc), DestReg, 0);
2986 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2987 .addReg(TempReg)
2988 .addImm(ARMPCLabelIndex);
2989 if (!Subtarget->isThumb())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002990 MIB.add(predOps(ARMCC::AL));
Peter Collingbourne97aae402015-10-26 18:23:16 +00002991
2992 if (UseGOT_PREL && Subtarget->isThumb()) {
2993 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
2994 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2995 TII.get(ARM::t2LDRi12), NewDestReg)
2996 .addReg(DestReg)
2997 .addImm(0);
2998 DestReg = NewDestReg;
2999 AddOptionalDefs(MIB);
3000 }
3001 return DestReg;
Jush Lu47172a02012-09-27 05:21:41 +00003002}
3003
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00003004bool ARMFastISel::fastLowerArguments() {
Evan Cheng615620c2013-02-11 01:27:15 +00003005 if (!FuncInfo.CanLowerReturn)
3006 return false;
3007
3008 const Function *F = FuncInfo.Fn;
3009 if (F->isVarArg())
3010 return false;
3011
3012 CallingConv::ID CC = F->getCallingConv();
3013 switch (CC) {
3014 default:
3015 return false;
3016 case CallingConv::Fast:
3017 case CallingConv::C:
3018 case CallingConv::ARM_AAPCS_VFP:
3019 case CallingConv::ARM_AAPCS:
3020 case CallingConv::ARM_APCS:
Manman Ren802cd6f2016-04-05 22:44:44 +00003021 case CallingConv::Swift:
Evan Cheng615620c2013-02-11 01:27:15 +00003022 break;
3023 }
3024
3025 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3026 // which are passed in r0 - r3.
Reid Kleckner6652a522017-04-28 18:37:16 +00003027 for (const Argument &Arg : F->args()) {
3028 if (Arg.getArgNo() >= 4)
Evan Cheng615620c2013-02-11 01:27:15 +00003029 return false;
3030
Reid Kleckner6652a522017-04-28 18:37:16 +00003031 if (Arg.hasAttribute(Attribute::InReg) ||
3032 Arg.hasAttribute(Attribute::StructRet) ||
3033 Arg.hasAttribute(Attribute::SwiftSelf) ||
3034 Arg.hasAttribute(Attribute::SwiftError) ||
3035 Arg.hasAttribute(Attribute::ByVal))
Evan Cheng615620c2013-02-11 01:27:15 +00003036 return false;
3037
Reid Kleckner6652a522017-04-28 18:37:16 +00003038 Type *ArgTy = Arg.getType();
Evan Cheng615620c2013-02-11 01:27:15 +00003039 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3040 return false;
3041
Mehdi Amini44ede332015-07-09 02:09:04 +00003042 EVT ArgVT = TLI.getValueType(DL, ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00003043 if (!ArgVT.isSimple()) return false;
Evan Cheng615620c2013-02-11 01:27:15 +00003044 switch (ArgVT.getSimpleVT().SimpleTy) {
3045 case MVT::i8:
3046 case MVT::i16:
3047 case MVT::i32:
3048 break;
3049 default:
3050 return false;
3051 }
3052 }
3053
Craig Toppere5e035a32015-12-05 07:13:35 +00003054 static const MCPhysReg GPRArgRegs[] = {
Evan Cheng615620c2013-02-11 01:27:15 +00003055 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3056 };
3057
Jim Grosbachd69f3ed2013-08-16 23:37:23 +00003058 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
Javed Absar5b8e4872017-07-18 10:19:48 +00003059 for (const Argument &Arg : F->args()) {
3060 unsigned ArgNo = Arg.getArgNo();
Reid Kleckner6652a522017-04-28 18:37:16 +00003061 unsigned SrcReg = GPRArgRegs[ArgNo];
Evan Cheng615620c2013-02-11 01:27:15 +00003062 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3063 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3064 // Without this, EmitLiveInCopies may eliminate the livein if its only
3065 // use is a bitcast (which isn't turned into an instruction).
3066 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3068 TII.get(TargetOpcode::COPY),
Evan Cheng615620c2013-02-11 01:27:15 +00003069 ResultReg).addReg(DstReg, getKillRegState(true));
Javed Absar5b8e4872017-07-18 10:19:48 +00003070 updateValueMap(&Arg, ResultReg);
Evan Cheng615620c2013-02-11 01:27:15 +00003071 }
3072
3073 return true;
3074}
3075
Eric Christopher84bdfd82010-07-21 22:26:11 +00003076namespace llvm {
Eugene Zelenko342257e2017-01-31 00:56:17 +00003077
Bob Wilson3e6fa462012-08-03 04:06:28 +00003078 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
3079 const TargetLibraryInfo *libInfo) {
Akira Hatanakaddf76aa2015-05-23 01:14:08 +00003080 if (funcInfo.MF->getSubtarget<ARMSubtarget>().useFastISel())
Bob Wilson3e6fa462012-08-03 04:06:28 +00003081 return new ARMFastISel(funcInfo, libInfo);
Akira Hatanakaddf76aa2015-05-23 01:14:08 +00003082
Craig Topper062a2ba2014-04-25 05:30:21 +00003083 return nullptr;
Eric Christopher84bdfd82010-07-21 22:26:11 +00003084 }
Eugene Zelenko342257e2017-01-31 00:56:17 +00003085
3086} // end namespace llvm