blob: f4776adb069c961e1eb4e7c25f2cf6e89306c9ca [file] [log] [blame]
Matt Arsenault7836f892016-01-20 21:22:21 +00001//===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===//
Tom Stellard75aadc22012-12-11 21:25:42 +00002//
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/// \file
11/// \brief Defines an instruction selector for the AMDGPU target.
12//
13//===----------------------------------------------------------------------===//
Matt Arsenault592d0682015-12-01 23:04:05 +000014
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000015#include "AMDGPU.h"
Matt Arsenault7016f132017-08-03 22:30:46 +000016#include "AMDGPUArgumentUsageInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "AMDGPUISelLowering.h" // For AMDGPUISD
Tom Stellard75aadc22012-12-11 21:25:42 +000018#include "AMDGPUInstrInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000019#include "AMDGPURegisterInfo.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000020#include "AMDGPUSubtarget.h"
Matt Arsenaultcc852232017-10-10 20:22:07 +000021#include "AMDGPUTargetMachine.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000022#include "SIDefines.h"
Christian Konigf82901a2013-02-26 17:52:23 +000023#include "SIISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "SIInstrInfo.h"
Tom Stellardb02094e2014-07-21 15:45:01 +000025#include "SIMachineFunctionInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000026#include "SIRegisterInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000027#include "llvm/ADT/APInt.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/StringRef.h"
Jan Veselyf97de002016-05-13 20:39:29 +000030#include "llvm/Analysis/ValueTracking.h"
Tom Stellard58ac7442014-04-29 23:12:48 +000031#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000032#include "llvm/CodeGen/ISDOpcodes.h"
33#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/MachineValueType.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000036#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037#include "llvm/CodeGen/SelectionDAGISel.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000038#include "llvm/CodeGen/SelectionDAGNodes.h"
39#include "llvm/CodeGen/ValueTypes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Instruction.h"
42#include "llvm/MC/MCInstrDesc.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/CodeGen.h"
45#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/MathExtras.h"
47#include <cassert>
48#include <cstdint>
49#include <new>
50#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000051
52using namespace llvm;
53
Matt Arsenaultd2759212016-02-13 01:24:08 +000054namespace llvm {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000055
Matt Arsenaultd2759212016-02-13 01:24:08 +000056class R600InstrInfo;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000057
58} // end namespace llvm
Matt Arsenaultd2759212016-02-13 01:24:08 +000059
Tom Stellard75aadc22012-12-11 21:25:42 +000060//===----------------------------------------------------------------------===//
61// Instruction Selector Implementation
62//===----------------------------------------------------------------------===//
63
64namespace {
Tom Stellardbc4497b2016-02-12 23:45:29 +000065
Tom Stellard75aadc22012-12-11 21:25:42 +000066/// AMDGPU specific code to select AMDGPU machine instructions for
67/// SelectionDAG operations.
68class AMDGPUDAGToDAGISel : public SelectionDAGISel {
69 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
70 // make the right decision when generating code for different targets.
Eric Christopher7792e322015-01-30 23:24:40 +000071 const AMDGPUSubtarget *Subtarget;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000072 AMDGPUAS AMDGPUASI;
Matt Arsenaultcc852232017-10-10 20:22:07 +000073 bool EnableLateStructurizeCFG;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +000074
Tom Stellard75aadc22012-12-11 21:25:42 +000075public:
Matt Arsenault7016f132017-08-03 22:30:46 +000076 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr,
77 CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
78 : SelectionDAGISel(*TM, OptLevel) {
79 AMDGPUASI = AMDGPU::getAMDGPUAS(*TM);
Matt Arsenaultcc852232017-10-10 20:22:07 +000080 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000081 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000082 ~AMDGPUDAGToDAGISel() override = default;
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +000083
Matt Arsenault7016f132017-08-03 22:30:46 +000084 void getAnalysisUsage(AnalysisUsage &AU) const override {
85 AU.addRequired<AMDGPUArgumentUsageInfo>();
86 SelectionDAGISel::getAnalysisUsage(AU);
87 }
88
Eric Christopher7792e322015-01-30 23:24:40 +000089 bool runOnMachineFunction(MachineFunction &MF) override;
Justin Bogner95927c02016-05-12 21:03:32 +000090 void Select(SDNode *N) override;
Mehdi Amini117296c2016-10-01 02:56:57 +000091 StringRef getPassName() const override;
Craig Topper5656db42014-04-29 07:57:24 +000092 void PostprocessISelDAG() override;
Tom Stellard75aadc22012-12-11 21:25:42 +000093
Tom Stellard20287692017-08-08 04:57:55 +000094protected:
95 void SelectBuildVector(SDNode *N, unsigned RegClassID);
96
Tom Stellard75aadc22012-12-11 21:25:42 +000097private:
Matt Arsenault156d3ae2017-05-17 21:02:58 +000098 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +000099 bool isNoNanSrc(SDValue N) const;
Matt Arsenaultfe267752016-07-28 00:32:02 +0000100 bool isInlineImmediate(const SDNode *N) const;
Vincent Lejeunec6896792013-06-04 23:17:15 +0000101 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
Tom Stellard84021442013-07-23 01:48:24 +0000102 const R600InstrInfo *TII);
Tom Stellard365366f2013-01-23 02:09:06 +0000103 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Vincent Lejeunec6896792013-06-04 23:17:15 +0000104 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellard75aadc22012-12-11 21:25:42 +0000105
Jan Vesely43b7b5b2016-04-07 19:23:11 +0000106 bool isConstantLoad(const MemSDNode *N, int cbID) const;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000107 bool isUniformBr(const SDNode *N) const;
108
Tom Stellard381a94a2015-05-12 15:00:49 +0000109 SDNode *glueCopyToM0(SDNode *N) const;
110
Tom Stellarddf94dc32013-08-14 23:24:24 +0000111 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
Tom Stellard365366f2013-01-23 02:09:06 +0000112 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
Matt Arsenault209a7b92014-04-18 07:40:20 +0000113 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
114 SDValue& Offset);
Tom Stellard20287692017-08-08 04:57:55 +0000115 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
116 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000117 bool isDSOffsetLegal(const SDValue &Base, unsigned Offset,
118 unsigned OffsetBits) const;
119 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
Tom Stellardf3fc5552014-08-22 18:49:35 +0000120 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
121 SDValue &Offset1) const;
Changpeng Fangb41574a2015-12-22 20:55:23 +0000122 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard155bbb72014-08-11 22:18:17 +0000123 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
124 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
125 SDValue &TFE) const;
126 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard1f9939f2015-02-27 14:59:41 +0000127 SDValue &SOffset, SDValue &Offset, SDValue &GLC,
128 SDValue &SLC, SDValue &TFE) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000129 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +0000130 SDValue &VAddr, SDValue &SOffset, SDValue &Offset,
Tom Stellard7980fc82014-09-25 18:30:26 +0000131 SDValue &SLC) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000132 bool SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000133 SDValue Addr, SDValue &RSrc, SDValue &VAddr,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000134 SDValue &SOffset, SDValue &ImmOffset) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000135 bool SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000136 SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000137 SDValue &Offset) const;
138
Tom Stellard155bbb72014-08-11 22:18:17 +0000139 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
140 SDValue &Offset, SDValue &GLC, SDValue &SLC,
Tom Stellardb02094e2014-07-21 15:45:01 +0000141 SDValue &TFE) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000142 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault88701812016-06-09 23:42:48 +0000143 SDValue &Offset, SDValue &SLC) const;
Jan Vesely43b7b5b2016-04-07 19:23:11 +0000144 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
145 SDValue &Offset) const;
Nicolai Haehnlea6092592016-06-15 07:13:05 +0000146 bool SelectMUBUFConstant(SDValue Constant,
Nicolai Haehnle3003ba02016-03-18 16:24:20 +0000147 SDValue &SOffset,
148 SDValue &ImmOffset) const;
149 bool SelectMUBUFIntrinsicOffset(SDValue Offset, SDValue &SOffset,
150 SDValue &ImmOffset) const;
151 bool SelectMUBUFIntrinsicVOffset(SDValue Offset, SDValue &SOffset,
152 SDValue &ImmOffset, SDValue &VOffset) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000153
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000154 bool SelectFlatAtomic(SDValue Addr, SDValue &VAddr,
155 SDValue &Offset, SDValue &SLC) const;
Matt Arsenault4e309b02017-07-29 01:03:53 +0000156 bool SelectFlatAtomicSigned(SDValue Addr, SDValue &VAddr,
157 SDValue &Offset, SDValue &SLC) const;
158
159 template <bool IsSigned>
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000160 bool SelectFlatOffset(SDValue Addr, SDValue &VAddr,
161 SDValue &Offset, SDValue &SLC) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000162
Tom Stellarddee26a22015-08-06 19:28:30 +0000163 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset,
164 bool &Imm) const;
165 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset,
166 bool &Imm) const;
167 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000168 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000169 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
170 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000171 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const;
Nicolai Haehnle7968c342016-07-12 08:12:16 +0000172 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000173
174 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000175 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods) const;
Tom Stellardb4a313a2014-08-01 00:32:39 +0000176 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Matt Arsenaultdf58e822017-04-25 21:17:38 +0000177 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const;
Tom Stellardb4a313a2014-08-01 00:32:39 +0000178 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
179 SDValue &Clamp, SDValue &Omod) const;
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000180 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
181 SDValue &Clamp, SDValue &Omod) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000182
Matt Arsenault4831ce52015-01-06 23:00:37 +0000183 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods,
184 SDValue &Clamp,
185 SDValue &Omod) const;
Matt Arsenault1cffa4c2014-11-13 19:49:04 +0000186
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +0000187 bool SelectVOP3OMods(SDValue In, SDValue &Src,
188 SDValue &Clamp, SDValue &Omod) const;
189
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000190 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
191 bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
192 SDValue &Clamp) const;
193
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +0000194 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const;
195 bool SelectVOP3OpSel0(SDValue In, SDValue &Src, SDValue &SrcMods,
196 SDValue &Clamp) const;
197
198 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
199 bool SelectVOP3OpSelMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
200 SDValue &Clamp) const;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000201 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const;
Matt Arsenault76935122017-09-20 20:28:39 +0000202 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +0000203
Matt Arsenaulte1cd4822017-11-13 00:22:09 +0000204 bool SelectHi16Elt(SDValue In, SDValue &Src) const;
205
Justin Bogner95927c02016-05-12 21:03:32 +0000206 void SelectADD_SUB_I64(SDNode *N);
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000207 void SelectUADDO_USUBO(SDNode *N);
Justin Bogner95927c02016-05-12 21:03:32 +0000208 void SelectDIV_SCALE(SDNode *N);
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000209 void SelectMAD_64_32(SDNode *N);
Tom Stellard8485fa02016-12-07 02:42:15 +0000210 void SelectFMA_W_CHAIN(SDNode *N);
211 void SelectFMUL_W_CHAIN(SDNode *N);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000212
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000213 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val,
Marek Olsak9b728682015-03-24 13:40:27 +0000214 uint32_t Offset, uint32_t Width);
Justin Bogner95927c02016-05-12 21:03:32 +0000215 void SelectS_BFEFromShifts(SDNode *N);
216 void SelectS_BFE(SDNode *N);
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +0000217 bool isCBranchSCC(const SDNode *N) const;
Justin Bogner95927c02016-05-12 21:03:32 +0000218 void SelectBRCOND(SDNode *N);
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000219 void SelectFMAD(SDNode *N);
Matt Arsenault88701812016-06-09 23:42:48 +0000220 void SelectATOMIC_CMP_SWAP(SDNode *N);
Marek Olsak9b728682015-03-24 13:40:27 +0000221
Tom Stellard20287692017-08-08 04:57:55 +0000222protected:
Tom Stellard75aadc22012-12-11 21:25:42 +0000223 // Include the pieces autogenerated from the target description.
224#include "AMDGPUGenDAGISel.inc"
225};
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000226
Tom Stellard20287692017-08-08 04:57:55 +0000227class R600DAGToDAGISel : public AMDGPUDAGToDAGISel {
228public:
229 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) :
230 AMDGPUDAGToDAGISel(TM, OptLevel) {}
231
232 void Select(SDNode *N) override;
233
234 bool SelectADDRIndirect(SDValue Addr, SDValue &Base,
235 SDValue &Offset) override;
236 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
237 SDValue &Offset) override;
238};
239
Tom Stellard75aadc22012-12-11 21:25:42 +0000240} // end anonymous namespace
241
Matt Arsenault7016f132017-08-03 22:30:46 +0000242INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "isel",
243 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
244INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo)
245INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "isel",
246 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
247
Tom Stellard75aadc22012-12-11 21:25:42 +0000248/// \brief This pass converts a legalized DAG into a AMDGPU-specific
249// DAG, ready for instruction scheduling.
Matt Arsenault7016f132017-08-03 22:30:46 +0000250FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM,
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +0000251 CodeGenOpt::Level OptLevel) {
252 return new AMDGPUDAGToDAGISel(TM, OptLevel);
Tom Stellard75aadc22012-12-11 21:25:42 +0000253}
254
Tom Stellard20287692017-08-08 04:57:55 +0000255/// \brief This pass converts a legalized DAG into a R600-specific
256// DAG, ready for instruction scheduling.
257FunctionPass *llvm::createR600ISelDag(TargetMachine *TM,
258 CodeGenOpt::Level OptLevel) {
259 return new R600DAGToDAGISel(TM, OptLevel);
260}
261
Eric Christopher7792e322015-01-30 23:24:40 +0000262bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000263 Subtarget = &MF.getSubtarget<AMDGPUSubtarget>();
Eric Christopher7792e322015-01-30 23:24:40 +0000264 return SelectionDAGISel::runOnMachineFunction(MF);
Tom Stellard75aadc22012-12-11 21:25:42 +0000265}
266
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000267bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const {
268 if (TM.Options.NoNaNsFPMath)
269 return true;
270
271 // TODO: Move into isKnownNeverNaN
Amara Emersond28f0cd42017-05-01 15:17:51 +0000272 if (N->getFlags().isDefined())
273 return N->getFlags().hasNoNaNs();
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000274
275 return CurDAG->isKnownNeverNaN(N);
276}
277
Matt Arsenaultfe267752016-07-28 00:32:02 +0000278bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N) const {
279 const SIInstrInfo *TII
280 = static_cast<const SISubtarget *>(Subtarget)->getInstrInfo();
281
282 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
283 return TII->isInlineConstant(C->getAPIntValue());
284
285 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
286 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt());
287
288 return false;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000289}
290
Tom Stellarddf94dc32013-08-14 23:24:24 +0000291/// \brief Determine the register class for \p OpNo
292/// \returns The register class of the virtual register that will be used for
293/// the given operand number \OpNo or NULL if the register class cannot be
294/// determined.
295const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
296 unsigned OpNo) const {
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000297 if (!N->isMachineOpcode()) {
298 if (N->getOpcode() == ISD::CopyToReg) {
299 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
300 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
301 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
302 return MRI.getRegClass(Reg);
303 }
304
305 const SIRegisterInfo *TRI
306 = static_cast<const SISubtarget *>(Subtarget)->getRegisterInfo();
307 return TRI->getPhysRegClass(Reg);
308 }
309
Matt Arsenault209a7b92014-04-18 07:40:20 +0000310 return nullptr;
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000311 }
Matt Arsenault209a7b92014-04-18 07:40:20 +0000312
Tom Stellarddf94dc32013-08-14 23:24:24 +0000313 switch (N->getMachineOpcode()) {
314 default: {
Eric Christopherd9134482014-08-04 21:25:23 +0000315 const MCInstrDesc &Desc =
Eric Christopher7792e322015-01-30 23:24:40 +0000316 Subtarget->getInstrInfo()->get(N->getMachineOpcode());
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000317 unsigned OpIdx = Desc.getNumDefs() + OpNo;
318 if (OpIdx >= Desc.getNumOperands())
Matt Arsenault209a7b92014-04-18 07:40:20 +0000319 return nullptr;
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000320 int RegClass = Desc.OpInfo[OpIdx].RegClass;
Matt Arsenault209a7b92014-04-18 07:40:20 +0000321 if (RegClass == -1)
322 return nullptr;
323
Eric Christopher7792e322015-01-30 23:24:40 +0000324 return Subtarget->getRegisterInfo()->getRegClass(RegClass);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000325 }
326 case AMDGPU::REG_SEQUENCE: {
Matt Arsenault209a7b92014-04-18 07:40:20 +0000327 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christopherd9134482014-08-04 21:25:23 +0000328 const TargetRegisterClass *SuperRC =
Eric Christopher7792e322015-01-30 23:24:40 +0000329 Subtarget->getRegisterInfo()->getRegClass(RCID);
Matt Arsenault209a7b92014-04-18 07:40:20 +0000330
331 SDValue SubRegOp = N->getOperand(OpNo + 1);
332 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
Eric Christopher7792e322015-01-30 23:24:40 +0000333 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC,
334 SubRegIdx);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000335 }
336 }
337}
338
Tom Stellard381a94a2015-05-12 15:00:49 +0000339SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const {
Matt Arsenault3f71c0e2017-11-29 00:55:57 +0000340 if (cast<MemSDNode>(N)->getAddressSpace() != AMDGPUASI.LOCAL_ADDRESS ||
341 !Subtarget->ldsRequiresM0Init())
Tom Stellard381a94a2015-05-12 15:00:49 +0000342 return N;
343
344 const SITargetLowering& Lowering =
345 *static_cast<const SITargetLowering*>(getTargetLowering());
346
347 // Write max value to m0 before each load operation
348
349 SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N),
350 CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32));
351
352 SDValue Glue = M0.getValue(1);
353
354 SmallVector <SDValue, 8> Ops;
355 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
356 Ops.push_back(N->getOperand(i));
357 }
358 Ops.push_back(Glue);
Matt Arsenaulte6667de2017-12-04 22:18:22 +0000359 return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops);
Tom Stellard381a94a2015-05-12 15:00:49 +0000360}
361
Matt Arsenault61cb6fa2015-11-11 00:01:36 +0000362static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) {
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000363 switch (NumVectorElts) {
364 case 1:
Marek Olsak79c05872016-11-25 17:37:09 +0000365 return AMDGPU::SReg_32_XM0RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000366 case 2:
367 return AMDGPU::SReg_64RegClassID;
368 case 4:
369 return AMDGPU::SReg_128RegClassID;
370 case 8:
371 return AMDGPU::SReg_256RegClassID;
372 case 16:
373 return AMDGPU::SReg_512RegClassID;
374 }
375
376 llvm_unreachable("invalid vector size");
377}
378
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000379static bool getConstantValue(SDValue N, uint32_t &Out) {
380 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
381 Out = C->getAPIntValue().getZExtValue();
382 return true;
383 }
384
385 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) {
386 Out = C->getValueAPF().bitcastToAPInt().getZExtValue();
387 return true;
388 }
389
390 return false;
391}
392
Tom Stellard20287692017-08-08 04:57:55 +0000393void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) {
Tom Stellard20287692017-08-08 04:57:55 +0000394 EVT VT = N->getValueType(0);
395 unsigned NumVectorElts = VT.getVectorNumElements();
396 EVT EltVT = VT.getVectorElementType();
397 const AMDGPURegisterInfo *TRI = Subtarget->getRegisterInfo();
398 SDLoc DL(N);
399 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
400
401 if (NumVectorElts == 1) {
402 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0),
403 RegClass);
404 return;
405 }
406
407 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
408 "supported yet");
409 // 16 = Max Num Vector Elements
410 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
411 // 1 = Vector Register Class
412 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
413
414 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
415 bool IsRegSeq = true;
416 unsigned NOps = N->getNumOperands();
417 for (unsigned i = 0; i < NOps; i++) {
418 // XXX: Why is this here?
419 if (isa<RegisterSDNode>(N->getOperand(i))) {
420 IsRegSeq = false;
421 break;
422 }
423 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
424 RegSeqArgs[1 + (2 * i) + 1] =
425 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL,
426 MVT::i32);
427 }
428 if (NOps != NumVectorElts) {
429 // Fill in the missing undef elements if this was a scalar_to_vector.
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000430 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
Tom Stellard20287692017-08-08 04:57:55 +0000431 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
432 DL, EltVT);
433 for (unsigned i = NOps; i < NumVectorElts; ++i) {
434 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
435 RegSeqArgs[1 + (2 * i) + 1] =
436 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, MVT::i32);
437 }
438 }
439
440 if (!IsRegSeq)
441 SelectCode(N);
442 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs);
443}
444
Justin Bogner95927c02016-05-12 21:03:32 +0000445void AMDGPUDAGToDAGISel::Select(SDNode *N) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000446 unsigned int Opc = N->getOpcode();
447 if (N->isMachineOpcode()) {
Tim Northover31d093c2013-09-22 08:21:56 +0000448 N->setNodeId(-1);
Justin Bogner95927c02016-05-12 21:03:32 +0000449 return; // Already selected.
Tom Stellard75aadc22012-12-11 21:25:42 +0000450 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000451
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000452 if (isa<AtomicSDNode>(N) ||
453 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC))
Tom Stellard381a94a2015-05-12 15:00:49 +0000454 N = glueCopyToM0(N);
455
Tom Stellard75aadc22012-12-11 21:25:42 +0000456 switch (Opc) {
Matt Arsenault84445dd2017-11-30 22:51:26 +0000457 default:
458 break;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000459 // We are selecting i64 ADD here instead of custom lower it during
460 // DAG legalization, so we can fold some i64 ADDs used for address
461 // calculation into the LOAD and STORE instructions.
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000462 case ISD::ADDC:
463 case ISD::ADDE:
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000464 case ISD::SUBC:
465 case ISD::SUBE: {
Tom Stellard20287692017-08-08 04:57:55 +0000466 if (N->getValueType(0) != MVT::i64)
Tom Stellard1f15bff2014-02-25 21:36:18 +0000467 break;
468
Justin Bogner95927c02016-05-12 21:03:32 +0000469 SelectADD_SUB_I64(N);
470 return;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000471 }
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000472 case ISD::UADDO:
473 case ISD::USUBO: {
474 SelectUADDO_USUBO(N);
475 return;
476 }
Tom Stellard8485fa02016-12-07 02:42:15 +0000477 case AMDGPUISD::FMUL_W_CHAIN: {
478 SelectFMUL_W_CHAIN(N);
479 return;
480 }
481 case AMDGPUISD::FMA_W_CHAIN: {
482 SelectFMA_W_CHAIN(N);
483 return;
484 }
485
Matt Arsenault064c2062014-06-11 17:40:32 +0000486 case ISD::SCALAR_TO_VECTOR:
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000487 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000488 EVT VT = N->getValueType(0);
489 unsigned NumVectorElts = VT.getVectorNumElements();
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000490
491 if (VT == MVT::v2i16 || VT == MVT::v2f16) {
492 if (Opc == ISD::BUILD_VECTOR) {
493 uint32_t LHSVal, RHSVal;
494 if (getConstantValue(N->getOperand(0), LHSVal) &&
495 getConstantValue(N->getOperand(1), RHSVal)) {
496 uint32_t K = LHSVal | (RHSVal << 16);
497 CurDAG->SelectNodeTo(N, AMDGPU::S_MOV_B32, VT,
498 CurDAG->getTargetConstant(K, SDLoc(N), MVT::i32));
499 return;
500 }
501 }
502
503 break;
504 }
505
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000506 assert(VT.getVectorElementType().bitsEq(MVT::i32));
Tom Stellard20287692017-08-08 04:57:55 +0000507 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts);
508 SelectBuildVector(N, RegClassID);
Justin Bogner95927c02016-05-12 21:03:32 +0000509 return;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000510 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000511 case ISD::BUILD_PAIR: {
512 SDValue RC, SubReg0, SubReg1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000513 SDLoc DL(N);
Tom Stellard754f80f2013-04-05 23:31:51 +0000514 if (N->getValueType(0) == MVT::i128) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000515 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32);
516 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32);
517 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000518 } else if (N->getValueType(0) == MVT::i64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000519 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32);
520 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
521 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000522 } else {
523 llvm_unreachable("Unhandled value type for BUILD_PAIR");
524 }
525 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
526 N->getOperand(1), SubReg1 };
Justin Bogner95927c02016-05-12 21:03:32 +0000527 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
528 N->getValueType(0), Ops));
529 return;
Tom Stellard754f80f2013-04-05 23:31:51 +0000530 }
Tom Stellard7ed0b522014-04-03 20:19:27 +0000531
532 case ISD::Constant:
533 case ISD::ConstantFP: {
Tom Stellard20287692017-08-08 04:57:55 +0000534 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
Tom Stellard7ed0b522014-04-03 20:19:27 +0000535 break;
536
537 uint64_t Imm;
538 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
539 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
540 else {
Tom Stellard3cbe0142014-04-07 19:31:13 +0000541 ConstantSDNode *C = cast<ConstantSDNode>(N);
Tom Stellard7ed0b522014-04-03 20:19:27 +0000542 Imm = C->getZExtValue();
543 }
544
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000545 SDLoc DL(N);
546 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
547 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL,
548 MVT::i32));
549 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
550 CurDAG->getConstant(Imm >> 32, DL, MVT::i32));
Tom Stellard7ed0b522014-04-03 20:19:27 +0000551 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000552 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
553 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
554 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
Tom Stellard7ed0b522014-04-03 20:19:27 +0000555 };
556
Justin Bogner95927c02016-05-12 21:03:32 +0000557 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
558 N->getValueType(0), Ops));
559 return;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000560 }
Matt Arsenault4bf43d42015-09-25 17:27:08 +0000561 case ISD::LOAD:
Tom Stellard096b8c12015-02-04 20:49:49 +0000562 case ISD::STORE: {
Tom Stellard381a94a2015-05-12 15:00:49 +0000563 N = glueCopyToM0(N);
Tom Stellard096b8c12015-02-04 20:49:49 +0000564 break;
565 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000566
567 case AMDGPUISD::BFE_I32:
568 case AMDGPUISD::BFE_U32: {
Matt Arsenault78b86702014-04-18 05:19:26 +0000569 // There is a scalar version available, but unlike the vector version which
570 // has a separate operand for the offset and width, the scalar version packs
571 // the width and offset into a single operand. Try to move to the scalar
572 // version if the offsets are constant, so that we can try to keep extended
573 // loads of kernel arguments in SGPRs.
574
575 // TODO: Technically we could try to pattern match scalar bitshifts of
576 // dynamic values, but it's probably not useful.
577 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
578 if (!Offset)
579 break;
580
581 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
582 if (!Width)
583 break;
584
585 bool Signed = Opc == AMDGPUISD::BFE_I32;
586
Matt Arsenault78b86702014-04-18 05:19:26 +0000587 uint32_t OffsetVal = Offset->getZExtValue();
588 uint32_t WidthVal = Width->getZExtValue();
589
Justin Bogner95927c02016-05-12 21:03:32 +0000590 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
591 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal));
592 return;
Matt Arsenault78b86702014-04-18 05:19:26 +0000593 }
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000594 case AMDGPUISD::DIV_SCALE: {
Justin Bogner95927c02016-05-12 21:03:32 +0000595 SelectDIV_SCALE(N);
596 return;
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000597 }
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000598 case AMDGPUISD::MAD_I64_I32:
599 case AMDGPUISD::MAD_U64_U32: {
600 SelectMAD_64_32(N);
601 return;
602 }
Tom Stellard3457a842014-10-09 19:06:00 +0000603 case ISD::CopyToReg: {
604 const SITargetLowering& Lowering =
605 *static_cast<const SITargetLowering*>(getTargetLowering());
Matt Arsenault0d0d6c22017-04-12 21:58:23 +0000606 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG);
Tom Stellard3457a842014-10-09 19:06:00 +0000607 break;
608 }
Marek Olsak9b728682015-03-24 13:40:27 +0000609 case ISD::AND:
610 case ISD::SRL:
611 case ISD::SRA:
Matt Arsenault7e8de012016-04-22 22:59:16 +0000612 case ISD::SIGN_EXTEND_INREG:
Tom Stellard20287692017-08-08 04:57:55 +0000613 if (N->getValueType(0) != MVT::i32)
Marek Olsak9b728682015-03-24 13:40:27 +0000614 break;
615
Justin Bogner95927c02016-05-12 21:03:32 +0000616 SelectS_BFE(N);
617 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000618 case ISD::BRCOND:
Justin Bogner95927c02016-05-12 21:03:32 +0000619 SelectBRCOND(N);
620 return;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000621 case ISD::FMAD:
622 SelectFMAD(N);
623 return;
Matt Arsenault88701812016-06-09 23:42:48 +0000624 case AMDGPUISD::ATOMIC_CMP_SWAP:
625 SelectATOMIC_CMP_SWAP(N);
626 return;
Tom Stellard75aadc22012-12-11 21:25:42 +0000627 }
Tom Stellard3457a842014-10-09 19:06:00 +0000628
Justin Bogner95927c02016-05-12 21:03:32 +0000629 SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000630}
631
Jan Vesely43b7b5b2016-04-07 19:23:11 +0000632bool AMDGPUDAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const {
633 if (!N->readMem())
634 return false;
Matt Arsenault209a7b92014-04-18 07:40:20 +0000635 if (CbId == -1)
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000636 return N->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS;
Matt Arsenault209a7b92014-04-18 07:40:20 +0000637
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000638 return N->getAddressSpace() == AMDGPUASI.CONSTANT_BUFFER_0 + CbId;
Matt Arsenault3f981402014-09-15 15:41:53 +0000639}
640
Tom Stellardbc4497b2016-02-12 23:45:29 +0000641bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const {
642 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000643 const Instruction *Term = BB->getTerminator();
644 return Term->getMetadata("amdgpu.uniform") ||
645 Term->getMetadata("structurizecfg.uniform");
Tom Stellardbc4497b2016-02-12 23:45:29 +0000646}
647
Mehdi Amini117296c2016-10-01 02:56:57 +0000648StringRef AMDGPUDAGToDAGISel::getPassName() const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000649 return "AMDGPU DAG->DAG Pattern Instruction Selection";
650}
651
Tom Stellard41fc7852013-07-23 01:48:42 +0000652//===----------------------------------------------------------------------===//
653// Complex Patterns
654//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000655
Tom Stellard365366f2013-01-23 02:09:06 +0000656bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
Matt Arsenault209a7b92014-04-18 07:40:20 +0000657 SDValue& IntPtr) {
Tom Stellard365366f2013-01-23 02:09:06 +0000658 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000659 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr),
660 true);
Tom Stellard365366f2013-01-23 02:09:06 +0000661 return true;
662 }
663 return false;
664}
665
666bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
667 SDValue& BaseReg, SDValue &Offset) {
Matt Arsenault209a7b92014-04-18 07:40:20 +0000668 if (!isa<ConstantSDNode>(Addr)) {
Tom Stellard365366f2013-01-23 02:09:06 +0000669 BaseReg = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000670 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true);
Tom Stellard365366f2013-01-23 02:09:06 +0000671 return true;
672 }
673 return false;
674}
675
Tom Stellard75aadc22012-12-11 21:25:42 +0000676bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
Tom Stellard20287692017-08-08 04:57:55 +0000677 SDValue &Offset) {
678 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000679}
680
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000681bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
682 SDValue &Offset) {
683 ConstantSDNode *C;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000684 SDLoc DL(Addr);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000685
686 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
687 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000688 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000689 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
690 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
691 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
692 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000693 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
694 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
695 Base = Addr.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000696 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000697 } else {
698 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000699 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000700 }
701
702 return true;
703}
Christian Konigd910b7d2013-02-26 17:52:16 +0000704
Matt Arsenault84445dd2017-11-30 22:51:26 +0000705// FIXME: Should only handle addcarry/subcarry
Justin Bogner95927c02016-05-12 21:03:32 +0000706void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000707 SDLoc DL(N);
708 SDValue LHS = N->getOperand(0);
709 SDValue RHS = N->getOperand(1);
710
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000711 unsigned Opcode = N->getOpcode();
712 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE);
713 bool ProduceCarry =
714 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC;
Matt Arsenault84445dd2017-11-30 22:51:26 +0000715 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000716
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000717 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
718 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000719
720 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
721 DL, MVT::i32, LHS, Sub0);
722 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
723 DL, MVT::i32, LHS, Sub1);
724
725 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
726 DL, MVT::i32, RHS, Sub0);
727 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
728 DL, MVT::i32, RHS, Sub1);
729
730 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000731
Tom Stellard80942a12014-09-05 14:07:59 +0000732 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000733 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
734
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000735 SDNode *AddLo;
736 if (!ConsumeCarry) {
737 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
738 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args);
739 } else {
740 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) };
741 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args);
742 }
743 SDValue AddHiArgs[] = {
744 SDValue(Hi0, 0),
745 SDValue(Hi1, 0),
746 SDValue(AddLo, 1)
747 };
748 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000749
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000750 SDValue RegSequenceArgs[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000751 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000752 SDValue(AddLo,0),
753 Sub0,
754 SDValue(AddHi,0),
755 Sub1,
756 };
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000757 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
758 MVT::i64, RegSequenceArgs);
759
760 if (ProduceCarry) {
761 // Replace the carry-use
762 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(AddHi, 1));
763 }
764
765 // Replace the remaining uses.
766 CurDAG->ReplaceAllUsesWith(N, RegSequence);
767 CurDAG->RemoveDeadNode(N);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000768}
769
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000770void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) {
771 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned
772 // carry out despite the _i32 name. These were renamed in VI to _U32.
773 // FIXME: We should probably rename the opcodes here.
774 unsigned Opc = N->getOpcode() == ISD::UADDO ?
775 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64;
776
777 CurDAG->SelectNodeTo(N, Opc, N->getVTList(),
778 { N->getOperand(0), N->getOperand(1) });
779}
780
Tom Stellard8485fa02016-12-07 02:42:15 +0000781void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) {
782 SDLoc SL(N);
783 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod
784 SDValue Ops[10];
785
786 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]);
787 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
788 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]);
789 Ops[8] = N->getOperand(0);
790 Ops[9] = N->getOperand(4);
791
792 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops);
793}
794
795void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) {
796 SDLoc SL(N);
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +0000797 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod
Tom Stellard8485fa02016-12-07 02:42:15 +0000798 SDValue Ops[8];
799
800 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]);
801 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
802 Ops[6] = N->getOperand(0);
803 Ops[7] = N->getOperand(3);
804
805 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops);
806}
807
Matt Arsenault044f1d12015-02-14 04:24:28 +0000808// We need to handle this here because tablegen doesn't support matching
809// instructions with multiple outputs.
Justin Bogner95927c02016-05-12 21:03:32 +0000810void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000811 SDLoc SL(N);
812 EVT VT = N->getValueType(0);
813
814 assert(VT == MVT::f32 || VT == MVT::f64);
815
816 unsigned Opc
817 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
818
Matt Arsenault3b99f122017-01-19 06:04:12 +0000819 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
820 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000821}
822
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000823// We need to handle this here because tablegen doesn't support matching
824// instructions with multiple outputs.
825void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) {
826 SDLoc SL(N);
827 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32;
828 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32;
829
830 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1);
831 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
832 Clamp };
833 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
834}
835
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000836bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset,
837 unsigned OffsetBits) const {
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000838 if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
839 (OffsetBits == 8 && !isUInt<8>(Offset)))
840 return false;
841
Matt Arsenault706f9302015-07-06 16:01:58 +0000842 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS ||
843 Subtarget->unsafeDSOffsetFoldingEnabled())
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000844 return true;
845
846 // On Southern Islands instruction with a negative base value and an offset
847 // don't seem to work.
848 return CurDAG->SignBitIsZero(Base);
849}
850
851bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
852 SDValue &Offset) const {
Tom Stellard92b24f32016-04-29 14:34:26 +0000853 SDLoc DL(Addr);
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000854 if (CurDAG->isBaseWithConstantOffset(Addr)) {
855 SDValue N0 = Addr.getOperand(0);
856 SDValue N1 = Addr.getOperand(1);
857 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
858 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
859 // (add n0, c0)
860 Base = N0;
Tom Stellard92b24f32016-04-29 14:34:26 +0000861 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000862 return true;
863 }
Matt Arsenault966a94f2015-09-08 19:34:22 +0000864 } else if (Addr.getOpcode() == ISD::SUB) {
865 // sub C, x -> add (sub 0, x), C
866 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
867 int64_t ByteOffset = C->getSExtValue();
868 if (isUInt<16>(ByteOffset)) {
Matt Arsenault966a94f2015-09-08 19:34:22 +0000869 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000870
Matt Arsenault966a94f2015-09-08 19:34:22 +0000871 // XXX - This is kind of hacky. Create a dummy sub node so we can check
872 // the known bits in isDSOffsetLegal. We need to emit the selected node
873 // here, so this is thrown away.
874 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
875 Zero, Addr.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000876
Matt Arsenault966a94f2015-09-08 19:34:22 +0000877 if (isDSOffsetLegal(Sub, ByteOffset, 16)) {
Matt Arsenault84445dd2017-11-30 22:51:26 +0000878 // FIXME: Select to VOP3 version for with-carry.
879 unsigned SubOp = Subtarget->hasAddNoCarry() ?
880 AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32;
881
Matt Arsenault966a94f2015-09-08 19:34:22 +0000882 MachineSDNode *MachineSub
Matt Arsenault84445dd2017-11-30 22:51:26 +0000883 = CurDAG->getMachineNode(SubOp, DL, MVT::i32,
Matt Arsenault966a94f2015-09-08 19:34:22 +0000884 Zero, Addr.getOperand(1));
885
886 Base = SDValue(MachineSub, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +0000887 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16);
Matt Arsenault966a94f2015-09-08 19:34:22 +0000888 return true;
889 }
890 }
891 }
892 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
893 // If we have a constant address, prefer to put the constant into the
894 // offset. This can save moves to load the constant address since multiple
895 // operations can share the zero base address register, and enables merging
896 // into read2 / write2 instructions.
897
898 SDLoc DL(Addr);
899
Matt Arsenaulte775f5f2014-10-14 17:21:19 +0000900 if (isUInt<16>(CAddr->getZExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000901 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardc8d79202014-10-15 21:08:59 +0000902 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000903 DL, MVT::i32, Zero);
Tom Stellardc8d79202014-10-15 21:08:59 +0000904 Base = SDValue(MovZero, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +0000905 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
Matt Arsenaulte775f5f2014-10-14 17:21:19 +0000906 return true;
907 }
908 }
909
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000910 // default case
911 Base = Addr;
Matt Arsenault966a94f2015-09-08 19:34:22 +0000912 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000913 return true;
914}
915
Matt Arsenault966a94f2015-09-08 19:34:22 +0000916// TODO: If offset is too big, put low 16-bit into offset.
Tom Stellardf3fc5552014-08-22 18:49:35 +0000917bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base,
918 SDValue &Offset0,
919 SDValue &Offset1) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000920 SDLoc DL(Addr);
921
Tom Stellardf3fc5552014-08-22 18:49:35 +0000922 if (CurDAG->isBaseWithConstantOffset(Addr)) {
923 SDValue N0 = Addr.getOperand(0);
924 SDValue N1 = Addr.getOperand(1);
925 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
926 unsigned DWordOffset0 = C1->getZExtValue() / 4;
927 unsigned DWordOffset1 = DWordOffset0 + 1;
928 // (add n0, c0)
929 if (isDSOffsetLegal(N0, DWordOffset1, 8)) {
930 Base = N0;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000931 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
932 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +0000933 return true;
934 }
Matt Arsenault966a94f2015-09-08 19:34:22 +0000935 } else if (Addr.getOpcode() == ISD::SUB) {
936 // sub C, x -> add (sub 0, x), C
937 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
938 unsigned DWordOffset0 = C->getZExtValue() / 4;
939 unsigned DWordOffset1 = DWordOffset0 + 1;
Tom Stellardf3fc5552014-08-22 18:49:35 +0000940
Matt Arsenault966a94f2015-09-08 19:34:22 +0000941 if (isUInt<8>(DWordOffset0)) {
942 SDLoc DL(Addr);
943 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
944
945 // XXX - This is kind of hacky. Create a dummy sub node so we can check
946 // the known bits in isDSOffsetLegal. We need to emit the selected node
947 // here, so this is thrown away.
948 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
949 Zero, Addr.getOperand(1));
950
951 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) {
Matt Arsenault84445dd2017-11-30 22:51:26 +0000952 unsigned SubOp = Subtarget->hasAddNoCarry() ?
953 AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32;
954
Matt Arsenault966a94f2015-09-08 19:34:22 +0000955 MachineSDNode *MachineSub
Matt Arsenault84445dd2017-11-30 22:51:26 +0000956 = CurDAG->getMachineNode(SubOp, DL, MVT::i32,
Matt Arsenault966a94f2015-09-08 19:34:22 +0000957 Zero, Addr.getOperand(1));
958
959 Base = SDValue(MachineSub, 0);
960 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
961 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
962 return true;
963 }
964 }
965 }
966 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
Matt Arsenault1a74aff2014-10-15 18:06:43 +0000967 unsigned DWordOffset0 = CAddr->getZExtValue() / 4;
968 unsigned DWordOffset1 = DWordOffset0 + 1;
969 assert(4 * DWordOffset0 == CAddr->getZExtValue());
970
971 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000972 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Matt Arsenault1a74aff2014-10-15 18:06:43 +0000973 MachineSDNode *MovZero
974 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000975 DL, MVT::i32, Zero);
Matt Arsenault1a74aff2014-10-15 18:06:43 +0000976 Base = SDValue(MovZero, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000977 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
978 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Matt Arsenault1a74aff2014-10-15 18:06:43 +0000979 return true;
980 }
981 }
982
Tom Stellardf3fc5552014-08-22 18:49:35 +0000983 // default case
Matt Arsenault0efdd062016-09-09 22:29:28 +0000984
985 // FIXME: This is broken on SI where we still need to check if the base
986 // pointer is positive here.
Tom Stellardf3fc5552014-08-22 18:49:35 +0000987 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000988 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8);
989 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +0000990 return true;
991}
992
Changpeng Fangb41574a2015-12-22 20:55:23 +0000993bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
Tom Stellard155bbb72014-08-11 22:18:17 +0000994 SDValue &VAddr, SDValue &SOffset,
995 SDValue &Offset, SDValue &Offen,
996 SDValue &Idxen, SDValue &Addr64,
997 SDValue &GLC, SDValue &SLC,
998 SDValue &TFE) const {
Changpeng Fangb41574a2015-12-22 20:55:23 +0000999 // Subtarget prefers to use flat instruction
1000 if (Subtarget->useFlatForGlobal())
1001 return false;
1002
Tom Stellardb02c2682014-06-24 23:33:07 +00001003 SDLoc DL(Addr);
1004
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001005 if (!GLC.getNode())
1006 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1007 if (!SLC.getNode())
1008 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001009 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellard155bbb72014-08-11 22:18:17 +00001010
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001011 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1012 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1013 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1);
1014 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001015
Tom Stellardb02c2682014-06-24 23:33:07 +00001016 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1017 SDValue N0 = Addr.getOperand(0);
1018 SDValue N1 = Addr.getOperand(1);
1019 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1020
Tom Stellard94b72312015-02-11 00:34:35 +00001021 if (N0.getOpcode() == ISD::ADD) {
1022 // (add (add N2, N3), C1) -> addr64
1023 SDValue N2 = N0.getOperand(0);
1024 SDValue N3 = N0.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001025 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
Tom Stellard94b72312015-02-11 00:34:35 +00001026 Ptr = N2;
1027 VAddr = N3;
1028 } else {
Tom Stellard155bbb72014-08-11 22:18:17 +00001029 // (add N0, C1) -> offset
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001030 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001031 Ptr = N0;
Tom Stellard94b72312015-02-11 00:34:35 +00001032 }
1033
Marek Olsakffadcb72017-11-09 01:52:17 +00001034 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) {
Matt Arsenault88701812016-06-09 23:42:48 +00001035 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1036 return true;
1037 }
1038
1039 if (isUInt<32>(C1->getZExtValue())) {
Tom Stellard94b72312015-02-11 00:34:35 +00001040 // Illegal offset, store it in soffset.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001041 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Tom Stellard94b72312015-02-11 00:34:35 +00001042 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001043 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)),
1044 0);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001045 return true;
Tom Stellardb02c2682014-06-24 23:33:07 +00001046 }
1047 }
Tom Stellard94b72312015-02-11 00:34:35 +00001048
Tom Stellardb02c2682014-06-24 23:33:07 +00001049 if (Addr.getOpcode() == ISD::ADD) {
Tom Stellard155bbb72014-08-11 22:18:17 +00001050 // (add N0, N1) -> addr64
Tom Stellardb02c2682014-06-24 23:33:07 +00001051 SDValue N0 = Addr.getOperand(0);
1052 SDValue N1 = Addr.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001053 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
Tom Stellard155bbb72014-08-11 22:18:17 +00001054 Ptr = N0;
1055 VAddr = N1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001056 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001057 return true;
Tom Stellardb02c2682014-06-24 23:33:07 +00001058 }
1059
Tom Stellard155bbb72014-08-11 22:18:17 +00001060 // default case -> offset
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001061 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001062 Ptr = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001063 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001064
1065 return true;
Tom Stellard155bbb72014-08-11 22:18:17 +00001066}
1067
1068bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001069 SDValue &VAddr, SDValue &SOffset,
Tom Stellard1f9939f2015-02-27 14:59:41 +00001070 SDValue &Offset, SDValue &GLC,
1071 SDValue &SLC, SDValue &TFE) const {
1072 SDValue Ptr, Offen, Idxen, Addr64;
Tom Stellard155bbb72014-08-11 22:18:17 +00001073
Tom Stellard70580f82015-07-20 14:28:41 +00001074 // addr64 bit was removed for volcanic islands.
1075 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1076 return false;
1077
Changpeng Fangb41574a2015-12-22 20:55:23 +00001078 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
1079 GLC, SLC, TFE))
1080 return false;
Tom Stellard155bbb72014-08-11 22:18:17 +00001081
1082 ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
1083 if (C->getSExtValue()) {
1084 SDLoc DL(Addr);
Matt Arsenault485defe2014-11-05 19:01:17 +00001085
1086 const SITargetLowering& Lowering =
1087 *static_cast<const SITargetLowering*>(getTargetLowering());
1088
1089 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001090 return true;
1091 }
Matt Arsenault485defe2014-11-05 19:01:17 +00001092
Tom Stellard155bbb72014-08-11 22:18:17 +00001093 return false;
1094}
1095
Tom Stellard7980fc82014-09-25 18:30:26 +00001096bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001097 SDValue &VAddr, SDValue &SOffset,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001098 SDValue &Offset,
1099 SDValue &SLC) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001100 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1);
Tom Stellard1f9939f2015-02-27 14:59:41 +00001101 SDValue GLC, TFE;
Tom Stellard7980fc82014-09-25 18:30:26 +00001102
Tom Stellard1f9939f2015-02-27 14:59:41 +00001103 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE);
Tom Stellard7980fc82014-09-25 18:30:26 +00001104}
1105
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001106static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) {
1107 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>();
1108 return PSV && PSV->isStack();
Matt Arsenaultac0fc842016-09-17 16:09:55 +00001109}
1110
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001111std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const {
1112 const MachineFunction &MF = CurDAG->getMachineFunction();
1113 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1114
1115 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) {
1116 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(),
1117 FI->getValueType(0));
1118
1119 // If we can resolve this to a frame index access, this is relative to the
1120 // frame pointer SGPR.
1121 return std::make_pair(TFI, CurDAG->getRegister(Info->getFrameOffsetReg(),
1122 MVT::i32));
1123 }
1124
1125 // If we don't know this private access is a local stack object, it needs to
1126 // be relative to the entry point's scratch wave offset register.
1127 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(),
1128 MVT::i32));
1129}
1130
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001131bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001132 SDValue Addr, SDValue &Rsrc,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001133 SDValue &VAddr, SDValue &SOffset,
1134 SDValue &ImmOffset) const {
Tom Stellardb02094e2014-07-21 15:45:01 +00001135
1136 SDLoc DL(Addr);
1137 MachineFunction &MF = CurDAG->getMachineFunction();
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001138 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardb02094e2014-07-21 15:45:01 +00001139
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001140 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Tom Stellardb02094e2014-07-21 15:45:01 +00001141
Matt Arsenault0774ea22017-04-24 19:40:59 +00001142 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1143 unsigned Imm = CAddr->getZExtValue();
Matt Arsenault0774ea22017-04-24 19:40:59 +00001144
1145 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32);
1146 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
1147 DL, MVT::i32, HighBits);
1148 VAddr = SDValue(MovHighBits, 0);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001149
1150 // In a call sequence, stores to the argument stack area are relative to the
1151 // stack pointer.
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001152 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001153 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1154 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1155
1156 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
Matt Arsenault0774ea22017-04-24 19:40:59 +00001157 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16);
1158 return true;
1159 }
1160
Tom Stellardb02094e2014-07-21 15:45:01 +00001161 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Matt Arsenault0774ea22017-04-24 19:40:59 +00001162 // (add n0, c1)
1163
Tom Stellard78655fc2015-07-16 19:40:09 +00001164 SDValue N0 = Addr.getOperand(0);
Tom Stellardb02094e2014-07-21 15:45:01 +00001165 SDValue N1 = Addr.getOperand(1);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001166
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001167 // Offsets in vaddr must be positive if range checking is enabled.
Matt Arsenault45b98182017-11-15 00:45:43 +00001168 //
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001169 // The total computation of vaddr + soffset + offset must not overflow. If
1170 // vaddr is negative, even if offset is 0 the sgpr offset add will end up
Matt Arsenault45b98182017-11-15 00:45:43 +00001171 // overflowing.
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001172 //
1173 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would
1174 // always perform a range check. If a negative vaddr base index was used,
1175 // this would fail the range check. The overall address computation would
1176 // compute a valid address, but this doesn't happen due to the range
1177 // check. For out-of-bounds MUBUF loads, a 0 is returned.
1178 //
1179 // Therefore it should be safe to fold any VGPR offset on gfx9 into the
1180 // MUBUF vaddr, but not on older subtargets which can only do this if the
1181 // sign bit is known 0.
Matt Arsenaultcd099612016-02-24 04:55:29 +00001182 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
Matt Arsenault45b98182017-11-15 00:45:43 +00001183 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) &&
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001184 (!Subtarget->privateMemoryResourceIsRangeChecked() ||
1185 CurDAG->SignBitIsZero(N0))) {
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001186 std::tie(VAddr, SOffset) = foldFrameIndex(N0);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001187 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1188 return true;
Tom Stellardb02094e2014-07-21 15:45:01 +00001189 }
1190 }
1191
Tom Stellardb02094e2014-07-21 15:45:01 +00001192 // (node)
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001193 std::tie(VAddr, SOffset) = foldFrameIndex(Addr);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001194 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Tom Stellardb02094e2014-07-21 15:45:01 +00001195 return true;
1196}
1197
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001198bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001199 SDValue Addr,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001200 SDValue &SRsrc,
1201 SDValue &SOffset,
1202 SDValue &Offset) const {
1203 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr);
Marek Olsakffadcb72017-11-09 01:52:17 +00001204 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue()))
Matt Arsenault0774ea22017-04-24 19:40:59 +00001205 return false;
1206
1207 SDLoc DL(Addr);
1208 MachineFunction &MF = CurDAG->getMachineFunction();
1209 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1210
1211 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001212
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001213 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001214 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1215 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1216
1217 // FIXME: Get from MachinePointerInfo? We should only be using the frame
1218 // offset if we know this is in a call sequence.
1219 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
1220
Matt Arsenault0774ea22017-04-24 19:40:59 +00001221 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1222 return true;
1223}
1224
Tom Stellard155bbb72014-08-11 22:18:17 +00001225bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1226 SDValue &SOffset, SDValue &Offset,
1227 SDValue &GLC, SDValue &SLC,
1228 SDValue &TFE) const {
1229 SDValue Ptr, VAddr, Offen, Idxen, Addr64;
Tom Stellard794c8c02014-12-02 17:05:41 +00001230 const SIInstrInfo *TII =
Eric Christopher7792e322015-01-30 23:24:40 +00001231 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellardb02094e2014-07-21 15:45:01 +00001232
Changpeng Fangb41574a2015-12-22 20:55:23 +00001233 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
1234 GLC, SLC, TFE))
1235 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001236
Tom Stellard155bbb72014-08-11 22:18:17 +00001237 if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
1238 !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
1239 !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
Tom Stellard794c8c02014-12-02 17:05:41 +00001240 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() |
Tom Stellard155bbb72014-08-11 22:18:17 +00001241 APInt::getAllOnesValue(32).getZExtValue(); // Size
1242 SDLoc DL(Addr);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00001243
1244 const SITargetLowering& Lowering =
1245 *static_cast<const SITargetLowering*>(getTargetLowering());
1246
1247 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001248 return true;
1249 }
1250 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001251}
1252
Tom Stellard7980fc82014-09-25 18:30:26 +00001253bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001254 SDValue &Soffset, SDValue &Offset
1255 ) const {
1256 SDValue GLC, SLC, TFE;
1257
1258 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE);
1259}
1260bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Tom Stellard7980fc82014-09-25 18:30:26 +00001261 SDValue &Soffset, SDValue &Offset,
Matt Arsenault88701812016-06-09 23:42:48 +00001262 SDValue &SLC) const {
1263 SDValue GLC, TFE;
Tom Stellard7980fc82014-09-25 18:30:26 +00001264
1265 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE);
1266}
1267
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001268bool AMDGPUDAGToDAGISel::SelectMUBUFConstant(SDValue Constant,
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001269 SDValue &SOffset,
1270 SDValue &ImmOffset) const {
1271 SDLoc DL(Constant);
Nicolai Haehnle312b64f2017-10-10 12:22:23 +00001272 const uint32_t Align = 4;
1273 const uint32_t MaxImm = alignDown(4095, Align);
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001274 uint32_t Imm = cast<ConstantSDNode>(Constant)->getZExtValue();
1275 uint32_t Overflow = 0;
1276
Nicolai Haehnle312b64f2017-10-10 12:22:23 +00001277 if (Imm > MaxImm) {
1278 if (Imm <= MaxImm + 64) {
1279 // Use an SOffset inline constant for 4..64
1280 Overflow = Imm - MaxImm;
1281 Imm = MaxImm;
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001282 } else {
1283 // Try to keep the same value in SOffset for adjacent loads, so that
1284 // the corresponding register contents can be re-used.
1285 //
Nicolai Haehnle312b64f2017-10-10 12:22:23 +00001286 // Load values with all low-bits (except for alignment bits) set into
1287 // SOffset, so that a larger range of values can be covered using
1288 // s_movk_i32.
1289 //
1290 // Atomic operations fail to work correctly when individual address
1291 // components are unaligned, even if their sum is aligned.
1292 uint32_t High = (Imm + Align) & ~4095;
1293 uint32_t Low = (Imm + Align) & 4095;
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001294 Imm = Low;
Nicolai Haehnle312b64f2017-10-10 12:22:23 +00001295 Overflow = High - Align;
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001296 }
1297 }
1298
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001299 // There is a hardware bug in SI and CI which prevents address clamping in
1300 // MUBUF instructions from working correctly with SOffsets. The immediate
1301 // offset is unaffected.
1302 if (Overflow > 0 &&
1303 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
1304 return false;
1305
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001306 ImmOffset = CurDAG->getTargetConstant(Imm, DL, MVT::i16);
1307
1308 if (Overflow <= 64)
1309 SOffset = CurDAG->getTargetConstant(Overflow, DL, MVT::i32);
1310 else
1311 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
1312 CurDAG->getTargetConstant(Overflow, DL, MVT::i32)),
1313 0);
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001314
1315 return true;
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001316}
1317
1318bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicOffset(SDValue Offset,
1319 SDValue &SOffset,
1320 SDValue &ImmOffset) const {
1321 SDLoc DL(Offset);
1322
1323 if (!isa<ConstantSDNode>(Offset))
1324 return false;
1325
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001326 return SelectMUBUFConstant(Offset, SOffset, ImmOffset);
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001327}
1328
1329bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicVOffset(SDValue Offset,
1330 SDValue &SOffset,
1331 SDValue &ImmOffset,
1332 SDValue &VOffset) const {
1333 SDLoc DL(Offset);
1334
1335 // Don't generate an unnecessary voffset for constant offsets.
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001336 if (isa<ConstantSDNode>(Offset)) {
1337 SDValue Tmp1, Tmp2;
1338
1339 // When necessary, use a voffset in <= CI anyway to work around a hardware
1340 // bug.
1341 if (Subtarget->getGeneration() > AMDGPUSubtarget::SEA_ISLANDS ||
1342 SelectMUBUFConstant(Offset, Tmp1, Tmp2))
1343 return false;
1344 }
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001345
1346 if (CurDAG->isBaseWithConstantOffset(Offset)) {
1347 SDValue N0 = Offset.getOperand(0);
1348 SDValue N1 = Offset.getOperand(1);
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001349 if (cast<ConstantSDNode>(N1)->getSExtValue() >= 0 &&
1350 SelectMUBUFConstant(N1, SOffset, ImmOffset)) {
1351 VOffset = N0;
1352 return true;
1353 }
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001354 }
1355
Nicolai Haehnlea6092592016-06-15 07:13:05 +00001356 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1357 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1358 VOffset = Offset;
1359
Nicolai Haehnle3003ba02016-03-18 16:24:20 +00001360 return true;
1361}
1362
Matt Arsenault4e309b02017-07-29 01:03:53 +00001363template <bool IsSigned>
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001364bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDValue Addr,
1365 SDValue &VAddr,
1366 SDValue &Offset,
1367 SDValue &SLC) const {
1368 int64_t OffsetVal = 0;
1369
1370 if (Subtarget->hasFlatInstOffsets() &&
1371 CurDAG->isBaseWithConstantOffset(Addr)) {
1372 SDValue N0 = Addr.getOperand(0);
1373 SDValue N1 = Addr.getOperand(1);
Matt Arsenault4e309b02017-07-29 01:03:53 +00001374 int64_t COffsetVal = cast<ConstantSDNode>(N1)->getSExtValue();
1375
1376 if ((IsSigned && isInt<13>(COffsetVal)) ||
1377 (!IsSigned && isUInt<12>(COffsetVal))) {
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001378 Addr = N0;
1379 OffsetVal = COffsetVal;
1380 }
1381 }
1382
Matt Arsenault7757c592016-06-09 23:42:54 +00001383 VAddr = Addr;
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001384 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16);
Matt Arsenault47ccafe2017-05-11 17:38:33 +00001385 SLC = CurDAG->getTargetConstant(0, SDLoc(), MVT::i1);
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001386
Matt Arsenault7757c592016-06-09 23:42:54 +00001387 return true;
1388}
1389
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001390bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDValue Addr,
1391 SDValue &VAddr,
1392 SDValue &Offset,
1393 SDValue &SLC) const {
Matt Arsenault4e309b02017-07-29 01:03:53 +00001394 return SelectFlatOffset<false>(Addr, VAddr, Offset, SLC);
1395}
1396
1397bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDValue Addr,
1398 SDValue &VAddr,
1399 SDValue &Offset,
1400 SDValue &SLC) const {
1401 return SelectFlatOffset<true>(Addr, VAddr, Offset, SLC);
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001402}
1403
Tom Stellarddee26a22015-08-06 19:28:30 +00001404bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode,
1405 SDValue &Offset, bool &Imm) const {
1406
1407 // FIXME: Handle non-constant offsets.
1408 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode);
1409 if (!C)
1410 return false;
1411
1412 SDLoc SL(ByteOffsetNode);
Marek Olsak8973a0a2017-05-24 14:53:50 +00001413 AMDGPUSubtarget::Generation Gen = Subtarget->getGeneration();
Tom Stellarddee26a22015-08-06 19:28:30 +00001414 int64_t ByteOffset = C->getSExtValue();
Tom Stellard08efb7e2017-01-27 18:41:14 +00001415 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001416
Tom Stellard08efb7e2017-01-27 18:41:14 +00001417 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001418 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1419 Imm = true;
1420 return true;
1421 }
1422
Tom Stellard217361c2015-08-06 19:28:38 +00001423 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset))
1424 return false;
1425
Marek Olsak8973a0a2017-05-24 14:53:50 +00001426 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) {
1427 // 32-bit Immediates are supported on Sea Islands.
Tom Stellard217361c2015-08-06 19:28:38 +00001428 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1429 } else {
Tom Stellarddee26a22015-08-06 19:28:30 +00001430 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32);
1431 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32,
1432 C32Bit), 0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001433 }
Tom Stellard217361c2015-08-06 19:28:38 +00001434 Imm = false;
1435 return true;
Tom Stellarddee26a22015-08-06 19:28:30 +00001436}
1437
1438bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase,
1439 SDValue &Offset, bool &Imm) const {
Tom Stellarddee26a22015-08-06 19:28:30 +00001440 SDLoc SL(Addr);
1441 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1442 SDValue N0 = Addr.getOperand(0);
1443 SDValue N1 = Addr.getOperand(1);
1444
1445 if (SelectSMRDOffset(N1, Offset, Imm)) {
1446 SBase = N0;
1447 return true;
1448 }
1449 }
1450 SBase = Addr;
1451 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32);
1452 Imm = true;
1453 return true;
1454}
1455
1456bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase,
1457 SDValue &Offset) const {
1458 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001459 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm;
1460}
Tom Stellarddee26a22015-08-06 19:28:30 +00001461
Marek Olsak8973a0a2017-05-24 14:53:50 +00001462bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase,
1463 SDValue &Offset) const {
1464
1465 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1466 return false;
1467
1468 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001469 if (!SelectSMRD(Addr, SBase, Offset, Imm))
1470 return false;
1471
Marek Olsak8973a0a2017-05-24 14:53:50 +00001472 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001473}
1474
Tom Stellarddee26a22015-08-06 19:28:30 +00001475bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase,
1476 SDValue &Offset) const {
1477 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001478 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm &&
1479 !isa<ConstantSDNode>(Offset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001480}
1481
1482bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr,
1483 SDValue &Offset) const {
1484 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001485 return SelectSMRDOffset(Addr, Offset, Imm) && Imm;
1486}
Tom Stellarddee26a22015-08-06 19:28:30 +00001487
Marek Olsak8973a0a2017-05-24 14:53:50 +00001488bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr,
1489 SDValue &Offset) const {
1490 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1491 return false;
1492
1493 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001494 if (!SelectSMRDOffset(Addr, Offset, Imm))
1495 return false;
1496
Marek Olsak8973a0a2017-05-24 14:53:50 +00001497 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001498}
1499
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001500bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index,
1501 SDValue &Base,
1502 SDValue &Offset) const {
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001503 SDLoc DL(Index);
1504
1505 if (CurDAG->isBaseWithConstantOffset(Index)) {
1506 SDValue N0 = Index.getOperand(0);
1507 SDValue N1 = Index.getOperand(1);
1508 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1509
1510 // (add n0, c0)
1511 Base = N0;
1512 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32);
1513 return true;
1514 }
1515
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001516 if (isa<ConstantSDNode>(Index))
1517 return false;
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001518
1519 Base = Index;
1520 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1521 return true;
1522}
1523
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001524SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL,
1525 SDValue Val, uint32_t Offset,
1526 uint32_t Width) {
Marek Olsak9b728682015-03-24 13:40:27 +00001527 // Transformation function, pack the offset and width of a BFE into
1528 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
1529 // source, bits [5:0] contain the offset and bits [22:16] the width.
1530 uint32_t PackedVal = Offset | (Width << 16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001531 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32);
Marek Olsak9b728682015-03-24 13:40:27 +00001532
1533 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst);
1534}
1535
Justin Bogner95927c02016-05-12 21:03:32 +00001536void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001537 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c)
1538 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c)
1539 // Predicate: 0 < b <= c < 32
1540
1541 const SDValue &Shl = N->getOperand(0);
1542 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1));
1543 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1544
1545 if (B && C) {
1546 uint32_t BVal = B->getZExtValue();
1547 uint32_t CVal = C->getZExtValue();
1548
1549 if (0 < BVal && BVal <= CVal && CVal < 32) {
1550 bool Signed = N->getOpcode() == ISD::SRA;
1551 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32;
1552
Justin Bogner95927c02016-05-12 21:03:32 +00001553 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal,
1554 32 - CVal));
1555 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001556 }
1557 }
Justin Bogner95927c02016-05-12 21:03:32 +00001558 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001559}
1560
Justin Bogner95927c02016-05-12 21:03:32 +00001561void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001562 switch (N->getOpcode()) {
1563 case ISD::AND:
1564 if (N->getOperand(0).getOpcode() == ISD::SRL) {
1565 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)"
1566 // Predicate: isMask(mask)
1567 const SDValue &Srl = N->getOperand(0);
1568 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
1569 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
1570
1571 if (Shift && Mask) {
1572 uint32_t ShiftVal = Shift->getZExtValue();
1573 uint32_t MaskVal = Mask->getZExtValue();
1574
1575 if (isMask_32(MaskVal)) {
1576 uint32_t WidthVal = countPopulation(MaskVal);
1577
Justin Bogner95927c02016-05-12 21:03:32 +00001578 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1579 Srl.getOperand(0), ShiftVal, WidthVal));
1580 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001581 }
1582 }
1583 }
1584 break;
1585 case ISD::SRL:
1586 if (N->getOperand(0).getOpcode() == ISD::AND) {
1587 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)"
1588 // Predicate: isMask(mask >> b)
1589 const SDValue &And = N->getOperand(0);
1590 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1));
1591 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1));
1592
1593 if (Shift && Mask) {
1594 uint32_t ShiftVal = Shift->getZExtValue();
1595 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal;
1596
1597 if (isMask_32(MaskVal)) {
1598 uint32_t WidthVal = countPopulation(MaskVal);
1599
Justin Bogner95927c02016-05-12 21:03:32 +00001600 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1601 And.getOperand(0), ShiftVal, WidthVal));
1602 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001603 }
1604 }
Justin Bogner95927c02016-05-12 21:03:32 +00001605 } else if (N->getOperand(0).getOpcode() == ISD::SHL) {
1606 SelectS_BFEFromShifts(N);
1607 return;
1608 }
Marek Olsak9b728682015-03-24 13:40:27 +00001609 break;
1610 case ISD::SRA:
Justin Bogner95927c02016-05-12 21:03:32 +00001611 if (N->getOperand(0).getOpcode() == ISD::SHL) {
1612 SelectS_BFEFromShifts(N);
1613 return;
1614 }
Marek Olsak9b728682015-03-24 13:40:27 +00001615 break;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001616
1617 case ISD::SIGN_EXTEND_INREG: {
1618 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8
1619 SDValue Src = N->getOperand(0);
1620 if (Src.getOpcode() != ISD::SRL)
1621 break;
1622
1623 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1));
1624 if (!Amt)
1625 break;
1626
1627 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
Justin Bogner95927c02016-05-12 21:03:32 +00001628 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0),
1629 Amt->getZExtValue(), Width));
1630 return;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001631 }
Marek Olsak9b728682015-03-24 13:40:27 +00001632 }
1633
Justin Bogner95927c02016-05-12 21:03:32 +00001634 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001635}
1636
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001637bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const {
1638 assert(N->getOpcode() == ISD::BRCOND);
1639 if (!N->hasOneUse())
1640 return false;
1641
1642 SDValue Cond = N->getOperand(1);
1643 if (Cond.getOpcode() == ISD::CopyToReg)
1644 Cond = Cond.getOperand(2);
1645
1646 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
1647 return false;
1648
1649 MVT VT = Cond.getOperand(0).getSimpleValueType();
1650 if (VT == MVT::i32)
1651 return true;
1652
1653 if (VT == MVT::i64) {
1654 auto ST = static_cast<const SISubtarget *>(Subtarget);
1655
1656 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1657 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64();
1658 }
1659
1660 return false;
1661}
1662
Justin Bogner95927c02016-05-12 21:03:32 +00001663void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001664 SDValue Cond = N->getOperand(1);
1665
Matt Arsenault327188a2016-12-15 21:57:11 +00001666 if (Cond.isUndef()) {
1667 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other,
1668 N->getOperand(2), N->getOperand(0));
1669 return;
1670 }
1671
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001672 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N);
1673 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ;
1674 unsigned CondReg = UseSCCBr ? AMDGPU::SCC : AMDGPU::VCC;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001675 SDLoc SL(N);
1676
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001677 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond);
1678 CurDAG->SelectNodeTo(N, BrOp, MVT::Other,
Justin Bogner95927c02016-05-12 21:03:32 +00001679 N->getOperand(2), // Basic Block
Matt Arsenaultf530e8b2016-11-07 19:09:33 +00001680 VCC.getValue(0));
Tom Stellardbc4497b2016-02-12 23:45:29 +00001681}
1682
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001683void AMDGPUDAGToDAGISel::SelectFMAD(SDNode *N) {
1684 MVT VT = N->getSimpleValueType(0);
1685 if (VT != MVT::f32 || !Subtarget->hasMadMixInsts()) {
1686 SelectCode(N);
1687 return;
1688 }
1689
1690 SDValue Src0 = N->getOperand(0);
1691 SDValue Src1 = N->getOperand(1);
1692 SDValue Src2 = N->getOperand(2);
1693 unsigned Src0Mods, Src1Mods, Src2Mods;
1694
1695 // Avoid using v_mad_mix_f32 unless there is actually an operand using the
1696 // conversion from f16.
1697 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods);
1698 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods);
1699 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods);
1700
1701 assert(!Subtarget->hasFP32Denormals() &&
1702 "fmad selected with denormals enabled");
1703 // TODO: We can select this with f32 denormals enabled if all the sources are
1704 // converted from f16 (in which case fmad isn't legal).
1705
1706 if (Sel0 || Sel1 || Sel2) {
1707 // For dummy operands.
1708 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
1709 SDValue Ops[] = {
1710 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0,
1711 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1,
1712 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2,
1713 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1),
1714 Zero, Zero
1715 };
1716
1717 CurDAG->SelectNodeTo(N, AMDGPU::V_MAD_MIX_F32, MVT::f32, Ops);
1718 } else {
1719 SelectCode(N);
1720 }
1721}
1722
Matt Arsenault88701812016-06-09 23:42:48 +00001723// This is here because there isn't a way to use the generated sub0_sub1 as the
1724// subreg index to EXTRACT_SUBREG in tablegen.
1725void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) {
1726 MemSDNode *Mem = cast<MemSDNode>(N);
1727 unsigned AS = Mem->getAddressSpace();
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001728 if (AS == AMDGPUASI.FLAT_ADDRESS) {
Matt Arsenault7757c592016-06-09 23:42:54 +00001729 SelectCode(N);
1730 return;
1731 }
Matt Arsenault88701812016-06-09 23:42:48 +00001732
1733 MVT VT = N->getSimpleValueType(0);
1734 bool Is32 = (VT == MVT::i32);
1735 SDLoc SL(N);
1736
1737 MachineSDNode *CmpSwap = nullptr;
1738 if (Subtarget->hasAddr64()) {
Vitaly Buka74503982017-10-15 05:35:02 +00001739 SDValue SRsrc, VAddr, SOffset, Offset, SLC;
Matt Arsenault88701812016-06-09 23:42:48 +00001740
1741 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00001742 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN :
1743 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00001744 SDValue CmpVal = Mem->getOperand(2);
1745
1746 // XXX - Do we care about glue operands?
1747
1748 SDValue Ops[] = {
1749 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain()
1750 };
1751
1752 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
1753 }
1754 }
1755
1756 if (!CmpSwap) {
1757 SDValue SRsrc, SOffset, Offset, SLC;
1758 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00001759 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN :
1760 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00001761
1762 SDValue CmpVal = Mem->getOperand(2);
1763 SDValue Ops[] = {
1764 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain()
1765 };
1766
1767 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
1768 }
1769 }
1770
1771 if (!CmpSwap) {
1772 SelectCode(N);
1773 return;
1774 }
1775
1776 MachineSDNode::mmo_iterator MMOs = MF->allocateMemRefsArray(1);
1777 *MMOs = Mem->getMemOperand();
1778 CmpSwap->setMemRefs(MMOs, MMOs + 1);
1779
1780 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
1781 SDValue Extract
1782 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0));
1783
1784 ReplaceUses(SDValue(N, 0), Extract);
1785 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1));
1786 CurDAG->RemoveDeadNode(N);
1787}
1788
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001789bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src,
1790 unsigned &Mods) const {
1791 Mods = 0;
Tom Stellardb4a313a2014-08-01 00:32:39 +00001792 Src = In;
1793
1794 if (Src.getOpcode() == ISD::FNEG) {
1795 Mods |= SISrcMods::NEG;
1796 Src = Src.getOperand(0);
1797 }
1798
1799 if (Src.getOpcode() == ISD::FABS) {
1800 Mods |= SISrcMods::ABS;
1801 Src = Src.getOperand(0);
1802 }
1803
Tom Stellardb4a313a2014-08-01 00:32:39 +00001804 return true;
1805}
1806
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001807bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
1808 SDValue &SrcMods) const {
1809 unsigned Mods;
1810 if (SelectVOP3ModsImpl(In, Src, Mods)) {
1811 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
1812 return true;
1813 }
1814
1815 return false;
1816}
1817
Matt Arsenaultf84e5d92017-01-31 03:07:46 +00001818bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src,
1819 SDValue &SrcMods) const {
1820 SelectVOP3Mods(In, Src, SrcMods);
1821 return isNoNanSrc(Src);
1822}
1823
Matt Arsenaultdf58e822017-04-25 21:17:38 +00001824bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const {
1825 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG)
1826 return false;
1827
1828 Src = In;
1829 return true;
Tom Stellarddb5a11f2015-07-13 15:47:57 +00001830}
1831
Tom Stellardb4a313a2014-08-01 00:32:39 +00001832bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
1833 SDValue &SrcMods, SDValue &Clamp,
1834 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001835 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00001836 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
1837 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellardb4a313a2014-08-01 00:32:39 +00001838
1839 return SelectVOP3Mods(In, Src, SrcMods);
1840}
1841
Matt Arsenault4831ce52015-01-06 23:00:37 +00001842bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src,
1843 SDValue &SrcMods,
1844 SDValue &Clamp,
1845 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001846 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
Matt Arsenault4831ce52015-01-06 23:00:37 +00001847 return SelectVOP3Mods(In, Src, SrcMods);
1848}
1849
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00001850bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src,
1851 SDValue &Clamp, SDValue &Omod) const {
1852 Src = In;
1853
1854 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00001855 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
1856 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00001857
1858 return true;
1859}
1860
Matt Arsenault98f29462017-05-17 20:30:58 +00001861static SDValue stripBitcast(SDValue Val) {
1862 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
1863}
1864
1865// Figure out if this is really an extract of the high 16-bits of a dword.
1866static bool isExtractHiElt(SDValue In, SDValue &Out) {
1867 In = stripBitcast(In);
1868 if (In.getOpcode() != ISD::TRUNCATE)
1869 return false;
1870
1871 SDValue Srl = In.getOperand(0);
1872 if (Srl.getOpcode() == ISD::SRL) {
1873 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
1874 if (ShiftAmt->getZExtValue() == 16) {
1875 Out = stripBitcast(Srl.getOperand(0));
1876 return true;
1877 }
1878 }
1879 }
1880
1881 return false;
1882}
1883
1884// Look through operations that obscure just looking at the low 16-bits of the
1885// same register.
1886static SDValue stripExtractLoElt(SDValue In) {
1887 if (In.getOpcode() == ISD::TRUNCATE) {
1888 SDValue Src = In.getOperand(0);
1889 if (Src.getValueType().getSizeInBits() == 32)
1890 return stripBitcast(Src);
1891 }
1892
1893 return In;
1894}
1895
Matt Arsenaulteb522e62017-02-27 22:15:25 +00001896bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src,
1897 SDValue &SrcMods) const {
1898 unsigned Mods = 0;
1899 Src = In;
1900
Matt Arsenaulteb522e62017-02-27 22:15:25 +00001901 if (Src.getOpcode() == ISD::FNEG) {
Matt Arsenault786eeea2017-05-17 20:00:00 +00001902 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI);
Matt Arsenaulteb522e62017-02-27 22:15:25 +00001903 Src = Src.getOperand(0);
1904 }
1905
Matt Arsenault786eeea2017-05-17 20:00:00 +00001906 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
1907 unsigned VecMods = Mods;
1908
Matt Arsenault98f29462017-05-17 20:30:58 +00001909 SDValue Lo = stripBitcast(Src.getOperand(0));
1910 SDValue Hi = stripBitcast(Src.getOperand(1));
Matt Arsenault786eeea2017-05-17 20:00:00 +00001911
1912 if (Lo.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00001913 Lo = stripBitcast(Lo.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00001914 Mods ^= SISrcMods::NEG;
1915 }
1916
1917 if (Hi.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00001918 Hi = stripBitcast(Hi.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00001919 Mods ^= SISrcMods::NEG_HI;
1920 }
1921
Matt Arsenault98f29462017-05-17 20:30:58 +00001922 if (isExtractHiElt(Lo, Lo))
1923 Mods |= SISrcMods::OP_SEL_0;
1924
1925 if (isExtractHiElt(Hi, Hi))
1926 Mods |= SISrcMods::OP_SEL_1;
1927
1928 Lo = stripExtractLoElt(Lo);
1929 Hi = stripExtractLoElt(Hi);
1930
Matt Arsenault786eeea2017-05-17 20:00:00 +00001931 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) {
1932 // Really a scalar input. Just select from the low half of the register to
1933 // avoid packing.
1934
1935 Src = Lo;
1936 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
1937 return true;
1938 }
1939
1940 Mods = VecMods;
1941 }
1942
Matt Arsenaulteb522e62017-02-27 22:15:25 +00001943 // Packed instructions do not have abs modifiers.
Matt Arsenaulteb522e62017-02-27 22:15:25 +00001944 Mods |= SISrcMods::OP_SEL_1;
1945
1946 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
1947 return true;
1948}
1949
1950bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src,
1951 SDValue &SrcMods,
1952 SDValue &Clamp) const {
1953 SDLoc SL(In);
1954
1955 // FIXME: Handle clamp and op_sel
1956 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
1957
1958 return SelectVOP3PMods(In, Src, SrcMods);
1959}
1960
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +00001961bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src,
1962 SDValue &SrcMods) const {
1963 Src = In;
1964 // FIXME: Handle op_sel
1965 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
1966 return true;
1967}
1968
1969bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src,
1970 SDValue &SrcMods,
1971 SDValue &Clamp) const {
1972 SDLoc SL(In);
1973
1974 // FIXME: Handle clamp
1975 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
1976
1977 return SelectVOP3OpSel(In, Src, SrcMods);
1978}
1979
1980bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src,
1981 SDValue &SrcMods) const {
1982 // FIXME: Handle op_sel
1983 return SelectVOP3Mods(In, Src, SrcMods);
1984}
1985
1986bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src,
1987 SDValue &SrcMods,
1988 SDValue &Clamp) const {
1989 SDLoc SL(In);
1990
1991 // FIXME: Handle clamp
1992 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
1993
1994 return SelectVOP3OpSelMods(In, Src, SrcMods);
1995}
1996
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001997// The return value is not whether the match is possible (which it always is),
1998// but whether or not it a conversion is really used.
1999bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src,
2000 unsigned &Mods) const {
2001 Mods = 0;
2002 SelectVOP3ModsImpl(In, Src, Mods);
2003
2004 if (Src.getOpcode() == ISD::FP_EXTEND) {
2005 Src = Src.getOperand(0);
2006 assert(Src.getValueType() == MVT::f16);
2007 Src = stripBitcast(Src);
2008
Matt Arsenault550c66d2017-10-13 20:45:49 +00002009 // Be careful about folding modifiers if we already have an abs. fneg is
2010 // applied last, so we don't want to apply an earlier fneg.
2011 if ((Mods & SISrcMods::ABS) == 0) {
2012 unsigned ModsTmp;
2013 SelectVOP3ModsImpl(Src, Src, ModsTmp);
2014
2015 if ((ModsTmp & SISrcMods::NEG) != 0)
2016 Mods ^= SISrcMods::NEG;
2017
2018 if ((ModsTmp & SISrcMods::ABS) != 0)
2019 Mods |= SISrcMods::ABS;
2020 }
2021
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002022 // op_sel/op_sel_hi decide the source type and source.
2023 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16.
2024 // If the sources's op_sel is set, it picks the high half of the source
2025 // register.
2026
2027 Mods |= SISrcMods::OP_SEL_1;
Matt Arsenault550c66d2017-10-13 20:45:49 +00002028 if (isExtractHiElt(Src, Src)) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002029 Mods |= SISrcMods::OP_SEL_0;
2030
Matt Arsenault550c66d2017-10-13 20:45:49 +00002031 // TODO: Should we try to look for neg/abs here?
2032 }
2033
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002034 return true;
2035 }
2036
2037 return false;
2038}
2039
Matt Arsenault76935122017-09-20 20:28:39 +00002040bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src,
2041 SDValue &SrcMods) const {
2042 unsigned Mods = 0;
2043 SelectVOP3PMadMixModsImpl(In, Src, Mods);
2044 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2045 return true;
2046}
2047
Matt Arsenaulte1cd4822017-11-13 00:22:09 +00002048// TODO: Can we identify things like v_mad_mixhi_f16?
2049bool AMDGPUDAGToDAGISel::SelectHi16Elt(SDValue In, SDValue &Src) const {
2050 if (In.isUndef()) {
2051 Src = In;
2052 return true;
2053 }
2054
2055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) {
2056 SDLoc SL(In);
2057 SDValue K = CurDAG->getTargetConstant(C->getZExtValue() << 16, SL, MVT::i32);
2058 MachineSDNode *MovK = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
2059 SL, MVT::i32, K);
2060 Src = SDValue(MovK, 0);
2061 return true;
2062 }
2063
2064 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) {
2065 SDLoc SL(In);
2066 SDValue K = CurDAG->getTargetConstant(
2067 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32);
2068 MachineSDNode *MovK = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
2069 SL, MVT::i32, K);
2070 Src = SDValue(MovK, 0);
2071 return true;
2072 }
2073
2074 return isExtractHiElt(In, Src);
2075}
2076
Christian Konigd910b7d2013-02-26 17:52:16 +00002077void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002078 const AMDGPUTargetLowering& Lowering =
Matt Arsenault209a7b92014-04-18 07:40:20 +00002079 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002080 bool IsModified = false;
2081 do {
2082 IsModified = false;
Matt Arsenault68f05052017-12-04 22:18:27 +00002083
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002084 // Go over all selected nodes and try to fold them a bit more
Matt Arsenault68f05052017-12-04 22:18:27 +00002085 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin();
2086 while (Position != CurDAG->allnodes_end()) {
2087 SDNode *Node = &*Position++;
2088 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002089 if (!MachineNode)
2090 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +00002091
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002092 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
Matt Arsenault68f05052017-12-04 22:18:27 +00002093 if (ResNode != Node) {
2094 if (ResNode)
2095 ReplaceUses(Node, ResNode);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002096 IsModified = true;
2097 }
Tom Stellard2183b702013-06-03 17:39:46 +00002098 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002099 CurDAG->RemoveDeadNodes();
2100 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +00002101}
Tom Stellard20287692017-08-08 04:57:55 +00002102
2103void R600DAGToDAGISel::Select(SDNode *N) {
2104 unsigned int Opc = N->getOpcode();
2105 if (N->isMachineOpcode()) {
2106 N->setNodeId(-1);
2107 return; // Already selected.
2108 }
2109
2110 switch (Opc) {
2111 default: break;
2112 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
2113 case ISD::SCALAR_TO_VECTOR:
2114 case ISD::BUILD_VECTOR: {
2115 EVT VT = N->getValueType(0);
2116 unsigned NumVectorElts = VT.getVectorNumElements();
2117 unsigned RegClassID;
2118 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
2119 // that adds a 128 bits reg copy when going through TwoAddressInstructions
2120 // pass. We want to avoid 128 bits copies as much as possible because they
2121 // can't be bundled by our scheduler.
2122 switch(NumVectorElts) {
2123 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
2124 case 4:
2125 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
2126 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID;
2127 else
2128 RegClassID = AMDGPU::R600_Reg128RegClassID;
2129 break;
2130 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
2131 }
2132 SelectBuildVector(N, RegClassID);
2133 return;
2134 }
2135 }
2136
2137 SelectCode(N);
2138}
2139
2140bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
2141 SDValue &Offset) {
2142 ConstantSDNode *C;
2143 SDLoc DL(Addr);
2144
2145 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
2146 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
2147 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2148 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
2149 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
2150 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
2151 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2152 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
2153 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
2154 Base = Addr.getOperand(0);
2155 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2156 } else {
2157 Base = Addr;
2158 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
2159 }
2160
2161 return true;
2162}
2163
2164bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
2165 SDValue &Offset) {
2166 ConstantSDNode *IMMOffset;
2167
2168 if (Addr.getOpcode() == ISD::ADD
2169 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
2170 && isInt<16>(IMMOffset->getZExtValue())) {
2171
2172 Base = Addr.getOperand(0);
2173 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2174 MVT::i32);
2175 return true;
2176 // If the pointer address is constant, we can move it to the offset field.
2177 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
2178 && isInt<16>(IMMOffset->getZExtValue())) {
2179 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
2180 SDLoc(CurDAG->getEntryNode()),
2181 AMDGPU::ZERO, MVT::i32);
2182 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2183 MVT::i32);
2184 return true;
2185 }
2186
2187 // Default case, no offset
2188 Base = Addr;
2189 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
2190 return true;
2191}