[JumpThreading] merge debug info when merging select+br
authorNick Desaulniers <ndesaulniers@google.com>
Tue, 13 Apr 2021 00:51:16 +0000 (17:51 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Tue, 13 Apr 2021 00:51:21 +0000 (17:51 -0700)
Jump threading can replace select then unconditional branch with
conditional branch, but when doing so loses debug info.

This destructive transform is eventually leading to a failed Verifier
run during full LTO builds of the Linux kernel with CFI and KCOV
enabled, as reported in PR39531.

ModuleSanitizerCoveragePass will insert calls to
__sanitizer_cov_trace_pc, and sometimes split critical edges,
using whatever debug info may or may not exist for the branch for
the added libcall. Since we can inline calls to
__sanitizer_cov_trace_pc due to LTO, this can lead to the error
observed in PR39531 when the debug info isn't propagated to
the libcall, because of prior destructive transforms that failed to
retain debug info.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D100137

llvm/lib/Transforms/Scalar/JumpThreading.cpp
llvm/test/Transforms/JumpThreading/branch-debug-info2.ll [new file with mode: 0644]

index 6c8d243..fab287e 100644 (file)
@@ -2750,7 +2750,8 @@ void JumpThreadingPass::unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB,
   PredTerm->removeFromParent();
   NewBB->getInstList().insert(NewBB->end(), PredTerm);
   // Create a conditional branch and update PHI nodes.
-  BranchInst::Create(NewBB, BB, SI->getCondition(), Pred);
+  auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred);
+  BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc());
   SIUse->setIncomingValue(Idx, SI->getFalseValue());
   SIUse->addIncoming(SI->getTrueValue(), NewBB);
 
diff --git a/llvm/test/Transforms/JumpThreading/branch-debug-info2.ll b/llvm/test/Transforms/JumpThreading/branch-debug-info2.ll
new file mode 100644 (file)
index 0000000..2bfa492
--- /dev/null
@@ -0,0 +1,70 @@
+; RUN: opt -passes=jump-threading -S %s -o - | FileCheck %s
+define void @prepare_next_shadow() !dbg !7 {
+entry:
+  br label %for.cond, !dbg !9
+
+for.cond:                                         ; preds = %for.inc, %entry
+  br label %shadow_to_ptr.exit, !dbg !10
+
+shadow_to_ptr.exit:                               ; preds = %for.cond
+  %shr.i = lshr i64 0, 12, !dbg !11
+  %cmp.i = icmp ult i64 %shr.i, undef, !dbg !12
+  br i1 %cmp.i, label %cleanup.i, label %if.end.i60, !dbg !13
+
+if.end.i60:                                       ; preds = %shadow_to_ptr.exit
+  %sub.i = sub i64 %shr.i, undef, !dbg !14
+  %cmp3.i = icmp ugt i64 %sub.i, 32763, !dbg !15
+  %conv7.i = trunc i64 %sub.i to i32, !dbg !16
+  %spec.select.i = select i1 %cmp3.i, i32 -1, i32 %conv7.i, !dbg !17
+; Jump threading is going to fold the select in to the branch. Ensure debug
+; info is not lost, and is merged from the select and the branch.
+; CHECK-NOT: br i1 %cmp3.i, label %for.inc, label %ptr_to_shadow.exit
+; CHECK: br i1 %cmp3.i, label %for.inc, label %ptr_to_shadow.exit, !dbg [[DBG:![0-9]+]]
+; CHECK: [[DBG]] = !DILocation(line: 9, column: 1, scope: !{{.*}})
+
+  br label %ptr_to_shadow.exit, !dbg !17
+
+cleanup.i:                                        ; preds = %shadow_to_ptr.exit
+  br label %ptr_to_shadow.exit, !dbg !19
+
+ptr_to_shadow.exit:                               ; preds = %cleanup.i, %if.end.i60
+  %call1861 = phi i32 [ %spec.select.i, %if.end.i60 ], [ -1, %cleanup.i ], !dbg !20
+  %cmp19 = icmp slt i32 %call1861, 0, !dbg !21
+  br i1 %cmp19, label %for.inc, label %if.end22, !dbg !22
+
+if.end22:                                         ; preds = %ptr_to_shadow.exit
+  unreachable, !dbg !23
+
+for.inc:                                          ; preds = %ptr_to_shadow.exit
+  br label %for.cond, !dbg !24
+}
+
+!llvm.ident = !{!0}
+!llvm.dbg.cu = !{!1}
+!llvm.debugify = !{!4, !5}
+!llvm.module.flags = !{!6}
+
+!0 = !{!""}
+!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !3)
+!2 = !DIFile(filename: "jump_threading.ll", directory: "/")
+!3 = !{}
+!4 = !{i32 16}
+!5 = !{i32 0}
+!6 = !{i32 2, !"Debug Info Version", i32 3}
+!7 = distinct !DISubprogram(name: "prepare_next_shadow", linkageName: "prepare_next_shadow", scope: null, file: !2, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !1, retainedNodes: !3)
+!8 = !DISubroutineType(types: !3)
+!9 = !DILocation(line: 1, column: 1, scope: !7)
+!10 = !DILocation(line: 2, column: 1, scope: !7)
+!11 = !DILocation(line: 3, column: 1, scope: !7)
+!12 = !DILocation(line: 4, column: 1, scope: !7)
+!13 = !DILocation(line: 5, column: 1, scope: !7)
+!14 = !DILocation(line: 6, column: 1, scope: !7)
+!15 = !DILocation(line: 7, column: 1, scope: !7)
+!16 = !DILocation(line: 8, column: 1, scope: !7)
+!17 = !DILocation(line: 9, column: 1, scope: !7)
+!19 = !DILocation(line: 11, column: 1, scope: !7)
+!20 = !DILocation(line: 12, column: 1, scope: !7)
+!21 = !DILocation(line: 13, column: 1, scope: !7)
+!22 = !DILocation(line: 14, column: 1, scope: !7)
+!23 = !DILocation(line: 15, column: 1, scope: !7)
+!24 = !DILocation(line: 16, column: 1, scope: !7)