DominanceFrontier.cpp revision 360784
1//===- DominanceFrontier.cpp - Dominance Frontier 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#include "llvm/Analysis/DominanceFrontier.h"
10#include "llvm/Analysis/DominanceFrontierImpl.h"
11#include "llvm/Config/llvm-config.h"
12#include "llvm/IR/Dominators.h"
13#include "llvm/IR/Function.h"
14#include "llvm/IR/PassManager.h"
15#include "llvm/InitializePasses.h"
16#include "llvm/Pass.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
23namespace llvm {
24
25template class DominanceFrontierBase<BasicBlock, false>;
26template class DominanceFrontierBase<BasicBlock, true>;
27template class ForwardDominanceFrontierBase<BasicBlock>;
28
29} // end namespace llvm
30
31char DominanceFrontierWrapperPass::ID = 0;
32
33INITIALIZE_PASS_BEGIN(DominanceFrontierWrapperPass, "domfrontier",
34                "Dominance Frontier Construction", true, true)
35INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
36INITIALIZE_PASS_END(DominanceFrontierWrapperPass, "domfrontier",
37                "Dominance Frontier Construction", true, true)
38
39DominanceFrontierWrapperPass::DominanceFrontierWrapperPass()
40    : FunctionPass(ID), DF() {
41  initializeDominanceFrontierWrapperPassPass(*PassRegistry::getPassRegistry());
42}
43
44void DominanceFrontierWrapperPass::releaseMemory() {
45  DF.releaseMemory();
46}
47
48bool DominanceFrontierWrapperPass::runOnFunction(Function &) {
49  releaseMemory();
50  DF.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
51  return false;
52}
53
54void DominanceFrontierWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
55  AU.setPreservesAll();
56  AU.addRequired<DominatorTreeWrapperPass>();
57}
58
59void DominanceFrontierWrapperPass::print(raw_ostream &OS, const Module *) const {
60  DF.print(OS);
61}
62
63#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
64LLVM_DUMP_METHOD void DominanceFrontierWrapperPass::dump() const {
65  print(dbgs());
66}
67#endif
68
69/// Handle invalidation explicitly.
70bool DominanceFrontier::invalidate(Function &F, const PreservedAnalyses &PA,
71                                   FunctionAnalysisManager::Invalidator &) {
72  // Check whether the analysis, all analyses on functions, or the function's
73  // CFG have been preserved.
74  auto PAC = PA.getChecker<DominanceFrontierAnalysis>();
75  return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
76           PAC.preservedSet<CFGAnalyses>());
77}
78
79AnalysisKey DominanceFrontierAnalysis::Key;
80
81DominanceFrontier DominanceFrontierAnalysis::run(Function &F,
82                                                 FunctionAnalysisManager &AM) {
83  DominanceFrontier DF;
84  DF.analyze(AM.getResult<DominatorTreeAnalysis>(F));
85  return DF;
86}
87
88DominanceFrontierPrinterPass::DominanceFrontierPrinterPass(raw_ostream &OS)
89  : OS(OS) {}
90
91PreservedAnalyses
92DominanceFrontierPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
93  OS << "DominanceFrontier for function: " << F.getName() << "\n";
94  AM.getResult<DominanceFrontierAnalysis>(F).print(OS);
95
96  return PreservedAnalyses::all();
97}
98