diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -2746,7 +2746,8 @@ 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 --- /dev/null +++ b/llvm/test/Transforms/JumpThreading/branch-debug-info2.ll @@ -0,0 +1,95 @@ +; RUN: opt -passes=jump-threading -S %s -o - | FileCheck %s +define void @prepare_next_shadow() !dbg !7 { +entry: + br label %for.cond, !dbg !21 + +for.cond: ; preds = %for.inc, %entry + br label %shadow_to_ptr.exit, !dbg !22 + +shadow_to_ptr.exit: ; preds = %for.cond + %shr.i = lshr i64 0, 12, !dbg !23 + call void @llvm.dbg.value(metadata i64 %shr.i, metadata !10, metadata !DIExpression()), !dbg !23 + %cmp.i = icmp ult i64 %shr.i, undef, !dbg !24 + call void @llvm.dbg.value(metadata i1 %cmp.i, metadata !12, metadata !DIExpression()), !dbg !24 + br i1 %cmp.i, label %cleanup.i, label %if.end.i60, !dbg !25 + +if.end.i60: ; preds = %shadow_to_ptr.exit + %sub.i = sub i64 %shr.i, undef, !dbg !26 + call void @llvm.dbg.value(metadata i64 %sub.i, metadata !14, metadata !DIExpression()), !dbg !26 + %cmp3.i = icmp ugt i64 %sub.i, 32763, !dbg !27 + call void @llvm.dbg.value(metadata i1 %cmp3.i, metadata !15, metadata !DIExpression()), !dbg !27 + %conv7.i = trunc i64 %sub.i to i32, !dbg !28 + call void @llvm.dbg.value(metadata i32 %conv7.i, metadata !16, metadata !DIExpression()), !dbg !28 +; 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 !29 + %spec.select.i = select i1 %cmp3.i, i32 -1, i32 %conv7.i, !dbg !29 + call void @llvm.dbg.value(metadata i32 %spec.select.i, metadata !18, metadata !DIExpression()), !dbg !29 + br label %ptr_to_shadow.exit, !dbg !30 + +cleanup.i: ; preds = %shadow_to_ptr.exit + br label %ptr_to_shadow.exit, !dbg !31 + +ptr_to_shadow.exit: ; preds = %cleanup.i, %if.end.i60 + %call1861 = phi i32 [ %spec.select.i, %if.end.i60 ], [ -1, %cleanup.i ], !dbg !32 + call void @llvm.dbg.value(metadata i32 %call1861, metadata !19, metadata !DIExpression()), !dbg !32 + %cmp19 = icmp slt i32 %call1861, 0, !dbg !33 + call void @llvm.dbg.value(metadata i1 %cmp19, metadata !20, metadata !DIExpression()), !dbg !33 + br i1 %cmp19, label %for.inc, label %if.end22, !dbg !34 + +if.end22: ; preds = %ptr_to_shadow.exit + unreachable, !dbg !35 + +for.inc: ; preds = %ptr_to_shadow.exit + br label %for.cond, !dbg !36 +} + +; Function Attrs: nofree nosync nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { "use-soft-float"="false" } +attributes #1 = { nofree nosync nounwind readnone speculatable willreturn } + +!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 8} +!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: !9) +!8 = !DISubroutineType(types: !3) +!9 = !{!10, !12, !14, !15, !16, !18, !19, !20} +!10 = !DILocalVariable(name: "1", scope: !7, file: !2, line: 3, type: !11) +!11 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned) +!12 = !DILocalVariable(name: "2", scope: !7, file: !2, line: 4, type: !13) +!13 = !DIBasicType(name: "ty8", size: 8, encoding: DW_ATE_unsigned) +!14 = !DILocalVariable(name: "3", scope: !7, file: !2, line: 6, type: !11) +!15 = !DILocalVariable(name: "4", scope: !7, file: !2, line: 7, type: !13) +!16 = !DILocalVariable(name: "5", scope: !7, file: !2, line: 8, type: !17) +!17 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned) +!18 = !DILocalVariable(name: "6", scope: !7, file: !2, line: 9, type: !17) +!19 = !DILocalVariable(name: "7", scope: !7, file: !2, line: 12, type: !17) +!20 = !DILocalVariable(name: "8", scope: !7, file: !2, line: 13, type: !13) +!21 = !DILocation(line: 1, column: 1, scope: !7) +!22 = !DILocation(line: 2, column: 1, scope: !7) +!23 = !DILocation(line: 3, column: 1, scope: !7) +!24 = !DILocation(line: 4, column: 1, scope: !7) +!25 = !DILocation(line: 5, column: 1, scope: !7) +!26 = !DILocation(line: 6, column: 1, scope: !7) +!27 = !DILocation(line: 7, column: 1, scope: !7) +!28 = !DILocation(line: 8, column: 1, scope: !7) +!29 = !DILocation(line: 9, column: 1, scope: !7) +!30 = !DILocation(line: 10, column: 1, scope: !7) +!31 = !DILocation(line: 11, column: 1, scope: !7) +!32 = !DILocation(line: 12, column: 1, scope: !7) +!33 = !DILocation(line: 13, column: 1, scope: !7) +!34 = !DILocation(line: 14, column: 1, scope: !7) +!35 = !DILocation(line: 15, column: 1, scope: !7) +!36 = !DILocation(line: 16, column: 1, scope: !7)