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