[llvm-bugs] [Bug 34063] New: division 1/a may be optimized better

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Aug 4 03:26:56 PDT 2017


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

            Bug ID: 34063
           Summary: division 1/a may be optimized better
           Product: libraries
           Version: 3.9
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: konstantin.vladimirov at gmail.com
                CC: llvm-bugs at lists.llvm.org

Really it is platform-independent optimization bug. Lets look it on x86:

int a = 1;

int
main ()
{
  int f = 1 / a;
  return (f == 1) ? 0 : 1;
}

compile with:

clang -v
clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix

command line:

clang -S -O2 div-bug.c

You shall see:

main:                                   # @main
        .cfi_startproc
# BB#0:                                 # %entry
        movl    a(%rip), %eax
        leal    1(%rax), %ecx
        cmpl    $3, %ecx
        sbbb    %cl, %cl
        cmpl    $1, %eax
        sete    %al
        andb    %cl, %al
        movzbl  %al, %eax
        xorl    $1, %eax
        retq

Comparison with 3 in assembler definitely unncessary.

Real problem is in platform independent instruction combiner. It transforms:

  %0 = load i32, i32* @a, align 4, !tbaa !1
  %div = sdiv i32 1, %0
  %cmp = icmp eq i32 %div, 1

to

  %0 = load i32, i32* @a, align 4, !tbaa !1
  %1 = add i32 %0, 1
  %2 = icmp ult i32 %1, 3
  %cmp2 = icmp eq i32 %0, 1
  %cmp = and i1 %2, %cmp2

Of course check if %1 less then 3 is excessive for this case.

-- 
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/20170804/7a5fca81/attachment.html>


More information about the llvm-bugs mailing list