From 938a6c7ce02905d7f600f1f45b5943c82bec9a8e Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 19 Jul 2016 17:50:30 +0000 Subject: [PATCH] [RegionInfo] Some cleanups - Use unique_ptr instead of managing a container of new'd pointers. - Use range based for loops. No functional change is intended. llvm-svn: 276001 --- llvm/include/llvm/Analysis/RegionInfo.h | 9 +- llvm/include/llvm/Analysis/RegionInfoImpl.h | 188 ++++++++++++---------------- 2 files changed, 87 insertions(+), 110 deletions(-) diff --git a/llvm/include/llvm/Analysis/RegionInfo.h b/llvm/include/llvm/Analysis/RegionInfo.h index 91bfd43..9c95a1f 100644 --- a/llvm/include/llvm/Analysis/RegionInfo.h +++ b/llvm/include/llvm/Analysis/RegionInfo.h @@ -39,6 +39,7 @@ #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/PointerIntPair.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/PassManager.h" @@ -278,7 +279,7 @@ class RegionBase : public RegionNodeBase { // The subregions of this region. RegionSet children; - typedef std::map BBNodeMapT; + typedef std::map> BBNodeMapT; // Save the BasicBlock RegionNodes that are element of this Region. mutable BBNodeMapT BBNodeMap; @@ -634,9 +635,15 @@ public: element_iterator element_begin(); element_iterator element_end(); + iterator_range elements() { + return make_range(element_begin(), element_end()); + } const_element_iterator element_begin() const; const_element_iterator element_end() const; + iterator_range elements() const { + return make_range(element_begin(), element_end()); + } //@} }; diff --git a/llvm/include/llvm/Analysis/RegionInfoImpl.h b/llvm/include/llvm/Analysis/RegionInfoImpl.h index 15dd1a2..bf4763e 100644 --- a/llvm/include/llvm/Analysis/RegionInfoImpl.h +++ b/llvm/include/llvm/Analysis/RegionInfoImpl.h @@ -38,12 +38,6 @@ RegionBase::RegionBase(BlockT *Entry, BlockT *Exit, template RegionBase::~RegionBase() { - // Free the cached nodes. - for (typename BBNodeMapT::iterator it = BBNodeMap.begin(), - ie = BBNodeMap.end(); - it != ie; ++it) - delete it->second; - // Only clean the cache for this Region. Caches of child Regions will be // cleaned when the child Regions are deleted. BBNodeMap.clear(); @@ -71,10 +65,9 @@ void RegionBase::replaceEntryRecursive(BlockT *NewEntry) { RegionQueue.pop_back(); R->replaceEntry(NewEntry); - for (typename RegionT::const_iterator RI = R->begin(), RE = R->end(); - RI != RE; ++RI) { - if ((*RI)->getEntry() == OldEntry) - RegionQueue.push_back(RI->get()); + for (std::unique_ptr &Child : *R) { + if (Child->getEntry() == OldEntry) + RegionQueue.push_back(Child.get()); } } } @@ -90,10 +83,9 @@ void RegionBase::replaceExitRecursive(BlockT *NewExit) { RegionQueue.pop_back(); R->replaceExit(NewExit); - for (typename RegionT::const_iterator RI = R->begin(), RE = R->end(); - RI != RE; ++RI) { - if ((*RI)->getExit() == OldExit) - RegionQueue.push_back(RI->get()); + for (std::unique_ptr &Child : *R) { + if (Child->getExit() == OldExit) + RegionQueue.push_back(Child.get()); } } } @@ -160,13 +152,10 @@ typename Tr::LoopT *RegionBase::outermostLoopInRegion(LoopInfoT *LI, template typename RegionBase::BlockT *RegionBase::getEnteringBlock() const { BlockT *entry = getEntry(); - BlockT *Pred; BlockT *enteringBlock = nullptr; - for (PredIterTy PI = InvBlockTraits::child_begin(entry), - PE = InvBlockTraits::child_end(entry); - PI != PE; ++PI) { - Pred = *PI; + for (BlockT *Pred : make_range(InvBlockTraits::child_begin(entry), + InvBlockTraits::child_end(entry))) { if (DT->getNode(Pred) && !contains(Pred)) { if (enteringBlock) return nullptr; @@ -181,16 +170,13 @@ typename RegionBase::BlockT *RegionBase::getEnteringBlock() const { template typename RegionBase::BlockT *RegionBase::getExitingBlock() const { BlockT *exit = getExit(); - BlockT *Pred; BlockT *exitingBlock = nullptr; if (!exit) return nullptr; - for (PredIterTy PI = InvBlockTraits::child_begin(exit), - PE = InvBlockTraits::child_end(exit); - PI != PE; ++PI) { - Pred = *PI; + for (BlockT *Pred : make_range(InvBlockTraits::child_begin(exit), + InvBlockTraits::child_end(exit))) { if (contains(Pred)) { if (exitingBlock) return nullptr; @@ -239,19 +225,17 @@ void RegionBase::verifyBBInRegion(BlockT *BB) const { BlockT *entry = getEntry(), *exit = getExit(); - for (SuccIterTy SI = BlockTraits::child_begin(BB), - SE = BlockTraits::child_end(BB); - SI != SE; ++SI) { - if (!contains(*SI) && exit != *SI) + for (BlockT *Succ : + make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) { + if (!contains(Succ) && exit != Succ) llvm_unreachable("Broken region found: edges leaving the region must go " "to the exit node!"); } if (entry != BB) { - for (PredIterTy SI = InvBlockTraits::child_begin(BB), - SE = InvBlockTraits::child_end(BB); - SI != SE; ++SI) { - if (!contains(*SI)) + for (BlockT *Pred : make_range(InvBlockTraits::child_begin(BB), + InvBlockTraits::child_end(BB))) { + if (!contains(Pred)) llvm_unreachable("Broken region found: edges entering the region must " "go to the entry node!"); } @@ -266,11 +250,10 @@ void RegionBase::verifyWalk(BlockT *BB, std::set *visited) const { verifyBBInRegion(BB); - for (SuccIterTy SI = BlockTraits::child_begin(BB), - SE = BlockTraits::child_end(BB); - SI != SE; ++SI) { - if (*SI != exit && visited->find(*SI) == visited->end()) - verifyWalk(*SI, visited); + for (BlockT *Succ : + make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) { + if (Succ != exit && visited->find(Succ) == visited->end()) + verifyWalk(Succ, visited); } } @@ -288,9 +271,8 @@ void RegionBase::verifyRegion() const { template void RegionBase::verifyRegionNest() const { - for (typename RegionT::const_iterator RI = begin(), RE = end(); RI != RE; - ++RI) - (*RI)->verifyRegionNest(); + for (const std::unique_ptr &R : *this) + R->verifyRegionNest(); verifyRegion(); } @@ -345,13 +327,14 @@ typename Tr::RegionNodeT *RegionBase::getBBNode(BlockT *BB) const { typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB); - if (at != BBNodeMap.end()) - return at->second; - - auto Deconst = const_cast *>(this); - RegionNodeT *NewNode = new RegionNodeT(static_cast(Deconst), BB); - BBNodeMap.insert(std::make_pair(BB, NewNode)); - return NewNode; + if (at == BBNodeMap.end()) { + auto Deconst = const_cast *>(this); + at = BBNodeMap + .emplace(BB, make_unique( + static_cast(Deconst), BB)) + .first; + } + return at->second.get(); } template @@ -365,9 +348,9 @@ typename Tr::RegionNodeT *RegionBase::getNode(BlockT *BB) const { template void RegionBase::transferChildrenTo(RegionT *To) { - for (iterator I = begin(), E = end(); I != E; ++I) { - (*I)->parent = To; - To->children.push_back(std::move(*I)); + for (std::unique_ptr &R : *this) { + R->parent = To; + To->children.push_back(std::move(R)); } children.clear(); } @@ -375,9 +358,10 @@ void RegionBase::transferChildrenTo(RegionT *To) { template void RegionBase::addSubRegion(RegionT *SubRegion, bool moveChildren) { assert(!SubRegion->parent && "SubRegion already has a parent!"); - assert(std::find_if(begin(), end(), [&](const std::unique_ptr &R) { - return R.get() == SubRegion; - }) == children.end() && + assert(find_if(*this, + [&](const std::unique_ptr &R) { + return R.get() == SubRegion; + }) == children.end() && "Subregion already exists!"); SubRegion->parent = static_cast(this); @@ -389,9 +373,9 @@ void RegionBase::addSubRegion(RegionT *SubRegion, bool moveChildren) { assert(SubRegion->children.empty() && "SubRegions that contain children are not supported"); - for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) { - if (!(*I)->isSubRegion()) { - BlockT *BB = (*I)->template getNodeAs(); + for (RegionNodeT *Element : elements()) { + if (!Element->isSubRegion()) { + BlockT *BB = Element->template getNodeAs(); if (SubRegion->contains(BB)) RI->setRegionFor(BB, SubRegion); @@ -399,12 +383,12 @@ void RegionBase::addSubRegion(RegionT *SubRegion, bool moveChildren) { } std::vector> Keep; - for (iterator I = begin(), E = end(); I != E; ++I) { - if (SubRegion->contains(I->get()) && I->get() != SubRegion) { - (*I)->parent = SubRegion; - SubRegion->children.push_back(std::move(*I)); + for (std::unique_ptr &R : *this) { + if (SubRegion->contains(R.get()) && R.get() != SubRegion) { + R->parent = SubRegion; + SubRegion->children.push_back(std::move(R)); } else - Keep.push_back(std::move(*I)); + Keep.push_back(std::move(R)); } children.clear(); @@ -418,9 +402,10 @@ template typename Tr::RegionT *RegionBase::removeSubRegion(RegionT *Child) { assert(Child->parent == this && "Child is not a child of this region!"); Child->parent = nullptr; - typename RegionSet::iterator I = std::find_if( - children.begin(), children.end(), - [&](const std::unique_ptr &R) { return R.get() == Child; }); + typename RegionSet::iterator I = + find_if(children, [&](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; @@ -446,10 +431,9 @@ typename Tr::RegionT *RegionBase::getExpandedRegion() const { RegionT *R = RI->getRegionFor(exit); if (R->getEntry() != exit) { - for (PredIterTy PI = InvBlockTraits::child_begin(getExit()), - PE = InvBlockTraits::child_end(getExit()); - PI != PE; ++PI) - if (!contains(*PI)) + for (BlockT *Pred : make_range(InvBlockTraits::child_begin(getExit()), + InvBlockTraits::child_end(getExit()))) + if (!contains(Pred)) return nullptr; if (Tr::getNumSuccessors(exit) == 1) return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT); @@ -459,10 +443,9 @@ typename Tr::RegionT *RegionBase::getExpandedRegion() const { while (R->getParent() && R->getParent()->getEntry() == exit) R = R->getParent(); - for (PredIterTy PI = InvBlockTraits::child_begin(getExit()), - PE = InvBlockTraits::child_end(getExit()); - PI != PE; ++PI) { - if (!(contains(*PI) || R->contains(*PI))) + for (BlockT *Pred : make_range(InvBlockTraits::child_begin(getExit()), + InvBlockTraits::child_end(getExit()))) { + if (!(contains(Pred) || R->contains(Pred))) return nullptr; } @@ -487,9 +470,8 @@ void RegionBase::print(raw_ostream &OS, bool print_tree, unsigned level, for (const auto *BB : blocks()) OS << BB->getName() << ", "; // TODO: remove the last "," } else if (Style == PrintRN) { - for (const_element_iterator I = element_begin(), E = element_end(); - I != E; ++I) { - OS << **I << ", "; // TODO: remove the last ", + for (const RegionNodeT *Element : elements()) { + OS << *Element << ", "; // TODO: remove the last ", } } @@ -497,8 +479,8 @@ void RegionBase::print(raw_ostream &OS, bool print_tree, unsigned level, } if (print_tree) { - for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI) - (*RI)->print(OS, print_tree, level + 1, Style); + for (const std::unique_ptr &R : *this) + R->print(OS, print_tree, level + 1, Style); } if (Style != PrintNone) @@ -514,15 +496,9 @@ void RegionBase::dump() const { template void RegionBase::clearNodeCache() { - // Free the cached nodes. - for (typename BBNodeMapT::iterator I = BBNodeMap.begin(), - IE = BBNodeMap.end(); - I != IE; ++I) - delete I->second; - BBNodeMap.clear(); - for (typename RegionT::iterator RI = begin(), RE = end(); RI != RE; ++RI) - (*RI)->clearNodeCache(); + for (std::unique_ptr &R : *this) + R->clearNodeCache(); } //===----------------------------------------------------------------------===// @@ -541,12 +517,12 @@ RegionInfoBase::~RegionInfoBase() { template void RegionInfoBase::verifyBBMap(const RegionT *R) const { assert(R && "Re must be non-null"); - for (auto I = R->element_begin(), E = R->element_end(); I != E; ++I) { - if (I->isSubRegion()) { - const RegionT *SR = I->template getNodeAs(); + for (const typename Tr::RegionNodeT *Element : R->elements()) { + if (Element->isSubRegion()) { + const RegionT *SR = Element->template getNodeAs(); verifyBBMap(SR); } else { - BlockT *BB = I->template getNodeAs(); + BlockT *BB = Element->template getNodeAs(); if (getRegionFor(BB) != R) llvm_unreachable("BB map does not match region nesting"); } @@ -556,10 +532,8 @@ void RegionInfoBase::verifyBBMap(const RegionT *R) const { template bool RegionInfoBase::isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit) const { - for (PredIterTy PI = InvBlockTraits::child_begin(BB), - PE = InvBlockTraits::child_end(BB); - PI != PE; ++PI) { - BlockT *P = *PI; + for (BlockT *P : make_range(InvBlockTraits::child_begin(BB), + InvBlockTraits::child_end(BB))) { if (DT->dominates(entry, P) && !DT->dominates(exit, P)) return false; } @@ -590,20 +564,18 @@ bool RegionInfoBase::isRegion(BlockT *entry, BlockT *exit) const { DST *exitSuccs = &DF->find(exit)->second; // Do not allow edges leaving the region. - for (typename DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end(); - SI != SE; ++SI) { - if (*SI == exit || *SI == entry) + for (BlockT *Succ : *entrySuccs) { + if (Succ == exit || Succ == entry) continue; - if (exitSuccs->find(*SI) == exitSuccs->end()) + if (exitSuccs->find(Succ) == exitSuccs->end()) return false; - if (!isCommonDomFrontier(*SI, entry, exit)) + if (!isCommonDomFrontier(Succ, entry, exit)) return false; } // Do not allow edges pointing into the region. - for (typename DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end(); - SI != SE; ++SI) { - if (DT->properlyDominates(entry, *SI) && *SI != exit) + for (BlockT *Succ : *exitSuccs) { + if (DT->properlyDominates(entry, Succ) && Succ != exit) return false; } @@ -663,7 +635,7 @@ typename Tr::RegionT *RegionInfoBase::createRegion(BlockT *entry, RegionT *region = new RegionT(entry, exit, static_cast(this), DT); - BBtoRegion.insert(std::make_pair(entry, region)); + BBtoRegion.insert({entry, region}); #ifdef EXPENSIVE_CHECKS region->verifyRegion(); @@ -758,9 +730,8 @@ void RegionInfoBase::buildRegionsTree(DomTreeNodeT *N, RegionT *region) { BBtoRegion[BB] = region; } - for (typename DomTreeNodeT::iterator CI = N->begin(), CE = N->end(); CI != CE; - ++CI) { - buildRegionsTree(*CI, region); + for (DomTreeNodeBase *C : *N) { + buildRegionsTree(C, region); } } @@ -850,10 +821,9 @@ RegionInfoBase::getMaxRegionExit(BlockT *BB) const { ExitR->getParent()->getEntry() == Exit) ExitR = ExitR->getParent(); - for (PredIterTy PI = InvBlockTraits::child_begin(Exit), - PE = InvBlockTraits::child_end(Exit); - PI != PE; ++PI) { - if (!R->contains(*PI) && !ExitR->contains(*PI)) + for (BlockT *Pred : make_range(InvBlockTraits::child_begin(Exit), + InvBlockTraits::child_end(Exit))) { + if (!R->contains(Pred) && !ExitR->contains(Pred)) break; } -- 2.7.4