46 #define DEBUG_TYPE "livedebugvalues"
48 STATISTIC(NumInserted,
"Number of DBG_VALUE instructions inserted");
72 class UserValueScopes {
84 LS.getMachineBasicBlocks(
DL, LBlocks);
85 return LBlocks.count(MBB) != 0 ||
LS.dominates(
DL, MBB);
90 typedef std::pair<const DILocalVariable *, const DILocation *>
93 struct DebugVariable :
public DebugVariableBase {
95 : DebugVariableBase(Var, InlinedAt) {}
100 bool operator<(
const DebugVariable &DV)
const {
101 if (getVar() == DV.getVar())
102 return getInlinedAt() < DV.getInlinedAt();
103 return getVar() < DV.getVar();
109 const DebugVariable Var;
111 mutable UserValueScopes UVS;
125 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
126 UVS(MI.getDebugLoc(), LS),
Kind(InvalidKind) {
127 static_assert((
sizeof(Loc) ==
sizeof(uint64_t)),
128 "hash does not cover all members of Loc");
131 if (
int RegNo = isDbgValueDescribedByReg(MI)) {
133 Loc.RegisterLoc.RegNo = RegNo;
140 if (Offset == INT64_MIN ||
std::abs(Offset) >= (1LL << 32))
143 Loc.RegisterLoc.Offset =
Offset;
151 return Loc.RegisterLoc.RegNo;
159 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
164 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
168 bool operator<(
const VarLoc &Other)
const {
169 if (Var == Other.Var)
170 return Loc.Hash < Other.Loc.Hash;
171 return Var < Other.Var;
178 struct SpillDebugPair {
188 class OpenRangesSet {
193 const VarLocSet &getVarLocs()
const {
return VarLocs; }
196 void erase(DebugVariable Var) {
197 auto It = Vars.
find(Var);
198 if (It != Vars.end()) {
199 unsigned ID = It->second;
207 void erase(
const VarLocSet &KillSet,
const VarLocMap &VarLocIDs) {
208 VarLocs.intersectWithComplement(KillSet);
209 for (
unsigned ID : KillSet)
210 Vars.erase(VarLocIDs[ID].Var);
214 void insert(
unsigned VarLocID, DebugVariableBase Var) {
215 VarLocs.set(VarLocID);
216 Vars.insert({Var, VarLocID});
227 assert(Vars.empty() == VarLocs.empty() &&
"open ranges are inconsistent");
228 return VarLocs.empty();
234 int extractSpillBaseRegAndOffset(
const MachineInstr &MI,
unsigned &
Reg);
236 void transferDebugValue(
const MachineInstr &MI, OpenRangesSet &OpenRanges,
237 VarLocMap &VarLocIDs);
238 void transferSpillInst(
MachineInstr &MI, OpenRangesSet &OpenRanges,
239 VarLocMap &VarLocIDs, SpillMap &Spills);
240 void transferRegisterDef(
MachineInstr &MI, OpenRangesSet &OpenRanges,
241 const VarLocMap &VarLocIDs);
242 bool transferTerminatorInst(
MachineInstr &MI, OpenRangesSet &OpenRanges,
243 VarLocInMBB &OutLocs,
const VarLocMap &VarLocIDs);
244 bool transfer(
MachineInstr &MI, OpenRangesSet &OpenRanges,
245 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, SpillMap &Spills,
246 bool transferSpills);
249 const VarLocMap &VarLocIDs,
271 const VarLocMap &VarLocIDs,
const char *msg,
296 void LiveDebugValues::getAnalysisUsage(
AnalysisUsage &AU)
const {
307 const VarLocInMBB &V,
308 const VarLocMap &VarLocIDs,
311 Out <<
'\n' << msg <<
'\n';
313 const auto &
L = V.lookup(&BB);
314 Out <<
"MBB: " << BB.getName() <<
":\n";
315 for (
unsigned VLL :
L) {
316 const VarLoc &VL = VarLocIDs[VLL];
317 Out <<
" Var: " << VL.Var.getVar()->getName();
328 int LiveDebugValues::extractSpillBaseRegAndOffset(
const MachineInstr &MI,
331 "Spill instruction does not have exactly one memory operand?");
335 "Inconsistent memory operand in spill instruction");
336 int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
338 return TFI->getFrameIndexReference(*MBB->
getParent(), FI,
Reg);
343 void LiveDebugValues::transferDebugValue(
const MachineInstr &MI,
344 OpenRangesSet &OpenRanges,
345 VarLocMap &VarLocIDs) {
350 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
352 "Expected inlined-at fields to agree");
355 DebugVariable V(Var, InlinedAt);
360 if (isDbgValueDescribedByReg(MI)) {
362 unsigned ID = VarLocIDs.insert(VL);
363 OpenRanges.insert(ID, VL.Var);
368 void LiveDebugValues::transferRegisterDef(
MachineInstr &MI,
369 OpenRangesSet &OpenRanges,
370 const VarLocMap &VarLocIDs) {
379 if (MO.isReg() && MO.isDef() && MO.getReg() &&
380 TRI->isPhysicalRegister(MO.getReg()) &&
381 !(MI.
isCall() && MO.getReg() == SP)) {
384 for (
unsigned ID : OpenRanges.getVarLocs())
387 }
else if (MO.isRegMask()) {
392 for (
unsigned ID : OpenRanges.getVarLocs()) {
393 unsigned Reg = VarLocIDs[
ID].isDescribedByReg();
394 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
399 OpenRanges.erase(KillSet, VarLocIDs);
408 bool LiveDebugValues::isSpillInstruction(
const MachineInstr &MI,
419 if (!((
TII->isStoreToStackSlotPostFE(MI, FI) ||
420 TII->hasStoreToStackSlot(MI, MMO, FI)) &&
428 if (MO.isReg() && MO.isUse() && MO.isKill()) {
442 void LiveDebugValues::transferSpillInst(
MachineInstr &MI,
443 OpenRangesSet &OpenRanges,
444 VarLocMap &VarLocIDs,
448 if (!isSpillInstruction(MI, MF, Reg))
452 for (
unsigned ID : OpenRanges.getVarLocs()) {
455 << VarLocIDs[ID].Var.getVar()->
getName() <<
")\n");
461 int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase);
467 DEBUG(
dbgs() <<
"Creating DBG_VALUE inst for spill: ";
472 SpillDebugPair MIP = {&
MI, SpDMI};
473 Spills.push_back(MIP);
476 OpenRanges.erase(VarLocIDs[ID].Var);
479 VarLoc VL(*SpDMI, LS);
480 unsigned SpillLocID = VarLocIDs.insert(VL);
481 OpenRanges.insert(SpillLocID, VL.Var);
488 bool LiveDebugValues::transferTerminatorInst(
MachineInstr &MI,
489 OpenRangesSet &OpenRanges,
490 VarLocInMBB &OutLocs,
491 const VarLocMap &VarLocIDs) {
492 bool Changed =
false;
497 if (OpenRanges.empty())
500 DEBUG(
for (
unsigned ID : OpenRanges.getVarLocs()) {
502 dbgs() <<
"Add to OutLocs: "; VarLocIDs[
ID].dump();
504 VarLocSet &VLS = OutLocs[CurMBB];
505 Changed = VLS |= OpenRanges.getVarLocs();
511 bool LiveDebugValues::transfer(
MachineInstr &MI, OpenRangesSet &OpenRanges,
512 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
513 SpillMap &Spills,
bool transferSpills) {
514 bool Changed =
false;
515 transferDebugValue(MI, OpenRanges, VarLocIDs);
516 transferRegisterDef(MI, OpenRanges, VarLocIDs);
518 transferSpillInst(MI, OpenRanges, VarLocIDs, Spills);
519 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
527 VarLocInMBB &InLocs,
const VarLocMap &VarLocIDs,
530 bool Changed =
false;
541 if (!Visited.
count(p))
543 auto OL = OutLocs.find(p);
545 if (OL == OutLocs.end())
551 InLocsT = OL->second;
553 InLocsT &= OL->second;
559 for (
auto ID : InLocsT)
560 if (!VarLocIDs[ID].dominates(MBB))
562 InLocsT.intersectWithComplement(KillSet);
568 "Should have processed at least one predecessor");
572 VarLocSet &ILS = InLocs[&MBB];
575 VarLocSet Diff = InLocsT;
576 Diff.intersectWithComplement(ILS);
577 for (
auto ID : Diff) {
581 const VarLoc &DiffIt = VarLocIDs[
ID];
601 DEBUG(
dbgs() <<
"\nDebug Range Extension\n");
603 bool Changed =
false;
604 bool OLChanged =
false;
605 bool MBBJoined =
false;
608 OpenRangesSet OpenRanges;
615 std::priority_queue<unsigned int, std::vector<unsigned int>,
616 std::greater<unsigned int>>
618 std::priority_queue<unsigned int, std::vector<unsigned int>,
619 std::greater<unsigned int>>
629 transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills,
632 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
"OutLocs after initialization",
636 unsigned int RPONumber = 0;
637 for (
auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
638 OrderToBB[RPONumber] = *RI;
639 BBToOrder[*RI] = RPONumber;
640 Worklist.push(RPONumber);
648 while (!Worklist.empty() || !Pending.empty()) {
653 DEBUG(
dbgs() <<
"Processing Worklist\n");
654 while (!Worklist.empty()) {
657 MBBJoined =
join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
665 for (
auto &MI : *MBB)
666 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills,
670 for (
auto &SP : Spills)
675 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
676 "OutLocs after propagating",
dbgs()));
677 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
678 "InLocs after propagating",
dbgs()));
682 for (
auto s : MBB->successors())
683 if (OnPending.
insert(s).second) {
684 Pending.push(BBToOrder[s]);
689 Worklist.swap(Pending);
692 assert(Pending.empty() &&
"Pending should be empty");
695 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
"Final OutLocs",
dbgs()));
696 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
"Final InLocs",
dbgs()));
710 bool Changed = ExtendRanges(MF);
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
instr_iterator instr_begin()
STATISTIC(NumFunctions,"Total number of functions")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE,"Live DEBUG_VALUE analysis", false, false) LiveDebugValues
Default construct and initialize the pass.
MachineInstr & instr_back()
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
iterator_range< mop_iterator > operands()
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
A description of a memory reference used in the backend.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Reg
All possible values of the reg field in the ModR/M byte.
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.
StringRef getName() const
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
unsigned getNumOperands() const
Access to explicit operands of the instruction.
void initialize(const MachineFunction &)
initialize - Scan machine function and constuct lexical scope nest, resets the instance if necessary...
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
void initializeLiveDebugValuesPass(PassRegistry &)
const MachineBasicBlock * getParent() const
TargetInstrInfo - Interface to description of machine instruction set.
iterator find(const_arg_type_t< KeyT > Val)
bool isDebugValue() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
char & LiveDebugValuesID
LiveDebugValues pass.
void setImm(int64_t immVal)
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
iterator_range< pred_iterator > predecessors()
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
unsigned getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
static unsigned isDescribedByReg(const MachineInstr &MI)
virtual const TargetFrameLowering * getFrameLowering() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
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...
virtual const TargetLowering * getTargetLowering() const
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Information about stack frame layout on the target.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Special value supplied for machine level alias analysis.
DISubprogram * getSubprogram() const
Get the attached subprogram.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static void clear(coro::Shape &Shape)
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
LexicalScopes - This class provides interface to collect and use lexical scoping information from mac...
bool isCall(QueryType Type=AnyInBundle) const
APFloat abs(APFloat X)
Returns the absolute value of the argument.
const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
virtual const TargetInstrInfo * getInstrInfo() const
This class implements an extremely fast bulk output stream that can only output to a stream...
std::string Hash(const Unit &U)
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this variable.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool operator==(uint64_t V1, const APInt &V2)
UniqueVector - This class produces a sequential ID number (base 1) for each unique entry that is adde...
Properties which a MachineFunction may have at a given point in time.
This file describes how to lower LLVM code to machine code.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.