DominanceFrontier.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // This should be considered deprecated, don't add any more uses of this data
  13. // structure.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_ANALYSIS_DOMINANCEFRONTIER_H
  17. #define LLVM_ANALYSIS_DOMINANCEFRONTIER_H
  18. #include "llvm/ADT/GraphTraits.h"
  19. #include "llvm/Config/llvm-config.h"
  20. #include "llvm/IR/PassManager.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/GenericDomTree.h"
  23. #include <cassert>
  24. #include <map>
  25. #include <set>
  26. #include <utility>
  27. namespace llvm {
  28. class Function;
  29. class raw_ostream;
  30. //===----------------------------------------------------------------------===//
  31. /// DominanceFrontierBase - Common base class for computing forward and inverse
  32. /// dominance frontiers for a function.
  33. ///
  34. template <class BlockT, bool IsPostDom>
  35. class DominanceFrontierBase {
  36. public:
  37. using DomSetType = std::set<BlockT *>; // Dom set for a bb
  38. using DomSetMapType = std::map<BlockT *, DomSetType>; // Dom set map
  39. protected:
  40. using BlockTraits = GraphTraits<BlockT *>;
  41. DomSetMapType Frontiers;
  42. // Postdominators can have multiple roots.
  43. SmallVector<BlockT *, IsPostDom ? 4 : 1> Roots;
  44. static constexpr bool IsPostDominators = IsPostDom;
  45. public:
  46. DominanceFrontierBase() = default;
  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).
  50. const SmallVectorImpl<BlockT *> &getRoots() const { return Roots; }
  51. BlockT *getRoot() const {
  52. assert(Roots.size() == 1 && "Should always have entry node!");
  53. return Roots[0];
  54. }
  55. /// isPostDominator - Returns true if analysis based of postdoms
  56. bool isPostDominator() const {
  57. return IsPostDominators;
  58. }
  59. void releaseMemory() {
  60. Frontiers.clear();
  61. }
  62. // Accessor interface:
  63. using iterator = typename DomSetMapType::iterator;
  64. using const_iterator = typename DomSetMapType::const_iterator;
  65. iterator begin() { return Frontiers.begin(); }
  66. const_iterator begin() const { return Frontiers.begin(); }
  67. iterator end() { return Frontiers.end(); }
  68. const_iterator end() const { return Frontiers.end(); }
  69. iterator find(BlockT *B) { return Frontiers.find(B); }
  70. const_iterator find(BlockT *B) const { return Frontiers.find(B); }
  71. iterator addBasicBlock(BlockT *BB, const DomSetType &frontier) {
  72. assert(find(BB) == end() && "Block already in DominanceFrontier!");
  73. return Frontiers.insert(std::make_pair(BB, frontier)).first;
  74. }
  75. /// removeBlock - Remove basic block BB's frontier.
  76. void removeBlock(BlockT *BB);
  77. void addToFrontier(iterator I, BlockT *Node);
  78. void removeFromFrontier(iterator I, BlockT *Node);
  79. /// compareDomSet - Return false if two domsets match. Otherwise
  80. /// return true;
  81. bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const;
  82. /// compare - Return true if the other dominance frontier base matches
  83. /// this dominance frontier base. Otherwise return false.
  84. bool compare(DominanceFrontierBase &Other) const;
  85. /// print - Convert to human readable form
  86. ///
  87. void print(raw_ostream &OS) const;
  88. /// dump - Dump the dominance frontier to dbgs().
  89. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  90. void dump() const;
  91. #endif
  92. };
  93. //===-------------------------------------
  94. /// DominanceFrontier Class - Concrete subclass of DominanceFrontierBase that is
  95. /// used to compute a forward dominator frontiers.
  96. ///
  97. template <class BlockT>
  98. class ForwardDominanceFrontierBase
  99. : public DominanceFrontierBase<BlockT, false> {
  100. private:
  101. using BlockTraits = GraphTraits<BlockT *>;
  102. public:
  103. using DomTreeT = DomTreeBase<BlockT>;
  104. using DomTreeNodeT = DomTreeNodeBase<BlockT>;
  105. using DomSetType = typename DominanceFrontierBase<BlockT, false>::DomSetType;
  106. void analyze(DomTreeT &DT) {
  107. assert(DT.root_size() == 1 &&
  108. "Only one entry block for forward domfronts!");
  109. this->Roots = {DT.getRoot()};
  110. calculate(DT, DT[this->Roots[0]]);
  111. }
  112. const DomSetType &calculate(const DomTreeT &DT, const DomTreeNodeT *Node);
  113. };
  114. class DominanceFrontier : public ForwardDominanceFrontierBase<BasicBlock> {
  115. public:
  116. using DomTreeT = DomTreeBase<BasicBlock>;
  117. using DomTreeNodeT = DomTreeNodeBase<BasicBlock>;
  118. using DomSetType = DominanceFrontierBase<BasicBlock, false>::DomSetType;
  119. using iterator = DominanceFrontierBase<BasicBlock, false>::iterator;
  120. using const_iterator =
  121. DominanceFrontierBase<BasicBlock, false>::const_iterator;
  122. /// Handle invalidation explicitly.
  123. bool invalidate(Function &F, const PreservedAnalyses &PA,
  124. FunctionAnalysisManager::Invalidator &);
  125. };
  126. class DominanceFrontierWrapperPass : public FunctionPass {
  127. DominanceFrontier DF;
  128. public:
  129. static char ID; // Pass ID, replacement for typeid
  130. DominanceFrontierWrapperPass();
  131. DominanceFrontier &getDominanceFrontier() { return DF; }
  132. const DominanceFrontier &getDominanceFrontier() const { return DF; }
  133. void releaseMemory() override;
  134. bool runOnFunction(Function &) override;
  135. void getAnalysisUsage(AnalysisUsage &AU) const override;
  136. void print(raw_ostream &OS, const Module * = nullptr) const override;
  137. void dump() const;
  138. };
  139. extern template class DominanceFrontierBase<BasicBlock, false>;
  140. extern template class DominanceFrontierBase<BasicBlock, true>;
  141. extern template class ForwardDominanceFrontierBase<BasicBlock>;
  142. /// Analysis pass which computes a \c DominanceFrontier.
  143. class DominanceFrontierAnalysis
  144. : public AnalysisInfoMixin<DominanceFrontierAnalysis> {
  145. friend AnalysisInfoMixin<DominanceFrontierAnalysis>;
  146. static AnalysisKey Key;
  147. public:
  148. /// Provide the result type for this analysis pass.
  149. using Result = DominanceFrontier;
  150. /// Run the analysis pass over a function and produce a dominator tree.
  151. DominanceFrontier run(Function &F, FunctionAnalysisManager &AM);
  152. };
  153. /// Printer pass for the \c DominanceFrontier.
  154. class DominanceFrontierPrinterPass
  155. : public PassInfoMixin<DominanceFrontierPrinterPass> {
  156. raw_ostream &OS;
  157. public:
  158. explicit DominanceFrontierPrinterPass(raw_ostream &OS);
  159. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  160. };
  161. } // end namespace llvm
  162. #endif // LLVM_ANALYSIS_DOMINANCEFRONTIER_H