-
Notifications
You must be signed in to change notification settings - Fork 11.6k
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
Fdim constant fold #109235
base: main
Are you sure you want to change the base?
Fdim constant fold #109235
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 |
---|---|---|
|
@@ -3080,6 +3080,41 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) { | |
return ConstantFP::get(CI->getType(), Rem); | ||
} | ||
|
||
/// Constant folds fdim | ||
Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) { | ||
const APFloat *X, *Y; | ||
// Check if both values are constants | ||
if (!match(CI->getArgOperand(0), m_APFloat(X)) || | ||
!match(CI->getArgOperand(1), m_APFloat(Y))) | ||
return nullptr; | ||
// If either argument is NaN, NaN is returned | ||
if (X->isNaN() || Y->isNaN()) | ||
return ConstantFP::getQNaN(CI->getType()); | ||
|
||
// If the difference if negative, return +0.0 | ||
if (*X <= *Y) | ||
return ConstantFP::get(CI->getType(), 0.0); | ||
|
||
APFloat ReturnVal = *X; | ||
APFloat::opStatus Status = | ||
ReturnVal.subtract(*Y, RoundingMode::NearestTiesToEven); | ||
switch (Status) { | ||
case APFloat::opStatus::opOK: | ||
break; | ||
case APFloat::opStatus::opOverflow: | ||
return ConstantFP::get( | ||
CI->getType(), | ||
APFloat::getLargest(X->getSemantics(), /*Negative=*/false)); | ||
case APFloat::opStatus::opUnderflow: | ||
return ConstantFP::get( | ||
CI->getType(), | ||
APFloat::getLargest(X->getSemantics(), /*Negative=*/true)); | ||
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 is incorrect. Underflow means the result is subnormal. |
||
default: | ||
return nullptr; | ||
} | ||
return ConstantFP::get(CI->getType(), ReturnVal); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Integer Library Call Optimizations | ||
//===----------------------------------------------------------------------===// | ||
|
@@ -4009,6 +4044,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI, | |
if (hasFloatVersion(M, CI->getCalledFunction()->getName())) | ||
return optimizeBinaryDoubleFP(CI, Builder, TLI); | ||
return nullptr; | ||
case LibFunc_fdim: | ||
case LibFunc_fdimf: | ||
case LibFunc_fdiml: | ||
return optimizeFdim(CI, Builder); | ||
case LibFunc_fminf: | ||
case LibFunc_fmin: | ||
case LibFunc_fminl: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
||
define double @fdim_double() { | ||
; CHECK-LABEL: define double @fdim_double() { | ||
; CHECK-NEXT: ret double 2.500000e+00 | ||
; | ||
%dim = call double @fdim (double 10.5, double 8.0) | ||
ret double %dim | ||
} | ||
|
||
define double @fdim_double1() { | ||
; CHECK-LABEL: define double @fdim_double1() { | ||
; CHECK-NEXT: ret double 0.000000e+00 | ||
; | ||
%dim = call double @fdim (double 7.0, double 8.0) | ||
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. Extra space before ( in all functions |
||
ret double %dim | ||
} | ||
|
||
define float @fdim_float() { | ||
; CHECK-LABEL: define float @fdim_float() { | ||
; CHECK-NEXT: ret float 2.500000e+00 | ||
; | ||
%dim = call float @fdimf (float 10.5, float 8.0) | ||
ret float %dim | ||
} | ||
|
||
define float @fdim_float1() { | ||
; CHECK-LABEL: define float @fdim_float1() { | ||
; CHECK-NEXT: ret float 0.000000e+00 | ||
; | ||
%dim = call float @fdimf (float 7.0, float 8.0) | ||
ret float %dim | ||
} | ||
|
||
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. Needs tests for all the edge cases. Can you also test poison/undef arguments in each position |
||
declare double @fdim(double, double) | ||
declare float @fdimf(float, float) |
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.
The result should be
inf
. Please add a tests forfdim(1e308, -1e308)
.BTW, it would be better to use
fmax(x - y, 0)
instead of handling all corner cases yourself.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.
yes, I read that on cppref but wasn't sure if it will handle all corner cases,
will change to that
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.
The special cases also set errno, in which case we cannot perform the fold unless the call is memory(none)