blob: 853ec1ac7c13af22438f863162de11f08a6029e8 [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>
Jakob Stoklund Olesen25dc2262011-01-12 23:36:21 +000043EnableLDV("live-debug-variables", cl::init(true),
Devang Patel51a666f2011-01-07 22:33:41 +000044 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.
Devang Patelf827cd72011-02-04 01:43:25 +000082 DebugLoc dl; ///< The debug location for the variable. This is
83 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000084 UserValue *leader; ///< Equivalence class leader.
85 UserValue *next; ///< Next value in equivalence class, or null.
86
87 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +000088 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000089
90 /// Map of slot indices where this value is live.
91 LocMap locInts;
92
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +000093 /// coalesceLocation - After LocNo was changed, check if it has become
94 /// identical to another location, and coalesce them. This may cause LocNo or
95 /// a later location to be erased, but no earlier location will be erased.
96 void coalesceLocation(unsigned LocNo);
97
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000098 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
99 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
100 LiveIntervals &LIS, const TargetInstrInfo &TII);
101
102 /// insertDebugKill - Insert an undef DBG_VALUE into MBB at Idx.
103 void insertDebugKill(MachineBasicBlock *MBB, SlotIndex Idx,
104 LiveIntervals &LIS, const TargetInstrInfo &TII);
105
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000106public:
107 /// UserValue - Create a new UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000108 UserValue(const MDNode *var, unsigned o, DebugLoc L,
109 LocMap::Allocator &alloc)
110 : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000111 {}
112
113 /// getLeader - Get the leader of this value's equivalence class.
114 UserValue *getLeader() {
115 UserValue *l = leader;
116 while (l != l->leader)
117 l = l->leader;
118 return leader = l;
119 }
120
121 /// getNext - Return the next UserValue in the equivalence class.
122 UserValue *getNext() const { return next; }
123
124 /// match - Does this UserValue match the aprameters?
125 bool match(const MDNode *Var, unsigned Offset) const {
126 return Var == variable && Offset == offset;
127 }
128
129 /// merge - Merge equivalence classes.
130 static UserValue *merge(UserValue *L1, UserValue *L2) {
131 L2 = L2->getLeader();
132 if (!L1)
133 return L2;
134 L1 = L1->getLeader();
135 if (L1 == L2)
136 return L1;
137 // Splice L2 before L1's members.
138 UserValue *End = L2;
139 while (End->next)
140 End->leader = L1, End = End->next;
141 End->leader = L1;
142 End->next = L1->next;
143 L1->next = L2;
144 return L1;
145 }
146
147 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000148 unsigned getLocationNo(const MachineOperand &LocMO) {
149 if (LocMO.isReg() && LocMO.getReg() == 0)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000150 return ~0u;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000151 for (unsigned i = 0, e = locations.size(); i != e; ++i)
152 if (LocMO.isIdenticalTo(locations[i]))
153 return i;
154 locations.push_back(LocMO);
155 // We are storing a MachineOperand outside a MachineInstr.
156 locations.back().clearParent();
157 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000158 }
159
160 /// addDef - Add a definition point to this value.
161 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
162 // Add a singular (Idx,Idx) -> Loc mapping.
163 LocMap::iterator I = locInts.find(Idx);
164 if (!I.valid() || I.start() != Idx)
165 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
166 }
167
168 /// extendDef - Extend the current definition as far as possible down the
169 /// dominator tree. Stop when meeting an existing def or when leaving the live
170 /// range of VNI.
171 /// @param Idx Starting point for the definition.
172 /// @param LocNo Location number to propagate.
173 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
174 /// @param VNI When LI is not null, this is the value to restrict to.
175 /// @param LIS Live intervals analysis.
176 /// @param MDT Dominator tree.
177 void extendDef(SlotIndex Idx, unsigned LocNo,
178 LiveInterval *LI, const VNInfo *VNI,
179 LiveIntervals &LIS, MachineDominatorTree &MDT);
180
181 /// computeIntervals - Compute the live intervals of all locations after
182 /// collecting all their def points.
183 void computeIntervals(LiveIntervals &LIS, MachineDominatorTree &MDT);
184
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000185 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
186 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
187 const TargetRegisterInfo *TRI);
188
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000189 /// rewriteLocations - Rewrite virtual register locations according to the
190 /// provided virtual register map.
191 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
192
193 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
194 void emitDebugValues(VirtRegMap *VRM,
195 LiveIntervals &LIS, const TargetInstrInfo &TRI);
196
Devang Patelf827cd72011-02-04 01:43:25 +0000197 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
198 /// variable may have more than one corresponding DBG_VALUE instructions.
199 /// Only first one needs DebugLoc to identify variable's lexical scope
200 /// in source file.
201 DebugLoc findDebugLoc();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000202 void print(raw_ostream&, const TargetRegisterInfo*);
203};
204} // namespace
205
206/// LDVImpl - Implementation of the LiveDebugVariables pass.
207namespace {
208class LDVImpl {
209 LiveDebugVariables &pass;
210 LocMap::Allocator allocator;
211 MachineFunction *MF;
212 LiveIntervals *LIS;
213 MachineDominatorTree *MDT;
214 const TargetRegisterInfo *TRI;
215
216 /// userValues - All allocated UserValue instances.
217 SmallVector<UserValue*, 8> userValues;
218
219 /// Map virtual register to eq class leader.
220 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000221 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000222
223 /// Map user variable to eq class leader.
224 typedef DenseMap<const MDNode *, UserValue*> UVMap;
225 UVMap userVarMap;
226
227 /// getUserValue - Find or create a UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000228 UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000229
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000230 /// lookupVirtReg - Find the EC leader for VirtReg or null.
231 UserValue *lookupVirtReg(unsigned VirtReg);
232
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000233 /// mapVirtReg - Map virtual register to an equivalence class.
234 void mapVirtReg(unsigned VirtReg, UserValue *EC);
235
236 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
237 /// @param MI DBG_VALUE instruction
238 /// @param Idx Last valid SLotIndex before instruction.
239 /// @return True if the DBG_VALUE instruction should be deleted.
240 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
241
242 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
243 /// a UserValue def for each instruction.
244 /// @param mf MachineFunction to be scanned.
245 /// @return True if any debug values were found.
246 bool collectDebugValues(MachineFunction &mf);
247
248 /// computeIntervals - Compute the live intervals of all user values after
249 /// collecting all their def points.
250 void computeIntervals();
251
252public:
253 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
254 bool runOnMachineFunction(MachineFunction &mf);
255
256 /// clear - Relase all memory.
257 void clear() {
258 DeleteContainerPointers(userValues);
259 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000260 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000261 userVarMap.clear();
262 }
263
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000264 /// renameRegister - Replace all references to OldReg wiht NewReg:SubIdx.
265 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
266
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000267 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
268 void emitDebugValues(VirtRegMap *VRM);
269
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000270 void print(raw_ostream&);
271};
272} // namespace
273
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000274void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
275 if (const MDString *MDS = dyn_cast<MDString>(variable->getOperand(2)))
276 OS << "!\"" << MDS->getString() << "\"\t";
277 if (offset)
278 OS << '+' << offset;
279 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
280 OS << " [" << I.start() << ';' << I.stop() << "):";
281 if (I.value() == ~0u)
282 OS << "undef";
283 else
284 OS << I.value();
285 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000286 for (unsigned i = 0, e = locations.size(); i != e; ++i)
287 OS << " Loc" << i << '=' << locations[i];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000288 OS << '\n';
289}
290
291void LDVImpl::print(raw_ostream &OS) {
292 OS << "********** DEBUG VARIABLES **********\n";
293 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
294 userValues[i]->print(OS, TRI);
295}
296
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000297void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000298 unsigned KeepLoc = 0;
299 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
300 if (KeepLoc == LocNo)
301 continue;
302 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
303 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000304 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000305 // No matches.
306 if (KeepLoc == locations.size())
307 return;
308
309 // Keep the smaller location, erase the larger one.
310 unsigned EraseLoc = LocNo;
311 if (KeepLoc > EraseLoc)
312 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000313 locations.erase(locations.begin() + EraseLoc);
314
315 // Rewrite values.
316 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
317 unsigned v = I.value();
318 if (v == EraseLoc)
319 I.setValue(KeepLoc); // Coalesce when possible.
320 else if (v > EraseLoc)
321 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
322 }
323}
324
Devang Patelf827cd72011-02-04 01:43:25 +0000325UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
326 DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000327 UserValue *&Leader = userVarMap[Var];
328 if (Leader) {
329 UserValue *UV = Leader->getLeader();
330 Leader = UV;
331 for (; UV; UV = UV->getNext())
332 if (UV->match(Var, Offset))
333 return UV;
334 }
335
Devang Patelf827cd72011-02-04 01:43:25 +0000336 UserValue *UV = new UserValue(Var, Offset, DL, allocator);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000337 userValues.push_back(UV);
338 Leader = UserValue::merge(Leader, UV);
339 return UV;
340}
341
342void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
343 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000344 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000345 Leader = UserValue::merge(Leader, EC);
346}
347
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000348UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000349 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000350 return UV->getLeader();
351 return 0;
352}
353
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000354bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
355 // DBG_VALUE loc, offset, variable
356 if (MI->getNumOperands() != 3 ||
357 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
358 DEBUG(dbgs() << "Can't handle " << *MI);
359 return false;
360 }
361
362 // Get or create the UserValue for (variable,offset).
363 unsigned Offset = MI->getOperand(1).getImm();
364 const MDNode *Var = MI->getOperand(2).getMetadata();
Devang Patelf827cd72011-02-04 01:43:25 +0000365 UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000366
367 // If the location is a virtual register, make sure it is mapped.
368 if (MI->getOperand(0).isReg()) {
369 unsigned Reg = MI->getOperand(0).getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000370 if (TargetRegisterInfo::isVirtualRegister(Reg))
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000371 mapVirtReg(Reg, UV);
372 }
373
374 UV->addDef(Idx, MI->getOperand(0));
375 return true;
376}
377
378bool LDVImpl::collectDebugValues(MachineFunction &mf) {
379 bool Changed = false;
380 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
381 ++MFI) {
382 MachineBasicBlock *MBB = MFI;
383 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
384 MBBI != MBBE;) {
385 if (!MBBI->isDebugValue()) {
386 ++MBBI;
387 continue;
388 }
389 // DBG_VALUE has no slot index, use the previous instruction instead.
390 SlotIndex Idx = MBBI == MBB->begin() ?
391 LIS->getMBBStartIdx(MBB) :
392 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
393 // Handle consecutive DBG_VALUE instructions with the same slot index.
394 do {
395 if (handleDebugValue(MBBI, Idx)) {
396 MBBI = MBB->erase(MBBI);
397 Changed = true;
398 } else
399 ++MBBI;
400 } while (MBBI != MBBE && MBBI->isDebugValue());
401 }
402 }
403 return Changed;
404}
405
406void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
407 LiveInterval *LI, const VNInfo *VNI,
408 LiveIntervals &LIS, MachineDominatorTree &MDT) {
409 SmallVector<SlotIndex, 16> Todo;
410 Todo.push_back(Idx);
411
412 do {
413 SlotIndex Start = Todo.pop_back_val();
414 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
415 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000416 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000417
418 // Limit to VNI's live range.
419 bool ToEnd = true;
420 if (LI && VNI) {
421 LiveRange *Range = LI->getLiveRangeContaining(Start);
422 if (!Range || Range->valno != VNI)
423 continue;
424 if (Range->end < Stop)
425 Stop = Range->end, ToEnd = false;
426 }
427
428 // There could already be a short def at Start.
429 if (I.valid() && I.start() <= Start) {
430 // Stop when meeting a different location or an already extended interval.
431 Start = Start.getNextSlot();
432 if (I.value() != LocNo || I.stop() != Start)
433 continue;
434 // This is a one-slot placeholder. Just skip it.
435 ++I;
436 }
437
438 // Limited by the next def.
439 if (I.valid() && I.start() < Stop)
440 Stop = I.start(), ToEnd = false;
441
442 if (Start >= Stop)
443 continue;
444
445 I.insert(Start, Stop, LocNo);
446
447 // If we extended to the MBB end, propagate down the dominator tree.
448 if (!ToEnd)
449 continue;
450 const std::vector<MachineDomTreeNode*> &Children =
451 MDT.getNode(MBB)->getChildren();
452 for (unsigned i = 0, e = Children.size(); i != e; ++i)
453 Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock()));
454 } while (!Todo.empty());
455}
456
457void
458UserValue::computeIntervals(LiveIntervals &LIS, MachineDominatorTree &MDT) {
459 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
460
461 // Collect all defs to be extended (Skipping undefs).
462 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
463 if (I.value() != ~0u)
464 Defs.push_back(std::make_pair(I.start(), I.value()));
465
466 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
467 SlotIndex Idx = Defs[i].first;
468 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000469 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000470
471 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000472 if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) {
473 LiveInterval *LI = &LIS.getInterval(Loc.getReg());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000474 const VNInfo *VNI = LI->getVNInfoAt(Idx);
475 extendDef(Idx, LocNo, LI, VNI, LIS, MDT);
476 } else
477 extendDef(Idx, LocNo, 0, 0, LIS, MDT);
478 }
479
480 // Finally, erase all the undefs.
481 for (LocMap::iterator I = locInts.begin(); I.valid();)
482 if (I.value() == ~0u)
483 I.erase();
484 else
485 ++I;
486}
487
488void LDVImpl::computeIntervals() {
489 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
490 userValues[i]->computeIntervals(*LIS, *MDT);
491}
492
493bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
494 MF = &mf;
495 LIS = &pass.getAnalysis<LiveIntervals>();
496 MDT = &pass.getAnalysis<MachineDominatorTree>();
497 TRI = mf.getTarget().getRegisterInfo();
498 clear();
499 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
500 << ((Value*)mf.getFunction())->getName()
501 << " **********\n");
502
503 bool Changed = collectDebugValues(mf);
504 computeIntervals();
505 DEBUG(print(dbgs()));
506 return Changed;
507}
508
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000509bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000510 if (!EnableLDV)
511 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000512 if (!pImpl)
513 pImpl = new LDVImpl(this);
514 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
515}
516
517void LiveDebugVariables::releaseMemory() {
518 if (pImpl)
519 static_cast<LDVImpl*>(pImpl)->clear();
520}
521
522LiveDebugVariables::~LiveDebugVariables() {
523 if (pImpl)
524 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000525}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000526
527void UserValue::
528renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
529 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000530 for (unsigned i = locations.size(); i; --i) {
531 unsigned LocNo = i - 1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000532 MachineOperand &Loc = locations[LocNo];
533 if (!Loc.isReg() || Loc.getReg() != OldReg)
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000534 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000535 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
536 Loc.substPhysReg(NewReg, *TRI);
537 else
538 Loc.substVirtReg(NewReg, SubIdx, *TRI);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000539 coalesceLocation(LocNo);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000540 }
541}
542
543void LDVImpl::
544renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000545 UserValue *UV = lookupVirtReg(OldReg);
546 if (!UV)
547 return;
548
549 if (TargetRegisterInfo::isVirtualRegister(NewReg))
550 mapVirtReg(NewReg, UV);
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000551 virtRegToEqClass.erase(OldReg);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000552
553 do {
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000554 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000555 UV = UV->getNext();
556 } while (UV);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000557}
558
559void LiveDebugVariables::
560renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
561 if (pImpl)
562 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
563}
564
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000565void
566UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
567 // Iterate over locations in reverse makes it easier to handle coalescing.
568 for (unsigned i = locations.size(); i ; --i) {
569 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000570 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000571 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000572 if (!Loc.isReg() || !Loc.getReg() ||
573 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000574 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000575 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000576 if (VRM.isAssignedReg(VirtReg) &&
577 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000578 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesenf0704d22011-01-12 23:14:07 +0000579 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT &&
580 VRM.isSpillSlotUsed(VRM.getStackSlot(VirtReg))) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000581 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000582 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000583 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000584 Loc.setReg(0);
585 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000586 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000587 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000588 }
589 DEBUG(print(dbgs(), &TRI));
590}
591
Devang Patelf827cd72011-02-04 01:43:25 +0000592/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000593/// instruction.
594static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000595findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000596 LiveIntervals &LIS) {
597 SlotIndex Start = LIS.getMBBStartIdx(MBB);
598 Idx = Idx.getBaseIndex();
599
600 // Try to find an insert location by going backwards from Idx.
601 MachineInstr *MI;
602 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
603 // We've reached the beginning of MBB.
604 if (Idx == Start) {
605 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000606 return I;
607 }
608 Idx = Idx.getPrevIndex();
609 }
Devang Patelf827cd72011-02-04 01:43:25 +0000610
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000611 // Don't insert anything after the first terminator, though.
612 return MI->getDesc().isTerminator() ? MBB->getFirstTerminator() :
613 llvm::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000614}
615
Devang Patelf827cd72011-02-04 01:43:25 +0000616DebugLoc UserValue::findDebugLoc() {
617 DebugLoc D = dl;
618 dl = DebugLoc();
619 return D;
620}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000621void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
622 unsigned LocNo,
623 LiveIntervals &LIS,
624 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000625 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000626 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000627
628 // Frame index locations may require a target callback.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000629 if (Loc.isFI()) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000630 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
Devang Patelf827cd72011-02-04 01:43:25 +0000631 Loc.getIndex(), offset, variable,
632 findDebugLoc());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000633 if (MI) {
634 MBB->insert(I, MI);
635 return;
636 }
637 }
638 // This is not a frame index, or the target is happy with a standard FI.
Devang Patelf827cd72011-02-04 01:43:25 +0000639 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000640 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000641}
642
643void UserValue::insertDebugKill(MachineBasicBlock *MBB, SlotIndex Idx,
644 LiveIntervals &LIS, const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000645 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
646 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)).addReg(0)
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000647 .addImm(offset).addMetadata(variable);
648}
649
650void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
651 const TargetInstrInfo &TII) {
652 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
653
654 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
655 SlotIndex Start = I.start();
656 SlotIndex Stop = I.stop();
657 unsigned LocNo = I.value();
658 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
659 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
660 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
661
662 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
663 insertDebugValue(MBB, Start, LocNo, LIS, TII);
664
665 // This interval may span multiple basic blocks.
666 // Insert a DBG_VALUE into each one.
667 while(Stop > MBBEnd) {
668 // Move to the next block.
669 Start = MBBEnd;
670 if (++MBB == MFEnd)
671 break;
672 MBBEnd = LIS.getMBBEndIdx(MBB);
673 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
674 insertDebugValue(MBB, Start, LocNo, LIS, TII);
675 }
676 DEBUG(dbgs() << '\n');
677 if (MBB == MFEnd)
678 break;
679
680 ++I;
681 if (Stop == MBBEnd)
682 continue;
683 // The current interval ends before MBB.
684 // Insert a kill if there is a gap.
685 if (!I.valid() || I.start() > Stop)
686 insertDebugKill(MBB, Stop, LIS, TII);
687 }
688}
689
690void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
691 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
692 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
693 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
694 userValues[i]->rewriteLocations(*VRM, *TRI);
695 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
696 }
697}
698
699void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
700 if (pImpl)
701 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
702}
703
704
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000705#ifndef NDEBUG
706void LiveDebugVariables::dump() {
707 if (pImpl)
708 static_cast<LDVImpl*>(pImpl)->print(dbgs());
709}
710#endif
711