Provide move semantics for (Small)BitVector.
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 1 Jun 2012 18:52:53 +0000 (18:52 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 1 Jun 2012 18:52:53 +0000 (18:52 +0000)
CodeGen makes a lot of BitVector copies.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157826 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/BitVector.h
include/llvm/ADT/SmallBitVector.h

index 3cbaf1a2f5ef969eb90101b22d11c439e2e1a65c..3e2e5f230a3a2fee212056f9f640fab9ad575e13 100644 (file)
@@ -14,6 +14,7 @@
 #ifndef LLVM_ADT_BITVECTOR_H
 #define LLVM_ADT_BITVECTOR_H
 
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
 #include <algorithm>
@@ -97,6 +98,13 @@ public:
     std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
   }
 
+#if LLVM_USE_RVALUE_REFERENCES
+  BitVector(BitVector &&RHS)
+    : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
+    RHS.Bits = 0;
+  }
+#endif
+
   ~BitVector() {
     std::free(Bits);
   }
@@ -371,6 +379,21 @@ public:
     return *this;
   }
 
+#if LLVM_USE_RVALUE_REFERENCES
+  const BitVector &operator=(BitVector &&RHS) {
+    if (this == &RHS) return *this;
+
+    std::free(Bits);
+    Bits = RHS.Bits;
+    Size = RHS.Size;
+    Capacity = RHS.Capacity;
+
+    RHS.Bits = 0;
+
+    return *this;
+  }
+#endif
+
   void swap(BitVector &RHS) {
     std::swap(Bits, RHS.Bits);
     std::swap(Size, RHS.Size);
index a3469a1c6226419fbe8d9ad8ae225f0c4a94addc..d43c7afb10458396712bcd9541045b37ad734112 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_ADT_SMALLBITVECTOR_H
 
 #include "llvm/ADT/BitVector.h"
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/MathExtras.h"
 #include <cassert>
 
@@ -152,6 +153,12 @@ public:
       switchToLarge(new BitVector(*RHS.getPointer()));
   }
 
+#if LLVM_USE_RVALUE_REFERENCES
+  SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
+    RHS.X = 1;
+  }
+#endif
+
   ~SmallBitVector() {
     if (!isSmall())
       delete getPointer();
@@ -422,6 +429,16 @@ public:
     return *this;
   }
 
+#if LLVM_USE_RVALUE_REFERENCES
+  const SmallBitVector &operator=(SmallBitVector &&RHS) {
+    if (this != &RHS) {
+      clear();
+      swap(RHS);
+    }
+    return *this;
+  }
+#endif
+
   void swap(SmallBitVector &RHS) {
     std::swap(X, RHS.X);
   }