<div dir="ltr">This seems like the opposite of the other recent change to remove/zero out the debug location of an instruction being commoned into a preceeding basic block.<br><br>Adding Dehao.<br><br>Wouldn't this hurt profile based optimizations by attributing code passing through <a href="http://other.bb">other.bb</a> to the entry? Making the Entry seem more common than it is?<br><br><div class="gmail_quote"><div dir="ltr">On Thu, Sep 15, 2016 at 2:52 PM Wolfgang Pieb <<a href="mailto:wolfgang.pieb@sony.com">wolfgang.pieb@sony.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">wolfgangp created this revision.<br class="gmail_msg">
wolfgangp added reviewers: aprantl, dblaikie.<br class="gmail_msg">
wolfgangp added subscribers: llvm-commits, andreadb.<br class="gmail_msg">
<br class="gmail_msg">
When the CodeGenPrepare pass sinks a compare instruction into the basic block of a user, it should preserve its debug location. Not doing so negatively affects source line attribution for debugging and AutoFDO.<br class="gmail_msg">
<br class="gmail_msg">
Patch by Andrea Di Biagio<br class="gmail_msg">
<br class="gmail_msg">
<a href="https://reviews.llvm.org/D24632" rel="noreferrer" class="gmail_msg" target="_blank">https://reviews.llvm.org/D24632</a><br class="gmail_msg">
<br class="gmail_msg">
Files:<br class="gmail_msg">
  lib/CodeGen/CodeGenPrepare.cpp<br class="gmail_msg">
  test/DebugInfo/Generic/sunk-compare.ll<br class="gmail_msg">
<br class="gmail_msg">
Index: test/DebugInfo/Generic/sunk-compare.ll<br class="gmail_msg">
===================================================================<br class="gmail_msg">
--- test/DebugInfo/Generic/sunk-compare.ll<br class="gmail_msg">
+++ test/DebugInfo/Generic/sunk-compare.ll<br class="gmail_msg">
@@ -0,0 +1,46 @@<br class="gmail_msg">
+; RUN: opt -S -codegenprepare < %s | FileCheck %s<br class="gmail_msg">
+;<br class="gmail_msg">
+; This test case has been generated by hand but is inspired by the<br class="gmail_msg">
+; observation that compares that are sunk into the basic blocks where<br class="gmail_msg">
+; their results are used did not retain their debug locs. This caused<br class="gmail_msg">
+; sample profiling to attribute code to the wrong source lines.<br class="gmail_msg">
+;<br class="gmail_msg">
+; We check that the compare instruction retains its debug loc after<br class="gmail_msg">
+; it is sunk into <a href="http://other.bb" rel="noreferrer" class="gmail_msg" target="_blank">other.bb</a> by the codegen prepare pass.<br class="gmail_msg">
+;<br class="gmail_msg">
+; CHECK:       <a href="http://other.bb" rel="noreferrer" class="gmail_msg" target="_blank">other.bb</a>:<br class="gmail_msg">
+; CHECK-NEXT:  icmp{{.*}}%x, 0, !dbg ![[MDHANDLE:[0-9]*]]<br class="gmail_msg">
+; CHECK:       ![[MDHANDLE]] = !DILocation(line: 2<br class="gmail_msg">
+;<br class="gmail_msg">
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br class="gmail_msg">
+<br class="gmail_msg">
+define i32 @_Z3fooii(i32 %x, i32 %y) !dbg !5 {<br class="gmail_msg">
+entry:<br class="gmail_msg">
+  %cmp17 = icmp sgt i32 %x, 0, !dbg !6<br class="gmail_msg">
+  br label %<a href="http://other.bb" rel="noreferrer" class="gmail_msg" target="_blank">other.bb</a>, !dbg !6<br class="gmail_msg">
+<br class="gmail_msg">
+<a href="http://other.bb" rel="noreferrer" class="gmail_msg" target="_blank">other.bb</a>:<br class="gmail_msg">
+  br i1 %cmp17, label %<a href="http://exit1.bb" rel="noreferrer" class="gmail_msg" target="_blank">exit1.bb</a>, label %<a href="http://exit2.bb" rel="noreferrer" class="gmail_msg" target="_blank">exit2.bb</a>, !dbg !7<br class="gmail_msg">
+<br class="gmail_msg">
+<a href="http://exit1.bb" rel="noreferrer" class="gmail_msg" target="_blank">exit1.bb</a>:<br class="gmail_msg">
+  %0 = add i32 %y, 42, !dbg !8<br class="gmail_msg">
+  ret i32 %0, !dbg !8<br class="gmail_msg">
+<br class="gmail_msg">
+<a href="http://exit2.bb" rel="noreferrer" class="gmail_msg" target="_blank">exit2.bb</a>:<br class="gmail_msg">
+  ret i32 44, !dbg !9<br class="gmail_msg">
+<br class="gmail_msg">
+}<br class="gmail_msg">
+<br class="gmail_msg">
+!<a href="http://llvm.dbg.cu" rel="noreferrer" class="gmail_msg" target="_blank">llvm.dbg.cu</a> = !{!0}<br class="gmail_msg">
+!llvm.module.flags = !{!3, !4}<br class="gmail_msg">
+<br class="gmail_msg">
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !2)<br class="gmail_msg">
+!1 = !DIFile(filename: "test.cpp", directory: "/debuginfo/bug/cgp")<br class="gmail_msg">
+!2 = !{}<br class="gmail_msg">
+!3 = !{i32 2, !"Dwarf Version", i32 4}<br class="gmail_msg">
+!4 = !{i32 2, !"Debug Info Version", i32 3}<br class="gmail_msg">
+!5 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 1, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)<br class="gmail_msg">
+!6 = !DILocation(line: 2, column: 0, scope: !5)<br class="gmail_msg">
+!7 = !DILocation(line: 3, column: 0, scope: !5)<br class="gmail_msg">
+!8 = !DILocation(line: 4, column: 0, scope: !5)<br class="gmail_msg">
+!9 = !DILocation(line: 5, column: 0, scope: !5)<br class="gmail_msg">
Index: lib/CodeGen/CodeGenPrepare.cpp<br class="gmail_msg">
===================================================================<br class="gmail_msg">
--- lib/CodeGen/CodeGenPrepare.cpp<br class="gmail_msg">
+++ lib/CodeGen/CodeGenPrepare.cpp<br class="gmail_msg">
@@ -926,6 +926,8 @@<br class="gmail_msg">
       InsertedCmp =<br class="gmail_msg">
           CmpInst::Create(CI->getOpcode(), CI->getPredicate(),<br class="gmail_msg">
                           CI->getOperand(0), CI->getOperand(1), "", &*InsertPt);<br class="gmail_msg">
+      // Propagate the debug info.<br class="gmail_msg">
+      InsertedCmp->setDebugLoc(CI->getDebugLoc());<br class="gmail_msg">
     }<br class="gmail_msg">
<br class="gmail_msg">
     // Replace a use of the cmp with a use of the new cmp.<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
</blockquote></div></div>