<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 - -O2 code miscompilation starting in clang 3.9 (still in 4.0.1rc1)"
   href="https://bugs.llvm.org/show_bug.cgi?id=33063">33063</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-O2 code miscompilation starting in clang 3.9 (still in 4.0.1rc1)
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>FreeBSD
          </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>terry-llvm@glaver.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=18451" name="attach_18451" title="Complete program to reproduce the bug">attachment 18451</a> <a href="attachment.cgi?id=18451&action=edit" title="Complete program to reproduce the bug">[details]</a></span>
Complete program to reproduce the bug

It appears that clang 3.9.1 is generating incorrect code when compiling the
attached example program (extracted from a much larger project written by dmr
many years ago) with optimization greater than -O0. 3.8.1 generates working
code at all of -O0 -O1 and -O2. I have not tried versions > 3.8.1 and < 3.9.1,
so I can't pin it down more definitively. 3.9.1 generates working code at -O0,
returns an incorrect numerical result at -O1, and returns nan at -O2 and -O3.
4.0.1rc1 behaves the same as 3.9.1. A relatively recent version of gcc (4.8.4)
also compiles the program correctly at -O2. The problem was reproduced on a
Linux system with different hardware, so it doesn't seem to be a
system-specific issue. All of the following tests were performed on: 

FreeBSD pool20.dom.ain 11.0-STABLE FreeBSD 11.0-STABLE #0 r318279: Sun May 14
 16:26:06 EDT 2017     root@pool20:/usr/obj/usr/src/sys/GENERIC  amd64

Here is the result of compilation / testing at -O1, -O2, and -O3 with 3.8.1 and
3.9.1:

Script started on Tue May 16 17:24:37 2017
(0:2) pool20:/sysprog/terry# clang38 -g -Wall -O0 -o single single.c -lm
(0:3) pool20:/sysprog/terry# ./single
pturbl (before) = -0.171976
q0 = 3063.745875, -v0 = -1202.450010
pturbl (after) = 2.676092
(0:4) pool20:/sysprog/terry# clang38 -g -Wall -O1 -o single single.c -lm
(0:5) pool20:/sysprog/terry# ./single
pturbl (before) = -0.171976
q0 = 3063.745875, -v0 = -1202.450010
pturbl (after) = 2.676092
(0:6) pool20:/sysprog/terry# clang38 -g -Wall -O2 -o single single.c -lm
(0:7) pool20:/sysprog/terry# ./single
pturbl (before) = -0.171976
q0 = 3063.745875, -v0 = -1202.450010
pturbl (after) = 2.676092
(0:8) pool20:/sysprog/terry# clang39 -g -Wall -O0 -o single single.c -lm
(0:9) pool20:/sysprog/terry# ./single
pturbl (before) = -0.171976
q0 = 3063.745875, -v0 = -1202.450010
pturbl (after) = 2.676092
(0:10) pool20:/sysprog/terry# clang39 -g -Wall -O1 -o single single.c -lm
(0:11) pool20:/sysprog/terry# ./single
pturbl (before) = -0.171976
q0 = 3063.745875, -v0 = -1202.450010
pturbl (after) = 9.653773
(0:12) pool20:/sysprog/terry# clang39 -g -Wall -O2 -o single single.c -lm
(0:13) pool20:/sysprog/terry# ./single
pturbl (before) = -0.171976
q0 = 3063.745875, -v0 = -1202.450010
pturbl (after) = nan
(0:14) pool20:/sysprog/terry# exit
Script done on Tue May 16 17:25:38 2017

The expected answer is 2.676092. As you can see, that happens in all but the
3.9.1 -O1 and -O2 tests.

The function in question is:

double
cosadd(int n, double coef, ...)
{
        double *coefp;
        char *cp;
        int i;
        double sum, a1, a2;

        sum = 0;
        cp = cacp;

loop:
        a1 = *cafp++;
        if(a1 == 0) {
                cacp = cp;
                return sum;
        }
        a2 = *cafp++;
        i = n;
        coefp = &coef;
        do
                a2 += *cp++ * *coefp++;
        while(--i);
        sum += a1 * cos(a2);
        goto loop;
}

Which is copied verbatim from dmr's source. While ugly, it passes -Wall. It
dates from the same era as dmr's famous "You are not expected to understand
this" comment.

I am attaching the complete program needed to reproduce this. If more
information is needed, just let me know.</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>