From 4abf9d3a5451474889e6cfaf0c53e05f4cb8c42b Mon Sep 17 00:00:00 2001 From: Tobias Grosser Date: Mon, 3 Mar 2014 13:00:39 +0000 Subject: [PATCH] [C++11] Add a basic block range view for RegionInfo This also switches the users in LLVM to ensure this functionality is tested. llvm-svn: 202705 --- llvm/include/llvm/Analysis/RegionInfo.h | 16 ++++++++++++++++ llvm/lib/Analysis/RegionInfo.cpp | 4 ++-- llvm/lib/Analysis/RegionPass.cpp | 5 ++--- llvm/lib/Analysis/RegionPrinter.cpp | 7 +++---- llvm/lib/Transforms/Scalar/StructurizeCFG.cpp | 7 +------ 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/llvm/include/llvm/Analysis/RegionInfo.h b/llvm/include/llvm/Analysis/RegionInfo.h index 8af02e3efbdd..a57b10088e18 100644 --- a/llvm/include/llvm/Analysis/RegionInfo.h +++ b/llvm/include/llvm/Analysis/RegionInfo.h @@ -27,6 +27,7 @@ #ifndef LLVM_ANALYSIS_REGIONINFO_H #define LLVM_ANALYSIS_REGIONINFO_H +#include "llvm/ADT/iterator_range.h" #include "llvm/ADT/PointerIntPair.h" #include "llvm/Analysis/DominanceFrontier.h" #include "llvm/Analysis/PostDominators.h" @@ -545,6 +546,21 @@ public: const_block_iterator block_end() const { return const_block_iterator(); } + + typedef iterator_range block_range; + typedef iterator_range const_block_range; + + /// @brief Returns a range view of the basic blocks in the region. + inline block_range blocks() { + return block_range(block_begin(), block_end()); + } + + /// @brief Returns a range view of the basic blocks in the region. + /// + /// This is the 'const' version of the range view. + inline const_block_range blocks() const { + return const_block_range(block_begin(), block_end()); + } //@} /// @name Element Iterators diff --git a/llvm/lib/Analysis/RegionInfo.cpp b/llvm/lib/Analysis/RegionInfo.cpp index 876d86b785b9..f4da598d8444 100644 --- a/llvm/lib/Analysis/RegionInfo.cpp +++ b/llvm/lib/Analysis/RegionInfo.cpp @@ -438,8 +438,8 @@ void Region::print(raw_ostream &OS, bool print_tree, unsigned level, OS.indent(level*2 + 2); if (Style == PrintBB) { - for (const_block_iterator I = block_begin(), E = block_end(); I != E; ++I) - OS << (*I)->getName() << ", "; // TODO: remove the last "," + 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 ", diff --git a/llvm/lib/Analysis/RegionPass.cpp b/llvm/lib/Analysis/RegionPass.cpp index 9208fa21d7ec..ac4e1149f086 100644 --- a/llvm/lib/Analysis/RegionPass.cpp +++ b/llvm/lib/Analysis/RegionPass.cpp @@ -195,9 +195,8 @@ public: virtual bool runOnRegion(Region *R, RGPassManager &RGM) { Out << Banner; - for (Region::block_iterator I = R->block_begin(), E = R->block_end(); - I != E; ++I) - (*I)->print(Out); + for (const auto &BB : R->blocks()) + BB->print(Out); return false; } diff --git a/llvm/lib/Analysis/RegionPrinter.cpp b/llvm/lib/Analysis/RegionPrinter.cpp index c5f1b925921b..6467f47cfbdb 100644 --- a/llvm/lib/Analysis/RegionPrinter.cpp +++ b/llvm/lib/Analysis/RegionPrinter.cpp @@ -121,11 +121,10 @@ struct DOTGraphTraits : public DOTGraphTraits { RegionInfo *RI = R->getRegionInfo(); - for (Region::const_block_iterator BI = R->block_begin(), - BE = R->block_end(); BI != BE; ++BI) - if (RI->getRegionFor(*BI) == R) + for (const auto &BB : R->blocks()) + if (RI->getRegionFor(BB) == R) O.indent(2 * (depth + 1)) << "Node" - << static_cast(RI->getTopLevelRegion()->getBBNode(*BI)) + << static_cast(RI->getTopLevelRegion()->getBBNode(BB)) << ";\n"; O.indent(2 * depth) << "}\n"; diff --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp index 60382889ef05..6066727b3ac5 100644 --- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp +++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp @@ -829,11 +829,7 @@ void StructurizeCFG::createFlow() { /// no longer dominate all their uses. Not sure if this is really nessasary void StructurizeCFG::rebuildSSA() { SSAUpdater Updater; - for (Region::block_iterator I = ParentRegion->block_begin(), - E = ParentRegion->block_end(); - I != E; ++I) { - - BasicBlock *BB = *I; + for (const auto &BB : ParentRegion->blocks()) for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II) { @@ -864,7 +860,6 @@ void StructurizeCFG::rebuildSSA() { Updater.RewriteUseAfterInsertions(*I); } } - } } /// \brief Run the transformation for each region found -- GitLab