<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 --- - InstCombine: frem X, (C ? 0 : Y) -> frem X, Y changes result"
   href="https://llvm.org/bugs/show_bug.cgi?id=26943">26943</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>InstCombine: frem X, (C ? 0 : Y) -> frem X, Y changes result
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>andres.noetzli@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following program:

$ cat ../example.ll
define i1 @main() {
  %w = call float @foo(i1 1, float 17.0)
  %w2 = frem float 17.0, 42.0
  %rr = fcmp oeq float %w, %w2
  ret i1 %rr
}

define float @foo(i1 %c, float %x) {
  %r = select i1 %c, float 0.0, float 42.0
  %rr = frem float %x, %r
  ret float %rr
}

gets optimized to:

$ bin/opt < ../example.ll -instcombine -S > ../example2.ll && cat
../example2.ll
; ModuleID = '<stdin>'

define i1 @main() {
  %w = call float @foo(i1 true, float 1.700000e+01)
  %rr = fcmp oeq float %w, 1.700000e+01
  ret i1 %rr
}

define float @foo(i1 %c, float %x) {
  %rr = frem float %x, 4.200000e+01
  ret float %rr
}

The output of the original program and the optimized program is not the same:

$ bin/lli ../example.ll; echo $?
0
$ bin/lli ../example2.ll; echo $?
1

It seems like the optimization assumes that a frem %x, 0.0 results in undefined
behavior but according to [0], the result should just be NaN and an error flag
should be set. My understanding is that frem corresponds to C's fmod (see e.g.
[1]), so it seems like the optimization is invalid.

[0] <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmod.html">http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmod.html</a>
[1]
<a href="http://llvm.org/docs/doxygen/html/classllvm_1_1APFloat.html#ab715d4ba706dd8dd1c7d51e43d067d4c">http://llvm.org/docs/doxygen/html/classllvm_1_1APFloat.html#ab715d4ba706dd8dd1c7d51e43d067d4c</a></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>