[llvm] 39e9862 - [ValueTracking] Use predicates for incoming phi-edges to deduce non-zero

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


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

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

LOG: [ValueTracking] Use predicates for incoming phi-edges to deduce non-zero

This is basically a copy and paste of the same logic we do in
`computeKnownBits` but adapts it for just `isKnownNonZero`.

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

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Analysis/ValueTracking/phi-known-nonzero.ll
    llvm/test/Transforms/FunctionAttrs/nonnull.ll
    llvm/test/Transforms/JumpThreading/phi-known.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 21aa8bd3e35e85..bdf2bc863fc9d2 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2674,6 +2674,23 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
       if (U.get() == PN)
         return true;
       RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
+      // Check if the branch on the phi excludes zero.
+      ICmpInst::Predicate Pred;
+      Value *X;
+      BasicBlock *TrueSucc, *FalseSucc;
+      if (match(RecQ.CxtI,
+                m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
+                     m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
+        // Check for cases of duplicate successors.
+        if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
+          // If we're using the false successor, invert the predicate.
+          if (FalseSucc == PN->getParent())
+            Pred = CmpInst::getInversePredicate(Pred);
+          if (cmpExcludesZero(Pred, X))
+            return true;
+        }
+      }
+      // Finally recurse on the edge and check it directly.
       return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ);
     });
   }

diff  --git a/llvm/test/Analysis/ValueTracking/phi-known-nonzero.ll b/llvm/test/Analysis/ValueTracking/phi-known-nonzero.ll
index 4fee6a4a9e0c74..d80d1704c73d1d 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-nonzero.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-nonzero.ll
@@ -7,9 +7,7 @@ define i1 @phi_ugt_non_zero(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], 32
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -30,9 +28,7 @@ define i1 @phi_uge_non_zero(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp uge i8 [[X:%.*]], 32
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -78,9 +74,7 @@ define i1 @phi_ult_non_zero(i8 %x) {
 ; CHECK:       T:
 ; CHECK-NEXT:    br label [[F]]
 ; CHECK:       F:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[T]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   %cmp = icmp ult i8 %x, 123
@@ -101,9 +95,7 @@ define i1 @phi_ule_non_zero(i8 %x) {
 ; CHECK:       T:
 ; CHECK-NEXT:    br label [[F]]
 ; CHECK:       F:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[T]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   %cmp = icmp ule i8 %x, 123
@@ -170,9 +162,7 @@ define i1 @phi_ne_non_zero(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ne 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:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -193,9 +183,7 @@ define i1 @phi_eq_non_zero(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 44
 ; CHECK-NEXT:    br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
 ; CHECK:       T:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;
@@ -218,9 +206,7 @@ define i1 @phi_eq_non_zero2(i8 %x) {
 ; CHECK:       T:
 ; CHECK-NEXT:    br label [[F]]
 ; CHECK:       F:
-; CHECK-NEXT:    [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[T]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   %cmp = icmp eq i8 %x, 0
@@ -239,9 +225,7 @@ define i1 @phi_sgt_non_zero(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:%.*]] ], [ -1, [[F]] ]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       F:
 ; CHECK-NEXT:    br label [[T]]
 ;

diff  --git a/llvm/test/Transforms/FunctionAttrs/nonnull.ll b/llvm/test/Transforms/FunctionAttrs/nonnull.ll
index 3e41b8a07bc720..33e9b377d70937 100644
--- a/llvm/test/Transforms/FunctionAttrs/nonnull.ll
+++ b/llvm/test/Transforms/FunctionAttrs/nonnull.ll
@@ -251,9 +251,8 @@ define ptr @test10(ptr %a, i64 %n) {
 ; char* test11(char *p) {
 ;   return p? p: nonnull();
 ; }
-; FIXME: missing nonnull
 define ptr @test11(ptr) local_unnamed_addr {
-; FNATTRS-LABEL: define ptr @test11
+; FNATTRS-LABEL: define nonnull ptr @test11
 ; FNATTRS-SAME: (ptr readnone [[TMP0:%.*]]) local_unnamed_addr {
 ; FNATTRS-NEXT:    [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
 ; FNATTRS-NEXT:    br i1 [[TMP2]], label [[TMP3:%.*]], label [[TMP5:%.*]]
@@ -264,7 +263,7 @@ define ptr @test11(ptr) local_unnamed_addr {
 ; FNATTRS-NEXT:    [[TMP6:%.*]] = phi ptr [ [[TMP4]], [[TMP3]] ], [ [[TMP0]], [[TMP1:%.*]] ]
 ; FNATTRS-NEXT:    ret ptr [[TMP6]]
 ;
-; ATTRIBUTOR-LABEL: define ptr @test11
+; ATTRIBUTOR-LABEL: define nonnull ptr @test11
 ; ATTRIBUTOR-SAME: (ptr [[TMP0:%.*]]) local_unnamed_addr {
 ; ATTRIBUTOR-NEXT:    [[TMP2:%.*]] = icmp eq ptr [[TMP0]], null
 ; ATTRIBUTOR-NEXT:    br i1 [[TMP2]], label [[TMP3:%.*]], label [[TMP5:%.*]]

diff  --git a/llvm/test/Transforms/JumpThreading/phi-known.ll b/llvm/test/Transforms/JumpThreading/phi-known.ll
index 3ffe20846e3bca..8c42f1fffe07ee 100644
--- a/llvm/test/Transforms/JumpThreading/phi-known.ll
+++ b/llvm/test/Transforms/JumpThreading/phi-known.ll
@@ -66,9 +66,7 @@ define void @test_mixed(ptr %p) {
 ; CHECK-NEXT:    [[CMP0:%.*]] = icmp eq ptr [[P]], null
 ; CHECK-NEXT:    br i1 [[CMP0]], label [[EXIT:%.*]], label [[LOOP:%.*]]
 ; CHECK:       loop:
-; CHECK-NEXT:    [[P1:%.*]] = phi ptr [ [[P]], [[ENTRY:%.*]] ], [ [[P1]], [[LOOP]] ]
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne ptr [[P1]], null
-; CHECK-NEXT:    br i1 [[CMP1]], label [[EXIT]], label [[LOOP]]
+; CHECK-NEXT:    br label [[EXIT]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret void
 ;


        


More information about the llvm-commits mailing list