-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[InstCombine] Fold ceil(X / (2 ^ C)) == 0 -> X == 0
#143683
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
6 commits
Select commit
Hold shift + click to select a range
8e9969d
pre-commit test
el-ev 1a91c57
[InstCombine] Fold `ceil(X >> C) == 0 -> X == 0`
el-ev 8830167
add test
el-ev 32cf848
match both or and add
el-ev e854101
add `stripNullTest` and update `isKnownNonZero`
el-ev da1b1d0
Update llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
el-ev 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
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,308 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
|
||
| define i1 @ceil_shift4(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift4( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP1]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %is_zero = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %is_zero | ||
| } | ||
|
|
||
| define i1 @ceil_shift4_add(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift4_add( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP6]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %ceil = add i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %ceil, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift6(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift6( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP1]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 6 | ||
| %rem = and i32 %arg0, 63 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift6_ne(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift6_ne( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[RES:%.*]] = icmp ne i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[RES]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 6 | ||
| %rem = and i32 %arg0, 63 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp ne i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift11(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift11( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP1]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 11 | ||
| %rem = and i32 %arg0, 2047 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift11_ne(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift11_ne( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[RES:%.*]] = icmp ne i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[RES]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 6 | ||
| %rem = and i32 %arg0, 63 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp ne i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift0(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift0( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP1]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 0 | ||
| %rem = and i32 %arg0, 0 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift4_comm(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift4_comm( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP6]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %zext_has_rem, %quot | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| declare void @use(i32) | ||
|
|
||
| define i1 @ceil_shift4_used_1(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift4_used_1( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[ARG0]], 4 | ||
| ; CHECK-NEXT: call void @use(i32 [[TMP1]]) | ||
| ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP6]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| call void @use(i32 %quot) | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift4_used_5(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift4_used_5( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[ARG0]], 4 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[ARG0]], 15 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = or i32 [[TMP1]], [[TMP4]] | ||
| ; CHECK-NEXT: call void @use(i32 [[TMP5]]) | ||
| ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP6]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| call void @use(i32 %quot_or_rem) | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift4_used_add_nuw_nsw(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift4_used_add_nuw_nsw( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[QUOT:%.*]] = lshr i32 [[ARG0]], 4 | ||
| ; CHECK-NEXT: [[REM:%.*]] = and i32 [[ARG0]], 15 | ||
| ; CHECK-NEXT: [[HAS_REM:%.*]] = icmp ne i32 [[REM]], 0 | ||
| ; CHECK-NEXT: [[ZEXT_HAS_REM:%.*]] = zext i1 [[HAS_REM]] to i32 | ||
| ; CHECK-NEXT: [[CEIL:%.*]] = add nuw nsw i32 [[QUOT]], [[ZEXT_HAS_REM]] | ||
| ; CHECK-NEXT: call void @use(i32 [[CEIL]]) | ||
| ; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[ARG0]], 0 | ||
| ; CHECK-NEXT: ret i1 [[RES]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %ceil = add nuw nsw i32 %quot, %zext_has_rem | ||
| call void @use(i32 %ceil) | ||
| %res = icmp eq i32 %ceil, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define <4 x i1> @ceil_shift4_v4i32(<4 x i32> %arg0) { | ||
| ; CHECK-LABEL: define <4 x i1> @ceil_shift4_v4i32( | ||
| ; CHECK-SAME: <4 x i32> [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <4 x i32> [[ARG0]], zeroinitializer | ||
| ; CHECK-NEXT: ret <4 x i1> [[TMP1]] | ||
| ; | ||
| %quot = lshr <4 x i32> %arg0, splat (i32 16) | ||
| %rem = and <4 x i32> %arg0, splat (i32 65535) | ||
| %has_rem = icmp ne <4 x i32> %rem, zeroinitializer | ||
| %zext_has_rem = zext <4 x i1> %has_rem to <4 x i32> | ||
| %quot_or_rem = or <4 x i32> %quot, %zext_has_rem | ||
| %res = icmp eq <4 x i32> %quot_or_rem, zeroinitializer | ||
| ret <4 x i1> %res | ||
| } | ||
|
|
||
| define <8 x i1> @ceil_shift4_v8i16(<8 x i16> %arg0) { | ||
| ; CHECK-LABEL: define <8 x i1> @ceil_shift4_v8i16( | ||
| ; CHECK-SAME: <8 x i16> [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <8 x i16> [[ARG0]], zeroinitializer | ||
| ; CHECK-NEXT: ret <8 x i1> [[TMP1]] | ||
| ; | ||
| %quot = lshr <8 x i16> %arg0, splat (i16 4) | ||
| %rem = and <8 x i16> %arg0, splat (i16 15) | ||
| %has_rem = icmp ne <8 x i16> %rem, zeroinitializer | ||
| %zext_has_rem = zext <8 x i1> %has_rem to <8 x i16> | ||
| %quot_or_rem = or <8 x i16> %quot, %zext_has_rem | ||
| %res = icmp eq <8 x i16> %quot_or_rem, zeroinitializer | ||
| ret <8 x i1> %res | ||
| } | ||
|
|
||
| ; negative tests | ||
|
|
||
| define i1 @ceil_shift_not_mask_1(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift_not_mask_1( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[ARG0]], 4 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[ARG0]], 31 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = or i32 [[TMP1]], [[TMP4]] | ||
| ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[TMP5]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP6]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 4 | ||
| %rem = and i32 %arg0, 31 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift_not_mask_2(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift_not_mask_2( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[ARG0]], 5 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[ARG0]], 15 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = or i32 [[TMP1]], [[TMP4]] | ||
| ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[TMP5]], 0 | ||
| ; CHECK-NEXT: ret i1 [[TMP6]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 5 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_or_rem = or i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_or_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i1 @ceil_shift_not_add_or(i32 %arg0) { | ||
| ; CHECK-LABEL: define i1 @ceil_shift_not_add_or( | ||
| ; CHECK-SAME: i32 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[REM:%.*]] = and i32 [[ARG0]], 15 | ||
| ; CHECK-NEXT: [[HAS_REM_NOT:%.*]] = icmp eq i32 [[REM]], 0 | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[ARG0]], 32 | ||
| ; CHECK-NEXT: [[RES1:%.*]] = icmp eq i32 [[TMP1]], 0 | ||
| ; CHECK-NEXT: [[RES:%.*]] = or i1 [[HAS_REM_NOT]], [[RES1]] | ||
| ; CHECK-NEXT: ret i1 [[RES]] | ||
| ; | ||
| %quot = lshr i32 %arg0, 5 | ||
| %rem = and i32 %arg0, 15 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %quot_and_rem = and i32 %quot, %zext_has_rem | ||
| %res = icmp eq i32 %quot_and_rem, 0 | ||
| ret i1 %res | ||
| } | ||
|
|
||
| define i32 @ceil_shift_should_infer_ge_zero(i32 %x) { | ||
| ; CHECK-LABEL: define i32 @ceil_shift_should_infer_ge_zero( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i32 [[X]], 0 | ||
| ; CHECK-NEXT: br i1 [[COND_NOT]], label %[[IF_ELSE:.*]], label %[[IF_THEN:.*]] | ||
| ; CHECK: [[IF_THEN]]: | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 20 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[X]], 1048575 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = add nuw nsw i32 [[TMP1]], [[TMP4]] | ||
| ; CHECK-NEXT: ret i32 [[TMP5]] | ||
| ; CHECK: [[IF_ELSE]]: | ||
| ; CHECK-NEXT: ret i32 0 | ||
| ; | ||
| %cond = icmp ne i32 %x, 0 | ||
| br i1 %cond, label %if.then, label %if.else | ||
|
|
||
| if.then: | ||
| %quot = lshr i32 %x, 20 | ||
| %rem = and i32 %x, 1048575 | ||
| %has_rem = icmp ne i32 %rem, 0 | ||
| %zext_has_rem = zext i1 %has_rem to i32 | ||
| %ceil = add nuw nsw i32 %quot, %zext_has_rem | ||
| %max = call i32 @llvm.umax.i32(i32 %ceil, i32 1) | ||
| ret i32 %max | ||
|
|
||
| if.else: | ||
| ret i32 0 | ||
| } |
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.