LLVM 23.0.0git
DominanceFrontier.h
Go to the documentation of this file.
1//===- llvm/Analysis/DominanceFrontier.h - Dominator Frontiers --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the DominanceFrontier class, which calculate and holds the
10// dominance frontier for a function.
11//
12// CAUTION: For SSA-construction-like problems there are more efficient ways to
13// do that, take a look at GenericIteratedDominanceFrontier.h/SSAUpdater.h. Also
14// note that that this analysis computes dominance frontiers for *every* block
15// which inherently increases complexity. Unless you do need *all* of them and
16// *without* any modifications to the DomTree/CFG in between queries there
17// should be better alternatives.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_ANALYSIS_DOMINANCEFRONTIER_H
22#define LLVM_ANALYSIS_DOMINANCEFRONTIER_H
23
24#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/SetVector.h"
27#include "llvm/IR/PassManager.h"
28#include "llvm/Pass.h"
30#include <cassert>
31
32namespace llvm {
33
34class BasicBlock;
35class Function;
36class raw_ostream;
37
38//===----------------------------------------------------------------------===//
39/// DominanceFrontierBase - Common base class for computing forward and inverse
40/// dominance frontiers for a function.
41///
42template <class BlockT, bool IsPostDom>
44public:
45 // Dom set for a bb. Use SetVector to make iterating dom frontiers of a bb
46 // deterministic.
51
52protected:
53 using GraphTy = std::conditional_t<IsPostDom, Inverse<BlockT *>, BlockT *>;
55
57 // Postdominators can have multiple roots.
59 static constexpr bool IsPostDominators = IsPostDom;
60
61public:
63
64 /// getRoots - Return the root blocks of the current CFG. This may include
65 /// multiple blocks if we are computing post dominators. For forward
66 /// dominators, this will always be a single block (the entry node).
67 const SmallVectorImpl<BlockT *> &getRoots() const { return Roots; }
68
69 BlockT *getRoot() const {
70 assert(Roots.size() == 1 && "Should always have entry node!");
71 return Roots[0];
72 }
73
74 /// isPostDominator - Returns true if analysis based of postdoms
75 bool isPostDominator() const {
76 return IsPostDominators;
77 }
78
80 Frontiers.clear();
81 }
82
83 // Accessor interface:
86
87 iterator begin() { return Frontiers.begin(); }
88 const_iterator begin() const { return Frontiers.begin(); }
89 iterator end() { return Frontiers.end(); }
90 const_iterator end() const { return Frontiers.end(); }
91 iterator find(BlockT *B) { return Frontiers.find(B); }
92 const_iterator find(BlockT *B) const { return Frontiers.find(B); }
93
94 /// print - Convert to human readable form
95 ///
96 void print(raw_ostream &OS) const;
97
98 /// dump - Dump the dominance frontier to dbgs().
99#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
100 void dump() const;
101#endif
102
103 void analyze(DomTreeT &DT) {
104 assert((IsPostDom || DT.root_size() == 1) &&
105 "Only one entry block for forward domfronts!");
106 assert(DT.root_size() == 1 &&
107 "Support for post-dom frontiers with multiple roots hasn't been "
108 "implemented yet!");
109 this->Roots = {DT.getRoot()};
110 calculate(DT, DT[this->Roots[0]]);
111 }
112
113 void calculate(const DomTreeT &DT, const DomTreeNodeT *Node);
114};
115
116class DominanceFrontier : public DominanceFrontierBase<BasicBlock, false> {
117public:
123
124 /// Handle invalidation explicitly.
125 bool invalidate(Function &F, const PreservedAnalyses &PA,
126 FunctionAnalysisManager::Invalidator &);
127};
128
131
132public:
133 static char ID; // Pass ID, replacement for typeid
134
136
138 const DominanceFrontier &getDominanceFrontier() const { return DF; }
139
140 void releaseMemory() override;
141
142 bool runOnFunction(Function &) override;
143
144 void getAnalysisUsage(AnalysisUsage &AU) const override;
145
146 void print(raw_ostream &OS, const Module * = nullptr) const override;
147
148 void dump() const;
149};
150
151extern template class DominanceFrontierBase<BasicBlock, false>;
152extern template class DominanceFrontierBase<BasicBlock, true>;
153
154/// Analysis pass which computes a \c DominanceFrontier.
158
159 static AnalysisKey Key;
160
161public:
162 /// Provide the result type for this analysis pass.
164
165 /// Run the analysis pass over a function and produce a dominator tree.
167};
168
169/// Printer pass for the \c DominanceFrontier.
172 raw_ostream &OS;
173
174public:
176
178
179 static bool isRequired() { return true; }
180};
181
182} // end namespace llvm
183
184#endif // LLVM_ANALYSIS_DOMINANCEFRONTIER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
This file implements a set that has insertion order iteration characteristics.
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
Base class for the actual dominator tree node.
Analysis pass which computes a DominanceFrontier.
DominanceFrontier Result
Provide the result type for this analysis pass.
DominanceFrontier run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce a dominator tree.
std::conditional_t< IsPostDom, Inverse< BlockT * >, BlockT * > GraphTy
DominatorTreeBase< BlockT, IsPostDom > DomTreeT
void print(raw_ostream &OS) const
print - Convert to human readable form
const_iterator find(BlockT *B) const
void calculate(const DomTreeT &DT, const DomTreeNodeT *Node)
typename DomSetMapType::iterator iterator
SmallVector< BlockT *, IsPostDom ? 4 :1 > Roots
const_iterator end() const
const SmallVectorImpl< BlockT * > & getRoots() const
getRoots - Return the root blocks of the current CFG.
static constexpr bool IsPostDominators
void dump() const
dump - Dump the dominance frontier to dbgs().
bool isPostDominator() const
isPostDominator - Returns true if analysis based of postdoms
DenseMap< BlockT *, DomSetType > DomSetMapType
DomTreeNodeBase< BlockT > DomTreeNodeT
GraphTraits< GraphTy > BlockTraits
SetVector< BlockT * > DomSetType
typename DomSetMapType::const_iterator const_iterator
const_iterator begin() const
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const DominanceFrontier & getDominanceFrontier() const
bool runOnFunction(Function &) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
DominanceFrontier & getDominanceFrontier()
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
DominanceFrontier::const_iterator const_iterator
DomTreeNodeBase< BasicBlock > DomTreeNodeT
DomTreeBase< BasicBlock > DomTreeT
DominanceFrontier::iterator iterator
DominanceFrontier::DomSetType DomSetType
Core dominator tree base class.
FunctionPass(char &pid)
Definition Pass.h:316
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
A vector that has set insertion semantics.
Definition SetVector.h:57
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
DominatorTreeBase< T, false > DomTreeBase
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70