<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Invalid optimization around abs() and integer comparison"
   href="https://bugs.llvm.org/show_bug.cgi?id=46449">46449</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Invalid optimization around abs() and integer comparison
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>mpeddie@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23648" name="attach_23648" title="Preprocessed source for the example program">attachment 23648</a> <a href="attachment.cgi?id=23648&action=edit" title="Preprocessed source for the example program">[details]</a></span>
Preprocessed source for the example program

My simple test program gives different results depending on whether or not I
enable optimizations with -O.  The program is as follows (preprocessed source
attached):

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <inttypes.h>
    #include <stdbool.h>

    int main(int argc __attribute__((unused)), char **argv
__attribute__((unused))) {
      const int32_t input[] = {INT32_MIN};

      const int32_t a = input[0];
      const int32_t absa = abs(a);
      printf("abs(a): %"PRIi32"\n", absa);
      const bool result = 0 > absa;

      printf("0 > %" PRIi32 ": %s\n", absa, result ? "true" : "false");
      printf("0 > %" PRIi32 ": %s\n", INT32_MIN, 0 > INT32_MIN ? "true" :
"false");

      return 0;
    }

If I enable optimizations when compiling and run this program, it prints out

    abs(a): -2147483648
    0 > -2147483648: false
    0 > -2147483648: true

If I compile without optimizations, it prints out

    abs(a): -2147483648
    0 > -2147483648: true
    0 > -2147483648: true

I expect both comparisons to be equal to true.  While the result of the call to
`abs()` is undefined for this input value and may be anything, I've stored the
result (whatever it may be) into a variable, and I expect the value there
(which I see is negative when I print it) to compare less than zero independent
of how it was calculated.

Passing `-fno-builtin-abs` along with `-O` results in both comparisons being
true again as expected.  Replacing the array access to define `a` with
`INT32_MIN` directly results in both comparisons being true again as expected.

The compiler invocation and complete output follows.

clang-10 -v -save-temps -std=c11 -O -o test -fwrapv -Wall -Wextra -Werror
-fsanitize=undefined -fsanitize=address test-on-concrete-abs-result.c
clang version 10.0.0-4 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
 "/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -E
-save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names
-main-file-name test-on-concrete-abs-result.c -mrelocation-model static
-mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math
-masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64
-dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v
-resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -internal-isystem
/usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include
-internal-externc-isystem /usr/include/x86_64-linux-gnu
-internal-externc-isystem /include -internal-externc-isystem /usr/include -Wall
-Wextra -Werror -std=c11 -fdebug-compilation-dir /home/peddie/butfor
-ferror-limit 19 -fmessage-length 0
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,vptr
-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-system-blacklist=/usr/lib/llvm-10/lib/clang/10.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new -fwrapv
-fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option
-fcolor-diagnostics -faddrsig -o test-on-concrete-abs-result.i -x c
test-on-concrete-abs-result.c
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target
x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/llvm-10/lib/clang/10.0.0/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-llvm-bc
-emit-llvm-uselists -save-temps=cwd -disable-free -disable-llvm-verifier
-discard-value-names -main-file-name test-on-concrete-abs-result.c
-mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno
-fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables
-target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining
-debugger-tuning=gdb -v -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -Wall
-Wextra -Werror -std=c11 -fdebug-compilation-dir /home/peddie/butfor
-ferror-limit 19 -fmessage-length 0
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,vptr
-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-system-blacklist=/usr/lib/llvm-10/lib/clang/10.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new -fwrapv
-fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option
-fcolor-diagnostics -disable-llvm-passes -faddrsig -o
test-on-concrete-abs-result.bc -x cpp-output test-on-concrete-abs-result.i
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target
x86_64-pc-linux-gnu
#include "..." search starts here:
End of search list.
 "/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -S
-save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names
-main-file-name test-on-concrete-abs-result.c -mrelocation-model static
-mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math
-masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64
-dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v
-resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -Wall -Wextra -Werror -std=c11
-fdebug-compilation-dir /home/peddie/butfor -ferror-limit 19 -fmessage-length 0
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,vptr
-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-system-blacklist=/usr/lib/llvm-10/lib/clang/10.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new -fwrapv
-fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option
-fcolor-diagnostics -faddrsig -o test-on-concrete-abs-result.s -x ir
test-on-concrete-abs-result.bc
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target
x86_64-pc-linux-gnu
 "/usr/lib/llvm-10/bin/clang" -cc1as -triple x86_64-pc-linux-gnu -filetype obj
-main-file-name test-on-concrete-abs-result.c -target-cpu x86-64
-dwarf-version=4 -mrelocation-model static -mrelax-all -o
test-on-concrete-abs-result.o test-on-concrete-abs-result.s
 "/usr/bin/ld" --hash-style=both --build-id --eh-frame-hdr -m elf_x86_64
-dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crt1.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crti.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/crtbegin.o
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/10
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu
-L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../..
-L/usr/lib/llvm-10/bin/../lib -L/lib -L/usr/lib --whole-archive
/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan-x86_64.a
--no-whole-archive
--dynamic-list=/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan-x86_64.a.syms
test-on-concrete-abs-result.o --no-as-needed -lpthread -lrt -lm -ldl -lgcc
--as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/crtend.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crtn.o</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>