<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 - llvm.bitreverse.i64 returns wrong value"
   href="https://bugs.llvm.org/show_bug.cgi?id=36253">36253</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>llvm.bitreverse.i64 returns wrong value
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>5.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Backend: PowerPC
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>kai@redstar.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19818" name="attach_19818" title="LLVM IR demonstring the problem.">attachment 19818</a> <a href="attachment.cgi?id=19818&action=edit" title="LLVM IR demonstring the problem.">[details]</a></span>
LLVM IR demonstring the problem.

The following code produces an unexpected output. It is a reduced version of
the following D code:

import ldc.intrinsics;
import core.stdc.stdio : printf;

int main()
{
    auto exp =  1UL << 63;
    printf("%lx\n", exp);
    auto arg = 1UL << 0;
    printf("%lx\n", arg);
    auto val = llvm_bitreverse(arg);
    printf("%lx\n", val);
    return exp != val;
}

LLVM IR:

source_filename = "bitop.d"
target datalayout = "e-m:e-i64:64-n32:64"
target triple = "powerpc64le-unknown-linux-gnu"

@.str = private unnamed_addr constant [5 x i8] c"%lx\0A\00"

declare signext i32 @printf(i8*, ...)

declare i64 @llvm.bitreverse.i64(i64)

define signext i32 @main(i32 signext %argc_arg, i8** %argv_arg) {
  %exp = alloca i64, align 8
  %arg = alloca i64, align 8
  %val = alloca i64, align 8
  store i64 -9223372036854775808, i64* %exp
  %1 = load i64, i64* %exp
  %2 = call signext i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x
i8], [5 x i8]* @.str, i32 0, i32 0), i64 %1)
  store i64 1, i64* %arg
  %3 = load i64, i64* %arg
  %4 = call signext i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x
i8], [5 x i8]* @.str, i32 0, i32 0), i64 %3)
  %5 = load i64, i64* %arg
  %6 = call i64 @llvm.bitreverse.i64(i64 %5)
  store i64 %6, i64* %val
  %7 = load i64, i64* %val
  %8 = call signext i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x
i8], [5 x i8]* @.str, i32 0, i32 0), i64 %7)
  %9 = load i64, i64* %exp
  %10 = load i64, i64* %val
  %11 = icmp ne i64 %9, %10
  %12 = zext i1 %11 to i32
  ret i32 %12
}

Compile and run:
llc -filetype=obj bitop.ll
gcc bitop.o -o bitop
./bitop
echo "Ret: $?"

Result:
8000000000000000
1
80000000
Ret: 1

If you change the line
    %6 = call i64 @llvm.bitreverse.i64(i64 %5)
to
    %6 = call i64 @llvm.bitreverse.i64(i64 %3)
or to
    %6 = call i64 @llvm.bitreverse.i64(i64 1)
then the expected output 

8000000000000000
1
8000000000000000
Ret: 0

is produced.

LLVM used:
ubuntu@ldc:/tmp/unittest$ /home/ubuntu/build-llvm/bin/llc --version
LLVM (<a href="http://llvm.org/">http://llvm.org/</a>):
  LLVM version 5.0.1git-6af15925d78
  DEBUG build with assertions.
  Default target: powerpc64le-unknown-linux-gnu
  Host CPU: pwr8

  Registered Targets:
    nvptx   - NVIDIA PTX 32-bit
    nvptx64 - NVIDIA PTX 64-bit
    ppc32   - PowerPC 32
    ppc64   - PowerPC 64
    ppc64le - PowerPC 64 LE

Environment:
ubuntu@ldc:/tmp/unittest$ uname -a
Linux ldc 4.13.0-32-generic #35-Ubuntu SMP Thu Jan 25 09:05:20 UTC 2018 ppc64le
ppc64le ppc64le GNU/Linux</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>