91 #define DEBUG_TYPE "divergence-analysis" 97 : F(F), RegionLoop(RegionLoop), DT(DT), LI(LI), SDA(SDA),
98 IsLCSSAForm(IsLCSSAForm) {}
101 assert(isa<Instruction>(DivVal) || isa<Argument>(DivVal));
103 DivergentValues.
insert(&DivVal);
107 UniformOverrides.
insert(&UniVal);
110 bool DivergenceAnalysis::updateTerminator(
const Instruction &Term)
const {
113 if (
auto *BranchTerm = dyn_cast<BranchInst>(&Term)) {
114 assert(BranchTerm->isConditional());
117 if (
auto *SwitchTerm = dyn_cast<SwitchInst>(&Term)) {
120 if (isa<InvokeInst>(Term)) {
127 bool DivergenceAnalysis::updateNormalInstruction(
const Instruction &
I)
const {
136 bool DivergenceAnalysis::isTemporalDivergent(
const BasicBlock &ObservingBlock,
137 const Value &Val)
const {
146 if (DivergentLoops.find(
Loop) != DivergentLoops.end())
153 bool DivergenceAnalysis::updatePHINode(
const PHINode &Phi)
const {
172 isTemporalDivergent(*Phi.
getParent(), *InVal)) {
189 void DivergenceAnalysis::taintLoopLiveOuts(
const BasicBlock &LoopHeader) {
191 assert(DivLoop &&
"loopHeader is not actually part of a loop");
194 DivLoop->getExitBlocks(TaintStack);
199 for (
auto *Block : TaintStack) {
202 Visited.
insert(&LoopHeader);
204 while (!TaintStack.empty()) {
205 auto *UserBlock = TaintStack.back();
206 TaintStack.pop_back();
212 assert(!DivLoop->contains(UserBlock) &&
213 "irreducible control flow detected");
216 if (!DT.
dominates(&LoopHeader, UserBlock)) {
218 for (
auto &Phi : UserBlock->phis()) {
219 Worklist.push_back(&Phi);
225 for (
auto &I : *UserBlock) {
235 if (DivLoop->contains(OpInst->getParent())) {
244 for (
auto *SuccBlock :
successors(UserBlock)) {
245 if (!Visited.
insert(SuccBlock).second) {
248 TaintStack.push_back(SuccBlock);
253 void DivergenceAnalysis::pushPHINodes(
const BasicBlock &Block) {
254 for (
const auto &Phi : Block.
phis()) {
257 Worklist.push_back(&Phi);
261 void DivergenceAnalysis::pushUsers(
const Value &V) {
273 Worklist.push_back(UserInst);
277 bool DivergenceAnalysis::propagateJoinDivergence(
const BasicBlock &JoinBlock,
278 const Loop *BranchLoop) {
287 pushPHINodes(JoinBlock);
290 if (BranchLoop && !BranchLoop->
contains(&JoinBlock)) {
295 markBlockJoinDivergent(JoinBlock);
299 void DivergenceAnalysis::propagateBranchDivergence(
const Instruction &Term) {
307 bool IsBranchLoopDivergent =
false;
311 for (
const auto *JoinBlock : SDA.
join_blocks(Term)) {
312 IsBranchLoopDivergent |= propagateJoinDivergence(*JoinBlock, BranchLoop);
316 if (IsBranchLoopDivergent) {
318 if (!DivergentLoops.insert(BranchLoop).second) {
321 propagateLoopDivergence(*BranchLoop);
325 void DivergenceAnalysis::propagateLoopDivergence(
const Loop &ExitingLoop) {
341 taintLoopLiveOuts(*ExitingLoop.
getHeader());
344 bool IsBranchLoopDivergent =
false;
349 for (
const auto *JoinBlock : SDA.
join_blocks(ExitingLoop)) {
350 IsBranchLoopDivergent |= propagateJoinDivergence(*JoinBlock, BranchLoop);
354 if (IsBranchLoopDivergent) {
356 if (!DivergentLoops.insert(BranchLoop).second) {
359 propagateLoopDivergence(*BranchLoop);
364 for (
auto *DivVal : DivergentValues) {
369 while (!Worklist.empty()) {
383 if (updateTerminator(I)) {
385 propagateBranchDivergence(I);
391 bool DivergentUpd =
false;
394 DivergentUpd = updatePHINode(*Phi);
396 DivergentUpd = updateNormalInstruction(I);
408 return UniformOverrides.
find(&V) != UniformOverrides.
end();
412 return DivergentValues.
find(&V) != DivergentValues.
end();
422 if (DivergentValues.
empty())
427 OS <<
"DIVERGENT:" << I <<
'\n';
437 : SDA(DT, PDT, LI), DA(F, nullptr, DT, LI, SDA,
false) {
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
void addUniformOverride(const Value &UniVal)
Mark UniVal as a value that is always uniform.
Implements a dense probed hash-table based set.
bool isTerminator() const
const ConstBlockSet & join_blocks(const Instruction &Term)
Computes divergent join points and loop exits caused by branch divergence in Term.
DivergenceAnalysis(const Function &F, const Loop *RegionLoop, const DominatorTree &DT, const LoopInfo &LI, SyncDependenceAnalysis &SDA, bool IsLCSSAForm)
This instance will analyze the whole function F or the loop RegionLoop.
void compute()
Propagate divergence to all instructions in the region.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
bool isDivergent(const Value &V) const
Whether V is divergent at its definition.
A Use represents the edge between a Value definition and its users.
const Function & getFunction() const
BlockT * getHeader() const
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Move duplicate certain instructions close to their use
LLVM Basic Block Representation.
std::pair< iterator, bool > insert(const ValueT &V)
Relates points of divergent control to join points in reducible CFGs.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
GPUDivergenceAnalysis(Function &F, const DominatorTree &DT, const PostDominatorTree &PDT, const LoopInfo &LI, const TargetTransformInfo &TTI)
Runs the divergence analysis on , a GPU kernel.
bool hasConstantOrUndefValue() const
Whether the specified PHI node always merges together the same value, assuming undefs are equal to a ...
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
bool isDivergentUse(const Use &U) const
Whether U is divergent. Uses of a uniform value can be divergent.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
iterator_range< user_iterator > users()
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
LoopT * getParentLoop() const
StringRef getName() const
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
bool inRegion(const BasicBlock &BB) const
Whether BB is part of the region.
bool isDivergent(const Value &Val) const
Whether Val is divergent at its definition.
iterator find(const_arg_type_t< ValueT > V)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
bool isAlwaysUniform(const Value &Val) const
Whether Val will always return a uniform value regardless of its operands.
void markDivergent(const Value &DivVal)
Mark DivVal as a value that is always divergent.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
succ_range successors(Instruction *I)
bool isDivergentUse(const Use &U) const
Whether U is divergent. Uses of a uniform value can be divergent.
This class implements an extremely fast bulk output stream that can only output to a stream...
inst_range instructions(Function *F)
void print(raw_ostream &OS, const Module *) const
void print(raw_ostream &OS, const Module *) const
Print all divergent values in the kernel.
iterator_range< arg_iterator > args()
const BasicBlock * getParent() const