112 #include <unordered_set> 114 #define DEBUG_TYPE "sync-dependence" 123 : FuncRPOT(DT.getRoot()->
getParent()), DT(DT), PDT(PDT), LI(LI) {}
154 : FuncRPOT(FuncRPOT), DT(DT), PDT(PDT), LI(LI),
159 bool WasAdded = DefMap.emplace(&Block, &DefBlock).second;
161 PendingUpdates.insert(&Block);
165 Out <<
"Propagator::DefMap {\n";
166 for (
const auto *Block : FuncRPOT) {
167 auto It = DefMap.find(Block);
168 Out << Block->getName() <<
" : ";
169 if (It == DefMap.end()) {
172 const auto *DefBlock = It->second;
173 Out << (DefBlock ? DefBlock->getName() :
"<null>") <<
"\n";
185 if (ParentLoop && !ParentLoop->
contains(&SuccBlock)) {
186 DefMap.emplace(&SuccBlock, &DefBlock);
187 ReachedLoopExits.
insert(&SuccBlock);
192 auto ItLastDef = DefMap.find(&SuccBlock);
193 if (ItLastDef == DefMap.end()) {
194 addPending(SuccBlock, DefBlock);
199 if (ItLastDef->second != &DefBlock) {
201 if (!JoinBlocks->insert(&SuccBlock).second)
205 addPending(SuccBlock, SuccBlock);
218 template <
typename SuccessorIterable>
219 std::unique_ptr<ConstBlockSet>
221 SuccessorIterable NodeSuccessors,
const Loop *ParentLoop) {
224 LLVM_DEBUG(
dbgs() <<
"SDA:computeJoinPoints. Parent loop: " << (ParentLoop ? ParentLoop->
getName() :
"<null>") <<
"\n" );
227 for (
const auto *SuccBlock : NodeSuccessors) {
228 DefMap.emplace(SuccBlock, SuccBlock);
230 if (ParentLoop && !ParentLoop->
contains(SuccBlock)) {
232 ReachedLoopExits.
insert(SuccBlock);
235 PendingUpdates.insert(SuccBlock);
240 dbgs() <<
"SDA: rpo order:\n";
241 for (
const auto * RpoBlock : FuncRPOT) {
242 dbgs() <<
"- " << RpoBlock->getName() <<
"\n";
246 auto ItBeginRPO = FuncRPOT.begin();
249 for (; *ItBeginRPO != &RootBlock; ++ItBeginRPO) {}
251 auto ItEndRPO = FuncRPOT.end();
252 assert(ItBeginRPO != ItEndRPO);
255 auto ItBlockRPO = ItBeginRPO;
256 while ((++ItBlockRPO != ItEndRPO) &&
257 !PendingUpdates.empty()) {
258 const auto *Block = *ItBlockRPO;
259 LLVM_DEBUG(
dbgs() <<
"SDA::joins. visiting " << Block->getName() <<
"\n");
262 auto ItPending = PendingUpdates.find(Block);
263 if (ItPending == PendingUpdates.end())
265 PendingUpdates.erase(ItPending);
268 auto ItDef = DefMap.find(Block);
269 const auto *DefBlock = ItDef->second;
274 (ParentLoop != BlockLoop && ParentLoop->
contains(BlockLoop))) {
278 BlockLoop->getExitBlocks(BlockLoopExits);
279 for (
const auto *BlockLoopExit : BlockLoopExits) {
280 visitSuccessor(*BlockLoopExit, ParentLoop, *DefBlock);
285 for (
const auto *SuccBlock :
successors(Block)) {
286 visitSuccessor(*SuccBlock, ParentLoop, *DefBlock);
308 if (!ReachedLoopExits.
empty()) {
310 ParentLoop ? ParentLoop->
getHeader() :
nullptr;
313 auto ItHeaderDef = DefMap.find(ParentLoopHeader);
314 const auto *HeaderDefBlock = (ItHeaderDef == DefMap.end()) ?
nullptr : ItHeaderDef->second;
317 assert(HeaderDefBlock &&
"no definition at header of carrying loop");
319 for (
const auto *ExitBlock : ReachedLoopExits) {
320 auto ItExitDef = DefMap.find(ExitBlock);
321 assert((ItExitDef != DefMap.end()) &&
322 "no reaching def at reachable loop exit");
323 if (ItExitDef->second != HeaderDefBlock) {
324 JoinBlocks->insert(ExitBlock);
329 return std::move(JoinBlocks);
335 LoopExitVec LoopExits;
337 if (LoopExits.size() < 1) {
338 return EmptyBlockSet;
342 auto ItCached = CachedLoopExitJoins.find(&Loop);
343 if (ItCached != CachedLoopExitJoins.end()) {
344 return *ItCached->second;
352 auto ItInserted = CachedLoopExitJoins.emplace(&Loop, std::move(JoinBlocks));
353 assert(ItInserted.second);
354 return *ItInserted.first->second;
361 return EmptyBlockSet;
365 auto ItCached = CachedBranchJoins.
find(&Term);
366 if (ItCached != CachedBranchJoins.end())
367 return *ItCached->second;
371 const auto &TermBlock = *Term.getParent();
373 TermBlock,
successors(Term.getParent()), LI.getLoopFor(&TermBlock));
375 auto ItInserted = CachedBranchJoins.emplace(&Term, std::move(JoinBlocks));
376 assert(ItInserted.second);
377 return *ItInserted.first->second;
SyncDependenceAnalysis(const DominatorTree &DT, const PostDominatorTree &PDT, const LoopInfo &LI)
This class represents lattice values for constants.
SmallPtrSet< const BasicBlock *, 4 > ConstBlockSet
~SyncDependenceAnalysis()
const ConstBlockSet & join_blocks(const Instruction &Term)
Computes divergent join points and loop exits caused by branch divergence in Term.
const PostDominatorTree & PDT
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
iterator find(ConstPtrType Ptr) const
SmallPtrSet< const BasicBlock *, 4 > ReachedLoopExits
BlockT * getHeader() const
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
void addPending(const BasicBlock &Block, const BasicBlock &DefBlock)
std::map< const BasicBlock *, const BasicBlock * > DefiningBlockMap
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void printDefs(raw_ostream &Out)
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
const FunctionRPOT & FuncRPOT
void visitSuccessor(const BasicBlock &SuccBlock, const Loop *ParentLoop, const BasicBlock &DefBlock)
LLVM Basic Block Representation.
LLVM_NODISCARD bool empty() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
DivergencePropagator(const FunctionRPOT &FuncRPOT, const DominatorTree &DT, const PostDominatorTree &PDT, const LoopInfo &LI)
std::unordered_set< const BasicBlock * > PendingUpdates
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
A range adaptor for a pair of iterators.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
LoopT * getParentLoop() const
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
std::unique_ptr< ConstBlockSet > JoinBlocks
StringRef getName() const
Represents a single loop in the control flow graph.
std::unique_ptr< ConstBlockSet > computeJoinPoints(const BasicBlock &RootBlock, SuccessorIterable NodeSuccessors, const Loop *ParentLoop)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
succ_range successors(Instruction *I)
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...