<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] CodeGenPrepare address-mode sinking drops dbg.values"
   href="https://bugs.llvm.org/show_bug.cgi?id=39937">39937</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[DebugInfo@O2] CodeGenPrepare address-mode sinking drops dbg.values
          </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>jeremy.morse.llvm@gmail.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jeremy.morse.llvm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>chackz0x12@gmail.com, dblaikie@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>Using latest trunk (r348754), running the LLVM IR below through "llc
-stop-after=codegenprepare" drops the single call to dbg.value un-necessarily.
Note the extra branch / basic block: it's contrived in this test to demonstrate
the overall problem.

-------->8--------
declare void @llvm.dbg.value(metadata, metadata, metadata)

define i32 @lala(i32 *%ptr) {
  %bees = getelementptr i32, i32* %ptr, i32 4
  br label %next

next:
  call void @llvm.dbg.value(metadata i32 *%bees, metadata !1, metadata
!DIExpression())
  store i32 1, i32 *%bees
  ret i32 1
}

!llvm.module.flags = !{!4}
!1 = !DILocalVariable(name: "bees", scope: !5, type: null)
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer:
"beards", isOptimized: true, runtimeVersion: 4, emissionKind: FullDebug)
!3 = !DIFile(filename: "bees.cpp", directory: "")
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "nope", scope: !2, file: !3, line: 1, unit:
!2)
!6 = !DILocation(line: 0, scope: !5)
--------8<--------

The following is the shortened output of llc (stopping at CGP):

-------->8--------
  define i32 @lala(i32* %ptr) {
  next:
    %0 = bitcast i32* %ptr to i8*
    %sunkaddr = getelementptr i8, i8* %0, i64 16
    %1 = bitcast i8* %sunkaddr to i32*
    store i32 1, i32* %1
    ret i32 1
  }
--------8<--------

CGP has applied two transformations, it sinks the GEP from the first BB into
the second (to allow it to be folded into machine addressing modes), after
which it merges the two basic blocks. The problem is in the first
transformation: the debug user (dbg.value call) of the dereferenced pointer is
not updated after the GEP is sunk, leaving it referencing a nonexistant value
and ultimately getting dropped. This problem can be decomposed further:
 * The dbg.value should have been updated to refer to the sunk GEP
 * If that was impossible, a dbg.value(undef...) should have been produced.

I'm seeing this in the context 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> (the placeDbgValues odyssey),
however the problem with the input above replicates with plain trunk.</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>