blob: da2cf7b076df6d82c806a8cbf4ae0775f2f61d47 [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"
42#include "llvm/IR/Instruction.h"
43#include "llvm/MC/MCInstrDesc.h"
44#include "llvm/Support/Casting.h"
45#include "llvm/Support/CodeGen.h"
46#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000047#include "llvm/Support/MachineValueType.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000048#include "llvm/Support/MathExtras.h"
49#include <cassert>
50#include <cstdint>
51#include <new>
52#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000053
Matt Arsenaulte8c03a22019-03-08 20:58:11 +000054#define DEBUG_TYPE "isel"
55
Tom Stellard75aadc22012-12-11 21:25:42 +000056using namespace llvm;
57
Matt Arsenaultd2759212016-02-13 01:24:08 +000058namespace llvm {
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000059
Matt Arsenaultd2759212016-02-13 01:24:08 +000060class R600InstrInfo;
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000061
62} // end namespace llvm
Matt Arsenaultd2759212016-02-13 01:24:08 +000063
Tom Stellard75aadc22012-12-11 21:25:42 +000064//===----------------------------------------------------------------------===//
65// Instruction Selector Implementation
66//===----------------------------------------------------------------------===//
67
68namespace {
Tom Stellardbc4497b2016-02-12 23:45:29 +000069
Matt Arsenaultb7f87c02019-06-20 16:01:09 +000070static bool isNullConstantOrUndef(SDValue V) {
71 if (V.isUndef())
72 return true;
73
74 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
75 return Const != nullptr && Const->isNullValue();
76}
77
Matt Arsenaulte24b34e2019-06-19 23:37:43 +000078static bool getConstantValue(SDValue N, uint32_t &Out) {
Matt Arsenaultb7f87c02019-06-20 16:01:09 +000079 // This is only used for packed vectors, where ussing 0 for undef should
80 // always be good.
81 if (N.isUndef()) {
82 Out = 0;
83 return true;
84 }
85
Matt Arsenaulte24b34e2019-06-19 23:37:43 +000086 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
87 Out = C->getAPIntValue().getSExtValue();
88 return true;
89 }
90
91 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) {
92 Out = C->getValueAPF().bitcastToAPInt().getSExtValue();
93 return true;
94 }
95
96 return false;
97}
98
99// TODO: Handle undef as zero
100static SDNode *packConstantV2I16(const SDNode *N, SelectionDAG &DAG,
101 bool Negate = false) {
102 assert(N->getOpcode() == ISD::BUILD_VECTOR && N->getNumOperands() == 2);
103 uint32_t LHSVal, RHSVal;
104 if (getConstantValue(N->getOperand(0), LHSVal) &&
105 getConstantValue(N->getOperand(1), RHSVal)) {
106 SDLoc SL(N);
107 uint32_t K = Negate ?
108 (-LHSVal & 0xffff) | (-RHSVal << 16) :
109 (LHSVal & 0xffff) | (RHSVal << 16);
110 return DAG.getMachineNode(AMDGPU::S_MOV_B32, SL, N->getValueType(0),
111 DAG.getTargetConstant(K, SL, MVT::i32));
112 }
113
114 return nullptr;
115}
116
117static SDNode *packNegConstantV2I16(const SDNode *N, SelectionDAG &DAG) {
118 return packConstantV2I16(N, DAG, true);
119}
120
Tom Stellard75aadc22012-12-11 21:25:42 +0000121/// AMDGPU specific code to select AMDGPU machine instructions for
122/// SelectionDAG operations.
123class AMDGPUDAGToDAGISel : public SelectionDAGISel {
124 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
125 // make the right decision when generating code for different targets.
Tom Stellard5bfbae52018-07-11 20:59:01 +0000126 const GCNSubtarget *Subtarget;
Matt Arsenaultcc852232017-10-10 20:22:07 +0000127 bool EnableLateStructurizeCFG;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000128
Tom Stellard75aadc22012-12-11 21:25:42 +0000129public:
Matt Arsenault7016f132017-08-03 22:30:46 +0000130 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr,
131 CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
132 : SelectionDAGISel(*TM, OptLevel) {
Matt Arsenaultcc852232017-10-10 20:22:07 +0000133 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000134 }
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000135 ~AMDGPUDAGToDAGISel() override = default;
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +0000136
Matt Arsenault7016f132017-08-03 22:30:46 +0000137 void getAnalysisUsage(AnalysisUsage &AU) const override {
138 AU.addRequired<AMDGPUArgumentUsageInfo>();
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000139 AU.addRequired<AMDGPUPerfHintAnalysis>();
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000140 AU.addRequired<LegacyDivergenceAnalysis>();
Matt Arsenault7016f132017-08-03 22:30:46 +0000141 SelectionDAGISel::getAnalysisUsage(AU);
142 }
143
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000144 bool matchLoadD16FromBuildVector(SDNode *N) const;
145
Eric Christopher7792e322015-01-30 23:24:40 +0000146 bool runOnMachineFunction(MachineFunction &MF) override;
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000147 void PreprocessISelDAG() override;
Justin Bogner95927c02016-05-12 21:03:32 +0000148 void Select(SDNode *N) override;
Mehdi Amini117296c2016-10-01 02:56:57 +0000149 StringRef getPassName() const override;
Craig Topper5656db42014-04-29 07:57:24 +0000150 void PostprocessISelDAG() override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000151
Tom Stellard20287692017-08-08 04:57:55 +0000152protected:
153 void SelectBuildVector(SDNode *N, unsigned RegClassID);
154
Tom Stellard75aadc22012-12-11 21:25:42 +0000155private:
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000156 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000157 bool isNoNanSrc(SDValue N) const;
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000158 bool isInlineImmediate(const SDNode *N, bool Negated = false) const;
159 bool isNegInlineImmediate(const SDNode *N) const {
160 return isInlineImmediate(N, true);
161 }
162
Alexander Timofeevdb7ee762018-09-11 11:56:50 +0000163 bool isVGPRImm(const SDNode *N) const;
Alexander Timofeev4d302f62018-09-13 09:06:56 +0000164 bool isUniformLoad(const SDNode *N) const;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000165 bool isUniformBr(const SDNode *N) const;
166
Tim Renouff1c7b922018-08-02 22:53:57 +0000167 MachineSDNode *buildSMovImm64(SDLoc &DL, uint64_t Val, EVT VT) const;
168
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000169 SDNode *glueCopyToM0LDSInit(SDNode *N) const;
170 SDNode *glueCopyToM0(SDNode *N, SDValue Val) const;
Tom Stellard381a94a2015-05-12 15:00:49 +0000171
Tom Stellarddf94dc32013-08-14 23:24:24 +0000172 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
Tom Stellard20287692017-08-08 04:57:55 +0000173 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
174 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000175 bool isDSOffsetLegal(SDValue Base, unsigned Offset,
Tom Stellard85e8b6d2014-08-22 18:49:33 +0000176 unsigned OffsetBits) const;
177 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
Tom Stellardf3fc5552014-08-22 18:49:35 +0000178 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
179 SDValue &Offset1) const;
Changpeng Fangb41574a2015-12-22 20:55:23 +0000180 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard155bbb72014-08-11 22:18:17 +0000181 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
182 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000183 SDValue &TFE, SDValue &DLC) const;
Tom Stellard155bbb72014-08-11 22:18:17 +0000184 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
Tom Stellard1f9939f2015-02-27 14:59:41 +0000185 SDValue &SOffset, SDValue &Offset, SDValue &GLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000186 SDValue &SLC, SDValue &TFE, SDValue &DLC) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000187 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +0000188 SDValue &VAddr, SDValue &SOffset, SDValue &Offset,
Tom Stellard7980fc82014-09-25 18:30:26 +0000189 SDValue &SLC) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000190 bool SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000191 SDValue Addr, SDValue &RSrc, SDValue &VAddr,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000192 SDValue &SOffset, SDValue &ImmOffset) const;
Matt Arsenaultb81495d2017-09-20 05:01:53 +0000193 bool SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +0000194 SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault0774ea22017-04-24 19:40:59 +0000195 SDValue &Offset) const;
196
Tom Stellard155bbb72014-08-11 22:18:17 +0000197 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
198 SDValue &Offset, SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000199 SDValue &TFE, SDValue &DLC) const;
Tom Stellard7980fc82014-09-25 18:30:26 +0000200 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
Matt Arsenault88701812016-06-09 23:42:48 +0000201 SDValue &Offset, SDValue &SLC) const;
Jan Vesely43b7b5b2016-04-07 19:23:11 +0000202 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
203 SDValue &Offset) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000204
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000205 bool SelectFlatAtomic(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000206 SDValue &Offset, SDValue &SLC) const;
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000207 bool SelectFlatAtomicSigned(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenault4e309b02017-07-29 01:03:53 +0000208 SDValue &Offset, SDValue &SLC) const;
209
210 template <bool IsSigned>
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +0000211 bool SelectFlatOffset(SDNode *N, SDValue Addr, SDValue &VAddr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +0000212 SDValue &Offset, SDValue &SLC) const;
Matt Arsenault7757c592016-06-09 23:42:54 +0000213
Tom Stellarddee26a22015-08-06 19:28:30 +0000214 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset,
215 bool &Imm) const;
Matt Arsenault923712b2018-02-09 16:57:57 +0000216 SDValue Expand32BitAddress(SDValue Addr) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000217 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset,
218 bool &Imm) const;
219 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000220 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
Tom Stellarddee26a22015-08-06 19:28:30 +0000221 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
222 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const;
Marek Olsak8973a0a2017-05-24 14:53:50 +0000223 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const;
Nicolai Haehnle7968c342016-07-12 08:12:16 +0000224 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const;
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000225
226 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000227 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods) const;
Tom Stellardb4a313a2014-08-01 00:32:39 +0000228 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Matt Arsenaultdf58e822017-04-25 21:17:38 +0000229 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const;
Tom Stellardb4a313a2014-08-01 00:32:39 +0000230 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
231 SDValue &Clamp, SDValue &Omod) const;
Tom Stellarddb5a11f2015-07-13 15:47:57 +0000232 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
233 SDValue &Clamp, SDValue &Omod) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000234
Matt Arsenault4831ce52015-01-06 23:00:37 +0000235 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods,
236 SDValue &Clamp,
237 SDValue &Omod) const;
Matt Arsenault1cffa4c2014-11-13 19:49:04 +0000238
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +0000239 bool SelectVOP3OMods(SDValue In, SDValue &Src,
240 SDValue &Clamp, SDValue &Omod) const;
241
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000242 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
243 bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
244 SDValue &Clamp) const;
245
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +0000246 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const;
247 bool SelectVOP3OpSel0(SDValue In, SDValue &Src, SDValue &SrcMods,
248 SDValue &Clamp) const;
249
250 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
251 bool SelectVOP3OpSelMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
252 SDValue &Clamp) const;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000253 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const;
Matt Arsenault76935122017-09-20 20:28:39 +0000254 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +0000255
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000256 SDValue getHi16Elt(SDValue In) const;
Matt Arsenaulte1cd4822017-11-13 00:22:09 +0000257
Justin Bogner95927c02016-05-12 21:03:32 +0000258 void SelectADD_SUB_I64(SDNode *N);
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000259 void SelectAddcSubb(SDNode *N);
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000260 void SelectUADDO_USUBO(SDNode *N);
Justin Bogner95927c02016-05-12 21:03:32 +0000261 void SelectDIV_SCALE(SDNode *N);
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000262 void SelectDIV_FMAS(SDNode *N);
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000263 void SelectMAD_64_32(SDNode *N);
Tom Stellard8485fa02016-12-07 02:42:15 +0000264 void SelectFMA_W_CHAIN(SDNode *N);
265 void SelectFMUL_W_CHAIN(SDNode *N);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000266
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000267 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val,
Marek Olsak9b728682015-03-24 13:40:27 +0000268 uint32_t Offset, uint32_t Width);
Justin Bogner95927c02016-05-12 21:03:32 +0000269 void SelectS_BFEFromShifts(SDNode *N);
270 void SelectS_BFE(SDNode *N);
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +0000271 bool isCBranchSCC(const SDNode *N) const;
Justin Bogner95927c02016-05-12 21:03:32 +0000272 void SelectBRCOND(SDNode *N);
Matt Arsenault0084adc2018-04-30 19:08:16 +0000273 void SelectFMAD_FMA(SDNode *N);
Matt Arsenault88701812016-06-09 23:42:48 +0000274 void SelectATOMIC_CMP_SWAP(SDNode *N);
Matt Arsenaultd3c84e62019-06-14 13:26:32 +0000275 void SelectDSAppendConsume(SDNode *N, unsigned IntrID);
Matt Arsenault4d55d022019-06-19 19:55:27 +0000276 void SelectDS_GWS(SDNode *N, unsigned IntrID);
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000277 void SelectINTRINSIC_W_CHAIN(SDNode *N);
Matt Arsenault4d55d022019-06-19 19:55:27 +0000278 void SelectINTRINSIC_VOID(SDNode *N);
Marek Olsak9b728682015-03-24 13:40:27 +0000279
Tom Stellard20287692017-08-08 04:57:55 +0000280protected:
Tom Stellard75aadc22012-12-11 21:25:42 +0000281 // Include the pieces autogenerated from the target description.
282#include "AMDGPUGenDAGISel.inc"
283};
Eugene Zelenko2bc2f332016-12-09 22:06:55 +0000284
Tom Stellard20287692017-08-08 04:57:55 +0000285class R600DAGToDAGISel : public AMDGPUDAGToDAGISel {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000286 const R600Subtarget *Subtarget;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000287
288 bool isConstantLoad(const MemSDNode *N, int cbID) const;
289 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
290 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
291 SDValue& Offset);
Tom Stellard20287692017-08-08 04:57:55 +0000292public:
293 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) :
Matt Arsenault0da63502018-08-31 05:49:54 +0000294 AMDGPUDAGToDAGISel(TM, OptLevel) {}
Tom Stellard20287692017-08-08 04:57:55 +0000295
296 void Select(SDNode *N) override;
297
298 bool SelectADDRIndirect(SDValue Addr, SDValue &Base,
299 SDValue &Offset) override;
300 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
301 SDValue &Offset) override;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000302
303 bool runOnMachineFunction(MachineFunction &MF) override;
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000304
305 void PreprocessISelDAG() override {}
306
Tom Stellardc5a154d2018-06-28 23:47:12 +0000307protected:
308 // Include the pieces autogenerated from the target description.
309#include "R600GenDAGISel.inc"
Tom Stellard20287692017-08-08 04:57:55 +0000310};
311
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000312static SDValue stripBitcast(SDValue Val) {
313 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
314}
315
316// Figure out if this is really an extract of the high 16-bits of a dword.
317static bool isExtractHiElt(SDValue In, SDValue &Out) {
318 In = stripBitcast(In);
319 if (In.getOpcode() != ISD::TRUNCATE)
320 return false;
321
322 SDValue Srl = In.getOperand(0);
323 if (Srl.getOpcode() == ISD::SRL) {
324 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
325 if (ShiftAmt->getZExtValue() == 16) {
326 Out = stripBitcast(Srl.getOperand(0));
327 return true;
328 }
329 }
330 }
331
332 return false;
333}
334
335// Look through operations that obscure just looking at the low 16-bits of the
336// same register.
337static SDValue stripExtractLoElt(SDValue In) {
338 if (In.getOpcode() == ISD::TRUNCATE) {
339 SDValue Src = In.getOperand(0);
340 if (Src.getValueType().getSizeInBits() == 32)
341 return stripBitcast(Src);
342 }
343
344 return In;
345}
346
Tom Stellard75aadc22012-12-11 21:25:42 +0000347} // end anonymous namespace
348
Fangrui Song3d76d362018-10-03 03:38:22 +0000349INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "amdgpu-isel",
Matt Arsenault7016f132017-08-03 22:30:46 +0000350 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
351INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo)
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000352INITIALIZE_PASS_DEPENDENCY(AMDGPUPerfHintAnalysis)
Nicolai Haehnle35617ed2018-08-30 14:21:36 +0000353INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
Fangrui Song3d76d362018-10-03 03:38:22 +0000354INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "amdgpu-isel",
Matt Arsenault7016f132017-08-03 22:30:46 +0000355 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
356
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000357/// This pass converts a legalized DAG into a AMDGPU-specific
Tom Stellard75aadc22012-12-11 21:25:42 +0000358// DAG, ready for instruction scheduling.
Matt Arsenault7016f132017-08-03 22:30:46 +0000359FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM,
Konstantin Zhuravlyov60a83732016-10-03 18:47:26 +0000360 CodeGenOpt::Level OptLevel) {
361 return new AMDGPUDAGToDAGISel(TM, OptLevel);
Tom Stellard75aadc22012-12-11 21:25:42 +0000362}
363
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000364/// This pass converts a legalized DAG into a R600-specific
Tom Stellard20287692017-08-08 04:57:55 +0000365// DAG, ready for instruction scheduling.
366FunctionPass *llvm::createR600ISelDag(TargetMachine *TM,
367 CodeGenOpt::Level OptLevel) {
368 return new R600DAGToDAGISel(TM, OptLevel);
369}
370
Eric Christopher7792e322015-01-30 23:24:40 +0000371bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000372 Subtarget = &MF.getSubtarget<GCNSubtarget>();
Eric Christopher7792e322015-01-30 23:24:40 +0000373 return SelectionDAGISel::runOnMachineFunction(MF);
Tom Stellard75aadc22012-12-11 21:25:42 +0000374}
375
Matt Arsenaulte8c03a22019-03-08 20:58:11 +0000376bool AMDGPUDAGToDAGISel::matchLoadD16FromBuildVector(SDNode *N) const {
377 assert(Subtarget->d16PreservesUnusedBits());
378 MVT VT = N->getValueType(0).getSimpleVT();
379 if (VT != MVT::v2i16 && VT != MVT::v2f16)
380 return false;
381
382 SDValue Lo = N->getOperand(0);
383 SDValue Hi = N->getOperand(1);
384
385 LoadSDNode *LdHi = dyn_cast<LoadSDNode>(stripBitcast(Hi));
386
387 // build_vector lo, (load ptr) -> load_d16_hi ptr, lo
388 // build_vector lo, (zextload ptr from i8) -> load_d16_hi_u8 ptr, lo
389 // build_vector lo, (sextload ptr from i8) -> load_d16_hi_i8 ptr, lo
390
391 // Need to check for possible indirect dependencies on the other half of the
392 // vector to avoid introducing a cycle.
393 if (LdHi && Hi.hasOneUse() && !LdHi->isPredecessorOf(Lo.getNode())) {
394 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
395
396 SDValue TiedIn = CurDAG->getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Lo);
397 SDValue Ops[] = {
398 LdHi->getChain(), LdHi->getBasePtr(), TiedIn
399 };
400
401 unsigned LoadOp = AMDGPUISD::LOAD_D16_HI;
402 if (LdHi->getMemoryVT() == MVT::i8) {
403 LoadOp = LdHi->getExtensionType() == ISD::SEXTLOAD ?
404 AMDGPUISD::LOAD_D16_HI_I8 : AMDGPUISD::LOAD_D16_HI_U8;
405 } else {
406 assert(LdHi->getMemoryVT() == MVT::i16);
407 }
408
409 SDValue NewLoadHi =
410 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdHi), VTList,
411 Ops, LdHi->getMemoryVT(),
412 LdHi->getMemOperand());
413
414 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadHi);
415 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdHi, 1), NewLoadHi.getValue(1));
416 return true;
417 }
418
419 // build_vector (load ptr), hi -> load_d16_lo ptr, hi
420 // build_vector (zextload ptr from i8), hi -> load_d16_lo_u8 ptr, hi
421 // build_vector (sextload ptr from i8), hi -> load_d16_lo_i8 ptr, hi
422 LoadSDNode *LdLo = dyn_cast<LoadSDNode>(stripBitcast(Lo));
423 if (LdLo && Lo.hasOneUse()) {
424 SDValue TiedIn = getHi16Elt(Hi);
425 if (!TiedIn || LdLo->isPredecessorOf(TiedIn.getNode()))
426 return false;
427
428 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other);
429 unsigned LoadOp = AMDGPUISD::LOAD_D16_LO;
430 if (LdLo->getMemoryVT() == MVT::i8) {
431 LoadOp = LdLo->getExtensionType() == ISD::SEXTLOAD ?
432 AMDGPUISD::LOAD_D16_LO_I8 : AMDGPUISD::LOAD_D16_LO_U8;
433 } else {
434 assert(LdLo->getMemoryVT() == MVT::i16);
435 }
436
437 TiedIn = CurDAG->getNode(ISD::BITCAST, SDLoc(N), VT, TiedIn);
438
439 SDValue Ops[] = {
440 LdLo->getChain(), LdLo->getBasePtr(), TiedIn
441 };
442
443 SDValue NewLoadLo =
444 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdLo), VTList,
445 Ops, LdLo->getMemoryVT(),
446 LdLo->getMemOperand());
447
448 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadLo);
449 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdLo, 1), NewLoadLo.getValue(1));
450 return true;
451 }
452
453 return false;
454}
455
456void AMDGPUDAGToDAGISel::PreprocessISelDAG() {
457 if (!Subtarget->d16PreservesUnusedBits())
458 return;
459
460 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
461
462 bool MadeChange = false;
463 while (Position != CurDAG->allnodes_begin()) {
464 SDNode *N = &*--Position;
465 if (N->use_empty())
466 continue;
467
468 switch (N->getOpcode()) {
469 case ISD::BUILD_VECTOR:
470 MadeChange |= matchLoadD16FromBuildVector(N);
471 break;
472 default:
473 break;
474 }
475 }
476
477 if (MadeChange) {
478 CurDAG->RemoveDeadNodes();
479 LLVM_DEBUG(dbgs() << "After PreProcess:\n";
480 CurDAG->dump(););
481 }
482}
483
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000484bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const {
485 if (TM.Options.NoNaNsFPMath)
486 return true;
487
488 // TODO: Move into isKnownNeverNaN
Amara Emersond28f0cd42017-05-01 15:17:51 +0000489 if (N->getFlags().isDefined())
490 return N->getFlags().hasNoNaNs();
Matt Arsenaultf84e5d92017-01-31 03:07:46 +0000491
492 return CurDAG->isKnownNeverNaN(N);
493}
494
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000495bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N,
496 bool Negated) const {
Matt Arsenaultb7f87c02019-06-20 16:01:09 +0000497 if (N->isUndef())
498 return true;
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000499
Tom Stellardc5a154d2018-06-28 23:47:12 +0000500 const SIInstrInfo *TII = Subtarget->getInstrInfo();
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000501 if (Negated) {
502 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
503 return TII->isInlineConstant(-C->getAPIntValue());
Matt Arsenaultfe267752016-07-28 00:32:02 +0000504
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000505 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
506 return TII->isInlineConstant(-C->getValueAPF().bitcastToAPInt());
Matt Arsenaultfe267752016-07-28 00:32:02 +0000507
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000508 } else {
509 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
510 return TII->isInlineConstant(C->getAPIntValue());
511
512 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
513 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt());
514 }
Matt Arsenaultfe267752016-07-28 00:32:02 +0000515
516 return false;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000517}
518
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000519/// Determine the register class for \p OpNo
Tom Stellarddf94dc32013-08-14 23:24:24 +0000520/// \returns The register class of the virtual register that will be used for
521/// the given operand number \OpNo or NULL if the register class cannot be
522/// determined.
523const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
524 unsigned OpNo) const {
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000525 if (!N->isMachineOpcode()) {
526 if (N->getOpcode() == ISD::CopyToReg) {
527 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
528 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
529 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
530 return MRI.getRegClass(Reg);
531 }
532
533 const SIRegisterInfo *TRI
Tom Stellard5bfbae52018-07-11 20:59:01 +0000534 = static_cast<const GCNSubtarget *>(Subtarget)->getRegisterInfo();
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000535 return TRI->getPhysRegClass(Reg);
536 }
537
Matt Arsenault209a7b92014-04-18 07:40:20 +0000538 return nullptr;
Matt Arsenaultc507cdb2016-11-01 23:22:17 +0000539 }
Matt Arsenault209a7b92014-04-18 07:40:20 +0000540
Tom Stellarddf94dc32013-08-14 23:24:24 +0000541 switch (N->getMachineOpcode()) {
542 default: {
Eric Christopherd9134482014-08-04 21:25:23 +0000543 const MCInstrDesc &Desc =
Eric Christopher7792e322015-01-30 23:24:40 +0000544 Subtarget->getInstrInfo()->get(N->getMachineOpcode());
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000545 unsigned OpIdx = Desc.getNumDefs() + OpNo;
546 if (OpIdx >= Desc.getNumOperands())
Matt Arsenault209a7b92014-04-18 07:40:20 +0000547 return nullptr;
Alexey Samsonov3186eb32013-08-15 07:11:34 +0000548 int RegClass = Desc.OpInfo[OpIdx].RegClass;
Matt Arsenault209a7b92014-04-18 07:40:20 +0000549 if (RegClass == -1)
550 return nullptr;
551
Eric Christopher7792e322015-01-30 23:24:40 +0000552 return Subtarget->getRegisterInfo()->getRegClass(RegClass);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000553 }
554 case AMDGPU::REG_SEQUENCE: {
Matt Arsenault209a7b92014-04-18 07:40:20 +0000555 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
Eric Christopherd9134482014-08-04 21:25:23 +0000556 const TargetRegisterClass *SuperRC =
Eric Christopher7792e322015-01-30 23:24:40 +0000557 Subtarget->getRegisterInfo()->getRegClass(RCID);
Matt Arsenault209a7b92014-04-18 07:40:20 +0000558
559 SDValue SubRegOp = N->getOperand(OpNo + 1);
560 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
Eric Christopher7792e322015-01-30 23:24:40 +0000561 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC,
562 SubRegIdx);
Tom Stellarddf94dc32013-08-14 23:24:24 +0000563 }
564 }
565}
566
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000567SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N, SDValue Val) const {
Tom Stellard381a94a2015-05-12 15:00:49 +0000568 const SITargetLowering& Lowering =
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000569 *static_cast<const SITargetLowering*>(getTargetLowering());
Tom Stellard381a94a2015-05-12 15:00:49 +0000570
571 // Write max value to m0 before each load operation
572
Matt Arsenault5a86dbc2019-06-14 13:33:36 +0000573 assert(N->getOperand(0).getValueType() == MVT::Other && "Expected chain");
574
575 SDValue M0 = Lowering.copyToM0(*CurDAG, N->getOperand(0), SDLoc(N),
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000576 Val);
Tom Stellard381a94a2015-05-12 15:00:49 +0000577
578 SDValue Glue = M0.getValue(1);
579
580 SmallVector <SDValue, 8> Ops;
Matt Arsenault5a86dbc2019-06-14 13:33:36 +0000581 Ops.push_back(M0); // Replace the chain.
582 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000583 Ops.push_back(N->getOperand(i));
584
Tom Stellard381a94a2015-05-12 15:00:49 +0000585 Ops.push_back(Glue);
Matt Arsenaulte6667de2017-12-04 22:18:22 +0000586 return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops);
Tom Stellard381a94a2015-05-12 15:00:49 +0000587}
588
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000589SDNode *AMDGPUDAGToDAGISel::glueCopyToM0LDSInit(SDNode *N) const {
590 if (cast<MemSDNode>(N)->getAddressSpace() != AMDGPUAS::LOCAL_ADDRESS ||
591 !Subtarget->ldsRequiresM0Init())
592 return N;
593 return glueCopyToM0(N, CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32));
594}
595
Tim Renouff1c7b922018-08-02 22:53:57 +0000596MachineSDNode *AMDGPUDAGToDAGISel::buildSMovImm64(SDLoc &DL, uint64_t Imm,
597 EVT VT) const {
598 SDNode *Lo = CurDAG->getMachineNode(
599 AMDGPU::S_MOV_B32, DL, MVT::i32,
600 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, MVT::i32));
601 SDNode *Hi =
602 CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
603 CurDAG->getConstant(Imm >> 32, DL, MVT::i32));
604 const SDValue Ops[] = {
605 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
606 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
607 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)};
608
609 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops);
610}
611
Matt Arsenault61cb6fa2015-11-11 00:01:36 +0000612static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) {
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000613 switch (NumVectorElts) {
614 case 1:
Marek Olsak79c05872016-11-25 17:37:09 +0000615 return AMDGPU::SReg_32_XM0RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000616 case 2:
617 return AMDGPU::SReg_64RegClassID;
Tim Renouf361b5b22019-03-21 12:01:21 +0000618 case 3:
619 return AMDGPU::SGPR_96RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000620 case 4:
621 return AMDGPU::SReg_128RegClassID;
Tim Renouf033f99a2019-03-22 10:11:21 +0000622 case 5:
623 return AMDGPU::SGPR_160RegClassID;
Matt Arsenaultf1aebbf2015-11-02 23:30:48 +0000624 case 8:
625 return AMDGPU::SReg_256RegClassID;
626 case 16:
627 return AMDGPU::SReg_512RegClassID;
628 }
629
630 llvm_unreachable("invalid vector size");
631}
632
Tom Stellard20287692017-08-08 04:57:55 +0000633void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) {
Tom Stellard20287692017-08-08 04:57:55 +0000634 EVT VT = N->getValueType(0);
635 unsigned NumVectorElts = VT.getVectorNumElements();
636 EVT EltVT = VT.getVectorElementType();
Tom Stellard20287692017-08-08 04:57:55 +0000637 SDLoc DL(N);
638 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
639
640 if (NumVectorElts == 1) {
641 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0),
642 RegClass);
643 return;
644 }
645
646 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
647 "supported yet");
648 // 16 = Max Num Vector Elements
649 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
650 // 1 = Vector Register Class
651 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
652
653 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
654 bool IsRegSeq = true;
655 unsigned NOps = N->getNumOperands();
656 for (unsigned i = 0; i < NOps; i++) {
657 // XXX: Why is this here?
658 if (isa<RegisterSDNode>(N->getOperand(i))) {
659 IsRegSeq = false;
660 break;
661 }
Simon Pilgrimede0e402018-05-19 12:46:02 +0000662 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
Tom Stellard20287692017-08-08 04:57:55 +0000663 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
Simon Pilgrimede0e402018-05-19 12:46:02 +0000664 RegSeqArgs[1 + (2 * i) + 1] = CurDAG->getTargetConstant(Sub, DL, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +0000665 }
666 if (NOps != NumVectorElts) {
667 // Fill in the missing undef elements if this was a scalar_to_vector.
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000668 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
Tom Stellard20287692017-08-08 04:57:55 +0000669 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
670 DL, EltVT);
671 for (unsigned i = NOps; i < NumVectorElts; ++i) {
Simon Pilgrimede0e402018-05-19 12:46:02 +0000672 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
Tom Stellard20287692017-08-08 04:57:55 +0000673 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
674 RegSeqArgs[1 + (2 * i) + 1] =
Simon Pilgrimede0e402018-05-19 12:46:02 +0000675 CurDAG->getTargetConstant(Sub, DL, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +0000676 }
677 }
678
679 if (!IsRegSeq)
680 SelectCode(N);
681 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs);
682}
683
Justin Bogner95927c02016-05-12 21:03:32 +0000684void AMDGPUDAGToDAGISel::Select(SDNode *N) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000685 unsigned int Opc = N->getOpcode();
686 if (N->isMachineOpcode()) {
Tim Northover31d093c2013-09-22 08:21:56 +0000687 N->setNodeId(-1);
Justin Bogner95927c02016-05-12 21:03:32 +0000688 return; // Already selected.
Tom Stellard75aadc22012-12-11 21:25:42 +0000689 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000690
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000691 if (isa<AtomicSDNode>(N) ||
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000692 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC ||
Matt Arsenaulta5840c32019-01-22 18:36:06 +0000693 Opc == ISD::ATOMIC_LOAD_FADD ||
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000694 Opc == AMDGPUISD::ATOMIC_LOAD_FMIN ||
695 Opc == AMDGPUISD::ATOMIC_LOAD_FMAX))
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000696 N = glueCopyToM0LDSInit(N);
Tom Stellard381a94a2015-05-12 15:00:49 +0000697
Tom Stellard75aadc22012-12-11 21:25:42 +0000698 switch (Opc) {
Matt Arsenault84445dd2017-11-30 22:51:26 +0000699 default:
700 break;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000701 // We are selecting i64 ADD here instead of custom lower it during
702 // DAG legalization, so we can fold some i64 ADDs used for address
703 // calculation into the LOAD and STORE instructions.
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000704 case ISD::ADDC:
705 case ISD::ADDE:
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000706 case ISD::SUBC:
707 case ISD::SUBE: {
Tom Stellard20287692017-08-08 04:57:55 +0000708 if (N->getValueType(0) != MVT::i64)
Tom Stellard1f15bff2014-02-25 21:36:18 +0000709 break;
710
Justin Bogner95927c02016-05-12 21:03:32 +0000711 SelectADD_SUB_I64(N);
712 return;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000713 }
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000714 case ISD::ADDCARRY:
715 case ISD::SUBCARRY:
716 if (N->getValueType(0) != MVT::i32)
717 break;
718
719 SelectAddcSubb(N);
720 return;
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +0000721 case ISD::UADDO:
722 case ISD::USUBO: {
723 SelectUADDO_USUBO(N);
724 return;
725 }
Tom Stellard8485fa02016-12-07 02:42:15 +0000726 case AMDGPUISD::FMUL_W_CHAIN: {
727 SelectFMUL_W_CHAIN(N);
728 return;
729 }
730 case AMDGPUISD::FMA_W_CHAIN: {
731 SelectFMA_W_CHAIN(N);
732 return;
733 }
734
Matt Arsenault064c2062014-06-11 17:40:32 +0000735 case ISD::SCALAR_TO_VECTOR:
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000736 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000737 EVT VT = N->getValueType(0);
738 unsigned NumVectorElts = VT.getVectorNumElements();
Matt Arsenault5a4ec812018-06-20 19:45:48 +0000739 if (VT.getScalarSizeInBits() == 16) {
740 if (Opc == ISD::BUILD_VECTOR && NumVectorElts == 2) {
Matt Arsenaulte24b34e2019-06-19 23:37:43 +0000741 if (SDNode *Packed = packConstantV2I16(N, *CurDAG)) {
742 ReplaceNode(N, Packed);
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000743 return;
744 }
745 }
746
747 break;
748 }
749
Tom Stellard03aa3ae2017-08-08 05:52:00 +0000750 assert(VT.getVectorElementType().bitsEq(MVT::i32));
Tom Stellard20287692017-08-08 04:57:55 +0000751 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts);
752 SelectBuildVector(N, RegClassID);
Justin Bogner95927c02016-05-12 21:03:32 +0000753 return;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000754 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000755 case ISD::BUILD_PAIR: {
756 SDValue RC, SubReg0, SubReg1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000757 SDLoc DL(N);
Tom Stellard754f80f2013-04-05 23:31:51 +0000758 if (N->getValueType(0) == MVT::i128) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000759 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32);
760 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32);
761 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000762 } else if (N->getValueType(0) == MVT::i64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000763 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32);
764 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
765 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000766 } else {
767 llvm_unreachable("Unhandled value type for BUILD_PAIR");
768 }
769 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
770 N->getOperand(1), SubReg1 };
Justin Bogner95927c02016-05-12 21:03:32 +0000771 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
772 N->getValueType(0), Ops));
773 return;
Tom Stellard754f80f2013-04-05 23:31:51 +0000774 }
Tom Stellard7ed0b522014-04-03 20:19:27 +0000775
776 case ISD::Constant:
777 case ISD::ConstantFP: {
Tom Stellard20287692017-08-08 04:57:55 +0000778 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
Tom Stellard7ed0b522014-04-03 20:19:27 +0000779 break;
780
781 uint64_t Imm;
782 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
783 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
784 else {
Tom Stellard3cbe0142014-04-07 19:31:13 +0000785 ConstantSDNode *C = cast<ConstantSDNode>(N);
Tom Stellard7ed0b522014-04-03 20:19:27 +0000786 Imm = C->getZExtValue();
787 }
788
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000789 SDLoc DL(N);
Tim Renouff1c7b922018-08-02 22:53:57 +0000790 ReplaceNode(N, buildSMovImm64(DL, Imm, N->getValueType(0)));
Justin Bogner95927c02016-05-12 21:03:32 +0000791 return;
Tom Stellard7ed0b522014-04-03 20:19:27 +0000792 }
Matt Arsenault4bf43d42015-09-25 17:27:08 +0000793 case ISD::LOAD:
Matt Arsenault3f8e7a32018-06-22 08:39:52 +0000794 case ISD::STORE:
795 case ISD::ATOMIC_LOAD:
796 case ISD::ATOMIC_STORE: {
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000797 N = glueCopyToM0LDSInit(N);
Tom Stellard096b8c12015-02-04 20:49:49 +0000798 break;
799 }
Matt Arsenault78b86702014-04-18 05:19:26 +0000800
801 case AMDGPUISD::BFE_I32:
802 case AMDGPUISD::BFE_U32: {
Matt Arsenault78b86702014-04-18 05:19:26 +0000803 // There is a scalar version available, but unlike the vector version which
804 // has a separate operand for the offset and width, the scalar version packs
805 // the width and offset into a single operand. Try to move to the scalar
806 // version if the offsets are constant, so that we can try to keep extended
807 // loads of kernel arguments in SGPRs.
808
809 // TODO: Technically we could try to pattern match scalar bitshifts of
810 // dynamic values, but it's probably not useful.
811 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
812 if (!Offset)
813 break;
814
815 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
816 if (!Width)
817 break;
818
819 bool Signed = Opc == AMDGPUISD::BFE_I32;
820
Matt Arsenault78b86702014-04-18 05:19:26 +0000821 uint32_t OffsetVal = Offset->getZExtValue();
822 uint32_t WidthVal = Width->getZExtValue();
823
Justin Bogner95927c02016-05-12 21:03:32 +0000824 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
825 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal));
826 return;
Matt Arsenault78b86702014-04-18 05:19:26 +0000827 }
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000828 case AMDGPUISD::DIV_SCALE: {
Justin Bogner95927c02016-05-12 21:03:32 +0000829 SelectDIV_SCALE(N);
830 return;
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +0000831 }
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +0000832 case AMDGPUISD::DIV_FMAS: {
833 SelectDIV_FMAS(N);
834 return;
835 }
Matt Arsenault4f6318f2017-11-06 17:04:37 +0000836 case AMDGPUISD::MAD_I64_I32:
837 case AMDGPUISD::MAD_U64_U32: {
838 SelectMAD_64_32(N);
839 return;
840 }
Tom Stellard3457a842014-10-09 19:06:00 +0000841 case ISD::CopyToReg: {
842 const SITargetLowering& Lowering =
843 *static_cast<const SITargetLowering*>(getTargetLowering());
Matt Arsenault0d0d6c22017-04-12 21:58:23 +0000844 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG);
Tom Stellard3457a842014-10-09 19:06:00 +0000845 break;
846 }
Marek Olsak9b728682015-03-24 13:40:27 +0000847 case ISD::AND:
848 case ISD::SRL:
849 case ISD::SRA:
Matt Arsenault7e8de012016-04-22 22:59:16 +0000850 case ISD::SIGN_EXTEND_INREG:
Tom Stellard20287692017-08-08 04:57:55 +0000851 if (N->getValueType(0) != MVT::i32)
Marek Olsak9b728682015-03-24 13:40:27 +0000852 break;
853
Justin Bogner95927c02016-05-12 21:03:32 +0000854 SelectS_BFE(N);
855 return;
Tom Stellardbc4497b2016-02-12 23:45:29 +0000856 case ISD::BRCOND:
Justin Bogner95927c02016-05-12 21:03:32 +0000857 SelectBRCOND(N);
858 return;
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000859 case ISD::FMAD:
Matt Arsenault0084adc2018-04-30 19:08:16 +0000860 case ISD::FMA:
861 SelectFMAD_FMA(N);
Matt Arsenaultd7e23032017-09-07 18:05:07 +0000862 return;
Matt Arsenault88701812016-06-09 23:42:48 +0000863 case AMDGPUISD::ATOMIC_CMP_SWAP:
864 SelectATOMIC_CMP_SWAP(N);
865 return;
Matt Arsenault709374d2018-08-01 20:13:58 +0000866 case AMDGPUISD::CVT_PKRTZ_F16_F32:
867 case AMDGPUISD::CVT_PKNORM_I16_F32:
868 case AMDGPUISD::CVT_PKNORM_U16_F32:
869 case AMDGPUISD::CVT_PK_U16_U32:
870 case AMDGPUISD::CVT_PK_I16_I32: {
871 // Hack around using a legal type if f16 is illegal.
872 if (N->getValueType(0) == MVT::i32) {
873 MVT NewVT = Opc == AMDGPUISD::CVT_PKRTZ_F16_F32 ? MVT::v2f16 : MVT::v2i16;
874 N = CurDAG->MorphNodeTo(N, N->getOpcode(), CurDAG->getVTList(NewVT),
875 { N->getOperand(0), N->getOperand(1) });
876 SelectCode(N);
877 return;
878 }
Matt Arsenaultcdd191d2019-01-28 20:14:49 +0000879
880 break;
881 }
882 case ISD::INTRINSIC_W_CHAIN: {
883 SelectINTRINSIC_W_CHAIN(N);
884 return;
Matt Arsenault709374d2018-08-01 20:13:58 +0000885 }
Matt Arsenault4d55d022019-06-19 19:55:27 +0000886 case ISD::INTRINSIC_VOID: {
887 SelectINTRINSIC_VOID(N);
888 return;
889 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000890 }
Tom Stellard3457a842014-10-09 19:06:00 +0000891
Justin Bogner95927c02016-05-12 21:03:32 +0000892 SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000893}
894
Tom Stellardbc4497b2016-02-12 23:45:29 +0000895bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const {
896 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
Nicolai Haehnle05b127d2016-04-14 17:42:35 +0000897 const Instruction *Term = BB->getTerminator();
898 return Term->getMetadata("amdgpu.uniform") ||
899 Term->getMetadata("structurizecfg.uniform");
Tom Stellardbc4497b2016-02-12 23:45:29 +0000900}
901
Mehdi Amini117296c2016-10-01 02:56:57 +0000902StringRef AMDGPUDAGToDAGISel::getPassName() const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000903 return "AMDGPU DAG->DAG Pattern Instruction Selection";
904}
905
Tom Stellard41fc7852013-07-23 01:48:42 +0000906//===----------------------------------------------------------------------===//
907// Complex Patterns
908//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000909
Tom Stellard75aadc22012-12-11 21:25:42 +0000910bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
Tom Stellard20287692017-08-08 04:57:55 +0000911 SDValue &Offset) {
912 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000913}
914
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000915bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
916 SDValue &Offset) {
917 ConstantSDNode *C;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000918 SDLoc DL(Addr);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000919
920 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000921 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000922 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000923 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
924 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000925 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Jan Vesely06200bd2017-01-06 21:00:46 +0000926 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000927 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
928 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
929 Base = Addr.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000930 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000931 } else {
932 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000933 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000934 }
935
936 return true;
937}
Christian Konigd910b7d2013-02-26 17:52:16 +0000938
Matt Arsenault84445dd2017-11-30 22:51:26 +0000939// FIXME: Should only handle addcarry/subcarry
Justin Bogner95927c02016-05-12 21:03:32 +0000940void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000941 SDLoc DL(N);
942 SDValue LHS = N->getOperand(0);
943 SDValue RHS = N->getOperand(1);
944
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000945 unsigned Opcode = N->getOpcode();
946 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE);
947 bool ProduceCarry =
948 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC;
Matt Arsenault84445dd2017-11-30 22:51:26 +0000949 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000950
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000951 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
952 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000953
954 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
955 DL, MVT::i32, LHS, Sub0);
956 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
957 DL, MVT::i32, LHS, Sub1);
958
959 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
960 DL, MVT::i32, RHS, Sub0);
961 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
962 DL, MVT::i32, RHS, Sub1);
963
964 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000965
Tom Stellard80942a12014-09-05 14:07:59 +0000966 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
Matt Arsenaultb8b51532014-06-23 18:00:38 +0000967 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
968
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000969 SDNode *AddLo;
970 if (!ConsumeCarry) {
971 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
972 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args);
973 } else {
974 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) };
975 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args);
976 }
977 SDValue AddHiArgs[] = {
978 SDValue(Hi0, 0),
979 SDValue(Hi1, 0),
980 SDValue(AddLo, 1)
981 };
982 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs);
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000983
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000984 SDValue RegSequenceArgs[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000985 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
Matt Arsenault9fa3f932014-06-23 18:00:34 +0000986 SDValue(AddLo,0),
987 Sub0,
988 SDValue(AddHi,0),
989 Sub1,
990 };
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000991 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
992 MVT::i64, RegSequenceArgs);
993
994 if (ProduceCarry) {
995 // Replace the carry-use
Nirav Dave3264c1b2018-03-19 20:19:46 +0000996 ReplaceUses(SDValue(N, 1), SDValue(AddHi, 1));
Nicolai Haehnle67624af2016-10-14 10:30:00 +0000997 }
998
999 // Replace the remaining uses.
Nirav Dave3264c1b2018-03-19 20:19:46 +00001000 ReplaceNode(N, RegSequence);
Matt Arsenault9fa3f932014-06-23 18:00:34 +00001001}
1002
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001003void AMDGPUDAGToDAGISel::SelectAddcSubb(SDNode *N) {
1004 SDLoc DL(N);
1005 SDValue LHS = N->getOperand(0);
1006 SDValue RHS = N->getOperand(1);
1007 SDValue CI = N->getOperand(2);
1008
1009 unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::V_ADDC_U32_e64
1010 : AMDGPU::V_SUBB_U32_e64;
1011 CurDAG->SelectNodeTo(
1012 N, Opc, N->getVTList(),
1013 {LHS, RHS, CI, CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
1014}
1015
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +00001016void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) {
1017 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned
1018 // carry out despite the _i32 name. These were renamed in VI to _U32.
1019 // FIXME: We should probably rename the opcodes here.
1020 unsigned Opc = N->getOpcode() == ISD::UADDO ?
1021 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64;
1022
Michael Liaoeea51772019-03-20 20:18:56 +00001023 CurDAG->SelectNodeTo(
1024 N, Opc, N->getVTList(),
1025 {N->getOperand(0), N->getOperand(1),
1026 CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/});
Matt Arsenaultee3f0ac2017-01-30 18:11:38 +00001027}
1028
Tom Stellard8485fa02016-12-07 02:42:15 +00001029void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) {
1030 SDLoc SL(N);
1031 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod
1032 SDValue Ops[10];
1033
1034 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]);
1035 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1036 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]);
1037 Ops[8] = N->getOperand(0);
1038 Ops[9] = N->getOperand(4);
1039
1040 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops);
1041}
1042
1043void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) {
1044 SDLoc SL(N);
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001045 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod
Tom Stellard8485fa02016-12-07 02:42:15 +00001046 SDValue Ops[8];
1047
1048 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]);
1049 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
1050 Ops[6] = N->getOperand(0);
1051 Ops[7] = N->getOperand(3);
1052
1053 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops);
1054}
1055
Matt Arsenault044f1d12015-02-14 04:24:28 +00001056// We need to handle this here because tablegen doesn't support matching
1057// instructions with multiple outputs.
Justin Bogner95927c02016-05-12 21:03:32 +00001058void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +00001059 SDLoc SL(N);
1060 EVT VT = N->getValueType(0);
1061
1062 assert(VT == MVT::f32 || VT == MVT::f64);
1063
1064 unsigned Opc
1065 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
1066
Matt Arsenault3b99f122017-01-19 06:04:12 +00001067 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1068 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
Matt Arsenaultf2b0aeb2014-06-23 18:28:28 +00001069}
1070
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001071void AMDGPUDAGToDAGISel::SelectDIV_FMAS(SDNode *N) {
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001072 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
1073 const SIRegisterInfo *TRI = ST->getRegisterInfo();
1074
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001075 SDLoc SL(N);
1076 EVT VT = N->getValueType(0);
1077
1078 assert(VT == MVT::f32 || VT == MVT::f64);
1079
1080 unsigned Opc
1081 = (VT == MVT::f64) ? AMDGPU::V_DIV_FMAS_F64 : AMDGPU::V_DIV_FMAS_F32;
1082
1083 SDValue CarryIn = N->getOperand(3);
1084 // V_DIV_FMAS implicitly reads VCC.
1085 SDValue VCC = CurDAG->getCopyToReg(CurDAG->getEntryNode(), SL,
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001086 TRI->getVCC(), CarryIn, SDValue());
Stanislav Mekhanoshin8f3da702019-04-26 16:37:51 +00001087
1088 SDValue Ops[10];
1089
1090 SelectVOP3Mods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]);
1091 SelectVOP3Mods(N->getOperand(1), Ops[3], Ops[2]);
1092 SelectVOP3Mods(N->getOperand(2), Ops[5], Ops[4]);
1093
1094 Ops[8] = VCC;
1095 Ops[9] = VCC.getValue(1);
1096
1097 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1098}
1099
Matt Arsenault4f6318f2017-11-06 17:04:37 +00001100// We need to handle this here because tablegen doesn't support matching
1101// instructions with multiple outputs.
1102void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) {
1103 SDLoc SL(N);
1104 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32;
1105 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32;
1106
1107 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1);
1108 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
1109 Clamp };
1110 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
1111}
1112
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00001113bool AMDGPUDAGToDAGISel::isDSOffsetLegal(SDValue Base, unsigned Offset,
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001114 unsigned OffsetBits) const {
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001115 if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
1116 (OffsetBits == 8 && !isUInt<8>(Offset)))
1117 return false;
1118
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00001119 if (Subtarget->hasUsableDSOffset() ||
Matt Arsenault706f9302015-07-06 16:01:58 +00001120 Subtarget->unsafeDSOffsetFoldingEnabled())
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001121 return true;
1122
1123 // On Southern Islands instruction with a negative base value and an offset
1124 // don't seem to work.
1125 return CurDAG->SignBitIsZero(Base);
1126}
1127
1128bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
1129 SDValue &Offset) const {
Tom Stellard92b24f32016-04-29 14:34:26 +00001130 SDLoc DL(Addr);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001131 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1132 SDValue N0 = Addr.getOperand(0);
1133 SDValue N1 = Addr.getOperand(1);
1134 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1135 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
1136 // (add n0, c0)
1137 Base = N0;
Tom Stellard92b24f32016-04-29 14:34:26 +00001138 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001139 return true;
1140 }
Matt Arsenault966a94f2015-09-08 19:34:22 +00001141 } else if (Addr.getOpcode() == ISD::SUB) {
1142 // sub C, x -> add (sub 0, x), C
1143 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1144 int64_t ByteOffset = C->getSExtValue();
1145 if (isUInt<16>(ByteOffset)) {
Matt Arsenault966a94f2015-09-08 19:34:22 +00001146 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001147
Matt Arsenault966a94f2015-09-08 19:34:22 +00001148 // XXX - This is kind of hacky. Create a dummy sub node so we can check
1149 // the known bits in isDSOffsetLegal. We need to emit the selected node
1150 // here, so this is thrown away.
1151 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1152 Zero, Addr.getOperand(1));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001153
Matt Arsenault966a94f2015-09-08 19:34:22 +00001154 if (isDSOffsetLegal(Sub, ByteOffset, 16)) {
Tim Renoufcfdfba92019-03-18 19:35:44 +00001155 SmallVector<SDValue, 3> Opnds;
1156 Opnds.push_back(Zero);
1157 Opnds.push_back(Addr.getOperand(1));
Matt Arsenault84445dd2017-11-30 22:51:26 +00001158
Tim Renoufcfdfba92019-03-18 19:35:44 +00001159 // FIXME: Select to VOP3 version for with-carry.
1160 unsigned SubOp = AMDGPU::V_SUB_I32_e32;
1161 if (Subtarget->hasAddNoCarry()) {
1162 SubOp = AMDGPU::V_SUB_U32_e64;
Michael Liaoeea51772019-03-20 20:18:56 +00001163 Opnds.push_back(
1164 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
Tim Renoufcfdfba92019-03-18 19:35:44 +00001165 }
1166
1167 MachineSDNode *MachineSub =
1168 CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001169
1170 Base = SDValue(MachineSub, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +00001171 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001172 return true;
1173 }
1174 }
1175 }
1176 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1177 // If we have a constant address, prefer to put the constant into the
1178 // offset. This can save moves to load the constant address since multiple
1179 // operations can share the zero base address register, and enables merging
1180 // into read2 / write2 instructions.
1181
1182 SDLoc DL(Addr);
1183
Matt Arsenaulte775f5f2014-10-14 17:21:19 +00001184 if (isUInt<16>(CAddr->getZExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001185 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellardc8d79202014-10-15 21:08:59 +00001186 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001187 DL, MVT::i32, Zero);
Tom Stellardc8d79202014-10-15 21:08:59 +00001188 Base = SDValue(MovZero, 0);
Tom Stellard26a2ab72016-06-10 00:01:04 +00001189 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
Matt Arsenaulte775f5f2014-10-14 17:21:19 +00001190 return true;
1191 }
1192 }
1193
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001194 // default case
1195 Base = Addr;
Matt Arsenault966a94f2015-09-08 19:34:22 +00001196 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16);
Tom Stellard85e8b6d2014-08-22 18:49:33 +00001197 return true;
1198}
1199
Matt Arsenault966a94f2015-09-08 19:34:22 +00001200// TODO: If offset is too big, put low 16-bit into offset.
Tom Stellardf3fc5552014-08-22 18:49:35 +00001201bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base,
1202 SDValue &Offset0,
1203 SDValue &Offset1) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001204 SDLoc DL(Addr);
1205
Tom Stellardf3fc5552014-08-22 18:49:35 +00001206 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1207 SDValue N0 = Addr.getOperand(0);
1208 SDValue N1 = Addr.getOperand(1);
1209 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1210 unsigned DWordOffset0 = C1->getZExtValue() / 4;
1211 unsigned DWordOffset1 = DWordOffset0 + 1;
1212 // (add n0, c0)
1213 if (isDSOffsetLegal(N0, DWordOffset1, 8)) {
1214 Base = N0;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1216 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +00001217 return true;
1218 }
Matt Arsenault966a94f2015-09-08 19:34:22 +00001219 } else if (Addr.getOpcode() == ISD::SUB) {
1220 // sub C, x -> add (sub 0, x), C
1221 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
1222 unsigned DWordOffset0 = C->getZExtValue() / 4;
1223 unsigned DWordOffset1 = DWordOffset0 + 1;
Tom Stellardf3fc5552014-08-22 18:49:35 +00001224
Matt Arsenault966a94f2015-09-08 19:34:22 +00001225 if (isUInt<8>(DWordOffset0)) {
1226 SDLoc DL(Addr);
1227 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
1228
1229 // XXX - This is kind of hacky. Create a dummy sub node so we can check
1230 // the known bits in isDSOffsetLegal. We need to emit the selected node
1231 // here, so this is thrown away.
1232 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
1233 Zero, Addr.getOperand(1));
1234
1235 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) {
Tim Renoufcfdfba92019-03-18 19:35:44 +00001236 SmallVector<SDValue, 3> Opnds;
1237 Opnds.push_back(Zero);
1238 Opnds.push_back(Addr.getOperand(1));
1239 unsigned SubOp = AMDGPU::V_SUB_I32_e32;
1240 if (Subtarget->hasAddNoCarry()) {
1241 SubOp = AMDGPU::V_SUB_U32_e64;
Michael Liaoeea51772019-03-20 20:18:56 +00001242 Opnds.push_back(
1243 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit
Tim Renoufcfdfba92019-03-18 19:35:44 +00001244 }
Matt Arsenault84445dd2017-11-30 22:51:26 +00001245
Matt Arsenault966a94f2015-09-08 19:34:22 +00001246 MachineSDNode *MachineSub
Tim Renoufcfdfba92019-03-18 19:35:44 +00001247 = CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds);
Matt Arsenault966a94f2015-09-08 19:34:22 +00001248
1249 Base = SDValue(MachineSub, 0);
1250 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1251 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
1252 return true;
1253 }
1254 }
1255 }
1256 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001257 unsigned DWordOffset0 = CAddr->getZExtValue() / 4;
1258 unsigned DWordOffset1 = DWordOffset0 + 1;
1259 assert(4 * DWordOffset0 == CAddr->getZExtValue());
1260
1261 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001262 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001263 MachineSDNode *MovZero
1264 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001265 DL, MVT::i32, Zero);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001266 Base = SDValue(MovZero, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001267 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
1268 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
Matt Arsenault1a74aff2014-10-15 18:06:43 +00001269 return true;
1270 }
1271 }
1272
Tom Stellardf3fc5552014-08-22 18:49:35 +00001273 // default case
Matt Arsenault0efdd062016-09-09 22:29:28 +00001274
Tom Stellardf3fc5552014-08-22 18:49:35 +00001275 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001276 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8);
1277 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8);
Tom Stellardf3fc5552014-08-22 18:49:35 +00001278 return true;
1279}
1280
Changpeng Fangb41574a2015-12-22 20:55:23 +00001281bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
Tom Stellard155bbb72014-08-11 22:18:17 +00001282 SDValue &VAddr, SDValue &SOffset,
1283 SDValue &Offset, SDValue &Offen,
1284 SDValue &Idxen, SDValue &Addr64,
1285 SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001286 SDValue &TFE, SDValue &DLC) const {
Changpeng Fangb41574a2015-12-22 20:55:23 +00001287 // Subtarget prefers to use flat instruction
1288 if (Subtarget->useFlatForGlobal())
1289 return false;
1290
Tom Stellardb02c2682014-06-24 23:33:07 +00001291 SDLoc DL(Addr);
1292
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001293 if (!GLC.getNode())
1294 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
1295 if (!SLC.getNode())
1296 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001297 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1);
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001298 DLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellard155bbb72014-08-11 22:18:17 +00001299
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001300 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1301 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1302 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1);
1303 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001304
Tim Renouff1c7b922018-08-02 22:53:57 +00001305 ConstantSDNode *C1 = nullptr;
1306 SDValue N0 = Addr;
Tom Stellardb02c2682014-06-24 23:33:07 +00001307 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Tim Renouff1c7b922018-08-02 22:53:57 +00001308 C1 = cast<ConstantSDNode>(Addr.getOperand(1));
1309 if (isUInt<32>(C1->getZExtValue()))
1310 N0 = Addr.getOperand(0);
1311 else
1312 C1 = nullptr;
Tom Stellardb02c2682014-06-24 23:33:07 +00001313 }
Tom Stellard94b72312015-02-11 00:34:35 +00001314
Tim Renouff1c7b922018-08-02 22:53:57 +00001315 if (N0.getOpcode() == ISD::ADD) {
1316 // (add N2, N3) -> addr64, or
1317 // (add (add N2, N3), C1) -> addr64
1318 SDValue N2 = N0.getOperand(0);
1319 SDValue N3 = N0.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001320 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
Tim Renouff1c7b922018-08-02 22:53:57 +00001321
1322 if (N2->isDivergent()) {
1323 if (N3->isDivergent()) {
1324 // Both N2 and N3 are divergent. Use N0 (the result of the add) as the
1325 // addr64, and construct the resource from a 0 address.
1326 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1327 VAddr = N0;
1328 } else {
1329 // N2 is divergent, N3 is not.
1330 Ptr = N3;
1331 VAddr = N2;
1332 }
1333 } else {
1334 // N2 is not divergent.
1335 Ptr = N2;
1336 VAddr = N3;
1337 }
1338 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1339 } else if (N0->isDivergent()) {
1340 // N0 is divergent. Use it as the addr64, and construct the resource from a
1341 // 0 address.
1342 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0);
1343 VAddr = N0;
1344 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1345 } else {
1346 // N0 -> offset, or
1347 // (N0 + C1) -> offset
1348 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
Tom Stellard155bbb72014-08-11 22:18:17 +00001349 Ptr = N0;
Tim Renouff1c7b922018-08-02 22:53:57 +00001350 }
1351
1352 if (!C1) {
1353 // No offset.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001354 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001355 return true;
Tom Stellardb02c2682014-06-24 23:33:07 +00001356 }
1357
Tim Renouff1c7b922018-08-02 22:53:57 +00001358 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) {
1359 // Legal offset for instruction.
1360 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1361 return true;
1362 }
Changpeng Fangb41574a2015-12-22 20:55:23 +00001363
Tim Renouff1c7b922018-08-02 22:53:57 +00001364 // Illegal offset, store it in soffset.
1365 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1366 SOffset =
1367 SDValue(CurDAG->getMachineNode(
1368 AMDGPU::S_MOV_B32, DL, MVT::i32,
1369 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)),
1370 0);
Changpeng Fangb41574a2015-12-22 20:55:23 +00001371 return true;
Tom Stellard155bbb72014-08-11 22:18:17 +00001372}
1373
1374bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001375 SDValue &VAddr, SDValue &SOffset,
Tom Stellard1f9939f2015-02-27 14:59:41 +00001376 SDValue &Offset, SDValue &GLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001377 SDValue &SLC, SDValue &TFE,
1378 SDValue &DLC) const {
Tom Stellard1f9939f2015-02-27 14:59:41 +00001379 SDValue Ptr, Offen, Idxen, Addr64;
Tom Stellard155bbb72014-08-11 22:18:17 +00001380
Tom Stellard70580f82015-07-20 14:28:41 +00001381 // addr64 bit was removed for volcanic islands.
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00001382 if (!Subtarget->hasAddr64())
Tom Stellard70580f82015-07-20 14:28:41 +00001383 return false;
1384
Changpeng Fangb41574a2015-12-22 20:55:23 +00001385 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001386 GLC, SLC, TFE, DLC))
Changpeng Fangb41574a2015-12-22 20:55:23 +00001387 return false;
Tom Stellard155bbb72014-08-11 22:18:17 +00001388
1389 ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
1390 if (C->getSExtValue()) {
1391 SDLoc DL(Addr);
Matt Arsenault485defe2014-11-05 19:01:17 +00001392
1393 const SITargetLowering& Lowering =
1394 *static_cast<const SITargetLowering*>(getTargetLowering());
1395
1396 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001397 return true;
1398 }
Matt Arsenault485defe2014-11-05 19:01:17 +00001399
Tom Stellard155bbb72014-08-11 22:18:17 +00001400 return false;
1401}
1402
Tom Stellard7980fc82014-09-25 18:30:26 +00001403bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
Tom Stellardc53861a2015-02-11 00:34:32 +00001404 SDValue &VAddr, SDValue &SOffset,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001405 SDValue &Offset,
1406 SDValue &SLC) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001407 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1);
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001408 SDValue GLC, TFE, DLC;
Tom Stellard7980fc82014-09-25 18:30:26 +00001409
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001410 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE, DLC);
Tom Stellard7980fc82014-09-25 18:30:26 +00001411}
1412
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001413static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) {
1414 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>();
1415 return PSV && PSV->isStack();
Matt Arsenaultac0fc842016-09-17 16:09:55 +00001416}
1417
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001418std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const {
1419 const MachineFunction &MF = CurDAG->getMachineFunction();
1420 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1421
1422 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) {
1423 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(),
1424 FI->getValueType(0));
1425
Matt Arsenaultb812b7a2019-06-05 22:20:47 +00001426 // If we can resolve this to a frame index access, this will be relative to
1427 // either the stack or frame pointer SGPR.
1428 return std::make_pair(
1429 TFI, CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32));
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001430 }
1431
1432 // If we don't know this private access is a local stack object, it needs to
1433 // be relative to the entry point's scratch wave offset register.
1434 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(),
1435 MVT::i32));
1436}
1437
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001438bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001439 SDValue Addr, SDValue &Rsrc,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001440 SDValue &VAddr, SDValue &SOffset,
1441 SDValue &ImmOffset) const {
Tom Stellardb02094e2014-07-21 15:45:01 +00001442
1443 SDLoc DL(Addr);
1444 MachineFunction &MF = CurDAG->getMachineFunction();
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001445 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardb02094e2014-07-21 15:45:01 +00001446
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001447 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Tom Stellardb02094e2014-07-21 15:45:01 +00001448
Matt Arsenault0774ea22017-04-24 19:40:59 +00001449 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1450 unsigned Imm = CAddr->getZExtValue();
Matt Arsenault0774ea22017-04-24 19:40:59 +00001451
1452 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32);
1453 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
1454 DL, MVT::i32, HighBits);
1455 VAddr = SDValue(MovHighBits, 0);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001456
1457 // In a call sequence, stores to the argument stack area are relative to the
1458 // stack pointer.
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001459 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001460 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1461 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1462
1463 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
Matt Arsenault0774ea22017-04-24 19:40:59 +00001464 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16);
1465 return true;
1466 }
1467
Tom Stellardb02094e2014-07-21 15:45:01 +00001468 if (CurDAG->isBaseWithConstantOffset(Addr)) {
Matt Arsenault0774ea22017-04-24 19:40:59 +00001469 // (add n0, c1)
1470
Tom Stellard78655fc2015-07-16 19:40:09 +00001471 SDValue N0 = Addr.getOperand(0);
Tom Stellardb02094e2014-07-21 15:45:01 +00001472 SDValue N1 = Addr.getOperand(1);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001473
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001474 // Offsets in vaddr must be positive if range checking is enabled.
Matt Arsenault45b98182017-11-15 00:45:43 +00001475 //
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001476 // The total computation of vaddr + soffset + offset must not overflow. If
1477 // vaddr is negative, even if offset is 0 the sgpr offset add will end up
Matt Arsenault45b98182017-11-15 00:45:43 +00001478 // overflowing.
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001479 //
1480 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would
1481 // always perform a range check. If a negative vaddr base index was used,
1482 // this would fail the range check. The overall address computation would
1483 // compute a valid address, but this doesn't happen due to the range
1484 // check. For out-of-bounds MUBUF loads, a 0 is returned.
1485 //
1486 // Therefore it should be safe to fold any VGPR offset on gfx9 into the
1487 // MUBUF vaddr, but not on older subtargets which can only do this if the
1488 // sign bit is known 0.
Matt Arsenaultcd099612016-02-24 04:55:29 +00001489 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
Matt Arsenault45b98182017-11-15 00:45:43 +00001490 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) &&
Matt Arsenaultcaf0ed42017-11-30 00:52:40 +00001491 (!Subtarget->privateMemoryResourceIsRangeChecked() ||
1492 CurDAG->SignBitIsZero(N0))) {
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001493 std::tie(VAddr, SOffset) = foldFrameIndex(N0);
Matt Arsenaultcd099612016-02-24 04:55:29 +00001494 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1495 return true;
Tom Stellardb02094e2014-07-21 15:45:01 +00001496 }
1497 }
1498
Tom Stellardb02094e2014-07-21 15:45:01 +00001499 // (node)
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001500 std::tie(VAddr, SOffset) = foldFrameIndex(Addr);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001501 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
Tom Stellardb02094e2014-07-21 15:45:01 +00001502 return true;
1503}
1504
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001505bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent,
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001506 SDValue Addr,
Matt Arsenault0774ea22017-04-24 19:40:59 +00001507 SDValue &SRsrc,
1508 SDValue &SOffset,
1509 SDValue &Offset) const {
1510 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr);
Marek Olsakffadcb72017-11-09 01:52:17 +00001511 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue()))
Matt Arsenault0774ea22017-04-24 19:40:59 +00001512 return false;
1513
1514 SDLoc DL(Addr);
1515 MachineFunction &MF = CurDAG->getMachineFunction();
1516 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1517
1518 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001519
Matt Arsenaultb81495d2017-09-20 05:01:53 +00001520 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
Matt Arsenault156d3ae2017-05-17 21:02:58 +00001521 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1522 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1523
1524 // FIXME: Get from MachinePointerInfo? We should only be using the frame
1525 // offset if we know this is in a call sequence.
1526 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
1527
Matt Arsenault0774ea22017-04-24 19:40:59 +00001528 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1529 return true;
1530}
1531
Tom Stellard155bbb72014-08-11 22:18:17 +00001532bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1533 SDValue &SOffset, SDValue &Offset,
1534 SDValue &GLC, SDValue &SLC,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001535 SDValue &TFE, SDValue &DLC) const {
Tom Stellard155bbb72014-08-11 22:18:17 +00001536 SDValue Ptr, VAddr, Offen, Idxen, Addr64;
Tom Stellard794c8c02014-12-02 17:05:41 +00001537 const SIInstrInfo *TII =
Eric Christopher7792e322015-01-30 23:24:40 +00001538 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellardb02094e2014-07-21 15:45:01 +00001539
Changpeng Fangb41574a2015-12-22 20:55:23 +00001540 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001541 GLC, SLC, TFE, DLC))
Changpeng Fangb41574a2015-12-22 20:55:23 +00001542 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001543
Tom Stellard155bbb72014-08-11 22:18:17 +00001544 if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
1545 !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
1546 !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
Tom Stellard794c8c02014-12-02 17:05:41 +00001547 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() |
Tom Stellard155bbb72014-08-11 22:18:17 +00001548 APInt::getAllOnesValue(32).getZExtValue(); // Size
1549 SDLoc DL(Addr);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00001550
1551 const SITargetLowering& Lowering =
1552 *static_cast<const SITargetLowering*>(getTargetLowering());
1553
1554 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0);
Tom Stellard155bbb72014-08-11 22:18:17 +00001555 return true;
1556 }
1557 return false;
Tom Stellardb02094e2014-07-21 15:45:01 +00001558}
1559
Tom Stellard7980fc82014-09-25 18:30:26 +00001560bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001561 SDValue &Soffset, SDValue &Offset
1562 ) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001563 SDValue GLC, SLC, TFE, DLC;
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001564
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001565 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC);
Jan Vesely43b7b5b2016-04-07 19:23:11 +00001566}
1567bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
Tom Stellard7980fc82014-09-25 18:30:26 +00001568 SDValue &Soffset, SDValue &Offset,
Matt Arsenault88701812016-06-09 23:42:48 +00001569 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001570 SDValue GLC, TFE, DLC;
Tom Stellard7980fc82014-09-25 18:30:26 +00001571
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001572 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC);
Tom Stellard7980fc82014-09-25 18:30:26 +00001573}
1574
Matt Arsenault4e309b02017-07-29 01:03:53 +00001575template <bool IsSigned>
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001576bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDNode *N,
1577 SDValue Addr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001578 SDValue &VAddr,
1579 SDValue &Offset,
1580 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001581 return static_cast<const SITargetLowering*>(getTargetLowering())->
1582 SelectFlatOffset(IsSigned, *CurDAG, N, Addr, VAddr, Offset, SLC);
Matt Arsenault7757c592016-06-09 23:42:54 +00001583}
1584
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001585bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDNode *N,
1586 SDValue Addr,
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001587 SDValue &VAddr,
1588 SDValue &Offset,
1589 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001590 return SelectFlatOffset<false>(N, Addr, VAddr, Offset, SLC);
Matt Arsenault4e309b02017-07-29 01:03:53 +00001591}
1592
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001593bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDNode *N,
1594 SDValue Addr,
Matt Arsenault4e309b02017-07-29 01:03:53 +00001595 SDValue &VAddr,
1596 SDValue &Offset,
1597 SDValue &SLC) const {
Stanislav Mekhanoshina6322942019-04-30 22:08:23 +00001598 return SelectFlatOffset<true>(N, Addr, VAddr, Offset, SLC);
Matt Arsenaultdb7c6a82017-06-12 16:53:51 +00001599}
1600
Tom Stellarddee26a22015-08-06 19:28:30 +00001601bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode,
1602 SDValue &Offset, bool &Imm) const {
1603
1604 // FIXME: Handle non-constant offsets.
1605 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode);
1606 if (!C)
1607 return false;
1608
1609 SDLoc SL(ByteOffsetNode);
Tom Stellard5bfbae52018-07-11 20:59:01 +00001610 GCNSubtarget::Generation Gen = Subtarget->getGeneration();
Tom Stellarddee26a22015-08-06 19:28:30 +00001611 int64_t ByteOffset = C->getSExtValue();
Tom Stellard08efb7e2017-01-27 18:41:14 +00001612 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001613
Tom Stellard08efb7e2017-01-27 18:41:14 +00001614 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001615 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1616 Imm = true;
1617 return true;
1618 }
1619
Tom Stellard217361c2015-08-06 19:28:38 +00001620 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset))
1621 return false;
1622
Marek Olsak8973a0a2017-05-24 14:53:50 +00001623 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) {
1624 // 32-bit Immediates are supported on Sea Islands.
Tom Stellard217361c2015-08-06 19:28:38 +00001625 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1626 } else {
Tom Stellarddee26a22015-08-06 19:28:30 +00001627 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32);
1628 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32,
1629 C32Bit), 0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001630 }
Tom Stellard217361c2015-08-06 19:28:38 +00001631 Imm = false;
1632 return true;
Tom Stellarddee26a22015-08-06 19:28:30 +00001633}
1634
Matt Arsenault923712b2018-02-09 16:57:57 +00001635SDValue AMDGPUDAGToDAGISel::Expand32BitAddress(SDValue Addr) const {
1636 if (Addr.getValueType() != MVT::i32)
1637 return Addr;
1638
1639 // Zero-extend a 32-bit address.
1640 SDLoc SL(Addr);
1641
1642 const MachineFunction &MF = CurDAG->getMachineFunction();
1643 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1644 unsigned AddrHiVal = Info->get32BitAddressHighBits();
1645 SDValue AddrHi = CurDAG->getTargetConstant(AddrHiVal, SL, MVT::i32);
1646
1647 const SDValue Ops[] = {
1648 CurDAG->getTargetConstant(AMDGPU::SReg_64_XEXECRegClassID, SL, MVT::i32),
1649 Addr,
1650 CurDAG->getTargetConstant(AMDGPU::sub0, SL, MVT::i32),
1651 SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, AddrHi),
1652 0),
1653 CurDAG->getTargetConstant(AMDGPU::sub1, SL, MVT::i32),
1654 };
1655
1656 return SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, SL, MVT::i64,
1657 Ops), 0);
1658}
1659
Tom Stellarddee26a22015-08-06 19:28:30 +00001660bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase,
1661 SDValue &Offset, bool &Imm) const {
Tom Stellarddee26a22015-08-06 19:28:30 +00001662 SDLoc SL(Addr);
Matt Arsenault923712b2018-02-09 16:57:57 +00001663
Marek Olsak3fc20792018-08-29 20:03:00 +00001664 // A 32-bit (address + offset) should not cause unsigned 32-bit integer
1665 // wraparound, because s_load instructions perform the addition in 64 bits.
1666 if ((Addr.getValueType() != MVT::i32 ||
1667 Addr->getFlags().hasNoUnsignedWrap()) &&
1668 CurDAG->isBaseWithConstantOffset(Addr)) {
Tom Stellarddee26a22015-08-06 19:28:30 +00001669 SDValue N0 = Addr.getOperand(0);
1670 SDValue N1 = Addr.getOperand(1);
1671
1672 if (SelectSMRDOffset(N1, Offset, Imm)) {
Matt Arsenault923712b2018-02-09 16:57:57 +00001673 SBase = Expand32BitAddress(N0);
Tom Stellarddee26a22015-08-06 19:28:30 +00001674 return true;
1675 }
1676 }
Matt Arsenault923712b2018-02-09 16:57:57 +00001677 SBase = Expand32BitAddress(Addr);
Tom Stellarddee26a22015-08-06 19:28:30 +00001678 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32);
1679 Imm = true;
1680 return true;
1681}
1682
1683bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase,
1684 SDValue &Offset) const {
1685 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001686 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm;
1687}
Tom Stellarddee26a22015-08-06 19:28:30 +00001688
Marek Olsak8973a0a2017-05-24 14:53:50 +00001689bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase,
1690 SDValue &Offset) const {
1691
1692 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1693 return false;
1694
1695 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001696 if (!SelectSMRD(Addr, SBase, Offset, Imm))
1697 return false;
1698
Marek Olsak8973a0a2017-05-24 14:53:50 +00001699 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001700}
1701
Tom Stellarddee26a22015-08-06 19:28:30 +00001702bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase,
1703 SDValue &Offset) const {
1704 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001705 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm &&
1706 !isa<ConstantSDNode>(Offset);
Tom Stellarddee26a22015-08-06 19:28:30 +00001707}
1708
1709bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr,
1710 SDValue &Offset) const {
1711 bool Imm;
Marek Olsak8973a0a2017-05-24 14:53:50 +00001712 return SelectSMRDOffset(Addr, Offset, Imm) && Imm;
1713}
Tom Stellarddee26a22015-08-06 19:28:30 +00001714
Marek Olsak8973a0a2017-05-24 14:53:50 +00001715bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr,
1716 SDValue &Offset) const {
1717 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1718 return false;
1719
1720 bool Imm;
Tom Stellard217361c2015-08-06 19:28:38 +00001721 if (!SelectSMRDOffset(Addr, Offset, Imm))
1722 return false;
1723
Marek Olsak8973a0a2017-05-24 14:53:50 +00001724 return !Imm && isa<ConstantSDNode>(Offset);
Tom Stellard217361c2015-08-06 19:28:38 +00001725}
1726
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001727bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index,
1728 SDValue &Base,
1729 SDValue &Offset) const {
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001730 SDLoc DL(Index);
1731
1732 if (CurDAG->isBaseWithConstantOffset(Index)) {
1733 SDValue N0 = Index.getOperand(0);
1734 SDValue N1 = Index.getOperand(1);
1735 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1736
1737 // (add n0, c0)
Changpeng Fang6f539292018-12-21 20:57:34 +00001738 // Don't peel off the offset (c0) if doing so could possibly lead
1739 // the base (n0) to be negative.
1740 if (C1->getSExtValue() <= 0 || CurDAG->SignBitIsZero(N0)) {
1741 Base = N0;
1742 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32);
1743 return true;
1744 }
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001745 }
1746
Nicolai Haehnle7968c342016-07-12 08:12:16 +00001747 if (isa<ConstantSDNode>(Index))
1748 return false;
Matt Arsenault1322b6f2016-07-09 01:13:56 +00001749
1750 Base = Index;
1751 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1752 return true;
1753}
1754
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001755SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL,
1756 SDValue Val, uint32_t Offset,
1757 uint32_t Width) {
Marek Olsak9b728682015-03-24 13:40:27 +00001758 // Transformation function, pack the offset and width of a BFE into
1759 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
1760 // source, bits [5:0] contain the offset and bits [22:16] the width.
1761 uint32_t PackedVal = Offset | (Width << 16);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001762 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32);
Marek Olsak9b728682015-03-24 13:40:27 +00001763
1764 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst);
1765}
1766
Justin Bogner95927c02016-05-12 21:03:32 +00001767void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001768 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c)
1769 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c)
1770 // Predicate: 0 < b <= c < 32
1771
1772 const SDValue &Shl = N->getOperand(0);
1773 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1));
1774 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1775
1776 if (B && C) {
1777 uint32_t BVal = B->getZExtValue();
1778 uint32_t CVal = C->getZExtValue();
1779
1780 if (0 < BVal && BVal <= CVal && CVal < 32) {
1781 bool Signed = N->getOpcode() == ISD::SRA;
1782 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32;
1783
Justin Bogner95927c02016-05-12 21:03:32 +00001784 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal,
1785 32 - CVal));
1786 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001787 }
1788 }
Justin Bogner95927c02016-05-12 21:03:32 +00001789 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001790}
1791
Justin Bogner95927c02016-05-12 21:03:32 +00001792void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) {
Marek Olsak9b728682015-03-24 13:40:27 +00001793 switch (N->getOpcode()) {
1794 case ISD::AND:
1795 if (N->getOperand(0).getOpcode() == ISD::SRL) {
1796 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)"
1797 // Predicate: isMask(mask)
1798 const SDValue &Srl = N->getOperand(0);
1799 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
1800 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
1801
1802 if (Shift && Mask) {
1803 uint32_t ShiftVal = Shift->getZExtValue();
1804 uint32_t MaskVal = Mask->getZExtValue();
1805
1806 if (isMask_32(MaskVal)) {
1807 uint32_t WidthVal = countPopulation(MaskVal);
1808
Justin Bogner95927c02016-05-12 21:03:32 +00001809 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1810 Srl.getOperand(0), ShiftVal, WidthVal));
1811 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001812 }
1813 }
1814 }
1815 break;
1816 case ISD::SRL:
1817 if (N->getOperand(0).getOpcode() == ISD::AND) {
1818 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)"
1819 // Predicate: isMask(mask >> b)
1820 const SDValue &And = N->getOperand(0);
1821 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1));
1822 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1));
1823
1824 if (Shift && Mask) {
1825 uint32_t ShiftVal = Shift->getZExtValue();
1826 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal;
1827
1828 if (isMask_32(MaskVal)) {
1829 uint32_t WidthVal = countPopulation(MaskVal);
1830
Justin Bogner95927c02016-05-12 21:03:32 +00001831 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1832 And.getOperand(0), ShiftVal, WidthVal));
1833 return;
Marek Olsak9b728682015-03-24 13:40:27 +00001834 }
1835 }
Justin Bogner95927c02016-05-12 21:03:32 +00001836 } else if (N->getOperand(0).getOpcode() == ISD::SHL) {
1837 SelectS_BFEFromShifts(N);
1838 return;
1839 }
Marek Olsak9b728682015-03-24 13:40:27 +00001840 break;
1841 case ISD::SRA:
Justin Bogner95927c02016-05-12 21:03:32 +00001842 if (N->getOperand(0).getOpcode() == ISD::SHL) {
1843 SelectS_BFEFromShifts(N);
1844 return;
1845 }
Marek Olsak9b728682015-03-24 13:40:27 +00001846 break;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001847
1848 case ISD::SIGN_EXTEND_INREG: {
1849 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8
1850 SDValue Src = N->getOperand(0);
1851 if (Src.getOpcode() != ISD::SRL)
1852 break;
1853
1854 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1));
1855 if (!Amt)
1856 break;
1857
1858 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
Justin Bogner95927c02016-05-12 21:03:32 +00001859 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0),
1860 Amt->getZExtValue(), Width));
1861 return;
Matt Arsenault7e8de012016-04-22 22:59:16 +00001862 }
Marek Olsak9b728682015-03-24 13:40:27 +00001863 }
1864
Justin Bogner95927c02016-05-12 21:03:32 +00001865 SelectCode(N);
Marek Olsak9b728682015-03-24 13:40:27 +00001866}
1867
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001868bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const {
1869 assert(N->getOpcode() == ISD::BRCOND);
1870 if (!N->hasOneUse())
1871 return false;
1872
1873 SDValue Cond = N->getOperand(1);
1874 if (Cond.getOpcode() == ISD::CopyToReg)
1875 Cond = Cond.getOperand(2);
1876
1877 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
1878 return false;
1879
1880 MVT VT = Cond.getOperand(0).getSimpleValueType();
1881 if (VT == MVT::i32)
1882 return true;
1883
1884 if (VT == MVT::i64) {
Tom Stellard5bfbae52018-07-11 20:59:01 +00001885 auto ST = static_cast<const GCNSubtarget *>(Subtarget);
Matt Arsenault7b1dc2c2016-09-17 02:02:19 +00001886
1887 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1888 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64();
1889 }
1890
1891 return false;
1892}
1893
Justin Bogner95927c02016-05-12 21:03:32 +00001894void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001895 SDValue Cond = N->getOperand(1);
1896
Matt Arsenault327188a2016-12-15 21:57:11 +00001897 if (Cond.isUndef()) {
1898 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other,
1899 N->getOperand(2), N->getOperand(0));
1900 return;
1901 }
1902
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001903 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget);
1904 const SIRegisterInfo *TRI = ST->getRegisterInfo();
1905
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001906 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N);
1907 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ;
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001908 unsigned CondReg = UseSCCBr ? (unsigned)AMDGPU::SCC : TRI->getVCC();
Tom Stellardbc4497b2016-02-12 23:45:29 +00001909 SDLoc SL(N);
1910
Tim Renouf6eaad1e2018-01-09 21:34:43 +00001911 if (!UseSCCBr) {
1912 // This is the case that we are selecting to S_CBRANCH_VCCNZ. We have not
1913 // analyzed what generates the vcc value, so we do not know whether vcc
1914 // bits for disabled lanes are 0. Thus we need to mask out bits for
1915 // disabled lanes.
1916 //
1917 // For the case that we select S_CBRANCH_SCC1 and it gets
1918 // changed to S_CBRANCH_VCCNZ in SIFixSGPRCopies, SIFixSGPRCopies calls
1919 // SIInstrInfo::moveToVALU which inserts the S_AND).
1920 //
1921 // We could add an analysis of what generates the vcc value here and omit
1922 // the S_AND when is unnecessary. But it would be better to add a separate
1923 // pass after SIFixSGPRCopies to do the unnecessary S_AND removal, so it
1924 // catches both cases.
Stanislav Mekhanoshin52500212019-06-16 17:13:09 +00001925 Cond = SDValue(CurDAG->getMachineNode(ST->isWave32() ? AMDGPU::S_AND_B32
1926 : AMDGPU::S_AND_B64,
1927 SL, MVT::i1,
1928 CurDAG->getRegister(ST->isWave32() ? AMDGPU::EXEC_LO
1929 : AMDGPU::EXEC,
1930 MVT::i1),
1931 Cond),
Tim Renouf6eaad1e2018-01-09 21:34:43 +00001932 0);
1933 }
1934
Matt Arsenaultd674e0a2017-10-10 20:34:49 +00001935 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond);
1936 CurDAG->SelectNodeTo(N, BrOp, MVT::Other,
Justin Bogner95927c02016-05-12 21:03:32 +00001937 N->getOperand(2), // Basic Block
Matt Arsenaultf530e8b2016-11-07 19:09:33 +00001938 VCC.getValue(0));
Tom Stellardbc4497b2016-02-12 23:45:29 +00001939}
1940
Matt Arsenault0084adc2018-04-30 19:08:16 +00001941void AMDGPUDAGToDAGISel::SelectFMAD_FMA(SDNode *N) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001942 MVT VT = N->getSimpleValueType(0);
Matt Arsenault0084adc2018-04-30 19:08:16 +00001943 bool IsFMA = N->getOpcode() == ISD::FMA;
1944 if (VT != MVT::f32 || (!Subtarget->hasMadMixInsts() &&
1945 !Subtarget->hasFmaMixInsts()) ||
1946 ((IsFMA && Subtarget->hasMadMixInsts()) ||
1947 (!IsFMA && Subtarget->hasFmaMixInsts()))) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001948 SelectCode(N);
1949 return;
1950 }
1951
1952 SDValue Src0 = N->getOperand(0);
1953 SDValue Src1 = N->getOperand(1);
1954 SDValue Src2 = N->getOperand(2);
1955 unsigned Src0Mods, Src1Mods, Src2Mods;
1956
Matt Arsenault0084adc2018-04-30 19:08:16 +00001957 // Avoid using v_mad_mix_f32/v_fma_mix_f32 unless there is actually an operand
1958 // using the conversion from f16.
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001959 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods);
1960 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods);
1961 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods);
1962
Matt Arsenault0084adc2018-04-30 19:08:16 +00001963 assert((IsFMA || !Subtarget->hasFP32Denormals()) &&
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001964 "fmad selected with denormals enabled");
1965 // TODO: We can select this with f32 denormals enabled if all the sources are
1966 // converted from f16 (in which case fmad isn't legal).
1967
1968 if (Sel0 || Sel1 || Sel2) {
1969 // For dummy operands.
1970 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
1971 SDValue Ops[] = {
1972 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0,
1973 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1,
1974 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2,
1975 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1),
1976 Zero, Zero
1977 };
1978
Matt Arsenault0084adc2018-04-30 19:08:16 +00001979 CurDAG->SelectNodeTo(N,
1980 IsFMA ? AMDGPU::V_FMA_MIX_F32 : AMDGPU::V_MAD_MIX_F32,
1981 MVT::f32, Ops);
Matt Arsenaultd7e23032017-09-07 18:05:07 +00001982 } else {
1983 SelectCode(N);
1984 }
1985}
1986
Matt Arsenault88701812016-06-09 23:42:48 +00001987// This is here because there isn't a way to use the generated sub0_sub1 as the
1988// subreg index to EXTRACT_SUBREG in tablegen.
1989void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) {
1990 MemSDNode *Mem = cast<MemSDNode>(N);
1991 unsigned AS = Mem->getAddressSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +00001992 if (AS == AMDGPUAS::FLAT_ADDRESS) {
Matt Arsenault7757c592016-06-09 23:42:54 +00001993 SelectCode(N);
1994 return;
1995 }
Matt Arsenault88701812016-06-09 23:42:48 +00001996
1997 MVT VT = N->getSimpleValueType(0);
1998 bool Is32 = (VT == MVT::i32);
1999 SDLoc SL(N);
2000
2001 MachineSDNode *CmpSwap = nullptr;
2002 if (Subtarget->hasAddr64()) {
Vitaly Buka74503982017-10-15 05:35:02 +00002003 SDValue SRsrc, VAddr, SOffset, Offset, SLC;
Matt Arsenault88701812016-06-09 23:42:48 +00002004
2005 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00002006 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN :
2007 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00002008 SDValue CmpVal = Mem->getOperand(2);
2009
2010 // XXX - Do we care about glue operands?
2011
2012 SDValue Ops[] = {
2013 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain()
2014 };
2015
2016 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2017 }
2018 }
2019
2020 if (!CmpSwap) {
2021 SDValue SRsrc, SOffset, Offset, SLC;
2022 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) {
Matt Arsenaulte5456ce2017-07-20 21:06:04 +00002023 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN :
2024 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN;
Matt Arsenault88701812016-06-09 23:42:48 +00002025
2026 SDValue CmpVal = Mem->getOperand(2);
2027 SDValue Ops[] = {
2028 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain()
2029 };
2030
2031 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
2032 }
2033 }
2034
2035 if (!CmpSwap) {
2036 SelectCode(N);
2037 return;
2038 }
2039
Chandler Carruth66654b72018-08-14 23:30:32 +00002040 MachineMemOperand *MMO = Mem->getMemOperand();
2041 CurDAG->setNodeMemRefs(CmpSwap, {MMO});
Matt Arsenault88701812016-06-09 23:42:48 +00002042
2043 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
2044 SDValue Extract
2045 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0));
2046
2047 ReplaceUses(SDValue(N, 0), Extract);
2048 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1));
2049 CurDAG->RemoveDeadNode(N);
2050}
2051
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002052void AMDGPUDAGToDAGISel::SelectDSAppendConsume(SDNode *N, unsigned IntrID) {
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002053 // The address is assumed to be uniform, so if it ends up in a VGPR, it will
2054 // be copied to an SGPR with readfirstlane.
2055 unsigned Opc = IntrID == Intrinsic::amdgcn_ds_append ?
2056 AMDGPU::DS_APPEND : AMDGPU::DS_CONSUME;
2057
2058 SDValue Chain = N->getOperand(0);
2059 SDValue Ptr = N->getOperand(2);
2060 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
Matt Arsenault9e5fa332019-06-14 21:01:24 +00002061 MachineMemOperand *MMO = M->getMemOperand();
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002062 bool IsGDS = M->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
2063
2064 SDValue Offset;
2065 if (CurDAG->isBaseWithConstantOffset(Ptr)) {
2066 SDValue PtrBase = Ptr.getOperand(0);
2067 SDValue PtrOffset = Ptr.getOperand(1);
2068
2069 const APInt &OffsetVal = cast<ConstantSDNode>(PtrOffset)->getAPIntValue();
2070 if (isDSOffsetLegal(PtrBase, OffsetVal.getZExtValue(), 16)) {
2071 N = glueCopyToM0(N, PtrBase);
2072 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i32);
2073 }
2074 }
2075
2076 if (!Offset) {
2077 N = glueCopyToM0(N, Ptr);
2078 Offset = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
2079 }
2080
2081 SDValue Ops[] = {
2082 Offset,
2083 CurDAG->getTargetConstant(IsGDS, SDLoc(), MVT::i32),
2084 Chain,
2085 N->getOperand(N->getNumOperands() - 1) // New glue
2086 };
2087
Matt Arsenault9e5fa332019-06-14 21:01:24 +00002088 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2089 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
Matt Arsenaultcdd191d2019-01-28 20:14:49 +00002090}
2091
Matt Arsenault740322f2019-06-20 21:11:42 +00002092static unsigned gwsIntrinToOpcode(unsigned IntrID) {
2093 switch (IntrID) {
2094 case Intrinsic::amdgcn_ds_gws_init:
2095 return AMDGPU::DS_GWS_INIT;
2096 case Intrinsic::amdgcn_ds_gws_barrier:
2097 return AMDGPU::DS_GWS_BARRIER;
2098 case Intrinsic::amdgcn_ds_gws_sema_v:
2099 return AMDGPU::DS_GWS_SEMA_V;
2100 case Intrinsic::amdgcn_ds_gws_sema_br:
2101 return AMDGPU::DS_GWS_SEMA_BR;
2102 case Intrinsic::amdgcn_ds_gws_sema_p:
2103 return AMDGPU::DS_GWS_SEMA_P;
2104 case Intrinsic::amdgcn_ds_gws_sema_release_all:
2105 return AMDGPU::DS_GWS_SEMA_RELEASE_ALL;
2106 default:
2107 llvm_unreachable("not a gws intrinsic");
2108 }
2109}
2110
Matt Arsenault4d55d022019-06-19 19:55:27 +00002111void AMDGPUDAGToDAGISel::SelectDS_GWS(SDNode *N, unsigned IntrID) {
Matt Arsenault740322f2019-06-20 21:11:42 +00002112 if (IntrID == Intrinsic::amdgcn_ds_gws_sema_release_all &&
2113 !Subtarget->hasGWSSemaReleaseAll()) {
2114 // Let this error.
2115 SelectCode(N);
2116 return;
2117 }
2118
2119 // Chain, intrinsic ID, vsrc, offset
2120 const bool HasVSrc = N->getNumOperands() == 4;
2121 assert(HasVSrc || N->getNumOperands() == 3);
2122
Matt Arsenault4d55d022019-06-19 19:55:27 +00002123 SDLoc SL(N);
Matt Arsenault740322f2019-06-20 21:11:42 +00002124 SDValue BaseOffset = N->getOperand(HasVSrc ? 3 : 2);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002125 int ImmOffset = 0;
2126 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N);
2127 MachineMemOperand *MMO = M->getMemOperand();
2128
2129 // Don't worry if the offset ends up in a VGPR. Only one lane will have
2130 // effect, so SIFixSGPRCopies will validly insert readfirstlane.
2131
2132 // The resource id offset is computed as (<isa opaque base> + M0[21:16] +
2133 // offset field) % 64. Some versions of the programming guide omit the m0
2134 // part, or claim it's from offset 0.
2135 if (ConstantSDNode *ConstOffset = dyn_cast<ConstantSDNode>(BaseOffset)) {
2136 // If we have a constant offset, try to use the default value for m0 as a
2137 // base to possibly avoid setting it up.
2138 glueCopyToM0(N, CurDAG->getTargetConstant(-1, SL, MVT::i32));
2139 ImmOffset = ConstOffset->getZExtValue() + 1;
2140 } else {
2141 if (CurDAG->isBaseWithConstantOffset(BaseOffset)) {
2142 ImmOffset = BaseOffset.getConstantOperandVal(1);
2143 BaseOffset = BaseOffset.getOperand(0);
2144 }
2145
2146 // Prefer to do the shift in an SGPR since it should be possible to use m0
2147 // as the result directly. If it's already an SGPR, it will be eliminated
2148 // later.
2149 SDNode *SGPROffset
2150 = CurDAG->getMachineNode(AMDGPU::V_READFIRSTLANE_B32, SL, MVT::i32,
2151 BaseOffset);
2152 // Shift to offset in m0
2153 SDNode *M0Base
2154 = CurDAG->getMachineNode(AMDGPU::S_LSHL_B32, SL, MVT::i32,
2155 SDValue(SGPROffset, 0),
2156 CurDAG->getTargetConstant(16, SL, MVT::i32));
2157 glueCopyToM0(N, SDValue(M0Base, 0));
2158 }
2159
Matt Arsenault740322f2019-06-20 21:11:42 +00002160 SDValue V0;
2161 SDValue Chain = N->getOperand(0);
2162 SDValue Glue;
2163 if (HasVSrc) {
2164 SDValue VSrc0 = N->getOperand(2);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002165
Matt Arsenault740322f2019-06-20 21:11:42 +00002166 // The manual doesn't mention this, but it seems only v0 works.
2167 V0 = CurDAG->getRegister(AMDGPU::VGPR0, MVT::i32);
2168
2169 SDValue CopyToV0 = CurDAG->getCopyToReg(
2170 N->getOperand(0), SL, V0, VSrc0,
2171 N->getOperand(N->getNumOperands() - 1));
2172 Chain = CopyToV0;
2173 Glue = CopyToV0.getValue(1);
2174 }
Matt Arsenault4d55d022019-06-19 19:55:27 +00002175
2176 SDValue OffsetField = CurDAG->getTargetConstant(ImmOffset, SL, MVT::i32);
2177
2178 // TODO: Can this just be removed from the instruction?
2179 SDValue GDS = CurDAG->getTargetConstant(1, SL, MVT::i1);
2180
Matt Arsenault740322f2019-06-20 21:11:42 +00002181 const unsigned Opc = gwsIntrinToOpcode(IntrID);
2182 SmallVector<SDValue, 5> Ops;
2183 if (HasVSrc)
2184 Ops.push_back(V0);
2185 Ops.push_back(OffsetField);
2186 Ops.push_back(GDS);
2187 Ops.push_back(Chain);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002188
Matt Arsenault740322f2019-06-20 21:11:42 +00002189 if (HasVSrc)
2190 Ops.push_back(Glue);
Matt Arsenault4d55d022019-06-19 19:55:27 +00002191
2192 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
2193 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO});
2194}
2195
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002196void AMDGPUDAGToDAGISel::SelectINTRINSIC_W_CHAIN(SDNode *N) {
2197 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2198 switch (IntrID) {
2199 case Intrinsic::amdgcn_ds_append:
2200 case Intrinsic::amdgcn_ds_consume: {
2201 if (N->getValueType(0) != MVT::i32)
2202 break;
2203 SelectDSAppendConsume(N, IntrID);
2204 return;
2205 }
Matt Arsenault4d55d022019-06-19 19:55:27 +00002206 }
2207
2208 SelectCode(N);
2209}
2210
2211void AMDGPUDAGToDAGISel::SelectINTRINSIC_VOID(SDNode *N) {
2212 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2213 switch (IntrID) {
2214 case Intrinsic::amdgcn_ds_gws_init:
2215 case Intrinsic::amdgcn_ds_gws_barrier:
Matt Arsenault740322f2019-06-20 21:11:42 +00002216 case Intrinsic::amdgcn_ds_gws_sema_v:
2217 case Intrinsic::amdgcn_ds_gws_sema_br:
2218 case Intrinsic::amdgcn_ds_gws_sema_p:
2219 case Intrinsic::amdgcn_ds_gws_sema_release_all:
Matt Arsenault4d55d022019-06-19 19:55:27 +00002220 SelectDS_GWS(N, IntrID);
2221 return;
Matt Arsenaultd3c84e62019-06-14 13:26:32 +00002222 default:
2223 break;
2224 }
2225
2226 SelectCode(N);
2227}
2228
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002229bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src,
2230 unsigned &Mods) const {
2231 Mods = 0;
Tom Stellardb4a313a2014-08-01 00:32:39 +00002232 Src = In;
2233
2234 if (Src.getOpcode() == ISD::FNEG) {
2235 Mods |= SISrcMods::NEG;
2236 Src = Src.getOperand(0);
2237 }
2238
2239 if (Src.getOpcode() == ISD::FABS) {
2240 Mods |= SISrcMods::ABS;
2241 Src = Src.getOperand(0);
2242 }
2243
Tom Stellardb4a313a2014-08-01 00:32:39 +00002244 return true;
2245}
2246
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002247bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
2248 SDValue &SrcMods) const {
2249 unsigned Mods;
2250 if (SelectVOP3ModsImpl(In, Src, Mods)) {
2251 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2252 return true;
2253 }
2254
2255 return false;
2256}
2257
Matt Arsenaultf84e5d92017-01-31 03:07:46 +00002258bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src,
2259 SDValue &SrcMods) const {
2260 SelectVOP3Mods(In, Src, SrcMods);
2261 return isNoNanSrc(Src);
2262}
2263
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002264bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const {
2265 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG)
2266 return false;
2267
2268 Src = In;
2269 return true;
Tom Stellarddb5a11f2015-07-13 15:47:57 +00002270}
2271
Tom Stellardb4a313a2014-08-01 00:32:39 +00002272bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
2273 SDValue &SrcMods, SDValue &Clamp,
2274 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002275 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002276 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2277 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Tom Stellardb4a313a2014-08-01 00:32:39 +00002278
2279 return SelectVOP3Mods(In, Src, SrcMods);
2280}
2281
Matt Arsenault4831ce52015-01-06 23:00:37 +00002282bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src,
2283 SDValue &SrcMods,
2284 SDValue &Clamp,
2285 SDValue &Omod) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002286 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
Matt Arsenault4831ce52015-01-06 23:00:37 +00002287 return SelectVOP3Mods(In, Src, SrcMods);
2288}
2289
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00002290bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src,
2291 SDValue &Clamp, SDValue &Omod) const {
2292 Src = In;
2293
2294 SDLoc DL(In);
Matt Arsenaultdf58e822017-04-25 21:17:38 +00002295 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
2296 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
Dmitry Preobrazhenskyc512d442017-03-27 15:57:17 +00002297
2298 return true;
2299}
2300
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002301bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src,
2302 SDValue &SrcMods) const {
2303 unsigned Mods = 0;
2304 Src = In;
2305
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002306 if (Src.getOpcode() == ISD::FNEG) {
Matt Arsenault786eeea2017-05-17 20:00:00 +00002307 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI);
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002308 Src = Src.getOperand(0);
2309 }
2310
Matt Arsenault786eeea2017-05-17 20:00:00 +00002311 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2312 unsigned VecMods = Mods;
2313
Matt Arsenault98f29462017-05-17 20:30:58 +00002314 SDValue Lo = stripBitcast(Src.getOperand(0));
2315 SDValue Hi = stripBitcast(Src.getOperand(1));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002316
2317 if (Lo.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00002318 Lo = stripBitcast(Lo.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002319 Mods ^= SISrcMods::NEG;
2320 }
2321
2322 if (Hi.getOpcode() == ISD::FNEG) {
Matt Arsenault98f29462017-05-17 20:30:58 +00002323 Hi = stripBitcast(Hi.getOperand(0));
Matt Arsenault786eeea2017-05-17 20:00:00 +00002324 Mods ^= SISrcMods::NEG_HI;
2325 }
2326
Matt Arsenault98f29462017-05-17 20:30:58 +00002327 if (isExtractHiElt(Lo, Lo))
2328 Mods |= SISrcMods::OP_SEL_0;
2329
2330 if (isExtractHiElt(Hi, Hi))
2331 Mods |= SISrcMods::OP_SEL_1;
2332
2333 Lo = stripExtractLoElt(Lo);
2334 Hi = stripExtractLoElt(Hi);
2335
Matt Arsenault786eeea2017-05-17 20:00:00 +00002336 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) {
2337 // Really a scalar input. Just select from the low half of the register to
2338 // avoid packing.
2339
2340 Src = Lo;
2341 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2342 return true;
2343 }
2344
2345 Mods = VecMods;
2346 }
2347
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002348 // Packed instructions do not have abs modifiers.
Matt Arsenaulteb522e62017-02-27 22:15:25 +00002349 Mods |= SISrcMods::OP_SEL_1;
2350
2351 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2352 return true;
2353}
2354
2355bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src,
2356 SDValue &SrcMods,
2357 SDValue &Clamp) const {
2358 SDLoc SL(In);
2359
2360 // FIXME: Handle clamp and op_sel
2361 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2362
2363 return SelectVOP3PMods(In, Src, SrcMods);
2364}
2365
Dmitry Preobrazhenskyabf28392017-07-21 13:54:11 +00002366bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src,
2367 SDValue &SrcMods) const {
2368 Src = In;
2369 // FIXME: Handle op_sel
2370 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
2371 return true;
2372}
2373
2374bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src,
2375 SDValue &SrcMods,
2376 SDValue &Clamp) const {
2377 SDLoc SL(In);
2378
2379 // FIXME: Handle clamp
2380 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2381
2382 return SelectVOP3OpSel(In, Src, SrcMods);
2383}
2384
2385bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src,
2386 SDValue &SrcMods) const {
2387 // FIXME: Handle op_sel
2388 return SelectVOP3Mods(In, Src, SrcMods);
2389}
2390
2391bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src,
2392 SDValue &SrcMods,
2393 SDValue &Clamp) const {
2394 SDLoc SL(In);
2395
2396 // FIXME: Handle clamp
2397 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2398
2399 return SelectVOP3OpSelMods(In, Src, SrcMods);
2400}
2401
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002402// The return value is not whether the match is possible (which it always is),
2403// but whether or not it a conversion is really used.
2404bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src,
2405 unsigned &Mods) const {
2406 Mods = 0;
2407 SelectVOP3ModsImpl(In, Src, Mods);
2408
2409 if (Src.getOpcode() == ISD::FP_EXTEND) {
2410 Src = Src.getOperand(0);
2411 assert(Src.getValueType() == MVT::f16);
2412 Src = stripBitcast(Src);
2413
Matt Arsenault550c66d2017-10-13 20:45:49 +00002414 // Be careful about folding modifiers if we already have an abs. fneg is
2415 // applied last, so we don't want to apply an earlier fneg.
2416 if ((Mods & SISrcMods::ABS) == 0) {
2417 unsigned ModsTmp;
2418 SelectVOP3ModsImpl(Src, Src, ModsTmp);
2419
2420 if ((ModsTmp & SISrcMods::NEG) != 0)
2421 Mods ^= SISrcMods::NEG;
2422
2423 if ((ModsTmp & SISrcMods::ABS) != 0)
2424 Mods |= SISrcMods::ABS;
2425 }
2426
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002427 // op_sel/op_sel_hi decide the source type and source.
2428 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16.
2429 // If the sources's op_sel is set, it picks the high half of the source
2430 // register.
2431
2432 Mods |= SISrcMods::OP_SEL_1;
Matt Arsenault550c66d2017-10-13 20:45:49 +00002433 if (isExtractHiElt(Src, Src)) {
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002434 Mods |= SISrcMods::OP_SEL_0;
2435
Matt Arsenault550c66d2017-10-13 20:45:49 +00002436 // TODO: Should we try to look for neg/abs here?
2437 }
2438
Matt Arsenaultd7e23032017-09-07 18:05:07 +00002439 return true;
2440 }
2441
2442 return false;
2443}
2444
Matt Arsenault76935122017-09-20 20:28:39 +00002445bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src,
2446 SDValue &SrcMods) const {
2447 unsigned Mods = 0;
2448 SelectVOP3PMadMixModsImpl(In, Src, Mods);
2449 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2450 return true;
2451}
2452
Matt Arsenaulte8c03a22019-03-08 20:58:11 +00002453SDValue AMDGPUDAGToDAGISel::getHi16Elt(SDValue In) const {
2454 if (In.isUndef())
2455 return CurDAG->getUNDEF(MVT::i32);
2456
2457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) {
2458 SDLoc SL(In);
2459 return CurDAG->getConstant(C->getZExtValue() << 16, SL, MVT::i32);
2460 }
2461
2462 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) {
2463 SDLoc SL(In);
2464 return CurDAG->getConstant(
2465 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32);
2466 }
2467
2468 SDValue Src;
2469 if (isExtractHiElt(In, Src))
2470 return Src;
2471
2472 return SDValue();
2473}
2474
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002475bool AMDGPUDAGToDAGISel::isVGPRImm(const SDNode * N) const {
Matt Arsenaulte4c2e9b2019-06-19 23:54:58 +00002476 assert(CurDAG->getTarget().getTargetTriple().getArch() == Triple::amdgcn);
2477
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002478 const SIRegisterInfo *SIRI =
2479 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
2480 const SIInstrInfo * SII =
2481 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
2482
2483 unsigned Limit = 0;
2484 bool AllUsesAcceptSReg = true;
2485 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
2486 Limit < 10 && U != E; ++U, ++Limit) {
2487 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
2488
2489 // If the register class is unknown, it could be an unknown
2490 // register class that needs to be an SGPR, e.g. an inline asm
2491 // constraint
2492 if (!RC || SIRI->isSGPRClass(RC))
2493 return false;
2494
2495 if (RC != &AMDGPU::VS_32RegClass) {
2496 AllUsesAcceptSReg = false;
2497 SDNode * User = *U;
2498 if (User->isMachineOpcode()) {
2499 unsigned Opc = User->getMachineOpcode();
2500 MCInstrDesc Desc = SII->get(Opc);
2501 if (Desc.isCommutable()) {
2502 unsigned OpIdx = Desc.getNumDefs() + U.getOperandNo();
2503 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
2504 if (SII->findCommutedOpIndices(Desc, OpIdx, CommuteIdx1)) {
2505 unsigned CommutedOpNo = CommuteIdx1 - Desc.getNumDefs();
2506 const TargetRegisterClass *CommutedRC = getOperandRegClass(*U, CommutedOpNo);
2507 if (CommutedRC == &AMDGPU::VS_32RegClass)
2508 AllUsesAcceptSReg = true;
2509 }
2510 }
2511 }
2512 // If "AllUsesAcceptSReg == false" so far we haven't suceeded
2513 // commuting current user. This means have at least one use
2514 // that strictly require VGPR. Thus, we will not attempt to commute
2515 // other user instructions.
2516 if (!AllUsesAcceptSReg)
2517 break;
2518 }
2519 }
2520 return !AllUsesAcceptSReg && (Limit < 10);
2521}
2522
Alexander Timofeev4d302f62018-09-13 09:06:56 +00002523bool AMDGPUDAGToDAGISel::isUniformLoad(const SDNode * N) const {
2524 auto Ld = cast<LoadSDNode>(N);
2525
2526 return Ld->getAlignment() >= 4 &&
2527 (
2528 (
2529 (
2530 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2531 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT
2532 )
2533 &&
2534 !N->isDivergent()
2535 )
2536 ||
2537 (
2538 Subtarget->getScalarizeGlobalBehavior() &&
2539 Ld->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
2540 !Ld->isVolatile() &&
2541 !N->isDivergent() &&
2542 static_cast<const SITargetLowering *>(
2543 getTargetLowering())->isMemOpHasNoClobberedMemOperand(N)
2544 )
2545 );
2546}
Alexander Timofeevdb7ee762018-09-11 11:56:50 +00002547
Christian Konigd910b7d2013-02-26 17:52:16 +00002548void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002549 const AMDGPUTargetLowering& Lowering =
Matt Arsenault209a7b92014-04-18 07:40:20 +00002550 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002551 bool IsModified = false;
2552 do {
2553 IsModified = false;
Matt Arsenault68f05052017-12-04 22:18:27 +00002554
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002555 // Go over all selected nodes and try to fold them a bit more
Matt Arsenault68f05052017-12-04 22:18:27 +00002556 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin();
2557 while (Position != CurDAG->allnodes_end()) {
2558 SDNode *Node = &*Position++;
2559 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002560 if (!MachineNode)
2561 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +00002562
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002563 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
Matt Arsenault68f05052017-12-04 22:18:27 +00002564 if (ResNode != Node) {
2565 if (ResNode)
2566 ReplaceUses(Node, ResNode);
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002567 IsModified = true;
2568 }
Tom Stellard2183b702013-06-03 17:39:46 +00002569 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +00002570 CurDAG->RemoveDeadNodes();
2571 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +00002572}
Tom Stellard20287692017-08-08 04:57:55 +00002573
Tom Stellardc5a154d2018-06-28 23:47:12 +00002574bool R600DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
2575 Subtarget = &MF.getSubtarget<R600Subtarget>();
2576 return SelectionDAGISel::runOnMachineFunction(MF);
2577}
2578
2579bool R600DAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const {
2580 if (!N->readMem())
2581 return false;
2582 if (CbId == -1)
Matt Arsenault0da63502018-08-31 05:49:54 +00002583 return N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
2584 N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002585
Matt Arsenault0da63502018-08-31 05:49:54 +00002586 return N->getAddressSpace() == AMDGPUAS::CONSTANT_BUFFER_0 + CbId;
Tom Stellardc5a154d2018-06-28 23:47:12 +00002587}
2588
2589bool R600DAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
2590 SDValue& IntPtr) {
2591 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
2592 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr),
2593 true);
2594 return true;
2595 }
2596 return false;
2597}
2598
2599bool R600DAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
2600 SDValue& BaseReg, SDValue &Offset) {
2601 if (!isa<ConstantSDNode>(Addr)) {
2602 BaseReg = Addr;
2603 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true);
2604 return true;
2605 }
2606 return false;
2607}
2608
Tom Stellard20287692017-08-08 04:57:55 +00002609void R600DAGToDAGISel::Select(SDNode *N) {
2610 unsigned int Opc = N->getOpcode();
2611 if (N->isMachineOpcode()) {
2612 N->setNodeId(-1);
2613 return; // Already selected.
2614 }
2615
2616 switch (Opc) {
2617 default: break;
2618 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
2619 case ISD::SCALAR_TO_VECTOR:
2620 case ISD::BUILD_VECTOR: {
2621 EVT VT = N->getValueType(0);
2622 unsigned NumVectorElts = VT.getVectorNumElements();
2623 unsigned RegClassID;
2624 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
2625 // that adds a 128 bits reg copy when going through TwoAddressInstructions
2626 // pass. We want to avoid 128 bits copies as much as possible because they
2627 // can't be bundled by our scheduler.
2628 switch(NumVectorElts) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002629 case 2: RegClassID = R600::R600_Reg64RegClassID; break;
Tom Stellard20287692017-08-08 04:57:55 +00002630 case 4:
2631 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
Tom Stellardc5a154d2018-06-28 23:47:12 +00002632 RegClassID = R600::R600_Reg128VerticalRegClassID;
Tom Stellard20287692017-08-08 04:57:55 +00002633 else
Tom Stellardc5a154d2018-06-28 23:47:12 +00002634 RegClassID = R600::R600_Reg128RegClassID;
Tom Stellard20287692017-08-08 04:57:55 +00002635 break;
2636 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
2637 }
2638 SelectBuildVector(N, RegClassID);
2639 return;
2640 }
2641 }
2642
2643 SelectCode(N);
2644}
2645
2646bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
2647 SDValue &Offset) {
2648 ConstantSDNode *C;
2649 SDLoc DL(Addr);
2650
2651 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002652 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002653 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2654 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
2655 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00002656 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002657 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2658 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
2659 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
2660 Base = Addr.getOperand(0);
2661 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2662 } else {
2663 Base = Addr;
2664 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
2665 }
2666
2667 return true;
2668}
2669
2670bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
2671 SDValue &Offset) {
2672 ConstantSDNode *IMMOffset;
2673
2674 if (Addr.getOpcode() == ISD::ADD
2675 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
2676 && isInt<16>(IMMOffset->getZExtValue())) {
2677
2678 Base = Addr.getOperand(0);
2679 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2680 MVT::i32);
2681 return true;
2682 // If the pointer address is constant, we can move it to the offset field.
2683 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
2684 && isInt<16>(IMMOffset->getZExtValue())) {
2685 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
2686 SDLoc(CurDAG->getEntryNode()),
Tom Stellardc5a154d2018-06-28 23:47:12 +00002687 R600::ZERO, MVT::i32);
Tom Stellard20287692017-08-08 04:57:55 +00002688 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2689 MVT::i32);
2690 return true;
2691 }
2692
2693 // Default case, no offset
2694 Base = Addr;
2695 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
2696 return true;
2697}