<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 - Better code for (x & 7) == 6)"
   href="https://bugs.llvm.org/show_bug.cgi?id=42695">42695</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Better code for (x & 7) == 6)
          </td>
        </tr>

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

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

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int f(unsigned i) {
  if ((i & 7) == 6) return 1;
  return i;
}

int g(unsigned i) {
  if (((i - 6) & 7) == 0) return 1;
  return i;
}

Clang -O3 trunk
f(unsigned int):                                  # @f(unsigned int)
        mov     eax, edi
        and     eax, 7
        cmp     eax, 6
        mov     eax, 1
        cmovne  eax, edi
        ret
g(unsigned int):                                  # @g(unsigned int)
        lea     eax, [rdi + 2]
        test    al, 7
        mov     eax, 1
        cmovne  eax, edi
        ret

f should produce same code as g. But more typical version - 'f' - produces
worse code..

define dso_local i32 @_Z1fj(i32) local_unnamed_addr #0 {
  %2 = and i32 %0, 7
  %3 = icmp eq i32 %2, 6
  %4 = select i1 %3, i32 1, i32 %0
  ret i32 %4
}

define dso_local i32 @_Z1gj(i32) local_unnamed_addr #0 {
  %2 = add i32 %0, 2
  %3 = and i32 %2, 7
  %4 = icmp eq i32 %3, 0
  %5 = select i1 %4, i32 1, i32 %0
  ret i32 %5
}

g has more IR intructions, but for X86, it looks better.. Probably missed
canonicalization? But maybe g is worse on other backends, I didn't check it.
(X86 fold only?).


<a href="https://godbolt.org/z/XMyGJR">https://godbolt.org/z/XMyGJR</a></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>