<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 - In O0 optimization, -nan cannot be calculated properly."
   href="https://bugs.llvm.org/show_bug.cgi?id=49304">49304</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>In O0 optimization, -nan cannot be calculated properly.
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>C++11
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>2077213809@qq.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Simple test cases

#include <iostream>
#include <stdio.h>
#include <limits>

int main()
{
  double a = std::numeric_limits<double>::quiet_NaN();
  double b = -a;
  std::cout << b << std::endl;
  return 0;
}

clang++ -O0 nan_bug.cpp
./a.out
nan

clang++ -O2 nan_bug.cpp
./a.out
-nan

when we use -O0 optimize, The negative operation does not seem to take effect.
The operation becomes sub 0.
the IR is
 47 define dso_local i32 @main() #4 {
 48 entry:
      ...
 53   %call = call double @_ZNSt14numeric_limitsIdE9quiet_NaNEv() #3
 54   store double %call, double* %a, align 8
 55   %0 = load double, double* %a, align 8
 56   %sub = fsub double -0.000000e+00, %0 // Desired Negative Operation
 57   store double %sub, double* %b, align 8
 58   %1 = load double, double* %b, align 8
     ...

when we use -O2, the -nan is directly calculated, so there's no problem.
%call.i6 = tail call dereferenceable(272) %"class.std::basic_ostream"*
@_ZNSo9_M_insertIdEERSoT_(%"class.std::basic_ostream"* nonnull @_ZSt4cout,
double 0xFFF8000000000000)</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>