Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -7116,28 +7116,30 @@ return MadeChange; } -// llvm.dbg.value is far away from the value then iSel may not be able -// handle it properly. iSel will drop llvm.dbg.value if it can not -// find a node corresponding to the value. +// A llvm.dbg.value may be using a value before its definition, due to +// optimizations in this pass and others. Scan for such dbg.values, and rescue +// them by moving the dbg.value to immediately after the value definition. +// FIXME: Ideally this should never be necessary, and this has the potential +// to re-order dbg.value intrinsics. bool CodeGenPrepare::placeDbgValues(Function &F) { bool MadeChange = false; + DominatorTree DT(F); + for (BasicBlock &BB : F) { - Instruction *PrevNonDbgInst = nullptr; for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { Instruction *Insn = &*BI++; DbgValueInst *DVI = dyn_cast(Insn); - // Leave dbg.values that refer to an alloca alone. These - // intrinsics describe the address of a variable (= the alloca) - // being taken. They should not be moved next to the alloca - // (and to the beginning of the scope), but rather stay close to - // where said address is used. - if (!DVI || (DVI->getValue() && isa(DVI->getValue()))) { - PrevNonDbgInst = Insn; + if (!DVI) continue; - } Instruction *VI = dyn_cast_or_null(DVI->getValue()); - if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) { + + // If the defining instruction dominates the dbg.value, we do not need + // to move the dbg.value. + if (VI && DT.dominates(VI, DVI)) + continue; + + if (VI && !VI->isTerminator()) { // If VI is a phi in a block with an EHPad terminator, we can't insert // after it. if (isa(VI) && VI->getParent()->getTerminator()->isEHPad()) Index: test/DebugInfo/COFF/register-variables.ll =================================================================== --- test/DebugInfo/COFF/register-variables.ll +++ test/DebugInfo/COFF/register-variables.ll @@ -32,9 +32,6 @@ ; ASM: #DEBUG_VALUE: f:p <- $esi ; ASM: callq getint ; ASM: [[after_getint:\.Ltmp.*]]: -; ASM: #DEBUG_VALUE: a <- $eax -; ASM: #DEBUG_VALUE: inlineinc:a <- $eax -; ASM: #DEBUG_VALUE: c <- $eax ; ASM: testl %esi, %esi ; ASM: je .LBB0_2 ; ASM: [[after_je:\.Ltmp.*]]: @@ -45,16 +42,17 @@ ; ASM: addl $1, %eax ; ASM: [[after_inc_eax:\.Ltmp.*]]: ; ASM: #DEBUG_VALUE: inlineinc:b <- $eax -; ASM: #DEBUG_VALUE: b <- $eax ; ASM: addl $1, x(%rip) ; ASM: [[after_if:\.Ltmp.*]]: ; ASM: .LBB0_2: # %if.else ; ASM: #DEBUG_VALUE: f:p <- $esi +; ASM: #DEBUG_VALUE: c <- $eax ; ASM: movl %eax, %ecx ; ASM: addq $32, %rsp ; ASM: popq %rsi ; ASM: [[func_end:\.Ltmp.*]]: ; ASM: jmp putint # TAILCALL +; ASM: [[func_finished:\.Ltmp.*]]: ; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "p" @@ -62,18 +60,15 @@ ; ASM: .cv_def_range [[p_ecx_esi]] [[func_end]], "A\021\027\000\000\000" ; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "c" -; ASM: .cv_def_range [[after_getint]] [[after_je]], "A\021\021\000\000\000" +; ASM: .cv_def_range [[after_if]] [[func_finished]], "A\021\021\000\000\000" ; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "a" -; ASM: .cv_def_range [[after_getint]] [[after_inc_eax]], "A\021\021\000\000\000" -; ASM: .short 4414 # Record kind: S_LOCAL -; ASM: .asciz "b" -; ASM: .cv_def_range [[after_inc_eax]] [[after_if]], "A\021\021\000\000\000" +; ASM: .cv_def_range [[after_je]] [[after_inc_eax]], "A\021\021\000\000\000" ; ASM: .short 4429 # Record kind: S_INLINESITE ; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "a" -; ASM: .cv_def_range [[after_getint]] [[after_inc_eax]], "A\021\021\000\000\000" +; ASM: .cv_def_range [[after_je]] [[after_inc_eax]], "A\021\021\000\000\000" ; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "b" ; ASM: .cv_def_range [[after_inc_eax]] [[after_if]], "A\021\021\000\000\000" @@ -116,9 +111,9 @@ ; OBJ: DefRangeRegisterSym { ; OBJ: Register: EAX (0x11) ; OBJ: LocalVariableAddrRange { -; OBJ: OffsetStart: .text+0xC +; OBJ: OffsetStart: .text+0x1A ; OBJ: ISectStart: 0x0 -; OBJ: Range: 0x4 +; OBJ: Range: 0xC ; OBJ: } ; OBJ: } ; OBJ: LocalSym { @@ -130,23 +125,9 @@ ; OBJ: DefRangeRegisterSym { ; OBJ: Register: EAX (0x11) ; OBJ: LocalVariableAddrRange { -; OBJ: OffsetStart: .text+0xC -; OBJ: ISectStart: 0x0 -; OBJ: Range: 0x7 -; OBJ: } -; OBJ: } -; OBJ: LocalSym { -; OBJ: Type: int (0x74) -; OBJ: Flags [ (0x0) -; OBJ: ] -; OBJ: VarName: b -; OBJ: } -; OBJ: DefRangeRegisterSym { -; OBJ: Register: EAX (0x11) -; OBJ: MayHaveNoName: 0 -; OBJ: OffsetStart: .text+0x13 +; OBJ: OffsetStart: .text+0x10 ; OBJ: ISectStart: 0x0 -; OBJ: Range: 0x7 +; OBJ: Range: 0x3 ; OBJ: } ; OBJ: } ; OBJ: InlineSiteSym { @@ -164,9 +145,9 @@ ; OBJ: DefRangeRegisterSym { ; OBJ: Register: EAX (0x11) ; OBJ: LocalVariableAddrRange { -; OBJ: OffsetStart: .text+0xC +; OBJ: OffsetStart: .text+0x10 ; OBJ: ISectStart: 0x0 -; OBJ: Range: 0x7 +; OBJ: Range: 0x3 ; OBJ: } ; OBJ: } ; OBJ: LocalSym { Index: test/DebugInfo/NVPTX/debug-info.ll =================================================================== --- test/DebugInfo/NVPTX/debug-info.ll +++ test/DebugInfo/NVPTX/debug-info.ll @@ -8381,8 +8381,8 @@ ; CHECK-NEXT: .b8 6 // DW_AT_call_line ; CHECK-NEXT: .b8 43 // Abbrev [43] 0x270e:0x22 DW_TAG_inlined_subroutine ; CHECK-NEXT: .b32 9791 // DW_AT_abstract_origin -; CHECK-NEXT: .b64 Ltmp10 // DW_AT_low_pc -; CHECK-NEXT: .b64 Ltmp11 // DW_AT_high_pc +; CHECK-NEXT: .b64 Ltmp9 // DW_AT_low_pc +; CHECK-NEXT: .b64 Ltmp10 // DW_AT_high_pc ; CHECK-NEXT: .b8 12 // DW_AT_call_file ; CHECK-NEXT: .b8 8 // DW_AT_call_line ; CHECK-NEXT: .b8 44 // Abbrev [44] 0x2725:0x5 DW_TAG_formal_parameter Index: test/DebugInfo/X86/PR37234.ll =================================================================== --- test/DebugInfo/X86/PR37234.ll +++ test/DebugInfo/X86/PR37234.ll @@ -20,9 +20,8 @@ ; CHECK-LABEL: # %bb.{{.*}}: ; CHECK: #DEBUG_VALUE: main:aa <- 0 -; CHECK: #DEBUG_VALUE: main:aa <- $[[REG:[0-9a-z]+]] ; CHECK: .LBB0_1: -; CHECK: #DEBUG_VALUE: main:aa <- $[[REG]] +; CHECK: #DEBUG_VALUE: main:aa <- $[[REG:[0-9a-z]+]] ; CHECK: je .LBB0_4 ; CHECK: # %bb.{{.*}}: ; CHECK: #DEBUG_VALUE: main:aa <- $[[REG]]