100 using namespace llvm;
102 #define DEBUG_TYPE "si-fix-sgpr-copies" 105 "amdgpu-enable-merge-m0",
106 cl::desc(
"Merge and hoist M0 initializations"),
127 StringRef getPassName()
const override {
return "SI Fix SGPR copies"; }
140 "SI Fix SGPR copies",
false,
false)
145 char SIFixSGPRCopies::
ID = 0;
150 return new SIFixSGPRCopies();
167 static std::pair<const TargetRegisterClass *, const TargetRegisterClass *>
185 return std::make_pair(SrcRC, DstRC);
191 return SrcRC != &AMDGPU::VReg_1RegClass && TRI.
isSGPRClass(DstRC) &&
198 return DstRC != &AMDGPU::VReg_1RegClass && TRI.
isSGPRClass(SrcRC) &&
214 const auto *
UseMI = MO.getParent();
272 if (SubReg != AMDGPU::NoSubRegister)
294 "Expected SGPR REG_SEQUENCE to only have SGPR inputs");
308 unsigned Opc = NewSrcRC == &AMDGPU::AGPR_32RegClass ?
309 AMDGPU::V_ACCVGPR_WRITE_B32 : AMDGPU::COPY;
346 case AMDGPU::V_MOV_B32_e32:
347 SMovOp = AMDGPU::S_MOV_B32;
349 case AMDGPU::V_MOV_B64_PSEUDO:
350 SMovOp = AMDGPU::S_MOV_B64;
357 template <
class UnaryPredicate>
368 while (!Worklist.empty()) {
371 if (!Visited.
insert(MBB).second)
398 if (MBBFrom == MBBTo)
428 using InitListMap = std::map<unsigned, std::list<MachineInstr *>>;
435 bool Changed =
false;
439 for (
auto &MO :
MI.operands()) {
440 if ((MO.isReg() && ((MO.isDef() && MO.getReg() !=
Reg) || !MO.isDef())) ||
441 (!MO.isImm() && !MO.isReg()) || (MO.isImm() && Imm)) {
444 }
else if (MO.isImm())
448 Inits[Imm->
getImm()].push_front(&
MI);
453 for (
auto &
Init : Inits) {
454 auto &Defs =
Init.second;
456 for (
auto I1 = Defs.begin(),
E = Defs.end(); I1 !=
E; ) {
459 for (
auto I2 = std::next(I1); I2 !=
E; ) {
471 bool MayClobberFrom =
isReachable(Clobber, &*From, MBBTo, MDT);
472 bool MayClobberTo =
isReachable(Clobber, &*To, MBBTo, MDT);
473 if (!MayClobberFrom && !MayClobberTo)
475 if ((MayClobberFrom && !MayClobberTo) ||
476 (!MayClobberFrom && MayClobberTo))
482 return !((MBBFrom == MBBTo &&
490 return C.first !=
Init.first &&
496 if (!interferes(MI2, MI1)) {
506 if (!interferes(MI1, MI2)) {
524 if (!interferes(MI1, I) && !interferes(MI2, I)) {
528 <<
"and moving from " 531 I->getParent()->splice(I, MI2->
getParent(), MI2);
545 for (
auto &
Init : Inits) {
546 auto &Defs =
Init.second;
547 auto I = Defs.begin();
548 while (
I != Defs.end()) {
549 if (MergedInstrs.
count(*
I)) {
550 (*I)->eraseFromParent();
558 for (
auto &
Init : Inits) {
559 auto &Defs =
Init.second;
560 for (
auto MI : Defs) {
561 auto MBB =
MI->getParent();
569 auto R = std::next(
MI->getReverseIterator());
573 if (R->readsRegister(Reg, TRI) || R->definesRegister(Reg, TRI) ||
579 MBB->splice(*R, MBB,
MI);
594 MDT = &getAnalysis<MachineDominatorTree>();
610 case AMDGPU::SOFT_WQM:
622 if (DstReg == AMDGPU::M0 &&
TRI->hasVectorRegisters(SrcRC)) {
624 = MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
627 TII->get(AMDGPU::V_READFIRSTLANE_B32), TmpReg)
638 TII->moveToVALU(MI, MDT);
653 TII->moveToVALU(MI, MDT);
664 case AMDGPU::REG_SEQUENCE:
665 if (
TRI->hasVectorRegisters(
TII->getOpRegClass(MI, 0)) ||
673 TII->moveToVALU(MI, MDT);
675 case AMDGPU::INSERT_SUBREG: {
680 if (
TRI->isSGPRClass(DstRC) &&
681 (
TRI->hasVectorRegisters(Src0RC) ||
682 TRI->hasVectorRegisters(Src1RC))) {
684 TII->moveToVALU(MI, MDT);
688 case AMDGPU::V_WRITELANE_B32: {
708 if ((Src0.isReg() &&
TRI->isSGPRReg(*MRI, Src0.getReg()) &&
709 Src0.getReg() != AMDGPU::M0) &&
711 Src1.
getReg() != AMDGPU::M0)) {
717 bool Resolved =
false;
721 if (DefMI &&
TII->isFoldableCopy(*DefMI)) {
724 MO->getReg() == Def.
getReg() &&
727 if (Copied.
isImm() &&
742 TII->get(AMDGPU::COPY), AMDGPU::M0)
760 unsigned numVGPRUses = 0;
761 bool AllAGPRUses =
true;
766 while (!worklist.
empty()) {
769 for (
const auto &
Use :
MRI->use_operands(Reg)) {
771 AllAGPRUses &= (UseMI->
isCopy() &&
780 if (Visited.
insert(UseMI).second)
786 if (UseMI->
isPHI()) {
788 if (!
TRI->isSGPRReg(*
MRI,
Use.getReg()) &&
789 UseRC != &AMDGPU::VReg_1RegClass)
796 if (!
TRI->isSGPRClass(OpRC) && OpRC != &AMDGPU::VS_32RegClass &&
797 OpRC != &AMDGPU::VS_64RegClass) {
805 if (AllAGPRUses && numVGPRUses && !
TRI->hasAGPRs(RC0)) {
807 MRI->setRegClass(PHIRes,
TRI->getEquivalentAGPRClass(RC0));
810 bool hasVGPRInput =
false;
814 if (
TRI->isVectorRegister(*
MRI, InputReg)) {
818 TRI->getRegClassForReg(*
MRI, SrcReg);
819 if (
TRI->isSGPRClass(RC))
832 if ((!
TRI->isVectorRegister(*
MRI, PHIRes) &&
833 RC0 != &AMDGPU::VReg_1RegClass) &&
834 (hasVGPRInput || numVGPRUses > 1)) {
840 TII->legalizeOperands(MI, MDT);
Interface definition for SIRegisterInfo.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
AMDGPU specific subclass of TargetSubtarget.
static bool isSafeToFoldImmIntoCopy(const MachineInstr *Copy, const MachineInstr *MoveImm, const SIInstrInfo *TII, unsigned &SMovOp, int64_t &Imm)
LLVM_NODISCARD T pop_back_val()
This class represents lattice values for constants.
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.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
const TargetRegisterClass * getEquivalentVGPRClass(const TargetRegisterClass *SRC) const
Implements a dense probed hash-table based set.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(unsigned Reg) const
unsigned getSubReg() const
bool isRegSequence() const
const SIInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
const TargetRegisterClass * getEquivalentSGPRClass(const TargetRegisterClass *VRC) const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
static bool hoistAndMergeSGPRInits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo *TRI, MachineDominatorTree &MDT, const TargetInstrInfo *TII)
static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI, const SIRegisterInfo *TRI, const SIInstrInfo *TII, MachineRegisterInfo &MRI)
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
static bool tryChangeVGPRtoSGPRinCopy(MachineInstr &MI, const SIRegisterInfo *TRI, const SIInstrInfo *TII)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned getNumOperands() const
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
virtual bool isBasicBlockPrologue(const MachineInstr &MI) const
True if the instruction is bound to the top of its basic block and no other instructions shall be ins...
bool isSGPRClass(const TargetRegisterClass *RC) const
bool insert(const value_type &X)
Insert a new element into the SetVector.
void clearKillFlags(unsigned Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
INITIALIZE_PASS_BEGIN(SIFixSGPRCopies, DEBUG_TYPE, "SI Fix SGPR copies", false, false) INITIALIZE_PASS_END(SIFixSGPRCopies
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
const TargetRegisterClass * getEquivalentAGPRClass(const TargetRegisterClass *SRC) const
void setReg(Register Reg)
Change the register this operand corresponds to.
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
static bool isSGPRToVGPRCopy(const TargetRegisterClass *SrcRC, const TargetRegisterClass *DstRC, const SIRegisterInfo &TRI)
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned const MachineRegisterInfo * MRI
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.
MachineInstrBuilder & UseMI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< iterator, bool > insert(const ValueT &V)
Represent the analysis usage information of a pass.
use_instr_iterator use_instr_begin(unsigned RegNo) const
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
FunctionPass class - This class is used to implement most global optimizations.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
const TargetRegisterClass * getSubRegClass(const TargetRegisterClass *RC, unsigned SubIdx) const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
pred_iterator pred_begin()
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
virtual bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const
Test if the given instruction should be considered a scheduling boundary.
Iterator for intrusive lists based on ilist_node.
static std::pair< const TargetRegisterClass *, const TargetRegisterClass * > getCopyRegClasses(const MachineInstr &Copy, const SIRegisterInfo &TRI, const MachineRegisterInfo &MRI)
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
BlockVerifier::State From
bool searchPredecessors(const MachineBasicBlock *MBB, const MachineBasicBlock *CutOff, UnaryPredicate Predicate)
bool hasAGPRs(const TargetRegisterClass *RC) const
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
MachineInstrBuilder MachineInstrBuilder & DefMI
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
bool properlyDominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
void setPreservesCFG()
This function should be called by the pass, iff they do not:
static cl::opt< bool > EnableM0Merge("amdgpu-enable-merge-m0", cl::desc("Merge and hoist M0 initializations"), cl::init(true))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Class for arbitrary precision integers.
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
void addImplicitDefUseOperands(MachineFunction &MF)
Add all implicit def and use operands to this instruction.
FunctionPass * createSIFixSGPRCopiesPass()
static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC, const TargetRegisterClass *DstRC, const SIRegisterInfo &TRI)
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.
Interface definition for SIInstrInfo.
bool hasOneUse(unsigned RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool empty() const
Determine if the SetVector is empty or not.
void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static MachineBasicBlock::iterator getFirstNonPrologue(MachineBasicBlock *MBB, const TargetInstrInfo *TII)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasVectorRegisters(const TargetRegisterClass *RC) const
A vector that has set insertion semantics.
const TargetRegisterClass * getPhysRegClass(unsigned Reg) const
Return the 'base' register class for this register.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
iterator_range< def_instr_iterator > def_instructions(unsigned Reg) const
void setRegClass(unsigned Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
StringRef - Represent a constant reference to a string, i.e.
unsigned getConstantBusLimit(unsigned Opcode) const
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
static bool isReachable(const MachineInstr *From, const MachineInstr *To, const MachineBasicBlock *CutOff, MachineDominatorTree &MDT)
static bool hasVectorOperands(const MachineInstr &MI, const SIRegisterInfo *TRI)
Wrapper class representing virtual and physical registers.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
const SIRegisterInfo * getRegisterInfo() const override