[llvm] 61df774 - [ValueTracking] Improve analysis of knownbits from incoming phi edges.

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 22 08:59:14 PDT 2023


Author: Noah Goldstein
Date: 2023-08-22T10:59:02-05:00
New Revision: 61df774ab7b7c630ba2a2f4e36cb8470e93c857b

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

LOG: [ValueTracking] Improve analysis of knownbits from incoming phi edges.

Just fill in missing cases (TODO) for `ugt`, `uge`, `sgt`, `sge`,
`slt`, and `sle`. These are all in the same spirit as `ult`/`uge`, but
each of the other conditions have different constraints.

Proofs: https://alive2.llvm.org/ce/z/gnj4o-

Differential Revision: https://reviews.llvm.org/D157800

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Analysis/ValueTracking/phi-known-bits.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 02ea7a3369058b..21aa8bd3e35e85 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1476,21 +1476,9 @@ static void computeKnownBitsFromOperator(const Operator *I,
               // If we're using the false successor, invert the predicate.
               if (FalseSucc == P->getParent())
                 Pred = CmpInst::getInversePredicate(Pred);
-
-              switch (Pred) {
-              case CmpInst::Predicate::ICMP_EQ:
-                Known2 = KnownBits::makeConstant(*RHSC);
-                break;
-              case CmpInst::Predicate::ICMP_ULE:
-                Known2.Zero.setHighBits(RHSC->countl_zero());
-                break;
-              case CmpInst::Predicate::ICMP_ULT:
-                Known2.Zero.setHighBits((*RHSC - 1).countl_zero());
-                break;
-              default:
-                // TODO - add additional integer predicate handling.
-                break;
-              }
+              // Get the knownbits implied by the incoming phi condition.
+              auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
+              Known2 = CR.toKnownBits();
             }
           }
         }

diff  --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
index be64f9cfe07aa2..2a60cd61788da9 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
@@ -7,9 +7,7 @@ define i8 @phi_ugt_high_bits(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], -65
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 64
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -32,9 +30,7 @@ define i8 @phi_ult_low_bits(i8 %x) {
 ; CHECK:       T:
 ; CHECK-NEXT:    br label [[F]]
 ; CHECK:       F:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[T]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 64
 ;
 entry:
   %cmp = icmp ult i8 %x, 192
@@ -76,9 +72,7 @@ define i8 @phi_uge_high_bits(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], -65
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 64
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -122,9 +116,7 @@ define i8 @phi_sge_high_bits(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], 95
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 96
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 96
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -168,9 +160,7 @@ define i8 @phi_sgt_high_bits(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ 0, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], -128
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -191,9 +181,7 @@ define i8 @phi_sgt_high_bits2(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], 63
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 64
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -237,9 +225,7 @@ define i8 @phi_slt_high_bits(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], -128
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 -128
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -260,9 +246,7 @@ define i8 @phi_slt_high_bits2(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], -64
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ 0, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -306,9 +290,7 @@ define i8 @phi_sle_high_bits(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], -64
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ 0, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -331,9 +313,7 @@ define i8 @phi_sle_low_bits(i8 %x) {
 ; CHECK:       T:
 ; CHECK-NEXT:    br label [[F]]
 ; CHECK:       F:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[T]] ]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[V]], 64
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 64
 ;
 entry:
   %cmp = icmp sle i8 %x, 64


        


More information about the llvm-commits mailing list