PostDominators.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
  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 implements the post-dominator construction algorithms.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Analysis/PostDominators.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/IR/PassManager.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "postdomtree"
  21. #ifdef EXPENSIVE_CHECKS
  22. static constexpr bool ExpensiveChecksEnabled = true;
  23. #else
  24. static constexpr bool ExpensiveChecksEnabled = false;
  25. #endif
  26. //===----------------------------------------------------------------------===//
  27. // PostDominatorTree Implementation
  28. //===----------------------------------------------------------------------===//
  29. char PostDominatorTreeWrapperPass::ID = 0;
  30. PostDominatorTreeWrapperPass::PostDominatorTreeWrapperPass()
  31. : FunctionPass(ID) {
  32. initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
  33. }
  34. INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree",
  35. "Post-Dominator Tree Construction", true, true)
  36. bool PostDominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
  37. FunctionAnalysisManager::Invalidator &) {
  38. // Check whether the analysis, all analyses on functions, or the function's
  39. // CFG have been preserved.
  40. auto PAC = PA.getChecker<PostDominatorTreeAnalysis>();
  41. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  42. PAC.preservedSet<CFGAnalyses>());
  43. }
  44. bool PostDominatorTree::dominates(const Instruction *I1,
  45. const Instruction *I2) const {
  46. assert(I1 && I2 && "Expecting valid I1 and I2");
  47. const BasicBlock *BB1 = I1->getParent();
  48. const BasicBlock *BB2 = I2->getParent();
  49. if (BB1 != BB2)
  50. return Base::dominates(BB1, BB2);
  51. // PHINodes in a block are unordered.
  52. if (isa<PHINode>(I1) && isa<PHINode>(I2))
  53. return false;
  54. // Loop through the basic block until we find I1 or I2.
  55. BasicBlock::const_iterator I = BB1->begin();
  56. for (; &*I != I1 && &*I != I2; ++I)
  57. /*empty*/;
  58. return &*I == I2;
  59. }
  60. bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
  61. DT.recalculate(F);
  62. return false;
  63. }
  64. void PostDominatorTreeWrapperPass::verifyAnalysis() const {
  65. if (VerifyDomInfo)
  66. assert(DT.verify(PostDominatorTree::VerificationLevel::Full));
  67. else if (ExpensiveChecksEnabled)
  68. assert(DT.verify(PostDominatorTree::VerificationLevel::Basic));
  69. }
  70. void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
  71. DT.print(OS);
  72. }
  73. FunctionPass* llvm::createPostDomTree() {
  74. return new PostDominatorTreeWrapperPass();
  75. }
  76. AnalysisKey PostDominatorTreeAnalysis::Key;
  77. PostDominatorTree PostDominatorTreeAnalysis::run(Function &F,
  78. FunctionAnalysisManager &) {
  79. PostDominatorTree PDT(F);
  80. return PDT;
  81. }
  82. PostDominatorTreePrinterPass::PostDominatorTreePrinterPass(raw_ostream &OS)
  83. : OS(OS) {}
  84. PreservedAnalyses
  85. PostDominatorTreePrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
  86. OS << "PostDominatorTree for function: " << F.getName() << "\n";
  87. AM.getResult<PostDominatorTreeAnalysis>(F).print(OS);
  88. return PreservedAnalyses::all();
  89. }