Skip to content

Commit 1d11351

Browse files
MaskRaygithub-actions[bot]
authored andcommitted
Automerge: [CycleInfo] Drop GraphTraits and df_iterator. NFC (#209847)
depth_first uses SmallPtrSet visited-set, which is pure overhead on a tree. Replace them with explicit child-stack walks and delete the unused GraphTraits specializations to prevent misuse.
2 parents 4e1d2d9 + 503c447 commit 1d11351

3 files changed

Lines changed: 40 additions & 64 deletions

File tree

llvm/include/llvm/ADT/GenericCycleImpl.h

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,13 @@ void GenericCycleInfoCompute<ContextT>::run(FunctionT *F) {
485485
/// \brief Recompute depth values of \p SubTree and all descendants.
486486
template <typename ContextT>
487487
void GenericCycleInfoCompute<ContextT>::updateDepth(CycleT *SubTree) {
488-
for (CycleT *Cycle : depth_first(SubTree))
488+
SmallVector<CycleT *, 8> Worklist = {SubTree};
489+
while (!Worklist.empty()) {
490+
CycleT *Cycle = Worklist.pop_back_val();
489491
Cycle->Depth = Cycle->ParentCycle ? Cycle->ParentCycle->Depth + 1 : 1;
492+
for (CycleT *Child : Cycle->children())
493+
Worklist.push_back(Child);
494+
}
490495
}
491496

492497
/// \brief Compute a DFS of basic blocks starting at the function entry.
@@ -634,21 +639,23 @@ void GenericCycleInfo<ContextT>::verifyCycleNest(bool VerifyFull) const {
634639
#ifndef NDEBUG
635640
DenseSet<BlockT *> CycleHeaders;
636641

637-
for (CycleT *TopCycle : toplevel_cycles()) {
638-
for (CycleT *Cycle : depth_first(TopCycle)) {
639-
BlockT *Header = Cycle->getHeader();
640-
assert(CycleHeaders.insert(Header).second);
641-
if (VerifyFull)
642-
verifyCycle(*Cycle);
643-
else
644-
verifyCycleNest(*Cycle);
645-
// Check the block map entries for blocks contained in this cycle.
646-
for (BlockT *BB : getBlocks(*Cycle)) {
647-
CycleT *CycleInBlockMap = getCycle(BB);
648-
assert(CycleInBlockMap != nullptr);
649-
assert(Cycle->contains(CycleInBlockMap));
650-
}
642+
SmallVector<CycleT *, 8> Worklist(toplevel_begin(), toplevel_end());
643+
while (!Worklist.empty()) {
644+
CycleT *Cycle = Worklist.pop_back_val();
645+
BlockT *Header = Cycle->getHeader();
646+
assert(CycleHeaders.insert(Header).second);
647+
if (VerifyFull)
648+
verifyCycle(*Cycle);
649+
else
650+
verifyCycleNest(*Cycle);
651+
// Check the block map entries for blocks contained in this cycle.
652+
for (BlockT *BB : getBlocks(*Cycle)) {
653+
CycleT *CycleInBlockMap = getCycle(BB);
654+
assert(CycleInBlockMap != nullptr);
655+
assert(Cycle->contains(CycleInBlockMap));
651656
}
657+
for (CycleT *Child : Cycle->children())
658+
Worklist.push_back(Child);
652659
}
653660
#endif
654661
}
@@ -661,12 +668,17 @@ template <typename ContextT> void GenericCycleInfo<ContextT>::verify() const {
661668
/// \brief Print the cycle info.
662669
template <typename ContextT>
663670
void GenericCycleInfo<ContextT>::print(raw_ostream &Out) const {
664-
for (const auto *TLC : toplevel_cycles()) {
665-
for (const CycleT *Cycle : depth_first(TLC)) {
671+
SmallVector<const CycleT *, 8> Stack;
672+
for (const CycleT *TLC : toplevel_cycles()) {
673+
Stack.push_back(TLC);
674+
while (!Stack.empty()) {
675+
const CycleT *Cycle = Stack.pop_back_val();
666676
for (unsigned I = 0; I < Cycle->Depth; ++I)
667677
Out << " ";
668678

669679
Out << print(Cycle) << '\n';
680+
for (const auto &Child : reverse(Cycle->Children))
681+
Stack.push_back(Child.get());
670682
}
671683
}
672684
}

llvm/include/llvm/ADT/GenericCycleInfo.h

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,13 @@ template <typename ContextT> class GenericCycle {
130130
using const_child_iterator_base =
131131
typename std::vector<std::unique_ptr<GenericCycle>>::const_iterator;
132132
struct const_child_iterator
133-
: iterator_adaptor_base<const_child_iterator, const_child_iterator_base> {
133+
: iterator_adaptor_base<const_child_iterator, const_child_iterator_base,
134+
std::random_access_iterator_tag, GenericCycle *,
135+
std::ptrdiff_t, GenericCycle *, GenericCycle *> {
134136
using Base =
135-
iterator_adaptor_base<const_child_iterator, const_child_iterator_base>;
137+
iterator_adaptor_base<const_child_iterator, const_child_iterator_base,
138+
std::random_access_iterator_tag, GenericCycle *,
139+
std::ptrdiff_t, GenericCycle *, GenericCycle *>;
136140

137141
const_child_iterator() = default;
138142
explicit const_child_iterator(const_child_iterator_base I) : Base(I) {}
@@ -351,50 +355,6 @@ template <typename ContextT> class GenericCycleInfo {
351355
//@}
352356
};
353357

354-
/// \brief GraphTraits for iterating over a sub-tree of the CycleT tree.
355-
template <typename CycleRefT, typename ChildIteratorT> struct CycleGraphTraits {
356-
using NodeRef = CycleRefT;
357-
358-
using nodes_iterator = ChildIteratorT;
359-
using ChildIteratorType = nodes_iterator;
360-
361-
static NodeRef getEntryNode(NodeRef Graph) { return Graph; }
362-
363-
static ChildIteratorType child_begin(NodeRef Ref) {
364-
return Ref->child_begin();
365-
}
366-
static ChildIteratorType child_end(NodeRef Ref) { return Ref->child_end(); }
367-
368-
// Not implemented:
369-
// static nodes_iterator nodes_begin(GraphType *G)
370-
// static nodes_iterator nodes_end (GraphType *G)
371-
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
372-
373-
// typedef EdgeRef - Type of Edge token in the graph, which should
374-
// be cheap to copy.
375-
// typedef ChildEdgeIteratorType - Type used to iterate over children edges in
376-
// graph, dereference to a EdgeRef.
377-
378-
// static ChildEdgeIteratorType child_edge_begin(NodeRef)
379-
// static ChildEdgeIteratorType child_edge_end(NodeRef)
380-
// Return iterators that point to the beginning and ending of the
381-
// edge list for the given callgraph node.
382-
//
383-
// static NodeRef edge_dest(EdgeRef)
384-
// Return the destination node of an edge.
385-
// static unsigned size (GraphType *G)
386-
// Return total number of nodes in the graph
387-
};
388-
389-
template <typename BlockT>
390-
struct GraphTraits<const GenericCycle<BlockT> *>
391-
: CycleGraphTraits<const GenericCycle<BlockT> *,
392-
typename GenericCycle<BlockT>::const_child_iterator> {};
393-
template <typename BlockT>
394-
struct GraphTraits<GenericCycle<BlockT> *>
395-
: CycleGraphTraits<GenericCycle<BlockT> *,
396-
typename GenericCycle<BlockT>::const_child_iterator> {};
397-
398358
} // namespace llvm
399359

400360
#endif // LLVM_ADT_GENERICCYCLEINFO_H

llvm/lib/Transforms/Utils/FixIrreducible.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,13 @@ static bool FixIrreducibleImpl(Function &F, CycleInfo &CI, DominatorTree &DT,
429429
<< F.getName() << "\n");
430430

431431
bool Changed = false;
432+
SmallVector<Cycle *, 8> Worklist;
432433
for (Cycle *TopCycle : CI.toplevel_cycles()) {
433-
for (Cycle *C : depth_first(TopCycle)) {
434+
Worklist.push_back(TopCycle);
435+
while (!Worklist.empty()) {
436+
Cycle *C = Worklist.pop_back_val();
434437
Changed |= fixIrreducible(*C, CI, DT, LI);
438+
llvm::append_range(Worklist, reverse(C->children()));
435439
}
436440
}
437441

0 commit comments

Comments
 (0)