[llvm] r332551 - [STLExtras] Add size() for ranges, and remove distance()
Vedant Kumar via llvm-commits
llvm-commits at lists.llvm.org
Wed May 16 16:20:43 PDT 2018
Author: vedantk
Date: Wed May 16 16:20:42 2018
New Revision: 332551
URL: http://llvm.org/viewvc/llvm-project?rev=332551&view=rev
Log:
[STLExtras] Add size() for ranges, and remove distance()
r332057 introduced distance() for ranges. Based on post-commit feedback,
this renames distance() to size(). The new size() is also only enabled
when the operation is O(1).
Differential Revision: https://reviews.llvm.org/D46976
Modified:
llvm/trunk/include/llvm/ADT/STLExtras.h
llvm/trunk/lib/Analysis/LazyCallGraph.cpp
llvm/trunk/lib/CodeGen/ImplicitNullChecks.cpp
llvm/trunk/lib/IR/Value.cpp
llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp
llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp
llvm/trunk/unittests/ADT/IteratorTest.cpp
llvm/trunk/unittests/IR/BasicBlockTest.cpp
Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Wed May 16 16:20:42 2018
@@ -1026,9 +1026,14 @@ void erase_if(Container &C, UnaryPredica
C.erase(remove_if(C, P), C.end());
}
-/// Wrapper function around std::distance which works with ranges.
+/// Get the size of a range. This is a wrapper function around std::distance
+/// which is only enabled when the operation is O(1).
template <typename R>
-auto distance(R &&Range)
+auto size(R &&Range, typename std::enable_if<
+ std::is_same<typename std::iterator_traits<decltype(
+ Range.begin())>::iterator_category,
+ std::random_access_iterator_tag>::value,
+ void>::type * = nullptr)
-> decltype(std::distance(Range.begin(), Range.end())) {
return std::distance(Range.begin(), Range.end());
}
Modified: llvm/trunk/lib/Analysis/LazyCallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LazyCallGraph.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/LazyCallGraph.cpp (original)
+++ llvm/trunk/lib/Analysis/LazyCallGraph.cpp Wed May 16 16:20:42 2018
@@ -1273,7 +1273,7 @@ LazyCallGraph::RefSCC::removeInternalRef
// the removal hasn't changed the structure at all. This is an important
// special case and we can directly exit the entire routine more
// efficiently as soon as we discover it.
- if (distance(RefSCCNodes) == NumRefSCCNodes) {
+ if (llvm::size(RefSCCNodes) == NumRefSCCNodes) {
// Clear out the low link field as we won't need it.
for (Node *N : RefSCCNodes)
N->LowLink = -1;
@@ -1739,7 +1739,7 @@ static void printNode(raw_ostream &OS, L
}
static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
- ptrdiff_t Size = distance(C);
+ ptrdiff_t Size = size(C);
OS << " SCC with " << Size << " functions:\n";
for (LazyCallGraph::Node &N : C)
@@ -1747,7 +1747,7 @@ static void printSCC(raw_ostream &OS, La
}
static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
- ptrdiff_t Size = distance(C);
+ ptrdiff_t Size = size(C);
OS << " RefSCC with " << Size << " call SCCs:\n";
for (LazyCallGraph::SCC &InnerC : C)
Modified: llvm/trunk/lib/CodeGen/ImplicitNullChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/ImplicitNullChecks.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/ImplicitNullChecks.cpp (original)
+++ llvm/trunk/lib/CodeGen/ImplicitNullChecks.cpp Wed May 16 16:20:42 2018
@@ -597,7 +597,7 @@ MachineInstr *ImplicitNullChecks::insert
unsigned DefReg = NoRegister;
if (NumDefs != 0) {
DefReg = MI->defs().begin()->getReg();
- assert(distance(MI->defs()) == 1 && "expected exactly one def!");
+ assert(NumDefs == 1 && "expected exactly one def!");
}
FaultMaps::FaultKind FK;
Modified: llvm/trunk/lib/IR/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Value.cpp (original)
+++ llvm/trunk/lib/IR/Value.cpp Wed May 16 16:20:42 2018
@@ -167,7 +167,9 @@ bool Value::isUsedInBasicBlock(const Bas
return false;
}
-unsigned Value::getNumUses() const { return (unsigned)distance(uses()); }
+unsigned Value::getNumUses() const {
+ return (unsigned)std::distance(use_begin(), use_end());
+}
static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
ST = nullptr;
Modified: llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/GVNHoist.cpp Wed May 16 16:20:42 2018
@@ -578,7 +578,7 @@ private:
// Returns true when the values are flowing out to each edge.
bool valueAnticipable(CHIArgs C, TerminatorInst *TI) const {
- if (TI->getNumSuccessors() > (unsigned)distance(C))
+ if (TI->getNumSuccessors() > (unsigned)size(C))
return false; // Not enough args in this CHI.
for (auto CHI : C) {
Modified: llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp Wed May 16 16:20:42 2018
@@ -285,7 +285,8 @@ bool MergedLoadStoreMotion::mergeStores(
return false; // No. More than 2 predecessors.
// #Instructions in Succ1 for Compile Time Control
- int Size1 = distance(Pred1->instructionsWithoutDebug());
+ auto InstsNoDbg = Pred1->instructionsWithoutDebug();
+ int Size1 = std::distance(InstsNoDbg.begin(), InstsNoDbg.end());
int NStores = 0;
for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
Modified: llvm/trunk/unittests/ADT/IteratorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/IteratorTest.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/IteratorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/IteratorTest.cpp Wed May 16 16:20:42 2018
@@ -369,8 +369,8 @@ TEST(RangeTest, Distance) {
std::vector<int> v1;
std::vector<int> v2{1, 2, 3};
- EXPECT_EQ(std::distance(v1.begin(), v1.end()), distance(v1));
- EXPECT_EQ(std::distance(v2.begin(), v2.end()), distance(v2));
+ EXPECT_EQ(std::distance(v1.begin(), v1.end()), size(v1));
+ EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
}
} // anonymous namespace
Modified: llvm/trunk/unittests/IR/BasicBlockTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/BasicBlockTest.cpp?rev=332551&r1=332550&r2=332551&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/BasicBlockTest.cpp (original)
+++ llvm/trunk/unittests/IR/BasicBlockTest.cpp Wed May 16 16:20:42 2018
@@ -73,9 +73,9 @@ TEST(BasicBlockTest, PhiRange) {
auto isPhi = [](Instruction &I) { return isa<PHINode>(&I); };
auto Phis = make_filter_range(*BB, isPhi);
auto ReversedPhis = reverse(make_filter_range(*BB, isPhi));
- EXPECT_EQ(distance(Phis), 3);
+ EXPECT_EQ(std::distance(Phis.begin(), Phis.end()), 3);
EXPECT_EQ(&*Phis.begin(), P1);
- EXPECT_EQ(distance(ReversedPhis), 3);
+ EXPECT_EQ(std::distance(ReversedPhis.begin(), ReversedPhis.end()), 3);
EXPECT_EQ(&*ReversedPhis.begin(), P3);
// And iterate a const range.
@@ -87,7 +87,8 @@ TEST(BasicBlockTest, PhiRange) {
}
#define CHECK_ITERATORS(Range1, Range2) \
- EXPECT_EQ(distance(Range1), distance(Range2)); \
+ EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \
+ std::distance(Range2.begin(), Range2.end())); \
for (auto Pair : zip(Range1, Range2)) \
EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
More information about the llvm-commits
mailing list