Index: lib/IR/DebugInfoMetadata.cpp =================================================================== --- lib/IR/DebugInfoMetadata.cpp +++ lib/IR/DebugInfoMetadata.cpp @@ -803,6 +803,9 @@ DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, SmallVectorImpl &Ops, bool StackValue) { + // If there are no ops to prepend, do not even add the DW_OP_stack_value. + if (Ops.empty()) + StackValue = false; if (Expr) for (auto Op : Expr->expr_ops()) { // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. Index: lib/Transforms/Utils/Local.cpp =================================================================== --- lib/Transforms/Utils/Local.cpp +++ lib/Transforms/Utils/Local.cpp @@ -1605,8 +1605,13 @@ auto doSalvage = [&](DbgInfoIntrinsic *DII, SmallVectorImpl &Ops) { auto *DIExpr = DII->getExpression(); - DIExpr = - DIExpression::prependOpcodes(DIExpr, Ops, DIExpression::WithStackValue); + if (!Ops.empty()) { + // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they + // are implicitly pointing out the value as a DWARF memory location + // description. + bool WithStackValue = isa(DII); + DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue); + } DII->setOperand(0, wrapMD(I.getOperand(0))); DII->setOperand(2, MetadataAsValue::get(I.getContext(), DIExpr)); LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); Index: test/Transforms/InstCombine/salvage-dbg-declare.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/salvage-dbg-declare.ll @@ -0,0 +1,49 @@ +; RUN: opt -instcombine -S -o - %s | FileCheck %s + +declare dso_local i32 @bar(i8*) + +; Function Attrs: nounwind +define internal i32 @foo() #0 !dbg !1 { +; CHECK: %[[VLA:.*]] = alloca [2 x i32] +; CHECK: call void @llvm.dbg.declare(metadata [2 x i32]* %[[VLA]], {{.*}}, metadata !DIExpression()) + +entry: + %vla = alloca i32, i64 2, align 4, !dbg !16 + call void @llvm.dbg.declare(metadata i32* %vla, metadata !19, metadata !DIExpression()), !dbg !20 + %0 = bitcast i32* %vla to i8*, !dbg !21 + %call = call i32 @bar(i8* %0), !dbg !22 + unreachable +} + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind } +attributes #1 = { nounwind readnone speculatable } + +!llvm.dbg.cu = !{!5} +!llvm.module.flags = !{!0} + +!0 = !{i32 2, !"Debug Info Version", i32 3} +!1 = distinct !DISubprogram(name: "a", scope: !2, file: !2, line: 232, type: !3, isLocal: true, isDefinition: true, scopeLine: 234, flags: DIFlagPrototyped, isOptimized: true, unit: !5, retainedNodes: !6) +!2 = !DIFile(filename: "b", directory: "c") +!3 = !DISubroutineType(types: !4) +!4 = !{} +!5 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang version", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !4, globals: !4) +!6 = !{!7, !11} +!7 = !DILocalVariable(name: "__vla_expr", scope: !8, type: !10, flags: DIFlagArtificial) +!8 = distinct !DILexicalBlock(scope: !9, file: !2, line: 238, column: 39) +!9 = distinct !DILexicalBlock(scope: !1, file: !2, line: 238, column: 6) +!10 = !DIBasicType(name: "long unsigned int", size: 64, encoding: DW_ATE_unsigned) +!11 = !DILocalVariable(name: "ptr32", scope: !8, file: !2, line: 240, type: !12) +!12 = !DICompositeType(tag: DW_TAG_array_type, baseType: !13, elements: !14) +!13 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!14 = !{!15} +!15 = !DISubrange(count: !7) +!16 = !DILocation(line: 240, column: 3, scope: !17) +!17 = distinct !DILexicalBlock(scope: !18, file: !2, line: 238, column: 39) +!18 = distinct !DILexicalBlock(scope: !1, file: !2, line: 238, column: 6) +!19 = !DILocalVariable(name: "ptr32", scope: !17, file: !2, line: 240, type: !12) +!20 = !DILocation(line: 240, column: 12, scope: !17) +!21 = !DILocation(line: 241, column: 65, scope: !17) +!22 = !DILocation(line: 241, column: 11, scope: !17) Index: test/Transforms/LICM/sinking.ll =================================================================== --- test/Transforms/LICM/sinking.ll +++ test/Transforms/LICM/sinking.ll @@ -243,7 +243,8 @@ define void @test11() { br label %Loop Loop: - %dead = getelementptr %Ty, %Ty* @X2, i64 0, i32 0 + %dead1 = getelementptr %Ty, %Ty* @X2, i64 0, i32 0 + %dead2 = getelementptr %Ty, %Ty* @X2, i64 0, i32 1 br i1 false, label %Loop, label %Out Out: ret void @@ -251,8 +252,14 @@ ; CHECK: Out: ; CHECK-NEXT: ret void +; The GEP in dead1 is adding a zero offset, so the DIExpression can be kept as +; a "register location". +; The GEP in dead2 is adding a 4 bytes to the pointer, so the DIExpression is +; turned into an "implicit location" using DW_OP_stack_value. +; ; DEBUGIFY-LABEL: @test11( -; DEBUGIFY: call void @llvm.dbg.value(metadata %Ty* @X2, metadata {{.*}}, metadata !DIExpression(DW_OP_stack_value)) +; DEBUGIFY: call void @llvm.dbg.value(metadata %Ty* @X2, metadata {{.*}}, metadata !DIExpression()) +; DEBUGIFY: call void @llvm.dbg.value(metadata %Ty* @X2, metadata {{.*}}, metadata !DIExpression(DW_OP_plus_uconst, 4, DW_OP_stack_value)) } @c = common global [1 x i32] zeroinitializer, align 4