diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp index 0e20b5e6204037..2a6997c48883d4 100644 --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1932,30 +1932,6 @@ bool IndVarSimplify::run(Loop *L) { } } - // The loop exit values have been updated; insert the debug location - // for the induction variable with its final value. - if (PHINode *IndVar = L->getInductionVariable(*SE)) { - const SCEV *IndVarSCEV = SE->getSCEVAtScope(IndVar, L->getParentLoop()); - if (IndVarSCEV->getSCEVType() == SCEVTypes::scConstant) { - Value *FinalIVValue = cast(IndVarSCEV)->getValue(); - SmallVector DbgUsers; - SmallVector DbgUsersCloned; - findDbgUsers(DbgUsers, IndVar); - for (auto &DebugUser : DbgUsers) { - auto *Cloned = cast(DebugUser->clone()); - Cloned->replaceVariableLocationOp(static_cast(0), - FinalIVValue); - DbgUsersCloned.push_back(Cloned); - } - - SmallVector ExitBlocks; - L->getExitBlocks(ExitBlocks); - for (BasicBlock *Exit : ExitBlocks) - for (auto &DebugUser : DbgUsersCloned) - DebugUser->insertBefore(Exit->getFirstNonPHI()); - } - } - // Eliminate redundant IV cycles. NumElimIV += Rewriter.replaceCongruentIVs(L, DT, DeadInsts, TTI); diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 002bc90c9b5677..2da3675569ace8 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -31,6 +31,7 @@ #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/IR/DIBuilder.h" +#include "llvm/IR/DebugInfo.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" @@ -1619,6 +1620,31 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, // The insertion point instruction may have been deleted; clear it out // so that the rewriter doesn't trip over it later. Rewriter.clearInsertPoint(); + + // The loop exit values have been updated; insert the debug location + // for the induction variable with its final value. + if (PHINode *IndVar = L->getInductionVariable(*SE)) { + const SCEV *IndVarSCEV = SE->getSCEVAtScope(IndVar, L->getParentLoop()); + if (isa(IndVarSCEV)) { + Value *FinalIVValue = cast(IndVarSCEV)->getValue(); + SmallVector DbgUsers; + SmallVector DbgUsersCloned; + findDbgUsers(DbgUsers, IndVar); + for (auto &DebugUser : DbgUsers) { + auto *Cloned = cast(DebugUser->clone()); + Cloned->replaceVariableLocationOp(static_cast(0), + FinalIVValue); + DbgUsersCloned.push_back(Cloned); + } + + SmallVector ExitBlocks; + L->getExitBlocks(ExitBlocks); + for (BasicBlock *Exit : ExitBlocks) + for (auto &DebugUser : DbgUsersCloned) + DebugUser->insertBefore(Exit->getFirstNonPHI()); + } + } + return NumReplaced; } diff --git a/llvm/test/Transforms/LoopSimplify/pr51735-1.ll b/llvm/test/Transforms/LoopSimplify/pr51735-1.ll index de9a7452209565..cb3e8fa43e3ecf 100644 --- a/llvm/test/Transforms/LoopSimplify/pr51735-1.ll +++ b/llvm/test/Transforms/LoopSimplify/pr51735-1.ll @@ -22,22 +22,12 @@ ; 16 } ; CHECK: for.cond: {{.*}} -; CHECK: call void @llvm.dbg.value(metadata i32 poison, metadata ![[DBG:[0-9]+]], {{.*}} -; CHECK: call void @llvm.dbg.value(metadata i32 poison, metadata ![[DBG:[0-9]+]], {{.*}} -; CHECK: br i1 false, label %for.cond, label %for.end, {{.*}} +; CHECK: call void @llvm.dbg.value(metadata i32 %Index.{{[0-9]+}}, metadata ![[DBG:[0-9]+]], {{.*}} +; CHECK: call void @llvm.dbg.value(metadata i32 %inc, metadata ![[DBG:[0-9]+]], {{.*}} ; CHECK: for.end: {{.*}} ; CHECK: call void @llvm.dbg.value(metadata i32 777, metadata ![[DBG:[0-9]+]], {{.*}} -; CHECK: %call = tail call noundef i32 @_Z3nopi(i32 noundef 777), {{.*}} ; CHECK-DAG: ![[DBG]] = !DILocalVariable(name: "Index"{{.*}}) -define dso_local noundef i32 @_Z3nopi(i32 noundef %Param) local_unnamed_addr #0 !dbg !10 { -entry: - %Param.addr = alloca i32, align 4 - store i32 %Param, ptr %Param.addr, align 4 - call void @llvm.dbg.declare(metadata ptr %Param.addr, metadata !15, metadata !DIExpression()), !dbg !16 - ret i32 0, !dbg !17 -} - define dso_local void @_Z3barv() local_unnamed_addr #2 !dbg !18 { entry: call void @llvm.dbg.value(metadata i32 777, metadata !21, metadata !DIExpression()), !dbg !22 @@ -55,24 +45,16 @@ for.cond: ; preds = %for.cond, %entry for.end: ; preds = %for.cond %Index.0.lcssa = phi i32 [ %Index.0, %for.cond ], !dbg !22 - %call = tail call noundef i32 @_Z3nopi(i32 noundef %Index.0.lcssa), !dbg !34 ret void, !dbg !35 } -define dso_local noundef i32 @main() local_unnamed_addr #3 !dbg !36 { -entry: - call void @_Z3barv(), !dbg !39 - ret i32 0, !dbg !40 -} - declare void @llvm.dbg.value(metadata, metadata, metadata) -declare void @llvm.dbg.declare(metadata, metadata, metadata) !llvm.dbg.cu = !{!0} !llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8} !llvm.ident = !{!9} -!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 18.0.0 (https://github.com/llvm/llvm-project.git 18c2eb2bf02bd7666523aa566e45d62053b7db80)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) !1 = !DIFile(filename: "test.cpp", directory: "") !2 = !{i32 7, !"Dwarf Version", i32 5} !3 = !{i32 2, !"Debug Info Version", i32 3} @@ -82,14 +64,9 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) !7 = !{i32 7, !"uwtable", i32 2} !8 = !{i32 7, !"frame-pointer", i32 2} !9 = !{!"clang version 18.0.0"} -!10 = distinct !DISubprogram(name: "nop", linkageName: "_Z3nopi", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) -!11 = !DISubroutineType(types: !12) !12 = !{!13, !13} !13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) !14 = !{} -!15 = !DILocalVariable(name: "Param", arg: 1, scope: !10, file: !1, line: 1, type: !13) -!16 = !DILocation(line: 1, column: 38, scope: !10) -!17 = !DILocation(line: 2, column: 3, scope: !10) !18 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !1, file: !1, line: 5, type: !19, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) !19 = !DISubroutineType(types: !20) !20 = !{null} @@ -108,8 +85,3 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) !33 = !{!"llvm.loop.mustprogress"} !34 = !DILocation(line: 11, column: 3, scope: !18) !35 = !DILocation(line: 12, column: 1, scope: !18) -!36 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 14, type: !37, scopeLine: 14, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0) -!37 = !DISubroutineType(types: !38) -!38 = !{!13} -!39 = !DILocation(line: 15, column: 3, scope: !36) -!40 = !DILocation(line: 16, column: 1, scope: !36) diff --git a/llvm/test/Transforms/LoopSimplify/pr51735.ll b/llvm/test/Transforms/LoopSimplify/pr51735.ll index b9bdc8ca1b6fb0..c86f8ed7d0274f 100644 --- a/llvm/test/Transforms/LoopSimplify/pr51735.ll +++ b/llvm/test/Transforms/LoopSimplify/pr51735.ll @@ -27,14 +27,8 @@ ; CHECK: br i1 false, label %for.cond, label %for.end, {{.*}} ; CHECK: for.end: {{.*}} ; CHECK: call void @llvm.dbg.value(metadata i32 777, metadata ![[DBG:[0-9]+]], {{.*}} -; CHECK: %call = tail call noundef i32 @_Z3nopv(), {{.*}} ; CHECK-DAG: ![[DBG]] = !DILocalVariable(name: "Index"{{.*}}) -define dso_local noundef i32 @_Z3nopv() local_unnamed_addr #0 !dbg !10 { -entry: - ret i32 0, !dbg !14 -} - define dso_local void @_Z3barv() local_unnamed_addr #1 !dbg !15 { entry: call void @llvm.dbg.value(metadata i32 777, metadata !19, metadata !DIExpression()), !dbg !20 @@ -51,23 +45,16 @@ for.cond: ; preds = %for.cond, %entry br i1 %cmp, label %for.cond, label %for.end, !dbg !28, !llvm.loop !29 for.end: ; preds = %for.cond - %call = tail call noundef i32 @_Z3nopv(), !dbg !32 ret void, !dbg !33 } -define dso_local noundef i32 @main() local_unnamed_addr #2 !dbg !34 { -entry: - call void @_Z3barv(), !dbg !35 - ret i32 0, !dbg !36 -} - declare void @llvm.dbg.value(metadata, metadata, metadata) !llvm.dbg.cu = !{!0} !llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8} !llvm.ident = !{!9} -!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 18.0.0 (https://github.com/llvm/llvm-project.git 18c2eb2bf02bd7666523aa566e45d62053b7db80)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) !1 = !DIFile(filename: "test.cpp", directory: "") !2 = !{i32 7, !"Dwarf Version", i32 5} !3 = !{i32 2, !"Debug Info Version", i32 3} @@ -77,11 +64,7 @@ declare void @llvm.dbg.value(metadata, metadata, metadata) !7 = !{i32 7, !"uwtable", i32 2} !8 = !{i32 7, !"frame-pointer", i32 2} !9 = !{!"clang version 18.0.0"} -!10 = distinct !DISubprogram(name: "nop", linkageName: "_Z3nopv", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0) -!11 = !DISubroutineType(types: !12) -!12 = !{!13} !13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) -!14 = !DILocation(line: 2, column: 3, scope: !10) !15 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !1, file: !1, line: 5, type: !16, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !18) !16 = !DISubroutineType(types: !17) !17 = !{null} @@ -99,8 +82,4 @@ declare void @llvm.dbg.value(metadata, metadata, metadata) !29 = distinct !{!29, !28, !30, !31} !30 = !DILocation(line: 10, column: 5, scope: !26) !31 = !{!"llvm.loop.mustprogress"} -!32 = !DILocation(line: 11, column: 3, scope: !15) !33 = !DILocation(line: 12, column: 1, scope: !15) -!34 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 14, type: !11, scopeLine: 14, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0) -!35 = !DILocation(line: 15, column: 3, scope: !34) -!36 = !DILocation(line: 16, column: 1, scope: !34)