Chris Lattner | d43023a | 2002-08-02 16:43:03 +0000 | [diff] [blame] | 1 | //===- PostDominators.cpp - Post-Dominator Calculation --------------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 081aabc | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 8 | // |
Chris Lattner | d43023a | 2002-08-02 16:43:03 +0000 | [diff] [blame] | 9 | // This file implements the post-dominator construction algorithms. |
Chris Lattner | 081aabc | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chris Lattner | c86203a | 2002-08-21 23:43:50 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/PostDominators.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Function.h" |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 15 | #include "llvm/IR/PassManager.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
Chris Lattner | f9f7c2d | 2003-12-07 00:35:42 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "postdomtree" |
| 22 | |
David Green | 7c35de1 | 2018-02-28 11:00:08 +0000 | [diff] [blame] | 23 | #ifdef EXPENSIVE_CHECKS |
| 24 | static constexpr bool ExpensiveChecksEnabled = true; |
| 25 | #else |
| 26 | static constexpr bool ExpensiveChecksEnabled = false; |
| 27 | #endif |
| 28 | |
Chris Lattner | c385beb | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Owen Anderson | f35a1db | 2007-04-15 08:47:27 +0000 | [diff] [blame] | 30 | // PostDominatorTree Implementation |
Nate Begeman | d5811b9 | 2006-03-11 02:20:46 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 33 | char PostDominatorTreeWrapperPass::ID = 0; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 34 | |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 35 | INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 36 | "Post-Dominator Tree Construction", true, true) |
Nate Begeman | d5811b9 | 2006-03-11 02:20:46 +0000 | [diff] [blame] | 37 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 38 | bool 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 Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 47 | bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) { |
| 48 | DT.recalculate(F); |
Owen Anderson | b60f254 | 2007-10-03 03:20:17 +0000 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
David Green | 7c35de1 | 2018-02-28 11:00:08 +0000 | [diff] [blame] | 52 | void 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 Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 59 | void PostDominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 60 | DT.print(OS); |
Torok Edwin | 2a45575 | 2008-05-03 20:25:26 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Owen Anderson | b1ba352 | 2008-05-29 17:00:13 +0000 | [diff] [blame] | 63 | FunctionPass* llvm::createPostDomTree() { |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 64 | return new PostDominatorTreeWrapperPass(); |
Owen Anderson | b1ba352 | 2008-05-29 17:00:13 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 67 | AnalysisKey PostDominatorTreeAnalysis::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 68 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 69 | PostDominatorTree PostDominatorTreeAnalysis::run(Function &F, |
| 70 | FunctionAnalysisManager &) { |
Jakub Kuderski | ef33edd | 2018-05-23 17:29:21 +0000 | [diff] [blame] | 71 | PostDominatorTree PDT(F); |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 72 | return PDT; |
| 73 | } |
| 74 | |
| 75 | PostDominatorTreePrinterPass::PostDominatorTreePrinterPass(raw_ostream &OS) |
| 76 | : OS(OS) {} |
| 77 | |
| 78 | PreservedAnalyses |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 79 | PostDominatorTreePrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 80 | OS << "PostDominatorTree for function: " << F.getName() << "\n"; |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 81 | AM.getResult<PostDominatorTreeAnalysis>(F).print(OS); |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 82 | |
| 83 | return PreservedAnalyses::all(); |
| 84 | } |