Index: llvm/lib/CodeGen/LiveDebugValues.cpp =================================================================== --- llvm/lib/CodeGen/LiveDebugValues.cpp +++ llvm/lib/CodeGen/LiveDebugValues.cpp @@ -691,9 +691,17 @@ "No register supplied when handling a restore of a debug value"); MachineFunction *MF = MI.getMF(); DIBuilder DIB(*const_cast(MF->getFunction()).getParent()); + + const DIExpression *NewExpr; + if (auto Fragment = DebugInstr->getDebugExpression()->getFragmentInfo()) + NewExpr = *DIExpression::createFragmentExpression(DIB.createExpression(), + Fragment->OffsetInBits, Fragment->SizeInBits); + else + NewExpr = DIB.createExpression(); + NewDebugInstr = BuildMI(*MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), false, - NewReg, DebugInstr->getDebugVariable(), DIB.createExpression()); + NewReg, DebugInstr->getDebugVariable(), NewExpr); VarLoc VL(*NewDebugInstr, LS); ProcessVarLoc(VL, NewDebugInstr); LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register restore: "; @@ -848,9 +856,14 @@ << "\n"); } // Check if the register or spill location is the location of a debug value. + // FIXME: Don't create a spill transfer if there is a complex expression, + // because we currently cannot recover the original expression on restore. for (unsigned ID : OpenRanges.getVarLocs()) { + const MachineInstr *DebugInstr = &VarLocIDs[ID].MI; + if (TKind == TransferKind::TransferSpill && - VarLocIDs[ID].isDescribedByReg() == Reg) { + VarLocIDs[ID].isDescribedByReg() == Reg && + !DebugInstr->getDebugExpression()->isComplex()) { LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '(' << VarLocIDs[ID].Var.getVar()->getName() << ")\n"); } else if (TKind == TransferKind::TransferRestore && Index: llvm/test/DebugInfo/MIR/X86/live-debug-values-restore.mir =================================================================== --- llvm/test/DebugInfo/MIR/X86/live-debug-values-restore.mir +++ llvm/test/DebugInfo/MIR/X86/live-debug-values-restore.mir @@ -22,6 +22,14 @@ # CHECK: $rdi = MOV64rm $rsp,{{.*-8.*}}:: (load 8 from %stack.0) # CHECK-NEXT: DBG_VALUE $rdi,{{.*}}![[MDIX]], !DIExpression() +# A second function has been appended as a regression test against a +# crash, caused by expressions being created from spill restores that did +# not preserve fragment information. Test that no empty expressions are +# created at all. + +# CHECK-LABEL: name: g +# CHECK-NOT: !DIExpression() + --- | define dso_local i32 @f(i32* readonly %p) local_unnamed_addr !dbg !7 { entry: @@ -39,6 +47,22 @@ ret i32 %0, !dbg !28 } + define dso_local i32 @g(i32* readonly %p) local_unnamed_addr !dbg !107 { + entry: + call void @llvm.dbg.value(metadata i32* %p, metadata !113, metadata !DIExpression()), !dbg !114 + %tobool = icmp eq i32* %p, null, !dbg !115 + br i1 %tobool, label %if.end, label %if.then, !dbg !117 + + if.then: ; preds = %entry + tail call void asm sideeffect "", "~{rax},~{rbx},~{rcx},~{rdx},~{rsi},~{rdi},~{rbp},~{r8},~{r9},~{r10},~{r11},~{r12},~{r13},~{r14},~{r15},~{dirflag},~{fpsr},~{flags}"(), !dbg !118, !srcloc !120 + br label %if.end, !dbg !121 + + if.end: ; preds = %entry, %if.then + %add.ptr = getelementptr inbounds i32, i32* %p, i64 1, !dbg !122 + %0 = load i32, i32* %add.ptr, align 4, !dbg !123, !tbaa !24 + ret i32 %0, !dbg !128 + } + declare void @llvm.dbg.value(metadata, metadata, metadata) !llvm.dbg.cu = !{!0} @@ -74,6 +98,22 @@ !26 = !{!"omnipotent char", !27, i64 0} !27 = !{!"Simple C/C++ TBAA"} !28 = !DILocation(line: 9, column: 3, scope: !7) + !101 = !DIBasicType(name: "looong int", size: 64, encoding: DW_ATE_signed) + !107 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 105, type: !8, scopeLine: 105, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !112) + !112 = !{!113} + !113 = !DILocalVariable(name: "q", arg: 1, scope: !107, file: !1, line: 105, type: !101) + !114 = !DILocation(line: 105, column: 12, scope: !107) + !115 = !DILocation(line: 106, column: 7, scope: !116) + !116 = distinct !DILexicalBlock(scope: !107, file: !1, line: 106, column: 7) + !117 = !DILocation(line: 106, column: 7, scope: !107) + !118 = !DILocation(line: 107, column: 5, scope: !119) + !119 = distinct !DILexicalBlock(scope: !116, file: !1, line: 106, column: 10) + !120 = !{i32 -2147471544} + !121 = !DILocation(line: 108, column: 3, scope: !119) + !122 = !DILocation(line: 109, column: 14, scope: !107) + !123 = !DILocation(line: 109, column: 10, scope: !107) + !128 = !DILocation(line: 109, column: 3, scope: !107) + ... --- @@ -187,3 +227,67 @@ RETQ $eax, debug-location !28 ... +--- +name: g +alignment: 4 +tracksRegLiveness: true +liveins: + - { reg: '$rdi', virtual-reg: '' } +frameInfo: + stackSize: 48 + offsetAdjustment: -48 + maxAlignment: 8 + cvBytesOfCalleeSavedRegisters: 48 + localFrameSize: 0 +fixedStack: + - { id: 0, type: spill-slot, offset: -56, size: 8, alignment: 8, stack-id: default, + callee-saved-register: '$rbx', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } + - { id: 1, type: spill-slot, offset: -48, size: 8, alignment: 16, stack-id: default, + callee-saved-register: '$r12', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } + - { id: 2, type: spill-slot, offset: -40, size: 8, alignment: 8, stack-id: default, + callee-saved-register: '$r13', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } + - { id: 3, type: spill-slot, offset: -32, size: 8, alignment: 16, stack-id: default, + callee-saved-register: '$r14', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } + - { id: 4, type: spill-slot, offset: -24, size: 8, alignment: 8, stack-id: default, + callee-saved-register: '$r15', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } + - { id: 5, type: spill-slot, offset: -16, size: 8, alignment: 16, stack-id: default, + callee-saved-register: '$rbp', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } +stack: + - { id: 0, name: '', type: spill-slot, offset: -64, size: 8, alignment: 8, + stack-id: default, callee-saved-register: '', callee-saved-restored: true, + debug-info-variable: '', debug-info-expression: '', debug-info-location: '' } +constants: [] +body: | + bb.0.entry: + successors: %bb.1(0x50000000) + liveins: $rdi, $rbx, $r12, $r13, $r14, $r15, $rbp + + DBG_VALUE $rdi, $noreg, !113, !DIExpression(DW_OP_LLVM_fragment, 0, 32), debug-location !114 + TEST64rr renamable $rdi, renamable $rdi, implicit-def $eflags, debug-location !115 + JMP_1 %bb.1, implicit $eflags, debug-location !117 + + bb.1.if.then: + successors: %bb.2(0x80000000) + liveins: $rdi, $rbp, $r15, $r14, $r13, $r12, $rbx + + MOV64mr $rsp, 1, $noreg, -8, $noreg, killed renamable $rdi :: (store 8 into %stack.0) + renamable $rdi = MOV64rm $rsp, 1, $noreg, -8, $noreg :: (load 8 from %stack.0) + + bb.2.if.end: + liveins: $rdi, $rbx, $r12, $r13, $r14, $r15, $rbp + + DBG_VALUE $rbx, $noreg, !113, !DIExpression(DW_OP_LLVM_fragment, 32, 32), debug-location !114 + MOV64mr $rsp, 1, $noreg, -8, $noreg, killed renamable $rbx :: (store 8 into %stack.0) + renamable $rsi = MOV64rm $rsp, 1, $noreg, -8, $noreg :: (load 8 from %stack.0) + + renamable $eax = MOV32rm killed renamable $rsi, 1, $noreg, 4, $noreg, debug-location !123 :: (load 4 from %ir.add.ptr, !tbaa !24) + $rdi = MOV64ri 0 + RETQ $eax, debug-location !128 + +...