-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CVP] Implement type narrowing for LShr #119577
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
44e162b
[CVP] Implement type narrowing for LShr
adam-bzowski 2e44ffd
[CVP] Implement type narrowing for LShr
adam-bzowski 0ce38dd
[CVP] Implement type narrowing for LShr
adam-bzowski e2cfa08
[CVP] Implement type narrowing for LShr
adam-bzowski 2d9af1c
[CVP] Implement type narrowing for LShr
adam-bzowski 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
115 changes: 115 additions & 0 deletions
115
llvm/test/Transforms/CorrelatedValuePropagation/lshr-plus-instcombine.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,115 @@ | ||
| ; RUN: opt < %s -passes="correlated-propagation,instcombine" -S | FileCheck %s | ||
|
|
||
| ; The tests below are the same as in lshr.ll | ||
| ; Here we test whether the CorrelatedValuePropagation pass | ||
| ; composed with InstCombinePass produces the expected optimizations. | ||
|
|
||
| ; CHECK-LABEL: @trunc_test1 | ||
| ; CHECK-NEXT: [[A1:%.*]] = lshr i32 [[A:%.*]], 16 | ||
| ; CHECK-NEXT: [[CARG:%.*]] = trunc nuw i32 [[A1]] to i16 | ||
| ; CHECK-NEXT: [[B1:%.*]] = trunc i32 [[B:%.*]] to i16 | ||
| ; CHECK-NEXT: [[CSHIFT:%.*]] = and i16 [[B1]], 15 | ||
| ; CHECK-NEXT: [[C1:%.*]] = lshr i16 [[CARG]], [[CSHIFT]] | ||
| ; CHECK-NEXT: ret i16 [[C1]] | ||
|
|
||
| define i16 @trunc_test1(i32 %a, i32 %b) { | ||
| %a.eff.trunc = lshr i32 %a, 16 | ||
| %b.eff.trunc = and i32 %b, 15 | ||
| %c = lshr i32 %a.eff.trunc, %b.eff.trunc | ||
| %c.trunc = trunc i32 %c to i16 | ||
| ret i16 %c.trunc | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @trunc_test2 | ||
| ; CHECK-NEXT: [[C1:%.*]] = lshr i16 [[A:%.*]], 2 | ||
| ; CHECK-NEXT: ret i16 [[C1]] | ||
|
|
||
| define i16 @trunc_test2(i16 %a) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has been handled by InstCombine ( |
||
| %a.ext = zext i16 %a to i32 | ||
| %c = lshr i32 %a.ext, 2 | ||
| %c.trunc = trunc i32 %c to i16 | ||
| ret i16 %c.trunc | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @trunc_test3 | ||
| ; CHECK-NEXT: [[B:%.*]] = lshr i16 [[A:%.*]], 2 | ||
| ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i16 [[B]], 123 | ||
| ; CHECK-NEXT: ret i16 [[C]] | ||
|
|
||
| define i16 @trunc_test3(i16 %a) { | ||
| %a.ext = zext i16 %a to i32 | ||
| %b = lshr i32 %a.ext, 2 | ||
| %c = add i32 %b, 123 | ||
| %c.trunc = trunc i32 %c to i16 | ||
| ret i16 %c.trunc | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @trunc_test4 | ||
| ; CHECK-NEXT: [[A1:%.*]] = udiv i32 [[A:%.*]], 17000000 | ||
| ; CHECK-NEXT: [[B:%.*]] = trunc nuw nsw i32 [[A1]] to i16 | ||
| ; CHECK-NEXT: [[B1:%.*]] = lshr i16 [[B]], 2 | ||
| ; CHECK-NEXT: ret i16 [[B1]] | ||
|
|
||
| define i16 @trunc_test4(i32 %a) { | ||
| %a.eff.trunc = udiv i32 %a, 17000000 ; larger than 2^24 | ||
| %b = lshr i32 %a.eff.trunc, 2 | ||
| %b.trunc.1 = trunc i32 %b to i16 | ||
| %b.trunc.2 = trunc i32 %b to i8 | ||
| ret i16 %b.trunc.1 | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @trunc_test5 | ||
| ; CHECK-NEXT: [[A1:%.*]] = udiv i32 [[A:%.*]], 17000000 | ||
| ; CHECK-NEXT: [[B:%.*]] = lshr i32 [[A1]], 2 | ||
| ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i32 [[B]], 123 | ||
| ; CHECK-NEXT: ret i32 [[C]] | ||
|
|
||
| define i32 @trunc_test5(i32 %a) { | ||
| %a.eff.trunc = udiv i32 %a, 17000000 ; larger than 2^24 | ||
| %b = lshr i32 %a.eff.trunc, 2 | ||
| %b.trunc.1 = trunc i32 %b to i16 | ||
| %b.trunc.2 = trunc i32 %b to i8 | ||
| %c = add i32 %b, 123 | ||
| ret i32 %c | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @zero_test1 | ||
| ; CHECK-NEXT: ret i32 poison | ||
|
|
||
| define i32 @zero_test1(i32 %a) { | ||
| %b = lshr i32 %a, 32 | ||
| %c = add i32 %b, 123 | ||
| ret i32 %c | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @zero_test2 | ||
| ; CHECK-NEXT: ret i32 poison | ||
|
|
||
| define i32 @zero_test2(i32 %a, i32 %b) { | ||
| %b.large = add nuw nsw i32 %b, 50 | ||
| %c = lshr i32 %a, %b.large | ||
| %d = add i32 %c, 123 | ||
| ret i32 %d | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @zero_test3 | ||
| ; CHECK-NEXT: ret i32 123 | ||
|
|
||
| define i32 @zero_test3(i32 %a, i32 %b) { | ||
| %a.small = lshr i32 %a, 16 | ||
| %b.large = add nuw nsw i32 %b, 20 | ||
| %c = lshr i32 %a.small, %b.large | ||
| %d = add i32 %c, 123 | ||
| ret i32 %d | ||
| } | ||
|
|
||
| ; CHECK-LABEL: @zero_test4 | ||
| ; CHECK-NEXT: ret i32 123 | ||
|
|
||
| define i32 @zero_test4(i32 %a, i32 %b) { | ||
| %a.small = lshr i32 %a, 16 | ||
| %b.large = add nuw nsw i32 %b, 20 | ||
| %c = lshr exact i32 %a.small, %b.large | ||
| %d = add i32 %c, 123 | ||
| ret i32 %d | ||
| } | ||
Oops, something went wrong.
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.
Can you demonstrate that this patch will improve the codegen? https://godbolt.org/z/hbP9qrsxv
BTW this transformation is incorrect: https://alive2.llvm.org/ce/z/j2w5Wa
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.
https://godbolt.org/z/7EPnj9a8d
With the proposed patch the two functions generate identical (better) code.