[clang] 4d0312c - Add proper move ctor/move assign to APValue. NFCI.

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 6 04:07:31 PDT 2020


Author: Benjamin Kramer
Date: 2020-09-06T13:02:11+02:00
New Revision: 4d0312c8e05be5353c6c29b31036647dceca3ce5

URL: https://github.com/llvm/llvm-project/commit/4d0312c8e05be5353c6c29b31036647dceca3ce5
DIFF: https://github.com/llvm/llvm-project/commit/4d0312c8e05be5353c6c29b31036647dceca3ce5.diff

LOG: Add proper move ctor/move assign to APValue. NFCI.

Swapping 64 bytes to make a move isn't cheap.

Added: 
    

Modified: 
    clang/include/clang/AST/APValue.h
    clang/lib/AST/APValue.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h
index 87e4bd7f84c1..5103cfa8604e 100644
--- a/clang/include/clang/AST/APValue.h
+++ b/clang/include/clang/AST/APValue.h
@@ -304,7 +304,7 @@ class APValue {
     MakeComplexFloat(); setComplexFloat(std::move(R), std::move(I));
   }
   APValue(const APValue &RHS);
-  APValue(APValue &&RHS) : Kind(None) { swap(RHS); }
+  APValue(APValue &&RHS);
   APValue(LValueBase B, const CharUnits &O, NoLValuePath N,
           bool IsNullPtr = false)
       : Kind(None) {
@@ -339,6 +339,9 @@ class APValue {
     return Result;
   }
 
+  APValue &operator=(const APValue &RHS);
+  APValue &operator=(APValue &&RHS);
+
   ~APValue() {
     if (Kind != None && Kind != Indeterminate)
       DestroyDataAndMakeUninit();
@@ -591,12 +594,6 @@ class APValue {
     ((AddrLabelDiffData*)(char*)Data.buffer)->RHSExpr = RHSExpr;
   }
 
-  /// Assign by swapping from a copy of the RHS.
-  APValue &operator=(APValue RHS) {
-    swap(RHS);
-    return *this;
-  }
-
 private:
   void DestroyDataAndMakeUninit();
   void MakeInt() {

diff  --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index 2a8834b4db0c..08ae0ff3c67d 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -304,6 +304,25 @@ APValue::APValue(const APValue &RHS) : Kind(None) {
   }
 }
 
+APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) {
+  RHS.Kind = None;
+}
+
+APValue &APValue::operator=(const APValue &RHS) {
+  if (this != &RHS)
+    *this = APValue(RHS);
+  return *this;
+}
+
+APValue &APValue::operator=(APValue &&RHS) {
+  if (Kind != None && Kind != Indeterminate)
+    DestroyDataAndMakeUninit();
+  Kind = RHS.Kind;
+  Data = RHS.Data;
+  RHS.Kind = None;
+  return *this;
+}
+
 void APValue::DestroyDataAndMakeUninit() {
   if (Kind == Int)
     ((APSInt*)(char*)Data.buffer)->~APSInt();
@@ -372,10 +391,7 @@ bool APValue::needsCleanup() const {
 
 void APValue::swap(APValue &RHS) {
   std::swap(Kind, RHS.Kind);
-  char TmpData[DataSize];
-  memcpy(TmpData, Data.buffer, DataSize);
-  memcpy(Data.buffer, RHS.Data.buffer, DataSize);
-  memcpy(RHS.Data.buffer, TmpData, DataSize);
+  std::swap(Data, RHS.Data);
 }
 
 static double GetApproxValue(const llvm::APFloat &F) {


        


More information about the cfe-commits mailing list