Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -4941,6 +4941,8 @@ if (!Arg) return false; + // FIXME: this function may hoist inlined function arguments across blocks. + MachineFunction &MF = DAG.getMachineFunction(); const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo(); @@ -5348,10 +5350,16 @@ return nullptr; } - // The value is not used in this block yet (or it would have an SDNode). - // We still want the value to appear for the user if possible -- if it has - // an associated VReg, we can refer to that instead. - if (!isa(V)) { + // Special rules apply for the first dbg.values of parameter variables in a + // function. Identify them by the fact they reference Argument Values, that + // they're parameters, and they are parameters of the current function. We + // need to let them dangle until they get an SDNode. + bool isParamOfFunc = isa(V) && Variable->isParameter() && + Variable->getScope() == FuncInfo.Fn->getSubprogram(); + if (!isParamOfFunc) { + // The value is not used in this block yet (or it would have an SDNode). + // We still want the value to appear for the user if possible -- if it has + // an associated VReg, we can refer to that instead. auto VMI = FuncInfo.ValueMap.find(V); if (VMI != FuncInfo.ValueMap.end()) { unsigned Reg = VMI->second; Index: test/DebugInfo/Sparc/subreg.ll =================================================================== --- test/DebugInfo/Sparc/subreg.ll +++ test/DebugInfo/Sparc/subreg.ll @@ -26,7 +26,7 @@ !8 = !DISubroutineType(types: !9) !9 = !{null, !10} !10 = !DIBasicType(name: "long double", size: 128, encoding: DW_ATE_float) -!13 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 1, type: !14) +!13 = !DILocalVariable(name: "a", scope: !7, arg: 1, file: !1, line: 1, type: !14) !14 = !DIBasicType(name: "complex", size: 256, encoding: DW_ATE_complex_float) !17 = !DILocation(line: 1, column: 48, scope: !7) !18 = !DIExpression(DW_OP_LLVM_fragment, 0, 128) Index: test/DebugInfo/X86/dbg-value-arg-movement.ll =================================================================== --- /dev/null +++ test/DebugInfo/X86/dbg-value-arg-movement.ll @@ -0,0 +1,121 @@ +; RUN: llc -start-after=codegenprepare -stop-before=expand-isel-pseudos %s -o - | FileCheck %s + +; Test the movement of dbg.values of arguments. SelectionDAG tries to be +; helpful and places DBG_VALUEs of Arguments at the start of functions. +; Unfortunately, this doesn't necessarily make sense, as one can specify an +; Argument IR Value as a variable location anywhere in the program. +; +; Distinguish cases where we want to hoist DBG_VALUEs, and those where we +; don't, by whether the referred to variable is a parameter to the current +; function. In the test below, 'xyzzy' is a parameter to an inlined function, +; but should not be hoisted to the start of the function. +; +; Original test case, in which 'xyzzy' became unavailable because its DBG_VALUE +; landed far from any uses, compiled "clang -O2 -g" with inlining, +; +; int ext(void); +; +; int +; foo(int xyzzy) +; { +; xyzzy = ext() * xyzzy; +; xyzzy += 1; +; ext(); +; return xyzzy; +; } +; +; int +; bar(int baz, int qux) +; { +; int fish; +; switch (qux) { +; case 12: fish = 8; break; +; case 848: fish = 0; break; +; case 99999: fish = -1; break; +; default: fish = 12; +; } +; qux %= fish; +; qux += foo(baz); +; return qux; +; } + +; CHECK: [[BAZVAR:![0-9]+]] = !DILocalVariable(name: "baz", +; CHECK: [[XYZVAR:![0-9]+]] = !DILocalVariable(name: "xyzzy", + +; Start of MIR function block, +; CHECK-LABEL: body +; Expect DBG_VALUE of physreg, +; CHECK: DBG_VALUE $edi, $noreg, [[BAZVAR]] +; Expect DBG_VALUE of virtreg, +; CHECK: DBG_VALUE [[ARGREG:%[0-9]+]], $noreg, [[BAZVAR]] +; Label for next block, +; CHECK-LABEL: bb.1.next +; Correctly place dbg.value in the 'next' block. +; CHECK: DBG_VALUE [[ARGREG]], $noreg, [[XYZVAR]] + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Function Attrs: noinline norecurse nounwind readonly uwtable +declare i32 @ext(); + +; Function Attrs: norecurse nounwind readonly uwtable +define dso_local i32 @bar(i32, i32) local_unnamed_addr !dbg !30 { + %3 = srem i32 %1, %0, !dbg !46 + call void @llvm.dbg.value(metadata i32 %0, metadata !34, metadata !DIExpression()), !dbg !37 + br label %next + +next: + call void @llvm.dbg.value(metadata i32 %0, metadata !24, metadata !DIExpression()), !dbg !47 + %4 = tail call i32 @ext(), !dbg !49 + %5 = mul nsw i32 %3, %4, !dbg !53 + %6 = add i32 %5, %0, !dbg !53 + ret i32 %6, !dbg !53 +} + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!7, !8, !9} +!llvm.ident = !{!10} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "lala", scope: !2, file: !3, line: 2, type: !6, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 8.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, nameTableKind: None) +!3 = !DIFile(filename: "test.c", directory: "/home/jmorse") +!4 = !{} +!5 = !{!0} +!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!7 = !{i32 2, !"Dwarf Version", i32 4} +!8 = !{i32 2, !"Debug Info Version", i32 3} +!9 = !{i32 1, !"wchar_size", i32 4} +!10 = !{!"clang version 8.0.0"} +!11 = distinct !DISubprogram(name: "ext", scope: !3, file: !3, line: 3, type: !12, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4) +!12 = !DISubroutineType(types: !13) +!13 = !{!6} +!14 = !DILocation(line: 6, column: 10, scope: !11) +!15 = !{!16, !16, i64 0} +!16 = !{!"int", !17, i64 0} +!17 = !{!"omnipotent char", !18, i64 0} +!18 = !{!"Simple C/C++ TBAA"} +!19 = !DILocation(line: 6, column: 3, scope: !11) +!20 = distinct !DISubprogram(name: "foo", scope: !3, file: !3, line: 10, type: !21, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !23) +!21 = !DISubroutineType(types: !22) +!22 = !{!6, !6} +!23 = !{!24} +!24 = !DILocalVariable(name: "xyzzy", arg: 1, scope: !20, file: !3, line: 10, type: !6) +!30 = distinct !DISubprogram(name: "bar", scope: !3, file: !3, line: 19, type: !31, scopeLine: 20, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !33) +!31 = !DISubroutineType(types: !32) +!32 = !{!6, !6, !6} +!33 = !{!34, !35, !36} +!34 = !DILocalVariable(name: "baz", arg: 1, scope: !30, file: !3, line: 19, type: !6) +!35 = !DILocalVariable(name: "qux", arg: 2, scope: !30, file: !3, line: 19, type: !6) +!36 = !DILocalVariable(name: "fish", scope: !30, file: !3, line: 21, type: !6) +!37 = !DILocation(line: 19, column: 9, scope: !30) +!42 = distinct !DILexicalBlock(scope: !30, file: !3, line: 22, column: 16) +!46 = !DILocation(line: 35, column: 7, scope: !30) +!47 = !DILocation(line: 10, column: 9, scope: !20, inlinedAt: !48) +!48 = distinct !DILocation(line: 36, column: 10, scope: !30) +!49 = !DILocation(line: 12, column: 11, scope: !20, inlinedAt: !48) +!53 = !DILocation(line: 37, column: 3, scope: !30)