<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@O2] CodeGenPrepare drops reference to local in dbg.value intrinsic"
   href="https://bugs.llvm.org/show_bug.cgi?id=39845">39845</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[DebugInfo@O2] CodeGenPrepare drops reference to local in dbg.value intrinsic
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>Common Code Generator Code
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>Wolfgang_Pieb@playstation.sony.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>extern void use(int);
extern int foo(long long);

void test(int p)
{
  int i = p + 4;
  (void)foo(i);  // sign extension of i
  if (p)
    return;
  use(i);        // use of original i
}
// =====================================

Compile with -g -O2.

CodeGenPrepare seems to drop a reference to a local from a dbg.value
intrinsic:

*** IR Dump Before CodeGen Prepare ***
...
entry:
  call void @llvm.dbg.value(metadata i32 %p, metadata !12, ...
  %add = add nsw i32 %p, 4, !dbg !15
  call void @llvm.dbg.value(metadata i32 %add, metadata !13, ...

*** IR Dump After CodeGen Prepare ***
entry:
  call void @llvm.dbg.value(metadata i32 %p, metadata !12, ...
  %add = add nsw i32 %p, 4, !dbg !15
  call void @llvm.dbg.value(metadata !2, metadata !13, ...

with:
!2 = !{}
!13 = !DILocalVariable(name: "i", scope: !7, ...

So "i" does not get any location information in the .o.
If we change "foo" to take an int instead of a long long, the reference
is not dropped, and i has good location info.

On initial investigation it seems that CodeGenPrepare tries to 
optimize sign/zero extensions and in the process replaces all uses of operands
that it found worthy of extension with the extended versions.
When it finds that the optimization is not worth it (after it has already
performed it), it attempts to undo the replacement, but fails to restore
the Metadata uses.

It might be worth assessing whether this optimization is really all that
useful, or find a way to restore the Metadata uses correctly.</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>