<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 - GVN must not perform substitution based on pointer equality"
   href="https://bugs.llvm.org/show_bug.cgi?id=35229">35229</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>GVN must not perform substitution based on pointer equality
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>5.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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>post+llvm@ralfj.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Clang/LLVM currently miscompiles the following program:

// gvnbug.c
#include <stdio.h>

int foo();

void test(int* gp1, int* gp2)
{
  int g = 0;
  int a = 0, b = 0;
  int x = 7777, y = 6666; // also try swapping these
  int* p = &g;
  int* q = &g;
  int* r = &g;

  if (foo()) {
    a = 1;
    p = &y+1;
    q = &x;
  }

  *gp1 = (int)p+1;
  if (q == p) {
    b = 1;
    *gp2 = (int)q+1;
    r = q;
  }
  *r = 42;

  printf("a = %d, b = %d, x = %d\n", a, b, x);
}

int main() {
  int gp1 = 0;
  int gp2 = 0;

  test(&gp1, &gp2);

  return 0;
}

// aux.c
int foo() { return 1; }

$ clang-5.0 aux.c gvnbug.c -o gvnbug -O3 && ./gvnbug 
a = 1, b = 1, x = 7777

This result is not allowed.  If a and b are both 1, the branch "q == p" must
have been taken, so r was set to &x (via q), so x cannot be 7777.

I think this issue has already come up in
<<a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - InstCombine cannot blindly assume that inttoptr(ptrtoint x) -> x"
   href="show_bug.cgi?id=34548">https://bugs.llvm.org/show_bug.cgi?id=34548</a>>, but so far there was no example
showing that the bug arises independent of the incorrect
inttoptr-simplification.

What is happening here (if my analysis is correct) is that GVN sees the
equality "q == p" and uses that to replace "q" by "p" in the then-branch. 
Next, LLVM notices that because p is derived from y, writing to r (which will
either have value &g or p in the line where the assignment happens) cannot
possibly affect x, and hence the initial value of x can be propagated into the
printf.  GVN is wrong to perform this kind of replacement; just because the bit
representations of two pointers are equal, that doesn't mean that their
provenance information is equal.

Test case by Gil Hur.</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>