[llvm-bugs] [Bug 49445] New: preserve_mostcc on X86 incorrectly restores RAX after storing return value

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Mar 4 19:11:57 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=49445

            Bug ID: 49445
           Summary: preserve_mostcc on X86 incorrectly restores RAX after
                    storing return value
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: jannh at google.com
                CC: craig.topper at gmail.com, juergen at apple.com,
                    llvm-bugs at lists.llvm.org, llvm-dev at redking.me.uk,
                    pengfei.wang at intel.com, spatel+llvm at rotateright.com

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, at 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","", at 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
$ 
```

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210305/64c3fdec/attachment.html>


More information about the llvm-bugs mailing list