blob: 644ba232db69833c8add50ad24ceed006b097048 [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
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000022#define DEBUG_TYPE "livedebug"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000023#include "LiveDebugVariables.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000024#include "llvm/ADT/IntervalMap.h"
Devang Patelb4568662011-08-04 18:45:38 +000025#include "llvm/ADT/Statistic.h"
Devang Patel37a62052011-08-10 21:25:34 +000026#include "llvm/CodeGen/LexicalScopes.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000027#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000028#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000032#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000033#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000035#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/Metadata.h"
37#include "llvm/IR/Value.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000038#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/Debug.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"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000043
David Blaikie2b1dfa72014-04-21 20:37:07 +000044#include <memory>
45
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000046using namespace llvm;
47
Devang Patelacbee0b2011-01-07 22:33:41 +000048static cl::opt<bool>
Jakob Stoklund Olesen74ded572011-01-12 23:36:21 +000049EnableLDV("live-debug-variables", cl::init(true),
Devang Patelacbee0b2011-01-07 22:33:41 +000050 cl::desc("Enable the live debug variables pass"), cl::Hidden);
51
Devang Patelb4568662011-08-04 18:45:38 +000052STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000053char LiveDebugVariables::ID = 0;
54
55INITIALIZE_PASS_BEGIN(LiveDebugVariables, "livedebugvars",
56 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000057INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000058INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
59INITIALIZE_PASS_END(LiveDebugVariables, "livedebugvars",
60 "Debug Variable Analysis", false, false)
61
62void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000063 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000064 AU.addRequiredTransitive<LiveIntervals>();
65 AU.setPreservesAll();
66 MachineFunctionPass::getAnalysisUsage(AU);
67}
68
Craig Topperc0196b12014-04-14 00:51:57 +000069LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID), pImpl(nullptr) {
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000070 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
71}
72
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000073/// LocMap - Map of where a user value is live, and its location.
74typedef IntervalMap<SlotIndex, unsigned, 4> LocMap;
75
Benjamin Kramer67b014b2011-09-16 00:35:06 +000076namespace {
Eric Christopher9d7d5da2013-11-20 00:54:25 +000077/// UserValueScopes - Keeps track of lexical scopes associated with a
Devang Patelf9e2ae92011-09-13 18:40:53 +000078/// user value's source location.
79class UserValueScopes {
80 DebugLoc DL;
81 LexicalScopes &LS;
82 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
83
84public:
85 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(D), LS(L) {}
86
87 /// dominates - Return true if current scope dominates at least one machine
88 /// instruction in a given machine basic block.
89 bool dominates(MachineBasicBlock *MBB) {
90 if (LBlocks.empty())
91 LS.getMachineBasicBlocks(DL, LBlocks);
92 if (LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB))
93 return true;
94 return false;
95 }
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 {
111 const MDNode *variable; ///< The debug info variable we are part of.
112 unsigned offset; ///< Byte offset into variable.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000113 bool IsIndirect; ///< true if this is a register-indirect+offset value.
Devang Patel26ffa012011-02-04 01:43:25 +0000114 DebugLoc dl; ///< The debug location for the variable. This is
115 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000116 UserValue *leader; ///< Equivalence class leader.
117 UserValue *next; ///< Next value in equivalence class, or null.
118
119 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000120 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000121
122 /// Map of slot indices where this value is live.
123 LocMap locInts;
124
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000125 /// coalesceLocation - After LocNo was changed, check if it has become
126 /// identical to another location, and coalesce them. This may cause LocNo or
127 /// a later location to be erased, but no earlier location will be erased.
128 void coalesceLocation(unsigned LocNo);
129
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000130 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
131 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
132 LiveIntervals &LIS, const TargetInstrInfo &TII);
133
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000134 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
135 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000136 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
137 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000138
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000139public:
140 /// UserValue - Create a new UserValue.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000141 UserValue(const MDNode *var, unsigned o, bool i, DebugLoc L,
Devang Patel26ffa012011-02-04 01:43:25 +0000142 LocMap::Allocator &alloc)
Adrian Prantl418d1d12013-07-09 20:28:37 +0000143 : variable(var), offset(o), IsIndirect(i), dl(L), leader(this),
Craig Topperc0196b12014-04-14 00:51:57 +0000144 next(nullptr), locInts(alloc)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000145 {}
146
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?
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000159 bool match(const MDNode *Var, unsigned Offset) const {
160 return Var == variable && Offset == offset;
161 }
162
163 /// merge - Merge equivalence classes.
164 static UserValue *merge(UserValue *L1, UserValue *L2) {
165 L2 = L2->getLeader();
166 if (!L1)
167 return L2;
168 L1 = L1->getLeader();
169 if (L1 == L2)
170 return L1;
171 // Splice L2 before L1's members.
172 UserValue *End = L2;
173 while (End->next)
174 End->leader = L1, End = End->next;
175 End->leader = L1;
176 End->next = L1->next;
177 L1->next = L2;
178 return L1;
179 }
180
181 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000182 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000183 if (LocMO.isReg()) {
184 if (LocMO.getReg() == 0)
185 return ~0u;
186 // For register locations we dont care about use/def and other flags.
187 for (unsigned i = 0, e = locations.size(); i != e; ++i)
188 if (locations[i].isReg() &&
189 locations[i].getReg() == LocMO.getReg() &&
190 locations[i].getSubReg() == LocMO.getSubReg())
191 return i;
192 } else
193 for (unsigned i = 0, e = locations.size(); i != e; ++i)
194 if (LocMO.isIdenticalTo(locations[i]))
195 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000196 locations.push_back(LocMO);
197 // We are storing a MachineOperand outside a MachineInstr.
198 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000199 // Don't store def operands.
200 if (locations.back().isReg())
201 locations.back().setIsUse();
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000202 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000203 }
204
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000205 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
206 void mapVirtRegs(LDVImpl *LDV);
207
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000208 /// addDef - Add a definition point to this value.
209 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
210 // Add a singular (Idx,Idx) -> Loc mapping.
211 LocMap::iterator I = locInts.find(Idx);
212 if (!I.valid() || I.start() != Idx)
213 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000214 else
215 // A later DBG_VALUE at the same SlotIndex overrides the old location.
216 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000217 }
218
219 /// extendDef - Extend the current definition as far as possible down the
220 /// dominator tree. Stop when meeting an existing def or when leaving the live
221 /// range of VNI.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000222 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000223 /// @param Idx Starting point for the definition.
224 /// @param LocNo Location number to propagate.
Matthias Braun34e1be92013-10-10 21:29:02 +0000225 /// @param LR Restrict liveness to where LR has the value VNI. May be null.
226 /// @param VNI When LR is not null, this is the value to restrict to.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000227 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000228 /// @param LIS Live intervals analysis.
229 /// @param MDT Dominator tree.
230 void extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun34e1be92013-10-10 21:29:02 +0000231 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000232 SmallVectorImpl<SlotIndex> *Kills,
Devang Patel37a62052011-08-10 21:25:34 +0000233 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000234 UserValueScopes &UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000235
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000236 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
237 /// registers. Determine if any of the copies are available at the kill
238 /// points, and add defs if possible.
239 /// @param LI Scan for copies of the value in LI->reg.
240 /// @param LocNo Location number of LI->reg.
241 /// @param Kills Points where the range of LocNo could be extended.
242 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
243 void addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
244 const SmallVectorImpl<SlotIndex> &Kills,
245 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
246 MachineRegisterInfo &MRI,
247 LiveIntervals &LIS);
248
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000249 /// computeIntervals - Compute the live intervals of all locations after
250 /// collecting all their def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000251 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Devang Patel37a62052011-08-10 21:25:34 +0000252 LiveIntervals &LIS, MachineDominatorTree &MDT,
Devang Patelf9e2ae92011-09-13 18:40:53 +0000253 UserValueScopes &UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000254
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000255 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
256 /// live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000257 bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
258 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000259
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000260 /// rewriteLocations - Rewrite virtual register locations according to the
261 /// provided virtual register map.
262 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
263
Eric Christopherbc671702013-02-13 02:29:18 +0000264 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000265 void emitDebugValues(VirtRegMap *VRM,
266 LiveIntervals &LIS, const TargetInstrInfo &TRI);
267
Devang Patel26ffa012011-02-04 01:43:25 +0000268 /// findDebugLoc - Return DebugLoc used for this DBG_VALUE instruction. A
269 /// variable may have more than one corresponding DBG_VALUE instructions.
270 /// Only first one needs DebugLoc to identify variable's lexical scope
271 /// in source file.
272 DebugLoc findDebugLoc();
Devang Patelf9e2ae92011-09-13 18:40:53 +0000273
274 /// getDebugLoc - Return DebugLoc of this UserValue.
275 DebugLoc getDebugLoc() { return dl;}
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000276 void print(raw_ostream&, const TargetMachine*);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000277};
278} // namespace
279
280/// LDVImpl - Implementation of the LiveDebugVariables pass.
281namespace {
282class LDVImpl {
283 LiveDebugVariables &pass;
284 LocMap::Allocator allocator;
285 MachineFunction *MF;
286 LiveIntervals *LIS;
Devang Patel37a62052011-08-10 21:25:34 +0000287 LexicalScopes LS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000288 MachineDominatorTree *MDT;
289 const TargetRegisterInfo *TRI;
290
Manman Ren7a4c8a72013-02-13 20:23:48 +0000291 /// Whether emitDebugValues is called.
292 bool EmitDone;
293 /// Whether the machine function is modified during the pass.
294 bool ModifiedMF;
295
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000296 /// userValues - All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000297 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000298
299 /// Map virtual register to eq class leader.
300 typedef DenseMap<unsigned, UserValue*> VRMap;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000301 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000302
303 /// Map user variable to eq class leader.
304 typedef DenseMap<const MDNode *, UserValue*> UVMap;
305 UVMap userVarMap;
306
307 /// getUserValue - Find or create a UserValue.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000308 UserValue *getUserValue(const MDNode *Var, unsigned Offset,
309 bool IsIndirect, DebugLoc DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000310
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000311 /// lookupVirtReg - Find the EC leader for VirtReg or null.
312 UserValue *lookupVirtReg(unsigned VirtReg);
313
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000314 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
315 /// @param MI DBG_VALUE instruction
316 /// @param Idx Last valid SLotIndex before instruction.
317 /// @return True if the DBG_VALUE instruction should be deleted.
318 bool handleDebugValue(MachineInstr *MI, SlotIndex Idx);
319
320 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
321 /// a UserValue def for each instruction.
322 /// @param mf MachineFunction to be scanned.
323 /// @return True if any debug values were found.
324 bool collectDebugValues(MachineFunction &mf);
325
326 /// computeIntervals - Compute the live intervals of all user values after
327 /// collecting all their def points.
328 void computeIntervals();
329
330public:
Manman Ren7a4c8a72013-02-13 20:23:48 +0000331 LDVImpl(LiveDebugVariables *ps) : pass(*ps), EmitDone(false),
332 ModifiedMF(false) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000333 bool runOnMachineFunction(MachineFunction &mf);
334
Manman Ren7a4c8a72013-02-13 20:23:48 +0000335 /// clear - Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000336 void clear() {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000337 userValues.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000338 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000339 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000340 // Make sure we call emitDebugValues if the machine function was modified.
341 assert((!ModifiedMF || EmitDone) &&
342 "Dbg values are not emitted in LDV");
343 EmitDone = false;
344 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000345 }
346
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000347 /// mapVirtReg - Map virtual register to an equivalence class.
348 void mapVirtReg(unsigned VirtReg, UserValue *EC);
349
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000350 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000351 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000352
Eric Christopherbc671702013-02-13 02:29:18 +0000353 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000354 void emitDebugValues(VirtRegMap *VRM);
355
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000356 void print(raw_ostream&);
357};
358} // namespace
359
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000360void UserValue::print(raw_ostream &OS, const TargetMachine *TM) {
Devang Patel6c1ed312011-08-09 01:03:35 +0000361 DIVariable DV(variable);
362 OS << "!\"";
363 DV.printExtendedName(OS);
364 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000365 if (offset)
366 OS << '+' << offset;
367 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
368 OS << " [" << I.start() << ';' << I.stop() << "):";
369 if (I.value() == ~0u)
370 OS << "undef";
371 else
372 OS << I.value();
373 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000374 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
375 OS << " Loc" << i << '=';
376 locations[i].print(OS, TM);
377 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000378 OS << '\n';
379}
380
381void LDVImpl::print(raw_ostream &OS) {
382 OS << "********** DEBUG VARIABLES **********\n";
383 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000384 userValues[i]->print(OS, &MF->getTarget());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000385}
386
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000387void UserValue::coalesceLocation(unsigned LocNo) {
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000388 unsigned KeepLoc = 0;
389 for (unsigned e = locations.size(); KeepLoc != e; ++KeepLoc) {
390 if (KeepLoc == LocNo)
391 continue;
392 if (locations[KeepLoc].isIdenticalTo(locations[LocNo]))
393 break;
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000394 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000395 // No matches.
396 if (KeepLoc == locations.size())
397 return;
398
399 // Keep the smaller location, erase the larger one.
400 unsigned EraseLoc = LocNo;
401 if (KeepLoc > EraseLoc)
402 std::swap(KeepLoc, EraseLoc);
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000403 locations.erase(locations.begin() + EraseLoc);
404
405 // Rewrite values.
406 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
407 unsigned v = I.value();
408 if (v == EraseLoc)
409 I.setValue(KeepLoc); // Coalesce when possible.
410 else if (v > EraseLoc)
411 I.setValueUnchecked(v-1); // Avoid coalescing with untransformed values.
412 }
413}
414
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000415void UserValue::mapVirtRegs(LDVImpl *LDV) {
416 for (unsigned i = 0, e = locations.size(); i != e; ++i)
417 if (locations[i].isReg() &&
418 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
419 LDV->mapVirtReg(locations[i].getReg(), this);
420}
421
Devang Patel26ffa012011-02-04 01:43:25 +0000422UserValue *LDVImpl::getUserValue(const MDNode *Var, unsigned Offset,
Adrian Prantl418d1d12013-07-09 20:28:37 +0000423 bool IsIndirect, DebugLoc DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000424 UserValue *&Leader = userVarMap[Var];
425 if (Leader) {
426 UserValue *UV = Leader->getLeader();
427 Leader = UV;
428 for (; UV; UV = UV->getNext())
429 if (UV->match(Var, Offset))
430 return UV;
431 }
432
David Blaikie2b1dfa72014-04-21 20:37:07 +0000433 userValues.push_back(
434 make_unique<UserValue>(Var, Offset, IsIndirect, DL, allocator));
435 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000436 Leader = UserValue::merge(Leader, UV);
437 return UV;
438}
439
440void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
441 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000442 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000443 Leader = UserValue::merge(Leader, EC);
444}
445
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000446UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000447 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000448 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000449 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000450}
451
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000452bool LDVImpl::handleDebugValue(MachineInstr *MI, SlotIndex Idx) {
453 // DBG_VALUE loc, offset, variable
454 if (MI->getNumOperands() != 3 ||
Adrian Prantl418d1d12013-07-09 20:28:37 +0000455 !(MI->getOperand(1).isReg() || MI->getOperand(1).isImm()) ||
456 !MI->getOperand(2).isMetadata()) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000457 DEBUG(dbgs() << "Can't handle " << *MI);
458 return false;
459 }
460
461 // Get or create the UserValue for (variable,offset).
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000462 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantl418d1d12013-07-09 20:28:37 +0000463 unsigned Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000464 const MDNode *Var = MI->getOperand(2).getMetadata();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000465 //here.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000466 UserValue *UV = getUserValue(Var, Offset, IsIndirect, MI->getDebugLoc());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000467 UV->addDef(Idx, MI->getOperand(0));
468 return true;
469}
470
471bool LDVImpl::collectDebugValues(MachineFunction &mf) {
472 bool Changed = false;
473 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
474 ++MFI) {
475 MachineBasicBlock *MBB = MFI;
476 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
477 MBBI != MBBE;) {
478 if (!MBBI->isDebugValue()) {
479 ++MBBI;
480 continue;
481 }
482 // DBG_VALUE has no slot index, use the previous instruction instead.
483 SlotIndex Idx = MBBI == MBB->begin() ?
484 LIS->getMBBStartIdx(MBB) :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000485 LIS->getInstructionIndex(std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000486 // Handle consecutive DBG_VALUE instructions with the same slot index.
487 do {
488 if (handleDebugValue(MBBI, Idx)) {
489 MBBI = MBB->erase(MBBI);
490 Changed = true;
491 } else
492 ++MBBI;
493 } while (MBBI != MBBE && MBBI->isDebugValue());
494 }
495 }
496 return Changed;
497}
498
499void UserValue::extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun34e1be92013-10-10 21:29:02 +0000500 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000501 SmallVectorImpl<SlotIndex> *Kills,
Devang Patel37a62052011-08-10 21:25:34 +0000502 LiveIntervals &LIS, MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000503 UserValueScopes &UVS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000504 SmallVector<SlotIndex, 16> Todo;
505 Todo.push_back(Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000506 do {
507 SlotIndex Start = Todo.pop_back_val();
508 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
509 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen2ffee662011-01-12 23:14:04 +0000510 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000511
512 // Limit to VNI's live range.
513 bool ToEnd = true;
Matthias Braun34e1be92013-10-10 21:29:02 +0000514 if (LR && VNI) {
515 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000516 if (!Segment || Segment->valno != VNI) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000517 if (Kills)
518 Kills->push_back(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000519 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000520 }
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000521 if (Segment->end < Stop)
522 Stop = Segment->end, ToEnd = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000523 }
524
525 // There could already be a short def at Start.
526 if (I.valid() && I.start() <= Start) {
527 // Stop when meeting a different location or an already extended interval.
528 Start = Start.getNextSlot();
529 if (I.value() != LocNo || I.stop() != Start)
530 continue;
531 // This is a one-slot placeholder. Just skip it.
532 ++I;
533 }
534
535 // Limited by the next def.
536 if (I.valid() && I.start() < Stop)
537 Stop = I.start(), ToEnd = false;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000538 // Limited by VNI's live range.
539 else if (!ToEnd && Kills)
540 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000541
542 if (Start >= Stop)
543 continue;
544
545 I.insert(Start, Stop, LocNo);
546
547 // If we extended to the MBB end, propagate down the dominator tree.
548 if (!ToEnd)
549 continue;
550 const std::vector<MachineDomTreeNode*> &Children =
551 MDT.getNode(MBB)->getChildren();
Devang Patel37a62052011-08-10 21:25:34 +0000552 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
553 MachineBasicBlock *MBB = Children[i]->getBlock();
Devang Patelf9e2ae92011-09-13 18:40:53 +0000554 if (UVS.dominates(MBB))
Devang Patel37a62052011-08-10 21:25:34 +0000555 Todo.push_back(LIS.getMBBStartIdx(MBB));
556 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000557 } while (!Todo.empty());
558}
559
560void
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000561UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
562 const SmallVectorImpl<SlotIndex> &Kills,
563 SmallVectorImpl<std::pair<SlotIndex, unsigned> > &NewDefs,
564 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
565 if (Kills.empty())
566 return;
567 // Don't track copies from physregs, there are too many uses.
568 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
569 return;
570
571 // Collect all the (vreg, valno) pairs that are copies of LI.
572 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000573 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
574 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000575 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000576 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000577 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000578 unsigned DstReg = MI->getOperand(0).getReg();
579
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000580 // Don't follow copies to physregs. These are usually setting up call
581 // arguments, and the argument registers are always call clobbered. We are
582 // better off in the source register which could be a callee-saved register,
583 // or it could be spilled.
584 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
585 continue;
586
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000587 // Is LocNo extended to reach this copy? If not, another def may be blocking
588 // it, or we are looking at a wrong value of LI.
589 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000590 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000591 if (!I.valid() || I.value() != LocNo)
592 continue;
593
594 if (!LIS.hasInterval(DstReg))
595 continue;
596 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000597 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
598 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000599 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
600 }
601
602 if (CopyValues.empty())
603 return;
604
605 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
606
607 // Try to add defs of the copied values for each kill point.
608 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
609 SlotIndex Idx = Kills[i];
610 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
611 LiveInterval *DstLI = CopyValues[j].first;
612 const VNInfo *DstVNI = CopyValues[j].second;
613 if (DstLI->getVNInfoAt(Idx) != DstVNI)
614 continue;
615 // Check that there isn't already a def at Idx
616 LocMap::iterator I = locInts.find(Idx);
617 if (I.valid() && I.start() <= Idx)
618 continue;
619 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
620 << DstVNI->id << " in " << *DstLI << '\n');
621 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
622 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
623 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
624 I.insert(Idx, Idx.getNextSlot(), LocNo);
625 NewDefs.push_back(std::make_pair(Idx, LocNo));
626 break;
627 }
628 }
629}
630
631void
632UserValue::computeIntervals(MachineRegisterInfo &MRI,
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000633 const TargetRegisterInfo &TRI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000634 LiveIntervals &LIS,
Devang Patel37a62052011-08-10 21:25:34 +0000635 MachineDominatorTree &MDT,
Eric Christopher6a0c6792012-03-15 21:33:39 +0000636 UserValueScopes &UVS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000637 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
638
639 // Collect all defs to be extended (Skipping undefs).
640 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
641 if (I.value() != ~0u)
642 Defs.push_back(std::make_pair(I.start(), I.value()));
643
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000644 // Extend all defs, and possibly add new ones along the way.
645 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000646 SlotIndex Idx = Defs[i].first;
647 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000648 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000649
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000650 if (!Loc.isReg()) {
Craig Topperc0196b12014-04-14 00:51:57 +0000651 extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000652 continue;
653 }
654
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000655 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000656 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000657 LiveInterval *LI = nullptr;
658 const VNInfo *VNI = nullptr;
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000659 if (LIS.hasInterval(Loc.getReg())) {
660 LI = &LIS.getInterval(Loc.getReg());
661 VNI = LI->getVNInfoAt(Idx);
662 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000663 SmallVector<SlotIndex, 16> Kills;
Devang Patelf9e2ae92011-09-13 18:40:53 +0000664 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS, MDT, UVS);
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000665 if (LI)
666 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000667 continue;
668 }
669
670 // For physregs, use the live range of the first regunit as a guide.
671 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
Matthias Braun34e1be92013-10-10 21:29:02 +0000672 LiveRange *LR = &LIS.getRegUnit(Unit);
673 const VNInfo *VNI = LR->getVNInfoAt(Idx);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000674 // Don't track copies from physregs, it is too expensive.
Craig Topperc0196b12014-04-14 00:51:57 +0000675 extendDef(Idx, LocNo, LR, VNI, nullptr, LIS, MDT, UVS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000676 }
677
678 // Finally, erase all the undefs.
679 for (LocMap::iterator I = locInts.begin(); I.valid();)
680 if (I.value() == ~0u)
681 I.erase();
682 else
683 ++I;
684}
685
686void LDVImpl::computeIntervals() {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000687 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Devang Patelf9e2ae92011-09-13 18:40:53 +0000688 UserValueScopes UVS(userValues[i]->getDebugLoc(), LS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000689 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, *MDT, UVS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000690 userValues[i]->mapVirtRegs(this);
691 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000692}
693
694bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
695 MF = &mf;
696 LIS = &pass.getAnalysis<LiveIntervals>();
697 MDT = &pass.getAnalysis<MachineDominatorTree>();
698 TRI = mf.getTarget().getRegisterInfo();
699 clear();
Devang Patel37a62052011-08-10 21:25:34 +0000700 LS.initialize(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000701 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikiec8c29202012-08-22 17:18:53 +0000702 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000703
704 bool Changed = collectDebugValues(mf);
705 computeIntervals();
706 DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000707 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000708 return Changed;
709}
710
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000711bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000712 if (!EnableLDV)
713 return false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000714 if (!pImpl)
715 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000716 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000717}
718
719void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000720 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000721 static_cast<LDVImpl*>(pImpl)->clear();
722}
723
724LiveDebugVariables::~LiveDebugVariables() {
725 if (pImpl)
726 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000727}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000728
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000729//===----------------------------------------------------------------------===//
730// Live Range Splitting
731//===----------------------------------------------------------------------===//
732
733bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000734UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
735 LiveIntervals& LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000736 DEBUG({
737 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +0000738 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000739 });
740 bool DidChange = false;
741 LocMap::iterator LocMapI;
742 LocMapI.setMap(locInts);
743 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000744 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000745 if (LI->empty())
746 continue;
747
748 // Don't allocate the new LocNo until it is needed.
749 unsigned NewLocNo = ~0u;
750
751 // Iterate over the overlaps between locInts and LI.
752 LocMapI.find(LI->beginIndex());
753 if (!LocMapI.valid())
754 continue;
755 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
756 LiveInterval::iterator LIE = LI->end();
757 while (LocMapI.valid() && LII != LIE) {
758 // At this point, we know that LocMapI.stop() > LII->start.
759 LII = LI->advanceTo(LII, LocMapI.start());
760 if (LII == LIE)
761 break;
762
763 // Now LII->end > LocMapI.start(). Do we have an overlap?
764 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
765 // Overlapping correct location. Allocate NewLocNo now.
766 if (NewLocNo == ~0u) {
767 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
768 MO.setSubReg(locations[OldLocNo].getSubReg());
769 NewLocNo = getLocationNo(MO);
770 DidChange = true;
771 }
772
773 SlotIndex LStart = LocMapI.start();
774 SlotIndex LStop = LocMapI.stop();
775
776 // Trim LocMapI down to the LII overlap.
777 if (LStart < LII->start)
778 LocMapI.setStartUnchecked(LII->start);
779 if (LStop > LII->end)
780 LocMapI.setStopUnchecked(LII->end);
781
782 // Change the value in the overlap. This may trigger coalescing.
783 LocMapI.setValue(NewLocNo);
784
785 // Re-insert any removed OldLocNo ranges.
786 if (LStart < LocMapI.start()) {
787 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
788 ++LocMapI;
789 assert(LocMapI.valid() && "Unexpected coalescing");
790 }
791 if (LStop > LocMapI.stop()) {
792 ++LocMapI;
793 LocMapI.insert(LII->end, LStop, OldLocNo);
794 --LocMapI;
795 }
796 }
797
798 // Advance to the next overlap.
799 if (LII->end < LocMapI.stop()) {
800 if (++LII == LIE)
801 break;
802 LocMapI.advanceTo(LII->start);
803 } else {
804 ++LocMapI;
805 if (!LocMapI.valid())
806 break;
807 LII = LI->advanceTo(LII, LocMapI.start());
808 }
809 }
810 }
811
812 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
813 locations.erase(locations.begin() + OldLocNo);
814 LocMapI.goToBegin();
815 while (LocMapI.valid()) {
816 unsigned v = LocMapI.value();
817 if (v == OldLocNo) {
818 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
819 << LocMapI.stop() << ")\n");
820 LocMapI.erase();
821 } else {
822 if (v > OldLocNo)
823 LocMapI.setValueUnchecked(v-1);
824 ++LocMapI;
825 }
826 }
827
Craig Topperc0196b12014-04-14 00:51:57 +0000828 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000829 return DidChange;
830}
831
832bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000833UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
834 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000835 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000836 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +0000837 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000838 for (unsigned i = locations.size(); i ; --i) {
839 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000840 const MachineOperand *Loc = &locations[LocNo];
841 if (!Loc->isReg() || Loc->getReg() != OldReg)
842 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000843 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000844 }
845 return DidChange;
846}
847
Mark Laceyf9ea8852013-08-14 23:50:04 +0000848void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000849 bool DidChange = false;
850 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +0000851 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000852
853 if (!DidChange)
854 return;
855
856 // Map all of the new virtual registers.
857 UserValue *UV = lookupVirtReg(OldReg);
858 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +0000859 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000860}
861
862void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +0000863splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000864 if (pImpl)
865 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
866}
867
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000868void
869UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
870 // Iterate over locations in reverse makes it easier to handle coalescing.
871 for (unsigned i = locations.size(); i ; --i) {
872 unsigned LocNo = i-1;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000873 MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000874 // Only virtual registers are rewritten.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000875 if (!Loc.isReg() || !Loc.getReg() ||
876 !TargetRegisterInfo::isVirtualRegister(Loc.getReg()))
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000877 continue;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000878 unsigned VirtReg = Loc.getReg();
Jakob Stoklund Olesen1a3534a2011-01-12 22:37:49 +0000879 if (VRM.isAssignedReg(VirtReg) &&
880 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000881 // This can create a %noreg operand in rare cases when the sub-register
882 // index is no longer available. That means the user value is in a
883 // non-existent sub-register, and %noreg is exactly what we want.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000884 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
Jakob Stoklund Olesen28df7ef2011-11-13 01:23:30 +0000885 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000886 // FIXME: Translate SubIdx to a stackslot offset.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000887 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000888 } else {
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000889 Loc.setReg(0);
890 Loc.setSubReg(0);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000891 }
Jakob Stoklund Olesen44086032010-12-03 22:25:07 +0000892 coalesceLocation(LocNo);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000893 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000894}
895
Devang Patel26ffa012011-02-04 01:43:25 +0000896/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000897/// instruction.
898static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +0000899findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000900 LiveIntervals &LIS) {
901 SlotIndex Start = LIS.getMBBStartIdx(MBB);
902 Idx = Idx.getBaseIndex();
903
904 // Try to find an insert location by going backwards from Idx.
905 MachineInstr *MI;
906 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
907 // We've reached the beginning of MBB.
908 if (Idx == Start) {
909 MachineBasicBlock::iterator I = MBB->SkipPHIsAndLabels(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000910 return I;
911 }
912 Idx = Idx.getPrevIndex();
913 }
Devang Patel26ffa012011-02-04 01:43:25 +0000914
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +0000915 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000916 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000917 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000918}
919
Devang Patel26ffa012011-02-04 01:43:25 +0000920DebugLoc UserValue::findDebugLoc() {
921 DebugLoc D = dl;
922 dl = DebugLoc();
923 return D;
924}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000925void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
926 unsigned LocNo,
927 LiveIntervals &LIS,
928 const TargetInstrInfo &TII) {
Devang Patel26ffa012011-02-04 01:43:25 +0000929 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000930 MachineOperand &Loc = locations[LocNo];
Devang Pateleabc3cea2011-08-04 20:42:11 +0000931 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000932
Adrian Prantl418d1d12013-07-09 20:28:37 +0000933 if (Loc.isReg())
934 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
935 IsIndirect, Loc.getReg(), offset, variable);
936 else
937 BuildMI(*MBB, I, findDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
938 .addOperand(Loc).addImm(offset).addMetadata(variable);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000939}
940
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000941void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
942 const TargetInstrInfo &TII) {
943 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
944
945 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
946 SlotIndex Start = I.start();
947 SlotIndex Stop = I.stop();
948 unsigned LocNo = I.value();
949 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
950 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
951 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
952
953 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
954 insertDebugValue(MBB, Start, LocNo, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000955 // This interval may span multiple basic blocks.
956 // Insert a DBG_VALUE into each one.
957 while(Stop > MBBEnd) {
958 // Move to the next block.
959 Start = MBBEnd;
960 if (++MBB == MFEnd)
961 break;
962 MBBEnd = LIS.getMBBEndIdx(MBB);
963 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
964 insertDebugValue(MBB, Start, LocNo, LIS, TII);
965 }
966 DEBUG(dbgs() << '\n');
967 if (MBB == MFEnd)
968 break;
969
970 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000971 }
972}
973
974void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
975 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
976 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
977 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000978 DEBUG(userValues[i]->print(dbgs(), &MF->getTarget()));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000979 userValues[i]->rewriteLocations(*VRM, *TRI);
980 userValues[i]->emitDebugValues(VRM, *LIS, *TII);
981 }
Manman Ren7a4c8a72013-02-13 20:23:48 +0000982 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000983}
984
985void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000986 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000987 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
988}
989
990
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000991#ifndef NDEBUG
992void LiveDebugVariables::dump() {
993 if (pImpl)
994 static_cast<LDVImpl*>(pImpl)->print(dbgs());
995}
996#endif