blob: ef04732c981ce7c797128d20d39652a8568ec76a [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>();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000142 AU.addRequired<LegacyDivergenceAnalysis>();
Alexander Timofeev66ac6b42019-07-02 18:16:42 +0000143#ifdef EXPENSIVE_CHECKS
Alexander Timofeev2ce560f2019-07-02 17:59:44 +0000144 AU.addRequired<DominatorTreeWrapperPass>();
145 AU.addRequired<LoopInfoWrapperPass>();
146#endif
Matt Arsenault7016f132017-08-03 22:30:46 +0000147 SelectionDAGISel::getAnalysisUsage(AU);
148 }
149
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000150 bool matchLoadD16FromBuildVector(SDNode *N) const;
151
Eric Christopher7792e322015-01-30 23:24:40 +0000152 bool runOnMachineFunction(MachineFunction &MF) override;
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000153 void PreprocessISelDAG() override;
Justin Bogner95927c02016-05-12 21:03:32 +0000154 void Select(SDNode *N) override;
Mehdi Amini117296c2016-10-01 02:56:57 +0000155 StringRef getPassName() const override;
Craig Topper5656db42014-04-29 07:57:24 +0000156 void PostprocessISelDAG() override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000157
Tom Stellard20287692017-08-08 04:57:55 +0000158protected:
159 void SelectBuildVector(SDNode *N, unsigned RegClassID);
160
Tom Stellard75aadc22012-12-11 21:25:42 +0000161private:
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000162 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000163 bool isNoNanSrc(SDValue N) const;
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000164 bool isInlineImmediate(const SDNode *N, bool Negated = false) const;
165 bool isNegInlineImmediate(const SDNode *N) const {
166 return isInlineImmediate(N, true);
167 }
168
Alexander Timofeevdb7ee762018-09-11 11:56:50 +0000169 bool isVGPRImm(const SDNode *N) const;
Alexander Timofeev4d302f62018-09-13 09:06:56 +0000170 bool isUniformLoad(const SDNode *N) const;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000171 bool isUniformBr(const SDNode *N) const;
172
Tim Renouff1c7b922018-08-02 22:53:57 +0000173 MachineSDNode *buildSMovImm64(SDLoc &DL, uint64_t Val, EVT VT) const;
174
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000175 SDNode *glueCopyToM0LDSInit(SDNode *N) const;
176 SDNode *glueCopyToM0(SDNode *N, SDValue Val) const;
Tom Stellard381a94a2015-05-12 15:00:49 +0000177
Tom Stellarddf94dc32013-08-14 23:24:24 +0000178 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
Tom Stellard20287692017-08-08 04:57:55 +0000179 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
180 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000181 bool isDSOffsetLegal(SDValue Base, unsigned Offset,
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000182 unsigned OffsetBits) const;
183 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
Tom Stellardf3fc5552014-08-22 18:49:35 +0000184 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
185 SDValue &Offset1) const;
Changpeng Fangb41574a2015-12-22 20:55:23 +0000186 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard155bbb72014-08-11 22:18:17 +0000187 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
188 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000189 SDValue &TFE, SDValue &DLC) const;
Tom Stellard155bbb72014-08-11 22:18:17 +0000190 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard1f9939f2015-02-27 14:59:41 +0000191 SDValue &SOffset, SDValue &Offset, SDValue &GLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000192 SDValue &SLC, SDValue &TFE, SDValue &DLC) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000193 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +0000194 SDValue &VAddr, SDValue &SOffset, SDValue &Offset,
Tom Stellard7980fc82014-09-25 18:30:26 +0000195 SDValue &SLC) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000196 bool SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000197 SDValue Addr, SDValue &RSrc, SDValue &VAddr,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000198 SDValue &SOffset, SDValue &ImmOffset) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000199 bool SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000200 SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000201 SDValue &Offset) const;
202
Tom Stellard155bbb72014-08-11 22:18:17 +0000203 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
204 SDValue &Offset, SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000205 SDValue &TFE, SDValue &DLC) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000206 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault88701812016-06-09 23:42:48 +0000207 SDValue &Offset, SDValue &SLC) const;
Jan Vesely43b7b5b2016-04-07 19:23:11 +0000208 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
209 SDValue &Offset) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000210
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000211 bool SelectFlatAtomic(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000212 SDValue &Offset, SDValue &SLC) const;
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000213 bool SelectFlatAtomicSigned(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenault4e309b02017-07-29 01:03:53 +0000214 SDValue &Offset, SDValue &SLC) const;
215
216 template <bool IsSigned>
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000217 bool SelectFlatOffset(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000218 SDValue &Offset, SDValue &SLC) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000219
Tom Stellarddee26a22015-08-06 19:28:30 +0000220 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset,
221 bool &Imm) const;
Matt Arsenault923712b2018-02-09 16:57:57 +0000222 SDValue Expand32BitAddress(SDValue Addr) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000223 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset,
224 bool &Imm) const;
225 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000226 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000227 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
228 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000229 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const;
Nicolai Haehnle7968c342016-07-12 08:12:16 +0000230 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000231
232 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Jay Foad7816ad92019-07-12 15:02:59 +0000233 bool SelectVOP3Mods_f32(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);
Carl Ritson00e89b42019-07-26 09:54:12 +0000285 void SelectINTRINSIC_WO_CHAIN(SDNode *N);
Matt Arsenault4d55d022019-06-19 19:55:27 +0000286 void SelectINTRINSIC_VOID(SDNode *N);
Marek Olsak9b728682015-03-24 13:40:27 +0000287
Tom Stellard20287692017-08-08 04:57:55 +0000288protected:
Tom Stellard75aadc22012-12-11 21:25:42 +0000289 // Include the pieces autogenerated from the target description.
290#include "AMDGPUGenDAGISel.inc"
291};
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000292
Tom Stellard20287692017-08-08 04:57:55 +0000293class R600DAGToDAGISel : public AMDGPUDAGToDAGISel {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000294 const R600Subtarget *Subtarget;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000295
296 bool isConstantLoad(const MemSDNode *N, int cbID) const;
297 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
298 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
299 SDValue& Offset);
Tom Stellard20287692017-08-08 04:57:55 +0000300public:
301 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) :
Matt Arsenault0da63502018-08-31 05:49:54 +0000302 AMDGPUDAGToDAGISel(TM, OptLevel) {}
Tom Stellard20287692017-08-08 04:57:55 +0000303
304 void Select(SDNode *N) override;
305
306 bool SelectADDRIndirect(SDValue Addr, SDValue &Base,
307 SDValue &Offset) override;
308 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
309 SDValue &Offset) override;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000310
311 bool runOnMachineFunction(MachineFunction &MF) override;
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000312
313 void PreprocessISelDAG() override {}
314
Tom Stellardc5a154d2018-06-28 23:47:12 +0000315protected:
316 // Include the pieces autogenerated from the target description.
317#include "R600GenDAGISel.inc"
Tom Stellard20287692017-08-08 04:57:55 +0000318};
319
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000320static SDValue stripBitcast(SDValue Val) {
321 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
322}
323
324// Figure out if this is really an extract of the high 16-bits of a dword.
325static bool isExtractHiElt(SDValue In, SDValue &Out) {
326 In = stripBitcast(In);
327 if (In.getOpcode() != ISD::TRUNCATE)
328 return false;
329
330 SDValue Srl = In.getOperand(0);
331 if (Srl.getOpcode() == ISD::SRL) {
332 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
333 if (ShiftAmt->getZExtValue() == 16) {
334 Out = stripBitcast(Srl.getOperand(0));
335 return true;
336 }
337 }
338 }
339
340 return false;
341}
342
343// Look through operations that obscure just looking at the low 16-bits of the
344// same register.
345static SDValue stripExtractLoElt(SDValue In) {
346 if (In.getOpcode() == ISD::TRUNCATE) {
347 SDValue Src = In.getOperand(0);
348 if (Src.getValueType().getSizeInBits() == 32)
349 return stripBitcast(Src);
350 }
351
352 return In;
353}
354
Tom Stellard75aadc22012-12-11 21:25:42 +0000355} // end anonymous namespace
356
Fangrui Song3d76d362018-10-03 03:38:22 +0000357INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "amdgpu-isel",
Matt Arsenault7016f132017-08-03 22:30:46 +0000358 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
359INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo)
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000360INITIALIZE_PASS_DEPENDENCY(AMDGPUPerfHintAnalysis)
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000361INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
Alexander Timofeev2ce560f2019-07-02 17:59:44 +0000362#ifdef EXPENSIVE_CHECKS
363INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
364INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
365#endif
Fangrui Song3d76d362018-10-03 03:38:22 +0000366INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "amdgpu-isel",
Matt Arsenault7016f132017-08-03 22:30:46 +0000367 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
368
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000369/// This pass converts a legalized DAG into a AMDGPU-specific
Tom Stellard75aadc22012-12-11 21:25:42 +0000370// DAG, ready for instruction scheduling.
Matt Arsenault7016f132017-08-03 22:30:46 +0000371FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM,
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +0000372 CodeGenOpt::Level OptLevel) {
373 return new AMDGPUDAGToDAGISel(TM, OptLevel);
Tom Stellard75aadc22012-12-11 21:25:42 +0000374}
375
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000376/// This pass converts a legalized DAG into a R600-specific
Tom Stellard20287692017-08-08 04:57:55 +0000377// DAG, ready for instruction scheduling.
378FunctionPass *llvm::createR600ISelDag(TargetMachine *TM,
379 CodeGenOpt::Level OptLevel) {
380 return new R600DAGToDAGISel(TM, OptLevel);
381}
382
Eric Christopher7792e322015-01-30 23:24:40 +0000383bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Alexander Timofeev2ce560f2019-07-02 17:59:44 +0000384#ifdef EXPENSIVE_CHECKS
385 DominatorTree & DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
386 LoopInfo * LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
387 for (auto &L : LI->getLoopsInPreorder()) {
388 assert(L->isLCSSAForm(DT));
389 }
390#endif
Tom Stellard5bfbae52018-07-11 20:59:01 +0000391 Subtarget = &MF.getSubtarget<GCNSubtarget>();
Eric Christopher7792e322015-01-30 23:24:40 +0000392 return SelectionDAGISel::runOnMachineFunction(MF);
Tom Stellard75aadc22012-12-11 21:25:42 +0000393}
394
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000395bool AMDGPUDAGToDAGISel::matchLoadD16FromBuildVector(SDNode *N) const {
396 assert(Subtarget->d16PreservesUnusedBits());
397 MVT VT = N->getValueType(0).getSimpleVT();
398 if (VT != MVT::v2i16 && VT != MVT::v2f16)
399 return false;
400
401 SDValue Lo = N->getOperand(0);
402 SDValue Hi = N->getOperand(1);
403
404 LoadSDNode *LdHi = dyn_cast<LoadSDNode>(stripBitcast(Hi));
405
406 // build_vector lo, (load ptr) -> load_d16_hi ptr, lo
407 // build_vector lo, (zextload ptr from i8) -> load_d16_hi_u8 ptr, lo
408 // build_vector lo, (sextload ptr from i8) -> load_d16_hi_i8 ptr, lo
409
410 // Need to check for possible indirect dependencies on the other half of the
411 // vector to avoid introducing a cycle.
412 if (LdHi && Hi.hasOneUse() && !LdHi->isPredecessorOf(Lo.getNode())) {
413 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
414
415 SDValue TiedIn = CurDAG->getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Lo);
416 SDValue Ops[] = {
417 LdHi->getChain(), LdHi->getBasePtr(), TiedIn
418 };
419
420 unsigned LoadOp = AMDGPUISD::LOAD_D16_HI;
421 if (LdHi->getMemoryVT() == MVT::i8) {
422 LoadOp = LdHi->getExtensionType() == ISD::SEXTLOAD ?
423 AMDGPUISD::LOAD_D16_HI_I8 : AMDGPUISD::LOAD_D16_HI_U8;
424 } else {
425 assert(LdHi->getMemoryVT() == MVT::i16);
426 }
427
428 SDValue NewLoadHi =
429 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdHi), VTList,
430 Ops, LdHi->getMemoryVT(),
431 LdHi->getMemOperand());
432
433 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadHi);
434 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdHi, 1), NewLoadHi.getValue(1));
435 return true;
436 }
437
438 // build_vector (load ptr), hi -> load_d16_lo ptr, hi
439 // build_vector (zextload ptr from i8), hi -> load_d16_lo_u8 ptr, hi
440 // build_vector (sextload ptr from i8), hi -> load_d16_lo_i8 ptr, hi
441 LoadSDNode *LdLo = dyn_cast<LoadSDNode>(stripBitcast(Lo));
442 if (LdLo && Lo.hasOneUse()) {
443 SDValue TiedIn = getHi16Elt(Hi);
444 if (!TiedIn || LdLo->isPredecessorOf(TiedIn.getNode()))
445 return false;
446
447 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
448 unsigned LoadOp = AMDGPUISD::LOAD_D16_LO;
449 if (LdLo->getMemoryVT() == MVT::i8) {
450 LoadOp = LdLo->getExtensionType() == ISD::SEXTLOAD ?
451 AMDGPUISD::LOAD_D16_LO_I8 : AMDGPUISD::LOAD_D16_LO_U8;
452 } else {
453 assert(LdLo->getMemoryVT() == MVT::i16);
454 }
455
456 TiedIn = CurDAG->getNode(ISD::BITCAST, SDLoc(N), VT, TiedIn);
457
458 SDValue Ops[] = {
459 LdLo->getChain(), LdLo->getBasePtr(), TiedIn
460 };
461
462 SDValue NewLoadLo =
463 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdLo), VTList,
464 Ops, LdLo->getMemoryVT(),
465 LdLo->getMemOperand());
466
467 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadLo);
468 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdLo, 1), NewLoadLo.getValue(1));
469 return true;
470 }
471
472 return false;
473}
474
475void AMDGPUDAGToDAGISel::PreprocessISelDAG() {
476 if (!Subtarget->d16PreservesUnusedBits())
477 return;
478
479 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
480
481 bool MadeChange = false;
482 while (Position != CurDAG->allnodes_begin()) {
483 SDNode *N = &*--Position;
484 if (N->use_empty())
485 continue;
486
487 switch (N->getOpcode()) {
488 case ISD::BUILD_VECTOR:
489 MadeChange |= matchLoadD16FromBuildVector(N);
490 break;
491 default:
492 break;
493 }
494 }
495
496 if (MadeChange) {
497 CurDAG->RemoveDeadNodes();
498 LLVM_DEBUG(dbgs() << "After PreProcess:\n";
499 CurDAG->dump(););
500 }
501}
502
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000503bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const {
504 if (TM.Options.NoNaNsFPMath)
505 return true;
506
507 // TODO: Move into isKnownNeverNaN
Amara Emersond28f0cd42017-05-01 15:17:51 +0000508 if (N->getFlags().isDefined())
509 return N->getFlags().hasNoNaNs();
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000510
511 return CurDAG->isKnownNeverNaN(N);
512}
513
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000514bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N,
515 bool Negated) const {
Matt Arsenaultb7f87c02019-06-20 16:01:09 +0000516 if (N->isUndef())
517 return true;
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000518
Tom Stellardc5a154d2018-06-28 23:47:12 +0000519 const SIInstrInfo *TII = Subtarget->getInstrInfo();
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000520 if (Negated) {
521 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
522 return TII->isInlineConstant(-C->getAPIntValue());
Matt Arsenaultfe267752016-07-28 00:32:02 +0000523
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000524 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
525 return TII->isInlineConstant(-C->getValueAPF().bitcastToAPInt());
Matt Arsenaultfe267752016-07-28 00:32:02 +0000526
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000527 } else {
528 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
529 return TII->isInlineConstant(C->getAPIntValue());
530
531 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
532 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt());
533 }
Matt Arsenaultfe267752016-07-28 00:32:02 +0000534
535 return false;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000536}
537
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000538/// Determine the register class for \p OpNo
Tom Stellarddf94dc32013-08-14 23:24:24 +0000539/// \returns The register class of the virtual register that will be used for
540/// the given operand number \OpNo or NULL if the register class cannot be
541/// determined.
542const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
543 unsigned OpNo) const {
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000544 if (!N->isMachineOpcode()) {
545 if (N->getOpcode() == ISD::CopyToReg) {
546 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
547 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
548 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
549 return MRI.getRegClass(Reg);
550 }
551
552 const SIRegisterInfo *TRI
Tom Stellard5bfbae52018-07-11 20:59:01 +0000553 = static_cast<const GCNSubtarget *>(Subtarget)->getRegisterInfo();
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000554 return TRI->getPhysRegClass(Reg);
555 }
556
Matt Arsenault209a7b92014-04-18 07:40:20 +0000557 return nullptr;
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000558 }
Matt Arsenault209a7b92014-04-18 07:40:20 +0000559
Tom Stellarddf94dc32013-08-14 23:24:24 +0000560 switch (N->getMachineOpcode()) {
561 default: {
Eric Christopherd9134482014-08-04 21:25:23 +0000562 const MCInstrDesc &Desc =
Eric Christopher7792e322015-01-30 23:24:40 +0000563 Subtarget->getInstrInfo()->get(N->getMachineOpcode());
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000564 unsigned OpIdx = Desc.getNumDefs() + OpNo;
565 if (OpIdx >= Desc.getNumOperands())
Matt Arsenault209a7b92014-04-18 07:40:20 +0000566 return nullptr;
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000567 int RegClass = Desc.OpInfo[OpIdx].RegClass;
Matt Arsenault209a7b92014-04-18 07:40:20 +0000568 if (RegClass == -1)
569 return nullptr;
570
Eric Christopher7792e322015-01-30 23:24:40 +0000571 return Subtarget->getRegisterInfo()->getRegClass(RegClass);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000572 }
573 case AMDGPU::REG_SEQUENCE: {
Matt Arsenault209a7b92014-04-18 07:40:20 +0000574 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christopherd9134482014-08-04 21:25:23 +0000575 const TargetRegisterClass *SuperRC =
Eric Christopher7792e322015-01-30 23:24:40 +0000576 Subtarget->getRegisterInfo()->getRegClass(RCID);
Matt Arsenault209a7b92014-04-18 07:40:20 +0000577
578 SDValue SubRegOp = N->getOperand(OpNo + 1);
579 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
Eric Christopher7792e322015-01-30 23:24:40 +0000580 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC,
581 SubRegIdx);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000582 }
583 }
584}
585
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000586SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N, SDValue Val) const {
Tom Stellard381a94a2015-05-12 15:00:49 +0000587 const SITargetLowering& Lowering =
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000588 *static_cast<const SITargetLowering*>(getTargetLowering());
Tom Stellard381a94a2015-05-12 15:00:49 +0000589
Matt Arsenault5a86dbc2019-06-14 13:33:36 +0000590 assert(N->getOperand(0).getValueType() == MVT::Other && "Expected chain");
591
592 SDValue M0 = Lowering.copyToM0(*CurDAG, N->getOperand(0), SDLoc(N),
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000593 Val);
Tom Stellard381a94a2015-05-12 15:00:49 +0000594
595 SDValue Glue = M0.getValue(1);
596
597 SmallVector <SDValue, 8> Ops;
Matt Arsenault5a86dbc2019-06-14 13:33:36 +0000598 Ops.push_back(M0); // Replace the chain.
599 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000600 Ops.push_back(N->getOperand(i));
601
Tom Stellard381a94a2015-05-12 15:00:49 +0000602 Ops.push_back(Glue);
Matt Arsenaulte6667de2017-12-04 22:18:22 +0000603 return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops);
Tom Stellard381a94a2015-05-12 15:00:49 +0000604}
605
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000606SDNode *AMDGPUDAGToDAGISel::glueCopyToM0LDSInit(SDNode *N) const {
Nicolai Haehnle4dc3b2b2019-07-01 17:17:45 +0000607 unsigned AS = cast<MemSDNode>(N)->getAddressSpace();
608 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
609 if (Subtarget->ldsRequiresM0Init())
610 return glueCopyToM0(N, CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32));
611 } else if (AS == AMDGPUAS::REGION_ADDRESS) {
612 MachineFunction &MF = CurDAG->getMachineFunction();
613 unsigned Value = MF.getInfo<SIMachineFunctionInfo>()->getGDSSize();
614 return
615 glueCopyToM0(N, CurDAG->getTargetConstant(Value, SDLoc(N), MVT::i32));
616 }
617 return N;
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000618}
619
Tim Renouff1c7b922018-08-02 22:53:57 +0000620MachineSDNode *AMDGPUDAGToDAGISel::buildSMovImm64(SDLoc &DL, uint64_t Imm,
621 EVT VT) const {
622 SDNode *Lo = CurDAG->getMachineNode(
623 AMDGPU::S_MOV_B32, DL, MVT::i32,
Matt Arsenault06eed422019-07-17 15:35:36 +0000624 CurDAG->getTargetConstant(Imm & 0xFFFFFFFF, DL, MVT::i32));
Tim Renouff1c7b922018-08-02 22:53:57 +0000625 SDNode *Hi =
626 CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
Matt Arsenault06eed422019-07-17 15:35:36 +0000627 CurDAG->getTargetConstant(Imm >> 32, DL, MVT::i32));
Tim Renouff1c7b922018-08-02 22:53:57 +0000628 const SDValue Ops[] = {
629 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
630 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
631 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)};
632
633 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops);
634}
635
Matt Arsenault61cb6fa2015-11-11 00:01:36 +0000636static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) {
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000637 switch (NumVectorElts) {
638 case 1:
Marek Olsak79c05872016-11-25 17:37:09 +0000639 return AMDGPU::SReg_32_XM0RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000640 case 2:
641 return AMDGPU::SReg_64RegClassID;
Tim Renouf361b5b22019-03-21 12:01:21 +0000642 case 3:
643 return AMDGPU::SGPR_96RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000644 case 4:
645 return AMDGPU::SReg_128RegClassID;
Tim Renouf033f99a2019-03-22 10:11:21 +0000646 case 5:
647 return AMDGPU::SGPR_160RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000648 case 8:
649 return AMDGPU::SReg_256RegClassID;
650 case 16:
651 return AMDGPU::SReg_512RegClassID;
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000652 case 32:
653 return AMDGPU::SReg_1024RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000654 }
655
656 llvm_unreachable("invalid vector size");
657}
658
Tom Stellard20287692017-08-08 04:57:55 +0000659void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) {
Tom Stellard20287692017-08-08 04:57:55 +0000660 EVT VT = N->getValueType(0);
661 unsigned NumVectorElts = VT.getVectorNumElements();
662 EVT EltVT = VT.getVectorElementType();
Tom Stellard20287692017-08-08 04:57:55 +0000663 SDLoc DL(N);
664 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
665
666 if (NumVectorElts == 1) {
667 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0),
668 RegClass);
669 return;
670 }
671
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000672 assert(NumVectorElts <= 32 && "Vectors with more than 32 elements not "
Tom Stellard20287692017-08-08 04:57:55 +0000673 "supported yet");
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000674 // 32 = Max Num Vector Elements
Tom Stellard20287692017-08-08 04:57:55 +0000675 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
676 // 1 = Vector Register Class
Stanislav Mekhanoshine67cc382019-07-11 21:19:33 +0000677 SmallVector<SDValue, 32 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
Tom Stellard20287692017-08-08 04:57:55 +0000678
679 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
680 bool IsRegSeq = true;
681 unsigned NOps = N->getNumOperands();
682 for (unsigned i = 0; i < NOps; i++) {
683 // XXX: Why is this here?
684 if (isa<RegisterSDNode>(N->getOperand(i))) {
685 IsRegSeq = false;
686 break;
687 }
Simon Pilgrimede0e402018-05-19 12:46:02 +0000688 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
Tom Stellard20287692017-08-08 04:57:55 +0000689 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
Simon Pilgrimede0e402018-05-19 12:46:02 +0000690 RegSeqArgs[1 + (2 * i) + 1] = CurDAG->getTargetConstant(Sub, DL, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +0000691 }
692 if (NOps != NumVectorElts) {
693 // Fill in the missing undef elements if this was a scalar_to_vector.
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000694 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
Tom Stellard20287692017-08-08 04:57:55 +0000695 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
696 DL, EltVT);
697 for (unsigned i = NOps; i < NumVectorElts; ++i) {
Simon Pilgrimede0e402018-05-19 12:46:02 +0000698 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
Tom Stellard20287692017-08-08 04:57:55 +0000699 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
700 RegSeqArgs[1 + (2 * i) + 1] =
Simon Pilgrimede0e402018-05-19 12:46:02 +0000701 CurDAG->getTargetConstant(Sub, DL, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +0000702 }
703 }
704
705 if (!IsRegSeq)
706 SelectCode(N);
707 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs);
708}
709
Justin Bogner95927c02016-05-12 21:03:32 +0000710void AMDGPUDAGToDAGISel::Select(SDNode *N) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000711 unsigned int Opc = N->getOpcode();
712 if (N->isMachineOpcode()) {
Tim Northover31d093c2013-09-22 08:21:56 +0000713 N->setNodeId(-1);
Justin Bogner95927c02016-05-12 21:03:32 +0000714 return; // Already selected.
Tom Stellard75aadc22012-12-11 21:25:42 +0000715 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000716
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000717 if (isa<AtomicSDNode>(N) ||
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000718 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC ||
Matt Arsenaulta5840c32019-01-22 18:36:06 +0000719 Opc == ISD::ATOMIC_LOAD_FADD ||
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000720 Opc == AMDGPUISD::ATOMIC_LOAD_FMIN ||
721 Opc == AMDGPUISD::ATOMIC_LOAD_FMAX))
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000722 N = glueCopyToM0LDSInit(N);
Tom Stellard381a94a2015-05-12 15:00:49 +0000723
Tom Stellard75aadc22012-12-11 21:25:42 +0000724 switch (Opc) {
Matt Arsenault84445dd2017-11-30 22:51:26 +0000725 default:
726 break;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000727 // We are selecting i64 ADD here instead of custom lower it during
728 // DAG legalization, so we can fold some i64 ADDs used for address
729 // calculation into the LOAD and STORE instructions.
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000730 case ISD::ADDC:
731 case ISD::ADDE:
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000732 case ISD::SUBC:
733 case ISD::SUBE: {
Tom Stellard20287692017-08-08 04:57:55 +0000734 if (N->getValueType(0) != MVT::i64)
Tom Stellard1f15bff2014-02-25 21:36:18 +0000735 break;
736
Justin Bogner95927c02016-05-12 21:03:32 +0000737 SelectADD_SUB_I64(N);
738 return;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000739 }
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000740 case ISD::ADDCARRY:
741 case ISD::SUBCARRY:
742 if (N->getValueType(0) != MVT::i32)
743 break;
744
745 SelectAddcSubb(N);
746 return;
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000747 case ISD::UADDO:
748 case ISD::USUBO: {
749 SelectUADDO_USUBO(N);
750 return;
751 }
Tom Stellard8485fa02016-12-07 02:42:15 +0000752 case AMDGPUISD::FMUL_W_CHAIN: {
753 SelectFMUL_W_CHAIN(N);
754 return;
755 }
756 case AMDGPUISD::FMA_W_CHAIN: {
757 SelectFMA_W_CHAIN(N);
758 return;
759 }
760
Matt Arsenault064c2062014-06-11 17:40:32 +0000761 case ISD::SCALAR_TO_VECTOR:
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000762 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000763 EVT VT = N->getValueType(0);
764 unsigned NumVectorElts = VT.getVectorNumElements();
Matt Arsenault5a4ec812018-06-20 19:45:48 +0000765 if (VT.getScalarSizeInBits() == 16) {
766 if (Opc == ISD::BUILD_VECTOR && NumVectorElts == 2) {
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000767 if (SDNode *Packed = packConstantV2I16(N, *CurDAG)) {
768 ReplaceNode(N, Packed);
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000769 return;
770 }
771 }
772
773 break;
774 }
775
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000776 assert(VT.getVectorElementType().bitsEq(MVT::i32));
Tom Stellard20287692017-08-08 04:57:55 +0000777 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts);
778 SelectBuildVector(N, RegClassID);
Justin Bogner95927c02016-05-12 21:03:32 +0000779 return;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000780 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000781 case ISD::BUILD_PAIR: {
782 SDValue RC, SubReg0, SubReg1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000783 SDLoc DL(N);
Tom Stellard754f80f2013-04-05 23:31:51 +0000784 if (N->getValueType(0) == MVT::i128) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000785 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32);
786 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32);
787 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000788 } else if (N->getValueType(0) == MVT::i64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000789 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32);
790 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
791 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000792 } else {
793 llvm_unreachable("Unhandled value type for BUILD_PAIR");
794 }
795 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
796 N->getOperand(1), SubReg1 };
Justin Bogner95927c02016-05-12 21:03:32 +0000797 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
798 N->getValueType(0), Ops));
799 return;
Tom Stellard754f80f2013-04-05 23:31:51 +0000800 }
Tom Stellard7ed0b522014-04-03 20:19:27 +0000801
802 case ISD::Constant:
803 case ISD::ConstantFP: {
Tom Stellard20287692017-08-08 04:57:55 +0000804 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
Tom Stellard7ed0b522014-04-03 20:19:27 +0000805 break;
806
807 uint64_t Imm;
808 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
809 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
810 else {
Tom Stellard3cbe0142014-04-07 19:31:13 +0000811 ConstantSDNode *C = cast<ConstantSDNode>(N);
Tom Stellard7ed0b522014-04-03 20:19:27 +0000812 Imm = C->getZExtValue();
813 }
814
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000815 SDLoc DL(N);
Tim Renouff1c7b922018-08-02 22:53:57 +0000816 ReplaceNode(N, buildSMovImm64(DL, Imm, N->getValueType(0)));
Justin Bogner95927c02016-05-12 21:03:32 +0000817 return;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000818 }
Matt Arsenault4bf43d42015-09-25 17:27:08 +0000819 case ISD::LOAD:
Matt Arsenault3f8e7a32018-06-22 08:39:52 +0000820 case ISD::STORE:
821 case ISD::ATOMIC_LOAD:
822 case ISD::ATOMIC_STORE: {
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000823 N = glueCopyToM0LDSInit(N);
Tom Stellard096b8c12015-02-04 20:49:49 +0000824 break;
825 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000826
827 case AMDGPUISD::BFE_I32:
828 case AMDGPUISD::BFE_U32: {
Matt Arsenault78b86702014-04-18 05:19:26 +0000829 // There is a scalar version available, but unlike the vector version which
830 // has a separate operand for the offset and width, the scalar version packs
831 // the width and offset into a single operand. Try to move to the scalar
832 // version if the offsets are constant, so that we can try to keep extended
833 // loads of kernel arguments in SGPRs.
834
835 // TODO: Technically we could try to pattern match scalar bitshifts of
836 // dynamic values, but it's probably not useful.
837 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
838 if (!Offset)
839 break;
840
841 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
842 if (!Width)
843 break;
844
845 bool Signed = Opc == AMDGPUISD::BFE_I32;
846
Matt Arsenault78b86702014-04-18 05:19:26 +0000847 uint32_t OffsetVal = Offset->getZExtValue();
848 uint32_t WidthVal = Width->getZExtValue();
849
Justin Bogner95927c02016-05-12 21:03:32 +0000850 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
851 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal));
852 return;
Matt Arsenault78b86702014-04-18 05:19:26 +0000853 }
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000854 case AMDGPUISD::DIV_SCALE: {
Justin Bogner95927c02016-05-12 21:03:32 +0000855 SelectDIV_SCALE(N);
856 return;
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000857 }
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000858 case AMDGPUISD::DIV_FMAS: {
859 SelectDIV_FMAS(N);
860 return;
861 }
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000862 case AMDGPUISD::MAD_I64_I32:
863 case AMDGPUISD::MAD_U64_U32: {
864 SelectMAD_64_32(N);
865 return;
866 }
Tom Stellard3457a842014-10-09 19:06:00 +0000867 case ISD::CopyToReg: {
868 const SITargetLowering& Lowering =
869 *static_cast<const SITargetLowering*>(getTargetLowering());
Matt Arsenault0d0d6c22017-04-12 21:58:23 +0000870 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG);
Tom Stellard3457a842014-10-09 19:06:00 +0000871 break;
872 }
Marek Olsak9b728682015-03-24 13:40:27 +0000873 case ISD::AND:
874 case ISD::SRL:
875 case ISD::SRA:
Matt Arsenault7e8de012016-04-22 22:59:16 +0000876 case ISD::SIGN_EXTEND_INREG:
Tom Stellard20287692017-08-08 04:57:55 +0000877 if (N->getValueType(0) != MVT::i32)
Marek Olsak9b728682015-03-24 13:40:27 +0000878 break;
879
Justin Bogner95927c02016-05-12 21:03:32 +0000880 SelectS_BFE(N);
881 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000882 case ISD::BRCOND:
Justin Bogner95927c02016-05-12 21:03:32 +0000883 SelectBRCOND(N);
884 return;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000885 case ISD::FMAD:
Matt Arsenault0084adc2018-04-30 19:08:16 +0000886 case ISD::FMA:
887 SelectFMAD_FMA(N);
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000888 return;
Matt Arsenault88701812016-06-09 23:42:48 +0000889 case AMDGPUISD::ATOMIC_CMP_SWAP:
890 SelectATOMIC_CMP_SWAP(N);
891 return;
Matt Arsenault709374d2018-08-01 20:13:58 +0000892 case AMDGPUISD::CVT_PKRTZ_F16_F32:
893 case AMDGPUISD::CVT_PKNORM_I16_F32:
894 case AMDGPUISD::CVT_PKNORM_U16_F32:
895 case AMDGPUISD::CVT_PK_U16_U32:
896 case AMDGPUISD::CVT_PK_I16_I32: {
897 // Hack around using a legal type if f16 is illegal.
898 if (N->getValueType(0) == MVT::i32) {
899 MVT NewVT = Opc == AMDGPUISD::CVT_PKRTZ_F16_F32 ? MVT::v2f16 : MVT::v2i16;
900 N = CurDAG->MorphNodeTo(N, N->getOpcode(), CurDAG->getVTList(NewVT),
901 { N->getOperand(0), N->getOperand(1) });
902 SelectCode(N);
903 return;
904 }
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000905
906 break;
907 }
908 case ISD::INTRINSIC_W_CHAIN: {
909 SelectINTRINSIC_W_CHAIN(N);
910 return;
Matt Arsenault709374d2018-08-01 20:13:58 +0000911 }
Carl Ritson00e89b42019-07-26 09:54:12 +0000912 case ISD::INTRINSIC_WO_CHAIN: {
913 SelectINTRINSIC_WO_CHAIN(N);
914 return;
915 }
Matt Arsenault4d55d022019-06-19 19:55:27 +0000916 case ISD::INTRINSIC_VOID: {
917 SelectINTRINSIC_VOID(N);
918 return;
919 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000920 }
Tom Stellard3457a842014-10-09 19:06:00 +0000921
Justin Bogner95927c02016-05-12 21:03:32 +0000922 SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000923}
924
Tom Stellardbc4497b2016-02-12 23:45:29 +0000925bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const {
926 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000927 const Instruction *Term = BB->getTerminator();
928 return Term->getMetadata("amdgpu.uniform") ||
929 Term->getMetadata("structurizecfg.uniform");
Tom Stellardbc4497b2016-02-12 23:45:29 +0000930}
931
Mehdi Amini117296c2016-10-01 02:56:57 +0000932StringRef AMDGPUDAGToDAGISel::getPassName() const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000933 return "AMDGPU DAG->DAG Pattern Instruction Selection";
934}
935
Tom Stellard41fc7852013-07-23 01:48:42 +0000936//===----------------------------------------------------------------------===//
937// Complex Patterns
938//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000939
Tom Stellard75aadc22012-12-11 21:25:42 +0000940bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
Tom Stellard20287692017-08-08 04:57:55 +0000941 SDValue &Offset) {
942 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000943}
944
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000945bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
946 SDValue &Offset) {
947 ConstantSDNode *C;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000948 SDLoc DL(Addr);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000949
950 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000951 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000952 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000953 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
954 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000955 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000956 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000957 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
958 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
959 Base = Addr.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000960 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000961 } else {
962 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000963 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000964 }
965
966 return true;
967}
Christian Konigd910b7d2013-02-26 17:52:16 +0000968
Matt Arsenault84445dd2017-11-30 22:51:26 +0000969// FIXME: Should only handle addcarry/subcarry
Justin Bogner95927c02016-05-12 21:03:32 +0000970void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000971 SDLoc DL(N);
972 SDValue LHS = N->getOperand(0);
973 SDValue RHS = N->getOperand(1);
974
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000975 unsigned Opcode = N->getOpcode();
976 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE);
977 bool ProduceCarry =
978 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC;
Matt Arsenault84445dd2017-11-30 22:51:26 +0000979 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000980
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000981 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
982 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000983
984 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
985 DL, MVT::i32, LHS, Sub0);
986 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
987 DL, MVT::i32, LHS, Sub1);
988
989 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
990 DL, MVT::i32, RHS, Sub0);
991 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
992 DL, MVT::i32, RHS, Sub1);
993
994 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000995
Tom Stellard80942a12014-09-05 14:07:59 +0000996 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000997 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
998
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000999 SDNode *AddLo;
1000 if (!ConsumeCarry) {
1001 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
1002 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args);
1003 } else {
1004 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) };
1005 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args);
1006 }
1007 SDValue AddHiArgs[] = {
1008 SDValue(Hi0, 0),
1009 SDValue(Hi1, 0),
1010 SDValue(AddLo, 1)
1011 };
1012 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs);
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001013
Nicolai Haehnle67624af2016-10-14 10:30:00 +00001014 SDValue RegSequenceArgs[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001015 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001016 SDValue(AddLo,0),
1017 Sub0,
1018 SDValue(AddHi,0),
1019 Sub1,
1020 };
Nicolai Haehnle67624af2016-10-14 10:30:00 +00001021 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
1022 MVT::i64, RegSequenceArgs);
1023
1024 if (ProduceCarry) {
1025 // Replace the carry-use
Nirav Dave3264c1b2018-03-19 20:19:46 +00001026 ReplaceUses(SDValue(N, 1), SDValue(AddHi, 1));
Nicolai Haehnle67624af2016-10-14 10:30:00 +00001027 }
1028
1029 // Replace the remaining uses.
Nirav Dave3264c1b2018-03-19 20:19:46 +00001030 ReplaceNode(N, RegSequence);
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001031}
1032
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001033void AMDGPUDAGToDAGISel::SelectAddcSubb(SDNode *N) {
1034 SDLoc DL(N);
1035 SDValue LHS = N->getOperand(0);
1036 SDValue RHS = N->getOperand(1);
1037 SDValue CI = N->getOperand(2);
1038
1039 unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::V_ADDC_U32_e64
1040 : AMDGPU::V_SUBB_U32_e64;
1041 CurDAG->SelectNodeTo(
1042 N, Opc, N->getVTList(),
1043 {LHS, RHS, CI, CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
1044}
1045
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +00001046void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) {
1047 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned
1048 // carry out despite the _i32 name. These were renamed in VI to _U32.
1049 // FIXME: We should probably rename the opcodes here.
1050 unsigned Opc = N->getOpcode() == ISD::UADDO ?
1051 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64;
1052
Michael Liaoeea51772019-03-20 20:18:56 +00001053 CurDAG->SelectNodeTo(
1054 N, Opc, N->getVTList(),
1055 {N->getOperand(0), N->getOperand(1),
1056 CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +00001057}
1058
Tom Stellard8485fa02016-12-07 02:42:15 +00001059void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) {
1060 SDLoc SL(N);
1061 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod
1062 SDValue Ops[10];
1063
1064 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]);
1065 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1066 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]);
1067 Ops[8] = N->getOperand(0);
1068 Ops[9] = N->getOperand(4);
1069
1070 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops);
1071}
1072
1073void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) {
1074 SDLoc SL(N);
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001075 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod
Tom Stellard8485fa02016-12-07 02:42:15 +00001076 SDValue Ops[8];
1077
1078 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]);
1079 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1080 Ops[6] = N->getOperand(0);
1081 Ops[7] = N->getOperand(3);
1082
1083 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops);
1084}
1085
Matt Arsenault044f1d12015-02-14 04:24:28 +00001086// We need to handle this here because tablegen doesn't support matching
1087// instructions with multiple outputs.
Justin Bogner95927c02016-05-12 21:03:32 +00001088void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +00001089 SDLoc SL(N);
1090 EVT VT = N->getValueType(0);
1091
1092 assert(VT == MVT::f32 || VT == MVT::f64);
1093
1094 unsigned Opc
1095 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
1096
Matt Arsenault3b99f122017-01-19 06:04:12 +00001097 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1098 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +00001099}
1100
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001101void AMDGPUDAGToDAGISel::SelectDIV_FMAS(SDNode *N) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001102 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
1103 const SIRegisterInfo *TRI = ST->getRegisterInfo();
1104
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001105 SDLoc SL(N);
1106 EVT VT = N->getValueType(0);
1107
1108 assert(VT == MVT::f32 || VT == MVT::f64);
1109
1110 unsigned Opc
1111 = (VT == MVT::f64) ? AMDGPU::V_DIV_FMAS_F64 : AMDGPU::V_DIV_FMAS_F32;
1112
1113 SDValue CarryIn = N->getOperand(3);
1114 // V_DIV_FMAS implicitly reads VCC.
1115 SDValue VCC = CurDAG->getCopyToReg(CurDAG->getEntryNode(), SL,
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001116 TRI->getVCC(), CarryIn, SDValue());
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001117
1118 SDValue Ops[10];
1119
1120 SelectVOP3Mods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]);
1121 SelectVOP3Mods(N->getOperand(1), Ops[3], Ops[2]);
1122 SelectVOP3Mods(N->getOperand(2), Ops[5], Ops[4]);
1123
1124 Ops[8] = VCC;
1125 Ops[9] = VCC.getValue(1);
1126
1127 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1128}
1129
Matt Arsenault4f6318f2017-11-06 17:04:37 +00001130// We need to handle this here because tablegen doesn't support matching
1131// instructions with multiple outputs.
1132void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) {
1133 SDLoc SL(N);
1134 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32;
1135 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32;
1136
1137 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1);
1138 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
1139 Clamp };
1140 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1141}
1142
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00001143bool AMDGPUDAGToDAGISel::isDSOffsetLegal(SDValue Base, unsigned Offset,
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001144 unsigned OffsetBits) const {
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001145 if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
1146 (OffsetBits == 8 && !isUInt<8>(Offset)))
1147 return false;
1148
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00001149 if (Subtarget->hasUsableDSOffset() ||
Matt Arsenault706f9302015-07-06 16:01:58 +00001150 Subtarget->unsafeDSOffsetFoldingEnabled())
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001151 return true;
1152
1153 // On Southern Islands instruction with a negative base value and an offset
1154 // don't seem to work.
1155 return CurDAG->SignBitIsZero(Base);
1156}
1157
1158bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
1159 SDValue &Offset) const {
Tom Stellard92b24f32016-04-29 14:34:26 +00001160 SDLoc DL(Addr);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001161 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1162 SDValue N0 = Addr.getOperand(0);
1163 SDValue N1 = Addr.getOperand(1);
1164 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1165 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
1166 // (add n0, c0)
1167 Base = N0;
Tom Stellard92b24f32016-04-29 14:34:26 +00001168 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001169 return true;
1170 }
Matt Arsenault966a94f2015-09-08 19:34:22 +00001171 } else if (Addr.getOpcode() == ISD::SUB) {
1172 // sub C, x -> add (sub 0, x), C
1173 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1174 int64_t ByteOffset = C->getSExtValue();
1175 if (isUInt<16>(ByteOffset)) {
Matt Arsenault966a94f2015-09-08 19:34:22 +00001176 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001177
Matt Arsenault966a94f2015-09-08 19:34:22 +00001178 // XXX - This is kind of hacky. Create a dummy sub node so we can check
1179 // the known bits in isDSOffsetLegal. We need to emit the selected node
1180 // here, so this is thrown away.
1181 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1182 Zero, Addr.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001183
Matt Arsenault966a94f2015-09-08 19:34:22 +00001184 if (isDSOffsetLegal(Sub, ByteOffset, 16)) {
Tim Renoufcfdfba92019-03-18 19:35:44 +00001185 SmallVector<SDValue, 3> Opnds;
1186 Opnds.push_back(Zero);
1187 Opnds.push_back(Addr.getOperand(1));
Matt Arsenault84445dd2017-11-30 22:51:26 +00001188
Tim Renoufcfdfba92019-03-18 19:35:44 +00001189 // FIXME: Select to VOP3 version for with-carry.
1190 unsigned SubOp = AMDGPU::V_SUB_I32_e32;
1191 if (Subtarget->hasAddNoCarry()) {
1192 SubOp = AMDGPU::V_SUB_U32_e64;
Michael Liaoeea51772019-03-20 20:18:56 +00001193 Opnds.push_back(
1194 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
Tim Renoufcfdfba92019-03-18 19:35:44 +00001195 }
1196
1197 MachineSDNode *MachineSub =
1198 CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001199
1200 Base = SDValue(MachineSub, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +00001201 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001202 return true;
1203 }
1204 }
1205 }
1206 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1207 // If we have a constant address, prefer to put the constant into the
1208 // offset. This can save moves to load the constant address since multiple
1209 // operations can share the zero base address register, and enables merging
1210 // into read2 / write2 instructions.
1211
1212 SDLoc DL(Addr);
1213
Matt Arsenaulte775f5f2014-10-14 17:21:19 +00001214 if (isUInt<16>(CAddr->getZExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardc8d79202014-10-15 21:08:59 +00001216 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001217 DL, MVT::i32, Zero);
Tom Stellardc8d79202014-10-15 21:08:59 +00001218 Base = SDValue(MovZero, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +00001219 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
Matt Arsenaulte775f5f2014-10-14 17:21:19 +00001220 return true;
1221 }
1222 }
1223
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001224 // default case
1225 Base = Addr;
Matt Arsenault966a94f2015-09-08 19:34:22 +00001226 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001227 return true;
1228}
1229
Matt Arsenault966a94f2015-09-08 19:34:22 +00001230// TODO: If offset is too big, put low 16-bit into offset.
Tom Stellardf3fc5552014-08-22 18:49:35 +00001231bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base,
1232 SDValue &Offset0,
1233 SDValue &Offset1) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001234 SDLoc DL(Addr);
1235
Tom Stellardf3fc5552014-08-22 18:49:35 +00001236 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1237 SDValue N0 = Addr.getOperand(0);
1238 SDValue N1 = Addr.getOperand(1);
1239 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1240 unsigned DWordOffset0 = C1->getZExtValue() / 4;
1241 unsigned DWordOffset1 = DWordOffset0 + 1;
1242 // (add n0, c0)
1243 if (isDSOffsetLegal(N0, DWordOffset1, 8)) {
1244 Base = N0;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001245 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1246 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +00001247 return true;
1248 }
Matt Arsenault966a94f2015-09-08 19:34:22 +00001249 } else if (Addr.getOpcode() == ISD::SUB) {
1250 // sub C, x -> add (sub 0, x), C
1251 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1252 unsigned DWordOffset0 = C->getZExtValue() / 4;
1253 unsigned DWordOffset1 = DWordOffset0 + 1;
Tom Stellardf3fc5552014-08-22 18:49:35 +00001254
Matt Arsenault966a94f2015-09-08 19:34:22 +00001255 if (isUInt<8>(DWordOffset0)) {
1256 SDLoc DL(Addr);
1257 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1258
1259 // XXX - This is kind of hacky. Create a dummy sub node so we can check
1260 // the known bits in isDSOffsetLegal. We need to emit the selected node
1261 // here, so this is thrown away.
1262 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1263 Zero, Addr.getOperand(1));
1264
1265 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) {
Tim Renoufcfdfba92019-03-18 19:35:44 +00001266 SmallVector<SDValue, 3> Opnds;
1267 Opnds.push_back(Zero);
1268 Opnds.push_back(Addr.getOperand(1));
1269 unsigned SubOp = AMDGPU::V_SUB_I32_e32;
1270 if (Subtarget->hasAddNoCarry()) {
1271 SubOp = AMDGPU::V_SUB_U32_e64;
Michael Liaoeea51772019-03-20 20:18:56 +00001272 Opnds.push_back(
1273 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
Tim Renoufcfdfba92019-03-18 19:35:44 +00001274 }
Matt Arsenault84445dd2017-11-30 22:51:26 +00001275
Matt Arsenault966a94f2015-09-08 19:34:22 +00001276 MachineSDNode *MachineSub
Tim Renoufcfdfba92019-03-18 19:35:44 +00001277 = CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001278
1279 Base = SDValue(MachineSub, 0);
1280 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1281 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
1282 return true;
1283 }
1284 }
1285 }
1286 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001287 unsigned DWordOffset0 = CAddr->getZExtValue() / 4;
1288 unsigned DWordOffset1 = DWordOffset0 + 1;
1289 assert(4 * DWordOffset0 == CAddr->getZExtValue());
1290
1291 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001292 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001293 MachineSDNode *MovZero
1294 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001295 DL, MVT::i32, Zero);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001296 Base = SDValue(MovZero, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001297 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1298 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001299 return true;
1300 }
1301 }
1302
Tom Stellardf3fc5552014-08-22 18:49:35 +00001303 // default case
Matt Arsenault0efdd062016-09-09 22:29:28 +00001304
Tom Stellardf3fc5552014-08-22 18:49:35 +00001305 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001306 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8);
1307 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +00001308 return true;
1309}
1310
Changpeng Fangb41574a2015-12-22 20:55:23 +00001311bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
Tom Stellard155bbb72014-08-11 22:18:17 +00001312 SDValue &VAddr, SDValue &SOffset,
1313 SDValue &Offset, SDValue &Offen,
1314 SDValue &Idxen, SDValue &Addr64,
1315 SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001316 SDValue &TFE, SDValue &DLC) const {
Changpeng Fangb41574a2015-12-22 20:55:23 +00001317 // Subtarget prefers to use flat instruction
1318 if (Subtarget->useFlatForGlobal())
1319 return false;
1320
Tom Stellardb02c2682014-06-24 23:33:07 +00001321 SDLoc DL(Addr);
1322
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001323 if (!GLC.getNode())
1324 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1325 if (!SLC.getNode())
1326 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001327 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1);
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001328 DLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellard155bbb72014-08-11 22:18:17 +00001329
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001330 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1331 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1332 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1);
1333 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001334
Tim Renouff1c7b922018-08-02 22:53:57 +00001335 ConstantSDNode *C1 = nullptr;
1336 SDValue N0 = Addr;
Tom Stellardb02c2682014-06-24 23:33:07 +00001337 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Tim Renouff1c7b922018-08-02 22:53:57 +00001338 C1 = cast<ConstantSDNode>(Addr.getOperand(1));
1339 if (isUInt<32>(C1->getZExtValue()))
1340 N0 = Addr.getOperand(0);
1341 else
1342 C1 = nullptr;
Tom Stellardb02c2682014-06-24 23:33:07 +00001343 }
Tom Stellard94b72312015-02-11 00:34:35 +00001344
Tim Renouff1c7b922018-08-02 22:53:57 +00001345 if (N0.getOpcode() == ISD::ADD) {
1346 // (add N2, N3) -> addr64, or
1347 // (add (add N2, N3), C1) -> addr64
1348 SDValue N2 = N0.getOperand(0);
1349 SDValue N3 = N0.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001350 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
Tim Renouff1c7b922018-08-02 22:53:57 +00001351
1352 if (N2->isDivergent()) {
1353 if (N3->isDivergent()) {
1354 // Both N2 and N3 are divergent. Use N0 (the result of the add) as the
1355 // addr64, and construct the resource from a 0 address.
1356 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1357 VAddr = N0;
1358 } else {
1359 // N2 is divergent, N3 is not.
1360 Ptr = N3;
1361 VAddr = N2;
1362 }
1363 } else {
1364 // N2 is not divergent.
1365 Ptr = N2;
1366 VAddr = N3;
1367 }
1368 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1369 } else if (N0->isDivergent()) {
1370 // N0 is divergent. Use it as the addr64, and construct the resource from a
1371 // 0 address.
1372 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1373 VAddr = N0;
1374 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1375 } else {
1376 // N0 -> offset, or
1377 // (N0 + C1) -> offset
1378 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001379 Ptr = N0;
Tim Renouff1c7b922018-08-02 22:53:57 +00001380 }
1381
1382 if (!C1) {
1383 // No offset.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001384 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001385 return true;
Tom Stellardb02c2682014-06-24 23:33:07 +00001386 }
1387
Tim Renouff1c7b922018-08-02 22:53:57 +00001388 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) {
1389 // Legal offset for instruction.
1390 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1391 return true;
1392 }
Changpeng Fangb41574a2015-12-22 20:55:23 +00001393
Tim Renouff1c7b922018-08-02 22:53:57 +00001394 // Illegal offset, store it in soffset.
1395 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1396 SOffset =
1397 SDValue(CurDAG->getMachineNode(
1398 AMDGPU::S_MOV_B32, DL, MVT::i32,
1399 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)),
1400 0);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001401 return true;
Tom Stellard155bbb72014-08-11 22:18:17 +00001402}
1403
1404bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001405 SDValue &VAddr, SDValue &SOffset,
Tom Stellard1f9939f2015-02-27 14:59:41 +00001406 SDValue &Offset, SDValue &GLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001407 SDValue &SLC, SDValue &TFE,
1408 SDValue &DLC) const {
Tom Stellard1f9939f2015-02-27 14:59:41 +00001409 SDValue Ptr, Offen, Idxen, Addr64;
Tom Stellard155bbb72014-08-11 22:18:17 +00001410
Tom Stellard70580f82015-07-20 14:28:41 +00001411 // addr64 bit was removed for volcanic islands.
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00001412 if (!Subtarget->hasAddr64())
Tom Stellard70580f82015-07-20 14:28:41 +00001413 return false;
1414
Changpeng Fangb41574a2015-12-22 20:55:23 +00001415 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001416 GLC, SLC, TFE, DLC))
Changpeng Fangb41574a2015-12-22 20:55:23 +00001417 return false;
Tom Stellard155bbb72014-08-11 22:18:17 +00001418
1419 ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
1420 if (C->getSExtValue()) {
1421 SDLoc DL(Addr);
Matt Arsenault485defe2014-11-05 19:01:17 +00001422
1423 const SITargetLowering& Lowering =
1424 *static_cast<const SITargetLowering*>(getTargetLowering());
1425
1426 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001427 return true;
1428 }
Matt Arsenault485defe2014-11-05 19:01:17 +00001429
Tom Stellard155bbb72014-08-11 22:18:17 +00001430 return false;
1431}
1432
Tom Stellard7980fc82014-09-25 18:30:26 +00001433bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001434 SDValue &VAddr, SDValue &SOffset,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001435 SDValue &Offset,
1436 SDValue &SLC) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001437 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1);
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001438 SDValue GLC, TFE, DLC;
Tom Stellard7980fc82014-09-25 18:30:26 +00001439
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001440 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE, DLC);
Tom Stellard7980fc82014-09-25 18:30:26 +00001441}
1442
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001443static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) {
1444 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>();
1445 return PSV && PSV->isStack();
Matt Arsenaultac0fc842016-09-17 16:09:55 +00001446}
1447
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001448std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const {
1449 const MachineFunction &MF = CurDAG->getMachineFunction();
1450 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1451
1452 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) {
1453 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(),
1454 FI->getValueType(0));
1455
Matt Arsenaultb812b7a2019-06-05 22:20:47 +00001456 // If we can resolve this to a frame index access, this will be relative to
1457 // either the stack or frame pointer SGPR.
1458 return std::make_pair(
1459 TFI, CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32));
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001460 }
1461
1462 // If we don't know this private access is a local stack object, it needs to
1463 // be relative to the entry point's scratch wave offset register.
1464 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(),
1465 MVT::i32));
1466}
1467
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001468bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001469 SDValue Addr, SDValue &Rsrc,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001470 SDValue &VAddr, SDValue &SOffset,
1471 SDValue &ImmOffset) const {
Tom Stellardb02094e2014-07-21 15:45:01 +00001472
1473 SDLoc DL(Addr);
1474 MachineFunction &MF = CurDAG->getMachineFunction();
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001475 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardb02094e2014-07-21 15:45:01 +00001476
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001477 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Tom Stellardb02094e2014-07-21 15:45:01 +00001478
Matt Arsenault0774ea22017-04-24 19:40:59 +00001479 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1480 unsigned Imm = CAddr->getZExtValue();
Matt Arsenault0774ea22017-04-24 19:40:59 +00001481
1482 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32);
1483 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
1484 DL, MVT::i32, HighBits);
1485 VAddr = SDValue(MovHighBits, 0);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001486
1487 // In a call sequence, stores to the argument stack area are relative to the
1488 // stack pointer.
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001489 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001490 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1491 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1492
1493 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
Matt Arsenault0774ea22017-04-24 19:40:59 +00001494 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16);
1495 return true;
1496 }
1497
Tom Stellardb02094e2014-07-21 15:45:01 +00001498 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Matt Arsenault0774ea22017-04-24 19:40:59 +00001499 // (add n0, c1)
1500
Tom Stellard78655fc2015-07-16 19:40:09 +00001501 SDValue N0 = Addr.getOperand(0);
Tom Stellardb02094e2014-07-21 15:45:01 +00001502 SDValue N1 = Addr.getOperand(1);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001503
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001504 // Offsets in vaddr must be positive if range checking is enabled.
Matt Arsenault45b98182017-11-15 00:45:43 +00001505 //
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001506 // The total computation of vaddr + soffset + offset must not overflow. If
1507 // vaddr is negative, even if offset is 0 the sgpr offset add will end up
Matt Arsenault45b98182017-11-15 00:45:43 +00001508 // overflowing.
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001509 //
1510 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would
1511 // always perform a range check. If a negative vaddr base index was used,
1512 // this would fail the range check. The overall address computation would
1513 // compute a valid address, but this doesn't happen due to the range
1514 // check. For out-of-bounds MUBUF loads, a 0 is returned.
1515 //
1516 // Therefore it should be safe to fold any VGPR offset on gfx9 into the
1517 // MUBUF vaddr, but not on older subtargets which can only do this if the
1518 // sign bit is known 0.
Matt Arsenaultcd099612016-02-24 04:55:29 +00001519 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
Matt Arsenault45b98182017-11-15 00:45:43 +00001520 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) &&
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001521 (!Subtarget->privateMemoryResourceIsRangeChecked() ||
1522 CurDAG->SignBitIsZero(N0))) {
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001523 std::tie(VAddr, SOffset) = foldFrameIndex(N0);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001524 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1525 return true;
Tom Stellardb02094e2014-07-21 15:45:01 +00001526 }
1527 }
1528
Tom Stellardb02094e2014-07-21 15:45:01 +00001529 // (node)
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001530 std::tie(VAddr, SOffset) = foldFrameIndex(Addr);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001531 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Tom Stellardb02094e2014-07-21 15:45:01 +00001532 return true;
1533}
1534
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001535bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001536 SDValue Addr,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001537 SDValue &SRsrc,
1538 SDValue &SOffset,
1539 SDValue &Offset) const {
1540 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr);
Marek Olsakffadcb72017-11-09 01:52:17 +00001541 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue()))
Matt Arsenault0774ea22017-04-24 19:40:59 +00001542 return false;
1543
1544 SDLoc DL(Addr);
1545 MachineFunction &MF = CurDAG->getMachineFunction();
1546 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1547
1548 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001549
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001550 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001551 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1552 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1553
1554 // FIXME: Get from MachinePointerInfo? We should only be using the frame
1555 // offset if we know this is in a call sequence.
1556 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
1557
Matt Arsenault0774ea22017-04-24 19:40:59 +00001558 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1559 return true;
1560}
1561
Tom Stellard155bbb72014-08-11 22:18:17 +00001562bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1563 SDValue &SOffset, SDValue &Offset,
1564 SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001565 SDValue &TFE, SDValue &DLC) const {
Tom Stellard155bbb72014-08-11 22:18:17 +00001566 SDValue Ptr, VAddr, Offen, Idxen, Addr64;
Tom Stellard794c8c02014-12-02 17:05:41 +00001567 const SIInstrInfo *TII =
Eric Christopher7792e322015-01-30 23:24:40 +00001568 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellardb02094e2014-07-21 15:45:01 +00001569
Changpeng Fangb41574a2015-12-22 20:55:23 +00001570 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001571 GLC, SLC, TFE, DLC))
Changpeng Fangb41574a2015-12-22 20:55:23 +00001572 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001573
Tom Stellard155bbb72014-08-11 22:18:17 +00001574 if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
1575 !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
1576 !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
Tom Stellard794c8c02014-12-02 17:05:41 +00001577 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() |
Tom Stellard155bbb72014-08-11 22:18:17 +00001578 APInt::getAllOnesValue(32).getZExtValue(); // Size
1579 SDLoc DL(Addr);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00001580
1581 const SITargetLowering& Lowering =
1582 *static_cast<const SITargetLowering*>(getTargetLowering());
1583
1584 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001585 return true;
1586 }
1587 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001588}
1589
Tom Stellard7980fc82014-09-25 18:30:26 +00001590bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001591 SDValue &Soffset, SDValue &Offset
1592 ) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001593 SDValue GLC, SLC, TFE, DLC;
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001594
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001595 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC);
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001596}
1597bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Tom Stellard7980fc82014-09-25 18:30:26 +00001598 SDValue &Soffset, SDValue &Offset,
Matt Arsenault88701812016-06-09 23:42:48 +00001599 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001600 SDValue GLC, TFE, DLC;
Tom Stellard7980fc82014-09-25 18:30:26 +00001601
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001602 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC);
Tom Stellard7980fc82014-09-25 18:30:26 +00001603}
1604
Matt Arsenault4e309b02017-07-29 01:03:53 +00001605template <bool IsSigned>
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001606bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDNode *N,
1607 SDValue Addr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001608 SDValue &VAddr,
1609 SDValue &Offset,
1610 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001611 return static_cast<const SITargetLowering*>(getTargetLowering())->
1612 SelectFlatOffset(IsSigned, *CurDAG, N, Addr, VAddr, Offset, SLC);
Matt Arsenault7757c592016-06-09 23:42:54 +00001613}
1614
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001615bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDNode *N,
1616 SDValue Addr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001617 SDValue &VAddr,
1618 SDValue &Offset,
1619 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001620 return SelectFlatOffset<false>(N, Addr, VAddr, Offset, SLC);
Matt Arsenault4e309b02017-07-29 01:03:53 +00001621}
1622
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001623bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDNode *N,
1624 SDValue Addr,
Matt Arsenault4e309b02017-07-29 01:03:53 +00001625 SDValue &VAddr,
1626 SDValue &Offset,
1627 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001628 return SelectFlatOffset<true>(N, Addr, VAddr, Offset, SLC);
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001629}
1630
Tom Stellarddee26a22015-08-06 19:28:30 +00001631bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode,
1632 SDValue &Offset, bool &Imm) const {
1633
1634 // FIXME: Handle non-constant offsets.
1635 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode);
1636 if (!C)
1637 return false;
1638
1639 SDLoc SL(ByteOffsetNode);
Tom Stellard5bfbae52018-07-11 20:59:01 +00001640 GCNSubtarget::Generation Gen = Subtarget->getGeneration();
Tom Stellarddee26a22015-08-06 19:28:30 +00001641 int64_t ByteOffset = C->getSExtValue();
Tom Stellard08efb7e2017-01-27 18:41:14 +00001642 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001643
Tom Stellard08efb7e2017-01-27 18:41:14 +00001644 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001645 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1646 Imm = true;
1647 return true;
1648 }
1649
Tom Stellard217361c2015-08-06 19:28:38 +00001650 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset))
1651 return false;
1652
Marek Olsak8973a0a2017-05-24 14:53:50 +00001653 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) {
1654 // 32-bit Immediates are supported on Sea Islands.
Tom Stellard217361c2015-08-06 19:28:38 +00001655 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1656 } else {
Tom Stellarddee26a22015-08-06 19:28:30 +00001657 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32);
1658 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32,
1659 C32Bit), 0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001660 }
Tom Stellard217361c2015-08-06 19:28:38 +00001661 Imm = false;
1662 return true;
Tom Stellarddee26a22015-08-06 19:28:30 +00001663}
1664
Matt Arsenault923712b2018-02-09 16:57:57 +00001665SDValue AMDGPUDAGToDAGISel::Expand32BitAddress(SDValue Addr) const {
1666 if (Addr.getValueType() != MVT::i32)
1667 return Addr;
1668
1669 // Zero-extend a 32-bit address.
1670 SDLoc SL(Addr);
1671
1672 const MachineFunction &MF = CurDAG->getMachineFunction();
1673 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1674 unsigned AddrHiVal = Info->get32BitAddressHighBits();
1675 SDValue AddrHi = CurDAG->getTargetConstant(AddrHiVal, SL, MVT::i32);
1676
1677 const SDValue Ops[] = {
1678 CurDAG->getTargetConstant(AMDGPU::SReg_64_XEXECRegClassID, SL, MVT::i32),
1679 Addr,
1680 CurDAG->getTargetConstant(AMDGPU::sub0, SL, MVT::i32),
1681 SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, AddrHi),
1682 0),
1683 CurDAG->getTargetConstant(AMDGPU::sub1, SL, MVT::i32),
1684 };
1685
1686 return SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, SL, MVT::i64,
1687 Ops), 0);
1688}
1689
Tom Stellarddee26a22015-08-06 19:28:30 +00001690bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase,
1691 SDValue &Offset, bool &Imm) const {
Tom Stellarddee26a22015-08-06 19:28:30 +00001692 SDLoc SL(Addr);
Matt Arsenault923712b2018-02-09 16:57:57 +00001693
Marek Olsak3fc20792018-08-29 20:03:00 +00001694 // A 32-bit (address + offset) should not cause unsigned 32-bit integer
1695 // wraparound, because s_load instructions perform the addition in 64 bits.
1696 if ((Addr.getValueType() != MVT::i32 ||
1697 Addr->getFlags().hasNoUnsignedWrap()) &&
1698 CurDAG->isBaseWithConstantOffset(Addr)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001699 SDValue N0 = Addr.getOperand(0);
1700 SDValue N1 = Addr.getOperand(1);
1701
1702 if (SelectSMRDOffset(N1, Offset, Imm)) {
Matt Arsenault923712b2018-02-09 16:57:57 +00001703 SBase = Expand32BitAddress(N0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001704 return true;
1705 }
1706 }
Matt Arsenault923712b2018-02-09 16:57:57 +00001707 SBase = Expand32BitAddress(Addr);
Tom Stellarddee26a22015-08-06 19:28:30 +00001708 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32);
1709 Imm = true;
1710 return true;
1711}
1712
1713bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase,
1714 SDValue &Offset) const {
1715 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001716 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm;
1717}
Tom Stellarddee26a22015-08-06 19:28:30 +00001718
Marek Olsak8973a0a2017-05-24 14:53:50 +00001719bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase,
1720 SDValue &Offset) const {
1721
1722 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1723 return false;
1724
1725 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001726 if (!SelectSMRD(Addr, SBase, Offset, Imm))
1727 return false;
1728
Marek Olsak8973a0a2017-05-24 14:53:50 +00001729 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001730}
1731
Tom Stellarddee26a22015-08-06 19:28:30 +00001732bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase,
1733 SDValue &Offset) const {
1734 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001735 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm &&
1736 !isa<ConstantSDNode>(Offset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001737}
1738
1739bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr,
1740 SDValue &Offset) const {
1741 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001742 return SelectSMRDOffset(Addr, Offset, Imm) && Imm;
1743}
Tom Stellarddee26a22015-08-06 19:28:30 +00001744
Marek Olsak8973a0a2017-05-24 14:53:50 +00001745bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr,
1746 SDValue &Offset) const {
1747 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1748 return false;
1749
1750 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001751 if (!SelectSMRDOffset(Addr, Offset, Imm))
1752 return false;
1753
Marek Olsak8973a0a2017-05-24 14:53:50 +00001754 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001755}
1756
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001757bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index,
1758 SDValue &Base,
1759 SDValue &Offset) const {
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001760 SDLoc DL(Index);
1761
1762 if (CurDAG->isBaseWithConstantOffset(Index)) {
1763 SDValue N0 = Index.getOperand(0);
1764 SDValue N1 = Index.getOperand(1);
1765 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1766
1767 // (add n0, c0)
Changpeng Fang6f539292018-12-21 20:57:34 +00001768 // Don't peel off the offset (c0) if doing so could possibly lead
1769 // the base (n0) to be negative.
1770 if (C1->getSExtValue() <= 0 || CurDAG->SignBitIsZero(N0)) {
1771 Base = N0;
1772 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32);
1773 return true;
1774 }
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001775 }
1776
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001777 if (isa<ConstantSDNode>(Index))
1778 return false;
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001779
1780 Base = Index;
1781 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1782 return true;
1783}
1784
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001785SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL,
1786 SDValue Val, uint32_t Offset,
1787 uint32_t Width) {
Marek Olsak9b728682015-03-24 13:40:27 +00001788 // Transformation function, pack the offset and width of a BFE into
1789 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
1790 // source, bits [5:0] contain the offset and bits [22:16] the width.
1791 uint32_t PackedVal = Offset | (Width << 16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001792 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32);
Marek Olsak9b728682015-03-24 13:40:27 +00001793
1794 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst);
1795}
1796
Justin Bogner95927c02016-05-12 21:03:32 +00001797void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001798 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c)
1799 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c)
1800 // Predicate: 0 < b <= c < 32
1801
1802 const SDValue &Shl = N->getOperand(0);
1803 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1));
1804 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1805
1806 if (B && C) {
1807 uint32_t BVal = B->getZExtValue();
1808 uint32_t CVal = C->getZExtValue();
1809
1810 if (0 < BVal && BVal <= CVal && CVal < 32) {
1811 bool Signed = N->getOpcode() == ISD::SRA;
1812 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32;
1813
Justin Bogner95927c02016-05-12 21:03:32 +00001814 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal,
1815 32 - CVal));
1816 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001817 }
1818 }
Justin Bogner95927c02016-05-12 21:03:32 +00001819 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001820}
1821
Justin Bogner95927c02016-05-12 21:03:32 +00001822void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001823 switch (N->getOpcode()) {
1824 case ISD::AND:
1825 if (N->getOperand(0).getOpcode() == ISD::SRL) {
1826 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)"
1827 // Predicate: isMask(mask)
1828 const SDValue &Srl = N->getOperand(0);
1829 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
1830 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
1831
1832 if (Shift && Mask) {
1833 uint32_t ShiftVal = Shift->getZExtValue();
1834 uint32_t MaskVal = Mask->getZExtValue();
1835
1836 if (isMask_32(MaskVal)) {
1837 uint32_t WidthVal = countPopulation(MaskVal);
1838
Justin Bogner95927c02016-05-12 21:03:32 +00001839 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1840 Srl.getOperand(0), ShiftVal, WidthVal));
1841 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001842 }
1843 }
1844 }
1845 break;
1846 case ISD::SRL:
1847 if (N->getOperand(0).getOpcode() == ISD::AND) {
1848 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)"
1849 // Predicate: isMask(mask >> b)
1850 const SDValue &And = N->getOperand(0);
1851 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1));
1852 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1));
1853
1854 if (Shift && Mask) {
1855 uint32_t ShiftVal = Shift->getZExtValue();
1856 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal;
1857
1858 if (isMask_32(MaskVal)) {
1859 uint32_t WidthVal = countPopulation(MaskVal);
1860
Justin Bogner95927c02016-05-12 21:03:32 +00001861 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1862 And.getOperand(0), ShiftVal, WidthVal));
1863 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001864 }
1865 }
Justin Bogner95927c02016-05-12 21:03:32 +00001866 } else if (N->getOperand(0).getOpcode() == ISD::SHL) {
1867 SelectS_BFEFromShifts(N);
1868 return;
1869 }
Marek Olsak9b728682015-03-24 13:40:27 +00001870 break;
1871 case ISD::SRA:
Justin Bogner95927c02016-05-12 21:03:32 +00001872 if (N->getOperand(0).getOpcode() == ISD::SHL) {
1873 SelectS_BFEFromShifts(N);
1874 return;
1875 }
Marek Olsak9b728682015-03-24 13:40:27 +00001876 break;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001877
1878 case ISD::SIGN_EXTEND_INREG: {
1879 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8
1880 SDValue Src = N->getOperand(0);
1881 if (Src.getOpcode() != ISD::SRL)
1882 break;
1883
1884 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1));
1885 if (!Amt)
1886 break;
1887
1888 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
Justin Bogner95927c02016-05-12 21:03:32 +00001889 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0),
1890 Amt->getZExtValue(), Width));
1891 return;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001892 }
Marek Olsak9b728682015-03-24 13:40:27 +00001893 }
1894
Justin Bogner95927c02016-05-12 21:03:32 +00001895 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001896}
1897
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001898bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const {
1899 assert(N->getOpcode() == ISD::BRCOND);
1900 if (!N->hasOneUse())
1901 return false;
1902
1903 SDValue Cond = N->getOperand(1);
1904 if (Cond.getOpcode() == ISD::CopyToReg)
1905 Cond = Cond.getOperand(2);
1906
1907 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
1908 return false;
1909
1910 MVT VT = Cond.getOperand(0).getSimpleValueType();
1911 if (VT == MVT::i32)
1912 return true;
1913
1914 if (VT == MVT::i64) {
Tom Stellard5bfbae52018-07-11 20:59:01 +00001915 auto ST = static_cast<const GCNSubtarget *>(Subtarget);
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001916
1917 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1918 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64();
1919 }
1920
1921 return false;
1922}
1923
Justin Bogner95927c02016-05-12 21:03:32 +00001924void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001925 SDValue Cond = N->getOperand(1);
1926
Matt Arsenault327188a2016-12-15 21:57:11 +00001927 if (Cond.isUndef()) {
1928 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other,
1929 N->getOperand(2), N->getOperand(0));
1930 return;
1931 }
1932
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001933 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
1934 const SIRegisterInfo *TRI = ST->getRegisterInfo();
1935
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001936 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N);
1937 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001938 unsigned CondReg = UseSCCBr ? (unsigned)AMDGPU::SCC : TRI->getVCC();
Tom Stellardbc4497b2016-02-12 23:45:29 +00001939 SDLoc SL(N);
1940
Tim Renouf6eaad1e2018-01-09 21:34:43 +00001941 if (!UseSCCBr) {
1942 // This is the case that we are selecting to S_CBRANCH_VCCNZ. We have not
1943 // analyzed what generates the vcc value, so we do not know whether vcc
1944 // bits for disabled lanes are 0. Thus we need to mask out bits for
1945 // disabled lanes.
1946 //
1947 // For the case that we select S_CBRANCH_SCC1 and it gets
1948 // changed to S_CBRANCH_VCCNZ in SIFixSGPRCopies, SIFixSGPRCopies calls
1949 // SIInstrInfo::moveToVALU which inserts the S_AND).
1950 //
1951 // We could add an analysis of what generates the vcc value here and omit
1952 // the S_AND when is unnecessary. But it would be better to add a separate
1953 // pass after SIFixSGPRCopies to do the unnecessary S_AND removal, so it
1954 // catches both cases.
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001955 Cond = SDValue(CurDAG->getMachineNode(ST->isWave32() ? AMDGPU::S_AND_B32
1956 : AMDGPU::S_AND_B64,
1957 SL, MVT::i1,
1958 CurDAG->getRegister(ST->isWave32() ? AMDGPU::EXEC_LO
1959 : AMDGPU::EXEC,
1960 MVT::i1),
1961 Cond),
Tim Renouf6eaad1e2018-01-09 21:34:43 +00001962 0);
1963 }
1964
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001965 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond);
1966 CurDAG->SelectNodeTo(N, BrOp, MVT::Other,
Justin Bogner95927c02016-05-12 21:03:32 +00001967 N->getOperand(2), // Basic Block
Matt Arsenaultf530e8b2016-11-07 19:09:33 +00001968 VCC.getValue(0));
Tom Stellardbc4497b2016-02-12 23:45:29 +00001969}
1970
Matt Arsenault0084adc2018-04-30 19:08:16 +00001971void AMDGPUDAGToDAGISel::SelectFMAD_FMA(SDNode *N) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001972 MVT VT = N->getSimpleValueType(0);
Matt Arsenault0084adc2018-04-30 19:08:16 +00001973 bool IsFMA = N->getOpcode() == ISD::FMA;
1974 if (VT != MVT::f32 || (!Subtarget->hasMadMixInsts() &&
1975 !Subtarget->hasFmaMixInsts()) ||
1976 ((IsFMA && Subtarget->hasMadMixInsts()) ||
1977 (!IsFMA && Subtarget->hasFmaMixInsts()))) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001978 SelectCode(N);
1979 return;
1980 }
1981
1982 SDValue Src0 = N->getOperand(0);
1983 SDValue Src1 = N->getOperand(1);
1984 SDValue Src2 = N->getOperand(2);
1985 unsigned Src0Mods, Src1Mods, Src2Mods;
1986
Matt Arsenault0084adc2018-04-30 19:08:16 +00001987 // Avoid using v_mad_mix_f32/v_fma_mix_f32 unless there is actually an operand
1988 // using the conversion from f16.
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001989 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods);
1990 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods);
1991 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods);
1992
Matt Arsenault0084adc2018-04-30 19:08:16 +00001993 assert((IsFMA || !Subtarget->hasFP32Denormals()) &&
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001994 "fmad selected with denormals enabled");
1995 // TODO: We can select this with f32 denormals enabled if all the sources are
1996 // converted from f16 (in which case fmad isn't legal).
1997
1998 if (Sel0 || Sel1 || Sel2) {
1999 // For dummy operands.
2000 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
2001 SDValue Ops[] = {
2002 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0,
2003 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1,
2004 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2,
2005 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1),
2006 Zero, Zero
2007 };
2008
Matt Arsenault0084adc2018-04-30 19:08:16 +00002009 CurDAG->SelectNodeTo(N,
2010 IsFMA ? AMDGPU::V_FMA_MIX_F32 : AMDGPU::V_MAD_MIX_F32,
2011 MVT::f32, Ops);
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002012 } else {
2013 SelectCode(N);
2014 }
2015}
2016
Matt Arsenault88701812016-06-09 23:42:48 +00002017// This is here because there isn't a way to use the generated sub0_sub1 as the
2018// subreg index to EXTRACT_SUBREG in tablegen.
2019void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) {
2020 MemSDNode *Mem = cast<MemSDNode>(N);
2021 unsigned AS = Mem->getAddressSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +00002022 if (AS == AMDGPUAS::FLAT_ADDRESS) {
Matt Arsenault7757c592016-06-09 23:42:54 +00002023 SelectCode(N);
2024 return;
2025 }
Matt Arsenault88701812016-06-09 23:42:48 +00002026
2027 MVT VT = N->getSimpleValueType(0);
2028 bool Is32 = (VT == MVT::i32);
2029 SDLoc SL(N);
2030
2031 MachineSDNode *CmpSwap = nullptr;
2032 if (Subtarget->hasAddr64()) {
Vitaly Buka74503982017-10-15 05:35:02 +00002033 SDValue SRsrc, VAddr, SOffset, Offset, SLC;
Matt Arsenault88701812016-06-09 23:42:48 +00002034
2035 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00002036 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN :
2037 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00002038 SDValue CmpVal = Mem->getOperand(2);
2039
2040 // XXX - Do we care about glue operands?
2041
2042 SDValue Ops[] = {
2043 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain()
2044 };
2045
2046 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2047 }
2048 }
2049
2050 if (!CmpSwap) {
2051 SDValue SRsrc, SOffset, Offset, SLC;
2052 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00002053 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN :
2054 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00002055
2056 SDValue CmpVal = Mem->getOperand(2);
2057 SDValue Ops[] = {
2058 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain()
2059 };
2060
2061 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2062 }
2063 }
2064
2065 if (!CmpSwap) {
2066 SelectCode(N);
2067 return;
2068 }
2069
Chandler Carruth66654b72018-08-14 23:30:32 +00002070 MachineMemOperand *MMO = Mem->getMemOperand();
2071 CurDAG->setNodeMemRefs(CmpSwap, {MMO});
Matt Arsenault88701812016-06-09 23:42:48 +00002072
2073 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
2074 SDValue Extract
2075 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0));
2076
2077 ReplaceUses(SDValue(N, 0), Extract);
2078 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1));
2079 CurDAG->RemoveDeadNode(N);
2080}
2081
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002082void AMDGPUDAGToDAGISel::SelectDSAppendConsume(SDNode *N, unsigned IntrID) {
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002083 // The address is assumed to be uniform, so if it ends up in a VGPR, it will
2084 // be copied to an SGPR with readfirstlane.
2085 unsigned Opc = IntrID == Intrinsic::amdgcn_ds_append ?
2086 AMDGPU::DS_APPEND : AMDGPU::DS_CONSUME;
2087
2088 SDValue Chain = N->getOperand(0);
2089 SDValue Ptr = N->getOperand(2);
2090 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
Matt Arsenault9e5fa332019-06-14 21:01:24 +00002091 MachineMemOperand *MMO = M->getMemOperand();
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002092 bool IsGDS = M->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
2093
2094 SDValue Offset;
2095 if (CurDAG->isBaseWithConstantOffset(Ptr)) {
2096 SDValue PtrBase = Ptr.getOperand(0);
2097 SDValue PtrOffset = Ptr.getOperand(1);
2098
2099 const APInt &OffsetVal = cast<ConstantSDNode>(PtrOffset)->getAPIntValue();
2100 if (isDSOffsetLegal(PtrBase, OffsetVal.getZExtValue(), 16)) {
2101 N = glueCopyToM0(N, PtrBase);
2102 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i32);
2103 }
2104 }
2105
2106 if (!Offset) {
2107 N = glueCopyToM0(N, Ptr);
2108 Offset = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
2109 }
2110
2111 SDValue Ops[] = {
2112 Offset,
2113 CurDAG->getTargetConstant(IsGDS, SDLoc(), MVT::i32),
2114 Chain,
2115 N->getOperand(N->getNumOperands() - 1) // New glue
2116 };
2117
Matt Arsenault9e5fa332019-06-14 21:01:24 +00002118 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2119 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002120}
2121
Matt Arsenault740322f2019-06-20 21:11:42 +00002122static unsigned gwsIntrinToOpcode(unsigned IntrID) {
2123 switch (IntrID) {
2124 case Intrinsic::amdgcn_ds_gws_init:
2125 return AMDGPU::DS_GWS_INIT;
2126 case Intrinsic::amdgcn_ds_gws_barrier:
2127 return AMDGPU::DS_GWS_BARRIER;
2128 case Intrinsic::amdgcn_ds_gws_sema_v:
2129 return AMDGPU::DS_GWS_SEMA_V;
2130 case Intrinsic::amdgcn_ds_gws_sema_br:
2131 return AMDGPU::DS_GWS_SEMA_BR;
2132 case Intrinsic::amdgcn_ds_gws_sema_p:
2133 return AMDGPU::DS_GWS_SEMA_P;
2134 case Intrinsic::amdgcn_ds_gws_sema_release_all:
2135 return AMDGPU::DS_GWS_SEMA_RELEASE_ALL;
2136 default:
2137 llvm_unreachable("not a gws intrinsic");
2138 }
2139}
2140
Matt Arsenault4d55d022019-06-19 19:55:27 +00002141void AMDGPUDAGToDAGISel::SelectDS_GWS(SDNode *N, unsigned IntrID) {
Matt Arsenault740322f2019-06-20 21:11:42 +00002142 if (IntrID == Intrinsic::amdgcn_ds_gws_sema_release_all &&
2143 !Subtarget->hasGWSSemaReleaseAll()) {
2144 // Let this error.
2145 SelectCode(N);
2146 return;
2147 }
2148
2149 // Chain, intrinsic ID, vsrc, offset
2150 const bool HasVSrc = N->getNumOperands() == 4;
2151 assert(HasVSrc || N->getNumOperands() == 3);
2152
Matt Arsenault4d55d022019-06-19 19:55:27 +00002153 SDLoc SL(N);
Matt Arsenault740322f2019-06-20 21:11:42 +00002154 SDValue BaseOffset = N->getOperand(HasVSrc ? 3 : 2);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002155 int ImmOffset = 0;
2156 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
2157 MachineMemOperand *MMO = M->getMemOperand();
2158
2159 // Don't worry if the offset ends up in a VGPR. Only one lane will have
2160 // effect, so SIFixSGPRCopies will validly insert readfirstlane.
2161
2162 // The resource id offset is computed as (<isa opaque base> + M0[21:16] +
2163 // offset field) % 64. Some versions of the programming guide omit the m0
2164 // part, or claim it's from offset 0.
2165 if (ConstantSDNode *ConstOffset = dyn_cast<ConstantSDNode>(BaseOffset)) {
Matt Arsenault48c0df52019-07-19 20:01:24 +00002166 // If we have a constant offset, try to use the 0 in m0 as the base.
2167 // TODO: Look into changing the default m0 initialization value. If the
2168 // default -1 only set the low 16-bits, we could leave it as-is and add 1 to
2169 // the immediate offset.
2170 glueCopyToM0(N, CurDAG->getTargetConstant(0, SL, MVT::i32));
2171 ImmOffset = ConstOffset->getZExtValue();
Matt Arsenault4d55d022019-06-19 19:55:27 +00002172 } else {
2173 if (CurDAG->isBaseWithConstantOffset(BaseOffset)) {
2174 ImmOffset = BaseOffset.getConstantOperandVal(1);
2175 BaseOffset = BaseOffset.getOperand(0);
2176 }
2177
2178 // Prefer to do the shift in an SGPR since it should be possible to use m0
2179 // as the result directly. If it's already an SGPR, it will be eliminated
2180 // later.
2181 SDNode *SGPROffset
2182 = CurDAG->getMachineNode(AMDGPU::V_READFIRSTLANE_B32, SL, MVT::i32,
2183 BaseOffset);
2184 // Shift to offset in m0
2185 SDNode *M0Base
2186 = CurDAG->getMachineNode(AMDGPU::S_LSHL_B32, SL, MVT::i32,
2187 SDValue(SGPROffset, 0),
2188 CurDAG->getTargetConstant(16, SL, MVT::i32));
2189 glueCopyToM0(N, SDValue(M0Base, 0));
2190 }
2191
Matt Arsenault740322f2019-06-20 21:11:42 +00002192 SDValue V0;
2193 SDValue Chain = N->getOperand(0);
2194 SDValue Glue;
2195 if (HasVSrc) {
2196 SDValue VSrc0 = N->getOperand(2);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002197
Matt Arsenault740322f2019-06-20 21:11:42 +00002198 // The manual doesn't mention this, but it seems only v0 works.
2199 V0 = CurDAG->getRegister(AMDGPU::VGPR0, MVT::i32);
2200
2201 SDValue CopyToV0 = CurDAG->getCopyToReg(
2202 N->getOperand(0), SL, V0, VSrc0,
2203 N->getOperand(N->getNumOperands() - 1));
2204 Chain = CopyToV0;
2205 Glue = CopyToV0.getValue(1);
2206 }
Matt Arsenault4d55d022019-06-19 19:55:27 +00002207
2208 SDValue OffsetField = CurDAG->getTargetConstant(ImmOffset, SL, MVT::i32);
2209
2210 // TODO: Can this just be removed from the instruction?
2211 SDValue GDS = CurDAG->getTargetConstant(1, SL, MVT::i1);
2212
Matt Arsenault740322f2019-06-20 21:11:42 +00002213 const unsigned Opc = gwsIntrinToOpcode(IntrID);
2214 SmallVector<SDValue, 5> Ops;
2215 if (HasVSrc)
2216 Ops.push_back(V0);
2217 Ops.push_back(OffsetField);
2218 Ops.push_back(GDS);
2219 Ops.push_back(Chain);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002220
Matt Arsenault740322f2019-06-20 21:11:42 +00002221 if (HasVSrc)
2222 Ops.push_back(Glue);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002223
2224 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2225 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
2226}
2227
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002228void AMDGPUDAGToDAGISel::SelectINTRINSIC_W_CHAIN(SDNode *N) {
2229 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2230 switch (IntrID) {
2231 case Intrinsic::amdgcn_ds_append:
2232 case Intrinsic::amdgcn_ds_consume: {
2233 if (N->getValueType(0) != MVT::i32)
2234 break;
2235 SelectDSAppendConsume(N, IntrID);
2236 return;
2237 }
Matt Arsenault4d55d022019-06-19 19:55:27 +00002238 }
2239
2240 SelectCode(N);
2241}
2242
Carl Ritson00e89b42019-07-26 09:54:12 +00002243void AMDGPUDAGToDAGISel::SelectINTRINSIC_WO_CHAIN(SDNode *N) {
2244 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2245 unsigned Opcode;
2246 switch (IntrID) {
2247 case Intrinsic::amdgcn_softwqm:
2248 Opcode = AMDGPU::SOFT_WQM;
2249 break;
2250 default:
2251 SelectCode(N);
2252 return;
2253 }
2254
2255 SDValue Src = N->getOperand(1);
2256 CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), {Src});
2257}
2258
Matt Arsenault4d55d022019-06-19 19:55:27 +00002259void AMDGPUDAGToDAGISel::SelectINTRINSIC_VOID(SDNode *N) {
2260 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2261 switch (IntrID) {
2262 case Intrinsic::amdgcn_ds_gws_init:
2263 case Intrinsic::amdgcn_ds_gws_barrier:
Matt Arsenault740322f2019-06-20 21:11:42 +00002264 case Intrinsic::amdgcn_ds_gws_sema_v:
2265 case Intrinsic::amdgcn_ds_gws_sema_br:
2266 case Intrinsic::amdgcn_ds_gws_sema_p:
2267 case Intrinsic::amdgcn_ds_gws_sema_release_all:
Matt Arsenault4d55d022019-06-19 19:55:27 +00002268 SelectDS_GWS(N, IntrID);
2269 return;
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002270 default:
2271 break;
2272 }
2273
2274 SelectCode(N);
2275}
2276
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002277bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src,
2278 unsigned &Mods) const {
2279 Mods = 0;
Tom Stellardb4a313a2014-08-01 00:32:39 +00002280 Src = In;
2281
2282 if (Src.getOpcode() == ISD::FNEG) {
2283 Mods |= SISrcMods::NEG;
2284 Src = Src.getOperand(0);
2285 }
2286
2287 if (Src.getOpcode() == ISD::FABS) {
2288 Mods |= SISrcMods::ABS;
2289 Src = Src.getOperand(0);
2290 }
2291
Tom Stellardb4a313a2014-08-01 00:32:39 +00002292 return true;
2293}
2294
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002295bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
2296 SDValue &SrcMods) const {
2297 unsigned Mods;
2298 if (SelectVOP3ModsImpl(In, Src, Mods)) {
2299 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2300 return true;
2301 }
2302
2303 return false;
2304}
2305
Matt Arsenaultf84e5d92017-01-31 03:07:46 +00002306bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src,
2307 SDValue &SrcMods) const {
2308 SelectVOP3Mods(In, Src, SrcMods);
2309 return isNoNanSrc(Src);
2310}
2311
Jay Foad7816ad92019-07-12 15:02:59 +00002312bool AMDGPUDAGToDAGISel::SelectVOP3Mods_f32(SDValue In, SDValue &Src,
2313 SDValue &SrcMods) const {
2314 if (In.getValueType() == MVT::f32)
2315 return SelectVOP3Mods(In, Src, SrcMods);
2316 Src = In;
2317 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);;
2318 return true;
2319}
2320
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002321bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const {
2322 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG)
2323 return false;
2324
2325 Src = In;
2326 return true;
Tom Stellarddb5a11f2015-07-13 15:47:57 +00002327}
2328
Tom Stellardb4a313a2014-08-01 00:32:39 +00002329bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
2330 SDValue &SrcMods, SDValue &Clamp,
2331 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002332 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002333 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2334 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellardb4a313a2014-08-01 00:32:39 +00002335
2336 return SelectVOP3Mods(In, Src, SrcMods);
2337}
2338
Matt Arsenault4831ce52015-01-06 23:00:37 +00002339bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src,
2340 SDValue &SrcMods,
2341 SDValue &Clamp,
2342 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002343 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
Matt Arsenault4831ce52015-01-06 23:00:37 +00002344 return SelectVOP3Mods(In, Src, SrcMods);
2345}
2346
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00002347bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src,
2348 SDValue &Clamp, SDValue &Omod) const {
2349 Src = In;
2350
2351 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002352 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2353 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00002354
2355 return true;
2356}
2357
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002358bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src,
2359 SDValue &SrcMods) const {
2360 unsigned Mods = 0;
2361 Src = In;
2362
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002363 if (Src.getOpcode() == ISD::FNEG) {
Matt Arsenault786eeea2017-05-17 20:00:00 +00002364 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI);
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002365 Src = Src.getOperand(0);
2366 }
2367
Matt Arsenault786eeea2017-05-17 20:00:00 +00002368 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2369 unsigned VecMods = Mods;
2370
Matt Arsenault98f29462017-05-17 20:30:58 +00002371 SDValue Lo = stripBitcast(Src.getOperand(0));
2372 SDValue Hi = stripBitcast(Src.getOperand(1));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002373
2374 if (Lo.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00002375 Lo = stripBitcast(Lo.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002376 Mods ^= SISrcMods::NEG;
2377 }
2378
2379 if (Hi.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00002380 Hi = stripBitcast(Hi.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002381 Mods ^= SISrcMods::NEG_HI;
2382 }
2383
Matt Arsenault98f29462017-05-17 20:30:58 +00002384 if (isExtractHiElt(Lo, Lo))
2385 Mods |= SISrcMods::OP_SEL_0;
2386
2387 if (isExtractHiElt(Hi, Hi))
2388 Mods |= SISrcMods::OP_SEL_1;
2389
2390 Lo = stripExtractLoElt(Lo);
2391 Hi = stripExtractLoElt(Hi);
2392
Matt Arsenault786eeea2017-05-17 20:00:00 +00002393 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) {
2394 // Really a scalar input. Just select from the low half of the register to
2395 // avoid packing.
2396
2397 Src = Lo;
2398 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2399 return true;
2400 }
2401
2402 Mods = VecMods;
2403 }
2404
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002405 // Packed instructions do not have abs modifiers.
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002406 Mods |= SISrcMods::OP_SEL_1;
2407
2408 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2409 return true;
2410}
2411
2412bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src,
2413 SDValue &SrcMods,
2414 SDValue &Clamp) const {
2415 SDLoc SL(In);
2416
2417 // FIXME: Handle clamp and op_sel
2418 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2419
2420 return SelectVOP3PMods(In, Src, SrcMods);
2421}
2422
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +00002423bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src,
2424 SDValue &SrcMods) const {
2425 Src = In;
2426 // FIXME: Handle op_sel
2427 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
2428 return true;
2429}
2430
2431bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src,
2432 SDValue &SrcMods,
2433 SDValue &Clamp) const {
2434 SDLoc SL(In);
2435
2436 // FIXME: Handle clamp
2437 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2438
2439 return SelectVOP3OpSel(In, Src, SrcMods);
2440}
2441
2442bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src,
2443 SDValue &SrcMods) const {
2444 // FIXME: Handle op_sel
2445 return SelectVOP3Mods(In, Src, SrcMods);
2446}
2447
2448bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src,
2449 SDValue &SrcMods,
2450 SDValue &Clamp) const {
2451 SDLoc SL(In);
2452
2453 // FIXME: Handle clamp
2454 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2455
2456 return SelectVOP3OpSelMods(In, Src, SrcMods);
2457}
2458
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002459// The return value is not whether the match is possible (which it always is),
2460// but whether or not it a conversion is really used.
2461bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src,
2462 unsigned &Mods) const {
2463 Mods = 0;
2464 SelectVOP3ModsImpl(In, Src, Mods);
2465
2466 if (Src.getOpcode() == ISD::FP_EXTEND) {
2467 Src = Src.getOperand(0);
2468 assert(Src.getValueType() == MVT::f16);
2469 Src = stripBitcast(Src);
2470
Matt Arsenault550c66d2017-10-13 20:45:49 +00002471 // Be careful about folding modifiers if we already have an abs. fneg is
2472 // applied last, so we don't want to apply an earlier fneg.
2473 if ((Mods & SISrcMods::ABS) == 0) {
2474 unsigned ModsTmp;
2475 SelectVOP3ModsImpl(Src, Src, ModsTmp);
2476
2477 if ((ModsTmp & SISrcMods::NEG) != 0)
2478 Mods ^= SISrcMods::NEG;
2479
2480 if ((ModsTmp & SISrcMods::ABS) != 0)
2481 Mods |= SISrcMods::ABS;
2482 }
2483
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002484 // op_sel/op_sel_hi decide the source type and source.
2485 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16.
2486 // If the sources's op_sel is set, it picks the high half of the source
2487 // register.
2488
2489 Mods |= SISrcMods::OP_SEL_1;
Matt Arsenault550c66d2017-10-13 20:45:49 +00002490 if (isExtractHiElt(Src, Src)) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002491 Mods |= SISrcMods::OP_SEL_0;
2492
Matt Arsenault550c66d2017-10-13 20:45:49 +00002493 // TODO: Should we try to look for neg/abs here?
2494 }
2495
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002496 return true;
2497 }
2498
2499 return false;
2500}
2501
Matt Arsenault76935122017-09-20 20:28:39 +00002502bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src,
2503 SDValue &SrcMods) const {
2504 unsigned Mods = 0;
2505 SelectVOP3PMadMixModsImpl(In, Src, Mods);
2506 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2507 return true;
2508}
2509
Matt Arsenaulte8c03a22019-03-08 20:58:11 +00002510SDValue AMDGPUDAGToDAGISel::getHi16Elt(SDValue In) const {
2511 if (In.isUndef())
2512 return CurDAG->getUNDEF(MVT::i32);
2513
2514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) {
2515 SDLoc SL(In);
2516 return CurDAG->getConstant(C->getZExtValue() << 16, SL, MVT::i32);
2517 }
2518
2519 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) {
2520 SDLoc SL(In);
2521 return CurDAG->getConstant(
2522 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32);
2523 }
2524
2525 SDValue Src;
2526 if (isExtractHiElt(In, Src))
2527 return Src;
2528
2529 return SDValue();
2530}
2531
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002532bool AMDGPUDAGToDAGISel::isVGPRImm(const SDNode * N) const {
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00002533 assert(CurDAG->getTarget().getTargetTriple().getArch() == Triple::amdgcn);
2534
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002535 const SIRegisterInfo *SIRI =
2536 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
2537 const SIInstrInfo * SII =
2538 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2539
2540 unsigned Limit = 0;
2541 bool AllUsesAcceptSReg = true;
2542 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
2543 Limit < 10 && U != E; ++U, ++Limit) {
2544 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
2545
2546 // If the register class is unknown, it could be an unknown
2547 // register class that needs to be an SGPR, e.g. an inline asm
2548 // constraint
2549 if (!RC || SIRI->isSGPRClass(RC))
2550 return false;
2551
2552 if (RC != &AMDGPU::VS_32RegClass) {
2553 AllUsesAcceptSReg = false;
2554 SDNode * User = *U;
2555 if (User->isMachineOpcode()) {
2556 unsigned Opc = User->getMachineOpcode();
2557 MCInstrDesc Desc = SII->get(Opc);
2558 if (Desc.isCommutable()) {
2559 unsigned OpIdx = Desc.getNumDefs() + U.getOperandNo();
2560 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
2561 if (SII->findCommutedOpIndices(Desc, OpIdx, CommuteIdx1)) {
2562 unsigned CommutedOpNo = CommuteIdx1 - Desc.getNumDefs();
2563 const TargetRegisterClass *CommutedRC = getOperandRegClass(*U, CommutedOpNo);
2564 if (CommutedRC == &AMDGPU::VS_32RegClass)
2565 AllUsesAcceptSReg = true;
2566 }
2567 }
2568 }
2569 // If "AllUsesAcceptSReg == false" so far we haven't suceeded
2570 // commuting current user. This means have at least one use
2571 // that strictly require VGPR. Thus, we will not attempt to commute
2572 // other user instructions.
2573 if (!AllUsesAcceptSReg)
2574 break;
2575 }
2576 }
2577 return !AllUsesAcceptSReg && (Limit < 10);
2578}
2579
Alexander Timofeev4d302f62018-09-13 09:06:56 +00002580bool AMDGPUDAGToDAGISel::isUniformLoad(const SDNode * N) const {
2581 auto Ld = cast<LoadSDNode>(N);
2582
2583 return Ld->getAlignment() >= 4 &&
2584 (
2585 (
2586 (
2587 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2588 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT
2589 )
2590 &&
2591 !N->isDivergent()
2592 )
2593 ||
2594 (
2595 Subtarget->getScalarizeGlobalBehavior() &&
2596 Ld->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
2597 !Ld->isVolatile() &&
2598 !N->isDivergent() &&
2599 static_cast<const SITargetLowering *>(
2600 getTargetLowering())->isMemOpHasNoClobberedMemOperand(N)
2601 )
2602 );
2603}
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002604
Christian Konigd910b7d2013-02-26 17:52:16 +00002605void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002606 const AMDGPUTargetLowering& Lowering =
Matt Arsenault209a7b92014-04-18 07:40:20 +00002607 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002608 bool IsModified = false;
2609 do {
2610 IsModified = false;
Matt Arsenault68f05052017-12-04 22:18:27 +00002611
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002612 // Go over all selected nodes and try to fold them a bit more
Matt Arsenault68f05052017-12-04 22:18:27 +00002613 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin();
2614 while (Position != CurDAG->allnodes_end()) {
2615 SDNode *Node = &*Position++;
2616 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002617 if (!MachineNode)
2618 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +00002619
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002620 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
Matt Arsenault68f05052017-12-04 22:18:27 +00002621 if (ResNode != Node) {
2622 if (ResNode)
2623 ReplaceUses(Node, ResNode);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002624 IsModified = true;
2625 }
Tom Stellard2183b702013-06-03 17:39:46 +00002626 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002627 CurDAG->RemoveDeadNodes();
2628 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +00002629}
Tom Stellard20287692017-08-08 04:57:55 +00002630
Tom Stellardc5a154d2018-06-28 23:47:12 +00002631bool R600DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
2632 Subtarget = &MF.getSubtarget<R600Subtarget>();
2633 return SelectionDAGISel::runOnMachineFunction(MF);
2634}
2635
2636bool R600DAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const {
2637 if (!N->readMem())
2638 return false;
2639 if (CbId == -1)
Matt Arsenault0da63502018-08-31 05:49:54 +00002640 return N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2641 N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002642
Matt Arsenault0da63502018-08-31 05:49:54 +00002643 return N->getAddressSpace() == AMDGPUAS::CONSTANT_BUFFER_0 + CbId;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002644}
2645
2646bool R600DAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
2647 SDValue& IntPtr) {
2648 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
2649 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr),
2650 true);
2651 return true;
2652 }
2653 return false;
2654}
2655
2656bool R600DAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
2657 SDValue& BaseReg, SDValue &Offset) {
2658 if (!isa<ConstantSDNode>(Addr)) {
2659 BaseReg = Addr;
2660 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true);
2661 return true;
2662 }
2663 return false;
2664}
2665
Tom Stellard20287692017-08-08 04:57:55 +00002666void R600DAGToDAGISel::Select(SDNode *N) {
2667 unsigned int Opc = N->getOpcode();
2668 if (N->isMachineOpcode()) {
2669 N->setNodeId(-1);
2670 return; // Already selected.
2671 }
2672
2673 switch (Opc) {
2674 default: break;
2675 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
2676 case ISD::SCALAR_TO_VECTOR:
2677 case ISD::BUILD_VECTOR: {
2678 EVT VT = N->getValueType(0);
2679 unsigned NumVectorElts = VT.getVectorNumElements();
2680 unsigned RegClassID;
2681 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
2682 // that adds a 128 bits reg copy when going through TwoAddressInstructions
2683 // pass. We want to avoid 128 bits copies as much as possible because they
2684 // can't be bundled by our scheduler.
2685 switch(NumVectorElts) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002686 case 2: RegClassID = R600::R600_Reg64RegClassID; break;
Tom Stellard20287692017-08-08 04:57:55 +00002687 case 4:
2688 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
Tom Stellardc5a154d2018-06-28 23:47:12 +00002689 RegClassID = R600::R600_Reg128VerticalRegClassID;
Tom Stellard20287692017-08-08 04:57:55 +00002690 else
Tom Stellardc5a154d2018-06-28 23:47:12 +00002691 RegClassID = R600::R600_Reg128RegClassID;
Tom Stellard20287692017-08-08 04:57:55 +00002692 break;
2693 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
2694 }
2695 SelectBuildVector(N, RegClassID);
2696 return;
2697 }
2698 }
2699
2700 SelectCode(N);
2701}
2702
2703bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
2704 SDValue &Offset) {
2705 ConstantSDNode *C;
2706 SDLoc DL(Addr);
2707
2708 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002709 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002710 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2711 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
2712 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002713 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002714 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2715 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
2716 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
2717 Base = Addr.getOperand(0);
2718 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2719 } else {
2720 Base = Addr;
2721 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
2722 }
2723
2724 return true;
2725}
2726
2727bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
2728 SDValue &Offset) {
2729 ConstantSDNode *IMMOffset;
2730
2731 if (Addr.getOpcode() == ISD::ADD
2732 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
2733 && isInt<16>(IMMOffset->getZExtValue())) {
2734
2735 Base = Addr.getOperand(0);
2736 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2737 MVT::i32);
2738 return true;
2739 // If the pointer address is constant, we can move it to the offset field.
2740 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
2741 && isInt<16>(IMMOffset->getZExtValue())) {
2742 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
2743 SDLoc(CurDAG->getEntryNode()),
Tom Stellardc5a154d2018-06-28 23:47:12 +00002744 R600::ZERO, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002745 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2746 MVT::i32);
2747 return true;
2748 }
2749
2750 // Default case, no offset
2751 Base = Addr;
2752 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
2753 return true;
2754}