82 #define DEBUG_TYPE "si-fix-sgpr-copies"
85 "amdgpu-enable-merge-m0",
86 cl::desc(
"Merge and hoist M0 initializations"),
102 StringRef getPassName()
const override {
return "SI Fix SGPR copies"; }
115 "SI Fix SGPR copies",
false,
false)
121 char SIFixSGPRCopies::
ID = 0;
126 return new SIFixSGPRCopies();
142 static std::pair<const TargetRegisterClass *, const TargetRegisterClass *>
162 return std::make_pair(SrcRC, DstRC);
183 unsigned SrcReg = Src.getReg();
189 const auto *
UseMI = MO.getParent();
247 if (SubReg != AMDGPU::NoSubRegister)
268 "Expected SGPR REG_SEQUENCE to only have SGPR inputs");
304 if (Visited.
count(Reg))
313 case AMDGPU::SI_BREAK:
314 case AMDGPU::SI_IF_BREAK:
315 case AMDGPU::SI_ELSE_BREAK:
329 if (
I->modifiesRegister(AMDGPU::EXEC, &TRI))
356 case AMDGPU::V_MOV_B32_e32:
357 SMovOp = AMDGPU::S_MOV_B32;
359 case AMDGPU::V_MOV_B64_PSEUDO:
360 SMovOp = AMDGPU::S_MOV_B64;
367 template <
class UnaryPredicate>
379 while (!Worklist.empty()) {
382 if (!Visited.
insert(MBB).second)
415 if (MBBFrom == MBBTo)
433 typedef std::map<unsigned, std::list<MachineInstr*>> InitListMap;
437 bool Changed =
false;
441 for (
auto &MO:
MI.operands()) {
442 if ((MO.isReg() && ((MO.isDef() && MO.getReg() !=
Reg) || !MO.isDef())) ||
443 (!MO.isImm() && !MO.isReg()) || (MO.isImm() && Imm)) {
446 }
else if (MO.isImm())
450 Inits[Imm->
getImm()].push_front(&
MI);
455 for (
auto &
Init : Inits) {
456 auto &Defs =
Init.second;
458 for (
auto I1 = Defs.begin(),
E = Defs.end(); I1 !=
E; ) {
461 for (
auto I2 = std::next(I1); I2 !=
E; ) {
470 auto interferes = [&MDT, From, To](
MachineInstr* &Clobber) ->
bool {
473 bool MayClobberFrom =
isReachable(Clobber, &*From, MBBTo, MDT);
474 bool MayClobberTo =
isReachable(Clobber, &*To, MBBTo, MDT);
475 if (!MayClobberFrom && !MayClobberTo)
477 if ((MayClobberFrom && !MayClobberTo) ||
478 (!MayClobberFrom && MayClobberTo))
484 return !((MBBFrom == MBBTo &&
490 return (
any_of(Clobbers, interferes)) ||
491 (
any_of(Inits, [&](InitListMap::value_type &
C) {
492 return C.first !=
Init.first &&
any_of(C.second, interferes);
497 if (!intereferes(MI2, MI1)) {
506 if (!intereferes(MI1, MI2)) {
523 if (!intereferes(MI1, I) && !intereferes(MI2, I)) {
525 <<
" " << *MI1 <<
"and moving from BB#"
527 << I->getParent()->getNumber() <<
" " << *MI2);
528 I->getParent()->splice(I, MI2->
getParent(), MI2);
552 MDT = &getAnalysis<MachineDominatorTree>();
613 DEBUG(
dbgs() <<
"Not fixing PHI for uniform branch: " << MI <<
'\n');
659 case AMDGPU::REG_SEQUENCE: {
666 DEBUG(
dbgs() <<
"Fixing REG_SEQUENCE: " << MI);
671 case AMDGPU::INSERT_SUBREG: {
678 DEBUG(
dbgs() <<
" Fixing INSERT_SUBREG: " << MI);
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static cl::opt< bool > EnableM0Merge("amdgpu-enable-merge-m0", cl::desc("Merge and hoist M0 initializations"), cl::init(false))
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)
bool properlyDominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
MachineBasicBlock * getMBB() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Implements a dense probed hash-table based set.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
const SIInstrInfo * getInstrInfo() const override
void moveToVALU(MachineInstr &MI) const
Replace this instruction's opcode with the equivalent VALU opcode.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
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.
bool isSGPRClass(const TargetRegisterClass *RC) const
static bool hasVGPROperands(const MachineInstr &MI, const SIRegisterInfo *TRI)
const TargetRegisterClass * getSubRegClass(const TargetRegisterClass *RC, unsigned SubIdx) const
static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI, const SIRegisterInfo *TRI, const SIInstrInfo *TII, MachineRegisterInfo &MRI)
static bool tryChangeVGPRtoSGPRinCopy(MachineInstr &MI, const SIRegisterInfo *TRI, const SIInstrInfo *TII)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
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.
use_instr_iterator use_instr_begin(unsigned RegNo) const
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.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
static bool hoistAndMergeSGPRInits(unsigned Reg, const MachineRegisterInfo &MRI, MachineDominatorTree &MDT)
unsigned getNumOperands() const
Access to explicit operands of the instruction.
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
static bool isSGPRToVGPRCopy(const TargetRegisterClass *SrcRC, const TargetRegisterClass *DstRC, const SIRegisterInfo &TRI)
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
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
INITIALIZE_PASS_BEGIN(SIFixSGPRCopies, DEBUG_TYPE,"SI Fix SGPR copies", false, false) INITIALIZE_PASS_END(SIFixSGPRCopies
initializer< Ty > init(const Ty &Val)
const TargetRegisterClass * getEquivalentVGPRClass(const TargetRegisterClass *SRC) const
unsigned const MachineRegisterInfo * MRI
bool hasVGPRs(const TargetRegisterClass *RC) const
MachineInstrBuilder & UseMI
const MachineOperand & getOperand(unsigned i) const
const TargetRegisterClass * getEquivalentSGPRClass(const TargetRegisterClass *VRC) const
std::pair< iterator, bool > insert(const ValueT &V)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
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.
unsigned getSubReg() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
pred_iterator pred_begin()
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
static bool phiHasBreakDef(const MachineInstr &PHI, const MachineRegisterInfo &MRI, SmallSet< unsigned, 8 > &Visited)
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...
bool searchPredecessors(const MachineBasicBlock *MBB, const MachineBasicBlock *CutOff, UnaryPredicate Predicate)
const SIRegisterInfo * getRegisterInfo() const override
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...
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
bool hasOneUse(unsigned RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
static bool hasTerminatorThatModifiesExec(const MachineBasicBlock &MBB, const TargetRegisterInfo &TRI)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void addImplicitDefUseOperands(MachineFunction &MF)
Add all implicit def and use operands to this instruction.
const TargetRegisterClass * getPhysRegClass(unsigned Reg) const
Return the 'base' register class for this register.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
FunctionPass * createSIFixSGPRCopiesPass()
static bool phiHasVGPROperands(const MachineInstr &PHI, const MachineRegisterInfo &MRI, const SIRegisterInfo *TRI, const SIInstrInfo *TII)
static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC, const TargetRegisterClass *DstRC, const SIRegisterInfo &TRI)
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.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
void clearKillFlags(unsigned Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
static bool predsHasDivergentTerminator(MachineBasicBlock *MBB, const TargetRegisterInfo *TRI)
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
unsigned getReg() const
getReg - Returns the register number.
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(unsigned Reg) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator_range< def_instr_iterator > def_instructions(unsigned Reg) const
const TargetRegisterClass * getOpRegClass(const MachineInstr &MI, unsigned OpNo) const
Return the correct register class for OpNo.
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.
bool isRegSequence() const
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction...
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) 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)