blob: dc936a3f5b85952f176c08b956979de26f32f0e6 [file] [log] [blame]
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +00001//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveDebugVariables analysis.
11//
12// Remove all DBG_VALUE instructions referencing virtual registers and replace
13// them with a data structure tracking where live user variables are kept - in a
14// virtual register or in a stack slot.
15//
16// Allow the data structure to be updated during register allocation when values
17// are moved between registers and stack slots. Finally emit new DBG_VALUE
18// instructions after register allocation is complete.
19//
20//===----------------------------------------------------------------------===//
21
22#include "LiveDebugVariables.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000023#include "llvm/ADT/IntervalMap.h"
Devang Patelad90d3a2011-08-04 18:45:38 +000024#include "llvm/ADT/Statistic.h"
Devang Patelc722c3d2011-08-10 21:25:34 +000025#include "llvm/CodeGen/LexicalScopes.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000026#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000027#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000031#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000032#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/Constants.h"
Stephen Hines36b56882014-04-23 16:57:46 -070034#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000035#include "llvm/IR/Metadata.h"
36#include "llvm/IR/Value.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000037#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +000039#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000040#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000041#include "llvm/Target/TargetRegisterInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080042#include "llvm/Target/TargetSubtargetInfo.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070043#include <memory>
44
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000045using namespace llvm;
46
Stephen Hinesdce4a402014-05-29 02:49:00 -070047#define DEBUG_TYPE "livedebug"
48
Devang Patel51a666f2011-01-07 22:33:41 +000049static cl::opt<bool>
Jakob Stoklund Olesen25dc2262011-01-12 23:36:21 +000050EnableLDV("live-debug-variables", cl::init(true),
Devang Patel51a666f2011-01-07 22:33:41 +000051 cl::desc("Enable the live debug variables pass"), cl::Hidden);
52
Devang Patelad90d3a2011-08-04 18:45:38 +000053STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000054char LiveDebugVariables::ID = 0;
55
56INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
57 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000058INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000059INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
60INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
61 "Debug Variable Analysis", false, false)
62
63void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000064 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000065 AU.addRequiredTransitive<LiveIntervals>();
66 AU.setPreservesAll();
67 MachineFunctionPass::getAnalysisUsage(AU);
68}
69
Stephen Hinesdce4a402014-05-29 02:49:00 -070070LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(nullptr) {
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +000071 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
72}
73
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +000074/// LocMap - Map of where a user value is live, and its location.
75typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
76
Benjamin Kramer76f58d22011-09-16 00:35:06 +000077namespace {
Stephen Hines36b56882014-04-23 16:57:46 -070078/// UserValueScopes - Keeps track of lexical scopes associated with a
Devang Patel3a2d80d2011-09-13 18:40:53 +000079/// user value's source location.
80class UserValueScopes {
81 DebugLoc DL;
82 LexicalScopes &LS;
83 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
84
85public:
86 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {}
87
88 /// dominates - Return true if current scope dominates at least one machine
89 /// instruction in a given machine basic block.
90 bool dominates(MachineBasicBlock *MBB) {
91 if (LBlocks.empty())
92 LS.getMachineBasicBlocks(DL, LBlocks);
93 if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB))
94 return true;
95 return false;
96 }
97};
Benjamin Kramer76f58d22011-09-16 00:35:06 +000098} // end anonymous namespace
Devang Patel3a2d80d2011-09-13 18:40:53 +000099
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000100/// UserValue - A user value is a part of a debug info user variable.
101///
102/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
103/// holds part of a user variable. The part is identified by a byte offset.
104///
105/// UserValues are grouped into equivalence classes for easier searching. Two
106/// user values are related if they refer to the same variable, or if they are
107/// held by the same virtual register. The equivalence class is the transitive
108/// closure of that relation.
109namespace {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000110class LDVImpl;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000111class UserValue {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800112 const MDNode *Variable; ///< The debug info variable we are part of.
113 const MDNode *Expression; ///< Any complex address expression.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000114 unsigned offset; ///< Byte offset into variable.
Adrian Prantl35176402013-07-09 20:28:37 +0000115 bool IsIndirect; ///< true if this is a register-indirect+offset value.
Devang Patelf827cd72011-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 Olesen06135162010-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 Olesen0804ead2011-01-09 05:33:21 +0000122 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000123
124 /// Map of slot indices where this value is live.
125 LocMap locInts;
126
Jakob Stoklund Olesen5daec222010-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 Olesen42acf062010-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 Olesenf42b6612011-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 Lacey1feb5852013-08-14 23:50:04 +0000138 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
139 LiveIntervals &LIS);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000140
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000141public:
142 /// UserValue - Create a new UserValue.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800143 UserValue(const MDNode *var, const MDNode *expr, unsigned o, bool i,
144 DebugLoc L, LocMap::Allocator &alloc)
145 : Variable(var), Expression(expr), offset(o), IsIndirect(i), dl(L),
146 leader(this), next(nullptr), locInts(alloc) {}
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000147
148 /// getLeader - Get the leader of this value's equivalence class.
149 UserValue *getLeader() {
150 UserValue *l = leader;
151 while (l != l->leader)
152 l = l->leader;
153 return leader = l;
154 }
155
156 /// getNext - Return the next UserValue in the equivalence class.
157 UserValue *getNext() const { return next; }
158
Devang Patela462d6e2011-07-06 23:09:51 +0000159 /// match - Does this UserValue match the parameters?
Stephen Hines37ed9c12014-12-01 14:51:49 -0800160 bool match(const MDNode *Var, const MDNode *Expr, unsigned Offset,
161 bool indirect) const {
162 return Var == Variable && Expr == Expression && Offset == offset &&
163 indirect == IsIndirect;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000164 }
165
166 /// merge - Merge equivalence classes.
167 static UserValue *merge(UserValue *L1, UserValue *L2) {
168 L2 = L2->getLeader();
169 if (!L1)
170 return L2;
171 L1 = L1->getLeader();
172 if (L1 == L2)
173 return L1;
174 // Splice L2 before L1's members.
175 UserValue *End = L2;
176 while (End->next)
177 End->leader = L1, End = End->next;
178 End->leader = L1;
179 End->next = L1->next;
180 L1->next = L2;
181 return L1;
182 }
183
184 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000185 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000186 if (LocMO.isReg()) {
187 if (LocMO.getReg() == 0)
188 return ~0u;
189 // For register locations we dont care about use/def and other flags.
190 for (unsigned i = 0, e = locations.size(); i != e; ++i)
191 if (locations[i].isReg() &&
192 locations[i].getReg() == LocMO.getReg() &&
193 locations[i].getSubReg() == LocMO.getSubReg())
194 return i;
195 } else
196 for (unsigned i = 0, e = locations.size(); i != e; ++i)
197 if (LocMO.isIdenticalTo(locations[i]))
198 return i;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000199 locations.push_back(LocMO);
200 // We are storing a MachineOperand outside a MachineInstr.
201 locations.back().clearParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000202 // Don't store def operands.
203 if (locations.back().isReg())
204 locations.back().setIsUse();
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000205 return locations.size() - 1;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000206 }
207
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000208 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
209 void mapVirtRegs(LDVImpl *LDV);
210
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000211 /// addDef - Add a definition point to this value.
212 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
213 // Add a singular (Idx,Idx) -> Loc mapping.
214 LocMap::iterator I = locInts.find(Idx);
215 if (!I.valid() || I.start() != Idx)
216 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen79513ed2011-08-03 23:44:31 +0000217 else
218 // A later DBG_VALUE at the same SlotIndex overrides the old location.
219 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000220 }
221
222 /// extendDef - Extend the current definition as far as possible down the
223 /// dominator tree. Stop when meeting an existing def or when leaving the live
224 /// range of VNI.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000225 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000226 /// @param Idx Starting point for the definition.
227 /// @param LocNo Location number to propagate.
Matthias Braun4f3b5e82013-10-10 21:29:02 +0000228 /// @param LR Restrict liveness to where LR has the value VNI. May be null.
229 /// @param VNI When LR is not null, this is the value to restrict to.
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000230 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000231 /// @param LIS Live intervals analysis.
232 /// @param MDT Dominator tree.
233 void extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun4f3b5e82013-10-10 21:29:02 +0000234 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000235 SmallVectorImpl<SlotIndex> *Kills,
Devang Patelc722c3d2011-08-10 21:25:34 +0000236 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopherb82062f2012-03-15 21:33:39 +0000237 UserValueScopes &UVS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000238
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000239 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
240 /// registers. Determine if any of the copies are available at the kill
241 /// points, and add defs if possible.
242 /// @param LI Scan for copies of the value in LI->reg.
243 /// @param LocNo Location number of LI->reg.
244 /// @param Kills Points where the range of LocNo could be extended.
245 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
246 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
247 const SmallVectorImpl<SlotIndex> &Kills,
248 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
249 MachineRegisterInfo &MRI,
250 LiveIntervals &LIS);
251
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000252 /// computeIntervals - Compute the live intervals of all locations after
253 /// collecting all their def points.
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000254 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Devang Patelc722c3d2011-08-10 21:25:34 +0000255 LiveIntervals &LIS, MachineDominatorTree &MDT,
Devang Patel3a2d80d2011-09-13 18:40:53 +0000256 UserValueScopes &UVS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000257
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000258 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
259 /// live. Returns true if any changes were made.
Mark Lacey1feb5852013-08-14 23:50:04 +0000260 bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
261 LiveIntervals &LIS);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000262
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000263 /// rewriteLocations - Rewrite virtual register locations according to the
264 /// provided virtual register map.
265 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
266
Eric Christopherb3cecdf2013-02-13 02:29:18 +0000267 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000268 void emitDebugValues(VirtRegMap *VRM,
269 LiveIntervals &LIS, const TargetInstrInfo &TRI);
270
Devang Patelf827cd72011-02-04 01:43:25 +0000271 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
Stephen Hines37ed9c12014-12-01 14:51:49 -0800272 /// variable may have more than one corresponding DBG_VALUE instructions.
Devang Patelf827cd72011-02-04 01:43:25 +0000273 /// Only first one needs DebugLoc to identify variable's lexical scope
274 /// in source file.
275 DebugLoc findDebugLoc();
Devang Patel3a2d80d2011-09-13 18:40:53 +0000276
277 /// getDebugLoc - Return DebugLoc of this UserValue.
278 DebugLoc getDebugLoc() { return dl;}
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000279 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000280};
281} // namespace
282
283/// LDVImpl - Implementation of the LiveDebugVariables pass.
284namespace {
285class LDVImpl {
286 LiveDebugVariables &pass;
287 LocMap::Allocator allocator;
288 MachineFunction *MF;
289 LiveIntervals *LIS;
Devang Patelc722c3d2011-08-10 21:25:34 +0000290 LexicalScopes LS;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000291 MachineDominatorTree *MDT;
292 const TargetRegisterInfo *TRI;
293
Manman Renf0986202013-02-13 20:23:48 +0000294 /// Whether emitDebugValues is called.
295 bool EmitDone;
296 /// Whether the machine function is modified during the pass.
297 bool ModifiedMF;
298
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000299 /// userValues - All allocated UserValue instances.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700300 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000301
302 /// Map virtual register to eq class leader.
303 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000304 VRMap virtRegToEqClass;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000305
306 /// Map user variable to eq class leader.
307 typedef DenseMap<const MDNode *, UserValue*> UVMap;
308 UVMap userVarMap;
309
310 /// getUserValue - Find or create a UserValue.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800311 UserValue *getUserValue(const MDNode *Var, const MDNode *Expr,
312 unsigned Offset, bool IsIndirect, DebugLoc DL);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000313
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000314 /// lookupVirtReg - Find the EC leader for VirtReg or null.
315 UserValue *lookupVirtReg(unsigned VirtReg);
316
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000317 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
318 /// @param MI DBG_VALUE instruction
319 /// @param Idx Last valid SLotIndex before instruction.
320 /// @return True if the DBG_VALUE instruction should be deleted.
321 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
322
323 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
324 /// a UserValue def for each instruction.
325 /// @param mf MachineFunction to be scanned.
326 /// @return True if any debug values were found.
327 bool collectDebugValues(MachineFunction &mf);
328
329 /// computeIntervals - Compute the live intervals of all user values after
330 /// collecting all their def points.
331 void computeIntervals();
332
333public:
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700334 LDVImpl(LiveDebugVariables *ps)
335 : pass(*ps), MF(nullptr), EmitDone(false), ModifiedMF(false) {}
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000336 bool runOnMachineFunction(MachineFunction &mf);
337
Manman Renf0986202013-02-13 20:23:48 +0000338 /// clear - Release all memory.
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000339 void clear() {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700340 MF = nullptr;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000341 userValues.clear();
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000342 virtRegToEqClass.clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000343 userVarMap.clear();
Manman Renf0986202013-02-13 20:23:48 +0000344 // Make sure we call emitDebugValues if the machine function was modified.
345 assert((!ModifiedMF || EmitDone) &&
346 "Dbg values are not emitted in LDV");
347 EmitDone = false;
348 ModifiedMF = false;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800349 LS.reset();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000350 }
351
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000352 /// mapVirtReg - Map virtual register to an equivalence class.
353 void mapVirtReg(unsigned VirtReg, UserValue *EC);
354
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000355 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Lacey1feb5852013-08-14 23:50:04 +0000356 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000357
Eric Christopherb3cecdf2013-02-13 02:29:18 +0000358 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000359 void emitDebugValues(VirtRegMap *VRM);
360
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000361 void print(raw_ostream&);
362};
363} // namespace
364
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000365void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800366 DIVariable DV(Variable);
367 OS << "!\"";
Devang Patela2b552d2011-08-09 01:03:35 +0000368 DV.printExtendedName(OS);
369 OS << "\"\t";
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000370 if (offset)
371 OS << '+' << offset;
372 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
373 OS << " [" << I.start() << ';' << I.stop() << "):";
374 if (I.value() == ~0u)
375 OS << "undef";
376 else
377 OS << I.value();
378 }
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000379 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
380 OS << " Loc" << i << '=';
381 locations[i].print(OS, TM);
382 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000383 OS << '\n';
384}
385
386void LDVImpl::print(raw_ostream &OS) {
387 OS << "********** DEBUG VARIABLES **********\n";
388 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesene77150b2011-05-06 17:59:59 +0000389 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000390}
391
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000392void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000393 unsigned KeepLoc = 0;
394 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
395 if (KeepLoc == LocNo)
396 continue;
397 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
398 break;
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000399 }
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000400 // No matches.
401 if (KeepLoc == locations.size())
402 return;
403
404 // Keep the smaller location, erase the larger one.
405 unsigned EraseLoc = LocNo;
406 if (KeepLoc > EraseLoc)
407 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000408 locations.erase(locations.begin() + EraseLoc);
409
410 // Rewrite values.
411 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
412 unsigned v = I.value();
413 if (v == EraseLoc)
414 I.setValue(KeepLoc); // Coalesce when possible.
415 else if (v > EraseLoc)
416 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
417 }
418}
419
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000420void UserValue::mapVirtRegs(LDVImpl *LDV) {
421 for (unsigned i = 0, e = locations.size(); i != e; ++i)
422 if (locations[i].isReg() &&
423 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
424 LDV->mapVirtReg(locations[i].getReg(), this);
425}
426
Stephen Hines37ed9c12014-12-01 14:51:49 -0800427UserValue *LDVImpl::getUserValue(const MDNode *Var, const MDNode *Expr,
428 unsigned Offset, bool IsIndirect,
429 DebugLoc DL) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000430 UserValue *&Leader = userVarMap[Var];
431 if (Leader) {
432 UserValue *UV = Leader->getLeader();
433 Leader = UV;
434 for (; UV; UV = UV->getNext())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800435 if (UV->match(Var, Expr, Offset, IsIndirect))
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000436 return UV;
437 }
438
Stephen Hinesdce4a402014-05-29 02:49:00 -0700439 userValues.push_back(
Stephen Hines37ed9c12014-12-01 14:51:49 -0800440 make_unique<UserValue>(Var, Expr, Offset, IsIndirect, DL, allocator));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700441 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000442 Leader = UserValue::merge(Leader, UV);
443 return UV;
444}
445
446void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
447 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000448 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000449 Leader = UserValue::merge(Leader, EC);
450}
451
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000452UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen6ed4c6a2010-12-03 22:25:09 +0000453 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000454 return UV->getLeader();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700455 return nullptr;
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000456}
457
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000458bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
459 // DBG_VALUE loc, offset, variable
Stephen Hines37ed9c12014-12-01 14:51:49 -0800460 if (MI->getNumOperands() != 4 ||
Adrian Prantl35176402013-07-09 20:28:37 +0000461 !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) ||
462 !MI->getOperand(2).isMetadata()) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000463 DEBUG(dbgs() << "Can't handle " << *MI);
464 return false;
465 }
466
467 // Get or create the UserValue for (variable,offset).
Adrian Prantl818833f2013-09-16 23:29:03 +0000468 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantl35176402013-07-09 20:28:37 +0000469 unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800470 const MDNode *Var = MI->getDebugVariable();
471 const MDNode *Expr = MI->getDebugExpression();
Adrian Prantl818833f2013-09-16 23:29:03 +0000472 //here.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800473 UserValue *UV =
474 getUserValue(Var, Expr, Offset, IsIndirect, MI->getDebugLoc());
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000475 UV->addDef(Idx, MI->getOperand(0));
476 return true;
477}
478
479bool LDVImpl::collectDebugValues(MachineFunction &mf) {
480 bool Changed = false;
481 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
482 ++MFI) {
483 MachineBasicBlock *MBB = MFI;
484 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
485 MBBI != MBBE;) {
486 if (!MBBI->isDebugValue()) {
487 ++MBBI;
488 continue;
489 }
490 // DBG_VALUE has no slot index, use the previous instruction instead.
491 SlotIndex Idx = MBBI == MBB->begin() ?
492 LIS->getMBBStartIdx(MBB) :
Stephen Hines36b56882014-04-23 16:57:46 -0700493 LIS->getInstructionIndex(std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000494 // Handle consecutive DBG_VALUE instructions with the same slot index.
495 do {
496 if (handleDebugValue(MBBI, Idx)) {
497 MBBI = MBB->erase(MBBI);
498 Changed = true;
499 } else
500 ++MBBI;
501 } while (MBBI != MBBE && MBBI->isDebugValue());
502 }
503 }
504 return Changed;
505}
506
507void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun4f3b5e82013-10-10 21:29:02 +0000508 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000509 SmallVectorImpl<SlotIndex> *Kills,
Devang Patelc722c3d2011-08-10 21:25:34 +0000510 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopherb82062f2012-03-15 21:33:39 +0000511 UserValueScopes &UVS) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000512 SmallVector<SlotIndex, 16> Todo;
513 Todo.push_back(Idx);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000514 do {
515 SlotIndex Start = Todo.pop_back_val();
516 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
517 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen12a40312011-01-12 23:14:04 +0000518 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000519
520 // Limit to VNI's live range.
521 bool ToEnd = true;
Matthias Braun4f3b5e82013-10-10 21:29:02 +0000522 if (LR && VNI) {
523 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
Matthias Braun331de112013-10-10 21:28:43 +0000524 if (!Segment || Segment->valno != VNI) {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000525 if (Kills)
526 Kills->push_back(Start);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000527 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000528 }
Matthias Braun331de112013-10-10 21:28:43 +0000529 if (Segment->end < Stop)
530 Stop = Segment->end, ToEnd = false;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000531 }
532
533 // There could already be a short def at Start.
534 if (I.valid() && I.start() <= Start) {
535 // Stop when meeting a different location or an already extended interval.
536 Start = Start.getNextSlot();
537 if (I.value() != LocNo || I.stop() != Start)
538 continue;
539 // This is a one-slot placeholder. Just skip it.
540 ++I;
541 }
542
543 // Limited by the next def.
544 if (I.valid() && I.start() < Stop)
545 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000546 // Limited by VNI's live range.
547 else if (!ToEnd && Kills)
548 Kills->push_back(Stop);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000549
550 if (Start >= Stop)
551 continue;
552
553 I.insert(Start, Stop, LocNo);
554
555 // If we extended to the MBB end, propagate down the dominator tree.
556 if (!ToEnd)
557 continue;
558 const std::vector<MachineDomTreeNode*> &Children =
559 MDT.getNode(MBB)->getChildren();
Devang Patelc722c3d2011-08-10 21:25:34 +0000560 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
561 MachineBasicBlock *MBB = Children[i]->getBlock();
Devang Patel3a2d80d2011-09-13 18:40:53 +0000562 if (UVS.dominates(MBB))
Devang Patelc722c3d2011-08-10 21:25:34 +0000563 Todo.push_back(LIS.getMBBStartIdx(MBB));
564 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000565 } while (!Todo.empty());
566}
567
568void
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000569UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
570 const SmallVectorImpl<SlotIndex> &Kills,
571 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
572 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
573 if (Kills.empty())
574 return;
575 // Don't track copies from physregs, there are too many uses.
576 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
577 return;
578
579 // Collect all the (vreg, valno) pairs that are copies of LI.
580 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Stephen Hines36b56882014-04-23 16:57:46 -0700581 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
582 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000583 // Copies of the full value.
Stephen Hines36b56882014-04-23 16:57:46 -0700584 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000585 continue;
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000586 unsigned DstReg = MI->getOperand(0).getReg();
587
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000588 // Don't follow copies to physregs. These are usually setting up call
589 // arguments, and the argument registers are always call clobbered. We are
590 // better off in the source register which could be a callee-saved register,
591 // or it could be spilled.
592 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
593 continue;
594
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000595 // Is LocNo extended to reach this copy? If not, another def may be blocking
596 // it, or we are looking at a wrong value of LI.
597 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000598 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000599 if (!I.valid() || I.value() != LocNo)
600 continue;
601
602 if (!LIS.hasInterval(DstReg))
603 continue;
604 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000605 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
606 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000607 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
608 }
609
610 if (CopyValues.empty())
611 return;
612
613 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
614
615 // Try to add defs of the copied values for each kill point.
616 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
617 SlotIndex Idx = Kills[i];
618 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
619 LiveInterval *DstLI = CopyValues[j].first;
620 const VNInfo *DstVNI = CopyValues[j].second;
621 if (DstLI->getVNInfoAt(Idx) != DstVNI)
622 continue;
623 // Check that there isn't already a def at Idx
624 LocMap::iterator I = locInts.find(Idx);
625 if (I.valid() && I.start() <= Idx)
626 continue;
627 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
628 << DstVNI->id << " in " << *DstLI << '\n');
629 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
630 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
631 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
632 I.insert(Idx, Idx.getNextSlot(), LocNo);
633 NewDefs.push_back(std::make_pair(Idx, LocNo));
634 break;
635 }
636 }
637}
638
639void
640UserValue::computeIntervals(MachineRegisterInfo &MRI,
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000641 const TargetRegisterInfo &TRI,
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000642 LiveIntervals &LIS,
Devang Patelc722c3d2011-08-10 21:25:34 +0000643 MachineDominatorTree &MDT,
Eric Christopherb82062f2012-03-15 21:33:39 +0000644 UserValueScopes &UVS) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000645 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
646
647 // Collect all defs to be extended (Skipping undefs).
648 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
649 if (I.value() != ~0u)
650 Defs.push_back(std::make_pair(I.start(), I.value()));
651
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000652 // Extend all defs, and possibly add new ones along the way.
653 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000654 SlotIndex Idx = Defs[i].first;
655 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000656 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000657
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000658 if (!Loc.isReg()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700659 extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000660 continue;
661 }
662
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000663 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000664 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700665 LiveInterval *LI = nullptr;
666 const VNInfo *VNI = nullptr;
Jakob Stoklund Olesenb1509302012-06-22 18:51:35 +0000667 if (LIS.hasInterval(Loc.getReg())) {
668 LI = &LIS.getInterval(Loc.getReg());
669 VNI = LI->getVNInfoAt(Idx);
670 }
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000671 SmallVector<SlotIndex, 16> Kills;
Devang Patel3a2d80d2011-09-13 18:40:53 +0000672 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
Jakob Stoklund Olesenb1509302012-06-22 18:51:35 +0000673 if (LI)
674 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000675 continue;
676 }
677
678 // For physregs, use the live range of the first regunit as a guide.
679 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
Matthias Braun4f3b5e82013-10-10 21:29:02 +0000680 LiveRange *LR = &LIS.getRegUnit(Unit);
681 const VNInfo *VNI = LR->getVNInfoAt(Idx);
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000682 // Don't track copies from physregs, it is too expensive.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700683 extendDef(Idx, LocNo, LR, VNI, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000684 }
685
686 // Finally, erase all the undefs.
687 for (LocMap::iterator I = locInts.begin(); I.valid();)
688 if (I.value() == ~0u)
689 I.erase();
690 else
691 ++I;
692}
693
694void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000695 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Devang Patel3a2d80d2011-09-13 18:40:53 +0000696 UserValueScopes UVS(userValues[i]->getDebugLoc(), LS);
Jakob Stoklund Olesene8a0a122012-06-22 17:15:32 +0000697 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
Jakob Stoklund Olesen1744e472011-03-18 21:42:19 +0000698 userValues[i]->mapVirtRegs(this);
699 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000700}
701
702bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700703 clear();
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000704 MF = &mf;
705 LIS = &pass.getAnalysis<LiveIntervals>();
706 MDT = &pass.getAnalysis<MachineDominatorTree>();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800707 TRI = mf.getSubtarget().getRegisterInfo();
Devang Patelc722c3d2011-08-10 21:25:34 +0000708 LS.initialize(mf);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000709 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikie986d76d2012-08-22 17:18:53 +0000710 << mf.getName() << " **********\n");
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000711
712 bool Changed = collectDebugValues(mf);
713 computeIntervals();
714 DEBUG(print(dbgs()));
Manman Renf0986202013-02-13 20:23:48 +0000715 ModifiedMF = Changed;
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000716 return Changed;
717}
718
Stephen Hines37ed9c12014-12-01 14:51:49 -0800719static void removeDebugValues(MachineFunction &mf) {
720 for (MachineBasicBlock &MBB : mf) {
721 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
722 if (!MBBI->isDebugValue()) {
723 ++MBBI;
724 continue;
725 }
726 MBBI = MBB.erase(MBBI);
727 }
728 }
729}
730
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000731bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patel51a666f2011-01-07 22:33:41 +0000732 if (!EnableLDV)
733 return false;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800734 if (!FunctionDIs.count(mf.getFunction())) {
735 removeDebugValues(mf);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700736 return false;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800737 }
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000738 if (!pImpl)
739 pImpl = new LDVImpl(this);
Manman Renf0986202013-02-13 20:23:48 +0000740 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000741}
742
743void LiveDebugVariables::releaseMemory() {
Manman Renf0986202013-02-13 20:23:48 +0000744 if (pImpl)
Jakob Stoklund Olesen06135162010-12-02 00:37:37 +0000745 static_cast<LDVImpl*>(pImpl)->clear();
746}
747
748LiveDebugVariables::~LiveDebugVariables() {
749 if (pImpl)
750 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesenbb7b23f2010-11-30 02:17:10 +0000751}
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +0000752
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000753//===----------------------------------------------------------------------===//
754// Live Range Splitting
755//===----------------------------------------------------------------------===//
756
757bool
Mark Lacey1feb5852013-08-14 23:50:04 +0000758UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
759 LiveIntervals& LIS) {
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000760 DEBUG({
761 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Stephen Hinesdce4a402014-05-29 02:49:00 -0700762 print(dbgs(), nullptr);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000763 });
764 bool DidChange = false;
765 LocMap::iterator LocMapI;
766 LocMapI.setMap(locInts);
767 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000768 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000769 if (LI->empty())
770 continue;
771
772 // Don't allocate the new LocNo until it is needed.
773 unsigned NewLocNo = ~0u;
774
775 // Iterate over the overlaps between locInts and LI.
776 LocMapI.find(LI->beginIndex());
777 if (!LocMapI.valid())
778 continue;
779 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
780 LiveInterval::iterator LIE = LI->end();
781 while (LocMapI.valid() && LII != LIE) {
782 // At this point, we know that LocMapI.stop() > LII->start.
783 LII = LI->advanceTo(LII, LocMapI.start());
784 if (LII == LIE)
785 break;
786
787 // Now LII->end > LocMapI.start(). Do we have an overlap?
788 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
789 // Overlapping correct location. Allocate NewLocNo now.
790 if (NewLocNo == ~0u) {
791 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
792 MO.setSubReg(locations[OldLocNo].getSubReg());
793 NewLocNo = getLocationNo(MO);
794 DidChange = true;
795 }
796
797 SlotIndex LStart = LocMapI.start();
798 SlotIndex LStop = LocMapI.stop();
799
800 // Trim LocMapI down to the LII overlap.
801 if (LStart < LII->start)
802 LocMapI.setStartUnchecked(LII->start);
803 if (LStop > LII->end)
804 LocMapI.setStopUnchecked(LII->end);
805
806 // Change the value in the overlap. This may trigger coalescing.
807 LocMapI.setValue(NewLocNo);
808
809 // Re-insert any removed OldLocNo ranges.
810 if (LStart < LocMapI.start()) {
811 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
812 ++LocMapI;
813 assert(LocMapI.valid() && "Unexpected coalescing");
814 }
815 if (LStop > LocMapI.stop()) {
816 ++LocMapI;
817 LocMapI.insert(LII->end, LStop, OldLocNo);
818 --LocMapI;
819 }
820 }
821
822 // Advance to the next overlap.
823 if (LII->end < LocMapI.stop()) {
824 if (++LII == LIE)
825 break;
826 LocMapI.advanceTo(LII->start);
827 } else {
828 ++LocMapI;
829 if (!LocMapI.valid())
830 break;
831 LII = LI->advanceTo(LII, LocMapI.start());
832 }
833 }
834 }
835
836 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
837 locations.erase(locations.begin() + OldLocNo);
838 LocMapI.goToBegin();
839 while (LocMapI.valid()) {
840 unsigned v = LocMapI.value();
841 if (v == OldLocNo) {
842 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
843 << LocMapI.stop() << ")\n");
844 LocMapI.erase();
845 } else {
846 if (v > OldLocNo)
847 LocMapI.setValueUnchecked(v-1);
848 ++LocMapI;
849 }
850 }
851
Stephen Hinesdce4a402014-05-29 02:49:00 -0700852 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000853 return DidChange;
854}
855
856bool
Mark Lacey1feb5852013-08-14 23:50:04 +0000857UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
858 LiveIntervals &LIS) {
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000859 bool DidChange = false;
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000860 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopher7cc51772012-03-15 21:33:35 +0000861 // safely erase unused locations.
Jakob Stoklund Olesen6212f9a2011-05-06 19:31:19 +0000862 for (unsigned i = locations.size(); i ; --i) {
863 unsigned LocNo = i-1;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000864 const MachineOperand *Loc = &locations[LocNo];
865 if (!Loc->isReg() || Loc->getReg() != OldReg)
866 continue;
Mark Lacey1feb5852013-08-14 23:50:04 +0000867 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000868 }
869 return DidChange;
870}
871
Mark Lacey1feb5852013-08-14 23:50:04 +0000872void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000873 bool DidChange = false;
874 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Lacey1feb5852013-08-14 23:50:04 +0000875 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000876
877 if (!DidChange)
878 return;
879
880 // Map all of the new virtual registers.
881 UserValue *UV = lookupVirtReg(OldReg);
882 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Lacey1feb5852013-08-14 23:50:04 +0000883 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000884}
885
886void LiveDebugVariables::
Mark Lacey1feb5852013-08-14 23:50:04 +0000887splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000888 if (pImpl)
889 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
890}
891
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000892void
893UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
894 // Iterate over locations in reverse makes it easier to handle coalescing.
895 for (unsigned i = locations.size(); i ; --i) {
896 unsigned LocNo = i-1;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000897 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000898 // Only virtual registers are rewritten.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000899 if (!Loc.isReg() || !Loc.getReg() ||
900 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000901 continue;
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000902 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesenf2036272011-01-12 22:37:49 +0000903 if (VRM.isAssignedReg(VirtReg) &&
904 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +0000905 // This can create a %noreg operand in rare cases when the sub-register
906 // index is no longer available. That means the user value is in a
907 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000908 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesencb390642011-11-13 01:23:30 +0000909 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000910 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000911 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000912 } else {
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000913 Loc.setReg(0);
914 Loc.setSubReg(0);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000915 }
Jakob Stoklund Olesen5daec222010-12-03 22:25:07 +0000916 coalesceLocation(LocNo);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000917 }
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000918}
919
Devang Patelf827cd72011-02-04 01:43:25 +0000920/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000921/// instruction.
922static MachineBasicBlock::iterator
Devang Patelf827cd72011-02-04 01:43:25 +0000923findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000924 LiveIntervals &LIS) {
925 SlotIndex Start = LIS.getMBBStartIdx(MBB);
926 Idx = Idx.getBaseIndex();
927
928 // Try to find an insert location by going backwards from Idx.
929 MachineInstr *MI;
930 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
931 // We've reached the beginning of MBB.
932 if (Idx == Start) {
933 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000934 return I;
935 }
936 Idx = Idx.getPrevIndex();
937 }
Devang Patelf827cd72011-02-04 01:43:25 +0000938
Jakob Stoklund Oleseneea666f2011-01-13 23:35:53 +0000939 // Don't insert anything after the first terminator, though.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000940 return MI->isTerminator() ? MBB->getFirstTerminator() :
Stephen Hines36b56882014-04-23 16:57:46 -0700941 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000942}
943
Devang Patelf827cd72011-02-04 01:43:25 +0000944DebugLoc UserValue::findDebugLoc() {
945 DebugLoc D = dl;
946 dl = DebugLoc();
947 return D;
948}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000949void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
950 unsigned LocNo,
951 LiveIntervals &LIS,
952 const TargetInstrInfo &TII) {
Devang Patelf827cd72011-02-04 01:43:25 +0000953 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen0804ead2011-01-09 05:33:21 +0000954 MachineOperand &Loc = locations[LocNo];
Devang Pateld9f3fc72011-08-04 20:42:11 +0000955 ++NumInsertedDebugValues;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000956
Adrian Prantl35176402013-07-09 20:28:37 +0000957 if (Loc.isReg())
958 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
Stephen Hines37ed9c12014-12-01 14:51:49 -0800959 IsIndirect, Loc.getReg(), offset, Variable, Expression);
Adrian Prantl35176402013-07-09 20:28:37 +0000960 else
961 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Stephen Hines37ed9c12014-12-01 14:51:49 -0800962 .addOperand(Loc)
963 .addImm(offset)
964 .addMetadata(Variable)
965 .addMetadata(Expression);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000966}
967
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000968void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
969 const TargetInstrInfo &TII) {
970 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
971
972 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
973 SlotIndex Start = I.start();
974 SlotIndex Stop = I.stop();
975 unsigned LocNo = I.value();
976 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
977 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
978 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
979
980 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
981 insertDebugValue(MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000982 // This interval may span multiple basic blocks.
983 // Insert a DBG_VALUE into each one.
984 while(Stop > MBBEnd) {
985 // Move to the next block.
986 Start = MBBEnd;
987 if (++MBB == MFEnd)
988 break;
989 MBBEnd = LIS.getMBBEndIdx(MBB);
990 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
991 insertDebugValue(MBB, Start, LocNo, LIS, TII);
992 }
993 DEBUG(dbgs() << '\n');
994 if (MBB == MFEnd)
995 break;
996
997 ++I;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +0000998 }
999}
1000
1001void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1002 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001003 if (!MF)
1004 return;
Stephen Hines37ed9c12014-12-01 14:51:49 -08001005 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +00001006 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesencf724f02011-05-08 19:21:08 +00001007 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +00001008 userValues[i]->rewriteLocations(*VRM, *TRI);
1009 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
1010 }
Manman Renf0986202013-02-13 20:23:48 +00001011 EmitDone = true;
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +00001012}
1013
1014void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Renf0986202013-02-13 20:23:48 +00001015 if (pImpl)
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +00001016 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1017}
1018
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001019bool LiveDebugVariables::doInitialization(Module &M) {
1020 FunctionDIs = makeSubprogramMap(M);
1021 return Pass::doInitialization(M);
1022}
Jakob Stoklund Olesen42acf062010-12-03 21:47:10 +00001023
Jakob Stoklund Olesen30e21282010-12-02 18:15:44 +00001024#ifndef NDEBUG
1025void LiveDebugVariables::dump() {
1026 if (pImpl)
1027 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1028}
1029#endif