<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 --- - Optimized assembly raises div-by-zero exception though C source code takes pains to avoid it"
   href="https://llvm.org/bugs/show_bug.cgi?id=23522">23522</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimized assembly raises div-by-zero exception though C source code takes pains to avoid it
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mick@nag.co.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=14326" name="attach_14326" title="Program showing clang optimization bug">attachment 14326</a> <a href="attachment.cgi?id=14326&action=edit" title="Program showing clang optimization bug">[details]</a></span>
Program showing clang optimization bug

The attached C program takes care to avoid division by zero when presented with
a = b = 0.0. It works correctly when compiled without optimization (using -O0).
When compiler optimization level -O1 or above is used, the assembly generated:

        movsd   -16(%rbp), %xmm0
        mulsd   %xmm0, %xmm0
        movsd   -24(%rbp), %xmm1
        mulsd   %xmm1, %xmm1
        addsd   %xmm0, %xmm1
        movsd   LCPI0_0(%rip), %xmm0
        divsd   %xmm1, %xmm0
        cmpltsd LCPI0_1(%rip), %xmm1
        movapd  %xmm1, %xmm2
        andnpd  %xmm0, %xmm2
        movsd   LCPI0_2(%rip), %xmm0
        andpd   %xmm1, %xmm0
        orpd    %xmm2, %xmm0
        sqrtsd  %xmm0, %xmm0

performs a floating-point division before checking for zero denominator and
hence raises a div-by-zero exception:

clang -O1 optbug.c
./a.out
Input a, b: 0 0
Floating exception</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>