blob: da4b017cdca01d29de656eaea3d9650a952844f9 [file] [log] [blame]
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +00001//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveDebugVariables analysis.
11//
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.
15//
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.
19//
20//===----------------------------------------------------------------------===//
21
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000022#define DEBUG_TYPE "livedebug"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000023#include "LiveDebugVariables.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000024#include "VirtRegMap.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000025#include "llvm/Constants.h"
26#include "llvm/Metadata.h"
27#include "llvm/Value.h"
28#include "llvm/ADT/IntervalMap.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000029#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000030#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000033#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000036#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000037#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000038#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000039
40using namespace llvm;
41
Devang Patel51a666f2011-01-07 22:33:41 +000042static cl::opt<bool>
43EnableLDV("live-debug-variables",
44 cl::desc("Enable the live debug variables pass"), cl::Hidden);
45
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000046char LiveDebugVariables::ID = 0;
47
48INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
49 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000050INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000051INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
52INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
53 "Debug Variable Analysis", false, false)
54
55void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000056 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000057 AU.addRequiredTransitive<LiveIntervals>();
58 AU.setPreservesAll();
59 MachineFunctionPass::getAnalysisUsage(AU);
60}
61
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000062LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) {
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000063 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
64}
65
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000066/// LocMap - Map of where a user value is live, and its location.
67typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
68
69/// UserValue - A user value is a part of a debug info user variable.
70///
71/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
72/// holds part of a user variable. The part is identified by a byte offset.
73///
74/// UserValues are grouped into equivalence classes for easier searching. Two
75/// user values are related if they refer to the same variable, or if they are
76/// held by the same virtual register. The equivalence class is the transitive
77/// closure of that relation.
78namespace {
79class UserValue {
80 const MDNode *variable; ///< The debug info variable we are part of.
81 unsigned offset; ///< Byte offset into variable.
82
83 UserValue *leader; ///< Equivalence class leader.
84 UserValue *next; ///< Next value in equivalence class, or null.
85
86 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +000087 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000088
89 /// Map of slot indices where this value is live.
90 LocMap locInts;
91
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +000092 /// coalesceLocation - After LocNo was changed, check if it has become
93 /// identical to another location, and coalesce them. This may cause LocNo or
94 /// a later location to be erased, but no earlier location will be erased.
95 void coalesceLocation(unsigned LocNo);
96
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000097 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
98 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
99 LiveIntervals &LIS, const TargetInstrInfo &TII);
100
101 /// insertDebugKill - Insert an undef DBG_VALUE into MBB at Idx.
102 void insertDebugKill(MachineBasicBlock *MBB, SlotIndex Idx,
103 LiveIntervals &LIS, const TargetInstrInfo &TII);
104
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000105public:
106 /// UserValue - Create a new UserValue.
107 UserValue(const MDNode *var, unsigned o, LocMap::Allocator &alloc)
108 : variable(var), offset(o), leader(this), next(0), locInts(alloc)
109 {}
110
111 /// getLeader - Get the leader of this value's equivalence class.
112 UserValue *getLeader() {
113 UserValue *l = leader;
114 while (l != l->leader)
115 l = l->leader;
116 return leader = l;
117 }
118
119 /// getNext - Return the next UserValue in the equivalence class.
120 UserValue *getNext() const { return next; }
121
122 /// match - Does this UserValue match the aprameters?
123 bool match(const MDNode *Var, unsigned Offset) const {
124 return Var == variable && Offset == offset;
125 }
126
127 /// merge - Merge equivalence classes.
128 static UserValue *merge(UserValue *L1, UserValue *L2) {
129 L2 = L2->getLeader();
130 if (!L1)
131 return L2;
132 L1 = L1->getLeader();
133 if (L1 == L2)
134 return L1;
135 // Splice L2 before L1's members.
136 UserValue *End = L2;
137 while (End->next)
138 End->leader = L1, End = End->next;
139 End->leader = L1;
140 End->next = L1->next;
141 L1->next = L2;
142 return L1;
143 }
144
145 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000146 unsigned getLocationNo(const MachineOperand &LocMO) {
147 if (LocMO.isReg() && LocMO.getReg() == 0)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000148 return ~0u;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000149 for (unsigned i = 0, e = locations.size(); i != e; ++i)
150 if (LocMO.isIdenticalTo(locations[i]))
151 return i;
152 locations.push_back(LocMO);
153 // We are storing a MachineOperand outside a MachineInstr.
154 locations.back().clearParent();
155 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000156 }
157
158 /// addDef - Add a definition point to this value.
159 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
160 // Add a singular (Idx,Idx) -> Loc mapping.
161 LocMap::iterator I = locInts.find(Idx);
162 if (!I.valid() || I.start() != Idx)
163 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
164 }
165
166 /// extendDef - Extend the current definition as far as possible down the
167 /// dominator tree. Stop when meeting an existing def or when leaving the live
168 /// range of VNI.
169 /// @param Idx Starting point for the definition.
170 /// @param LocNo Location number to propagate.
171 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
172 /// @param VNI When LI is not null, this is the value to restrict to.
173 /// @param LIS Live intervals analysis.
174 /// @param MDT Dominator tree.
175 void extendDef(SlotIndex Idx, unsigned LocNo,
176 LiveInterval *LI, const VNInfo *VNI,
177 LiveIntervals &LIS, MachineDominatorTree &MDT);
178
179 /// computeIntervals - Compute the live intervals of all locations after
180 /// collecting all their def points.
181 void computeIntervals(LiveIntervals &LIS, MachineDominatorTree &MDT);
182
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000183 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
184 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
185 const TargetRegisterInfo *TRI);
186
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000187 /// rewriteLocations - Rewrite virtual register locations according to the
188 /// provided virtual register map.
189 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
190
191 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
192 void emitDebugValues(VirtRegMap *VRM,
193 LiveIntervals &LIS, const TargetInstrInfo &TRI);
194
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000195 void print(raw_ostream&, const TargetRegisterInfo*);
196};
197} // namespace
198
199/// LDVImpl - Implementation of the LiveDebugVariables pass.
200namespace {
201class LDVImpl {
202 LiveDebugVariables &pass;
203 LocMap::Allocator allocator;
204 MachineFunction *MF;
205 LiveIntervals *LIS;
206 MachineDominatorTree *MDT;
207 const TargetRegisterInfo *TRI;
208
209 /// userValues - All allocated UserValue instances.
210 SmallVector<UserValue*, 8> userValues;
211
212 /// Map virtual register to eq class leader.
213 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000214 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000215
216 /// Map user variable to eq class leader.
217 typedef DenseMap<const MDNode *, UserValue*> UVMap;
218 UVMap userVarMap;
219
220 /// getUserValue - Find or create a UserValue.
221 UserValue *getUserValue(const MDNode *Var, unsigned Offset);
222
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000223 /// lookupVirtReg - Find the EC leader for VirtReg or null.
224 UserValue *lookupVirtReg(unsigned VirtReg);
225
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000226 /// mapVirtReg - Map virtual register to an equivalence class.
227 void mapVirtReg(unsigned VirtReg, UserValue *EC);
228
229 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
230 /// @param MI DBG_VALUE instruction
231 /// @param Idx Last valid SLotIndex before instruction.
232 /// @return True if the DBG_VALUE instruction should be deleted.
233 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
234
235 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
236 /// a UserValue def for each instruction.
237 /// @param mf MachineFunction to be scanned.
238 /// @return True if any debug values were found.
239 bool collectDebugValues(MachineFunction &mf);
240
241 /// computeIntervals - Compute the live intervals of all user values after
242 /// collecting all their def points.
243 void computeIntervals();
244
245public:
246 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
247 bool runOnMachineFunction(MachineFunction &mf);
248
249 /// clear - Relase all memory.
250 void clear() {
251 DeleteContainerPointers(userValues);
252 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000253 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000254 userVarMap.clear();
255 }
256
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000257 /// renameRegister - Replace all references to OldReg wiht NewReg:SubIdx.
258 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
259
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000260 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
261 void emitDebugValues(VirtRegMap *VRM);
262
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000263 void print(raw_ostream&);
264};
265} // namespace
266
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000267void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
268 if (const MDString *MDS = dyn_cast<MDString>(variable->getOperand(2)))
269 OS << "!\"" << MDS->getString() << "\"\t";
270 if (offset)
271 OS << '+' << offset;
272 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
273 OS << " [" << I.start() << ';' << I.stop() << "):";
274 if (I.value() == ~0u)
275 OS << "undef";
276 else
277 OS << I.value();
278 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000279 for (unsigned i = 0, e = locations.size(); i != e; ++i)
280 OS << " Loc" << i << '=' << locations[i];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000281 OS << '\n';
282}
283
284void LDVImpl::print(raw_ostream &OS) {
285 OS << "********** DEBUG VARIABLES **********\n";
286 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
287 userValues[i]->print(OS, TRI);
288}
289
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000290void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000291 unsigned KeepLoc = 0;
292 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
293 if (KeepLoc == LocNo)
294 continue;
295 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
296 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000297 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000298 // No matches.
299 if (KeepLoc == locations.size())
300 return;
301
302 // Keep the smaller location, erase the larger one.
303 unsigned EraseLoc = LocNo;
304 if (KeepLoc > EraseLoc)
305 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000306 locations.erase(locations.begin() + EraseLoc);
307
308 // Rewrite values.
309 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
310 unsigned v = I.value();
311 if (v == EraseLoc)
312 I.setValue(KeepLoc); // Coalesce when possible.
313 else if (v > EraseLoc)
314 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
315 }
316}
317
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000318UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset) {
319 UserValue *&Leader = userVarMap[Var];
320 if (Leader) {
321 UserValue *UV = Leader->getLeader();
322 Leader = UV;
323 for (; UV; UV = UV->getNext())
324 if (UV->match(Var, Offset))
325 return UV;
326 }
327
328 UserValue *UV = new UserValue(Var, Offset, allocator);
329 userValues.push_back(UV);
330 Leader = UserValue::merge(Leader, UV);
331 return UV;
332}
333
334void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
335 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000336 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000337 Leader = UserValue::merge(Leader, EC);
338}
339
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000340UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000341 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000342 return UV->getLeader();
343 return 0;
344}
345
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000346bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
347 // DBG_VALUE loc, offset, variable
348 if (MI->getNumOperands() != 3 ||
349 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
350 DEBUG(dbgs() << "Can't handle " << *MI);
351 return false;
352 }
353
354 // Get or create the UserValue for (variable,offset).
355 unsigned Offset = MI->getOperand(1).getImm();
356 const MDNode *Var = MI->getOperand(2).getMetadata();
357 UserValue *UV = getUserValue(Var, Offset);
358
359 // If the location is a virtual register, make sure it is mapped.
360 if (MI->getOperand(0).isReg()) {
361 unsigned Reg = MI->getOperand(0).getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000362 if (TargetRegisterInfo::isVirtualRegister(Reg))
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000363 mapVirtReg(Reg, UV);
364 }
365
366 UV->addDef(Idx, MI->getOperand(0));
367 return true;
368}
369
370bool LDVImpl::collectDebugValues(MachineFunction &mf) {
371 bool Changed = false;
372 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
373 ++MFI) {
374 MachineBasicBlock *MBB = MFI;
375 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
376 MBBI != MBBE;) {
377 if (!MBBI->isDebugValue()) {
378 ++MBBI;
379 continue;
380 }
381 // DBG_VALUE has no slot index, use the previous instruction instead.
382 SlotIndex Idx = MBBI == MBB->begin() ?
383 LIS->getMBBStartIdx(MBB) :
384 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
385 // Handle consecutive DBG_VALUE instructions with the same slot index.
386 do {
387 if (handleDebugValue(MBBI, Idx)) {
388 MBBI = MBB->erase(MBBI);
389 Changed = true;
390 } else
391 ++MBBI;
392 } while (MBBI != MBBE && MBBI->isDebugValue());
393 }
394 }
395 return Changed;
396}
397
398void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
399 LiveInterval *LI, const VNInfo *VNI,
400 LiveIntervals &LIS, MachineDominatorTree &MDT) {
401 SmallVector<SlotIndex, 16> Todo;
402 Todo.push_back(Idx);
403
404 do {
405 SlotIndex Start = Todo.pop_back_val();
406 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
407 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
408 LocMap::iterator I = locInts.find(Idx);
409
410 // Limit to VNI's live range.
411 bool ToEnd = true;
412 if (LI && VNI) {
413 LiveRange *Range = LI->getLiveRangeContaining(Start);
414 if (!Range || Range->valno != VNI)
415 continue;
416 if (Range->end < Stop)
417 Stop = Range->end, ToEnd = false;
418 }
419
420 // There could already be a short def at Start.
421 if (I.valid() && I.start() <= Start) {
422 // Stop when meeting a different location or an already extended interval.
423 Start = Start.getNextSlot();
424 if (I.value() != LocNo || I.stop() != Start)
425 continue;
426 // This is a one-slot placeholder. Just skip it.
427 ++I;
428 }
429
430 // Limited by the next def.
431 if (I.valid() && I.start() < Stop)
432 Stop = I.start(), ToEnd = false;
433
434 if (Start >= Stop)
435 continue;
436
437 I.insert(Start, Stop, LocNo);
438
439 // If we extended to the MBB end, propagate down the dominator tree.
440 if (!ToEnd)
441 continue;
442 const std::vector<MachineDomTreeNode*> &Children =
443 MDT.getNode(MBB)->getChildren();
444 for (unsigned i = 0, e = Children.size(); i != e; ++i)
445 Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock()));
446 } while (!Todo.empty());
447}
448
449void
450UserValue::computeIntervals(LiveIntervals &LIS, MachineDominatorTree &MDT) {
451 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
452
453 // Collect all defs to be extended (Skipping undefs).
454 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
455 if (I.value() != ~0u)
456 Defs.push_back(std::make_pair(I.start(), I.value()));
457
458 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
459 SlotIndex Idx = Defs[i].first;
460 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000461 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000462
463 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000464 if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) {
465 LiveInterval *LI = &LIS.getInterval(Loc.getReg());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000466 const VNInfo *VNI = LI->getVNInfoAt(Idx);
467 extendDef(Idx, LocNo, LI, VNI, LIS, MDT);
468 } else
469 extendDef(Idx, LocNo, 0, 0, LIS, MDT);
470 }
471
472 // Finally, erase all the undefs.
473 for (LocMap::iterator I = locInts.begin(); I.valid();)
474 if (I.value() == ~0u)
475 I.erase();
476 else
477 ++I;
478}
479
480void LDVImpl::computeIntervals() {
481 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
482 userValues[i]->computeIntervals(*LIS, *MDT);
483}
484
485bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
486 MF = &mf;
487 LIS = &pass.getAnalysis<LiveIntervals>();
488 MDT = &pass.getAnalysis<MachineDominatorTree>();
489 TRI = mf.getTarget().getRegisterInfo();
490 clear();
491 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
492 << ((Value*)mf.getFunction())->getName()
493 << " **********\n");
494
495 bool Changed = collectDebugValues(mf);
496 computeIntervals();
497 DEBUG(print(dbgs()));
498 return Changed;
499}
500
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000501bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000502 if (!EnableLDV)
503 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000504 if (!pImpl)
505 pImpl = new LDVImpl(this);
506 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
507}
508
509void LiveDebugVariables::releaseMemory() {
510 if (pImpl)
511 static_cast<LDVImpl*>(pImpl)->clear();
512}
513
514LiveDebugVariables::~LiveDebugVariables() {
515 if (pImpl)
516 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000517}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000518
519void UserValue::
520renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
521 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000522 for (unsigned i = locations.size(); i; --i) {
523 unsigned LocNo = i - 1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000524 MachineOperand &Loc = locations[LocNo];
525 if (!Loc.isReg() || Loc.getReg() != OldReg)
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000526 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000527 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
528 Loc.substPhysReg(NewReg, *TRI);
529 else
530 Loc.substVirtReg(NewReg, SubIdx, *TRI);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000531 coalesceLocation(LocNo);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000532 }
533}
534
535void LDVImpl::
536renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000537 UserValue *UV = lookupVirtReg(OldReg);
538 if (!UV)
539 return;
540
541 if (TargetRegisterInfo::isVirtualRegister(NewReg))
542 mapVirtReg(NewReg, UV);
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000543 virtRegToEqClass.erase(OldReg);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000544
545 do {
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000546 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000547 UV = UV->getNext();
548 } while (UV);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000549}
550
551void LiveDebugVariables::
552renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
553 if (pImpl)
554 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
555}
556
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000557void
558UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
559 // Iterate over locations in reverse makes it easier to handle coalescing.
560 for (unsigned i = locations.size(); i ; --i) {
561 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000562 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000563 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000564 if (!Loc.isReg() || !Loc.getReg() ||
565 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000566 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000567 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000568 if (VRM.isAssignedReg(VirtReg)) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000569 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000570 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000571 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000572 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000573 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000574 Loc.setReg(0);
575 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000576 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000577 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000578 }
579 DEBUG(print(dbgs(), &TRI));
580}
581
582/// findInsertLocation - Find an iterator and DebugLoc for inserting a DBG_VALUE
583/// instruction.
584static MachineBasicBlock::iterator
585findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, DebugLoc &DL,
586 LiveIntervals &LIS) {
587 SlotIndex Start = LIS.getMBBStartIdx(MBB);
588 Idx = Idx.getBaseIndex();
589
590 // Try to find an insert location by going backwards from Idx.
591 MachineInstr *MI;
592 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
593 // We've reached the beginning of MBB.
594 if (Idx == Start) {
595 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
596 if (I != MBB->end())
597 DL = I->getDebugLoc();
598 return I;
599 }
600 Idx = Idx.getPrevIndex();
601 }
602 // We found an instruction. The insert point is after the instr.
603 DL = MI->getDebugLoc();
604 return llvm::next(MachineBasicBlock::iterator(MI));
605}
606
607void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
608 unsigned LocNo,
609 LiveIntervals &LIS,
610 const TargetInstrInfo &TII) {
611 DebugLoc DL;
612 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, DL, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000613 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000614
615 // Frame index locations may require a target callback.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000616 if (Loc.isFI()) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000617 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000618 Loc.getIndex(), offset, variable, DL);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000619 if (MI) {
620 MBB->insert(I, MI);
621 return;
622 }
623 }
624 // This is not a frame index, or the target is happy with a standard FI.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000625 BuildMI(*MBB, I, DL, TII.get(TargetOpcode::DBG_VALUE))
626 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000627}
628
629void UserValue::insertDebugKill(MachineBasicBlock *MBB, SlotIndex Idx,
630 LiveIntervals &LIS, const TargetInstrInfo &TII) {
631 DebugLoc DL;
632 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, DL, LIS);
633 BuildMI(*MBB, I, DL, TII.get(TargetOpcode::DBG_VALUE)).addReg(0)
634 .addImm(offset).addMetadata(variable);
635}
636
637void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
638 const TargetInstrInfo &TII) {
639 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
640
641 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
642 SlotIndex Start = I.start();
643 SlotIndex Stop = I.stop();
644 unsigned LocNo = I.value();
645 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
646 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
647 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
648
649 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
650 insertDebugValue(MBB, Start, LocNo, LIS, TII);
651
652 // This interval may span multiple basic blocks.
653 // Insert a DBG_VALUE into each one.
654 while(Stop > MBBEnd) {
655 // Move to the next block.
656 Start = MBBEnd;
657 if (++MBB == MFEnd)
658 break;
659 MBBEnd = LIS.getMBBEndIdx(MBB);
660 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
661 insertDebugValue(MBB, Start, LocNo, LIS, TII);
662 }
663 DEBUG(dbgs() << '\n');
664 if (MBB == MFEnd)
665 break;
666
667 ++I;
668 if (Stop == MBBEnd)
669 continue;
670 // The current interval ends before MBB.
671 // Insert a kill if there is a gap.
672 if (!I.valid() || I.start() > Stop)
673 insertDebugKill(MBB, Stop, LIS, TII);
674 }
675}
676
677void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
678 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
679 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
680 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
681 userValues[i]->rewriteLocations(*VRM, *TRI);
682 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
683 }
684}
685
686void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
687 if (pImpl)
688 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
689}
690
691
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000692#ifndef NDEBUG
693void LiveDebugVariables::dump() {
694 if (pImpl)
695 static_cast<LDVImpl*>(pImpl)->print(dbgs());
696}
697#endif
698