40 #define DEBUG_TYPE "si-i1-copies" 54 bool IsWave32 =
false;
79 StringRef getPassName()
const override {
return "SI Lower i1 Copies"; }
89 void lowerCopiesFromI1();
91 void lowerCopiesToI1();
92 bool isConstantLaneMask(
unsigned Reg,
bool &Val)
const;
95 unsigned DstReg,
unsigned PrevReg,
unsigned CurReg);
99 bool isVreg1(
unsigned Reg)
const {
104 bool isLaneMaskReg(
unsigned Reg)
const {
133 class PhiIncomingAnalysis {
149 return ReachableMap.
find(&MBB)->second;
157 ReachableMap.
clear();
158 ReachableOrdered.
clear();
159 Predecessors.
clear();
167 if (MBB == &DefBlock) {
168 ReachableMap[&DefBlock] =
true;
177 bool Divergent =
false;
179 if (
MI.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO ||
180 MI.getOpcode() == AMDGPU::SI_IF ||
181 MI.getOpcode() == AMDGPU::SI_ELSE ||
182 MI.getOpcode() == AMDGPU::SI_LOOP) {
188 if (Divergent && PDT.
dominates(&DefBlock, MBB)) {
194 while (!Stack.
empty()) {
205 bool HaveReachablePred =
false;
207 if (ReachableMap.
count(Pred)) {
208 HaveReachablePred =
true;
213 if (!HaveReachablePred)
214 ReachableMap[MBB] =
true;
215 if (HaveReachablePred) {
217 if (
llvm::find(Predecessors, UnreachablePred) == Predecessors.
end())
277 unsigned FoundLoopLevel = ~0u;
285 : DT(DT), PDT(PDT) {}
289 CommonDominators.
clear();
292 VisitedPostDom =
nullptr;
293 FoundLoopLevel = ~0u;
309 while (PDNode->
getBlock() != PostDom) {
310 if (PDNode->
getBlock() == VisitedPostDom)
314 if (FoundLoopLevel == Level)
332 if (!inLoopLevel(*Dom, LoopLevel, Blocks)) {
338 if (!inLoopLevel(*Pred, LoopLevel, Blocks))
347 auto DomIt = Visited.
find(&MBB);
348 if (DomIt != Visited.
end() && DomIt->second <= LoopLevel)
357 void advanceLevel() {
360 if (!VisitedPostDom) {
361 VisitedPostDom = DefBlock;
362 VisitedDom = DefBlock;
366 VisitedDom = CommonDominators.
back();
368 for (
unsigned i = 0; i < NextLevel.
size();) {
369 if (PDT.
dominates(VisitedPostDom, NextLevel[i])) {
372 NextLevel[i] = NextLevel.
back();
380 unsigned Level = CommonDominators.
size();
381 while (!Stack.
empty()) {
386 Visited[MBB] =
Level;
390 if (Succ == DefBlock) {
391 if (MBB == VisitedPostDom)
392 FoundLoopLevel = std::min(FoundLoopLevel, Level + 1);
394 FoundLoopLevel = std::min(FoundLoopLevel, Level);
399 if (MBB == VisitedPostDom)
420 char SILowerI1Copies::ID = 0;
425 return new SILowerI1Copies();
432 : &AMDGPU::SReg_64RegClass);
457 DT = &getAnalysis<MachineDominatorTree>();
458 PDT = &getAnalysis<MachinePostDominatorTree>();
461 TII = ST->getInstrInfo();
462 IsWave32 = ST->isWave32();
465 ExecReg = AMDGPU::EXEC_LO;
466 MovOp = AMDGPU::S_MOV_B32;
467 AndOp = AMDGPU::S_AND_B32;
468 OrOp = AMDGPU::S_OR_B32;
469 XorOp = AMDGPU::S_XOR_B32;
470 AndN2Op = AMDGPU::S_ANDN2_B32;
471 OrN2Op = AMDGPU::S_ORN2_B32;
473 ExecReg = AMDGPU::EXEC;
474 MovOp = AMDGPU::S_MOV_B64;
475 AndOp = AMDGPU::S_AND_B64;
476 OrOp = AMDGPU::S_OR_B64;
477 XorOp = AMDGPU::S_XOR_B64;
478 AndN2Op = AMDGPU::S_ANDN2_B64;
479 OrN2Op = AMDGPU::S_ORN2_B64;
486 for (
unsigned Reg : ConstrainRegs)
488 ConstrainRegs.clear();
497 unsigned Size = TRI.getRegSizeInBits(Reg, MRI);
498 return Size == 1 || Size == 32;
502 void SILowerI1Copies::lowerCopiesFromI1() {
507 if (
MI.getOpcode() != AMDGPU::COPY)
512 if (!isVreg1(SrcReg))
515 if (isLaneMaskReg(DstReg) || isVreg1(DstReg))
523 assert(!
MI.getOperand(0).getSubReg());
525 ConstrainRegs.
insert(SrcReg);
526 BuildMI(MBB,
MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
536 MI->eraseFromParent();
541 void SILowerI1Copies::lowerPhis() {
543 LoopFinder LF(*DT, *PDT);
544 PhiIncomingAnalysis PIA(*PDT);
555 if (isVreg1(
MI.getOperand(0).getReg()))
563 if (&MBB != PrevMBB) {
571 MRI->
setRegClass(DstReg, IsWave32 ? &AMDGPU::SReg_32RegClass
572 : &AMDGPU::SReg_64RegClass);
575 for (
unsigned i = 1; i <
MI->getNumOperands(); i += 2) {
576 assert(i + 1 <
MI->getNumOperands());
577 Register IncomingReg =
MI->getOperand(i).getReg();
581 if (IncomingDef->
getOpcode() == AMDGPU::COPY) {
583 assert(isLaneMaskReg(IncomingReg) || isVreg1(IncomingReg));
585 }
else if (IncomingDef->
getOpcode() == AMDGPU::IMPLICIT_DEF) {
596 PhiRegisters.
insert(DstReg);
601 std::vector<MachineBasicBlock *> DomBlocks = {&MBB};
607 unsigned FoundLoopLevel = LF.findLoop(PostDomBound);
611 if (FoundLoopLevel) {
612 LF.addLoopEntries(FoundLoopLevel, SSAUpdater, IncomingBlocks);
614 for (
unsigned i = 0; i < IncomingRegs.
size(); ++i) {
617 IncomingUpdated.
back());
620 for (
unsigned i = 0; i < IncomingRegs.
size(); ++i) {
623 IMBB, getSaluInsertionAtEnd(IMBB), {}, IncomingUpdated[i],
629 PIA.analyze(MBB, IncomingBlocks);
634 for (
unsigned i = 0; i < IncomingRegs.
size(); ++i) {
636 if (PIA.isSource(IMBB)) {
645 for (
unsigned i = 0; i < IncomingRegs.
size(); ++i) {
646 if (!IncomingUpdated[i])
651 IMBB, getSaluInsertionAtEnd(IMBB), {}, IncomingUpdated[i],
657 if (NewReg != DstReg) {
659 MI->eraseFromParent();
662 IncomingBlocks.
clear();
663 IncomingRegs.
clear();
664 IncomingUpdated.
clear();
668 void SILowerI1Copies::lowerCopiesToI1() {
670 LoopFinder LF(*DT, *PDT);
677 if (
MI.getOpcode() != AMDGPU::IMPLICIT_DEF &&
678 MI.getOpcode() != AMDGPU::COPY)
682 if (!isVreg1(DstReg))
692 MRI->
setRegClass(DstReg, IsWave32 ? &AMDGPU::SReg_32RegClass
693 : &AMDGPU::SReg_64RegClass);
694 if (
MI.getOpcode() == AMDGPU::IMPLICIT_DEF)
699 assert(!
MI.getOperand(1).getSubReg());
702 (!isLaneMaskReg(SrcReg) && !isVreg1(SrcReg))) {
705 BuildMI(MBB,
MI, DL, TII->get(AMDGPU::V_CMP_NE_U32_e64), TmpReg)
708 MI.getOperand(1).setReg(TmpReg);
714 std::vector<MachineBasicBlock *> DomBlocks = {&MBB};
716 DomBlocks.push_back(
Use.getParent());
720 unsigned FoundLoopLevel = LF.findLoop(PostDomBound);
721 if (FoundLoopLevel) {
724 LF.addLoopEntries(FoundLoopLevel, SSAUpdater);
726 buildMergeLaneMasks(MBB,
MI, DL, DstReg,
733 MI->eraseFromParent();
738 bool SILowerI1Copies::isConstantLaneMask(
unsigned Reg,
bool &Val)
const {
748 if (!isLaneMaskReg(Reg))
776 if (MO.isReg() && MO.getReg() == AMDGPU::SCC) {
790 bool TerminatorsUseSCC =
false;
791 for (
auto I = InsertionPt,
E = MBB.
end();
I !=
E; ++
I) {
794 if (TerminatorsUseSCC || DefsSCC)
798 if (!TerminatorsUseSCC)
801 while (InsertionPt != MBB.
begin()) {
816 const DebugLoc &DL,
unsigned DstReg,
817 unsigned PrevReg,
unsigned CurReg) {
819 bool PrevConstant = isConstantLaneMask(PrevReg, PrevVal);
821 bool CurConstant = isConstantLaneMask(CurReg, CurVal);
823 if (PrevConstant && CurConstant) {
824 if (PrevVal == CurVal) {
825 BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), DstReg).addReg(CurReg);
827 BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), DstReg).addReg(ExecReg);
829 BuildMI(MBB, I, DL, TII->get(XorOp), DstReg)
836 unsigned PrevMaskedReg = 0;
837 unsigned CurMaskedReg = 0;
839 if (CurConstant && CurVal) {
840 PrevMaskedReg = PrevReg;
843 BuildMI(MBB, I, DL, TII->get(AndN2Op), PrevMaskedReg)
850 if (PrevConstant && PrevVal) {
851 CurMaskedReg = CurReg;
854 BuildMI(MBB, I, DL, TII->get(AndOp), CurMaskedReg)
860 if (PrevConstant && !PrevVal) {
861 BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), DstReg)
862 .addReg(CurMaskedReg);
863 }
else if (CurConstant && !CurVal) {
864 BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), DstReg)
865 .addReg(PrevMaskedReg);
866 }
else if (PrevConstant && PrevVal) {
867 BuildMI(MBB, I, DL, TII->get(OrN2Op), DstReg)
868 .addReg(CurMaskedReg)
871 BuildMI(MBB, I, DL, TII->get(OrOp), DstReg)
872 .addReg(PrevMaskedReg)
873 .
addReg(CurMaskedReg ? CurMaskedReg : ExecReg);
Helper class for SSA formation on a set of values defined in multiple blocks.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AMDGPU specific subclass of TargetSubtarget.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
This class represents lattice values for constants.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void push_back(const T &Elt)
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
INITIALIZE_PASS_BEGIN(SILowerI1Copies, DEBUG_TYPE, "SI Lower i1 Copies", false, false) INITIALIZE_PASS_END(SILowerI1Copies
unsigned getSubReg() const
iterator_range< iterator > phis()
Returns a range that iterates over the phis in the basic block.
const SIInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
static void instrDefsUsesSCC(const MachineInstr &MI, bool &Def, bool &Use)
MachineDomTreeNode * getNode(MachineBasicBlock *BB) const
iterator_range< mop_iterator > operands()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineSSAUpdater - This class updates SSA form for a set of virtual registers defined in multiple bl...
const SIRegisterInfo & getRegisterInfo() const
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
iterator_range< succ_iterator > successors()
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isSGPRReg(const MachineRegisterInfo &MRI, unsigned Reg) const
A Use represents the edge between a Value definition and its users.
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
iterator_range< iterator > terminators()
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
FunctionPass * createSILowerI1CopiesPass()
Base class for the actual dominator tree node.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B) const
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
void Initialize(unsigned V)
Initialize - Reset this object to get ready for a new set of SSA updates.
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator find(const_arg_type_t< KeyT > Val)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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.
DomTreeNodeBase * getIDom() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(const ValueT &V)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find 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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
unsigned getWavefrontSize() const
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...
LLVM_NODISCARD T pop_back_val()
pred_range predecessors(BasicBlock *BB)
void setPreservesCFG()
This function should be called by the pass, iff they do not:
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static bool isVRegCompatibleReg(const SIRegisterInfo &TRI, const MachineRegisterInfo &MRI, Register Reg)
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.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
static unsigned createLaneMaskReg(MachineFunction &MF)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
static unsigned insertUndefLaneMask(MachineBasicBlock &MBB)
void push_back(MachineInstr *MI)
unsigned GetValueInMiddleOfBlock(MachineBasicBlock *BB)
GetValueInMiddleOfBlock - Construct SSA form, materializing a value that is live in the middle of the...
void AddAvailableValue(MachineBasicBlock *BB, unsigned V)
AddAvailableValue - Indicate that a rewritten value is available at the end of the specified block wi...
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
iterator_range< use_instr_iterator > use_instructions(unsigned Reg) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
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.
void initializeSILowerI1CopiesPass(PassRegistry &)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Wrapper class representing virtual and physical registers.