<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 with `fmax` and `-inf`"
   href="https://bugs.llvm.org/show_bug.cgi?id=37776">37776</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Invalid optimization with `fmax` and `-inf`
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>normal
          </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>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20420" name="attach_20420" title="Preprocessed source file">attachment 20420</a> <a href="attachment.cgi?id=20420&action=edit" title="Preprocessed source file">[details]</a></span>
Preprocessed source file

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

    #include <stdio.h>
    #include <stdbool.h>
    #include <math.h>

    bool example(double dub)
    {
      return fmax(((double)NAN), dub) < -1.0;
    }

    int main(void)
    {
      const bool result = example(-HUGE_VAL);
      printf("example(-inf) = %s\n", result ? "true" : "false");
      return result ? 0 : 1;
    }

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

    example(-inf) = false

and returns 1.  If I don't enable optimizations, running it prints out

    example(-inf) = true

and returns 0.  This is the result I expect, since fmax(NAN, x) is defined to
be x, and in this case x = -HUGE_VAL (negative infinity) is less than -1.0.  

If I inline the function example() into main() or I replace the call to fmax()
with a call to fmin(), the program behaves correctly whether or not
optimizations are enabled during compilation.  If I invert signs by either
changing NAN to (-NAN) or changing -HUGE_VAL to HUGE_VAL, the program always
behaves correctly, printing "true" in the former case and "false" in the latter
case, since HUGE_VAL < -1.0 is false.  The order of arguments to fmax() does
not affect the result of the program.

The attached file test.i is the preprocessed source file.  The test program
compiles without errors or warnings and never triggers the undefined-behavior
sanitizer.  I observe the same behavior with clang version 6.0.0-3+b1
(tags/RELEASE_600/final) but not with clang version 5.0.2-2
(tags/RELEASE_502/final).

The compiler invocation and complete output follows:

clang-7 -O -v -save-temps -Wall -Wextra -Werror -fsanitize=undefined
-fsanitize=address -fno-strict-aliasing -fwrapv -lm test.c -o test
clang version 7.0.0- (trunk)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
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.4.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.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
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.4.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.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
 "/usr/lib/llvm-7/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.c -mrelocation-model static -mthread-model posix
-relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases
-munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info
-debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir
/usr/lib/llvm-7/lib/clang/7.0.0 -internal-isystem /usr/local/include
-internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include
-internal-externc-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/8/include
-internal-externc-isystem /usr/include/x86_64-linux-gnu
-internal-externc-isystem /include -internal-externc-isystem /usr/include -O2
-Wall -Wextra -Werror -fdebug-compilation-dir /home/peddie/clang-bug
-ferror-limit 19 -fmessage-length 106
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,object-size,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,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-blacklist=/usr/lib/llvm-7/lib/clang/7.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new -fwrapv
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-vectorize-loops -vectorize-slp -o test.i -x c test.c
clang -cc1 version 7.0.0 based upon LLVM 7.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-7/lib/clang/7.0.0/include
 /usr/bin/../lib/gcc/x86_64-linux-gnu/8/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/lib/llvm-7/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.c -mrelocation-model static
-mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose
-mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64
-dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v
-resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -O2 -Wall -Wextra -Werror
-fdebug-compilation-dir /home/peddie/clang-bug -ferror-limit 19
-fmessage-length 106
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,object-size,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,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-blacklist=/usr/lib/llvm-7/lib/clang/7.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new -fwrapv
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-vectorize-loops -vectorize-slp -disable-llvm-passes -o test.bc -x cpp-output
test.i
clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target
x86_64-pc-linux-gnu
#include "..." search starts here:
End of search list.
 "/usr/lib/llvm-7/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.c -mrelocation-model static -mthread-model posix
-relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases
-munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info
-debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir
/usr/lib/llvm-7/lib/clang/7.0.0 -O2 -Wall -Wextra -Werror
-fdebug-compilation-dir /home/peddie/clang-bug -ferror-limit 19
-fmessage-length 106
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,object-size,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,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-blacklist=/usr/lib/llvm-7/lib/clang/7.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new -fwrapv
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-vectorize-loops -vectorize-slp -o test.s -x ir test.bc
clang -cc1 version 7.0.0 based upon LLVM 7.0.0 default target
x86_64-pc-linux-gnu
 "/usr/lib/llvm-7/bin/clang" -cc1as -triple x86_64-pc-linux-gnu -filetype obj
-main-file-name test.c -target-cpu x86-64 -dwarf-version=4 -mrelocation-model
static -o test.o test.s
 "/usr/bin/ld" --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/8/../../../x86_64-linux-gnu/crt1.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtbegin.o
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/8
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../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/8/../../.. -L/usr/lib/llvm-7/bin/../lib
-L/lib -L/usr/lib --whole-archive
/usr/lib/llvm-7/lib/clang/7.0.0/lib/linux/libclang_rt.asan-x86_64.a
--no-whole-archive
--dynamic-list=/usr/lib/llvm-7/lib/clang/7.0.0/lib/linux/libclang_rt.asan-x86_64.a.syms
-lm test.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/8/crtend.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../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>