LLVM 23.0.0git
AMDGPUInstructionSelector.h
Go to the documentation of this file.
1//===- AMDGPUInstructionSelector --------------------------------*- C++ -*-==//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file declares the targeting of the InstructionSelector class for
10/// AMDGPU.
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUINSTRUCTIONSELECTOR_H
14#define LLVM_LIB_TARGET_AMDGPU_AMDGPUINSTRUCTIONSELECTOR_H
15
16#include "SIDefines.h"
18#include "llvm/IR/InstrTypes.h"
19
20namespace {
21#define GET_GLOBALISEL_PREDICATE_BITSET
22#define AMDGPUSubtarget GCNSubtarget
23#include "AMDGPUGenGlobalISel.inc"
24#undef GET_GLOBALISEL_PREDICATE_BITSET
25#undef AMDGPUSubtarget
26}
27
28namespace llvm {
29
30namespace AMDGPU {
32}
33
38class GCNSubtarget;
39class MachineInstr;
41class MachineOperand;
43class RegisterBank;
44class SIInstrInfo;
45class SIRegisterInfo;
47
49private:
51 const GCNSubtarget *Subtarget;
52
53public:
55 const AMDGPURegisterBankInfo &RBI,
56 const AMDGPUTargetMachine &TM);
57
58 bool select(MachineInstr &I) override;
59 static const char *getName();
60
63 BlockFrequencyInfo *BFI) override;
64
65private:
66 struct GEPInfo {
69 int64_t Imm = 0;
70 };
71
72 bool isSGPR(Register Reg) const;
73
74 bool isInstrUniform(const MachineInstr &MI) const;
75 bool isVCC(Register Reg, const MachineRegisterInfo &MRI) const;
76
77 const RegisterBank *getArtifactRegBank(
79 const TargetRegisterInfo &TRI) const;
80
81 /// tblgen-erated 'select' implementation.
82 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
83
84 MachineOperand getSubOperand64(MachineOperand &MO,
85 const TargetRegisterClass &SubRC,
86 unsigned SubIdx) const;
87
88 bool constrainCopyLikeIntrin(MachineInstr &MI, unsigned NewOpc) const;
89 bool selectCOPY(MachineInstr &I) const;
90 bool selectCOPY_SCC_VCC(MachineInstr &I) const;
91 bool selectCOPY_VCC_SCC(MachineInstr &I) const;
92 bool selectReadAnyLane(MachineInstr &I) const;
93 bool selectPHI(MachineInstr &I) const;
94 bool selectG_TRUNC(MachineInstr &I) const;
95 bool selectG_SZA_EXT(MachineInstr &I) const;
96 bool selectG_FPEXT(MachineInstr &I) const;
97 bool selectG_FNEG(MachineInstr &I) const;
98 bool selectG_FABS(MachineInstr &I) const;
99 bool selectG_AND_OR_XOR(MachineInstr &I) const;
100 bool selectG_ADD_SUB(MachineInstr &I) const;
101 bool selectG_UADDO_USUBO_UADDE_USUBE(MachineInstr &I) const;
102 bool selectG_AMDGPU_MAD_64_32(MachineInstr &I) const;
103 bool selectG_EXTRACT(MachineInstr &I) const;
104 bool selectG_FMA_FMAD(MachineInstr &I) const;
105 bool selectS16MergeToS32(MachineInstr &MI) const;
106 bool selectG_MERGE_VALUES(MachineInstr &I) const;
107 bool selectG_UNMERGE_VALUES(MachineInstr &I) const;
108 bool selectG_BUILD_VECTOR(MachineInstr &I) const;
109 bool selectG_IMPLICIT_DEF(MachineInstr &I) const;
110 bool selectG_INSERT(MachineInstr &I) const;
111 bool selectG_SBFX_UBFX(MachineInstr &I) const;
112
113 bool selectInterpP1F16(MachineInstr &MI) const;
114 bool selectWritelane(MachineInstr &MI) const;
115 bool selectDivScale(MachineInstr &MI) const;
116 bool selectIntrinsicCmp(MachineInstr &MI) const;
117 bool selectBallot(MachineInstr &I) const;
118 bool selectRelocConstant(MachineInstr &I) const;
119 bool selectGroupStaticSize(MachineInstr &I) const;
120 bool selectReturnAddress(MachineInstr &I) const;
121 bool selectG_INTRINSIC(MachineInstr &I) const;
122
123 bool selectEndCfIntrinsic(MachineInstr &MI) const;
124 bool selectDSOrderedIntrinsic(MachineInstr &MI, Intrinsic::ID IID) const;
125 bool selectDSGWSIntrinsic(MachineInstr &MI, Intrinsic::ID IID) const;
126 bool selectDSAppendConsume(MachineInstr &MI, bool IsAppend) const;
127 bool selectInitWholeWave(MachineInstr &MI) const;
128 bool selectDSBvhStackIntrinsic(MachineInstr &MI) const;
129
130 bool selectImageIntrinsic(MachineInstr &MI,
131 const AMDGPU::ImageDimIntrinsicInfo *Intr) const;
132 bool selectG_INTRINSIC_W_SIDE_EFFECTS(MachineInstr &I) const;
133 int getS_CMPOpcode(CmpInst::Predicate P, unsigned Size) const;
134 bool selectG_ICMP_or_FCMP(MachineInstr &I) const;
135 bool hasVgprParts(ArrayRef<GEPInfo> AddrInfo) const;
136 void getAddrModeInfo(const MachineInstr &Load, const MachineRegisterInfo &MRI,
137 SmallVectorImpl<GEPInfo> &AddrInfo) const;
138
139 void initM0(MachineInstr &I) const;
140 bool selectG_LOAD_STORE_ATOMICRMW(MachineInstr &I) const;
141 bool selectG_SELECT(MachineInstr &I) const;
142 bool selectG_BRCOND(MachineInstr &I) const;
143 bool selectG_GLOBAL_VALUE(MachineInstr &I) const;
144 bool selectG_PTRMASK(MachineInstr &I) const;
145 bool selectG_EXTRACT_VECTOR_ELT(MachineInstr &I) const;
146 bool selectG_INSERT_VECTOR_ELT(MachineInstr &I) const;
147 bool selectBufferLoadLds(MachineInstr &MI) const;
148 bool selectGlobalLoadLds(MachineInstr &MI) const;
149 bool selectTensorLoadStore(MachineInstr &MI, Intrinsic::ID IID) const;
150 bool selectBVHIntersectRayIntrinsic(MachineInstr &I) const;
151 bool selectSMFMACIntrin(MachineInstr &I) const;
152 bool selectPermlaneSwapIntrin(MachineInstr &I, Intrinsic::ID IntrID) const;
153 bool selectWaveAddress(MachineInstr &I) const;
154 bool selectBITOP3(MachineInstr &I) const;
155 bool selectStackRestore(MachineInstr &MI) const;
156 bool selectNamedBarrierInit(MachineInstr &I, Intrinsic::ID IID) const;
157 bool selectNamedBarrierInst(MachineInstr &I, Intrinsic::ID IID) const;
158 bool selectSBarrierSignalIsfirst(MachineInstr &I, Intrinsic::ID IID) const;
159 bool selectSGetBarrierState(MachineInstr &I, Intrinsic::ID IID) const;
160 bool selectSBarrierLeave(MachineInstr &I) const;
161 bool selectWaveShuffleIntrin(MachineInstr &I) const;
162
163 std::pair<Register, unsigned> selectVOP3ModsImpl(Register Src,
164 bool IsCanonicalizing = true,
165 bool AllowAbs = true,
166 bool OpSel = false) const;
167 std::pair<Register, unsigned> selectVOP3PModsF32Impl(Register Src) const;
168
169 Register copyToVGPRIfSrcFolded(Register Src, unsigned Mods,
170 MachineOperand Root, MachineInstr *InsertPt,
171 bool ForceVGPR = false) const;
172
174 selectVCSRC(MachineOperand &Root) const;
175
177 selectVSRC0(MachineOperand &Root) const;
178
180 selectVOP3Mods0(MachineOperand &Root) const;
182 selectVOP3BMods0(MachineOperand &Root) const;
184 selectVOP3OMods(MachineOperand &Root) const;
186 selectVOP3Mods(MachineOperand &Root) const;
188 selectVOP3ModsNonCanonicalizing(MachineOperand &Root) const;
190 selectVOP3BMods(MachineOperand &Root) const;
191
192 ComplexRendererFns selectVOP3NoMods(MachineOperand &Root) const;
193
194 std::pair<Register, unsigned>
195 selectVOP3PModsImpl(Register RootReg, const MachineRegisterInfo &MRI,
196 bool IsDOT = false) const;
198 selectVOP3PRetHelper(MachineOperand &Root, bool IsDOT = false) const;
199
201 selectVOP3PMods(MachineOperand &Root) const;
202
204 selectVOP3PModsDOT(MachineOperand &Root) const;
206 selectVOP3PNoModsDOT(MachineOperand &Root) const;
208 selectVOP3PModsF32(MachineOperand &Root) const;
210 selectVOP3PNoModsF32(MachineOperand &Root) const;
211
213 selectWMMAOpSelVOP3PMods(MachineOperand &Root) const;
214
216 selectWMMAModsF32NegAbs(MachineOperand &Root) const;
218 selectWMMAModsF16Neg(MachineOperand &Root) const;
220 selectWMMAModsF16NegAbs(MachineOperand &Root) const;
222 selectWMMAVISrc(MachineOperand &Root) const;
224 selectSWMMACIndex8(MachineOperand &Root) const;
226 selectSWMMACIndex16(MachineOperand &Root) const;
228 selectSWMMACIndex32(MachineOperand &Root) const;
229
231 selectVOP3OpSelMods(MachineOperand &Root) const;
232
234 selectVINTERPMods(MachineOperand &Root) const;
236 selectVINTERPModsHi(MachineOperand &Root) const;
237
238 bool selectScaleOffset(MachineOperand &Root, Register &Offset,
239 bool IsSigned) const;
240 bool selectSmrdOffset(MachineOperand &Root, Register &Base, Register *SOffset,
241 int64_t *Offset, bool *ScaleOffset) const;
243 selectSmrdImm(MachineOperand &Root) const;
245 selectSmrdImm32(MachineOperand &Root) const;
247 selectSmrdSgpr(MachineOperand &Root) const;
249 selectSmrdSgprImm(MachineOperand &Root) const;
250
251 std::pair<Register, int> selectFlatOffsetImpl(MachineOperand &Root,
252 uint64_t FlatVariant) const;
253
255 selectFlatOffset(MachineOperand &Root) const;
257 selectGlobalOffset(MachineOperand &Root) const;
259 selectScratchOffset(MachineOperand &Root) const;
260
262 selectGlobalSAddr(MachineOperand &Root, unsigned CPolBits,
263 bool NeedIOffset = true) const;
265 selectGlobalSAddr(MachineOperand &Root) const;
267 selectGlobalSAddrCPol(MachineOperand &Root) const;
269 selectGlobalSAddrCPolM0(MachineOperand &Root) const;
271 selectGlobalSAddrGLC(MachineOperand &Root) const;
273 selectGlobalSAddrNoIOffset(MachineOperand &Root) const;
275 selectGlobalSAddrNoIOffsetM0(MachineOperand &Root) const;
276
278 selectScratchSAddr(MachineOperand &Root) const;
279 bool checkFlatScratchSVSSwizzleBug(Register VAddr, Register SAddr,
280 uint64_t ImmOffset) const;
282 selectScratchSVAddr(MachineOperand &Root) const;
283
285 selectMUBUFScratchOffen(MachineOperand &Root) const;
287 selectMUBUFScratchOffset(MachineOperand &Root) const;
288
289 bool isDSOffsetLegal(Register Base, int64_t Offset) const;
290 bool isDSOffset2Legal(Register Base, int64_t Offset0, int64_t Offset1,
291 unsigned Size) const;
292 bool isFlatScratchBaseLegal(Register Addr) const;
293 bool isFlatScratchBaseLegalSV(Register Addr) const;
294 bool isFlatScratchBaseLegalSVImm(Register Addr) const;
295
296 std::pair<Register, unsigned>
297 selectDS1Addr1OffsetImpl(MachineOperand &Root) const;
299 selectDS1Addr1Offset(MachineOperand &Root) const;
300
302 selectDS64Bit4ByteAligned(MachineOperand &Root) const;
303
305 selectDS128Bit8ByteAligned(MachineOperand &Root) const;
306
307 std::pair<Register, unsigned> selectDSReadWrite2Impl(MachineOperand &Root,
308 unsigned size) const;
310 selectDSReadWrite2(MachineOperand &Root, unsigned size) const;
311
312 std::tuple<Register, int64_t, bool>
313 getPtrBaseWithConstantOffset(Register Root,
314 const MachineRegisterInfo &MRI) const;
315
316 // Parse out a chain of up to two g_ptr_add instructions.
317 // g_ptr_add (n0, _)
318 // g_ptr_add (n0, (n1 = g_ptr_add n2, n3))
319 struct MUBUFAddressData {
320 Register N0, N2, N3;
321 int64_t Offset = 0;
322 };
323
324 bool shouldUseAddr64(MUBUFAddressData AddrData) const;
325
326 void splitIllegalMUBUFOffset(MachineIRBuilder &B,
327 Register &SOffset, int64_t &ImmOffset) const;
328
329 MUBUFAddressData parseMUBUFAddress(Register Src) const;
330
331 bool selectMUBUFAddr64Impl(MachineOperand &Root, Register &VAddr,
332 Register &RSrcReg, Register &SOffset,
333 int64_t &Offset) const;
334
335 bool selectMUBUFOffsetImpl(MachineOperand &Root, Register &RSrcReg,
336 Register &SOffset, int64_t &Offset) const;
337
339 selectBUFSOffset(MachineOperand &Root) const;
340
342 selectMUBUFAddr64(MachineOperand &Root) const;
343
345 selectMUBUFOffset(MachineOperand &Root) const;
346
347 ComplexRendererFns selectSMRDBufferImm(MachineOperand &Root) const;
348 ComplexRendererFns selectSMRDBufferImm32(MachineOperand &Root) const;
349 ComplexRendererFns selectSMRDBufferSgprImm(MachineOperand &Root) const;
350
351 std::pair<Register, unsigned> selectVOP3PMadMixModsImpl(MachineOperand &Root,
352 bool &Matched) const;
353 ComplexRendererFns selectVOP3PMadMixModsExt(MachineOperand &Root) const;
354 ComplexRendererFns selectVOP3PMadMixMods(MachineOperand &Root) const;
355
356 void renderTruncImm32(MachineInstrBuilder &MIB, const MachineInstr &MI,
357 int OpIdx = -1) const;
358
359 void renderTruncTImm(MachineInstrBuilder &MIB, const MachineInstr &MI,
360 int OpIdx) const;
361 void renderZextBoolTImm(MachineInstrBuilder &MIB, const MachineInstr &MI,
362 int OpIdx) const;
363
364 void renderOpSelTImm(MachineInstrBuilder &MIB, const MachineInstr &MI,
365 int OpIdx) const;
366
367 void renderSrcAndDstSelToOpSelXForm_0_0(MachineInstrBuilder &MIB,
368 const MachineInstr &MI,
369 int OpIdx) const;
370
371 void renderSrcAndDstSelToOpSelXForm_0_1(MachineInstrBuilder &MIB,
372 const MachineInstr &MI,
373 int OpIdx) const;
374
375 void renderSrcAndDstSelToOpSelXForm_1_0(MachineInstrBuilder &MIB,
376 const MachineInstr &MI,
377 int OpIdx) const;
378
379 void renderSrcAndDstSelToOpSelXForm_1_1(MachineInstrBuilder &MIB,
380 const MachineInstr &MI,
381 int OpIdx) const;
382
383 void renderDstSelToOpSelXForm(MachineInstrBuilder &MIB,
384 const MachineInstr &MI, int OpIdx) const;
385
386 void renderSrcSelToOpSelXForm(MachineInstrBuilder &MIB,
387 const MachineInstr &MI, int OpIdx) const;
388
389 void renderSrcAndDstSelToOpSelXForm_2_0(MachineInstrBuilder &MIB,
390 const MachineInstr &MI,
391 int OpIdx) const;
392
393 void renderDstSelToOpSel3XFormXForm(MachineInstrBuilder &MIB,
394 const MachineInstr &MI, int OpIdx) const;
395
396 void renderNegateImm(MachineInstrBuilder &MIB, const MachineInstr &MI,
397 int OpIdx) const;
398
399 void renderBitcastFPImm(MachineInstrBuilder &MIB, const MachineInstr &MI,
400 int OpIdx) const;
401
402 void renderBitcastFPImm32(MachineInstrBuilder &MIB, const MachineInstr &MI,
403 int OpIdx) const {
404 renderBitcastFPImm(MIB, MI, OpIdx);
405 }
406 void renderBitcastFPImm64(MachineInstrBuilder &MIB, const MachineInstr &MI,
407 int OpIdx) const {
408 renderBitcastFPImm(MIB, MI, OpIdx);
409 }
410
411 void renderCountTrailingOnesImm(MachineInstrBuilder &MIB,
412 const MachineInstr &MI, int OpIdx) const;
413 void renderExtractCPol(MachineInstrBuilder &MIB, const MachineInstr &MI,
414 int OpIdx) const;
415 void renderExtractSWZ(MachineInstrBuilder &MIB, const MachineInstr &MI,
416 int OpIdx) const;
417 void renderExtractCpolSetGLC(MachineInstrBuilder &MIB, const MachineInstr &MI,
418 int OpIdx) const;
419
420 void renderFrameIndex(MachineInstrBuilder &MIB, const MachineInstr &MI,
421 int OpIdx) const;
422
423 void renderFPPow2ToExponent(MachineInstrBuilder &MIB, const MachineInstr &MI,
424 int OpIdx) const;
425
426 void renderRoundMode(MachineInstrBuilder &MIB, const MachineInstr &MI,
427 int OpIdx) const;
428
429 void renderVOP3PModsNeg(MachineInstrBuilder &MIB, const MachineInstr &MI,
430 int OpIdx) const;
431 void renderVOP3PModsNegs(MachineInstrBuilder &MIB, const MachineInstr &MI,
432 int OpIdx) const;
433 void renderVOP3PModsNegAbs(MachineInstrBuilder &MIB, const MachineInstr &MI,
434 int OpIdx) const;
435
436 void renderPrefetchLoc(MachineInstrBuilder &MIB, const MachineInstr &MI,
437 int OpIdx) const;
438
439 void renderScaledMAIIntrinsicOperand(MachineInstrBuilder &MIB,
440 const MachineInstr &MI, int OpIdx) const;
441
442 bool isInlineImmediate(const APInt &Imm) const;
443 bool isInlineImmediate(const APFloat &Imm) const;
444
445 // Returns true if TargetOpcode::G_AND MachineInstr `MI`'s masking of the
446 // shift amount operand's `ShAmtBits` bits is unneeded.
447 bool isUnneededShiftMask(const MachineInstr &MI, unsigned ShAmtBits) const;
448
449 /// Match a zero extend from a 32-bit value to 64-bits.
450 Register matchZeroExtendFromS32(Register Reg) const;
451 /// Match a sign extend from a 32-bit value to 64-bits.
452 Register matchSignExtendFromS32(Register Reg) const;
453 /// Match a zero extend from a 32-bit value to 64-bits, or \p Reg itself if it
454 /// is 32-bit.
455 Register matchZeroExtendFromS32OrS32(Register Reg) const;
456 /// Match a sign extend from a 32-bit value to 64-bits, or \p Reg itself if it
457 /// is 32-bit.
458 Register matchSignExtendFromS32OrS32(Register Reg) const;
459 /// Match either sign or zero extend depending on the \p IsSigned from a
460 /// 32-bit value to 64-bits, or \p Reg itself if it is 32-bit.
461 Register matchExtendFromS32OrS32(Register Reg, bool IsSigned) const;
462 /// Match an any extend from a 32-bit value to 64-bit.
463 Register matchAnyExtendFromS32(Register Reg) const;
464
465 const SIInstrInfo &TII;
466 const SIRegisterInfo &TRI;
467 const AMDGPURegisterBankInfo &RBI;
468 const AMDGPUTargetMachine &TM;
469 const GCNSubtarget &STI;
470#define GET_GLOBALISEL_PREDICATES_DECL
471#define AMDGPUSubtarget GCNSubtarget
472#include "AMDGPUGenGlobalISel.inc"
473#undef GET_GLOBALISEL_PREDICATES_DECL
474#undef AMDGPUSubtarget
475
476#define GET_GLOBALISEL_TEMPORARIES_DECL
477#include "AMDGPUGenGlobalISel.inc"
478#undef GET_GLOBALISEL_TEMPORARIES_DECL
479};
480
481} // End llvm namespace.
482#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
MachineInstr unsigned OpIdx
#define P(N)
AMDGPUInstructionSelector(const GCNSubtarget &STI, const AMDGPURegisterBankInfo &RBI, const AMDGPUTargetMachine &TM)
static const char * getName()
bool select(MachineInstr &I) override
Select the (possibly generic) instruction I to only use target-specific opcodes.
void setupMF(MachineFunction &MF, GISelValueTracking *VT, CodeGenCoverage *CoverageInfo, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) override
Setup per-MF executor state.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
std::optional< SmallVector< std::function< void(MachineInstrBuilder &)>, 4 > > ComplexRendererFns
Helper class to build MachineInstr.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Analysis providing profile information.
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1668