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 file defines the DominanceFrontier class, which calculate and holds the
11 // dominance frontier for a function.
13 // This should be considered deprecated, don't add any more uses of this data
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ANALYSIS_DOMINANCEFRONTIER_H
19 #define LLVM_ANALYSIS_DOMINANCEFRONTIER_H
21 #include "llvm/IR/Dominators.h"
27 //===----------------------------------------------------------------------===//
28 /// DominanceFrontierBase - Common base class for computing forward and inverse
29 /// dominance frontiers for a function.
31 template <class BlockT>
32 class DominanceFrontierBase {
34 typedef std::set<BlockT *> DomSetType; // Dom set for a bb
35 typedef std::map<BlockT *, DomSetType> DomSetMapType; // Dom set map
38 typedef GraphTraits<BlockT *> BlockTraits;
40 DomSetMapType Frontiers;
41 std::vector<BlockT *> Roots;
42 const bool IsPostDominators;
45 DominanceFrontierBase(bool isPostDom) : IsPostDominators(isPostDom) {}
47 /// getRoots - Return the root blocks of the current CFG. This may include
48 /// multiple blocks if we are computing post dominators. For forward
49 /// dominators, this will always be a single block (the entry node).
51 inline const std::vector<BlockT *> &getRoots() const {
55 BlockT *getRoot() const {
56 assert(Roots.size() == 1 && "Should always have entry node!");
60 /// isPostDominator - Returns true if analysis based of postdoms
62 bool isPostDominator() const {
63 return IsPostDominators;
66 void releaseMemory() {
70 // Accessor interface:
71 typedef typename DomSetMapType::iterator iterator;
72 typedef typename DomSetMapType::const_iterator const_iterator;
73 iterator begin() { return Frontiers.begin(); }
74 const_iterator begin() const { return Frontiers.begin(); }
75 iterator end() { return Frontiers.end(); }
76 const_iterator end() const { return Frontiers.end(); }
77 iterator find(BlockT *B) { return Frontiers.find(B); }
78 const_iterator find(BlockT *B) const { return Frontiers.find(B); }
80 iterator addBasicBlock(BlockT *BB, const DomSetType &frontier) {
81 assert(find(BB) == end() && "Block already in DominanceFrontier!");
82 return Frontiers.insert(std::make_pair(BB, frontier)).first;
85 /// removeBlock - Remove basic block BB's frontier.
86 void removeBlock(BlockT *BB);
88 void addToFrontier(iterator I, BlockT *Node);
90 void removeFromFrontier(iterator I, BlockT *Node);
92 /// compareDomSet - Return false if two domsets match. Otherwise
94 bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const;
96 /// compare - Return true if the other dominance frontier base matches
97 /// this dominance frontier base. Otherwise return false.
98 bool compare(DominanceFrontierBase<BlockT> &Other) const;
100 /// print - Convert to human readable form
102 void print(raw_ostream &OS) const;
104 /// dump - Dump the dominance frontier to dbgs().
105 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
110 //===-------------------------------------
111 /// DominanceFrontier Class - Concrete subclass of DominanceFrontierBase that is
112 /// used to compute a forward dominator frontiers.
114 template <class BlockT>
115 class ForwardDominanceFrontierBase : public DominanceFrontierBase<BlockT> {
117 typedef GraphTraits<BlockT *> BlockTraits;
120 typedef DominatorTreeBase<BlockT> DomTreeT;
121 typedef DomTreeNodeBase<BlockT> DomTreeNodeT;
122 typedef typename DominanceFrontierBase<BlockT>::DomSetType DomSetType;
124 ForwardDominanceFrontierBase() : DominanceFrontierBase<BlockT>(false) {}
126 void analyze(DomTreeT &DT) {
127 this->Roots = DT.getRoots();
128 assert(this->Roots.size() == 1 &&
129 "Only one entry block for forward domfronts!");
130 calculate(DT, DT[this->Roots[0]]);
133 const DomSetType &calculate(const DomTreeT &DT, const DomTreeNodeT *Node);
136 class DominanceFrontier : public FunctionPass {
137 ForwardDominanceFrontierBase<BasicBlock> Base;
140 typedef DominatorTreeBase<BasicBlock> DomTreeT;
141 typedef DomTreeNodeBase<BasicBlock> DomTreeNodeT;
142 typedef DominanceFrontierBase<BasicBlock>::DomSetType DomSetType;
143 typedef DominanceFrontierBase<BasicBlock>::iterator iterator;
144 typedef DominanceFrontierBase<BasicBlock>::const_iterator const_iterator;
146 static char ID; // Pass ID, replacement for typeid
150 ForwardDominanceFrontierBase<BasicBlock> &getBase() { return Base; }
152 inline const std::vector<BasicBlock *> &getRoots() const {
153 return Base.getRoots();
156 BasicBlock *getRoot() const { return Base.getRoot(); }
158 bool isPostDominator() const { return Base.isPostDominator(); }
160 iterator begin() { return Base.begin(); }
162 const_iterator begin() const { return Base.begin(); }
164 iterator end() { return Base.end(); }
166 const_iterator end() const { return Base.end(); }
168 iterator find(BasicBlock *B) { return Base.find(B); }
170 const_iterator find(BasicBlock *B) const { return Base.find(B); }
172 iterator addBasicBlock(BasicBlock *BB, const DomSetType &frontier) {
173 return Base.addBasicBlock(BB, frontier);
176 void removeBlock(BasicBlock *BB) { return Base.removeBlock(BB); }
178 void addToFrontier(iterator I, BasicBlock *Node) {
179 return Base.addToFrontier(I, Node);
182 void removeFromFrontier(iterator I, BasicBlock *Node) {
183 return Base.removeFromFrontier(I, Node);
186 bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const {
187 return Base.compareDomSet(DS1, DS2);
190 bool compare(DominanceFrontierBase<BasicBlock> &Other) const {
191 return Base.compare(Other);
194 void releaseMemory() override;
196 bool runOnFunction(Function &) override;
198 void getAnalysisUsage(AnalysisUsage &AU) const override;
200 void print(raw_ostream &OS, const Module * = nullptr) const override;
205 extern template class DominanceFrontierBase<BasicBlock>;
206 extern template class ForwardDominanceFrontierBase<BasicBlock>;
208 } // End llvm namespace