PostDominators.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=- llvm/Analysis/PostDominators.h - Post Dominator Calculation --*- 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 exposes interfaces to post dominance information.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_POSTDOMINATORS_H
  18. #define LLVM_ANALYSIS_POSTDOMINATORS_H
  19. #include "llvm/ADT/DepthFirstIterator.h"
  20. #include "llvm/IR/Dominators.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/Pass.h"
  23. namespace llvm {
  24. class Function;
  25. class raw_ostream;
  26. /// PostDominatorTree Class - Concrete subclass of DominatorTree that is used to
  27. /// compute the post-dominator tree.
  28. class PostDominatorTree : public PostDomTreeBase<BasicBlock> {
  29. public:
  30. using Base = PostDomTreeBase<BasicBlock>;
  31. PostDominatorTree() = default;
  32. explicit PostDominatorTree(Function &F) { recalculate(F); }
  33. /// Handle invalidation explicitly.
  34. bool invalidate(Function &F, const PreservedAnalyses &PA,
  35. FunctionAnalysisManager::Invalidator &);
  36. // Ensure base-class overloads are visible.
  37. using Base::dominates;
  38. /// Return true if \p I1 dominates \p I2. This checks if \p I2 comes before
  39. /// \p I1 if they belongs to the same basic block.
  40. bool dominates(const Instruction *I1, const Instruction *I2) const;
  41. };
  42. /// Analysis pass which computes a \c PostDominatorTree.
  43. class PostDominatorTreeAnalysis
  44. : public AnalysisInfoMixin<PostDominatorTreeAnalysis> {
  45. friend AnalysisInfoMixin<PostDominatorTreeAnalysis>;
  46. static AnalysisKey Key;
  47. public:
  48. /// Provide the result type for this analysis pass.
  49. using Result = PostDominatorTree;
  50. /// Run the analysis pass over a function and produce a post dominator
  51. /// tree.
  52. PostDominatorTree run(Function &F, FunctionAnalysisManager &);
  53. };
  54. /// Printer pass for the \c PostDominatorTree.
  55. class PostDominatorTreePrinterPass
  56. : public PassInfoMixin<PostDominatorTreePrinterPass> {
  57. raw_ostream &OS;
  58. public:
  59. explicit PostDominatorTreePrinterPass(raw_ostream &OS);
  60. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  61. };
  62. struct PostDominatorTreeWrapperPass : public FunctionPass {
  63. static char ID; // Pass identification, replacement for typeid
  64. PostDominatorTree DT;
  65. PostDominatorTreeWrapperPass();
  66. PostDominatorTree &getPostDomTree() { return DT; }
  67. const PostDominatorTree &getPostDomTree() const { return DT; }
  68. bool runOnFunction(Function &F) override;
  69. void verifyAnalysis() const override;
  70. void getAnalysisUsage(AnalysisUsage &AU) const override {
  71. AU.setPreservesAll();
  72. }
  73. void releaseMemory() override { DT.reset(); }
  74. void print(raw_ostream &OS, const Module*) const override;
  75. };
  76. FunctionPass* createPostDomTree();
  77. template <> struct GraphTraits<PostDominatorTree*>
  78. : public GraphTraits<DomTreeNode*> {
  79. static NodeRef getEntryNode(PostDominatorTree *DT) {
  80. return DT->getRootNode();
  81. }
  82. static nodes_iterator nodes_begin(PostDominatorTree *N) {
  83. return df_begin(getEntryNode(N));
  84. }
  85. static nodes_iterator nodes_end(PostDominatorTree *N) {
  86. return df_end(getEntryNode(N));
  87. }
  88. };
  89. } // end namespace llvm
  90. #endif // LLVM_ANALYSIS_POSTDOMINATORS_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif