Index: llvm/lib/CodeGen/LiveDebugVariables.cpp =================================================================== --- llvm/lib/CodeGen/LiveDebugVariables.cpp +++ llvm/lib/CodeGen/LiveDebugVariables.cpp @@ -97,27 +97,27 @@ enum : unsigned { UndefLocNo = ~0U }; /// Describes a debug variable value by location number and expression. -class DbgValueLocation { +class DbgVariableValue { public: - DbgValueLocation() = default; - DbgValueLocation(unsigned LocNo, const DIExpression &Expression) + DbgVariableValue() = default; + DbgVariableValue(unsigned LocNo, const DIExpression &Expression) : LocNo(LocNo), Expression(&Expression) {} - unsigned locNo() const { return LocNo; } + unsigned getLocNo() const { return LocNo; } const DIExpression *getExpression() { return Expression; } - bool isUndef() const { return locNo() == UndefLocNo; } + bool isUndef() const { return getLocNo() == UndefLocNo; } - DbgValueLocation changeLocNo(unsigned NewLocNo) const { - return DbgValueLocation(NewLocNo, *Expression); + DbgVariableValue changeLocNo(unsigned NewLocNo) const { + return DbgVariableValue(NewLocNo, *Expression); } - friend inline bool operator==(const DbgValueLocation &LHS, - const DbgValueLocation &RHS) { + friend inline bool operator==(const DbgVariableValue &LHS, + const DbgVariableValue &RHS) { return LHS.LocNo == RHS.LocNo && LHS.Expression == RHS.Expression; } - friend inline bool operator!=(const DbgValueLocation &LHS, - const DbgValueLocation &RHS) { + friend inline bool operator!=(const DbgVariableValue &LHS, + const DbgVariableValue &RHS) { return !(LHS == RHS); } @@ -126,8 +126,8 @@ const DIExpression *Expression = nullptr; }; -/// Map of where a user value is live, and its location. -using LocMap = IntervalMap; +/// Map of where a user value is live to that value. +using LocMap = IntervalMap; /// Map of stack slot offsets for spilled locations. /// Non-spilled locations are not added to the map. @@ -148,6 +148,8 @@ /// closure of that relation. class UserValue { const DILocalVariable *Variable; ///< The debug info variable we are part of. + // FIXME: This is only used to get the FragmentInfo that describes the part + // of the variable we are a part of. We should just store the FragmentInfo. const DIExpression *Expression; ///< Any complex address expression. DebugLoc dl; ///< The debug location for the variable. This is ///< used by dwarf writer to find lexical scope. @@ -160,10 +162,10 @@ /// Map of slot indices where this value is live. LocMap locInts; - /// Insert a DBG_VALUE into MBB at Idx for LocNo. + /// Insert a DBG_VALUE into MBB at Idx for DbgValue. void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, - SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled, - unsigned SpillOffset, LiveIntervals &LIS, + SlotIndex StopIdx, DbgVariableValue DbgValue, + bool Spilled, unsigned SpillOffset, LiveIntervals &LIS, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI); @@ -193,8 +195,16 @@ /// Does this UserValue match the parameters? bool match(const DILocalVariable *Var, const DIExpression *Expr, const DILocation *IA) const { - // FIXME: The fragment should be part of the equivalence class, but not - // other things in the expression like stack values. + // FIXME: Handle partially overlapping fragments. + // A DBG_VALUE with a fragment which overlaps a previous DBG_VALUE fragment + // for the same variable terminates the interval opened by the first. + // getUserValue() uses match() to filter DBG_VALUEs into interval maps to + // represent these intervals. + // Given two _partially_ overlapping fragments match() will always return + // false. The DBG_VALUEs will be filtered into separate interval maps and + // therefore we do not faithfully represent the original intervals. + // See D70121#1849741 for a more detailed explanation and further + // discussion. return Var == Variable && Expr->getFragmentInfo() == Expression->getFragmentInfo() && dl->getInlinedAt() == IA; @@ -257,34 +267,34 @@ void removeLocationIfUnused(unsigned LocNo) { // Bail out if LocNo still is used. for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { - DbgValueLocation Loc = I.value(); - if (Loc.locNo() == LocNo) + DbgVariableValue DbgValue = I.value(); + if (DbgValue.getLocNo() == LocNo) return; } // Remove the entry in the locations vector, and adjust all references to // location numbers above the removed entry. locations.erase(locations.begin() + LocNo); for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { - DbgValueLocation Loc = I.value(); - if (!Loc.isUndef() && Loc.locNo() > LocNo) - I.setValueUnchecked(Loc.changeLocNo(Loc.locNo() - 1)); + DbgVariableValue DbgValue = I.value(); + if (!DbgValue.isUndef() && DbgValue.getLocNo() > LocNo) + I.setValueUnchecked(DbgValue.changeLocNo(DbgValue.getLocNo() - 1)); } } /// Ensure that all virtual register locations are mapped. void mapVirtRegs(LDVImpl *LDV); - /// Add a definition point to this value. + /// Add a definition point to this user value. void addDef(SlotIndex Idx, const MachineOperand &LocMO, const DIExpression &Expr) { - DbgValueLocation Loc(getLocationNo(LocMO), Expr); - // Add a singular (Idx,Idx) -> Loc mapping. + DbgVariableValue DbgValue(getLocationNo(LocMO), Expr); + // Add a singular (Idx,Idx) -> value mapping. LocMap::iterator I = locInts.find(Idx); if (!I.valid() || I.start() != Idx) - I.insert(Idx, Idx.getNextSlot(), Loc); + I.insert(Idx, Idx.getNextSlot(), DbgValue); else // A later DBG_VALUE at the same SlotIndex overrides the old location. - I.setValue(Loc); + I.setValue(DbgValue); } /// Extend the current definition as far as possible down. @@ -301,23 +311,22 @@ /// \param VNI When LR is not null, this is the value to restrict to. /// \param [out] Kills Append end points of VNI's live range to Kills. /// \param LIS Live intervals analysis. - void extendDef(SlotIndex Idx, DbgValueLocation Loc, - LiveRange *LR, const VNInfo *VNI, - SmallVectorImpl *Kills, + void extendDef(SlotIndex Idx, DbgVariableValue Loc, LiveRange *LR, + const VNInfo *VNI, SmallVectorImpl *Kills, LiveIntervals &LIS); - /// The value in LI/LocNo may be copies to other registers. Determine if + /// The value in LI may be copies to other registers. Determine if /// any of the copies are available at the kill points, and add defs if /// possible. /// /// \param LI Scan for copies of the value in LI->reg. - /// \param Loc Location number of LI->reg. - /// \param Kills Points where the range of LocNo could be extended. - /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here. + /// \param DbgValue Location number of LI->reg, and DIExpression. + /// \param Kills Points where the range of DbgValue could be extended. + /// \param [in,out] NewDefs Append (Idx, DbgValue) of inserted defs here. void addDefsFromCopies( - LiveInterval *LI, DbgValueLocation Loc, + LiveInterval *LI, DbgVariableValue DbgValue, const SmallVectorImpl &Kills, - SmallVectorImpl> &NewDefs, + SmallVectorImpl> &NewDefs, MachineRegisterInfo &MRI, LiveIntervals &LIS); /// Compute the live intervals of all locations after collecting all their @@ -534,7 +543,7 @@ if (I.value().isUndef()) OS << "undef"; else { - OS << I.value().locNo(); + OS << I.value().getLocNo(); } } for (unsigned i = 0, e = locations.size(); i != e; ++i) { @@ -647,8 +656,7 @@ "DBG_VALUE with nonzero offset"); const DILocalVariable *Var = MI.getDebugVariable(); const DIExpression *Expr = MI.getDebugExpression(); - UserValue *UV = - getUserValue(Var, Expr, MI.getDebugLoc()); + UserValue *UV = getUserValue(Var, Expr, MI.getDebugLoc()); if (!Discard) UV->addDef(Idx, MI.getOperand(0), *Expr); else { @@ -717,7 +725,7 @@ return Changed; } -void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR, +void UserValue::extendDef(SlotIndex Idx, DbgVariableValue Loc, LiveRange *LR, const VNInfo *VNI, SmallVectorImpl *Kills, LiveIntervals &LIS) { SlotIndex Start = Idx; @@ -762,9 +770,9 @@ } void UserValue::addDefsFromCopies( - LiveInterval *LI, DbgValueLocation Loc, + LiveInterval *LI, DbgVariableValue DbgValue, const SmallVectorImpl &Kills, - SmallVectorImpl> &NewDefs, + SmallVectorImpl> &NewDefs, MachineRegisterInfo &MRI, LiveIntervals &LIS) { if (Kills.empty()) return; @@ -788,11 +796,11 @@ if (!Register::isVirtualRegister(DstReg)) continue; - // Is LocNo extended to reach this copy? If not, another def may be blocking - // it, or we are looking at a wrong value of LI. + // Is the value extended to reach this copy? If not, another def may be + // blocking it, or we are looking at a wrong value of LI. SlotIndex Idx = LIS.getInstructionIndex(*MI); LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); - if (!I.valid() || I.value() != Loc) + if (!I.valid() || I.value() != DbgValue) continue; if (!LIS.hasInterval(DstReg)) @@ -826,9 +834,9 @@ MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); - DbgValueLocation NewLoc = Loc.changeLocNo(LocNo); - I.insert(Idx, Idx.getNextSlot(), NewLoc); - NewDefs.push_back(std::make_pair(Idx, NewLoc)); + DbgVariableValue NewValue = DbgValue.changeLocNo(LocNo); + I.insert(Idx, Idx.getNextSlot(), NewValue); + NewDefs.push_back(std::make_pair(Idx, NewValue)); break; } } @@ -837,7 +845,7 @@ void UserValue::computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, LiveIntervals &LIS, LexicalScopes &LS) { - SmallVector, 16> Defs; + SmallVector, 16> Defs; // Collect all defs to be extended (Skipping undefs). for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) @@ -847,11 +855,11 @@ // Extend all defs, and possibly add new ones along the way. for (unsigned i = 0; i != Defs.size(); ++i) { SlotIndex Idx = Defs[i].first; - DbgValueLocation Loc = Defs[i].second; - const MachineOperand &LocMO = locations[Loc.locNo()]; + DbgVariableValue DbgValue = Defs[i].second; + const MachineOperand &LocMO = locations[DbgValue.getLocNo()]; if (!LocMO.isReg()) { - extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS); + extendDef(Idx, DbgValue, nullptr, nullptr, nullptr, LIS); continue; } @@ -864,7 +872,7 @@ VNI = LI->getVNInfoAt(Idx); } SmallVector Kills; - extendDef(Idx, Loc, LI, VNI, &Kills, LIS); + extendDef(Idx, DbgValue, LI, VNI, &Kills, LIS); // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that // if the original location for example is %vreg0:sub_hi, and we find a // full register copy in addDefsFromCopies (at the moment it only handles @@ -874,7 +882,7 @@ // sub-register in that regclass). For now, simply skip handling copies if // a sub-register is involved. if (LI && !LocMO.getSubReg()) - addDefsFromCopies(LI, Loc, Kills, Defs, MRI, LIS); + addDefsFromCopies(LI, DbgValue, Kills, Defs, MRI, LIS); continue; } @@ -911,7 +919,7 @@ // I.stop() >= PrevEnd. Check for overlap. if (PrevEnd && I.start() < PrevEnd) { SlotIndex IStop = I.stop(); - DbgValueLocation Loc = I.value(); + DbgVariableValue DbgValue = I.value(); // Stop overlaps previous end - trim the end of the interval to // the scope range. @@ -921,7 +929,7 @@ // If the interval also overlaps the start of the "next" (i.e. // current) range create a new interval for the remainder if (RStart < IStop) - I.insert(RStart, IStop, Loc); + I.insert(RStart, IStop, DbgValue); } // Advance I so that I.stop() >= RStart, and check for overlap. @@ -1041,7 +1049,8 @@ break; // Now LII->end > LocMapI.start(). Do we have an overlap? - if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) { + if (LocMapI.value().getLocNo() == OldLocNo && + LII->start < LocMapI.stop()) { // Overlapping correct location. Allocate NewLocNo now. if (NewLocNo == UndefLocNo) { MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); @@ -1051,8 +1060,8 @@ } SlotIndex LStart = LocMapI.start(); - SlotIndex LStop = LocMapI.stop(); - DbgValueLocation OldLoc = LocMapI.value(); + SlotIndex LStop = LocMapI.stop(); + DbgVariableValue OldDbgValue = LocMapI.value(); // Trim LocMapI down to the LII overlap. if (LStart < LII->start) @@ -1061,17 +1070,17 @@ LocMapI.setStopUnchecked(LII->end); // Change the value in the overlap. This may trigger coalescing. - LocMapI.setValue(OldLoc.changeLocNo(NewLocNo)); + LocMapI.setValue(OldDbgValue.changeLocNo(NewLocNo)); - // Re-insert any removed OldLocNo ranges. + // Re-insert any removed OldDbgValue ranges. if (LStart < LocMapI.start()) { - LocMapI.insert(LStart, LocMapI.start(), OldLoc); + LocMapI.insert(LStart, LocMapI.start(), OldDbgValue); ++LocMapI; assert(LocMapI.valid() && "Unexpected coalescing"); } if (LStop > LocMapI.stop()) { ++LocMapI; - LocMapI.insert(LII->end, LStop, OldLoc); + LocMapI.insert(LII->end, LStop, OldDbgValue); --LocMapI; } } @@ -1097,6 +1106,9 @@ // register to the spill slot). So for a while we can have locations that map // to virtual registers that have been removed from both the MachineFunction // and from LiveIntervals. + // + // We may also just be using the location for a value with a different + // expression. removeLocationIfUnused(OldLocNo); LLVM_DEBUG({ @@ -1215,13 +1227,13 @@ // DBG_VALUE intervals with different vregs that were allocated to the same // physical register. for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { - DbgValueLocation Loc = I.value(); + DbgVariableValue DbgValue = I.value(); // Undef values don't exist in locations (and thus not in LocNoMap either) // so skip over them. See getLocationNo(). - if (Loc.isUndef()) + if (DbgValue.isUndef()) continue; - unsigned NewLocNo = LocNoMap[Loc.locNo()]; - I.setValueUnchecked(Loc.changeLocNo(NewLocNo)); + unsigned NewLocNo = LocNoMap[DbgValue.getLocNo()]; + I.setValueUnchecked(DbgValue.changeLocNo(NewLocNo)); I.setStart(I.start()); } } @@ -1275,7 +1287,7 @@ } void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, - SlotIndex StopIdx, DbgValueLocation Loc, + SlotIndex StopIdx, DbgVariableValue DbgValue, bool Spilled, unsigned SpillOffset, LiveIntervals &LIS, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI) { @@ -1285,12 +1297,14 @@ MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS); // 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); + MachineOperand MO = + !DbgValue.isUndef() + ? locations[DbgValue.getLocNo()] + : MachineOperand::CreateReg( + /* Reg */ 0, /* isDef */ false, /* isImp */ false, + /* isKill */ false, /* isDead */ false, + /* isUndef */ false, /* isEarlyClobber */ false, + /* SubReg */ 0, /* isDebug */ true); ++NumInsertedDebugValues; @@ -1302,7 +1316,7 @@ // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate // that the original virtual register was a pointer. Also, add the stack slot // offset for the spilled register to the expression. - const DIExpression *Expr = Loc.getExpression(); + const DIExpression *Expr = DbgValue.getExpression(); if (Spilled) Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, SpillOffset); @@ -1336,19 +1350,20 @@ for (LocMap::const_iterator I = locInts.begin(); I.valid();) { SlotIndex Start = I.start(); SlotIndex Stop = I.stop(); - DbgValueLocation Loc = I.value(); - auto SpillIt = - !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end(); + DbgVariableValue DbgValue = I.value(); + auto SpillIt = !DbgValue.isUndef() ? SpillOffsets.find(DbgValue.getLocNo()) + : SpillOffsets.end(); bool Spilled = SpillIt != SpillOffsets.end(); unsigned SpillOffset = Spilled ? SpillIt->second : 0; - LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo()); + LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop + << "):" << DbgValue.getLocNo()); MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB); LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); - insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII, - TRI); + insertDebugValue(&*MBB, Start, Stop, DbgValue, Spilled, SpillOffset, LIS, + TII, TRI); // This interval may span multiple basic blocks. // Insert a DBG_VALUE into each one. while (Stop > MBBEnd) { @@ -1358,8 +1373,8 @@ break; MBBEnd = LIS.getMBBEndIdx(&*MBB); LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); - insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII, - TRI); + insertDebugValue(&*MBB, Start, Stop, DbgValue, Spilled, SpillOffset, LIS, + TII, TRI); } LLVM_DEBUG(dbgs() << '\n'); if (MBB == MFEnd)