Index: include/llvm/BinaryFormat/Dwarf.def =================================================================== --- include/llvm/BinaryFormat/Dwarf.def +++ include/llvm/BinaryFormat/Dwarf.def @@ -632,6 +632,9 @@ // Vendor extensions: // Extensions for GNU-style thread-local storage. HANDLE_DW_OP(0xe0, GNU_push_tls_address, 0, GNU) +// The GNU entry value extension. +// See http://www.dwarfstd.org/ShowIssue.php?issue=100909.1&type=open . +HANDLE_DW_OP(0xf3, GNU_entry_value, 0, GNU) // Extensions for Fission proposal. HANDLE_DW_OP(0xfb, GNU_addr_index, 0, GNU) HANDLE_DW_OP(0xfc, GNU_const_index, 0, GNU) Index: include/llvm/IR/DebugInfoMetadata.h =================================================================== --- include/llvm/IR/DebugInfoMetadata.h +++ include/llvm/IR/DebugInfoMetadata.h @@ -2525,19 +2525,21 @@ unsigned &AddrClass); /// Constants for DIExpression::prepend. - enum { NoDeref = false, WithDeref = true, WithStackValue = true }; + enum { NoDeref = false, WithDeref = true, WithStackValue = true, + WithEntryValue = true }; /// Prepend \p DIExpr with a deref and offset operation and optionally turn it /// into a stack value. static DIExpression *prepend(const DIExpression *Expr, bool DerefBefore, int64_t Offset = 0, bool DerefAfter = false, - bool StackValue = false); + bool StackValue = false, bool EntryValue = false); /// Prepend \p DIExpr with the given opcodes and optionally turn it into a /// stack value. static DIExpression *prependOpcodes(const DIExpression *Expr, SmallVectorImpl &Ops, - bool StackValue = false); + bool StackValue = false, + bool EntryValue = false); /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the /// returned expression is a stack value only if \p DIExpr is a stack value. @@ -2591,6 +2593,12 @@ return true; return fragmentCmp(Other) == 0; } + + /// Check if the expression contains entry value operand. + bool isEntryValue() const { + return getNumElements() > 0 && + getElement(0) == dwarf::DW_OP_GNU_entry_value; + } }; /// Global variables. Index: lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp +++ lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp @@ -41,6 +41,10 @@ static unsigned isDescribedByReg(const MachineInstr &MI) { assert(MI.isDebugValue()); assert(MI.getNumOperands() == 4); + // If location of variable is entry value (DW_OP_entry_value) + // do not consider it as a register location. + if (MI.getDebugExpression()->isEntryValue()) + return 0; // If location of variable is described using a register (directly or // indirectly), this register is always a first operand. return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; Index: lib/CodeGen/AsmPrinter/DebugLocEntry.h =================================================================== --- lib/CodeGen/AsmPrinter/DebugLocEntry.h +++ lib/CodeGen/AsmPrinter/DebugLocEntry.h @@ -73,6 +73,7 @@ const ConstantInt *getConstantInt() const { return Constant.CIP; } MachineLocation getLoc() const { return Loc; } bool isFragment() const { return getExpression()->isFragment(); } + bool isEntryVal() const { return getExpression()->isEntryValue(); } const DIExpression *getExpression() const { return Expression; } friend bool operator==(const Value &, const Value &); friend bool operator<(const Value &, const Value &); Index: lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -269,8 +269,8 @@ // It would be better if this were unconditional, but malformed input that // mixes non-fragments and fragments for the same variable is too expensive // to detect in the verifier. - if (DwarfExpr->isUnknownLocation()) - DwarfExpr->setMemoryLocationKind(); + if (DwarfExpr->isLocationUnknown()) + DwarfExpr->setMemoryLocationFlag(); DwarfExpr->addExpression(Expr); } if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { @@ -674,7 +674,7 @@ if (Expr) Ops.append(Expr->elements_begin(), Expr->elements_end()); DIExpressionCursor Cursor(Ops); - DwarfExpr.setMemoryLocationKind(); + DwarfExpr.setMemoryLocationFlag(); if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol()) addOpAddress(*Loc, FrameSymbol); else @@ -1105,7 +1105,7 @@ DIELoc *Loc = new (DIEValueAllocator) DIELoc; DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); if (Location.isIndirect()) - DwarfExpr.setMemoryLocationKind(); + DwarfExpr.setMemoryLocationFlag(); DIExpressionCursor Cursor({}); const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); @@ -1129,7 +1129,7 @@ const DIExpression *DIExpr = DV.getSingleExpression(); DwarfExpr.addFragmentOffset(DIExpr); if (Location.isIndirect()) - DwarfExpr.setMemoryLocationKind(); + DwarfExpr.setMemoryLocationFlag(); DIExpressionCursor Cursor(DIExpr); const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); Index: lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.h +++ lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -14,6 +14,7 @@ #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H #include "AddressPool.h" +#include "DebugLocEntry.h" #include "DebugLocStream.h" #include "DwarfFile.h" #include "llvm/ADT/ArrayRef.h" @@ -51,6 +52,7 @@ class DebugLocEntry; class DIE; class DwarfCompileUnit; +class DwarfExpression; class DwarfTypeUnit; class DwarfUnit; class LexicalScope; @@ -732,6 +734,10 @@ void addSectionLabel(const MCSymbol *Sym); const MCSymbol *getSectionLabel(const MCSection *S); + + static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, + const DebugLocEntry::Value &Value, + DwarfExpression &DwarfExpr); }; } // end namespace llvm Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -1954,9 +1954,9 @@ } } -static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, - const DebugLocEntry::Value &Value, - DwarfExpression &DwarfExpr) { +void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, + const DebugLocEntry::Value &Value, + DwarfExpression &DwarfExpr) { auto *DIExpr = Value.getExpression(); DIExpressionCursor ExprCursor(DIExpr); DwarfExpr.addFragmentOffset(DIExpr); @@ -1969,9 +1969,16 @@ DwarfExpr.addUnsignedConstant(Value.getInt()); } else if (Value.isLocation()) { MachineLocation Location = Value.getLoc(); - if (Location.isIndirect()) - DwarfExpr.setMemoryLocationKind(); DIExpressionCursor Cursor(DIExpr); + if (Location.isIndirect()) + DwarfExpr.setMemoryLocationFlag(); + + if (DIExpr->isEntryValue()) { + DwarfExpr.setEntryValueLocationFlag(); + if (!DwarfExpr.addEntryValueExpression(Cursor)) + return; + } + const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) return; @@ -2003,11 +2010,11 @@ "fragments are expected to be sorted"); for (auto Fragment : Values) - emitDebugLocValue(AP, BT, Fragment, DwarfExpr); + DwarfDebug::emitDebugLocValue(AP, BT, Fragment, DwarfExpr); } else { assert(Values.size() == 1 && "only fragments may have >1 value"); - emitDebugLocValue(AP, BT, Value, DwarfExpr); + DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr); } DwarfExpr.finalize(); } Index: lib/CodeGen/AsmPrinter/DwarfExpression.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfExpression.h +++ lib/CodeGen/AsmPrinter/DwarfExpression.h @@ -117,9 +117,38 @@ unsigned SubRegisterSizeInBits = 0; unsigned SubRegisterOffsetInBits = 0; - /// The kind of location description being produced. - enum { Unknown = 0, Register, Memory, Implicit } LocationKind = Unknown; + /// The flags of location description being produced. + enum LocationFlags { + Unknown = 0, + Register = 1, + Memory = 1 << 1, + Implicit = 1 << 2, + EntryValueExpr = 1 << 3 + }; + + uint8_t LocationFlags = 0; + +public: + bool isLocationUnknown() { + return LocationFlags == Unknown; + } + + bool isLocationRegister() { + return LocationFlags & Register; + } + + bool isLocationMemory() { + return LocationFlags & Memory; + } + bool isLocationImplicit() { + return LocationFlags & Implicit; + } + + bool isLocationEntryValueExpr() { + return LocationFlags & EntryValueExpr; + } +protected: /// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed /// to represent a subregister. void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) { @@ -218,13 +247,15 @@ /// Emit an unsigned constant. void addUnsignedConstant(const APInt &Value); - bool isMemoryLocation() const { return LocationKind == Memory; } - bool isUnknownLocation() const { return LocationKind == Unknown; } - /// Lock this down to become a memory location description. - void setMemoryLocationKind() { - assert(LocationKind == Unknown); - LocationKind = Memory; + void setMemoryLocationFlag() { + assert(isLocationUnknown() || isLocationEntryValueExpr()); + LocationFlags |= Memory; + } + + /// Set this to become a entry value location + void setEntryValueLocationFlag() { + LocationFlags |= EntryValueExpr; } /// Emit a machine register location. As an optimization this may also consume @@ -241,6 +272,9 @@ DIExpressionCursor &Expr, unsigned MachineReg, unsigned FragmentOffsetInBits = 0); + /// Emit entry value dwarf operation. + bool addEntryValueExpression(DIExpressionCursor &ExprCursor); + /// Emit all remaining operations in the DIExpressionCursor. /// /// \param FragmentOffsetInBits If this is one fragment out of multiple Index: lib/CodeGen/AsmPrinter/DwarfExpression.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfExpression.cpp +++ lib/CodeGen/AsmPrinter/DwarfExpression.cpp @@ -40,9 +40,10 @@ void DwarfExpression::addReg(int DwarfReg, const char *Comment) { assert(DwarfReg >= 0 && "invalid negative dwarf register number"); - assert((LocationKind == Unknown || LocationKind == Register) && + assert((isLocationUnknown() || isLocationRegister() || + isLocationEntryValueExpr()) && "location description already locked down"); - LocationKind = Register; + LocationFlags |= Register; if (DwarfReg < 32) { emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment); } else { @@ -53,7 +54,7 @@ void DwarfExpression::addBReg(int DwarfReg, int Offset) { assert(DwarfReg >= 0 && "invalid negative dwarf register number"); - assert(LocationKind != Register && "location description already locked down"); + assert(!isLocationRegister() && "location description already locked down"); if (DwarfReg < 32) { emitOp(dwarf::DW_OP_breg0 + DwarfReg); } else { @@ -184,21 +185,24 @@ } void DwarfExpression::addSignedConstant(int64_t Value) { - assert(LocationKind == Implicit || LocationKind == Unknown); - LocationKind = Implicit; + assert(isLocationImplicit() || isLocationUnknown() || + isLocationEntryValueExpr()); + LocationFlags |= Implicit; emitOp(dwarf::DW_OP_consts); emitSigned(Value); } void DwarfExpression::addUnsignedConstant(uint64_t Value) { - assert(LocationKind == Implicit || LocationKind == Unknown); - LocationKind = Implicit; + assert(isLocationImplicit() || isLocationUnknown() || + isLocationEntryValueExpr()); + LocationFlags |= Implicit; emitConstu(Value); } void DwarfExpression::addUnsignedConstant(const APInt &Value) { - assert(LocationKind == Implicit || LocationKind == Unknown); - LocationKind = Implicit; + assert(isLocationImplicit() || isLocationUnknown() || + isLocationEntryValueExpr()); + LocationFlags |= Implicit; unsigned Size = Value.getBitWidth(); const uint64_t *Data = Value.getRawData(); @@ -222,7 +226,7 @@ unsigned FragmentOffsetInBits) { auto Fragment = ExprCursor.getFragmentInfo(); if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) { - LocationKind = Unknown; + LocationFlags = Unknown; return false; } @@ -237,17 +241,21 @@ // operation to multiple DW_OP_pieces. if (HasComplexExpression && DwarfRegs.size() > 1) { DwarfRegs.clear(); - LocationKind = Unknown; + LocationFlags = Unknown; return false; } // Handle simple register locations. - if (LocationKind != Memory && !HasComplexExpression) { + if (!isLocationMemory() && !HasComplexExpression) { for (auto &Reg : DwarfRegs) { if (Reg.DwarfRegNo >= 0) addReg(Reg.DwarfRegNo, Reg.Comment); addOpPiece(Reg.Size); } + + if (isLocationEntryValueExpr() && DwarfVersion >= 4) + emitOp(dwarf::DW_OP_stack_value); + DwarfRegs.clear(); return true; } @@ -258,7 +266,7 @@ return Op.getOp() == dwarf::DW_OP_stack_value; })) { DwarfRegs.clear(); - LocationKind = Unknown; + LocationFlags = Unknown; return false; } @@ -273,6 +281,7 @@ if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) { SignedOffset = Op->getArg(0); ExprCursor.take(); + Op = ExprCursor.peek(); } // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset] @@ -283,7 +292,7 @@ if (N && (N->getOp() == dwarf::DW_OP_plus || (N->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) { int Offset = Op->getArg(0); - SignedOffset = (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset; + SignedOffset += (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset; ExprCursor.consume(2); } } @@ -311,6 +320,22 @@ return true; } +bool DwarfExpression::addEntryValueExpression(DIExpressionCursor &ExprCursor) { + auto Op = ExprCursor.take(); + assert(Op && Op->getOp() == dwarf::DW_OP_GNU_entry_value); + + // We do not support entry value of a memory location. + if (isLocationMemory()) { + LocationFlags = Unknown; + return false; + } + + emitOp(dwarf::DW_OP_GNU_entry_value); + emitUnsigned(Op->getArg(0)); + + return true; +} + void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor, unsigned FragmentOffsetInBits) { // If we need to mask out a subregister, do it now, unless the next @@ -323,7 +348,7 @@ while (ExprCursor) { auto Op = ExprCursor.take(); - switch (Op->getOp()) { + switch (Op->getOp) { case dwarf::DW_OP_LLVM_fragment: { unsigned SizeInBits = Op->getArg(1); unsigned FragmentOffset = Op->getArg(0); @@ -343,18 +368,18 @@ SizeInBits = std::min(SizeInBits, SubRegisterSizeInBits); // Emit a DW_OP_stack_value for implicit location descriptions. - if (LocationKind == Implicit) + if (isLocationImplicit()) addStackValue(); // Emit the DW_OP_piece. addOpPiece(SizeInBits, SubRegisterOffsetInBits); setSubRegisterPiece(0, 0); // Reset the location description kind. - LocationKind = Unknown; + LocationFlags = Unknown; return; } case dwarf::DW_OP_plus_uconst: - assert(LocationKind != Register); + assert(!isLocationRegister()); emitOp(dwarf::DW_OP_plus_uconst); emitUnsigned(Op->getArg(0)); break; @@ -375,16 +400,16 @@ emitOp(Op->getOp()); break; case dwarf::DW_OP_deref: - assert(LocationKind != Register); - if (LocationKind != Memory && ::isMemoryLocation(ExprCursor)) + assert(!isLocationRegister()); + if (!isLocationMemory() && isMemoryLocation(ExprCursor)) // Turning this into a memory location description makes the deref // implicit. - LocationKind = Memory; + LocationFlags |= Memory; else emitOp(dwarf::DW_OP_deref); break; case dwarf::DW_OP_constu: - assert(LocationKind != Register); + assert(!isLocationRegister()); emitConstu(Op->getArg(0)); break; case dwarf::DW_OP_LLVM_convert: { @@ -424,14 +449,14 @@ break; } case dwarf::DW_OP_stack_value: - LocationKind = Implicit; + LocationFlags |= Implicit; break; case dwarf::DW_OP_swap: - assert(LocationKind != Register); + assert(!isLocationRegister()); emitOp(dwarf::DW_OP_swap); break; case dwarf::DW_OP_xderef: - assert(LocationKind != Register); + assert(!isLocationRegister()); emitOp(dwarf::DW_OP_xderef); break; default: @@ -439,7 +464,7 @@ } } - if (LocationKind == Implicit) + if (isLocationImplicit()) // Turn this into an implicit location description. addStackValue(); } Index: lib/CodeGen/LiveDebugValues.cpp =================================================================== --- lib/CodeGen/LiveDebugValues.cpp +++ lib/CodeGen/LiveDebugValues.cpp @@ -66,6 +66,9 @@ STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); +static cl::opt EmitEntryDbgValues("emit-entry-values", cl::Hidden, + cl::init(false)); + // If @MI is a DBG_VALUE with debug value described by a defined // register, returns the number of this register. In the other case, returns 0. static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) { @@ -143,7 +146,8 @@ enum VarLocKind { InvalidKind = 0, RegisterKind, - SpillLocKind + SpillLocKind, + EntryValueKind } Kind = InvalidKind; /// The value location. Stored separately to avoid repeatedly @@ -154,15 +158,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 +191,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 +204,23 @@ #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; + if (Var == Other.Var) { + if (Other.Kind == Kind) + return Loc.Hash < Other.Loc.Hash; + return Other.Kind < Kind; + } + return Var < Other.Var; } }; + using ParamSet = SmallVector; using VarLocMap = UniqueVector; using VarLocSet = SparseBitVector<>; using VarLocInMBB = SmallDenseMap; @@ -282,15 +297,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, @@ -409,13 +431,64 @@ // Add the VarLoc to OpenRanges from this DBG_VALUE. // TODO: Currently handles DBG_VALUE which has only reg as location. + // Also, this handles entry values. + 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() && + VarLocIDs[ID].Var.getVar()->isParameter()) { + const MachineInstr *CurrDMI = &VarLocIDs[ID].MI; + auto DMI = std::find_if( + ParamEntryVals.begin(), ParamEntryVals.end(), + [&CurrDMI](MachineInstr *MII) { + return CurrDMI->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 (DMI == ParamEntryVals.end()) + continue; + + DIExpression *NewExpr = + DIExpression::prepend((*DMI)->getDebugExpression(), false, 0, false, + false, DIExpression::WithEntryValue); + MachineInstr *EntryValDbgMI = BuildMI( + *MF, (*DMI)->getDebugLoc(), (*DMI)->getDesc(), + (*DMI)->isIndirectDebugValue(), (*DMI)->getOperand(0).getReg(), + (*DMI)->getDebugVariable(), NewExpr); + + + if ((*DMI)->isIndirectDebugValue()) + EntryValDbgMI->getOperand(1).setImm((*DMI)->getOperand(1).getImm()); + + TransferDebugPair MIP = {&MI, EntryValDbgMI}; + Transfers.push_back(MIP); + 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 @@ -492,9 +565,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 +597,9 @@ } } OpenRanges.erase(KillSet, VarLocIDs); + if (EmitEntryDbgValues) + 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); @@ -850,15 +929,53 @@ 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()); + + auto IsNewParameter = [&ParamEntryVals](const MachineInstr &MI) { + for (auto MII : ParamEntryVals) + if (MII->getDebugVariable() == MI.getDebugVariable()) + return false; + return true; + }; + + const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); + unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); + for (auto &MI : First_MBB) + if (MI.isDebugValue() && MI.getDebugVariable()->isParameter() && + MI.getDebugVariable()->isNotModified() && + !MI.getDebugLoc()->getInlinedAt() && MI.getOperand(0).isReg() && + MI.getOperand(0).getReg() && MI.getOperand(0).getReg() != SP) { + 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,12 +1022,13 @@ // 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) MBB->insertAfter(MachineBasicBlock::iterator(*TR.TransferInst), TR.DebugInst); + Transfers.clear(); LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, Index: lib/DebugInfo/DWARF/DWARFExpression.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFExpression.cpp +++ lib/DebugInfo/DWARF/DWARFExpression.cpp @@ -97,6 +97,7 @@ Descriptions[DW_OP_addrx] = Desc(Op::Dwarf4, Op::SizeLEB); Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB); Descriptions[DW_OP_GNU_const_index] = Desc(Op::Dwarf4, Op::SizeLEB); + Descriptions[DW_OP_GNU_entry_value] = Desc(Op::Dwarf4, Op::SizeLEB); Descriptions[DW_OP_convert] = Desc(Op::Dwarf5, Op::BaseTypeRef); @@ -271,7 +272,7 @@ } else { if (Signed) OS << format(" %+" PRId64, (int64_t)Operands[Operand]); - else + else if (Opcode != DW_OP_GNU_entry_value) OS << format(" 0x%" PRIx64, Operands[Operand]); } } @@ -280,6 +281,7 @@ void DWARFExpression::print(raw_ostream &OS, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool IsEH) const { + uint32_t EntryValExprSize = 0; for (auto &Op : *this) { if (!Op.print(OS, this, RegInfo, U, IsEH)) { uint32_t FailOffset = Op.getEndOffset(); @@ -287,6 +289,19 @@ OS << format(" %02x", Data.getU8(&FailOffset)); return; } + + if (Op.getCode() == DW_OP_GNU_entry_value) { + OS << "("; + EntryValExprSize = Op.getRawOperand(0); + continue; + } + + if (EntryValExprSize) { + EntryValExprSize--; + if (EntryValExprSize == 0) + OS << ")"; + } + if (Op.getEndOffset() < Data.getData().size()) OS << ", "; } Index: lib/IR/DebugInfoMetadata.cpp =================================================================== --- lib/IR/DebugInfoMetadata.cpp +++ lib/IR/DebugInfoMetadata.cpp @@ -834,6 +834,7 @@ return 3; case dwarf::DW_OP_constu: case dwarf::DW_OP_plus_uconst: + case dwarf::DW_OP_GNU_entry_value: return 2; default: return 1; @@ -891,6 +892,7 @@ case dwarf::DW_OP_deref: case dwarf::DW_OP_xderef: case dwarf::DW_OP_lit0: + case dwarf::DW_OP_GNU_entry_value: case dwarf::DW_OP_not: case dwarf::DW_OP_dup: break; @@ -966,7 +968,7 @@ DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore, int64_t Offset, bool DerefAfter, - bool StackValue) { + bool StackValue, bool EntryValue) { SmallVector Ops; if (DerefBefore) Ops.push_back(dwarf::DW_OP_deref); @@ -975,14 +977,21 @@ if (DerefAfter) Ops.push_back(dwarf::DW_OP_deref); - return prependOpcodes(Expr, Ops, StackValue); + return prependOpcodes(Expr, Ops, StackValue, EntryValue); } DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, SmallVectorImpl &Ops, - bool StackValue) { + bool StackValue, bool EntryValue) { assert(Expr && "Can't prepend ops to this expression"); + if (EntryValue) { + Ops.push_back(dwarf::DW_OP_GNU_entry_value); + // Add size info needed for entry value expression. + // Add plus one for target register operand. + Ops.push_back(Expr->getNumElements() + 1); + } + // If there are no ops to prepend, do not even add the DW_OP_stack_value. if (Ops.empty()) StackValue = false; Index: lib/Target/Mips/MipsAsmPrinter.cpp =================================================================== --- lib/Target/Mips/MipsAsmPrinter.cpp +++ lib/Target/Mips/MipsAsmPrinter.cpp @@ -188,13 +188,6 @@ unsigned Opc = MI->getOpcode(); TS.forbidModuleDirective(); - if (MI->isDebugValue()) { - SmallString<128> Str; - raw_svector_ostream OS(Str); - - PrintDebugValueComment(MI, OS); - return; - } if (MI->isDebugLabel()) return; @@ -255,6 +248,14 @@ if (emitPseudoExpansionLowering(*OutStreamer, &*I)) continue; + if (I->isDebugValue()) { + SmallString<128> Str; + raw_svector_ostream OS(Str); + + PrintDebugValueComment(&*I, OS); + return; + } + if (I->getOpcode() == Mips::PseudoReturn || I->getOpcode() == Mips::PseudoReturn64 || I->getOpcode() == Mips::PseudoIndirectBranch || Index: test/DebugInfo/MIR/X86/dbginfo-entryvals.mir =================================================================== --- /dev/null +++ test/DebugInfo/MIR/X86/dbginfo-entryvals.mir @@ -0,0 +1,115 @@ +# RUN: llc -emit-entry-values=1 -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_GNU_entry_value, 1), debug-location {{.*}} +# CHECK: DBG_VALUE $esi, $noreg, !15, !DIExpression(DW_OP_GNU_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 + +...