[llvm-bugs] [Bug 41907] New: Useless code generation with code that has no side effects

via llvm-bugs llvm-bugs at lists.llvm.org
Thu May 16 06:58:49 PDT 2019


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

            Bug ID: 41907
           Summary: Useless code generation with code that has no side
                    effects
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Loop Optimizer
          Assignee: unassignedbugs at nondot.org
          Reporter: gabravier at gmail.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 21954
  --> https://bugs.llvm.org/attachment.cgi?id=21954&action=edit
Archive containing the file necessary to reproduce this bug and the relevant
generated assembly files

When compiling the attached code with -O3, the optimizer fails to recognize
that the code should be simplified a simple jump to "fn", and instead generates
useless code that needs not be present for the code to work. It also generates
some very weird code, such as 

.LBB0_3:                                # =>This Inner Loop Header: Depth=1
        cmp     ecx, eax
        jle     .LBB0_4
# %bb.9:                                #   in Loop: Header=BB0_3 Depth=1
        cmp     ecx, eax
        jle     .LBB0_10

This strange code generation remains when adding a statement that forces the
code to have a side effect, such as "return d;" at the end of the code.

This is entirely redundant and the second part of the code should be optimized
out.

(%bb.9 being a comment, no code jumps there)

Attached is a 7z archive containing :

- The source file
- The generated assembly file "test.s" when compiling with "clang -S test.c -O3
-masm=intel -m32 -o test32.s" and "clang -S test.c -O3 -masm=intel -o test64.s"
with "x86_64-unknown-linux-gnu" as the target (for which clang was built on my
computer) and with clang 8.0.0, from Fedora 30

PS : I originally found this while trying to analyse
http://www.ioccc.org/1984/laman/laman.c using Godbolt, as seen here :
https://godbolt.org/z/1M_cJi. I was also using trunk, which is why I'm filing
this under trunk ; the bug is reproducible there (note that GCC and ICC
(correctly ?) convert the entire code to a call to fn).

PPS : The weird comparison duplication disappears when removing the duplicate
loop, but the loop itself remains

-- 
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/20190516/4a36a279/attachment.html>


More information about the llvm-bugs mailing list