Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 8be61a8

Browse files
committed
Modify df_iterator to support post-order actions
Summary: This makes a change to the state used to maintain visited information for depth first iterator. We know assume a method "completed(...)" which is called after all children of a node have been visited. In all existing cases, this method does nothing so this patch has no functional changes. It will however allow a client to distinguish back from cross edges in a DFS tree. Reviewers: nadav, mehdi_amini, dberlin Subscribers: MatzeB, mzolotukhin, twoh, freik, llvm-commits Differential Revision: https://reviews.llvm.org/D25191 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283391 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5ea3570 commit 8be61a8

14 files changed

+48
-29
lines changed

include/llvm/ADT/DepthFirstIterator.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,25 @@ class df_iterator_storage<SetType, true> {
5858
SetType &Visited;
5959
};
6060

61+
// The visited stated for the iteration is a simple set augmented with
62+
// one more method, completed, which is invoked when all children of a
63+
// node have been processed. It is intended to distinguish of back and
64+
// cross edges in the spanning tree but is not used in the common case.
65+
template <typename NodeRef, unsigned SmallSize=8>
66+
struct df_iterator_default_set : public llvm::SmallPtrSet<NodeRef, SmallSize> {
67+
typedef llvm::SmallPtrSet<NodeRef, SmallSize> BaseSet;
68+
typedef typename BaseSet::iterator iterator;
69+
std::pair<iterator,bool> insert(NodeRef N) { return BaseSet::insert(N) ; }
70+
template <typename IterT>
71+
void insert(IterT Begin, IterT End) { BaseSet::insert(Begin,End); }
72+
73+
void completed(NodeRef) { }
74+
};
75+
6176
// Generic Depth First Iterator
6277
template <class GraphT,
6378
class SetType =
64-
llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeRef, 8>,
79+
df_iterator_default_set<typename GraphTraits<GraphT>::NodeRef>,
6580
bool ExtStorage = false, class GT = GraphTraits<GraphT>>
6681
class df_iterator
6782
: public std::iterator<std::forward_iterator_tag, typename GT::NodeRef>,
@@ -89,10 +104,8 @@ class df_iterator
89104
}
90105
inline df_iterator(NodeRef Node, SetType &S)
91106
: df_iterator_storage<SetType, ExtStorage>(S) {
92-
if (!S.count(Node)) {
107+
if (this->Visited.insert(Node).second)
93108
VisitStack.push_back(StackElement(Node, None));
94-
this->Visited.insert(Node);
95-
}
96109
}
97110
inline df_iterator(SetType &S)
98111
: df_iterator_storage<SetType, ExtStorage>(S) {
@@ -119,7 +132,8 @@ class df_iterator
119132
return;
120133
}
121134
}
122-
135+
this->Visited.completed(Node);
136+
123137
// Oops, ran out of successors... go up a level on the stack.
124138
VisitStack.pop_back();
125139
} while (!VisitStack.empty());
@@ -235,7 +249,8 @@ iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
235249

236250
// Provide global definitions of inverse depth first iterators...
237251
template <class T,
238-
class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeRef, 8>,
252+
class SetTy =
253+
df_iterator_default_set<typename GraphTraits<T>::NodeRef>,
239254
bool External = false>
240255
struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
241256
idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)

