[llvm-bugs] [Bug 35271] New: Missed optimization opportunity for constant folding

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Nov 9 21:14:04 PST 2017


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

            Bug ID: 35271
           Summary: Missed optimization opportunity for constant folding
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: ishiura-compiler at ml.kwansei.ac.jp
                CC: llvm-bugs at lists.llvm.org

We compiled two programs (A1.c, A2.c) by LLVM/clang-6.0 with -O3 option.
Though the two programs are equivalent, LLVM/clang outputs different assembly
codes.


+---------------------------------+---------------------------------+
|                A1.c             |              A2.c               |
+---------------------------------+---------------------------------+
|int main (void)                  |int main (void)                  |
|{                                |{                                |
|  volatile int x = 1;            |  volatile int x = 1;            |
|  int t = 1/(1%(2/x));           |  int t = 1;                     |
|                                 |                                 |
|  if (t != 1) __builtin_abort(); |  if (t != 1) __builtin_abort(); |
|  return 0;                      |  return 0;                      |
|}                                |}                                |
+---------------------------------+---------------------------------+

+-------------------------------------+-------------------------------------+
|    A1.s (clang-6.0 A1.c -O3 -S)     |    A2.s (clang-6.0 A2.c -O3 -S)     |
+-------------------------------------+-------------------------------------+
|main:    # @main                     |main:    # @main                     |
|    .cfi_startproc                   |    .cfi_startproc                   |
|# BB#0:                              |# BB#0:                              |
|    pushq   %rax                     |                                     |
|    .cfi_def_cfa_offset 16           |                                     |
|    movl    $1, 4(%rsp)              |     movl    $1, -4(%rsp)            |
|    movl    $2, %eax                 |                                     |
|    xorl    %edx, %edx               |                                     |
|    idivl   4(%rsp)                  |                                     |
|    movl    %eax, %ecx               |                                     |
|    movl    $1, %eax                 |                                     |
|    xorl    %edx, %edx               |                                     |
|    idivl   %ecx                     |                                     |
|    cmpl    $1, %edx                 |                                     |
|    jne     .LBB0_2                  |                                     |
|# BB#1:                              |                                     |
|    xorl    %eax, %eax               |    xorl    %eax, %eax               |
|    popq    %rcx                     |                                     |
|    retq                             |    retq                             |
|.LBB0_2:                             |                                     |
|    callq   abort                    |                                     |
|.Lfunc_end0:                         |.Lfunc_end0:                         |
|    .size   main, .Lfunc_end0-main   |    .size   main, .Lfunc_end0-main   |
+-------------------------------------+-------------------------------------+


FYI, GCC generates almost the same assembly codes for A1.c and A2.c.
+-------------------------------------+-------------------------------------+
|    A1.s (gcc-8.0.0 A1.c -O3 -S)     |    A2.s (gcc-8.0.0 A2.c -O3 -S)     |
+-------------------------------------+-------------------------------------+
|main:                                |main:                                |
|                                     |                                     |
|.LFB0:                               |.LFB0:                               |
|.cfi_startproc                       |.cfi_startproc                       |
|movl    $1, -4(%rsp)                 |movl    $1, -4(%rsp)                 |
|movl    -4(%rsp), %eax               |                                     |
|xorl    %eax, %eax                   |xorl    %eax, %eax                   |
|ret                                  |ret                                  |
|.cfi_endproc                         |.cfi_endproc                         |
|.LFE0:                               |.LFE0:                               |
|.size   main, .-main                 |.size   main, .-main                 |
+-------------------------------------+-------------------------------------+

clang version 6.0.0 (trunk 317454)
Target: x86_64-unknown-linux-gnu
Thread model: posix

gcc-8.0 (GCC) 8.0.0 20171109 (experimental)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-- 
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/20171110/448b1be8/attachment.html>


More information about the llvm-bugs mailing list