[llvm-bugs] [Bug 27151] New: InstructionSimplify turns NaN to 0.0

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Mar 30 19:48:41 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=27151

            Bug ID: 27151
           Summary: InstructionSimplify turns NaN to 0.0
           Product: new-bugs
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: davemm at cs.rutgers.edu
                CC: llvm-bugs at lists.llvm.org,
                    santosh.nagarakatte at gmail.com
    Classification: Unclassified

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());
  }

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160331/0738e4f1/attachment-0001.html>


More information about the llvm-bugs mailing list