| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_IR_DOMINATORS_H |
| 15 |
#define LLVM_IR_DOMINATORS_H |
| 16 |
|
| 17 |
#include "llvm/ADT/ArrayRef.h" |
| 18 |
#include "llvm/ADT/DenseMap.h" |
| 19 |
#include "llvm/ADT/DenseMapInfo.h" |
| 20 |
#include "llvm/ADT/DepthFirstIterator.h" |
| 21 |
#include "llvm/ADT/Hashing.h" |
| 22 |
#include "llvm/ADT/PointerIntPair.h" |
| 23 |
#include "llvm/ADT/SmallVector.h" |
| 24 |
#include "llvm/ADT/Twine.h" |
| 25 |
#include "llvm/IR/BasicBlock.h" |
| 26 |
#include "llvm/IR/CFG.h" |
| 27 |
#include "llvm/IR/PassManager.h" |
| 28 |
#include "llvm/IR/Use.h" |
| 29 |
#include "llvm/Pass.h" |
| 30 |
#include "llvm/Support/CFGDiff.h" |
| 31 |
#include "llvm/Support/CFGUpdate.h" |
| 32 |
#include "llvm/Support/GenericDomTree.h" |
| 33 |
#include "llvm/Support/GenericDomTreeConstruction.h" |
| 34 |
#include <utility> |
| 35 |
#include <vector> |
| 36 |
|
| 37 |
namespace llvm { |
| 38 |
|
| 39 |
class Function; |
| 40 |
class Instruction; |
| 41 |
class Module; |
| 42 |
class Value; |
| 43 |
class raw_ostream; |
| 44 |
template <class GraphType> struct GraphTraits; |
| 45 |
|
| 46 |
extern template class DomTreeNodeBase<BasicBlock>; |
| 47 |
extern template class DominatorTreeBase<BasicBlock, false>; |
| 48 |
extern template class DominatorTreeBase<BasicBlock, true>; |
| 49 |
|
| 50 |
extern template class cfg::Update<BasicBlock *>; |
| 51 |
|
| 52 |
namespace DomTreeBuilder { |
| 53 |
using BBDomTree = DomTreeBase<BasicBlock>; |
| 54 |
using BBPostDomTree = PostDomTreeBase<BasicBlock>; |
| 55 |
|
| 56 |
using BBUpdates = ArrayRef<llvm::cfg::Update<BasicBlock *>>; |
| 57 |
|
| 58 |
using BBDomTreeGraphDiff = GraphDiff<BasicBlock *, false>; |
| 59 |
using BBPostDomTreeGraphDiff = GraphDiff<BasicBlock *, true>; |
| 60 |
|
| 61 |
extern template void Calculate<BBDomTree>(BBDomTree &DT); |
| 62 |
extern template void CalculateWithUpdates<BBDomTree>(BBDomTree &DT, |
| 63 |
BBUpdates U); |
| 64 |
|
| 65 |
extern template void Calculate<BBPostDomTree>(BBPostDomTree &DT); |
| 66 |
|
| 67 |
extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From, |
| 68 |
BasicBlock *To); |
| 69 |
extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT, |
| 70 |
BasicBlock *From, |
| 71 |
BasicBlock *To); |
| 72 |
|
| 73 |
extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From, |
| 74 |
BasicBlock *To); |
| 75 |
extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT, |
| 76 |
BasicBlock *From, |
| 77 |
BasicBlock *To); |
| 78 |
|
| 79 |
extern template void ApplyUpdates<BBDomTree>(BBDomTree &DT, |
| 80 |
BBDomTreeGraphDiff &, |
| 81 |
BBDomTreeGraphDiff *); |
| 82 |
extern template void ApplyUpdates<BBPostDomTree>(BBPostDomTree &DT, |
| 83 |
BBPostDomTreeGraphDiff &, |
| 84 |
BBPostDomTreeGraphDiff *); |
| 85 |
|
| 86 |
extern template bool Verify<BBDomTree>(const BBDomTree &DT, |
| 87 |
BBDomTree::VerificationLevel VL); |
| 88 |
extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT, |
| 89 |
BBPostDomTree::VerificationLevel VL); |
| 90 |
} |
| 91 |
|
| 92 |
using DomTreeNode = DomTreeNodeBase<BasicBlock>; |
| 93 |
|
| 94 |
class BasicBlockEdge { |
| 95 |
const BasicBlock *Start; |
| 96 |
const BasicBlock *End; |
| 97 |
|
| 98 |
public: |
| 99 |
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) : |
| 100 |
Start(Start_), End(End_) {} |
| 101 |
|
| 102 |
BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair) |
| 103 |
: Start(Pair.first), End(Pair.second) {} |
| 104 |
|
| 105 |
BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair) |
| 106 |
: Start(Pair.first), End(Pair.second) {} |
| 107 |
|
| 108 |
const BasicBlock *getStart() const { |
| 109 |
return Start; |
| 110 |
} |
| 111 |
|
| 112 |
const BasicBlock *getEnd() const { |
| 113 |
return End; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
bool isSingleEdge() const; |
| 118 |
}; |
| 119 |
|
| 120 |
template <> struct DenseMapInfo<BasicBlockEdge> { |
| 121 |
using BBInfo = DenseMapInfo<const BasicBlock *>; |
| 122 |
|
| 123 |
static unsigned getHashValue(const BasicBlockEdge *V); |
| 124 |
|
| 125 |
static inline BasicBlockEdge getEmptyKey() { |
| 126 |
return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey()); |
| 127 |
} |
| 128 |
|
| 129 |
static inline BasicBlockEdge getTombstoneKey() { |
| 130 |
return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey()); |
| 131 |
} |
| 132 |
|
| 133 |
static unsigned getHashValue(const BasicBlockEdge &Edge) { |
| 134 |
return hash_combine(BBInfo::getHashValue(Edge.getStart()), |
| 135 |
BBInfo::getHashValue(Edge.getEnd())); |
| 136 |
} |
| 137 |
|
| 138 |
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) { |
| 139 |
return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) && |
| 140 |
BBInfo::isEqual(LHS.getEnd(), RHS.getEnd()); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
class DominatorTree : public DominatorTreeBase<BasicBlock, false> { |
| 163 |
public: |
| 164 |
using Base = DominatorTreeBase<BasicBlock, false>; |
| 165 |
|
| 166 |
DominatorTree() = default; |
| 167 |
explicit DominatorTree(Function &F) { recalculate(F); } |
| 168 |
explicit DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U) { |
| 169 |
recalculate(*DT.Parent, U); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
bool invalidate(Function &F, const PreservedAnalyses &PA, |
| 174 |
FunctionAnalysisManager::Invalidator &); |
| 175 |
|
| 176 |
|
| 177 |
using Base::dominates; |
| 178 |
|
| 179 |
|
| 180 |
bool dominates(const BasicBlock *BB, const Use &U) const; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
bool dominates(const Value *Def, const Use &U) const; |
| 192 |
|
| 193 |
|
| 194 |
bool dominates(const Value *Def, const Instruction *User) const; |
| 195 |
|
| 196 |
|
| 197 |
bool dominates(const Instruction *Def, const BasicBlock *BB) const; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
bool dominates(const BasicBlockEdge &BBE, const Use &U) const; |
| 204 |
bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const; |
| 205 |
|
| 206 |
bool dominates(const BasicBlockEdge &BBE1, const BasicBlockEdge &BBE2) const; |
| 207 |
|
| 208 |
|
| 209 |
using Base::isReachableFromEntry; |
| 210 |
|
| 211 |
|
| 212 |
bool isReachableFromEntry(const Use &U) const; |
| 213 |
|
| 214 |
|
| 215 |
void viewGraph(const Twine &Name, const Twine &Title); |
| 216 |
void viewGraph(); |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase { |
| 224 |
using NodeRef = Node *; |
| 225 |
using ChildIteratorType = ChildIterator; |
| 226 |
using nodes_iterator = df_iterator<Node *, df_iterator_default_set<Node*>>; |
| 227 |
|
| 228 |
static NodeRef getEntryNode(NodeRef N) { return N; } |
| 229 |
static ChildIteratorType child_begin(NodeRef N) { return N->begin(); } |
| 230 |
static ChildIteratorType child_end(NodeRef N) { return N->end(); } |
| 231 |
|
| 232 |
static nodes_iterator nodes_begin(NodeRef N) { |
| 233 |
return df_begin(getEntryNode(N)); |
| 234 |
} |
| 235 |
|
| 236 |
static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); } |
| 237 |
}; |
| 238 |
|
| 239 |
template <> |
| 240 |
struct GraphTraits<DomTreeNode *> |
| 241 |
: public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::const_iterator> { |
| 242 |
}; |
| 243 |
|
| 244 |
template <> |
| 245 |
struct GraphTraits<const DomTreeNode *> |
| 246 |
: public DomTreeGraphTraitsBase<const DomTreeNode, |
| 247 |
DomTreeNode::const_iterator> {}; |
| 248 |
|
| 249 |
template <> struct GraphTraits<DominatorTree*> |
| 250 |
: public GraphTraits<DomTreeNode*> { |
| 251 |
static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); } |
| 252 |
|
| 253 |
static nodes_iterator nodes_begin(DominatorTree *N) { |
| 254 |
return df_begin(getEntryNode(N)); |
| 255 |
} |
| 256 |
|
| 257 |
static nodes_iterator nodes_end(DominatorTree *N) { |
| 258 |
return df_end(getEntryNode(N)); |
| 259 |
} |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> { |
| 264 |
friend AnalysisInfoMixin<DominatorTreeAnalysis>; |
| 265 |
static AnalysisKey Key; |
| 266 |
|
| 267 |
public: |
| 268 |
|
| 269 |
using Result = DominatorTree; |
| 270 |
|
| 271 |
|
| 272 |
DominatorTree run(Function &F, FunctionAnalysisManager &); |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
class DominatorTreePrinterPass |
| 277 |
: public PassInfoMixin<DominatorTreePrinterPass> { |
| 278 |
raw_ostream &OS; |
| 279 |
|
| 280 |
public: |
| 281 |
explicit DominatorTreePrinterPass(raw_ostream &OS); |
| 282 |
|
| 283 |
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> { |
| 288 |
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
extern bool VerifyDomInfo; |
| 296 |
|
| 297 |
|
| 298 |
class DominatorTreeWrapperPass : public FunctionPass { |
| 299 |
DominatorTree DT; |
| 300 |
|
| 301 |
public: |
| 302 |
static char ID; |
| 303 |
|
| 304 |
DominatorTreeWrapperPass(); |
| 305 |
|
| 306 |
DominatorTree &getDomTree() { return DT; } |
| 307 |
const DominatorTree &getDomTree() const { return DT; } |
| 308 |
|
| 309 |
bool runOnFunction(Function &F) override; |
| 310 |
|
| 311 |
void verifyAnalysis() const override; |
| 312 |
|
| 313 |
void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 314 |
AU.setPreservesAll(); |
| 315 |
} |
| 316 |
|
| 317 |
void releaseMemory() override { DT.reset(); } |
| 318 |
|
| 319 |
void print(raw_ostream &OS, const Module *M = nullptr) const override; |
| 320 |
}; |
| 321 |
} |
| 322 |
|
| 323 |
#endif |