165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser//===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===// 265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// 365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// The LLVM Compiler Infrastructure 465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// 565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// This file is distributed under the University of Illinois Open Source 665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// License. See LICENSE.TXT for details. 765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// 865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser//===----------------------------------------------------------------------===// 965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// 1065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// This file implements RegionPass and RGPassManager. All region optimization 1165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// and transformation passes are derived from RegionPass. RGPassManager is 1265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// responsible for managing RegionPasses. 1365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// most of these codes are COPY from LoopPass.cpp 1465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// 1565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser//===----------------------------------------------------------------------===// 1665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser#include "llvm/Analysis/RegionPass.h" 1765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser#include "llvm/Analysis/RegionIterator.h" 1865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser#include "llvm/Support/Timer.h" 1965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 2065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser#include "llvm/Support/Debug.h" 2165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserusing namespace llvm; 2265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 23dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "regionpassmgr" 24dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 2565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser//===----------------------------------------------------------------------===// 2665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// RGPassManager 2765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// 2865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 2965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserchar RGPassManager::ID = 0; 3065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 310e122d1c2422285c872f68fc0ae1f7e5d2739572Andrew TrickRGPassManager::RGPassManager() 320e122d1c2422285c872f68fc0ae1f7e5d2739572Andrew Trick : FunctionPass(ID), PMDataManager() { 3365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser skipThisRegion = false; 3465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser redoThisRegion = false; 35dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines RI = nullptr; 36dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CurrentRegion = nullptr; 3765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 3865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 3965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// Recurse through all subregions and all regions into RQ. 40dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) { 41dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines RQ.push_back(&R); 42dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines for (const auto &E : R) 43dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines addRegionIntoQueue(*E, RQ); 4465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 4565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 4665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser/// Pass Manager itself does not invalidate any analysis info. 4765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosservoid RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const { 4865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Info.addRequired<RegionInfo>(); 4965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Info.setPreservesAll(); 5065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 5165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 5265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser/// run - Execute all of the passes scheduled for execution. Keep track of 5365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser/// whether any of the passes modifies the function, and if so, return true. 5465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserbool RGPassManager::runOnFunction(Function &F) { 5565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RI = &getAnalysis<RegionInfo>(); 5665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser bool Changed = false; 5765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 5865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Collect inherited analysis from Module level pass manager. 5965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser populateInheritedAnalysis(TPM->activeStack); 6065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 61dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines addRegionIntoQueue(*RI->getTopLevelRegion(), RQ); 6265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 6365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (RQ.empty()) // No regions, skip calling finalizers 6465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser return false; 6565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 6665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Initialization 6765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser for (std::deque<Region *>::const_iterator I = RQ.begin(), E = RQ.end(); 6865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser I != E; ++I) { 6965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Region *R = *I; 7065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 7165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RegionPass *RP = (RegionPass *)getContainedPass(Index); 7265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Changed |= RP->doInitialization(R, *this); 7365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 7465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 7565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 7665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Walk Regions 7765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser while (!RQ.empty()) { 7865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 7965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser CurrentRegion = RQ.back(); 8065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser skipThisRegion = false; 8165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser redoThisRegion = false; 8265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 8365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Run all passes on the current Region. 8465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 8565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RegionPass *P = (RegionPass*)getContainedPass(Index); 8665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 8765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG, 8865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser CurrentRegion->getNameStr()); 8965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dumpRequiredSet(P); 9065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 9165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser initializeAnalysisImpl(P); 9265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 9365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser { 9465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry()); 9565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 9665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser TimeRegion PassTimer(getPassTimer(P)); 9765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Changed |= P->runOnRegion(CurrentRegion, *this); 9865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 9965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 10065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (Changed) 10165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG, 10265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser skipThisRegion ? "<deleted>" : 10365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser CurrentRegion->getNameStr()); 10465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dumpPreservedSet(P); 10565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 10665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (!skipThisRegion) { 10765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Manually check that this region is still healthy. This is done 10865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // instead of relying on RegionInfo::verifyRegion since RegionInfo 10965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // is a function pass and it's really expensive to verify every 11065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Region in the function every time. That level of checking can be 11165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // enabled with the -verify-region-info option. 11265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser { 11365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser TimeRegion PassTimer(getPassTimer(P)); 11465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser CurrentRegion->verifyRegion(); 11565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 11665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 11765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Then call the regular verifyAnalysis functions. 11865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser verifyPreservedAnalysis(P); 11965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 12065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 12165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser removeNotPreservedAnalysis(P); 12265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser recordAvailableAnalysis(P); 12365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser removeDeadPasses(P, 12465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser skipThisRegion ? "<deleted>" : 12565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser CurrentRegion->getNameStr(), 12665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser ON_REGION_MSG); 12765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 12865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (skipThisRegion) 12965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Do not run other passes on this region. 13065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser break; 13165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 13265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 13365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // If the region was deleted, release all the region passes. This frees up 13465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // some memory, and avoids trouble with the pass manager trying to call 13565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // verifyAnalysis on them. 13665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (skipThisRegion) 13765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 13865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Pass *P = getContainedPass(Index); 13965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser freePass(P, "<deleted>", ON_REGION_MSG); 14065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 14165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 14265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Pop the region from queue after running all passes. 14365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RQ.pop_back(); 14465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 14565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (redoThisRegion) 14665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RQ.push_back(CurrentRegion); 14765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 14865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Free all region nodes created in region passes. 14965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RI->clearNodeCache(); 15065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 15165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 15265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Finalization 15365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 15465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RegionPass *P = (RegionPass*)getContainedPass(Index); 15565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Changed |= P->doFinalization(); 15665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 15765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 15865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Print the region tree after all pass. 15965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser DEBUG( 16065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dbgs() << "\nRegion tree of function " << F.getName() 16165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser << " after all region Pass:\n"; 16265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RI->dump(); 16365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dbgs() << "\n"; 16465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser ); 16565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 16665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser return Changed; 16765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 16865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 16965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser/// Print passes managed by this manager 17065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosservoid RGPassManager::dumpPassStructure(unsigned Offset) { 17165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser errs().indent(Offset*2) << "Region Pass Manager\n"; 17265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 17365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Pass *P = getContainedPass(Index); 17465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser P->dumpPassStructure(Offset + 1); 17565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser dumpLastUses(P, Offset+1); 17665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 17765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 17865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 17965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grossernamespace { 18065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser//===----------------------------------------------------------------------===// 18165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// PrintRegionPass 18265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserclass PrintRegionPass : public RegionPass { 18365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserprivate: 18465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser std::string Banner; 18565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser raw_ostream &Out; // raw_ostream to print on. 18665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 18765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserpublic: 18865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser static char ID; 18965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PrintRegionPass(const std::string &B, raw_ostream &o) 19065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser : RegionPass(ID), Banner(B), Out(o) {} 19165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 19236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void getAnalysisUsage(AnalysisUsage &AU) const override { 19365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser AU.setPreservesAll(); 19465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 19565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 19636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool runOnRegion(Region *R, RGPassManager &RGM) override { 19765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser Out << Banner; 198cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines for (const auto &BB : R->blocks()) { 199cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines if (BB) 200cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines BB->print(Out); 201cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines else 202cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines Out << "Printing <null> Block"; 203cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines } 20465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 20565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser return false; 20665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 20765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser}; 20865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 20965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosserchar PrintRegionPass::ID = 0; 21065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} //end anonymous namespace 21165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 21265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser//===----------------------------------------------------------------------===// 21365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// RegionPass 21465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 21565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// Check if this pass is suitable for the current RGPassManager, if 21665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// available. This pass P is not suitable for a RGPassManager if P 21765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// is not preserving higher level analysis info used by other 21865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// RGPassManager passes. In such case, pop RGPassManager from the 21965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// stack. This will force assignPassManager() to create new 22065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser// LPPassManger as expected. 22165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosservoid RegionPass::preparePassManager(PMStack &PMS) { 22265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 22365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Find RGPassManager 22465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser while (!PMS.empty() && 22565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMS.top()->getPassManagerType() > PMT_RegionPassManager) 22665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMS.pop(); 22765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 22865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 22965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // If this pass is destroying high level information that is used 23065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // by other passes that are managed by LPM then do not insert 23165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // this pass in current LPM. Use new RGPassManager. 23265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (PMS.top()->getPassManagerType() == PMT_RegionPassManager && 23365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser !PMS.top()->preserveHigherLevelAnalysis(this)) 23465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMS.pop(); 23565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 23665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 23765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser/// Assign pass manager to manage this pass. 23865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosservoid RegionPass::assignPassManager(PMStack &PMS, 23965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PassManagerType PreferredType) { 24065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Find RGPassManager 24165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser while (!PMS.empty() && 24265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMS.top()->getPassManagerType() > PMT_RegionPassManager) 24365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMS.pop(); 24465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 24565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RGPassManager *RGPM; 24665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 24765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // Create new Region Pass Manager if it does not exist. 24865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser if (PMS.top()->getPassManagerType() == PMT_RegionPassManager) 24965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RGPM = (RGPassManager*)PMS.top(); 25065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser else { 25165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 25265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser assert (!PMS.empty() && "Unable to create Region Pass Manager"); 25365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMDataManager *PMD = PMS.top(); 25465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 255f262e161612eb44b7bb3dd2008950be3e77b57ddHongbin Zheng // [1] Create new Region Pass Manager 2560e122d1c2422285c872f68fc0ae1f7e5d2739572Andrew Trick RGPM = new RGPassManager(); 25765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RGPM->populateInheritedAnalysis(PMS); 25865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 25965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // [2] Set up new manager's top level manager 26065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMTopLevelManager *TPM = PMD->getTopLevelManager(); 26165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser TPM->addIndirectPassManager(RGPM); 26265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 26365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // [3] Assign manager to manage this new manager. This may create 26465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // and push new managers into PMS 265d713acb63105f55ef5ed0f3a499da81c3eeacd49Tobias Grosser TPM->schedulePass(RGPM); 26665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 26765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser // [4] Push new manager into PMS 26865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser PMS.push(RGPM); 26965513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser } 27065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 27165513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser RGPM->add(this); 27265513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 27365513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser 27465513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser/// Get the printer pass 27565513605353c7e3ee8be6fc92892f257ad399d92Tobias GrosserPass *RegionPass::createPrinterPass(raw_ostream &O, 27665513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser const std::string &Banner) const { 27765513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser return new PrintRegionPass(Banner, O); 27865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser} 279