diff --git a/lib/IR/TypeFinder.cpp b/lib/IR/TypeFinder.cpp index 403ae45756a..724b8f6b6ad 100644 --- a/lib/IR/TypeFinder.cpp +++ b/lib/IR/TypeFinder.cpp @@ -14,6 +14,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constant.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/Instruction.h" @@ -152,6 +153,14 @@ void TypeFinder::incorporateMDNode(const MDNode *V) { if (!VisitedMetadata.insert(V).second) return; + // The arguments in DIArgList are not exposed as operands, so handle such + // nodes specifically here. + if (const auto *AL = dyn_cast(V)) { + for (auto *Arg : AL->getArgs()) + incorporateValue(Arg->getValue()); + return; + } + // Look in operands for types. for (Metadata *Op : V->operands()) { if (!Op) diff --git a/test/Transforms/GlobalOpt/deadglobal-diarglist-use.ll b/test/Transforms/GlobalOpt/deadglobal-diarglist-use.ll new file mode 100644 index 00000000000..b6b64a524c9 --- /dev/null +++ b/test/Transforms/GlobalOpt/deadglobal-diarglist-use.ll @@ -0,0 +1,59 @@ +; RUN: opt < %s -globalopt -S | llvm-as | llvm-dis | FileCheck %s + +; The %struct.S type would not get emitted after @s was removed, resulting in +; llvm-as failing to parse the dbg.value intrinsic using that type. The +; FileCheck checks are just added to verify that the desired transformation is +; done. + +; CHECK-NOT: @s +; CHECK: %struct.S = type { i32 } +; CHECK-NOT: @s + +; CHECK: call void @llvm.dbg.value(metadata !DIArgList([1 x %struct.S]* undef + +%struct.S = type { i32 } + +@s = internal global [1 x %struct.S] zeroinitializer, align 4, !dbg !0 +@idx = dso_local global i32 0, align 4, !dbg !6 + +define dso_local void @fn() !dbg !20 { +entry: + %0 = load i32, i32* @idx, align 4, !dbg !26 + %idxprom = sext i32 %0 to i64, !dbg !26 + call void @llvm.dbg.value(metadata !DIArgList([1 x %struct.S]* @s, i64 %idxprom), metadata !24, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 4, DW_OP_mul, DW_OP_plus, DW_OP_stack_value)), !dbg !26 + ret void, !dbg !26 +} + +declare void @llvm.dbg.value(metadata, metadata, metadata) + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!15, !16, !17, !18} +!llvm.ident = !{!19} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "s", scope: !2, file: !3, line: 1, type: !9, isLocal: true, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 13.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "foo.c", directory: "/") +!4 = !{} +!5 = !{!0, !6} +!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression()) +!7 = distinct !DIGlobalVariable(name: "idx", scope: !2, file: !3, line: 2, type: !8, isLocal: false, isDefinition: true) +!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!9 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 32, elements: !13) +!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S", file: !3, line: 1, size: 32, elements: !11) +!11 = !{!12} +!12 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !10, file: !3, line: 1, baseType: !8, size: 32) +!13 = !{!14} +!14 = !DISubrange(count: 1) +!15 = !{i32 7, !"Dwarf Version", i32 4} +!16 = !{i32 2, !"Debug Info Version", i32 3} +!17 = !{i32 1, !"wchar_size", i32 4} +!18 = !{i32 7, !"uwtable", i32 1} +!19 = !{!"clang version 13.0.0"} +!20 = distinct !DISubprogram(name: "fn", scope: !3, file: !3, line: 4, type: !21, scopeLine: 4, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !23) +!21 = !DISubroutineType(types: !22) +!22 = !{null} +!23 = !{!24} +!24 = !DILocalVariable(name: "local", scope: !20, file: !3, line: 5, type: !25) +!25 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!26 = !DILocation(line: 5, scope: !20)