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