1 //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LiveDebugVariables analysis.
12 // Remove all DBG_VALUE instructions referencing virtual registers and replace
13 // them with a data structure tracking where live user variables are kept - in a
14 // virtual register or in a stack slot.
16 // Allow the data structure to be updated during register allocation when values
17 // are moved between registers and stack slots. Finally emit new DBG_VALUE
18 // instructions after register allocation is complete.
20 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "livedebug"
23 #include "LiveDebugVariables.h"
24 #include "VirtRegMap.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Metadata.h"
27 #include "llvm/Value.h"
28 #include "llvm/ADT/IntervalMap.h"
29 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
30 #include "llvm/CodeGen/MachineDominators.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/Passes.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetRegisterInfo.h"
42 char LiveDebugVariables::ID = 0;
44 INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
45 "Debug Variable Analysis", false, false)
46 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
47 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
48 INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
49 "Debug Variable Analysis", false, false)
51 void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequired<MachineDominatorTree>();
53 AU.addRequiredTransitive<LiveIntervals>();
55 MachineFunctionPass::getAnalysisUsage(AU);
58 LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) {
59 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
62 /// Location - All the different places a user value can reside.
63 /// Note that this includes immediate values that technically aren't locations.
66 /// kind - What kind of location is this?
72 /// Kind - One of the following:
74 /// 2. Register number (physical or virtual), data.SubIdx is the subreg index.
75 /// 3. ~Frame index, data.Offset is the offset.
76 /// 4. locImm, data.ImmVal is the constant integer value.
77 /// 5. locFPImm, data.CFP points to the floating point constant.
80 /// Data - Extra data about location.
82 unsigned SubIdx; ///< For virtual registers.
83 int64_t Offset; ///< For frame indices.
84 int64_t ImmVal; ///< For locImm.
85 const ConstantFP *CFP; ///< For locFPImm.
88 Location(const MachineOperand &MO) {
89 switch(MO.getType()) {
90 case MachineOperand::MO_Register:
92 Data.SubIdx = MO.getSubReg();
94 case MachineOperand::MO_Immediate:
96 Data.ImmVal = MO.getImm();
98 case MachineOperand::MO_FPImmediate:
100 Data.CFP = MO.getFPImm();
102 case MachineOperand::MO_FrameIndex:
103 Kind = ~MO.getIndex();
104 // FIXME: MO_FrameIndex should support an offset.
113 /// addOperand - Add this location as a machine operand to MI.
114 MachineInstrBuilder addOperand(MachineInstrBuilder MI) const {
117 return MI.addImm(Data.ImmVal);
119 return MI.addFPImm(Data.CFP);
122 return MI.addFrameIndex(getFrameIndex());
124 return MI.addReg(Kind); // reg and undef.
128 bool operator==(const Location &RHS) const {
129 if (Kind != RHS.Kind)
135 return Data.ImmVal == RHS.Data.ImmVal;
137 return Data.CFP == RHS.Data.CFP;
140 return Data.SubIdx == RHS.Data.SubIdx;
142 return Data.Offset == RHS.Data.Offset;
146 /// isUndef - is this the singleton undef?
147 bool isUndef() const { return Kind == locUndef; }
149 /// isReg - is this a register location?
150 bool isReg() const { return Kind && Kind < locImm; }
152 /// isFrameIndex - is this a frame index location?
153 bool isFrameIndex() const { return Kind > locFPImm; }
155 int getFrameIndex() const { return ~Kind; }
157 void print(raw_ostream&, const TargetRegisterInfo*);
161 /// LocMap - Map of where a user value is live, and its location.
162 typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
164 /// UserValue - A user value is a part of a debug info user variable.
166 /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
167 /// holds part of a user variable. The part is identified by a byte offset.
169 /// UserValues are grouped into equivalence classes for easier searching. Two
170 /// user values are related if they refer to the same variable, or if they are
171 /// held by the same virtual register. The equivalence class is the transitive
172 /// closure of that relation.
175 const MDNode *variable; ///< The debug info variable we are part of.
176 unsigned offset; ///< Byte offset into variable.
178 UserValue *leader; ///< Equivalence class leader.
179 UserValue *next; ///< Next value in equivalence class, or null.
181 /// Numbered locations referenced by locmap.
182 SmallVector<Location, 4> locations;
184 /// Map of slot indices where this value is live.
187 /// coalesceLocation - After LocNo was changed, check if it has become
188 /// identical to another location, and coalesce them. This may cause LocNo or
189 /// a later location to be erased, but no earlier location will be erased.
190 void coalesceLocation(unsigned LocNo);
192 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
193 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
194 LiveIntervals &LIS, const TargetInstrInfo &TII);
196 /// insertDebugKill - Insert an undef DBG_VALUE into MBB at Idx.
197 void insertDebugKill(MachineBasicBlock *MBB, SlotIndex Idx,
198 LiveIntervals &LIS, const TargetInstrInfo &TII);
201 /// UserValue - Create a new UserValue.
202 UserValue(const MDNode *var, unsigned o, LocMap::Allocator &alloc)
203 : variable(var), offset(o), leader(this), next(0), locInts(alloc)
206 /// getLeader - Get the leader of this value's equivalence class.
207 UserValue *getLeader() {
208 UserValue *l = leader;
209 while (l != l->leader)
214 /// getNext - Return the next UserValue in the equivalence class.
215 UserValue *getNext() const { return next; }
217 /// match - Does this UserValue match the aprameters?
218 bool match(const MDNode *Var, unsigned Offset) const {
219 return Var == variable && Offset == offset;
222 /// merge - Merge equivalence classes.
223 static UserValue *merge(UserValue *L1, UserValue *L2) {
224 L2 = L2->getLeader();
227 L1 = L1->getLeader();
230 // Splice L2 before L1's members.
233 End->leader = L1, End = End->next;
235 End->next = L1->next;
240 /// getLocationNo - Return the location number that matches Loc.
241 unsigned getLocationNo(Location Loc) {
244 unsigned n = std::find(locations.begin(), locations.end(), Loc) -
246 if (n == locations.size())
247 locations.push_back(Loc);
251 /// addDef - Add a definition point to this value.
252 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
253 // Add a singular (Idx,Idx) -> Loc mapping.
254 LocMap::iterator I = locInts.find(Idx);
255 if (!I.valid() || I.start() != Idx)
256 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
259 /// extendDef - Extend the current definition as far as possible down the
260 /// dominator tree. Stop when meeting an existing def or when leaving the live
262 /// @param Idx Starting point for the definition.
263 /// @param LocNo Location number to propagate.
264 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
265 /// @param VNI When LI is not null, this is the value to restrict to.
266 /// @param LIS Live intervals analysis.
267 /// @param MDT Dominator tree.
268 void extendDef(SlotIndex Idx, unsigned LocNo,
269 LiveInterval *LI, const VNInfo *VNI,
270 LiveIntervals &LIS, MachineDominatorTree &MDT);
272 /// computeIntervals - Compute the live intervals of all locations after
273 /// collecting all their def points.
274 void computeIntervals(LiveIntervals &LIS, MachineDominatorTree &MDT);
276 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
277 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
278 const TargetRegisterInfo *TRI);
280 /// rewriteLocations - Rewrite virtual register locations according to the
281 /// provided virtual register map.
282 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
284 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
285 void emitDebugValues(VirtRegMap *VRM,
286 LiveIntervals &LIS, const TargetInstrInfo &TRI);
288 void print(raw_ostream&, const TargetRegisterInfo*);
292 /// LDVImpl - Implementation of the LiveDebugVariables pass.
295 LiveDebugVariables &pass;
296 LocMap::Allocator allocator;
299 MachineDominatorTree *MDT;
300 const TargetRegisterInfo *TRI;
302 /// userValues - All allocated UserValue instances.
303 SmallVector<UserValue*, 8> userValues;
305 /// Map virtual register to eq class leader.
306 typedef DenseMap<unsigned, UserValue*> VRMap;
307 VRMap virtRegToEqClass;
309 /// Map user variable to eq class leader.
310 typedef DenseMap<const MDNode *, UserValue*> UVMap;
313 /// getUserValue - Find or create a UserValue.
314 UserValue *getUserValue(const MDNode *Var, unsigned Offset);
316 /// lookupVirtReg - Find the EC leader for VirtReg or null.
317 UserValue *lookupVirtReg(unsigned VirtReg);
319 /// mapVirtReg - Map virtual register to an equivalence class.
320 void mapVirtReg(unsigned VirtReg, UserValue *EC);
322 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
323 /// @param MI DBG_VALUE instruction
324 /// @param Idx Last valid SLotIndex before instruction.
325 /// @return True if the DBG_VALUE instruction should be deleted.
326 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
328 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
329 /// a UserValue def for each instruction.
330 /// @param mf MachineFunction to be scanned.
331 /// @return True if any debug values were found.
332 bool collectDebugValues(MachineFunction &mf);
334 /// computeIntervals - Compute the live intervals of all user values after
335 /// collecting all their def points.
336 void computeIntervals();
339 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
340 bool runOnMachineFunction(MachineFunction &mf);
342 /// clear - Relase all memory.
344 DeleteContainerPointers(userValues);
346 virtRegToEqClass.clear();
350 /// renameRegister - Replace all references to OldReg wiht NewReg:SubIdx.
351 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
353 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
354 void emitDebugValues(VirtRegMap *VRM);
356 void print(raw_ostream&);
360 void Location::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
366 OS << "int:" << Data.ImmVal;
369 OS << "fp:" << Data.CFP->getValueAPF().convertToDouble();
373 if (TargetRegisterInfo::isVirtualRegister(Kind)) {
374 OS << "%reg" << Kind;
376 OS << ':' << TRI->getSubRegIndexName(Data.SubIdx);
378 OS << '%' << TRI->getName(Kind);
380 OS << "fi#" << ~Kind;
382 OS << '+' << Data.Offset;
388 void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
389 if (const MDString *MDS = dyn_cast<MDString>(variable->getOperand(2)))
390 OS << "!\"" << MDS->getString() << "\"\t";
393 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
394 OS << " [" << I.start() << ';' << I.stop() << "):";
395 if (I.value() == ~0u)
400 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
401 OS << " Loc" << i << '=';
402 locations[i].print(OS, TRI);
407 void LDVImpl::print(raw_ostream &OS) {
408 OS << "********** DEBUG VARIABLES **********\n";
409 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
410 userValues[i]->print(OS, TRI);
413 void UserValue::coalesceLocation(unsigned LocNo) {
414 unsigned KeepLoc = std::find(locations.begin(), locations.begin() + LocNo,
415 locations[LocNo]) - locations.begin();
416 unsigned EraseLoc = LocNo;
417 if (KeepLoc == LocNo) {
418 EraseLoc = std::find(locations.begin() + LocNo + 1, locations.end(),
419 locations[LocNo]) - locations.begin();
421 if (EraseLoc == locations.size())
424 assert(KeepLoc < EraseLoc);
425 locations.erase(locations.begin() + EraseLoc);
428 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
429 unsigned v = I.value();
431 I.setValue(KeepLoc); // Coalesce when possible.
432 else if (v > EraseLoc)
433 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
437 UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset) {
438 UserValue *&Leader = userVarMap[Var];
440 UserValue *UV = Leader->getLeader();
442 for (; UV; UV = UV->getNext())
443 if (UV->match(Var, Offset))
447 UserValue *UV = new UserValue(Var, Offset, allocator);
448 userValues.push_back(UV);
449 Leader = UserValue::merge(Leader, UV);
453 void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
454 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
455 UserValue *&Leader = virtRegToEqClass[VirtReg];
456 Leader = UserValue::merge(Leader, EC);
459 UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
460 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
461 return UV->getLeader();
465 bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
466 // DBG_VALUE loc, offset, variable
467 if (MI->getNumOperands() != 3 ||
468 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
469 DEBUG(dbgs() << "Can't handle " << *MI);
473 // Get or create the UserValue for (variable,offset).
474 unsigned Offset = MI->getOperand(1).getImm();
475 const MDNode *Var = MI->getOperand(2).getMetadata();
476 UserValue *UV = getUserValue(Var, Offset);
478 // If the location is a virtual register, make sure it is mapped.
479 if (MI->getOperand(0).isReg()) {
480 unsigned Reg = MI->getOperand(0).getReg();
481 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg))
485 UV->addDef(Idx, MI->getOperand(0));
489 bool LDVImpl::collectDebugValues(MachineFunction &mf) {
490 bool Changed = false;
491 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
493 MachineBasicBlock *MBB = MFI;
494 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
496 if (!MBBI->isDebugValue()) {
500 // DBG_VALUE has no slot index, use the previous instruction instead.
501 SlotIndex Idx = MBBI == MBB->begin() ?
502 LIS->getMBBStartIdx(MBB) :
503 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
504 // Handle consecutive DBG_VALUE instructions with the same slot index.
506 if (handleDebugValue(MBBI, Idx)) {
507 MBBI = MBB->erase(MBBI);
511 } while (MBBI != MBBE && MBBI->isDebugValue());
517 void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
518 LiveInterval *LI, const VNInfo *VNI,
519 LiveIntervals &LIS, MachineDominatorTree &MDT) {
520 SmallVector<SlotIndex, 16> Todo;
524 SlotIndex Start = Todo.pop_back_val();
525 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
526 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
527 LocMap::iterator I = locInts.find(Idx);
529 // Limit to VNI's live range.
532 LiveRange *Range = LI->getLiveRangeContaining(Start);
533 if (!Range || Range->valno != VNI)
535 if (Range->end < Stop)
536 Stop = Range->end, ToEnd = false;
539 // There could already be a short def at Start.
540 if (I.valid() && I.start() <= Start) {
541 // Stop when meeting a different location or an already extended interval.
542 Start = Start.getNextSlot();
543 if (I.value() != LocNo || I.stop() != Start)
545 // This is a one-slot placeholder. Just skip it.
549 // Limited by the next def.
550 if (I.valid() && I.start() < Stop)
551 Stop = I.start(), ToEnd = false;
556 I.insert(Start, Stop, LocNo);
558 // If we extended to the MBB end, propagate down the dominator tree.
561 const std::vector<MachineDomTreeNode*> &Children =
562 MDT.getNode(MBB)->getChildren();
563 for (unsigned i = 0, e = Children.size(); i != e; ++i)
564 Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock()));
565 } while (!Todo.empty());
569 UserValue::computeIntervals(LiveIntervals &LIS, MachineDominatorTree &MDT) {
570 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
572 // Collect all defs to be extended (Skipping undefs).
573 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
574 if (I.value() != ~0u)
575 Defs.push_back(std::make_pair(I.start(), I.value()));
577 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
578 SlotIndex Idx = Defs[i].first;
579 unsigned LocNo = Defs[i].second;
580 const Location &Loc = locations[LocNo];
582 // Register locations are constrained to where the register value is live.
583 if (Loc.isReg() && LIS.hasInterval(Loc.Kind)) {
584 LiveInterval *LI = &LIS.getInterval(Loc.Kind);
585 const VNInfo *VNI = LI->getVNInfoAt(Idx);
586 extendDef(Idx, LocNo, LI, VNI, LIS, MDT);
588 extendDef(Idx, LocNo, 0, 0, LIS, MDT);
591 // Finally, erase all the undefs.
592 for (LocMap::iterator I = locInts.begin(); I.valid();)
593 if (I.value() == ~0u)
599 void LDVImpl::computeIntervals() {
600 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
601 userValues[i]->computeIntervals(*LIS, *MDT);
604 bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
606 LIS = &pass.getAnalysis<LiveIntervals>();
607 MDT = &pass.getAnalysis<MachineDominatorTree>();
608 TRI = mf.getTarget().getRegisterInfo();
610 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
611 << ((Value*)mf.getFunction())->getName()
614 bool Changed = collectDebugValues(mf);
616 DEBUG(print(dbgs()));
620 bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
622 pImpl = new LDVImpl(this);
623 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
626 void LiveDebugVariables::releaseMemory() {
628 static_cast<LDVImpl*>(pImpl)->clear();
631 LiveDebugVariables::~LiveDebugVariables() {
633 delete static_cast<LDVImpl*>(pImpl);
637 renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
638 const TargetRegisterInfo *TRI) {
639 for (unsigned i = locations.size(); i; --i) {
640 unsigned LocNo = i - 1;
641 Location &Loc = locations[LocNo];
642 if (Loc.Kind != OldReg)
645 if (SubIdx && Loc.Data.SubIdx)
646 Loc.Data.SubIdx = TRI->composeSubRegIndices(SubIdx, Loc.Data.SubIdx);
647 coalesceLocation(LocNo);
652 renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
653 UserValue *UV = lookupVirtReg(OldReg);
657 if (TargetRegisterInfo::isVirtualRegister(NewReg))
658 mapVirtReg(NewReg, UV);
659 virtRegToEqClass.erase(OldReg);
662 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
667 void LiveDebugVariables::
668 renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
670 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
674 UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
675 // Iterate over locations in reverse makes it easier to handle coalescing.
676 for (unsigned i = locations.size(); i ; --i) {
677 unsigned LocNo = i-1;
678 Location &Loc = locations[LocNo];
679 // Only virtual registers are rewritten.
680 if (!Loc.isReg() || !TargetRegisterInfo::isVirtualRegister(Loc.Kind))
682 unsigned VirtReg = Loc.Kind;
683 if (VRM.isAssignedReg(VirtReg)) {
684 unsigned PhysReg = VRM.getPhys(VirtReg);
686 PhysReg = TRI.getSubReg(PhysReg, Loc.Data.SubIdx);
689 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
690 Loc.Kind = ~VRM.getStackSlot(VirtReg);
691 // FIXME: Translate SubIdx to a stackslot offset.
694 Loc.Kind = Location::locUndef;
696 coalesceLocation(LocNo);
698 DEBUG(print(dbgs(), &TRI));
701 /// findInsertLocation - Find an iterator and DebugLoc for inserting a DBG_VALUE
703 static MachineBasicBlock::iterator
704 findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, DebugLoc &DL,
705 LiveIntervals &LIS) {
706 SlotIndex Start = LIS.getMBBStartIdx(MBB);
707 Idx = Idx.getBaseIndex();
709 // Try to find an insert location by going backwards from Idx.
711 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
712 // We've reached the beginning of MBB.
714 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
716 DL = I->getDebugLoc();
719 Idx = Idx.getPrevIndex();
721 // We found an instruction. The insert point is after the instr.
722 DL = MI->getDebugLoc();
723 return llvm::next(MachineBasicBlock::iterator(MI));
726 void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
729 const TargetInstrInfo &TII) {
731 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, DL, LIS);
732 Location &Loc = locations[LocNo];
734 // Frame index locations may require a target callback.
735 if (Loc.isFrameIndex()) {
736 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
738 offset, variable, DL);
744 // This is not a frame index, or the target is happy with a standard FI.
745 Loc.addOperand(BuildMI(*MBB, I, DL, TII.get(TargetOpcode::DBG_VALUE)))
746 .addImm(offset).addMetadata(variable);
749 void UserValue::insertDebugKill(MachineBasicBlock *MBB, SlotIndex Idx,
750 LiveIntervals &LIS, const TargetInstrInfo &TII) {
752 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, DL, LIS);
753 BuildMI(*MBB, I, DL, TII.get(TargetOpcode::DBG_VALUE)).addReg(0)
754 .addImm(offset).addMetadata(variable);
757 void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
758 const TargetInstrInfo &TII) {
759 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
761 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
762 SlotIndex Start = I.start();
763 SlotIndex Stop = I.stop();
764 unsigned LocNo = I.value();
765 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
766 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
767 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
769 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
770 insertDebugValue(MBB, Start, LocNo, LIS, TII);
772 // This interval may span multiple basic blocks.
773 // Insert a DBG_VALUE into each one.
774 while(Stop > MBBEnd) {
775 // Move to the next block.
779 MBBEnd = LIS.getMBBEndIdx(MBB);
780 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
781 insertDebugValue(MBB, Start, LocNo, LIS, TII);
783 DEBUG(dbgs() << '\n');
790 // The current interval ends before MBB.
791 // Insert a kill if there is a gap.
792 if (!I.valid() || I.start() > Stop)
793 insertDebugKill(MBB, Stop, LIS, TII);
797 void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
798 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
799 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
800 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
801 userValues[i]->rewriteLocations(*VRM, *TRI);
802 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
806 void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
808 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
813 void LiveDebugVariables::dump() {
815 static_cast<LDVImpl*>(pImpl)->print(dbgs());