<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - The preserve_all and preserve_most calling conventions on X86 trash the return value."
   href="https://bugs.llvm.org/show_bug.cgi?id=42394">42394</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>The preserve_all and preserve_most calling conventions on X86 trash the return value.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sunil_srivastava@playstation.sony.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The preserve_all and preserve_most calling convention attributes are meant to
save and restore more than just the normal callee-save set of registers. 

However, at least on x86,  they seem to save and restore the return register
also, thus trashing the return value.

//--------------------
#include <stdio.h>
int __attribute__((preserve_most)) foo(int x) { return x+1; }
int main() { printf("%d %d\n", foo(1), foo(2)); }
//--------------------

$ clang -o p.out p.cpp
$ .\p.out
4195616 4195616

Interestingly, with -O2, the result is correct because foo gets inlined. If the
caller and the callee are in two different files, -O2 shows the same problem.

The assembly code shows clearly what is happening:

_Z3fooi:                                # @_Z3fooi
        pushq   %rdi
        pushq   %rax
        leal    1(%rdi), %eax
        popq    %rax           <--- return value trashed
        popq    %rdi
        retq</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>