<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 - Interaction between C's restrict pointer and pointer casted from integer is ambiguous in LLVM's LangRef"
   href="https://bugs.llvm.org/show_bug.cgi?id=39846">39846</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Interaction between C's restrict pointer and pointer casted from integer is ambiguous in LLVM's LangRef
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>juneyoung.lee@sf.snu.ac.kr
          </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=21180" name="attach_21180" title="The example code">attachment 21180</a> <a href="attachment.cgi?id=21180&action=edit" title="The example code">[details]</a></span>
The example code

Consider this example:

#include <stdint.h>

int foo(int *restrict x, int *restrict y) {
  *y = 23;
  uintptr_t xi = (uintptr_t)x;
  uintptr_t yi = (uintptr_t)y;
  if (xi != yi) { // xi == yi, so this is never taken
    yi = xi;
  }
  *(int*)yi = 42; // changes *y or UB?
  return *y;
}

int bar() {
  int x;
  return foo(&x, &x); // returns 23
}

After -O3, foo(&x, &x) is optimized into 'ret i32 23' (
<a href="https://godbolt.org/z/ty73Xk">https://godbolt.org/z/ty73Xk</a> ).


Here are several ways to explain this result:

(1) foo(&x, &x) is not allowed in LLVM IR - but I believe this makes the
semantics more undefined than C (because C allows multiple restrict pointers to
point have the same address if they are only read, but never written)

(2) *(int*)yi = 42 is not allowed - but LLVM IR LangRef's pointer aliasing
rules ( <a href="https://llvm.org/docs/LangRef.html#pointer-aliasing-rules">https://llvm.org/docs/LangRef.html#pointer-aliasing-rules</a> ) say that
(int *)yi is based on y, so I think this behavior is well-defined. Perhaps we
should update the definition of based-on relation, if we follow this option.

(3) Restrict pointer cannot be casted into integer - if we choose this option,
LangRef should be updated to deal with this.

If we're to choose (2) and (3), we should update LLVM LangRef.

If this is considered to be miscompilation, I think this is related to 
<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> .</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>