-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Extend fcmp+select folding to minnum/maxnum intrinsics #112088
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 all commits
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 |
---|---|---|
|
@@ -67,10 +67,10 @@ define float @clamp_float_fast_ordered_nonstrict_minmax(float %x) { | |
; (X < C1) ? C1 : MIN(X, C2) | ||
define float @clamp_float_fast_unordered_strict_maxmin(float %x) { | ||
; CHECK-LABEL: @clamp_float_fast_unordered_strict_maxmin( | ||
; CHECK-NEXT: [[CMP2_INV:%.*]] = fcmp fast oge float [[X:%.*]], 2.550000e+02 | ||
; CHECK-NEXT: [[MIN:%.*]] = select fast i1 [[CMP2_INV]], float 2.550000e+02, float [[X]] | ||
; CHECK-NEXT: [[R1:%.*]] = call fast float @llvm.maxnum.f32(float [[MIN]], float 1.000000e+00) | ||
; CHECK-NEXT: ret float [[R1]] | ||
; CHECK-NEXT: [[MIN:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 2.550000e+02) | ||
; CHECK-NEXT: [[CMP1:%.*]] = fcmp fast ult float [[X]], 1.000000e+00 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], float 1.000000e+00, float [[MIN]] | ||
; CHECK-NEXT: ret float [[R]] | ||
; | ||
%cmp2 = fcmp fast ult float %x, 255.0 | ||
%min = select i1 %cmp2, float %x, float 255.0 | ||
|
@@ -82,10 +82,10 @@ define float @clamp_float_fast_unordered_strict_maxmin(float %x) { | |
; (X <= C1) ? C1 : MIN(X, C2) | ||
define float @clamp_float_fast_unordered_nonstrict_maxmin(float %x) { | ||
; CHECK-LABEL: @clamp_float_fast_unordered_nonstrict_maxmin( | ||
; CHECK-NEXT: [[CMP2_INV:%.*]] = fcmp fast oge float [[X:%.*]], 2.550000e+02 | ||
; CHECK-NEXT: [[MIN:%.*]] = select fast i1 [[CMP2_INV]], float 2.550000e+02, float [[X]] | ||
; CHECK-NEXT: [[R1:%.*]] = call fast float @llvm.maxnum.f32(float [[MIN]], float 1.000000e+00) | ||
; CHECK-NEXT: ret float [[R1]] | ||
; CHECK-NEXT: [[MIN:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 2.550000e+02) | ||
; CHECK-NEXT: [[CMP1:%.*]] = fcmp fast ule float [[X]], 1.000000e+00 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], float 1.000000e+00, float [[MIN]] | ||
; CHECK-NEXT: ret float [[R]] | ||
; | ||
%cmp2 = fcmp fast ult float %x, 255.0 | ||
%min = select i1 %cmp2, float %x, float 255.0 | ||
|
@@ -97,10 +97,10 @@ define float @clamp_float_fast_unordered_nonstrict_maxmin(float %x) { | |
; (X > C1) ? C1 : MAX(X, C2) | ||
define float @clamp_float_fast_unordered_strict_minmax(float %x) { | ||
; CHECK-LABEL: @clamp_float_fast_unordered_strict_minmax( | ||
; CHECK-NEXT: [[CMP2_INV:%.*]] = fcmp fast ole float [[X:%.*]], 1.000000e+00 | ||
; CHECK-NEXT: [[MAX:%.*]] = select fast i1 [[CMP2_INV]], float 1.000000e+00, float [[X]] | ||
; CHECK-NEXT: [[R1:%.*]] = call fast float @llvm.minnum.f32(float [[MAX]], float 2.550000e+02) | ||
; CHECK-NEXT: ret float [[R1]] | ||
; CHECK-NEXT: [[MAX:%.*]] = call fast float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00) | ||
; CHECK-NEXT: [[CMP1:%.*]] = fcmp fast ugt float [[X]], 2.550000e+02 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], float 2.550000e+02, float [[MAX]] | ||
; CHECK-NEXT: ret float [[R]] | ||
; | ||
%cmp2 = fcmp fast ugt float %x, 1.0 | ||
%max = select i1 %cmp2, float %x, float 1.0 | ||
|
@@ -112,10 +112,10 @@ define float @clamp_float_fast_unordered_strict_minmax(float %x) { | |
; (X >= C1) ? C1 : MAX(X, C2) | ||
define float @clamp_float_fast_unordered_nonstrict_minmax(float %x) { | ||
; CHECK-LABEL: @clamp_float_fast_unordered_nonstrict_minmax( | ||
; CHECK-NEXT: [[CMP2_INV:%.*]] = fcmp fast ole float [[X:%.*]], 1.000000e+00 | ||
; CHECK-NEXT: [[MAX:%.*]] = select fast i1 [[CMP2_INV]], float 1.000000e+00, float [[X]] | ||
; CHECK-NEXT: [[R1:%.*]] = call fast float @llvm.minnum.f32(float [[MAX]], float 2.550000e+02) | ||
; CHECK-NEXT: ret float [[R1]] | ||
; CHECK-NEXT: [[MAX:%.*]] = call fast float @llvm.maxnum.f32(float [[X:%.*]], float 1.000000e+00) | ||
; CHECK-NEXT: [[CMP1:%.*]] = fcmp fast uge float [[X]], 2.550000e+02 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP1]], float 2.550000e+02, float [[MAX]] | ||
; CHECK-NEXT: ret float [[R]] | ||
; | ||
%cmp2 = fcmp fast ugt float %x, 1.0 | ||
%max = select i1 %cmp2, float %x, float 1.0 | ||
|
@@ -127,13 +127,12 @@ define float @clamp_float_fast_unordered_nonstrict_minmax(float %x) { | |
; Some more checks with fast | ||
|
||
; (X > 1.0) ? min(x, 255.0) : 1.0 | ||
; That did not match because select was in inverse order. | ||
define float @clamp_test_1(float %x) { | ||
; CHECK-LABEL: @clamp_test_1( | ||
; CHECK-NEXT: [[INNER_CMP_INV:%.*]] = fcmp fast oge float [[X:%.*]], 2.550000e+02 | ||
; CHECK-NEXT: [[INNER_SEL:%.*]] = select fast i1 [[INNER_CMP_INV]], float 2.550000e+02, float [[X]] | ||
; CHECK-NEXT: [[R1:%.*]] = call fast float @llvm.maxnum.f32(float [[INNER_SEL]], float 1.000000e+00) | ||
; CHECK-NEXT: ret float [[R1]] | ||
; CHECK-NEXT: [[INNER_SEL:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 2.550000e+02) | ||
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. This traded a maxnum at the end for a minnum at the beginning. But I don't see how either of these matched. The fast flags are on the fcmp, and not the select? I suppose the nsz is implied on the select by the usage, but it isn't propagated? 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 happens due to this transformation: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L3773-L3794. The first pass of InstCombine propagates Considering that all operands of |
||
; CHECK-NEXT: [[OUTER_CMP:%.*]] = fcmp fast ugt float [[X]], 1.000000e+00 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[OUTER_CMP]], float [[INNER_SEL]], float 1.000000e+00 | ||
; CHECK-NEXT: ret float [[R]] | ||
; | ||
%inner_cmp = fcmp fast ult float %x, 255.0 | ||
%inner_sel = select i1 %inner_cmp, float %x, float 255.0 | ||
|
@@ -147,8 +146,7 @@ define float @clamp_test_1(float %x) { | |
; Like @clamp_test_1 but HighConst < LowConst | ||
define float @clamp_negative_wrong_const(float %x) { | ||
; CHECK-LABEL: @clamp_negative_wrong_const( | ||
; CHECK-NEXT: [[INNER_CMP_INV:%.*]] = fcmp fast oge float [[X:%.*]], 2.550000e+02 | ||
; CHECK-NEXT: [[INNER_SEL:%.*]] = select fast i1 [[INNER_CMP_INV]], float 2.550000e+02, float [[X]] | ||
; CHECK-NEXT: [[INNER_SEL:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 2.550000e+02) | ||
; CHECK-NEXT: [[OUTER_CMP:%.*]] = fcmp fast ugt float [[X]], 5.120000e+02 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[OUTER_CMP]], float [[INNER_SEL]], float 5.120000e+02 | ||
; CHECK-NEXT: ret float [[R]] | ||
|
@@ -163,8 +161,7 @@ define float @clamp_negative_wrong_const(float %x) { | |
; Like @clamp_test_1 but both are min | ||
define float @clamp_negative_same_op(float %x) { | ||
; CHECK-LABEL: @clamp_negative_same_op( | ||
; CHECK-NEXT: [[INNER_CMP_INV:%.*]] = fcmp fast oge float [[X:%.*]], 2.550000e+02 | ||
; CHECK-NEXT: [[INNER_SEL:%.*]] = select fast i1 [[INNER_CMP_INV]], float 2.550000e+02, float [[X]] | ||
; CHECK-NEXT: [[INNER_SEL:%.*]] = call fast float @llvm.minnum.f32(float [[X:%.*]], float 2.550000e+02) | ||
; CHECK-NEXT: [[OUTER_CMP:%.*]] = fcmp fast ult float [[X]], 1.000000e+00 | ||
; CHECK-NEXT: [[R:%.*]] = select i1 [[OUTER_CMP]], float [[INNER_SEL]], float 1.000000e+00 | ||
; CHECK-NEXT: ret float [[R]] | ||
|
Uh oh!
There was an error while loading. Please reload this page.