blob: 85bed465694471668bab80d2f458f5c34dee39c9 [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 Olesen06135162010-12-02 00:37:37 +000024#include "llvm/ADT/IntervalMap.h"
Devang Patelad90d3a2011-08-04 18:45:38 +000025#include "llvm/ADT/Statistic.h"
Devang Patelc722c3d2011-08-10 21:25:34 +000026#include "llvm/CodeGen/LexicalScopes.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000027#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000028#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000032#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000033#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000034#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000035#include "llvm/IR/Constants.h"
36#include "llvm/IR/Metadata.h"
37#include "llvm/IR/Value.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
Manman Renf0986202013-02-13 20:23:48 +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
Benjamin Kramer76f58d22011-09-16 00:35:06 +000074namespace {
Devang Patel3a2d80d2011-09-13 18:40:53 +000075/// UserValueScopes - Keeps track of lexical scopes associated with an
76/// user value's source location.
77class UserValueScopes {
78 DebugLoc DL;
79 LexicalScopes &LS;
80 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
81
82public:
83 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {}
84
85 /// dominates - Return true if current scope dominates at least one machine
86 /// instruction in a given machine basic block.
87 bool dominates(MachineBasicBlock *MBB) {
88 if (LBlocks.empty())
89 LS.getMachineBasicBlocks(DL, LBlocks);
90 if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB))
91 return true;
92 return false;
93 }
94};
Benjamin Kramer76f58d22011-09-16 00:35:06 +000095} // end anonymous namespace
Devang Patel3a2d80d2011-09-13 18:40:53 +000096
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000097/// UserValue - A user value is a part of a debug info user variable.
98///
99/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
100/// holds part of a user variable. The part is identified by a byte offset.
101///
102/// UserValues are grouped into equivalence classes for easier searching. Two
103/// user values are related if they refer to the same variable, or if they are
104/// held by the same virtual register. The equivalence class is the transitive
105/// closure of that relation.
106namespace {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000107class LDVImpl;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000108class UserValue {
109 const MDNode *variable; ///< The debug info variable we are part of.
110 unsigned offset; ///< Byte offset into variable.
Adrian Prantl35176402013-07-09 20:28:37 +0000111 bool IsIndirect; ///< true if this is a register-indirect+offset value.
Devang Patelf827cd72011-02-04 01:43:25 +0000112 DebugLoc dl; ///< The debug location for the variable. This is
113 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000114 UserValue *leader; ///< Equivalence class leader.
115 UserValue *next; ///< Next value in equivalence class, or null.
116
117 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000118 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000119
120 /// Map of slot indices where this value is live.
121 LocMap locInts;
122
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000123 /// coalesceLocation - After LocNo was changed, check if it has become
124 /// identical to another location, and coalesce them. This may cause LocNo or
125 /// a later location to be erased, but no earlier location will be erased.
126 void coalesceLocation(unsigned LocNo);
127
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000128 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
129 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
130 LiveIntervals &LIS, const TargetInstrInfo &TII);
131
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000132 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
133 /// is live. Returns true if any changes were made.
134 bool splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
135
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000136public:
137 /// UserValue - Create a new UserValue.
Adrian Prantl35176402013-07-09 20:28:37 +0000138 UserValue(const MDNode *var, unsigned o, bool i, DebugLoc L,
Devang Patelf827cd72011-02-04 01:43:25 +0000139 LocMap::Allocator &alloc)
Adrian Prantl35176402013-07-09 20:28:37 +0000140 : variable(var), offset(o), IsIndirect(i), dl(L), leader(this),
141 next(0), locInts(alloc)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000142 {}
143
144 /// getLeader - Get the leader of this value's equivalence class.
145 UserValue *getLeader() {
146 UserValue *l = leader;
147 while (l != l->leader)
148 l = l->leader;
149 return leader = l;
150 }
151
152 /// getNext - Return the next UserValue in the equivalence class.
153 UserValue *getNext() const { return next; }
154
Devang Patela462d6e2011-07-06 23:09:51 +0000155 /// match - Does this UserValue match the parameters?
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000156 bool match(const MDNode *Var, unsigned Offset) const {
157 return Var == variable && Offset == offset;
158 }
159
160 /// merge - Merge equivalence classes.
161 static UserValue *merge(UserValue *L1, UserValue *L2) {
162 L2 = L2->getLeader();
163 if (!L1)
164 return L2;
165 L1 = L1->getLeader();
166 if (L1 == L2)
167 return L1;
168 // Splice L2 before L1's members.
169 UserValue *End = L2;
170 while (End->next)
171 End->leader = L1, End = End->next;
172 End->leader = L1;
173 End->next = L1->next;
174 L1->next = L2;
175 return L1;
176 }
177
178 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000179 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000180 if (LocMO.isReg()) {
181 if (LocMO.getReg() == 0)
182 return ~0u;
183 // For register locations we dont care about use/def and other flags.
184 for (unsigned i = 0, e = locations.size(); i != e; ++i)
185 if (locations[i].isReg() &&
186 locations[i].getReg() == LocMO.getReg() &&
187 locations[i].getSubReg() == LocMO.getSubReg())
188 return i;
189 } else
190 for (unsigned i = 0, e = locations.size(); i != e; ++i)
191 if (LocMO.isIdenticalTo(locations[i]))
192 return i;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000193 locations.push_back(LocMO);
194 // We are storing a MachineOperand outside a MachineInstr.
195 locations.back().clearParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000196 // Don't store def operands.
197 if (locations.back().isReg())
198 locations.back().setIsUse();
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000199 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000200 }
201
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000202 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
203 void mapVirtRegs(LDVImpl *LDV);
204
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000205 /// addDef - Add a definition point to this value.
206 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
207 // Add a singular (Idx,Idx) -> Loc mapping.
208 LocMap::iterator I = locInts.find(Idx);
209 if (!I.valid() || I.start() != Idx)
210 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen79513ed2011-08-03 23:44:31 +0000211 else
212 // A later DBG_VALUE at the same SlotIndex overrides the old location.
213 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000214 }
215
216 /// extendDef - Extend the current definition as far as possible down the
217 /// dominator tree. Stop when meeting an existing def or when leaving the live
218 /// range of VNI.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000219 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000220 /// @param Idx Starting point for the definition.
221 /// @param LocNo Location number to propagate.
222 /// @param LI Restrict liveness to where LI has the value VNI. May be null.
223 /// @param VNI When LI is not null, this is the value to restrict to.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000224 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000225 /// @param LIS Live intervals analysis.
226 /// @param MDT Dominator tree.
227 void extendDef(SlotIndex Idx, unsigned LocNo,
228 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000229 SmallVectorImpl<SlotIndex> *Kills,
Devang Patelc722c3d2011-08-10 21:25:34 +0000230 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopherb82062f2012-03-15 21:33:39 +0000231 UserValueScopes &UVS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000232
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000233 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
234 /// registers. Determine if any of the copies are available at the kill
235 /// points, and add defs if possible.
236 /// @param LI Scan for copies of the value in LI->reg.
237 /// @param LocNo Location number of LI->reg.
238 /// @param Kills Points where the range of LocNo could be extended.
239 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
240 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
241 const SmallVectorImpl<SlotIndex> &Kills,
242 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
243 MachineRegisterInfo &MRI,
244 LiveIntervals &LIS);
245
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000246 /// computeIntervals - Compute the live intervals of all locations after
247 /// collecting all their def points.
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000248 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Devang Patelc722c3d2011-08-10 21:25:34 +0000249 LiveIntervals &LIS, MachineDominatorTree &MDT,
Devang Patel3a2d80d2011-09-13 18:40:53 +0000250 UserValueScopes &UVS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000251
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000252 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
253 /// live. Returns true if any changes were made.
254 bool splitRegister(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs);
255
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000256 /// rewriteLocations - Rewrite virtual register locations according to the
257 /// provided virtual register map.
258 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
259
Eric Christopherb3cecdf2013-02-13 02:29:18 +0000260 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000261 void emitDebugValues(VirtRegMap *VRM,
262 LiveIntervals &LIS, const TargetInstrInfo &TRI);
263
Devang Patelf827cd72011-02-04 01:43:25 +0000264 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
265 /// variable may have more than one corresponding DBG_VALUE instructions.
266 /// Only first one needs DebugLoc to identify variable's lexical scope
267 /// in source file.
268 DebugLoc findDebugLoc();
Devang Patel3a2d80d2011-09-13 18:40:53 +0000269
270 /// getDebugLoc - Return DebugLoc of this UserValue.
271 DebugLoc getDebugLoc() { return dl;}
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000272 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000273};
274} // namespace
275
276/// LDVImpl - Implementation of the LiveDebugVariables pass.
277namespace {
278class LDVImpl {
279 LiveDebugVariables &pass;
280 LocMap::Allocator allocator;
281 MachineFunction *MF;
282 LiveIntervals *LIS;
Devang Patelc722c3d2011-08-10 21:25:34 +0000283 LexicalScopes LS;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000284 MachineDominatorTree *MDT;
285 const TargetRegisterInfo *TRI;
286
Manman Renf0986202013-02-13 20:23:48 +0000287 /// Whether emitDebugValues is called.
288 bool EmitDone;
289 /// Whether the machine function is modified during the pass.
290 bool ModifiedMF;
291
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000292 /// userValues - All allocated UserValue instances.
293 SmallVector<UserValue*, 8> userValues;
294
295 /// Map virtual register to eq class leader.
296 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000297 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000298
299 /// Map user variable to eq class leader.
300 typedef DenseMap<const MDNode *, UserValue*> UVMap;
301 UVMap userVarMap;
302
303 /// getUserValue - Find or create a UserValue.
Adrian Prantl35176402013-07-09 20:28:37 +0000304 UserValue *getUserValue(const MDNode *Var, unsigned Offset,
305 bool IsIndirect, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000306
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000307 /// lookupVirtReg - Find the EC leader for VirtReg or null.
308 UserValue *lookupVirtReg(unsigned VirtReg);
309
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000310 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
311 /// @param MI DBG_VALUE instruction
312 /// @param Idx Last valid SLotIndex before instruction.
313 /// @return True if the DBG_VALUE instruction should be deleted.
314 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
315
316 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
317 /// a UserValue def for each instruction.
318 /// @param mf MachineFunction to be scanned.
319 /// @return True if any debug values were found.
320 bool collectDebugValues(MachineFunction &mf);
321
322 /// computeIntervals - Compute the live intervals of all user values after
323 /// collecting all their def points.
324 void computeIntervals();
325
326public:
Manman Renf0986202013-02-13 20:23:48 +0000327 LDVImpl(LiveDebugVariables *ps) : pass(*ps), EmitDone(false),
328 ModifiedMF(false) {}
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000329 bool runOnMachineFunction(MachineFunction &mf);
330
Manman Renf0986202013-02-13 20:23:48 +0000331 /// clear - Release all memory.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000332 void clear() {
333 DeleteContainerPointers(userValues);
334 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000335 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000336 userVarMap.clear();
Manman Renf0986202013-02-13 20:23:48 +0000337 // Make sure we call emitDebugValues if the machine function was modified.
338 assert((!ModifiedMF || EmitDone) &&
339 "Dbg values are not emitted in LDV");
340 EmitDone = false;
341 ModifiedMF = false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000342 }
343
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000344 /// mapVirtReg - Map virtual register to an equivalence class.
345 void mapVirtReg(unsigned VirtReg, UserValue *EC);
346
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000347 /// splitRegister - Replace all references to OldReg with NewRegs.
348 void splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs);
349
Eric Christopherb3cecdf2013-02-13 02:29:18 +0000350 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000351 void emitDebugValues(VirtRegMap *VRM);
352
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000353 void print(raw_ostream&);
354};
355} // namespace
356
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000357void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Devang Patela2b552d2011-08-09 01:03:35 +0000358 DIVariable DV(variable);
359 OS << "!\"";
360 DV.printExtendedName(OS);
361 OS << "\"\t";
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000362 if (offset)
363 OS << '+' << offset;
364 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
365 OS << " [" << I.start() << ';' << I.stop() << "):";
366 if (I.value() == ~0u)
367 OS << "undef";
368 else
369 OS << I.value();
370 }
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000371 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
372 OS << " Loc" << i << '=';
373 locations[i].print(OS, TM);
374 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000375 OS << '\n';
376}
377
378void LDVImpl::print(raw_ostream &OS) {
379 OS << "********** DEBUG VARIABLES **********\n";
380 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000381 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000382}
383
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000384void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000385 unsigned KeepLoc = 0;
386 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
387 if (KeepLoc == LocNo)
388 continue;
389 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
390 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000391 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000392 // No matches.
393 if (KeepLoc == locations.size())
394 return;
395
396 // Keep the smaller location, erase the larger one.
397 unsigned EraseLoc = LocNo;
398 if (KeepLoc > EraseLoc)
399 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000400 locations.erase(locations.begin() + EraseLoc);
401
402 // Rewrite values.
403 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
404 unsigned v = I.value();
405 if (v == EraseLoc)
406 I.setValue(KeepLoc); // Coalesce when possible.
407 else if (v > EraseLoc)
408 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
409 }
410}
411
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000412void UserValue::mapVirtRegs(LDVImpl *LDV) {
413 for (unsigned i = 0, e = locations.size(); i != e; ++i)
414 if (locations[i].isReg() &&
415 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
416 LDV->mapVirtReg(locations[i].getReg(), this);
417}
418
Devang Patelf827cd72011-02-04 01:43:25 +0000419UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
Adrian Prantl35176402013-07-09 20:28:37 +0000420 bool IsIndirect, DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000421 UserValue *&Leader = userVarMap[Var];
422 if (Leader) {
423 UserValue *UV = Leader->getLeader();
424 Leader = UV;
425 for (; UV; UV = UV->getNext())
426 if (UV->match(Var, Offset))
427 return UV;
428 }
429
Adrian Prantl35176402013-07-09 20:28:37 +0000430 UserValue *UV = new UserValue(Var, Offset, IsIndirect, DL, allocator);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000431 userValues.push_back(UV);
432 Leader = UserValue::merge(Leader, UV);
433 return UV;
434}
435
436void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
437 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000438 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000439 Leader = UserValue::merge(Leader, EC);
440}
441
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000442UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000443 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000444 return UV->getLeader();
445 return 0;
446}
447
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000448bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
449 // DBG_VALUE loc, offset, variable
450 if (MI->getNumOperands() != 3 ||
Adrian Prantl35176402013-07-09 20:28:37 +0000451 !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) ||
452 !MI->getOperand(2).isMetadata()) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000453 DEBUG(dbgs() << "Can't handle " << *MI);
454 return false;
455 }
456
457 // Get or create the UserValue for (variable,offset).
Adrian Prantl35176402013-07-09 20:28:37 +0000458 bool IsIndirect = MI->getOperand(1).isImm();
459 unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000460 const MDNode *Var = MI->getOperand(2).getMetadata();
Adrian Prantl35176402013-07-09 20:28:37 +0000461 UserValue *UV = getUserValue(Var, Offset, IsIndirect, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000462 UV->addDef(Idx, MI->getOperand(0));
463 return true;
464}
465
466bool LDVImpl::collectDebugValues(MachineFunction &mf) {
467 bool Changed = false;
468 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
469 ++MFI) {
470 MachineBasicBlock *MBB = MFI;
471 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
472 MBBI != MBBE;) {
473 if (!MBBI->isDebugValue()) {
474 ++MBBI;
475 continue;
476 }
477 // DBG_VALUE has no slot index, use the previous instruction instead.
478 SlotIndex Idx = MBBI == MBB->begin() ?
479 LIS->getMBBStartIdx(MBB) :
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000480 LIS->getInstructionIndex(llvm::prior(MBBI)).getRegSlot();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000481 // Handle consecutive DBG_VALUE instructions with the same slot index.
482 do {
483 if (handleDebugValue(MBBI, Idx)) {
484 MBBI = MBB->erase(MBBI);
485 Changed = true;
486 } else
487 ++MBBI;
488 } while (MBBI != MBBE && MBBI->isDebugValue());
489 }
490 }
491 return Changed;
492}
493
494void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
495 LiveInterval *LI, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000496 SmallVectorImpl<SlotIndex> *Kills,
Devang Patelc722c3d2011-08-10 21:25:34 +0000497 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopherb82062f2012-03-15 21:33:39 +0000498 UserValueScopes &UVS) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000499 SmallVector<SlotIndex, 16> Todo;
500 Todo.push_back(Idx);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000501 do {
502 SlotIndex Start = Todo.pop_back_val();
503 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
504 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000505 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000506
507 // Limit to VNI's live range.
508 bool ToEnd = true;
509 if (LI && VNI) {
510 LiveRange *Range = LI->getLiveRangeContaining(Start);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000511 if (!Range || Range->valno != VNI) {
512 if (Kills)
513 Kills->push_back(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000514 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000515 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000516 if (Range->end < Stop)
517 Stop = Range->end, ToEnd = false;
518 }
519
520 // There could already be a short def at Start.
521 if (I.valid() && I.start() <= Start) {
522 // Stop when meeting a different location or an already extended interval.
523 Start = Start.getNextSlot();
524 if (I.value() != LocNo || I.stop() != Start)
525 continue;
526 // This is a one-slot placeholder. Just skip it.
527 ++I;
528 }
529
530 // Limited by the next def.
531 if (I.valid() && I.start() < Stop)
532 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000533 // Limited by VNI's live range.
534 else if (!ToEnd && Kills)
535 Kills->push_back(Stop);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000536
537 if (Start >= Stop)
538 continue;
539
540 I.insert(Start, Stop, LocNo);
541
542 // If we extended to the MBB end, propagate down the dominator tree.
543 if (!ToEnd)
544 continue;
545 const std::vector<MachineDomTreeNode*> &Children =
546 MDT.getNode(MBB)->getChildren();
Devang Patelc722c3d2011-08-10 21:25:34 +0000547 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
548 MachineBasicBlock *MBB = Children[i]->getBlock();
Devang Patel3a2d80d2011-09-13 18:40:53 +0000549 if (UVS.dominates(MBB))
Devang Patelc722c3d2011-08-10 21:25:34 +0000550 Todo.push_back(LIS.getMBBStartIdx(MBB));
551 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000552 } while (!Todo.empty());
553}
554
555void
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000556UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
557 const SmallVectorImpl<SlotIndex> &Kills,
558 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
559 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
560 if (Kills.empty())
561 return;
562 // Don't track copies from physregs, there are too many uses.
563 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
564 return;
565
566 // Collect all the (vreg, valno) pairs that are copies of LI.
567 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
568 for (MachineRegisterInfo::use_nodbg_iterator
569 UI = MRI.use_nodbg_begin(LI->reg),
570 UE = MRI.use_nodbg_end(); UI != UE; ++UI) {
571 // Copies of the full value.
572 if (UI.getOperand().getSubReg() || !UI->isCopy())
573 continue;
574 MachineInstr *MI = &*UI;
575 unsigned DstReg = MI->getOperand(0).getReg();
576
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000577 // Don't follow copies to physregs. These are usually setting up call
578 // arguments, and the argument registers are always call clobbered. We are
579 // better off in the source register which could be a callee-saved register,
580 // or it could be spilled.
581 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
582 continue;
583
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000584 // Is LocNo extended to reach this copy? If not, another def may be blocking
585 // it, or we are looking at a wrong value of LI.
586 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000587 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000588 if (!I.valid() || I.value() != LocNo)
589 continue;
590
591 if (!LIS.hasInterval(DstReg))
592 continue;
593 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000594 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
595 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000596 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
597 }
598
599 if (CopyValues.empty())
600 return;
601
602 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
603
604 // Try to add defs of the copied values for each kill point.
605 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
606 SlotIndex Idx = Kills[i];
607 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
608 LiveInterval *DstLI = CopyValues[j].first;
609 const VNInfo *DstVNI = CopyValues[j].second;
610 if (DstLI->getVNInfoAt(Idx) != DstVNI)
611 continue;
612 // Check that there isn't already a def at Idx
613 LocMap::iterator I = locInts.find(Idx);
614 if (I.valid() && I.start() <= Idx)
615 continue;
616 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
617 << DstVNI->id << " in " << *DstLI << '\n');
618 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
619 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
620 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
621 I.insert(Idx, Idx.getNextSlot(), LocNo);
622 NewDefs.push_back(std::make_pair(Idx, LocNo));
623 break;
624 }
625 }
626}
627
628void
629UserValue::computeIntervals(MachineRegisterInfo &MRI,
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000630 const TargetRegisterInfo &TRI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000631 LiveIntervals &LIS,
Devang Patelc722c3d2011-08-10 21:25:34 +0000632 MachineDominatorTree &MDT,
Eric Christopherb82062f2012-03-15 21:33:39 +0000633 UserValueScopes &UVS) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000634 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
635
636 // Collect all defs to be extended (Skipping undefs).
637 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
638 if (I.value() != ~0u)
639 Defs.push_back(std::make_pair(I.start(), I.value()));
640
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000641 // Extend all defs, and possibly add new ones along the way.
642 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000643 SlotIndex Idx = Defs[i].first;
644 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000645 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000646
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000647 if (!Loc.isReg()) {
648 extendDef(Idx, LocNo, 0, 0, 0, LIS, MDT, UVS);
649 continue;
650 }
651
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000652 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000653 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
Jakob Stoklund Olesenb1509302012-06-22 18:51:35 +0000654 LiveInterval *LI = 0;
655 const VNInfo *VNI = 0;
656 if (LIS.hasInterval(Loc.getReg())) {
657 LI = &LIS.getInterval(Loc.getReg());
658 VNI = LI->getVNInfoAt(Idx);
659 }
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000660 SmallVector<SlotIndex, 16> Kills;
Devang Patel3a2d80d2011-09-13 18:40:53 +0000661 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
Jakob Stoklund Olesenb1509302012-06-22 18:51:35 +0000662 if (LI)
663 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000664 continue;
665 }
666
667 // For physregs, use the live range of the first regunit as a guide.
668 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
669 LiveInterval *LI = &LIS.getRegUnit(Unit);
670 const VNInfo *VNI = LI->getVNInfoAt(Idx);
671 // Don't track copies from physregs, it is too expensive.
672 extendDef(Idx, LocNo, LI, VNI, 0, LIS, MDT, UVS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000673 }
674
675 // Finally, erase all the undefs.
676 for (LocMap::iterator I = locInts.begin(); I.valid();)
677 if (I.value() == ~0u)
678 I.erase();
679 else
680 ++I;
681}
682
683void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000684 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Devang Patel3a2d80d2011-09-13 18:40:53 +0000685 UserValueScopes UVS(userValues[i]->getDebugLoc(), LS);
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000686 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000687 userValues[i]->mapVirtRegs(this);
688 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000689}
690
691bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
692 MF = &mf;
693 LIS = &pass.getAnalysis<LiveIntervals>();
694 MDT = &pass.getAnalysis<MachineDominatorTree>();
695 TRI = mf.getTarget().getRegisterInfo();
696 clear();
Devang Patelc722c3d2011-08-10 21:25:34 +0000697 LS.initialize(mf);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000698 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikie986d76d2012-08-22 17:18:53 +0000699 << mf.getName() << " **********\n");
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000700
701 bool Changed = collectDebugValues(mf);
702 computeIntervals();
703 DEBUG(print(dbgs()));
Devang Patelc722c3d2011-08-10 21:25:34 +0000704 LS.releaseMemory();
Manman Renf0986202013-02-13 20:23:48 +0000705 ModifiedMF = Changed;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000706 return Changed;
707}
708
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000709bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000710 if (!EnableLDV)
711 return false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000712 if (!pImpl)
713 pImpl = new LDVImpl(this);
Manman Renf0986202013-02-13 20:23:48 +0000714 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000715}
716
717void LiveDebugVariables::releaseMemory() {
Manman Renf0986202013-02-13 20:23:48 +0000718 if (pImpl)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000719 static_cast<LDVImpl*>(pImpl)->clear();
720}
721
722LiveDebugVariables::~LiveDebugVariables() {
723 if (pImpl)
724 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000725}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000726
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000727//===----------------------------------------------------------------------===//
728// Live Range Splitting
729//===----------------------------------------------------------------------===//
730
731bool
732UserValue::splitLocation(unsigned OldLocNo, ArrayRef<LiveInterval*> NewRegs) {
733 DEBUG({
734 dbgs() << "Splitting Loc" << OldLocNo << '\t';
735 print(dbgs(), 0);
736 });
737 bool DidChange = false;
738 LocMap::iterator LocMapI;
739 LocMapI.setMap(locInts);
740 for (unsigned i = 0; i != NewRegs.size(); ++i) {
741 LiveInterval *LI = NewRegs[i];
742 if (LI->empty())
743 continue;
744
745 // Don't allocate the new LocNo until it is needed.
746 unsigned NewLocNo = ~0u;
747
748 // Iterate over the overlaps between locInts and LI.
749 LocMapI.find(LI->beginIndex());
750 if (!LocMapI.valid())
751 continue;
752 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
753 LiveInterval::iterator LIE = LI->end();
754 while (LocMapI.valid() && LII != LIE) {
755 // At this point, we know that LocMapI.stop() > LII->start.
756 LII = LI->advanceTo(LII, LocMapI.start());
757 if (LII == LIE)
758 break;
759
760 // Now LII->end > LocMapI.start(). Do we have an overlap?
761 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
762 // Overlapping correct location. Allocate NewLocNo now.
763 if (NewLocNo == ~0u) {
764 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
765 MO.setSubReg(locations[OldLocNo].getSubReg());
766 NewLocNo = getLocationNo(MO);
767 DidChange = true;
768 }
769
770 SlotIndex LStart = LocMapI.start();
771 SlotIndex LStop = LocMapI.stop();
772
773 // Trim LocMapI down to the LII overlap.
774 if (LStart < LII->start)
775 LocMapI.setStartUnchecked(LII->start);
776 if (LStop > LII->end)
777 LocMapI.setStopUnchecked(LII->end);
778
779 // Change the value in the overlap. This may trigger coalescing.
780 LocMapI.setValue(NewLocNo);
781
782 // Re-insert any removed OldLocNo ranges.
783 if (LStart < LocMapI.start()) {
784 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
785 ++LocMapI;
786 assert(LocMapI.valid() && "Unexpected coalescing");
787 }
788 if (LStop > LocMapI.stop()) {
789 ++LocMapI;
790 LocMapI.insert(LII->end, LStop, OldLocNo);
791 --LocMapI;
792 }
793 }
794
795 // Advance to the next overlap.
796 if (LII->end < LocMapI.stop()) {
797 if (++LII == LIE)
798 break;
799 LocMapI.advanceTo(LII->start);
800 } else {
801 ++LocMapI;
802 if (!LocMapI.valid())
803 break;
804 LII = LI->advanceTo(LII, LocMapI.start());
805 }
806 }
807 }
808
809 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
810 locations.erase(locations.begin() + OldLocNo);
811 LocMapI.goToBegin();
812 while (LocMapI.valid()) {
813 unsigned v = LocMapI.value();
814 if (v == OldLocNo) {
815 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
816 << LocMapI.stop() << ")\n");
817 LocMapI.erase();
818 } else {
819 if (v > OldLocNo)
820 LocMapI.setValueUnchecked(v-1);
821 ++LocMapI;
822 }
823 }
824
825 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), 0);});
826 return DidChange;
827}
828
829bool
830UserValue::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
831 bool DidChange = false;
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000832 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopher7cc51772012-03-15 21:33:35 +0000833 // safely erase unused locations.
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000834 for (unsigned i = locations.size(); i ; --i) {
835 unsigned LocNo = i-1;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000836 const MachineOperand *Loc = &locations[LocNo];
837 if (!Loc->isReg() || Loc->getReg() != OldReg)
838 continue;
839 DidChange |= splitLocation(LocNo, NewRegs);
840 }
841 return DidChange;
842}
843
844void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
845 bool DidChange = false;
846 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
847 DidChange |= UV->splitRegister(OldReg, NewRegs);
848
849 if (!DidChange)
850 return;
851
852 // Map all of the new virtual registers.
853 UserValue *UV = lookupVirtReg(OldReg);
854 for (unsigned i = 0; i != NewRegs.size(); ++i)
855 mapVirtReg(NewRegs[i]->reg, UV);
856}
857
858void LiveDebugVariables::
859splitRegister(unsigned OldReg, ArrayRef<LiveInterval*> NewRegs) {
860 if (pImpl)
861 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
862}
863
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000864void
865UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
866 // Iterate over locations in reverse makes it easier to handle coalescing.
867 for (unsigned i = locations.size(); i ; --i) {
868 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000869 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000870 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000871 if (!Loc.isReg() || !Loc.getReg() ||
872 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000873 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000874 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000875 if (VRM.isAssignedReg(VirtReg) &&
876 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000877 // This can create a %noreg operand in rare cases when the sub-register
878 // index is no longer available. That means the user value is in a
879 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000880 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesencb390642011-11-13 01:23:30 +0000881 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000882 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000883 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000884 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000885 Loc.setReg(0);
886 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000887 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000888 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000889 }
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000890}
891
Devang Patelf827cd72011-02-04 01:43:25 +0000892/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000893/// instruction.
894static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000895findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000896 LiveIntervals &LIS) {
897 SlotIndex Start = LIS.getMBBStartIdx(MBB);
898 Idx = Idx.getBaseIndex();
899
900 // Try to find an insert location by going backwards from Idx.
901 MachineInstr *MI;
902 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
903 // We've reached the beginning of MBB.
904 if (Idx == Start) {
905 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000906 return I;
907 }
908 Idx = Idx.getPrevIndex();
909 }
Devang Patelf827cd72011-02-04 01:43:25 +0000910
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000911 // Don't insert anything after the first terminator, though.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000912 return MI->isTerminator() ? MBB->getFirstTerminator() :
913 llvm::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000914}
915
Devang Patelf827cd72011-02-04 01:43:25 +0000916DebugLoc UserValue::findDebugLoc() {
917 DebugLoc D = dl;
918 dl = DebugLoc();
919 return D;
920}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000921void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
922 unsigned LocNo,
923 LiveIntervals &LIS,
924 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000925 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000926 MachineOperand &Loc = locations[LocNo];
Devang Pateld9f3fc72011-08-04 20:42:11 +0000927 ++NumInsertedDebugValues;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000928
Adrian Prantl35176402013-07-09 20:28:37 +0000929 if (Loc.isReg())
930 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
931 IsIndirect, Loc.getReg(), offset, variable);
932 else
933 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
934 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000935}
936
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000937void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
938 const TargetInstrInfo &TII) {
939 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
940
941 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
942 SlotIndex Start = I.start();
943 SlotIndex Stop = I.stop();
944 unsigned LocNo = I.value();
945 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
946 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
947 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
948
949 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
950 insertDebugValue(MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000951 // This interval may span multiple basic blocks.
952 // Insert a DBG_VALUE into each one.
953 while(Stop > MBBEnd) {
954 // Move to the next block.
955 Start = MBBEnd;
956 if (++MBB == MFEnd)
957 break;
958 MBBEnd = LIS.getMBBEndIdx(MBB);
959 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
960 insertDebugValue(MBB, Start, LocNo, LIS, TII);
961 }
962 DEBUG(dbgs() << '\n');
963 if (MBB == MFEnd)
964 break;
965
966 ++I;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000967 }
968}
969
970void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
971 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
972 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
973 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000974 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000975 userValues[i]->rewriteLocations(*VRM, *TRI);
976 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
977 }
Manman Renf0986202013-02-13 20:23:48 +0000978 EmitDone = true;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000979}
980
981void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Renf0986202013-02-13 20:23:48 +0000982 if (pImpl)
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000983 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
984}
985
986
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000987#ifndef NDEBUG
988void LiveDebugVariables::dump() {
989 if (pImpl)
990 static_cast<LDVImpl*>(pImpl)->print(dbgs());
991}
992#endif