blob: 89813e5a11765519ce48e2827fc3c058119521e5 [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"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000031#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000032#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000036#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000037#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000039#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000040#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000041#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000042
43using namespace llvm;
44
Devang Patel51a666f2011-01-07 22:33:41 +000045static cl::opt<bool>
Jakob Stoklund Olesen25dc2262011-01-12 23:36:21 +000046EnableLDV("live-debug-variables", cl::init(true),
Devang Patel51a666f2011-01-07 22:33:41 +000047 cl::desc("Enable the live debug variables pass"), cl::Hidden);
48
Devang Patelad90d3a2011-08-04 18:45:38 +000049STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000050char LiveDebugVariables::ID = 0;
51
52INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
53 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000054INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000055INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
56INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
57 "Debug Variable Analysis", false, false)
58
59void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000060 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000061 AU.addRequiredTransitive<LiveIntervals>();
62 AU.setPreservesAll();
63 MachineFunctionPass::getAnalysisUsage(AU);
64}
65
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000066LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(0) {
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000067 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
68}
69
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000070/// LocMap - Map of where a user value is live, and its location.
71typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
72
73/// UserValue - A user value is a part of a debug info user variable.
74///
75/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
76/// holds part of a user variable. The part is identified by a byte offset.
77///
78/// UserValues are grouped into equivalence classes for easier searching. Two
79/// user values are related if they refer to the same variable, or if they are
80/// held by the same virtual register. The equivalence class is the transitive
81/// closure of that relation.
82namespace {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000083class LDVImpl;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000084class UserValue {
85 const MDNode *variable; ///< The debug info variable we are part of.
86 unsigned offset; ///< Byte offset into variable.
Devang Patelf827cd72011-02-04 01:43:25 +000087 DebugLoc dl; ///< The debug location for the variable. This is
88 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000089 UserValue *leader; ///< Equivalence class leader.
90 UserValue *next; ///< Next value in equivalence class, or null.
91
92 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +000093 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000094
95 /// Map of slot indices where this value is live.
96 LocMap locInts;
97
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +000098 /// coalesceLocation - After LocNo was changed, check if it has become
99 /// identical to another location, and coalesce them. This may cause LocNo or
100 /// a later location to be erased, but no earlier location will be erased.
101 void coalesceLocation(unsigned LocNo);
102
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000103 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
104 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
105 LiveIntervals &LIS, const TargetInstrInfo &TII);
106
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000107 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
108 /// is live. Returns true if any changes were made.
109 bool splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
110
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000111public:
112 /// UserValue - Create a new UserValue.
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000113 UserValue(const MDNode *var, unsigned o, DebugLoc L,
Devang Patelf827cd72011-02-04 01:43:25 +0000114 LocMap::Allocator &alloc)
115 : variable(var), offset(o), dl(L), leader(this), next(0), locInts(alloc)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000116 {}
117
118 /// getLeader - Get the leader of this value's equivalence class.
119 UserValue *getLeader() {
120 UserValue *l = leader;
121 while (l != l->leader)
122 l = l->leader;
123 return leader = l;
124 }
125
126 /// getNext - Return the next UserValue in the equivalence class.
127 UserValue *getNext() const { return next; }
128
Devang Patela462d6e2011-07-06 23:09:51 +0000129 /// match - Does this UserValue match the parameters?
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000130 bool match(const MDNode *Var, unsigned Offset) const {
131 return Var == variable && Offset == offset;
132 }
133
134 /// merge - Merge equivalence classes.
135 static UserValue *merge(UserValue *L1, UserValue *L2) {
136 L2 = L2->getLeader();
137 if (!L1)
138 return L2;
139 L1 = L1->getLeader();
140 if (L1 == L2)
141 return L1;
142 // Splice L2 before L1's members.
143 UserValue *End = L2;
144 while (End->next)
145 End->leader = L1, End = End->next;
146 End->leader = L1;
147 End->next = L1->next;
148 L1->next = L2;
149 return L1;
150 }
151
152 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000153 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000154 if (LocMO.isReg()) {
155 if (LocMO.getReg() == 0)
156 return ~0u;
157 // For register locations we dont care about use/def and other flags.
158 for (unsigned i = 0, e = locations.size(); i != e; ++i)
159 if (locations[i].isReg() &&
160 locations[i].getReg() == LocMO.getReg() &&
161 locations[i].getSubReg() == LocMO.getSubReg())
162 return i;
163 } else
164 for (unsigned i = 0, e = locations.size(); i != e; ++i)
165 if (LocMO.isIdenticalTo(locations[i]))
166 return i;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000167 locations.push_back(LocMO);
168 // We are storing a MachineOperand outside a MachineInstr.
169 locations.back().clearParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000170 // Don't store def operands.
171 if (locations.back().isReg())
172 locations.back().setIsUse();
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000173 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000174 }
175
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000176 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
177 void mapVirtRegs(LDVImpl *LDV);
178
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000179 /// addDef - Add a definition point to this value.
180 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
181 // Add a singular (Idx,Idx) -> Loc mapping.
182 LocMap::iterator I = locInts.find(Idx);
183 if (!I.valid() || I.start() != Idx)
184 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen79513ed2011-08-03 23:44:31 +0000185 else
186 // A later DBG_VALUE at the same SlotIndex overrides the old location.
187 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000188 }
189
190 /// extendDef - Extend the current definition as far as possible down the
191 /// dominator tree. Stop when meeting an existing def or when leaving the live
192 /// range of VNI.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000193 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000194 /// @param Idx Starting point for the definition.
195 /// @param LocNo Location number to propagate.
196 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
197 /// @param VNI When LI is not null, this is the value to restrict to.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000198 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000199 /// @param LIS Live intervals analysis.
200 /// @param MDT Dominator tree.
201 void extendDef(SlotIndex Idx, unsigned LocNo,
202 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000203 SmallVectorImpl<SlotIndex> *Kills,
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000204 LiveIntervals &LIS, MachineDominatorTree &MDT);
205
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000206 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
207 /// registers. Determine if any of the copies are available at the kill
208 /// points, and add defs if possible.
209 /// @param LI Scan for copies of the value in LI->reg.
210 /// @param LocNo Location number of LI->reg.
211 /// @param Kills Points where the range of LocNo could be extended.
212 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
213 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
214 const SmallVectorImpl<SlotIndex> &Kills,
215 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
216 MachineRegisterInfo &MRI,
217 LiveIntervals &LIS);
218
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000219 /// computeIntervals - Compute the live intervals of all locations after
220 /// collecting all their def points.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000221 void computeIntervals(MachineRegisterInfo &MRI,
222 LiveIntervals &LIS, MachineDominatorTree &MDT);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000223
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000224 /// renameRegister - Update locations to rewrite OldReg as NewReg:SubIdx.
225 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
226 const TargetRegisterInfo *TRI);
227
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000228 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
229 /// live. Returns true if any changes were made.
230 bool splitRegister(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
231
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000232 /// rewriteLocations - Rewrite virtual register locations according to the
233 /// provided virtual register map.
234 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
235
236 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
237 void emitDebugValues(VirtRegMap *VRM,
238 LiveIntervals &LIS, const TargetInstrInfo &TRI);
239
Devang Patelf827cd72011-02-04 01:43:25 +0000240 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
241 /// variable may have more than one corresponding DBG_VALUE instructions.
242 /// Only first one needs DebugLoc to identify variable's lexical scope
243 /// in source file.
244 DebugLoc findDebugLoc();
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000245 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000246};
247} // namespace
248
249/// LDVImpl - Implementation of the LiveDebugVariables pass.
250namespace {
251class LDVImpl {
252 LiveDebugVariables &pass;
253 LocMap::Allocator allocator;
254 MachineFunction *MF;
255 LiveIntervals *LIS;
256 MachineDominatorTree *MDT;
257 const TargetRegisterInfo *TRI;
258
259 /// userValues - All allocated UserValue instances.
260 SmallVector<UserValue*, 8> userValues;
261
262 /// Map virtual register to eq class leader.
263 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000264 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000265
266 /// Map user variable to eq class leader.
267 typedef DenseMap<const MDNode *, UserValue*> UVMap;
268 UVMap userVarMap;
269
270 /// getUserValue - Find or create a UserValue.
Devang Patelf827cd72011-02-04 01:43:25 +0000271 UserValue *getUserValue(const MDNode *Var, unsigned Offset, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000272
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000273 /// lookupVirtReg - Find the EC leader for VirtReg or null.
274 UserValue *lookupVirtReg(unsigned VirtReg);
275
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000276 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
277 /// @param MI DBG_VALUE instruction
278 /// @param Idx Last valid SLotIndex before instruction.
279 /// @return True if the DBG_VALUE instruction should be deleted.
280 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
281
282 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
283 /// a UserValue def for each instruction.
284 /// @param mf MachineFunction to be scanned.
285 /// @return True if any debug values were found.
286 bool collectDebugValues(MachineFunction &mf);
287
288 /// computeIntervals - Compute the live intervals of all user values after
289 /// collecting all their def points.
290 void computeIntervals();
291
292public:
293 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
294 bool runOnMachineFunction(MachineFunction &mf);
295
296 /// clear - Relase all memory.
297 void clear() {
298 DeleteContainerPointers(userValues);
299 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000300 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000301 userVarMap.clear();
302 }
303
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000304 /// mapVirtReg - Map virtual register to an equivalence class.
305 void mapVirtReg(unsigned VirtReg, UserValue *EC);
306
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000307 /// renameRegister - Replace all references to OldReg with NewReg:SubIdx.
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000308 void renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx);
309
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000310 /// splitRegister - Replace all references to OldReg with NewRegs.
311 void splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs);
312
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000313 /// emitDebugVariables - Recreate DBG_VALUE instruction from data structures.
314 void emitDebugValues(VirtRegMap *VRM);
315
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000316 void print(raw_ostream&);
317};
318} // namespace
319
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000320void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Devang Patela2b552d2011-08-09 01:03:35 +0000321 DIVariable DV(variable);
322 OS << "!\"";
323 DV.printExtendedName(OS);
324 OS << "\"\t";
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000325 if (offset)
326 OS << '+' << offset;
327 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
328 OS << " [" << I.start() << ';' << I.stop() << "):";
329 if (I.value() == ~0u)
330 OS << "undef";
331 else
332 OS << I.value();
333 }
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000334 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
335 OS << " Loc" << i << '=';
336 locations[i].print(OS, TM);
337 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000338 OS << '\n';
339}
340
341void LDVImpl::print(raw_ostream &OS) {
342 OS << "********** DEBUG VARIABLES **********\n";
343 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000344 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000345}
346
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000347void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000348 unsigned KeepLoc = 0;
349 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
350 if (KeepLoc == LocNo)
351 continue;
352 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
353 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000354 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000355 // No matches.
356 if (KeepLoc == locations.size())
357 return;
358
359 // Keep the smaller location, erase the larger one.
360 unsigned EraseLoc = LocNo;
361 if (KeepLoc > EraseLoc)
362 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000363 locations.erase(locations.begin() + EraseLoc);
364
365 // Rewrite values.
366 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
367 unsigned v = I.value();
368 if (v == EraseLoc)
369 I.setValue(KeepLoc); // Coalesce when possible.
370 else if (v > EraseLoc)
371 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
372 }
373}
374
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000375void UserValue::mapVirtRegs(LDVImpl *LDV) {
376 for (unsigned i = 0, e = locations.size(); i != e; ++i)
377 if (locations[i].isReg() &&
378 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
379 LDV->mapVirtReg(locations[i].getReg(), this);
380}
381
Devang Patelf827cd72011-02-04 01:43:25 +0000382UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
383 DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000384 UserValue *&Leader = userVarMap[Var];
385 if (Leader) {
386 UserValue *UV = Leader->getLeader();
387 Leader = UV;
388 for (; UV; UV = UV->getNext())
389 if (UV->match(Var, Offset))
390 return UV;
391 }
392
Devang Patelf827cd72011-02-04 01:43:25 +0000393 UserValue *UV = new UserValue(Var, Offset, DL, allocator);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000394 userValues.push_back(UV);
395 Leader = UserValue::merge(Leader, UV);
396 return UV;
397}
398
399void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
400 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000401 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000402 Leader = UserValue::merge(Leader, EC);
403}
404
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000405UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000406 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000407 return UV->getLeader();
408 return 0;
409}
410
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000411bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
412 // DBG_VALUE loc, offset, variable
413 if (MI->getNumOperands() != 3 ||
414 !MI->getOperand(1).isImm() || !MI->getOperand(2).isMetadata()) {
415 DEBUG(dbgs() << "Can't handle " << *MI);
416 return false;
417 }
418
419 // Get or create the UserValue for (variable,offset).
420 unsigned Offset = MI->getOperand(1).getImm();
421 const MDNode *Var = MI->getOperand(2).getMetadata();
Devang Patelf827cd72011-02-04 01:43:25 +0000422 UserValue *UV = getUserValue(Var, Offset, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000423 UV->addDef(Idx, MI->getOperand(0));
424 return true;
425}
426
427bool LDVImpl::collectDebugValues(MachineFunction &mf) {
428 bool Changed = false;
429 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
430 ++MFI) {
431 MachineBasicBlock *MBB = MFI;
432 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
433 MBBI != MBBE;) {
434 if (!MBBI->isDebugValue()) {
435 ++MBBI;
436 continue;
437 }
438 // DBG_VALUE has no slot index, use the previous instruction instead.
439 SlotIndex Idx = MBBI == MBB->begin() ?
440 LIS->getMBBStartIdx(MBB) :
441 LIS->getInstructionIndex(llvm::prior(MBBI)).getDefIndex();
442 // Handle consecutive DBG_VALUE instructions with the same slot index.
443 do {
444 if (handleDebugValue(MBBI, Idx)) {
445 MBBI = MBB->erase(MBBI);
446 Changed = true;
447 } else
448 ++MBBI;
449 } while (MBBI != MBBE && MBBI->isDebugValue());
450 }
451 }
452 return Changed;
453}
454
455void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
456 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000457 SmallVectorImpl<SlotIndex> *Kills,
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000458 LiveIntervals &LIS, MachineDominatorTree &MDT) {
459 SmallVector<SlotIndex, 16> Todo;
460 Todo.push_back(Idx);
461
462 do {
463 SlotIndex Start = Todo.pop_back_val();
464 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
465 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000466 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000467
468 // Limit to VNI's live range.
469 bool ToEnd = true;
470 if (LI && VNI) {
471 LiveRange *Range = LI->getLiveRangeContaining(Start);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000472 if (!Range || Range->valno != VNI) {
473 if (Kills)
474 Kills->push_back(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000475 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000476 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000477 if (Range->end < Stop)
478 Stop = Range->end, ToEnd = false;
479 }
480
481 // There could already be a short def at Start.
482 if (I.valid() && I.start() <= Start) {
483 // Stop when meeting a different location or an already extended interval.
484 Start = Start.getNextSlot();
485 if (I.value() != LocNo || I.stop() != Start)
486 continue;
487 // This is a one-slot placeholder. Just skip it.
488 ++I;
489 }
490
491 // Limited by the next def.
492 if (I.valid() && I.start() < Stop)
493 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000494 // Limited by VNI's live range.
495 else if (!ToEnd && Kills)
496 Kills->push_back(Stop);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000497
498 if (Start >= Stop)
499 continue;
500
501 I.insert(Start, Stop, LocNo);
502
503 // If we extended to the MBB end, propagate down the dominator tree.
504 if (!ToEnd)
505 continue;
506 const std::vector<MachineDomTreeNode*> &Children =
507 MDT.getNode(MBB)->getChildren();
508 for (unsigned i = 0, e = Children.size(); i != e; ++i)
509 Todo.push_back(LIS.getMBBStartIdx(Children[i]->getBlock()));
510 } while (!Todo.empty());
511}
512
513void
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000514UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
515 const SmallVectorImpl<SlotIndex> &Kills,
516 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
517 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
518 if (Kills.empty())
519 return;
520 // Don't track copies from physregs, there are too many uses.
521 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
522 return;
523
524 // Collect all the (vreg, valno) pairs that are copies of LI.
525 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
526 for (MachineRegisterInfo::use_nodbg_iterator
527 UI = MRI.use_nodbg_begin(LI->reg),
528 UE = MRI.use_nodbg_end(); UI != UE; ++UI) {
529 // Copies of the full value.
530 if (UI.getOperand().getSubReg() || !UI->isCopy())
531 continue;
532 MachineInstr *MI = &*UI;
533 unsigned DstReg = MI->getOperand(0).getReg();
534
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000535 // Don't follow copies to physregs. These are usually setting up call
536 // arguments, and the argument registers are always call clobbered. We are
537 // better off in the source register which could be a callee-saved register,
538 // or it could be spilled.
539 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
540 continue;
541
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000542 // Is LocNo extended to reach this copy? If not, another def may be blocking
543 // it, or we are looking at a wrong value of LI.
544 SlotIndex Idx = LIS.getInstructionIndex(MI);
545 LocMap::iterator I = locInts.find(Idx.getUseIndex());
546 if (!I.valid() || I.value() != LocNo)
547 continue;
548
549 if (!LIS.hasInterval(DstReg))
550 continue;
551 LiveInterval *DstLI = &LIS.getInterval(DstReg);
552 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getDefIndex());
553 assert(DstVNI && DstVNI->def == Idx.getDefIndex() && "Bad copy value");
554 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
555 }
556
557 if (CopyValues.empty())
558 return;
559
560 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
561
562 // Try to add defs of the copied values for each kill point.
563 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
564 SlotIndex Idx = Kills[i];
565 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
566 LiveInterval *DstLI = CopyValues[j].first;
567 const VNInfo *DstVNI = CopyValues[j].second;
568 if (DstLI->getVNInfoAt(Idx) != DstVNI)
569 continue;
570 // Check that there isn't already a def at Idx
571 LocMap::iterator I = locInts.find(Idx);
572 if (I.valid() && I.start() <= Idx)
573 continue;
574 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
575 << DstVNI->id << " in " << *DstLI << '\n');
576 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
577 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
578 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
579 I.insert(Idx, Idx.getNextSlot(), LocNo);
580 NewDefs.push_back(std::make_pair(Idx, LocNo));
581 break;
582 }
583 }
584}
585
586void
587UserValue::computeIntervals(MachineRegisterInfo &MRI,
588 LiveIntervals &LIS,
589 MachineDominatorTree &MDT) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000590 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
591
592 // Collect all defs to be extended (Skipping undefs).
593 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
594 if (I.value() != ~0u)
595 Defs.push_back(std::make_pair(I.start(), I.value()));
596
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000597 // Extend all defs, and possibly add new ones along the way.
598 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000599 SlotIndex Idx = Defs[i].first;
600 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000601 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000602
603 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000604 if (Loc.isReg() && LIS.hasInterval(Loc.getReg())) {
605 LiveInterval *LI = &LIS.getInterval(Loc.getReg());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000606 const VNInfo *VNI = LI->getVNInfoAt(Idx);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000607 SmallVector<SlotIndex, 16> Kills;
608 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT);
609 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000610 } else
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000611 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000612 }
613
614 // Finally, erase all the undefs.
615 for (LocMap::iterator I = locInts.begin(); I.valid();)
616 if (I.value() == ~0u)
617 I.erase();
618 else
619 ++I;
620}
621
622void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000623 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
624 userValues[i]->computeIntervals(MF->getRegInfo(), *LIS, *MDT);
625 userValues[i]->mapVirtRegs(this);
626 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000627}
628
629bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
630 MF = &mf;
631 LIS = &pass.getAnalysis<LiveIntervals>();
632 MDT = &pass.getAnalysis<MachineDominatorTree>();
633 TRI = mf.getTarget().getRegisterInfo();
634 clear();
635 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
636 << ((Value*)mf.getFunction())->getName()
637 << " **********\n");
638
639 bool Changed = collectDebugValues(mf);
640 computeIntervals();
641 DEBUG(print(dbgs()));
642 return Changed;
643}
644
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000645bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000646 if (!EnableLDV)
647 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000648 if (!pImpl)
649 pImpl = new LDVImpl(this);
650 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
651}
652
653void LiveDebugVariables::releaseMemory() {
654 if (pImpl)
655 static_cast<LDVImpl*>(pImpl)->clear();
656}
657
658LiveDebugVariables::~LiveDebugVariables() {
659 if (pImpl)
660 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000661}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000662
663void UserValue::
664renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx,
665 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000666 for (unsigned i = locations.size(); i; --i) {
667 unsigned LocNo = i - 1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000668 MachineOperand &Loc = locations[LocNo];
669 if (!Loc.isReg() || Loc.getReg() != OldReg)
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000670 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000671 if (TargetRegisterInfo::isPhysicalRegister(NewReg))
672 Loc.substPhysReg(NewReg, *TRI);
673 else
674 Loc.substVirtReg(NewReg, SubIdx, *TRI);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000675 coalesceLocation(LocNo);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000676 }
677}
678
679void LDVImpl::
680renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000681 UserValue *UV = lookupVirtReg(OldReg);
682 if (!UV)
683 return;
684
685 if (TargetRegisterInfo::isVirtualRegister(NewReg))
686 mapVirtReg(NewReg, UV);
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000687 virtRegToEqClass.erase(OldReg);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000688
689 do {
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000690 UV->renameRegister(OldReg, NewReg, SubIdx, TRI);
Jakob Stoklund Olesen8d2584a2010-12-03 21:47:08 +0000691 UV = UV->getNext();
692 } while (UV);
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000693}
694
695void LiveDebugVariables::
696renameRegister(unsigned OldReg, unsigned NewReg, unsigned SubIdx) {
697 if (pImpl)
698 static_cast<LDVImpl*>(pImpl)->renameRegister(OldReg, NewReg, SubIdx);
699}
700
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000701//===----------------------------------------------------------------------===//
702// Live Range Splitting
703//===----------------------------------------------------------------------===//
704
705bool
706UserValue::splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs) {
707 DEBUG({
708 dbgs() << "Splitting Loc" << OldLocNo << '\t';
709 print(dbgs(), 0);
710 });
711 bool DidChange = false;
712 LocMap::iterator LocMapI;
713 LocMapI.setMap(locInts);
714 for (unsigned i = 0; i != NewRegs.size(); ++i) {
715 LiveInterval *LI = NewRegs[i];
716 if (LI->empty())
717 continue;
718
719 // Don't allocate the new LocNo until it is needed.
720 unsigned NewLocNo = ~0u;
721
722 // Iterate over the overlaps between locInts and LI.
723 LocMapI.find(LI->beginIndex());
724 if (!LocMapI.valid())
725 continue;
726 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
727 LiveInterval::iterator LIE = LI->end();
728 while (LocMapI.valid() && LII != LIE) {
729 // At this point, we know that LocMapI.stop() > LII->start.
730 LII = LI->advanceTo(LII, LocMapI.start());
731 if (LII == LIE)
732 break;
733
734 // Now LII->end > LocMapI.start(). Do we have an overlap?
735 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
736 // Overlapping correct location. Allocate NewLocNo now.
737 if (NewLocNo == ~0u) {
738 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
739 MO.setSubReg(locations[OldLocNo].getSubReg());
740 NewLocNo = getLocationNo(MO);
741 DidChange = true;
742 }
743
744 SlotIndex LStart = LocMapI.start();
745 SlotIndex LStop = LocMapI.stop();
746
747 // Trim LocMapI down to the LII overlap.
748 if (LStart < LII->start)
749 LocMapI.setStartUnchecked(LII->start);
750 if (LStop > LII->end)
751 LocMapI.setStopUnchecked(LII->end);
752
753 // Change the value in the overlap. This may trigger coalescing.
754 LocMapI.setValue(NewLocNo);
755
756 // Re-insert any removed OldLocNo ranges.
757 if (LStart < LocMapI.start()) {
758 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
759 ++LocMapI;
760 assert(LocMapI.valid() && "Unexpected coalescing");
761 }
762 if (LStop > LocMapI.stop()) {
763 ++LocMapI;
764 LocMapI.insert(LII->end, LStop, OldLocNo);
765 --LocMapI;
766 }
767 }
768
769 // Advance to the next overlap.
770 if (LII->end < LocMapI.stop()) {
771 if (++LII == LIE)
772 break;
773 LocMapI.advanceTo(LII->start);
774 } else {
775 ++LocMapI;
776 if (!LocMapI.valid())
777 break;
778 LII = LI->advanceTo(LII, LocMapI.start());
779 }
780 }
781 }
782
783 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
784 locations.erase(locations.begin() + OldLocNo);
785 LocMapI.goToBegin();
786 while (LocMapI.valid()) {
787 unsigned v = LocMapI.value();
788 if (v == OldLocNo) {
789 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
790 << LocMapI.stop() << ")\n");
791 LocMapI.erase();
792 } else {
793 if (v > OldLocNo)
794 LocMapI.setValueUnchecked(v-1);
795 ++LocMapI;
796 }
797 }
798
799 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);});
800 return DidChange;
801}
802
803bool
804UserValue::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
805 bool DidChange = false;
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000806 // Split locations referring to OldReg. Iterate backwards so splitLocation can
807 // safely erase unuused locations.
808 for (unsigned i = locations.size(); i ; --i) {
809 unsigned LocNo = i-1;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000810 const MachineOperand *Loc = &locations[LocNo];
811 if (!Loc->isReg() || Loc->getReg() != OldReg)
812 continue;
813 DidChange |= splitLocation(LocNo, NewRegs);
814 }
815 return DidChange;
816}
817
818void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
819 bool DidChange = false;
820 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
821 DidChange |= UV->splitRegister(OldReg, NewRegs);
822
823 if (!DidChange)
824 return;
825
826 // Map all of the new virtual registers.
827 UserValue *UV = lookupVirtReg(OldReg);
828 for (unsigned i = 0; i != NewRegs.size(); ++i)
829 mapVirtReg(NewRegs[i]->reg, UV);
830}
831
832void LiveDebugVariables::
833splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
834 if (pImpl)
835 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
836}
837
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000838void
839UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
840 // Iterate over locations in reverse makes it easier to handle coalescing.
841 for (unsigned i = locations.size(); i ; --i) {
842 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000843 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000844 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000845 if (!Loc.isReg() || !Loc.getReg() ||
846 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000847 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000848 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000849 if (VRM.isAssignedReg(VirtReg) &&
850 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000851 // This can create a %noreg operand in rare cases when the sub-register
852 // index is no longer available. That means the user value is in a
853 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000854 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesenf0704d22011-01-12 23:14:07 +0000855 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT &&
856 VRM.isSpillSlotUsed(VRM.getStackSlot(VirtReg))) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000857 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000858 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000859 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000860 Loc.setReg(0);
861 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000862 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000863 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000864 }
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000865}
866
Devang Patelf827cd72011-02-04 01:43:25 +0000867/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000868/// instruction.
869static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000870findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000871 LiveIntervals &LIS) {
872 SlotIndex Start = LIS.getMBBStartIdx(MBB);
873 Idx = Idx.getBaseIndex();
874
875 // Try to find an insert location by going backwards from Idx.
876 MachineInstr *MI;
877 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
878 // We've reached the beginning of MBB.
879 if (Idx == Start) {
880 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000881 return I;
882 }
883 Idx = Idx.getPrevIndex();
884 }
Devang Patelf827cd72011-02-04 01:43:25 +0000885
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000886 // Don't insert anything after the first terminator, though.
887 return MI->getDesc().isTerminator() ? MBB->getFirstTerminator() :
888 llvm::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000889}
890
Devang Patelf827cd72011-02-04 01:43:25 +0000891DebugLoc UserValue::findDebugLoc() {
892 DebugLoc D = dl;
893 dl = DebugLoc();
894 return D;
895}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000896void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
897 unsigned LocNo,
898 LiveIntervals &LIS,
899 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000900 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000901 MachineOperand &Loc = locations[LocNo];
Devang Pateld9f3fc72011-08-04 20:42:11 +0000902 ++NumInsertedDebugValues;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000903
904 // Frame index locations may require a target callback.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000905 if (Loc.isFI()) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000906 MachineInstr *MI = TII.emitFrameIndexDebugValue(*MBB->getParent(),
Devang Patelf827cd72011-02-04 01:43:25 +0000907 Loc.getIndex(), offset, variable,
908 findDebugLoc());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000909 if (MI) {
910 MBB->insert(I, MI);
911 return;
912 }
913 }
914 // This is not a frame index, or the target is happy with a standard FI.
Devang Patelf827cd72011-02-04 01:43:25 +0000915 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000916 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000917}
918
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000919void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
920 const TargetInstrInfo &TII) {
921 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
922
923 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
924 SlotIndex Start = I.start();
925 SlotIndex Stop = I.stop();
926 unsigned LocNo = I.value();
927 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
928 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
929 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
930
931 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
932 insertDebugValue(MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000933 // This interval may span multiple basic blocks.
934 // Insert a DBG_VALUE into each one.
935 while(Stop > MBBEnd) {
936 // Move to the next block.
937 Start = MBBEnd;
938 if (++MBB == MFEnd)
939 break;
940 MBBEnd = LIS.getMBBEndIdx(MBB);
941 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
942 insertDebugValue(MBB, Start, LocNo, LIS, TII);
943 }
944 DEBUG(dbgs() << '\n');
945 if (MBB == MFEnd)
946 break;
947
948 ++I;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000949 }
950}
951
952void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
953 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
954 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
955 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000956 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000957 userValues[i]->rewriteLocations(*VRM, *TRI);
958 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
959 }
960}
961
962void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
963 if (pImpl)
964 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
965}
966
967
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000968#ifndef NDEBUG
969void LiveDebugVariables::dump() {
970 if (pImpl)
971 static_cast<LDVImpl*>(pImpl)->print(dbgs());
972}
973#endif
974