-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[ValueTracking] Support ptrtoaddr in inequality implication #173362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikic
wants to merge
1
commit into
llvm:main
Choose a base branch
from
nikic:ptrtoaddr-implied-non-equal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+63
−5
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`ptrtoaddr(p1) - ptrtoaddr(p2) == non-zero` implies `p1 != p2`, same as for ptrtoint.
Member
|
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-llvm-analysis Author: Nikita Popov (nikic) Changes
Full diff: https://github.com/llvm/llvm-project/pull/173362.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 6d8c966deb7e0..39e639d0b644a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9675,10 +9675,10 @@ isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
match(L1, m_APInt(L1C)) && !L1C->isZero() &&
match(L0, m_Sub(m_Value(A), m_Value(B))) &&
((A == R0 && B == R1) || (A == R1 && B == R0) ||
- (match(A, m_PtrToInt(m_Specific(R0))) &&
- match(B, m_PtrToInt(m_Specific(R1)))) ||
- (match(A, m_PtrToInt(m_Specific(R1))) &&
- match(B, m_PtrToInt(m_Specific(R0)))))) {
+ (match(A, m_PtrToIntOrAddr(m_Specific(R0))) &&
+ match(B, m_PtrToIntOrAddr(m_Specific(R1)))) ||
+ (match(A, m_PtrToIntOrAddr(m_Specific(R1))) &&
+ match(B, m_PtrToIntOrAddr(m_Specific(R0)))))) {
return RPred.dropSameSign() == ICmpInst::ICMP_NE;
}
@@ -10460,7 +10460,8 @@ addValueAffectedByCondition(Value *V,
// Peek through unary operators to find the source of the condition.
Value *Op;
- if (match(I, m_CombineOr(m_PtrToInt(m_Value(Op)), m_Trunc(m_Value(Op))))) {
+ if (match(I, m_CombineOr(m_PtrToIntOrAddr(m_Value(Op)),
+ m_Trunc(m_Value(Op))))) {
if (isa<Instruction>(Op) || isa<Argument>(Op))
InsertAffected(Op);
}
diff --git a/llvm/test/Transforms/InstSimplify/ptrtoaddr.ll b/llvm/test/Transforms/InstSimplify/ptrtoaddr.ll
index d517414ff93d7..88ff0080f229a 100644
--- a/llvm/test/Transforms/InstSimplify/ptrtoaddr.ll
+++ b/llvm/test/Transforms/InstSimplify/ptrtoaddr.ll
@@ -528,3 +528,60 @@ define i1 @non_zero_ptrtoint_smaller_addrsize(ptr addrspace(1) nonnull %p, i16 %
%cmp = icmp ne i16 %or, 0
ret i1 %cmp
}
+
+define i1 @ptrtoaddr_diff_non_equal(ptr %p0, ptr %p1) {
+; CHECK-LABEL: define i1 @ptrtoaddr_diff_non_equal(
+; CHECK-SAME: ptr [[P0:%.*]], ptr [[P1:%.*]]) {
+; CHECK-NEXT: [[I0:%.*]] = ptrtoaddr ptr [[P0]] to i64
+; CHECK-NEXT: [[I1:%.*]] = ptrtoaddr ptr [[P1]] to i64
+; CHECK-NEXT: [[DIFF:%.*]] = sub i64 [[I0]], [[I1]]
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[DIFF]], 12
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND]])
+; CHECK-NEXT: ret i1 false
+;
+ %i0 = ptrtoaddr ptr %p0 to i64
+ %i1 = ptrtoaddr ptr %p1 to i64
+ %diff = sub i64 %i0, %i1
+ %cond = icmp eq i64 %diff, 12
+ call void @llvm.assume(i1 %cond)
+ %cmp = icmp eq ptr %p0, %p1
+ ret i1 %cmp
+}
+
+define i1 @ptrtoaddr_diff_non_equal_addrsize(ptr addrspace(1) %p0, ptr addrspace(1) %p1) {
+; CHECK-LABEL: define i1 @ptrtoaddr_diff_non_equal_addrsize(
+; CHECK-SAME: ptr addrspace(1) [[P0:%.*]], ptr addrspace(1) [[P1:%.*]]) {
+; CHECK-NEXT: [[I0:%.*]] = ptrtoaddr ptr addrspace(1) [[P0]] to i32
+; CHECK-NEXT: [[I1:%.*]] = ptrtoaddr ptr addrspace(1) [[P1]] to i32
+; CHECK-NEXT: [[DIFF:%.*]] = sub i32 [[I0]], [[I1]]
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[DIFF]], 12
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND]])
+; CHECK-NEXT: ret i1 false
+;
+ %i0 = ptrtoaddr ptr addrspace(1) %p0 to i32
+ %i1 = ptrtoaddr ptr addrspace(1) %p1 to i32
+ %diff = sub i32 %i0, %i1
+ %cond = icmp eq i32 %diff, 12
+ call void @llvm.assume(i1 %cond)
+ %cmp = icmp eq ptr addrspace(1) %p0, %p1
+ ret i1 %cmp
+}
+
+define i1 @ptrtoaddr_diff_non_equal_addrsize_commuted(ptr addrspace(1) %p0, ptr addrspace(1) %p1) {
+; CHECK-LABEL: define i1 @ptrtoaddr_diff_non_equal_addrsize_commuted(
+; CHECK-SAME: ptr addrspace(1) [[P0:%.*]], ptr addrspace(1) [[P1:%.*]]) {
+; CHECK-NEXT: [[I0:%.*]] = ptrtoaddr ptr addrspace(1) [[P0]] to i32
+; CHECK-NEXT: [[I1:%.*]] = ptrtoaddr ptr addrspace(1) [[P1]] to i32
+; CHECK-NEXT: [[DIFF:%.*]] = sub i32 [[I0]], [[I1]]
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[DIFF]], 12
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND]])
+; CHECK-NEXT: ret i1 false
+;
+ %i0 = ptrtoaddr ptr addrspace(1) %p0 to i32
+ %i1 = ptrtoaddr ptr addrspace(1) %p1 to i32
+ %diff = sub i32 %i0, %i1
+ %cond = icmp eq i32 %diff, 12
+ call void @llvm.assume(i1 %cond)
+ %cmp = icmp eq ptr addrspace(1) %p1, %p0
+ ret i1 %cmp
+}
|
arichardson
approved these changes
Dec 23, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
llvm:analysis
Includes value tracking, cost tables and constant folding
llvm:instcombine
Covers the InstCombine, InstSimplify and AggressiveInstCombine passes
llvm:transforms
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ptrtoaddr(p1) - ptrtoaddr(p2) == non-zeroimpliesp1 != p2, same as for ptrtoint.