blob: eb9bbacb7b35054d3b76e574dcec04d0791a8550 [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 Olesenf42b6612011-05-06 18:00:02 +0000104 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
105 /// is live. Returns true if any changes were made.
106 bool splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
107
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000108public:
109 /// UserValue - Create a new UserValue.
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000110 UserValue(const MDNode *var, unsigned o, DebugLoc L,
Devang Patelf827cd72011-02-04 01:43:25 +0000111 LocMap::Allocator &alloc)
112 : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000113 {}
114
115 /// getLeader - Get the leader of this value's equivalence class.
116 UserValue *getLeader() {
117 UserValue *l = leader;
118 while (l != l->leader)
119 l = l->leader;
120 return leader = l;
121 }
122
123 /// getNext - Return the next UserValue in the equivalence class.
124 UserValue *getNext() const { return next; }
125
Devang Patela462d6e2011-07-06 23:09:51 +0000126 /// match - Does this UserValue match the parameters?
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000127 bool match(const MDNode *Var, unsigned Offset) const {
128 return Var == variable && Offset == offset;
129 }
130
131 /// merge - Merge equivalence classes.
132 static UserValue *merge(UserValue *L1, UserValue *L2) {
133 L2 = L2->getLeader();
134 if (!L1)
135 return L2;
136 L1 = L1->getLeader();
137 if (L1 == L2)
138 return L1;
139 // Splice L2 before L1's members.
140 UserValue *End = L2;
141 while (End->next)
142 End->leader = L1, End = End->next;
143 End->leader = L1;
144 End->next = L1->next;
145 L1->next = L2;
146 return L1;
147 }
148
149 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000150 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000151 if (LocMO.isReg()) {
152 if (LocMO.getReg() == 0)
153 return ~0u;
154 // For register locations we dont care about use/def and other flags.
155 for (unsigned i = 0, e = locations.size(); i != e; ++i)
156 if (locations[i].isReg() &&
157 locations[i].getReg() == LocMO.getReg() &&
158 locations[i].getSubReg() == LocMO.getSubReg())
159 return i;
160 } else
161 for (unsigned i = 0, e = locations.size(); i != e; ++i)
162 if (LocMO.isIdenticalTo(locations[i]))
163 return i;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000164 locations.push_back(LocMO);
165 // We are storing a MachineOperand outside a MachineInstr.
166 locations.back().clearParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000167 // Don't store def operands.
168 if (locations.back().isReg())
169 locations.back().setIsUse();
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000170 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000171 }
172
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000173 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
174 void mapVirtRegs(LDVImpl *LDV);
175
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000176 /// addDef - Add a definition point to this value.
177 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
178 // Add a singular (Idx,Idx) -> Loc mapping.
179 LocMap::iterator I = locInts.find(Idx);
180 if (!I.valid() || I.start() != Idx)
181 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen79513ed2011-08-03 23:44:31 +0000182 else
183 // A later DBG_VALUE at the same SlotIndex overrides the old location.
184 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000185 }
186
187 /// extendDef - Extend the current definition as far as possible down the
188 /// dominator tree. Stop when meeting an existing def or when leaving the live
189 /// range of VNI.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000190 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000191 /// @param Idx Starting point for the definition.
192 /// @param LocNo Location number to propagate.
193 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
194 /// @param VNI When LI is not null, this is the value to restrict to.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000195 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000196 /// @param LIS Live intervals analysis.
197 /// @param MDT Dominator tree.
198 void extendDef(SlotIndex Idx, unsigned LocNo,
199 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000200 SmallVectorImpl<SlotIndex> *Kills,
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000201 LiveIntervals &LIS, MachineDominatorTree &MDT);
202
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000203 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
204 /// registers. Determine if any of the copies are available at the kill
205 /// points, and add defs if possible.
206 /// @param LI Scan for copies of the value in LI->reg.
207 /// @param LocNo Location number of LI->reg.
208 /// @param Kills Points where the range of LocNo could be extended.
209 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
210 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
211 const SmallVectorImpl<SlotIndex> &Kills,
212 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
213 MachineRegisterInfo &MRI,
214 LiveIntervals &LIS);
215
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000216 /// computeIntervals - Compute the live intervals of all locations after
217 /// collecting all their def points.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000218 void computeIntervals(MachineRegisterInfo &MRI,
219 LiveIntervals &LIS, MachineDominatorTree &MDT);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000220
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000221 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
222 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
223 const TargetRegisterInfo *TRI);
224
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000225 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
226 /// live. Returns true if any changes were made.
227 bool splitRegister(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
228
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000229 /// rewriteLocations - Rewrite virtual register locations according to the
230 /// provided virtual register map.
231 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
232
233 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
234 void emitDebugValues(VirtRegMap *VRM,
235 LiveIntervals &LIS, const TargetInstrInfo &TRI);
236
Devang Patelf827cd72011-02-04 01:43:25 +0000237 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
238 /// variable may have more than one corresponding DBG_VALUE instructions.
239 /// Only first one needs DebugLoc to identify variable's lexical scope
240 /// in source file.
241 DebugLoc findDebugLoc();
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000242 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000243};
244} // namespace
245
246/// LDVImpl - Implementation of the LiveDebugVariables pass.
247namespace {
248class LDVImpl {
249 LiveDebugVariables &pass;
250 LocMap::Allocator allocator;
251 MachineFunction *MF;
252 LiveIntervals *LIS;
253 MachineDominatorTree *MDT;
254 const TargetRegisterInfo *TRI;
255
256 /// userValues - All allocated UserValue instances.
257 SmallVector<UserValue*, 8> userValues;
258
259 /// Map virtual register to eq class leader.
260 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000261 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000262
263 /// Map user variable to eq class leader.
264 typedef DenseMap<const MDNode *, UserValue*> UVMap;
265 UVMap userVarMap;
266
267 /// getUserValue - Find or create a UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000268 UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000269
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000270 /// lookupVirtReg - Find the EC leader for VirtReg or null.
271 UserValue *lookupVirtReg(unsigned VirtReg);
272
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000273 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
274 /// @param MI DBG_VALUE instruction
275 /// @param Idx Last valid SLotIndex before instruction.
276 /// @return True if the DBG_VALUE instruction should be deleted.
277 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
278
279 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
280 /// a UserValue def for each instruction.
281 /// @param mf MachineFunction to be scanned.
282 /// @return True if any debug values were found.
283 bool collectDebugValues(MachineFunction &mf);
284
285 /// computeIntervals - Compute the live intervals of all user values after
286 /// collecting all their def points.
287 void computeIntervals();
288
289public:
290 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
291 bool runOnMachineFunction(MachineFunction &mf);
292
293 /// clear - Relase all memory.
294 void clear() {
295 DeleteContainerPointers(userValues);
296 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000297 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000298 userVarMap.clear();
299 }
300
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000301 /// mapVirtReg - Map virtual register to an equivalence class.
302 void mapVirtReg(unsigned VirtReg, UserValue *EC);
303
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000304 /// renameRegister - Replace all references to OldReg with NewReg:SubIdx.
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000305 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
306
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000307 /// splitRegister - Replace all references to OldReg with NewRegs.
308 void splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs);
309
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000310 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
311 void emitDebugValues(VirtRegMap *VRM);
312
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000313 void print(raw_ostream&);
314};
315} // namespace
316
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000317void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000318 if (const MDString *MDS = dyn_cast<MDString>(variable->getOperand(2)))
319 OS << "!\"" << MDS->getString() << "\"\t";
320 if (offset)
321 OS << '+' << offset;
322 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
323 OS << " [" << I.start() << ';' << I.stop() << "):";
324 if (I.value() == ~0u)
325 OS << "undef";
326 else
327 OS << I.value();
328 }
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000329 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
330 OS << " Loc" << i << '=';
331 locations[i].print(OS, TM);
332 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000333 OS << '\n';
334}
335
336void LDVImpl::print(raw_ostream &OS) {
337 OS << "********** DEBUG VARIABLES **********\n";
338 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000339 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000340}
341
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000342void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000343 unsigned KeepLoc = 0;
344 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
345 if (KeepLoc == LocNo)
346 continue;
347 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
348 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000349 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000350 // No matches.
351 if (KeepLoc == locations.size())
352 return;
353
354 // Keep the smaller location, erase the larger one.
355 unsigned EraseLoc = LocNo;
356 if (KeepLoc > EraseLoc)
357 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000358 locations.erase(locations.begin() + EraseLoc);
359
360 // Rewrite values.
361 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
362 unsigned v = I.value();
363 if (v == EraseLoc)
364 I.setValue(KeepLoc); // Coalesce when possible.
365 else if (v > EraseLoc)
366 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
367 }
368}
369
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000370void UserValue::mapVirtRegs(LDVImpl *LDV) {
371 for (unsigned i = 0, e = locations.size(); i != e; ++i)
372 if (locations[i].isReg() &&
373 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
374 LDV->mapVirtReg(locations[i].getReg(), this);
375}
376
Devang Patelf827cd72011-02-04 01:43:25 +0000377UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
378 DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000379 UserValue *&Leader = userVarMap[Var];
380 if (Leader) {
381 UserValue *UV = Leader->getLeader();
382 Leader = UV;
383 for (; UV; UV = UV->getNext())
384 if (UV->match(Var, Offset))
385 return UV;
386 }
387
Devang Patelf827cd72011-02-04 01:43:25 +0000388 UserValue *UV = new UserValue(Var, Offset, DL, allocator);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000389 userValues.push_back(UV);
390 Leader = UserValue::merge(Leader, UV);
391 return UV;
392}
393
394void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
395 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000396 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000397 Leader = UserValue::merge(Leader, EC);
398}
399
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000400UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000401 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000402 return UV->getLeader();
403 return 0;
404}
405
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000406bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
407 // DBG_VALUE loc, offset, variable
408 if (MI->getNumOperands() != 3 ||
409 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
410 DEBUG(dbgs() << "Can't handle " << *MI);
411 return false;
412 }
413
414 // Get or create the UserValue for (variable,offset).
415 unsigned Offset = MI->getOperand(1).getImm();
416 const MDNode *Var = MI->getOperand(2).getMetadata();
Devang Patelf827cd72011-02-04 01:43:25 +0000417 UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000418 UV->addDef(Idx, MI->getOperand(0));
419 return true;
420}
421
422bool LDVImpl::collectDebugValues(MachineFunction &mf) {
423 bool Changed = false;
424 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
425 ++MFI) {
426 MachineBasicBlock *MBB = MFI;
427 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
428 MBBI != MBBE;) {
429 if (!MBBI->isDebugValue()) {
430 ++MBBI;
431 continue;
432 }
433 // DBG_VALUE has no slot index, use the previous instruction instead.
434 SlotIndex Idx = MBBI == MBB->begin() ?
435 LIS->getMBBStartIdx(MBB) :
436 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
437 // Handle consecutive DBG_VALUE instructions with the same slot index.
438 do {
439 if (handleDebugValue(MBBI, Idx)) {
440 MBBI = MBB->erase(MBBI);
441 Changed = true;
442 } else
443 ++MBBI;
444 } while (MBBI != MBBE && MBBI->isDebugValue());
445 }
446 }
447 return Changed;
448}
449
450void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
451 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000452 SmallVectorImpl<SlotIndex> *Kills,
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000453 LiveIntervals &LIS, MachineDominatorTree &MDT) {
454 SmallVector<SlotIndex, 16> Todo;
455 Todo.push_back(Idx);
456
457 do {
458 SlotIndex Start = Todo.pop_back_val();
459 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
460 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000461 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000462
463 // Limit to VNI's live range.
464 bool ToEnd = true;
465 if (LI && VNI) {
466 LiveRange *Range = LI->getLiveRangeContaining(Start);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000467 if (!Range || Range->valno != VNI) {
468 if (Kills)
469 Kills->push_back(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000470 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000471 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000472 if (Range->end < Stop)
473 Stop = Range->end, ToEnd = false;
474 }
475
476 // There could already be a short def at Start.
477 if (I.valid() && I.start() <= Start) {
478 // Stop when meeting a different location or an already extended interval.
479 Start = Start.getNextSlot();
480 if (I.value() != LocNo || I.stop() != Start)
481 continue;
482 // This is a one-slot placeholder. Just skip it.
483 ++I;
484 }
485
486 // Limited by the next def.
487 if (I.valid() && I.start() < Stop)
488 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000489 // Limited by VNI's live range.
490 else if (!ToEnd && Kills)
491 Kills->push_back(Stop);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000492
493 if (Start >= Stop)
494 continue;
495
496 I.insert(Start, Stop, LocNo);
497
498 // If we extended to the MBB end, propagate down the dominator tree.
499 if (!ToEnd)
500 continue;
501 const std::vector<MachineDomTreeNode*> &Children =
502 MDT.getNode(MBB)->getChildren();
503 for (unsigned i = 0, e = Children.size(); i != e; ++i)
504 Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock()));
505 } while (!Todo.empty());
506}
507
508void
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000509UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
510 const SmallVectorImpl<SlotIndex> &Kills,
511 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
512 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
513 if (Kills.empty())
514 return;
515 // Don't track copies from physregs, there are too many uses.
516 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
517 return;
518
519 // Collect all the (vreg, valno) pairs that are copies of LI.
520 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
521 for (MachineRegisterInfo::use_nodbg_iterator
522 UI = MRI.use_nodbg_begin(LI->reg),
523 UE = MRI.use_nodbg_end(); UI != UE; ++UI) {
524 // Copies of the full value.
525 if (UI.getOperand().getSubReg() || !UI->isCopy())
526 continue;
527 MachineInstr *MI = &*UI;
528 unsigned DstReg = MI->getOperand(0).getReg();
529
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000530 // Don't follow copies to physregs. These are usually setting up call
531 // arguments, and the argument registers are always call clobbered. We are
532 // better off in the source register which could be a callee-saved register,
533 // or it could be spilled.
534 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
535 continue;
536
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000537 // Is LocNo extended to reach this copy? If not, another def may be blocking
538 // it, or we are looking at a wrong value of LI.
539 SlotIndex Idx = LIS.getInstructionIndex(MI);
540 LocMap::iterator I = locInts.find(Idx.getUseIndex());
541 if (!I.valid() || I.value() != LocNo)
542 continue;
543
544 if (!LIS.hasInterval(DstReg))
545 continue;
546 LiveInterval *DstLI = &LIS.getInterval(DstReg);
547 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getDefIndex());
548 assert(DstVNI && DstVNI->def == Idx.getDefIndex() && "Bad copy value");
549 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
550 }
551
552 if (CopyValues.empty())
553 return;
554
555 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
556
557 // Try to add defs of the copied values for each kill point.
558 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
559 SlotIndex Idx = Kills[i];
560 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
561 LiveInterval *DstLI = CopyValues[j].first;
562 const VNInfo *DstVNI = CopyValues[j].second;
563 if (DstLI->getVNInfoAt(Idx) != DstVNI)
564 continue;
565 // Check that there isn't already a def at Idx
566 LocMap::iterator I = locInts.find(Idx);
567 if (I.valid() && I.start() <= Idx)
568 continue;
569 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
570 << DstVNI->id << " in " << *DstLI << '\n');
571 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
572 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
573 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
574 I.insert(Idx, Idx.getNextSlot(), LocNo);
575 NewDefs.push_back(std::make_pair(Idx, LocNo));
576 break;
577 }
578 }
579}
580
581void
582UserValue::computeIntervals(MachineRegisterInfo &MRI,
583 LiveIntervals &LIS,
584 MachineDominatorTree &MDT) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000585 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
586
587 // Collect all defs to be extended (Skipping undefs).
588 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
589 if (I.value() != ~0u)
590 Defs.push_back(std::make_pair(I.start(), I.value()));
591
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000592 // Extend all defs, and possibly add new ones along the way.
593 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000594 SlotIndex Idx = Defs[i].first;
595 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000596 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000597
598 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000599 if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) {
600 LiveInterval *LI = &LIS.getInterval(Loc.getReg());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000601 const VNInfo *VNI = LI->getVNInfoAt(Idx);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000602 SmallVector<SlotIndex, 16> Kills;
603 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT);
604 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000605 } else
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000606 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000607 }
608
609 // Finally, erase all the undefs.
610 for (LocMap::iterator I = locInts.begin(); I.valid();)
611 if (I.value() == ~0u)
612 I.erase();
613 else
614 ++I;
615}
616
617void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000618 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
619 userValues[i]->computeIntervals(MF->getRegInfo(), *LIS, *MDT);
620 userValues[i]->mapVirtRegs(this);
621 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000622}
623
624bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
625 MF = &mf;
626 LIS = &pass.getAnalysis<LiveIntervals>();
627 MDT = &pass.getAnalysis<MachineDominatorTree>();
628 TRI = mf.getTarget().getRegisterInfo();
629 clear();
630 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
631 << ((Value*)mf.getFunction())->getName()
632 << " **********\n");
633
634 bool Changed = collectDebugValues(mf);
635 computeIntervals();
636 DEBUG(print(dbgs()));
637 return Changed;
638}
639
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000640bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000641 if (!EnableLDV)
642 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000643 if (!pImpl)
644 pImpl = new LDVImpl(this);
645 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
646}
647
648void LiveDebugVariables::releaseMemory() {
649 if (pImpl)
650 static_cast<LDVImpl*>(pImpl)->clear();
651}
652
653LiveDebugVariables::~LiveDebugVariables() {
654 if (pImpl)
655 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000656}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000657
658void UserValue::
659renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
660 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000661 for (unsigned i = locations.size(); i; --i) {
662 unsigned LocNo = i - 1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000663 MachineOperand &Loc = locations[LocNo];
664 if (!Loc.isReg() || Loc.getReg() != OldReg)
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000665 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000666 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
667 Loc.substPhysReg(NewReg, *TRI);
668 else
669 Loc.substVirtReg(NewReg, SubIdx, *TRI);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000670 coalesceLocation(LocNo);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000671 }
672}
673
674void LDVImpl::
675renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000676 UserValue *UV = lookupVirtReg(OldReg);
677 if (!UV)
678 return;
679
680 if (TargetRegisterInfo::isVirtualRegister(NewReg))
681 mapVirtReg(NewReg, UV);
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000682 virtRegToEqClass.erase(OldReg);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000683
684 do {
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000685 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000686 UV = UV->getNext();
687 } while (UV);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000688}
689
690void LiveDebugVariables::
691renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
692 if (pImpl)
693 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
694}
695
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000696//===----------------------------------------------------------------------===//
697// Live Range Splitting
698//===----------------------------------------------------------------------===//
699
700bool
701UserValue::splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs) {
702 DEBUG({
703 dbgs() << "Splitting Loc" << OldLocNo << '\t';
704 print(dbgs(), 0);
705 });
706 bool DidChange = false;
707 LocMap::iterator LocMapI;
708 LocMapI.setMap(locInts);
709 for (unsigned i = 0; i != NewRegs.size(); ++i) {
710 LiveInterval *LI = NewRegs[i];
711 if (LI->empty())
712 continue;
713
714 // Don't allocate the new LocNo until it is needed.
715 unsigned NewLocNo = ~0u;
716
717 // Iterate over the overlaps between locInts and LI.
718 LocMapI.find(LI->beginIndex());
719 if (!LocMapI.valid())
720 continue;
721 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
722 LiveInterval::iterator LIE = LI->end();
723 while (LocMapI.valid() && LII != LIE) {
724 // At this point, we know that LocMapI.stop() > LII->start.
725 LII = LI->advanceTo(LII, LocMapI.start());
726 if (LII == LIE)
727 break;
728
729 // Now LII->end > LocMapI.start(). Do we have an overlap?
730 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
731 // Overlapping correct location. Allocate NewLocNo now.
732 if (NewLocNo == ~0u) {
733 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
734 MO.setSubReg(locations[OldLocNo].getSubReg());
735 NewLocNo = getLocationNo(MO);
736 DidChange = true;
737 }
738
739 SlotIndex LStart = LocMapI.start();
740 SlotIndex LStop = LocMapI.stop();
741
742 // Trim LocMapI down to the LII overlap.
743 if (LStart < LII->start)
744 LocMapI.setStartUnchecked(LII->start);
745 if (LStop > LII->end)
746 LocMapI.setStopUnchecked(LII->end);
747
748 // Change the value in the overlap. This may trigger coalescing.
749 LocMapI.setValue(NewLocNo);
750
751 // Re-insert any removed OldLocNo ranges.
752 if (LStart < LocMapI.start()) {
753 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
754 ++LocMapI;
755 assert(LocMapI.valid() && "Unexpected coalescing");
756 }
757 if (LStop > LocMapI.stop()) {
758 ++LocMapI;
759 LocMapI.insert(LII->end, LStop, OldLocNo);
760 --LocMapI;
761 }
762 }
763
764 // Advance to the next overlap.
765 if (LII->end < LocMapI.stop()) {
766 if (++LII == LIE)
767 break;
768 LocMapI.advanceTo(LII->start);
769 } else {
770 ++LocMapI;
771 if (!LocMapI.valid())
772 break;
773 LII = LI->advanceTo(LII, LocMapI.start());
774 }
775 }
776 }
777
778 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
779 locations.erase(locations.begin() + OldLocNo);
780 LocMapI.goToBegin();
781 while (LocMapI.valid()) {
782 unsigned v = LocMapI.value();
783 if (v == OldLocNo) {
784 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
785 << LocMapI.stop() << ")\n");
786 LocMapI.erase();
787 } else {
788 if (v > OldLocNo)
789 LocMapI.setValueUnchecked(v-1);
790 ++LocMapI;
791 }
792 }
793
794 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);});
795 return DidChange;
796}
797
798bool
799UserValue::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
800 bool DidChange = false;
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000801 // Split locations referring to OldReg. Iterate backwards so splitLocation can
802 // safely erase unuused locations.
803 for (unsigned i = locations.size(); i ; --i) {
804 unsigned LocNo = i-1;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000805 const MachineOperand *Loc = &locations[LocNo];
806 if (!Loc->isReg() || Loc->getReg() != OldReg)
807 continue;
808 DidChange |= splitLocation(LocNo, NewRegs);
809 }
810 return DidChange;
811}
812
813void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
814 bool DidChange = false;
815 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
816 DidChange |= UV->splitRegister(OldReg, NewRegs);
817
818 if (!DidChange)
819 return;
820
821 // Map all of the new virtual registers.
822 UserValue *UV = lookupVirtReg(OldReg);
823 for (unsigned i = 0; i != NewRegs.size(); ++i)
824 mapVirtReg(NewRegs[i]->reg, UV);
825}
826
827void LiveDebugVariables::
828splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
829 if (pImpl)
830 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
831}
832
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000833void
834UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
835 // Iterate over locations in reverse makes it easier to handle coalescing.
836 for (unsigned i = locations.size(); i ; --i) {
837 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000838 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000839 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000840 if (!Loc.isReg() || !Loc.getReg() ||
841 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000842 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000843 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000844 if (VRM.isAssignedReg(VirtReg) &&
845 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000846 // This can create a %noreg operand in rare cases when the sub-register
847 // index is no longer available. That means the user value is in a
848 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000849 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesenf0704d22011-01-12 23:14:07 +0000850 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT &&
851 VRM.isSpillSlotUsed(VRM.getStackSlot(VirtReg))) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000852 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000853 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000854 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000855 Loc.setReg(0);
856 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000857 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000858 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000859 }
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000860}
861
Devang Patelf827cd72011-02-04 01:43:25 +0000862/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000863/// instruction.
864static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000865findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000866 LiveIntervals &LIS) {
867 SlotIndex Start = LIS.getMBBStartIdx(MBB);
868 Idx = Idx.getBaseIndex();
869
870 // Try to find an insert location by going backwards from Idx.
871 MachineInstr *MI;
872 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
873 // We've reached the beginning of MBB.
874 if (Idx == Start) {
875 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000876 return I;
877 }
878 Idx = Idx.getPrevIndex();
879 }
Devang Patelf827cd72011-02-04 01:43:25 +0000880
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000881 // Don't insert anything after the first terminator, though.
882 return MI->getDesc().isTerminator() ? MBB->getFirstTerminator() :
883 llvm::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000884}
885
Devang Patelf827cd72011-02-04 01:43:25 +0000886DebugLoc UserValue::findDebugLoc() {
887 DebugLoc D = dl;
888 dl = DebugLoc();
889 return D;
890}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000891void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
892 unsigned LocNo,
893 LiveIntervals &LIS,
894 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000895 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000896 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000897
898 // Frame index locations may require a target callback.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000899 if (Loc.isFI()) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000900 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
Devang Patelf827cd72011-02-04 01:43:25 +0000901 Loc.getIndex(), offset, variable,
902 findDebugLoc());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000903 if (MI) {
904 MBB->insert(I, MI);
905 return;
906 }
907 }
908 // This is not a frame index, or the target is happy with a standard FI.
Devang Patelf827cd72011-02-04 01:43:25 +0000909 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000910 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000911}
912
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000913void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
914 const TargetInstrInfo &TII) {
915 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
916
917 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
918 SlotIndex Start = I.start();
919 SlotIndex Stop = I.stop();
920 unsigned LocNo = I.value();
921 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
922 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
923 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
924
925 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
926 insertDebugValue(MBB, Start, LocNo, LIS, TII);
927
928 // This interval may span multiple basic blocks.
929 // Insert a DBG_VALUE into each one.
930 while(Stop > MBBEnd) {
931 // Move to the next block.
932 Start = MBBEnd;
933 if (++MBB == MFEnd)
934 break;
935 MBBEnd = LIS.getMBBEndIdx(MBB);
936 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
937 insertDebugValue(MBB, Start, LocNo, LIS, TII);
938 }
939 DEBUG(dbgs() << '\n');
940 if (MBB == MFEnd)
941 break;
942
943 ++I;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000944 }
945}
946
947void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
948 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
949 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
950 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000951 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000952 userValues[i]->rewriteLocations(*VRM, *TRI);
953 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
954 }
955}
956
957void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
958 if (pImpl)
959 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
960}
961
962
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000963#ifndef NDEBUG
964void LiveDebugVariables::dump() {
965 if (pImpl)
966 static_cast<LDVImpl*>(pImpl)->print(dbgs());
967}
968#endif
969