diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp --- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -484,8 +484,12 @@ : Range.TheStores) dbgs() << *SI << '\n'; dbgs() << "With: " << *AMemSet << '\n'); - if (!Range.TheStores.empty()) - AMemSet->setDebugLoc(Range.TheStores[0]->getDebugLoc()); + if (!Range.TheStores.empty()) { + SmallVector StoreLocs; + for (auto *StoreInst : Range.TheStores) + StoreLocs.push_back(StoreInst->getDebugLoc().get()); + AMemSet->setDebugLoc(DebugLoc(DILocation::getMergedLocations(StoreLocs))); + } if (MSSAU) { assert(LastMemDef && MemInsertPoint && diff --git a/llvm/test/Transforms/MemCpyOpt/memset-merged-loc.ll b/llvm/test/Transforms/MemCpyOpt/memset-merged-loc.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/MemCpyOpt/memset-merged-loc.ll @@ -0,0 +1,69 @@ +; RUN: opt -memcpyopt -S < %s | FileCheck %s +;; In test3 function, this test case checks whether the generated new memset +;; preserves the debug locations. +;; In test4 function, this test case checks whether the generated new memset +;; merges the debug locations. +; CHECK: define void @test3 +; CHECK: call void @llvm.memset.p0i8.i64(i8* align 4 %1, i8 0, i64 19, i1 false), !dbg ![[LOC1:[0-9]+]] +source_filename = "abc_debug.ll" +; CHECK: define void @test4 +; CHECK: call void @llvm.memset.p0i8.i64(i8* align 4 %1, i8 0, i64 19, i1 false), !dbg ![[LOC2:[0-9]+]] +; CHECK: ![[LOC1]] = !DILocation(line: 5, column: 1, scope +; CHECK: ![[LOC2]] = !DILocation(line: 0, scope: + +target triple = "x86_64-unknown-linux-gnu" + +define void @test3(i32* %P) !dbg !6 { +entry: + %arrayidx = getelementptr inbounds i32, i32* %P, i64 1, !dbg !8 + %add.ptrone = getelementptr inbounds i32, i32* %P, i64 2, !dbg !9 + %add.ptrtwo = getelementptr inbounds i32, i32* %P, i64 3, !dbg !10 + %0 = bitcast i32* %add.ptrtwo to i8*, !dbg !11 + store i32 0, i32* %arrayidx, align 4, !dbg !12 + store i32 0, i32* %add.ptrone, align 4, !dbg !12 + tail call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 11, i1 false), !dbg !12 + ret void, !dbg !15 +} + +define void @test4(i32* %P) !dbg !16 { +entry: + %arrayidx = getelementptr inbounds i32, i32* %P, i64 1, !dbg !17 + %add.ptrone = getelementptr inbounds i32, i32* %P, i64 2, !dbg !18 + %add.ptrtwo = getelementptr inbounds i32, i32* %P, i64 3, !dbg !19 + %0 = bitcast i32* %add.ptrtwo to i8*, !dbg !20 + store i32 0, i32* %arrayidx, align 4, !dbg !21 + store i32 0, i32* %add.ptrone, align 4, !dbg !22 + tail call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 11, i1 false), !dbg !23 + ret void, !dbg !24 +} + +; Function Attrs: argmemonly nofree nosync nounwind willreturn writeonly +declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) + +!llvm.module.flags = !{!0} +!llvm.dbg.cu = !{!1} +!llvm.debugify = !{!4, !5} + +!0 = !{i32 2, !"Debug Info Version", i32 3} +!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !3) +!2 = !DIFile(filename: "", directory: "/") +!3 = !{} +!4 = !{i32 16} +!5 = !{i32 0} +!6 = distinct !DISubprogram(name: "test3", linkageName: "test3", scope: null, file: !2, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !1, retainedNodes: !3) +!7 = !DISubroutineType(types: !3) +!8 = !DILocation(line: 1, column: 1, scope: !6) +!9 = !DILocation(line: 2, column: 1, scope: !6) +!10 = !DILocation(line: 3, column: 1, scope: !6) +!11 = !DILocation(line: 4, column: 1, scope: !6) +!12 = !DILocation(line: 5, column: 1, scope: !6) +!15 = !DILocation(line: 8, column: 1, scope: !6) +!16 = distinct !DISubprogram(name: "test4", linkageName: "test4", scope: null, file: !2, line: 9, type: !7, scopeLine: 9, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !1, retainedNodes: !3) +!17 = !DILocation(line: 9, column: 1, scope: !16) +!18 = !DILocation(line: 10, column: 1, scope: !16) +!19 = !DILocation(line: 11, column: 1, scope: !16) +!20 = !DILocation(line: 12, column: 1, scope: !16) +!21 = !DILocation(line: 13, column: 1, scope: !16) +!22 = !DILocation(line: 14, column: 1, scope: !16) +!23 = !DILocation(line: 15, column: 1, scope: !16) +!24 = !DILocation(line: 16, column: 1, scope: !16)