blob: 438adfaf35c936316130528b70fd283481cf9ab1 [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"
Devang Patela2b552d2011-08-09 01:03:35 +000028#include "llvm/Analysis/DebugInfo.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000029#include "llvm/ADT/IntervalMap.h"
Devang Patelad90d3a2011-08-04 18:45:38 +000030#include "llvm/ADT/Statistic.h"
Devang Patelc722c3d2011-08-10 21:25:34 +000031#include "llvm/CodeGen/LexicalScopes.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000033#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000034#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000037#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000038#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000040#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000041#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000042#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000043
44using namespace llvm;
45
Devang Patel51a666f2011-01-07 22:33:41 +000046static cl::opt<bool>
Jakob Stoklund Olesen25dc2262011-01-12 23:36:21 +000047EnableLDV("live-debug-variables", cl::init(true),
Devang Patel51a666f2011-01-07 22:33:41 +000048 cl::desc("Enable the live debug variables pass"), cl::Hidden);
49
Devang Patelad90d3a2011-08-04 18:45:38 +000050STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000051char LiveDebugVariables::ID = 0;
52
53INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
54 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000055INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000056INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
57INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
58 "Debug Variable Analysis", false, false)
59
60void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000061 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000062 AU.addRequiredTransitive<LiveIntervals>();
63 AU.setPreservesAll();
64 MachineFunctionPass::getAnalysisUsage(AU);
65}
66
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000067LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) {
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000068 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
69}
70
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000071/// LocMap - Map of where a user value is live, and its location.
72typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
73
74/// UserValue - A user value is a part of a debug info user variable.
75///
76/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
77/// holds part of a user variable. The part is identified by a byte offset.
78///
79/// UserValues are grouped into equivalence classes for easier searching. Two
80/// user values are related if they refer to the same variable, or if they are
81/// held by the same virtual register. The equivalence class is the transitive
82/// closure of that relation.
83namespace {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000084class LDVImpl;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000085class UserValue {
86 const MDNode *variable; ///< The debug info variable we are part of.
87 unsigned offset; ///< Byte offset into variable.
Devang Patelf827cd72011-02-04 01:43:25 +000088 DebugLoc dl; ///< The debug location for the variable. This is
89 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000090 UserValue *leader; ///< Equivalence class leader.
91 UserValue *next; ///< Next value in equivalence class, or null.
92
93 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +000094 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000095
96 /// Map of slot indices where this value is live.
97 LocMap locInts;
98
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +000099 /// coalesceLocation - After LocNo was changed, check if it has become
100 /// identical to another location, and coalesce them. This may cause LocNo or
101 /// a later location to be erased, but no earlier location will be erased.
102 void coalesceLocation(unsigned LocNo);
103
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000104 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
105 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
106 LiveIntervals &LIS, const TargetInstrInfo &TII);
107
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000108 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
109 /// is live. Returns true if any changes were made.
110 bool splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
111
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000112public:
113 /// UserValue - Create a new UserValue.
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000114 UserValue(const MDNode *var, unsigned o, DebugLoc L,
Devang Patelf827cd72011-02-04 01:43:25 +0000115 LocMap::Allocator &alloc)
116 : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000117 {}
118
119 /// getLeader - Get the leader of this value's equivalence class.
120 UserValue *getLeader() {
121 UserValue *l = leader;
122 while (l != l->leader)
123 l = l->leader;
124 return leader = l;
125 }
126
127 /// getNext - Return the next UserValue in the equivalence class.
128 UserValue *getNext() const { return next; }
129
Devang Patela462d6e2011-07-06 23:09:51 +0000130 /// match - Does this UserValue match the parameters?
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000131 bool match(const MDNode *Var, unsigned Offset) const {
132 return Var == variable && Offset == offset;
133 }
134
135 /// merge - Merge equivalence classes.
136 static UserValue *merge(UserValue *L1, UserValue *L2) {
137 L2 = L2->getLeader();
138 if (!L1)
139 return L2;
140 L1 = L1->getLeader();
141 if (L1 == L2)
142 return L1;
143 // Splice L2 before L1's members.
144 UserValue *End = L2;
145 while (End->next)
146 End->leader = L1, End = End->next;
147 End->leader = L1;
148 End->next = L1->next;
149 L1->next = L2;
150 return L1;
151 }
152
153 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000154 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000155 if (LocMO.isReg()) {
156 if (LocMO.getReg() == 0)
157 return ~0u;
158 // For register locations we dont care about use/def and other flags.
159 for (unsigned i = 0, e = locations.size(); i != e; ++i)
160 if (locations[i].isReg() &&
161 locations[i].getReg() == LocMO.getReg() &&
162 locations[i].getSubReg() == LocMO.getSubReg())
163 return i;
164 } else
165 for (unsigned i = 0, e = locations.size(); i != e; ++i)
166 if (LocMO.isIdenticalTo(locations[i]))
167 return i;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000168 locations.push_back(LocMO);
169 // We are storing a MachineOperand outside a MachineInstr.
170 locations.back().clearParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000171 // Don't store def operands.
172 if (locations.back().isReg())
173 locations.back().setIsUse();
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000174 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000175 }
176
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000177 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
178 void mapVirtRegs(LDVImpl *LDV);
179
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000180 /// addDef - Add a definition point to this value.
181 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
182 // Add a singular (Idx,Idx) -> Loc mapping.
183 LocMap::iterator I = locInts.find(Idx);
184 if (!I.valid() || I.start() != Idx)
185 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen79513ed2011-08-03 23:44:31 +0000186 else
187 // A later DBG_VALUE at the same SlotIndex overrides the old location.
188 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000189 }
190
191 /// extendDef - Extend the current definition as far as possible down the
192 /// dominator tree. Stop when meeting an existing def or when leaving the live
193 /// range of VNI.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000194 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000195 /// @param Idx Starting point for the definition.
196 /// @param LocNo Location number to propagate.
197 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
198 /// @param VNI When LI is not null, this is the value to restrict to.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000199 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000200 /// @param LIS Live intervals analysis.
201 /// @param MDT Dominator tree.
202 void extendDef(SlotIndex Idx, unsigned LocNo,
203 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000204 SmallVectorImpl<SlotIndex> *Kills,
Devang Patelc722c3d2011-08-10 21:25:34 +0000205 LiveIntervals &LIS, MachineDominatorTree &MDT,
206 LexicalScopes &LS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000207
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000208 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
209 /// registers. Determine if any of the copies are available at the kill
210 /// points, and add defs if possible.
211 /// @param LI Scan for copies of the value in LI->reg.
212 /// @param LocNo Location number of LI->reg.
213 /// @param Kills Points where the range of LocNo could be extended.
214 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
215 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
216 const SmallVectorImpl<SlotIndex> &Kills,
217 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
218 MachineRegisterInfo &MRI,
219 LiveIntervals &LIS);
220
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000221 /// computeIntervals - Compute the live intervals of all locations after
222 /// collecting all their def points.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000223 void computeIntervals(MachineRegisterInfo &MRI,
Devang Patelc722c3d2011-08-10 21:25:34 +0000224 LiveIntervals &LIS, MachineDominatorTree &MDT,
225 LexicalScopes &LS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000226
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000227 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
228 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
229 const TargetRegisterInfo *TRI);
230
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000231 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
232 /// live. Returns true if any changes were made.
233 bool splitRegister(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
234
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000235 /// rewriteLocations - Rewrite virtual register locations according to the
236 /// provided virtual register map.
237 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
238
239 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
240 void emitDebugValues(VirtRegMap *VRM,
241 LiveIntervals &LIS, const TargetInstrInfo &TRI);
242
Devang Patelf827cd72011-02-04 01:43:25 +0000243 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
244 /// variable may have more than one corresponding DBG_VALUE instructions.
245 /// Only first one needs DebugLoc to identify variable's lexical scope
246 /// in source file.
247 DebugLoc findDebugLoc();
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000248 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000249};
250} // namespace
251
252/// LDVImpl - Implementation of the LiveDebugVariables pass.
253namespace {
254class LDVImpl {
255 LiveDebugVariables &pass;
256 LocMap::Allocator allocator;
257 MachineFunction *MF;
258 LiveIntervals *LIS;
Devang Patelc722c3d2011-08-10 21:25:34 +0000259 LexicalScopes LS;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000260 MachineDominatorTree *MDT;
261 const TargetRegisterInfo *TRI;
262
263 /// userValues - All allocated UserValue instances.
264 SmallVector<UserValue*, 8> userValues;
265
266 /// Map virtual register to eq class leader.
267 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000268 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000269
270 /// Map user variable to eq class leader.
271 typedef DenseMap<const MDNode *, UserValue*> UVMap;
272 UVMap userVarMap;
273
274 /// getUserValue - Find or create a UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000275 UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000276
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000277 /// lookupVirtReg - Find the EC leader for VirtReg or null.
278 UserValue *lookupVirtReg(unsigned VirtReg);
279
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000280 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
281 /// @param MI DBG_VALUE instruction
282 /// @param Idx Last valid SLotIndex before instruction.
283 /// @return True if the DBG_VALUE instruction should be deleted.
284 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
285
286 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
287 /// a UserValue def for each instruction.
288 /// @param mf MachineFunction to be scanned.
289 /// @return True if any debug values were found.
290 bool collectDebugValues(MachineFunction &mf);
291
292 /// computeIntervals - Compute the live intervals of all user values after
293 /// collecting all their def points.
294 void computeIntervals();
295
296public:
297 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
298 bool runOnMachineFunction(MachineFunction &mf);
299
300 /// clear - Relase all memory.
301 void clear() {
302 DeleteContainerPointers(userValues);
303 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000304 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000305 userVarMap.clear();
306 }
307
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000308 /// mapVirtReg - Map virtual register to an equivalence class.
309 void mapVirtReg(unsigned VirtReg, UserValue *EC);
310
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000311 /// renameRegister - Replace all references to OldReg with NewReg:SubIdx.
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000312 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
313
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000314 /// splitRegister - Replace all references to OldReg with NewRegs.
315 void splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs);
316
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000317 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
318 void emitDebugValues(VirtRegMap *VRM);
319
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000320 void print(raw_ostream&);
321};
322} // namespace
323
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000324void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Devang Patela2b552d2011-08-09 01:03:35 +0000325 DIVariable DV(variable);
326 OS << "!\"";
327 DV.printExtendedName(OS);
328 OS << "\"\t";
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000329 if (offset)
330 OS << '+' << offset;
331 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
332 OS << " [" << I.start() << ';' << I.stop() << "):";
333 if (I.value() == ~0u)
334 OS << "undef";
335 else
336 OS << I.value();
337 }
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000338 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
339 OS << " Loc" << i << '=';
340 locations[i].print(OS, TM);
341 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000342 OS << '\n';
343}
344
345void LDVImpl::print(raw_ostream &OS) {
346 OS << "********** DEBUG VARIABLES **********\n";
347 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000348 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000349}
350
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000351void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000352 unsigned KeepLoc = 0;
353 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
354 if (KeepLoc == LocNo)
355 continue;
356 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
357 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000358 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000359 // No matches.
360 if (KeepLoc == locations.size())
361 return;
362
363 // Keep the smaller location, erase the larger one.
364 unsigned EraseLoc = LocNo;
365 if (KeepLoc > EraseLoc)
366 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000367 locations.erase(locations.begin() + EraseLoc);
368
369 // Rewrite values.
370 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
371 unsigned v = I.value();
372 if (v == EraseLoc)
373 I.setValue(KeepLoc); // Coalesce when possible.
374 else if (v > EraseLoc)
375 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
376 }
377}
378
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000379void UserValue::mapVirtRegs(LDVImpl *LDV) {
380 for (unsigned i = 0, e = locations.size(); i != e; ++i)
381 if (locations[i].isReg() &&
382 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
383 LDV->mapVirtReg(locations[i].getReg(), this);
384}
385
Devang Patelf827cd72011-02-04 01:43:25 +0000386UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
387 DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000388 UserValue *&Leader = userVarMap[Var];
389 if (Leader) {
390 UserValue *UV = Leader->getLeader();
391 Leader = UV;
392 for (; UV; UV = UV->getNext())
393 if (UV->match(Var, Offset))
394 return UV;
395 }
396
Devang Patelf827cd72011-02-04 01:43:25 +0000397 UserValue *UV = new UserValue(Var, Offset, DL, allocator);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000398 userValues.push_back(UV);
399 Leader = UserValue::merge(Leader, UV);
400 return UV;
401}
402
403void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
404 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000405 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000406 Leader = UserValue::merge(Leader, EC);
407}
408
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000409UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000410 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000411 return UV->getLeader();
412 return 0;
413}
414
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000415bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
416 // DBG_VALUE loc, offset, variable
417 if (MI->getNumOperands() != 3 ||
418 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
419 DEBUG(dbgs() << "Can't handle " << *MI);
420 return false;
421 }
422
423 // Get or create the UserValue for (variable,offset).
424 unsigned Offset = MI->getOperand(1).getImm();
425 const MDNode *Var = MI->getOperand(2).getMetadata();
Devang Patelf827cd72011-02-04 01:43:25 +0000426 UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000427 UV->addDef(Idx, MI->getOperand(0));
428 return true;
429}
430
431bool LDVImpl::collectDebugValues(MachineFunction &mf) {
432 bool Changed = false;
433 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
434 ++MFI) {
435 MachineBasicBlock *MBB = MFI;
436 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
437 MBBI != MBBE;) {
438 if (!MBBI->isDebugValue()) {
439 ++MBBI;
440 continue;
441 }
442 // DBG_VALUE has no slot index, use the previous instruction instead.
443 SlotIndex Idx = MBBI == MBB->begin() ?
444 LIS->getMBBStartIdx(MBB) :
445 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
446 // Handle consecutive DBG_VALUE instructions with the same slot index.
447 do {
448 if (handleDebugValue(MBBI, Idx)) {
449 MBBI = MBB->erase(MBBI);
450 Changed = true;
451 } else
452 ++MBBI;
453 } while (MBBI != MBBE && MBBI->isDebugValue());
454 }
455 }
456 return Changed;
457}
458
459void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
460 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000461 SmallVectorImpl<SlotIndex> *Kills,
Devang Patelc722c3d2011-08-10 21:25:34 +0000462 LiveIntervals &LIS, MachineDominatorTree &MDT,
463 LexicalScopes &LS) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000464 SmallVector<SlotIndex, 16> Todo;
465 Todo.push_back(Idx);
Devang Patelc722c3d2011-08-10 21:25:34 +0000466 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
467 LS.getMachineBasicBlocks(dl, LBlocks);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000468 do {
469 SlotIndex Start = Todo.pop_back_val();
470 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
471 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000472 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000473
474 // Limit to VNI's live range.
475 bool ToEnd = true;
476 if (LI && VNI) {
477 LiveRange *Range = LI->getLiveRangeContaining(Start);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000478 if (!Range || Range->valno != VNI) {
479 if (Kills)
480 Kills->push_back(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000481 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000482 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000483 if (Range->end < Stop)
484 Stop = Range->end, ToEnd = false;
485 }
486
487 // There could already be a short def at Start.
488 if (I.valid() && I.start() <= Start) {
489 // Stop when meeting a different location or an already extended interval.
490 Start = Start.getNextSlot();
491 if (I.value() != LocNo || I.stop() != Start)
492 continue;
493 // This is a one-slot placeholder. Just skip it.
494 ++I;
495 }
496
497 // Limited by the next def.
498 if (I.valid() && I.start() < Stop)
499 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000500 // Limited by VNI's live range.
501 else if (!ToEnd && Kills)
502 Kills->push_back(Stop);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000503
504 if (Start >= Stop)
505 continue;
506
507 I.insert(Start, Stop, LocNo);
508
509 // If we extended to the MBB end, propagate down the dominator tree.
510 if (!ToEnd)
511 continue;
512 const std::vector<MachineDomTreeNode*> &Children =
513 MDT.getNode(MBB)->getChildren();
Devang Patelc722c3d2011-08-10 21:25:34 +0000514 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
515 MachineBasicBlock *MBB = Children[i]->getBlock();
516 if (LBlocks.count(MBB) != 0 || LS.dominates(dl, MBB))
517 Todo.push_back(LIS.getMBBStartIdx(MBB));
518 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000519 } while (!Todo.empty());
520}
521
522void
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000523UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
524 const SmallVectorImpl<SlotIndex> &Kills,
525 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
526 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
527 if (Kills.empty())
528 return;
529 // Don't track copies from physregs, there are too many uses.
530 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
531 return;
532
533 // Collect all the (vreg, valno) pairs that are copies of LI.
534 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
535 for (MachineRegisterInfo::use_nodbg_iterator
536 UI = MRI.use_nodbg_begin(LI->reg),
537 UE = MRI.use_nodbg_end(); UI != UE; ++UI) {
538 // Copies of the full value.
539 if (UI.getOperand().getSubReg() || !UI->isCopy())
540 continue;
541 MachineInstr *MI = &*UI;
542 unsigned DstReg = MI->getOperand(0).getReg();
543
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000544 // Don't follow copies to physregs. These are usually setting up call
545 // arguments, and the argument registers are always call clobbered. We are
546 // better off in the source register which could be a callee-saved register,
547 // or it could be spilled.
548 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
549 continue;
550
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000551 // Is LocNo extended to reach this copy? If not, another def may be blocking
552 // it, or we are looking at a wrong value of LI.
553 SlotIndex Idx = LIS.getInstructionIndex(MI);
554 LocMap::iterator I = locInts.find(Idx.getUseIndex());
555 if (!I.valid() || I.value() != LocNo)
556 continue;
557
558 if (!LIS.hasInterval(DstReg))
559 continue;
560 LiveInterval *DstLI = &LIS.getInterval(DstReg);
561 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getDefIndex());
562 assert(DstVNI && DstVNI->def == Idx.getDefIndex() && "Bad copy value");
563 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
564 }
565
566 if (CopyValues.empty())
567 return;
568
569 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
570
571 // Try to add defs of the copied values for each kill point.
572 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
573 SlotIndex Idx = Kills[i];
574 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
575 LiveInterval *DstLI = CopyValues[j].first;
576 const VNInfo *DstVNI = CopyValues[j].second;
577 if (DstLI->getVNInfoAt(Idx) != DstVNI)
578 continue;
579 // Check that there isn't already a def at Idx
580 LocMap::iterator I = locInts.find(Idx);
581 if (I.valid() && I.start() <= Idx)
582 continue;
583 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
584 << DstVNI->id << " in " << *DstLI << '\n');
585 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
586 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
587 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
588 I.insert(Idx, Idx.getNextSlot(), LocNo);
589 NewDefs.push_back(std::make_pair(Idx, LocNo));
590 break;
591 }
592 }
593}
594
595void
596UserValue::computeIntervals(MachineRegisterInfo &MRI,
597 LiveIntervals &LIS,
Devang Patelc722c3d2011-08-10 21:25:34 +0000598 MachineDominatorTree &MDT,
599 LexicalScopes &LS) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000600 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
601
602 // Collect all defs to be extended (Skipping undefs).
603 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
604 if (I.value() != ~0u)
605 Defs.push_back(std::make_pair(I.start(), I.value()));
606
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000607 // Extend all defs, and possibly add new ones along the way.
608 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000609 SlotIndex Idx = Defs[i].first;
610 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000611 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000612
613 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000614 if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) {
615 LiveInterval *LI = &LIS.getInterval(Loc.getReg());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000616 const VNInfo *VNI = LI->getVNInfoAt(Idx);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000617 SmallVector<SlotIndex, 16> Kills;
Devang Patelc722c3d2011-08-10 21:25:34 +0000618 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, LS);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000619 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000620 } else
Devang Patelc722c3d2011-08-10 21:25:34 +0000621 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT, LS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000622 }
623
624 // Finally, erase all the undefs.
625 for (LocMap::iterator I = locInts.begin(); I.valid();)
626 if (I.value() == ~0u)
627 I.erase();
628 else
629 ++I;
630}
631
632void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000633 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Devang Patelc722c3d2011-08-10 21:25:34 +0000634 userValues[i]->computeIntervals(MF->getRegInfo(), *LIS, *MDT, LS);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000635 userValues[i]->mapVirtRegs(this);
636 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000637}
638
639bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
640 MF = &mf;
641 LIS = &pass.getAnalysis<LiveIntervals>();
642 MDT = &pass.getAnalysis<MachineDominatorTree>();
643 TRI = mf.getTarget().getRegisterInfo();
644 clear();
Devang Patelc722c3d2011-08-10 21:25:34 +0000645 LS.initialize(mf);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000646 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
647 << ((Value*)mf.getFunction())->getName()
648 << " **********\n");
649
650 bool Changed = collectDebugValues(mf);
651 computeIntervals();
652 DEBUG(print(dbgs()));
Devang Patelc722c3d2011-08-10 21:25:34 +0000653 LS.releaseMemory();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000654 return Changed;
655}
656
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000657bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000658 if (!EnableLDV)
659 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000660 if (!pImpl)
661 pImpl = new LDVImpl(this);
662 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
663}
664
665void LiveDebugVariables::releaseMemory() {
666 if (pImpl)
667 static_cast<LDVImpl*>(pImpl)->clear();
668}
669
670LiveDebugVariables::~LiveDebugVariables() {
671 if (pImpl)
672 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000673}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000674
675void UserValue::
676renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
677 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000678 for (unsigned i = locations.size(); i; --i) {
679 unsigned LocNo = i - 1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000680 MachineOperand &Loc = locations[LocNo];
681 if (!Loc.isReg() || Loc.getReg() != OldReg)
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000682 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000683 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
684 Loc.substPhysReg(NewReg, *TRI);
685 else
686 Loc.substVirtReg(NewReg, SubIdx, *TRI);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000687 coalesceLocation(LocNo);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000688 }
689}
690
691void LDVImpl::
692renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000693 UserValue *UV = lookupVirtReg(OldReg);
694 if (!UV)
695 return;
696
697 if (TargetRegisterInfo::isVirtualRegister(NewReg))
698 mapVirtReg(NewReg, UV);
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000699 virtRegToEqClass.erase(OldReg);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000700
701 do {
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000702 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000703 UV = UV->getNext();
704 } while (UV);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000705}
706
707void LiveDebugVariables::
708renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
709 if (pImpl)
710 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
711}
712
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000713//===----------------------------------------------------------------------===//
714// Live Range Splitting
715//===----------------------------------------------------------------------===//
716
717bool
718UserValue::splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs) {
719 DEBUG({
720 dbgs() << "Splitting Loc" << OldLocNo << '\t';
721 print(dbgs(), 0);
722 });
723 bool DidChange = false;
724 LocMap::iterator LocMapI;
725 LocMapI.setMap(locInts);
726 for (unsigned i = 0; i != NewRegs.size(); ++i) {
727 LiveInterval *LI = NewRegs[i];
728 if (LI->empty())
729 continue;
730
731 // Don't allocate the new LocNo until it is needed.
732 unsigned NewLocNo = ~0u;
733
734 // Iterate over the overlaps between locInts and LI.
735 LocMapI.find(LI->beginIndex());
736 if (!LocMapI.valid())
737 continue;
738 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
739 LiveInterval::iterator LIE = LI->end();
740 while (LocMapI.valid() && LII != LIE) {
741 // At this point, we know that LocMapI.stop() > LII->start.
742 LII = LI->advanceTo(LII, LocMapI.start());
743 if (LII == LIE)
744 break;
745
746 // Now LII->end > LocMapI.start(). Do we have an overlap?
747 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
748 // Overlapping correct location. Allocate NewLocNo now.
749 if (NewLocNo == ~0u) {
750 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
751 MO.setSubReg(locations[OldLocNo].getSubReg());
752 NewLocNo = getLocationNo(MO);
753 DidChange = true;
754 }
755
756 SlotIndex LStart = LocMapI.start();
757 SlotIndex LStop = LocMapI.stop();
758
759 // Trim LocMapI down to the LII overlap.
760 if (LStart < LII->start)
761 LocMapI.setStartUnchecked(LII->start);
762 if (LStop > LII->end)
763 LocMapI.setStopUnchecked(LII->end);
764
765 // Change the value in the overlap. This may trigger coalescing.
766 LocMapI.setValue(NewLocNo);
767
768 // Re-insert any removed OldLocNo ranges.
769 if (LStart < LocMapI.start()) {
770 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
771 ++LocMapI;
772 assert(LocMapI.valid() && "Unexpected coalescing");
773 }
774 if (LStop > LocMapI.stop()) {
775 ++LocMapI;
776 LocMapI.insert(LII->end, LStop, OldLocNo);
777 --LocMapI;
778 }
779 }
780
781 // Advance to the next overlap.
782 if (LII->end < LocMapI.stop()) {
783 if (++LII == LIE)
784 break;
785 LocMapI.advanceTo(LII->start);
786 } else {
787 ++LocMapI;
788 if (!LocMapI.valid())
789 break;
790 LII = LI->advanceTo(LII, LocMapI.start());
791 }
792 }
793 }
794
795 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
796 locations.erase(locations.begin() + OldLocNo);
797 LocMapI.goToBegin();
798 while (LocMapI.valid()) {
799 unsigned v = LocMapI.value();
800 if (v == OldLocNo) {
801 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
802 << LocMapI.stop() << ")\n");
803 LocMapI.erase();
804 } else {
805 if (v > OldLocNo)
806 LocMapI.setValueUnchecked(v-1);
807 ++LocMapI;
808 }
809 }
810
811 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);});
812 return DidChange;
813}
814
815bool
816UserValue::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
817 bool DidChange = false;
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000818 // Split locations referring to OldReg. Iterate backwards so splitLocation can
819 // safely erase unuused locations.
820 for (unsigned i = locations.size(); i ; --i) {
821 unsigned LocNo = i-1;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000822 const MachineOperand *Loc = &locations[LocNo];
823 if (!Loc->isReg() || Loc->getReg() != OldReg)
824 continue;
825 DidChange |= splitLocation(LocNo, NewRegs);
826 }
827 return DidChange;
828}
829
830void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
831 bool DidChange = false;
832 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
833 DidChange |= UV->splitRegister(OldReg, NewRegs);
834
835 if (!DidChange)
836 return;
837
838 // Map all of the new virtual registers.
839 UserValue *UV = lookupVirtReg(OldReg);
840 for (unsigned i = 0; i != NewRegs.size(); ++i)
841 mapVirtReg(NewRegs[i]->reg, UV);
842}
843
844void LiveDebugVariables::
845splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
846 if (pImpl)
847 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
848}
849
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000850void
851UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
852 // Iterate over locations in reverse makes it easier to handle coalescing.
853 for (unsigned i = locations.size(); i ; --i) {
854 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000855 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000856 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000857 if (!Loc.isReg() || !Loc.getReg() ||
858 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000859 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000860 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000861 if (VRM.isAssignedReg(VirtReg) &&
862 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000863 // This can create a %noreg operand in rare cases when the sub-register
864 // index is no longer available. That means the user value is in a
865 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000866 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesenf0704d22011-01-12 23:14:07 +0000867 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT &&
868 VRM.isSpillSlotUsed(VRM.getStackSlot(VirtReg))) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000869 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000870 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000871 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000872 Loc.setReg(0);
873 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000874 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000875 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000876 }
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000877}
878
Devang Patelf827cd72011-02-04 01:43:25 +0000879/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000880/// instruction.
881static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000882findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000883 LiveIntervals &LIS) {
884 SlotIndex Start = LIS.getMBBStartIdx(MBB);
885 Idx = Idx.getBaseIndex();
886
887 // Try to find an insert location by going backwards from Idx.
888 MachineInstr *MI;
889 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
890 // We've reached the beginning of MBB.
891 if (Idx == Start) {
892 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000893 return I;
894 }
895 Idx = Idx.getPrevIndex();
896 }
Devang Patelf827cd72011-02-04 01:43:25 +0000897
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000898 // Don't insert anything after the first terminator, though.
899 return MI->getDesc().isTerminator() ? MBB->getFirstTerminator() :
900 llvm::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000901}
902
Devang Patelf827cd72011-02-04 01:43:25 +0000903DebugLoc UserValue::findDebugLoc() {
904 DebugLoc D = dl;
905 dl = DebugLoc();
906 return D;
907}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000908void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
909 unsigned LocNo,
910 LiveIntervals &LIS,
911 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000912 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000913 MachineOperand &Loc = locations[LocNo];
Devang Pateld9f3fc72011-08-04 20:42:11 +0000914 ++NumInsertedDebugValues;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000915
916 // Frame index locations may require a target callback.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000917 if (Loc.isFI()) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000918 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
Devang Patelf827cd72011-02-04 01:43:25 +0000919 Loc.getIndex(), offset, variable,
920 findDebugLoc());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000921 if (MI) {
922 MBB->insert(I, MI);
923 return;
924 }
925 }
926 // This is not a frame index, or the target is happy with a standard FI.
Devang Patelf827cd72011-02-04 01:43:25 +0000927 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000928 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000929}
930
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000931void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
932 const TargetInstrInfo &TII) {
933 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
934
935 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
936 SlotIndex Start = I.start();
937 SlotIndex Stop = I.stop();
938 unsigned LocNo = I.value();
939 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
940 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
941 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
942
943 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
944 insertDebugValue(MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000945 // This interval may span multiple basic blocks.
946 // Insert a DBG_VALUE into each one.
947 while(Stop > MBBEnd) {
948 // Move to the next block.
949 Start = MBBEnd;
950 if (++MBB == MFEnd)
951 break;
952 MBBEnd = LIS.getMBBEndIdx(MBB);
953 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
954 insertDebugValue(MBB, Start, LocNo, LIS, TII);
955 }
956 DEBUG(dbgs() << '\n');
957 if (MBB == MFEnd)
958 break;
959
960 ++I;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000961 }
962}
963
964void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
965 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
966 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
967 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000968 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000969 userValues[i]->rewriteLocations(*VRM, *TRI);
970 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
971 }
972}
973
974void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
975 if (pImpl)
976 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
977}
978
979
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000980#ifndef NDEBUG
981void LiveDebugVariables::dump() {
982 if (pImpl)
983 static_cast<LDVImpl*>(pImpl)->print(dbgs());
984}
985#endif
986