-
Notifications
You must be signed in to change notification settings - Fork 12k
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
[InstCombine] Handle ceil division idiom #100977
Open
antoniofrighetto
wants to merge
7
commits into
llvm:main
Choose a base branch
from
antoniofrighetto:feature/ic-ceil-div-idiom
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.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
91d8aea
[InstCombine] Introduce test for PR100977 (NFC)
antoniofrighetto af0d68c
[InstCombine] Handle ceil division idiom
antoniofrighetto b9593d2
!fixup change to m_value
antoniofrighetto cdf886f
!fixup pow2 no zero
antoniofrighetto 8552378
!fixup drop oneuse in shift amount
antoniofrighetto 4e5cd1a
!fixup style
antoniofrighetto cca259c
!fixup style
antoniofrighetto 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 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
213 changes: 213 additions & 0 deletions
213
llvm/test/Transforms/InstCombine/fold-ceil-div-idiom.ll
This file contains 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,213 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 | ||
; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
||
define i8 @ceil_div_idiom(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[X]], i8 [[Y]]) | ||
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 | ||
; CHECK-NEXT: [[OV_NOT:%.*]] = xor i1 [[OV]], true | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[OV_NOT]]) | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[SUB]], [[Y]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%wo = call {i8, i1} @llvm.uadd.with.overflow(i8 %x, i8 %y) | ||
%ov = extractvalue {i8, i1} %wo, 1 | ||
%ov.not = xor i1 %ov, true | ||
call void @llvm.assume(i1 %ov.not) | ||
|
||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%div = udiv i8 %sub, %y | ||
%add = add i8 %div, %bias | ||
ret i8 %add | ||
} | ||
|
||
define i8 @ceil_div_idiom_2(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom_2( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[OV_NOT:%.*]] = add nuw i8 [[X]], [[Y]] | ||
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[OV_NOT]] to i1 | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[TRUNC]]) | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[SUB]], [[Y]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%ov.not = add nuw i8 %x, %y | ||
%trunc = trunc i8 %ov.not to i1 | ||
call void @llvm.assume(i1 %trunc) | ||
|
||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%div = udiv i8 %sub, %y | ||
%add = add i8 %div, %bias | ||
ret i8 %add | ||
} | ||
|
||
define i8 @ceil_div_idiom_with_lshr(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom_with_lshr( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[X]], i8 [[Y]]) | ||
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 | ||
; CHECK-NEXT: [[OV_NOT:%.*]] = xor i1 [[OV]], true | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[OV_NOT]]) | ||
; CHECK-NEXT: [[CTPOPULATION:%.*]] = call range(i8 0, 9) i8 @llvm.ctpop.i8(i8 [[Y]]) | ||
; CHECK-NEXT: [[IS_POW_2:%.*]] = icmp eq i8 [[CTPOPULATION]], 1 | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[IS_POW_2]]) | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[CTLZ:%.*]] = tail call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[Y]], i1 true) | ||
; CHECK-NEXT: [[N:%.*]] = xor i8 [[CTLZ]], 7 | ||
; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[SUB]], [[N]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%wo = call {i8, i1} @llvm.uadd.with.overflow(i8 %x, i8 %y) | ||
%ov = extractvalue {i8, i1} %wo, 1 | ||
%ov.not = xor i1 %ov, true | ||
call void @llvm.assume(i1 %ov.not) | ||
|
||
%ctpopulation = call i8 @llvm.ctpop.i8(i8 %y) | ||
%is_pow_2 = icmp eq i8 %ctpopulation, 1 | ||
call void @llvm.assume(i1 %is_pow_2) | ||
|
||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%ctlz = tail call i8 @llvm.ctlz.i8(i8 %y, i1 true) | ||
%n = sub i8 7, %ctlz | ||
%div = lshr i8 %sub, %n | ||
%add = add i8 %div, %bias | ||
ret i8 %add | ||
} | ||
|
||
define i8 @ceil_div_idiom_add_may_overflow(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom_add_may_overflow( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[SUB]], [[Y]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%div = udiv i8 %sub, %y | ||
%add = add i8 %div, %bias | ||
ret i8 %add | ||
} | ||
|
||
define i8 @ceil_div_idiom_multiuse_bias(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom_multiuse_bias( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[X]], i8 [[Y]]) | ||
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 | ||
; CHECK-NEXT: [[OV_NOT:%.*]] = xor i1 [[OV]], true | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[OV_NOT]]) | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[SUB]], [[Y]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: call void @use(i8 [[BIAS]]) | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%wo = call {i8, i1} @llvm.uadd.with.overflow(i8 %x, i8 %y) | ||
%ov = extractvalue {i8, i1} %wo, 1 | ||
%ov.not = xor i1 %ov, true | ||
call void @llvm.assume(i1 %ov.not) | ||
|
||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%div = udiv i8 %sub, %y | ||
%add = add i8 %div, %bias | ||
call void @use(i8 %bias) | ||
ret i8 %add | ||
} | ||
|
||
define i8 @ceil_div_idiom_with_lshr_not_power_2(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom_with_lshr_not_power_2( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[X]], i8 [[Y]]) | ||
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 | ||
; CHECK-NEXT: [[OV_NOT:%.*]] = xor i1 [[OV]], true | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[OV_NOT]]) | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[CTLZ:%.*]] = tail call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[Y]], i1 true) | ||
; CHECK-NEXT: [[N:%.*]] = xor i8 [[CTLZ]], 7 | ||
; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[SUB]], [[N]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%wo = call {i8, i1} @llvm.uadd.with.overflow(i8 %x, i8 %y) | ||
%ov = extractvalue {i8, i1} %wo, 1 | ||
%ov.not = xor i1 %ov, true | ||
call void @llvm.assume(i1 %ov.not) | ||
|
||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%ctlz = tail call i8 @llvm.ctlz.i8(i8 %y, i1 true) | ||
%n = sub i8 7, %ctlz | ||
%div = lshr i8 %sub, %n | ||
%add = add i8 %div, %bias | ||
ret i8 %add | ||
} | ||
|
||
define i8 @ceil_div_idiom_with_lshr_wrong_bw(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define i8 @ceil_div_idiom_with_lshr_wrong_bw( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[X]], i8 [[Y]]) | ||
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 | ||
; CHECK-NEXT: [[OV_NOT:%.*]] = xor i1 [[OV]], true | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[OV_NOT]]) | ||
; CHECK-NEXT: [[CTPOPULATION:%.*]] = call range(i8 0, 9) i8 @llvm.ctpop.i8(i8 [[Y]]) | ||
; CHECK-NEXT: [[IS_POW_2:%.*]] = icmp eq i8 [[CTPOPULATION]], 1 | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[IS_POW_2]]) | ||
; CHECK-NEXT: [[NONZERO:%.*]] = icmp ne i8 [[X]], 0 | ||
; CHECK-NEXT: [[BIAS:%.*]] = zext i1 [[NONZERO]] to i8 | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[BIAS]] | ||
; CHECK-NEXT: [[CTLZ:%.*]] = tail call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[Y]], i1 true) | ||
; CHECK-NEXT: [[N:%.*]] = sub nuw nsw i8 8, [[CTLZ]] | ||
; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[SUB]], [[N]] | ||
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[DIV]], [[BIAS]] | ||
; CHECK-NEXT: ret i8 [[ADD]] | ||
; | ||
%wo = call {i8, i1} @llvm.uadd.with.overflow(i8 %x, i8 %y) | ||
%ov = extractvalue {i8, i1} %wo, 1 | ||
%ov.not = xor i1 %ov, true | ||
call void @llvm.assume(i1 %ov.not) | ||
|
||
%ctpopulation = call i8 @llvm.ctpop.i8(i8 %y) | ||
%is_pow_2 = icmp eq i8 %ctpopulation, 1 | ||
call void @llvm.assume(i1 %is_pow_2) | ||
|
||
%nonzero = icmp ne i8 %x, 0 | ||
%bias = zext i1 %nonzero to i8 | ||
%sub = sub i8 %x, %bias | ||
%ctlz = tail call i8 @llvm.ctlz.i8(i8 %y, i1 true) | ||
%n = sub i8 8, %ctlz | ||
%div = lshr i8 %sub, %n | ||
%add = add i8 %div, %bias | ||
ret i8 %add | ||
} | ||
|
||
declare { i8, i1 } @llvm.uadd.with.overflow.i8(i8, i8) | ||
declare i8 @llvm.ctpop.i8(i8) | ||
declare void @llvm.assume(i1) | ||
declare void @use(i8) |
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.
Why did you merge the log2 ceil part into this function? I don't think they share common code.
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.
They do not, but they do both involve the ceil part, thus I thought it was nice to batch them together under a single
foldCeilIdioms
.