48 #define DEBUG_TYPE "livedebug"
54 STATISTIC(NumInsertedDebugValues,
"Number of DBG_VALUEs inserted");
58 "Debug Variable Analysis",
false,
false)
64 void LiveDebugVariables::getAnalysisUsage(
AnalysisUsage &AU)
const {
108 void coalesceLocation(
unsigned LocNo);
121 UserValue(
const MDNode *var,
const MDNode *expr,
unsigned o,
bool i,
123 : Variable(var), Expression(expr), offset(o), IsIndirect(i),
124 dl(std::move(L)), leader(
this), next(
nullptr), locInts(alloc) {}
127 UserValue *getLeader() {
128 UserValue *l = leader;
129 while (l != l->leader)
135 UserValue *getNext()
const {
return next; }
139 unsigned Offset,
bool indirect)
const {
140 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA &&
141 Offset == offset && indirect == IsIndirect;
145 static UserValue *
merge(UserValue *L1, UserValue *L2) {
146 L2 = L2->getLeader();
149 L1 = L1->getLeader();
159 End->next = L1->next;
170 for (
unsigned i = 0, e = locations.size(); i != e; ++
i)
171 if (locations[i].
isReg() &&
172 locations[
i].getReg() == LocMO.
getReg() &&
173 locations[
i].getSubReg() == LocMO.
getSubReg())
176 for (
unsigned i = 0, e = locations.size(); i != e; ++
i)
179 locations.push_back(LocMO);
181 locations.back().clearParent();
183 if (locations.back().isReg())
184 locations.back().setIsUse();
185 return locations.size() - 1;
189 void mapVirtRegs(LDVImpl *LDV);
212 void extendDef(
SlotIndex Idx,
unsigned LocNo,
224 void addDefsFromCopies(
LiveInterval *LI,
unsigned LocNo,
249 DebugLoc getDebugLoc() {
return dl;}
273 VRMap virtRegToEqClass;
280 UserValue *getUserValue(
const MDNode *Var,
const MDNode *Expr,
284 UserValue *lookupVirtReg(
unsigned VirtReg);
300 void computeIntervals();
311 virtRegToEqClass.clear();
314 assert((!ModifiedMF || EmitDone) &&
315 "Dbg values are not emitted in LDV");
321 void mapVirtReg(
unsigned VirtReg, UserValue *EC);
338 auto *Scope = cast<DIScope>(DL.
getScope());
340 CommentOS << Scope->getFilename();
341 CommentOS <<
':' << DL.
getLine();
343 CommentOS <<
':' << DL.
getCol();
359 OS << Res <<
"," << V->
getLine();
360 if (
auto *InlinedAt = DL->getInlinedAt()) {
361 if (
DebugLoc InlinedAtDL = InlinedAt) {
370 auto *DV = cast<DILocalVariable>(Variable);
378 OS <<
" [" <<
I.start() <<
';' <<
I.stop() <<
"):";
379 if (
I.value() == ~0u)
384 for (
unsigned i = 0, e = locations.size();
i != e; ++
i) {
385 OS <<
" Loc" <<
i <<
'=';
386 locations[
i].print(OS, TRI);
392 OS <<
"********** DEBUG VARIABLES **********\n";
393 for (
unsigned i = 0, e = userValues.size();
i != e; ++
i)
394 userValues[
i]->print(OS, TRI);
397 void UserValue::coalesceLocation(
unsigned LocNo) {
398 unsigned KeepLoc = 0;
399 for (
unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
400 if (KeepLoc == LocNo)
402 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
406 if (KeepLoc == locations.size())
410 unsigned EraseLoc = LocNo;
411 if (KeepLoc > EraseLoc)
413 locations.erase(locations.begin() + EraseLoc);
417 unsigned v =
I.value();
420 else if (v > EraseLoc)
421 I.setValueUnchecked(v-1);
425 void UserValue::mapVirtRegs(LDVImpl *LDV) {
426 for (
unsigned i = 0, e = locations.size();
i != e; ++
i)
427 if (locations[
i].
isReg() &&
429 LDV->mapVirtReg(locations[
i].getReg(),
this);
432 UserValue *LDVImpl::getUserValue(
const MDNode *Var,
const MDNode *Expr,
433 unsigned Offset,
bool IsIndirect,
435 UserValue *&Leader = userVarMap[Var];
437 UserValue *UV = Leader->getLeader();
439 for (; UV; UV = UV->getNext())
440 if (UV->match(Var, Expr, DL->getInlinedAt(),
Offset, IsIndirect))
444 userValues.push_back(
445 make_unique<UserValue>(Var, Expr, Offset, IsIndirect, DL, allocator));
446 UserValue *UV = userValues.back().get();
451 void LDVImpl::mapVirtReg(
unsigned VirtReg, UserValue *EC) {
453 UserValue *&Leader = virtRegToEqClass[VirtReg];
457 UserValue *LDVImpl::lookupVirtReg(
unsigned VirtReg) {
458 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
459 return UV->getLeader();
478 UserValue *UV = getUserValue(Var, Expr, Offset, IsIndirect, MI.
getDebugLoc());
484 bool Changed =
false;
490 if (!MBBI->isDebugValue()) {
497 ? LIS->getMBBStartIdx(MBB)
498 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
501 if (handleDebugValue(*MBBI, Idx)) {
502 MBBI = MBB->
erase(MBBI);
506 }
while (MBBI != MBBE && MBBI->isDebugValue());
526 if (!Segment || Segment->valno != VNI) {
531 if (Segment->end < Stop) {
541 if (I.
value() != LocNo || I.
stop() != Start)
553 else if (!ToEnd && Kills)
557 I.
insert(Start, Stop, LocNo);
561 UserValue::addDefsFromCopies(
LiveInterval *LI,
unsigned LocNo,
576 if (MO.getSubReg() || !MI->
isCopy())
599 CopyValues.
push_back(std::make_pair(DstLI, DstVNI));
602 if (CopyValues.
empty())
605 DEBUG(
dbgs() <<
"Got " << CopyValues.
size() <<
" copies of " << *LI <<
'\n');
608 for (
unsigned i = 0, e = Kills.
size();
i != e; ++
i) {
610 for (
unsigned j = 0, e = CopyValues.
size(); j != e; ++j) {
612 const VNInfo *DstVNI = CopyValues[j].second;
619 DEBUG(
dbgs() <<
"Kill at " << Idx <<
" covered by valno #"
620 << DstVNI->
id <<
" in " << *DstLI <<
'\n');
623 unsigned LocNo = getLocationNo(CopyMI->
getOperand(0));
625 NewDefs.push_back(std::make_pair(Idx, LocNo));
639 if (I.
value() != ~0u)
643 for (
unsigned i = 0;
i != Defs.
size(); ++
i) {
645 unsigned LocNo = Defs[
i].second;
649 extendDef(Idx, LocNo,
nullptr,
nullptr,
nullptr, LIS);
656 const VNInfo *VNI =
nullptr;
662 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS);
664 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
671 const VNInfo *VNI = LR->getVNInfoAt(Idx);
673 extendDef(Idx, LocNo, LR, VNI,
nullptr, LIS);
678 if (I.
value() == ~0u)
684 void LDVImpl::computeIntervals() {
685 for (
unsigned i = 0, e = userValues.size();
i != e; ++
i) {
686 userValues[
i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS);
687 userValues[
i]->mapVirtRegs(
this);
696 DEBUG(
dbgs() <<
"********** COMPUTING LIVE DEBUG VARIABLES: "
697 << mf.
getName() <<
" **********\n");
702 ModifiedMF = Changed;
708 for (
auto MBBI = MBB.
begin(), MBBE = MBB.
end(); MBBI != MBBE; ) {
709 if (!MBBI->isDebugValue()) {
713 MBBI = MBB.
erase(MBBI);
726 pImpl =
new LDVImpl(
this);
727 return static_cast<LDVImpl*
>(pImpl)->runOnMachineFunction(mf);
730 void LiveDebugVariables::releaseMemory() {
732 static_cast<LDVImpl*
>(pImpl)->
clear();
737 delete static_cast<LDVImpl*
>(pImpl);
748 dbgs() <<
"Splitting Loc" << OldLocNo <<
'\t';
749 print(
dbgs(),
nullptr);
751 bool DidChange =
false;
754 for (
unsigned i = 0;
i != NewRegs.
size(); ++
i) {
760 unsigned NewLocNo = ~0u;
764 if (!LocMapI.
valid())
768 while (LocMapI.
valid() && LII != LIE) {
775 if (LocMapI.
value() == OldLocNo && LII->start < LocMapI.
stop()) {
777 if (NewLocNo == ~0u) {
779 MO.
setSubReg(locations[OldLocNo].getSubReg());
780 NewLocNo = getLocationNo(MO);
788 if (LStart < LII->start)
790 if (LStop > LII->end)
797 if (LStart < LocMapI.
start()) {
800 assert(LocMapI.
valid() &&
"Unexpected coalescing");
802 if (LStop > LocMapI.
stop()) {
804 LocMapI.
insert(LII->end, LStop, OldLocNo);
810 if (LII->end < LocMapI.
stop()) {
816 if (!LocMapI.
valid())
824 locations.erase(locations.begin() + OldLocNo);
826 while (LocMapI.
valid()) {
827 unsigned v = LocMapI.
value();
830 << LocMapI.
stop() <<
")\n");
839 DEBUG({
dbgs() <<
"Split result: \t"; print(
dbgs(),
nullptr);});
846 bool DidChange =
false;
849 for (
unsigned i = locations.size();
i ; --
i) {
850 unsigned LocNo = i-1;
854 DidChange |= splitLocation(LocNo, NewRegs, LIS);
860 bool DidChange =
false;
861 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
862 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
868 UserValue *UV = lookupVirtReg(OldReg);
869 for (
unsigned i = 0; i != NewRegs.
size(); ++
i)
870 mapVirtReg(NewRegs[i], UV);
876 static_cast<LDVImpl*
>(pImpl)->
splitRegister(OldReg, NewRegs);
882 for (
unsigned i = locations.size();
i ; --
i) {
883 unsigned LocNo = i-1;
889 unsigned VirtReg = Loc.
getReg();
903 coalesceLocation(LocNo);
937 ++NumInsertedDebugValues;
939 assert(cast<DILocalVariable>(Variable)
940 ->isValidLocationForIntrinsic(getDebugLoc()) &&
941 "Expected inlined-at fields to agree");
943 BuildMI(*MBB, I, getDebugLoc(), TII.
get(TargetOpcode::DBG_VALUE),
944 IsIndirect, Loc.
getReg(), offset, Variable, Expression);
946 BuildMI(*MBB, I, getDebugLoc(), TII.
get(TargetOpcode::DBG_VALUE))
949 .addMetadata(Variable)
950 .addMetadata(Expression);
960 unsigned LocNo = I.value();
961 DEBUG(
dbgs() <<
"\t[" << Start <<
';' << Stop <<
"):" << LocNo);
965 DEBUG(
dbgs() <<
" BB#" << MBB->getNumber() <<
'-' << MBBEnd);
966 insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
969 while(Stop > MBBEnd) {
975 DEBUG(
dbgs() <<
" BB#" << MBB->getNumber() <<
'-' << MBBEnd);
976 insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
986 void LDVImpl::emitDebugValues(
VirtRegMap *VRM) {
987 DEBUG(
dbgs() <<
"********** EMITTING LIVE DEBUG VARIABLES **********\n");
991 for (
unsigned i = 0, e = userValues.size(); i != e; ++
i) {
992 DEBUG(userValues[i]->print(
dbgs(), TRI));
993 userValues[
i]->rewriteLocations(*VRM, *TRI);
994 userValues[
i]->emitDebugValues(VRM, *LIS, *TII);
1004 bool LiveDebugVariables::doInitialization(
Module &M) {
1011 static_cast<LDVImpl*
>(pImpl)->
print(
dbgs());
static bool isReg(const MCInst &MI, unsigned OpNo)
void setValueUnchecked(ValT x)
setValueUnchecked - Change the mapped value of the current interval without checking for coalescing...
void push_back(const T &Elt)
void setValue(ValT x)
setValue - Change the mapped value of the current interval.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Segments::iterator iterator
SlotIndex def
The index of the defining instruction.
STATISTIC(NumFunctions,"Total number of functions")
MDNode * getScope() const
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
MachineFunction & getMachineFunction() const
A Module instance is used to store all the information related to an LLVM module. ...
static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS, const LLVMContext &Ctx)
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
LiveInterval - This class represents the liveness of a register, or stack slot.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void initializeLiveDebugVariablesPass(PassRegistry &)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
iterator advanceTo(iterator I, SlotIndex Pos)
advanceTo - Advance the specified iterator to point to the Segment containing the specified position...
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const
Return the last index in the given basic block.
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void setMap(const IntervalMap &m)
setMap - Change the map iterated over.
VNInfo - Value Number Information.
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
This class represents the liveness of a register, stack slot, etc.
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
safe Safe Stack instrumentation pass
bool match(Val *V, const Pattern &P)
#define INITIALIZE_PASS_DEPENDENCY(depName)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
~LiveDebugVariables() override
void emitDebugValues(VirtRegMap *VRM)
emitDebugValues - Emit new DBG_VALUE instructions reflecting the changes that happened during registe...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void advanceTo(KeyT x)
advanceTo - Move to the first interval with stop >= x, or end().
RecyclingAllocator - This class wraps an Allocator, adding the functionality of recycling deleted obj...
StringRef getName() const
LLVM_NODISCARD bool empty() const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
void erase()
erase - Erase the current interval.
size_t size() const
size - Get the array size.
SlotIndex getPrevIndex() const
Returns the previous index.
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
void setStartUnchecked(KeyT a)
setStartUnchecked - Move the start of the current interval without checking for coalescing or overlap...
const MachineBasicBlock * getParent() const
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
const KeyT & start() const
start - Return the beginning of the current interval.
unsigned const MachineRegisterInfo * MRI
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
static void removeDebugValues(MachineFunction &mf)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This is an important class for using LLVM in a threaded context.
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void find(KeyT x)
find - Move to the first interval with stop >= x, or end().
const MachineOperand & getOperand(unsigned i) const
void goToBegin()
goToBegin - Move to the first interval in map.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
static const unsigned End
void substPhysReg(unsigned Reg, const TargetRegisterInfo &)
substPhysReg - Substitute the current register with the physical register Reg, taking any existing Su...
self_iterator getIterator()
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
unsigned getSubReg() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
DILocation * getInlinedAt() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
unsigned id
The ID number of this value.
IntervalMap< SlotIndex, unsigned, 4 > LocMap
LocMap - Map of where a user value is live, and its location.
Iterator for intrusive lists based on ilist_node.
INITIALIZE_PASS_BEGIN(LiveDebugVariables,"livedebugvars","Debug Variable Analysis", false, false) INITIALIZE_PASS_END(LiveDebugVariables
void splitRegister(unsigned OldReg, ArrayRef< unsigned > NewRegs, LiveIntervals &LIS)
splitRegister - Move any user variables in OldReg to the live ranges in NewRegs where they are live...
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...
LiveInterval & getInterval(unsigned Reg)
bool valid() const
valid - Return true if the current position is valid, false for end().
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void dump()
dump - Print data structures to dbgs().
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
DISubprogram * getSubprogram() const
Get the attached subprogram.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static void clear(coro::Shape &Shape)
const KeyT & stop() const
stop - Return the end of the current interval.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
SlotIndex beginIndex() const
beginIndex - Return the lowest numbered slot covered.
void insert(KeyT a, KeyT b, ValT y)
insert - Insert mapping [a;b] -> y before the current position.
Representation of each machine instruction.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
static void printExtendedName(raw_ostream &OS, const DILocalVariable *V, const DILocation *DL)
void setReg(unsigned Reg)
Change the register this operand corresponds to.
static cl::opt< bool > EnableLDV("live-debug-variables", cl::init(true), cl::desc("Enable the live debug variables pass"), cl::Hidden)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
void setSubReg(unsigned subReg)
LLVMContext & getContext() const
bool hasInterval(unsigned Reg) const
bool isAssignedReg(unsigned virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
std::vector< uint8_t > Unit
static void collectDebugValues(MachineInstr &MI, SmallVectorImpl< MachineInstr * > &DbgValues)
collectDebgValues - Scan instructions following MI and collect any matching DBG_VALUEs.
unsigned getReg() const
getReg - Returns the register number.
void setStopUnchecked(KeyT b)
setStopUnchecked - Move the end of the current interval without checking for coalescing or overlaps...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int getStackSlot(unsigned virtReg) const
returns the stack slot mapped to the specified virtual register
static cl::opt< bool, true > Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag))
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
This class implements an extremely fast bulk output stream that can only output to a stream...
iterator SkipPHIsLabelsAndDebug(iterator I)
Return the first instruction in MBB after I that is not a PHI, label or debug.
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Return the first index in the given basic block.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned getPhys(unsigned virtReg) const
returns the physical register mapped to the specified virtual register
virtual void print(raw_ostream &O, const Module *M) const
print - Print out the internal state of the pass.
StringRef - Represent a constant reference to a string, i.e.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const ValT & value() const
value - Return the mapped value at the current interval.
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static MachineBasicBlock::iterator findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, LiveIntervals &LIS)
findInsertLocation - Find an iterator for inserting a DBG_VALUE instruction.
SlotIndex getNextSlot() const
Returns the next slot in the index list.
SlotIndex - An opaque wrapper around machine indexes.
static MachineOperand CreateFI(int Idx)
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
LiveRange & getRegUnit(unsigned Unit)
getRegUnit - Return the live range for Unit.