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 
13 #include "llvm/Analysis/PostDominators.h"
14 #include "llvm/IR/Function.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 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "postdomtree"
23 
24 #ifdef EXPENSIVE_CHECKS
25 static constexpr bool ExpensiveChecksEnabled = true;
26 #else
27 static constexpr bool ExpensiveChecksEnabled = false;
28 #endif
29 
30 //===----------------------------------------------------------------------===//
31 //  PostDominatorTree Implementation
32 //===----------------------------------------------------------------------===//
33 
34 char PostDominatorTreeWrapperPass::ID = 0;
35 
36 PostDominatorTreeWrapperPass::PostDominatorTreeWrapperPass()
37     : FunctionPass(ID) {
38   initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
39 }
40 
41 INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree",
42                 "Post-Dominator Tree Construction", true, true)
43 
44 bool PostDominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
45                                    FunctionAnalysisManager::Invalidator &) {
46   // Check whether the analysis, all analyses on functions, or the function's
47   // CFG have been preserved.
48   auto PAC = PA.getChecker<PostDominatorTreeAnalysis>();
49   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
50            PAC.preservedSet<CFGAnalyses>());
51 }
52 
53 bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
54   DT.recalculate(F);
55   return false;
56 }
57 
58 void PostDominatorTreeWrapperPass::verifyAnalysis() const {
59   if (VerifyDomInfo)
60     assert(DT.verify(PostDominatorTree::VerificationLevel::Full));
61   else if (ExpensiveChecksEnabled)
62     assert(DT.verify(PostDominatorTree::VerificationLevel::Basic));
63 }
64 
65 void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
66   DT.print(OS);
67 }
68 
69 FunctionPass* llvm::createPostDomTree() {
70   return new PostDominatorTreeWrapperPass();
71 }
72 
73 AnalysisKey PostDominatorTreeAnalysis::Key;
74 
75 PostDominatorTree PostDominatorTreeAnalysis::run(Function &F,
76                                                  FunctionAnalysisManager &) {
77   PostDominatorTree PDT(F);
78   return PDT;
79 }
80 
81 PostDominatorTreePrinterPass::PostDominatorTreePrinterPass(raw_ostream &OS)
82   : OS(OS) {}
83 
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 
89   return PreservedAnalyses::all();
90 }
91