22 #define DEBUG_TYPE "livedebug"
50 STATISTIC(NumInsertedDebugValues,
"Number of DBG_VALUEs inserted");
54 "Debug Variable Analysis",
false,
false)
60 void LiveDebugVariables::getAnalysisUsage(
AnalysisUsage &AU)
const {
77 class UserValueScopes {
89 LS.getMachineBasicBlocks(DL, LBlocks);
90 if (LBlocks.count(MBB) != 0 ||
LS.dominates(DL, MBB))
126 void coalesceLocation(
unsigned LocNo);
141 : variable(var), offset(o), IsIndirect(i), dl(L), leader(this),
142 next(0), locInts(alloc)
146 UserValue *getLeader() {
147 UserValue *l = leader;
148 while (l != l->leader)
154 UserValue *getNext()
const {
return next; }
157 bool match(
const MDNode *Var,
unsigned Offset)
const {
158 return Var == variable && Offset == offset;
162 static UserValue *merge(UserValue *L1, UserValue *L2) {
163 L2 = L2->getLeader();
166 L1 = L1->getLeader();
172 End->leader = L1, End = End->next;
174 End->next = L1->next;
185 for (
unsigned i = 0, e = locations.size(); i != e; ++i)
186 if (locations[i].
isReg() &&
187 locations[i].getReg() == LocMO.
getReg() &&
188 locations[i].getSubReg() == LocMO.
getSubReg())
191 for (
unsigned i = 0, e = locations.size(); i != e; ++i)
194 locations.push_back(LocMO);
196 locations.back().clearParent();
198 if (locations.back().isReg())
199 locations.back().setIsUse();
200 return locations.size() - 1;
204 void mapVirtRegs(LDVImpl *LDV);
228 void extendDef(
SlotIndex Idx,
unsigned LocNo,
232 UserValueScopes &UVS);
251 UserValueScopes &UVS);
273 DebugLoc getDebugLoc() {
return dl;}
299 VRMap virtRegToEqClass;
306 UserValue *getUserValue(
const MDNode *Var,
unsigned Offset,
310 UserValue *lookupVirtReg(
unsigned VirtReg);
326 void computeIntervals();
337 virtRegToEqClass.clear();
340 assert((!ModifiedMF || EmitDone) &&
341 "Dbg values are not emitted in LDV");
347 void mapVirtReg(
unsigned VirtReg, UserValue *EC);
362 DV.printExtendedName(OS);
367 OS <<
" [" << I.
start() <<
';' << I.
stop() <<
"):";
368 if (I.
value() == ~0u)
373 for (
unsigned i = 0, e = locations.size(); i != e; ++i) {
374 OS <<
" Loc" << i <<
'=';
375 locations[i].print(OS, TM);
381 OS <<
"********** DEBUG VARIABLES **********\n";
382 for (
unsigned i = 0, e = userValues.size(); i != e; ++i)
383 userValues[i]->print(OS, &MF->getTarget());
386 void UserValue::coalesceLocation(
unsigned LocNo) {
387 unsigned KeepLoc = 0;
388 for (
unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
389 if (KeepLoc == LocNo)
391 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
395 if (KeepLoc == locations.size())
399 unsigned EraseLoc = LocNo;
400 if (KeepLoc > EraseLoc)
402 locations.erase(locations.begin() + EraseLoc);
406 unsigned v = I.
value();
409 else if (v > EraseLoc)
414 void UserValue::mapVirtRegs(LDVImpl *LDV) {
415 for (
unsigned i = 0, e = locations.size(); i != e; ++i)
416 if (locations[i].
isReg() &&
418 LDV->mapVirtReg(locations[i].getReg(),
this);
421 UserValue *LDVImpl::getUserValue(
const MDNode *Var,
unsigned Offset,
423 UserValue *&Leader = userVarMap[Var];
425 UserValue *UV = Leader->getLeader();
427 for (; UV; UV = UV->getNext())
428 if (UV->match(Var, Offset))
432 UserValue *UV =
new UserValue(Var, Offset, IsIndirect, DL, allocator);
433 userValues.push_back(UV);
434 Leader = UserValue::merge(Leader, UV);
438 void LDVImpl::mapVirtReg(
unsigned VirtReg, UserValue *EC) {
440 UserValue *&Leader = virtRegToEqClass[VirtReg];
441 Leader = UserValue::merge(Leader, EC);
444 UserValue *LDVImpl::lookupVirtReg(
unsigned VirtReg) {
445 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
446 return UV->getLeader();
464 UserValue *UV = getUserValue(Var, Offset, IsIndirect, MI->
getDebugLoc());
470 bool Changed =
false;
476 if (!MBBI->isDebugValue()) {
482 LIS->getMBBStartIdx(MBB) :
483 LIS->getInstructionIndex(
llvm::prior(MBBI)).getRegSlot();
486 if (handleDebugValue(MBBI, Idx)) {
487 MBBI = MBB->
erase(MBBI);
491 }
while (MBBI != MBBE && MBBI->isDebugValue());
497 void UserValue::extendDef(
SlotIndex Idx,
unsigned LocNo,
501 UserValueScopes &UVS) {
514 if (!Segment || Segment->valno != VNI) {
519 if (Segment->end < Stop)
520 Stop = Segment->end, ToEnd =
false;
527 if (I.
value() != LocNo || I.
stop() != Start)
535 Stop = I.
start(), ToEnd =
false;
537 else if (!ToEnd && Kills)
543 I.
insert(Start, Stop, LocNo);
548 const std::vector<MachineDomTreeNode*> &Children =
550 for (
unsigned i = 0, e = Children.size(); i != e; ++i) {
552 if (UVS.dominates(MBB))
555 }
while (!Todo.
empty());
575 if (UI.getOperand().getSubReg() || !UI->isCopy())
598 assert(DstVNI && DstVNI->
def == Idx.
getRegSlot() &&
"Bad copy value");
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');
622 assert(CopyMI && CopyMI->
isCopy() &&
"Bad copy value");
623 unsigned LocNo = getLocationNo(CopyMI->
getOperand(0));
625 NewDefs.push_back(std::make_pair(Idx, LocNo));
636 UserValueScopes &UVS) {
641 if (I.
value() != ~0u)
645 for (
unsigned i = 0; i != Defs.
size(); ++i) {
647 unsigned LocNo = Defs[i].second;
651 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT, UVS);
664 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
666 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
673 const VNInfo *VNI = LR->getVNInfoAt(Idx);
675 extendDef(Idx, LocNo, LR, VNI, 0, LIS, MDT, UVS);
680 if (I.
value() == ~0u)
686 void LDVImpl::computeIntervals() {
687 for (
unsigned i = 0, e = userValues.size(); i != e; ++i) {
688 UserValueScopes UVS(userValues[i]->getDebugLoc(),
LS);
689 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
690 userValues[i]->mapVirtRegs(
this);
701 DEBUG(
dbgs() <<
"********** COMPUTING LIVE DEBUG VARIABLES: "
702 << mf.
getName() <<
" **********\n");
708 ModifiedMF = Changed;
716 pImpl =
new LDVImpl(
this);
717 return static_cast<LDVImpl*
>(pImpl)->runOnMachineFunction(mf);
720 void LiveDebugVariables::releaseMemory() {
722 static_cast<LDVImpl*
>(pImpl)->clear();
727 delete static_cast<LDVImpl*
>(pImpl);
738 dbgs() <<
"Splitting Loc" << OldLocNo <<
'\t';
741 bool DidChange =
false;
744 for (
unsigned i = 0; i != NewRegs.
size(); ++i) {
750 unsigned NewLocNo = ~0u;
754 if (!LocMapI.
valid())
758 while (LocMapI.
valid() && LII != LIE) {
765 if (LocMapI.
value() == OldLocNo && LII->start < LocMapI.
stop()) {
767 if (NewLocNo == ~0u) {
769 MO.
setSubReg(locations[OldLocNo].getSubReg());
770 NewLocNo = getLocationNo(MO);
778 if (LStart < LII->start)
780 if (LStop > LII->end)
787 if (LStart < LocMapI.
start()) {
790 assert(LocMapI.
valid() &&
"Unexpected coalescing");
792 if (LStop > LocMapI.
stop()) {
794 LocMapI.
insert(LII->end, LStop, OldLocNo);
800 if (LII->end < LocMapI.
stop()) {
806 if (!LocMapI.
valid())
814 locations.erase(locations.begin() + OldLocNo);
816 while (LocMapI.
valid()) {
817 unsigned v = LocMapI.
value();
820 << LocMapI.
stop() <<
")\n");
836 bool DidChange =
false;
839 for (
unsigned i = locations.size(); i ; --i) {
840 unsigned LocNo = i-1;
844 DidChange |= splitLocation(LocNo, NewRegs, LIS);
850 bool DidChange =
false;
851 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
852 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
858 UserValue *UV = lookupVirtReg(OldReg);
859 for (
unsigned i = 0; i != NewRegs.
size(); ++i)
860 mapVirtReg(NewRegs[i], UV);
866 static_cast<LDVImpl*
>(pImpl)->
splitRegister(OldReg, NewRegs);
872 for (
unsigned i = locations.size(); i ; --i) {
873 unsigned LocNo = i-1;
879 unsigned VirtReg = Loc.
getReg();
893 coalesceLocation(LocNo);
921 DebugLoc UserValue::findDebugLoc() {
932 ++NumInsertedDebugValues;
936 IsIndirect, Loc.
getReg(), offset, variable);
949 unsigned LocNo = I.value();
950 DEBUG(
dbgs() <<
"\t[" << Start <<
';' << Stop <<
"):" << LocNo);
954 DEBUG(
dbgs() <<
" BB#" << MBB->getNumber() <<
'-' << MBBEnd);
955 insertDebugValue(MBB, Start, LocNo, LIS, TII);
958 while(Stop > MBBEnd) {
964 DEBUG(
dbgs() <<
" BB#" << MBB->getNumber() <<
'-' << MBBEnd);
965 insertDebugValue(MBB, Start, LocNo, LIS, TII);
975 void LDVImpl::emitDebugValues(
VirtRegMap *VRM) {
976 DEBUG(
dbgs() <<
"********** EMITTING LIVE DEBUG VARIABLES **********\n");
978 for (
unsigned i = 0, e = userValues.size(); i != e; ++i) {
979 DEBUG(userValues[i]->print(
dbgs(), &MF->getTarget()));
980 userValues[i]->rewriteLocations(*VRM, *TRI);
981 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
995 static_cast<LDVImpl*
>(pImpl)->
print(
dbgs());
static bool isReg(const MCInst &MI, unsigned OpNo)
void setValueUnchecked(ValT x)
void DeleteContainerPointers(Container &C)
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
void push_back(const T &Elt)
instr_iterator erase(instr_iterator I)
const Segment * getSegmentContaining(SlotIndex Idx) const
static PassRegistry * getPassRegistry()
Segments::iterator iterator
SlotIndex def
The index of the defining instruction.
MachineFunction & getMachineFunction() const
SlotIndex getBaseIndex() const
STATISTIC(NumInsertedDebugValues,"Number of DBG_VALUEs inserted")
iterator getFirstTerminator()
void initializeLiveDebugVariablesPass(PassRegistry &)
SlotIndex getInstructionIndex(const MachineInstr *instr) const
Returns the base index of the given instruction.
static bool isVirtualRegister(unsigned Reg)
iterator advanceTo(iterator I, SlotIndex Pos)
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const
Return the last index in the given basic block.
MDNode - a tuple of other values.
void setMap(const IntervalMap &m)
const MDNode * getMetadata() const
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
static use_nodbg_iterator use_nodbg_end()
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
bool isTerminator(QueryType Type=AnyInBundle) const
LoopInfoBase< BlockT, LoopT > * LI
bool match(Val *V, const Pattern &P)
#define INITIALIZE_PASS_DEPENDENCY(depName)
void emitDebugValues(VirtRegMap *VRM)
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)
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
ID
LLVM Calling Convention Representation.
const MachineInstrBuilder & addImm(int64_t Val) const
unsigned getNumOperands() const
MachineDomTreeNode * getNode(MachineBasicBlock *BB) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
void erase()
erase - Erase the current interval.
size_t size() const
size - Get the array size.
SlotIndex getPrevIndex() const
void setStartUnchecked(KeyT a)
bundle_iterator< MachineInstr, instr_iterator > iterator
initializer< Ty > init(const Ty &Val)
const KeyT & start() const
start - Return the beginning of the current interval.
iterator SkipPHIsAndLabels(iterator I)
bool isIndirectDebugValue() const
const MachineOperand & getOperand(unsigned i) const
Two Address instruction pass
void goToBegin()
goToBegin - Move to the first interval in map.
ItTy next(ItTy it, Dist n)
void substPhysReg(unsigned Reg, const TargetRegisterInfo &)
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
unsigned getSubReg() const
const MCInstrDesc & get(unsigned Opcode) const
const std::vector< DomTreeNodeBase< NodeT > * > & getChildren() const
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.
INITIALIZE_PASS_BEGIN(LiveDebugVariables,"livedebugvars","Debug Variable Analysis", false, false) INITIALIZE_PASS_END(LiveDebugVariables
void splitRegister(unsigned OldReg, ArrayRef< unsigned > NewRegs, LiveIntervals &LIS)
LiveInterval & getInterval(unsigned Reg)
bool valid() const
valid - Return true if the current position is valid, false for end().
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static void collectDebugValues(MachineInstr *MI, SmallVectorImpl< MachineInstr * > &DbgValues)
const KeyT & stop() const
stop - Return the end of the current interval.
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.
static bool isPhysicalRegister(unsigned Reg)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
void setReg(unsigned Reg)
DBG_VALUE - a mapping of the llvm.dbg.value intrinsic.
static cl::opt< bool > EnableLDV("live-debug-variables", cl::init(true), cl::desc("Enable the live debug variables pass"), cl::Hidden)
void setSubReg(unsigned subReg)
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
const TargetMachine & getTarget() const
virtual const TargetRegisterInfo * getRegisterInfo() 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.
SlotIndex getRegSlot(bool EC=false) const
unsigned getReg() const
getReg - Returns the register number.
void setStopUnchecked(KeyT b)
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.
BasicBlockListType::iterator iterator
ItTy prior(ItTy it, Dist n)
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Return the first index in the given basic block.
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
const MCRegisterInfo & MRI
StringRef getName() const
const ValT & value() const
value - Return the mapped value at the current interval.
bool isIdenticalTo(const MachineOperand &Other) const
static MachineBasicBlock::iterator findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, LiveIntervals &LIS)
SlotIndex getNextSlot() const
SlotIndex - An opaque wrapper around machine indexes.
static MachineOperand CreateFI(int Idx)
use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const
LiveRange & getRegUnit(unsigned Unit)
DebugLoc getDebugLoc() const
void dump()
dump - Print data structures to dbgs().