<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] icmp-eq-chain to switch optimisation drops dbg.values"
   href="https://bugs.llvm.org/show_bug.cgi?id=39866">39866</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[DebugInfo@O2] icmp-eq-chain to switch optimisation drops dbg.values
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>6.0
          </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
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The dbg.value in the code below is dropped when run through simplifycfg:

    opt-6.0 < test.ll -simplifycfg -S

in any recent version of LLVM. It's relatively easy to understand: chains of
equality comparisons are converted into being a switch statement. However, any
debug metadata on the path from the first comparison to any of the leafs gets
dropped, because the switch statement point at the leafs and nothing is
salvaged from the intermediate blocks. Thus, if you put that dbg.value in any
block other than 'cont1' below, it survives to output.

The example is contrived (and the DebugInfo cooked), but IMHO we can easily do
better by replicating intervening dbg.value's into the leaf blocks.

Context: this is another bug seen when investigating <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>. Conditional
code like this [0] (and line 9688) gets converted to being a switch, and the
dbg.value's of the casted expression usually get shifted to an earlier block by
placeDbgValues. When left in the correct place (in the 'if' condition) a
condition similar to the example below occur.

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

define i32 @lala(i32 *%ptr, i32 *%otherptr) {
  %val = load i32, i32 *%ptr
  %isone = icmp eq i32 %val, 1
  br i1 %isone, label %isoneb, label %cont1

isoneb:
  store i32 1, i32 *%otherptr
  ret i32 1

cont1:
  call void @llvm.dbg.value(metadata i32 %val, metadata !1, metadata
!DIExpression()), !dbg !6
  %istwo = icmp eq i32 %val, 2
  br i1 %istwo, label %istwob, label %cont2

istwob:
  store i32 2, i32 *%otherptr
  ret i32 2

cont2:
  ret i32 3
}

!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<--------

[0]
<a href="https://github.com/llvm-mirror/clang/blob/aa528ab4a083268edddd038d0f251afe792cfa36/lib/Sema/SemaOpenMP.cpp#L9694">https://github.com/llvm-mirror/clang/blob/aa528ab4a083268edddd038d0f251afe792cfa36/lib/Sema/SemaOpenMP.cpp#L9694</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>
          </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>