<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_CONFIRMED "
   title="CONFIRMED - [DebugInfo@O2][Dexter] dbg.value's of isel vreg's can be accidentally dropped"
   href="https://bugs.llvm.org/show_bug.cgi?id=39755">39755</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[DebugInfo@O2][Dexter] dbg.value's of isel vreg's can be accidentally dropped
          </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>CONFIRMED
          </td>
        </tr>

        <tr>
          <th>Keywords</th>
          <td>wrong-debug
          </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>jeremy.morse.llvm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>chackz0x12@gmail.com, greg.bedwell@sony.com, international.phantom@gmail.com, llvm-bugs@lists.llvm.org, paul.robinson@am.sony.com
          </td>
        </tr>

        <tr>
          <th>Blocks</th>
          <td>38754, 38768
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This bug is spun out of <a class="bz_bug_link 
          bz_status_CONFIRMED "
   title="CONFIRMED - [DebugInfo@O2][Dexter] Illegal value appears in variable when conditional blocks folded"
   href="show_bug.cgi?id=38754">bug 38754</a> to represent a regression I saw there; using
llvm/clang r346686 with options "-O2 -g -fno-inline", and with CodeGenPrepare's
placeDbgValues function disabled, the "blah" variable in the code below does
not get a location. (It does if placeDbgValues is enabled).

--------8<--------
struct faces {
  unsigned bar : 15;
  unsigned baz : 15;
  unsigned qux : 1;
  unsigned wat;
};

void forks(faces *foo, bool cond, int someval) {
  foo->wat = foo->bar;
  if (someval == 4) {
    unsigned blah = foo->bar;
    foo->baz = 3;
    if (cond)
      blah |= 4;
    else
      blah |= 8;
    foo->bar = blah;
  }
}

int
main()
{
  volatile int foo = 4;
  bool cond = foo == 4;
  faces spoon;
  spoon.bar = 0;
  spoon.baz = 0;
  forks(&spoon, cond, foo);
  return spoon.bar;
}
-------->8--------

The IR for the start of the outer conditional block in the 'forks' function,
where !24 is the DIVariable for "blah":

--------8<--------
  if.then:                                          ; preds = %entry
    %1 = bitcast %struct.faces* %foo to i32*
    call void @llvm.dbg.value(metadata i32 %bf.clear, metadata !24, [...])
    %bf.clear4 = and i32 %bf.load, -1073741824
    call void @llvm.dbg.value(metadata i32 undef, metadata !24, [...])
    %blah.0 = select i1 %cond, i32 98308, i32 98312
-------->8--------

Some facts:
 * The inner condition has been optimised into a single select instruction
 * The stores to foo->baz and foo->bar gets merged into a single store
 * The read of foo->bar at the start of the function makes the field live
   in the %bf.clear SSA register, the whole memory word lives in %bf.load
 * The middle line (the "and" instruction) survives into the object file

With the IR above, I would expect that "blah" would have a location on the
"and" instruction, and to then lose it afterwards. The dbg.value(undef) comes
from the merged stores getting instcombined with the logic insns, leaving no
single value for "blah".

Unfortunately, the first dbg.value dangles because %bf.clear isn't read until a
few insns further down the IR BB, and the dbg.value(undef) then causes the
first one to get dropped. That feature [0] is to avoid emitting spurious
DBG_VALUEs after a new valuation is available; in this circumstance we end up
dropping a legitimate dbg.value because we didn't know it was _going_ to be
legitimate yet.

IMHO, in the theme of this WIP [1] patch, we need to distinguish what's a
read/write of a VReg from general SDNodes. The value being referred to (in a
VReg) is already available and the DBG_VALUE could be emitted without
generating spurious code, we just don't recognise it.

There's also the matter that the value of "blah" could probably be recovered
from the instcombined Value, but is instead marked as undef, however that's
probably a different ticket.

[0]
<a href="https://github.com/llvm-mirror/llvm/blob/5018f6ea8fcd1c655d36a2ae1900e0ccee906b96/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L1121">https://github.com/llvm-mirror/llvm/blob/5018f6ea8fcd1c655d36a2ae1900e0ccee906b96/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L1121</a>
[1] <a href="https://reviews.llvm.org/D54715">https://reviews.llvm.org/D54715</a></pre>
        </div>
      </p>

        <div id="referenced">
          <hr style="border: 1px dashed #969696">
          <b>Referenced Bugs:</b>
          <ul>
              <li>
                [<a class="bz_bug_link 
          bz_status_CONFIRMED "
   title="CONFIRMED - [DebugInfo@O2][Dexter] Illegal value appears in variable when conditional blocks folded"
   href="https://bugs.llvm.org/show_bug.cgi?id=38754">Bug 38754</a>] [DebugInfo@O2][Dexter] Illegal value appears in variable when conditional blocks folded
              </li>
              <li>
                [<a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [meta][DebugInfo] Umbrella bug for poor debug experiences"
   href="https://bugs.llvm.org/show_bug.cgi?id=38768">Bug 38768</a>] [meta][DebugInfo] Umbrella bug for poor debug experiences
              </li>
          </ul>
        </div>
        <br>

      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>