[llvm] ebf83c3 - [Analysis] simplify code for scaleShuffleMask

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 23 08:47:21 PDT 2020


Author: Sanjay Patel
Date: 2020-03-23T11:47:14-04:00
New Revision: ebf83c36e29ab6f480860de640790795870dcccb

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

LOG: [Analysis] simplify code for scaleShuffleMask

This is NFC-ish. The results should be identical, but perf is hopefully
better with the fast-path for no scaling. Added a unit test for that.

The code is adapted from what used to be the DAGCombiner equivalent
function before D76508 (rG0eeee83d7513).

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/VectorUtils.h
    llvm/unittests/Analysis/VectorUtilsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/VectorUtils.h b/llvm/include/llvm/Analysis/VectorUtils.h
index e96bc156b4cc..6797ed2369d8 100644
--- a/llvm/include/llvm/Analysis/VectorUtils.h
+++ b/llvm/include/llvm/Analysis/VectorUtils.h
@@ -343,23 +343,17 @@ template <typename T>
 void scaleShuffleMask(size_t Scale, ArrayRef<T> Mask,
                       SmallVectorImpl<T> &ScaledMask) {
   assert(Scale > 0 && "Unexpected scaling factor");
-  size_t NumElts = Mask.size();
-  ScaledMask.assign(NumElts * Scale, -1);
 
-  for (size_t i = 0; i != NumElts; ++i) {
-    int M = Mask[i];
-
-    // Repeat sentinel values in every mask element.
-    if (M < 0) {
-      for (size_t s = 0; s != Scale; ++s)
-        ScaledMask[(Scale * i) + s] = M;
-      continue;
-    }
-
-    // Scale mask element and increment across each mask element.
-    for (size_t s = 0; s != Scale; ++s)
-      ScaledMask[(Scale * i) + s] = (Scale * M) + s;
+  // Fast-path: if no scaling, then it is just a copy.
+  if (Scale == 1) {
+    ScaledMask.assign(Mask.begin(), Mask.end());
+    return;
   }
+
+  ScaledMask.clear();
+  for (int MaskElt : Mask)
+    for (int ScaleElt = 0; ScaleElt != (int)Scale; ++ScaleElt)
+      ScaledMask.push_back(MaskElt < 0 ? MaskElt : Scale * MaskElt + ScaleElt);
 }
 
 /// Compute a map of integer instructions to their minimum legal type

diff  --git a/llvm/unittests/Analysis/VectorUtilsTest.cpp b/llvm/unittests/Analysis/VectorUtilsTest.cpp
index f867c2ca0791..d471e79842ca 100644
--- a/llvm/unittests/Analysis/VectorUtilsTest.cpp
+++ b/llvm/unittests/Analysis/VectorUtilsTest.cpp
@@ -100,6 +100,8 @@ TEST_F(BasicTest, isSplat) {
 
 TEST_F(BasicTest, scaleShuffleMask) {
   SmallVector<int, 16> ScaledMask;
+  scaleShuffleMask<int>(1, {3,2,0,-2}, ScaledMask);
+  EXPECT_EQ(makeArrayRef<int>(ScaledMask), makeArrayRef<int>({3,2,0,-2}));
   scaleShuffleMask<int>(4, {3,2,0,-1}, ScaledMask);
   EXPECT_EQ(makeArrayRef<int>(ScaledMask), makeArrayRef<int>({12,13,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}));
 }


        


More information about the llvm-commits mailing list