<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 - Unexpected result because of undefined behavior. (GNV pass)"
   href="https://bugs.llvm.org/show_bug.cgi?id=35238">35238</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Unexpected result because of undefined behavior. (GNV pass)
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>pichet2000@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Lately I resynced an out of tree target and we found a regression in our test
suite
The same problem happen using x86 on Windows or MacOS

#include "stdio.h"

int func(short *data,int data_size)  {
  short pattern[] = {0,1,2,3};
  short d[4]={1, 2, 3, 4};
  int i, ret;
  int *ptr_int;

  for ( i = 0 ; i < data_size ; i++ )
    d[pattern[i]] = data[i];
  ptr_int = (int *)d; 
  ret = *ptr_int;  // undefined behavior

  return ret;
}
int main() {
  short data[] = {0x32,0x31};
  int result =  func(data, 4);
  printf("%x\n", result);
}

clang -O0 test.c
will print 310032

clang -Os test.c
will print: 20001  (surprising result)

Both GCC 6.5 and MSVC will print 310032 (Optimized or not).
clang 5.0 will also print out 310032 so its a recent regression.

Disabling the GVN pass on trunk will cause clang to print 310032 even in
optimized mode.

Now I understand that dereferencing a pointer after a pointer type cast is
undefined behavior.

But do we really want LLVM to be that aggressive? My personal opinion is that
LLVM shouldn't give such unexpected result even if the code contains undefined
behavior.</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>