44 #include "llvm/Config/llvm-config.h" 65 #define DEBUG_TYPE "livedebugvalues" 67 STATISTIC(NumInserted,
"Number of DBG_VALUE instructions inserted");
91 class UserValueScopes {
109 using DebugVariableBase =
110 std::pair<const DILocalVariable *, const DILocation *>;
112 struct DebugVariable :
public DebugVariableBase {
114 : DebugVariableBase(Var, InlinedAt) {}
119 bool operator<(
const DebugVariable &DV)
const {
120 if (getVar() == DV.getVar())
121 return getInlinedAt() < DV.getInlinedAt();
122 return getVar() < DV.getVar();
128 const DebugVariable Var;
130 mutable UserValueScopes UVS;
143 static_assert((
sizeof(Loc) ==
sizeof(uint64_t)),
144 "hash does not cover all members of Loc");
165 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 170 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
174 bool operator<(
const VarLoc &Other)
const {
175 if (Var == Other.Var)
176 return Loc.Hash < Other.Loc.Hash;
177 return Var < Other.Var;
184 struct TransferDebugPair {
194 class OpenRangesSet {
199 const VarLocSet &getVarLocs()
const {
return VarLocs; }
202 void erase(DebugVariable Var) {
203 auto It = Vars.
find(Var);
204 if (It != Vars.
end()) {
205 unsigned ID = It->second;
213 void erase(
const VarLocSet &KillSet,
const VarLocMap &VarLocIDs) {
214 VarLocs.intersectWithComplement(KillSet);
215 for (
unsigned ID : KillSet)
220 void insert(
unsigned VarLocID, DebugVariableBase Var) {
221 VarLocs.set(VarLocID);
222 Vars.
insert({Var, VarLocID});
233 assert(Vars.
empty() == VarLocs.empty() &&
"open ranges are inconsistent");
234 return VarLocs.empty();
240 int extractSpillBaseRegAndOffset(
const MachineInstr &MI,
unsigned &Reg);
241 void insertTransferDebugPair(
MachineInstr &MI, OpenRangesSet &OpenRanges,
242 TransferMap &Transfers, VarLocMap &VarLocIDs,
243 unsigned OldVarID,
unsigned NewReg = 0);
245 void transferDebugValue(
const MachineInstr &MI, OpenRangesSet &OpenRanges,
246 VarLocMap &VarLocIDs);
247 void transferSpillInst(
MachineInstr &MI, OpenRangesSet &OpenRanges,
248 VarLocMap &VarLocIDs, TransferMap &Transfers);
249 void transferRegisterCopy(
MachineInstr &MI, OpenRangesSet &OpenRanges,
250 VarLocMap &VarLocIDs, TransferMap &Transfers);
251 void transferRegisterDef(
MachineInstr &MI, OpenRangesSet &OpenRanges,
252 const VarLocMap &VarLocIDs);
253 bool transferTerminatorInst(
MachineInstr &MI, OpenRangesSet &OpenRanges,
254 VarLocInMBB &OutLocs,
const VarLocMap &VarLocIDs);
255 bool process(
MachineInstr &MI, OpenRangesSet &OpenRanges,
256 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
257 TransferMap &Transfers,
bool transferChanges);
260 const VarLocMap &VarLocIDs,
282 const VarLocMap &VarLocIDs,
const char *msg,
309 void LiveDebugValues::getAnalysisUsage(
AnalysisUsage &AU)
const {
320 const VarLocInMBB &V,
321 const VarLocMap &VarLocIDs,
324 Out <<
'\n' << msg <<
'\n';
326 const auto &L = V.lookup(&BB);
327 Out <<
"MBB: " << BB.getName() <<
":\n";
328 for (
unsigned VLL : L) {
329 const VarLoc &VL = VarLocIDs[VLL];
330 Out <<
" Var: " << VL.Var.getVar()->getName();
341 int LiveDebugValues::extractSpillBaseRegAndOffset(
const MachineInstr &
MI,
344 "Spill instruction does not have exactly one memory operand?");
348 "Inconsistent memory operand in spill instruction");
349 int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
351 return TFI->getFrameIndexReference(*MBB->
getParent(), FI,
Reg);
356 void LiveDebugValues::transferDebugValue(
const MachineInstr &MI,
357 OpenRangesSet &OpenRanges,
358 VarLocMap &VarLocIDs) {
363 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
365 "Expected inlined-at fields to agree");
368 DebugVariable V(Var, InlinedAt);
375 unsigned ID = VarLocIDs.insert(VL);
376 OpenRanges.insert(ID, VL.Var);
385 void LiveDebugValues::insertTransferDebugPair(
386 MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers,
387 VarLocMap &VarLocIDs,
unsigned OldVarID,
unsigned NewReg) {
399 LLVM_DEBUG(
dbgs() <<
"Creating DBG_VALUE inst for register copy: ";
405 int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase);
411 NewDMI->print(
dbgs(),
false,
false,
false,
TII));
416 TransferDebugPair MIP = {&
MI, NewDMI};
417 Transfers.push_back(MIP);
420 OpenRanges.erase(VarLocIDs[OldVarID].Var);
423 VarLoc VL(*NewDMI,
LS);
424 unsigned LocID = VarLocIDs.insert(VL);
425 OpenRanges.insert(LocID, VL.Var);
429 void LiveDebugValues::transferRegisterDef(
MachineInstr &MI,
430 OpenRangesSet &OpenRanges,
431 const VarLocMap &VarLocIDs) {
440 if (MO.isReg() && MO.isDef() && MO.getReg() &&
441 TRI->isPhysicalRegister(MO.getReg()) &&
442 !(MI.
isCall() && MO.getReg() == SP)) {
445 for (
unsigned ID : OpenRanges.getVarLocs())
448 }
else if (MO.isRegMask()) {
453 for (
unsigned ID : OpenRanges.getVarLocs()) {
454 unsigned Reg = VarLocIDs[
ID].isDescribedByReg();
455 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
460 OpenRanges.erase(KillSet, VarLocIDs);
469 bool LiveDebugValues::isSpillInstruction(
const MachineInstr &MI,
480 if (!((
TII->isStoreToStackSlotPostFE(MI, FI) ||
497 if (isKilledReg(MO, Reg))
511 if (isKilledReg(MONext, RegNext) && RegNext ==
Reg)
526 void LiveDebugValues::transferSpillInst(
MachineInstr &MI,
527 OpenRangesSet &OpenRanges,
528 VarLocMap &VarLocIDs,
529 TransferMap &Transfers) {
532 if (!isSpillInstruction(MI, MF, Reg))
536 for (
unsigned ID : OpenRanges.getVarLocs()) {
539 << VarLocIDs[
ID].Var.getVar()->getName() <<
")\n");
540 insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs,
ID);
549 void LiveDebugValues::transferRegisterCopy(
MachineInstr &MI,
550 OpenRangesSet &OpenRanges,
551 VarLocMap &VarLocIDs,
552 TransferMap &Transfers) {
555 if (!
TII->isCopyInstr(MI, SrcRegOp, DestRegOp) || !SrcRegOp->
isKill() ||
559 auto isCalleSavedReg = [&](
unsigned Reg) {
561 if (CalleeSavedRegs.test(*RAI))
566 unsigned SrcReg = SrcRegOp->
getReg();
567 unsigned DestReg = DestRegOp->
getReg();
574 if (!isCalleSavedReg(DestReg))
577 for (
unsigned ID : OpenRanges.getVarLocs()) {
579 insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs,
ID,
587 bool LiveDebugValues::transferTerminatorInst(
MachineInstr &MI,
588 OpenRangesSet &OpenRanges,
589 VarLocInMBB &OutLocs,
590 const VarLocMap &VarLocIDs) {
591 bool Changed =
false;
596 if (OpenRanges.empty())
600 : OpenRanges.getVarLocs()) {
602 dbgs() <<
"Add to OutLocs: ";
603 VarLocIDs[
ID].dump();
605 VarLocSet &VLS = OutLocs[CurMBB];
606 Changed = VLS |= OpenRanges.getVarLocs();
612 bool LiveDebugValues::process(
MachineInstr &MI, OpenRangesSet &OpenRanges,
613 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
614 TransferMap &Transfers,
bool transferChanges) {
615 bool Changed =
false;
616 transferDebugValue(MI, OpenRanges, VarLocIDs);
617 transferRegisterDef(MI, OpenRanges, VarLocIDs);
618 if (transferChanges) {
619 transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers);
620 transferSpillInst(MI, OpenRanges, VarLocIDs, Transfers);
622 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
630 VarLocInMBB &InLocs,
const VarLocMap &VarLocIDs,
633 bool Changed =
false;
644 if (!Visited.
count(p))
646 auto OL = OutLocs.find(p);
648 if (OL == OutLocs.end())
654 InLocsT = OL->second;
656 InLocsT &= OL->second;
662 for (
auto ID : InLocsT)
663 if (!VarLocIDs[
ID].dominates(MBB))
665 InLocsT.intersectWithComplement(KillSet);
671 "Should have processed at least one predecessor");
675 VarLocSet &ILS = InLocs[&MBB];
678 VarLocSet Diff = InLocsT;
679 Diff.intersectWithComplement(ILS);
680 for (
auto ID : Diff) {
684 const VarLoc &DiffIt = VarLocIDs[
ID];
705 bool Changed =
false;
706 bool OLChanged =
false;
707 bool MBBJoined =
false;
710 OpenRangesSet OpenRanges;
713 TransferMap Transfers;
717 std::priority_queue<unsigned int, std::vector<unsigned int>,
718 std::greater<unsigned int>>
720 std::priority_queue<unsigned int, std::vector<unsigned int>,
721 std::greater<unsigned int>>
724 enum :
bool { dontTransferChanges =
false, transferChanges =
true };
733 process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers,
734 dontTransferChanges);
736 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
737 "OutLocs after initialization",
dbgs()));
740 unsigned int RPONumber = 0;
741 for (
auto RI = RPOT.
begin(), RE = RPOT.
end(); RI != RE; ++RI) {
742 OrderToBB[RPONumber] = *RI;
743 BBToOrder[*RI] = RPONumber;
744 Worklist.push(RPONumber);
752 while (!Worklist.empty() || !Pending.empty()) {
758 while (!Worklist.empty()) {
761 MBBJoined =
join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
769 for (
auto &MI : *MBB)
770 OLChanged |= process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers,
774 for (
auto &TR : Transfers)
779 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
780 "OutLocs after propagating",
dbgs()));
781 LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
782 "InLocs after propagating",
dbgs()));
786 for (
auto s : MBB->successors())
787 if (OnPending.
insert(s).second) {
788 Pending.push(BBToOrder[s]);
793 Worklist.swap(Pending);
796 assert(Pending.empty() &&
"Pending should be empty");
799 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
"Final OutLocs",
dbgs()));
800 LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
"Final InLocs",
dbgs()));
818 make_unique<RegScavenger>().
get());
821 bool Changed = ExtendRanges(MF);
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
instr_iterator instr_begin()
bool isCall(QueryType Type=AnyInBundle) const
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
Compute iterated dominance frontiers using a linear time algorithm.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool dominates(const DILocation *DL, MachineBasicBlock *MBB)
dominates - Return true if DebugLoc's lexical scope dominates at least one machine instruction's lexi...
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
unsigned getReg() const
getReg - Returns the register number.
virtual const TargetLowering * getTargetLowering() const
bool hasStoreToStackSlot(const MachineInstr &MI, const MachineMemOperand *&MMO, int &FrameIndex) const override
Check if the instruction or the bundle of instructions has store to stack slots.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
iterator_range< mop_iterator > operands()
static DIExpression * prepend(const DIExpression *Expr, bool DerefBefore, int64_t Offset=0, bool DerefAfter=false, bool StackValue=false)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value...
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
A description of a memory reference used in the backend.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", false, false) LiveDebugValues
Default construct and initialize the pass.
virtual const TargetInstrInfo * getInstrInfo() const
void initializeLiveDebugValuesPass(PassRegistry &)
TargetInstrInfo - Interface to description of machine instruction set.
iterator find(const_arg_type_t< KeyT > Val)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool erase(const KeyT &Val)
This file declares the machine register scavenger class.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
DISubprogram * getSubprogram() const
Get the attached subprogram.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
LLVM_NODISCARD bool empty() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this variable.
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
char & LiveDebugValuesID
LiveDebugValues pass.
void setImm(int64_t immVal)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
self_iterator getIterator()
iterator_range< pred_iterator > predecessors()
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void getMachineBasicBlocks(const DILocation *DL, SmallPtrSetImpl< const MachineBasicBlock *> &MBBs)
getMachineBasicBlocks - Populate given set using machine basic blocks which have machine instructions...
static unsigned isDescribedByReg(const MachineInstr &MI)
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
bool isDebugValue() const
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Information about stack frame layout on the target.
static unsigned isDbgValueDescribedByReg(const MachineInstr &MI)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void setPreservesCFG()
This function should be called by the pass, iff they do not:
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Special value supplied for machine level alias analysis.
static void clear(coro::Shape &Shape)
const MachineBasicBlock * getParent() const
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LexicalScopes - This class provides interface to collect and use lexical scoping information from mac...
virtual const TargetFrameLowering * getFrameLowering() const
LLVM_NODISCARD bool empty() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
unsigned getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
This class implements an extremely fast bulk output stream that can only output to a stream...
bool operator==(uint64_t V1, const APInt &V2)
const MachineOperand & getOperand(unsigned i) const
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
UniqueVector - This class produces a sequential ID number (base 1) for each unique entry that is adde...
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
Properties which a MachineFunction may have at a given point in time.
This file describes how to lower LLVM code to machine code.