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