blob: afd3d506ade8a626b8a9c0d79298c794e413a65b [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellard75aadc22012-12-11 21:25:42 +00006//
7//==-----------------------------------------------------------------------===//
8//
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// Defines an instruction selector for the AMDGPU target.
Tom Stellard75aadc22012-12-11 21:25:42 +000011//
12//===----------------------------------------------------------------------===//
Matt Arsenault592d0682015-12-01 23:04:05 +000013
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000014#include "AMDGPU.h"
Matt Arsenault7016f132017-08-03 22:30:46 +000015#include "AMDGPUArgumentUsageInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "AMDGPUISelLowering.h" // For AMDGPUISD
Tom Stellard75aadc22012-12-11 21:25:42 +000017#include "AMDGPUInstrInfo.h"
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +000018#include "AMDGPUPerfHintAnalysis.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"
Tom Stellard44b30b42018-05-22 02:03:23 +000027#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000028#include "llvm/ADT/APInt.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
Nicolai Haehnle35617ed2018-08-30 14:21:36 +000031#include "llvm/Analysis/LegacyDivergenceAnalysis.h"
Jan Veselyf97de002016-05-13 20:39:29 +000032#include "llvm/Analysis/ValueTracking.h"
Tom Stellard58ac7442014-04-29 23:12:48 +000033#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000034#include "llvm/CodeGen/ISDOpcodes.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000037#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000038#include "llvm/CodeGen/SelectionDAGISel.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000039#include "llvm/CodeGen/SelectionDAGNodes.h"
Craig Topper2fa14362018-03-29 17:21:10 +000040#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000041#include "llvm/IR/BasicBlock.h"
Alexander Timofeev2ce560f2019-07-02 17:59:44 +000042#ifdef EXPENSIVE_CHECKS
43#include "llvm/IR/Dominators.h"
44#endif
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000045#include "llvm/IR/Instruction.h"
46#include "llvm/MC/MCInstrDesc.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Support/CodeGen.h"
49#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000050#include "llvm/Support/MachineValueType.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000051#include "llvm/Support/MathExtras.h"
52#include <cassert>
53#include <cstdint>
54#include <new>
55#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000056
Matt Arsenaulte8c03a22019-03-08 20:58:11 +000057#define DEBUG_TYPE "isel"
58
Tom Stellard75aadc22012-12-11 21:25:42 +000059using namespace llvm;
60
Matt Arsenaultd2759212016-02-13 01:24:08 +000061namespace llvm {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000062
Matt Arsenaultd2759212016-02-13 01:24:08 +000063class R600InstrInfo;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000064
65} // end namespace llvm
Matt Arsenaultd2759212016-02-13 01:24:08 +000066
Tom Stellard75aadc22012-12-11 21:25:42 +000067//===----------------------------------------------------------------------===//
68// Instruction Selector Implementation
69//===----------------------------------------------------------------------===//
70
71namespace {
Tom Stellardbc4497b2016-02-12 23:45:29 +000072
Matt Arsenaultb7f87c02019-06-20 16:01:09 +000073static bool isNullConstantOrUndef(SDValue V) {
74 if (V.isUndef())
75 return true;
76
77 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
78 return Const != nullptr && Const->isNullValue();
79}
80
Matt Arsenaulte24b34e2019-06-19 23:37:43 +000081static bool getConstantValue(SDValue N, uint32_t &Out) {
Matt Arsenaultb7f87c02019-06-20 16:01:09 +000082 // This is only used for packed vectors, where ussing 0 for undef should
83 // always be good.
84 if (N.isUndef()) {
85 Out = 0;
86 return true;
87 }
88
Matt Arsenaulte24b34e2019-06-19 23:37:43 +000089 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
90 Out = C->getAPIntValue().getSExtValue();
91 return true;
92 }
93
94 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) {
95 Out = C->getValueAPF().bitcastToAPInt().getSExtValue();
96 return true;
97 }
98
99 return false;
100}
101
102// TODO: Handle undef as zero
103static SDNode *packConstantV2I16(const SDNode *N, SelectionDAG &DAG,
104 bool Negate = false) {
105 assert(N->getOpcode() == ISD::BUILD_VECTOR && N->getNumOperands() == 2);
106 uint32_t LHSVal, RHSVal;
107 if (getConstantValue(N->getOperand(0), LHSVal) &&
108 getConstantValue(N->getOperand(1), RHSVal)) {
109 SDLoc SL(N);
110 uint32_t K = Negate ?
111 (-LHSVal & 0xffff) | (-RHSVal << 16) :
112 (LHSVal & 0xffff) | (RHSVal << 16);
113 return DAG.getMachineNode(AMDGPU::S_MOV_B32, SL, N->getValueType(0),
114 DAG.getTargetConstant(K, SL, MVT::i32));
115 }
116
117 return nullptr;
118}
119
120static SDNode *packNegConstantV2I16(const SDNode *N, SelectionDAG &DAG) {
121 return packConstantV2I16(N, DAG, true);
122}
123
Tom Stellard75aadc22012-12-11 21:25:42 +0000124/// AMDGPU specific code to select AMDGPU machine instructions for
125/// SelectionDAG operations.
126class AMDGPUDAGToDAGISel : public SelectionDAGISel {
127 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
128 // make the right decision when generating code for different targets.
Tom Stellard5bfbae52018-07-11 20:59:01 +0000129 const GCNSubtarget *Subtarget;
Matt Arsenaultcc852232017-10-10 20:22:07 +0000130 bool EnableLateStructurizeCFG;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000131
Tom Stellard75aadc22012-12-11 21:25:42 +0000132public:
Matt Arsenault7016f132017-08-03 22:30:46 +0000133 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr,
134 CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
135 : SelectionDAGISel(*TM, OptLevel) {
Matt Arsenaultcc852232017-10-10 20:22:07 +0000136 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000137 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000138 ~AMDGPUDAGToDAGISel() override = default;
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +0000139
Matt Arsenault7016f132017-08-03 22:30:46 +0000140 void getAnalysisUsage(AnalysisUsage &AU) const override {
141 AU.addRequired<AMDGPUArgumentUsageInfo>();
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000142 AU.addRequired<AMDGPUPerfHintAnalysis>();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000143 AU.addRequired<LegacyDivergenceAnalysis>();
Alexander Timofeev66ac6b42019-07-02 18:16:42 +0000144#ifdef EXPENSIVE_CHECKS
Alexander Timofeev2ce560f2019-07-02 17:59:44 +0000145 AU.addRequired<DominatorTreeWrapperPass>();
146 AU.addRequired<LoopInfoWrapperPass>();
147#endif
Matt Arsenault7016f132017-08-03 22:30:46 +0000148 SelectionDAGISel::getAnalysisUsage(AU);
149 }
150
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000151 bool matchLoadD16FromBuildVector(SDNode *N) const;
152
Eric Christopher7792e322015-01-30 23:24:40 +0000153 bool runOnMachineFunction(MachineFunction &MF) override;
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000154 void PreprocessISelDAG() override;
Justin Bogner95927c02016-05-12 21:03:32 +0000155 void Select(SDNode *N) override;
Mehdi Amini117296c2016-10-01 02:56:57 +0000156 StringRef getPassName() const override;
Craig Topper5656db42014-04-29 07:57:24 +0000157 void PostprocessISelDAG() override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000158
Tom Stellard20287692017-08-08 04:57:55 +0000159protected:
160 void SelectBuildVector(SDNode *N, unsigned RegClassID);
161
Tom Stellard75aadc22012-12-11 21:25:42 +0000162private:
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000163 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000164 bool isNoNanSrc(SDValue N) const;
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000165 bool isInlineImmediate(const SDNode *N, bool Negated = false) const;
166 bool isNegInlineImmediate(const SDNode *N) const {
167 return isInlineImmediate(N, true);
168 }
169
Alexander Timofeevdb7ee762018-09-11 11:56:50 +0000170 bool isVGPRImm(const SDNode *N) const;
Alexander Timofeev4d302f62018-09-13 09:06:56 +0000171 bool isUniformLoad(const SDNode *N) const;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000172 bool isUniformBr(const SDNode *N) const;
173
Tim Renouff1c7b922018-08-02 22:53:57 +0000174 MachineSDNode *buildSMovImm64(SDLoc &DL, uint64_t Val, EVT VT) const;
175
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000176 SDNode *glueCopyToM0LDSInit(SDNode *N) const;
177 SDNode *glueCopyToM0(SDNode *N, SDValue Val) const;
Tom Stellard381a94a2015-05-12 15:00:49 +0000178
Tom Stellarddf94dc32013-08-14 23:24:24 +0000179 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
Tom Stellard20287692017-08-08 04:57:55 +0000180 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
181 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000182 bool isDSOffsetLegal(SDValue Base, unsigned Offset,
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000183 unsigned OffsetBits) const;
184 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
Tom Stellardf3fc5552014-08-22 18:49:35 +0000185 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
186 SDValue &Offset1) const;
Changpeng Fangb41574a2015-12-22 20:55:23 +0000187 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard155bbb72014-08-11 22:18:17 +0000188 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
189 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000190 SDValue &TFE, SDValue &DLC) const;
Tom Stellard155bbb72014-08-11 22:18:17 +0000191 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard1f9939f2015-02-27 14:59:41 +0000192 SDValue &SOffset, SDValue &Offset, SDValue &GLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000193 SDValue &SLC, SDValue &TFE, SDValue &DLC) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000194 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +0000195 SDValue &VAddr, SDValue &SOffset, SDValue &Offset,
Tom Stellard7980fc82014-09-25 18:30:26 +0000196 SDValue &SLC) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000197 bool SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000198 SDValue Addr, SDValue &RSrc, SDValue &VAddr,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000199 SDValue &SOffset, SDValue &ImmOffset) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000200 bool SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000201 SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000202 SDValue &Offset) const;
203
Tom Stellard155bbb72014-08-11 22:18:17 +0000204 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
205 SDValue &Offset, SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000206 SDValue &TFE, SDValue &DLC) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000207 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault88701812016-06-09 23:42:48 +0000208 SDValue &Offset, SDValue &SLC) const;
Jan Vesely43b7b5b2016-04-07 19:23:11 +0000209 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
210 SDValue &Offset) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000211
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000212 bool SelectFlatAtomic(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000213 SDValue &Offset, SDValue &SLC) const;
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000214 bool SelectFlatAtomicSigned(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenault4e309b02017-07-29 01:03:53 +0000215 SDValue &Offset, SDValue &SLC) const;
216
217 template <bool IsSigned>
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000218 bool SelectFlatOffset(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000219 SDValue &Offset, SDValue &SLC) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000220
Tom Stellarddee26a22015-08-06 19:28:30 +0000221 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset,
222 bool &Imm) const;
Matt Arsenault923712b2018-02-09 16:57:57 +0000223 SDValue Expand32BitAddress(SDValue Addr) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000224 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset,
225 bool &Imm) const;
226 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000227 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000228 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
229 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000230 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const;
Nicolai Haehnle7968c342016-07-12 08:12:16 +0000231 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000232
233 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000234 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods) const;
Tom Stellardb4a313a2014-08-01 00:32:39 +0000235 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Matt Arsenaultdf58e822017-04-25 21:17:38 +0000236 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const;
Tom Stellardb4a313a2014-08-01 00:32:39 +0000237 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
238 SDValue &Clamp, SDValue &Omod) const;
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000239 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
240 SDValue &Clamp, SDValue &Omod) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000241
Matt Arsenault4831ce52015-01-06 23:00:37 +0000242 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods,
243 SDValue &Clamp,
244 SDValue &Omod) const;
Matt Arsenault1cffa4c2014-11-13 19:49:04 +0000245
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +0000246 bool SelectVOP3OMods(SDValue In, SDValue &Src,
247 SDValue &Clamp, SDValue &Omod) const;
248
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000249 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
250 bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
251 SDValue &Clamp) const;
252
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +0000253 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const;
254 bool SelectVOP3OpSel0(SDValue In, SDValue &Src, SDValue &SrcMods,
255 SDValue &Clamp) const;
256
257 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
258 bool SelectVOP3OpSelMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
259 SDValue &Clamp) const;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000260 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const;
Matt Arsenault76935122017-09-20 20:28:39 +0000261 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +0000262
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000263 SDValue getHi16Elt(SDValue In) const;
Matt Arsenaulte1cd4822017-11-13 00:22:09 +0000264
Justin Bogner95927c02016-05-12 21:03:32 +0000265 void SelectADD_SUB_I64(SDNode *N);
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000266 void SelectAddcSubb(SDNode *N);
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000267 void SelectUADDO_USUBO(SDNode *N);
Justin Bogner95927c02016-05-12 21:03:32 +0000268 void SelectDIV_SCALE(SDNode *N);
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000269 void SelectDIV_FMAS(SDNode *N);
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000270 void SelectMAD_64_32(SDNode *N);
Tom Stellard8485fa02016-12-07 02:42:15 +0000271 void SelectFMA_W_CHAIN(SDNode *N);
272 void SelectFMUL_W_CHAIN(SDNode *N);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000273
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000274 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val,
Marek Olsak9b728682015-03-24 13:40:27 +0000275 uint32_t Offset, uint32_t Width);
Justin Bogner95927c02016-05-12 21:03:32 +0000276 void SelectS_BFEFromShifts(SDNode *N);
277 void SelectS_BFE(SDNode *N);
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +0000278 bool isCBranchSCC(const SDNode *N) const;
Justin Bogner95927c02016-05-12 21:03:32 +0000279 void SelectBRCOND(SDNode *N);
Matt Arsenault0084adc2018-04-30 19:08:16 +0000280 void SelectFMAD_FMA(SDNode *N);
Matt Arsenault88701812016-06-09 23:42:48 +0000281 void SelectATOMIC_CMP_SWAP(SDNode *N);
Matt Arsenaultd3c84e62019-06-14 13:26:32 +0000282 void SelectDSAppendConsume(SDNode *N, unsigned IntrID);
Matt Arsenault4d55d022019-06-19 19:55:27 +0000283 void SelectDS_GWS(SDNode *N, unsigned IntrID);
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000284 void SelectINTRINSIC_W_CHAIN(SDNode *N);
Matt Arsenault4d55d022019-06-19 19:55:27 +0000285 void SelectINTRINSIC_VOID(SDNode *N);
Marek Olsak9b728682015-03-24 13:40:27 +0000286
Tom Stellard20287692017-08-08 04:57:55 +0000287protected:
Tom Stellard75aadc22012-12-11 21:25:42 +0000288 // Include the pieces autogenerated from the target description.
289#include "AMDGPUGenDAGISel.inc"
290};
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000291
Tom Stellard20287692017-08-08 04:57:55 +0000292class R600DAGToDAGISel : public AMDGPUDAGToDAGISel {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000293 const R600Subtarget *Subtarget;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000294
295 bool isConstantLoad(const MemSDNode *N, int cbID) const;
296 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
297 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
298 SDValue& Offset);
Tom Stellard20287692017-08-08 04:57:55 +0000299public:
300 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) :
Matt Arsenault0da63502018-08-31 05:49:54 +0000301 AMDGPUDAGToDAGISel(TM, OptLevel) {}
Tom Stellard20287692017-08-08 04:57:55 +0000302
303 void Select(SDNode *N) override;
304
305 bool SelectADDRIndirect(SDValue Addr, SDValue &Base,
306 SDValue &Offset) override;
307 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
308 SDValue &Offset) override;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000309
310 bool runOnMachineFunction(MachineFunction &MF) override;
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000311
312 void PreprocessISelDAG() override {}
313
Tom Stellardc5a154d2018-06-28 23:47:12 +0000314protected:
315 // Include the pieces autogenerated from the target description.
316#include "R600GenDAGISel.inc"
Tom Stellard20287692017-08-08 04:57:55 +0000317};
318
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000319static SDValue stripBitcast(SDValue Val) {
320 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
321}
322
323// Figure out if this is really an extract of the high 16-bits of a dword.
324static bool isExtractHiElt(SDValue In, SDValue &Out) {
325 In = stripBitcast(In);
326 if (In.getOpcode() != ISD::TRUNCATE)
327 return false;
328
329 SDValue Srl = In.getOperand(0);
330 if (Srl.getOpcode() == ISD::SRL) {
331 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
332 if (ShiftAmt->getZExtValue() == 16) {
333 Out = stripBitcast(Srl.getOperand(0));
334 return true;
335 }
336 }
337 }
338
339 return false;
340}
341
342// Look through operations that obscure just looking at the low 16-bits of the
343// same register.
344static SDValue stripExtractLoElt(SDValue In) {
345 if (In.getOpcode() == ISD::TRUNCATE) {
346 SDValue Src = In.getOperand(0);
347 if (Src.getValueType().getSizeInBits() == 32)
348 return stripBitcast(Src);
349 }
350
351 return In;
352}
353
Tom Stellard75aadc22012-12-11 21:25:42 +0000354} // end anonymous namespace
355
Fangrui Song3d76d362018-10-03 03:38:22 +0000356INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "amdgpu-isel",
Matt Arsenault7016f132017-08-03 22:30:46 +0000357 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
358INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo)
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000359INITIALIZE_PASS_DEPENDENCY(AMDGPUPerfHintAnalysis)
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000360INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
Alexander Timofeev2ce560f2019-07-02 17:59:44 +0000361#ifdef EXPENSIVE_CHECKS
362INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
363INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
364#endif
Fangrui Song3d76d362018-10-03 03:38:22 +0000365INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "amdgpu-isel",
Matt Arsenault7016f132017-08-03 22:30:46 +0000366 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
367
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000368/// This pass converts a legalized DAG into a AMDGPU-specific
Tom Stellard75aadc22012-12-11 21:25:42 +0000369// DAG, ready for instruction scheduling.
Matt Arsenault7016f132017-08-03 22:30:46 +0000370FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM,
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +0000371 CodeGenOpt::Level OptLevel) {
372 return new AMDGPUDAGToDAGISel(TM, OptLevel);
Tom Stellard75aadc22012-12-11 21:25:42 +0000373}
374
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000375/// This pass converts a legalized DAG into a R600-specific
Tom Stellard20287692017-08-08 04:57:55 +0000376// DAG, ready for instruction scheduling.
377FunctionPass *llvm::createR600ISelDag(TargetMachine *TM,
378 CodeGenOpt::Level OptLevel) {
379 return new R600DAGToDAGISel(TM, OptLevel);
380}
381
Eric Christopher7792e322015-01-30 23:24:40 +0000382bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Alexander Timofeev2ce560f2019-07-02 17:59:44 +0000383#ifdef EXPENSIVE_CHECKS
384 DominatorTree & DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
385 LoopInfo * LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
386 for (auto &L : LI->getLoopsInPreorder()) {
387 assert(L->isLCSSAForm(DT));
388 }
389#endif
Tom Stellard5bfbae52018-07-11 20:59:01 +0000390 Subtarget = &MF.getSubtarget<GCNSubtarget>();
Eric Christopher7792e322015-01-30 23:24:40 +0000391 return SelectionDAGISel::runOnMachineFunction(MF);
Tom Stellard75aadc22012-12-11 21:25:42 +0000392}
393
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000394bool AMDGPUDAGToDAGISel::matchLoadD16FromBuildVector(SDNode *N) const {
395 assert(Subtarget->d16PreservesUnusedBits());
396 MVT VT = N->getValueType(0).getSimpleVT();
397 if (VT != MVT::v2i16 && VT != MVT::v2f16)
398 return false;
399
400 SDValue Lo = N->getOperand(0);
401 SDValue Hi = N->getOperand(1);
402
403 LoadSDNode *LdHi = dyn_cast<LoadSDNode>(stripBitcast(Hi));
404
405 // build_vector lo, (load ptr) -> load_d16_hi ptr, lo
406 // build_vector lo, (zextload ptr from i8) -> load_d16_hi_u8 ptr, lo
407 // build_vector lo, (sextload ptr from i8) -> load_d16_hi_i8 ptr, lo
408
409 // Need to check for possible indirect dependencies on the other half of the
410 // vector to avoid introducing a cycle.
411 if (LdHi && Hi.hasOneUse() && !LdHi->isPredecessorOf(Lo.getNode())) {
412 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
413
414 SDValue TiedIn = CurDAG->getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Lo);
415 SDValue Ops[] = {
416 LdHi->getChain(), LdHi->getBasePtr(), TiedIn
417 };
418
419 unsigned LoadOp = AMDGPUISD::LOAD_D16_HI;
420 if (LdHi->getMemoryVT() == MVT::i8) {
421 LoadOp = LdHi->getExtensionType() == ISD::SEXTLOAD ?
422 AMDGPUISD::LOAD_D16_HI_I8 : AMDGPUISD::LOAD_D16_HI_U8;
423 } else {
424 assert(LdHi->getMemoryVT() == MVT::i16);
425 }
426
427 SDValue NewLoadHi =
428 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdHi), VTList,
429 Ops, LdHi->getMemoryVT(),
430 LdHi->getMemOperand());
431
432 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadHi);
433 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdHi, 1), NewLoadHi.getValue(1));
434 return true;
435 }
436
437 // build_vector (load ptr), hi -> load_d16_lo ptr, hi
438 // build_vector (zextload ptr from i8), hi -> load_d16_lo_u8 ptr, hi
439 // build_vector (sextload ptr from i8), hi -> load_d16_lo_i8 ptr, hi
440 LoadSDNode *LdLo = dyn_cast<LoadSDNode>(stripBitcast(Lo));
441 if (LdLo && Lo.hasOneUse()) {
442 SDValue TiedIn = getHi16Elt(Hi);
443 if (!TiedIn || LdLo->isPredecessorOf(TiedIn.getNode()))
444 return false;
445
446 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
447 unsigned LoadOp = AMDGPUISD::LOAD_D16_LO;
448 if (LdLo->getMemoryVT() == MVT::i8) {
449 LoadOp = LdLo->getExtensionType() == ISD::SEXTLOAD ?
450 AMDGPUISD::LOAD_D16_LO_I8 : AMDGPUISD::LOAD_D16_LO_U8;
451 } else {
452 assert(LdLo->getMemoryVT() == MVT::i16);
453 }
454
455 TiedIn = CurDAG->getNode(ISD::BITCAST, SDLoc(N), VT, TiedIn);
456
457 SDValue Ops[] = {
458 LdLo->getChain(), LdLo->getBasePtr(), TiedIn
459 };
460
461 SDValue NewLoadLo =
462 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdLo), VTList,
463 Ops, LdLo->getMemoryVT(),
464 LdLo->getMemOperand());
465
466 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadLo);
467 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdLo, 1), NewLoadLo.getValue(1));
468 return true;
469 }
470
471 return false;
472}
473
474void AMDGPUDAGToDAGISel::PreprocessISelDAG() {
475 if (!Subtarget->d16PreservesUnusedBits())
476 return;
477
478 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
479
480 bool MadeChange = false;
481 while (Position != CurDAG->allnodes_begin()) {
482 SDNode *N = &*--Position;
483 if (N->use_empty())
484 continue;
485
486 switch (N->getOpcode()) {
487 case ISD::BUILD_VECTOR:
488 MadeChange |= matchLoadD16FromBuildVector(N);
489 break;
490 default:
491 break;
492 }
493 }
494
495 if (MadeChange) {
496 CurDAG->RemoveDeadNodes();
497 LLVM_DEBUG(dbgs() << "After PreProcess:\n";
498 CurDAG->dump(););
499 }
500}
501
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000502bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const {
503 if (TM.Options.NoNaNsFPMath)
504 return true;
505
506 // TODO: Move into isKnownNeverNaN
Amara Emersond28f0cd42017-05-01 15:17:51 +0000507 if (N->getFlags().isDefined())
508 return N->getFlags().hasNoNaNs();
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000509
510 return CurDAG->isKnownNeverNaN(N);
511}
512
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000513bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N,
514 bool Negated) const {
Matt Arsenaultb7f87c02019-06-20 16:01:09 +0000515 if (N->isUndef())
516 return true;
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000517
Tom Stellardc5a154d2018-06-28 23:47:12 +0000518 const SIInstrInfo *TII = Subtarget->getInstrInfo();
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000519 if (Negated) {
520 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
521 return TII->isInlineConstant(-C->getAPIntValue());
Matt Arsenaultfe267752016-07-28 00:32:02 +0000522
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000523 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
524 return TII->isInlineConstant(-C->getValueAPF().bitcastToAPInt());
Matt Arsenaultfe267752016-07-28 00:32:02 +0000525
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000526 } else {
527 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
528 return TII->isInlineConstant(C->getAPIntValue());
529
530 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
531 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt());
532 }
Matt Arsenaultfe267752016-07-28 00:32:02 +0000533
534 return false;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000535}
536
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000537/// Determine the register class for \p OpNo
Tom Stellarddf94dc32013-08-14 23:24:24 +0000538/// \returns The register class of the virtual register that will be used for
539/// the given operand number \OpNo or NULL if the register class cannot be
540/// determined.
541const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
542 unsigned OpNo) const {
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000543 if (!N->isMachineOpcode()) {
544 if (N->getOpcode() == ISD::CopyToReg) {
545 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
546 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
547 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
548 return MRI.getRegClass(Reg);
549 }
550
551 const SIRegisterInfo *TRI
Tom Stellard5bfbae52018-07-11 20:59:01 +0000552 = static_cast<const GCNSubtarget *>(Subtarget)->getRegisterInfo();
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000553 return TRI->getPhysRegClass(Reg);
554 }
555
Matt Arsenault209a7b92014-04-18 07:40:20 +0000556 return nullptr;
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000557 }
Matt Arsenault209a7b92014-04-18 07:40:20 +0000558
Tom Stellarddf94dc32013-08-14 23:24:24 +0000559 switch (N->getMachineOpcode()) {
560 default: {
Eric Christopherd9134482014-08-04 21:25:23 +0000561 const MCInstrDesc &Desc =
Eric Christopher7792e322015-01-30 23:24:40 +0000562 Subtarget->getInstrInfo()->get(N->getMachineOpcode());
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000563 unsigned OpIdx = Desc.getNumDefs() + OpNo;
564 if (OpIdx >= Desc.getNumOperands())
Matt Arsenault209a7b92014-04-18 07:40:20 +0000565 return nullptr;
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000566 int RegClass = Desc.OpInfo[OpIdx].RegClass;
Matt Arsenault209a7b92014-04-18 07:40:20 +0000567 if (RegClass == -1)
568 return nullptr;
569
Eric Christopher7792e322015-01-30 23:24:40 +0000570 return Subtarget->getRegisterInfo()->getRegClass(RegClass);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000571 }
572 case AMDGPU::REG_SEQUENCE: {
Matt Arsenault209a7b92014-04-18 07:40:20 +0000573 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christopherd9134482014-08-04 21:25:23 +0000574 const TargetRegisterClass *SuperRC =
Eric Christopher7792e322015-01-30 23:24:40 +0000575 Subtarget->getRegisterInfo()->getRegClass(RCID);
Matt Arsenault209a7b92014-04-18 07:40:20 +0000576
577 SDValue SubRegOp = N->getOperand(OpNo + 1);
578 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
Eric Christopher7792e322015-01-30 23:24:40 +0000579 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC,
580 SubRegIdx);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000581 }
582 }
583}
584
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000585SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N, SDValue Val) const {
Tom Stellard381a94a2015-05-12 15:00:49 +0000586 const SITargetLowering& Lowering =
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000587 *static_cast<const SITargetLowering*>(getTargetLowering());
Tom Stellard381a94a2015-05-12 15:00:49 +0000588
Matt Arsenault5a86dbc2019-06-14 13:33:36 +0000589 assert(N->getOperand(0).getValueType() == MVT::Other && "Expected chain");
590
591 SDValue M0 = Lowering.copyToM0(*CurDAG, N->getOperand(0), SDLoc(N),
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000592 Val);
Tom Stellard381a94a2015-05-12 15:00:49 +0000593
594 SDValue Glue = M0.getValue(1);
595
596 SmallVector <SDValue, 8> Ops;
Matt Arsenault5a86dbc2019-06-14 13:33:36 +0000597 Ops.push_back(M0); // Replace the chain.
598 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000599 Ops.push_back(N->getOperand(i));
600
Tom Stellard381a94a2015-05-12 15:00:49 +0000601 Ops.push_back(Glue);
Matt Arsenaulte6667de2017-12-04 22:18:22 +0000602 return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops);
Tom Stellard381a94a2015-05-12 15:00:49 +0000603}
604
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000605SDNode *AMDGPUDAGToDAGISel::glueCopyToM0LDSInit(SDNode *N) const {
Nicolai Haehnle4dc3b2b2019-07-01 17:17:45 +0000606 unsigned AS = cast<MemSDNode>(N)->getAddressSpace();
607 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
608 if (Subtarget->ldsRequiresM0Init())
609 return glueCopyToM0(N, CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32));
610 } else if (AS == AMDGPUAS::REGION_ADDRESS) {
611 MachineFunction &MF = CurDAG->getMachineFunction();
612 unsigned Value = MF.getInfo<SIMachineFunctionInfo>()->getGDSSize();
613 return
614 glueCopyToM0(N, CurDAG->getTargetConstant(Value, SDLoc(N), MVT::i32));
615 }
616 return N;
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000617}
618
Tim Renouff1c7b922018-08-02 22:53:57 +0000619MachineSDNode *AMDGPUDAGToDAGISel::buildSMovImm64(SDLoc &DL, uint64_t Imm,
620 EVT VT) const {
621 SDNode *Lo = CurDAG->getMachineNode(
622 AMDGPU::S_MOV_B32, DL, MVT::i32,
623 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, MVT::i32));
624 SDNode *Hi =
625 CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
626 CurDAG->getConstant(Imm >> 32, DL, MVT::i32));
627 const SDValue Ops[] = {
628 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
629 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
630 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)};
631
632 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops);
633}
634
Matt Arsenault61cb6fa2015-11-11 00:01:36 +0000635static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) {
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000636 switch (NumVectorElts) {
637 case 1:
Marek Olsak79c05872016-11-25 17:37:09 +0000638 return AMDGPU::SReg_32_XM0RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000639 case 2:
640 return AMDGPU::SReg_64RegClassID;
Tim Renouf361b5b22019-03-21 12:01:21 +0000641 case 3:
642 return AMDGPU::SGPR_96RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000643 case 4:
644 return AMDGPU::SReg_128RegClassID;
Tim Renouf033f99a2019-03-22 10:11:21 +0000645 case 5:
646 return AMDGPU::SGPR_160RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000647 case 8:
648 return AMDGPU::SReg_256RegClassID;
649 case 16:
650 return AMDGPU::SReg_512RegClassID;
651 }
652
653 llvm_unreachable("invalid vector size");
654}
655
Tom Stellard20287692017-08-08 04:57:55 +0000656void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) {
Tom Stellard20287692017-08-08 04:57:55 +0000657 EVT VT = N->getValueType(0);
658 unsigned NumVectorElts = VT.getVectorNumElements();
659 EVT EltVT = VT.getVectorElementType();
Tom Stellard20287692017-08-08 04:57:55 +0000660 SDLoc DL(N);
661 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
662
663 if (NumVectorElts == 1) {
664 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0),
665 RegClass);
666 return;
667 }
668
669 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
670 "supported yet");
671 // 16 = Max Num Vector Elements
672 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
673 // 1 = Vector Register Class
674 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
675
676 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
677 bool IsRegSeq = true;
678 unsigned NOps = N->getNumOperands();
679 for (unsigned i = 0; i < NOps; i++) {
680 // XXX: Why is this here?
681 if (isa<RegisterSDNode>(N->getOperand(i))) {
682 IsRegSeq = false;
683 break;
684 }
Simon Pilgrimede0e402018-05-19 12:46:02 +0000685 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
Tom Stellard20287692017-08-08 04:57:55 +0000686 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
Simon Pilgrimede0e402018-05-19 12:46:02 +0000687 RegSeqArgs[1 + (2 * i) + 1] = CurDAG->getTargetConstant(Sub, DL, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +0000688 }
689 if (NOps != NumVectorElts) {
690 // Fill in the missing undef elements if this was a scalar_to_vector.
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000691 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
Tom Stellard20287692017-08-08 04:57:55 +0000692 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
693 DL, EltVT);
694 for (unsigned i = NOps; i < NumVectorElts; ++i) {
Simon Pilgrimede0e402018-05-19 12:46:02 +0000695 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
Tom Stellard20287692017-08-08 04:57:55 +0000696 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
697 RegSeqArgs[1 + (2 * i) + 1] =
Simon Pilgrimede0e402018-05-19 12:46:02 +0000698 CurDAG->getTargetConstant(Sub, DL, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +0000699 }
700 }
701
702 if (!IsRegSeq)
703 SelectCode(N);
704 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs);
705}
706
Justin Bogner95927c02016-05-12 21:03:32 +0000707void AMDGPUDAGToDAGISel::Select(SDNode *N) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000708 unsigned int Opc = N->getOpcode();
709 if (N->isMachineOpcode()) {
Tim Northover31d093c2013-09-22 08:21:56 +0000710 N->setNodeId(-1);
Justin Bogner95927c02016-05-12 21:03:32 +0000711 return; // Already selected.
Tom Stellard75aadc22012-12-11 21:25:42 +0000712 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000713
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000714 if (isa<AtomicSDNode>(N) ||
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000715 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC ||
Matt Arsenaulta5840c32019-01-22 18:36:06 +0000716 Opc == ISD::ATOMIC_LOAD_FADD ||
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000717 Opc == AMDGPUISD::ATOMIC_LOAD_FMIN ||
718 Opc == AMDGPUISD::ATOMIC_LOAD_FMAX))
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000719 N = glueCopyToM0LDSInit(N);
Tom Stellard381a94a2015-05-12 15:00:49 +0000720
Tom Stellard75aadc22012-12-11 21:25:42 +0000721 switch (Opc) {
Matt Arsenault84445dd2017-11-30 22:51:26 +0000722 default:
723 break;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000724 // We are selecting i64 ADD here instead of custom lower it during
725 // DAG legalization, so we can fold some i64 ADDs used for address
726 // calculation into the LOAD and STORE instructions.
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000727 case ISD::ADDC:
728 case ISD::ADDE:
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000729 case ISD::SUBC:
730 case ISD::SUBE: {
Tom Stellard20287692017-08-08 04:57:55 +0000731 if (N->getValueType(0) != MVT::i64)
Tom Stellard1f15bff2014-02-25 21:36:18 +0000732 break;
733
Justin Bogner95927c02016-05-12 21:03:32 +0000734 SelectADD_SUB_I64(N);
735 return;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000736 }
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000737 case ISD::ADDCARRY:
738 case ISD::SUBCARRY:
739 if (N->getValueType(0) != MVT::i32)
740 break;
741
742 SelectAddcSubb(N);
743 return;
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000744 case ISD::UADDO:
745 case ISD::USUBO: {
746 SelectUADDO_USUBO(N);
747 return;
748 }
Tom Stellard8485fa02016-12-07 02:42:15 +0000749 case AMDGPUISD::FMUL_W_CHAIN: {
750 SelectFMUL_W_CHAIN(N);
751 return;
752 }
753 case AMDGPUISD::FMA_W_CHAIN: {
754 SelectFMA_W_CHAIN(N);
755 return;
756 }
757
Matt Arsenault064c2062014-06-11 17:40:32 +0000758 case ISD::SCALAR_TO_VECTOR:
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000759 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000760 EVT VT = N->getValueType(0);
761 unsigned NumVectorElts = VT.getVectorNumElements();
Matt Arsenault5a4ec812018-06-20 19:45:48 +0000762 if (VT.getScalarSizeInBits() == 16) {
763 if (Opc == ISD::BUILD_VECTOR && NumVectorElts == 2) {
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000764 if (SDNode *Packed = packConstantV2I16(N, *CurDAG)) {
765 ReplaceNode(N, Packed);
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000766 return;
767 }
768 }
769
770 break;
771 }
772
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000773 assert(VT.getVectorElementType().bitsEq(MVT::i32));
Tom Stellard20287692017-08-08 04:57:55 +0000774 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts);
775 SelectBuildVector(N, RegClassID);
Justin Bogner95927c02016-05-12 21:03:32 +0000776 return;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000777 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000778 case ISD::BUILD_PAIR: {
779 SDValue RC, SubReg0, SubReg1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000780 SDLoc DL(N);
Tom Stellard754f80f2013-04-05 23:31:51 +0000781 if (N->getValueType(0) == MVT::i128) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32);
783 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32);
784 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000785 } else if (N->getValueType(0) == MVT::i64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000786 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32);
787 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
788 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000789 } else {
790 llvm_unreachable("Unhandled value type for BUILD_PAIR");
791 }
792 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
793 N->getOperand(1), SubReg1 };
Justin Bogner95927c02016-05-12 21:03:32 +0000794 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
795 N->getValueType(0), Ops));
796 return;
Tom Stellard754f80f2013-04-05 23:31:51 +0000797 }
Tom Stellard7ed0b522014-04-03 20:19:27 +0000798
799 case ISD::Constant:
800 case ISD::ConstantFP: {
Tom Stellard20287692017-08-08 04:57:55 +0000801 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
Tom Stellard7ed0b522014-04-03 20:19:27 +0000802 break;
803
804 uint64_t Imm;
805 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
806 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
807 else {
Tom Stellard3cbe0142014-04-07 19:31:13 +0000808 ConstantSDNode *C = cast<ConstantSDNode>(N);
Tom Stellard7ed0b522014-04-03 20:19:27 +0000809 Imm = C->getZExtValue();
810 }
811
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000812 SDLoc DL(N);
Tim Renouff1c7b922018-08-02 22:53:57 +0000813 ReplaceNode(N, buildSMovImm64(DL, Imm, N->getValueType(0)));
Justin Bogner95927c02016-05-12 21:03:32 +0000814 return;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000815 }
Matt Arsenault4bf43d42015-09-25 17:27:08 +0000816 case ISD::LOAD:
Matt Arsenault3f8e7a32018-06-22 08:39:52 +0000817 case ISD::STORE:
818 case ISD::ATOMIC_LOAD:
819 case ISD::ATOMIC_STORE: {
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000820 N = glueCopyToM0LDSInit(N);
Tom Stellard096b8c12015-02-04 20:49:49 +0000821 break;
822 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000823
824 case AMDGPUISD::BFE_I32:
825 case AMDGPUISD::BFE_U32: {
Matt Arsenault78b86702014-04-18 05:19:26 +0000826 // There is a scalar version available, but unlike the vector version which
827 // has a separate operand for the offset and width, the scalar version packs
828 // the width and offset into a single operand. Try to move to the scalar
829 // version if the offsets are constant, so that we can try to keep extended
830 // loads of kernel arguments in SGPRs.
831
832 // TODO: Technically we could try to pattern match scalar bitshifts of
833 // dynamic values, but it's probably not useful.
834 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
835 if (!Offset)
836 break;
837
838 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
839 if (!Width)
840 break;
841
842 bool Signed = Opc == AMDGPUISD::BFE_I32;
843
Matt Arsenault78b86702014-04-18 05:19:26 +0000844 uint32_t OffsetVal = Offset->getZExtValue();
845 uint32_t WidthVal = Width->getZExtValue();
846
Justin Bogner95927c02016-05-12 21:03:32 +0000847 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
848 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal));
849 return;
Matt Arsenault78b86702014-04-18 05:19:26 +0000850 }
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000851 case AMDGPUISD::DIV_SCALE: {
Justin Bogner95927c02016-05-12 21:03:32 +0000852 SelectDIV_SCALE(N);
853 return;
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000854 }
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000855 case AMDGPUISD::DIV_FMAS: {
856 SelectDIV_FMAS(N);
857 return;
858 }
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000859 case AMDGPUISD::MAD_I64_I32:
860 case AMDGPUISD::MAD_U64_U32: {
861 SelectMAD_64_32(N);
862 return;
863 }
Tom Stellard3457a842014-10-09 19:06:00 +0000864 case ISD::CopyToReg: {
865 const SITargetLowering& Lowering =
866 *static_cast<const SITargetLowering*>(getTargetLowering());
Matt Arsenault0d0d6c22017-04-12 21:58:23 +0000867 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG);
Tom Stellard3457a842014-10-09 19:06:00 +0000868 break;
869 }
Marek Olsak9b728682015-03-24 13:40:27 +0000870 case ISD::AND:
871 case ISD::SRL:
872 case ISD::SRA:
Matt Arsenault7e8de012016-04-22 22:59:16 +0000873 case ISD::SIGN_EXTEND_INREG:
Tom Stellard20287692017-08-08 04:57:55 +0000874 if (N->getValueType(0) != MVT::i32)
Marek Olsak9b728682015-03-24 13:40:27 +0000875 break;
876
Justin Bogner95927c02016-05-12 21:03:32 +0000877 SelectS_BFE(N);
878 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000879 case ISD::BRCOND:
Justin Bogner95927c02016-05-12 21:03:32 +0000880 SelectBRCOND(N);
881 return;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000882 case ISD::FMAD:
Matt Arsenault0084adc2018-04-30 19:08:16 +0000883 case ISD::FMA:
884 SelectFMAD_FMA(N);
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000885 return;
Matt Arsenault88701812016-06-09 23:42:48 +0000886 case AMDGPUISD::ATOMIC_CMP_SWAP:
887 SelectATOMIC_CMP_SWAP(N);
888 return;
Matt Arsenault709374d2018-08-01 20:13:58 +0000889 case AMDGPUISD::CVT_PKRTZ_F16_F32:
890 case AMDGPUISD::CVT_PKNORM_I16_F32:
891 case AMDGPUISD::CVT_PKNORM_U16_F32:
892 case AMDGPUISD::CVT_PK_U16_U32:
893 case AMDGPUISD::CVT_PK_I16_I32: {
894 // Hack around using a legal type if f16 is illegal.
895 if (N->getValueType(0) == MVT::i32) {
896 MVT NewVT = Opc == AMDGPUISD::CVT_PKRTZ_F16_F32 ? MVT::v2f16 : MVT::v2i16;
897 N = CurDAG->MorphNodeTo(N, N->getOpcode(), CurDAG->getVTList(NewVT),
898 { N->getOperand(0), N->getOperand(1) });
899 SelectCode(N);
900 return;
901 }
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000902
903 break;
904 }
905 case ISD::INTRINSIC_W_CHAIN: {
906 SelectINTRINSIC_W_CHAIN(N);
907 return;
Matt Arsenault709374d2018-08-01 20:13:58 +0000908 }
Matt Arsenault4d55d022019-06-19 19:55:27 +0000909 case ISD::INTRINSIC_VOID: {
910 SelectINTRINSIC_VOID(N);
911 return;
912 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000913 }
Tom Stellard3457a842014-10-09 19:06:00 +0000914
Justin Bogner95927c02016-05-12 21:03:32 +0000915 SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000916}
917
Tom Stellardbc4497b2016-02-12 23:45:29 +0000918bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const {
919 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000920 const Instruction *Term = BB->getTerminator();
921 return Term->getMetadata("amdgpu.uniform") ||
922 Term->getMetadata("structurizecfg.uniform");
Tom Stellardbc4497b2016-02-12 23:45:29 +0000923}
924
Mehdi Amini117296c2016-10-01 02:56:57 +0000925StringRef AMDGPUDAGToDAGISel::getPassName() const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000926 return "AMDGPU DAG->DAG Pattern Instruction Selection";
927}
928
Tom Stellard41fc7852013-07-23 01:48:42 +0000929//===----------------------------------------------------------------------===//
930// Complex Patterns
931//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000932
Tom Stellard75aadc22012-12-11 21:25:42 +0000933bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
Tom Stellard20287692017-08-08 04:57:55 +0000934 SDValue &Offset) {
935 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000936}
937
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000938bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
939 SDValue &Offset) {
940 ConstantSDNode *C;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000941 SDLoc DL(Addr);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000942
943 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000944 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000945 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000946 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
947 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000948 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000949 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000950 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
951 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
952 Base = Addr.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000953 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000954 } else {
955 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000956 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000957 }
958
959 return true;
960}
Christian Konigd910b7d2013-02-26 17:52:16 +0000961
Matt Arsenault84445dd2017-11-30 22:51:26 +0000962// FIXME: Should only handle addcarry/subcarry
Justin Bogner95927c02016-05-12 21:03:32 +0000963void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000964 SDLoc DL(N);
965 SDValue LHS = N->getOperand(0);
966 SDValue RHS = N->getOperand(1);
967
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000968 unsigned Opcode = N->getOpcode();
969 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE);
970 bool ProduceCarry =
971 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC;
Matt Arsenault84445dd2017-11-30 22:51:26 +0000972 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000973
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000974 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
975 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000976
977 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
978 DL, MVT::i32, LHS, Sub0);
979 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
980 DL, MVT::i32, LHS, Sub1);
981
982 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
983 DL, MVT::i32, RHS, Sub0);
984 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
985 DL, MVT::i32, RHS, Sub1);
986
987 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000988
Tom Stellard80942a12014-09-05 14:07:59 +0000989 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000990 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
991
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000992 SDNode *AddLo;
993 if (!ConsumeCarry) {
994 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
995 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args);
996 } else {
997 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) };
998 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args);
999 }
1000 SDValue AddHiArgs[] = {
1001 SDValue(Hi0, 0),
1002 SDValue(Hi1, 0),
1003 SDValue(AddLo, 1)
1004 };
1005 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs);
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001006
Nicolai Haehnle67624af2016-10-14 10:30:00 +00001007 SDValue RegSequenceArgs[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001008 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001009 SDValue(AddLo,0),
1010 Sub0,
1011 SDValue(AddHi,0),
1012 Sub1,
1013 };
Nicolai Haehnle67624af2016-10-14 10:30:00 +00001014 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
1015 MVT::i64, RegSequenceArgs);
1016
1017 if (ProduceCarry) {
1018 // Replace the carry-use
Nirav Dave3264c1b2018-03-19 20:19:46 +00001019 ReplaceUses(SDValue(N, 1), SDValue(AddHi, 1));
Nicolai Haehnle67624af2016-10-14 10:30:00 +00001020 }
1021
1022 // Replace the remaining uses.
Nirav Dave3264c1b2018-03-19 20:19:46 +00001023 ReplaceNode(N, RegSequence);
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001024}
1025
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001026void AMDGPUDAGToDAGISel::SelectAddcSubb(SDNode *N) {
1027 SDLoc DL(N);
1028 SDValue LHS = N->getOperand(0);
1029 SDValue RHS = N->getOperand(1);
1030 SDValue CI = N->getOperand(2);
1031
1032 unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::V_ADDC_U32_e64
1033 : AMDGPU::V_SUBB_U32_e64;
1034 CurDAG->SelectNodeTo(
1035 N, Opc, N->getVTList(),
1036 {LHS, RHS, CI, CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
1037}
1038
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +00001039void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) {
1040 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned
1041 // carry out despite the _i32 name. These were renamed in VI to _U32.
1042 // FIXME: We should probably rename the opcodes here.
1043 unsigned Opc = N->getOpcode() == ISD::UADDO ?
1044 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64;
1045
Michael Liaoeea51772019-03-20 20:18:56 +00001046 CurDAG->SelectNodeTo(
1047 N, Opc, N->getVTList(),
1048 {N->getOperand(0), N->getOperand(1),
1049 CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +00001050}
1051
Tom Stellard8485fa02016-12-07 02:42:15 +00001052void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) {
1053 SDLoc SL(N);
1054 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod
1055 SDValue Ops[10];
1056
1057 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]);
1058 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1059 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]);
1060 Ops[8] = N->getOperand(0);
1061 Ops[9] = N->getOperand(4);
1062
1063 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops);
1064}
1065
1066void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) {
1067 SDLoc SL(N);
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001068 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod
Tom Stellard8485fa02016-12-07 02:42:15 +00001069 SDValue Ops[8];
1070
1071 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]);
1072 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1073 Ops[6] = N->getOperand(0);
1074 Ops[7] = N->getOperand(3);
1075
1076 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops);
1077}
1078
Matt Arsenault044f1d12015-02-14 04:24:28 +00001079// We need to handle this here because tablegen doesn't support matching
1080// instructions with multiple outputs.
Justin Bogner95927c02016-05-12 21:03:32 +00001081void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +00001082 SDLoc SL(N);
1083 EVT VT = N->getValueType(0);
1084
1085 assert(VT == MVT::f32 || VT == MVT::f64);
1086
1087 unsigned Opc
1088 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
1089
Matt Arsenault3b99f122017-01-19 06:04:12 +00001090 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1091 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +00001092}
1093
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001094void AMDGPUDAGToDAGISel::SelectDIV_FMAS(SDNode *N) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001095 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
1096 const SIRegisterInfo *TRI = ST->getRegisterInfo();
1097
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001098 SDLoc SL(N);
1099 EVT VT = N->getValueType(0);
1100
1101 assert(VT == MVT::f32 || VT == MVT::f64);
1102
1103 unsigned Opc
1104 = (VT == MVT::f64) ? AMDGPU::V_DIV_FMAS_F64 : AMDGPU::V_DIV_FMAS_F32;
1105
1106 SDValue CarryIn = N->getOperand(3);
1107 // V_DIV_FMAS implicitly reads VCC.
1108 SDValue VCC = CurDAG->getCopyToReg(CurDAG->getEntryNode(), SL,
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001109 TRI->getVCC(), CarryIn, SDValue());
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001110
1111 SDValue Ops[10];
1112
1113 SelectVOP3Mods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]);
1114 SelectVOP3Mods(N->getOperand(1), Ops[3], Ops[2]);
1115 SelectVOP3Mods(N->getOperand(2), Ops[5], Ops[4]);
1116
1117 Ops[8] = VCC;
1118 Ops[9] = VCC.getValue(1);
1119
1120 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1121}
1122
Matt Arsenault4f6318f2017-11-06 17:04:37 +00001123// We need to handle this here because tablegen doesn't support matching
1124// instructions with multiple outputs.
1125void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) {
1126 SDLoc SL(N);
1127 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32;
1128 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32;
1129
1130 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1);
1131 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
1132 Clamp };
1133 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1134}
1135
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00001136bool AMDGPUDAGToDAGISel::isDSOffsetLegal(SDValue Base, unsigned Offset,
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001137 unsigned OffsetBits) const {
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001138 if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
1139 (OffsetBits == 8 && !isUInt<8>(Offset)))
1140 return false;
1141
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00001142 if (Subtarget->hasUsableDSOffset() ||
Matt Arsenault706f9302015-07-06 16:01:58 +00001143 Subtarget->unsafeDSOffsetFoldingEnabled())
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001144 return true;
1145
1146 // On Southern Islands instruction with a negative base value and an offset
1147 // don't seem to work.
1148 return CurDAG->SignBitIsZero(Base);
1149}
1150
1151bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
1152 SDValue &Offset) const {
Tom Stellard92b24f32016-04-29 14:34:26 +00001153 SDLoc DL(Addr);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001154 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1155 SDValue N0 = Addr.getOperand(0);
1156 SDValue N1 = Addr.getOperand(1);
1157 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1158 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
1159 // (add n0, c0)
1160 Base = N0;
Tom Stellard92b24f32016-04-29 14:34:26 +00001161 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001162 return true;
1163 }
Matt Arsenault966a94f2015-09-08 19:34:22 +00001164 } else if (Addr.getOpcode() == ISD::SUB) {
1165 // sub C, x -> add (sub 0, x), C
1166 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1167 int64_t ByteOffset = C->getSExtValue();
1168 if (isUInt<16>(ByteOffset)) {
Matt Arsenault966a94f2015-09-08 19:34:22 +00001169 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001170
Matt Arsenault966a94f2015-09-08 19:34:22 +00001171 // XXX - This is kind of hacky. Create a dummy sub node so we can check
1172 // the known bits in isDSOffsetLegal. We need to emit the selected node
1173 // here, so this is thrown away.
1174 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1175 Zero, Addr.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001176
Matt Arsenault966a94f2015-09-08 19:34:22 +00001177 if (isDSOffsetLegal(Sub, ByteOffset, 16)) {
Tim Renoufcfdfba92019-03-18 19:35:44 +00001178 SmallVector<SDValue, 3> Opnds;
1179 Opnds.push_back(Zero);
1180 Opnds.push_back(Addr.getOperand(1));
Matt Arsenault84445dd2017-11-30 22:51:26 +00001181
Tim Renoufcfdfba92019-03-18 19:35:44 +00001182 // FIXME: Select to VOP3 version for with-carry.
1183 unsigned SubOp = AMDGPU::V_SUB_I32_e32;
1184 if (Subtarget->hasAddNoCarry()) {
1185 SubOp = AMDGPU::V_SUB_U32_e64;
Michael Liaoeea51772019-03-20 20:18:56 +00001186 Opnds.push_back(
1187 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
Tim Renoufcfdfba92019-03-18 19:35:44 +00001188 }
1189
1190 MachineSDNode *MachineSub =
1191 CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001192
1193 Base = SDValue(MachineSub, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +00001194 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001195 return true;
1196 }
1197 }
1198 }
1199 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1200 // If we have a constant address, prefer to put the constant into the
1201 // offset. This can save moves to load the constant address since multiple
1202 // operations can share the zero base address register, and enables merging
1203 // into read2 / write2 instructions.
1204
1205 SDLoc DL(Addr);
1206
Matt Arsenaulte775f5f2014-10-14 17:21:19 +00001207 if (isUInt<16>(CAddr->getZExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001208 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardc8d79202014-10-15 21:08:59 +00001209 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001210 DL, MVT::i32, Zero);
Tom Stellardc8d79202014-10-15 21:08:59 +00001211 Base = SDValue(MovZero, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +00001212 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
Matt Arsenaulte775f5f2014-10-14 17:21:19 +00001213 return true;
1214 }
1215 }
1216
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001217 // default case
1218 Base = Addr;
Matt Arsenault966a94f2015-09-08 19:34:22 +00001219 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001220 return true;
1221}
1222
Matt Arsenault966a94f2015-09-08 19:34:22 +00001223// TODO: If offset is too big, put low 16-bit into offset.
Tom Stellardf3fc5552014-08-22 18:49:35 +00001224bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base,
1225 SDValue &Offset0,
1226 SDValue &Offset1) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001227 SDLoc DL(Addr);
1228
Tom Stellardf3fc5552014-08-22 18:49:35 +00001229 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1230 SDValue N0 = Addr.getOperand(0);
1231 SDValue N1 = Addr.getOperand(1);
1232 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1233 unsigned DWordOffset0 = C1->getZExtValue() / 4;
1234 unsigned DWordOffset1 = DWordOffset0 + 1;
1235 // (add n0, c0)
1236 if (isDSOffsetLegal(N0, DWordOffset1, 8)) {
1237 Base = N0;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001238 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1239 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +00001240 return true;
1241 }
Matt Arsenault966a94f2015-09-08 19:34:22 +00001242 } else if (Addr.getOpcode() == ISD::SUB) {
1243 // sub C, x -> add (sub 0, x), C
1244 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1245 unsigned DWordOffset0 = C->getZExtValue() / 4;
1246 unsigned DWordOffset1 = DWordOffset0 + 1;
Tom Stellardf3fc5552014-08-22 18:49:35 +00001247
Matt Arsenault966a94f2015-09-08 19:34:22 +00001248 if (isUInt<8>(DWordOffset0)) {
1249 SDLoc DL(Addr);
1250 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1251
1252 // XXX - This is kind of hacky. Create a dummy sub node so we can check
1253 // the known bits in isDSOffsetLegal. We need to emit the selected node
1254 // here, so this is thrown away.
1255 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1256 Zero, Addr.getOperand(1));
1257
1258 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) {
Tim Renoufcfdfba92019-03-18 19:35:44 +00001259 SmallVector<SDValue, 3> Opnds;
1260 Opnds.push_back(Zero);
1261 Opnds.push_back(Addr.getOperand(1));
1262 unsigned SubOp = AMDGPU::V_SUB_I32_e32;
1263 if (Subtarget->hasAddNoCarry()) {
1264 SubOp = AMDGPU::V_SUB_U32_e64;
Michael Liaoeea51772019-03-20 20:18:56 +00001265 Opnds.push_back(
1266 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
Tim Renoufcfdfba92019-03-18 19:35:44 +00001267 }
Matt Arsenault84445dd2017-11-30 22:51:26 +00001268
Matt Arsenault966a94f2015-09-08 19:34:22 +00001269 MachineSDNode *MachineSub
Tim Renoufcfdfba92019-03-18 19:35:44 +00001270 = CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001271
1272 Base = SDValue(MachineSub, 0);
1273 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1274 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
1275 return true;
1276 }
1277 }
1278 }
1279 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001280 unsigned DWordOffset0 = CAddr->getZExtValue() / 4;
1281 unsigned DWordOffset1 = DWordOffset0 + 1;
1282 assert(4 * DWordOffset0 == CAddr->getZExtValue());
1283
1284 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001285 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001286 MachineSDNode *MovZero
1287 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001288 DL, MVT::i32, Zero);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001289 Base = SDValue(MovZero, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001290 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1291 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001292 return true;
1293 }
1294 }
1295
Tom Stellardf3fc5552014-08-22 18:49:35 +00001296 // default case
Matt Arsenault0efdd062016-09-09 22:29:28 +00001297
Tom Stellardf3fc5552014-08-22 18:49:35 +00001298 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001299 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8);
1300 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +00001301 return true;
1302}
1303
Changpeng Fangb41574a2015-12-22 20:55:23 +00001304bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
Tom Stellard155bbb72014-08-11 22:18:17 +00001305 SDValue &VAddr, SDValue &SOffset,
1306 SDValue &Offset, SDValue &Offen,
1307 SDValue &Idxen, SDValue &Addr64,
1308 SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001309 SDValue &TFE, SDValue &DLC) const {
Changpeng Fangb41574a2015-12-22 20:55:23 +00001310 // Subtarget prefers to use flat instruction
1311 if (Subtarget->useFlatForGlobal())
1312 return false;
1313
Tom Stellardb02c2682014-06-24 23:33:07 +00001314 SDLoc DL(Addr);
1315
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001316 if (!GLC.getNode())
1317 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1318 if (!SLC.getNode())
1319 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001320 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1);
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001321 DLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellard155bbb72014-08-11 22:18:17 +00001322
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001323 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1324 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1325 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1);
1326 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001327
Tim Renouff1c7b922018-08-02 22:53:57 +00001328 ConstantSDNode *C1 = nullptr;
1329 SDValue N0 = Addr;
Tom Stellardb02c2682014-06-24 23:33:07 +00001330 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Tim Renouff1c7b922018-08-02 22:53:57 +00001331 C1 = cast<ConstantSDNode>(Addr.getOperand(1));
1332 if (isUInt<32>(C1->getZExtValue()))
1333 N0 = Addr.getOperand(0);
1334 else
1335 C1 = nullptr;
Tom Stellardb02c2682014-06-24 23:33:07 +00001336 }
Tom Stellard94b72312015-02-11 00:34:35 +00001337
Tim Renouff1c7b922018-08-02 22:53:57 +00001338 if (N0.getOpcode() == ISD::ADD) {
1339 // (add N2, N3) -> addr64, or
1340 // (add (add N2, N3), C1) -> addr64
1341 SDValue N2 = N0.getOperand(0);
1342 SDValue N3 = N0.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001343 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
Tim Renouff1c7b922018-08-02 22:53:57 +00001344
1345 if (N2->isDivergent()) {
1346 if (N3->isDivergent()) {
1347 // Both N2 and N3 are divergent. Use N0 (the result of the add) as the
1348 // addr64, and construct the resource from a 0 address.
1349 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1350 VAddr = N0;
1351 } else {
1352 // N2 is divergent, N3 is not.
1353 Ptr = N3;
1354 VAddr = N2;
1355 }
1356 } else {
1357 // N2 is not divergent.
1358 Ptr = N2;
1359 VAddr = N3;
1360 }
1361 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1362 } else if (N0->isDivergent()) {
1363 // N0 is divergent. Use it as the addr64, and construct the resource from a
1364 // 0 address.
1365 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1366 VAddr = N0;
1367 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1368 } else {
1369 // N0 -> offset, or
1370 // (N0 + C1) -> offset
1371 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001372 Ptr = N0;
Tim Renouff1c7b922018-08-02 22:53:57 +00001373 }
1374
1375 if (!C1) {
1376 // No offset.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001377 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001378 return true;
Tom Stellardb02c2682014-06-24 23:33:07 +00001379 }
1380
Tim Renouff1c7b922018-08-02 22:53:57 +00001381 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) {
1382 // Legal offset for instruction.
1383 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1384 return true;
1385 }
Changpeng Fangb41574a2015-12-22 20:55:23 +00001386
Tim Renouff1c7b922018-08-02 22:53:57 +00001387 // Illegal offset, store it in soffset.
1388 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1389 SOffset =
1390 SDValue(CurDAG->getMachineNode(
1391 AMDGPU::S_MOV_B32, DL, MVT::i32,
1392 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)),
1393 0);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001394 return true;
Tom Stellard155bbb72014-08-11 22:18:17 +00001395}
1396
1397bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001398 SDValue &VAddr, SDValue &SOffset,
Tom Stellard1f9939f2015-02-27 14:59:41 +00001399 SDValue &Offset, SDValue &GLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001400 SDValue &SLC, SDValue &TFE,
1401 SDValue &DLC) const {
Tom Stellard1f9939f2015-02-27 14:59:41 +00001402 SDValue Ptr, Offen, Idxen, Addr64;
Tom Stellard155bbb72014-08-11 22:18:17 +00001403
Tom Stellard70580f82015-07-20 14:28:41 +00001404 // addr64 bit was removed for volcanic islands.
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00001405 if (!Subtarget->hasAddr64())
Tom Stellard70580f82015-07-20 14:28:41 +00001406 return false;
1407
Changpeng Fangb41574a2015-12-22 20:55:23 +00001408 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001409 GLC, SLC, TFE, DLC))
Changpeng Fangb41574a2015-12-22 20:55:23 +00001410 return false;
Tom Stellard155bbb72014-08-11 22:18:17 +00001411
1412 ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
1413 if (C->getSExtValue()) {
1414 SDLoc DL(Addr);
Matt Arsenault485defe2014-11-05 19:01:17 +00001415
1416 const SITargetLowering& Lowering =
1417 *static_cast<const SITargetLowering*>(getTargetLowering());
1418
1419 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001420 return true;
1421 }
Matt Arsenault485defe2014-11-05 19:01:17 +00001422
Tom Stellard155bbb72014-08-11 22:18:17 +00001423 return false;
1424}
1425
Tom Stellard7980fc82014-09-25 18:30:26 +00001426bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001427 SDValue &VAddr, SDValue &SOffset,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001428 SDValue &Offset,
1429 SDValue &SLC) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001430 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1);
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001431 SDValue GLC, TFE, DLC;
Tom Stellard7980fc82014-09-25 18:30:26 +00001432
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001433 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE, DLC);
Tom Stellard7980fc82014-09-25 18:30:26 +00001434}
1435
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001436static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) {
1437 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>();
1438 return PSV && PSV->isStack();
Matt Arsenaultac0fc842016-09-17 16:09:55 +00001439}
1440
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001441std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const {
1442 const MachineFunction &MF = CurDAG->getMachineFunction();
1443 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1444
1445 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) {
1446 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(),
1447 FI->getValueType(0));
1448
Matt Arsenaultb812b7a2019-06-05 22:20:47 +00001449 // If we can resolve this to a frame index access, this will be relative to
1450 // either the stack or frame pointer SGPR.
1451 return std::make_pair(
1452 TFI, CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32));
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001453 }
1454
1455 // If we don't know this private access is a local stack object, it needs to
1456 // be relative to the entry point's scratch wave offset register.
1457 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(),
1458 MVT::i32));
1459}
1460
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001461bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001462 SDValue Addr, SDValue &Rsrc,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001463 SDValue &VAddr, SDValue &SOffset,
1464 SDValue &ImmOffset) const {
Tom Stellardb02094e2014-07-21 15:45:01 +00001465
1466 SDLoc DL(Addr);
1467 MachineFunction &MF = CurDAG->getMachineFunction();
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001468 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardb02094e2014-07-21 15:45:01 +00001469
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001470 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Tom Stellardb02094e2014-07-21 15:45:01 +00001471
Matt Arsenault0774ea22017-04-24 19:40:59 +00001472 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1473 unsigned Imm = CAddr->getZExtValue();
Matt Arsenault0774ea22017-04-24 19:40:59 +00001474
1475 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32);
1476 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
1477 DL, MVT::i32, HighBits);
1478 VAddr = SDValue(MovHighBits, 0);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001479
1480 // In a call sequence, stores to the argument stack area are relative to the
1481 // stack pointer.
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001482 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001483 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1484 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1485
1486 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
Matt Arsenault0774ea22017-04-24 19:40:59 +00001487 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16);
1488 return true;
1489 }
1490
Tom Stellardb02094e2014-07-21 15:45:01 +00001491 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Matt Arsenault0774ea22017-04-24 19:40:59 +00001492 // (add n0, c1)
1493
Tom Stellard78655fc2015-07-16 19:40:09 +00001494 SDValue N0 = Addr.getOperand(0);
Tom Stellardb02094e2014-07-21 15:45:01 +00001495 SDValue N1 = Addr.getOperand(1);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001496
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001497 // Offsets in vaddr must be positive if range checking is enabled.
Matt Arsenault45b98182017-11-15 00:45:43 +00001498 //
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001499 // The total computation of vaddr + soffset + offset must not overflow. If
1500 // vaddr is negative, even if offset is 0 the sgpr offset add will end up
Matt Arsenault45b98182017-11-15 00:45:43 +00001501 // overflowing.
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001502 //
1503 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would
1504 // always perform a range check. If a negative vaddr base index was used,
1505 // this would fail the range check. The overall address computation would
1506 // compute a valid address, but this doesn't happen due to the range
1507 // check. For out-of-bounds MUBUF loads, a 0 is returned.
1508 //
1509 // Therefore it should be safe to fold any VGPR offset on gfx9 into the
1510 // MUBUF vaddr, but not on older subtargets which can only do this if the
1511 // sign bit is known 0.
Matt Arsenaultcd099612016-02-24 04:55:29 +00001512 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
Matt Arsenault45b98182017-11-15 00:45:43 +00001513 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) &&
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001514 (!Subtarget->privateMemoryResourceIsRangeChecked() ||
1515 CurDAG->SignBitIsZero(N0))) {
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001516 std::tie(VAddr, SOffset) = foldFrameIndex(N0);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001517 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1518 return true;
Tom Stellardb02094e2014-07-21 15:45:01 +00001519 }
1520 }
1521
Tom Stellardb02094e2014-07-21 15:45:01 +00001522 // (node)
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001523 std::tie(VAddr, SOffset) = foldFrameIndex(Addr);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001524 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Tom Stellardb02094e2014-07-21 15:45:01 +00001525 return true;
1526}
1527
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001528bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001529 SDValue Addr,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001530 SDValue &SRsrc,
1531 SDValue &SOffset,
1532 SDValue &Offset) const {
1533 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr);
Marek Olsakffadcb72017-11-09 01:52:17 +00001534 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue()))
Matt Arsenault0774ea22017-04-24 19:40:59 +00001535 return false;
1536
1537 SDLoc DL(Addr);
1538 MachineFunction &MF = CurDAG->getMachineFunction();
1539 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1540
1541 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001542
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001543 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001544 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1545 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1546
1547 // FIXME: Get from MachinePointerInfo? We should only be using the frame
1548 // offset if we know this is in a call sequence.
1549 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
1550
Matt Arsenault0774ea22017-04-24 19:40:59 +00001551 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1552 return true;
1553}
1554
Tom Stellard155bbb72014-08-11 22:18:17 +00001555bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1556 SDValue &SOffset, SDValue &Offset,
1557 SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001558 SDValue &TFE, SDValue &DLC) const {
Tom Stellard155bbb72014-08-11 22:18:17 +00001559 SDValue Ptr, VAddr, Offen, Idxen, Addr64;
Tom Stellard794c8c02014-12-02 17:05:41 +00001560 const SIInstrInfo *TII =
Eric Christopher7792e322015-01-30 23:24:40 +00001561 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellardb02094e2014-07-21 15:45:01 +00001562
Changpeng Fangb41574a2015-12-22 20:55:23 +00001563 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001564 GLC, SLC, TFE, DLC))
Changpeng Fangb41574a2015-12-22 20:55:23 +00001565 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001566
Tom Stellard155bbb72014-08-11 22:18:17 +00001567 if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
1568 !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
1569 !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
Tom Stellard794c8c02014-12-02 17:05:41 +00001570 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() |
Tom Stellard155bbb72014-08-11 22:18:17 +00001571 APInt::getAllOnesValue(32).getZExtValue(); // Size
1572 SDLoc DL(Addr);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00001573
1574 const SITargetLowering& Lowering =
1575 *static_cast<const SITargetLowering*>(getTargetLowering());
1576
1577 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001578 return true;
1579 }
1580 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001581}
1582
Tom Stellard7980fc82014-09-25 18:30:26 +00001583bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001584 SDValue &Soffset, SDValue &Offset
1585 ) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001586 SDValue GLC, SLC, TFE, DLC;
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001587
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001588 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC);
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001589}
1590bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Tom Stellard7980fc82014-09-25 18:30:26 +00001591 SDValue &Soffset, SDValue &Offset,
Matt Arsenault88701812016-06-09 23:42:48 +00001592 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001593 SDValue GLC, TFE, DLC;
Tom Stellard7980fc82014-09-25 18:30:26 +00001594
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001595 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC);
Tom Stellard7980fc82014-09-25 18:30:26 +00001596}
1597
Matt Arsenault4e309b02017-07-29 01:03:53 +00001598template <bool IsSigned>
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001599bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDNode *N,
1600 SDValue Addr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001601 SDValue &VAddr,
1602 SDValue &Offset,
1603 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001604 return static_cast<const SITargetLowering*>(getTargetLowering())->
1605 SelectFlatOffset(IsSigned, *CurDAG, N, Addr, VAddr, Offset, SLC);
Matt Arsenault7757c592016-06-09 23:42:54 +00001606}
1607
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001608bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDNode *N,
1609 SDValue Addr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001610 SDValue &VAddr,
1611 SDValue &Offset,
1612 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001613 return SelectFlatOffset<false>(N, Addr, VAddr, Offset, SLC);
Matt Arsenault4e309b02017-07-29 01:03:53 +00001614}
1615
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001616bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDNode *N,
1617 SDValue Addr,
Matt Arsenault4e309b02017-07-29 01:03:53 +00001618 SDValue &VAddr,
1619 SDValue &Offset,
1620 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001621 return SelectFlatOffset<true>(N, Addr, VAddr, Offset, SLC);
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001622}
1623
Tom Stellarddee26a22015-08-06 19:28:30 +00001624bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode,
1625 SDValue &Offset, bool &Imm) const {
1626
1627 // FIXME: Handle non-constant offsets.
1628 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode);
1629 if (!C)
1630 return false;
1631
1632 SDLoc SL(ByteOffsetNode);
Tom Stellard5bfbae52018-07-11 20:59:01 +00001633 GCNSubtarget::Generation Gen = Subtarget->getGeneration();
Tom Stellarddee26a22015-08-06 19:28:30 +00001634 int64_t ByteOffset = C->getSExtValue();
Tom Stellard08efb7e2017-01-27 18:41:14 +00001635 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001636
Tom Stellard08efb7e2017-01-27 18:41:14 +00001637 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001638 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1639 Imm = true;
1640 return true;
1641 }
1642
Tom Stellard217361c2015-08-06 19:28:38 +00001643 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset))
1644 return false;
1645
Marek Olsak8973a0a2017-05-24 14:53:50 +00001646 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) {
1647 // 32-bit Immediates are supported on Sea Islands.
Tom Stellard217361c2015-08-06 19:28:38 +00001648 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1649 } else {
Tom Stellarddee26a22015-08-06 19:28:30 +00001650 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32);
1651 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32,
1652 C32Bit), 0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001653 }
Tom Stellard217361c2015-08-06 19:28:38 +00001654 Imm = false;
1655 return true;
Tom Stellarddee26a22015-08-06 19:28:30 +00001656}
1657
Matt Arsenault923712b2018-02-09 16:57:57 +00001658SDValue AMDGPUDAGToDAGISel::Expand32BitAddress(SDValue Addr) const {
1659 if (Addr.getValueType() != MVT::i32)
1660 return Addr;
1661
1662 // Zero-extend a 32-bit address.
1663 SDLoc SL(Addr);
1664
1665 const MachineFunction &MF = CurDAG->getMachineFunction();
1666 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1667 unsigned AddrHiVal = Info->get32BitAddressHighBits();
1668 SDValue AddrHi = CurDAG->getTargetConstant(AddrHiVal, SL, MVT::i32);
1669
1670 const SDValue Ops[] = {
1671 CurDAG->getTargetConstant(AMDGPU::SReg_64_XEXECRegClassID, SL, MVT::i32),
1672 Addr,
1673 CurDAG->getTargetConstant(AMDGPU::sub0, SL, MVT::i32),
1674 SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, AddrHi),
1675 0),
1676 CurDAG->getTargetConstant(AMDGPU::sub1, SL, MVT::i32),
1677 };
1678
1679 return SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, SL, MVT::i64,
1680 Ops), 0);
1681}
1682
Tom Stellarddee26a22015-08-06 19:28:30 +00001683bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase,
1684 SDValue &Offset, bool &Imm) const {
Tom Stellarddee26a22015-08-06 19:28:30 +00001685 SDLoc SL(Addr);
Matt Arsenault923712b2018-02-09 16:57:57 +00001686
Marek Olsak3fc20792018-08-29 20:03:00 +00001687 // A 32-bit (address + offset) should not cause unsigned 32-bit integer
1688 // wraparound, because s_load instructions perform the addition in 64 bits.
1689 if ((Addr.getValueType() != MVT::i32 ||
1690 Addr->getFlags().hasNoUnsignedWrap()) &&
1691 CurDAG->isBaseWithConstantOffset(Addr)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001692 SDValue N0 = Addr.getOperand(0);
1693 SDValue N1 = Addr.getOperand(1);
1694
1695 if (SelectSMRDOffset(N1, Offset, Imm)) {
Matt Arsenault923712b2018-02-09 16:57:57 +00001696 SBase = Expand32BitAddress(N0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001697 return true;
1698 }
1699 }
Matt Arsenault923712b2018-02-09 16:57:57 +00001700 SBase = Expand32BitAddress(Addr);
Tom Stellarddee26a22015-08-06 19:28:30 +00001701 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32);
1702 Imm = true;
1703 return true;
1704}
1705
1706bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase,
1707 SDValue &Offset) const {
1708 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001709 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm;
1710}
Tom Stellarddee26a22015-08-06 19:28:30 +00001711
Marek Olsak8973a0a2017-05-24 14:53:50 +00001712bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase,
1713 SDValue &Offset) const {
1714
1715 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1716 return false;
1717
1718 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001719 if (!SelectSMRD(Addr, SBase, Offset, Imm))
1720 return false;
1721
Marek Olsak8973a0a2017-05-24 14:53:50 +00001722 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001723}
1724
Tom Stellarddee26a22015-08-06 19:28:30 +00001725bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase,
1726 SDValue &Offset) const {
1727 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001728 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm &&
1729 !isa<ConstantSDNode>(Offset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001730}
1731
1732bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr,
1733 SDValue &Offset) const {
1734 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001735 return SelectSMRDOffset(Addr, Offset, Imm) && Imm;
1736}
Tom Stellarddee26a22015-08-06 19:28:30 +00001737
Marek Olsak8973a0a2017-05-24 14:53:50 +00001738bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr,
1739 SDValue &Offset) const {
1740 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1741 return false;
1742
1743 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001744 if (!SelectSMRDOffset(Addr, Offset, Imm))
1745 return false;
1746
Marek Olsak8973a0a2017-05-24 14:53:50 +00001747 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001748}
1749
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001750bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index,
1751 SDValue &Base,
1752 SDValue &Offset) const {
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001753 SDLoc DL(Index);
1754
1755 if (CurDAG->isBaseWithConstantOffset(Index)) {
1756 SDValue N0 = Index.getOperand(0);
1757 SDValue N1 = Index.getOperand(1);
1758 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1759
1760 // (add n0, c0)
Changpeng Fang6f539292018-12-21 20:57:34 +00001761 // Don't peel off the offset (c0) if doing so could possibly lead
1762 // the base (n0) to be negative.
1763 if (C1->getSExtValue() <= 0 || CurDAG->SignBitIsZero(N0)) {
1764 Base = N0;
1765 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32);
1766 return true;
1767 }
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001768 }
1769
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001770 if (isa<ConstantSDNode>(Index))
1771 return false;
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001772
1773 Base = Index;
1774 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1775 return true;
1776}
1777
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001778SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL,
1779 SDValue Val, uint32_t Offset,
1780 uint32_t Width) {
Marek Olsak9b728682015-03-24 13:40:27 +00001781 // Transformation function, pack the offset and width of a BFE into
1782 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
1783 // source, bits [5:0] contain the offset and bits [22:16] the width.
1784 uint32_t PackedVal = Offset | (Width << 16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001785 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32);
Marek Olsak9b728682015-03-24 13:40:27 +00001786
1787 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst);
1788}
1789
Justin Bogner95927c02016-05-12 21:03:32 +00001790void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001791 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c)
1792 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c)
1793 // Predicate: 0 < b <= c < 32
1794
1795 const SDValue &Shl = N->getOperand(0);
1796 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1));
1797 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1798
1799 if (B && C) {
1800 uint32_t BVal = B->getZExtValue();
1801 uint32_t CVal = C->getZExtValue();
1802
1803 if (0 < BVal && BVal <= CVal && CVal < 32) {
1804 bool Signed = N->getOpcode() == ISD::SRA;
1805 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32;
1806
Justin Bogner95927c02016-05-12 21:03:32 +00001807 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal,
1808 32 - CVal));
1809 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001810 }
1811 }
Justin Bogner95927c02016-05-12 21:03:32 +00001812 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001813}
1814
Justin Bogner95927c02016-05-12 21:03:32 +00001815void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001816 switch (N->getOpcode()) {
1817 case ISD::AND:
1818 if (N->getOperand(0).getOpcode() == ISD::SRL) {
1819 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)"
1820 // Predicate: isMask(mask)
1821 const SDValue &Srl = N->getOperand(0);
1822 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
1823 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
1824
1825 if (Shift && Mask) {
1826 uint32_t ShiftVal = Shift->getZExtValue();
1827 uint32_t MaskVal = Mask->getZExtValue();
1828
1829 if (isMask_32(MaskVal)) {
1830 uint32_t WidthVal = countPopulation(MaskVal);
1831
Justin Bogner95927c02016-05-12 21:03:32 +00001832 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1833 Srl.getOperand(0), ShiftVal, WidthVal));
1834 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001835 }
1836 }
1837 }
1838 break;
1839 case ISD::SRL:
1840 if (N->getOperand(0).getOpcode() == ISD::AND) {
1841 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)"
1842 // Predicate: isMask(mask >> b)
1843 const SDValue &And = N->getOperand(0);
1844 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1));
1845 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1));
1846
1847 if (Shift && Mask) {
1848 uint32_t ShiftVal = Shift->getZExtValue();
1849 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal;
1850
1851 if (isMask_32(MaskVal)) {
1852 uint32_t WidthVal = countPopulation(MaskVal);
1853
Justin Bogner95927c02016-05-12 21:03:32 +00001854 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1855 And.getOperand(0), ShiftVal, WidthVal));
1856 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001857 }
1858 }
Justin Bogner95927c02016-05-12 21:03:32 +00001859 } else if (N->getOperand(0).getOpcode() == ISD::SHL) {
1860 SelectS_BFEFromShifts(N);
1861 return;
1862 }
Marek Olsak9b728682015-03-24 13:40:27 +00001863 break;
1864 case ISD::SRA:
Justin Bogner95927c02016-05-12 21:03:32 +00001865 if (N->getOperand(0).getOpcode() == ISD::SHL) {
1866 SelectS_BFEFromShifts(N);
1867 return;
1868 }
Marek Olsak9b728682015-03-24 13:40:27 +00001869 break;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001870
1871 case ISD::SIGN_EXTEND_INREG: {
1872 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8
1873 SDValue Src = N->getOperand(0);
1874 if (Src.getOpcode() != ISD::SRL)
1875 break;
1876
1877 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1));
1878 if (!Amt)
1879 break;
1880
1881 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
Justin Bogner95927c02016-05-12 21:03:32 +00001882 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0),
1883 Amt->getZExtValue(), Width));
1884 return;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001885 }
Marek Olsak9b728682015-03-24 13:40:27 +00001886 }
1887
Justin Bogner95927c02016-05-12 21:03:32 +00001888 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001889}
1890
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001891bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const {
1892 assert(N->getOpcode() == ISD::BRCOND);
1893 if (!N->hasOneUse())
1894 return false;
1895
1896 SDValue Cond = N->getOperand(1);
1897 if (Cond.getOpcode() == ISD::CopyToReg)
1898 Cond = Cond.getOperand(2);
1899
1900 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
1901 return false;
1902
1903 MVT VT = Cond.getOperand(0).getSimpleValueType();
1904 if (VT == MVT::i32)
1905 return true;
1906
1907 if (VT == MVT::i64) {
Tom Stellard5bfbae52018-07-11 20:59:01 +00001908 auto ST = static_cast<const GCNSubtarget *>(Subtarget);
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001909
1910 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1911 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64();
1912 }
1913
1914 return false;
1915}
1916
Justin Bogner95927c02016-05-12 21:03:32 +00001917void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001918 SDValue Cond = N->getOperand(1);
1919
Matt Arsenault327188a2016-12-15 21:57:11 +00001920 if (Cond.isUndef()) {
1921 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other,
1922 N->getOperand(2), N->getOperand(0));
1923 return;
1924 }
1925
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001926 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
1927 const SIRegisterInfo *TRI = ST->getRegisterInfo();
1928
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001929 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N);
1930 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001931 unsigned CondReg = UseSCCBr ? (unsigned)AMDGPU::SCC : TRI->getVCC();
Tom Stellardbc4497b2016-02-12 23:45:29 +00001932 SDLoc SL(N);
1933
Tim Renouf6eaad1e2018-01-09 21:34:43 +00001934 if (!UseSCCBr) {
1935 // This is the case that we are selecting to S_CBRANCH_VCCNZ. We have not
1936 // analyzed what generates the vcc value, so we do not know whether vcc
1937 // bits for disabled lanes are 0. Thus we need to mask out bits for
1938 // disabled lanes.
1939 //
1940 // For the case that we select S_CBRANCH_SCC1 and it gets
1941 // changed to S_CBRANCH_VCCNZ in SIFixSGPRCopies, SIFixSGPRCopies calls
1942 // SIInstrInfo::moveToVALU which inserts the S_AND).
1943 //
1944 // We could add an analysis of what generates the vcc value here and omit
1945 // the S_AND when is unnecessary. But it would be better to add a separate
1946 // pass after SIFixSGPRCopies to do the unnecessary S_AND removal, so it
1947 // catches both cases.
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001948 Cond = SDValue(CurDAG->getMachineNode(ST->isWave32() ? AMDGPU::S_AND_B32
1949 : AMDGPU::S_AND_B64,
1950 SL, MVT::i1,
1951 CurDAG->getRegister(ST->isWave32() ? AMDGPU::EXEC_LO
1952 : AMDGPU::EXEC,
1953 MVT::i1),
1954 Cond),
Tim Renouf6eaad1e2018-01-09 21:34:43 +00001955 0);
1956 }
1957
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001958 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond);
1959 CurDAG->SelectNodeTo(N, BrOp, MVT::Other,
Justin Bogner95927c02016-05-12 21:03:32 +00001960 N->getOperand(2), // Basic Block
Matt Arsenaultf530e8b2016-11-07 19:09:33 +00001961 VCC.getValue(0));
Tom Stellardbc4497b2016-02-12 23:45:29 +00001962}
1963
Matt Arsenault0084adc2018-04-30 19:08:16 +00001964void AMDGPUDAGToDAGISel::SelectFMAD_FMA(SDNode *N) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001965 MVT VT = N->getSimpleValueType(0);
Matt Arsenault0084adc2018-04-30 19:08:16 +00001966 bool IsFMA = N->getOpcode() == ISD::FMA;
1967 if (VT != MVT::f32 || (!Subtarget->hasMadMixInsts() &&
1968 !Subtarget->hasFmaMixInsts()) ||
1969 ((IsFMA && Subtarget->hasMadMixInsts()) ||
1970 (!IsFMA && Subtarget->hasFmaMixInsts()))) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001971 SelectCode(N);
1972 return;
1973 }
1974
1975 SDValue Src0 = N->getOperand(0);
1976 SDValue Src1 = N->getOperand(1);
1977 SDValue Src2 = N->getOperand(2);
1978 unsigned Src0Mods, Src1Mods, Src2Mods;
1979
Matt Arsenault0084adc2018-04-30 19:08:16 +00001980 // Avoid using v_mad_mix_f32/v_fma_mix_f32 unless there is actually an operand
1981 // using the conversion from f16.
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001982 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods);
1983 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods);
1984 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods);
1985
Matt Arsenault0084adc2018-04-30 19:08:16 +00001986 assert((IsFMA || !Subtarget->hasFP32Denormals()) &&
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001987 "fmad selected with denormals enabled");
1988 // TODO: We can select this with f32 denormals enabled if all the sources are
1989 // converted from f16 (in which case fmad isn't legal).
1990
1991 if (Sel0 || Sel1 || Sel2) {
1992 // For dummy operands.
1993 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
1994 SDValue Ops[] = {
1995 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0,
1996 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1,
1997 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2,
1998 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1),
1999 Zero, Zero
2000 };
2001
Matt Arsenault0084adc2018-04-30 19:08:16 +00002002 CurDAG->SelectNodeTo(N,
2003 IsFMA ? AMDGPU::V_FMA_MIX_F32 : AMDGPU::V_MAD_MIX_F32,
2004 MVT::f32, Ops);
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002005 } else {
2006 SelectCode(N);
2007 }
2008}
2009
Matt Arsenault88701812016-06-09 23:42:48 +00002010// This is here because there isn't a way to use the generated sub0_sub1 as the
2011// subreg index to EXTRACT_SUBREG in tablegen.
2012void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) {
2013 MemSDNode *Mem = cast<MemSDNode>(N);
2014 unsigned AS = Mem->getAddressSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +00002015 if (AS == AMDGPUAS::FLAT_ADDRESS) {
Matt Arsenault7757c592016-06-09 23:42:54 +00002016 SelectCode(N);
2017 return;
2018 }
Matt Arsenault88701812016-06-09 23:42:48 +00002019
2020 MVT VT = N->getSimpleValueType(0);
2021 bool Is32 = (VT == MVT::i32);
2022 SDLoc SL(N);
2023
2024 MachineSDNode *CmpSwap = nullptr;
2025 if (Subtarget->hasAddr64()) {
Vitaly Buka74503982017-10-15 05:35:02 +00002026 SDValue SRsrc, VAddr, SOffset, Offset, SLC;
Matt Arsenault88701812016-06-09 23:42:48 +00002027
2028 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00002029 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN :
2030 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00002031 SDValue CmpVal = Mem->getOperand(2);
2032
2033 // XXX - Do we care about glue operands?
2034
2035 SDValue Ops[] = {
2036 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain()
2037 };
2038
2039 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2040 }
2041 }
2042
2043 if (!CmpSwap) {
2044 SDValue SRsrc, SOffset, Offset, SLC;
2045 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00002046 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN :
2047 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00002048
2049 SDValue CmpVal = Mem->getOperand(2);
2050 SDValue Ops[] = {
2051 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain()
2052 };
2053
2054 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2055 }
2056 }
2057
2058 if (!CmpSwap) {
2059 SelectCode(N);
2060 return;
2061 }
2062
Chandler Carruth66654b72018-08-14 23:30:32 +00002063 MachineMemOperand *MMO = Mem->getMemOperand();
2064 CurDAG->setNodeMemRefs(CmpSwap, {MMO});
Matt Arsenault88701812016-06-09 23:42:48 +00002065
2066 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
2067 SDValue Extract
2068 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0));
2069
2070 ReplaceUses(SDValue(N, 0), Extract);
2071 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1));
2072 CurDAG->RemoveDeadNode(N);
2073}
2074
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002075void AMDGPUDAGToDAGISel::SelectDSAppendConsume(SDNode *N, unsigned IntrID) {
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002076 // The address is assumed to be uniform, so if it ends up in a VGPR, it will
2077 // be copied to an SGPR with readfirstlane.
2078 unsigned Opc = IntrID == Intrinsic::amdgcn_ds_append ?
2079 AMDGPU::DS_APPEND : AMDGPU::DS_CONSUME;
2080
2081 SDValue Chain = N->getOperand(0);
2082 SDValue Ptr = N->getOperand(2);
2083 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
Matt Arsenault9e5fa332019-06-14 21:01:24 +00002084 MachineMemOperand *MMO = M->getMemOperand();
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002085 bool IsGDS = M->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
2086
2087 SDValue Offset;
2088 if (CurDAG->isBaseWithConstantOffset(Ptr)) {
2089 SDValue PtrBase = Ptr.getOperand(0);
2090 SDValue PtrOffset = Ptr.getOperand(1);
2091
2092 const APInt &OffsetVal = cast<ConstantSDNode>(PtrOffset)->getAPIntValue();
2093 if (isDSOffsetLegal(PtrBase, OffsetVal.getZExtValue(), 16)) {
2094 N = glueCopyToM0(N, PtrBase);
2095 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i32);
2096 }
2097 }
2098
2099 if (!Offset) {
2100 N = glueCopyToM0(N, Ptr);
2101 Offset = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
2102 }
2103
2104 SDValue Ops[] = {
2105 Offset,
2106 CurDAG->getTargetConstant(IsGDS, SDLoc(), MVT::i32),
2107 Chain,
2108 N->getOperand(N->getNumOperands() - 1) // New glue
2109 };
2110
Matt Arsenault9e5fa332019-06-14 21:01:24 +00002111 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2112 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002113}
2114
Matt Arsenault740322f2019-06-20 21:11:42 +00002115static unsigned gwsIntrinToOpcode(unsigned IntrID) {
2116 switch (IntrID) {
2117 case Intrinsic::amdgcn_ds_gws_init:
2118 return AMDGPU::DS_GWS_INIT;
2119 case Intrinsic::amdgcn_ds_gws_barrier:
2120 return AMDGPU::DS_GWS_BARRIER;
2121 case Intrinsic::amdgcn_ds_gws_sema_v:
2122 return AMDGPU::DS_GWS_SEMA_V;
2123 case Intrinsic::amdgcn_ds_gws_sema_br:
2124 return AMDGPU::DS_GWS_SEMA_BR;
2125 case Intrinsic::amdgcn_ds_gws_sema_p:
2126 return AMDGPU::DS_GWS_SEMA_P;
2127 case Intrinsic::amdgcn_ds_gws_sema_release_all:
2128 return AMDGPU::DS_GWS_SEMA_RELEASE_ALL;
2129 default:
2130 llvm_unreachable("not a gws intrinsic");
2131 }
2132}
2133
Matt Arsenault4d55d022019-06-19 19:55:27 +00002134void AMDGPUDAGToDAGISel::SelectDS_GWS(SDNode *N, unsigned IntrID) {
Matt Arsenault740322f2019-06-20 21:11:42 +00002135 if (IntrID == Intrinsic::amdgcn_ds_gws_sema_release_all &&
2136 !Subtarget->hasGWSSemaReleaseAll()) {
2137 // Let this error.
2138 SelectCode(N);
2139 return;
2140 }
2141
2142 // Chain, intrinsic ID, vsrc, offset
2143 const bool HasVSrc = N->getNumOperands() == 4;
2144 assert(HasVSrc || N->getNumOperands() == 3);
2145
Matt Arsenault4d55d022019-06-19 19:55:27 +00002146 SDLoc SL(N);
Matt Arsenault740322f2019-06-20 21:11:42 +00002147 SDValue BaseOffset = N->getOperand(HasVSrc ? 3 : 2);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002148 int ImmOffset = 0;
2149 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
2150 MachineMemOperand *MMO = M->getMemOperand();
2151
2152 // Don't worry if the offset ends up in a VGPR. Only one lane will have
2153 // effect, so SIFixSGPRCopies will validly insert readfirstlane.
2154
2155 // The resource id offset is computed as (<isa opaque base> + M0[21:16] +
2156 // offset field) % 64. Some versions of the programming guide omit the m0
2157 // part, or claim it's from offset 0.
2158 if (ConstantSDNode *ConstOffset = dyn_cast<ConstantSDNode>(BaseOffset)) {
2159 // If we have a constant offset, try to use the default value for m0 as a
2160 // base to possibly avoid setting it up.
2161 glueCopyToM0(N, CurDAG->getTargetConstant(-1, SL, MVT::i32));
2162 ImmOffset = ConstOffset->getZExtValue() + 1;
2163 } else {
2164 if (CurDAG->isBaseWithConstantOffset(BaseOffset)) {
2165 ImmOffset = BaseOffset.getConstantOperandVal(1);
2166 BaseOffset = BaseOffset.getOperand(0);
2167 }
2168
2169 // Prefer to do the shift in an SGPR since it should be possible to use m0
2170 // as the result directly. If it's already an SGPR, it will be eliminated
2171 // later.
2172 SDNode *SGPROffset
2173 = CurDAG->getMachineNode(AMDGPU::V_READFIRSTLANE_B32, SL, MVT::i32,
2174 BaseOffset);
2175 // Shift to offset in m0
2176 SDNode *M0Base
2177 = CurDAG->getMachineNode(AMDGPU::S_LSHL_B32, SL, MVT::i32,
2178 SDValue(SGPROffset, 0),
2179 CurDAG->getTargetConstant(16, SL, MVT::i32));
2180 glueCopyToM0(N, SDValue(M0Base, 0));
2181 }
2182
Matt Arsenault740322f2019-06-20 21:11:42 +00002183 SDValue V0;
2184 SDValue Chain = N->getOperand(0);
2185 SDValue Glue;
2186 if (HasVSrc) {
2187 SDValue VSrc0 = N->getOperand(2);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002188
Matt Arsenault740322f2019-06-20 21:11:42 +00002189 // The manual doesn't mention this, but it seems only v0 works.
2190 V0 = CurDAG->getRegister(AMDGPU::VGPR0, MVT::i32);
2191
2192 SDValue CopyToV0 = CurDAG->getCopyToReg(
2193 N->getOperand(0), SL, V0, VSrc0,
2194 N->getOperand(N->getNumOperands() - 1));
2195 Chain = CopyToV0;
2196 Glue = CopyToV0.getValue(1);
2197 }
Matt Arsenault4d55d022019-06-19 19:55:27 +00002198
2199 SDValue OffsetField = CurDAG->getTargetConstant(ImmOffset, SL, MVT::i32);
2200
2201 // TODO: Can this just be removed from the instruction?
2202 SDValue GDS = CurDAG->getTargetConstant(1, SL, MVT::i1);
2203
Matt Arsenault740322f2019-06-20 21:11:42 +00002204 const unsigned Opc = gwsIntrinToOpcode(IntrID);
2205 SmallVector<SDValue, 5> Ops;
2206 if (HasVSrc)
2207 Ops.push_back(V0);
2208 Ops.push_back(OffsetField);
2209 Ops.push_back(GDS);
2210 Ops.push_back(Chain);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002211
Matt Arsenault740322f2019-06-20 21:11:42 +00002212 if (HasVSrc)
2213 Ops.push_back(Glue);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002214
2215 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2216 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
2217}
2218
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002219void AMDGPUDAGToDAGISel::SelectINTRINSIC_W_CHAIN(SDNode *N) {
2220 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2221 switch (IntrID) {
2222 case Intrinsic::amdgcn_ds_append:
2223 case Intrinsic::amdgcn_ds_consume: {
2224 if (N->getValueType(0) != MVT::i32)
2225 break;
2226 SelectDSAppendConsume(N, IntrID);
2227 return;
2228 }
Matt Arsenault4d55d022019-06-19 19:55:27 +00002229 }
2230
2231 SelectCode(N);
2232}
2233
2234void AMDGPUDAGToDAGISel::SelectINTRINSIC_VOID(SDNode *N) {
2235 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2236 switch (IntrID) {
2237 case Intrinsic::amdgcn_ds_gws_init:
2238 case Intrinsic::amdgcn_ds_gws_barrier:
Matt Arsenault740322f2019-06-20 21:11:42 +00002239 case Intrinsic::amdgcn_ds_gws_sema_v:
2240 case Intrinsic::amdgcn_ds_gws_sema_br:
2241 case Intrinsic::amdgcn_ds_gws_sema_p:
2242 case Intrinsic::amdgcn_ds_gws_sema_release_all:
Matt Arsenault4d55d022019-06-19 19:55:27 +00002243 SelectDS_GWS(N, IntrID);
2244 return;
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002245 default:
2246 break;
2247 }
2248
2249 SelectCode(N);
2250}
2251
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002252bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src,
2253 unsigned &Mods) const {
2254 Mods = 0;
Tom Stellardb4a313a2014-08-01 00:32:39 +00002255 Src = In;
2256
2257 if (Src.getOpcode() == ISD::FNEG) {
2258 Mods |= SISrcMods::NEG;
2259 Src = Src.getOperand(0);
2260 }
2261
2262 if (Src.getOpcode() == ISD::FABS) {
2263 Mods |= SISrcMods::ABS;
2264 Src = Src.getOperand(0);
2265 }
2266
Tom Stellardb4a313a2014-08-01 00:32:39 +00002267 return true;
2268}
2269
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002270bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
2271 SDValue &SrcMods) const {
2272 unsigned Mods;
2273 if (SelectVOP3ModsImpl(In, Src, Mods)) {
2274 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2275 return true;
2276 }
2277
2278 return false;
2279}
2280
Matt Arsenaultf84e5d92017-01-31 03:07:46 +00002281bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src,
2282 SDValue &SrcMods) const {
2283 SelectVOP3Mods(In, Src, SrcMods);
2284 return isNoNanSrc(Src);
2285}
2286
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002287bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const {
2288 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG)
2289 return false;
2290
2291 Src = In;
2292 return true;
Tom Stellarddb5a11f2015-07-13 15:47:57 +00002293}
2294
Tom Stellardb4a313a2014-08-01 00:32:39 +00002295bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
2296 SDValue &SrcMods, SDValue &Clamp,
2297 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002298 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002299 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2300 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellardb4a313a2014-08-01 00:32:39 +00002301
2302 return SelectVOP3Mods(In, Src, SrcMods);
2303}
2304
Matt Arsenault4831ce52015-01-06 23:00:37 +00002305bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src,
2306 SDValue &SrcMods,
2307 SDValue &Clamp,
2308 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002309 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
Matt Arsenault4831ce52015-01-06 23:00:37 +00002310 return SelectVOP3Mods(In, Src, SrcMods);
2311}
2312
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00002313bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src,
2314 SDValue &Clamp, SDValue &Omod) const {
2315 Src = In;
2316
2317 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002318 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2319 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00002320
2321 return true;
2322}
2323
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002324bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src,
2325 SDValue &SrcMods) const {
2326 unsigned Mods = 0;
2327 Src = In;
2328
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002329 if (Src.getOpcode() == ISD::FNEG) {
Matt Arsenault786eeea2017-05-17 20:00:00 +00002330 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI);
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002331 Src = Src.getOperand(0);
2332 }
2333
Matt Arsenault786eeea2017-05-17 20:00:00 +00002334 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2335 unsigned VecMods = Mods;
2336
Matt Arsenault98f29462017-05-17 20:30:58 +00002337 SDValue Lo = stripBitcast(Src.getOperand(0));
2338 SDValue Hi = stripBitcast(Src.getOperand(1));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002339
2340 if (Lo.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00002341 Lo = stripBitcast(Lo.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002342 Mods ^= SISrcMods::NEG;
2343 }
2344
2345 if (Hi.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00002346 Hi = stripBitcast(Hi.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002347 Mods ^= SISrcMods::NEG_HI;
2348 }
2349
Matt Arsenault98f29462017-05-17 20:30:58 +00002350 if (isExtractHiElt(Lo, Lo))
2351 Mods |= SISrcMods::OP_SEL_0;
2352
2353 if (isExtractHiElt(Hi, Hi))
2354 Mods |= SISrcMods::OP_SEL_1;
2355
2356 Lo = stripExtractLoElt(Lo);
2357 Hi = stripExtractLoElt(Hi);
2358
Matt Arsenault786eeea2017-05-17 20:00:00 +00002359 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) {
2360 // Really a scalar input. Just select from the low half of the register to
2361 // avoid packing.
2362
2363 Src = Lo;
2364 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2365 return true;
2366 }
2367
2368 Mods = VecMods;
2369 }
2370
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002371 // Packed instructions do not have abs modifiers.
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002372 Mods |= SISrcMods::OP_SEL_1;
2373
2374 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2375 return true;
2376}
2377
2378bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src,
2379 SDValue &SrcMods,
2380 SDValue &Clamp) const {
2381 SDLoc SL(In);
2382
2383 // FIXME: Handle clamp and op_sel
2384 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2385
2386 return SelectVOP3PMods(In, Src, SrcMods);
2387}
2388
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +00002389bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src,
2390 SDValue &SrcMods) const {
2391 Src = In;
2392 // FIXME: Handle op_sel
2393 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
2394 return true;
2395}
2396
2397bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src,
2398 SDValue &SrcMods,
2399 SDValue &Clamp) const {
2400 SDLoc SL(In);
2401
2402 // FIXME: Handle clamp
2403 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2404
2405 return SelectVOP3OpSel(In, Src, SrcMods);
2406}
2407
2408bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src,
2409 SDValue &SrcMods) const {
2410 // FIXME: Handle op_sel
2411 return SelectVOP3Mods(In, Src, SrcMods);
2412}
2413
2414bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src,
2415 SDValue &SrcMods,
2416 SDValue &Clamp) const {
2417 SDLoc SL(In);
2418
2419 // FIXME: Handle clamp
2420 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2421
2422 return SelectVOP3OpSelMods(In, Src, SrcMods);
2423}
2424
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002425// The return value is not whether the match is possible (which it always is),
2426// but whether or not it a conversion is really used.
2427bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src,
2428 unsigned &Mods) const {
2429 Mods = 0;
2430 SelectVOP3ModsImpl(In, Src, Mods);
2431
2432 if (Src.getOpcode() == ISD::FP_EXTEND) {
2433 Src = Src.getOperand(0);
2434 assert(Src.getValueType() == MVT::f16);
2435 Src = stripBitcast(Src);
2436
Matt Arsenault550c66d2017-10-13 20:45:49 +00002437 // Be careful about folding modifiers if we already have an abs. fneg is
2438 // applied last, so we don't want to apply an earlier fneg.
2439 if ((Mods & SISrcMods::ABS) == 0) {
2440 unsigned ModsTmp;
2441 SelectVOP3ModsImpl(Src, Src, ModsTmp);
2442
2443 if ((ModsTmp & SISrcMods::NEG) != 0)
2444 Mods ^= SISrcMods::NEG;
2445
2446 if ((ModsTmp & SISrcMods::ABS) != 0)
2447 Mods |= SISrcMods::ABS;
2448 }
2449
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002450 // op_sel/op_sel_hi decide the source type and source.
2451 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16.
2452 // If the sources's op_sel is set, it picks the high half of the source
2453 // register.
2454
2455 Mods |= SISrcMods::OP_SEL_1;
Matt Arsenault550c66d2017-10-13 20:45:49 +00002456 if (isExtractHiElt(Src, Src)) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002457 Mods |= SISrcMods::OP_SEL_0;
2458
Matt Arsenault550c66d2017-10-13 20:45:49 +00002459 // TODO: Should we try to look for neg/abs here?
2460 }
2461
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002462 return true;
2463 }
2464
2465 return false;
2466}
2467
Matt Arsenault76935122017-09-20 20:28:39 +00002468bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src,
2469 SDValue &SrcMods) const {
2470 unsigned Mods = 0;
2471 SelectVOP3PMadMixModsImpl(In, Src, Mods);
2472 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2473 return true;
2474}
2475
Matt Arsenaulte8c03a22019-03-08 20:58:11 +00002476SDValue AMDGPUDAGToDAGISel::getHi16Elt(SDValue In) const {
2477 if (In.isUndef())
2478 return CurDAG->getUNDEF(MVT::i32);
2479
2480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) {
2481 SDLoc SL(In);
2482 return CurDAG->getConstant(C->getZExtValue() << 16, SL, MVT::i32);
2483 }
2484
2485 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) {
2486 SDLoc SL(In);
2487 return CurDAG->getConstant(
2488 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32);
2489 }
2490
2491 SDValue Src;
2492 if (isExtractHiElt(In, Src))
2493 return Src;
2494
2495 return SDValue();
2496}
2497
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002498bool AMDGPUDAGToDAGISel::isVGPRImm(const SDNode * N) const {
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00002499 assert(CurDAG->getTarget().getTargetTriple().getArch() == Triple::amdgcn);
2500
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002501 const SIRegisterInfo *SIRI =
2502 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
2503 const SIInstrInfo * SII =
2504 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2505
2506 unsigned Limit = 0;
2507 bool AllUsesAcceptSReg = true;
2508 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
2509 Limit < 10 && U != E; ++U, ++Limit) {
2510 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
2511
2512 // If the register class is unknown, it could be an unknown
2513 // register class that needs to be an SGPR, e.g. an inline asm
2514 // constraint
2515 if (!RC || SIRI->isSGPRClass(RC))
2516 return false;
2517
2518 if (RC != &AMDGPU::VS_32RegClass) {
2519 AllUsesAcceptSReg = false;
2520 SDNode * User = *U;
2521 if (User->isMachineOpcode()) {
2522 unsigned Opc = User->getMachineOpcode();
2523 MCInstrDesc Desc = SII->get(Opc);
2524 if (Desc.isCommutable()) {
2525 unsigned OpIdx = Desc.getNumDefs() + U.getOperandNo();
2526 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
2527 if (SII->findCommutedOpIndices(Desc, OpIdx, CommuteIdx1)) {
2528 unsigned CommutedOpNo = CommuteIdx1 - Desc.getNumDefs();
2529 const TargetRegisterClass *CommutedRC = getOperandRegClass(*U, CommutedOpNo);
2530 if (CommutedRC == &AMDGPU::VS_32RegClass)
2531 AllUsesAcceptSReg = true;
2532 }
2533 }
2534 }
2535 // If "AllUsesAcceptSReg == false" so far we haven't suceeded
2536 // commuting current user. This means have at least one use
2537 // that strictly require VGPR. Thus, we will not attempt to commute
2538 // other user instructions.
2539 if (!AllUsesAcceptSReg)
2540 break;
2541 }
2542 }
2543 return !AllUsesAcceptSReg && (Limit < 10);
2544}
2545
Alexander Timofeev4d302f62018-09-13 09:06:56 +00002546bool AMDGPUDAGToDAGISel::isUniformLoad(const SDNode * N) const {
2547 auto Ld = cast<LoadSDNode>(N);
2548
2549 return Ld->getAlignment() >= 4 &&
2550 (
2551 (
2552 (
2553 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2554 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT
2555 )
2556 &&
2557 !N->isDivergent()
2558 )
2559 ||
2560 (
2561 Subtarget->getScalarizeGlobalBehavior() &&
2562 Ld->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
2563 !Ld->isVolatile() &&
2564 !N->isDivergent() &&
2565 static_cast<const SITargetLowering *>(
2566 getTargetLowering())->isMemOpHasNoClobberedMemOperand(N)
2567 )
2568 );
2569}
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002570
Christian Konigd910b7d2013-02-26 17:52:16 +00002571void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002572 const AMDGPUTargetLowering& Lowering =
Matt Arsenault209a7b92014-04-18 07:40:20 +00002573 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002574 bool IsModified = false;
2575 do {
2576 IsModified = false;
Matt Arsenault68f05052017-12-04 22:18:27 +00002577
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002578 // Go over all selected nodes and try to fold them a bit more
Matt Arsenault68f05052017-12-04 22:18:27 +00002579 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin();
2580 while (Position != CurDAG->allnodes_end()) {
2581 SDNode *Node = &*Position++;
2582 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002583 if (!MachineNode)
2584 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +00002585
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002586 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
Matt Arsenault68f05052017-12-04 22:18:27 +00002587 if (ResNode != Node) {
2588 if (ResNode)
2589 ReplaceUses(Node, ResNode);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002590 IsModified = true;
2591 }
Tom Stellard2183b702013-06-03 17:39:46 +00002592 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002593 CurDAG->RemoveDeadNodes();
2594 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +00002595}
Tom Stellard20287692017-08-08 04:57:55 +00002596
Tom Stellardc5a154d2018-06-28 23:47:12 +00002597bool R600DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
2598 Subtarget = &MF.getSubtarget<R600Subtarget>();
2599 return SelectionDAGISel::runOnMachineFunction(MF);
2600}
2601
2602bool R600DAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const {
2603 if (!N->readMem())
2604 return false;
2605 if (CbId == -1)
Matt Arsenault0da63502018-08-31 05:49:54 +00002606 return N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2607 N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002608
Matt Arsenault0da63502018-08-31 05:49:54 +00002609 return N->getAddressSpace() == AMDGPUAS::CONSTANT_BUFFER_0 + CbId;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002610}
2611
2612bool R600DAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
2613 SDValue& IntPtr) {
2614 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
2615 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr),
2616 true);
2617 return true;
2618 }
2619 return false;
2620}
2621
2622bool R600DAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
2623 SDValue& BaseReg, SDValue &Offset) {
2624 if (!isa<ConstantSDNode>(Addr)) {
2625 BaseReg = Addr;
2626 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true);
2627 return true;
2628 }
2629 return false;
2630}
2631
Tom Stellard20287692017-08-08 04:57:55 +00002632void R600DAGToDAGISel::Select(SDNode *N) {
2633 unsigned int Opc = N->getOpcode();
2634 if (N->isMachineOpcode()) {
2635 N->setNodeId(-1);
2636 return; // Already selected.
2637 }
2638
2639 switch (Opc) {
2640 default: break;
2641 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
2642 case ISD::SCALAR_TO_VECTOR:
2643 case ISD::BUILD_VECTOR: {
2644 EVT VT = N->getValueType(0);
2645 unsigned NumVectorElts = VT.getVectorNumElements();
2646 unsigned RegClassID;
2647 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
2648 // that adds a 128 bits reg copy when going through TwoAddressInstructions
2649 // pass. We want to avoid 128 bits copies as much as possible because they
2650 // can't be bundled by our scheduler.
2651 switch(NumVectorElts) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002652 case 2: RegClassID = R600::R600_Reg64RegClassID; break;
Tom Stellard20287692017-08-08 04:57:55 +00002653 case 4:
2654 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
Tom Stellardc5a154d2018-06-28 23:47:12 +00002655 RegClassID = R600::R600_Reg128VerticalRegClassID;
Tom Stellard20287692017-08-08 04:57:55 +00002656 else
Tom Stellardc5a154d2018-06-28 23:47:12 +00002657 RegClassID = R600::R600_Reg128RegClassID;
Tom Stellard20287692017-08-08 04:57:55 +00002658 break;
2659 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
2660 }
2661 SelectBuildVector(N, RegClassID);
2662 return;
2663 }
2664 }
2665
2666 SelectCode(N);
2667}
2668
2669bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
2670 SDValue &Offset) {
2671 ConstantSDNode *C;
2672 SDLoc DL(Addr);
2673
2674 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002675 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002676 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2677 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
2678 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002679 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002680 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2681 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
2682 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
2683 Base = Addr.getOperand(0);
2684 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2685 } else {
2686 Base = Addr;
2687 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
2688 }
2689
2690 return true;
2691}
2692
2693bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
2694 SDValue &Offset) {
2695 ConstantSDNode *IMMOffset;
2696
2697 if (Addr.getOpcode() == ISD::ADD
2698 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
2699 && isInt<16>(IMMOffset->getZExtValue())) {
2700
2701 Base = Addr.getOperand(0);
2702 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2703 MVT::i32);
2704 return true;
2705 // If the pointer address is constant, we can move it to the offset field.
2706 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
2707 && isInt<16>(IMMOffset->getZExtValue())) {
2708 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
2709 SDLoc(CurDAG->getEntryNode()),
Tom Stellardc5a154d2018-06-28 23:47:12 +00002710 R600::ZERO, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002711 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2712 MVT::i32);
2713 return true;
2714 }
2715
2716 // Default case, no offset
2717 Base = Addr;
2718 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
2719 return true;
2720}