1 //===- SIInstrInfo.h - SI Instruction Info Interface ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Interface definition for SIInstrInfo.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_AMDGPU_SIINSTRINFO_H
15 #define LLVM_LIB_TARGET_AMDGPU_SIINSTRINFO_H
17 #include "AMDGPUInstrInfo.h"
18 #include "SIDefines.h"
19 #include "SIRegisterInfo.h"
20 #include "Utils/AMDGPUBaseInfo.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineOperand.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/Support/Compiler.h"
33 #define GET_INSTRINFO_HEADER
34 #include "AMDGPUGenInstrInfo.inc"
39 class MachineDominatorTree
;
40 class MachineRegisterInfo
;
43 class TargetRegisterClass
;
45 class SIInstrInfo final
: public AMDGPUGenInstrInfo
{
47 const SIRegisterInfo RI
;
48 const GCNSubtarget
&ST
;
50 // The inverse predicate should have the negative value.
51 enum BranchPredicate
{
61 using SetVectorType
= SmallSetVector
<MachineInstr
*, 32>;
63 static unsigned getBranchOpcode(BranchPredicate Cond
);
64 static BranchPredicate
getBranchPredicate(unsigned Opcode
);
67 unsigned buildExtractSubReg(MachineBasicBlock::iterator MI
,
68 MachineRegisterInfo
&MRI
,
69 MachineOperand
&SuperReg
,
70 const TargetRegisterClass
*SuperRC
,
72 const TargetRegisterClass
*SubRC
) const;
73 MachineOperand
buildExtractSubRegOrImm(MachineBasicBlock::iterator MI
,
74 MachineRegisterInfo
&MRI
,
75 MachineOperand
&SuperReg
,
76 const TargetRegisterClass
*SuperRC
,
78 const TargetRegisterClass
*SubRC
) const;
80 void swapOperands(MachineInstr
&Inst
) const;
82 bool moveScalarAddSub(SetVectorType
&Worklist
, MachineInstr
&Inst
,
83 MachineDominatorTree
*MDT
= nullptr) const;
85 void lowerScalarAbs(SetVectorType
&Worklist
,
86 MachineInstr
&Inst
) const;
88 void lowerScalarXnor(SetVectorType
&Worklist
,
89 MachineInstr
&Inst
) const;
91 void splitScalarNotBinop(SetVectorType
&Worklist
,
93 unsigned Opcode
) const;
95 void splitScalarBinOpN2(SetVectorType
&Worklist
,
97 unsigned Opcode
) const;
99 void splitScalar64BitUnaryOp(SetVectorType
&Worklist
,
100 MachineInstr
&Inst
, unsigned Opcode
) const;
102 void splitScalar64BitAddSub(SetVectorType
&Worklist
, MachineInstr
&Inst
,
103 MachineDominatorTree
*MDT
= nullptr) const;
105 void splitScalar64BitBinaryOp(SetVectorType
&Worklist
, MachineInstr
&Inst
,
107 MachineDominatorTree
*MDT
= nullptr) const;
109 void splitScalar64BitXnor(SetVectorType
&Worklist
, MachineInstr
&Inst
,
110 MachineDominatorTree
*MDT
= nullptr) const;
112 void splitScalar64BitBCNT(SetVectorType
&Worklist
,
113 MachineInstr
&Inst
) const;
114 void splitScalar64BitBFE(SetVectorType
&Worklist
,
115 MachineInstr
&Inst
) const;
116 void movePackToVALU(SetVectorType
&Worklist
,
117 MachineRegisterInfo
&MRI
,
118 MachineInstr
&Inst
) const;
120 void addUsersToMoveToVALUWorklist(unsigned Reg
, MachineRegisterInfo
&MRI
,
121 SetVectorType
&Worklist
) const;
123 void addSCCDefUsersToVALUWorklist(MachineOperand
&Op
,
124 MachineInstr
&SCCDefInst
,
125 SetVectorType
&Worklist
) const;
127 const TargetRegisterClass
*
128 getDestEquivalentVGPRClass(const MachineInstr
&Inst
) const;
130 bool checkInstOffsetsDoNotOverlap(const MachineInstr
&MIa
,
131 const MachineInstr
&MIb
) const;
133 unsigned findUsedSGPR(const MachineInstr
&MI
, int OpIndices
[3]) const;
136 bool swapSourceModifiers(MachineInstr
&MI
,
137 MachineOperand
&Src0
, unsigned Src0OpName
,
138 MachineOperand
&Src1
, unsigned Src1OpName
) const;
140 MachineInstr
*commuteInstructionImpl(MachineInstr
&MI
, bool NewMI
,
142 unsigned OpIdx1
) const override
;
145 enum TargetOperandFlags
{
149 // MO_GOTPCREL -> symbol@GOTPCREL -> R_AMDGPU_GOTPCREL.
151 // MO_GOTPCREL32_LO -> symbol@gotpcrel32@lo -> R_AMDGPU_GOTPCREL32_LO.
153 MO_GOTPCREL32_LO
= 2,
154 // MO_GOTPCREL32_HI -> symbol@gotpcrel32@hi -> R_AMDGPU_GOTPCREL32_HI.
155 MO_GOTPCREL32_HI
= 3,
156 // MO_REL32_LO -> symbol@rel32@lo -> R_AMDGPU_REL32_LO.
159 // MO_REL32_HI -> symbol@rel32@hi -> R_AMDGPU_REL32_HI.
162 MO_LONG_BRANCH_FORWARD
= 6,
163 MO_LONG_BRANCH_BACKWARD
= 7,
169 explicit SIInstrInfo(const GCNSubtarget
&ST
);
171 const SIRegisterInfo
&getRegisterInfo() const {
175 bool isReallyTriviallyReMaterializable(const MachineInstr
&MI
,
176 AAResults
*AA
) const override
;
178 bool areLoadsFromSameBasePtr(SDNode
*Load1
, SDNode
*Load2
,
180 int64_t &Offset2
) const override
;
182 bool getMemOperandWithOffset(const MachineInstr
&LdSt
,
183 const MachineOperand
*&BaseOp
,
185 const TargetRegisterInfo
*TRI
) const final
;
187 bool shouldClusterMemOps(const MachineOperand
&BaseOp1
,
188 const MachineOperand
&BaseOp2
,
189 unsigned NumLoads
) const override
;
191 bool shouldScheduleLoadsNear(SDNode
*Load0
, SDNode
*Load1
, int64_t Offset0
,
192 int64_t Offset1
, unsigned NumLoads
) const override
;
194 void copyPhysReg(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
195 const DebugLoc
&DL
, unsigned DestReg
, unsigned SrcReg
,
196 bool KillSrc
) const override
;
198 unsigned calculateLDSSpillAddress(MachineBasicBlock
&MBB
, MachineInstr
&MI
,
199 RegScavenger
*RS
, unsigned TmpReg
,
200 unsigned Offset
, unsigned Size
) const;
202 void materializeImmediate(MachineBasicBlock
&MBB
,
203 MachineBasicBlock::iterator MI
,
206 int64_t Value
) const;
208 const TargetRegisterClass
*getPreferredSelectRegClass(
209 unsigned Size
) const;
211 unsigned insertNE(MachineBasicBlock
*MBB
,
212 MachineBasicBlock::iterator I
, const DebugLoc
&DL
,
213 unsigned SrcReg
, int Value
) const;
215 unsigned insertEQ(MachineBasicBlock
*MBB
,
216 MachineBasicBlock::iterator I
, const DebugLoc
&DL
,
217 unsigned SrcReg
, int Value
) const;
219 void storeRegToStackSlot(MachineBasicBlock
&MBB
,
220 MachineBasicBlock::iterator MI
, unsigned SrcReg
,
221 bool isKill
, int FrameIndex
,
222 const TargetRegisterClass
*RC
,
223 const TargetRegisterInfo
*TRI
) const override
;
225 void loadRegFromStackSlot(MachineBasicBlock
&MBB
,
226 MachineBasicBlock::iterator MI
, unsigned DestReg
,
227 int FrameIndex
, const TargetRegisterClass
*RC
,
228 const TargetRegisterInfo
*TRI
) const override
;
230 bool expandPostRAPseudo(MachineInstr
&MI
) const override
;
232 // Splits a V_MOV_B64_DPP_PSEUDO opcode into a pair of v_mov_b32_dpp
233 // instructions. Returns a pair of generated instructions.
234 // Can split either post-RA with physical registers or pre-RA with
235 // virtual registers. In latter case IR needs to be in SSA form and
236 // and a REG_SEQUENCE is produced to define original register.
237 std::pair
<MachineInstr
*, MachineInstr
*>
238 expandMovDPP64(MachineInstr
&MI
) const;
240 // Returns an opcode that can be used to move a value to a \p DstRC
241 // register. If there is no hardware instruction that can store to \p
242 // DstRC, then AMDGPU::COPY is returned.
243 unsigned getMovOpcode(const TargetRegisterClass
*DstRC
) const;
246 int commuteOpcode(unsigned Opc
) const;
249 inline int commuteOpcode(const MachineInstr
&MI
) const {
250 return commuteOpcode(MI
.getOpcode());
253 bool findCommutedOpIndices(const MachineInstr
&MI
, unsigned &SrcOpIdx1
,
254 unsigned &SrcOpIdx2
) const override
;
256 bool findCommutedOpIndices(MCInstrDesc Desc
, unsigned & SrcOpIdx0
,
257 unsigned & SrcOpIdx1
) const;
259 bool isBranchOffsetInRange(unsigned BranchOpc
,
260 int64_t BrOffset
) const override
;
262 MachineBasicBlock
*getBranchDestBlock(const MachineInstr
&MI
) const override
;
264 unsigned insertIndirectBranch(MachineBasicBlock
&MBB
,
265 MachineBasicBlock
&NewDestBB
,
268 RegScavenger
*RS
= nullptr) const override
;
270 bool analyzeBranchImpl(MachineBasicBlock
&MBB
,
271 MachineBasicBlock::iterator I
,
272 MachineBasicBlock
*&TBB
,
273 MachineBasicBlock
*&FBB
,
274 SmallVectorImpl
<MachineOperand
> &Cond
,
275 bool AllowModify
) const;
277 bool analyzeBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*&TBB
,
278 MachineBasicBlock
*&FBB
,
279 SmallVectorImpl
<MachineOperand
> &Cond
,
280 bool AllowModify
= false) const override
;
282 unsigned removeBranch(MachineBasicBlock
&MBB
,
283 int *BytesRemoved
= nullptr) const override
;
285 unsigned insertBranch(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
286 MachineBasicBlock
*FBB
, ArrayRef
<MachineOperand
> Cond
,
288 int *BytesAdded
= nullptr) const override
;
290 bool reverseBranchCondition(
291 SmallVectorImpl
<MachineOperand
> &Cond
) const override
;
293 bool canInsertSelect(const MachineBasicBlock
&MBB
,
294 ArrayRef
<MachineOperand
> Cond
,
295 unsigned TrueReg
, unsigned FalseReg
,
297 int &TrueCycles
, int &FalseCycles
) const override
;
299 void insertSelect(MachineBasicBlock
&MBB
,
300 MachineBasicBlock::iterator I
, const DebugLoc
&DL
,
301 unsigned DstReg
, ArrayRef
<MachineOperand
> Cond
,
302 unsigned TrueReg
, unsigned FalseReg
) const override
;
304 void insertVectorSelect(MachineBasicBlock
&MBB
,
305 MachineBasicBlock::iterator I
, const DebugLoc
&DL
,
306 unsigned DstReg
, ArrayRef
<MachineOperand
> Cond
,
307 unsigned TrueReg
, unsigned FalseReg
) const;
309 unsigned getAddressSpaceForPseudoSourceKind(
310 unsigned Kind
) const override
;
313 areMemAccessesTriviallyDisjoint(const MachineInstr
&MIa
,
314 const MachineInstr
&MIb
) const override
;
316 bool isFoldableCopy(const MachineInstr
&MI
) const;
318 bool FoldImmediate(MachineInstr
&UseMI
, MachineInstr
&DefMI
, unsigned Reg
,
319 MachineRegisterInfo
*MRI
) const final
;
321 unsigned getMachineCSELookAheadLimit() const override
{ return 500; }
323 MachineInstr
*convertToThreeAddress(MachineFunction::iterator
&MBB
,
325 LiveVariables
*LV
) const override
;
327 bool isSchedulingBoundary(const MachineInstr
&MI
,
328 const MachineBasicBlock
*MBB
,
329 const MachineFunction
&MF
) const override
;
331 static bool isSALU(const MachineInstr
&MI
) {
332 return MI
.getDesc().TSFlags
& SIInstrFlags::SALU
;
335 bool isSALU(uint16_t Opcode
) const {
336 return get(Opcode
).TSFlags
& SIInstrFlags::SALU
;
339 static bool isVALU(const MachineInstr
&MI
) {
340 return MI
.getDesc().TSFlags
& SIInstrFlags::VALU
;
343 bool isVALU(uint16_t Opcode
) const {
344 return get(Opcode
).TSFlags
& SIInstrFlags::VALU
;
347 static bool isVMEM(const MachineInstr
&MI
) {
348 return isMUBUF(MI
) || isMTBUF(MI
) || isMIMG(MI
);
351 bool isVMEM(uint16_t Opcode
) const {
352 return isMUBUF(Opcode
) || isMTBUF(Opcode
) || isMIMG(Opcode
);
355 static bool isSOP1(const MachineInstr
&MI
) {
356 return MI
.getDesc().TSFlags
& SIInstrFlags::SOP1
;
359 bool isSOP1(uint16_t Opcode
) const {
360 return get(Opcode
).TSFlags
& SIInstrFlags::SOP1
;
363 static bool isSOP2(const MachineInstr
&MI
) {
364 return MI
.getDesc().TSFlags
& SIInstrFlags::SOP2
;
367 bool isSOP2(uint16_t Opcode
) const {
368 return get(Opcode
).TSFlags
& SIInstrFlags::SOP2
;
371 static bool isSOPC(const MachineInstr
&MI
) {
372 return MI
.getDesc().TSFlags
& SIInstrFlags::SOPC
;
375 bool isSOPC(uint16_t Opcode
) const {
376 return get(Opcode
).TSFlags
& SIInstrFlags::SOPC
;
379 static bool isSOPK(const MachineInstr
&MI
) {
380 return MI
.getDesc().TSFlags
& SIInstrFlags::SOPK
;
383 bool isSOPK(uint16_t Opcode
) const {
384 return get(Opcode
).TSFlags
& SIInstrFlags::SOPK
;
387 static bool isSOPP(const MachineInstr
&MI
) {
388 return MI
.getDesc().TSFlags
& SIInstrFlags::SOPP
;
391 bool isSOPP(uint16_t Opcode
) const {
392 return get(Opcode
).TSFlags
& SIInstrFlags::SOPP
;
395 static bool isPacked(const MachineInstr
&MI
) {
396 return MI
.getDesc().TSFlags
& SIInstrFlags::IsPacked
;
399 bool isPacked(uint16_t Opcode
) const {
400 return get(Opcode
).TSFlags
& SIInstrFlags::IsPacked
;
403 static bool isVOP1(const MachineInstr
&MI
) {
404 return MI
.getDesc().TSFlags
& SIInstrFlags::VOP1
;
407 bool isVOP1(uint16_t Opcode
) const {
408 return get(Opcode
).TSFlags
& SIInstrFlags::VOP1
;
411 static bool isVOP2(const MachineInstr
&MI
) {
412 return MI
.getDesc().TSFlags
& SIInstrFlags::VOP2
;
415 bool isVOP2(uint16_t Opcode
) const {
416 return get(Opcode
).TSFlags
& SIInstrFlags::VOP2
;
419 static bool isVOP3(const MachineInstr
&MI
) {
420 return MI
.getDesc().TSFlags
& SIInstrFlags::VOP3
;
423 bool isVOP3(uint16_t Opcode
) const {
424 return get(Opcode
).TSFlags
& SIInstrFlags::VOP3
;
427 static bool isSDWA(const MachineInstr
&MI
) {
428 return MI
.getDesc().TSFlags
& SIInstrFlags::SDWA
;
431 bool isSDWA(uint16_t Opcode
) const {
432 return get(Opcode
).TSFlags
& SIInstrFlags::SDWA
;
435 static bool isVOPC(const MachineInstr
&MI
) {
436 return MI
.getDesc().TSFlags
& SIInstrFlags::VOPC
;
439 bool isVOPC(uint16_t Opcode
) const {
440 return get(Opcode
).TSFlags
& SIInstrFlags::VOPC
;
443 static bool isMUBUF(const MachineInstr
&MI
) {
444 return MI
.getDesc().TSFlags
& SIInstrFlags::MUBUF
;
447 bool isMUBUF(uint16_t Opcode
) const {
448 return get(Opcode
).TSFlags
& SIInstrFlags::MUBUF
;
451 static bool isMTBUF(const MachineInstr
&MI
) {
452 return MI
.getDesc().TSFlags
& SIInstrFlags::MTBUF
;
455 bool isMTBUF(uint16_t Opcode
) const {
456 return get(Opcode
).TSFlags
& SIInstrFlags::MTBUF
;
459 static bool isSMRD(const MachineInstr
&MI
) {
460 return MI
.getDesc().TSFlags
& SIInstrFlags::SMRD
;
463 bool isSMRD(uint16_t Opcode
) const {
464 return get(Opcode
).TSFlags
& SIInstrFlags::SMRD
;
467 bool isBufferSMRD(const MachineInstr
&MI
) const;
469 static bool isDS(const MachineInstr
&MI
) {
470 return MI
.getDesc().TSFlags
& SIInstrFlags::DS
;
473 bool isDS(uint16_t Opcode
) const {
474 return get(Opcode
).TSFlags
& SIInstrFlags::DS
;
477 bool isAlwaysGDS(uint16_t Opcode
) const;
479 static bool isMIMG(const MachineInstr
&MI
) {
480 return MI
.getDesc().TSFlags
& SIInstrFlags::MIMG
;
483 bool isMIMG(uint16_t Opcode
) const {
484 return get(Opcode
).TSFlags
& SIInstrFlags::MIMG
;
487 static bool isGather4(const MachineInstr
&MI
) {
488 return MI
.getDesc().TSFlags
& SIInstrFlags::Gather4
;
491 bool isGather4(uint16_t Opcode
) const {
492 return get(Opcode
).TSFlags
& SIInstrFlags::Gather4
;
495 static bool isFLAT(const MachineInstr
&MI
) {
496 return MI
.getDesc().TSFlags
& SIInstrFlags::FLAT
;
499 // Is a FLAT encoded instruction which accesses a specific segment,
500 // i.e. global_* or scratch_*.
501 static bool isSegmentSpecificFLAT(const MachineInstr
&MI
) {
502 auto Flags
= MI
.getDesc().TSFlags
;
503 return (Flags
& SIInstrFlags::FLAT
) && !(Flags
& SIInstrFlags::LGKM_CNT
);
506 // FIXME: Make this more precise
507 static bool isFLATScratch(const MachineInstr
&MI
) {
508 return isSegmentSpecificFLAT(MI
);
511 // Any FLAT encoded instruction, including global_* and scratch_*.
512 bool isFLAT(uint16_t Opcode
) const {
513 return get(Opcode
).TSFlags
& SIInstrFlags::FLAT
;
516 static bool isEXP(const MachineInstr
&MI
) {
517 return MI
.getDesc().TSFlags
& SIInstrFlags::EXP
;
520 bool isEXP(uint16_t Opcode
) const {
521 return get(Opcode
).TSFlags
& SIInstrFlags::EXP
;
524 static bool isWQM(const MachineInstr
&MI
) {
525 return MI
.getDesc().TSFlags
& SIInstrFlags::WQM
;
528 bool isWQM(uint16_t Opcode
) const {
529 return get(Opcode
).TSFlags
& SIInstrFlags::WQM
;
532 static bool isDisableWQM(const MachineInstr
&MI
) {
533 return MI
.getDesc().TSFlags
& SIInstrFlags::DisableWQM
;
536 bool isDisableWQM(uint16_t Opcode
) const {
537 return get(Opcode
).TSFlags
& SIInstrFlags::DisableWQM
;
540 static bool isVGPRSpill(const MachineInstr
&MI
) {
541 return MI
.getDesc().TSFlags
& SIInstrFlags::VGPRSpill
;
544 bool isVGPRSpill(uint16_t Opcode
) const {
545 return get(Opcode
).TSFlags
& SIInstrFlags::VGPRSpill
;
548 static bool isSGPRSpill(const MachineInstr
&MI
) {
549 return MI
.getDesc().TSFlags
& SIInstrFlags::SGPRSpill
;
552 bool isSGPRSpill(uint16_t Opcode
) const {
553 return get(Opcode
).TSFlags
& SIInstrFlags::SGPRSpill
;
556 static bool isDPP(const MachineInstr
&MI
) {
557 return MI
.getDesc().TSFlags
& SIInstrFlags::DPP
;
560 bool isDPP(uint16_t Opcode
) const {
561 return get(Opcode
).TSFlags
& SIInstrFlags::DPP
;
564 static bool isVOP3P(const MachineInstr
&MI
) {
565 return MI
.getDesc().TSFlags
& SIInstrFlags::VOP3P
;
568 bool isVOP3P(uint16_t Opcode
) const {
569 return get(Opcode
).TSFlags
& SIInstrFlags::VOP3P
;
572 static bool isVINTRP(const MachineInstr
&MI
) {
573 return MI
.getDesc().TSFlags
& SIInstrFlags::VINTRP
;
576 bool isVINTRP(uint16_t Opcode
) const {
577 return get(Opcode
).TSFlags
& SIInstrFlags::VINTRP
;
580 static bool isMAI(const MachineInstr
&MI
) {
581 return MI
.getDesc().TSFlags
& SIInstrFlags::IsMAI
;
584 bool isMAI(uint16_t Opcode
) const {
585 return get(Opcode
).TSFlags
& SIInstrFlags::IsMAI
;
588 static bool isDOT(const MachineInstr
&MI
) {
589 return MI
.getDesc().TSFlags
& SIInstrFlags::IsDOT
;
592 bool isDOT(uint16_t Opcode
) const {
593 return get(Opcode
).TSFlags
& SIInstrFlags::IsDOT
;
596 static bool isScalarUnit(const MachineInstr
&MI
) {
597 return MI
.getDesc().TSFlags
& (SIInstrFlags::SALU
| SIInstrFlags::SMRD
);
600 static bool usesVM_CNT(const MachineInstr
&MI
) {
601 return MI
.getDesc().TSFlags
& SIInstrFlags::VM_CNT
;
604 static bool usesLGKM_CNT(const MachineInstr
&MI
) {
605 return MI
.getDesc().TSFlags
& SIInstrFlags::LGKM_CNT
;
608 static bool sopkIsZext(const MachineInstr
&MI
) {
609 return MI
.getDesc().TSFlags
& SIInstrFlags::SOPK_ZEXT
;
612 bool sopkIsZext(uint16_t Opcode
) const {
613 return get(Opcode
).TSFlags
& SIInstrFlags::SOPK_ZEXT
;
616 /// \returns true if this is an s_store_dword* instruction. This is more
617 /// specific than than isSMEM && mayStore.
618 static bool isScalarStore(const MachineInstr
&MI
) {
619 return MI
.getDesc().TSFlags
& SIInstrFlags::SCALAR_STORE
;
622 bool isScalarStore(uint16_t Opcode
) const {
623 return get(Opcode
).TSFlags
& SIInstrFlags::SCALAR_STORE
;
626 static bool isFixedSize(const MachineInstr
&MI
) {
627 return MI
.getDesc().TSFlags
& SIInstrFlags::FIXED_SIZE
;
630 bool isFixedSize(uint16_t Opcode
) const {
631 return get(Opcode
).TSFlags
& SIInstrFlags::FIXED_SIZE
;
634 static bool hasFPClamp(const MachineInstr
&MI
) {
635 return MI
.getDesc().TSFlags
& SIInstrFlags::FPClamp
;
638 bool hasFPClamp(uint16_t Opcode
) const {
639 return get(Opcode
).TSFlags
& SIInstrFlags::FPClamp
;
642 static bool hasIntClamp(const MachineInstr
&MI
) {
643 return MI
.getDesc().TSFlags
& SIInstrFlags::IntClamp
;
646 uint64_t getClampMask(const MachineInstr
&MI
) const {
647 const uint64_t ClampFlags
= SIInstrFlags::FPClamp
|
648 SIInstrFlags::IntClamp
|
649 SIInstrFlags::ClampLo
|
650 SIInstrFlags::ClampHi
;
651 return MI
.getDesc().TSFlags
& ClampFlags
;
654 static bool usesFPDPRounding(const MachineInstr
&MI
) {
655 return MI
.getDesc().TSFlags
& SIInstrFlags::FPDPRounding
;
658 bool usesFPDPRounding(uint16_t Opcode
) const {
659 return get(Opcode
).TSFlags
& SIInstrFlags::FPDPRounding
;
662 static bool isFPAtomic(const MachineInstr
&MI
) {
663 return MI
.getDesc().TSFlags
& SIInstrFlags::FPAtomic
;
666 bool isFPAtomic(uint16_t Opcode
) const {
667 return get(Opcode
).TSFlags
& SIInstrFlags::FPAtomic
;
670 bool isVGPRCopy(const MachineInstr
&MI
) const {
672 unsigned Dest
= MI
.getOperand(0).getReg();
673 const MachineFunction
&MF
= *MI
.getParent()->getParent();
674 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
675 return !RI
.isSGPRReg(MRI
, Dest
);
678 bool hasVGPRUses(const MachineInstr
&MI
) const {
679 const MachineFunction
&MF
= *MI
.getParent()->getParent();
680 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
681 return llvm::any_of(MI
.explicit_uses(),
682 [&MRI
, this](const MachineOperand
&MO
) {
683 return MO
.isReg() && RI
.isVGPR(MRI
, MO
.getReg());});
686 /// Whether we must prevent this instruction from executing with EXEC = 0.
687 bool hasUnwantedEffectsWhenEXECEmpty(const MachineInstr
&MI
) const;
689 /// Returns true if the instruction could potentially depend on the value of
690 /// exec. If false, exec dependencies may safely be ignored.
691 bool mayReadEXEC(const MachineRegisterInfo
&MRI
, const MachineInstr
&MI
) const;
693 bool isInlineConstant(const APInt
&Imm
) const;
695 bool isInlineConstant(const MachineOperand
&MO
, uint8_t OperandType
) const;
697 bool isInlineConstant(const MachineOperand
&MO
,
698 const MCOperandInfo
&OpInfo
) const {
699 return isInlineConstant(MO
, OpInfo
.OperandType
);
702 /// \p returns true if \p UseMO is substituted with \p DefMO in \p MI it would
703 /// be an inline immediate.
704 bool isInlineConstant(const MachineInstr
&MI
,
705 const MachineOperand
&UseMO
,
706 const MachineOperand
&DefMO
) const {
707 assert(UseMO
.getParent() == &MI
);
708 int OpIdx
= MI
.getOperandNo(&UseMO
);
709 if (!MI
.getDesc().OpInfo
|| OpIdx
>= MI
.getDesc().NumOperands
) {
713 return isInlineConstant(DefMO
, MI
.getDesc().OpInfo
[OpIdx
]);
716 /// \p returns true if the operand \p OpIdx in \p MI is a valid inline
718 bool isInlineConstant(const MachineInstr
&MI
, unsigned OpIdx
) const {
719 const MachineOperand
&MO
= MI
.getOperand(OpIdx
);
720 return isInlineConstant(MO
, MI
.getDesc().OpInfo
[OpIdx
].OperandType
);
723 bool isInlineConstant(const MachineInstr
&MI
, unsigned OpIdx
,
724 const MachineOperand
&MO
) const {
725 if (!MI
.getDesc().OpInfo
|| OpIdx
>= MI
.getDesc().NumOperands
)
729 unsigned Size
= getOpSize(MI
, OpIdx
);
730 assert(Size
== 8 || Size
== 4);
732 uint8_t OpType
= (Size
== 8) ?
733 AMDGPU::OPERAND_REG_IMM_INT64
: AMDGPU::OPERAND_REG_IMM_INT32
;
734 return isInlineConstant(MO
, OpType
);
737 return isInlineConstant(MO
, MI
.getDesc().OpInfo
[OpIdx
].OperandType
);
740 bool isInlineConstant(const MachineOperand
&MO
) const {
741 const MachineInstr
*Parent
= MO
.getParent();
742 return isInlineConstant(*Parent
, Parent
->getOperandNo(&MO
));
745 bool isLiteralConstant(const MachineOperand
&MO
,
746 const MCOperandInfo
&OpInfo
) const {
747 return MO
.isImm() && !isInlineConstant(MO
, OpInfo
.OperandType
);
750 bool isLiteralConstant(const MachineInstr
&MI
, int OpIdx
) const {
751 const MachineOperand
&MO
= MI
.getOperand(OpIdx
);
752 return MO
.isImm() && !isInlineConstant(MI
, OpIdx
);
755 // Returns true if this operand could potentially require a 32-bit literal
756 // operand, but not necessarily. A FrameIndex for example could resolve to an
757 // inline immediate value that will not require an additional 4-bytes; this
758 // assumes that it will.
759 bool isLiteralConstantLike(const MachineOperand
&MO
,
760 const MCOperandInfo
&OpInfo
) const;
762 bool isImmOperandLegal(const MachineInstr
&MI
, unsigned OpNo
,
763 const MachineOperand
&MO
) const;
765 /// Return true if this 64-bit VALU instruction has a 32-bit encoding.
766 /// This function will return false if you pass it a 32-bit instruction.
767 bool hasVALU32BitEncoding(unsigned Opcode
) const;
769 /// Returns true if this operand uses the constant bus.
770 bool usesConstantBus(const MachineRegisterInfo
&MRI
,
771 const MachineOperand
&MO
,
772 const MCOperandInfo
&OpInfo
) const;
774 /// Return true if this instruction has any modifiers.
775 /// e.g. src[012]_mod, omod, clamp.
776 bool hasModifiers(unsigned Opcode
) const;
778 bool hasModifiersSet(const MachineInstr
&MI
,
779 unsigned OpName
) const;
780 bool hasAnyModifiersSet(const MachineInstr
&MI
) const;
782 bool canShrink(const MachineInstr
&MI
,
783 const MachineRegisterInfo
&MRI
) const;
785 MachineInstr
*buildShrunkInst(MachineInstr
&MI
,
786 unsigned NewOpcode
) const;
788 bool verifyInstruction(const MachineInstr
&MI
,
789 StringRef
&ErrInfo
) const override
;
791 unsigned getVALUOp(const MachineInstr
&MI
) const;
793 /// Return the correct register class for \p OpNo. For target-specific
794 /// instructions, this will return the register class that has been defined
795 /// in tablegen. For generic instructions, like REG_SEQUENCE it will return
796 /// the register class of its machine operand.
797 /// to infer the correct register class base on the other operands.
798 const TargetRegisterClass
*getOpRegClass(const MachineInstr
&MI
,
799 unsigned OpNo
) const;
801 /// Return the size in bytes of the operand OpNo on the given
802 // instruction opcode.
803 unsigned getOpSize(uint16_t Opcode
, unsigned OpNo
) const {
804 const MCOperandInfo
&OpInfo
= get(Opcode
).OpInfo
[OpNo
];
806 if (OpInfo
.RegClass
== -1) {
807 // If this is an immediate operand, this must be a 32-bit literal.
808 assert(OpInfo
.OperandType
== MCOI::OPERAND_IMMEDIATE
);
812 return RI
.getRegSizeInBits(*RI
.getRegClass(OpInfo
.RegClass
)) / 8;
815 /// This form should usually be preferred since it handles operands
816 /// with unknown register classes.
817 unsigned getOpSize(const MachineInstr
&MI
, unsigned OpNo
) const {
818 const MachineOperand
&MO
= MI
.getOperand(OpNo
);
820 if (unsigned SubReg
= MO
.getSubReg()) {
821 assert(RI
.getRegSizeInBits(*RI
.getSubClassWithSubReg(
822 MI
.getParent()->getParent()->getRegInfo().
823 getRegClass(MO
.getReg()), SubReg
)) >= 32 &&
824 "Sub-dword subregs are not supported");
825 return RI
.getSubRegIndexLaneMask(SubReg
).getNumLanes() * 4;
828 return RI
.getRegSizeInBits(*getOpRegClass(MI
, OpNo
)) / 8;
831 /// Legalize the \p OpIndex operand of this instruction by inserting
832 /// a MOV. For example:
833 /// ADD_I32_e32 VGPR0, 15
836 /// ADD_I32_e32 VGPR0, VGPR1
838 /// If the operand being legalized is a register, then a COPY will be used
840 void legalizeOpWithMove(MachineInstr
&MI
, unsigned OpIdx
) const;
842 /// Check if \p MO is a legal operand if it was the \p OpIdx Operand
844 bool isOperandLegal(const MachineInstr
&MI
, unsigned OpIdx
,
845 const MachineOperand
*MO
= nullptr) const;
847 /// Check if \p MO would be a valid operand for the given operand
848 /// definition \p OpInfo. Note this does not attempt to validate constant bus
849 /// restrictions (e.g. literal constant usage).
850 bool isLegalVSrcOperand(const MachineRegisterInfo
&MRI
,
851 const MCOperandInfo
&OpInfo
,
852 const MachineOperand
&MO
) const;
854 /// Check if \p MO (a register operand) is a legal register for the
855 /// given operand description.
856 bool isLegalRegOperand(const MachineRegisterInfo
&MRI
,
857 const MCOperandInfo
&OpInfo
,
858 const MachineOperand
&MO
) const;
860 /// Legalize operands in \p MI by either commuting it or inserting a
862 void legalizeOperandsVOP2(MachineRegisterInfo
&MRI
, MachineInstr
&MI
) const;
864 /// Fix operands in \p MI to satisfy constant bus requirements.
865 void legalizeOperandsVOP3(MachineRegisterInfo
&MRI
, MachineInstr
&MI
) const;
867 /// Copy a value from a VGPR (\p SrcReg) to SGPR. This function can only
868 /// be used when it is know that the value in SrcReg is same across all
869 /// threads in the wave.
870 /// \returns The SGPR register that \p SrcReg was copied to.
871 unsigned readlaneVGPRToSGPR(unsigned SrcReg
, MachineInstr
&UseMI
,
872 MachineRegisterInfo
&MRI
) const;
874 void legalizeOperandsSMRD(MachineRegisterInfo
&MRI
, MachineInstr
&MI
) const;
876 void legalizeGenericOperand(MachineBasicBlock
&InsertMBB
,
877 MachineBasicBlock::iterator I
,
878 const TargetRegisterClass
*DstRC
,
879 MachineOperand
&Op
, MachineRegisterInfo
&MRI
,
880 const DebugLoc
&DL
) const;
882 /// Legalize all operands in this instruction. This function may create new
883 /// instructions and control-flow around \p MI. If present, \p MDT is
885 void legalizeOperands(MachineInstr
&MI
,
886 MachineDominatorTree
*MDT
= nullptr) const;
888 /// Replace this instruction's opcode with the equivalent VALU
889 /// opcode. This function will also move the users of \p MI to the
890 /// VALU if necessary. If present, \p MDT is updated.
891 void moveToVALU(MachineInstr
&MI
, MachineDominatorTree
*MDT
= nullptr) const;
893 void insertWaitStates(MachineBasicBlock
&MBB
,MachineBasicBlock::iterator MI
,
896 void insertNoop(MachineBasicBlock
&MBB
,
897 MachineBasicBlock::iterator MI
) const override
;
899 void insertReturn(MachineBasicBlock
&MBB
) const;
900 /// Return the number of wait states that result from executing this
902 static unsigned getNumWaitStates(const MachineInstr
&MI
);
904 /// Returns the operand named \p Op. If \p MI does not have an
905 /// operand named \c Op, this function returns nullptr.
907 MachineOperand
*getNamedOperand(MachineInstr
&MI
, unsigned OperandName
) const;
910 const MachineOperand
*getNamedOperand(const MachineInstr
&MI
,
911 unsigned OpName
) const {
912 return getNamedOperand(const_cast<MachineInstr
&>(MI
), OpName
);
915 /// Get required immediate operand
916 int64_t getNamedImmOperand(const MachineInstr
&MI
, unsigned OpName
) const {
917 int Idx
= AMDGPU::getNamedOperandIdx(MI
.getOpcode(), OpName
);
918 return MI
.getOperand(Idx
).getImm();
921 uint64_t getDefaultRsrcDataFormat() const;
922 uint64_t getScratchRsrcWords23() const;
924 bool isLowLatencyInstruction(const MachineInstr
&MI
) const;
925 bool isHighLatencyInstruction(const MachineInstr
&MI
) const;
927 /// Return the descriptor of the target-specific machine instruction
928 /// that corresponds to the specified pseudo or native opcode.
929 const MCInstrDesc
&getMCOpcodeFromPseudo(unsigned Opcode
) const {
930 return get(pseudoToMCOpcode(Opcode
));
933 unsigned isStackAccess(const MachineInstr
&MI
, int &FrameIndex
) const;
934 unsigned isSGPRStackAccess(const MachineInstr
&MI
, int &FrameIndex
) const;
936 unsigned isLoadFromStackSlot(const MachineInstr
&MI
,
937 int &FrameIndex
) const override
;
938 unsigned isStoreToStackSlot(const MachineInstr
&MI
,
939 int &FrameIndex
) const override
;
941 unsigned getInstBundleSize(const MachineInstr
&MI
) const;
942 unsigned getInstSizeInBytes(const MachineInstr
&MI
) const override
;
944 bool mayAccessFlatAddressSpace(const MachineInstr
&MI
) const;
946 bool isNonUniformBranchInstr(MachineInstr
&Instr
) const;
948 void convertNonUniformIfRegion(MachineBasicBlock
*IfEntry
,
949 MachineBasicBlock
*IfEnd
) const;
951 void convertNonUniformLoopRegion(MachineBasicBlock
*LoopEntry
,
952 MachineBasicBlock
*LoopEnd
) const;
954 std::pair
<unsigned, unsigned>
955 decomposeMachineOperandsTargetFlags(unsigned TF
) const override
;
957 ArrayRef
<std::pair
<int, const char *>>
958 getSerializableTargetIndices() const override
;
960 ArrayRef
<std::pair
<unsigned, const char *>>
961 getSerializableDirectMachineOperandTargetFlags() const override
;
963 ScheduleHazardRecognizer
*
964 CreateTargetPostRAHazardRecognizer(const InstrItineraryData
*II
,
965 const ScheduleDAG
*DAG
) const override
;
967 ScheduleHazardRecognizer
*
968 CreateTargetPostRAHazardRecognizer(const MachineFunction
&MF
) const override
;
970 bool isBasicBlockPrologue(const MachineInstr
&MI
) const override
;
972 MachineInstr
*createPHIDestinationCopy(MachineBasicBlock
&MBB
,
973 MachineBasicBlock::iterator InsPt
,
974 const DebugLoc
&DL
, Register Src
,
975 Register Dst
) const override
;
977 MachineInstr
*createPHISourceCopy(MachineBasicBlock
&MBB
,
978 MachineBasicBlock::iterator InsPt
,
979 const DebugLoc
&DL
, Register Src
,
981 Register Dst
) const override
;
983 bool isWave32() const;
985 /// Return a partially built integer add instruction without carry.
986 /// Caller must add source operands.
987 /// For pre-GFX9 it will generate unused carry destination operand.
988 /// TODO: After GFX9 it should return a no-carry operation.
989 MachineInstrBuilder
getAddNoCarry(MachineBasicBlock
&MBB
,
990 MachineBasicBlock::iterator I
,
992 unsigned DestReg
) const;
994 MachineInstrBuilder
getAddNoCarry(MachineBasicBlock
&MBB
,
995 MachineBasicBlock::iterator I
,
998 RegScavenger
&RS
) const;
1000 static bool isKillTerminator(unsigned Opcode
);
1001 const MCInstrDesc
&getKillTerminatorFromPseudo(unsigned Opcode
) const;
1003 static bool isLegalMUBUFImmOffset(unsigned Imm
) {
1004 return isUInt
<12>(Imm
);
1007 unsigned getNumFlatOffsetBits(unsigned AddrSpace
, bool Signed
) const;
1009 /// Returns if \p Offset is legal for the subtarget as the offset to a FLAT
1010 /// encoded instruction. If \p Signed, this is for an instruction that
1011 /// interprets the offset as signed.
1012 bool isLegalFLATOffset(int64_t Offset
, unsigned AddrSpace
,
1015 /// \brief Return a target-specific opcode if Opcode is a pseudo instruction.
1016 /// Return -1 if the target-specific opcode for the pseudo instruction does
1017 /// not exist. If Opcode is not a pseudo instruction, this is identity.
1018 int pseudoToMCOpcode(int Opcode
) const;
1020 const TargetRegisterClass
*getRegClass(const MCInstrDesc
&TID
, unsigned OpNum
,
1021 const TargetRegisterInfo
*TRI
,
1022 const MachineFunction
&MF
)
1024 if (OpNum
>= TID
.getNumOperands())
1026 return RI
.getRegClass(TID
.OpInfo
[OpNum
].RegClass
);
1029 void fixImplicitOperands(MachineInstr
&MI
) const;
1032 /// \brief Returns true if a reg:subreg pair P has a TRC class
1033 inline bool isOfRegClass(const TargetInstrInfo::RegSubRegPair
&P
,
1034 const TargetRegisterClass
&TRC
,
1035 MachineRegisterInfo
&MRI
) {
1036 auto *RC
= MRI
.getRegClass(P
.Reg
);
1039 auto *TRI
= MRI
.getTargetRegisterInfo();
1040 return RC
== TRI
->getMatchingSuperRegClass(RC
, &TRC
, P
.SubReg
);
1043 /// \brief Create RegSubRegPair from a register MachineOperand
1045 TargetInstrInfo::RegSubRegPair
getRegSubRegPair(const MachineOperand
&O
) {
1047 return TargetInstrInfo::RegSubRegPair(O
.getReg(), O
.getSubReg());
1050 /// \brief Return the SubReg component from REG_SEQUENCE
1051 TargetInstrInfo::RegSubRegPair
getRegSequenceSubReg(MachineInstr
&MI
,
1054 /// \brief Return the defining instruction for a given reg:subreg pair
1055 /// skipping copy like instructions and subreg-manipulation pseudos.
1056 /// Following another subreg of a reg:subreg isn't supported.
1057 MachineInstr
*getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair
&P
,
1058 MachineRegisterInfo
&MRI
);
1060 /// \brief Return false if EXEC is not changed between the def of \p VReg at \p
1061 /// DefMI and the use at \p UseMI. Should be run on SSA. Currently does not
1062 /// attempt to track between blocks.
1063 bool execMayBeModifiedBeforeUse(const MachineRegisterInfo
&MRI
,
1065 const MachineInstr
&DefMI
,
1066 const MachineInstr
&UseMI
);
1068 /// \brief Return false if EXEC is not changed between the def of \p VReg at \p
1069 /// DefMI and all its uses. Should be run on SSA. Currently does not attempt to
1070 /// track between blocks.
1071 bool execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo
&MRI
,
1073 const MachineInstr
&DefMI
);
1078 int getVOPe64(uint16_t Opcode
);
1081 int getVOPe32(uint16_t Opcode
);
1084 int getSDWAOp(uint16_t Opcode
);
1087 int getDPPOp32(uint16_t Opcode
);
1090 int getBasicFromSDWAOp(uint16_t Opcode
);
1093 int getCommuteRev(uint16_t Opcode
);
1096 int getCommuteOrig(uint16_t Opcode
);
1099 int getAddr64Inst(uint16_t Opcode
);
1101 /// Check if \p Opcode is an Addr64 opcode.
1103 /// \returns \p Opcode if it is an Addr64 opcode, otherwise -1.
1105 int getIfAddr64Inst(uint16_t Opcode
);
1108 int getMUBUFNoLdsInst(uint16_t Opcode
);
1111 int getAtomicRetOp(uint16_t Opcode
);
1114 int getAtomicNoRetOp(uint16_t Opcode
);
1117 int getSOPKOp(uint16_t Opcode
);
1120 int getGlobalSaddrOp(uint16_t Opcode
);
1123 int getVCMPXNoSDstOp(uint16_t Opcode
);
1125 const uint64_t RSRC_DATA_FORMAT
= 0xf00000000000LL
;
1126 const uint64_t RSRC_ELEMENT_SIZE_SHIFT
= (32 + 19);
1127 const uint64_t RSRC_INDEX_STRIDE_SHIFT
= (32 + 21);
1128 const uint64_t RSRC_TID_ENABLE
= UINT64_C(1) << (32 + 23);
1130 } // end namespace AMDGPU
1133 namespace KernelInputOffsets
{
1135 /// Offsets in bytes from the start of the input buffer
1148 } // end namespace KernelInputOffsets
1149 } // end namespace SI
1151 } // end namespace llvm
1153 #endif // LLVM_LIB_TARGET_AMDGPU_SIINSTRINFO_H