blob: 2790008b92cbede077f17ccf532e881ce18ac555 [file] [log] [blame]
Jakob Stoklund Olesend4900a62010-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
22#include "LiveDebugVariables.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000023#include "llvm/ADT/IntervalMap.h"
Devang Patelb4568662011-08-04 18:45:38 +000024#include "llvm/ADT/Statistic.h"
Devang Patel37a62052011-08-10 21:25:34 +000025#include "llvm/CodeGen/LexicalScopes.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000026#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000027#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000031#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000032#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000034#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/Metadata.h"
36#include "llvm/IR/Value.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000037#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/Debug.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000039#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000040#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000041#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000042#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000043
David Blaikie2b1dfa72014-04-21 20:37:07 +000044#include <memory>
45
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000046using namespace llvm;
47
Chandler Carruth1b9dde02014-04-22 02:02:50 +000048#define DEBUG_TYPE "livedebug"
49
Devang Patelacbee0b2011-01-07 22:33:41 +000050static cl::opt<bool>
Jakob Stoklund Olesen74ded572011-01-12 23:36:21 +000051EnableLDV("live-debug-variables", cl::init(true),
Devang Patelacbee0b2011-01-07 22:33:41 +000052 cl::desc("Enable the live debug variables pass"), cl::Hidden);
53
Devang Patelb4568662011-08-04 18:45:38 +000054STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000055char LiveDebugVariables::ID = 0;
56
57INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
58 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000059INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000060INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
61INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
62 "Debug Variable Analysis", false, false)
63
64void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000065 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000066 AU.addRequiredTransitive<LiveIntervals>();
67 AU.setPreservesAll();
68 MachineFunctionPass::getAnalysisUsage(AU);
69}
70
Craig Topperc0196b12014-04-14 00:51:57 +000071LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(nullptr) {
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000072 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
73}
74
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000075/// LocMap - Map of where a user value is live, and its location.
76typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
77
Benjamin Kramer67b014b2011-09-16 00:35:06 +000078namespace {
Eric Christopher9d7d5da2013-11-20 00:54:25 +000079/// UserValueScopes - Keeps track of lexical scopes associated with a
Devang Patelf9e2ae92011-09-13 18:40:53 +000080/// user value's source location.
81class UserValueScopes {
82 DebugLoc DL;
83 LexicalScopes &LS;
84 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
85
86public:
87 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {}
88
89 /// dominates - Return true if current scope dominates at least one machine
90 /// instruction in a given machine basic block.
91 bool dominates(MachineBasicBlock *MBB) {
92 if (LBlocks.empty())
93 LS.getMachineBasicBlocks(DL, LBlocks);
94 if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB))
95 return true;
96 return false;
97 }
98};
Benjamin Kramer67b014b2011-09-16 00:35:06 +000099} // end anonymous namespace
Devang Patelf9e2ae92011-09-13 18:40:53 +0000100
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000101/// UserValue - A user value is a part of a debug info user variable.
102///
103/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
104/// holds part of a user variable. The part is identified by a byte offset.
105///
106/// UserValues are grouped into equivalence classes for easier searching. Two
107/// user values are related if they refer to the same variable, or if they are
108/// held by the same virtual register. The equivalence class is the transitive
109/// closure of that relation.
110namespace {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000111class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000112class UserValue {
113 const MDNode *variable; ///< The debug info variable we are part of.
114 unsigned offset; ///< Byte offset into variable.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000115 bool IsIndirect; ///< true if this is a register-indirect+offset value.
Devang Patel26ffa012011-02-04 01:43:25 +0000116 DebugLoc dl; ///< The debug location for the variable. This is
117 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000118 UserValue *leader; ///< Equivalence class leader.
119 UserValue *next; ///< Next value in equivalence class, or null.
120
121 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000122 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000123
124 /// Map of slot indices where this value is live.
125 LocMap locInts;
126
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000127 /// coalesceLocation - After LocNo was changed, check if it has become
128 /// identical to another location, and coalesce them. This may cause LocNo or
129 /// a later location to be erased, but no earlier location will be erased.
130 void coalesceLocation(unsigned LocNo);
131
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000132 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
133 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
134 LiveIntervals &LIS, const TargetInstrInfo &TII);
135
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000136 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
137 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000138 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
139 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000140
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000141public:
142 /// UserValue - Create a new UserValue.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000143 UserValue(const MDNode *var, unsigned o, bool i, DebugLoc L,
Devang Patel26ffa012011-02-04 01:43:25 +0000144 LocMap::Allocator &alloc)
Adrian Prantl418d1d12013-07-09 20:28:37 +0000145 : variable(var), offset(o), IsIndirect(i), dl(L), leader(this),
Craig Topperc0196b12014-04-14 00:51:57 +0000146 next(nullptr), locInts(alloc)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000147 {}
148
149 /// getLeader - Get the leader of this value's equivalence class.
150 UserValue *getLeader() {
151 UserValue *l = leader;
152 while (l != l->leader)
153 l = l->leader;
154 return leader = l;
155 }
156
157 /// getNext - Return the next UserValue in the equivalence class.
158 UserValue *getNext() const { return next; }
159
Devang Patel338e4322011-07-06 23:09:51 +0000160 /// match - Does this UserValue match the parameters?
Adrian Prantl32da8892014-04-25 20:49:25 +0000161 bool match(const MDNode *Var, unsigned Offset, bool indirect) const {
162 return Var == variable && Offset == offset && indirect == IsIndirect;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000163 }
164
165 /// merge - Merge equivalence classes.
166 static UserValue *merge(UserValue *L1, UserValue *L2) {
167 L2 = L2->getLeader();
168 if (!L1)
169 return L2;
170 L1 = L1->getLeader();
171 if (L1 == L2)
172 return L1;
173 // Splice L2 before L1's members.
174 UserValue *End = L2;
175 while (End->next)
176 End->leader = L1, End = End->next;
177 End->leader = L1;
178 End->next = L1->next;
179 L1->next = L2;
180 return L1;
181 }
182
183 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000184 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000185 if (LocMO.isReg()) {
186 if (LocMO.getReg() == 0)
187 return ~0u;
188 // For register locations we dont care about use/def and other flags.
189 for (unsigned i = 0, e = locations.size(); i != e; ++i)
190 if (locations[i].isReg() &&
191 locations[i].getReg() == LocMO.getReg() &&
192 locations[i].getSubReg() == LocMO.getSubReg())
193 return i;
194 } else
195 for (unsigned i = 0, e = locations.size(); i != e; ++i)
196 if (LocMO.isIdenticalTo(locations[i]))
197 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000198 locations.push_back(LocMO);
199 // We are storing a MachineOperand outside a MachineInstr.
200 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000201 // Don't store def operands.
202 if (locations.back().isReg())
203 locations.back().setIsUse();
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000204 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000205 }
206
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000207 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
208 void mapVirtRegs(LDVImpl *LDV);
209
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000210 /// addDef - Add a definition point to this value.
211 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
212 // Add a singular (Idx,Idx) -> Loc mapping.
213 LocMap::iterator I = locInts.find(Idx);
214 if (!I.valid() || I.start() != Idx)
215 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000216 else
217 // A later DBG_VALUE at the same SlotIndex overrides the old location.
218 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000219 }
220
221 /// extendDef - Extend the current definition as far as possible down the
222 /// dominator tree. Stop when meeting an existing def or when leaving the live
223 /// range of VNI.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000224 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000225 /// @param Idx Starting point for the definition.
226 /// @param LocNo Location number to propagate.
Matthias Braun34e1be92013-10-10 21:29:02 +0000227 /// @param LR Restrict liveness to where LR has the value VNI. May be null.
228 /// @param VNI When LR is not null, this is the value to restrict to.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000229 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000230 /// @param LIS Live intervals analysis.
231 /// @param MDT Dominator tree.
232 void extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun34e1be92013-10-10 21:29:02 +0000233 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000234 SmallVectorImpl<SlotIndex> *Kills,
Devang Patel37a62052011-08-10 21:25:34 +0000235 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000236 UserValueScopes &UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000237
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000238 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
239 /// registers. Determine if any of the copies are available at the kill
240 /// points, and add defs if possible.
241 /// @param LI Scan for copies of the value in LI->reg.
242 /// @param LocNo Location number of LI->reg.
243 /// @param Kills Points where the range of LocNo could be extended.
244 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
245 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
246 const SmallVectorImpl<SlotIndex> &Kills,
247 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
248 MachineRegisterInfo &MRI,
249 LiveIntervals &LIS);
250
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000251 /// computeIntervals - Compute the live intervals of all locations after
252 /// collecting all their def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000253 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Devang Patel37a62052011-08-10 21:25:34 +0000254 LiveIntervals &LIS, MachineDominatorTree &MDT,
Devang Patelf9e2ae92011-09-13 18:40:53 +0000255 UserValueScopes &UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000256
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000257 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
258 /// live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000259 bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
260 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000261
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000262 /// rewriteLocations - Rewrite virtual register locations according to the
263 /// provided virtual register map.
264 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
265
Eric Christopherbc671702013-02-13 02:29:18 +0000266 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000267 void emitDebugValues(VirtRegMap *VRM,
268 LiveIntervals &LIS, const TargetInstrInfo &TRI);
269
Devang Patel26ffa012011-02-04 01:43:25 +0000270 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
Frederic Risse6bb1872014-08-07 20:04:00 +0000271 /// variable may have more than one corresponding DBG_VALUE instructions.
Devang Patel26ffa012011-02-04 01:43:25 +0000272 /// Only first one needs DebugLoc to identify variable's lexical scope
273 /// in source file.
274 DebugLoc findDebugLoc();
Devang Patelf9e2ae92011-09-13 18:40:53 +0000275
276 /// getDebugLoc - Return DebugLoc of this UserValue.
277 DebugLoc getDebugLoc() { return dl;}
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000278 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000279};
280} // namespace
281
282/// LDVImpl - Implementation of the LiveDebugVariables pass.
283namespace {
284class LDVImpl {
285 LiveDebugVariables &pass;
286 LocMap::Allocator allocator;
287 MachineFunction *MF;
288 LiveIntervals *LIS;
Devang Patel37a62052011-08-10 21:25:34 +0000289 LexicalScopes LS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000290 MachineDominatorTree *MDT;
291 const TargetRegisterInfo *TRI;
292
Manman Ren7a4c8a72013-02-13 20:23:48 +0000293 /// Whether emitDebugValues is called.
294 bool EmitDone;
295 /// Whether the machine function is modified during the pass.
296 bool ModifiedMF;
297
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000298 /// userValues - All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000299 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000300
301 /// Map virtual register to eq class leader.
302 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000303 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000304
305 /// Map user variable to eq class leader.
306 typedef DenseMap<const MDNode *, UserValue*> UVMap;
307 UVMap userVarMap;
308
309 /// getUserValue - Find or create a UserValue.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000310 UserValue *getUserValue(const MDNode *Var, unsigned Offset,
311 bool IsIndirect, DebugLoc DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000312
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000313 /// lookupVirtReg - Find the EC leader for VirtReg or null.
314 UserValue *lookupVirtReg(unsigned VirtReg);
315
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000316 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
317 /// @param MI DBG_VALUE instruction
318 /// @param Idx Last valid SLotIndex before instruction.
319 /// @return True if the DBG_VALUE instruction should be deleted.
320 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
321
322 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
323 /// a UserValue def for each instruction.
324 /// @param mf MachineFunction to be scanned.
325 /// @return True if any debug values were found.
326 bool collectDebugValues(MachineFunction &mf);
327
328 /// computeIntervals - Compute the live intervals of all user values after
329 /// collecting all their def points.
330 void computeIntervals();
331
332public:
David Blaikie2f040112014-07-25 16:10:16 +0000333 LDVImpl(LiveDebugVariables *ps)
334 : pass(*ps), MF(nullptr), EmitDone(false), ModifiedMF(false) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000335 bool runOnMachineFunction(MachineFunction &mf);
336
Manman Ren7a4c8a72013-02-13 20:23:48 +0000337 /// clear - Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000338 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000339 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000340 userValues.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000341 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000342 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000343 // Make sure we call emitDebugValues if the machine function was modified.
344 assert((!ModifiedMF || EmitDone) &&
345 "Dbg values are not emitted in LDV");
346 EmitDone = false;
347 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000348 }
349
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000350 /// mapVirtReg - Map virtual register to an equivalence class.
351 void mapVirtReg(unsigned VirtReg, UserValue *EC);
352
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000353 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000354 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000355
Eric Christopherbc671702013-02-13 02:29:18 +0000356 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000357 void emitDebugValues(VirtRegMap *VRM);
358
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000359 void print(raw_ostream&);
360};
361} // namespace
362
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000363void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Devang Patel6c1ed312011-08-09 01:03:35 +0000364 DIVariable DV(variable);
Frederic Risse6bb1872014-08-07 20:04:00 +0000365 OS << "!\"";
Devang Patel6c1ed312011-08-09 01:03:35 +0000366 DV.printExtendedName(OS);
367 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000368 if (offset)
369 OS << '+' << offset;
370 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
371 OS << " [" << I.start() << ';' << I.stop() << "):";
372 if (I.value() == ~0u)
373 OS << "undef";
374 else
375 OS << I.value();
376 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000377 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
378 OS << " Loc" << i << '=';
379 locations[i].print(OS, TM);
380 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000381 OS << '\n';
382}
383
384void LDVImpl::print(raw_ostream &OS) {
385 OS << "********** DEBUG VARIABLES **********\n";
386 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000387 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000388}
389
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000390void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000391 unsigned KeepLoc = 0;
392 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
393 if (KeepLoc == LocNo)
394 continue;
395 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
396 break;
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000397 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000398 // No matches.
399 if (KeepLoc == locations.size())
400 return;
401
402 // Keep the smaller location, erase the larger one.
403 unsigned EraseLoc = LocNo;
404 if (KeepLoc > EraseLoc)
405 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000406 locations.erase(locations.begin() + EraseLoc);
407
408 // Rewrite values.
409 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
410 unsigned v = I.value();
411 if (v == EraseLoc)
412 I.setValue(KeepLoc); // Coalesce when possible.
413 else if (v > EraseLoc)
414 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
415 }
416}
417
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000418void UserValue::mapVirtRegs(LDVImpl *LDV) {
419 for (unsigned i = 0, e = locations.size(); i != e; ++i)
420 if (locations[i].isReg() &&
421 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
422 LDV->mapVirtReg(locations[i].getReg(), this);
423}
424
Devang Patel26ffa012011-02-04 01:43:25 +0000425UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
Adrian Prantl418d1d12013-07-09 20:28:37 +0000426 bool IsIndirect, DebugLoc DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000427 UserValue *&Leader = userVarMap[Var];
428 if (Leader) {
429 UserValue *UV = Leader->getLeader();
430 Leader = UV;
431 for (; UV; UV = UV->getNext())
Adrian Prantl32da8892014-04-25 20:49:25 +0000432 if (UV->match(Var, Offset, IsIndirect))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000433 return UV;
434 }
435
David Blaikie2b1dfa72014-04-21 20:37:07 +0000436 userValues.push_back(
437 make_unique<UserValue>(Var, Offset, IsIndirect, DL, allocator));
438 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000439 Leader = UserValue::merge(Leader, UV);
440 return UV;
441}
442
443void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
444 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000445 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000446 Leader = UserValue::merge(Leader, EC);
447}
448
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000449UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000450 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000451 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000452 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000453}
454
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000455bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
456 // DBG_VALUE loc, offset, variable
457 if (MI->getNumOperands() != 3 ||
Adrian Prantl418d1d12013-07-09 20:28:37 +0000458 !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) ||
459 !MI->getOperand(2).isMetadata()) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000460 DEBUG(dbgs() << "Can't handle " << *MI);
461 return false;
462 }
463
464 // Get or create the UserValue for (variable,offset).
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000465 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantl418d1d12013-07-09 20:28:37 +0000466 unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000467 const MDNode *Var = MI->getOperand(2).getMetadata();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000468 //here.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000469 UserValue *UV = getUserValue(Var, Offset, IsIndirect, MI->getDebugLoc());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000470 UV->addDef(Idx, MI->getOperand(0));
471 return true;
472}
473
474bool LDVImpl::collectDebugValues(MachineFunction &mf) {
475 bool Changed = false;
476 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
477 ++MFI) {
478 MachineBasicBlock *MBB = MFI;
479 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
480 MBBI != MBBE;) {
481 if (!MBBI->isDebugValue()) {
482 ++MBBI;
483 continue;
484 }
485 // DBG_VALUE has no slot index, use the previous instruction instead.
486 SlotIndex Idx = MBBI == MBB->begin() ?
487 LIS->getMBBStartIdx(MBB) :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000488 LIS->getInstructionIndex(std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000489 // Handle consecutive DBG_VALUE instructions with the same slot index.
490 do {
491 if (handleDebugValue(MBBI, Idx)) {
492 MBBI = MBB->erase(MBBI);
493 Changed = true;
494 } else
495 ++MBBI;
496 } while (MBBI != MBBE && MBBI->isDebugValue());
497 }
498 }
499 return Changed;
500}
501
502void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun34e1be92013-10-10 21:29:02 +0000503 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000504 SmallVectorImpl<SlotIndex> *Kills,
Devang Patel37a62052011-08-10 21:25:34 +0000505 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000506 UserValueScopes &UVS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000507 SmallVector<SlotIndex, 16> Todo;
508 Todo.push_back(Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000509 do {
510 SlotIndex Start = Todo.pop_back_val();
511 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
512 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen2ffee662011-01-12 23:14:04 +0000513 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000514
515 // Limit to VNI's live range.
516 bool ToEnd = true;
Matthias Braun34e1be92013-10-10 21:29:02 +0000517 if (LR && VNI) {
518 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000519 if (!Segment || Segment->valno != VNI) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000520 if (Kills)
521 Kills->push_back(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000522 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000523 }
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000524 if (Segment->end < Stop)
525 Stop = Segment->end, ToEnd = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000526 }
527
528 // There could already be a short def at Start.
529 if (I.valid() && I.start() <= Start) {
530 // Stop when meeting a different location or an already extended interval.
531 Start = Start.getNextSlot();
532 if (I.value() != LocNo || I.stop() != Start)
533 continue;
534 // This is a one-slot placeholder. Just skip it.
535 ++I;
536 }
537
538 // Limited by the next def.
539 if (I.valid() && I.start() < Stop)
540 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000541 // Limited by VNI's live range.
542 else if (!ToEnd && Kills)
543 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000544
545 if (Start >= Stop)
546 continue;
547
548 I.insert(Start, Stop, LocNo);
549
550 // If we extended to the MBB end, propagate down the dominator tree.
551 if (!ToEnd)
552 continue;
553 const std::vector<MachineDomTreeNode*> &Children =
554 MDT.getNode(MBB)->getChildren();
Devang Patel37a62052011-08-10 21:25:34 +0000555 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
556 MachineBasicBlock *MBB = Children[i]->getBlock();
Devang Patelf9e2ae92011-09-13 18:40:53 +0000557 if (UVS.dominates(MBB))
Devang Patel37a62052011-08-10 21:25:34 +0000558 Todo.push_back(LIS.getMBBStartIdx(MBB));
559 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000560 } while (!Todo.empty());
561}
562
563void
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000564UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
565 const SmallVectorImpl<SlotIndex> &Kills,
566 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
567 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
568 if (Kills.empty())
569 return;
570 // Don't track copies from physregs, there are too many uses.
571 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
572 return;
573
574 // Collect all the (vreg, valno) pairs that are copies of LI.
575 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000576 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
577 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000578 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000579 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000580 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000581 unsigned DstReg = MI->getOperand(0).getReg();
582
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000583 // Don't follow copies to physregs. These are usually setting up call
584 // arguments, and the argument registers are always call clobbered. We are
585 // better off in the source register which could be a callee-saved register,
586 // or it could be spilled.
587 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
588 continue;
589
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000590 // Is LocNo extended to reach this copy? If not, another def may be blocking
591 // it, or we are looking at a wrong value of LI.
592 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000593 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000594 if (!I.valid() || I.value() != LocNo)
595 continue;
596
597 if (!LIS.hasInterval(DstReg))
598 continue;
599 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000600 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
601 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000602 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
603 }
604
605 if (CopyValues.empty())
606 return;
607
608 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
609
610 // Try to add defs of the copied values for each kill point.
611 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
612 SlotIndex Idx = Kills[i];
613 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
614 LiveInterval *DstLI = CopyValues[j].first;
615 const VNInfo *DstVNI = CopyValues[j].second;
616 if (DstLI->getVNInfoAt(Idx) != DstVNI)
617 continue;
618 // Check that there isn't already a def at Idx
619 LocMap::iterator I = locInts.find(Idx);
620 if (I.valid() && I.start() <= Idx)
621 continue;
622 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
623 << DstVNI->id << " in " << *DstLI << '\n');
624 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
625 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
626 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
627 I.insert(Idx, Idx.getNextSlot(), LocNo);
628 NewDefs.push_back(std::make_pair(Idx, LocNo));
629 break;
630 }
631 }
632}
633
634void
635UserValue::computeIntervals(MachineRegisterInfo &MRI,
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000636 const TargetRegisterInfo &TRI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000637 LiveIntervals &LIS,
Devang Patel37a62052011-08-10 21:25:34 +0000638 MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000639 UserValueScopes &UVS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000640 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
641
642 // Collect all defs to be extended (Skipping undefs).
643 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
644 if (I.value() != ~0u)
645 Defs.push_back(std::make_pair(I.start(), I.value()));
646
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000647 // Extend all defs, and possibly add new ones along the way.
648 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000649 SlotIndex Idx = Defs[i].first;
650 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000651 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000652
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000653 if (!Loc.isReg()) {
Craig Topperc0196b12014-04-14 00:51:57 +0000654 extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000655 continue;
656 }
657
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000658 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000659 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000660 LiveInterval *LI = nullptr;
661 const VNInfo *VNI = nullptr;
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000662 if (LIS.hasInterval(Loc.getReg())) {
663 LI = &LIS.getInterval(Loc.getReg());
664 VNI = LI->getVNInfoAt(Idx);
665 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000666 SmallVector<SlotIndex, 16> Kills;
Devang Patelf9e2ae92011-09-13 18:40:53 +0000667 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000668 if (LI)
669 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000670 continue;
671 }
672
673 // For physregs, use the live range of the first regunit as a guide.
674 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
Matthias Braun34e1be92013-10-10 21:29:02 +0000675 LiveRange *LR = &LIS.getRegUnit(Unit);
676 const VNInfo *VNI = LR->getVNInfoAt(Idx);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000677 // Don't track copies from physregs, it is too expensive.
Craig Topperc0196b12014-04-14 00:51:57 +0000678 extendDef(Idx, LocNo, LR, VNI, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000679 }
680
681 // Finally, erase all the undefs.
682 for (LocMap::iterator I = locInts.begin(); I.valid();)
683 if (I.value() == ~0u)
684 I.erase();
685 else
686 ++I;
687}
688
689void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000690 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Devang Patelf9e2ae92011-09-13 18:40:53 +0000691 UserValueScopes UVS(userValues[i]->getDebugLoc(), LS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000692 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000693 userValues[i]->mapVirtRegs(this);
694 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000695}
696
697bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000698 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000699 MF = &mf;
700 LIS = &pass.getAnalysis<LiveIntervals>();
701 MDT = &pass.getAnalysis<MachineDominatorTree>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000702 TRI = mf.getSubtarget().getRegisterInfo();
Devang Patel37a62052011-08-10 21:25:34 +0000703 LS.initialize(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000704 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikiec8c29202012-08-22 17:18:53 +0000705 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000706
707 bool Changed = collectDebugValues(mf);
708 computeIntervals();
709 DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000710 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000711 return Changed;
712}
713
David Blaikie2f040112014-07-25 16:10:16 +0000714static void removeDebugValues(MachineFunction &mf) {
715 for (MachineBasicBlock &MBB : mf) {
716 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
717 if (!MBBI->isDebugValue()) {
718 ++MBBI;
719 continue;
720 }
721 MBBI = MBB.erase(MBBI);
722 }
723 }
724}
725
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000726bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000727 if (!EnableLDV)
728 return false;
David Blaikie2f040112014-07-25 16:10:16 +0000729 if (!FunctionDIs.count(mf.getFunction())) {
730 removeDebugValues(mf);
731 return false;
732 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000733 if (!pImpl)
734 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000735 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000736}
737
738void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000739 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000740 static_cast<LDVImpl*>(pImpl)->clear();
741}
742
743LiveDebugVariables::~LiveDebugVariables() {
744 if (pImpl)
745 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000746}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000747
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000748//===----------------------------------------------------------------------===//
749// Live Range Splitting
750//===----------------------------------------------------------------------===//
751
752bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000753UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
754 LiveIntervals& LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000755 DEBUG({
756 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +0000757 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000758 });
759 bool DidChange = false;
760 LocMap::iterator LocMapI;
761 LocMapI.setMap(locInts);
762 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000763 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000764 if (LI->empty())
765 continue;
766
767 // Don't allocate the new LocNo until it is needed.
768 unsigned NewLocNo = ~0u;
769
770 // Iterate over the overlaps between locInts and LI.
771 LocMapI.find(LI->beginIndex());
772 if (!LocMapI.valid())
773 continue;
774 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
775 LiveInterval::iterator LIE = LI->end();
776 while (LocMapI.valid() && LII != LIE) {
777 // At this point, we know that LocMapI.stop() > LII->start.
778 LII = LI->advanceTo(LII, LocMapI.start());
779 if (LII == LIE)
780 break;
781
782 // Now LII->end > LocMapI.start(). Do we have an overlap?
783 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
784 // Overlapping correct location. Allocate NewLocNo now.
785 if (NewLocNo == ~0u) {
786 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
787 MO.setSubReg(locations[OldLocNo].getSubReg());
788 NewLocNo = getLocationNo(MO);
789 DidChange = true;
790 }
791
792 SlotIndex LStart = LocMapI.start();
793 SlotIndex LStop = LocMapI.stop();
794
795 // Trim LocMapI down to the LII overlap.
796 if (LStart < LII->start)
797 LocMapI.setStartUnchecked(LII->start);
798 if (LStop > LII->end)
799 LocMapI.setStopUnchecked(LII->end);
800
801 // Change the value in the overlap. This may trigger coalescing.
802 LocMapI.setValue(NewLocNo);
803
804 // Re-insert any removed OldLocNo ranges.
805 if (LStart < LocMapI.start()) {
806 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
807 ++LocMapI;
808 assert(LocMapI.valid() && "Unexpected coalescing");
809 }
810 if (LStop > LocMapI.stop()) {
811 ++LocMapI;
812 LocMapI.insert(LII->end, LStop, OldLocNo);
813 --LocMapI;
814 }
815 }
816
817 // Advance to the next overlap.
818 if (LII->end < LocMapI.stop()) {
819 if (++LII == LIE)
820 break;
821 LocMapI.advanceTo(LII->start);
822 } else {
823 ++LocMapI;
824 if (!LocMapI.valid())
825 break;
826 LII = LI->advanceTo(LII, LocMapI.start());
827 }
828 }
829 }
830
831 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
832 locations.erase(locations.begin() + OldLocNo);
833 LocMapI.goToBegin();
834 while (LocMapI.valid()) {
835 unsigned v = LocMapI.value();
836 if (v == OldLocNo) {
837 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
838 << LocMapI.stop() << ")\n");
839 LocMapI.erase();
840 } else {
841 if (v > OldLocNo)
842 LocMapI.setValueUnchecked(v-1);
843 ++LocMapI;
844 }
845 }
846
Craig Topperc0196b12014-04-14 00:51:57 +0000847 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000848 return DidChange;
849}
850
851bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000852UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
853 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000854 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000855 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +0000856 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000857 for (unsigned i = locations.size(); i ; --i) {
858 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000859 const MachineOperand *Loc = &locations[LocNo];
860 if (!Loc->isReg() || Loc->getReg() != OldReg)
861 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000862 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000863 }
864 return DidChange;
865}
866
Mark Laceyf9ea8852013-08-14 23:50:04 +0000867void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000868 bool DidChange = false;
869 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +0000870 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000871
872 if (!DidChange)
873 return;
874
875 // Map all of the new virtual registers.
876 UserValue *UV = lookupVirtReg(OldReg);
877 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +0000878 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000879}
880
881void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +0000882splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000883 if (pImpl)
884 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
885}
886
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000887void
888UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
889 // Iterate over locations in reverse makes it easier to handle coalescing.
890 for (unsigned i = locations.size(); i ; --i) {
891 unsigned LocNo = i-1;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000892 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000893 // Only virtual registers are rewritten.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000894 if (!Loc.isReg() || !Loc.getReg() ||
895 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000896 continue;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000897 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesen1a3534a2011-01-12 22:37:49 +0000898 if (VRM.isAssignedReg(VirtReg) &&
899 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000900 // This can create a %noreg operand in rare cases when the sub-register
901 // index is no longer available. That means the user value is in a
902 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000903 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesen28df7ef2011-11-13 01:23:30 +0000904 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000905 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000906 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000907 } else {
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000908 Loc.setReg(0);
909 Loc.setSubReg(0);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000910 }
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000911 coalesceLocation(LocNo);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000912 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000913}
914
Devang Patel26ffa012011-02-04 01:43:25 +0000915/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000916/// instruction.
917static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +0000918findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000919 LiveIntervals &LIS) {
920 SlotIndex Start = LIS.getMBBStartIdx(MBB);
921 Idx = Idx.getBaseIndex();
922
923 // Try to find an insert location by going backwards from Idx.
924 MachineInstr *MI;
925 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
926 // We've reached the beginning of MBB.
927 if (Idx == Start) {
928 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000929 return I;
930 }
931 Idx = Idx.getPrevIndex();
932 }
Devang Patel26ffa012011-02-04 01:43:25 +0000933
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +0000934 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000935 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000936 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000937}
938
Devang Patel26ffa012011-02-04 01:43:25 +0000939DebugLoc UserValue::findDebugLoc() {
940 DebugLoc D = dl;
941 dl = DebugLoc();
942 return D;
943}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000944void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
945 unsigned LocNo,
946 LiveIntervals &LIS,
947 const TargetInstrInfo &TII) {
Devang Patel26ffa012011-02-04 01:43:25 +0000948 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000949 MachineOperand &Loc = locations[LocNo];
Devang Pateleabc3cea2011-08-04 20:42:11 +0000950 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000951
Adrian Prantl418d1d12013-07-09 20:28:37 +0000952 if (Loc.isReg())
953 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
954 IsIndirect, Loc.getReg(), offset, variable);
955 else
956 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
957 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000958}
959
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000960void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
961 const TargetInstrInfo &TII) {
962 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
963
964 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
965 SlotIndex Start = I.start();
966 SlotIndex Stop = I.stop();
967 unsigned LocNo = I.value();
968 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
969 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
970 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
971
972 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
973 insertDebugValue(MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000974 // This interval may span multiple basic blocks.
975 // Insert a DBG_VALUE into each one.
976 while(Stop > MBBEnd) {
977 // Move to the next block.
978 Start = MBBEnd;
979 if (++MBB == MFEnd)
980 break;
981 MBBEnd = LIS.getMBBEndIdx(MBB);
982 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
983 insertDebugValue(MBB, Start, LocNo, LIS, TII);
984 }
985 DEBUG(dbgs() << '\n');
986 if (MBB == MFEnd)
987 break;
988
989 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000990 }
991}
992
993void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
994 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +0000995 if (!MF)
996 return;
Eric Christopherfc6de422014-08-05 02:39:49 +0000997 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000998 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000999 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001000 userValues[i]->rewriteLocations(*VRM, *TRI);
1001 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
1002 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001003 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001004}
1005
1006void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001007 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001008 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1009}
1010
David Blaikie2f040112014-07-25 16:10:16 +00001011bool LiveDebugVariables::doInitialization(Module &M) {
1012 FunctionDIs = makeSubprogramMap(M);
1013 return Pass::doInitialization(M);
1014}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001015
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001016#ifndef NDEBUG
1017void LiveDebugVariables::dump() {
1018 if (pImpl)
1019 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1020}
1021#endif