blob: e6b660fe26d7a3bd6b7f646adc187669d68ab01d [file] [log] [blame]
Chris Lattnerd43023a2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner081aabc2001-07-02 05:46:38 +00009//
Chris Lattnerd43023a2002-08-02 16:43:03 +000010// This file implements the post-dominator construction algorithms.
Chris Lattner081aabc2001-07-02 05:46:38 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc86203a2002-08-21 23:43:50 +000014#include "llvm/Analysis/PostDominators.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000015#include "llvm/IR/Function.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000016#include "llvm/IR/PassManager.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000017#include "llvm/Pass.h"
18#include "llvm/Support/raw_ostream.h"
19
Chris Lattnerf9f7c2d2003-12-07 00:35:42 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chandler Carruthf1221bd2014-04-22 02:48:03 +000022#define DEBUG_TYPE "postdomtree"
23
David Green7c35de12018-02-28 11:00:08 +000024#ifdef EXPENSIVE_CHECKS
25static constexpr bool ExpensiveChecksEnabled = true;
26#else
27static constexpr bool ExpensiveChecksEnabled = false;
28#endif
29
Chris Lattnerc385beb2001-07-06 16:58:22 +000030//===----------------------------------------------------------------------===//
Owen Andersonf35a1db2007-04-15 08:47:27 +000031// PostDominatorTree Implementation
Nate Begemand5811b92006-03-11 02:20:46 +000032//===----------------------------------------------------------------------===//
33
Hongbin Zheng3f978402016-02-25 17:54:07 +000034char PostDominatorTreeWrapperPass::ID = 0;
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000035
Hongbin Zheng3f978402016-02-25 17:54:07 +000036INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree",
Owen Andersondf7a4f22010-10-07 22:25:06 +000037 "Post-Dominator Tree Construction", true, true)
Nate Begemand5811b92006-03-11 02:20:46 +000038
Chandler Carruthca68a3e2017-01-15 06:32:49 +000039bool PostDominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
40 FunctionAnalysisManager::Invalidator &) {
41 // Check whether the analysis, all analyses on functions, or the function's
42 // CFG have been preserved.
43 auto PAC = PA.getChecker<PostDominatorTreeAnalysis>();
44 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
45 PAC.preservedSet<CFGAnalyses>());
46}
47
Hongbin Zheng3f978402016-02-25 17:54:07 +000048bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
49 DT.recalculate(F);
Owen Andersonb60f2542007-10-03 03:20:17 +000050 return false;
51}
52
David Green7c35de12018-02-28 11:00:08 +000053void PostDominatorTreeWrapperPass::verifyAnalysis() const {
54 if (VerifyDomInfo)
55 assert(DT.verify(PostDominatorTree::VerificationLevel::Full));
56 else if (ExpensiveChecksEnabled)
57 assert(DT.verify(PostDominatorTree::VerificationLevel::Basic));
58}
59
Hongbin Zheng3f978402016-02-25 17:54:07 +000060void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
61 DT.print(OS);
Torok Edwin2a455752008-05-03 20:25:26 +000062}
63
Owen Andersonb1ba3522008-05-29 17:00:13 +000064FunctionPass* llvm::createPostDomTree() {
Hongbin Zheng3f978402016-02-25 17:54:07 +000065 return new PostDominatorTreeWrapperPass();
Owen Andersonb1ba3522008-05-29 17:00:13 +000066}
67
Chandler Carruthdab4eae2016-11-23 17:53:26 +000068AnalysisKey PostDominatorTreeAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +000069
Chandler Carruth164a2aa62016-06-17 00:11:01 +000070PostDominatorTree PostDominatorTreeAnalysis::run(Function &F,
71 FunctionAnalysisManager &) {
Jakub Kuderskief33edd2018-05-23 17:29:21 +000072 PostDominatorTree PDT(F);
Hongbin Zheng3f978402016-02-25 17:54:07 +000073 return PDT;
74}
75
76PostDominatorTreePrinterPass::PostDominatorTreePrinterPass(raw_ostream &OS)
77 : OS(OS) {}
78
79PreservedAnalyses
Chandler Carruthb47f8012016-03-11 11:05:24 +000080PostDominatorTreePrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zheng3f978402016-02-25 17:54:07 +000081 OS << "PostDominatorTree for function: " << F.getName() << "\n";
Chandler Carruthb47f8012016-03-11 11:05:24 +000082 AM.getResult<PostDominatorTreeAnalysis>(F).print(OS);
Hongbin Zheng3f978402016-02-25 17:54:07 +000083
84 return PreservedAnalyses::all();
85}