diff --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp --- a/llvm/lib/Transforms/Scalar/SCCP.cpp +++ b/llvm/lib/Transforms/Scalar/SCCP.cpp @@ -1693,6 +1693,7 @@ continue; if (IV.getConstantRange().isAllNonNegative()) { auto *ZExt = new ZExtInst(ExtOp, Inst.getType(), "", &Inst); + ZExt->setDebugLoc(Inst.getDebugLoc()); InsertedValues.insert(ZExt); Inst.replaceAllUsesWith(ZExt); Solver.removeLatticeValueFor(&Inst); diff --git a/llvm/test/Transforms/SCCP/sccp_preserve_debugloc.ll b/llvm/test/Transforms/SCCP/sccp_preserve_debugloc.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/SCCP/sccp_preserve_debugloc.ll @@ -0,0 +1,73 @@ +; RUN: opt -sccp -S < %s | FileCheck %s +; COM: Check whether the debug location is preserved. +; CHECK: %{{[a-zA-Z0-9_]*}} = zext i32 %tmp9 to i64, !dbg ![[DBGLOC:[0-9]+]] +; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 %{{[0-9]+}}, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg ![[DBGLOC]] +source_filename = "abc.ll" +target triple = "x86_64-unknown-linux-gnu" + +define void @foo(i64* %arg) !dbg !6 { +bb: + %tmp = zext i8 undef to i32, !dbg !20 + %tmp1 = getelementptr inbounds i64, i64* %arg, i32 0, !dbg !21 + %tmp2 = load i64, i64* %tmp1, align 8, !dbg !22 + switch i32 %tmp, label %bb20 [ + i32 1, label %bb3 + i32 2, label %bb4 + i32 4, label %bb19 + ], !dbg !23 + +bb3: ; preds = %bb + unreachable, !dbg !24 + +bb4: ; preds = %bb + %tmp5 = add i64 %tmp2, 3, !dbg !25 + %tmp6 = and i64 %tmp5, 3, !dbg !26 + %tmp7 = sub i64 3, %tmp6, !dbg !27 + %tmp8 = shl i64 %tmp7, 1, !dbg !28 + %tmp9 = trunc i64 %tmp8 to i32, !dbg !29 + %tmp10 = sext i32 %tmp9 to i64, !dbg !30 + call void @llvm.dbg.value(metadata i64 %tmp10, metadata !19, metadata !DIExpression()), !dbg !30 + unreachable, !dbg !31 + +bb19: ; preds = %bb + unreachable, !dbg !32 + +bb20: ; preds = %bb + ret void, !dbg !33 +} + +; Function Attrs: nofree nosync nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #0 + +attributes #0 = { nofree nosync nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.debugify = !{!3, !4} +!llvm.module.flags = !{!5} + +!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "abc_simp.ll", directory: "/") +!2 = !{} +!3 = !{i32 14} +!4 = !{i32 9} +!5 = !{i32 2, !"Debug Info Version", i32 3} +!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8) +!7 = !DISubroutineType(types: !2) +!8 = !{!19} +!10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned) +!12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned) +!19 = !DILocalVariable(name: "9", scope: !6, file: !1, line: 11, type: !12) +!20 = !DILocation(line: 1, column: 1, scope: !6) +!21 = !DILocation(line: 2, column: 1, scope: !6) +!22 = !DILocation(line: 3, column: 1, scope: !6) +!23 = !DILocation(line: 4, column: 1, scope: !6) +!24 = !DILocation(line: 5, column: 1, scope: !6) +!25 = !DILocation(line: 6, column: 1, scope: !6) +!26 = !DILocation(line: 7, column: 1, scope: !6) +!27 = !DILocation(line: 8, column: 1, scope: !6) +!28 = !DILocation(line: 9, column: 1, scope: !6) +!29 = !DILocation(line: 10, column: 1, scope: !6) +!30 = !DILocation(line: 11, column: 1, scope: !6) +!31 = !DILocation(line: 12, column: 1, scope: !6) +!32 = !DILocation(line: 13, column: 1, scope: !6) +!33 = !DILocation(line: 14, column: 1, scope: !6)