From f5cc2195dcfabbed1fb868ffe9ff7eac2d3a50f2 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Tue, 15 Apr 2014 18:32:43 +0000 Subject: [PATCH] Use unique_ptr to manage ownership of child Regions within llvm::Region llvm-svn: 206310 --- include/llvm/Analysis/RegionInfo.h | 3 ++- lib/Analysis/RegionInfo.cpp | 34 +++++++++++++++++------------- lib/Analysis/RegionPass.cpp | 10 ++++----- lib/Analysis/RegionPrinter.cpp | 22 +++++++++---------- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/include/llvm/Analysis/RegionInfo.h b/include/llvm/Analysis/RegionInfo.h index 6b43ae31249..82a788d1bb8 100644 --- a/include/llvm/Analysis/RegionInfo.h +++ b/include/llvm/Analysis/RegionInfo.h @@ -33,6 +33,7 @@ #include "llvm/Analysis/PostDominators.h" #include "llvm/Support/Allocator.h" #include +#include namespace llvm { @@ -213,7 +214,7 @@ class Region : public RegionNode { // (The entry BasicBlock is part of RegionNode) BasicBlock *exit; - typedef std::vector RegionSet; + typedef std::vector> RegionSet; // The subregions of this region. RegionSet children; diff --git a/lib/Analysis/RegionInfo.cpp b/lib/Analysis/RegionInfo.cpp index c9a07ab60db..6f3b4c29516 100644 --- a/lib/Analysis/RegionInfo.cpp +++ b/lib/Analysis/RegionInfo.cpp @@ -19,6 +19,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" #include +#include #include using namespace llvm; @@ -62,9 +63,6 @@ Region::~Region() { // Only clean the cache for this Region. Caches of child Regions will be // cleaned when the child Regions are deleted. BBNodeMap.clear(); - - for (iterator I = begin(), E = end(); I != E; ++I) - delete *I; } void Region::replaceEntry(BasicBlock *BB) { @@ -88,7 +86,7 @@ void Region::replaceEntryRecursive(BasicBlock *NewEntry) { R->replaceEntry(NewEntry); for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) if ((*RI)->getEntry() == OldEntry) - RegionQueue.push_back(*RI); + RegionQueue.push_back(RI->get()); } } @@ -104,7 +102,7 @@ void Region::replaceExitRecursive(BasicBlock *NewExit) { R->replaceExit(NewExit); for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) if ((*RI)->getExit() == OldExit) - RegionQueue.push_back(*RI); + RegionQueue.push_back(RI->get()); } } @@ -333,18 +331,20 @@ RegionNode* Region::getNode(BasicBlock *BB) const { void Region::transferChildrenTo(Region *To) { for (iterator I = begin(), E = end(); I != E; ++I) { (*I)->parent = To; - To->children.push_back(*I); + To->children.push_back(std::move(*I)); } children.clear(); } void Region::addSubRegion(Region *SubRegion, bool moveChildren) { assert(!SubRegion->parent && "SubRegion already has a parent!"); - assert(std::find(begin(), end(), SubRegion) == children.end() - && "Subregion already exists!"); + assert(std::find_if(begin(), end(), [&](const std::unique_ptr &R) { + return R.get() == SubRegion; + }) == children.end() && + "Subregion already exists!"); SubRegion->parent = this; - children.push_back(SubRegion); + children.push_back(std::unique_ptr(SubRegion)); if (!moveChildren) return; @@ -360,23 +360,27 @@ void Region::addSubRegion(Region *SubRegion, bool moveChildren) { RI->setRegionFor(BB, SubRegion); } - std::vector Keep; + std::vector> Keep; for (iterator I = begin(), E = end(); I != E; ++I) - if (SubRegion->contains(*I) && *I != SubRegion) { - SubRegion->children.push_back(*I); + if (SubRegion->contains(I->get()) && I->get() != SubRegion) { + SubRegion->children.push_back(std::move(*I)); (*I)->parent = SubRegion; } else - Keep.push_back(*I); + Keep.push_back(std::move(*I)); children.clear(); - children.insert(children.begin(), Keep.begin(), Keep.end()); + children.insert(children.begin(), + std::move_iterator(Keep.begin()), + std::move_iterator(Keep.end())); } Region *Region::removeSubRegion(Region *Child) { assert(Child->parent == this && "Child is not a child of this region!"); Child->parent = nullptr; - RegionSet::iterator I = std::find(children.begin(), children.end(), Child); + RegionSet::iterator I = std::find_if( + children.begin(), children.end(), + [&](const std::unique_ptr &R) { return R.get() == Child; }); assert(I != children.end() && "Region does not exit. Unable to remove."); children.erase(children.begin()+(I-begin())); return Child; diff --git a/lib/Analysis/RegionPass.cpp b/lib/Analysis/RegionPass.cpp index b29070a4fbf..1798c36fc13 100644 --- a/lib/Analysis/RegionPass.cpp +++ b/lib/Analysis/RegionPass.cpp @@ -36,10 +36,10 @@ RGPassManager::RGPassManager() } // Recurse through all subregions and all regions into RQ. -static void addRegionIntoQueue(Region *R, std::deque &RQ) { - RQ.push_back(R); - for (Region::iterator I = R->begin(), E = R->end(); I != E; ++I) - addRegionIntoQueue(*I, RQ); +static void addRegionIntoQueue(Region &R, std::deque &RQ) { + RQ.push_back(&R); + for (const auto &E : R) + addRegionIntoQueue(*E, RQ); } /// Pass Manager itself does not invalidate any analysis info. @@ -57,7 +57,7 @@ bool RGPassManager::runOnFunction(Function &F) { // Collect inherited analysis from Module level pass manager. populateInheritedAnalysis(TPM->activeStack); - addRegionIntoQueue(RI->getTopLevelRegion(), RQ); + addRegionIntoQueue(*RI->getTopLevelRegion(), RQ); if (RQ.empty()) // No regions, skip calling finalizers return false; diff --git a/lib/Analysis/RegionPrinter.cpp b/lib/Analysis/RegionPrinter.cpp index 6467f47cfbd..893210a5d70 100644 --- a/lib/Analysis/RegionPrinter.cpp +++ b/lib/Analysis/RegionPrinter.cpp @@ -98,31 +98,31 @@ struct DOTGraphTraits : public DOTGraphTraits { // Print the cluster of the subregions. This groups the single basic blocks // and adds a different background color for each group. - static void printRegionCluster(const Region *R, GraphWriter &GW, + static void printRegionCluster(const Region &R, GraphWriter &GW, unsigned depth = 0) { raw_ostream &O = GW.getOStream(); - O.indent(2 * depth) << "subgraph cluster_" << static_cast(R) + O.indent(2 * depth) << "subgraph cluster_" << static_cast(&R) << " {\n"; O.indent(2 * (depth + 1)) << "label = \"\";\n"; - if (!onlySimpleRegions || R->isSimple()) { + if (!onlySimpleRegions || R.isSimple()) { O.indent(2 * (depth + 1)) << "style = filled;\n"; O.indent(2 * (depth + 1)) << "color = " - << ((R->getDepth() * 2 % 12) + 1) << "\n"; + << ((R.getDepth() * 2 % 12) + 1) << "\n"; } else { O.indent(2 * (depth + 1)) << "style = solid;\n"; O.indent(2 * (depth + 1)) << "color = " - << ((R->getDepth() * 2 % 12) + 2) << "\n"; + << ((R.getDepth() * 2 % 12) + 2) << "\n"; } - for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) - printRegionCluster(*RI, GW, depth + 1); + for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI) + printRegionCluster(**RI, GW, depth + 1); - RegionInfo *RI = R->getRegionInfo(); + RegionInfo *RI = R.getRegionInfo(); - for (const auto &BB : R->blocks()) - if (RI->getRegionFor(BB) == R) + for (const auto &BB : R.blocks()) + if (RI->getRegionFor(BB) == &R) O.indent(2 * (depth + 1)) << "Node" << static_cast(RI->getTopLevelRegion()->getBBNode(BB)) << ";\n"; @@ -134,7 +134,7 @@ struct DOTGraphTraits : public DOTGraphTraits { GraphWriter &GW) { raw_ostream &O = GW.getOStream(); O << "\tcolorscheme = \"paired12\"\n"; - printRegionCluster(RI->getTopLevelRegion(), GW, 4); + printRegionCluster(*RI->getTopLevelRegion(), GW, 4); } }; } //end namespace llvm