include/llvm/Analysis/LoopInfoImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ void LoopBase<BlockT, LoopT>::verifyLoop() const {
228228
// Setup for using a depth-first iterator to visit every block in the loop.
229229
SmallVector<BlockT*, 8> ExitBBs;
230230
getExitBlocks(ExitBBs);
231-
llvm::SmallPtrSet<BlockT*, 8> VisitSet;
231+
df_iterator_default_set<BlockT*> VisitSet;
232232
VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
233-
df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> >
233+
df_ext_iterator<BlockT*, df_iterator_default_set<BlockT*>>
234234
BI = df_ext_begin(getHeader(), VisitSet),
235235
BE = df_ext_end(getHeader(), VisitSet);
236236

include/llvm/Analysis/RegionInfo.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,14 @@ class RegionBase : public RegionNodeBase<Tr> {
626626
/// are direct children of this Region. It does not iterate over any
627627
/// RegionNodes that are also element of a subregion of this Region.
628628
//@{
629-
typedef df_iterator<RegionNodeT *, SmallPtrSet<RegionNodeT *, 8>, false,
630-
GraphTraits<RegionNodeT *>> element_iterator;
631-
632-
typedef df_iterator<const RegionNodeT *, SmallPtrSet<const RegionNodeT *, 8>,
633-
false,
634-
GraphTraits<const RegionNodeT *>> const_element_iterator;
629+
typedef df_iterator<RegionNodeT *, df_iterator_default_set<RegionNodeT *>,
630+
false, GraphTraits<RegionNodeT *>>
631+
element_iterator;
632+
633+
typedef df_iterator<const RegionNodeT *,
634+
df_iterator_default_set<const RegionNodeT *>, false,
635+
GraphTraits<const RegionNodeT *>>
636+
const_element_iterator;
635637

636638
element_iterator element_begin();
637639
element_iterator element_end();

include/llvm/Analysis/RegionIterator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ inline RNSuccIterator<NodeRef, BlockT, RegionT> succ_end(NodeRef Node) {
294294
template <> \
295295
struct GraphTraits<FlatIt<RegionT *>> \
296296
: public GraphTraits<FlatIt<NodeT *>> { \
297-
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false, \
297+
typedef df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false, \
298298
GraphTraits<FlatIt<NodeRef>>> \
299299
nodes_iterator; \
300300
static NodeRef getEntryNode(RegionT *R) { \
@@ -316,7 +316,7 @@ RegionGraphTraits(const Region, const RegionNode);
316316

317317
template <> struct GraphTraits<RegionInfo*>
318318
: public GraphTraits<FlatIt<RegionNode*> > {
319-
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false,
319+
typedef df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
320320
GraphTraits<FlatIt<NodeRef>>>
321321
nodes_iterator;
322322

@@ -333,7 +333,7 @@ template <> struct GraphTraits<RegionInfo*>
333333

334334
template <> struct GraphTraits<RegionInfoPass*>
335335
: public GraphTraits<RegionInfo *> {
336-
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false,
336+
typedef df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
337337
GraphTraits<FlatIt<NodeRef>>>
338338
nodes_iterator;
339339

include/llvm/CodeGen/MachineRegionInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ RegionGraphTraits(const MachineRegion, const MachineRegionNode);
142142

143143
template <> struct GraphTraits<MachineRegionInfo*>
144144
: public GraphTraits<FlatIt<MachineRegionNode*> > {
145-
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false,
145+
typedef df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
146146
GraphTraits<FlatIt<NodeRef>>>
147147
nodes_iterator;
148148

@@ -159,7 +159,7 @@ template <> struct GraphTraits<MachineRegionInfo*>
159159

160160
template <> struct GraphTraits<MachineRegionInfoPass*>
161161
: public GraphTraits<MachineRegionInfo *> {
162-
typedef df_iterator<NodeRef, SmallPtrSet<NodeRef, 8>, false,
162+
typedef df_iterator<NodeRef, df_iterator_default_set<NodeRef>, false,
163163
GraphTraits<FlatIt<NodeRef>>>
164164
nodes_iterator;
165165

include/llvm/IR/Dominators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class DominatorTree : public DominatorTreeBase<BasicBlock> {
157157
template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
158158
typedef Node *NodeRef;
159159
typedef ChildIterator ChildIteratorType;
160-
typedef df_iterator<Node *, SmallPtrSet<NodeRef, 8>> nodes_iterator;
160+
typedef df_iterator<Node *, df_iterator_default_set<Node*>> nodes_iterator;
161161

162162
static NodeRef getEntryNode(NodeRef N) { return N; }
163163
static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }

lib/CodeGen/LiveIntervalAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill,
599599
// Find all blocks that are reachable from KillMBB without leaving VNI's live
600600
// range. It is possible that KillMBB itself is reachable, so start a DFS
601601
// from each successor.
602-
typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy;
602+
typedef df_iterator_default_set<MachineBasicBlock*,9> VisitedTy;
603603
VisitedTy Visited;
604604
for (MachineBasicBlock::succ_iterator
605605
SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end();

lib/CodeGen/LiveVariables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
643643
// register before its uses due to dominance properties of SSA (except for PHI
644644
// nodes, which are treated as a special case).
645645
MachineBasicBlock *Entry = &MF->front();
646-
SmallPtrSet<MachineBasicBlock*,16> Visited;
646+
df_iterator_default_set<MachineBasicBlock*,16> Visited;
647647

648648
for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) {
649649
runOnBlock(MBB, NumRegs);

lib/CodeGen/MachineVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,11 +2014,11 @@ void MachineVerifier::verifyStackFrame() {
20142014

20152015
SmallVector<StackStateOfBB, 8> SPState;
20162016
SPState.resize(MF->getNumBlockIDs());
2017-
SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
2017+
df_iterator_default_set<const MachineBasicBlock*> Reachable;
20182018

20192019
// Visit the MBBs in DFS order.
20202020
for (df_ext_iterator<const MachineFunction*,
2021-
SmallPtrSet<const MachineBasicBlock*, 8> >
2021+
df_iterator_default_set<const MachineBasicBlock*> >
20222022
DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
20232023
DFI != DFE; ++DFI) {
20242024
const MachineBasicBlock *MBB = *DFI;

lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ void PEI::replaceFrameIndices(MachineFunction &Fn) {
10081008
// Store SPAdj at exit of a basic block.
10091009
SmallVector<int, 8> SPState;
10101010
SPState.resize(Fn.getNumBlockIDs());
1011-
SmallPtrSet<MachineBasicBlock*, 8> Reachable;
1011+
df_iterator_default_set<MachineBasicBlock*> Reachable;
10121012

10131013
// Iterate over the reachable blocks in DFS order.
10141014
for (auto DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable);

lib/CodeGen/UnreachableBlockElim.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
using namespace llvm;
4141

4242
static bool eliminateUnreachableBlock(Function &F) {
43-
SmallPtrSet<BasicBlock*, 8> Reachable;
43+
df_iterator_default_set<BasicBlock*> Reachable;
4444

4545
// Mark all reachable blocks.
4646
for (BasicBlock *BB : depth_first_ext(&F, Reachable))
@@ -130,7 +130,7 @@ void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
130130
}
131131

132132
bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
133-
SmallPtrSet<MachineBasicBlock*, 8> Reachable;
133+
df_iterator_default_set<MachineBasicBlock*> Reachable;
134134
bool ModifiedPHI = false;
135135

136136
MMI = getAnalysisIfAvailable<MachineModuleInfo>();

lib/Target/X86/X86FloatingPoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ bool FPS::runOnMachineFunction(MachineFunction &MF) {
326326

327327
// Process the function in depth first order so that we process at least one
328328
// of the predecessors for every reachable block in the function.
329-
SmallPtrSet<MachineBasicBlock*, 8> Processed;
329+
df_iterator_default_set<MachineBasicBlock*> Processed;
330330
MachineBasicBlock *Entry = &MF.front();
331331

332332
bool Changed = false;

lib/Transforms/IPO/ArgumentPromotion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ static bool isSafeToPromoteArgument(Argument *Arg, bool isByValOrInAlloca,
600600

601601
// Because there could be several/many load instructions, remember which
602602
// blocks we know to be transparent to the load.
603-
SmallPtrSet<BasicBlock*, 16> TranspBlocks;
603+
df_iterator_default_set<BasicBlock*, 16> TranspBlocks;
604604

605605
for (LoadInst *Load : Loads) {
606606
// Check to see if the load is invalidated from the start of the block to

unittests/ADT/DepthFirstIteratorTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ template <typename T> struct CountedSet {
2727
}
2828

2929
size_t count(const T &Item) const { return S.count(Item); }
30+
31+
void completed(T) { }
3032
};
3133

3234
template <typename T> class df_iterator_storage<CountedSet<T>, true> {

0 commit comments

Comments
 (0)