[llvm] r195261 - Make the moved-from SmallPtrSet be a valid, empty, small-state object.

Chandler Carruth chandlerc at gmail.com
Wed Nov 20 10:29:56 PST 2013


Author: chandlerc
Date: Wed Nov 20 12:29:56 2013
New Revision: 195261

URL: http://llvm.org/viewvc/llvm-project?rev=195261&view=rev
Log:
Make the moved-from SmallPtrSet be a valid, empty, small-state object.
Enhance the tests to actually require moves in C++11 mode, in addition
to testing the moved-from state. Further enhance the tests to cover
copy-assignment into a moved-from object and moving a large-state
object. (Note that we can't really test small-state vs. large-state as
that isn't an observable property of the API really.) This should finish
addressing review on r195239.

Modified:
    llvm/trunk/include/llvm/ADT/SmallPtrSet.h
    llvm/trunk/lib/Support/SmallPtrSet.cpp
    llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp

Modified: llvm/trunk/include/llvm/ADT/SmallPtrSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallPtrSet.h?rev=195261&r1=195260&r2=195261&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallPtrSet.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallPtrSet.h Wed Nov 20 12:29:56 2013
@@ -140,7 +140,7 @@ protected:
 
   void CopyFrom(const SmallPtrSetImpl &RHS);
 #if LLVM_HAS_RVALUE_REFERENCES
-  void MoveFrom(SmallPtrSetImpl &&RHS);
+  void MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS);
 #endif
 };
 
@@ -300,7 +300,7 @@ public:
 #if LLVM_HAS_RVALUE_REFERENCES
   SmallPtrSet<PtrType, SmallSize>&
   operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) {
-    MoveFrom(std::move(RHS));
+    MoveFrom(SmallSizePowTwo, std::move(RHS));
     return *this;
   }
 #endif

Modified: llvm/trunk/lib/Support/SmallPtrSet.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SmallPtrSet.cpp?rev=195261&r1=195260&r2=195261&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SmallPtrSet.cpp (original)
+++ llvm/trunk/lib/Support/SmallPtrSet.cpp Wed Nov 20 12:29:56 2013
@@ -206,6 +206,12 @@ SmallPtrSetImpl::SmallPtrSetImpl(const v
   // Otherwise, we steal the large memory allocation and no copy is needed.
   CurArray = that.CurArray;
   that.CurArray = that.SmallArray;
+
+  // Make the "that" object small and empty.
+  that.CurArraySize = SmallSize;
+  assert(that.CurArray == that.SmallArray);
+  that.NumElements = 0;
+  that.NumTombstones = 0;
 }
 #endif
 
@@ -246,7 +252,7 @@ void SmallPtrSetImpl::CopyFrom(const Sma
 }
 
 #if LLVM_HAS_RVALUE_REFERENCES
-void SmallPtrSetImpl::MoveFrom(SmallPtrSetImpl &&RHS) {
+void SmallPtrSetImpl::MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS) {
   if (!isSmall())
     free(CurArray);
 
@@ -263,6 +269,12 @@ void SmallPtrSetImpl::MoveFrom(SmallPtrS
   CurArraySize = RHS.CurArraySize;
   NumElements = RHS.NumElements;
   NumTombstones = RHS.NumTombstones;
+
+  // Make the RHS small and empty.
+  RHS.CurArraySize = SmallSize;
+  assert(RHS.CurArray == RHS.SmallArray);
+  RHS.NumElements = 0;
+  RHS.NumTombstones = 0;
 }
 #endif
 

Modified: llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp?rev=195261&r1=195260&r2=195261&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp Wed Nov 20 12:29:56 2013
@@ -121,28 +121,42 @@ TEST(SmallPtrSetTest, CopyAndMoveTest) {
 
   s1 = s2;
   EXPECT_EQ(4U, s1.size());
+  EXPECT_EQ(4U, s2.size());
   for (int i = 0; i < 8; ++i)
     if (i < 4)
       EXPECT_TRUE(s1.count(&buf[i]));
     else
       EXPECT_FALSE(s1.count(&buf[i]));
 
-  SmallPtrSet<int *, 4> s3(llvm_move(s1));
+#if LLVM_HAS_RVALUE_REFERENCES
+  SmallPtrSet<int *, 4> s3(std::move(s1));
   EXPECT_EQ(4U, s3.size());
+  EXPECT_TRUE(s1.empty());
   for (int i = 0; i < 8; ++i)
     if (i < 4)
       EXPECT_TRUE(s3.count(&buf[i]));
     else
       EXPECT_FALSE(s3.count(&buf[i]));
 
-  // Move assign to the moved-from object.
+  // Move assign into the moved-from object. Also test move of a non-small
+  // container.
+  s3.insert(&buf[4]);
+  s3.insert(&buf[5]);
+  s3.insert(&buf[6]);
+  s3.insert(&buf[7]);
   s1 = llvm_move(s3);
-  EXPECT_EQ(4U, s1.size());
+  EXPECT_EQ(8U, s1.size());
+  EXPECT_TRUE(s3.empty());
   for (int i = 0; i < 8; ++i)
-    if (i < 4)
-      EXPECT_TRUE(s1.count(&buf[i]));
-    else
-      EXPECT_FALSE(s1.count(&buf[i]));
+    EXPECT_TRUE(s1.count(&buf[i]));
+
+  // Copy assign into a moved-from object.
+  s3 = s1;
+  EXPECT_EQ(8U, s3.size());
+  EXPECT_EQ(8U, s1.size());
+  for (int i = 0; i < 8; ++i)
+    EXPECT_TRUE(s3.count(&buf[i]));
+#endif
 }
 
 TEST(SmallPtrSetTest, SwapTest) {





More information about the llvm-commits mailing list