<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 --- - Wrong optimization with aliasing"
   href="https://llvm.org/bugs/show_bug.cgi?id=26603">26603</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong optimization with aliasing
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>cherepan@mccme.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The program:

#include <stdlib.h>
#include <stdio.h>

int main()
{
  void *volatile p = malloc(10);
  int *pi = p;
  double *pd = p;

  *pi = 1;
  printf("*pi = %d\n", *pi);

  int a = *pi;
  *pd = 0;
  *pi = a;

  printf("p = %p\n", p);
  printf("*pi = %d\n", *pi);
}

compiled with `clang -std=c11 -O2` prints this:

*pi = 1
p = 0x929010
*pi = 0

The last value is wrong, it should be 1.

Tested with clang 3.0, 3.5 and 3.9.0 (trunk 260831) -- all affected.

AFAICT the code above doesn't violate any aliasing rules. All reads are with
the right type. And writes to allocated objects are free to change the
effective type according to C11, 6.5p6: "If a value is stored into an object
having no declared type through an lvalue having a type that is not a character
type, then the type of the lvalue becomes the effective type of the object for
that access and for subsequent accesses that do not modify the stored value."

Similar gcc bug -- <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69776">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69776</a> .</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>