1 //===- llvm/Analysis/DominanceFrontier.h - Dominator Frontiers --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the generic implementation of the DominanceFrontier class, which
11 // calculate and holds the dominance frontier for a function for.
13 // This should be considered deprecated, don't add any more uses of this data
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
19 #define LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H
21 #include "llvm/ADT/GraphTraits.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/Analysis/DominanceFrontier.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/GenericDomTree.h"
27 #include "llvm/Support/raw_ostream.h"
35 template <class BlockT>
36 class DFCalculateWorkObject {
38 using DomTreeNodeT = DomTreeNodeBase<BlockT>;
40 DFCalculateWorkObject(BlockT *B, BlockT *P, const DomTreeNodeT *N,
41 const DomTreeNodeT *PN)
42 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
46 const DomTreeNodeT *Node;
47 const DomTreeNodeT *parentNode;
50 template <class BlockT, bool IsPostDom>
51 void DominanceFrontierBase<BlockT, IsPostDom>::removeBlock(BlockT *BB) {
52 assert(find(BB) != end() && "Block is not in DominanceFrontier!");
53 for (iterator I = begin(), E = end(); I != E; ++I)
58 template <class BlockT, bool IsPostDom>
59 void DominanceFrontierBase<BlockT, IsPostDom>::addToFrontier(iterator I,
61 assert(I != end() && "BB is not in DominanceFrontier!");
62 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
63 I->second.erase(Node);
66 template <class BlockT, bool IsPostDom>
67 void DominanceFrontierBase<BlockT, IsPostDom>::removeFromFrontier(
68 iterator I, BlockT *Node) {
69 assert(I != end() && "BB is not in DominanceFrontier!");
70 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
71 I->second.erase(Node);
74 template <class BlockT, bool IsPostDom>
75 bool DominanceFrontierBase<BlockT, IsPostDom>::compareDomSet(
76 DomSetType &DS1, const DomSetType &DS2) const {
77 std::set<BlockT *> tmpSet;
78 for (BlockT *BB : DS2)
81 for (typename DomSetType::const_iterator I = DS1.begin(), E = DS1.end();
85 if (tmpSet.erase(Node) == 0)
86 // Node is in DS1 but tnot in DS2.
90 if (!tmpSet.empty()) {
91 // There are nodes that are in DS2 but not in DS1.
95 // DS1 and DS2 matches.
99 template <class BlockT, bool IsPostDom>
100 bool DominanceFrontierBase<BlockT, IsPostDom>::compare(
101 DominanceFrontierBase<BlockT, IsPostDom> &Other) const {
102 DomSetMapType tmpFrontiers;
103 for (typename DomSetMapType::const_iterator I = Other.begin(),
106 tmpFrontiers.insert(std::make_pair(I->first, I->second));
108 for (typename DomSetMapType::iterator I = tmpFrontiers.begin(),
109 E = tmpFrontiers.end();
111 BlockT *Node = I->first;
112 const_iterator DFI = find(Node);
116 if (compareDomSet(I->second, DFI->second))
120 tmpFrontiers.erase(Node);
123 if (!tmpFrontiers.empty())
129 template <class BlockT, bool IsPostDom>
130 void DominanceFrontierBase<BlockT, IsPostDom>::print(raw_ostream &OS) const {
131 for (const_iterator I = begin(), E = end(); I != E; ++I) {
132 OS << " DomFrontier for BB ";
134 I->first->printAsOperand(OS, false);
136 OS << " <<exit node>>";
139 const std::set<BlockT *> &BBs = I->second;
141 for (const BlockT *BB : BBs) {
144 BB->printAsOperand(OS, false);
146 OS << "<<exit node>>";
152 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
153 template <class BlockT, bool IsPostDom>
154 void DominanceFrontierBase<BlockT, IsPostDom>::dump() const {
159 template <class BlockT>
160 const typename ForwardDominanceFrontierBase<BlockT>::DomSetType &
161 ForwardDominanceFrontierBase<BlockT>::calculate(const DomTreeT &DT,
162 const DomTreeNodeT *Node) {
163 BlockT *BB = Node->getBlock();
164 DomSetType *Result = nullptr;
166 std::vector<DFCalculateWorkObject<BlockT>> workList;
167 SmallPtrSet<BlockT *, 32> visited;
169 workList.push_back(DFCalculateWorkObject<BlockT>(BB, nullptr, Node, nullptr));
171 DFCalculateWorkObject<BlockT> *currentW = &workList.back();
172 assert(currentW && "Missing work object.");
174 BlockT *currentBB = currentW->currentBB;
175 BlockT *parentBB = currentW->parentBB;
176 const DomTreeNodeT *currentNode = currentW->Node;
177 const DomTreeNodeT *parentNode = currentW->parentNode;
178 assert(currentBB && "Invalid work object. Missing current Basic Block");
179 assert(currentNode && "Invalid work object. Missing current Node");
180 DomSetType &S = this->Frontiers[currentBB];
182 // Visit each block only once.
183 if (visited.insert(currentBB).second) {
184 // Loop over CFG successors to calculate DFlocal[currentNode]
185 for (const auto Succ : children<BlockT *>(currentBB)) {
186 // Does Node immediately dominate this successor?
187 if (DT[Succ]->getIDom() != currentNode)
192 // At this point, S is DFlocal. Now we union in DFup's of our children...
193 // Loop through and visit the nodes that Node immediately dominates (Node's
194 // children in the IDomTree)
195 bool visitChild = false;
196 for (typename DomTreeNodeT::const_iterator NI = currentNode->begin(),
197 NE = currentNode->end();
199 DomTreeNodeT *IDominee = *NI;
200 BlockT *childBB = IDominee->getBlock();
201 if (visited.count(childBB) == 0) {
202 workList.push_back(DFCalculateWorkObject<BlockT>(
203 childBB, currentBB, IDominee, currentNode));
208 // If all children are visited or there is any child then pop this block
209 // from the workList.
216 typename DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
217 DomSetType &parentSet = this->Frontiers[parentBB];
218 for (; CDFI != CDFE; ++CDFI) {
219 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
220 parentSet.insert(*CDFI);
225 } while (!workList.empty());
230 } // end namespace llvm
232 #endif // LLVM_ANALYSIS_DOMINANCEFRONTIERIMPL_H