diff options
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 5 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/cos-intrinsic.ll | 26 |
2 files changed, 31 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 017d897def7..d87a0800262 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1443,6 +1443,11 @@ Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty, ArrayRef<Constant *> Operands, const TargetLibraryInfo *TLI) { if (Operands.size() == 1) { + if (isa<UndefValue>(Operands[0])) { + // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN + if (IntrinsicID == Intrinsic::cos) + return Constant::getNullValue(Ty); + } if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { if (IntrinsicID == Intrinsic::convert_to_fp16) { APFloat Val(Op->getValueAPF()); diff --git a/llvm/test/Transforms/InstCombine/cos-intrinsic.ll b/llvm/test/Transforms/InstCombine/cos-intrinsic.ll new file mode 100644 index 00000000000..b4d07cf8047 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/cos-intrinsic.ll @@ -0,0 +1,26 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s +; This test makes sure that the undef is propagated for the cos instrinsic + +declare double @llvm.cos.f64(double %Val) +declare float @llvm.cos.f32(float %Val) + +; Function Attrs: nounwind readnone +define double @test1() { +; CHECK-LABEL: define double @test1( +; CHECK-NEXT: ret double 0.000000e+00 + %1 = call double @llvm.cos.f64(double undef) + ret double %1 +} + + +; Function Attrs: nounwind readnone +define float @test2(float %d) { +; CHECK-LABEL: define float @test2( +; CHECK-NEXT: %cosval = call float @llvm.cos.f32(float %d) + %cosval = call float @llvm.cos.f32(float %d) + %cosval2 = call float @llvm.cos.f32(float undef) + %fsum = fadd float %cosval2, %cosval + ret float %fsum +; CHECK-NEXT: %fsum +; CHECK: ret float %fsum +} |

