<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 - Wrong optimizations for pointers: `p == q ? p : q` -> `q`"
   href="https://bugs.llvm.org/show_bug.cgi?id=44374">44374</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong optimizations for pointers: `p == q ? p : q` -> `q`
          </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>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>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ch3root@openwall.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Similar to <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Wrong optimizations for pointers: `if (q == p) use p` -> `if (q == p) use q`"
   href="show_bug.cgi?id=44313">bug 44313</a>.

The optimizer sometimes changes `p == q ? p : q` to `q`. This is wrong when the
actual provenance of `p` differs from that of `q`.
There are two forms -- with the actual conditional operator and with the `if`
statement.

The ideal example would be constructed with the help of restricted pointers but
it's run into a theoretical problem -- see the first testcase in <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Optimization with `restrict`: is `p == q ? p : q` "based" on `p`?"
   href="show_bug.cgi?id=44373">bug 44373</a>.
My other examples require two conditionals to eliminate the possibility of UB.
Comparison of integers should give stable results, hopefully that would be
enough to demonstrate the problem.

gcc bug -- <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93052">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93052</a>.

Example with the conditional operator and with dead malloc (the wrong
optimization seems to be applied in Early CSE):

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

__attribute__((noipa,optnone)) // imagine it in a separate TU
static void *opaque(void *p) { return p; }

int main()
{
    int *q = malloc(sizeof(int));
    opaque(q);
    uintptr_t iq = (uintptr_t)(void *)q;
    free(q);

    int *p = malloc(sizeof(int));
    opaque(p);
    uintptr_t ip = (uintptr_t)(void *)p;

    uintptr_t ir = ip == iq ? ip : iq;
    if (ip == iq) {
        *p = 1;
        *(int *)(void *)ir = 2;
        printf("result: %d\n", *p);
    }
}
----------------------------------------------------------------------
$ clang -std=c11 -Weverything -Wno-unknown-attributes test.c && ./a.out
result: 2
$ clang -std=c11 -Weverything -Wno-unknown-attributes -O3 test.c && ./a.out
result: 1
----------------------------------------------------------------------
clang x86-64 version: clang version 10.0.0
(<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a>
fccac1ec16951e9a9811abf19e2c18be147854fc)
----------------------------------------------------------------------

The idea of problems arising from `p == q ? p : q` is from Chung-Kil Hur via
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65752#c15">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65752#c15</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>