<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - InstructionSimplify turns NaN to 0.0"
   href="https://llvm.org/bugs/show_bug.cgi?id=27151">27151</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>InstructionSimplify turns NaN to 0.0
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>davemm@cs.rutgers.edu
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, santosh.nagarakatte@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>LLVM optimizes this code:

define float @foo(float %x) #0 {
entry:
  %0 = fsub nnan ninf float 0.0, %x
  %1 = fadd float %0, %x
  ret float %1
}

to this:

define float @foo(float %x) #0 {
entry:
  ret float 0.000000e+00
}


My reading of the language reference is that the original program will return
NaN when %x is NaN (or +/-infinity). Specifically, the nnan flag on %0 means
that it will return an undefined value when %x is NaN, but it is required to
have defined behavior. That means %1 is calculated as normal, which means it
must return NaN, since %x is NaN.

>From the reference:

---
No NaNs - Allow optimizations to assume the arguments and result are not NaN.
Such optimizations are required to retain defined behavior over NaNs, but the
value of the result is undefined.
---

Counter argument: Since this is described in terms of optimizations instead of
instructions, it could be argued that the nnan in %0 is sufficient to allow the
optimization, even though %1 does not have nnan. This would mean adding nnan to
%0 affects every instruction that uses %x.


Here is the relevant code from InstructionSimplify.cpp:

  // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0
  //   where nnan and ninf have to occur at least once somewhere in this
  //   expression
  Value *SubOp = nullptr;
  if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0))))
    SubOp = Op1;
  else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1))))
    SubOp = Op0;
  if (SubOp) {
    Instruction *FSub = cast<Instruction>(SubOp);
    if ((FMF.noNaNs() || FSub->hasNoNaNs()) &&
        (FMF.noInfs() || FSub->hasNoInfs()))
      return Constant::getNullValue(Op0->getType());
  }</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>