[llvm] r206310 - Use unique_ptr to manage ownership of child Regions within llvm::Region
David Blaikie
dblaikie at gmail.com
Tue Apr 15 11:32:43 PDT 2014
Author: dblaikie
Date: Tue Apr 15 13:32:43 2014
New Revision: 206310
URL: http://llvm.org/viewvc/llvm-project?rev=206310&view=rev
Log:
Use unique_ptr to manage ownership of child Regions within llvm::Region
Modified:
llvm/trunk/include/llvm/Analysis/RegionInfo.h
llvm/trunk/lib/Analysis/RegionInfo.cpp
llvm/trunk/lib/Analysis/RegionPass.cpp
llvm/trunk/lib/Analysis/RegionPrinter.cpp
Modified: llvm/trunk/include/llvm/Analysis/RegionInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/RegionInfo.h?rev=206310&r1=206309&r2=206310&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/RegionInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/RegionInfo.h Tue Apr 15 13:32:43 2014
@@ -33,6 +33,7 @@
#include "llvm/Analysis/PostDominators.h"
#include "llvm/Support/Allocator.h"
#include <map>
+#include <memory>
namespace llvm {
@@ -213,7 +214,7 @@ class Region : public RegionNode {
// (The entry BasicBlock is part of RegionNode)
BasicBlock *exit;
- typedef std::vector<Region*> RegionSet;
+ typedef std::vector<std::unique_ptr<Region>> RegionSet;
// The subregions of this region.
RegionSet children;
Modified: llvm/trunk/lib/Analysis/RegionInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/RegionInfo.cpp?rev=206310&r1=206309&r2=206310&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/RegionInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/RegionInfo.cpp Tue Apr 15 13:32:43 2014
@@ -19,6 +19,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include <algorithm>
+#include <iterator>
#include <set>
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(Basic
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(BasicB
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 *
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<Region> &R) {
+ return R.get() == SubRegion;
+ }) == children.end() &&
+ "Subregion already exists!");
SubRegion->parent = this;
- children.push_back(SubRegion);
+ children.push_back(std::unique_ptr<Region>(SubRegion));
if (!moveChildren)
return;
@@ -360,23 +360,27 @@ void Region::addSubRegion(Region *SubReg
RI->setRegionFor(BB, SubRegion);
}
- std::vector<Region*> Keep;
+ std::vector<std::unique_ptr<Region>> 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<RegionSet::iterator>(Keep.begin()),
+ std::move_iterator<RegionSet::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<Region> &R) { return R.get() == Child; });
assert(I != children.end() && "Region does not exit. Unable to remove.");
children.erase(children.begin()+(I-begin()));
return Child;
Modified: llvm/trunk/lib/Analysis/RegionPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/RegionPass.cpp?rev=206310&r1=206309&r2=206310&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/RegionPass.cpp (original)
+++ llvm/trunk/lib/Analysis/RegionPass.cpp Tue Apr 15 13:32:43 2014
@@ -36,10 +36,10 @@ RGPassManager::RGPassManager()
}
// Recurse through all subregions and all regions into RQ.
-static void addRegionIntoQueue(Region *R, std::deque<Region *> &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<Region *> &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(Functi
// 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;
Modified: llvm/trunk/lib/Analysis/RegionPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/RegionPrinter.cpp?rev=206310&r1=206309&r2=206310&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/RegionPrinter.cpp (original)
+++ llvm/trunk/lib/Analysis/RegionPrinter.cpp Tue Apr 15 13:32:43 2014
@@ -98,31 +98,31 @@ struct DOTGraphTraits<RegionInfo*> : pub
// 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<RegionInfo*> &GW,
+ static void printRegionCluster(const Region &R, GraphWriter<RegionInfo*> &GW,
unsigned depth = 0) {
raw_ostream &O = GW.getOStream();
- O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(R)
+ O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&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<const void*>(RI->getTopLevelRegion()->getBBNode(BB))
<< ";\n";
@@ -134,7 +134,7 @@ struct DOTGraphTraits<RegionInfo*> : pub
GraphWriter<RegionInfo*> &GW) {
raw_ostream &O = GW.getOStream();
O << "\tcolorscheme = \"paired12\"\n";
- printRegionCluster(RI->getTopLevelRegion(), GW, 4);
+ printRegionCluster(*RI->getTopLevelRegion(), GW, 4);
}
};
} //end namespace llvm
More information about the llvm-commits
mailing list