<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - missed opportunities to use lower precision cmath functions"
   href="http://llvm.org/bugs/show_bug.cgi?id=17850">17850</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>missed opportunities to use lower precision cmath functions
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Transformation Utilities
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kkhoo@perfwizard.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>LLVM isn't optimizing math.h/cmath library calls based on precision of the
inputs and outputs:

$ cat ~/Desktop/cos.c
#include <math.h>
float foo(float x) { return cos(x); }

$ ./clang -O3 -S -o - ~/Desktop/cos.c
...
    cvtss2sd    %xmm0, %xmm0
    callq    _cos
    cvtsd2ss    %xmm0, %xmm0
    popq    %rbp
    ret
...

GCC gets rid of the precision conversions and makes this a call to _cosf.

Based on the code in LibCallSimplifierImpl::lookupOptimization(), it appears
that LLVM should be making this optimization assuming unsafe math, but there's
no difference if I use -ffast-math.

So there are 2 potential bugs here:
1. Why is LLVM failing to optimize this code with fast-math?
2. Why doesn't LLVM do this optimization regardless of fast-math (unsafe FP)?


This is with:
$ ./clang -v
clang version 3.4 (trunk 194153)
Target: x86_64-apple-darwin11.4.2
Thread model: posix


And here's the IR:
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.7.0"

; Function Attrs: nounwind readnone ssp uwtable
define float @foo(float %x) #0 {
entry:
  %conv = fpext float %x to double
  %call = tail call double @cos(double %conv) #2
  %conv1 = fptrunc double %call to float
  ret float %conv1
}

; Function Attrs: nounwind readnone
declare double @cos(double) #1

attributes #0 = { nounwind readnone ssp uwtable "less-precise-fpmad"="false"
"no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"
"no-infs-fp-math"="false" "no-nans-fp-math"="false"
"stack-protector-buffer-size"="8" "unsafe-fp-math"="false"
"use-soft-float"="false" }
attributes #1 = { nounwind readnone "less-precise-fpmad"="false"
"no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"
"no-infs-fp-math"="false" "no-nans-fp-math"="false"
"stack-protector-buffer-size"="8" "unsafe-fp-math"="false"
"use-soft-float"="false" }
attributes #2 = { nounwind readnone }

!llvm.ident = !{!0}

!0 = metadata !{metadata !"clang version 3.4 (trunk 194153)"}</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>