diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.h b/llvm/lib/Target/AMDGPU/SIInstrInfo.h --- a/llvm/lib/Target/AMDGPU/SIInstrInfo.h +++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.h @@ -277,6 +277,10 @@ MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const override; + /// Return whether the block terminate with divergent branch. + /// Note this only work before lowering the pseudo control flow instructions. + bool hasDivergentBranch(const MachineBasicBlock *MBB) const; + void insertIndirectBranch(MachineBasicBlock &MBB, MachineBasicBlock &NewDestBB, MachineBasicBlock &RestoreBB, const DebugLoc &DL, diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp --- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp +++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp @@ -2379,6 +2379,16 @@ return MI.getOperand(0).getMBB(); } +bool SIInstrInfo::hasDivergentBranch(const MachineBasicBlock *MBB) const { + for (const MachineInstr &MI : MBB->terminators()) { + if (MI.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO || + MI.getOpcode() == AMDGPU::SI_IF || MI.getOpcode() == AMDGPU::SI_ELSE || + MI.getOpcode() == AMDGPU::SI_LOOP) + return true; + } + return false; +} + void SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, MachineBasicBlock &DestBB, MachineBasicBlock &RestoreBB, diff --git a/llvm/lib/Target/AMDGPU/SILowerI1Copies.cpp b/llvm/lib/Target/AMDGPU/SILowerI1Copies.cpp --- a/llvm/lib/Target/AMDGPU/SILowerI1Copies.cpp +++ b/llvm/lib/Target/AMDGPU/SILowerI1Copies.cpp @@ -124,6 +124,7 @@ /// class PhiIncomingAnalysis { MachinePostDominatorTree &PDT; + const SIInstrInfo *TII; // For each reachable basic block, whether it is a source in the induced // subgraph of the CFG. @@ -133,7 +134,8 @@ SmallVector Predecessors; public: - PhiIncomingAnalysis(MachinePostDominatorTree &PDT) : PDT(PDT) {} + PhiIncomingAnalysis(MachinePostDominatorTree &PDT, const SIInstrInfo *TII) + : PDT(PDT), TII(TII) {} /// Returns whether \p MBB is a source in the induced subgraph of reachable /// blocks. @@ -166,18 +168,7 @@ // If this block has a divergent terminator and the def block is its // post-dominator, the wave may first visit the other successors. - bool Divergent = false; - for (MachineInstr &MI : MBB->terminators()) { - if (MI.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO || - MI.getOpcode() == AMDGPU::SI_IF || - MI.getOpcode() == AMDGPU::SI_ELSE || - MI.getOpcode() == AMDGPU::SI_LOOP) { - Divergent = true; - break; - } - } - - if (Divergent && PDT.dominates(&DefBlock, MBB)) + if (TII->hasDivergentBranch(MBB) && PDT.dominates(&DefBlock, MBB)) append_range(Stack, MBB->successors()); } @@ -541,7 +532,7 @@ bool SILowerI1Copies::lowerPhis() { MachineSSAUpdater SSAUpdater(*MF); LoopFinder LF(*DT, *PDT); - PhiIncomingAnalysis PIA(*PDT); + PhiIncomingAnalysis PIA(*PDT, TII); SmallVector Vreg1Phis; SmallVector IncomingBlocks; SmallVector IncomingRegs;