Index: lib/CodeGen/LiveDebugValues.cpp =================================================================== --- lib/CodeGen/LiveDebugValues.cpp +++ lib/CodeGen/LiveDebugValues.cpp @@ -38,6 +38,7 @@ #include "llvm/CodeGen/TargetFrameLowering.h" #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/CodeGen/TargetLowering.h" +#include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/CodeGen/TargetRegisterInfo.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/Config/llvm-config.h" @@ -143,7 +144,8 @@ enum VarLocKind { InvalidKind = 0, RegisterKind, - SpillLocKind + SpillLocKind, + EntryValueKind } Kind = InvalidKind; /// The value location. Stored separately to avoid repeatedly @@ -154,15 +156,16 @@ uint64_t Hash; } Loc; - VarLoc(const MachineInstr &MI, LexicalScopes &LS) + VarLoc(const MachineInstr &MI, LexicalScopes &LS, VarLocKind K = InvalidKind) : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI), - UVS(MI.getDebugLoc(), LS) { + UVS(MI.getDebugLoc(), LS), Kind(K) { static_assert((sizeof(Loc) == sizeof(uint64_t)), "hash does not cover all members of Loc"); assert(MI.isDebugValue() && "not a DBG_VALUE"); assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); if (int RegNo = isDbgValueDescribedByReg(MI)) { - Kind = RegisterKind; + if (Kind != EntryValueKind) + Kind = RegisterKind; Loc.RegNo = RegNo; } } @@ -186,6 +189,10 @@ return 0; } + bool isEntryValueKind() const { + return Kind == EntryValueKind; + } + /// Determine whether the lexical scope of this value's debug location /// dominates MBB. bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); } @@ -195,17 +202,21 @@ #endif bool operator==(const VarLoc &Other) const { - return Var == Other.Var && Loc.Hash == Other.Loc.Hash; + return Var == Other.Var && + Loc.Hash == Other.Loc.Hash && Kind == Other.Kind; } /// This operator guarantees that VarLocs are sorted by Variable first. bool operator<(const VarLoc &Other) const { - if (Var == Other.Var) - return Loc.Hash < Other.Loc.Hash; - return Var < Other.Var; + if (Var != Other.Var) + return Var < Other.Var; + if (Other.Kind != Kind) + return Other.Kind < Kind; + return Loc.Hash < Other.Loc.Hash; } }; + using ParamSet = SmallVector; using VarLocMap = UniqueVector; using VarLocSet = SparseBitVector<>; using VarLocInMBB = SmallDenseMap; @@ -282,15 +293,22 @@ VarLocMap &VarLocIDs); void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs, TransferMap &Transfers); + void emitEntryValues(MachineInstr &MI, OpenRangesSet &OpenRanges, + VarLocMap &VarLocIDs, TransferMap &Transfers, + ParamSet &ParamEntryVals, + SparseBitVector<> &KillSet); void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs, TransferMap &Transfers); void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, - const VarLocMap &VarLocIDs); + VarLocMap &VarLocIDs, TransferMap &Transfers, + ParamSet &ParamEntryVals); bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); bool process(MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, - TransferMap &Transfers, bool transferChanges); + TransferMap &Transfers, + ParamSet &ParamEntryVals, + bool transferChanges); bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, const VarLocMap &VarLocIDs, @@ -408,14 +426,66 @@ OpenRanges.erase(V); // Add the VarLoc to OpenRanges from this DBG_VALUE. - // TODO: Currently handles DBG_VALUE which has only reg as location. + // TODO: Currently handles DBG_VALUE which has only reg + // (with/out entry value) as location. + VarLoc::VarLocKind K = VarLoc::InvalidKind; if (isDbgValueDescribedByReg(MI)) { - VarLoc VL(MI, LS); + K = VarLoc::RegisterKind; + const DIExpression *Expr = MI.getDebugExpression(); + if (Expr->isEntryValue()) + K = VarLoc::EntryValueKind; + } + + if (K != VarLoc::InvalidKind) { + VarLoc VL(MI, LS, K); unsigned ID = VarLocIDs.insert(VL); OpenRanges.insert(ID, VL.Var); } } +void LiveDebugValues::emitEntryValues(MachineInstr &MI, + OpenRangesSet &OpenRanges, + VarLocMap &VarLocIDs, + TransferMap &Transfers, + ParamSet &ParamEntryVals, + SparseBitVector<> &KillSet) { + MachineFunction *MF = MI.getParent()->getParent(); + for (unsigned ID : KillSet) { + if (!VarLocIDs[ID].Var.getVar()->isParameter()) + continue; + + const MachineInstr *CurrDebugInstr = &VarLocIDs[ID].MI; + auto DebugInstr = std::find_if(ParamEntryVals.begin(), ParamEntryVals.end(), + [&CurrDebugInstr](MachineInstr *MII) { + return CurrDebugInstr->getDebugVariable() == + MII->getDebugVariable(); + }); + + // If parameter's DBG_VALUE is not in the vector that means that its value + // is changed troughout the function. That means that we can't + // use parameter's entry value. + if (DebugInstr == ParamEntryVals.end()) + continue; + + auto &ParamDebugInstr = **DebugInstr; + DIExpression *NewExpr = + DIExpression::prepend(ParamDebugInstr.getDebugExpression(), false, 0, false, + false, DIExpression::WithEntryValue); + MachineInstr *EntryValDbgMI = BuildMI( + *MF, ParamDebugInstr.getDebugLoc(), ParamDebugInstr.getDesc(), + ParamDebugInstr.isIndirectDebugValue(), ParamDebugInstr.getOperand(0).getReg(), + ParamDebugInstr.getDebugVariable(), NewExpr); + + if (ParamDebugInstr.isIndirectDebugValue()) + EntryValDbgMI->getOperand(1).setImm(ParamDebugInstr.getOperand(1).getImm()); + + Transfers.push_back({&MI, EntryValDbgMI}); + VarLoc VL(*EntryValDbgMI, LS, VarLoc::EntryValueKind); + unsigned EntryValLocID = VarLocIDs.insert(VL); + OpenRanges.insert(EntryValLocID, VL.Var); + } +} + /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc /// with \p OldVarID should be deleted form \p OpenRanges and replaced with /// new VarLoc. If \p NewReg is different than default zero value then the @@ -425,17 +495,17 @@ MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers, VarLocMap &VarLocIDs, unsigned OldVarID, TransferKind Kind, unsigned NewReg) { - const MachineInstr *DMI = &VarLocIDs[OldVarID].MI; + const MachineInstr *DebugInstr = &VarLocIDs[OldVarID].MI; MachineFunction *MF = MI.getParent()->getParent(); - MachineInstr *NewDMI; + MachineInstr *NewDebugInstr; auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, - &VarLocIDs](VarLoc &VL, MachineInstr *NewDMI) { + &VarLocIDs](VarLoc &VL, MachineInstr *NewDebugInstr) { unsigned LocId = VarLocIDs.insert(VL); OpenRanges.insert(LocId, VL.Var); - // The newly created DBG_VALUE instruction NewDMI must be inserted after + // The newly created DBG_VALUE instruction NewDebugInstr must be inserted after // MI. Keep track of the pairing. - TransferDebugPair MIP = {&MI, NewDMI}; + TransferDebugPair MIP = {&MI, NewDebugInstr}; Transfers.push_back(MIP); }; @@ -447,15 +517,15 @@ "No register supplied when handling a copy of a debug value"); // Create a DBG_VALUE instruction to describe the Var in its new // register location. - NewDMI = BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), - DMI->isIndirectDebugValue(), NewReg, - DMI->getDebugVariable(), DMI->getDebugExpression()); - if (DMI->isIndirectDebugValue()) - NewDMI->getOperand(1).setImm(DMI->getOperand(1).getImm()); - VarLoc VL(*NewDMI, LS); - ProcessVarLoc(VL, NewDMI); + NewDebugInstr = BuildMI(*MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), + DebugInstr->isIndirectDebugValue(), NewReg, + DebugInstr->getDebugVariable(), DebugInstr->getDebugExpression()); + if (DebugInstr->isIndirectDebugValue()) + NewDebugInstr->getOperand(1).setImm(DebugInstr->getOperand(1).getImm()); + VarLoc VL(*NewDebugInstr, LS); + ProcessVarLoc(VL, NewDebugInstr); LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register copy: "; - NewDMI->print(dbgs(), false, false, false, TII)); + NewDebugInstr->print(dbgs(), false, false, false, TII)); return; } case TransferKind::TransferSpill: { @@ -463,15 +533,15 @@ // location. VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI); auto *SpillExpr = - DIExpression::prepend(DMI->getDebugExpression(), DIExpression::NoDeref, + DIExpression::prepend(DebugInstr->getDebugExpression(), DIExpression::NoDeref, SpillLocation.SpillOffset); - NewDMI = - BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true, - SpillLocation.SpillBase, DMI->getDebugVariable(), SpillExpr); - VarLoc VL(*NewDMI, SpillLocation.SpillBase, SpillLocation.SpillOffset, LS); - ProcessVarLoc(VL, NewDMI); + NewDebugInstr = + BuildMI(*MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), true, + SpillLocation.SpillBase, DebugInstr->getDebugVariable(), SpillExpr); + VarLoc VL(*NewDebugInstr, SpillLocation.SpillBase, SpillLocation.SpillOffset, LS); + ProcessVarLoc(VL, NewDebugInstr); LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: "; - NewDMI->print(dbgs(), false, false, false, TII)); + NewDebugInstr->print(dbgs(), false, false, false, TII)); return; } case TransferKind::TransferRestore: { @@ -479,12 +549,12 @@ "No register supplied when handling a restore of a debug value"); MachineFunction *MF = MI.getMF(); DIBuilder DIB(*const_cast(MF->getFunction()).getParent()); - NewDMI = BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), false, NewReg, - DMI->getDebugVariable(), DIB.createExpression()); - VarLoc VL(*NewDMI, LS); - ProcessVarLoc(VL, NewDMI); + NewDebugInstr = BuildMI(*MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), false, NewReg, + DebugInstr->getDebugVariable(), DIB.createExpression()); + VarLoc VL(*NewDebugInstr, LS); + ProcessVarLoc(VL, NewDebugInstr); LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register restore: "; - NewDMI->print(dbgs(), false, false, false, TII)); + NewDebugInstr->print(dbgs(), false, false, false, TII)); return; } } @@ -492,9 +562,9 @@ } /// A definition of a register may mark the end of a range. -void LiveDebugValues::transferRegisterDef(MachineInstr &MI, - OpenRangesSet &OpenRanges, - const VarLocMap &VarLocIDs) { +void LiveDebugValues::transferRegisterDef( + MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs, + TransferMap &Transfers, ParamSet &ParamEntryVals) { MachineFunction *MF = MI.getMF(); const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); @@ -524,6 +594,12 @@ } } OpenRanges.erase(KillSet, VarLocIDs); + + auto *TPC = getAnalysisIfAvailable(); + auto &TM = TPC->getTM(); + if (TM.Options.EnableParamEntryValues) + emitEntryValues(MI, OpenRanges, VarLocIDs, Transfers, ParamEntryVals, + KillSet); } /// Decide if @MI is a spill instruction and return true if it is. We use 2 @@ -711,10 +787,13 @@ /// This routine creates OpenRanges and OutLocs. bool LiveDebugValues::process(MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, - TransferMap &Transfers, bool transferChanges) { + TransferMap &Transfers, + ParamSet &ParamEntryVals, + bool transferChanges) { bool Changed = false; transferDebugValue(MI, OpenRanges, VarLocIDs); - transferRegisterDef(MI, OpenRanges, VarLocIDs); + transferRegisterDef(MI, OpenRanges, VarLocIDs, Transfers, + ParamEntryVals); if (transferChanges) { transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers); transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers); @@ -805,13 +884,13 @@ // new range is started for the var from the mbb's beginning by inserting // a new DBG_VALUE. process() will end this range however appropriate. const VarLoc &DiffIt = VarLocIDs[ID]; - const MachineInstr *DMI = &DiffIt.MI; + const MachineInstr *DebugInstr = &DiffIt.MI; MachineInstr *MI = - BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(), - DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), - DMI->getDebugVariable(), DMI->getDebugExpression()); - if (DMI->isIndirectDebugValue()) - MI->getOperand(1).setImm(DMI->getOperand(1).getImm()); + BuildMI(MBB, MBB.instr_begin(), DebugInstr->getDebugLoc(), DebugInstr->getDesc(), + DebugInstr->isIndirectDebugValue(), DebugInstr->getOperand(0).getReg(), + DebugInstr->getDebugVariable(), DebugInstr->getDebugExpression()); + if (DebugInstr->isIndirectDebugValue()) + MI->getOperand(1).setImm(DebugInstr->getOperand(1).getImm()); LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump();); ILS.set(ID); ++NumInserted; @@ -850,15 +929,57 @@ enum : bool { dontTransferChanges = false, transferChanges = true }; + // Insert DEBUG_VALUE instructions that track parameters into ParamEntryVals. + // When we encounter a variable that we have already entered + // we assume that we have found all parameter's entry locations and stop. + // Note: We check whether a MI is inlined in order to deduce whether + // the variable that it tracks comes from a different function. + // If that is the case we can't track its entry value. + // TODO: Add support for modified arguments that can be expreseed + // by using its entry value. + ParamSet ParamEntryVals; + MachineBasicBlock &First_MBB = *(MF.begin()); + + const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); + unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); + auto IsRegOtherThanSP = [&](const MachineOperand &Op) -> bool { + return Op.isReg() && Op.getReg() && Op.getReg() != SP; + }; + + auto IsNewParameter = [&ParamEntryVals](const MachineInstr &MI) { + for (auto MII : ParamEntryVals) + if (MII->getDebugVariable() == MI.getDebugVariable()) + return false; + return true; + }; + + for (auto &MI : First_MBB) + if (MI.isDebugValue() && MI.getDebugVariable()->isParameter() && + MI.getDebugVariable()->isNotModified() && + !MI.getDebugLoc()->getInlinedAt() && + IsRegOtherThanSP(MI.getOperand(0))) { + if (IsNewParameter(MI) && MI.getOperand(0).isReg()) + ParamEntryVals.push_back(&MI); + else + break; + } + // Initialize every mbb with OutLocs. // We are not looking at any spill instructions during the initial pass // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE // instructions for spills of registers that are known to be user variables // within the BB in which the spill occurs. - for (auto &MBB : MF) + for (auto &MBB : MF) { for (auto &MI : MBB) process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers, - dontTransferChanges); + ParamEntryVals, dontTransferChanges); + // Add any entry DBG_VALUE instructions necessitated by parameter + // clobbering. + for (auto &TR : Transfers) + MBB.insertAfter(MachineBasicBlock::iterator(*TR.TransferInst), + TR.DebugInst); + Transfers.clear(); + } auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { if (const DebugLoc &DL = MI.getDebugLoc()) @@ -905,7 +1026,7 @@ // correspond to user variables. for (auto &MI : *MBB) OLChanged |= process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers, - transferChanges); + ParamEntryVals, transferChanges); // Add any DBG_VALUE instructions necessitated by spills. for (auto &TR : Transfers) Index: test/DebugInfo/MIR/X86/dbginfo-entryvals.mir =================================================================== --- /dev/null +++ test/DebugInfo/MIR/X86/dbginfo-entryvals.mir @@ -0,0 +1,115 @@ +# RUN: llc -param-entry-values -run-pass=livedebugvalues -march=x86-64 -o - %s | FileCheck %s +# +#extern void fn2(int); +# +#__attribute__((noinline)) +#void +#fn1 (int x, int y) { +# int u = x + y; +# if (x > 1) +# u += 1; +# else +# u += 2; +# int a = 7; +# fn2 (a); +# u --; +#} +# CHECK: DBG_VALUE $edi, $noreg, !14, !DIExpression(DW_OP_entry_value, 1), debug-location {{.*}} +# CHECK: DBG_VALUE $esi, $noreg, !15, !DIExpression(DW_OP_entry_value, 1), debug-location {{.*}} + +--- | + ; ModuleID = 'test.c' + source_filename = "test.c" + target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + target triple = "x86_64-unknown-linux-gnu" + + ; Function Attrs: noinline nounwind uwtable + define dso_local void @fn1(i32 %x, i32 %y) local_unnamed_addr !dbg !9 { + entry: + call void @llvm.dbg.value(metadata i32 %x, metadata !14, metadata !DIExpression()), !dbg !18 + call void @llvm.dbg.value(metadata i32 %y, metadata !15, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32 7, metadata !17, metadata !DIExpression()), !dbg !20 + tail call void @fn2(i32 7) #3, !dbg !21 + ret void, !dbg !22 + } + + declare !dbg !4 dso_local void @fn2(i32) local_unnamed_addr + + ; Function Attrs: nounwind readnone speculatable + declare void @llvm.dbg.value(metadata, metadata, metadata) + + ; Function Attrs: nounwind + declare void @llvm.stackprotector(i8*, i8**) + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!5, !6, !7} + !llvm.ident = !{!8} + + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, nameTableKind: None) + !1 = !DIFile(filename: "test.c", directory: "/dir") + !2 = !{} + !3 = !{!4} + !4 = !DISubprogram(name: "fn2", scope: !1, file: !1, line: 11, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) + !5 = !{i32 2, !"Dwarf Version", i32 4} + !6 = !{i32 2, !"Debug Info Version", i32 3} + !7 = !{i32 1, !"wchar_size", i32 4} + !8 = !{!"clang version 9.0.0"} + !9 = distinct !DISubprogram(name: "fn1", scope: !1, file: !1, line: 15, type: !10, scopeLine: 15, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13) + !10 = !DISubroutineType(types: !11) + !11 = !{null, !12, !12} + !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !13 = !{!14, !15, !16, !17} + !14 = !DILocalVariable(name: "x", arg: 1, scope: !9, file: !1, line: 15, type: !12, flags: DIFlagArgumentNotModified) + !15 = !DILocalVariable(name: "y", arg: 2, scope: !9, file: !1, line: 15, type: !12, flags: DIFlagArgumentNotModified) + !16 = !DILocalVariable(name: "u", scope: !9, file: !1, line: 16, type: !12, flags: DIFlagArgumentNotModified) + !17 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 21, type: !12, flags: DIFlagArgumentNotModified) + !18 = !DILocation(line: 15, column: 10, scope: !9) + !19 = !DILocation(line: 15, column: 17, scope: !9) + !20 = !DILocation(line: 21, column: 7, scope: !9) + !21 = !DILocation(line: 22, column: 3, scope: !9) + !22 = !DILocation(line: 24, column: 1, scope: !9) + +... +--- +name: fn1 +alignment: 4 +exposesReturnsTwice: false +legalized: false +regBankSelected: false +selected: false +failedISel: false +tracksRegLiveness: true +hasWinCFI: false +registers: [] +liveins: [] +frameInfo: + isFrameAddressTaken: false + isReturnAddressTaken: false + hasStackMap: false + hasPatchPoint: false + stackSize: 0 + offsetAdjustment: 0 + maxAlignment: 0 + adjustsStack: false + hasCalls: false + stackProtector: '' + maxCallFrameSize: 0 + cvBytesOfCalleeSavedRegisters: 0 + hasOpaqueSPAdjustment: false + hasVAStart: false + hasMustTailInVarArgFunc: false + localFrameSize: 0 + savePoint: '' + restorePoint: '' +fixedStack: [] +stack: [] +constants: [] +body: | + bb.0.entry: + DBG_VALUE $edi, $noreg, !14, !DIExpression(), debug-location !18 + DBG_VALUE $esi, $noreg, !15, !DIExpression(), debug-location !19 + DBG_VALUE 7, $noreg, !17, !DIExpression(), debug-location !20 + $edi = MOV32ri 7, debug-location !21 + TAILJMPd64 @fn2, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit killed $edi, debug-location !21 + +...