[LLVMbugs] [Bug 12207] New: x86-64: sign extension of parameters and returns values should match GCC

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Mar 7 16:25:55 PST 2012


http://llvm.org/bugs/show_bug.cgi?id=12207

             Bug #: 12207
           Summary: x86-64: sign extension of parameters and returns
                    values should match GCC
           Product: new-bugs
           Version: trunk
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: meadori at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified


Created attachment 8153
  --> http://llvm.org/bugs/attachment.cgi?id=8153
Reproduction source.

I recently noticed a difference between the way GCC and LLVM treat
sign extension for parameters and return values on x86-64.  I could
not find a clear answer in the x86-64 ABI [1] concerning whether
parameters should be sign extended by the caller or callee and
similarly whether return values should be sign extended by the caller
or callee.

Consider a simple 'signed char' division routine:

signed char sdiv(signed char x, signed char y)
{
 return x / y;
}

On my Fedora 16 Linux host GCC 4.6.2 produces (-O3):

sdiv:
.LFB11:
       .cfi_startproc
       movsbl  %dil, %eax
       movsbl  %sil, %esi
       movl    %eax, %edx
       sarl    $31, %edx
       idivl   %esi
       ret

where as clang 2.9 produces (-O3):

sdiv:                                   # @sdiv
.Leh_func_begin0:
# BB#0:
       movl    %edi, %eax
       cltd
       idivl   %esi
       movsbl  %al, %eax
       ret

I also built a clang/llvm toolchain on OS X from trunk which produces similar
code:

$ ./Debug+Asserts/bin/clang --versionclang version 3.1 (trunk 152229)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
$ ./Debug+Asserts/bin/clang -S -O3 ~/Code/llvm/bugs/sdiv.c
$ cat sdiv.s
    .section    __TEXT,__text,regular,pure_instructions
    .globl    _sdiv
    .align    4, 0x90
_sdiv:                                  ## @sdiv
    .cfi_startproc
## BB#0:                                ## %entry
    pushq    %rbp
Ltmp2:
    .cfi_def_cfa_offset 16
Ltmp3:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp4:
    .cfi_def_cfa_register %rbp
    movl    %edi, %eax
    cltd
    idivl    %esi
    movsbl    %al, %eax
    popq    %rbp
    ret
    .cfi_endproc


.subsections_via_symbols

GCC assumes the parameters have *not* been sign extended and assumes
it is the receivers job to extend the return value.  LLVM assumes the
parameters *have* been sign extended and that the receiver expects a
sign extended result.

It would be nice if LLVM followed the GCC.  This would, of course, increase ABI
compatibility between the tools and lessen the burden on things like libffi to
support ABI variants.

[1] http://www.x86-64.org/documentation/abi.pdf

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list