<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 - Stale variable value in optimised code"
   href="https://bugs.llvm.org/show_bug.cgi?id=38753">38753</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Stale variable value in optimised code
          </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>jeremy.morse.llvm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>greg.bedwell@sony.com, international.phantom@gmail.com, llvm-bugs@lists.llvm.org, paul_robinson@playstation.sony.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>With the (contrived) test below, the "cheese" variable has a stale value
reported inside the "if" block. Using an up-to-date toolchain (r340912), and
compiling with "clang++ test.cpp -g -O2 -o a.out" for x86_64, on the marked
line gdb and lldb report the value of "cheese" to be four, wheras if compiled
-O0 the correct value of eight is seen.

The likely cause is SimplifyCFG's tryCSEWithPredecessor replacing the duplicate
"read1 + read2" expression, however the debug value for "cheese" is either lost
or not updated, causing debuggers to report a state the program isn't in.

It should be possible to get this right or mark "cheese" optimised out, the
true-if-block isn't merged with anything else.

Found using DExTer ( <a href="https://github.com/SNSystems/dexter">https://github.com/SNSystems/dexter</a> ).

-------->8--------
int
main()
{
  volatile int foo = 4;
  int read1 = foo;
  int read2 = foo;

  int cheese = foo;
  int a = read1 + read2;
  a += cheese;

  if (foo == 4) {
    cheese = read1 + read2;
    a -= cheese - 12;
    a *= 20;   // <------ stale value seen
    a /= 3;
  } else {
    a = 0;
  }

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