[llvm] 1010c44 - [ValueTracking] Add computeKnownBits DemandedElts support to EXTRACTELEMENT/OR/BSWAP/BITREVERSE instructions (PR36319)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 18 11:51:04 PDT 2020


Author: Simon Pilgrim
Date: 2020-03-18T18:49:58Z
New Revision: 1010c44b4c0e32afb49597ac67f1d0768de8265f

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

LOG: [ValueTracking] Add computeKnownBits DemandedElts support to EXTRACTELEMENT/OR/BSWAP/BITREVERSE instructions (PR36319)

These are all covered by the bswap/bitreverse vector tests.

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstSimplify/bitreverse.ll
    llvm/test/Transforms/InstSimplify/bswap.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 48591c54a762..d47d38dca8a6 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1128,8 +1128,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
     break;
   }
   case Instruction::Or:
-    computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
-    computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
+    computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
+    computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
 
     // Output known-0 bits are only known if clear in both the LHS & RHS.
     Known.Zero &= Known2.Zero;
@@ -1605,12 +1605,12 @@ static void computeKnownBitsFromOperator(const Operator *I,
       switch (II->getIntrinsicID()) {
       default: break;
       case Intrinsic::bitreverse:
-        computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
+        computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
         Known.Zero |= Known2.Zero.reverseBits();
         Known.One |= Known2.One.reverseBits();
         break;
       case Intrinsic::bswap:
-        computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
+        computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
         Known.Zero |= Known2.Zero.byteSwap();
         Known.One |= Known2.One.byteSwap();
         break;
@@ -1762,13 +1762,20 @@ static void computeKnownBitsFromOperator(const Operator *I,
     }
     break;
   }
-  case Instruction::ExtractElement:
-    // Look through extract element. At the moment we keep this simple and skip
-    // tracking the specific element. But at least we might find information
-    // valid for all elements of the vector (for example if vector is sign
-    // extended, shifted, etc).
-    computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
+  case Instruction::ExtractElement: {
+    // Look through extract element. If the index is non-constant or
+    // out-of-range demand all elements, otherwise just the extracted element.
+    auto* EEI = cast<ExtractElementInst>(I);
+    const Value* Vec = EEI->getVectorOperand();
+    const Value* Idx = EEI->getIndexOperand();
+    auto *CIdx = dyn_cast<ConstantInt>(Idx);
+    unsigned NumElts = Vec->getType()->getVectorNumElements();
+    APInt DemandedVecElts = APInt::getAllOnesValue(NumElts);
+    if (CIdx && CIdx->getValue().ult(NumElts))
+      DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
+    computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
     break;
+  }
   case Instruction::ExtractValue:
     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
       const ExtractValueInst *EVI = cast<ExtractValueInst>(I);

diff  --git a/llvm/test/Transforms/InstSimplify/bitreverse.ll b/llvm/test/Transforms/InstSimplify/bitreverse.ll
index fc7a9df00e2a..2194fd45418b 100644
--- a/llvm/test/Transforms/InstSimplify/bitreverse.ll
+++ b/llvm/test/Transforms/InstSimplify/bitreverse.ll
@@ -16,11 +16,7 @@ define i1 @test1(i32 %arg) {
 
 define i1 @test1v(<2 x i32> %arg) {
 ; CHECK-LABEL: @test1v(
-; CHECK-NEXT:    [[A:%.*]] = or <2 x i32> [[ARG:%.*]], <i32 1, i32 0>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> [[A]])
-; CHECK-NEXT:    [[C:%.*]] = extractelement <2 x i32> [[B]], i32 0
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i32 [[C]], 0
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = or <2 x i32> %arg, <i32 1, i32 0>
   %b = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> %a)
@@ -41,11 +37,7 @@ define i1 @test2(i32 %arg) {
 
 define i1 @test2v(<2 x i32> %arg) {
 ; CHECK-LABEL: @test2v(
-; CHECK-NEXT:    [[A:%.*]] = or <2 x i32> [[ARG:%.*]], <i32 0, i32 1024>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> [[A]])
-; CHECK-NEXT:    [[C:%.*]] = extractelement <2 x i32> [[B]], i32 1
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i32 [[C]], 0
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = or <2 x i32> %arg, <i32 0, i32 1024>
   %b = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> %a)
@@ -67,12 +59,7 @@ define i1 @test3(i32 %arg) {
 
 define i1 @test3v(<2 x i32> %arg) {
 ; CHECK-LABEL: @test3v(
-; CHECK-NEXT:    [[A:%.*]] = and <2 x i32> [[ARG:%.*]], <i32 1, i32 -1>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> [[A]])
-; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[B]], <i32 1, i32 -1>
-; CHECK-NEXT:    [[EXT:%.*]] = extractelement <2 x i32> [[AND]], i32 0
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i32 [[EXT]], 1
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = and <2 x i32> %arg, <i32 1, i32 -1>
   %b = call <2 x i32> @llvm.bitreverse.v2i32(<2 x i32> %a)

diff  --git a/llvm/test/Transforms/InstSimplify/bswap.ll b/llvm/test/Transforms/InstSimplify/bswap.ll
index 904b47fd1c46..3e7616c3bdc5 100644
--- a/llvm/test/Transforms/InstSimplify/bswap.ll
+++ b/llvm/test/Transforms/InstSimplify/bswap.ll
@@ -16,11 +16,7 @@ define i1 @test1(i16 %arg) {
 
 define i1 @test1v(<2 x i16> %arg) {
 ; CHECK-LABEL: @test1v(
-; CHECK-NEXT:    [[A:%.*]] = or <2 x i16> [[ARG:%.*]], <i16 1, i16 0>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
-; CHECK-NEXT:    [[C:%.*]] = extractelement <2 x i16> [[B]], i32 0
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i16 [[C]], 0
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = or <2 x i16> %arg, <i16 1, i16 0>
   %b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)
@@ -41,11 +37,7 @@ define i1 @test2(i16 %arg) {
 
 define i1 @test2v(<2 x i16> %arg) {
 ; CHECK-LABEL: @test2v(
-; CHECK-NEXT:    [[A:%.*]] = or <2 x i16> [[ARG:%.*]], <i16 0, i16 1024>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
-; CHECK-NEXT:    [[C:%.*]] = extractelement <2 x i16> [[B]], i32 1
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i16 [[C]], 0
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = or <2 x i16> %arg, <i16 0, i16 1024>
   %b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)
@@ -67,12 +59,7 @@ define i1 @test3(i16 %arg) {
 
 define i1 @test3v(<2 x i16> %arg) {
 ; CHECK-LABEL: @test3v(
-; CHECK-NEXT:    [[A:%.*]] = and <2 x i16> [[ARG:%.*]], <i16 1, i16 -1>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
-; CHECK-NEXT:    [[C:%.*]] = extractelement <2 x i16> [[B]], i32 0
-; CHECK-NEXT:    [[AND:%.*]] = and i16 [[C]], 1
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i16 [[AND]], 1
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = and <2 x i16> %arg, <i16 1, i16 -1>
   %b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)
@@ -95,12 +82,7 @@ define i1 @test4(i16 %arg) {
 
 define i1 @test4v(<2 x i16> %arg) {
 ; CHECK-LABEL: @test4v(
-; CHECK-NEXT:    [[A:%.*]] = and <2 x i16> [[ARG:%.*]], <i16 511, i16 511>
-; CHECK-NEXT:    [[B:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[A]])
-; CHECK-NEXT:    [[AND:%.*]] = and <2 x i16> [[B]], <i16 255, i16 256>
-; CHECK-NEXT:    [[EXT:%.*]] = extractelement <2 x i16> [[AND]], i32 1
-; CHECK-NEXT:    [[RES:%.*]] = icmp eq i16 [[EXT]], 1
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 false
 ;
   %a = and <2 x i16> %arg, <i16 511, i16 511>
   %b = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> %a)


        


More information about the llvm-commits mailing list