26 #define DEBUG_TYPE "si-fold-operands" 31 struct FoldCandidate {
38 unsigned char UseOpNo;
43 bool Commuted_ =
false) :
46 if (FoldOp->
isImm()) {
47 ImmToFold = FoldOp->
getImm();
48 }
else if (FoldOp->
isFI()) {
49 FrameIndexToFold = FoldOp->
getIndex();
68 bool isCommuted()
const {
92 std::pair<const MachineOperand *, int> isOMod(
const MachineInstr &MI)
const;
102 StringRef getPassName()
const override {
return "SI Fold Operands"; }
113 "SI Fold Operands",
false,
false)
130 case AMDGPU::V_MAC_F32_e64:
131 case AMDGPU::V_MAC_F16_e64:
132 case AMDGPU::V_FMAC_F32_e64: {
136 if (static_cast<int>(OpNo) == Src2Idx) {
137 bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e64;
138 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
140 unsigned Opc = IsFMA ?
141 AMDGPU::V_FMA_F32 : (IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
153 return new SIFoldOperands();
170 ModIdx = AMDGPU::OpName::src0_modifiers;
172 ModIdx = AMDGPU::OpName::src1_modifiers;
174 ModIdx = AMDGPU::OpName::src2_modifiers;
178 unsigned Val = Mod.
getImm();
183 if (!(Fold.ImmToFold & 0xffff)) {
217 for (
auto Candidate : FoldList) {
218 if (Candidate.UseMI == MI)
232 if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
233 Opc == AMDGPU::V_FMAC_F32_e64) &&
235 bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e64;
236 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
237 unsigned NewOpc = IsFMA ?
238 AMDGPU::V_FMA_F32 : (IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
252 if (Opc == AMDGPU::S_SETREG_B32 && OpToFold->
isImm()) {
253 MI->
setDesc(TII->get(AMDGPU::S_SETREG_IMM32_B32));
254 FoldList.
push_back(FoldCandidate(MI, OpNo, OpToFold));
271 if (CommuteIdx0 == OpNo)
273 else if (CommuteIdx1 == OpNo)
286 !TII->commuteInstruction(*MI,
false, CommuteIdx0, CommuteIdx1))
290 TII->commuteInstruction(*MI,
false, CommuteIdx0, CommuteIdx1);
294 FoldList.
push_back(FoldCandidate(MI, OpNo, OpToFold,
true));
298 FoldList.
push_back(FoldCandidate(MI, OpNo, OpToFold));
311 void SIFoldOperands::foldOperand(
348 RSUse =
MRI->use_begin(RegSeqDstReg), RSE =
MRI->use_end();
349 RSUse != RSE; ++RSUse) {
352 if (RSUse->getSubReg() != RegSeqDstSubReg)
355 foldOperand(OpToFold, RSUseMI, RSUse.
getOperandNo(), FoldList,
363 bool FoldingImm = OpToFold.
isImm();
367 if (FoldingImm && UseMI->
isCopy()) {
371 MRI->getRegClass(DestReg) :
372 TRI->getPhysRegClass(DestReg);
374 unsigned MovOp =
TII->getMovOpcode(DestRC);
375 if (MovOp == AMDGPU::COPY)
411 MRI->getRegClass(UseReg) :
412 TRI->getPhysRegClass(UseReg);
422 Imm = Imm.getHiBits(32);
438 case AMDGPU::V_AND_B32_e64:
439 case AMDGPU::V_AND_B32_e32:
440 case AMDGPU::S_AND_B32:
443 case AMDGPU::V_OR_B32_e64:
444 case AMDGPU::V_OR_B32_e32:
445 case AMDGPU::S_OR_B32:
448 case AMDGPU::V_XOR_B32_e64:
449 case AMDGPU::V_XOR_B32_e32:
450 case AMDGPU::S_XOR_B32:
453 case AMDGPU::V_LSHL_B32_e64:
454 case AMDGPU::V_LSHL_B32_e32:
455 case AMDGPU::S_LSHL_B32:
457 Result = LHS << (RHS & 31);
459 case AMDGPU::V_LSHLREV_B32_e64:
460 case AMDGPU::V_LSHLREV_B32_e32:
461 Result = RHS << (LHS & 31);
463 case AMDGPU::V_LSHR_B32_e64:
464 case AMDGPU::V_LSHR_B32_e32:
465 case AMDGPU::S_LSHR_B32:
466 Result = LHS >> (RHS & 31);
468 case AMDGPU::V_LSHRREV_B32_e64:
469 case AMDGPU::V_LSHRREV_B32_e32:
470 Result = RHS >> (LHS & 31);
472 case AMDGPU::V_ASHR_I32_e64:
473 case AMDGPU::V_ASHR_I32_e32:
474 case AMDGPU::S_ASHR_I32:
475 Result =
static_cast<int32_t
>(LHS) >> (RHS & 31);
477 case AMDGPU::V_ASHRREV_I32_e64:
478 case AMDGPU::V_ASHRREV_I32_e32:
479 Result =
static_cast<int32_t
>(RHS) >> (LHS & 31);
487 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
512 if (Op.
getSubReg() != AMDGPU::NoSubRegister ||
535 if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
536 Opc == AMDGPU::S_NOT_B32) {
550 if (!Src0->
isImm() && !Src1->isImm())
556 if (Src0->
isImm() && Src1->isImm()) {
575 if (Src0->
isImm() && !Src1->isImm()) {
580 int32_t Src1Val =
static_cast<int32_t
>(Src1->getImm());
581 if (Opc == AMDGPU::V_OR_B32_e64 ||
582 Opc == AMDGPU::V_OR_B32_e32 ||
583 Opc == AMDGPU::S_OR_B32) {
588 }
else if (Src1Val == -1) {
598 if (MI->
getOpcode() == AMDGPU::V_AND_B32_e64 ||
599 MI->
getOpcode() == AMDGPU::V_AND_B32_e32 ||
605 }
else if (Src1Val == -1) {
616 if (MI->
getOpcode() == AMDGPU::V_XOR_B32_e64 ||
617 MI->
getOpcode() == AMDGPU::V_XOR_B32_e32 ||
635 if (Opc == AMDGPU::V_CNDMASK_B32_e32 ||
636 Opc == AMDGPU::V_CNDMASK_B32_e64 ||
637 Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) {
665 bool FoldingImm = OpToFold.
isImm() || OpToFold.
isFI();
667 unsigned NumLiteralUses = 0;
669 int NonInlineUseOpNo = -1;
675 NextUse = std::next(
Use);
716 foldOperand(OpToFold, UseMI, OpNo, FoldList, CopiesToReplace);
718 if (++NumLiteralUses == 1) {
719 NonInlineUse = &*
Use;
720 NonInlineUseOpNo = OpNo;
725 if (NumLiteralUses == 1) {
727 foldOperand(OpToFold, UseMI, NonInlineUseOpNo, FoldList, CopiesToReplace);
737 FoldList, CopiesToReplace);
744 Copy->addImplicitDefUseOperands(*MF);
746 for (FoldCandidate &Fold : FoldList) {
750 assert(Fold.OpToFold && Fold.OpToFold->isReg());
754 MRI->clearKillFlags(Fold.OpToFold->getReg());
757 << static_cast<int>(Fold.UseOpNo) <<
" of " 758 << *Fold.UseMI <<
'\n');
760 }
else if (Fold.isCommuted()) {
762 TII->commuteInstruction(*Fold.UseMI,
false);
772 case AMDGPU::V_MAX_F32_e64:
773 case AMDGPU::V_MAX_F16_e64:
774 case AMDGPU::V_MAX_F64:
775 case AMDGPU::V_PK_MAX_F16: {
776 if (!
TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
785 Src0->
getSubReg() != AMDGPU::NoSubRegister)
789 if (
TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
793 =
TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm();
795 =
TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm();
800 if (Src0Mods != UnsetMods && Src1Mods != UnsetMods)
831 if (
TII->getClampMask(*Def) !=
TII->getClampMask(MI))
838 LLVM_DEBUG(
dbgs() <<
"Folding clamp " << *DefClamp <<
" into " << *Def
850 case AMDGPU::V_MUL_F32_e64: {
851 switch (static_cast<uint32_t>(Val)) {
862 case AMDGPU::V_MUL_F16_e64: {
863 switch (static_cast<uint16_t>(Val)) {
882 std::pair<const MachineOperand *, int>
886 case AMDGPU::V_MUL_F32_e64:
887 case AMDGPU::V_MUL_F16_e64: {
889 if ((Op == AMDGPU::V_MUL_F32_e64 &&
ST->hasFP32Denormals()) ||
890 (Op == AMDGPU::V_MUL_F16_e64 &&
ST->hasFP16Denormals()))
900 }
else if (Src1->
isImm()) {
908 TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
909 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
910 TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
911 TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
914 return std::make_pair(RegOp, OMod);
916 case AMDGPU::V_ADD_F32_e64:
917 case AMDGPU::V_ADD_F16_e64: {
919 if ((Op == AMDGPU::V_ADD_F32_e64 &&
ST->hasFP32Denormals()) ||
920 (Op == AMDGPU::V_ADD_F16_e64 &&
ST->hasFP16Denormals()))
929 !
TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
930 !
TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
931 !
TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
932 !
TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
946 std::tie(RegOp, OMod) = isOMod(MI);
948 RegOp->
getSubReg() != AMDGPU::NoSubRegister ||
959 if (
TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
962 LLVM_DEBUG(
dbgs() <<
"Folding omod " << MI <<
" into " << *Def <<
'\n');
976 TII =
ST->getInstrInfo();
990 for (I = MBB->begin(); I != MBB->end(); I = Next) {
996 if (!TII->isFoldableCopy(MI)) {
997 if (IsIEEEMode || !tryFoldOMod(MI))
1003 bool FoldingImm = OpToFold.
isImm() || OpToFold.
isFI();
1006 if (!FoldingImm && !OpToFold.
isReg())
1009 if (OpToFold.
isReg() &&
1024 foldInstOperand(MI, OpToFold);
static bool isReg(const MCInst &MI, unsigned OpNo)
unsigned getNumImplicitUses() const
Return the number of implicit uses this instruction has.
void push_back(const T &Elt)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AMDGPU specific subclass of TargetSubtarget.
unsigned getNumImplicitDefs() const
Return the number of implicit defs this instruct has.
Compute iterated dominance frontiers using a linear time algorithm.
static bool isUseMIInFoldList(ArrayRef< FoldCandidate > FoldList, const MachineInstr *MI)
bool isOperandLegal(const MachineInstr &MI, unsigned OpIdx, const MachineOperand *MO=nullptr) const
Check if MO is a legal operand if it was the OpIdx Operand for MI.
static bool isUseSafeToFold(const SIInstrInfo *TII, const MachineInstr &MI, const MachineOperand &UseMO)
Describe properties that are true of each instruction in the target description file.
unsigned getReg() const
getReg - Returns the register number.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
void setIsUndef(bool Val=true)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned getSubReg() const
unsigned getRegBitWidth(unsigned RCID)
Get the size in bits of a register from the register class RC.
bool isRegSequence() const
unsigned const TargetRegisterInfo * TRI
void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo &)
substVirtReg - Substitute the current register with the virtual subregister Reg:SubReg.
bool isInlineConstant(const APInt &Imm) const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const SIRegisterInfo & getRegisterInfo() const
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction...
static unsigned getMovOpc(bool IsScalar)
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const HexagonInstrInfo * TII
bool isSGPRReg(const MachineRegisterInfo &MRI, unsigned Reg) const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
static bool hasOneNonDBGUseInst(const MachineRegisterInfo &MRI, unsigned Reg)
A Use represents the edge between a Value definition and its users.
static bool tryAddToFoldList(SmallVectorImpl< FoldCandidate > &FoldList, MachineInstr *MI, unsigned OpNo, MachineOperand *OpToFold, const SIInstrInfo *TII)
static bool updateOperand(FoldCandidate &Fold, const TargetRegisterInfo &TRI)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
static MachineOperand * getImmOrMaterializedImm(MachineRegisterInfo &MRI, MachineOperand &Op)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getID() const
Return the register class ID number.
static int getOModValue(unsigned Opc, int64_t Val)
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
uint8_t OperandType
Information about the type of the operand.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
unsigned getOperandNo() const
Return the operand # of this use in its User.
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static const unsigned CommuteAnyOperandIndex
unsigned const MachineRegisterInfo * MRI
static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool isVariadic() const
Return true if this instruction can have a variable number of operands.
MachineInstrBuilder & UseMI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
bool hasNoSignedZerosFPMath() const
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
static bool tryFoldInst(const SIInstrInfo *TII, MachineInstr *MI)
static void stripExtraCopyOperands(MachineInstr &MI)
Remove any leftover implicit operands from mutating the instruction.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
The access may modify the value stored in memory.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result, uint32_t LHS, uint32_t RHS)
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void initializeSIFoldOperandsPass(PassRegistry &)
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
int16_t RegClass
This specifies the register class enumeration of the operand if the operand is a register.
FunctionPass * createSIFoldOperandsPass()
static unsigned UseReg(const MachineOperand &MO)
static MachineOperand CreateImm(int64_t Val)
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
static bool isInlineConstantIfFolded(const SIInstrInfo *TII, const MachineInstr &UseMI, unsigned OpNo, const MachineOperand &OpToFold)
Abstract Stack Frame Index.
constexpr bool isUInt< 16 >(uint64_t x)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
iterator_range< df_iterator< T > > depth_first(const T &G)
static use_instr_nodbg_iterator use_instr_nodbg_end()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const
static bool isSDWA(const MachineInstr &MI)
const MCOperandInfo * OpInfo
void ChangeToFrameIndex(int Idx)
Replace this operand with a frame index.
StringRef - Represent a constant reference to a string, i.e.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
static bool tryConstantFoldOp(MachineRegisterInfo &MRI, const SIInstrInfo *TII, MachineInstr *MI, MachineOperand *ImmOp)
const MachineOperand & getOperand(unsigned i) const
reg_begin/reg_end - Provide iteration support to walk over all definitions and uses of a register wit...
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bool isCommutable(QueryType Type=IgnoreBundle) const
Return true if this may be a 2- or 3-address instruction (of the form "X = op Y, Z, ..."), which produces the same result if Y and Z are exchanged.
bool findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const override
const SIRegisterInfo * getRegisterInfo() const override