blob: 24b268a7f8e7d6e71f31479270dd218e81407c09 [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 Olesen1744e472011-03-18 21:42:19 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000034#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000037#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000038#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000039#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000040
41using namespace llvm;
42
Devang Patel51a666f2011-01-07 22:33:41 +000043static cl::opt<bool>
Jakob Stoklund Olesen25dc2262011-01-12 23:36:21 +000044EnableLDV("live-debug-variables", cl::init(true),
Devang Patel51a666f2011-01-07 22:33:41 +000045 cl::desc("Enable the live debug variables pass"), cl::Hidden);
46
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000047char LiveDebugVariables::ID = 0;
48
49INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
50 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000051INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000052INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
53INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
54 "Debug Variable Analysis", false, false)
55
56void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000057 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000058 AU.addRequiredTransitive<LiveIntervals>();
59 AU.setPreservesAll();
60 MachineFunctionPass::getAnalysisUsage(AU);
61}
62
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000063LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) {
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000064 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
65}
66
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000067/// LocMap - Map of where a user value is live, and its location.
68typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
69
70/// UserValue - A user value is a part of a debug info user variable.
71///
72/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
73/// holds part of a user variable. The part is identified by a byte offset.
74///
75/// UserValues are grouped into equivalence classes for easier searching. Two
76/// user values are related if they refer to the same variable, or if they are
77/// held by the same virtual register. The equivalence class is the transitive
78/// closure of that relation.
79namespace {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000080class LDVImpl;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000081class UserValue {
82 const MDNode *variable; ///< The debug info variable we are part of.
83 unsigned offset; ///< Byte offset into variable.
Devang Patelf827cd72011-02-04 01:43:25 +000084 DebugLoc dl; ///< The debug location for the variable. This is
85 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000086 UserValue *leader; ///< Equivalence class leader.
87 UserValue *next; ///< Next value in equivalence class, or null.
88
89 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +000090 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000091
92 /// Map of slot indices where this value is live.
93 LocMap locInts;
94
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +000095 /// coalesceLocation - After LocNo was changed, check if it has become
96 /// identical to another location, and coalesce them. This may cause LocNo or
97 /// a later location to be erased, but no earlier location will be erased.
98 void coalesceLocation(unsigned LocNo);
99
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000100 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
101 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
102 LiveIntervals &LIS, const TargetInstrInfo &TII);
103
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000104public:
105 /// UserValue - Create a new UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000106 UserValue(const MDNode *var, unsigned o, DebugLoc L,
107 LocMap::Allocator &alloc)
108 : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000109 {}
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) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000147 if (LocMO.isReg()) {
148 if (LocMO.getReg() == 0)
149 return ~0u;
150 // For register locations we dont care about use/def and other flags.
151 for (unsigned i = 0, e = locations.size(); i != e; ++i)
152 if (locations[i].isReg() &&
153 locations[i].getReg() == LocMO.getReg() &&
154 locations[i].getSubReg() == LocMO.getSubReg())
155 return i;
156 } else
157 for (unsigned i = 0, e = locations.size(); i != e; ++i)
158 if (LocMO.isIdenticalTo(locations[i]))
159 return i;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000160 locations.push_back(LocMO);
161 // We are storing a MachineOperand outside a MachineInstr.
162 locations.back().clearParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000163 // Don't store def operands.
164 if (locations.back().isReg())
165 locations.back().setIsUse();
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000166 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000167 }
168
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000169 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
170 void mapVirtRegs(LDVImpl *LDV);
171
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000172 /// addDef - Add a definition point to this value.
173 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
174 // Add a singular (Idx,Idx) -> Loc mapping.
175 LocMap::iterator I = locInts.find(Idx);
176 if (!I.valid() || I.start() != Idx)
177 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
178 }
179
180 /// extendDef - Extend the current definition as far as possible down the
181 /// dominator tree. Stop when meeting an existing def or when leaving the live
182 /// range of VNI.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000183 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000184 /// @param Idx Starting point for the definition.
185 /// @param LocNo Location number to propagate.
186 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
187 /// @param VNI When LI is not null, this is the value to restrict to.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000188 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000189 /// @param LIS Live intervals analysis.
190 /// @param MDT Dominator tree.
191 void extendDef(SlotIndex Idx, unsigned LocNo,
192 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000193 SmallVectorImpl<SlotIndex> *Kills,
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000194 LiveIntervals &LIS, MachineDominatorTree &MDT);
195
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000196 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
197 /// registers. Determine if any of the copies are available at the kill
198 /// points, and add defs if possible.
199 /// @param LI Scan for copies of the value in LI->reg.
200 /// @param LocNo Location number of LI->reg.
201 /// @param Kills Points where the range of LocNo could be extended.
202 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
203 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
204 const SmallVectorImpl<SlotIndex> &Kills,
205 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
206 MachineRegisterInfo &MRI,
207 LiveIntervals &LIS);
208
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000209 /// computeIntervals - Compute the live intervals of all locations after
210 /// collecting all their def points.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000211 void computeIntervals(MachineRegisterInfo &MRI,
212 LiveIntervals &LIS, MachineDominatorTree &MDT);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000213
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000214 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
215 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
216 const TargetRegisterInfo *TRI);
217
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000218 /// rewriteLocations - Rewrite virtual register locations according to the
219 /// provided virtual register map.
220 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
221
222 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
223 void emitDebugValues(VirtRegMap *VRM,
224 LiveIntervals &LIS, const TargetInstrInfo &TRI);
225
Devang Patelf827cd72011-02-04 01:43:25 +0000226 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
227 /// variable may have more than one corresponding DBG_VALUE instructions.
228 /// Only first one needs DebugLoc to identify variable's lexical scope
229 /// in source file.
230 DebugLoc findDebugLoc();
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000231 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000232};
233} // namespace
234
235/// LDVImpl - Implementation of the LiveDebugVariables pass.
236namespace {
237class LDVImpl {
238 LiveDebugVariables &pass;
239 LocMap::Allocator allocator;
240 MachineFunction *MF;
241 LiveIntervals *LIS;
242 MachineDominatorTree *MDT;
243 const TargetRegisterInfo *TRI;
244
245 /// userValues - All allocated UserValue instances.
246 SmallVector<UserValue*, 8> userValues;
247
248 /// Map virtual register to eq class leader.
249 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000250 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000251
252 /// Map user variable to eq class leader.
253 typedef DenseMap<const MDNode *, UserValue*> UVMap;
254 UVMap userVarMap;
255
256 /// getUserValue - Find or create a UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000257 UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000258
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000259 /// lookupVirtReg - Find the EC leader for VirtReg or null.
260 UserValue *lookupVirtReg(unsigned VirtReg);
261
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000262 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
263 /// @param MI DBG_VALUE instruction
264 /// @param Idx Last valid SLotIndex before instruction.
265 /// @return True if the DBG_VALUE instruction should be deleted.
266 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
267
268 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
269 /// a UserValue def for each instruction.
270 /// @param mf MachineFunction to be scanned.
271 /// @return True if any debug values were found.
272 bool collectDebugValues(MachineFunction &mf);
273
274 /// computeIntervals - Compute the live intervals of all user values after
275 /// collecting all their def points.
276 void computeIntervals();
277
278public:
279 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
280 bool runOnMachineFunction(MachineFunction &mf);
281
282 /// clear - Relase all memory.
283 void clear() {
284 DeleteContainerPointers(userValues);
285 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000286 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000287 userVarMap.clear();
288 }
289
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000290 /// mapVirtReg - Map virtual register to an equivalence class.
291 void mapVirtReg(unsigned VirtReg, UserValue *EC);
292
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000293 /// renameRegister - Replace all references to OldReg with NewReg:SubIdx.
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000294 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
295
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000296 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
297 void emitDebugValues(VirtRegMap *VRM);
298
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000299 void print(raw_ostream&);
300};
301} // namespace
302
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000303void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000304 if (const MDString *MDS = dyn_cast<MDString>(variable->getOperand(2)))
305 OS << "!\"" << MDS->getString() << "\"\t";
306 if (offset)
307 OS << '+' << offset;
308 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
309 OS << " [" << I.start() << ';' << I.stop() << "):";
310 if (I.value() == ~0u)
311 OS << "undef";
312 else
313 OS << I.value();
314 }
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000315 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
316 OS << " Loc" << i << '=';
317 locations[i].print(OS, TM);
318 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000319 OS << '\n';
320}
321
322void LDVImpl::print(raw_ostream &OS) {
323 OS << "********** DEBUG VARIABLES **********\n";
324 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000325 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000326}
327
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000328void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000329 unsigned KeepLoc = 0;
330 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
331 if (KeepLoc == LocNo)
332 continue;
333 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
334 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000335 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000336 // No matches.
337 if (KeepLoc == locations.size())
338 return;
339
340 // Keep the smaller location, erase the larger one.
341 unsigned EraseLoc = LocNo;
342 if (KeepLoc > EraseLoc)
343 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000344 locations.erase(locations.begin() + EraseLoc);
345
346 // Rewrite values.
347 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
348 unsigned v = I.value();
349 if (v == EraseLoc)
350 I.setValue(KeepLoc); // Coalesce when possible.
351 else if (v > EraseLoc)
352 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
353 }
354}
355
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000356void UserValue::mapVirtRegs(LDVImpl *LDV) {
357 for (unsigned i = 0, e = locations.size(); i != e; ++i)
358 if (locations[i].isReg() &&
359 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
360 LDV->mapVirtReg(locations[i].getReg(), this);
361}
362
Devang Patelf827cd72011-02-04 01:43:25 +0000363UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
364 DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000365 UserValue *&Leader = userVarMap[Var];
366 if (Leader) {
367 UserValue *UV = Leader->getLeader();
368 Leader = UV;
369 for (; UV; UV = UV->getNext())
370 if (UV->match(Var, Offset))
371 return UV;
372 }
373
Devang Patelf827cd72011-02-04 01:43:25 +0000374 UserValue *UV = new UserValue(Var, Offset, DL, allocator);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000375 userValues.push_back(UV);
376 Leader = UserValue::merge(Leader, UV);
377 return UV;
378}
379
380void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
381 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000382 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000383 Leader = UserValue::merge(Leader, EC);
384}
385
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000386UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000387 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000388 return UV->getLeader();
389 return 0;
390}
391
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000392bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
393 // DBG_VALUE loc, offset, variable
394 if (MI->getNumOperands() != 3 ||
395 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
396 DEBUG(dbgs() << "Can't handle " << *MI);
397 return false;
398 }
399
400 // Get or create the UserValue for (variable,offset).
401 unsigned Offset = MI->getOperand(1).getImm();
402 const MDNode *Var = MI->getOperand(2).getMetadata();
Devang Patelf827cd72011-02-04 01:43:25 +0000403 UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000404 UV->addDef(Idx, MI->getOperand(0));
405 return true;
406}
407
408bool LDVImpl::collectDebugValues(MachineFunction &mf) {
409 bool Changed = false;
410 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
411 ++MFI) {
412 MachineBasicBlock *MBB = MFI;
413 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
414 MBBI != MBBE;) {
415 if (!MBBI->isDebugValue()) {
416 ++MBBI;
417 continue;
418 }
419 // DBG_VALUE has no slot index, use the previous instruction instead.
420 SlotIndex Idx = MBBI == MBB->begin() ?
421 LIS->getMBBStartIdx(MBB) :
422 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
423 // Handle consecutive DBG_VALUE instructions with the same slot index.
424 do {
425 if (handleDebugValue(MBBI, Idx)) {
426 MBBI = MBB->erase(MBBI);
427 Changed = true;
428 } else
429 ++MBBI;
430 } while (MBBI != MBBE && MBBI->isDebugValue());
431 }
432 }
433 return Changed;
434}
435
436void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
437 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000438 SmallVectorImpl<SlotIndex> *Kills,
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000439 LiveIntervals &LIS, MachineDominatorTree &MDT) {
440 SmallVector<SlotIndex, 16> Todo;
441 Todo.push_back(Idx);
442
443 do {
444 SlotIndex Start = Todo.pop_back_val();
445 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
446 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000447 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000448
449 // Limit to VNI's live range.
450 bool ToEnd = true;
451 if (LI && VNI) {
452 LiveRange *Range = LI->getLiveRangeContaining(Start);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000453 if (!Range || Range->valno != VNI) {
454 if (Kills)
455 Kills->push_back(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000456 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000457 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000458 if (Range->end < Stop)
459 Stop = Range->end, ToEnd = false;
460 }
461
462 // There could already be a short def at Start.
463 if (I.valid() && I.start() <= Start) {
464 // Stop when meeting a different location or an already extended interval.
465 Start = Start.getNextSlot();
466 if (I.value() != LocNo || I.stop() != Start)
467 continue;
468 // This is a one-slot placeholder. Just skip it.
469 ++I;
470 }
471
472 // Limited by the next def.
473 if (I.valid() && I.start() < Stop)
474 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000475 // Limited by VNI's live range.
476 else if (!ToEnd && Kills)
477 Kills->push_back(Stop);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000478
479 if (Start >= Stop)
480 continue;
481
482 I.insert(Start, Stop, LocNo);
483
484 // If we extended to the MBB end, propagate down the dominator tree.
485 if (!ToEnd)
486 continue;
487 const std::vector<MachineDomTreeNode*> &Children =
488 MDT.getNode(MBB)->getChildren();
489 for (unsigned i = 0, e = Children.size(); i != e; ++i)
490 Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock()));
491 } while (!Todo.empty());
492}
493
494void
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000495UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
496 const SmallVectorImpl<SlotIndex> &Kills,
497 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
498 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
499 if (Kills.empty())
500 return;
501 // Don't track copies from physregs, there are too many uses.
502 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
503 return;
504
505 // Collect all the (vreg, valno) pairs that are copies of LI.
506 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
507 for (MachineRegisterInfo::use_nodbg_iterator
508 UI = MRI.use_nodbg_begin(LI->reg),
509 UE = MRI.use_nodbg_end(); UI != UE; ++UI) {
510 // Copies of the full value.
511 if (UI.getOperand().getSubReg() || !UI->isCopy())
512 continue;
513 MachineInstr *MI = &*UI;
514 unsigned DstReg = MI->getOperand(0).getReg();
515
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000516 // Don't follow copies to physregs. These are usually setting up call
517 // arguments, and the argument registers are always call clobbered. We are
518 // better off in the source register which could be a callee-saved register,
519 // or it could be spilled.
520 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
521 continue;
522
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000523 // Is LocNo extended to reach this copy? If not, another def may be blocking
524 // it, or we are looking at a wrong value of LI.
525 SlotIndex Idx = LIS.getInstructionIndex(MI);
526 LocMap::iterator I = locInts.find(Idx.getUseIndex());
527 if (!I.valid() || I.value() != LocNo)
528 continue;
529
530 if (!LIS.hasInterval(DstReg))
531 continue;
532 LiveInterval *DstLI = &LIS.getInterval(DstReg);
533 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getDefIndex());
534 assert(DstVNI && DstVNI->def == Idx.getDefIndex() && "Bad copy value");
535 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
536 }
537
538 if (CopyValues.empty())
539 return;
540
541 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
542
543 // Try to add defs of the copied values for each kill point.
544 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
545 SlotIndex Idx = Kills[i];
546 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
547 LiveInterval *DstLI = CopyValues[j].first;
548 const VNInfo *DstVNI = CopyValues[j].second;
549 if (DstLI->getVNInfoAt(Idx) != DstVNI)
550 continue;
551 // Check that there isn't already a def at Idx
552 LocMap::iterator I = locInts.find(Idx);
553 if (I.valid() && I.start() <= Idx)
554 continue;
555 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
556 << DstVNI->id << " in " << *DstLI << '\n');
557 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
558 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
559 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
560 I.insert(Idx, Idx.getNextSlot(), LocNo);
561 NewDefs.push_back(std::make_pair(Idx, LocNo));
562 break;
563 }
564 }
565}
566
567void
568UserValue::computeIntervals(MachineRegisterInfo &MRI,
569 LiveIntervals &LIS,
570 MachineDominatorTree &MDT) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000571 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
572
573 // Collect all defs to be extended (Skipping undefs).
574 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
575 if (I.value() != ~0u)
576 Defs.push_back(std::make_pair(I.start(), I.value()));
577
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000578 // Extend all defs, and possibly add new ones along the way.
579 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000580 SlotIndex Idx = Defs[i].first;
581 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000582 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000583
584 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000585 if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) {
586 LiveInterval *LI = &LIS.getInterval(Loc.getReg());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000587 const VNInfo *VNI = LI->getVNInfoAt(Idx);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000588 SmallVector<SlotIndex, 16> Kills;
589 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT);
590 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000591 } else
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000592 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000593 }
594
595 // Finally, erase all the undefs.
596 for (LocMap::iterator I = locInts.begin(); I.valid();)
597 if (I.value() == ~0u)
598 I.erase();
599 else
600 ++I;
601}
602
603void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000604 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
605 userValues[i]->computeIntervals(MF->getRegInfo(), *LIS, *MDT);
606 userValues[i]->mapVirtRegs(this);
607 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000608}
609
610bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
611 MF = &mf;
612 LIS = &pass.getAnalysis<LiveIntervals>();
613 MDT = &pass.getAnalysis<MachineDominatorTree>();
614 TRI = mf.getTarget().getRegisterInfo();
615 clear();
616 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
617 << ((Value*)mf.getFunction())->getName()
618 << " **********\n");
619
620 bool Changed = collectDebugValues(mf);
621 computeIntervals();
622 DEBUG(print(dbgs()));
623 return Changed;
624}
625
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000626bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000627 if (!EnableLDV)
628 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000629 if (!pImpl)
630 pImpl = new LDVImpl(this);
631 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
632}
633
634void LiveDebugVariables::releaseMemory() {
635 if (pImpl)
636 static_cast<LDVImpl*>(pImpl)->clear();
637}
638
639LiveDebugVariables::~LiveDebugVariables() {
640 if (pImpl)
641 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000642}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000643
644void UserValue::
645renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
646 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000647 for (unsigned i = locations.size(); i; --i) {
648 unsigned LocNo = i - 1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000649 MachineOperand &Loc = locations[LocNo];
650 if (!Loc.isReg() || Loc.getReg() != OldReg)
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000651 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000652 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
653 Loc.substPhysReg(NewReg, *TRI);
654 else
655 Loc.substVirtReg(NewReg, SubIdx, *TRI);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000656 coalesceLocation(LocNo);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000657 }
658}
659
660void LDVImpl::
661renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000662 UserValue *UV = lookupVirtReg(OldReg);
663 if (!UV)
664 return;
665
666 if (TargetRegisterInfo::isVirtualRegister(NewReg))
667 mapVirtReg(NewReg, UV);
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000668 virtRegToEqClass.erase(OldReg);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000669
670 do {
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000671 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000672 UV = UV->getNext();
673 } while (UV);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000674}
675
676void LiveDebugVariables::
677renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
678 if (pImpl)
679 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
680}
681
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000682void
683UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
684 // Iterate over locations in reverse makes it easier to handle coalescing.
685 for (unsigned i = locations.size(); i ; --i) {
686 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000687 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000688 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000689 if (!Loc.isReg() || !Loc.getReg() ||
690 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000691 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000692 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000693 if (VRM.isAssignedReg(VirtReg) &&
694 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000695 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesenf0704d22011-01-12 23:14:07 +0000696 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT &&
697 VRM.isSpillSlotUsed(VRM.getStackSlot(VirtReg))) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000698 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000699 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000700 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000701 Loc.setReg(0);
702 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000703 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000704 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000705 }
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000706}
707
Devang Patelf827cd72011-02-04 01:43:25 +0000708/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000709/// instruction.
710static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000711findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000712 LiveIntervals &LIS) {
713 SlotIndex Start = LIS.getMBBStartIdx(MBB);
714 Idx = Idx.getBaseIndex();
715
716 // Try to find an insert location by going backwards from Idx.
717 MachineInstr *MI;
718 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
719 // We've reached the beginning of MBB.
720 if (Idx == Start) {
721 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000722 return I;
723 }
724 Idx = Idx.getPrevIndex();
725 }
Devang Patelf827cd72011-02-04 01:43:25 +0000726
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000727 // Don't insert anything after the first terminator, though.
728 return MI->getDesc().isTerminator() ? MBB->getFirstTerminator() :
729 llvm::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000730}
731
Devang Patelf827cd72011-02-04 01:43:25 +0000732DebugLoc UserValue::findDebugLoc() {
733 DebugLoc D = dl;
734 dl = DebugLoc();
735 return D;
736}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000737void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
738 unsigned LocNo,
739 LiveIntervals &LIS,
740 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000741 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000742 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000743
744 // Frame index locations may require a target callback.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000745 if (Loc.isFI()) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000746 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
Devang Patelf827cd72011-02-04 01:43:25 +0000747 Loc.getIndex(), offset, variable,
748 findDebugLoc());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000749 if (MI) {
750 MBB->insert(I, MI);
751 return;
752 }
753 }
754 // This is not a frame index, or the target is happy with a standard FI.
Devang Patelf827cd72011-02-04 01:43:25 +0000755 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000756 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000757}
758
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000759void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
760 const TargetInstrInfo &TII) {
761 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
762
763 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
764 SlotIndex Start = I.start();
765 SlotIndex Stop = I.stop();
766 unsigned LocNo = I.value();
767 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
768 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
769 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
770
771 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
772 insertDebugValue(MBB, Start, LocNo, LIS, TII);
773
774 // This interval may span multiple basic blocks.
775 // Insert a DBG_VALUE into each one.
776 while(Stop > MBBEnd) {
777 // Move to the next block.
778 Start = MBBEnd;
779 if (++MBB == MFEnd)
780 break;
781 MBBEnd = LIS.getMBBEndIdx(MBB);
782 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
783 insertDebugValue(MBB, Start, LocNo, LIS, TII);
784 }
785 DEBUG(dbgs() << '\n');
786 if (MBB == MFEnd)
787 break;
788
789 ++I;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000790 }
791}
792
793void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
794 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
795 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
796 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
797 userValues[i]->rewriteLocations(*VRM, *TRI);
798 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
799 }
800}
801
802void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
803 if (pImpl)
804 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
805}
806
807
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000808#ifndef NDEBUG
809void LiveDebugVariables::dump() {
810 if (pImpl)
811 static_cast<LDVImpl*>(pImpl)->print(dbgs());
812}
813#endif
814