<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 - [DebugInfo][Dexter] Divide-before-return displays wrong value in debugger"
   href="https://bugs.llvm.org/show_bug.cgi?id=38773">38773</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[DebugInfo][Dexter] Divide-before-return displays wrong value in debugger
          </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>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>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>aprantl@apple.com, chackz0x12@gmail.com, dblaikie@gmail.com, greg.bedwell@sony.com, international.phantom@gmail.com, jdevlieghere@apple.com, llvm-bugs@lists.llvm.org, paul_robinson@playstation.sony.com
          </td>
        </tr>

        <tr>
          <th>Blocks</th>
          <td>38768
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Under a debugger the test below reports the wrong return value, on the return
statement, which is misleading. The program should take the "case 3" path
through the switch, divide four by four to reach one, and return that.

Compiling with llvm/clang @ r340912 and options "-O2 -g" for x86_64, both gdb
and lldb step through the program just fine, but on the return statement "print
read1" reports the value four, while "print $rax" reports one (when parked on
the ret insn).

This test is supposed to stimulate SimplifyCFGs SimplifySelectOnSwitch, which
turns the switch into a conditional branch. Dwarf data further down shows
DebugInfo is wrong, and I get the feeling this must be lower than SimplifyCFG
because the transform is simple, and it doesn't go wrong if one takes the other
path through the switch.

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

  switch ((read1 == 4) ? 3 : 1) {
  case 1:
    read1 *= read2;
    break;
  case 3:
    read1 /= read2;
    break;
  }

  return read1;
}
--------8<--------

The trimmed output of `llvm-dwarfdump --name=read1`:
-------->8--------
0x00000051: DW_TAG_variable
              DW_AT_location    (0x00000000
                 0x000000000000000c - 0x000000000000001a: DW_OP_reg0 RAX
                 0x000000000000001e - 0x000000000000001f: DW_OP_reg2 RCX
                 0x000000000000001f - 0x0000000000000022: DW_OP_reg0 RAX
                 0x0000000000000022 - 0x0000000000000025: DW_OP_reg2 RCX)
              DW_AT_name        ("read1")
--------8<--------

And the "case 3" branch of the switch:

-------->8--------
   0x00000000004004b5 <+21>:    mov    $0x4,%eax
   0x00000000004004ba <+26>:    xor    %edx,%edx
   0x00000000004004bc <+28>:    idiv   %ecx
=> 0x00000000004004be <+30>:    retq
--------8<--------

(Start addr for the function is 0x4004a0). The output of idiv is eax, but the
location data specifically moves the location of read1 to ecx for the return
instruction, which is wrong (AFAICT).</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_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>