-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[InstCombine] Optimize icmp(sub(a, c), sub(b, c)) to icmp(a, b) if a, b, and c are pointers #161698
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
bababuck
wants to merge
2
commits into
llvm:main
Choose a base branch
from
bababuck:bababuck/PtrSubCmp
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.
+148
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
||
define ptr @icmp_ptrdiff_gt(ptr %0, ptr %1, ptr %2) { | ||
; CHECK-LABEL: @icmp_ptrdiff_gt( | ||
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0:%.*]], i64 1 | ||
; CHECK-NEXT: [[TMP10:%.*]] = icmp ugt ptr [[TMP4]], [[TMP2:%.*]] | ||
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP10]], ptr [[TMP2]], ptr [[TMP4]] | ||
; CHECK-NEXT: ret ptr [[TMP11]] | ||
; | ||
%4 = getelementptr inbounds nuw i8, ptr %0, i64 1 | ||
%5 = ptrtoint ptr %4 to i64 | ||
%6 = ptrtoint ptr %1 to i64 | ||
%7 = sub i64 %5, %6 | ||
%8 = ptrtoint ptr %2 to i64 | ||
%9 = sub i64 %8, %6 | ||
%10 = icmp sgt i64 %7, %9 | ||
%11 = select i1 %10, ptr %2, ptr %4 | ||
ret ptr %11 | ||
} | ||
|
||
define ptr @icmp_ptrdiff_lt(ptr %0, ptr %1, ptr %2) { | ||
; CHECK-LABEL: @icmp_ptrdiff_lt( | ||
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0:%.*]], i64 1 | ||
; CHECK-NEXT: [[TMP10:%.*]] = icmp ult ptr [[TMP4]], [[TMP2:%.*]] | ||
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP10]], ptr [[TMP2]], ptr [[TMP4]] | ||
; CHECK-NEXT: ret ptr [[TMP11]] | ||
; | ||
%4 = getelementptr inbounds nuw i8, ptr %0, i64 1 | ||
%5 = ptrtoint ptr %4 to i64 | ||
%6 = ptrtoint ptr %1 to i64 | ||
%7 = sub i64 %5, %6 | ||
%8 = ptrtoint ptr %2 to i64 | ||
%9 = sub i64 %8, %6 | ||
%10 = icmp slt i64 %7, %9 | ||
%11 = select i1 %10, ptr %2, ptr %4 | ||
ret ptr %11 | ||
} | ||
|
||
define ptr @icmp_ptrdiff_ge(ptr %0, ptr %1, ptr %2) { | ||
; CHECK-LABEL: @icmp_ptrdiff_ge( | ||
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0:%.*]], i64 1 | ||
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp ult ptr [[TMP4]], [[TMP2:%.*]] | ||
; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[DOTNOT]], ptr [[TMP4]], ptr [[TMP2]] | ||
; CHECK-NEXT: ret ptr [[TMP10]] | ||
; | ||
%4 = getelementptr inbounds nuw i8, ptr %0, i64 1 | ||
%5 = ptrtoint ptr %4 to i64 | ||
%6 = ptrtoint ptr %1 to i64 | ||
%7 = sub i64 %5, %6 | ||
%8 = ptrtoint ptr %2 to i64 | ||
%9 = sub i64 %8, %6 | ||
%10 = icmp sge i64 %7, %9 | ||
%11 = select i1 %10, ptr %2, ptr %4 | ||
ret ptr %11 | ||
} | ||
|
||
define ptr @icmp_ptrdiff_le(ptr %0, ptr %1, ptr %2) { | ||
; CHECK-LABEL: @icmp_ptrdiff_le( | ||
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0:%.*]], i64 1 | ||
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp ugt ptr [[TMP4]], [[TMP2:%.*]] | ||
; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[DOTNOT]], ptr [[TMP4]], ptr [[TMP2]] | ||
; CHECK-NEXT: ret ptr [[TMP10]] | ||
; | ||
%4 = getelementptr inbounds nuw i8, ptr %0, i64 1 | ||
%5 = ptrtoint ptr %4 to i64 | ||
%6 = ptrtoint ptr %1 to i64 | ||
%7 = sub i64 %5, %6 | ||
%8 = ptrtoint ptr %2 to i64 | ||
%9 = sub i64 %8, %6 | ||
%10 = icmp sle i64 %7, %9 | ||
%11 = select i1 %10, ptr %2, ptr %4 | ||
ret ptr %11 | ||
} | ||
|
||
define ptr @icmp_ptrdiff_eq(ptr %0, ptr %1, ptr %2) { | ||
; CHECK-LABEL: @icmp_ptrdiff_eq( | ||
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0:%.*]], i64 1 | ||
; CHECK-NEXT: ret ptr [[TMP4]] | ||
; | ||
%4 = getelementptr inbounds nuw i8, ptr %0, i64 1 | ||
%5 = ptrtoint ptr %4 to i64 | ||
%6 = ptrtoint ptr %1 to i64 | ||
%7 = sub i64 %5, %6 | ||
%8 = ptrtoint ptr %2 to i64 | ||
%9 = sub i64 %8, %6 | ||
%10 = icmp eq i64 %7, %9 | ||
%11 = select i1 %10, ptr %2, ptr %4 | ||
ret ptr %11 | ||
} | ||
|
||
define ptr @icmp_ptrdiff_ne(ptr %0, ptr %1, ptr %2) { | ||
; CHECK-LABEL: @icmp_ptrdiff_ne( | ||
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0:%.*]], i64 1 | ||
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq ptr [[TMP4]], [[TMP2:%.*]] | ||
; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[DOTNOT]], ptr [[TMP4]], ptr [[TMP2]] | ||
; CHECK-NEXT: ret ptr [[TMP5]] | ||
; | ||
%4 = getelementptr inbounds nuw i8, ptr %0, i64 1 | ||
%5 = ptrtoint ptr %4 to i64 | ||
%6 = ptrtoint ptr %1 to i64 | ||
%7 = sub i64 %5, %6 | ||
%8 = ptrtoint ptr %2 to i64 | ||
%9 = sub i64 %8, %6 | ||
%10 = icmp ne i64 %7, %9 | ||
%11 = select i1 %10, ptr %2, ptr %4 | ||
ret ptr %11 | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it doesn't work in LLVM: https://alive2.llvm.org/ce/z/AYPSer
Can we set nsw for pointer difference here (we use sdiv to compute the number of elements, so it is meaningless if the sub signed wraps)?
See also
llvm-project/clang/lib/CodeGen/CGExprScalar.cpp
Lines 4695 to 4744 in d68f0c2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably okay, based strictly on the standard. But every time we touch this sort of thing, it breaks someone, somehow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using NSW runs into the issue that it the optimization would occur without adjusting the signed comparison into an unsigned comparison, and the pointer comparison won't behave correctly as a signed comparison. (I tried that approach initially).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the case listed here is UB since pointer subtraction is only defined for two pointers to the same object.
@keinflue dug up this in draft N3220 adjacent to C23 says in §6.5.7:
@efriedma-quic is this something that you would rather not have merged in? My understanding is that this doesn't hit often (only hits once in Spec2017 (502.GCC)), so if you think this will be a headache down the line it might not be worth merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, the pointers have to point to the same object, and the size of an object is limited. (From the C standard: "If the result is not representable in an object of that type, the behavior is undefined." From LangRef: "the size of all allocated objects must be non-negative and not exceed the largest signed integer that fits into the index type.")
That said, because people with security-sensitive codebases are very sensitive to undefined behavior, we're trying to be conservative with new forms of undefined behavior: there should be a sanitizer to detect the behavior, and there should be a flag to disable it. Which we can do here, I guess, but I'm not sure it's worth the trouble.