[PATCH] D18863: Propagate Undef in llvm.cos Intrinsic

Anna Thomas via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 7 08:18:28 PDT 2016


anna created this revision.
anna added a reviewer: sanjoy.
anna added a subscriber: llvm-commits.
anna changed the visibility of this Differential Revision from "Public (No Login Required)" to "All Users".


the llvm cos intrinsic currently does not propagate undef's. This change transforms cos(undef) to null value or 0.

There are 2 test cases added as well. 

http://reviews.llvm.org/D18863

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/InstCombine/cos_intrinsic.ll

Index: test/Transforms/InstCombine/cos_intrinsic.ll
===================================================================
--- test/Transforms/InstCombine/cos_intrinsic.ll
+++ test/Transforms/InstCombine/cos_intrinsic.ll
@@ -0,0 +1,28 @@
+; 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 i32 @test1() {
+; CHECK-LABEL: define i32 @test1(
+; CHECK-NEXT: ret i32 0 
+  %1 = call double @llvm.cos.f64(double undef) 
+  %2 = fcmp ogt double %1, 0.000000e+00
+  %3 = zext i1 %2 to i32
+  ret i32 %3
+}
+
+
+; 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 
+}
Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1443,6 +1443,11 @@
                                  ArrayRef<Constant *> Operands,
                                  const TargetLibraryInfo *TLI) {
   if (Operands.size() == 1) {
+    if (UndefValue *Op = dyn_cast<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());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18863.52925.patch
Type: text/x-patch
Size: 1854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160407/f46842e9/attachment.bin>


More information about the llvm-commits mailing list