<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 - FP exception on optimized code"
   href="https://bugs.llvm.org/show_bug.cgi?id=40280">40280</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>FP exception on optimized code
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>6.0
          </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>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>graeser@mi.fu-berlin.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21310" name="attach_21310" title="Minimal example">attachment 21310</a> <a href="attachment.cgi?id=21310&action=edit" title="Minimal example">[details]</a></span>
Minimal example

The following code snippet should never divide by zero. However, if compiled
with '-O3' it leads to a floating point exception when called using a certain
pattern. 
See attachment for a minimal example. 

  for (int i=0; i<=3; i++)can be expected. It seems that clang 

    for (int j=0; j<=3; j++)
      for (int k=0; k<=3; k++)
        if ((m!=i) && (m!=j) && (m!=k))
          z += x*(1.0/(m-k))*(1.0/(m-j))*(1.0/(m-i));

If the trapping of FP errors is not enabled, the correct result is computed. So
it seems that clang generates operations for the ruled out cases. Notice that
the order of the factors in the arithmetic expression as well as the way the
function containing this code is called does matter. 

It seems that clang implicitly makes the assumption that user code does not
track FP errors on '-O3'. Gcc only makes this assumption if we explicitly allow
this using '-O3  -fno-trapping-math' where the same behaviour is observed (but
expected).

The problem shows up on a 64bit ubuntu 18.04 with clang-5.0.1 and clang-6.0.0
but not with clang-4.0.0.
<a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - likely wrong code bug: floating point exception executing code compiled at -O3"
   href="show_bug.cgi?id=11356">Bug 11356</a> and <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - FP exception when optimized; code relocation implicated"
   href="show_bug.cgi?id=26969">bug 26969</a> look related but are either marked fixed or have been
filed against an old clang-3.7 while this one does not show up before
clang-5.0.</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>