-
Notifications
You must be signed in to change notification settings - Fork 18.1k
[InstCombine] Recognize non-negative subtraction patterns #182597
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ca482d1
[InstCombine] Recognize non-negative subtraction patterns
user1342234 a03b0bc
Canonicalize ICmp to smin intrinsic
user1342234 fa5323b
Removed canonicalization of select instruction so smin
user1342234 520c731
Removed `select` instruction case in `isKnownNonNegativeFromMinOrGuar…
user1342234 cb11231
Renamed function to better match usage
user1342234 665a43f
Resolved requested changes
user1342234 da242a9
Fix commutative test in `sext-nonneg-sub.ll`
user1342234 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,51 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 | ||
| ; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
|
||
| ; Test that b - smin(b, a) is recognized as non-negative | ||
| define i64 @func1(i32 %a, i32 %b) { | ||
| ; CHECK-LABEL: define i64 @func1( | ||
| ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = tail call i32 @llvm.smin.i32(i32 [[B]], i32 [[A]]) | ||
| ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[B]], [[SPEC_SELECT]] | ||
| ; CHECK-NEXT: [[CONV:%.*]] = zext nneg i32 [[SUB]] to i64 | ||
| ; CHECK-NEXT: ret i64 [[CONV]] | ||
| ; | ||
| entry: | ||
| %spec.select = tail call i32 @llvm.smin.i32(i32 %b, i32 %a) | ||
| %sub = sub nsw i32 %b, %spec.select | ||
| %conv = sext i32 %sub to i64 | ||
| ret i64 %conv | ||
| } | ||
|
|
||
| ; Test commutative smin pattern: a - smin(a, b) should also optimize | ||
|
user1342234 marked this conversation as resolved.
Outdated
|
||
| define i64 @smin_commutative(i32 %a, i32 %b) { | ||
| ; CHECK-LABEL: define i64 @smin_commutative( | ||
| ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) { | ||
| ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[A]], i32 [[B]]) | ||
| ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[A]], [[MIN]] | ||
| ; CHECK-NEXT: [[EXT:%.*]] = zext nneg i32 [[SUB]] to i64 | ||
| ; CHECK-NEXT: ret i64 [[EXT]] | ||
| ; | ||
| %min = call i32 @llvm.smin.i32(i32 %a, i32 %b) | ||
| %sub = sub nsw i32 %a, %min | ||
| %ext = sext i32 %sub to i64 | ||
| ret i64 %ext | ||
| } | ||
|
|
||
|
|
||
|
user1342234 marked this conversation as resolved.
|
||
| ; NEGATIVE TEST: unguarded subtraction should NOT optimize | ||
| define i64 @neg_unguarded_sub(i32 %a, i32 %b) { | ||
| ; CHECK-LABEL: define i64 @neg_unguarded_sub( | ||
| ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) { | ||
| ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[B]], [[A]] | ||
| ; CHECK-NEXT: [[EXT:%.*]] = sext i32 [[SUB]] to i64 | ||
| ; CHECK-NEXT: ret i64 [[EXT]] | ||
| ; | ||
| %sub = sub nsw i32 %b, %a | ||
| %ext = sext i32 %sub to i64 | ||
| ret i64 %ext | ||
| } | ||
|
|
||
|
|
||
| declare i32 @llvm.smin.i32(i32, i32) | ||
|
user1342234 marked this conversation as resolved.
Outdated
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.