[llvm] [ValueTracking] isNonZero sub of ptr2int's with recursive GEP (PR #68680)

via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 15 04:34:32 PST 2023


https://github.com/bipmis updated https://github.com/llvm/llvm-project/pull/68680

>From d5298637c004f09eb6945c065580f8d57022b971 Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Mon, 9 Oct 2023 15:07:19 +0100
Subject: [PATCH] Precommit tests for ValueTracking isNonZeroSub with recursive
 GEP

---
 .../Analysis/ValueTracking/phi-known-bits.ll  | 167 ++++++++++++++++--
 1 file changed, 150 insertions(+), 17 deletions(-)

diff --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
index 450b3b864c1920..99eddb2583997e 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
@@ -427,7 +427,7 @@ F:
 
 ;Illustrate if 2 pointers are non-equal when one of them is a recursive GEP.
 ;Cases which folds to a canonical icmp(ptr1, ptr2)
-define i1 @recursiveGEP_withPtrSub1(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub1(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub1(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -458,7 +458,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub1_PhiOperandsCommuted(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub1_PhiOperandsCommuted(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub1_PhiOperandsCommuted(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -489,7 +489,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub1_SubOperandsCommuted(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub1_SubOperandsCommuted(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub1_SubOperandsCommuted(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -520,7 +520,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub2(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub2(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub2(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -551,7 +551,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub3(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub3(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub3(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST_VAL1:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 7
@@ -585,7 +585,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual1(ptr %val1, i64 %val2) {
+define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual1(ptr noundef %val1, i64 %val2) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub1_notKnownNonEqual1(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -619,7 +619,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual2(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual2(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub1_notKnownNonEqual2(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST_VAL1:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 -1
@@ -653,7 +653,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual3(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual3(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub1_notKnownNonEqual3(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST_VAL1:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 5
@@ -687,7 +687,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub_maybeZero(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_maybeZero(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_maybeZero(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -721,7 +721,7 @@ while.end.i:
 
 ;Non-inbounds test.
 ;Test where Step is non-inbound.
-define i1 @recursiveGEP_withPtrSub_noninboundStep1(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_noninboundStep1(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_noninboundStep1(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -753,7 +753,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub_noninboundStep2(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_noninboundStep2(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_noninboundStep2(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -786,7 +786,7 @@ while.end.i:
 }
 
 ;Test where Step and GEP B are non-inbound.
-define i1 @recursiveGEP_withPtrSub_noninboundStepAndB(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_noninboundStepAndB(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_noninboundStepAndB(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 2
@@ -822,7 +822,7 @@ while.end.i:
 }
 
 ;Test where Start and Step are non-inbound.
-define i1 @recursiveGEP_withPtrSub_noninboundStartAndStep(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_noninboundStartAndStep(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_noninboundStartAndStep(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST:%.*]] = getelementptr i8, ptr [[VAL1:%.*]], i64 1
@@ -857,7 +857,7 @@ while.end.i:
 }
 
 ;Test where Start and GEP B are non-inbounds pointer with same definition.
-define i1 @recursiveGEP_withPtrSub_noninboundSameDefStartAndB(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_noninboundSameDefStartAndB(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_noninboundSameDefStartAndB(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST:%.*]] = getelementptr i8, ptr [[VAL1:%.*]], i64 1
@@ -893,7 +893,7 @@ while.end.i:
 }
 
 ;Test where Start and GEP B are non-inbounds and exactly same pointers.
-define i1 @recursiveGEP_withPtrSub_noninboundSameStartAndB(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_noninboundSameStartAndB(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_noninboundSameStartAndB(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TEST:%.*]] = getelementptr i8, ptr [[VAL1:%.*]], i64 1
@@ -926,7 +926,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub_scalableGEP(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_scalableGEP(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_scalableGEP(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -958,7 +958,7 @@ while.end.i:
   ret i1 %bool
 }
 
-define i1 @recursiveGEP_withPtrSub_scalableGEP_inbounds(ptr %val1) {
+define i1 @recursiveGEP_withPtrSub_scalableGEP_inbounds(ptr noundef %val1) {
 ; CHECK-LABEL: @recursiveGEP_withPtrSub_scalableGEP_inbounds(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
@@ -989,3 +989,136 @@ while.end.i:
   %bool = icmp eq i64 %sub.ptr.sub.i, 0
   ret i1 %bool
 }
+
+; Test cmp(or), where one of argument to OR is a SUB of ptr2int with a recursive GEP.
+define i1 @recursiveGEP_orcmp(ptr noundef %val1, i64 %val2) {
+; CHECK-LABEL: @recursiveGEP_orcmp(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
+; CHECK:       while.cond.i:
+; CHECK-NEXT:    [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[VAL1:%.*]], [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
+; CHECK-NEXT:    [[TMP0:%.*]] = load i8, ptr [[TEST_0_I]], align 2
+; CHECK-NEXT:    [[CMP3_NOT_I:%.*]] = icmp eq i8 [[TMP0]], 0
+; CHECK-NEXT:    br i1 [[CMP3_NOT_I]], label [[WHILE_END_I:%.*]], label [[WHILE_COND_I]]
+; CHECK:       while.end.i:
+; CHECK-NEXT:    [[SUB_PTR_LHS_CAST_I:%.*]] = ptrtoint ptr [[TEST_0_I]] to i64
+; CHECK-NEXT:    [[SUB_PTR_RHS_CAST_I:%.*]] = ptrtoint ptr [[VAL1]] to i64
+; CHECK-NEXT:    [[SUB_PTR_SUB_I:%.*]] = sub i64 [[SUB_PTR_LHS_CAST_I]], [[SUB_PTR_RHS_CAST_I]]
+; CHECK-NEXT:    [[ORVAL:%.*]] = or i64 [[SUB_PTR_SUB_I]], [[VAL2:%.*]]
+; CHECK-NEXT:    [[BOOL:%.*]] = icmp eq i64 [[ORVAL]], 0
+; CHECK-NEXT:    ret i1 [[BOOL]]
+;
+entry:
+  br label %while.cond.i
+
+while.cond.i:
+  %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
+  %test.0.i = getelementptr inbounds i8, ptr %a.pn.i, i64 1
+  %0 = load i8, ptr %test.0.i, align 2
+  %cmp3.not.i = icmp eq i8 %0, 0
+  br i1 %cmp3.not.i, label %while.end.i, label %while.cond.i
+
+while.end.i:
+  %sub.ptr.lhs.cast.i = ptrtoint ptr %test.0.i to i64
+  %sub.ptr.rhs.cast.i = ptrtoint ptr %val1 to i64
+  %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
+  %orval = or i64 %sub.ptr.sub.i, %val2
+  %bool = icmp eq i64 %orval, 0
+  ret i1 %bool
+}
+
+define i1 @recursiveGEP_orcmp_orOperandsCommuted(ptr noundef %val1, i64 %val2) {
+; CHECK-LABEL: @recursiveGEP_orcmp_orOperandsCommuted(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
+; CHECK:       while.cond.i:
+; CHECK-NEXT:    [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[VAL1:%.*]], [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
+; CHECK-NEXT:    [[TMP0:%.*]] = load i8, ptr [[TEST_0_I]], align 2
+; CHECK-NEXT:    [[CMP3_NOT_I:%.*]] = icmp eq i8 [[TMP0]], 0
+; CHECK-NEXT:    br i1 [[CMP3_NOT_I]], label [[WHILE_END_I:%.*]], label [[WHILE_COND_I]]
+; CHECK:       while.end.i:
+; CHECK-NEXT:    [[SUB_PTR_LHS_CAST_I:%.*]] = ptrtoint ptr [[TEST_0_I]] to i64
+; CHECK-NEXT:    [[SUB_PTR_RHS_CAST_I:%.*]] = ptrtoint ptr [[VAL1]] to i64
+; CHECK-NEXT:    [[SUB_PTR_SUB_I:%.*]] = sub i64 [[SUB_PTR_LHS_CAST_I]], [[SUB_PTR_RHS_CAST_I]]
+; CHECK-NEXT:    [[ORVAL:%.*]] = or i64 [[SUB_PTR_SUB_I]], [[VAL2:%.*]]
+; CHECK-NEXT:    [[BOOL:%.*]] = icmp eq i64 [[ORVAL]], 0
+; CHECK-NEXT:    ret i1 [[BOOL]]
+;
+entry:
+  br label %while.cond.i
+
+while.cond.i:
+  %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
+  %test.0.i = getelementptr inbounds i8, ptr %a.pn.i, i64 1
+  %0 = load i8, ptr %test.0.i, align 2
+  %cmp3.not.i = icmp eq i8 %0, 0
+  br i1 %cmp3.not.i, label %while.end.i, label %while.cond.i
+
+while.end.i:
+  %sub.ptr.lhs.cast.i = ptrtoint ptr %test.0.i to i64
+  %sub.ptr.rhs.cast.i = ptrtoint ptr %val1 to i64
+  %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
+  %orval = or i64 %val2, %sub.ptr.sub.i
+  %bool = icmp eq i64 %orval, 0
+  ret i1 %bool
+}
+
+; Test one of the argument to SUB is a ptr2int of a recursive GEP, with multiple use of SUB.
+define i1 @recursiveGEP_orcmpMultiUse(ptr %val1, i64 %val2, ptr %dv1, ptr %dv2) {
+; CHECK-LABEL: @recursiveGEP_orcmpMultiUse(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[WHILE_COND_I:%.*]]
+; CHECK:       while.cond.i:
+; CHECK-NEXT:    [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[VAL1:%.*]], [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
+; CHECK-NEXT:    [[TMP0:%.*]] = load i8, ptr [[TEST_0_I]], align 2
+; CHECK-NEXT:    [[CMP3_NOT_I:%.*]] = icmp eq i8 [[TMP0]], 0
+; CHECK-NEXT:    br i1 [[CMP3_NOT_I]], label [[WHILE_END_I:%.*]], label [[WHILE_COND_I]]
+; CHECK:       while.end.i:
+; CHECK-NEXT:    [[SUB_PTR_LHS_CAST_I:%.*]] = ptrtoint ptr [[TEST_0_I]] to i64
+; CHECK-NEXT:    [[SUB_PTR_RHS_CAST_I:%.*]] = ptrtoint ptr [[VAL1]] to i64
+; CHECK-NEXT:    [[SUB_PTR_SUB_I:%.*]] = sub i64 [[SUB_PTR_LHS_CAST_I]], [[SUB_PTR_RHS_CAST_I]]
+; CHECK-NEXT:    [[ORVAL:%.*]] = or i64 [[SUB_PTR_SUB_I]], [[VAL2:%.*]]
+; CHECK-NEXT:    [[OR_COND:%.*]] = icmp eq i64 [[ORVAL]], 0
+; CHECK-NEXT:    br i1 [[OR_COND]], label [[IF_THEN:%.*]], label [[IF_END4:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr [[DV1:%.*]], [[DV2:%.*]]
+; CHECK-NEXT:    br label [[CLEANUP:%.*]]
+; CHECK:       if.end4:
+; CHECK-NEXT:    br label [[CLEANUP]]
+; CHECK:       cleanup:
+; CHECK-NEXT:    [[RETVAL_0:%.*]] = phi i1 [ [[CMP]], [[IF_THEN]] ], [ true, [[IF_END4]] ]
+; CHECK-NEXT:    ret i1 [[RETVAL_0]]
+;
+entry:
+  br label %while.cond.i
+
+while.cond.i:
+  %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
+  %test.0.i = getelementptr inbounds i8, ptr %a.pn.i, i64 1
+  %0 = load i8, ptr %test.0.i, align 2
+  %cmp3.not.i = icmp eq i8 %0, 0
+  br i1 %cmp3.not.i, label %while.end.i, label %while.cond.i
+
+while.end.i:
+  %sub.ptr.lhs.cast.i = ptrtoint ptr %test.0.i to i64
+  %sub.ptr.rhs.cast.i = ptrtoint ptr %val1 to i64
+  %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
+  %orval = or i64 %sub.ptr.sub.i, %val2
+  %or.cond = icmp eq i64 %orval, 0
+  br i1 %or.cond, label %if.then, label %if.end4
+
+if.then:
+  %cmp = icmp eq ptr %dv1, %dv2
+  br label %cleanup
+
+if.end4:
+  %tobool = icmp ne i64 %sub.ptr.sub.i, 0
+  br label %cleanup
+
+cleanup:
+  %retval.0 = phi i1 [ %cmp, %if.then ], [ %tobool, %if.end4 ]
+  ret i1 %retval.0
+}



More information about the llvm-commits mailing list