-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine]: Replace overflow calculation with intrinsic #168195
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
kper
wants to merge
1
commit into
llvm:main
Choose a base branch
from
kper:instcombine/162717-suboverflow
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.
+170
−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
103 changes: 103 additions & 0 deletions
103
llvm/test/Transforms/InstCombine/icmp-fold-ssub-overflow.ll
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,103 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
| ; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
|
||
| ; Fold | ||
| ; | ||
| ; int result = x - y; | ||
| ; return !(INT_MIN <= result && result <= INT_MAX); | ||
| ; | ||
| ; into | ||
| ; | ||
| ; __builtin_ssub_overflow(x, y) | ||
|
|
||
| define i1 @idiomatic_check_sub_i16(i16 %x, i16 %y) { | ||
| ; CHECK-LABEL: @idiomatic_check_sub_i16( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = call { i16, i1 } @llvm.ssub.with.overflow.i16(i16 [[X:%.*]], i16 [[Y:%.*]]) | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i16, i1 } [[TMP1]], 1 | ||
| ; CHECK-NEXT: ret i1 [[TMP2]] | ||
| ; | ||
| %3 = sext i16 %x to i32 | ||
| %4 = sext i16 %y to i32 | ||
| %5 = add nsw i32 %3, -32768 | ||
| %6 = sub nsw i32 %5, %4 | ||
| %7 = icmp ult i32 %6, -65536 | ||
| ret i1 %7 | ||
| } | ||
|
|
||
| define i1 @idiomatic_check_sub_i16_no_flags(i16 %x, i16 %y) { | ||
| ; CHECK-LABEL: @idiomatic_check_sub_i16_no_flags( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = call { i16, i1 } @llvm.ssub.with.overflow.i16(i16 [[X:%.*]], i16 [[Y:%.*]]) | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i16, i1 } [[TMP1]], 1 | ||
| ; CHECK-NEXT: ret i1 [[TMP2]] | ||
| ; | ||
| %3 = sext i16 %x to i32 | ||
| %4 = sext i16 %y to i32 | ||
| %5 = add i32 %3, -32768 | ||
| %6 = sub i32 %5, %4 | ||
| %7 = icmp ult i32 %6, -65536 | ||
| ret i1 %7 | ||
| } | ||
|
|
||
| define i1 @idiomatic_check_sub_i32(i32 %x, i32 %y) { | ||
| ; CHECK-LABEL: @idiomatic_check_sub_i32( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 [[X:%.*]], i32 [[Y:%.*]]) | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = extractvalue { i32, i1 } [[TMP1]], 1 | ||
| ; CHECK-NEXT: ret i1 [[TMP2]] | ||
| ; | ||
| %3 = sext i32 %x to i64 | ||
| %4 = sext i32 %y to i64 | ||
| %5 = add nsw i64 %3, -2147483648 | ||
| %6 = sub nsw i64 %5, %4 | ||
| %7 = icmp ult i64 %6, -4294967296 | ||
| ret i1 %7 | ||
| } | ||
|
|
||
| define i1 @idiomatic_check_sub_i32_negative_test_1(i32 %x, i32 %y) { | ||
| ; CHECK-LABEL: @idiomatic_check_sub_i32_negative_test_1( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[Y:%.*]] to i64 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = sub nsw i64 [[TMP1]], [[TMP2]] | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = icmp ult i64 [[TMP3]], -4294967296 | ||
| ; CHECK-NEXT: ret i1 [[TMP4]] | ||
| ; | ||
| %3 = sext i32 %x to i64 | ||
| %4 = sext i32 %y to i64 | ||
| %5 = add nsw i64 %3, 0 ; Constant wrong | ||
| %6 = sub nsw i64 %5, %4 | ||
| %7 = icmp ult i64 %6, -4294967296 | ||
| ret i1 %7 | ||
| } | ||
|
|
||
| define i1 @idiomatic_check_sub_i32_negative_test_2(i32 %x, i32 %y) { | ||
| ; CHECK-LABEL: @idiomatic_check_sub_i32_negative_test_2( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[Y:%.*]] to i64 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[TMP1]], -2147483648 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = sub nsw i64 [[TMP3]], [[TMP2]] | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = icmp ult i64 [[TMP4]], -4294967295 | ||
| ; CHECK-NEXT: ret i1 [[TMP5]] | ||
| ; | ||
| %3 = sext i32 %x to i64 | ||
| %4 = sext i32 %y to i64 | ||
| %5 = add nsw i64 %3, -2147483648 | ||
| %6 = sub nsw i64 %5, %4 | ||
| %7 = icmp ult i64 %6, -4294967295 ; Constant wrong | ||
| ret i1 %7 | ||
| } | ||
|
|
||
| define i1 @idiomatic_check_sub_i32_negative_test_3(i32 %x, i32 %y) { | ||
| ; CHECK-LABEL: @idiomatic_check_sub_i32_negative_test_3( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[Y:%.*]] to i64 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[TMP1]], -2147483648 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = sub nsw i64 [[TMP3]], [[TMP2]] | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = icmp slt i64 [[TMP4]], -4294967296 | ||
| ; CHECK-NEXT: ret i1 [[TMP5]] | ||
| ; | ||
| %3 = sext i32 %x to i64 | ||
| %4 = sext i32 %y to i64 | ||
| %5 = add nsw i64 %3, -2147483648 | ||
| %6 = sub nsw i64 %5, %4 | ||
| %7 = icmp slt i64 %6, -4294967296 ; wrong Condition | ||
| ret i1 %7 | ||
| } |
Oops, something went wrong.
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.
I use
getSExtValuebecause the variables have different bitwidths and I wasn't able to compare them directly.