Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 1 | //===- RegionInfo.cpp - SESE region detection analysis --------------------===// |
| 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 |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // Detects single entry single exit regions in the control flow graph. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "llvm/Analysis/RegionInfo.h" |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Statistic.h" |
Michael Kruse | 20dcc9f | 2015-08-10 13:21:59 +0000 | [diff] [blame] | 13 | #ifndef NDEBUG |
| 14 | #include "llvm/Analysis/RegionPrinter.h" |
| 15 | #endif |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/RegionInfoImpl.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 17 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Function.h" |
| 19 | #include "llvm/IR/PassManager.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "region" |
| 28 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 29 | namespace llvm { |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 30 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 31 | template class RegionBase<RegionTraits<Function>>; |
| 32 | template class RegionNodeBase<RegionTraits<Function>>; |
| 33 | template class RegionInfoBase<RegionTraits<Function>>; |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 34 | |
| 35 | } // end namespace llvm |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 36 | |
| 37 | STATISTIC(numRegions, "The # of regions"); |
| 38 | STATISTIC(numSimpleRegions, "The # of simple regions"); |
| 39 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 40 | // Always verify if expensive checking is enabled. |
| 41 | |
| 42 | static cl::opt<bool,true> |
| 43 | VerifyRegionInfoX( |
| 44 | "verify-region-info", |
| 45 | cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo), |
| 46 | cl::desc("Verify region info (time consuming)")); |
| 47 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 48 | static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style", |
| 49 | cl::location(RegionInfo::printStyle), |
Tobias Grosser | 8b304ff | 2011-04-04 07:19:18 +0000 | [diff] [blame] | 50 | cl::Hidden, |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 51 | cl::desc("style of printing regions"), |
| 52 | cl::values( |
Tobias Grosser | 8b304ff | 2011-04-04 07:19:18 +0000 | [diff] [blame] | 53 | clEnumValN(Region::PrintNone, "none", "print no details"), |
| 54 | clEnumValN(Region::PrintBB, "bb", |
Hongbin Zheng | 14c05c4 | 2012-08-27 13:49:24 +0000 | [diff] [blame] | 55 | "print regions in detail with block_iterator"), |
Tobias Grosser | 8b304ff | 2011-04-04 07:19:18 +0000 | [diff] [blame] | 56 | clEnumValN(Region::PrintRN, "rn", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 57 | "print regions in detail with element_iterator"))); |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 58 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 60 | // Region implementation |
| 61 | // |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 62 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 63 | Region::Region(BasicBlock *Entry, BasicBlock *Exit, |
| 64 | RegionInfo* RI, |
| 65 | DominatorTree *DT, Region *Parent) : |
| 66 | RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) { |
Daniel Dunbar | 18e39ce | 2010-07-28 20:28:50 +0000 | [diff] [blame] | 67 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 70 | Region::~Region() = default; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 71 | |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | // RegionInfo implementation |
| 74 | // |
| 75 | |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 76 | RegionInfo::RegionInfo() = default; |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 77 | |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 78 | RegionInfo::~RegionInfo() = default; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 80 | bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 81 | FunctionAnalysisManager::Invalidator &) { |
| 82 | // Check whether the analysis, all analyses on functions, or the function's |
Jiading Gai | a269437 | 2018-07-22 20:04:42 +0000 | [diff] [blame] | 83 | // CFG has been preserved. |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 84 | auto PAC = PA.getChecker<RegionInfoAnalysis>(); |
| 85 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || |
| 86 | PAC.preservedSet<CFGAnalyses>()); |
| 87 | } |
| 88 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 89 | void RegionInfo::updateStatistics(Region *R) { |
| 90 | ++numRegions; |
| 91 | |
| 92 | // TODO: Slow. Should only be enabled if -stats is used. |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 93 | if (R->isSimple()) |
| 94 | ++numSimpleRegions; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 95 | } |
| 96 | |
NAKAMURA Takumi | 8eb82fc | 2014-07-20 03:57:51 +0000 | [diff] [blame] | 97 | void RegionInfo::recalculate(Function &F, DominatorTree *DT_, |
| 98 | PostDominatorTree *PDT_, DominanceFrontier *DF_) { |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 99 | DT = DT_; |
| 100 | PDT = PDT_; |
| 101 | DF = DF_; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 102 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 103 | TopLevelRegion = new Region(&F.getEntryBlock(), nullptr, |
| 104 | this, DT, nullptr); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 105 | updateStatistics(TopLevelRegion); |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 106 | calculate(F); |
| 107 | } |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 108 | |
Michael Kruse | 20dcc9f | 2015-08-10 13:21:59 +0000 | [diff] [blame] | 109 | #ifndef NDEBUG |
| 110 | void RegionInfo::view() { viewRegion(this); } |
| 111 | |
| 112 | void RegionInfo::viewOnly() { viewRegionOnly(this); } |
| 113 | #endif |
| 114 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
| 116 | // RegionInfoPass implementation |
| 117 | // |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 118 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 119 | RegionInfoPass::RegionInfoPass() : FunctionPass(ID) { |
| 120 | initializeRegionInfoPassPass(*PassRegistry::getPassRegistry()); |
| 121 | } |
| 122 | |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 123 | RegionInfoPass::~RegionInfoPass() = default; |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 124 | |
| 125 | bool RegionInfoPass::runOnFunction(Function &F) { |
| 126 | releaseMemory(); |
| 127 | |
| 128 | auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 129 | auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree(); |
Hongbin Zheng | 751337f | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 130 | auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier(); |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 131 | |
| 132 | RI.recalculate(F, DT, PDT, DF); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 136 | void RegionInfoPass::releaseMemory() { |
| 137 | RI.releaseMemory(); |
| 138 | } |
| 139 | |
| 140 | void RegionInfoPass::verifyAnalysis() const { |
| 141 | RI.verifyAnalysis(); |
| 142 | } |
| 143 | |
| 144 | void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 145 | AU.setPreservesAll(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 146 | AU.addRequiredTransitive<DominatorTreeWrapperPass>(); |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 147 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
Hongbin Zheng | 751337f | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 148 | AU.addRequired<DominanceFrontierWrapperPass>(); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 151 | void RegionInfoPass::print(raw_ostream &OS, const Module *) const { |
| 152 | RI.print(OS); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 155 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 156 | LLVM_DUMP_METHOD void RegionInfoPass::dump() const { |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 157 | RI.dump(); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 158 | } |
NAKAMURA Takumi | 118b0c7 | 2014-07-20 00:00:42 +0000 | [diff] [blame] | 159 | #endif |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 160 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 161 | char RegionInfoPass::ID = 0; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 162 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 163 | INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions", |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 164 | "Detect single entry single exit regions", true, true) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 165 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 166 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
Hongbin Zheng | 751337f | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 167 | INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass) |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 168 | INITIALIZE_PASS_END(RegionInfoPass, "regions", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 169 | "Detect single entry single exit regions", true, true) |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 170 | |
| 171 | // Create methods available outside of this file, to use them |
| 172 | // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by |
| 173 | // the link time optimization. |
| 174 | |
| 175 | namespace llvm { |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 176 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 177 | FunctionPass *createRegionInfoPass() { |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 178 | return new RegionInfoPass(); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 179 | } |
Eugene Zelenko | 4f820d0 | 2017-06-27 21:52:05 +0000 | [diff] [blame] | 180 | |
| 181 | } // end namespace llvm |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 182 | |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 183 | //===----------------------------------------------------------------------===// |
| 184 | // RegionInfoAnalysis implementation |
| 185 | // |
| 186 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 187 | AnalysisKey RegionInfoAnalysis::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 188 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 189 | RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) { |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 190 | RegionInfo RI; |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 191 | auto *DT = &AM.getResult<DominatorTreeAnalysis>(F); |
| 192 | auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F); |
| 193 | auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F); |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 194 | |
| 195 | RI.recalculate(F, DT, PDT, DF); |
| 196 | return RI; |
| 197 | } |
| 198 | |
| 199 | RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS) |
| 200 | : OS(OS) {} |
| 201 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 202 | PreservedAnalyses RegionInfoPrinterPass::run(Function &F, |
| 203 | FunctionAnalysisManager &AM) { |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 204 | OS << "Region Tree for function: " << F.getName() << "\n"; |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 205 | AM.getResult<RegionInfoAnalysis>(F).print(OS); |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 206 | |
| 207 | return PreservedAnalyses::all(); |
| 208 | } |
| 209 | |
| 210 | PreservedAnalyses RegionInfoVerifierPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 211 | FunctionAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 212 | AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis(); |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 213 | |
| 214 | return PreservedAnalyses::all(); |
| 215 | } |