Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 1 | //===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===// |
| 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 | // |
| 10 | // This file implements RegionPass and RGPassManager. All region optimization |
| 11 | // and transformation passes are derived from RegionPass. RGPassManager is |
| 12 | // responsible for managing RegionPasses. |
David Majnemer | 2666c82 | 2016-07-19 17:50:27 +0000 | [diff] [blame] | 13 | // Most of this code has been COPIED from LoopPass.cpp |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | #include "llvm/Analysis/RegionPass.h" |
Eli Friedman | dca4238 | 2017-06-01 21:22:26 +0000 | [diff] [blame] | 17 | #include "llvm/IR/OptBisect.h" |
Fedor Sergeev | 9f6be22 | 2018-08-28 21:06:51 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PassTimingInfo.h" |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Timer.h" |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | 4da2537 | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "regionpassmgr" |
| 25 | |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // RGPassManager |
| 28 | // |
| 29 | |
| 30 | char RGPassManager::ID = 0; |
| 31 | |
Andrew Trick | 0e122d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 32 | RGPassManager::RGPassManager() |
| 33 | : FunctionPass(ID), PMDataManager() { |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 34 | skipThisRegion = false; |
| 35 | redoThisRegion = false; |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 36 | RI = nullptr; |
| 37 | CurrentRegion = nullptr; |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Recurse through all subregions and all regions into RQ. |
David Blaikie | 2bbc5a7 | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 41 | static void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) { |
| 42 | RQ.push_back(&R); |
| 43 | for (const auto &E : R) |
| 44 | addRegionIntoQueue(*E, RQ); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /// Pass Manager itself does not invalidate any analysis info. |
| 48 | void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const { |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 49 | Info.addRequired<RegionInfoPass>(); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 50 | Info.setPreservesAll(); |
| 51 | } |
| 52 | |
| 53 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 54 | /// whether any of the passes modifies the function, and if so, return true. |
| 55 | bool RGPassManager::runOnFunction(Function &F) { |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 56 | RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 57 | bool Changed = false; |
| 58 | |
| 59 | // Collect inherited analysis from Module level pass manager. |
| 60 | populateInheritedAnalysis(TPM->activeStack); |
| 61 | |
David Blaikie | 2bbc5a7 | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 62 | addRegionIntoQueue(*RI->getTopLevelRegion(), RQ); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 63 | |
| 64 | if (RQ.empty()) // No regions, skip calling finalizers |
| 65 | return false; |
| 66 | |
| 67 | // Initialization |
David Majnemer | 2666c82 | 2016-07-19 17:50:27 +0000 | [diff] [blame] | 68 | for (Region *R : RQ) { |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 69 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 70 | RegionPass *RP = (RegionPass *)getContainedPass(Index); |
| 71 | Changed |= RP->doInitialization(R, *this); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Walk Regions |
| 76 | while (!RQ.empty()) { |
| 77 | |
| 78 | CurrentRegion = RQ.back(); |
| 79 | skipThisRegion = false; |
| 80 | redoThisRegion = false; |
| 81 | |
| 82 | // Run all passes on the current Region. |
| 83 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 84 | RegionPass *P = (RegionPass*)getContainedPass(Index); |
| 85 | |
Chad Rosier | 3ad5494 | 2015-03-06 16:15:04 +0000 | [diff] [blame] | 86 | if (isPassDebuggingExecutionsOrMore()) { |
| 87 | dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG, |
| 88 | CurrentRegion->getNameStr()); |
| 89 | dumpRequiredSet(P); |
| 90 | } |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 91 | |
| 92 | initializeAnalysisImpl(P); |
| 93 | |
| 94 | { |
| 95 | PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry()); |
| 96 | |
| 97 | TimeRegion PassTimer(getPassTimer(P)); |
| 98 | Changed |= P->runOnRegion(CurrentRegion, *this); |
| 99 | } |
| 100 | |
Chad Rosier | 3ad5494 | 2015-03-06 16:15:04 +0000 | [diff] [blame] | 101 | if (isPassDebuggingExecutionsOrMore()) { |
| 102 | if (Changed) |
| 103 | dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG, |
| 104 | skipThisRegion ? "<deleted>" : |
| 105 | CurrentRegion->getNameStr()); |
| 106 | dumpPreservedSet(P); |
| 107 | } |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 108 | |
| 109 | if (!skipThisRegion) { |
| 110 | // Manually check that this region is still healthy. This is done |
| 111 | // instead of relying on RegionInfo::verifyRegion since RegionInfo |
| 112 | // is a function pass and it's really expensive to verify every |
| 113 | // Region in the function every time. That level of checking can be |
| 114 | // enabled with the -verify-region-info option. |
| 115 | { |
| 116 | TimeRegion PassTimer(getPassTimer(P)); |
| 117 | CurrentRegion->verifyRegion(); |
| 118 | } |
| 119 | |
| 120 | // Then call the regular verifyAnalysis functions. |
| 121 | verifyPreservedAnalysis(P); |
| 122 | } |
| 123 | |
| 124 | removeNotPreservedAnalysis(P); |
| 125 | recordAvailableAnalysis(P); |
| 126 | removeDeadPasses(P, |
Chad Rosier | 3ad5494 | 2015-03-06 16:15:04 +0000 | [diff] [blame] | 127 | (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ? |
| 128 | "<deleted>" : CurrentRegion->getNameStr(), |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 129 | ON_REGION_MSG); |
| 130 | |
| 131 | if (skipThisRegion) |
| 132 | // Do not run other passes on this region. |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | // If the region was deleted, release all the region passes. This frees up |
| 137 | // some memory, and avoids trouble with the pass manager trying to call |
| 138 | // verifyAnalysis on them. |
| 139 | if (skipThisRegion) |
| 140 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 141 | Pass *P = getContainedPass(Index); |
| 142 | freePass(P, "<deleted>", ON_REGION_MSG); |
| 143 | } |
| 144 | |
| 145 | // Pop the region from queue after running all passes. |
| 146 | RQ.pop_back(); |
| 147 | |
| 148 | if (redoThisRegion) |
| 149 | RQ.push_back(CurrentRegion); |
| 150 | |
| 151 | // Free all region nodes created in region passes. |
| 152 | RI->clearNodeCache(); |
| 153 | } |
| 154 | |
| 155 | // Finalization |
| 156 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 157 | RegionPass *P = (RegionPass*)getContainedPass(Index); |
| 158 | Changed |= P->doFinalization(); |
| 159 | } |
| 160 | |
| 161 | // Print the region tree after all pass. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 162 | LLVM_DEBUG(dbgs() << "\nRegion tree of function " << F.getName() |
| 163 | << " after all region Pass:\n"; |
| 164 | RI->dump(); dbgs() << "\n";); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 165 | |
| 166 | return Changed; |
| 167 | } |
| 168 | |
| 169 | /// Print passes managed by this manager |
| 170 | void RGPassManager::dumpPassStructure(unsigned Offset) { |
| 171 | errs().indent(Offset*2) << "Region Pass Manager\n"; |
| 172 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 173 | Pass *P = getContainedPass(Index); |
| 174 | P->dumpPassStructure(Offset + 1); |
| 175 | dumpLastUses(P, Offset+1); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | namespace { |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | // PrintRegionPass |
| 182 | class PrintRegionPass : public RegionPass { |
| 183 | private: |
| 184 | std::string Banner; |
| 185 | raw_ostream &Out; // raw_ostream to print on. |
| 186 | |
| 187 | public: |
| 188 | static char ID; |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 189 | PrintRegionPass(const std::string &B, raw_ostream &o) |
| 190 | : RegionPass(ID), Banner(B), Out(o) {} |
| 191 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 192 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 193 | AU.setPreservesAll(); |
| 194 | } |
| 195 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 196 | bool runOnRegion(Region *R, RGPassManager &RGM) override { |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 197 | Out << Banner; |
Richard Trieu | 64b905a | 2015-04-15 01:21:15 +0000 | [diff] [blame] | 198 | for (const auto *BB : R->blocks()) { |
Richard Trieu | 7921239 | 2014-06-21 02:43:02 +0000 | [diff] [blame] | 199 | if (BB) |
| 200 | BB->print(Out); |
| 201 | else |
| 202 | Out << "Printing <null> Block"; |
Richard Trieu | f31ecd3 | 2014-06-09 22:53:16 +0000 | [diff] [blame] | 203 | } |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 204 | |
| 205 | return false; |
| 206 | } |
Yaron Keren | ea56368 | 2017-03-10 07:09:20 +0000 | [diff] [blame] | 207 | |
Michael Kruse | f29303d | 2017-08-25 12:38:53 +0000 | [diff] [blame] | 208 | StringRef getPassName() const override { return "Print Region IR"; } |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | char PrintRegionPass::ID = 0; |
| 212 | } //end anonymous namespace |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // RegionPass |
| 216 | |
| 217 | // Check if this pass is suitable for the current RGPassManager, if |
| 218 | // available. This pass P is not suitable for a RGPassManager if P |
| 219 | // is not preserving higher level analysis info used by other |
| 220 | // RGPassManager passes. In such case, pop RGPassManager from the |
| 221 | // stack. This will force assignPassManager() to create new |
| 222 | // LPPassManger as expected. |
| 223 | void RegionPass::preparePassManager(PMStack &PMS) { |
| 224 | |
| 225 | // Find RGPassManager |
| 226 | while (!PMS.empty() && |
| 227 | PMS.top()->getPassManagerType() > PMT_RegionPassManager) |
| 228 | PMS.pop(); |
| 229 | |
| 230 | |
| 231 | // If this pass is destroying high level information that is used |
| 232 | // by other passes that are managed by LPM then do not insert |
| 233 | // this pass in current LPM. Use new RGPassManager. |
| 234 | if (PMS.top()->getPassManagerType() == PMT_RegionPassManager && |
| 235 | !PMS.top()->preserveHigherLevelAnalysis(this)) |
| 236 | PMS.pop(); |
| 237 | } |
| 238 | |
| 239 | /// Assign pass manager to manage this pass. |
| 240 | void RegionPass::assignPassManager(PMStack &PMS, |
| 241 | PassManagerType PreferredType) { |
| 242 | // Find RGPassManager |
| 243 | while (!PMS.empty() && |
| 244 | PMS.top()->getPassManagerType() > PMT_RegionPassManager) |
| 245 | PMS.pop(); |
| 246 | |
| 247 | RGPassManager *RGPM; |
| 248 | |
| 249 | // Create new Region Pass Manager if it does not exist. |
| 250 | if (PMS.top()->getPassManagerType() == PMT_RegionPassManager) |
| 251 | RGPM = (RGPassManager*)PMS.top(); |
| 252 | else { |
| 253 | |
| 254 | assert (!PMS.empty() && "Unable to create Region Pass Manager"); |
| 255 | PMDataManager *PMD = PMS.top(); |
| 256 | |
Hongbin Zheng | f262e16 | 2011-05-05 13:59:38 +0000 | [diff] [blame] | 257 | // [1] Create new Region Pass Manager |
Andrew Trick | 0e122d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 258 | RGPM = new RGPassManager(); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 259 | RGPM->populateInheritedAnalysis(PMS); |
| 260 | |
| 261 | // [2] Set up new manager's top level manager |
| 262 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 263 | TPM->addIndirectPassManager(RGPM); |
| 264 | |
| 265 | // [3] Assign manager to manage this new manager. This may create |
| 266 | // and push new managers into PMS |
Tobias Grosser | d713acb | 2010-12-12 21:58:28 +0000 | [diff] [blame] | 267 | TPM->schedulePass(RGPM); |
Tobias Grosser | 6551360 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 268 | |
| 269 | // [4] Push new manager into PMS |
| 270 | PMS.push(RGPM); |
| 271 | } |
| 272 | |
| 273 | RGPM->add(this); |
| 274 | } |
| 275 | |
| 276 | /// Get the printer pass |
| 277 | Pass *RegionPass::createPrinterPass(raw_ostream &O, |
| 278 | const std::string &Banner) const { |
| 279 | return new PrintRegionPass(Banner, O); |
| 280 | } |
Eli Friedman | dca4238 | 2017-06-01 21:22:26 +0000 | [diff] [blame] | 281 | |
| 282 | bool RegionPass::skipRegion(Region &R) const { |
| 283 | Function &F = *R.getEntry()->getParent(); |
Fedor Sergeev | 558f76f | 2018-03-27 16:57:20 +0000 | [diff] [blame] | 284 | if (!F.getContext().getOptPassGate().shouldRunPass(this, R)) |
Eli Friedman | dca4238 | 2017-06-01 21:22:26 +0000 | [diff] [blame] | 285 | return true; |
| 286 | |
| 287 | if (F.hasFnAttribute(Attribute::OptimizeNone)) { |
| 288 | // Report this only once per function. |
| 289 | if (R.getEntry() == &F.getEntryBlock()) |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 290 | LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() |
| 291 | << "' on function " << F.getName() << "\n"); |
Eli Friedman | dca4238 | 2017-06-01 21:22:26 +0000 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | return false; |
| 295 | } |