<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 - missing sign extension when converting int to long long"
   href="https://bugs.llvm.org/show_bug.cgi?id=50801">50801</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>missing sign extension when converting int to long long
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>12.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>nico@fluxnic.net
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</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>