blob: 654328714bc59b8448bf17c0886cefc838a97334 [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"
Benjamin Kramer799003b2015-03-23 19:32:43 +000039#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000040#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000041#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000042#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000043#include "llvm/Target/TargetSubtargetInfo.h"
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);
Rafael Espindola84921b92015-10-24 23:11:13 +000094 return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
Devang Patelf9e2ae92011-09-13 18:40:53 +000095 }
96};
Benjamin Kramer67b014b2011-09-16 00:35:06 +000097} // end anonymous namespace
Devang Patelf9e2ae92011-09-13 18:40:53 +000098
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000099/// UserValue - A user value is a part of a debug info user variable.
100///
101/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
102/// holds part of a user variable. The part is identified by a byte offset.
103///
104/// UserValues are grouped into equivalence classes for easier searching. Two
105/// user values are related if they refer to the same variable, or if they are
106/// held by the same virtual register. The equivalence class is the transitive
107/// closure of that relation.
108namespace {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000109class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000110class UserValue {
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000111 const MDNode *Variable; ///< The debug info variable we are part of.
112 const MDNode *Expression; ///< Any complex address expression.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000113 unsigned offset; ///< Byte offset into variable.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000114 bool IsIndirect; ///< true if this is a register-indirect+offset value.
Devang Patel26ffa012011-02-04 01:43:25 +0000115 DebugLoc dl; ///< The debug location for the variable. This is
116 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000117 UserValue *leader; ///< Equivalence class leader.
118 UserValue *next; ///< Next value in equivalence class, or null.
119
120 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000121 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000122
123 /// Map of slot indices where this value is live.
124 LocMap locInts;
125
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000126 /// coalesceLocation - After LocNo was changed, check if it has become
127 /// identical to another location, and coalesce them. This may cause LocNo or
128 /// a later location to be erased, but no earlier location will be erased.
129 void coalesceLocation(unsigned LocNo);
130
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000131 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
132 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
133 LiveIntervals &LIS, const TargetInstrInfo &TII);
134
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000135 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
136 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000137 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
138 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000139
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000140public:
141 /// UserValue - Create a new UserValue.
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000142 UserValue(const MDNode *var, const MDNode *expr, unsigned o, bool i,
143 DebugLoc L, LocMap::Allocator &alloc)
144 : Variable(var), Expression(expr), offset(o), IsIndirect(i), dl(L),
145 leader(this), next(nullptr), locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000146
147 /// getLeader - Get the leader of this value's equivalence class.
148 UserValue *getLeader() {
149 UserValue *l = leader;
150 while (l != l->leader)
151 l = l->leader;
152 return leader = l;
153 }
154
155 /// getNext - Return the next UserValue in the equivalence class.
156 UserValue *getNext() const { return next; }
157
Devang Patel338e4322011-07-06 23:09:51 +0000158 /// match - Does this UserValue match the parameters?
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000159 bool match(const MDNode *Var, const MDNode *Expr, const DILocation *IA,
Duncan P. N. Exon Smith7bb480d2015-04-16 22:27:54 +0000160 unsigned Offset, bool indirect) const {
161 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA &&
162 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 Patelf9e2ae92011-09-13 18:40:53 +0000270 /// getDebugLoc - Return DebugLoc of this UserValue.
271 DebugLoc getDebugLoc() { return dl;}
Eric Christopher1cdefae2015-02-27 00:11:34 +0000272 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-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 Patel37a62052011-08-10 21:25:34 +0000283 LexicalScopes LS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000284 MachineDominatorTree *MDT;
285 const TargetRegisterInfo *TRI;
286
Manman Ren7a4c8a72013-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 Olesen4be0bd72010-12-02 00:37:37 +0000292 /// userValues - All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000293 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000294
295 /// Map virtual register to eq class leader.
296 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000297 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-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 Prantl87b7eb92014-10-01 18:55:02 +0000304 UserValue *getUserValue(const MDNode *Var, const MDNode *Expr,
305 unsigned Offset, bool IsIndirect, DebugLoc DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000306
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000307 /// lookupVirtReg - Find the EC leader for VirtReg or null.
308 UserValue *lookupVirtReg(unsigned VirtReg);
309
Jakob Stoklund Olesen4be0bd72010-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:
David Blaikie2f040112014-07-25 16:10:16 +0000327 LDVImpl(LiveDebugVariables *ps)
328 : pass(*ps), MF(nullptr), EmitDone(false), ModifiedMF(false) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000329 bool runOnMachineFunction(MachineFunction &mf);
330
Manman Ren7a4c8a72013-02-13 20:23:48 +0000331 /// clear - Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000332 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000333 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000334 userValues.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000335 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000336 userVarMap.clear();
Manman Ren7a4c8a72013-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;
Marcello Maggioni22594002014-10-24 02:46:50 +0000342 LS.reset();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000343 }
344
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000345 /// mapVirtReg - Map virtual register to an equivalence class.
346 void mapVirtReg(unsigned VirtReg, UserValue *EC);
347
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000348 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000349 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000350
Eric Christopherbc671702013-02-13 02:29:18 +0000351 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000352 void emitDebugValues(VirtRegMap *VRM);
353
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000354 void print(raw_ostream&);
355};
356} // namespace
357
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000358static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
359 const LLVMContext &Ctx) {
360 if (!DL)
361 return;
362
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000363 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000364 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000365 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000366 CommentOS << ':' << DL.getLine();
367 if (DL.getCol() != 0)
368 CommentOS << ':' << DL.getCol();
369
370 DebugLoc InlinedAtDL = DL.getInlinedAt();
371 if (!InlinedAtDL)
372 return;
373
374 CommentOS << " @[ ";
375 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
376 CommentOS << " ]";
377}
378
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000379static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
380 const DILocation *DL) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000381 const LLVMContext &Ctx = V->getContext();
382 StringRef Res = V->getName();
383 if (!Res.empty())
384 OS << Res << "," << V->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000385 if (auto *InlinedAt = DL->getInlinedAt()) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000386 if (DebugLoc InlinedAtDL = InlinedAt) {
387 OS << " @[";
388 printDebugLoc(InlinedAtDL, OS, Ctx);
389 OS << "]";
390 }
391 }
392}
393
Eric Christopher1cdefae2015-02-27 00:11:34 +0000394void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000395 auto *DV = cast<DILocalVariable>(Variable);
Frederic Risse6bb1872014-08-07 20:04:00 +0000396 OS << "!\"";
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000397 printExtendedName(OS, DV, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000398
Devang Patel6c1ed312011-08-09 01:03:35 +0000399 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000400 if (offset)
401 OS << '+' << offset;
402 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
403 OS << " [" << I.start() << ';' << I.stop() << "):";
404 if (I.value() == ~0u)
405 OS << "undef";
406 else
407 OS << I.value();
408 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000409 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
410 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000411 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000412 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000413 OS << '\n';
414}
415
416void LDVImpl::print(raw_ostream &OS) {
417 OS << "********** DEBUG VARIABLES **********\n";
418 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Eric Christopher1cdefae2015-02-27 00:11:34 +0000419 userValues[i]->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000420}
421
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000422void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000423 unsigned KeepLoc = 0;
424 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
425 if (KeepLoc == LocNo)
426 continue;
427 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
428 break;
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000429 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000430 // No matches.
431 if (KeepLoc == locations.size())
432 return;
433
434 // Keep the smaller location, erase the larger one.
435 unsigned EraseLoc = LocNo;
436 if (KeepLoc > EraseLoc)
437 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000438 locations.erase(locations.begin() + EraseLoc);
439
440 // Rewrite values.
441 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
442 unsigned v = I.value();
443 if (v == EraseLoc)
444 I.setValue(KeepLoc); // Coalesce when possible.
445 else if (v > EraseLoc)
446 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
447 }
448}
449
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000450void UserValue::mapVirtRegs(LDVImpl *LDV) {
451 for (unsigned i = 0, e = locations.size(); i != e; ++i)
452 if (locations[i].isReg() &&
453 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
454 LDV->mapVirtReg(locations[i].getReg(), this);
455}
456
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000457UserValue *LDVImpl::getUserValue(const MDNode *Var, const MDNode *Expr,
458 unsigned Offset, bool IsIndirect,
459 DebugLoc DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000460 UserValue *&Leader = userVarMap[Var];
461 if (Leader) {
462 UserValue *UV = Leader->getLeader();
463 Leader = UV;
464 for (; UV; UV = UV->getNext())
Duncan P. N. Exon Smith7bb480d2015-04-16 22:27:54 +0000465 if (UV->match(Var, Expr, DL->getInlinedAt(), Offset, IsIndirect))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000466 return UV;
467 }
468
David Blaikie2b1dfa72014-04-21 20:37:07 +0000469 userValues.push_back(
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000470 make_unique<UserValue>(Var, Expr, Offset, IsIndirect, DL, allocator));
David Blaikie2b1dfa72014-04-21 20:37:07 +0000471 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000472 Leader = UserValue::merge(Leader, UV);
473 return UV;
474}
475
476void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
477 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000478 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000479 Leader = UserValue::merge(Leader, EC);
480}
481
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000482UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000483 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000484 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000485 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000486}
487
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000488bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
489 // DBG_VALUE loc, offset, variable
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000490 if (MI->getNumOperands() != 4 ||
Adrian Prantl418d1d12013-07-09 20:28:37 +0000491 !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) ||
492 !MI->getOperand(2).isMetadata()) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000493 DEBUG(dbgs() << "Can't handle " << *MI);
494 return false;
495 }
496
497 // Get or create the UserValue for (variable,offset).
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000498 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantl418d1d12013-07-09 20:28:37 +0000499 unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000500 const MDNode *Var = MI->getDebugVariable();
501 const MDNode *Expr = MI->getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000502 //here.
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000503 UserValue *UV =
504 getUserValue(Var, Expr, Offset, IsIndirect, MI->getDebugLoc());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000505 UV->addDef(Idx, MI->getOperand(0));
506 return true;
507}
508
509bool LDVImpl::collectDebugValues(MachineFunction &mf) {
510 bool Changed = false;
511 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
512 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000513 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000514 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
515 MBBI != MBBE;) {
516 if (!MBBI->isDebugValue()) {
517 ++MBBI;
518 continue;
519 }
520 // DBG_VALUE has no slot index, use the previous instruction instead.
521 SlotIndex Idx = MBBI == MBB->begin() ?
522 LIS->getMBBStartIdx(MBB) :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000523 LIS->getInstructionIndex(std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000524 // Handle consecutive DBG_VALUE instructions with the same slot index.
525 do {
526 if (handleDebugValue(MBBI, Idx)) {
527 MBBI = MBB->erase(MBBI);
528 Changed = true;
529 } else
530 ++MBBI;
531 } while (MBBI != MBBE && MBBI->isDebugValue());
532 }
533 }
534 return Changed;
535}
536
537void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun34e1be92013-10-10 21:29:02 +0000538 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000539 SmallVectorImpl<SlotIndex> *Kills,
Devang Patel37a62052011-08-10 21:25:34 +0000540 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000541 UserValueScopes &UVS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000542 SmallVector<SlotIndex, 16> Todo;
543 Todo.push_back(Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000544 do {
545 SlotIndex Start = Todo.pop_back_val();
546 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
547 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen2ffee662011-01-12 23:14:04 +0000548 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000549
550 // Limit to VNI's live range.
551 bool ToEnd = true;
Matthias Braun34e1be92013-10-10 21:29:02 +0000552 if (LR && VNI) {
553 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000554 if (!Segment || Segment->valno != VNI) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000555 if (Kills)
556 Kills->push_back(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000557 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000558 }
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000559 if (Segment->end < Stop)
560 Stop = Segment->end, ToEnd = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000561 }
562
563 // There could already be a short def at Start.
564 if (I.valid() && I.start() <= Start) {
565 // Stop when meeting a different location or an already extended interval.
566 Start = Start.getNextSlot();
567 if (I.value() != LocNo || I.stop() != Start)
568 continue;
569 // This is a one-slot placeholder. Just skip it.
570 ++I;
571 }
572
573 // Limited by the next def.
574 if (I.valid() && I.start() < Stop)
575 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000576 // Limited by VNI's live range.
577 else if (!ToEnd && Kills)
578 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000579
580 if (Start >= Stop)
581 continue;
582
583 I.insert(Start, Stop, LocNo);
584
585 // If we extended to the MBB end, propagate down the dominator tree.
586 if (!ToEnd)
587 continue;
588 const std::vector<MachineDomTreeNode*> &Children =
589 MDT.getNode(MBB)->getChildren();
Devang Patel37a62052011-08-10 21:25:34 +0000590 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
591 MachineBasicBlock *MBB = Children[i]->getBlock();
Devang Patelf9e2ae92011-09-13 18:40:53 +0000592 if (UVS.dominates(MBB))
Devang Patel37a62052011-08-10 21:25:34 +0000593 Todo.push_back(LIS.getMBBStartIdx(MBB));
594 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000595 } while (!Todo.empty());
596}
597
598void
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000599UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
600 const SmallVectorImpl<SlotIndex> &Kills,
601 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
602 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
603 if (Kills.empty())
604 return;
605 // Don't track copies from physregs, there are too many uses.
606 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
607 return;
608
609 // Collect all the (vreg, valno) pairs that are copies of LI.
610 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000611 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
612 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000613 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000614 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000615 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000616 unsigned DstReg = MI->getOperand(0).getReg();
617
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000618 // Don't follow copies to physregs. These are usually setting up call
619 // arguments, and the argument registers are always call clobbered. We are
620 // better off in the source register which could be a callee-saved register,
621 // or it could be spilled.
622 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
623 continue;
624
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000625 // Is LocNo extended to reach this copy? If not, another def may be blocking
626 // it, or we are looking at a wrong value of LI.
627 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000628 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000629 if (!I.valid() || I.value() != LocNo)
630 continue;
631
632 if (!LIS.hasInterval(DstReg))
633 continue;
634 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000635 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
636 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000637 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
638 }
639
640 if (CopyValues.empty())
641 return;
642
643 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
644
645 // Try to add defs of the copied values for each kill point.
646 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
647 SlotIndex Idx = Kills[i];
648 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
649 LiveInterval *DstLI = CopyValues[j].first;
650 const VNInfo *DstVNI = CopyValues[j].second;
651 if (DstLI->getVNInfoAt(Idx) != DstVNI)
652 continue;
653 // Check that there isn't already a def at Idx
654 LocMap::iterator I = locInts.find(Idx);
655 if (I.valid() && I.start() <= Idx)
656 continue;
657 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
658 << DstVNI->id << " in " << *DstLI << '\n');
659 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
660 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
661 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
662 I.insert(Idx, Idx.getNextSlot(), LocNo);
663 NewDefs.push_back(std::make_pair(Idx, LocNo));
664 break;
665 }
666 }
667}
668
669void
670UserValue::computeIntervals(MachineRegisterInfo &MRI,
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000671 const TargetRegisterInfo &TRI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000672 LiveIntervals &LIS,
Devang Patel37a62052011-08-10 21:25:34 +0000673 MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000674 UserValueScopes &UVS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000675 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
676
677 // Collect all defs to be extended (Skipping undefs).
678 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
679 if (I.value() != ~0u)
680 Defs.push_back(std::make_pair(I.start(), I.value()));
681
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000682 // Extend all defs, and possibly add new ones along the way.
683 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000684 SlotIndex Idx = Defs[i].first;
685 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000686 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000687
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000688 if (!Loc.isReg()) {
Craig Topperc0196b12014-04-14 00:51:57 +0000689 extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000690 continue;
691 }
692
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000693 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000694 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000695 LiveInterval *LI = nullptr;
696 const VNInfo *VNI = nullptr;
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000697 if (LIS.hasInterval(Loc.getReg())) {
698 LI = &LIS.getInterval(Loc.getReg());
699 VNI = LI->getVNInfoAt(Idx);
700 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000701 SmallVector<SlotIndex, 16> Kills;
Devang Patelf9e2ae92011-09-13 18:40:53 +0000702 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000703 if (LI)
704 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000705 continue;
706 }
707
708 // For physregs, use the live range of the first regunit as a guide.
709 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
Matthias Braun34e1be92013-10-10 21:29:02 +0000710 LiveRange *LR = &LIS.getRegUnit(Unit);
711 const VNInfo *VNI = LR->getVNInfoAt(Idx);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000712 // Don't track copies from physregs, it is too expensive.
Craig Topperc0196b12014-04-14 00:51:57 +0000713 extendDef(Idx, LocNo, LR, VNI, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000714 }
715
716 // Finally, erase all the undefs.
717 for (LocMap::iterator I = locInts.begin(); I.valid();)
718 if (I.value() == ~0u)
719 I.erase();
720 else
721 ++I;
722}
723
724void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000725 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Devang Patelf9e2ae92011-09-13 18:40:53 +0000726 UserValueScopes UVS(userValues[i]->getDebugLoc(), LS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000727 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000728 userValues[i]->mapVirtRegs(this);
729 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000730}
731
732bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000733 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000734 MF = &mf;
735 LIS = &pass.getAnalysis<LiveIntervals>();
736 MDT = &pass.getAnalysis<MachineDominatorTree>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000737 TRI = mf.getSubtarget().getRegisterInfo();
Devang Patel37a62052011-08-10 21:25:34 +0000738 LS.initialize(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000739 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikiec8c29202012-08-22 17:18:53 +0000740 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000741
742 bool Changed = collectDebugValues(mf);
743 computeIntervals();
744 DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000745 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000746 return Changed;
747}
748
David Blaikie2f040112014-07-25 16:10:16 +0000749static void removeDebugValues(MachineFunction &mf) {
750 for (MachineBasicBlock &MBB : mf) {
751 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
752 if (!MBBI->isDebugValue()) {
753 ++MBBI;
754 continue;
755 }
756 MBBI = MBB.erase(MBBI);
757 }
758 }
759}
760
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000761bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000762 if (!EnableLDV)
763 return false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000764 if (!mf.getFunction()->getSubprogram()) {
David Blaikie2f040112014-07-25 16:10:16 +0000765 removeDebugValues(mf);
766 return false;
767 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000768 if (!pImpl)
769 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000770 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000771}
772
773void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000774 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000775 static_cast<LDVImpl*>(pImpl)->clear();
776}
777
778LiveDebugVariables::~LiveDebugVariables() {
779 if (pImpl)
780 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000781}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000782
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000783//===----------------------------------------------------------------------===//
784// Live Range Splitting
785//===----------------------------------------------------------------------===//
786
787bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000788UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
789 LiveIntervals& LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000790 DEBUG({
791 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +0000792 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000793 });
794 bool DidChange = false;
795 LocMap::iterator LocMapI;
796 LocMapI.setMap(locInts);
797 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000798 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000799 if (LI->empty())
800 continue;
801
802 // Don't allocate the new LocNo until it is needed.
803 unsigned NewLocNo = ~0u;
804
805 // Iterate over the overlaps between locInts and LI.
806 LocMapI.find(LI->beginIndex());
807 if (!LocMapI.valid())
808 continue;
809 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
810 LiveInterval::iterator LIE = LI->end();
811 while (LocMapI.valid() && LII != LIE) {
812 // At this point, we know that LocMapI.stop() > LII->start.
813 LII = LI->advanceTo(LII, LocMapI.start());
814 if (LII == LIE)
815 break;
816
817 // Now LII->end > LocMapI.start(). Do we have an overlap?
818 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
819 // Overlapping correct location. Allocate NewLocNo now.
820 if (NewLocNo == ~0u) {
821 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
822 MO.setSubReg(locations[OldLocNo].getSubReg());
823 NewLocNo = getLocationNo(MO);
824 DidChange = true;
825 }
826
827 SlotIndex LStart = LocMapI.start();
828 SlotIndex LStop = LocMapI.stop();
829
830 // Trim LocMapI down to the LII overlap.
831 if (LStart < LII->start)
832 LocMapI.setStartUnchecked(LII->start);
833 if (LStop > LII->end)
834 LocMapI.setStopUnchecked(LII->end);
835
836 // Change the value in the overlap. This may trigger coalescing.
837 LocMapI.setValue(NewLocNo);
838
839 // Re-insert any removed OldLocNo ranges.
840 if (LStart < LocMapI.start()) {
841 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
842 ++LocMapI;
843 assert(LocMapI.valid() && "Unexpected coalescing");
844 }
845 if (LStop > LocMapI.stop()) {
846 ++LocMapI;
847 LocMapI.insert(LII->end, LStop, OldLocNo);
848 --LocMapI;
849 }
850 }
851
852 // Advance to the next overlap.
853 if (LII->end < LocMapI.stop()) {
854 if (++LII == LIE)
855 break;
856 LocMapI.advanceTo(LII->start);
857 } else {
858 ++LocMapI;
859 if (!LocMapI.valid())
860 break;
861 LII = LI->advanceTo(LII, LocMapI.start());
862 }
863 }
864 }
865
866 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
867 locations.erase(locations.begin() + OldLocNo);
868 LocMapI.goToBegin();
869 while (LocMapI.valid()) {
870 unsigned v = LocMapI.value();
871 if (v == OldLocNo) {
872 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
873 << LocMapI.stop() << ")\n");
874 LocMapI.erase();
875 } else {
876 if (v > OldLocNo)
877 LocMapI.setValueUnchecked(v-1);
878 ++LocMapI;
879 }
880 }
881
Craig Topperc0196b12014-04-14 00:51:57 +0000882 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000883 return DidChange;
884}
885
886bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000887UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
888 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000889 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000890 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +0000891 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000892 for (unsigned i = locations.size(); i ; --i) {
893 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000894 const MachineOperand *Loc = &locations[LocNo];
895 if (!Loc->isReg() || Loc->getReg() != OldReg)
896 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000897 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000898 }
899 return DidChange;
900}
901
Mark Laceyf9ea8852013-08-14 23:50:04 +0000902void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000903 bool DidChange = false;
904 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +0000905 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000906
907 if (!DidChange)
908 return;
909
910 // Map all of the new virtual registers.
911 UserValue *UV = lookupVirtReg(OldReg);
912 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +0000913 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000914}
915
916void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +0000917splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000918 if (pImpl)
919 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
920}
921
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000922void
923UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
924 // Iterate over locations in reverse makes it easier to handle coalescing.
925 for (unsigned i = locations.size(); i ; --i) {
926 unsigned LocNo = i-1;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000927 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000928 // Only virtual registers are rewritten.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000929 if (!Loc.isReg() || !Loc.getReg() ||
930 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000931 continue;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000932 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesen1a3534a2011-01-12 22:37:49 +0000933 if (VRM.isAssignedReg(VirtReg) &&
934 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000935 // This can create a %noreg operand in rare cases when the sub-register
936 // index is no longer available. That means the user value is in a
937 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000938 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesen28df7ef2011-11-13 01:23:30 +0000939 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000940 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000941 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000942 } else {
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000943 Loc.setReg(0);
944 Loc.setSubReg(0);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000945 }
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000946 coalesceLocation(LocNo);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000947 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000948}
949
Devang Patel26ffa012011-02-04 01:43:25 +0000950/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000951/// instruction.
952static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +0000953findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000954 LiveIntervals &LIS) {
955 SlotIndex Start = LIS.getMBBStartIdx(MBB);
956 Idx = Idx.getBaseIndex();
957
958 // Try to find an insert location by going backwards from Idx.
959 MachineInstr *MI;
960 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
961 // We've reached the beginning of MBB.
962 if (Idx == Start) {
963 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000964 return I;
965 }
966 Idx = Idx.getPrevIndex();
967 }
Devang Patel26ffa012011-02-04 01:43:25 +0000968
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +0000969 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000970 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000971 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000972}
973
974void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
975 unsigned LocNo,
976 LiveIntervals &LIS,
977 const TargetInstrInfo &TII) {
Devang Patel26ffa012011-02-04 01:43:25 +0000978 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000979 MachineOperand &Loc = locations[LocNo];
Devang Pateleabc3cea2011-08-04 20:42:11 +0000980 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000981
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000982 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000983 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000984 "Expected inlined-at fields to agree");
Adrian Prantl418d1d12013-07-09 20:28:37 +0000985 if (Loc.isReg())
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000986 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000987 IsIndirect, Loc.getReg(), offset, Variable, Expression);
Adrian Prantl418d1d12013-07-09 20:28:37 +0000988 else
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000989 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000990 .addOperand(Loc)
991 .addImm(offset)
992 .addMetadata(Variable)
993 .addMetadata(Expression);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000994}
995
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000996void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
997 const TargetInstrInfo &TII) {
998 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
999
1000 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1001 SlotIndex Start = I.start();
1002 SlotIndex Stop = I.stop();
1003 unsigned LocNo = I.value();
1004 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001005 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1006 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001007
1008 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001009 insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001010 // This interval may span multiple basic blocks.
1011 // Insert a DBG_VALUE into each one.
1012 while(Stop > MBBEnd) {
1013 // Move to the next block.
1014 Start = MBBEnd;
1015 if (++MBB == MFEnd)
1016 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001017 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001018 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001019 insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001020 }
1021 DEBUG(dbgs() << '\n');
1022 if (MBB == MFEnd)
1023 break;
1024
1025 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001026 }
1027}
1028
1029void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1030 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001031 if (!MF)
1032 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001033 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001034 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001035 DEBUG(userValues[i]->print(dbgs(), TRI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001036 userValues[i]->rewriteLocations(*VRM, *TRI);
1037 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
1038 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001039 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001040}
1041
1042void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001043 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001044 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1045}
1046
David Blaikie2f040112014-07-25 16:10:16 +00001047bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001048 return Pass::doInitialization(M);
1049}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001050
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001051#ifndef NDEBUG
1052void LiveDebugVariables::dump() {
1053 if (pImpl)
1054 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1055}
1056#endif