DominanceFrontier.h 6.8 KB

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