<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 - [x86] Redundant XORs generated withwith BSR from "__builtin_clz"."
   href="https://bugs.llvm.org/show_bug.cgi?id=47603">47603</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86] Redundant XORs generated withwith BSR from "__builtin_clz".
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>ToHe_EMA@gmx.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following simple C function to be compiled with Clang:

char Func(unsigned DataOffset)
{
        return 31 ^ __builtin_clz(DataOffset);
}

Unfortunately it compiles to this assembly:

bsr     eax, edi
xor     eax, 31
xor     al, 31
ret

The relevant LLVM IR is as follows:

declare i32 @llvm.ctlz.i32(i32, i1 immarg) #1
define signext i8 @Func(i32 %0) {
  %2 = tail call i32 @llvm.ctlz.i32(i32 %0, i1 true)
  %3 = trunc i32 %2 to i8
  %4 = xor i8 %3, 31
  ret i8 %4
}

Obviously the two XORs should be entirely eliminated in the generated machine
code. This problem only occurs if the return type is a 8 bit integer (char).
For 16 bit, 32 bit and 64 bit integers the xors are elided correctly. Another
variant that leads to the same issue (without an 8 bit integer) is this:

extern char Lookup[32];
int LookupFunc(unsigned DataOffset)
{
        return Lookup[31 ^ __builtin_clz(DataOffset)];
}

With results in the following relevant LLVM IR code:

@Lookup = external dso_local local_unnamed_addr global [32 x i8], align 16
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #1
define i32 @LookupFuncj(i32 %0) {
  %2 = tail call i32 @llvm.ctlz.i32(i32 %0, i1 true)
  %3 = xor i32 %2, 31
  %4 = zext i32 %3 to i64
  %5 = getelementptr inbounds [32 x i8], [32 x i8]* @Lookup, i64 0, i64 %4
  %6 = load i8, i8* %5, align 1
  %7 = sext i8 %6 to i32
  ret i32 %7
}</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>