<html>
    <head>
      <base href="http://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 --- - Wrong optimization of a division by a negative power of two and a comparison of the result"
   href="http://llvm.org/bugs/show_bug.cgi?id=20288">20288</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong optimization of a division by a negative power of two and a comparison of the result
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.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>normal
          </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>thomas.mertes@gmx.at
          </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=12761" name="attach_12761" title="Should write lines with 0. Compile with -O1 or -O2 to trigger the error.">attachment 12761</a> <a href="attachment.cgi?id=12761&action=edit" title="Should write lines with 0. Compile with -O1 or -O2 to trigger the error.">[details]</a></span>
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:  <a href="http://seed7.sourceforge.net">http://seed7.sourceforge.net</a>
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.</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>