<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 code at -O2 code on MacOSX with clang 13.0.0"
   href="https://bugs.llvm.org/show_bug.cgi?id=52361">52361</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>wrong code at -O2 code on MacOSX with clang 13.0.0
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>13.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>kharris@mathworks.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=25405" name="attach_25405" title="source code and run script to demonstate this problem.">attachment 25405</a> <a href="attachment.cgi?id=25405&action=edit" title="source code and run script to demonstate this problem.">[details]</a></span>
source code and run script to demonstate this problem.

See code below,
At -O2 optimization, it appears that clang boils away the "else" path in the
"bool test(const Name& name)" routine below - based only on a slight variation
in the anatomy of the structure  (i.e. adding the "long t" field).

Initially, we discovered and reproduced this with Apple's Xcode 13. We then
obtained clang 13.0.0 directly from llvm.org and reproduced identical results.

The attached reproduction contains the source and script for three test
scenarios.

The first demonstrates expected behavior (no optimization).
The second demonstrates expected behavior with -O2 optimization.
The third demonstrates the unexpected behavior triggered by the addition of
"long t;" to the structure.

Thank you very much for looking into this!

REPRO CODE:

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

struct Name
{
        char j[32];
#if EXPOSEBUG
        long t;
#endif
};

bool test(const Name& name)
{
    if (0 == ((uint64_t) &name & 2)) {
        return 0;
    } else {
        return 1;
    }
}

int main()
{
        Name myname;
        bool r = test(myname);
        printf("NORMAL CASE :%s\n", r ? "!!!WRONG!!!" : "CORRECT");

        const Name *px = (const Name *) 0x1e2;
        r = test(*px);
        printf("SPECIAL CASE:%s\n", r ? "CORRECT" : "!!!WRONG!!!");

        return 0;
}</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>