-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[ConstantFold] Fold tgamma and tgammaf when the input parameter is a constant value.
#114065
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ | |
| #include <cassert> | ||
| #include <cerrno> | ||
| #include <cfenv> | ||
| #include <cfloat> | ||
| #include <cmath> | ||
| #include <cstdint> | ||
|
|
||
|
|
@@ -1698,9 +1699,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) { | |
| Name == "sinh" || Name == "sinhf" || | ||
| Name == "sqrt" || Name == "sqrtf"; | ||
| case 't': | ||
| return Name == "tan" || Name == "tanf" || | ||
| Name == "tanh" || Name == "tanhf" || | ||
| Name == "trunc" || Name == "truncf"; | ||
| return Name == "tan" || Name == "tanf" || Name == "tanh" || | ||
| Name == "tanhf" || Name == "trunc" || Name == "truncf" || | ||
| Name == "tgamma" || Name == "tgammaf"; | ||
| case '_': | ||
| // Check for various function names that get used for the math functions | ||
| // when the header files are preprocessed with the macro | ||
|
|
@@ -2417,6 +2418,15 @@ static Constant *ConstantFoldScalarCall1(StringRef Name, | |
| if (TLI->has(Func)) | ||
| return ConstantFoldFP(erf, APF, Ty); | ||
| break; | ||
| case LibFunc_tgamma: | ||
| case LibFunc_tgammaf: | ||
| // NOTE: These boundaries are somewhat conservative. | ||
| if (TLI->has(Func) && (Ty->isDoubleTy() && (APF > APFloat(1 / DBL_MAX) && | ||
| APF < APFloat(171.0)) || | ||
| Ty->isFloatTy() && (APF > APFloat(1 / FLT_MAX) && | ||
| APF < APFloat(35.0f)))) | ||
| return ConstantFoldFP(tgamma, APF, Ty); | ||
| break; | ||
| case LibFunc_nearbyint: | ||
| case LibFunc_nearbyintf: | ||
| case LibFunc_rint: | ||
|
|
@@ -3629,6 +3639,10 @@ bool llvm::isMathLibCallNoop(const CallBase *Call, | |
| case LibFunc_sqrtf: | ||
| return Op.isNaN() || Op.isZero() || !Op.isNegative(); | ||
|
|
||
| case LibFunc_tgamma: | ||
| case LibFunc_tgammaf: | ||
| return true; | ||
|
Contributor
Author
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. Since we explicitly limit the input range, it should be safe to return true here. Ideally, this function could be integrated into the constant folding process. |
||
|
|
||
| // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p, | ||
| // maybe others? | ||
| default: | ||
|
|
||
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,237 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
|
||
| define float @tgammaf_const() { | ||
| ; CHECK-LABEL: define float @tgammaf_const() { | ||
| ; CHECK-NEXT: ret float 0x479A216280000000 | ||
| ; | ||
| %r = call float @tgammaf(float 34.0) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_const() { | ||
| ; CHECK-LABEL: define double @tgamma_const() { | ||
| ; CHECK-NEXT: ret double 0x605166C698CF183B | ||
| ; | ||
| %r = call double @tgamma(double 100.0) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_const_overflow() { | ||
| ; CHECK-LABEL: define float @tgammaf_const_overflow() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 3.600000e+01) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 36.0) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_const_overflow() { | ||
| ; CHECK-LABEL: define double @tgamma_const_overflow() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 1.720000e+02) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 172.0) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_minus_one() { | ||
| ; CHECK-LABEL: define float @tgammaf_minus_one() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float -1.000000e+00) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float -1.000000e+00) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_minus_one() { | ||
| ; CHECK-LABEL: define double @tgamma_minus_one() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double -1.000000e+00) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double -1.000000e+00) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_minus_one_memory_none() { | ||
| ; CHECK-LABEL: define float @tgammaf_minus_one_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float -1.000000e+00) #[[ATTR1:[0-9]+]] | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float -1.000000e+00) readnone | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_minus_one_memory_none() { | ||
| ; CHECK-LABEL: define double @tgamma_minus_one_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double -1.000000e+00) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double -1.000000e+00) readnone | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_zero() { | ||
| ; CHECK-LABEL: define float @tgammaf_zero() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0.000000e+00) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0.000000e+00) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_zero() { | ||
| ; CHECK-LABEL: define double @tgamma_zero() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0.000000e+00) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0.000000e+00) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_neg_zero() { | ||
| ; CHECK-LABEL: define float @tgammaf_neg_zero() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float -0.000000e+00) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float -0.000000e+00) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_neg_zero() { | ||
| ; CHECK-LABEL: define double @tgamma_neg_zero() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double -0.000000e+00) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double -0.000000e+00) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_inf() { | ||
| ; CHECK-LABEL: define float @tgammaf_inf() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF0000000000000) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0x7FF0000000000000) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_inf() { | ||
| ; CHECK-LABEL: define double @tgamma_inf() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF0000000000000) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0x7FF0000000000000) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_inf_memory_none() { | ||
| ; CHECK-LABEL: define float @tgammaf_inf_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF0000000000000) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0x7FF0000000000000) readnone | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_inf_memory_none() { | ||
| ; CHECK-LABEL: define double @tgamma_inf_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF0000000000000) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0x7FF0000000000000) readnone | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_neg_inf() { | ||
| ; CHECK-LABEL: define float @tgammaf_neg_inf() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0xFFF0000000000000) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0xFFF0000000000000) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_neg_inf() { | ||
| ; CHECK-LABEL: define double @tgamma_neg_inf() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0xFFF0000000000000) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0xFFF0000000000000) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_neg_inf_memory_none() { | ||
| ; CHECK-LABEL: define float @tgammaf_neg_inf_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0xFFF0000000000000) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0xFFF0000000000000) readnone | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_neg_inf_memory_none() { | ||
| ; CHECK-LABEL: define double @tgamma_neg_inf_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0xFFF0000000000000) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0xFFF0000000000000) readnone | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_nan() { | ||
| ; CHECK-LABEL: define float @tgammaf_nan() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF8000000000000) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0x7FF8000000000000) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_nan() { | ||
| ; CHECK-LABEL: define double @tgamma_nan() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF8000000000000) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0x7FF8000000000000) | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_nan_memory_none() { | ||
| ; CHECK-LABEL: define float @tgammaf_nan_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF8000000000000) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float 0x7FF8000000000000) readnone | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_nan_memory_none() { | ||
| ; CHECK-LABEL: define double @tgamma_nan_memory_none() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF8000000000000) #[[ATTR1]] | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double 0x7FF8000000000000) readnone | ||
| ret double %r | ||
| } | ||
|
|
||
| define float @tgammaf_poison() { | ||
| ; CHECK-LABEL: define float @tgammaf_poison() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float poison) | ||
| ; CHECK-NEXT: ret float [[R]] | ||
| ; | ||
| %r = call float @tgammaf(float poison) | ||
| ret float %r | ||
| } | ||
|
|
||
| define double @tgamma_poison() { | ||
| ; CHECK-LABEL: define double @tgamma_poison() { | ||
| ; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double poison) | ||
| ; CHECK-NEXT: ret double [[R]] | ||
| ; | ||
| %r = call double @tgamma(double poison) | ||
| ret double %r | ||
| } | ||
|
|
||
| declare float @tgammaf(float) willreturn | ||
| declare double @tgamma(double) willreturn |
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.
Tests for at and above these limits?
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.
llvm-project/llvm/test/Transforms/InstCombine/tgamma.ll
Lines 20 to 36 in 8c345ec
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.
Don't think the double case is covered? Also the 1 / MAX?
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.
Added test case for in and out of range.