<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 --- - Duplicate loading of double constants"
   href="http://llvm.org/bugs/show_bug.cgi?id=16938">16938</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Duplicate loading of double constants
          </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>Linux
          </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>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>eltoder@gmail.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>I found that in some cases llvm generates duplicate loads of double constants,
e.g.

$ cat t.c
double f(double* p, int n)
{
    double s = 0;
    if (n)
        s += *p;
    return s;
}
$ clang -S -O3 t.c -o -
...
f:                                      # @f
        .cfi_startproc
# BB#0:
        xorps   %xmm0, %xmm0
        testl   %esi, %esi
        je      .LBB0_2
# BB#1:
        xorps   %xmm0, %xmm0
        addsd   (%rdi), %xmm0
.LBB0_2:
        ret
...

Note that there are 2 xorps instructions, the one in BB#1 being clearly
redundant
as it's dominated by the first one. Two xorps come from 2 FsFLD0SD generated by
instruction selection and never eliminated by machine passes. My guess would be
machine CSE should take care of it.

A variation of this case without indirection has the same problem, as well as a
related but separate issue: not commuting addps, resulting in an extra movps:

$ cat t.c
double f(double p, int n)
{
    double s = 0;
    if (n)
        s += p;
    return s;
}
$ clang -S -O3 t.c -o -
...
f:                                      # @f
        .cfi_startproc
# BB#0:
        xorps   %xmm1, %xmm1
        testl   %edi, %edi
        je      .LBB0_2
# BB#1:
        xorps   %xmm1, %xmm1
        addsd   %xmm1, %xmm0
        movaps  %xmm0, %xmm1
.LBB0_2:
        movaps  %xmm1, %xmm0
        ret
...</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>