-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[InstCombine] When canonicalizing clamp like, also consider certain sgt/slt cases #153240
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
Open
bababuck
wants to merge
8
commits into
llvm:main
Choose a base branch
from
bababuck:bababuck/FoldICmpSel
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.
+190
−6
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
46767d0
Add test for clamp like case where the first select is a ([s/z]ext (i…
bababuck cdfaef9
[InstCombine] When canoncicalizing clamp like, also consider certain …
bababuck 3b55bb9
Simplify pattern matching
bababuck 35eaeb2
Formating
bababuck 73f7dcc
Update llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
bababuck 4456779
Formatting
bababuck 9f930fc
Fix typo
bababuck a14b782
Remove SwapReplacement logic
bababuck 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
159 changes: 159 additions & 0 deletions
159
llvm/test/Transforms/InstCombine/canonicalize-clamp-like-pattern-i1.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,159 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
| ; RUN: opt < %s -passes='instcombine<no-verify-fixpoint>' -S | FileCheck %s | ||
|
|
||
| ; Given a pattern like: | ||
| ; %old_cmp1 = icmp sgt i32 %x, C2 | ||
| ; %old_replacement = sext i1 %old_cmp1 to i32 | ||
| ; %old_cmp0 = icmp ult i32 %x, C0 | ||
| ; %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement | ||
| ; it can be rewriten as more canonical pattern: | ||
| ; %new_cmp2 = icmp sge i32 %x, C0 | ||
| ; %new_clamped_low = smax i32 %target_low, i32 %x | ||
| ; %r = select i1 %new_cmp2, i32 -1, i32 %new_clamped_low | ||
| ; Iff 0 s<= C2 s<= C0 | ||
| ; Also, ULT predicate can also be UGE; or UGT iff C0 != -1 (+invert result) | ||
| ; Also, SLT predicate can also be SGE; or SGT iff C2 != INT_MAX (+invert res.) | ||
|
|
||
| ;------------------------------------------------------------------------------- | ||
|
|
||
| ; clamp-like max case, can be optimized with max | ||
| define i32 @clamp_max_sgt(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_sgt( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 255 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 0) | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[TMP2]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult i32 %x, 256 | ||
| %cmp2 = icmp sgt i32 %x, 0 | ||
| %cond = sext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } | ||
|
|
||
| ; clamp-like max case with vector, can be optimized with max | ||
| define <2 x i32> @clamp_max_sgt_vec(<2 x i32> %x) { | ||
| ; CHECK-LABEL: @clamp_max_sgt_vec( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> [[X:%.*]], <i32 99, i32 255> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[X]], <2 x i32> zeroinitializer) | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> splat (i32 -1), <2 x i32> [[TMP2]] | ||
| ; CHECK-NEXT: ret <2 x i32> [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult <2 x i32> %x, <i32 100, i32 256> | ||
| %cmp2 = icmp sgt <2 x i32> %x, <i32 98, i32 254> | ||
| %cond = sext <2 x i1> %cmp2 to <2 x i32> | ||
| %cond3 = select <2 x i1> %or.cond, <2 x i32> %x, <2 x i32> %cond | ||
| ret <2 x i32> %cond3 | ||
| } | ||
|
|
||
| ; Not clamp-like vector | ||
| define <2 x i32> @clamp_max_vec(<2 x i32> %x) { | ||
| ; CHECK-LABEL: @clamp_max_vec( | ||
| ; CHECK-NEXT: [[OR_COND:%.*]] = icmp ult <2 x i32> [[X:%.*]], <i32 100, i32 256> | ||
| ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt <2 x i32> [[X]], <i32 128, i32 0> | ||
| ; CHECK-NEXT: [[COND:%.*]] = sext <2 x i1> [[CMP2]] to <2 x i32> | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select <2 x i1> [[OR_COND]], <2 x i32> [[X]], <2 x i32> [[COND]] | ||
| ; CHECK-NEXT: ret <2 x i32> [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult <2 x i32> %x, <i32 100, i32 256> | ||
| %cmp2 = icmp sgt <2 x i32> %x, <i32 128, i32 0> | ||
| %cond = sext <2 x i1> %cmp2 to <2 x i32> | ||
| %cond3 = select <2 x i1> %or.cond, <2 x i32> %x, <2 x i32> %cond | ||
| ret <2 x i32> %cond3 | ||
| } | ||
|
|
||
| ; clamp-like max case, can be optimized with max | ||
| define i32 @clamp_max_sgt_neg1(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_sgt_neg1( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 255 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 0) | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[TMP2]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult i32 %x, 256 | ||
| %cmp2 = icmp sgt i32 %x, -1 | ||
| %cond = sext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } | ||
|
|
||
| ; clamp-like max case, can be optimized with max | ||
| define i32 @clamp_max_sge(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_sge( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 255 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 0) | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[TMP2]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult i32 %x, 256 | ||
| %cmp2 = icmp sge i32 %x, 0 | ||
| %cond = sext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } | ||
|
|
||
| ; Don't support SLT cases, need to select 0 as the low value, -1 as high value | ||
| define i32 @clamp_max_slt(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_slt( | ||
| ; CHECK-NEXT: [[OR_COND:%.*]] = icmp ult i32 [[X:%.*]], 256 | ||
| ; CHECK-NEXT: [[COND:%.*]] = ashr i32 [[X]], 31 | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[OR_COND]], i32 [[X]], i32 [[COND]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult i32 %x, 256 | ||
| %cmp2 = icmp slt i32 %x, 0 | ||
| %cond = sext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } | ||
|
|
||
| ; Don't support SLE cases, need to select 0 as the low value, -1 as high value | ||
| define i32 @clamp_max_sle(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_sle( | ||
| ; CHECK-NEXT: [[OR_COND:%.*]] = icmp ult i32 [[X:%.*]], 256 | ||
| ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[X]], 1 | ||
| ; CHECK-NEXT: [[COND:%.*]] = sext i1 [[CMP2]] to i32 | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[OR_COND]], i32 [[X]], i32 [[COND]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult i32 %x, 256 | ||
| %cmp2 = icmp sle i32 %x, 0 | ||
| %cond = sext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } | ||
|
|
||
| ; Not selecting between 0, x, and -1, so can't be optimized with max | ||
| ; Select between 0, x, and 1 | ||
| define i32 @clamp_max_bad_values(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_bad_values( | ||
| ; CHECK-NEXT: [[OR_COND:%.*]] = icmp ult i32 [[X:%.*]], 256 | ||
| ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X]], 0 | ||
| ; CHECK-NEXT: [[COND:%.*]] = zext i1 [[CMP2]] to i32 | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[OR_COND]], i32 [[X]], i32 [[COND]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %or.cond = icmp ult i32 %x, 256 | ||
| %cmp2 = icmp sgt i32 %x, 0 | ||
| %cond = zext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } | ||
|
|
||
| ; Boundaries of range are not 0 and x (x is some positive integer) | ||
| define i32 @clamp_max_offset(i32 %x) { | ||
| ; CHECK-LABEL: @clamp_max_offset( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], -10 | ||
| ; CHECK-NEXT: [[OR_COND:%.*]] = icmp ult i32 [[TMP1]], 246 | ||
| ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[X]], 10 | ||
| ; CHECK-NEXT: [[COND:%.*]] = sext i1 [[CMP2]] to i32 | ||
| ; CHECK-NEXT: [[COND3:%.*]] = select i1 [[OR_COND]], i32 [[X]], i32 [[COND]] | ||
| ; CHECK-NEXT: ret i32 [[COND3]] | ||
| ; | ||
| %1 = add i32 %x, -10 | ||
| %or.cond = icmp ult i32 %1, 246 | ||
| %cmp2 = icmp sgt i32 %x, 10 | ||
| %cond = sext i1 %cmp2 to i32 | ||
| %cond3 = select i1 %or.cond, i32 %x, i32 %cond | ||
| ret i32 %cond3 | ||
| } |
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.