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: unassignedclangb...@nondot.org
          Reporter: br...@clisp.org
                CC: blitzrak...@gmail.com, dgre...@apple.com,
                    erik.pilking...@gmail.com, llvm-bugs@lists.llvm.org,
                    richard-l...@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,@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","",@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to