[PATCH] D25191: Modify df_iterator to support post-order actions
David Callahan via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 3 13:43:05 PDT 2016
david2050 added a comment.
For illustration, this is what the client code looks like (abstracted a bit) for at least one back edge in a loop.
(Note this code is still order dependent in that the specific edges selected as back edges is order dependent for irreducible loops but is well defined otherwise).
// This stores state for the depth-first iterator. In addition
// to recording which nodes have been visited we also record whether
// a node is currently on the "stack" of active ancestors of the current
// node.
class DFState {
DenseMap<BasicBlock *, bool> Status;
public:
typedef typename DenseMap<BasicBlock *, bool>::iterator iterator;
DFState(unsigned N) { Status.reserve(N); }
std::pair<iterator, bool> insert(BasicBlock *BB) {
return Status.insert(std::make_pair(BB, true));
}
unsigned count(BasicBlock *BB) { return Status.count(BB); }
// Invoked after we have visited all children of a node.
void completed(BasicBlock *BB) { Status[BB] = false; }
// Return true if \p BB is currently on the active stack
// of ancestors.
bool onStack(BasicBlock *BB) {
auto Iter = Status.find(BB);
return Iter != Status.end() && Iter->second;
}
} State(F.size());
// Iterate over blocks in depth-first pre-order and
// treat all edges to a block already seen as loop back edges
// and mark the branch live it if there is a back edge.
typedef df_iterator<BasicBlock *, DFState, true> Iterator;
auto Iter = Iterator::begin(&F.getEntryBlock(), State);
auto End = Iterator::end(&F.getEntryBlock(), State);
for (; Iter != End; ++Iter) {
auto *BB = *Iter;
for (auto Succ : successors(BB))
if (State.onStack(Succ)) {
// back edge....
}
}
}
https://reviews.llvm.org/D25191
More information about the llvm-commits
mailing list