[llvm] 1604a10 - [SLP][NFC]Avoid extra useless ConstantVector creation, use PointerUnion

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 27 10:54:25 PDT 2023


Author: Alexey Bataev
Date: 2023-04-27T10:48:14-07:00
New Revision: 1604a100f1ced3f1b49d64bd1c6a80ee609b9c8c

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

LOG: [SLP][NFC]Avoid extra useless ConstantVector creation, use PointerUnion
instead, NFC.

Better to use PointerUnion<Value *, const TreeEntry *> instead of extra
attempts of creating null vector values, where possible.

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 0879de730c34..d625a9451016 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -6727,7 +6727,7 @@ class BaseShuffleAnalysis {
 class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
   bool IsFinalized = false;
   SmallVector<int> CommonMask;
-  SmallVector<Value *, 2> InVectors;
+  SmallVector<PointerUnion<Value *, const TreeEntry *> , 2> InVectors;
   const TargetTransformInfo &TTI;
   InstructionCost Cost = 0;
   ArrayRef<Value *> VectorizedVals;
@@ -6939,7 +6939,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
     // If the resulting type is scalarized, do not adjust the cost.
     unsigned VecNumParts = TTI.getNumberOfParts(VecTy);
     if (VecNumParts == VecTy->getNumElements()) {
-      InVectors.assign(1, Constant::getNullValue(VecTy));
+      InVectors.assign(1, E);
       return nullptr;
     }
     DenseMap<Value *, int> ExtractVectorsTys;
@@ -7023,26 +7023,22 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
     // into a vector and can be represented as a permutation elements in a
     // single input vector or of 2 input vectors.
     Cost += computeExtractCost(VL, Mask, ShuffleKind);
-    InVectors.assign(1, Constant::getNullValue(VecTy));
+    InVectors.assign(1, E);
     return VecBase;
   }
   void add(const TreeEntry *E1, const TreeEntry *E2, ArrayRef<int> Mask) {
     CommonMask.assign(Mask.begin(), Mask.end());
-    InVectors.assign(
-        2, Constant::getNullValue(FixedVectorType::get(
-               E1->Scalars.front()->getType(),
-               std::max(E1->getVectorFactor(), E2->getVectorFactor()))));
+    InVectors.assign({E1, E2});
   }
   void add(const TreeEntry *E1, ArrayRef<int> Mask) {
     CommonMask.assign(Mask.begin(), Mask.end());
-    InVectors.assign(
-        1, Constant::getNullValue(FixedVectorType::get(
-               E1->Scalars.front()->getType(), E1->getVectorFactor())));
+    InVectors.assign(1, E1);
   }
   void gather(ArrayRef<Value *> VL, Value *Root = nullptr) {
     Cost += getBuildVectorCost(VL, Root);
     if (!Root) {
       assert(InVectors.empty() && "Unexpected input vectors for buildvector.");
+      // FIXME: Need to find a way to avoid use of getNullValue here.
       InVectors.assign(1, Constant::getNullValue(FixedVectorType::get(
                               VL.front()->getType(), VL.size())));
     }
@@ -7057,13 +7053,18 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
     if (all_of(CommonMask, [=](int Idx) { return Idx < Limit; }) &&
         ShuffleVectorInst::isIdentityMask(CommonMask))
       return Cost;
+    Type *ScalarTy;
+    if (auto *V = InVectors.front().dyn_cast<Value *>())
+      ScalarTy = cast<VectorType>(V->getType())->getElementType();
+    else
+      ScalarTy = InVectors.front()
+                     .get<const TreeEntry *>()
+                     ->Scalars.front()
+                     ->getType();
     return Cost +
            TTI.getShuffleCost(InVectors.size() == 2 ? TTI::SK_PermuteTwoSrc
                                                     : TTI::SK_PermuteSingleSrc,
-                              FixedVectorType::get(
-                                  cast<VectorType>(InVectors.front()->getType())
-                                      ->getElementType(),
-                                  CommonMask.size()),
+                              FixedVectorType::get(ScalarTy, CommonMask.size()),
                               CommonMask);
   }
 


        


More information about the llvm-commits mailing list