diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2018-07-28 10:51:19 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2018-07-28 10:51:19 +0000 |
commit | eb11fae6d08f479c0799db45860a98af528fa6e7 (patch) | |
tree | 44d492a50c8c1a7eb8e2d17ea3360ec4d066f042 /lib/CodeGen/LiveDebugVariables.cpp | |
parent | b8a2042aa938069e862750553db0e4d82d25822c (diff) |
Notes
Diffstat (limited to 'lib/CodeGen/LiveDebugVariables.cpp')
-rw-r--r-- | lib/CodeGen/LiveDebugVariables.cpp | 131 |
1 files changed, 92 insertions, 39 deletions
diff --git a/lib/CodeGen/LiveDebugVariables.cpp b/lib/CodeGen/LiveDebugVariables.cpp index 34572f24c181c..3ff03ec4a7ee7 100644 --- a/lib/CodeGen/LiveDebugVariables.cpp +++ b/lib/CodeGen/LiveDebugVariables.cpp @@ -44,6 +44,7 @@ #include "llvm/CodeGen/TargetRegisterInfo.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/CodeGen/VirtRegMap.h" +#include "llvm/Config/llvm-config.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DebugLoc.h" #include "llvm/IR/Function.h" @@ -223,7 +224,12 @@ public: return L1; } - /// getLocationNo - Return the location number that matches Loc. + /// Return the location number that matches Loc. + /// + /// For undef values we always return location number UndefLocNo without + /// inserting anything in locations. Since locations is a vector and the + /// location number is the position in the vector and UndefLocNo is ~0, + /// we would need a very big vector to put the value at the right position. unsigned getLocationNo(const MachineOperand &LocMO) { if (LocMO.isReg()) { if (LocMO.getReg() == 0) @@ -242,8 +248,11 @@ public: // We are storing a MachineOperand outside a MachineInstr. locations.back().clearParent(); // Don't store def operands. - if (locations.back().isReg()) + if (locations.back().isReg()) { + if (locations.back().isDef()) + locations.back().setIsDead(false); locations.back().setIsUse(); + } return locations.size() - 1; } @@ -298,7 +307,7 @@ public: /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is /// live. Returns true if any changes were made. - bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, + bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS); /// rewriteLocations - Rewrite virtual register locations according to the @@ -507,10 +516,43 @@ bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) { if (MI.getNumOperands() != 4 || !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) || !MI.getOperand(2).isMetadata()) { - DEBUG(dbgs() << "Can't handle " << MI); + LLVM_DEBUG(dbgs() << "Can't handle " << MI); return false; } + // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual + // register that hasn't been defined yet. If we do not remove those here, then + // the re-insertion of the DBG_VALUE instruction after register allocation + // will be incorrect. + // TODO: If earlier passes are corrected to generate sane debug information + // (and if the machine verifier is improved to catch this), then these checks + // could be removed or replaced by asserts. + bool Discard = false; + if (MI.getOperand(0).isReg() && + TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg())) { + const unsigned Reg = MI.getOperand(0).getReg(); + if (!LIS->hasInterval(Reg)) { + // The DBG_VALUE is described by a virtual register that does not have a + // live interval. Discard the DBG_VALUE. + Discard = true; + LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx + << " " << MI); + } else { + // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg + // is defined dead at Idx (where Idx is the slot index for the instruction + // preceeding the DBG_VALUE). + const LiveInterval &LI = LIS->getInterval(Reg); + LiveQueryResult LRQ = LI.Query(Idx); + if (!LRQ.valueOutOrDead()) { + // We have found a DBG_VALUE with the value in a virtual register that + // is not live. Discard the DBG_VALUE. + Discard = true; + LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx + << " " << MI); + } + } + } + // Get or create the UserValue for (variable,offset) here. bool IsIndirect = MI.getOperand(1).isImm(); if (IsIndirect) @@ -519,7 +561,13 @@ bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) { const DIExpression *Expr = MI.getDebugExpression(); UserValue *UV = getUserValue(Var, Expr, MI.getDebugLoc()); - UV->addDef(Idx, MI.getOperand(0), IsIndirect); + if (!Discard) + UV->addDef(Idx, MI.getOperand(0), IsIndirect); + else { + MachineOperand MO = MachineOperand::CreateReg(0U, false); + MO.setIsDebug(); + UV->addDef(Idx, MO, false); + } return true; } @@ -645,7 +693,8 @@ void UserValue::addDefsFromCopies( if (CopyValues.empty()) return; - DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); + LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI + << '\n'); // Try to add defs of the copied values for each kill point. for (unsigned i = 0, e = Kills.size(); i != e; ++i) { @@ -659,8 +708,8 @@ void UserValue::addDefsFromCopies( LocMap::iterator I = locInts.find(Idx); if (I.valid() && I.start() <= Idx) continue; - DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" - << DstVNI->id << " in " << *DstLI << '\n'); + LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" + << DstVNI->id << " in " << *DstLI << '\n'); MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); @@ -717,13 +766,6 @@ void UserValue::computeIntervals(MachineRegisterInfo &MRI, // function). } - // Erase all the undefs. - for (LocMap::iterator I = locInts.begin(); I.valid();) - if (I.value().isUndef()) - I.erase(); - else - ++I; - // The computed intervals may extend beyond the range of the debug // location's lexical scope. In this case, splitting of an interval // can result in an interval outside of the scope being created, @@ -808,12 +850,12 @@ bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { MF = &mf; LIS = &pass.getAnalysis<LiveIntervals>(); TRI = mf.getSubtarget().getRegisterInfo(); - DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " - << mf.getName() << " **********\n"); + LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " + << mf.getName() << " **********\n"); bool Changed = collectDebugValues(mf); computeIntervals(); - DEBUG(print(dbgs())); + LLVM_DEBUG(print(dbgs())); ModifiedMF = Changed; return Changed; } @@ -859,7 +901,7 @@ LiveDebugVariables::~LiveDebugVariables() { bool UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, LiveIntervals& LIS) { - DEBUG({ + LLVM_DEBUG({ dbgs() << "Splitting Loc" << OldLocNo << '\t'; print(dbgs(), nullptr); }); @@ -942,17 +984,22 @@ UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, while (LocMapI.valid()) { DbgValueLocation v = LocMapI.value(); if (v.locNo() == OldLocNo) { - DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' - << LocMapI.stop() << ")\n"); + LLVM_DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' + << LocMapI.stop() << ")\n"); LocMapI.erase(); } else { - if (v.locNo() > OldLocNo) + // Undef values always have location number UndefLocNo, so don't change + // locNo in that case. See getLocationNo(). + if (!v.isUndef() && v.locNo() > OldLocNo) LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1)); ++LocMapI; } } - DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);}); + LLVM_DEBUG({ + dbgs() << "Split result: \t"; + print(dbgs(), nullptr); + }); return DidChange; } @@ -1052,6 +1099,10 @@ void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI, // physical register. for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { DbgValueLocation Loc = I.value(); + // Undef values don't exist in locations (and thus not in LocNoMap either) + // so skip over them. See getLocationNo(). + if (Loc.isUndef()) + continue; unsigned NewLocNo = LocNoMap[Loc.locNo()]; I.setValueUnchecked(Loc.changeLocNo(NewLocNo)); I.setStart(I.start()); @@ -1094,7 +1145,7 @@ findNextInsertLocation(MachineBasicBlock *MBB, unsigned Reg = LocMO.getReg(); // Find the next instruction in the MBB that define the register Reg. - while (I != MBB->end()) { + while (I != MBB->end() && !I->isTerminator()) { if (!LIS.isNotInMIMap(*I) && SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I))) break; @@ -1116,7 +1167,15 @@ void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, // Only search within the current MBB. StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx; MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS); - MachineOperand &MO = locations[Loc.locNo()]; + // Undef values don't exist in locations so create new "noreg" register MOs + // for them. See getLocationNo(). + MachineOperand MO = !Loc.isUndef() ? + locations[Loc.locNo()] : + MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false, + /* isKill */ false, /* isDead */ false, + /* isUndef */ false, /* isEarlyClobber */ false, + /* SubReg */ 0, /* isDebug */ true); + ++NumInsertedDebugValues; assert(cast<DILocalVariable>(Variable) @@ -1137,14 +1196,8 @@ void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index"); do { - MachineInstrBuilder MIB = - BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) - .add(MO); - if (IsIndirect) - MIB.addImm(0U); - else - MIB.addReg(0U, RegState::Debug); - MIB.addMetadata(Variable).addMetadata(Expr); + BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE), + IsIndirect, MO, Variable, Expr); // Continue and insert DBG_VALUES after every redefinition of register // associated with the debug value within the range @@ -1170,11 +1223,11 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, if (trimmedDefs.count(Start)) Start = Start.getPrevIndex(); - DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo()); + LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo()); MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB); - DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); + LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, LIS, TII, TRI); // This interval may span multiple basic blocks. // Insert a DBG_VALUE into each one. @@ -1184,10 +1237,10 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, if (++MBB == MFEnd) break; MBBEnd = LIS.getMBBEndIdx(&*MBB); - DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); + LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, LIS, TII, TRI); } - DEBUG(dbgs() << '\n'); + LLVM_DEBUG(dbgs() << '\n'); if (MBB == MFEnd) break; @@ -1196,13 +1249,13 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, } void LDVImpl::emitDebugValues(VirtRegMap *VRM) { - DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); + LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); if (!MF) return; const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); BitVector SpilledLocations; for (unsigned i = 0, e = userValues.size(); i != e; ++i) { - DEBUG(userValues[i]->print(dbgs(), TRI)); + LLVM_DEBUG(userValues[i]->print(dbgs(), TRI)); userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations); userValues[i]->emitDebugValues(VRM, *LIS, *TII, *TRI, SpilledLocations); } |