[LLVMbugs] [Bug 20288] New: Wrong optimization of a division by a negative power of two and a comparison of the result

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Jul 12 04:59:05 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20288

            Bug ID: 20288
           Summary: Wrong optimization of a division by a negative power
                    of two and a comparison of the result
           Product: clang
           Version: 3.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: thomas.mertes at gmx.at
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 12761
  --> http://llvm.org/bugs/attachment.cgi?id=12761&action=edit
Should write lines with 0. Compile with -O1 or -O2 to trigger the error.

I tested signed 64-bit divisions, where the dividend is unknown at compile time
and the divisor is known at compile time. The division is done and the result
is compared to a constant. E.g.:

printf("%d\n", intExpr(-9223372036854775807L-1L) / -2147483648L !=
4294967296L);

The function intExpr returns its argument but hinders the optimizer to compute
it at compile time. If I compile the line above without optimization it writes:

  0

But when I use -O1 or -O2 it writes:

  1

Just writing the result of the division with:

  printf("%ld\n", intExpr(-9223372036854775807L-1L) / -2147483648L);

writes

  4294967296

which is the correct result. I discovered that the comparison gives wrong
results, when the most negative number is divided by a negative power of two
and the result is compared. The combination of division and comparison is
necessary to trigger the error. Somehow the optimizer thinks that the
division cannot return the expected quotient.

I am wondering that the test suite of clang does not check such things.

I have attached a little C program that should write lines with 0. When it
is compiled with -O1 or -O2 it writes 1 in some lines that are marked with
/*FAIL*/ in the attached program.


Greetings Thomas Mertes

--
Seed7 Homepage:  http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.

-- 
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/20140712/ccf45f47/attachment.html>


More information about the llvm-bugs mailing list