blob: 9a82e3c42ddd48e0f5db231a0070270e17fb8877 [file] [log] [blame]
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +00001//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveDebugVariables analysis.
11//
12// Remove all DBG_VALUE instructions referencing virtual registers and replace
13// them with a data structure tracking where live user variables are kept - in a
14// virtual register or in a stack slot.
15//
16// Allow the data structure to be updated during register allocation when values
17// are moved between registers and stack slots. Finally emit new DBG_VALUE
18// instructions after register allocation is complete.
19//
20//===----------------------------------------------------------------------===//
21
22#include "LiveDebugVariables.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000023#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/DenseMap.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000025#include "llvm/ADT/IntervalMap.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000026#include "llvm/ADT/STLExtras.h"
Robert Lougher10f740d2017-08-03 11:54:02 +000027#include "llvm/ADT/SmallSet.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000028#include "llvm/ADT/SmallVector.h"
Devang Patelb4568662011-08-04 18:45:38 +000029#include "llvm/ADT/Statistic.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000030#include "llvm/ADT/StringRef.h"
Robert Lougher10f740d2017-08-03 11:54:02 +000031#include "llvm/CodeGen/LexicalScopes.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000032#include "llvm/CodeGen/LiveInterval.h"
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000033#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000034#include "llvm/CodeGen/MachineBasicBlock.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000035#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000036#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000037#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000038#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000039#include "llvm/CodeGen/MachineOperand.h"
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +000040#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000041#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000042#include "llvm/CodeGen/VirtRegMap.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000043#include "llvm/IR/DebugInfoMetadata.h"
44#include "llvm/IR/DebugLoc.h"
45#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Metadata.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000047#include "llvm/MC/MCRegisterInfo.h"
48#include "llvm/Pass.h"
49#include "llvm/Support/Casting.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000050#include "llvm/Support/CommandLine.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000051#include "llvm/Support/Compiler.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000052#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000053#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000054#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000055#include "llvm/Target/TargetOpcodes.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000056#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000057#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000058#include <algorithm>
59#include <cassert>
60#include <iterator>
David Blaikie2b1dfa72014-04-21 20:37:07 +000061#include <memory>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000062#include <utility>
David Blaikie2b1dfa72014-04-21 20:37:07 +000063
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000064using namespace llvm;
65
Matthias Braun1527baa2017-05-25 21:26:32 +000066#define DEBUG_TYPE "livedebugvars"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000067
Devang Patelacbee0b2011-01-07 22:33:41 +000068static cl::opt<bool>
Jakob Stoklund Olesen74ded572011-01-12 23:36:21 +000069EnableLDV("live-debug-variables", cl::init(true),
Devang Patelacbee0b2011-01-07 22:33:41 +000070 cl::desc("Enable the live debug variables pass"), cl::Hidden);
71
Devang Patelb4568662011-08-04 18:45:38 +000072STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Eugene Zelenko5df3d892017-08-24 21:21:39 +000073
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000074char LiveDebugVariables::ID = 0;
75
Matthias Braun1527baa2017-05-25 21:26:32 +000076INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000077 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000078INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000079INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Matthias Braun1527baa2017-05-25 21:26:32 +000080INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000081 "Debug Variable Analysis", false, false)
82
83void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000084 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000085 AU.addRequiredTransitive<LiveIntervals>();
86 AU.setPreservesAll();
87 MachineFunctionPass::getAnalysisUsage(AU);
88}
89
Eugene Zelenko5df3d892017-08-24 21:21:39 +000090LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000091 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
92}
93
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000094/// LocMap - Map of where a user value is live, and its location.
Eugene Zelenko5df3d892017-08-24 21:21:39 +000095using LocMap = IntervalMap<SlotIndex, unsigned, 4>;
96
97namespace {
98
99class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000100
101/// UserValue - A user value is a part of a debug info user variable.
102///
103/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
104/// holds part of a user variable. The part is identified by a byte offset.
105///
106/// UserValues are grouped into equivalence classes for easier searching. Two
107/// user values are related if they refer to the same variable, or if they are
108/// held by the same virtual register. The equivalence class is the transitive
109/// closure of that relation.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000110class UserValue {
Reid Kleckner4e040282017-09-20 18:19:08 +0000111 const DILocalVariable *Variable; ///< The debug info variable we are part of.
112 const DIExpression *Expression; ///< Any complex address expression.
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.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000117 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000118
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
Robert Lougher10f740d2017-08-03 11:54:02 +0000125 /// Set of interval start indexes that have been trimmed to the
126 /// lexical scope.
127 SmallSet<SlotIndex, 2> trimmedDefs;
128
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000129 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
Reid Kleckner4e040282017-09-20 18:19:08 +0000130 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
131 unsigned LocNo, bool Spilled, LiveIntervals &LIS,
132 const TargetInstrInfo &TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000133
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.
Reid Kleckner4e040282017-09-20 18:19:08 +0000141 UserValue(const DILocalVariable *var, const DIExpression *expr, bool i,
142 DebugLoc L, LocMap::Allocator &alloc)
Adrian Prantlb2811e52017-07-28 23:06:50 +0000143 : Variable(var), Expression(expr), IsIndirect(i), dl(std::move(L)),
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000144 leader(this), locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000145
146 /// getLeader - Get the leader of this value's equivalence class.
147 UserValue *getLeader() {
148 UserValue *l = leader;
149 while (l != l->leader)
150 l = l->leader;
151 return leader = l;
152 }
153
154 /// getNext - Return the next UserValue in the equivalence class.
155 UserValue *getNext() const { return next; }
156
Devang Patel338e4322011-07-06 23:09:51 +0000157 /// match - Does this UserValue match the parameters?
Reid Kleckner4e040282017-09-20 18:19:08 +0000158 bool match(const DILocalVariable *Var, const DIExpression *Expr,
159 const DILocation *IA, bool indirect) const {
Duncan P. N. Exon Smith7bb480d2015-04-16 22:27:54 +0000160 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA &&
Adrian Prantlb2811e52017-07-28 23:06:50 +0000161 indirect == IsIndirect;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000162 }
163
Reid Klecknereed09732017-09-15 22:08:50 +0000164 enum : unsigned { UndefLocNo = ~0U };
165
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000166 /// 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;
Richard Trieu7a083812016-02-18 22:09:30 +0000176 while (End->next) {
177 End->leader = L1;
178 End = End->next;
179 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000180 End->leader = L1;
181 End->next = L1->next;
182 L1->next = L2;
183 return L1;
184 }
185
186 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000187 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000188 if (LocMO.isReg()) {
189 if (LocMO.getReg() == 0)
Reid Klecknereed09732017-09-15 22:08:50 +0000190 return UndefLocNo;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000191 // For register locations we dont care about use/def and other flags.
192 for (unsigned i = 0, e = locations.size(); i != e; ++i)
193 if (locations[i].isReg() &&
194 locations[i].getReg() == LocMO.getReg() &&
195 locations[i].getSubReg() == LocMO.getSubReg())
196 return i;
197 } else
198 for (unsigned i = 0, e = locations.size(); i != e; ++i)
199 if (LocMO.isIdenticalTo(locations[i]))
200 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000201 locations.push_back(LocMO);
202 // We are storing a MachineOperand outside a MachineInstr.
203 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000204 // Don't store def operands.
205 if (locations.back().isReg())
206 locations.back().setIsUse();
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000207 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000208 }
209
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000210 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
211 void mapVirtRegs(LDVImpl *LDV);
212
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000213 /// addDef - Add a definition point to this value.
214 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
215 // Add a singular (Idx,Idx) -> Loc mapping.
216 LocMap::iterator I = locInts.find(Idx);
217 if (!I.valid() || I.start() != Idx)
218 I.insert(Idx, Idx.getNextSlot(), getLocationNo(LocMO));
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000219 else
220 // A later DBG_VALUE at the same SlotIndex overrides the old location.
221 I.setValue(getLocationNo(LocMO));
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000222 }
223
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000224 /// extendDef - Extend the current definition as far as possible down.
225 /// Stop when meeting an existing def or when leaving the live
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000226 /// range of VNI.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000227 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000228 /// @param Idx Starting point for the definition.
229 /// @param LocNo Location number to propagate.
Matthias Braun34e1be92013-10-10 21:29:02 +0000230 /// @param LR Restrict liveness to where LR has the value VNI. May be null.
231 /// @param VNI When LR is not null, this is the value to restrict to.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000232 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000233 /// @param LIS Live intervals analysis.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000234 void extendDef(SlotIndex Idx, unsigned LocNo,
Matthias Braun34e1be92013-10-10 21:29:02 +0000235 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000236 SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000237 LiveIntervals &LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000238
Jakob Stoklund Olesen816f5f42011-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,
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000247 const SmallVectorImpl<SlotIndex> &Kills,
248 SmallVectorImpl<std::pair<SlotIndex, unsigned>> &NewDefs,
249 MachineRegisterInfo &MRI,
250 LiveIntervals &LIS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000251
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000252 /// computeIntervals - Compute the live intervals of all locations after
253 /// collecting all their def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000254 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Robert Lougher10f740d2017-08-03 11:54:02 +0000255 LiveIntervals &LIS, LexicalScopes &LS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000256
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000257 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
258 /// live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000259 bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
260 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000261
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000262 /// rewriteLocations - Rewrite virtual register locations according to the
Reid Kleckner4e040282017-09-20 18:19:08 +0000263 /// provided virtual register map. Record which locations were spilled.
264 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
265 BitVector &SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000266
Eric Christopherbc671702013-02-13 02:29:18 +0000267 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Reid Kleckner4e040282017-09-20 18:19:08 +0000268 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
269 const TargetInstrInfo &TRI,
270 const BitVector &SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000271
Devang Patelf9e2ae92011-09-13 18:40:53 +0000272 /// getDebugLoc - Return DebugLoc of this UserValue.
273 DebugLoc getDebugLoc() { return dl;}
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000274
Eric Christopher1cdefae2015-02-27 00:11:34 +0000275 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000276};
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000277
278/// LDVImpl - Implementation of the LiveDebugVariables pass.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000279class LDVImpl {
280 LiveDebugVariables &pass;
281 LocMap::Allocator allocator;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000282 MachineFunction *MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000283 LiveIntervals *LIS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000284 const TargetRegisterInfo *TRI;
285
Manman Ren7a4c8a72013-02-13 20:23:48 +0000286 /// Whether emitDebugValues is called.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000287 bool EmitDone = false;
288
Manman Ren7a4c8a72013-02-13 20:23:48 +0000289 /// Whether the machine function is modified during the pass.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000290 bool ModifiedMF = false;
Manman Ren7a4c8a72013-02-13 20:23:48 +0000291
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000292 /// userValues - All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000293 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000294
295 /// Map virtual register to eq class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000296 using VRMap = DenseMap<unsigned, UserValue *>;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000297 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000298
299 /// Map user variable to eq class leader.
Reid Kleckner4e040282017-09-20 18:19:08 +0000300 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000301 UVMap userVarMap;
302
303 /// getUserValue - Find or create a UserValue.
Reid Kleckner4e040282017-09-20 18:19:08 +0000304 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
Adrian Prantlb2811e52017-07-28 23:06:50 +0000305 bool IsIndirect, const DebugLoc &DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000306
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000307 /// lookupVirtReg - Find the EC leader for VirtReg or null.
308 UserValue *lookupVirtReg(unsigned VirtReg);
309
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000310 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
311 /// @param MI DBG_VALUE instruction
312 /// @param Idx Last valid SLotIndex before instruction.
313 /// @return True if the DBG_VALUE instruction should be deleted.
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000314 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000315
316 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
317 /// a UserValue def for each instruction.
318 /// @param mf MachineFunction to be scanned.
319 /// @return True if any debug values were found.
320 bool collectDebugValues(MachineFunction &mf);
321
322 /// computeIntervals - Compute the live intervals of all user values after
323 /// collecting all their def points.
324 void computeIntervals();
325
326public:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000327 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
328
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000329 bool runOnMachineFunction(MachineFunction &mf);
330
Manman Ren7a4c8a72013-02-13 20:23:48 +0000331 /// clear - Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000332 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000333 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000334 userValues.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000335 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000336 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000337 // Make sure we call emitDebugValues if the machine function was modified.
338 assert((!ModifiedMF || EmitDone) &&
339 "Dbg values are not emitted in LDV");
340 EmitDone = false;
341 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000342 }
343
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000344 /// mapVirtReg - Map virtual register to an equivalence class.
345 void mapVirtReg(unsigned VirtReg, UserValue *EC);
346
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000347 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000348 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000349
Eric Christopherbc671702013-02-13 02:29:18 +0000350 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000351 void emitDebugValues(VirtRegMap *VRM);
352
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000353 void print(raw_ostream&);
354};
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000355
356} // end anonymous namespace
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000357
Don Hinton53eb6372017-09-27 21:19:56 +0000358#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000359static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000360 const LLVMContext &Ctx) {
361 if (!DL)
362 return;
363
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000364 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000365 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000366 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000367 CommentOS << ':' << DL.getLine();
368 if (DL.getCol() != 0)
369 CommentOS << ':' << DL.getCol();
370
371 DebugLoc InlinedAtDL = DL.getInlinedAt();
372 if (!InlinedAtDL)
373 return;
374
375 CommentOS << " @[ ";
376 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
377 CommentOS << " ]";
378}
379
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000380static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
381 const DILocation *DL) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000382 const LLVMContext &Ctx = V->getContext();
383 StringRef Res = V->getName();
384 if (!Res.empty())
385 OS << Res << "," << V->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000386 if (auto *InlinedAt = DL->getInlinedAt()) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000387 if (DebugLoc InlinedAtDL = InlinedAt) {
388 OS << " @[";
389 printDebugLoc(InlinedAtDL, OS, Ctx);
390 OS << "]";
391 }
392 }
393}
394
Eric Christopher1cdefae2015-02-27 00:11:34 +0000395void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000396 auto *DV = cast<DILocalVariable>(Variable);
Frederic Risse6bb1872014-08-07 20:04:00 +0000397 OS << "!\"";
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000398 printExtendedName(OS, DV, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000399
Devang Patel6c1ed312011-08-09 01:03:35 +0000400 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000401 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
402 OS << " [" << I.start() << ';' << I.stop() << "):";
Reid Klecknereed09732017-09-15 22:08:50 +0000403 if (I.value() == UndefLocNo)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000404 OS << "undef";
405 else
406 OS << I.value();
407 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000408 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
409 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000410 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000411 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000412 OS << '\n';
413}
414
415void LDVImpl::print(raw_ostream &OS) {
416 OS << "********** DEBUG VARIABLES **********\n";
417 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Eric Christopher1cdefae2015-02-27 00:11:34 +0000418 userValues[i]->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000419}
Florian Hahn6b3216a2017-07-31 10:07:49 +0000420#endif
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000421
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000422void UserValue::mapVirtRegs(LDVImpl *LDV) {
423 for (unsigned i = 0, e = locations.size(); i != e; ++i)
424 if (locations[i].isReg() &&
425 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
426 LDV->mapVirtReg(locations[i].getReg(), this);
427}
428
Reid Kleckner4e040282017-09-20 18:19:08 +0000429UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
430 const DIExpression *Expr, bool IsIndirect,
431 const DebugLoc &DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000432 UserValue *&Leader = userVarMap[Var];
433 if (Leader) {
434 UserValue *UV = Leader->getLeader();
435 Leader = UV;
436 for (; UV; UV = UV->getNext())
Adrian Prantlb2811e52017-07-28 23:06:50 +0000437 if (UV->match(Var, Expr, DL->getInlinedAt(), IsIndirect))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000438 return UV;
439 }
440
David Blaikie2b1dfa72014-04-21 20:37:07 +0000441 userValues.push_back(
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000442 llvm::make_unique<UserValue>(Var, Expr, IsIndirect, DL, allocator));
David Blaikie2b1dfa72014-04-21 20:37:07 +0000443 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000444 Leader = UserValue::merge(Leader, UV);
445 return UV;
446}
447
448void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
449 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000450 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000451 Leader = UserValue::merge(Leader, EC);
452}
453
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000454UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000455 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000456 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000457 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000458}
459
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000460bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000461 // DBG_VALUE loc, offset, variable
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000462 if (MI.getNumOperands() != 4 ||
463 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
464 !MI.getOperand(2).isMetadata()) {
465 DEBUG(dbgs() << "Can't handle " << MI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000466 return false;
467 }
468
469 // Get or create the UserValue for (variable,offset).
Reid Kleckner4e040282017-09-20 18:19:08 +0000470 bool IsIndirect = MI.getOperand(1).isImm();
Adrian Prantlb2811e52017-07-28 23:06:50 +0000471 if (IsIndirect)
472 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
Reid Kleckner4e040282017-09-20 18:19:08 +0000473 const DILocalVariable *Var = MI.getDebugVariable();
474 const DIExpression *Expr = MI.getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000475 //here.
Adrian Prantlb2811e52017-07-28 23:06:50 +0000476 UserValue *UV = getUserValue(Var, Expr, IsIndirect, MI.getDebugLoc());
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000477 UV->addDef(Idx, MI.getOperand(0));
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000478 return true;
479}
480
481bool LDVImpl::collectDebugValues(MachineFunction &mf) {
482 bool Changed = false;
483 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
484 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000485 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000486 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
487 MBBI != MBBE;) {
488 if (!MBBI->isDebugValue()) {
489 ++MBBI;
490 continue;
491 }
492 // DBG_VALUE has no slot index, use the previous instruction instead.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000493 SlotIndex Idx =
494 MBBI == MBB->begin()
495 ? LIS->getMBBStartIdx(MBB)
496 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000497 // Handle consecutive DBG_VALUE instructions with the same slot index.
498 do {
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000499 if (handleDebugValue(*MBBI, Idx)) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000500 MBBI = MBB->erase(MBBI);
501 Changed = true;
502 } else
503 ++MBBI;
504 } while (MBBI != MBBE && MBBI->isDebugValue());
505 }
506 }
507 return Changed;
508}
509
Adrian Prantlce858132015-12-21 20:03:00 +0000510/// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
511/// data-flow analysis to propagate them beyond basic block boundaries.
512void UserValue::extendDef(SlotIndex Idx, unsigned LocNo, LiveRange *LR,
513 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000514 LiveIntervals &LIS) {
Adrian Prantlce858132015-12-21 20:03:00 +0000515 SlotIndex Start = Idx;
516 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
517 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
518 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000519
Adrian Prantlce858132015-12-21 20:03:00 +0000520 // Limit to VNI's live range.
521 bool ToEnd = true;
522 if (LR && VNI) {
523 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
524 if (!Segment || Segment->valno != VNI) {
525 if (Kills)
526 Kills->push_back(Start);
527 return;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000528 }
Richard Trieu7a083812016-02-18 22:09:30 +0000529 if (Segment->end < Stop) {
530 Stop = Segment->end;
531 ToEnd = false;
532 }
Adrian Prantlce858132015-12-21 20:03:00 +0000533 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000534
Adrian Prantlce858132015-12-21 20:03:00 +0000535 // There could already be a short def at Start.
536 if (I.valid() && I.start() <= Start) {
537 // Stop when meeting a different location or an already extended interval.
538 Start = Start.getNextSlot();
539 if (I.value() != LocNo || I.stop() != Start)
540 return;
541 // This is a one-slot placeholder. Just skip it.
542 ++I;
543 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000544
Adrian Prantlce858132015-12-21 20:03:00 +0000545 // Limited by the next def.
Richard Trieu7a083812016-02-18 22:09:30 +0000546 if (I.valid() && I.start() < Stop) {
547 Stop = I.start();
548 ToEnd = false;
549 }
Adrian Prantlce858132015-12-21 20:03:00 +0000550 // Limited by VNI's live range.
551 else if (!ToEnd && Kills)
552 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000553
Adrian Prantlce858132015-12-21 20:03:00 +0000554 if (Start < Stop)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000555 I.insert(Start, Stop, LocNo);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000556}
557
558void
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000559UserValue::addDefsFromCopies(LiveInterval *LI, unsigned LocNo,
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000560 const SmallVectorImpl<SlotIndex> &Kills,
561 SmallVectorImpl<std::pair<SlotIndex, unsigned>> &NewDefs,
562 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000563 if (Kills.empty())
564 return;
565 // Don't track copies from physregs, there are too many uses.
566 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
567 return;
568
569 // Collect all the (vreg, valno) pairs that are copies of LI.
570 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000571 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
572 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000573 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000574 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000575 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000576 unsigned DstReg = MI->getOperand(0).getReg();
577
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000578 // Don't follow copies to physregs. These are usually setting up call
579 // arguments, and the argument registers are always call clobbered. We are
580 // better off in the source register which could be a callee-saved register,
581 // or it could be spilled.
582 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
583 continue;
584
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000585 // Is LocNo extended to reach this copy? If not, another def may be blocking
586 // it, or we are looking at a wrong value of LI.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000587 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000588 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000589 if (!I.valid() || I.value() != LocNo)
590 continue;
591
592 if (!LIS.hasInterval(DstReg))
593 continue;
594 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000595 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
596 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000597 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
598 }
599
600 if (CopyValues.empty())
601 return;
602
603 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
604
605 // Try to add defs of the copied values for each kill point.
606 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
607 SlotIndex Idx = Kills[i];
608 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
609 LiveInterval *DstLI = CopyValues[j].first;
610 const VNInfo *DstVNI = CopyValues[j].second;
611 if (DstLI->getVNInfoAt(Idx) != DstVNI)
612 continue;
613 // Check that there isn't already a def at Idx
614 LocMap::iterator I = locInts.find(Idx);
615 if (I.valid() && I.start() <= Idx)
616 continue;
617 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
618 << DstVNI->id << " in " << *DstLI << '\n');
619 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
620 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
621 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
622 I.insert(Idx, Idx.getNextSlot(), LocNo);
623 NewDefs.push_back(std::make_pair(Idx, LocNo));
624 break;
625 }
626 }
627}
628
Robert Lougher10f740d2017-08-03 11:54:02 +0000629void UserValue::computeIntervals(MachineRegisterInfo &MRI,
630 const TargetRegisterInfo &TRI,
631 LiveIntervals &LIS, LexicalScopes &LS) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000632 SmallVector<std::pair<SlotIndex, unsigned>, 16> Defs;
633
634 // Collect all defs to be extended (Skipping undefs).
635 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
Reid Klecknereed09732017-09-15 22:08:50 +0000636 if (I.value() != UndefLocNo)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000637 Defs.push_back(std::make_pair(I.start(), I.value()));
638
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000639 // Extend all defs, and possibly add new ones along the way.
640 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000641 SlotIndex Idx = Defs[i].first;
642 unsigned LocNo = Defs[i].second;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000643 const MachineOperand &Loc = locations[LocNo];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000644
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000645 if (!Loc.isReg()) {
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000646 extendDef(Idx, LocNo, nullptr, nullptr, nullptr, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000647 continue;
648 }
649
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000650 // Register locations are constrained to where the register value is live.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000651 if (TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000652 LiveInterval *LI = nullptr;
653 const VNInfo *VNI = nullptr;
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000654 if (LIS.hasInterval(Loc.getReg())) {
655 LI = &LIS.getInterval(Loc.getReg());
656 VNI = LI->getVNInfoAt(Idx);
657 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000658 SmallVector<SlotIndex, 16> Kills;
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000659 extendDef(Idx, LocNo, LI, VNI, &Kills, LIS);
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000660 if (LI)
661 addDefsFromCopies(LI, LocNo, Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000662 continue;
663 }
664
665 // For physregs, use the live range of the first regunit as a guide.
666 unsigned Unit = *MCRegUnitIterator(Loc.getReg(), &TRI);
Matthias Braun34e1be92013-10-10 21:29:02 +0000667 LiveRange *LR = &LIS.getRegUnit(Unit);
668 const VNInfo *VNI = LR->getVNInfoAt(Idx);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000669 // Don't track copies from physregs, it is too expensive.
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000670 extendDef(Idx, LocNo, LR, VNI, nullptr, LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000671 }
672
Robert Lougher10f740d2017-08-03 11:54:02 +0000673 // Erase all the undefs.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000674 for (LocMap::iterator I = locInts.begin(); I.valid();)
Reid Klecknereed09732017-09-15 22:08:50 +0000675 if (I.value() == UndefLocNo)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000676 I.erase();
677 else
678 ++I;
Robert Lougher10f740d2017-08-03 11:54:02 +0000679
680 // The computed intervals may extend beyond the range of the debug
681 // location's lexical scope. In this case, splitting of an interval
682 // can result in an interval outside of the scope being created,
683 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
684 // this, trim the intervals to the lexical scope.
685
686 LexicalScope *Scope = LS.findLexicalScope(dl);
687 if (!Scope)
688 return;
689
690 SlotIndex PrevEnd;
691 LocMap::iterator I = locInts.begin();
692
693 // Iterate over the lexical scope ranges. Each time round the loop
694 // we check the intervals for overlap with the end of the previous
695 // range and the start of the next. The first range is handled as
696 // a special case where there is no PrevEnd.
697 for (const InsnRange &Range : Scope->getRanges()) {
698 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
699 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
700
701 // At the start of each iteration I has been advanced so that
702 // I.stop() >= PrevEnd. Check for overlap.
703 if (PrevEnd && I.start() < PrevEnd) {
704 SlotIndex IStop = I.stop();
705 unsigned LocNo = I.value();
706
707 // Stop overlaps previous end - trim the end of the interval to
708 // the scope range.
709 I.setStopUnchecked(PrevEnd);
710 ++I;
711
712 // If the interval also overlaps the start of the "next" (i.e.
713 // current) range create a new interval for the remainder (which
714 // may be further trimmed).
715 if (RStart < IStop)
716 I.insert(RStart, IStop, LocNo);
717 }
718
719 // Advance I so that I.stop() >= RStart, and check for overlap.
720 I.advanceTo(RStart);
721 if (!I.valid())
722 return;
723
724 if (I.start() < RStart) {
725 // Interval start overlaps range - trim to the scope range.
726 I.setStartUnchecked(RStart);
727 // Remember that this interval was trimmed.
728 trimmedDefs.insert(RStart);
729 }
730
731 // The end of a lexical scope range is the last instruction in the
732 // range. To convert to an interval we need the index of the
733 // instruction after it.
734 REnd = REnd.getNextIndex();
735
736 // Advance I to first interval outside current range.
737 I.advanceTo(REnd);
738 if (!I.valid())
739 return;
740
741 PrevEnd = REnd;
742 }
743
744 // Check for overlap with end of final range.
745 if (PrevEnd && I.start() < PrevEnd)
746 I.setStopUnchecked(PrevEnd);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000747}
748
749void LDVImpl::computeIntervals() {
Robert Lougher10f740d2017-08-03 11:54:02 +0000750 LexicalScopes LS;
751 LS.initialize(*MF);
752
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000753 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Robert Lougher10f740d2017-08-03 11:54:02 +0000754 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000755 userValues[i]->mapVirtRegs(this);
756 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000757}
758
759bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000760 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000761 MF = &mf;
762 LIS = &pass.getAnalysis<LiveIntervals>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000763 TRI = mf.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000764 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikiec8c29202012-08-22 17:18:53 +0000765 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000766
767 bool Changed = collectDebugValues(mf);
768 computeIntervals();
769 DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000770 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000771 return Changed;
772}
773
David Blaikie2f040112014-07-25 16:10:16 +0000774static void removeDebugValues(MachineFunction &mf) {
775 for (MachineBasicBlock &MBB : mf) {
776 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
777 if (!MBBI->isDebugValue()) {
778 ++MBBI;
779 continue;
780 }
781 MBBI = MBB.erase(MBBI);
782 }
783 }
784}
785
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000786bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000787 if (!EnableLDV)
788 return false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000789 if (!mf.getFunction()->getSubprogram()) {
David Blaikie2f040112014-07-25 16:10:16 +0000790 removeDebugValues(mf);
791 return false;
792 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000793 if (!pImpl)
794 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000795 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000796}
797
798void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000799 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000800 static_cast<LDVImpl*>(pImpl)->clear();
801}
802
803LiveDebugVariables::~LiveDebugVariables() {
804 if (pImpl)
805 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000806}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000807
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000808//===----------------------------------------------------------------------===//
809// Live Range Splitting
810//===----------------------------------------------------------------------===//
811
812bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000813UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
814 LiveIntervals& LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000815 DEBUG({
816 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +0000817 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000818 });
819 bool DidChange = false;
820 LocMap::iterator LocMapI;
821 LocMapI.setMap(locInts);
822 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000823 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000824 if (LI->empty())
825 continue;
826
827 // Don't allocate the new LocNo until it is needed.
Reid Klecknereed09732017-09-15 22:08:50 +0000828 unsigned NewLocNo = UndefLocNo;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000829
830 // Iterate over the overlaps between locInts and LI.
831 LocMapI.find(LI->beginIndex());
832 if (!LocMapI.valid())
833 continue;
834 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
835 LiveInterval::iterator LIE = LI->end();
836 while (LocMapI.valid() && LII != LIE) {
837 // At this point, we know that LocMapI.stop() > LII->start.
838 LII = LI->advanceTo(LII, LocMapI.start());
839 if (LII == LIE)
840 break;
841
842 // Now LII->end > LocMapI.start(). Do we have an overlap?
843 if (LocMapI.value() == OldLocNo && LII->start < LocMapI.stop()) {
844 // Overlapping correct location. Allocate NewLocNo now.
Reid Klecknereed09732017-09-15 22:08:50 +0000845 if (NewLocNo == UndefLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000846 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
847 MO.setSubReg(locations[OldLocNo].getSubReg());
848 NewLocNo = getLocationNo(MO);
849 DidChange = true;
850 }
851
852 SlotIndex LStart = LocMapI.start();
853 SlotIndex LStop = LocMapI.stop();
854
855 // Trim LocMapI down to the LII overlap.
856 if (LStart < LII->start)
857 LocMapI.setStartUnchecked(LII->start);
858 if (LStop > LII->end)
859 LocMapI.setStopUnchecked(LII->end);
860
861 // Change the value in the overlap. This may trigger coalescing.
862 LocMapI.setValue(NewLocNo);
863
864 // Re-insert any removed OldLocNo ranges.
865 if (LStart < LocMapI.start()) {
866 LocMapI.insert(LStart, LocMapI.start(), OldLocNo);
867 ++LocMapI;
868 assert(LocMapI.valid() && "Unexpected coalescing");
869 }
870 if (LStop > LocMapI.stop()) {
871 ++LocMapI;
872 LocMapI.insert(LII->end, LStop, OldLocNo);
873 --LocMapI;
874 }
875 }
876
877 // Advance to the next overlap.
878 if (LII->end < LocMapI.stop()) {
879 if (++LII == LIE)
880 break;
881 LocMapI.advanceTo(LII->start);
882 } else {
883 ++LocMapI;
884 if (!LocMapI.valid())
885 break;
886 LII = LI->advanceTo(LII, LocMapI.start());
887 }
888 }
889 }
890
891 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
892 locations.erase(locations.begin() + OldLocNo);
893 LocMapI.goToBegin();
894 while (LocMapI.valid()) {
895 unsigned v = LocMapI.value();
896 if (v == OldLocNo) {
897 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
898 << LocMapI.stop() << ")\n");
899 LocMapI.erase();
900 } else {
901 if (v > OldLocNo)
902 LocMapI.setValueUnchecked(v-1);
903 ++LocMapI;
904 }
905 }
906
Craig Topperc0196b12014-04-14 00:51:57 +0000907 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000908 return DidChange;
909}
910
911bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000912UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
913 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000914 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000915 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +0000916 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000917 for (unsigned i = locations.size(); i ; --i) {
918 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000919 const MachineOperand *Loc = &locations[LocNo];
920 if (!Loc->isReg() || Loc->getReg() != OldReg)
921 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000922 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000923 }
924 return DidChange;
925}
926
Mark Laceyf9ea8852013-08-14 23:50:04 +0000927void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000928 bool DidChange = false;
929 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +0000930 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000931
932 if (!DidChange)
933 return;
934
935 // Map all of the new virtual registers.
936 UserValue *UV = lookupVirtReg(OldReg);
937 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +0000938 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000939}
940
941void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +0000942splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000943 if (pImpl)
944 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
945}
946
Reid Kleckner4e040282017-09-20 18:19:08 +0000947void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
948 BitVector &SpilledLocations) {
Reid Kleckner92687d42017-09-20 17:32:54 +0000949 // Build a set of new locations with new numbers so we can coalesce our
950 // IntervalMap if two vreg intervals collapse to the same physical location.
951 // Use MapVector instead of SetVector because MapVector::insert returns the
Reid Kleckner4e040282017-09-20 18:19:08 +0000952 // position of the previously or newly inserted element. The boolean value
953 // tracks if the location was produced by a spill.
954 // FIXME: This will be problematic if we ever support direct and indirect
955 // frame index locations, i.e. expressing both variables in memory and
956 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
Reid Kleckner92687d42017-09-20 17:32:54 +0000957 MapVector<MachineOperand, bool> NewLocations;
958 SmallVector<unsigned, 4> LocNoMap(locations.size());
959 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
Reid Kleckner4e040282017-09-20 18:19:08 +0000960 bool Spilled = false;
Reid Kleckner92687d42017-09-20 17:32:54 +0000961 MachineOperand Loc = locations[I];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000962 // Only virtual registers are rewritten.
Reid Kleckner92687d42017-09-20 17:32:54 +0000963 if (Loc.isReg() && Loc.getReg() &&
964 TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
965 unsigned VirtReg = Loc.getReg();
966 if (VRM.isAssignedReg(VirtReg) &&
967 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
968 // This can create a %noreg operand in rare cases when the sub-register
969 // index is no longer available. That means the user value is in a
970 // non-existent sub-register, and %noreg is exactly what we want.
971 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
972 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
973 // FIXME: Translate SubIdx to a stackslot offset.
974 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Reid Kleckner4e040282017-09-20 18:19:08 +0000975 Spilled = true;
Reid Kleckner92687d42017-09-20 17:32:54 +0000976 } else {
977 Loc.setReg(0);
978 Loc.setSubReg(0);
979 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000980 }
Reid Kleckner92687d42017-09-20 17:32:54 +0000981
982 // Insert this location if it doesn't already exist and record a mapping
983 // from the old number to the new number.
Reid Kleckner4e040282017-09-20 18:19:08 +0000984 auto InsertResult = NewLocations.insert({Loc, Spilled});
985 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
986 LocNoMap[I] = NewLocNo;
Reid Kleckner92687d42017-09-20 17:32:54 +0000987 }
988
Reid Kleckner4e040282017-09-20 18:19:08 +0000989 // Rewrite the locations and record which ones were spill slots.
Reid Kleckner92687d42017-09-20 17:32:54 +0000990 locations.clear();
Reid Kleckner4e040282017-09-20 18:19:08 +0000991 SpilledLocations.clear();
992 SpilledLocations.resize(NewLocations.size());
993 for (auto &Pair : NewLocations) {
Reid Kleckner92687d42017-09-20 17:32:54 +0000994 locations.push_back(Pair.first);
Reid Kleckner4e040282017-09-20 18:19:08 +0000995 if (Pair.second) {
996 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
997 SpilledLocations.set(NewLocNo);
998 }
999 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001000
1001 // Update the interval map, but only coalesce left, since intervals to the
1002 // right use the old location numbers. This should merge two contiguous
1003 // DBG_VALUE intervals with different vregs that were allocated to the same
1004 // physical register.
1005 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
1006 unsigned NewLocNo = LocNoMap[I.value()];
1007 I.setValueUnchecked(NewLocNo);
1008 I.setStart(I.start());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001009 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001010}
1011
Devang Patel26ffa012011-02-04 01:43:25 +00001012/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001013/// instruction.
1014static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +00001015findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001016 LiveIntervals &LIS) {
1017 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1018 Idx = Idx.getBaseIndex();
1019
1020 // Try to find an insert location by going backwards from Idx.
1021 MachineInstr *MI;
1022 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1023 // We've reached the beginning of MBB.
1024 if (Idx == Start) {
Keith Walker830a8c12016-09-16 14:07:29 +00001025 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001026 return I;
1027 }
1028 Idx = Idx.getPrevIndex();
1029 }
Devang Patel26ffa012011-02-04 01:43:25 +00001030
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +00001031 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001032 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001033 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001034}
1035
1036void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
Reid Kleckner4e040282017-09-20 18:19:08 +00001037 unsigned LocNo, bool Spilled,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001038 LiveIntervals &LIS,
1039 const TargetInstrInfo &TII) {
Devang Patel26ffa012011-02-04 01:43:25 +00001040 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +00001041 MachineOperand &Loc = locations[LocNo];
Devang Pateleabc3cea2011-08-04 20:42:11 +00001042 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001043
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001044 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001045 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001046 "Expected inlined-at fields to agree");
Reid Kleckner4e040282017-09-20 18:19:08 +00001047
1048 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1049 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1050 // that the original virtual register was a pointer.
1051 bool NewIndirect = IsIndirect || Spilled;
1052 const DIExpression *Expr = Expression;
1053 if (Spilled && IsIndirect)
1054 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
1055
1056 assert((!Spilled || Loc.isFI()) &&
1057 "a spilled location must be a frame index");
1058
1059 MachineInstrBuilder MIB =
1060 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
1061 .add(Loc);
1062 if (NewIndirect)
1063 MIB.addImm(0U);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001064 else
Reid Kleckner4e040282017-09-20 18:19:08 +00001065 MIB.addReg(0U, RegState::Debug);
1066 MIB.addMetadata(Variable).addMetadata(Expr);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001067}
1068
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001069void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +00001070 const TargetInstrInfo &TII,
1071 const BitVector &SpilledLocations) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001072 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1073
1074 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1075 SlotIndex Start = I.start();
1076 SlotIndex Stop = I.stop();
1077 unsigned LocNo = I.value();
Reid Kleckner4e040282017-09-20 18:19:08 +00001078 bool Spilled = LocNo != UndefLocNo ? SpilledLocations.test(LocNo) : false;
Robert Lougher10f740d2017-08-03 11:54:02 +00001079
1080 // If the interval start was trimmed to the lexical scope insert the
1081 // DBG_VALUE at the previous index (otherwise it appears after the
1082 // first instruction in the range).
1083 if (trimmedDefs.count(Start))
1084 Start = Start.getPrevIndex();
1085
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001086 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << LocNo);
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001087 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1088 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001089
1090 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
Reid Kleckner4e040282017-09-20 18:19:08 +00001091 insertDebugValue(&*MBB, Start, LocNo, Spilled, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001092 // This interval may span multiple basic blocks.
1093 // Insert a DBG_VALUE into each one.
1094 while(Stop > MBBEnd) {
1095 // Move to the next block.
1096 Start = MBBEnd;
1097 if (++MBB == MFEnd)
1098 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001099 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001100 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
Reid Kleckner4e040282017-09-20 18:19:08 +00001101 insertDebugValue(&*MBB, Start, LocNo, Spilled, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001102 }
1103 DEBUG(dbgs() << '\n');
1104 if (MBB == MFEnd)
1105 break;
1106
1107 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001108 }
1109}
1110
1111void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1112 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001113 if (!MF)
1114 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001115 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Reid Kleckner4e040282017-09-20 18:19:08 +00001116 BitVector SpilledLocations;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001117 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001118 DEBUG(userValues[i]->print(dbgs(), TRI));
Reid Kleckner4e040282017-09-20 18:19:08 +00001119 userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations);
1120 userValues[i]->emitDebugValues(VRM, *LIS, *TII, SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001121 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001122 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001123}
1124
1125void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001126 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001127 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1128}
1129
David Blaikie2f040112014-07-25 16:10:16 +00001130bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001131 return Pass::doInitialization(M);
1132}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001133
Matthias Braun8c209aa2017-01-28 02:02:38 +00001134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +00001135LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001136 if (pImpl)
1137 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1138}
1139#endif