<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 - preserve_mostcc on X86 incorrectly restores RAX after storing return value"
   href="https://bugs.llvm.org/show_bug.cgi?id=49445">49445</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>preserve_mostcc on X86 incorrectly restores RAX after storing return value
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jannh@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, juergen@apple.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, pengfei.wang@intel.com, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When a function with preserve_mostcc calling convention returns a value through
RAX, the epilogue incorrectly restores RAX after the return value has been
written into it.


Minimal testcase:
```
target triple = "x86_64-unknown-linux-gnu"
define preserve_mostcc i32 @foobar() {
  ret i32 1
}
```

Resulting assembly from `llc -o=- test2.ll`:
```
        .text
        .file   "test2.ll"
        .globl  foobar                          # -- Begin function foobar
        .p2align        4, 0x90
        .type   foobar,@function
foobar:                                 # @foobar
        .cfi_startproc
# %bb.0:
        pushq   %rax
        .cfi_def_cfa_offset 16
        .cfi_offset %rax, -16
        movl    $1, %eax
        popq    %rax
        .cfi_def_cfa_offset 8
        retq
.Lfunc_end0:
        .size   foobar, .Lfunc_end0-foobar
        .cfi_endproc
                                        # -- End function
        .section        ".note.GNU-stack","",@progbits

```

I think this essentially means that `__attribute__((preserve_most))` is broken
for any function that returns a value on X86?


Simple test with C code:

```
$ cat demo.c
#include <stdio.h>
__attribute__((preserve_most))
__attribute__((noinline))
unsigned int get_number(void) {
  return 0x41414141;
}
int main(void) {
  printf("get_number() = 0x%x\n", get_number());
  return 0;
}
$ /h/git/foreign/llvmp-build-debug/bin/clang-12 -o demo demo.c -Wall
$ ./demo
get_number() = 0x401140

```</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>