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