-
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
Changes from 1 commit
44e162b
2e44ffd
0ce38dd
e2cfa08
2d9af1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| ; 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: [[CSHIFT:%.*]] = trunc i32 [[B:%.*]] to i16 | ||
| ; CHECK-NEXT: [[C1:%.*]] = lshr i16 [[CARG]], [[CSHIFT]] | ||
| ; CHECK-NEXT: ret i16 [[C1]] | ||
|
|
||
| define i16 @trunc_test1(i32 %a, i32 %b) { | ||
|
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. 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
Contributor
Author
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.
https://godbolt.org/z/7EPnj9a8d With the proposed patch the two functions generate identical (better) code.
|
||
| %a.eff.trunc = lshr i32 %a, 16 | ||
| %b.eff.trunc = and i32 %b, 65535 | ||
| %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 | ||
| } | ||
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.
We should avoid narrowing the type to an illegal type. See also
InstCombinerImpl::shouldChangeType.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.
This part is taken from narrowUDivOrURem and narrowSDivOrSRem. It does look suspicious, though. I can correct in all 3 places or leave it.
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 fixed the types in narrowUDivOrURem, narrowSDivOrSRem, and narrowLShr. Now the narrowing can result only in i8, i16, or i32.