[LLVMbugs] [Bug 23522] New: Optimized assembly raises div-by-zero exception though C source code takes pains to avoid it

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu May 14 02:19:31 PDT 2015


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

            Bug ID: 23522
           Summary: Optimized assembly raises div-by-zero exception though
                    C source code takes pains to avoid it
           Product: clang
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mick at nag.co.uk
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 14326
  --> https://llvm.org/bugs/attachment.cgi?id=14326&action=edit
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

-- 
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/20150514/e48dbce8/attachment.html>


More information about the llvm-bugs mailing list