[llvm] c2072d9 - [CVP] Support vectors for and elision

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 3 05:40:58 PDT 2024


Author: Nikita Popov
Date: 2024-07-03T14:40:48+02:00
New Revision: c2072d993a443f08ab1bac8a3d5575e1a48663c7

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

LOG: [CVP] Support vectors for and elision

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
    llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 34304c2245e30..2bfae0ec28e17 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -33,6 +33,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Operator.h"
+#include "llvm/IR/PatternMatch.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/Type.h"
 #include "llvm/IR/Value.h"
@@ -1189,21 +1190,20 @@ static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI) {
 }
 
 static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI) {
-  if (BinOp->getType()->isVectorTy())
-    return false;
+  using namespace llvm::PatternMatch;
 
   // Pattern match (and lhs, C) where C includes a superset of bits which might
   // be set in lhs.  This is a common truncation idiom created by instcombine.
   const Use &LHS = BinOp->getOperandUse(0);
-  ConstantInt *RHS = dyn_cast<ConstantInt>(BinOp->getOperand(1));
-  if (!RHS || !RHS->getValue().isMask())
+  const APInt *RHS;
+  if (!match(BinOp->getOperand(1), m_LowBitMask(RHS)))
     return false;
 
   // We can only replace the AND with LHS based on range info if the range does
   // not include undef.
   ConstantRange LRange =
       LVI->getConstantRangeAtUse(LHS, /*UndefAllowed=*/false);
-  if (!LRange.getUnsignedMax().ule(RHS->getValue()))
+  if (!LRange.getUnsignedMax().ule(*RHS))
     return false;
 
   BinOp->replaceAllUsesWith(LHS);

diff  --git a/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll b/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll
index a06fa2c106609..0024b0a5c75c9 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll
@@ -199,15 +199,24 @@ define <2 x float> @sitofp(<2 x i8> %a) {
   ret <2 x float> %res
 }
 
-; TODO: Add support for this.
 define <2 x i16> @and(<2 x i8> %a) {
 ; CHECK-LABEL: define <2 x i16> @and(
 ; CHECK-SAME: <2 x i8> [[A:%.*]]) {
 ; CHECK-NEXT:    [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16>
-; CHECK-NEXT:    [[RES:%.*]] = and <2 x i16> [[ZEXT]], <i16 255, i16 255>
-; CHECK-NEXT:    ret <2 x i16> [[RES]]
+; CHECK-NEXT:    ret <2 x i16> [[ZEXT]]
 ;
   %zext = zext <2 x i8> %a to <2 x i16>
   %res = and <2 x i16> %zext, splat (i16 u0xff)
   ret <2 x i16> %res
 }
+
+define <2 x i16> @and_with_poison(<2 x i8> %a) {
+; CHECK-LABEL: define <2 x i16> @and_with_poison(
+; CHECK-SAME: <2 x i8> [[A:%.*]]) {
+; CHECK-NEXT:    [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16>
+; CHECK-NEXT:    ret <2 x i16> [[ZEXT]]
+;
+  %zext = zext <2 x i8> %a to <2 x i16>
+  %res = and <2 x i16> %zext, <i16 u0xff, i16 poison>
+  ret <2 x i16> %res
+}


        


More information about the llvm-commits mailing list