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