[llvm] a11e5b3 - [InstCombine][X86] simplifyX86immShift - handle variable out-of-range vector shift by immediate amounts (PR40391)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 19 11:28:37 PDT 2020


Author: Simon Pilgrim
Date: 2020-03-19T18:27:31Z
New Revision: a11e5b32dfb1fbdb72f00440351f9e3956788d13

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

LOG: [InstCombine][X86] simplifyX86immShift - handle variable out-of-range vector shift by immediate amounts (PR40391)

If we know the SSE shift amount is out of range then we can simplify to zero value (logical) or a 'signsplat' bitwidth-1 shift (arithmetic). This allows us to remove the equivalent ConstantInt constant folding path from simplifyX86immShift.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index d655cbbc3b0b..38c7a95f90dd 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -380,7 +380,8 @@ static Value *simplifyX86immShift(const IntrinsicInst &II,
   unsigned BitWidth = SVT->getPrimitiveSizeInBits();
 
   // If the shift amount is guaranteed to be in-range we can replace it with a
-  // generic shift.
+  // generic shift. If its guaranteed to be out of range, logical shifts combine to
+  // zero and arithmetic shifts are clamped to (BitWidth - 1).
   if (IsImm) {
     assert(Amt->getType()->isIntegerTy(32) &&
            "Unexpected shift-by-immediate type");
@@ -393,13 +394,18 @@ static Value *simplifyX86immShift(const IntrinsicInst &II,
                                         : Builder.CreateLShr(Vec, Amt))
                            : Builder.CreateAShr(Vec, Amt));
     }
+    if (KnownAmtBits.getMinValue().uge(BitWidth)) {
+      if (LogicalShift)
+        return ConstantAggregateZero::get(VT);
+      Amt = ConstantInt::get(SVT, BitWidth - 1);
+      return Builder.CreateAShr(Vec, Builder.CreateVectorSplat(VWidth, Amt));
+    }
   }
 
-  // Simplify if count is constant.
+  // Simplify if count is constant vector.
   auto CAZ = dyn_cast<ConstantAggregateZero>(Amt);
   auto CDV = dyn_cast<ConstantDataVector>(Amt);
-  auto CInt = dyn_cast<ConstantInt>(Amt);
-  if (!CAZ && !CDV && !CInt)
+  if (!CAZ && !CDV)
     return nullptr;
 
   APInt Count(64, 0);
@@ -419,8 +425,6 @@ static Value *simplifyX86immShift(const IntrinsicInst &II,
       Count |= SubElt->getValue().zextOrTrunc(64);
     }
   }
-  else if (CInt)
-    Count = CInt->getValue();
 
   // If shift-by-zero then just return the original value.
   if (Count.isNullValue())


        


More information about the llvm-commits mailing list