[PATCH] D78425: [ValueLattice] Add move constructor

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 18 07:00:38 PDT 2020


nikic created this revision.
nikic added a reviewer: fhahn.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Following the rule of five, declare move constructor and move assignment operator for ValueLatticeElement. This allows moving the ConstantRange (and the APInts it contains) rather than copying it.

This gives no improvement for most practical purposes, because we tend to work on ranges with <= 64 bit, in which case copies are cheap. This would only make a difference if the APInts have allocated backing storage.

So, not sure if worthwhile, but I figured it put it out there anyway...


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78425

Files:
  include/llvm/Analysis/ValueLattice.h


Index: include/llvm/Analysis/ValueLattice.h
===================================================================
--- include/llvm/Analysis/ValueLattice.h
+++ include/llvm/Analysis/ValueLattice.h
@@ -114,6 +114,10 @@
     *this = Other;
   }
 
+  ValueLatticeElement(ValueLatticeElement &&Other) : Tag(unknown) {
+    *this = std::move(Other);
+  }
+
   /// Custom assignment operator, to ensure Range gets initialized when
   /// assigning a constant range lattice element.
   ValueLatticeElement &operator=(const ValueLatticeElement &Other) {
@@ -122,12 +126,6 @@
     if (isConstantRange() && !Other.isConstantRange())
       Range.~ConstantRange();
 
-    // If we change the state of this from a valid ConstVal to another a state
-    // without a valid ConstVal, zero the pointer.
-    if ((isConstant() || isNotConstant()) && !Other.isConstant() &&
-        !Other.isNotConstant())
-      ConstVal = nullptr;
-
     switch (Other.Tag) {
     case constantrange:
     case constantrange_including_undef:
@@ -150,6 +148,30 @@
     return *this;
   }
 
+  ValueLatticeElement &operator=(ValueLatticeElement &&Other) {
+    if (isConstantRange())
+      Range.~ConstantRange();
+
+    switch (Other.Tag) {
+    case constantrange:
+    case constantrange_including_undef:
+      new (&Range) ConstantRange(std::move(Other.Range));
+      NumRangeExtensions = Other.NumRangeExtensions;
+      break;
+    case constant:
+    case notconstant:
+      ConstVal = Other.ConstVal;
+      break;
+    case overdefined:
+    case unknown:
+    case undef:
+      break;
+    }
+    Tag = Other.Tag;
+    Other.Tag = unknown;
+    return *this;
+  }
+
   static ValueLatticeElement get(Constant *C) {
     ValueLatticeElement Res;
     if (isa<UndefValue>(C))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78425.258517.patch
Type: text/x-patch
Size: 1753 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200418/e586e020/attachment.bin>


More information about the llvm-commits mailing list