25 #define DEBUG_TYPE "si-fold-operands"
30 struct FoldCandidate {
37 unsigned char UseOpNo;
42 bool Commuted_ =
false) :
45 if (FoldOp->
isImm()) {
46 ImmToFold = FoldOp->
getImm();
47 }
else if (FoldOp->
isFI()) {
48 FrameIndexToFold = FoldOp->
getIndex();
67 bool isCommuted()
const {
91 std::pair<const MachineOperand *, int> isOMod(
const MachineInstr &MI)
const;
101 StringRef getPassName()
const override {
return "SI Fold Operands"; }
112 "SI Fold Operands",
false,
false)
114 char SIFoldOperands::
ID = 0;
124 if (TII->isInlineConstant(UseMI, OpNo, OpToFold))
127 unsigned Opc = UseMI.getOpcode();
129 case AMDGPU::V_MAC_F32_e64:
130 case AMDGPU::V_MAC_F16_e64: {
134 if (static_cast<int>(OpNo) == Src2Idx) {
135 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
137 = TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
148 return new SIFoldOperands();
183 for (
auto Candidate : FoldList) {
184 if (Candidate.UseMI == MI)
198 if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64) &&
200 bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
204 MI->
setDesc(TII->get(IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16));
214 if (Opc == AMDGPU::S_SETREG_B32 && OpToFold->
isImm()) {
215 MI->
setDesc(TII->get(AMDGPU::S_SETREG_IMM32_B32));
216 FoldList.
push_back(FoldCandidate(MI, OpNo, OpToFold));
233 if (CommuteIdx0 == OpNo)
235 else if (CommuteIdx1 == OpNo)
248 !TII->commuteInstruction(*MI,
false, CommuteIdx0, CommuteIdx1))
252 TII->commuteInstruction(*MI,
false, CommuteIdx0, CommuteIdx1);
256 FoldList.
push_back(FoldCandidate(MI, OpNo, OpToFold,
true));
260 FoldList.
push_back(FoldCandidate(MI, OpNo, OpToFold));
273 void SIFoldOperands::foldOperand(
310 RSUse =
MRI->use_begin(RegSeqDstReg), RSE =
MRI->use_end();
311 RSUse != RSE; ++RSUse) {
314 if (RSUse->getSubReg() != RegSeqDstSubReg)
317 foldOperand(OpToFold, RSUseMI, RSUse.
getOperandNo(), FoldList,
325 bool FoldingImm = OpToFold.
isImm();
329 if (FoldingImm && UseMI->
isCopy()) {
333 MRI->getRegClass(DestReg) :
334 TRI->getPhysRegClass(DestReg);
336 unsigned MovOp =
TII->getMovOpcode(DestRC);
337 if (MovOp == AMDGPU::COPY)
372 MRI->getRegClass(UseReg) :
373 TRI->getPhysRegClass(UseReg);
383 Imm = Imm.getHiBits(32);
399 case AMDGPU::V_AND_B32_e64:
400 case AMDGPU::V_AND_B32_e32:
401 case AMDGPU::S_AND_B32:
404 case AMDGPU::V_OR_B32_e64:
405 case AMDGPU::V_OR_B32_e32:
406 case AMDGPU::S_OR_B32:
409 case AMDGPU::V_XOR_B32_e64:
410 case AMDGPU::V_XOR_B32_e32:
411 case AMDGPU::S_XOR_B32:
414 case AMDGPU::V_LSHL_B32_e64:
415 case AMDGPU::V_LSHL_B32_e32:
416 case AMDGPU::S_LSHL_B32:
418 Result = LHS << (RHS & 31);
420 case AMDGPU::V_LSHLREV_B32_e64:
421 case AMDGPU::V_LSHLREV_B32_e32:
422 Result = RHS << (LHS & 31);
424 case AMDGPU::V_LSHR_B32_e64:
425 case AMDGPU::V_LSHR_B32_e32:
426 case AMDGPU::S_LSHR_B32:
427 Result = LHS >> (RHS & 31);
429 case AMDGPU::V_LSHRREV_B32_e64:
430 case AMDGPU::V_LSHRREV_B32_e32:
431 Result = RHS >> (LHS & 31);
433 case AMDGPU::V_ASHR_I32_e64:
434 case AMDGPU::V_ASHR_I32_e32:
435 case AMDGPU::S_ASHR_I32:
436 Result =
static_cast<int32_t
>(LHS) >> (RHS & 31);
438 case AMDGPU::V_ASHRREV_I32_e64:
439 case AMDGPU::V_ASHRREV_I32_e32:
440 Result =
static_cast<int32_t
>(RHS) >> (LHS & 31);
448 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
473 if (Op.
getSubReg() != AMDGPU::NoSubRegister)
495 if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
496 Opc == AMDGPU::S_NOT_B32) {
510 if (!Src0->
isImm() && !Src1->isImm())
516 if (Src0->
isImm() && Src1->isImm()) {
535 if (Src0->
isImm() && !Src1->isImm()) {
540 int32_t Src1Val =
static_cast<int32_t
>(Src1->getImm());
541 if (Opc == AMDGPU::V_OR_B32_e64 ||
542 Opc == AMDGPU::V_OR_B32_e32 ||
543 Opc == AMDGPU::S_OR_B32) {
548 }
else if (Src1Val == -1) {
558 if (MI->
getOpcode() == AMDGPU::V_AND_B32_e64 ||
559 MI->
getOpcode() == AMDGPU::V_AND_B32_e32 ||
565 }
else if (Src1Val == -1) {
576 if (MI->
getOpcode() == AMDGPU::V_XOR_B32_e64 ||
577 MI->
getOpcode() == AMDGPU::V_XOR_B32_e32 ||
595 if (Opc == AMDGPU::V_CNDMASK_B32_e32 ||
596 Opc == AMDGPU::V_CNDMASK_B32_e64 ||
597 Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) {
601 DEBUG(
dbgs() <<
"Folded " << *MI <<
" into ");
625 bool FoldingImm = OpToFold.
isImm() || OpToFold.
isFI();
627 unsigned NumLiteralUses = 0;
629 int NonInlineUseOpNo = -1;
635 NextUse = std::next(
Use);
649 DEBUG(
dbgs() <<
"Constant folded " << *UseMI <<
'\n');
676 foldOperand(OpToFold, UseMI, OpNo, FoldList, CopiesToReplace);
678 if (++NumLiteralUses == 1) {
679 NonInlineUse = &*
Use;
680 NonInlineUseOpNo = OpNo;
685 if (NumLiteralUses == 1) {
687 foldOperand(OpToFold, UseMI, NonInlineUseOpNo, FoldList, CopiesToReplace);
697 FoldList, CopiesToReplace);
704 Copy->addImplicitDefUseOperands(*MF);
706 for (FoldCandidate &Fold : FoldList) {
710 assert(Fold.OpToFold && Fold.OpToFold->isReg());
714 MRI->clearKillFlags(Fold.OpToFold->getReg());
716 DEBUG(
dbgs() <<
"Folded source from " << MI <<
" into OpNo " <<
717 static_cast<int>(Fold.UseOpNo) <<
" of " << *Fold.UseMI <<
'\n');
719 }
else if (Fold.isCommuted()) {
721 TII->commuteInstruction(*Fold.UseMI,
false);
729 case AMDGPU::V_MAX_F32_e64:
730 case AMDGPU::V_MAX_F16_e64:
731 case AMDGPU::V_MAX_F64: {
732 if (!
TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
740 Src0->
getSubReg() != AMDGPU::NoSubRegister)
744 if (
TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
745 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
746 TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
774 if (!
TII->hasFPClamp(*Def))
780 DEBUG(
dbgs() <<
"Folding clamp " << *DefClamp <<
" into " << *Def <<
'\n');
791 case AMDGPU::V_MUL_F32_e64: {
792 switch (static_cast<uint32_t>(Val)) {
803 case AMDGPU::V_MUL_F16_e64: {
804 switch (static_cast<uint16_t>(Val)) {
823 std::pair<const MachineOperand *, int>
827 case AMDGPU::V_MUL_F32_e64:
828 case AMDGPU::V_MUL_F16_e64: {
830 if ((Op == AMDGPU::V_MUL_F32_e64 &&
ST->hasFP32Denormals()) ||
831 (Op == AMDGPU::V_MUL_F16_e64 &&
ST->hasFP16Denormals()))
841 }
else if (Src1->
isImm()) {
849 TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
850 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
851 TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
852 TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
855 return std::make_pair(RegOp, OMod);
857 case AMDGPU::V_ADD_F32_e64:
858 case AMDGPU::V_ADD_F16_e64: {
860 if ((Op == AMDGPU::V_ADD_F32_e64 &&
ST->hasFP32Denormals()) ||
861 (Op == AMDGPU::V_ADD_F16_e64 &&
ST->hasFP16Denormals()))
870 !
TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
871 !
TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
872 !
TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
873 !
TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
887 std::tie(RegOp, OMod) = isOMod(MI);
889 RegOp->
getSubReg() != AMDGPU::NoSubRegister ||
900 if (
TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
903 DEBUG(
dbgs() <<
"Folding omod " << MI <<
" into " << *Def <<
'\n');
917 TII =
ST->getInstrInfo();
931 for (I = MBB->begin(); I != MBB->end(); I = Next) {
937 if (!TII->isFoldableCopy(MI)) {
938 if (IsIEEEMode || !tryFoldOMod(MI))
944 bool FoldingImm = OpToFold.
isImm() || OpToFold.
isFI();
947 if (!FoldingImm && !OpToFold.
isReg())
950 if (OpToFold.
isReg() &&
965 foldInstOperand(MI, OpToFold);
static bool isReg(const MCInst &MI, unsigned OpNo)
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
unsigned getNumImplicitUses() const
Return the number of implicit uses this instruction has.
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.
static bool isUseMIInFoldList(ArrayRef< FoldCandidate > FoldList, const MachineInstr *MI)
static bool isUseSafeToFold(const SIInstrInfo *TII, const MachineInstr &MI, const MachineOperand &UseMO)
unsigned getNumImplicitDefs() const
Return the number of implicit defs this instruct has.
use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const
Describe properties that are true of each instruction in the target description file.
void setIsUndef(bool Val=true)
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
unsigned getRegBitWidth(unsigned RCID)
Get the size in bits of a register from the register class RC.
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.
unsigned getID() const
Return the register class ID number.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo &)
substVirtReg - Substitute the current register with the virtual subregister Reg:SubReg.
bool isVariadic() const
Return true if this instruction can have a variable number of operands.
static unsigned getMovOpc(bool IsScalar)
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
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...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
static MachineOperand * getImmOrMaterializedImm(MachineRegisterInfo &MRI, MachineOperand &Op)
Reg
All possible values of the reg field in the ModR/M byte.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
static int getOModValue(unsigned Opc, int64_t Val)
uint8_t OperandType
Information about the type of the operand.
void RemoveOperand(unsigned i)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
const SIRegisterInfo & getRegisterInfo() const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
const MachineBasicBlock * getParent() const
static const unsigned CommuteAnyOperandIndex
bool hasNoSignedZerosFPMath() const
unsigned const MachineRegisterInfo * MRI
static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc)
unsigned getOperandNo() const
Return the operand # of this use in its User.
MachineInstrBuilder & UseMI
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
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.
unsigned getSubReg() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#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...
const SIRegisterInfo * getRegisterInfo() const override
MachineOperand class - Representation of each machine instruction operand.
bool isSGPRReg(const MachineRegisterInfo &MRI, unsigned Reg) const
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.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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)
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
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)
static bool isInlineConstantIfFolded(const SIInstrInfo *TII, const MachineInstr &UseMI, unsigned OpNo, const MachineOperand &OpToFold)
Abstract Stack Frame Index.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
iterator_range< df_iterator< T > > depth_first(const T &G)
unsigned getReg() const
getReg - Returns the register number.
static use_instr_nodbg_iterator use_instr_nodbg_end()
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isSDWA(const MachineInstr &MI)
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const MCOperandInfo * OpInfo
void ChangeToFrameIndex(int Idx)
Replace this operand with a frame index.
StringRef - Represent a constant reference to a string, i.e.
static bool tryConstantFoldOp(MachineRegisterInfo &MRI, const SIInstrInfo *TII, MachineInstr *MI, MachineOperand *ImmOp)
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
bool isRegSequence() const
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction...
reg_begin/reg_end - Provide iteration support to walk over all definitions and uses of a register wit...
bool findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const override