From 4a39ef301ec40e4ee6182868df37ee2b379ad289 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Tue, 24 Oct 2017 22:55:12 +0000 Subject: [PATCH] Implement salavageDebugInfo functionality for SelectionDAG. Similar to how llvm::salvagDebugInfo hooks into InstCombine, this adds a hook that can be invoked before an SDNode that is associated with an SDDbgValue is erased to capture the effect of the deleted node in a DIExpression. The motivating example is an SDDebugValue attached to an ADD operation that gets folded into a LOAD+OFFSET operation. rdar://problem/32121503 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316525 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/SelectionDAG.h | 4 + lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 34 ++++++ lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 1 + test/DebugInfo/X86/sdag-salvage-add.ll | 110 ++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 test/DebugInfo/X86/sdag-salvage-add.ll diff --git a/include/llvm/CodeGen/SelectionDAG.h b/include/llvm/CodeGen/SelectionDAG.h index 61ae1c91073..460e58c9dea 100644 --- a/include/llvm/CodeGen/SelectionDAG.h +++ b/include/llvm/CodeGen/SelectionDAG.h @@ -1286,6 +1286,10 @@ public: return DbgInfo->ByvalParmDbgEnd(); } + /// To be invoked on an SDNode that is slated to be erased. This + /// function mirrors \c llvm::salvageDebugInfo. + void salvageDebugInfo(SDNode &N); + void dump() const; /// Create a stack temporary, suitable for holding the specified value type. diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index f712862f184..dd5e1e5a3ee 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -6978,6 +6978,40 @@ SDDbgValue *SelectionDAG::getFrameIndexDbgValue(DIVariable *Var, return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, FI, DL, O); } +void SelectionDAG::salvageDebugInfo(SDNode &N) { + if (!N.getHasDebugValue()) + return; + for (auto DV : GetDbgValues(&N)) { + if (DV->isInvalidated()) + continue; + switch (N.getOpcode()) { + default: + break; + case ISD::ADD: + SDValue N0 = N.getOperand(0); + SDValue N1 = N.getOperand(1); + if (!isConstantIntBuildVectorOrConstantInt(N0) && + isConstantIntBuildVectorOrConstantInt(N1)) { + uint64_t Offset = N.getConstantOperandVal(1); + // Rewrite an ADD constant node into a DIExpression. Since we are + // performing arithmetic to compute the variable's *value* in the + // DIExpression, we need to mark the expression with a + // DW_OP_stack_value. + auto *DIExpr = DV->getExpression(); + DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset, + DIExpression::WithStackValue); + SDDbgValue *Clone = + getDbgValue(DV->getVariable(), DIExpr, N0.getNode(), N0.getResNo(), + DV->isIndirect(), DV->getDebugLoc(), DV->getOrder()); + DV->setIsInvalidated(); + AddDbgValue(Clone, N0.getNode(), false); + DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this); + dbgs() << " into " << *DIExpr << '\n'); + } + } + } +} + namespace { /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 5311ef437e9..4c4d196427e 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -3550,6 +3550,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch, "NodeToMatch was removed partway through selection"); SelectionDAG::DAGNodeDeletedListener NDL(*CurDAG, [&](SDNode *N, SDNode *E) { + CurDAG->salvageDebugInfo(*N); auto &Chain = ChainNodesMatched; assert((!E || !is_contained(Chain, N)) && "Chain node replaced during MorphNode"); diff --git a/test/DebugInfo/X86/sdag-salvage-add.ll b/test/DebugInfo/X86/sdag-salvage-add.ll new file mode 100644 index 00000000000..827aed6662f --- /dev/null +++ b/test/DebugInfo/X86/sdag-salvage-add.ll @@ -0,0 +1,110 @@ +; RUN: llc -mtriple=x86_64-unknown-unknown -stop-before livedebugvalues %s -o - \ +; RUN: | FileCheck %s +; +; Generated at -O1 from: +; typedef struct { +; unsigned long long c; +; } S1; +; struct S3 { +; unsigned long long packed; +; }; +; struct S6 { +; struct S0 *b; +; }; +; void f(struct S3 *a3) +; { +; struct S4 *s4 = (struct S4 *)(a3->packed + 0x1000UL); +; struct S6 *myVar = (struct S6 *)s4; +; struct S0 *b = myVar->b; +; use(b); +; } +; +; The debug info is attached to the ADD 4096 operation, which doesn't survive +; instruction selection as it is folded into the load. +; +; CHECK: ![[S4:.*]] = !DILocalVariable(name: "s4", +; CHECK: ![[MYVAR:.*]] = !DILocalVariable(name: "myVar", +; CHECK: DBG_VALUE debug-use %rax, debug-use _, ![[MYVAR]], +; CHECK-SAME: !DIExpression(DW_OP_plus_uconst, 4096, DW_OP_stack_value) +; CHECK-NEXT: DBG_VALUE debug-use %rax, debug-use _, ![[S4]], +; CHECK-SAME: !DIExpression(DW_OP_plus_uconst, 4096, DW_OP_stack_value) +; CHECK-NEXT: %rdi = MOV64rm killed %rax, 1, _, 4096, _, + +source_filename = "test.c" +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.13.0" + +%struct.S3 = type { i64 } +%struct.S4 = type opaque +%struct.S0 = type opaque + +; Function Attrs: noinline nounwind ssp uwtable +define void @f(%struct.S3* nocapture readonly %a3) local_unnamed_addr #0 !dbg !6 { +entry: + tail call void @llvm.dbg.value(metadata %struct.S3* %a3, metadata !15, metadata !DIExpression()), !dbg !30 + %packed = getelementptr inbounds %struct.S3, %struct.S3* %a3, i64 0, i32 0, !dbg !31 + %0 = load i64, i64* %packed, align 8, !dbg !31 + %add = add i64 %0, 4096, !dbg !37 + %1 = inttoptr i64 %add to %struct.S4*, !dbg !38 + tail call void @llvm.dbg.value(metadata %struct.S4* %1, metadata !16, metadata !DIExpression()), !dbg !39 + tail call void @llvm.dbg.value(metadata %struct.S4* %1, metadata !17, metadata !DIExpression()), !dbg !40 + %b1 = bitcast %struct.S4* %1 to %struct.S0**, !dbg !41 + %2 = load %struct.S0*, %struct.S0** %b1, align 8, !dbg !41 + tail call void @llvm.dbg.value(metadata %struct.S0* %2, metadata !24, metadata !DIExpression()), !dbg !45 + %call = tail call i32 (%struct.S0*, ...) bitcast (i32 (...)* @use to i32 (%struct.S0*, ...)*)(%struct.S0* %2) #3, !dbg !46 + ret void, !dbg !47 +} + +declare i32 @use(...) local_unnamed_addr + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) #2 + +attributes #0 = { noinline nounwind ssp uwtable } +attributes #2 = { nounwind readnone speculatable } +attributes #3 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!25, !26, !27, !28} +!llvm.ident = !{!29} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 (trunk 316467) (llvm/trunk 316466)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3) +!1 = !DIFile(filename: "test.c", directory: "/") +!2 = !{} +!3 = !{!4, !18} +!4 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64) +!5 = !DICompositeType(tag: DW_TAG_structure_type, name: "S4", scope: !6, file: !1, line: 20, flags: DIFlagFwdDecl) +!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 18, type: !7, isLocal: false, isDefinition: true, scopeLine: 19, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !14) +!7 = !DISubroutineType(types: !8) +!8 = !{null, !9} +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S3", file: !1, line: 5, size: 64, elements: !11) +!11 = !{!12} +!12 = !DIDerivedType(tag: DW_TAG_member, name: "packed", scope: !10, file: !1, line: 6, baseType: !13, size: 64) +!13 = !DIBasicType(name: "long long unsigned int", size: 64, encoding: DW_ATE_unsigned) +!14 = !{!15, !16, !17, !24} +!15 = !DILocalVariable(name: "a3", arg: 1, scope: !6, file: !1, line: 18, type: !9) +!16 = !DILocalVariable(name: "s4", scope: !6, file: !1, line: 20, type: !4) +!17 = !DILocalVariable(name: "myVar", scope: !6, file: !1, line: 21, type: !18) +!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !19, size: 64) +!19 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S6", file: !1, line: 8, size: 64, elements: !20) +!20 = !{!21} +!21 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !19, file: !1, line: 9, baseType: !22, size: 64) +!22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !23, size: 64) +!23 = !DICompositeType(tag: DW_TAG_structure_type, name: "S0", file: !1, line: 4, flags: DIFlagFwdDecl) +!24 = !DILocalVariable(name: "b", scope: !6, file: !1, line: 22, type: !22) +!25 = !{i32 2, !"Dwarf Version", i32 4} +!26 = !{i32 2, !"Debug Info Version", i32 3} +!27 = !{i32 1, !"wchar_size", i32 4} +!28 = !{i32 7, !"PIC Level", i32 2} +!29 = !{!"clang version 6.0.0 (trunk 316467) (llvm/trunk 316466)"} +!30 = !DILocation(line: 18, column: 14, scope: !6) +!31 = !DILocation(line: 20, column: 37, scope: !6) +!37 = !DILocation(line: 20, column: 44, scope: !6) +!38 = !DILocation(line: 20, column: 19, scope: !6) +!39 = !DILocation(line: 20, column: 14, scope: !6) +!40 = !DILocation(line: 21, column: 14, scope: !6) +!41 = !DILocation(line: 22, column: 25, scope: !6) +!45 = !DILocation(line: 22, column: 14, scope: !6) +!46 = !DILocation(line: 23, column: 3, scope: !6) +!47 = !DILocation(line: 24, column: 1, scope: !6) -- 2.40.0