[llvm-bugs] [Bug 42695] New: Better code for (x & 7) == 6)

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jul 20 03:42:13 PDT 2019


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

            Bug ID: 42695
           Summary: Better code for (x & 7) == 6)
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

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?).


https://godbolt.org/z/XMyGJR

-- 
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/20190720/fc952706/attachment-0001.html>


More information about the llvm-bugs mailing list