[llvm-bugs] [Bug 50801] New: missing sign extension when converting int to long long

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jun 22 09:02:33 PDT 2021


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

            Bug ID: 50801
           Summary: missing sign extension when converting int to long
                    long
           Product: new-bugs
           Version: 12.0
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: nico at fluxnic.net
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Given the following code:

int bar(long long x);

int foo(int x)
{
        if (x < 0 || x >= 1024) return -1;
        return bar(x);
}

Invoking clang with -O2 produces this for arm:

foo:
        mov     r1, #0
        cmp     r1, r0, lsr #10
        mvnne   r0, #0
        bxne    lr
        mov     r1, #0
        b       bar

And the following for i386:

foo:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        cmpl    $1023, %eax                     # imm = 0x3FF
        jbe     .LBB0_1
        movl    $-1, %eax
        popl    %ebp
        retl
.LBB0_1:
        pushl   $0
        pushl   %eax
        calll   bar
        addl    $8, %esp
        popl    %ebp
        retl

Notice that in both cases the argument to bar() is not sign extended as it
ought to be.

The same can be observed on aarch64 by replacing "long long" with "__int128":

foo:                                    // @foo
        cmp     w0, #1023                       // =1023
        b.ls    .LBB0_2
        mov     w0, #-1
        ret
.LBB0_2:
        mov     w0, w0
        mov     x1, xzr
        b       bar

And similarly for x86_64.

A slight simplification in the conditional e.g. removing the "x < 0" part is
enough for this issue to disappear.

-- 
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/20210622/3bf4649f/attachment-0001.html>


More information about the llvm-bugs mailing list