Index: llvm/lib/Transforms/Scalar/ADCE.cpp =================================================================== --- llvm/lib/Transforms/Scalar/ADCE.cpp +++ llvm/lib/Transforms/Scalar/ADCE.cpp @@ -534,6 +534,8 @@ } }); + bool FoundNonDebugInstr = false; + // The inverse of the live set is the dead set. These are those instructions // that have no side effects and do not influence the control flow or return // value of the function, and may therefore be deleted safely. @@ -556,20 +558,33 @@ // Fallthrough and drop the intrinsic. } - // Prepare to delete. + // Prepare to delete. Remember if we found any non-debug instructions to + // delete. + if (!isa(&I)) + FoundNonDebugInstr = true; + Worklist.push_back(&I); salvageDebugInfo(I); } - for (Instruction *&I : Worklist) - I->dropAllReferences(); + bool InvalidateAnalyses = FoundNonDebugInstr || RegionsUpdated; - for (Instruction *&I : Worklist) { - ++NumRemoved; - I->eraseFromParent(); + // Only actully remove the dead instructions if we know we will invalidate + // cached analysis results. If we only found debug intrinsics to remove and + // we then invalidate analyses because of that, later passes may behave + // differently which then makes the presence of debug info affect code + // generation. + if (InvalidateAnalyses) { + for (Instruction *&I : Worklist) + I->dropAllReferences(); + + for (Instruction *&I : Worklist) { + ++NumRemoved; + I->eraseFromParent(); + } } - return !Worklist.empty() || RegionsUpdated; + return InvalidateAnalyses; } // A dead region is the set of dead blocks with a common live post-dominator. Index: llvm/test/Transforms/ADCE/debug-info-intrinsic-2.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/ADCE/debug-info-intrinsic-2.ll @@ -0,0 +1,57 @@ +; RUN: opt -passes=adce -S < %s | FileCheck %s + +; ADCE can remove the dbg.declare in remove_dbg_declare_and_other as it is also +; removing a non-debug instruction. + +; CHECK: define i16 @remove_dbg_declare_and_other() { +; CHECK-NEXT: entry: +; CHECK-NEXT: ret i16 0 +; CHECK-NEXT: } + +define i16 @remove_dbg_declare_and_other() { +entry: + call void @llvm.dbg.declare(metadata ptr undef, metadata !4, metadata !DIExpression()), !dbg !16 + %dead = add i8 0, 0 + ret i16 0 +} + +; In dont_remove_only_dbg_declare we must leave the dbg.declare since we're not +; removing any non-debug instructions. + +; CHECK: define i16 @dont_remove_only_dbg_declare() { +; CHECK-NEXT: entry: +; CHECK-NEXT: call void @llvm.dbg.declare +; CHECK-NEXT: ret i16 0 +; CHECK-NEXT: } + +define i16 @dont_remove_only_dbg_declare() { +entry: + call void @llvm.dbg.declare(metadata ptr undef, metadata !4, metadata !DIExpression()), !dbg !16 + ret i16 0 +} + +; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) +declare void @llvm.dbg.declare(metadata, metadata, metadata) #0 + +attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } + +!llvm.dbg.cu = !{} +!llvm.module.flags = !{!0, !1, !2, !3} + +!0 = !{i32 7, !"Dwarf Version", i32 4} +!1 = !{i32 2, !"Debug Info Version", i32 3} +!2 = !{i32 1, !"wchar_size", i32 1} +!3 = !{i32 7, !"frame-pointer", i32 2} +!4 = !DILocalVariable(name: "w", scope: !5, file: !6, line: 18, type: !11) +!5 = distinct !DILexicalBlock(scope: !7, file: !6, line: 18, column: 8) +!6 = !DIFile(filename: "foo2.c", directory: "/llvm") +!7 = distinct !DISubprogram(name: "test1", scope: !6, file: !6, line: 14, type: !8, scopeLine: 14, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !10) +!8 = !DISubroutineType(types: !9) +!9 = !{} +!10 = distinct !DICompileUnit(language: DW_LANG_C99, file: !6, producer: "clang version 16.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !9, splitDebugInlining: false, nameTableKind: None) +!11 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint64_t", file: !12, line: 60, baseType: !13) +!12 = !DIFile(filename: "/include/sys/_stdint.h", directory: "") +!13 = !DIDerivedType(tag: DW_TAG_typedef, name: "__uint64_t", file: !14, line: 108, baseType: !15) +!14 = !DIFile(filename: "/include/machine/_default_types.h", directory: "") +!15 = !DIBasicType(name: "unsigned long long", size: 64, encoding: DW_ATE_unsigned) +!16 = !DILocation(line: 18, column: 8, scope: !5) Index: llvm/test/Transforms/ADCE/debug-info-intrinsic.ll =================================================================== --- llvm/test/Transforms/ADCE/debug-info-intrinsic.ll +++ llvm/test/Transforms/ADCE/debug-info-intrinsic.ll @@ -26,6 +26,14 @@ ;; sink(); ;;} +; Note: The instruction +; %dead = add i8 0, 0 +; in +; @variable_in_unused_subscope +; and +; @calls_empty_function_with_unused_variable_in_unused_subscope +; is just there to let ADCE remove debug intrinsics at all. It only removes +; debug intrinsics if it also found something else to remove. declare void @llvm.dbg.value(metadata, metadata, metadata) declare void @sink() @@ -38,6 +46,7 @@ entry: call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !17), !dbg !18 call void @sink(), !dbg !19 + %dead = add i8 0, 0 ret void, !dbg !20 } @@ -61,6 +70,7 @@ entry: call void @llvm.dbg.value(metadata i32 0, metadata !26, metadata !17), !dbg !28 call void @sink(), !dbg !31 + %dead = add i8 0, 0 ret void, !dbg !32 } Index: llvm/test/Transforms/MergeFunc/mergefunc-preserve-debug-info.ll =================================================================== --- llvm/test/Transforms/MergeFunc/mergefunc-preserve-debug-info.ll +++ llvm/test/Transforms/MergeFunc/mergefunc-preserve-debug-info.ll @@ -147,6 +147,16 @@ ; OPTIMIZATION_LEVEL_2: define void @f() ; OPTIMIZATION_LEVEL_2-NEXT: entry: +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.value(metadata i32 3, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.value(metadata i32 4, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.declare(metadata ptr undef, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.declare(metadata ptr undef, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.value(metadata i32 4, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.value(metadata i32 1, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.value(metadata i32 9, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.declare(metadata ptr undef, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.declare(metadata ptr undef, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} +; OPTIMIZATION_LEVEL_2-NEXT: call void @llvm.dbg.value(metadata i32 9, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}} ; OPTIMIZATION_LEVEL_2-NEXT: ret void, !dbg !{{[0-9]+}} %call = call i32 @maxA(i32 3, i32 4), !dbg !60