blob: de7f62cf4ecd7ef9bb1f862baf4f0e5b1e69d48f [file] [log] [blame]
Cameron Zwarich46765992011-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 Arsenault661ca492014-07-12 21:59:52 +000011#include "llvm/Analysis/DominanceFrontierImpl.h"
Nico Weber0f38c602018-04-30 14:59:11 +000012#include "llvm/Config/llvm-config.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000013#include "llvm/IR/Dominators.h"
14#include "llvm/IR/Function.h"
Hongbin Zheng15969222016-02-25 17:54:15 +000015#include "llvm/IR/PassManager.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000016#include "llvm/Pass.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/raw_ostream.h"
Matt Arsenault661ca492014-07-12 21:59:52 +000020
Cameron Zwarich46765992011-01-18 06:06:27 +000021using namespace llvm;
22
Matt Arsenault661ca492014-07-12 21:59:52 +000023namespace llvm {
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000024
Jakub Kuderskicb105522017-07-14 18:26:09 +000025template class DominanceFrontierBase<BasicBlock, false>;
26template class DominanceFrontierBase<BasicBlock, true>;
Matt Arsenault661ca492014-07-12 21:59:52 +000027template class ForwardDominanceFrontierBase<BasicBlock>;
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000028
29} // end namespace llvm
Matt Arsenault661ca492014-07-12 21:59:52 +000030
Hongbin Zheng15969222016-02-25 17:54:15 +000031char DominanceFrontierWrapperPass::ID = 0;
Matt Arsenault661ca492014-07-12 21:59:52 +000032
Hongbin Zheng15969222016-02-25 17:54:15 +000033INITIALIZE_PASS_BEGIN(DominanceFrontierWrapperPass, "domfrontier",
Cameron Zwarich46765992011-01-18 06:06:27 +000034 "Dominance Frontier Construction", true, true)
Chandler Carruth7f2eff72014-01-13 13:07:17 +000035INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng15969222016-02-25 17:54:15 +000036INITIALIZE_PASS_END(DominanceFrontierWrapperPass, "domfrontier",
Cameron Zwarich46765992011-01-18 06:06:27 +000037 "Dominance Frontier Construction", true, true)
38
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000039DominanceFrontierWrapperPass::DominanceFrontierWrapperPass()
Hongbin Zheng15969222016-02-25 17:54:15 +000040 : FunctionPass(ID), DF() {
41 initializeDominanceFrontierWrapperPassPass(*PassRegistry::getPassRegistry());
Cameron Zwarich46765992011-01-18 06:06:27 +000042}
43
Hongbin Zheng15969222016-02-25 17:54:15 +000044void DominanceFrontierWrapperPass::releaseMemory() {
45 DF.releaseMemory();
Cameron Zwarich46765992011-01-18 06:06:27 +000046}
47
Hongbin Zheng15969222016-02-25 17:54:15 +000048bool DominanceFrontierWrapperPass::runOnFunction(Function &) {
Matt Arsenault661ca492014-07-12 21:59:52 +000049 releaseMemory();
Hongbin Zheng15969222016-02-25 17:54:15 +000050 DF.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
Matt Arsenault661ca492014-07-12 21:59:52 +000051 return false;
52}
53
Hongbin Zheng15969222016-02-25 17:54:15 +000054void DominanceFrontierWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Matt Arsenault661ca492014-07-12 21:59:52 +000055 AU.setPreservesAll();
56 AU.addRequired<DominatorTreeWrapperPass>();
57}
58
Hongbin Zheng15969222016-02-25 17:54:15 +000059void DominanceFrontierWrapperPass::print(raw_ostream &OS, const Module *) const {
60 DF.print(OS);
Cameron Zwarich46765992011-01-18 06:06:27 +000061}
62
Aaron Ballman1d03d382017-10-15 14:32:27 +000063#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Hongbin Zheng15969222016-02-25 17:54:15 +000064LLVM_DUMP_METHOD void DominanceFrontierWrapperPass::dump() const {
Cameron Zwarich46765992011-01-18 06:06:27 +000065 print(dbgs());
66}
Manman Rencc77eec2012-09-06 19:55:56 +000067#endif
Hongbin Zheng15969222016-02-25 17:54:15 +000068
Chandler Carruth10dd00c2017-01-15 06:32:49 +000069/// 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
Chandler Carruth33d56812016-11-23 17:53:26 +000079AnalysisKey DominanceFrontierAnalysis::Key;
NAKAMURA Takumid9b6afb2016-02-28 17:17:00 +000080
Hongbin Zheng15969222016-02-25 17:54:15 +000081DominanceFrontier DominanceFrontierAnalysis::run(Function &F,
Chandler Carruth8e27cb22016-03-11 11:05:24 +000082 FunctionAnalysisManager &AM) {
Hongbin Zheng15969222016-02-25 17:54:15 +000083 DominanceFrontier DF;
Chandler Carruth8e27cb22016-03-11 11:05:24 +000084 DF.analyze(AM.getResult<DominatorTreeAnalysis>(F));
Hongbin Zheng15969222016-02-25 17:54:15 +000085 return DF;
86}
87
88DominanceFrontierPrinterPass::DominanceFrontierPrinterPass(raw_ostream &OS)
89 : OS(OS) {}
90
91PreservedAnalyses
Chandler Carruth8e27cb22016-03-11 11:05:24 +000092DominanceFrontierPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zheng15969222016-02-25 17:54:15 +000093 OS << "DominanceFrontier for function: " << F.getName() << "\n";
Chandler Carruth8e27cb22016-03-11 11:05:24 +000094 AM.getResult<DominanceFrontierAnalysis>(F).print(OS);
Hongbin Zheng15969222016-02-25 17:54:15 +000095
96 return PreservedAnalyses::all();
97}