diff options
author | c8ef <c8ef@outlook.com> | 2024-10-22 12:58:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-22 12:58:11 +0800 |
commit | b90ea5caade7b92796276937467a0dabc355a62e (patch) | |
tree | d030c92958db526d6e5f8e710819ba05ebfc80bc | |
parent | 2437784a178adb299cf6e363427e98611e3d2460 (diff) | |
download | llvm-b90ea5caade7b92796276937467a0dabc355a62e.zip llvm-b90ea5caade7b92796276937467a0dabc355a62e.tar.gz llvm-b90ea5caade7b92796276937467a0dabc355a62e.tar.bz2 |
[ConstantFold] Fold `erf` and `erff` when the input parameter is a constant value. (#113079)
This patch adds support for constant folding for the `erf` and `erff`
libc functions.
-rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 10 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/erf.ll | 221 |
2 files changed, 228 insertions, 3 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 530e928..c5a2c2f 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1672,8 +1672,8 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) { Name == "cos" || Name == "cosf" || Name == "cosh" || Name == "coshf"; case 'e': - return Name == "exp" || Name == "expf" || - Name == "exp2" || Name == "exp2f"; + return Name == "exp" || Name == "expf" || Name == "exp2" || + Name == "exp2f" || Name == "erf" || Name == "erff"; case 'f': return Name == "fabs" || Name == "fabsf" || Name == "floor" || Name == "floorf" || @@ -2412,6 +2412,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name, break; case LibFunc_logl: return nullptr; + case LibFunc_erf: + case LibFunc_erff: + if (TLI->has(Func)) + return ConstantFoldFP(erf, APF, Ty); + break; case LibFunc_nearbyint: case LibFunc_nearbyintf: case LibFunc_rint: @@ -3597,7 +3602,6 @@ bool llvm::isMathLibCallNoop(const CallBase *Call, // Per POSIX, this MAY fail if Op is denormal. We choose not failing. return true; - case LibFunc_asinl: case LibFunc_asin: case LibFunc_asinf: diff --git a/llvm/test/Transforms/InstCombine/erf.ll b/llvm/test/Transforms/InstCombine/erf.ll new file mode 100644 index 0000000..13d6545 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/erf.ll @@ -0,0 +1,221 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt < %s -passes=instcombine -mtriple x86_64-unknown-linux-gnu -S | FileCheck %s + +define float @erff_const() { +; CHECK-LABEL: define float @erff_const() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 5.000000e-01) +; CHECK-NEXT: ret float 0x3FE0A7EF60000000 +; + %r = call float @erff(float 5.000000e-01) + ret float %r +} + +define double @erf_const() { +; CHECK-LABEL: define double @erf_const() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double -5.000000e-01) +; CHECK-NEXT: ret double 0xBFE0A7EF5C18EDD2 +; + %r = call double @erf(double -5.000000e-01) + ret double %r +} + +define float @erff_zero() { +; CHECK-LABEL: define float @erff_zero() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0.000000e+00) +; CHECK-NEXT: ret float 0.000000e+00 +; + %r = call float @erff(float 0.000000e+00) + ret float %r +} + +define double @erf_zero() { +; CHECK-LABEL: define double @erf_zero() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0.000000e+00) +; CHECK-NEXT: ret double 0.000000e+00 +; + %r = call double @erf(double 0.000000e+00) + ret double %r +} + +define float @erff_neg_zero() { +; CHECK-LABEL: define float @erff_neg_zero() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float -0.000000e+00) +; CHECK-NEXT: ret float -0.000000e+00 +; + %r = call float @erff(float -0.000000e+00) + ret float %r +} + +define double @erf_neg_zero() { +; CHECK-LABEL: define double @erf_neg_zero() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double -0.000000e+00) +; CHECK-NEXT: ret double -0.000000e+00 +; + %r = call double @erf(double -0.000000e+00) + ret double %r +} + +define float @erff_inf() { +; CHECK-LABEL: define float @erff_inf() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF0000000000000) +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0x7FF0000000000000) + ret float %r +} + +define double @erf_inf() { +; CHECK-LABEL: define double @erf_inf() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF0000000000000) +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0x7FF0000000000000) + ret double %r +} + +define float @erff_inf_memory_none() { +; CHECK-LABEL: define float @erff_inf_memory_none() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF0000000000000) #[[ATTR1:[0-9]+]] +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0x7FF0000000000000) readnone + ret float %r +} + +define double @erf_inf_memory_none() { +; CHECK-LABEL: define double @erf_inf_memory_none() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF0000000000000) #[[ATTR1]] +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0x7FF0000000000000) readnone + ret double %r +} + +define float @erff_neg_inf() { +; CHECK-LABEL: define float @erff_neg_inf() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0xFFF0000000000000) +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0xFFF0000000000000) + ret float %r +} + +define double @erf_neg_inf() { +; CHECK-LABEL: define double @erf_neg_inf() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0xFFF0000000000000) +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0xFFF0000000000000) + ret double %r +} + +define float @erff_neg_inf_memory_none() { +; CHECK-LABEL: define float @erff_neg_inf_memory_none() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0xFFF0000000000000) #[[ATTR1]] +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0xFFF0000000000000) readnone + ret float %r +} + +define double @erf_neg_inf_memory_none() { +; CHECK-LABEL: define double @erf_neg_inf_memory_none() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0xFFF0000000000000) #[[ATTR1]] +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0xFFF0000000000000) readnone + ret double %r +} + +define float @erff_nan() { +; CHECK-LABEL: define float @erff_nan() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0x7FF8000000000000) + ret float %r +} + +define double @erf_nan() { +; CHECK-LABEL: define double @erf_nan() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0x7FF8000000000000) + ret double %r +} + +define float @erff_nan_memory_none() { +; CHECK-LABEL: define float @erff_nan_memory_none() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) #[[ATTR1]] +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0x7FF8000000000000) readnone + ret float %r +} + +define double @erf_nan_memory_none() { +; CHECK-LABEL: define double @erf_nan_memory_none() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) #[[ATTR1]] +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0x7FF8000000000000) readnone + ret double %r +} + +define float @erff_poison() { +; CHECK-LABEL: define float @erff_poison() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float poison) +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float poison) + ret float %r +} + +define double @erf_poison() { +; CHECK-LABEL: define double @erf_poison() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double poison) +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double poison) + ret double %r +} + +define float @erff_const_strictfp() { +; CHECK-LABEL: define float @erff_const_strictfp() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 5.000000e-01) #[[ATTR2:[0-9]+]] +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 5.000000e-01) strictfp + ret float %r +} + +define double @erf_const_strictfp() { +; CHECK-LABEL: define double @erf_const_strictfp() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double -5.000000e-01) #[[ATTR2]] +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double -5.000000e-01) strictfp + ret double %r +} + +define float @erff_nan_strictfp() { +; CHECK-LABEL: define float @erff_nan_strictfp() { +; CHECK-NEXT: [[R:%.*]] = call float @erff(float 0x7FF8000000000000) #[[ATTR2]] +; CHECK-NEXT: ret float [[R]] +; + %r = call float @erff(float 0x7FF8000000000000) strictfp + ret float %r +} + +define double @erf_nan_strictfp() { +; CHECK-LABEL: define double @erf_nan_strictfp() { +; CHECK-NEXT: [[R:%.*]] = call double @erf(double 0x7FF8000000000000) #[[ATTR2]] +; CHECK-NEXT: ret double [[R]] +; + %r = call double @erf(double 0x7FF8000000000000) strictfp + ret double %r +} + +declare float @erff(float) willreturn +declare double @erf(double) willreturn |