[llvm-bugs] [Bug 46055] New: Invalid optimization: two different array indices are considered equal

via llvm-bugs llvm-bugs at lists.llvm.org
Sun May 24 00:53:55 PDT 2020


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

            Bug ID: 46055
           Summary: Invalid optimization: two different array indices are
                    considered equal
           Product: clang
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bruno at clisp.org
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Created attachment 23527
  --> https://bugs.llvm.org/attachment.cgi?id=23527&action=edit
Test case

The attached program, foo.c, ought to exit with code 3 if calloc() fails, and
with code 2 if calloc() succeeds.

Without optimization, it's as expected:
$ clang -Wall foo.c
$ ./a.out; echo $?
3

With optimization, it's wrong:
$ clang -Wall -O2 foo.c
$ ./a.out; echo $?
0

Here's the output of the clang optimizer:
$ clang -Wall -O2 -S foo.c && cat foo.s
        .text
        .file   "foo.c"
        .globl  main                    # -- Begin function main
        .p2align        4, 0x90
        .type   main, at function
main:                                   # @main
        .cfi_startproc
# %bb.0:
        xorl    %eax, %eax
        retq
.Lfunc_end0:
        .size   main, .Lfunc_end0-main
        .cfi_endproc
                                        # -- End function
        .ident  "clang version 10.0.0 "
        .section        ".note.GNU-stack","", at progbits
        .addrsig

As you can see, clang must have evaluated the condition (s[n - 1].c[0]) to
true. But since the memory of s was freshly allocated and zero-filled and the
index n-1 is different from 0, this condition ought to have evaluated to false.

Probably the bug is related to the fact that (n-1) * sizeof (S8) is a multiple
of 2^64.

If clang is assuming a flat address space (of size 2^64), it may indeed
simplify (n-1) * sizeof (S8) to zero, but then it must not assume that calloc()
will return a non-NULL pointer.

If clang is NOT assuming a flat address space, it must not simplify (n-1) *
sizeof (S8) to zero.

-- 
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/20200524/eb693b17/attachment-0001.html>


More information about the llvm-bugs mailing list