[llvm] [ValueTracking] isNonEqual Pointers with with a recursive GEP (PR #70459)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 20 05:14:18 PST 2023
https://github.com/bipmis updated https://github.com/llvm/llvm-project/pull/70459
>From 39652a34b4f7a351575f3e6eb11fef2abb929c2e Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Fri, 27 Oct 2023 15:28:44 +0100
Subject: [PATCH 1/6] Precommit tests for isNonEqualPointers with Recursive GEP
---
.../Analysis/ValueTracking/known-non-equal.ll | 38 ++
.../Analysis/ValueTracking/phi-known-bits.ll | 380 ++++++++++++++++++
2 files changed, 418 insertions(+)
diff --git a/llvm/test/Analysis/ValueTracking/known-non-equal.ll b/llvm/test/Analysis/ValueTracking/known-non-equal.ll
index 79b9b3fd8511b60..bbed4eb92c005fa 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-equal.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-equal.ll
@@ -1813,5 +1813,43 @@ exit:
ret i1 %res
}
+; Illustrate if 2 pointers are non-equal when one of them is a recursive GEP.
+define i1 @icmp_recursiveGEP_withPtr(ptr noundef %val1) {
+; CHECK-LABEL: @icmp_recursiveGEP_withPtr(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], 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: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[BOOL]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, 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:
+ %bool = icmp eq ptr %test.0.i, %val1
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i1 [ %bool, %while.end.i ], [ true, %entry ]
+ ret i1 %retval.0.i
+}
!0 = !{ i8 1, i8 5 }
diff --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
index 5227661c837347e..1efb36e41e8088f 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
@@ -424,3 +424,383 @@ T:
F:
br label %T
}
+
+;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_withPtr_phisub1(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, 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
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub1_PhiOperandsCommuted(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_PhiOperandsCommuted(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK: while.cond.i:
+; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[VAL1]], [[ENTRY:%.*]] ], [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ]
+; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+
+while.cond.i:
+ %a.pn.i = phi ptr [ %val1, %entry ], [ %test.0.i, %while.cond.i ]
+ %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
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub1_SubOperandsCommuted(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_SubOperandsCommuted(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, 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.rhs.cast.i, %sub.ptr.lhs.cast.i
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub2(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, 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
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub3(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub3(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK: while.cond.i:
+; CHECK-NEXT: [[A_PN_I_IDX:%.*]] = phi i64 [ [[A_PN_I_ADD:%.*]], [[WHILE_COND_I]] ], [ 7, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[A_PN_I_ADD]] = add nuw nsw i64 [[A_PN_I_IDX]], 1
+; CHECK-NEXT: [[TEST_0_I_PTR:%.*]] = getelementptr inbounds i8, ptr [[VAL1]], i64 [[A_PN_I_ADD]]
+; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[TEST_0_I_PTR]], 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: [[TMP1:%.*]] = icmp eq i64 [[A_PN_I_ADD]], 5
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %test.val1 = getelementptr inbounds i8, ptr %val1, i64 7
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+
+while.cond.i:
+ %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %test.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
+ %1 = getelementptr inbounds i8, ptr %val1, i64 5
+ %sub.ptr.rhs.cast.i = ptrtoint ptr %1 to i64
+ %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual1(ptr noundef %val1, i64 %val2) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_notKnownNonEqual1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], 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: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[VAL1]], i64 [[VAL2:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TEST_0_I]], [[TMP1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP2]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, 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
+ %1 = getelementptr inbounds i8, ptr %val1, i64 %val2
+ %sub.ptr.rhs.cast.i = ptrtoint ptr %1 to i64
+ %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual2(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_notKnownNonEqual2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TEST_VAL1:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 -1
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK: while.cond.i:
+; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[TEST_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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %test.val1 = getelementptr inbounds i8, ptr %val1, i64 -1
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+
+while.cond.i:
+ %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %test.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
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual3(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_notKnownNonEqual3(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TEST_VAL1:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 5
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK: while.cond.i:
+; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[TEST_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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %test.val1 = getelementptr inbounds i8, ptr %val1, i64 5
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+
+while.cond.i:
+ %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %test.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
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtr_phisub_maybeZero(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtr_phisub_maybeZero(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], 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: [[TMP0:%.*]] = load i8, ptr [[A_PN_I]], align 2
+; CHECK-NEXT: [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
+; 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: [[TMP1:%.*]] = icmp eq ptr [[A_PN_I]], [[VAL1]]
+; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
+; CHECK: _Z9stringlenPKs.exit:
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+;
+entry:
+ %cmp.i = icmp eq ptr %val1, null
+ br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+
+while.cond.i:
+ %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
+ %0 = load i8, ptr %a.pn.i, align 2
+ %test.0.i = getelementptr inbounds i8, ptr %a.pn.i, i64 1
+ %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 %a.pn.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
+ br label %_Z9stringlenPKs.exit
+
+_Z9stringlenPKs.exit:
+ %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
+ %bool = icmp eq i64 %retval.0.i, 0
+ ret i1 %bool
+}
>From 6ce297fa59bb070820ab4b8cc0d8bab1f6aa550d Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Fri, 27 Oct 2023 15:33:10 +0100
Subject: [PATCH 2/6] Extend isNonEqual for isNonEqualPointers with Recursive
GEP
---
llvm/lib/Analysis/ValueTracking.cpp | 70 +++++++++++++++++++
.../Analysis/ValueTracking/known-non-equal.ll | 3 +-
.../Analysis/ValueTracking/phi-known-bits.ll | 30 +++-----
llvm/test/Transforms/InstCombine/sub-gep.ll | 3 +-
4 files changed, 82 insertions(+), 24 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index c303d261107eb19..1fd4ce496fcae9f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3141,6 +3141,72 @@ static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
isKnownNonEqual(SI1->getFalseValue(), V2, Depth + 1, Q);
}
+static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
+ unsigned Depth,
+ const SimplifyQuery &Q) {
+ // Where A is a recursive GEP for an incoming value of PHI indicating a
+ // loop. B can be a ptr/GEP.
+ // If the PHI has 2 incoming values, one of it being the recursive GEP A
+ // and other a ptr at same base and at an same/higher offset than B we are
+ // only incrementing the pointer further in loop if offset of recursive GEP is
+ // greater than 0.
+ if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
+ return false;
+
+ auto *GEPA = dyn_cast<GEPOperator>(A);
+ // Make sure GEPA pointer operand is a PHI.
+ if (!GEPA || !isa<PHINode>(GEPA->getPointerOperand()))
+ return false;
+
+ // Handle 2 incoming PHI values with one being a recursive GEP.
+ auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
+ if (PN->getNumIncomingValues() == 2) {
+ // Recursive GEP in second incoming value. Always keep Recursive GEP as
+ // FirstInst
+ Value *FirstInst = nullptr, *SecondInst = nullptr;
+ for (unsigned i = 0; i != 2; ++i) {
+ // Check if A is a Recursive GEP in one of the incoming values.
+ if (PN->getIncomingValue(i) == A && GEPA->getNumIndices() == 1 &&
+ isa<Constant>(GEPA->idx_begin())) {
+ FirstInst = PN->getIncomingValue(i);
+ SecondInst = PN->getIncomingValue(1 - i);
+ continue;
+ }
+ }
+ if (FirstInst && SecondInst) {
+ // Other incoming node base should match the B base.
+ // SecondInstOffset >= OffsetB && FirstInstOffset > 0?
+ // SecondInstOffset <= OffsetB && FirstInstOffset < 0?
+ // Is non-equal if above are true.
+ APInt FirstInstOffset(Q.DL.getIndexTypeSizeInBits(FirstInst->getType()),
+ 0);
+ FirstInst = FirstInst->stripAndAccumulateConstantOffsets(
+ Q.DL, FirstInstOffset, /* AllowNonInbounds */ true);
+ APInt SecondInstOffset(Q.DL.getIndexTypeSizeInBits(SecondInst->getType()),
+ 0);
+ SecondInst = SecondInst->stripAndAccumulateConstantOffsets(
+ Q.DL, SecondInstOffset, /* AllowNonInbounds */ true);
+ APInt OffsetB(Q.DL.getIndexTypeSizeInBits(B->getType()), 0);
+ B = B->stripAndAccumulateConstantOffsets(Q.DL, OffsetB,
+ /* AllowNonInbounds */ true);
+ if (SecondInst == B &&
+ ((SecondInstOffset.sge(OffsetB) && FirstInstOffset.sgt(0)) ||
+ (SecondInstOffset.sle(OffsetB) && FirstInstOffset.slt(0))))
+ return true;
+ }
+ }
+ return false;
+}
+
+static bool isNonEqualPointers(const Value *V1, const Value *V2, unsigned Depth,
+ const SimplifyQuery &Q) {
+ // Check if A is a recursive GEP which is not equal to B.
+ if (isNonEqualPointersWithRecursiveGEP(V1, V2, Depth, Q))
+ return true;
+
+ return false;
+}
+
/// Return true if it is known that V1 != V2.
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
const SimplifyQuery &Q) {
@@ -3194,6 +3260,10 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
if (isNonEqualSelect(V1, V2, Depth, Q) || isNonEqualSelect(V2, V1, Depth, Q))
return true;
+ if (isNonEqualPointers(V1, V2, Depth, Q) ||
+ isNonEqualPointers(V2, V1, Depth, Q))
+ return true;
+
return false;
}
diff --git a/llvm/test/Analysis/ValueTracking/known-non-equal.ll b/llvm/test/Analysis/ValueTracking/known-non-equal.ll
index bbed4eb92c005fa..365a0ea9bc21dbb 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-equal.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-equal.ll
@@ -1826,10 +1826,9 @@ define i1 @icmp_recursiveGEP_withPtr(ptr noundef %val1) {
; 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: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[BOOL]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ false, [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
;
entry:
diff --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
index 1efb36e41e8088f..1fed11792f13521 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
@@ -439,11 +439,9 @@ define i1 @recursiveGEP_withPtr_phisub1(ptr noundef %val1) {
; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: ret i1 [[CMP_I]]
;
entry:
%cmp.i = icmp eq ptr %val1, null
@@ -480,11 +478,9 @@ define i1 @recursiveGEP_withPtr_phisub1_PhiOperandsCommuted(ptr noundef %val1) {
; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: ret i1 [[CMP_I]]
;
entry:
%cmp.i = icmp eq ptr %val1, null
@@ -521,11 +517,9 @@ define i1 @recursiveGEP_withPtr_phisub1_SubOperandsCommuted(ptr noundef %val1) {
; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: ret i1 [[CMP_I]]
;
entry:
%cmp.i = icmp eq ptr %val1, null
@@ -562,11 +556,9 @@ define i1 @recursiveGEP_withPtr_phisub2(ptr noundef %val1) {
; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: ret i1 [[CMP_I]]
;
entry:
%cmp.i = icmp eq ptr %val1, null
@@ -594,21 +586,19 @@ _Z9stringlenPKs.exit:
define i1 @recursiveGEP_withPtr_phisub3(ptr noundef %val1) {
; CHECK-LABEL: @recursiveGEP_withPtr_phisub3(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
+; CHECK-NEXT: [[TEST_VAL1:%.*]] = getelementptr inbounds i8, ptr [[VAL1:%.*]], i64 7
+; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1]], null
; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
; CHECK: while.cond.i:
-; CHECK-NEXT: [[A_PN_I_IDX:%.*]] = phi i64 [ [[A_PN_I_ADD:%.*]], [[WHILE_COND_I]] ], [ 7, [[ENTRY:%.*]] ]
-; CHECK-NEXT: [[A_PN_I_ADD]] = add nuw nsw i64 [[A_PN_I_IDX]], 1
-; CHECK-NEXT: [[TEST_0_I_PTR:%.*]] = getelementptr inbounds i8, ptr [[VAL1]], i64 [[A_PN_I_ADD]]
-; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[TEST_0_I_PTR]], align 2
+; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[TEST_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: [[TMP1:%.*]] = icmp eq i64 [[A_PN_I_ADD]], 5
; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: ret i1 [[CMP_I]]
;
entry:
%test.val1 = getelementptr inbounds i8, ptr %val1, i64 7
diff --git a/llvm/test/Transforms/InstCombine/sub-gep.ll b/llvm/test/Transforms/InstCombine/sub-gep.ll
index a8f8a82e675b026..3997ef44bf328f2 100644
--- a/llvm/test/Transforms/InstCombine/sub-gep.ll
+++ b/llvm/test/Transforms/InstCombine/sub-gep.ll
@@ -386,10 +386,9 @@ define i1 @_gep_phi1(ptr noundef %str1) {
; CHECK-NEXT: [[CMP3_NOT_I:%.*]] = icmp eq i8 [[TMP1]], 0
; CHECK-NEXT: br i1 [[CMP3_NOT_I]], label [[WHILE_END_I:%.*]], label [[WHILE_COND_I]]
; CHECK: while.end.i:
-; CHECK-NEXT: [[TMP2:%.*]] = icmp ne ptr [[TEST_0_I]], [[STR1]]
; CHECK-NEXT: br label [[_Z3FOOPKC_EXIT]]
; CHECK: _Z3fooPKc.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP2]], [[WHILE_END_I]] ], [ false, [[LOR_LHS_FALSE_I]] ], [ false, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ true, [[WHILE_END_I]] ], [ false, [[LOR_LHS_FALSE_I]] ], [ false, [[ENTRY:%.*]] ]
; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
;
entry:
>From 2cb596572258cdb9a8fc01fc9158ffacc7d3b1dc Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Mon, 6 Nov 2023 12:57:28 +0000
Subject: [PATCH 3/6] Early return if condition fails for
isNonEqualPointersWithRecursiveGEP
---
llvm/lib/Analysis/ValueTracking.cpp | 67 +++++++++++++++--------------
1 file changed, 34 insertions(+), 33 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1fd4ce496fcae9f..98f2b412355013e 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3160,41 +3160,42 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
// Handle 2 incoming PHI values with one being a recursive GEP.
auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
- if (PN->getNumIncomingValues() == 2) {
- // Recursive GEP in second incoming value. Always keep Recursive GEP as
- // FirstInst
- Value *FirstInst = nullptr, *SecondInst = nullptr;
- for (unsigned i = 0; i != 2; ++i) {
- // Check if A is a Recursive GEP in one of the incoming values.
- if (PN->getIncomingValue(i) == A && GEPA->getNumIndices() == 1 &&
- isa<Constant>(GEPA->idx_begin())) {
- FirstInst = PN->getIncomingValue(i);
- SecondInst = PN->getIncomingValue(1 - i);
- continue;
- }
- }
- if (FirstInst && SecondInst) {
- // Other incoming node base should match the B base.
- // SecondInstOffset >= OffsetB && FirstInstOffset > 0?
- // SecondInstOffset <= OffsetB && FirstInstOffset < 0?
- // Is non-equal if above are true.
- APInt FirstInstOffset(Q.DL.getIndexTypeSizeInBits(FirstInst->getType()),
- 0);
- FirstInst = FirstInst->stripAndAccumulateConstantOffsets(
- Q.DL, FirstInstOffset, /* AllowNonInbounds */ true);
- APInt SecondInstOffset(Q.DL.getIndexTypeSizeInBits(SecondInst->getType()),
- 0);
- SecondInst = SecondInst->stripAndAccumulateConstantOffsets(
- Q.DL, SecondInstOffset, /* AllowNonInbounds */ true);
- APInt OffsetB(Q.DL.getIndexTypeSizeInBits(B->getType()), 0);
- B = B->stripAndAccumulateConstantOffsets(Q.DL, OffsetB,
- /* AllowNonInbounds */ true);
- if (SecondInst == B &&
- ((SecondInstOffset.sge(OffsetB) && FirstInstOffset.sgt(0)) ||
- (SecondInstOffset.sle(OffsetB) && FirstInstOffset.slt(0))))
- return true;
+ if (PN->getNumIncomingValues() != 2)
+ return false;
+
+ // Recursive GEP in second incoming value. Always keep Recursive GEP as
+ // FirstInst
+ Value *FirstInst = nullptr, *SecondInst = nullptr;
+ for (unsigned i = 0; i != 2; ++i) {
+ // Check if A is a Recursive GEP in one of the incoming values.
+ if (PN->getIncomingValue(i) == A && GEPA->getNumIndices() == 1 &&
+ isa<Constant>(GEPA->idx_begin())) {
+ FirstInst = PN->getIncomingValue(i);
+ SecondInst = PN->getIncomingValue(1 - i);
+ continue;
}
}
+ if (FirstInst == nullptr || SecondInst == nullptr)
+ return false;
+
+ // Other incoming node base should match the B base.
+ // SecondInstOffset >= OffsetB && FirstInstOffset > 0?
+ // SecondInstOffset <= OffsetB && FirstInstOffset < 0?
+ // Is non-equal if above are true.
+ APInt FirstInstOffset(Q.DL.getIndexTypeSizeInBits(FirstInst->getType()), 0);
+ FirstInst = FirstInst->stripAndAccumulateConstantOffsets(
+ Q.DL, FirstInstOffset, /* AllowNonInbounds */ true);
+ APInt SecondInstOffset(Q.DL.getIndexTypeSizeInBits(SecondInst->getType()), 0);
+ SecondInst = SecondInst->stripAndAccumulateConstantOffsets(
+ Q.DL, SecondInstOffset, /* AllowNonInbounds */ true);
+ APInt OffsetB(Q.DL.getIndexTypeSizeInBits(B->getType()), 0);
+ B = B->stripAndAccumulateConstantOffsets(Q.DL, OffsetB,
+ /* AllowNonInbounds */ true);
+ if (SecondInst == B &&
+ ((SecondInstOffset.sge(OffsetB) && FirstInstOffset.sgt(0)) ||
+ (SecondInstOffset.sle(OffsetB) && FirstInstOffset.slt(0))))
+ return true;
+
return false;
}
>From 3c01f743a6f1dc5735b257fe77a61e7f78bae323 Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Tue, 7 Nov 2023 22:03:01 +0000
Subject: [PATCH 4/6] Add tests for non-inbound and scalable gep
---
.../Analysis/ValueTracking/known-non-equal.ll | 19 +-
.../Analysis/ValueTracking/phi-known-bits.ll | 330 ++++++++++--------
2 files changed, 196 insertions(+), 153 deletions(-)
diff --git a/llvm/test/Analysis/ValueTracking/known-non-equal.ll b/llvm/test/Analysis/ValueTracking/known-non-equal.ll
index 365a0ea9bc21dbb..4bf425d9ffcefd4 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-equal.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-equal.ll
@@ -1817,23 +1817,18 @@ exit:
define i1 @icmp_recursiveGEP_withPtr(ptr noundef %val1) {
; CHECK-LABEL: @icmp_recursiveGEP_withPtr(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; 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: [[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: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ false, [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: ret i1 false
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
@@ -1844,11 +1839,7 @@ while.cond.i:
while.end.i:
%bool = icmp eq ptr %test.0.i, %val1
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i1 [ %bool, %while.end.i ], [ true, %entry ]
- ret i1 %retval.0.i
+ ret i1 %bool
}
!0 = !{ i8 1, i8 5 }
diff --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
index 1fed11792f13521..c9e9148b0ad7965 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
@@ -427,25 +427,21 @@ 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_withPtr_phisub1(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub1(
+define i1 @recursiveGEP_withPtrSub1(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub1(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; 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: [[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: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: ret i1 [[CMP_I]]
+; CHECK-NEXT: ret i1 false
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
@@ -458,33 +454,25 @@ 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
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub1_PhiOperandsCommuted(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_PhiOperandsCommuted(
+define i1 @recursiveGEP_withPtrSub1_PhiOperandsCommuted(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub1_PhiOperandsCommuted(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK-NEXT: br label [[WHILE_COND_I:%.*]]
; CHECK: while.cond.i:
-; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[VAL1]], [[ENTRY:%.*]] ], [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ]
+; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[VAL1:%.*]], [[ENTRY:%.*]] ], [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ]
; 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: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: ret i1 [[CMP_I]]
+; CHECK-NEXT: ret i1 false
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %val1, %entry ], [ %test.0.i, %while.cond.i ]
@@ -497,33 +485,25 @@ 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
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub1_SubOperandsCommuted(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_SubOperandsCommuted(
+define i1 @recursiveGEP_withPtrSub1_SubOperandsCommuted(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub1_SubOperandsCommuted(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; 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: [[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: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: ret i1 [[CMP_I]]
+; CHECK-NEXT: ret i1 false
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
@@ -536,33 +516,25 @@ 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.rhs.cast.i, %sub.ptr.lhs.cast.i
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub2(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub2(
+define i1 @recursiveGEP_withPtrSub2(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub2(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; 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: [[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: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: ret i1 [[CMP_I]]
+; CHECK-NEXT: ret i1 false
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
@@ -575,20 +547,15 @@ 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
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub3(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub3(
+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
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK-NEXT: br label [[WHILE_COND_I:%.*]]
; CHECK: while.cond.i:
; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[TEST_VAL1]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
@@ -596,14 +563,11 @@ define i1 @recursiveGEP_withPtr_phisub3(ptr noundef %val1) {
; 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: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: ret i1 [[CMP_I]]
+; CHECK-NEXT: ret i1 false
;
entry:
%test.val1 = getelementptr inbounds i8, ptr %val1, i64 7
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %test.val1, %entry ]
@@ -617,36 +581,27 @@ while.end.i:
%1 = getelementptr inbounds i8, ptr %val1, i64 5
%sub.ptr.rhs.cast.i = ptrtoint ptr %1 to i64
%sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual1(ptr noundef %val1, i64 %val2) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_notKnownNonEqual1(
+define i1 @recursiveGEP_withPtrSub1_notKnownNonEqual1(ptr noundef %val1, i64 %val2) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub1_notKnownNonEqual1(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; 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: [[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: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[VAL1]], i64 [[VAL2:%.*]]
-; CHECK-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TEST_0_I]], [[TMP1]]
-; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP2]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[TMP1]]
+; CHECK-NEXT: ret i1 [[BOOL]]
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
@@ -660,20 +615,15 @@ while.end.i:
%1 = getelementptr inbounds i8, ptr %val1, i64 %val2
%sub.ptr.rhs.cast.i = ptrtoint ptr %1 to i64
%sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual2(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_notKnownNonEqual2(
+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
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK-NEXT: br label [[WHILE_COND_I:%.*]]
; CHECK: while.cond.i:
; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[TEST_VAL1]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
@@ -681,16 +631,12 @@ define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual2(ptr noundef %val1) {
; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
-; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: ret i1 [[BOOL]]
;
entry:
%test.val1 = getelementptr inbounds i8, ptr %val1, i64 -1
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %test.val1, %entry ]
@@ -703,20 +649,15 @@ 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
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual3(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub1_notKnownNonEqual3(
+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
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; CHECK-NEXT: br label [[WHILE_COND_I:%.*]]
; CHECK: while.cond.i:
; CHECK-NEXT: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[TEST_VAL1]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 -1
@@ -724,16 +665,12 @@ define i1 @recursiveGEP_withPtr_phisub1_notKnownNonEqual3(ptr noundef %val1) {
; 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: [[TMP1:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
-; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: ret i1 [[BOOL]]
;
entry:
%test.val1 = getelementptr inbounds i8, ptr %val1, i64 5
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %test.val1, %entry ]
@@ -746,35 +683,26 @@ 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
- br label %_Z9stringlenPKs.exit
-
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
-define i1 @recursiveGEP_withPtr_phisub_maybeZero(ptr noundef %val1) {
-; CHECK-LABEL: @recursiveGEP_withPtr_phisub_maybeZero(
+define i1 @recursiveGEP_withPtrSub_maybeZero(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub_maybeZero(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL1:%.*]], null
-; CHECK-NEXT: br i1 [[CMP_I]], label [[_Z9STRINGLENPKS_EXIT:%.*]], label [[WHILE_COND_I:%.*]]
+; 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: [[A_PN_I:%.*]] = phi ptr [ [[TEST_0_I:%.*]], [[WHILE_COND_I]] ], [ [[VAL1:%.*]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[A_PN_I]], align 2
; CHECK-NEXT: [[TEST_0_I]] = getelementptr inbounds i8, ptr [[A_PN_I]], i64 1
; 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: [[TMP1:%.*]] = icmp eq ptr [[A_PN_I]], [[VAL1]]
-; CHECK-NEXT: br label [[_Z9STRINGLENPKS_EXIT]]
-; CHECK: _Z9stringlenPKs.exit:
-; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ true, [[ENTRY]] ]
-; CHECK-NEXT: ret i1 [[RETVAL_0_I]]
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[A_PN_I]], [[VAL1]]
+; CHECK-NEXT: ret i1 [[BOOL]]
;
entry:
- %cmp.i = icmp eq ptr %val1, null
- br i1 %cmp.i, label %_Z9stringlenPKs.exit, label %while.cond.i
+ br label %while.cond.i
while.cond.i:
%a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
@@ -787,10 +715,134 @@ while.end.i:
%sub.ptr.lhs.cast.i = ptrtoint ptr %a.pn.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
- br label %_Z9stringlenPKs.exit
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtrSub_noninbound1(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub_noninbound1(
+; 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 i8, ptr [[A_PN_I]], i64 4294967297
+; 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: [[TEST_1_I:%.*]] = getelementptr i8, ptr [[VAL1]], i64 1
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[TEST_1_I]]
+; 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 i8, ptr %a.pn.i, i64 4294967297
+ %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
+ %test.1.i = getelementptr i8, ptr %val1, i64 1
+ %sub.ptr.rhs.cast.i = ptrtoint ptr %test.1.i to i64
+ %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtrSub_noninbound2(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub_noninbound2(
+; 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 i8, ptr [[A_PN_I]], i64 4294967297
+; 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: ret i1 false
+;
+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 i8, ptr %a.pn.i, i64 4294967297
+ %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
-_Z9stringlenPKs.exit:
- %retval.0.i = phi i64 [ %sub.ptr.sub.i, %while.end.i ], [ 0, %entry ]
- %bool = icmp eq i64 %retval.0.i, 0
+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
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtrSub_noninbound3(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub_noninbound3(
+; 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 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: ret i1 false
+;
+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 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
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
+ ret i1 %bool
+}
+
+define i1 @recursiveGEP_withPtrSub_scalableGEP(ptr noundef %val1) {
+; CHECK-LABEL: @recursiveGEP_withPtrSub_scalableGEP(
+; 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 <vscale x 16 x i8>, ptr [[A_PN_I]], i64 1
+; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[TEST_0_I]], align 1
+; 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: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; 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 <vscale x 16 x i8>, ptr %a.pn.i, i64 1
+ %0 = load i8, ptr %test.0.i, align 1
+ %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
+ %bool = icmp eq i64 %sub.ptr.sub.i, 0
ret i1 %bool
}
>From a924a5f5f6f824f1c3edfa13f787ea1d3412aafe Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Wed, 8 Nov 2023 13:18:27 +0000
Subject: [PATCH 5/6] Update as per review comments
---
llvm/lib/Analysis/ValueTracking.cpp | 40 +++++++++++++++--------------
1 file changed, 21 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 98f2b412355013e..6c22a21c5e34265 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3154,46 +3154,48 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
return false;
auto *GEPA = dyn_cast<GEPOperator>(A);
- // Make sure GEPA pointer operand is a PHI.
- if (!GEPA || !isa<PHINode>(GEPA->getPointerOperand()))
+ // Is of type GEP.
+ if (!GEPA)
return false;
// Handle 2 incoming PHI values with one being a recursive GEP.
auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
- if (PN->getNumIncomingValues() != 2)
+ if (!PN || PN->getNumIncomingValues() != 2)
return false;
// Recursive GEP in second incoming value. Always keep Recursive GEP as
- // FirstInst
- Value *FirstInst = nullptr, *SecondInst = nullptr;
+ // Step
+ Value *Start = nullptr, *Step = nullptr;
for (unsigned i = 0; i != 2; ++i) {
// Check if A is a Recursive GEP in one of the incoming values.
if (PN->getIncomingValue(i) == A && GEPA->getNumIndices() == 1 &&
isa<Constant>(GEPA->idx_begin())) {
- FirstInst = PN->getIncomingValue(i);
- SecondInst = PN->getIncomingValue(1 - i);
+ Step = PN->getIncomingValue(i);
+ Start = PN->getIncomingValue(1 - i);
continue;
}
}
- if (FirstInst == nullptr || SecondInst == nullptr)
+ if (Start == nullptr || Step == nullptr)
return false;
// Other incoming node base should match the B base.
- // SecondInstOffset >= OffsetB && FirstInstOffset > 0?
- // SecondInstOffset <= OffsetB && FirstInstOffset < 0?
+ // StartOffset >= OffsetB && StepOffset > 0?
+ // StartOffset <= OffsetB && StepOffset < 0?
// Is non-equal if above are true.
- APInt FirstInstOffset(Q.DL.getIndexTypeSizeInBits(FirstInst->getType()), 0);
- FirstInst = FirstInst->stripAndAccumulateConstantOffsets(
- Q.DL, FirstInstOffset, /* AllowNonInbounds */ true);
- APInt SecondInstOffset(Q.DL.getIndexTypeSizeInBits(SecondInst->getType()), 0);
- SecondInst = SecondInst->stripAndAccumulateConstantOffsets(
- Q.DL, SecondInstOffset, /* AllowNonInbounds */ true);
+ APInt StartOffset(Q.DL.getIndexTypeSizeInBits(Start->getType()), 0);
+ Start = Start->stripAndAccumulateConstantOffsets(Q.DL, StartOffset,
+ /* AllowNonInbounds */ true);
+ APInt StepOffset(Q.DL.getIndexTypeSizeInBits(Step->getType()), 0);
+ Step = Step->stripAndAccumulateConstantOffsets(Q.DL, StepOffset,
+ /* AllowNonInbounds */ true);
+ // Check if Base Pointer of Step matches the PHI.
+ if (Step != PN)
+ return false;
APInt OffsetB(Q.DL.getIndexTypeSizeInBits(B->getType()), 0);
B = B->stripAndAccumulateConstantOffsets(Q.DL, OffsetB,
/* AllowNonInbounds */ true);
- if (SecondInst == B &&
- ((SecondInstOffset.sge(OffsetB) && FirstInstOffset.sgt(0)) ||
- (SecondInstOffset.sle(OffsetB) && FirstInstOffset.slt(0))))
+ if (Start == B && ((StartOffset.sge(OffsetB) && StepOffset.sgt(0)) ||
+ (StartOffset.sle(OffsetB) && StepOffset.slt(0))))
return true;
return false;
>From a024401126c3dd883969c93358bc702e9ad2b309 Mon Sep 17 00:00:00 2001
From: bipmis <biplob.mishra at arm.com>
Date: Mon, 20 Nov 2023 13:13:52 +0000
Subject: [PATCH 6/6] Handle non-inbounds GEP's correctly
---
llvm/lib/Analysis/ValueTracking.cpp | 22 +++++--------------
.../Analysis/ValueTracking/phi-known-bits.ll | 6 +++--
2 files changed, 9 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 6c22a21c5e34265..57d254fe67b5b34 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3183,17 +3183,14 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
// StartOffset <= OffsetB && StepOffset < 0?
// Is non-equal if above are true.
APInt StartOffset(Q.DL.getIndexTypeSizeInBits(Start->getType()), 0);
- Start = Start->stripAndAccumulateConstantOffsets(Q.DL, StartOffset,
- /* AllowNonInbounds */ true);
+ Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
APInt StepOffset(Q.DL.getIndexTypeSizeInBits(Step->getType()), 0);
- Step = Step->stripAndAccumulateConstantOffsets(Q.DL, StepOffset,
- /* AllowNonInbounds */ true);
+ Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
// Check if Base Pointer of Step matches the PHI.
if (Step != PN)
return false;
APInt OffsetB(Q.DL.getIndexTypeSizeInBits(B->getType()), 0);
- B = B->stripAndAccumulateConstantOffsets(Q.DL, OffsetB,
- /* AllowNonInbounds */ true);
+ B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
if (Start == B && ((StartOffset.sge(OffsetB) && StepOffset.sgt(0)) ||
(StartOffset.sle(OffsetB) && StepOffset.slt(0))))
return true;
@@ -3201,15 +3198,6 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
return false;
}
-static bool isNonEqualPointers(const Value *V1, const Value *V2, unsigned Depth,
- const SimplifyQuery &Q) {
- // Check if A is a recursive GEP which is not equal to B.
- if (isNonEqualPointersWithRecursiveGEP(V1, V2, Depth, Q))
- return true;
-
- return false;
-}
-
/// Return true if it is known that V1 != V2.
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
const SimplifyQuery &Q) {
@@ -3263,8 +3251,8 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
if (isNonEqualSelect(V1, V2, Depth, Q) || isNonEqualSelect(V2, V1, Depth, Q))
return true;
- if (isNonEqualPointers(V1, V2, Depth, Q) ||
- isNonEqualPointers(V2, V1, Depth, Q))
+ if (isNonEqualPointersWithRecursiveGEP(V1, V2, Depth, Q) ||
+ isNonEqualPointersWithRecursiveGEP(V2, V1, Depth, Q))
return true;
return false;
diff --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
index c9e9148b0ad7965..87ae7793fb22130 100644
--- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll
@@ -764,7 +764,8 @@ define i1 @recursiveGEP_withPtrSub_noninbound2(ptr noundef %val1) {
; 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: ret i1 false
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: ret i1 [[BOOL]]
;
entry:
br label %while.cond.i
@@ -795,7 +796,8 @@ define i1 @recursiveGEP_withPtrSub_noninbound3(ptr noundef %val1) {
; 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: ret i1 false
+; CHECK-NEXT: [[BOOL:%.*]] = icmp eq ptr [[TEST_0_I]], [[VAL1]]
+; CHECK-NEXT: ret i1 [[BOOL]]
;
entry:
br label %while.cond.i
More information about the llvm-commits
mailing list