blob: 4afe22bd5342afe1da2253f8d84b313bff487f02 [file] [log] [blame]
Chris Lattnerd43023a2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner081aabc2001-07-02 05:46:38 +00008//
Chris Lattnerd43023a2002-08-02 16:43:03 +00009// This file implements the post-dominator construction algorithms.
Chris Lattner081aabc2001-07-02 05:46:38 +000010//
11//===----------------------------------------------------------------------===//
12
Chris Lattnerc86203a2002-08-21 23:43:50 +000013#include "llvm/Analysis/PostDominators.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000014#include "llvm/IR/Function.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000015#include "llvm/IR/PassManager.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000016#include "llvm/Pass.h"
17#include "llvm/Support/raw_ostream.h"
18
Chris Lattnerf9f7c2d2003-12-07 00:35:42 +000019using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000020
Chandler Carruthf1221bd2014-04-22 02:48:03 +000021#define DEBUG_TYPE "postdomtree"
22
David Green7c35de12018-02-28 11:00:08 +000023#ifdef EXPENSIVE_CHECKS
24static constexpr bool ExpensiveChecksEnabled = true;
25#else
26static constexpr bool ExpensiveChecksEnabled = false;
27#endif
28
Chris Lattnerc385beb2001-07-06 16:58:22 +000029//===----------------------------------------------------------------------===//
Owen Andersonf35a1db2007-04-15 08:47:27 +000030// PostDominatorTree Implementation
Nate Begemand5811b92006-03-11 02:20:46 +000031//===----------------------------------------------------------------------===//
32
Hongbin Zheng3f978402016-02-25 17:54:07 +000033char PostDominatorTreeWrapperPass::ID = 0;
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000034
Hongbin Zheng3f978402016-02-25 17:54:07 +000035INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree",
Owen Andersondf7a4f22010-10-07 22:25:06 +000036 "Post-Dominator Tree Construction", true, true)
Nate Begemand5811b92006-03-11 02:20:46 +000037
Chandler Carruthca68a3e2017-01-15 06:32:49 +000038bool PostDominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
39 FunctionAnalysisManager::Invalidator &) {
40 // Check whether the analysis, all analyses on functions, or the function's
41 // CFG have been preserved.
42 auto PAC = PA.getChecker<PostDominatorTreeAnalysis>();
43 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
44 PAC.preservedSet<CFGAnalyses>());
45}
46
Hongbin Zheng3f978402016-02-25 17:54:07 +000047bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
48 DT.recalculate(F);
Owen Andersonb60f2542007-10-03 03:20:17 +000049 return false;
50}
51
David Green7c35de12018-02-28 11:00:08 +000052void PostDominatorTreeWrapperPass::verifyAnalysis() const {
53 if (VerifyDomInfo)
54 assert(DT.verify(PostDominatorTree::VerificationLevel::Full));
55 else if (ExpensiveChecksEnabled)
56 assert(DT.verify(PostDominatorTree::VerificationLevel::Basic));
57}
58
Hongbin Zheng3f978402016-02-25 17:54:07 +000059void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
60 DT.print(OS);
Torok Edwin2a455752008-05-03 20:25:26 +000061}
62
Owen Andersonb1ba3522008-05-29 17:00:13 +000063FunctionPass* llvm::createPostDomTree() {
Hongbin Zheng3f978402016-02-25 17:54:07 +000064 return new PostDominatorTreeWrapperPass();
Owen Andersonb1ba3522008-05-29 17:00:13 +000065}
66
Chandler Carruthdab4eae2016-11-23 17:53:26 +000067AnalysisKey PostDominatorTreeAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +000068
Chandler Carruth164a2aa62016-06-17 00:11:01 +000069PostDominatorTree PostDominatorTreeAnalysis::run(Function &F,
70 FunctionAnalysisManager &) {
Jakub Kuderskief33edd2018-05-23 17:29:21 +000071 PostDominatorTree PDT(F);
Hongbin Zheng3f978402016-02-25 17:54:07 +000072 return PDT;
73}
74
75PostDominatorTreePrinterPass::PostDominatorTreePrinterPass(raw_ostream &OS)
76 : OS(OS) {}
77
78PreservedAnalyses
Chandler Carruthb47f8012016-03-11 11:05:24 +000079PostDominatorTreePrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zheng3f978402016-02-25 17:54:07 +000080 OS << "PostDominatorTree for function: " << F.getName() << "\n";
Chandler Carruthb47f8012016-03-11 11:05:24 +000081 AM.getResult<PostDominatorTreeAnalysis>(F).print(OS);
Hongbin Zheng3f978402016-02-25 17:54:07 +000082
83 return PreservedAnalyses::all();
84}