[llvm] r202662 - Give APInt move semantics.

Benjamin Kramer benny.kra at googlemail.com
Sun Mar 2 12:56:28 PST 2014


Author: d0k
Date: Sun Mar  2 14:56:28 2014
New Revision: 202662

URL: http://llvm.org/viewvc/llvm-project?rev=202662&view=rev
Log:
Give APInt move semantics.

The interaction between defaulted operators and move elision isn't
totally obvious, add a unit test so it doesn't break unintentionally.

Added:
    llvm/trunk/unittests/ADT/APSIntTest.cpp
Modified:
    llvm/trunk/include/llvm/ADT/APSInt.h
    llvm/trunk/unittests/ADT/CMakeLists.txt

Modified: llvm/trunk/include/llvm/ADT/APSInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APSInt.h?rev=202662&r1=202661&r2=202662&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APSInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APSInt.h Sun Mar  2 14:56:28 2014
@@ -30,18 +30,12 @@ public:
   explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
    : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
 
-  explicit APSInt(const APInt &I, bool isUnsigned = true)
-   : APInt(I), IsUnsigned(isUnsigned) {}
+  explicit APSInt(APInt I, bool isUnsigned = true)
+   : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
 
-  APSInt &operator=(const APSInt &RHS) {
-    APInt::operator=(RHS);
-    IsUnsigned = RHS.IsUnsigned;
-    return *this;
-  }
-
-  APSInt &operator=(const APInt &RHS) {
+  APSInt &operator=(APInt RHS) {
     // Retain our current sign.
-    APInt::operator=(RHS);
+    APInt::operator=(std::move(RHS));
     return *this;
   }
 

Added: llvm/trunk/unittests/ADT/APSIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APSIntTest.cpp?rev=202662&view=auto
==============================================================================
--- llvm/trunk/unittests/ADT/APSIntTest.cpp (added)
+++ llvm/trunk/unittests/ADT/APSIntTest.cpp Sun Mar  2 14:56:28 2014
@@ -0,0 +1,44 @@
+//===- llvm/unittest/ADT/APSIntTest.cpp - APSInt unit tests ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/APSInt.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(APSIntTest, MoveTest) {
+  APSInt A(32, true);
+  EXPECT_TRUE(A.isUnsigned());
+
+  APSInt B(128, false);
+  A = B;
+  EXPECT_FALSE(A.isUnsigned());
+
+  APSInt C(B);
+  EXPECT_FALSE(C.isUnsigned());
+
+  APInt Wide(256, 0);
+  const uint64_t *Bits = Wide.getRawData();
+  APSInt D(std::move(Wide));
+  EXPECT_TRUE(D.isUnsigned());
+  EXPECT_EQ(Bits, D.getRawData()); // Verify that "Wide" was really moved.
+
+  A = APSInt(64, true);
+  EXPECT_TRUE(A.isUnsigned());
+
+  Wide = APInt(128, 1);
+  Bits = Wide.getRawData();
+  A = std::move(Wide);
+  EXPECT_TRUE(A.isUnsigned());
+  EXPECT_EQ(Bits, A.getRawData()); // Verify that "Wide" was really moved.
+}
+
+}

Modified: llvm/trunk/unittests/ADT/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/CMakeLists.txt?rev=202662&r1=202661&r2=202662&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/CMakeLists.txt (original)
+++ llvm/trunk/unittests/ADT/CMakeLists.txt Sun Mar  2 14:56:28 2014
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
 set(ADTSources
   APFloatTest.cpp
   APIntTest.cpp
+  APSIntTest.cpp
   ArrayRefTest.cpp
   BitVectorTest.cpp
   DAGDeltaAlgorithmTest.cpp





More information about the llvm-commits mailing list