blob: d713ae44772e9858d711f67f1c04ab298e31195a [file] [log] [blame]
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +00001//===- DominanceFrontier.cpp - Dominance Frontier Calculation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DominanceFrontier.h"
Matt Arsenault4181ea32014-07-12 21:59:52 +000011#include "llvm/Analysis/DominanceFrontierImpl.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000012#include "llvm/IR/Dominators.h"
13#include "llvm/IR/Function.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000014#include "llvm/IR/PassManager.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000015#include "llvm/Pass.h"
16#include "llvm/Support/Compiler.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
Matt Arsenault4181ea32014-07-12 21:59:52 +000019
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000020using namespace llvm;
21
Matt Arsenault4181ea32014-07-12 21:59:52 +000022namespace llvm {
Eugene Zelenko48666a62017-07-24 23:16:33 +000023
Jakub Kuderskib292c222017-07-14 18:26:09 +000024template class DominanceFrontierBase<BasicBlock, false>;
25template class DominanceFrontierBase<BasicBlock, true>;
Matt Arsenault4181ea32014-07-12 21:59:52 +000026template class ForwardDominanceFrontierBase<BasicBlock>;
Eugene Zelenko48666a62017-07-24 23:16:33 +000027
28} // end namespace llvm
Matt Arsenault4181ea32014-07-12 21:59:52 +000029
Hongbin Zheng751337f2016-02-25 17:54:15 +000030char DominanceFrontierWrapperPass::ID = 0;
Matt Arsenault4181ea32014-07-12 21:59:52 +000031
Hongbin Zheng751337f2016-02-25 17:54:15 +000032INITIALIZE_PASS_BEGIN(DominanceFrontierWrapperPass, "domfrontier",
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000033 "Dominance Frontier Construction", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +000034INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng751337f2016-02-25 17:54:15 +000035INITIALIZE_PASS_END(DominanceFrontierWrapperPass, "domfrontier",
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000036 "Dominance Frontier Construction", true, true)
37
Eugene Zelenko48666a62017-07-24 23:16:33 +000038DominanceFrontierWrapperPass::DominanceFrontierWrapperPass()
Hongbin Zheng751337f2016-02-25 17:54:15 +000039 : FunctionPass(ID), DF() {
40 initializeDominanceFrontierWrapperPassPass(*PassRegistry::getPassRegistry());
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000041}
42
Hongbin Zheng751337f2016-02-25 17:54:15 +000043void DominanceFrontierWrapperPass::releaseMemory() {
44 DF.releaseMemory();
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000045}
46
Hongbin Zheng751337f2016-02-25 17:54:15 +000047bool DominanceFrontierWrapperPass::runOnFunction(Function &) {
Matt Arsenault4181ea32014-07-12 21:59:52 +000048 releaseMemory();
Hongbin Zheng751337f2016-02-25 17:54:15 +000049 DF.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
Matt Arsenault4181ea32014-07-12 21:59:52 +000050 return false;
51}
52
Hongbin Zheng751337f2016-02-25 17:54:15 +000053void DominanceFrontierWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Matt Arsenault4181ea32014-07-12 21:59:52 +000054 AU.setPreservesAll();
55 AU.addRequired<DominatorTreeWrapperPass>();
56}
57
Hongbin Zheng751337f2016-02-25 17:54:15 +000058void DominanceFrontierWrapperPass::print(raw_ostream &OS, const Module *) const {
59 DF.print(OS);
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000060}
61
Don Hinton3e0199f2017-10-12 16:16:06 +000062#ifdef LLVM_ENABLE_DUMP
Hongbin Zheng751337f2016-02-25 17:54:15 +000063LLVM_DUMP_METHOD void DominanceFrontierWrapperPass::dump() const {
Cameron Zwarich6b0c4c92011-01-18 06:06:27 +000064 print(dbgs());
65}
Manman Renc3366cc2012-09-06 19:55:56 +000066#endif
Hongbin Zheng751337f2016-02-25 17:54:15 +000067
Chandler Carruthca68a3e2017-01-15 06:32:49 +000068/// Handle invalidation explicitly.
69bool DominanceFrontier::invalidate(Function &F, const PreservedAnalyses &PA,
70 FunctionAnalysisManager::Invalidator &) {
71 // Check whether the analysis, all analyses on functions, or the function's
72 // CFG have been preserved.
73 auto PAC = PA.getChecker<DominanceFrontierAnalysis>();
74 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
75 PAC.preservedSet<CFGAnalyses>());
76}
77
Chandler Carruthdab4eae2016-11-23 17:53:26 +000078AnalysisKey DominanceFrontierAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +000079
Hongbin Zheng751337f2016-02-25 17:54:15 +000080DominanceFrontier DominanceFrontierAnalysis::run(Function &F,
Chandler Carruthb47f8012016-03-11 11:05:24 +000081 FunctionAnalysisManager &AM) {
Hongbin Zheng751337f2016-02-25 17:54:15 +000082 DominanceFrontier DF;
Chandler Carruthb47f8012016-03-11 11:05:24 +000083 DF.analyze(AM.getResult<DominatorTreeAnalysis>(F));
Hongbin Zheng751337f2016-02-25 17:54:15 +000084 return DF;
85}
86
87DominanceFrontierPrinterPass::DominanceFrontierPrinterPass(raw_ostream &OS)
88 : OS(OS) {}
89
90PreservedAnalyses
Chandler Carruthb47f8012016-03-11 11:05:24 +000091DominanceFrontierPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zheng751337f2016-02-25 17:54:15 +000092 OS << "DominanceFrontier for function: " << F.getName() << "\n";
Chandler Carruthb47f8012016-03-11 11:05:24 +000093 AM.getResult<DominanceFrontierAnalysis>(F).print(OS);
Hongbin Zheng751337f2016-02-25 17:54:15 +000094
95 return PreservedAnalyses::all();
96}