blob: 7a4694f966350e5bbb613185afd5ea6f143c09f4 [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
Reid Kleckner04e25e02017-10-03 17:59:02 +000094enum : unsigned { UndefLocNo = ~0U };
95
96/// Describes a location by number along with some flags about the original
97/// usage of the location.
98class DbgValueLocation {
99public:
100 DbgValueLocation(unsigned LocNo, bool WasIndirect)
101 : LocNo(LocNo), WasIndirect(WasIndirect) {
102 static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
103 assert(locNo() == LocNo && "location truncation");
104 }
105
106 DbgValueLocation() : LocNo(0), WasIndirect(0) {}
107
108 unsigned locNo() const {
109 // Fix up the undef location number, which gets truncated.
110 return LocNo == INT_MAX ? UndefLocNo : LocNo;
111 }
112 bool wasIndirect() const { return WasIndirect; }
113 bool isUndef() const { return locNo() == UndefLocNo; }
114
115 DbgValueLocation changeLocNo(unsigned NewLocNo) const {
116 return DbgValueLocation(NewLocNo, WasIndirect);
117 }
118
119 bool operator==(const DbgValueLocation &O) const {
120 return LocNo == O.LocNo && WasIndirect == O.WasIndirect;
121 }
122 bool operator!=(const DbgValueLocation &O) const { return !(*this == O); }
123
124private:
125 unsigned LocNo : 31;
126 unsigned WasIndirect : 1;
127};
128
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000129/// LocMap - Map of where a user value is live, and its location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000130using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000131
132namespace {
133
134class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000135
136/// UserValue - A user value is a part of a debug info user variable.
137///
138/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
139/// holds part of a user variable. The part is identified by a byte offset.
140///
141/// UserValues are grouped into equivalence classes for easier searching. Two
142/// user values are related if they refer to the same variable, or if they are
143/// held by the same virtual register. The equivalence class is the transitive
144/// closure of that relation.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000145class UserValue {
Reid Kleckner4e040282017-09-20 18:19:08 +0000146 const DILocalVariable *Variable; ///< The debug info variable we are part of.
147 const DIExpression *Expression; ///< Any complex address expression.
Devang Patel26ffa012011-02-04 01:43:25 +0000148 DebugLoc dl; ///< The debug location for the variable. This is
149 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000150 UserValue *leader; ///< Equivalence class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000151 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000152
153 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000154 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000155
156 /// Map of slot indices where this value is live.
157 LocMap locInts;
158
Robert Lougher10f740d2017-08-03 11:54:02 +0000159 /// Set of interval start indexes that have been trimmed to the
160 /// lexical scope.
161 SmallSet<SlotIndex, 2> trimmedDefs;
162
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000163 /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
Reid Kleckner4e040282017-09-20 18:19:08 +0000164 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000165 DbgValueLocation Loc, bool Spilled, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +0000166 const TargetInstrInfo &TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000167
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000168 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
169 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000170 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
171 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000172
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000173public:
174 /// UserValue - Create a new UserValue.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000175 UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
176 LocMap::Allocator &alloc)
177 : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
178 locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000179
180 /// getLeader - Get the leader of this value's equivalence class.
181 UserValue *getLeader() {
182 UserValue *l = leader;
183 while (l != l->leader)
184 l = l->leader;
185 return leader = l;
186 }
187
188 /// getNext - Return the next UserValue in the equivalence class.
189 UserValue *getNext() const { return next; }
190
Devang Patel338e4322011-07-06 23:09:51 +0000191 /// match - Does this UserValue match the parameters?
Reid Kleckner4e040282017-09-20 18:19:08 +0000192 bool match(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000193 const DILocation *IA) const {
194 // FIXME: The fragment should be part of the equivalence class, but not
195 // other things in the expression like stack values.
196 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000197 }
198
199 /// merge - Merge equivalence classes.
200 static UserValue *merge(UserValue *L1, UserValue *L2) {
201 L2 = L2->getLeader();
202 if (!L1)
203 return L2;
204 L1 = L1->getLeader();
205 if (L1 == L2)
206 return L1;
207 // Splice L2 before L1's members.
208 UserValue *End = L2;
Richard Trieu7a083812016-02-18 22:09:30 +0000209 while (End->next) {
210 End->leader = L1;
211 End = End->next;
212 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000213 End->leader = L1;
214 End->next = L1->next;
215 L1->next = L2;
216 return L1;
217 }
218
219 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000220 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000221 if (LocMO.isReg()) {
222 if (LocMO.getReg() == 0)
Reid Klecknereed09732017-09-15 22:08:50 +0000223 return UndefLocNo;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000224 // For register locations we dont care about use/def and other flags.
225 for (unsigned i = 0, e = locations.size(); i != e; ++i)
226 if (locations[i].isReg() &&
227 locations[i].getReg() == LocMO.getReg() &&
228 locations[i].getSubReg() == LocMO.getSubReg())
229 return i;
230 } else
231 for (unsigned i = 0, e = locations.size(); i != e; ++i)
232 if (LocMO.isIdenticalTo(locations[i]))
233 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000234 locations.push_back(LocMO);
235 // We are storing a MachineOperand outside a MachineInstr.
236 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000237 // Don't store def operands.
238 if (locations.back().isReg())
239 locations.back().setIsUse();
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000240 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000241 }
242
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000243 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
244 void mapVirtRegs(LDVImpl *LDV);
245
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000246 /// addDef - Add a definition point to this value.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000247 void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) {
248 DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000249 // Add a singular (Idx,Idx) -> Loc mapping.
250 LocMap::iterator I = locInts.find(Idx);
251 if (!I.valid() || I.start() != Idx)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000252 I.insert(Idx, Idx.getNextSlot(), Loc);
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000253 else
254 // A later DBG_VALUE at the same SlotIndex overrides the old location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000255 I.setValue(Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000256 }
257
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000258 /// extendDef - Extend the current definition as far as possible down.
259 /// Stop when meeting an existing def or when leaving the live
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000260 /// range of VNI.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000261 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000262 /// @param Idx Starting point for the definition.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000263 /// @param Loc Location number to propagate.
Matthias Braun34e1be92013-10-10 21:29:02 +0000264 /// @param LR Restrict liveness to where LR has the value VNI. May be null.
265 /// @param VNI When LR is not null, this is the value to restrict to.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000266 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000267 /// @param LIS Live intervals analysis.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000268 void extendDef(SlotIndex Idx, DbgValueLocation Loc,
Matthias Braun34e1be92013-10-10 21:29:02 +0000269 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000270 SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000271 LiveIntervals &LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000272
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000273 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
274 /// registers. Determine if any of the copies are available at the kill
275 /// points, and add defs if possible.
276 /// @param LI Scan for copies of the value in LI->reg.
277 /// @param LocNo Location number of LI->reg.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000278 /// @param WasIndirect Indicates if the original use of LI->reg was indirect
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000279 /// @param Kills Points where the range of LocNo could be extended.
280 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000281 void addDefsFromCopies(
282 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
283 const SmallVectorImpl<SlotIndex> &Kills,
284 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
285 MachineRegisterInfo &MRI, LiveIntervals &LIS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000286
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000287 /// computeIntervals - Compute the live intervals of all locations after
288 /// collecting all their def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000289 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Robert Lougher10f740d2017-08-03 11:54:02 +0000290 LiveIntervals &LIS, LexicalScopes &LS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000291
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000292 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
293 /// live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000294 bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
295 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000296
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000297 /// rewriteLocations - Rewrite virtual register locations according to the
Reid Kleckner4e040282017-09-20 18:19:08 +0000298 /// provided virtual register map. Record which locations were spilled.
299 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
300 BitVector &SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000301
Eric Christopherbc671702013-02-13 02:29:18 +0000302 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Reid Kleckner4e040282017-09-20 18:19:08 +0000303 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
304 const TargetInstrInfo &TRI,
305 const BitVector &SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000306
Devang Patelf9e2ae92011-09-13 18:40:53 +0000307 /// getDebugLoc - Return DebugLoc of this UserValue.
308 DebugLoc getDebugLoc() { return dl;}
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000309
Eric Christopher1cdefae2015-02-27 00:11:34 +0000310 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000311};
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000312
313/// LDVImpl - Implementation of the LiveDebugVariables pass.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000314class LDVImpl {
315 LiveDebugVariables &pass;
316 LocMap::Allocator allocator;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000317 MachineFunction *MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000318 LiveIntervals *LIS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000319 const TargetRegisterInfo *TRI;
320
Manman Ren7a4c8a72013-02-13 20:23:48 +0000321 /// Whether emitDebugValues is called.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000322 bool EmitDone = false;
323
Manman Ren7a4c8a72013-02-13 20:23:48 +0000324 /// Whether the machine function is modified during the pass.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000325 bool ModifiedMF = false;
Manman Ren7a4c8a72013-02-13 20:23:48 +0000326
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000327 /// userValues - All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000328 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000329
330 /// Map virtual register to eq class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000331 using VRMap = DenseMap<unsigned, UserValue *>;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000332 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000333
334 /// Map user variable to eq class leader.
Reid Kleckner4e040282017-09-20 18:19:08 +0000335 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000336 UVMap userVarMap;
337
338 /// getUserValue - Find or create a UserValue.
Reid Kleckner4e040282017-09-20 18:19:08 +0000339 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000340 const DebugLoc &DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000341
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000342 /// lookupVirtReg - Find the EC leader for VirtReg or null.
343 UserValue *lookupVirtReg(unsigned VirtReg);
344
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000345 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
346 /// @param MI DBG_VALUE instruction
347 /// @param Idx Last valid SLotIndex before instruction.
348 /// @return True if the DBG_VALUE instruction should be deleted.
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000349 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000350
351 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
352 /// a UserValue def for each instruction.
353 /// @param mf MachineFunction to be scanned.
354 /// @return True if any debug values were found.
355 bool collectDebugValues(MachineFunction &mf);
356
357 /// computeIntervals - Compute the live intervals of all user values after
358 /// collecting all their def points.
359 void computeIntervals();
360
361public:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000362 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
363
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000364 bool runOnMachineFunction(MachineFunction &mf);
365
Manman Ren7a4c8a72013-02-13 20:23:48 +0000366 /// clear - Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000367 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000368 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000369 userValues.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000370 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000371 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000372 // Make sure we call emitDebugValues if the machine function was modified.
373 assert((!ModifiedMF || EmitDone) &&
374 "Dbg values are not emitted in LDV");
375 EmitDone = false;
376 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000377 }
378
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000379 /// mapVirtReg - Map virtual register to an equivalence class.
380 void mapVirtReg(unsigned VirtReg, UserValue *EC);
381
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000382 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000383 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000384
Eric Christopherbc671702013-02-13 02:29:18 +0000385 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000386 void emitDebugValues(VirtRegMap *VRM);
387
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000388 void print(raw_ostream&);
389};
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000390
391} // end anonymous namespace
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000392
Don Hinton53eb6372017-09-27 21:19:56 +0000393#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000394static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000395 const LLVMContext &Ctx) {
396 if (!DL)
397 return;
398
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000399 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000400 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000401 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000402 CommentOS << ':' << DL.getLine();
403 if (DL.getCol() != 0)
404 CommentOS << ':' << DL.getCol();
405
406 DebugLoc InlinedAtDL = DL.getInlinedAt();
407 if (!InlinedAtDL)
408 return;
409
410 CommentOS << " @[ ";
411 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
412 CommentOS << " ]";
413}
414
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000415static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
416 const DILocation *DL) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000417 const LLVMContext &Ctx = V->getContext();
418 StringRef Res = V->getName();
419 if (!Res.empty())
420 OS << Res << "," << V->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000421 if (auto *InlinedAt = DL->getInlinedAt()) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000422 if (DebugLoc InlinedAtDL = InlinedAt) {
423 OS << " @[";
424 printDebugLoc(InlinedAtDL, OS, Ctx);
425 OS << "]";
426 }
427 }
428}
429
Eric Christopher1cdefae2015-02-27 00:11:34 +0000430void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000431 auto *DV = cast<DILocalVariable>(Variable);
Frederic Risse6bb1872014-08-07 20:04:00 +0000432 OS << "!\"";
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000433 printExtendedName(OS, DV, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000434
Devang Patel6c1ed312011-08-09 01:03:35 +0000435 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000436 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
437 OS << " [" << I.start() << ';' << I.stop() << "):";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000438 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000439 OS << "undef";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000440 else {
441 OS << I.value().locNo();
442 if (I.value().wasIndirect())
443 OS << " ind";
444 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000445 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000446 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
447 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000448 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000449 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000450 OS << '\n';
451}
452
453void LDVImpl::print(raw_ostream &OS) {
454 OS << "********** DEBUG VARIABLES **********\n";
455 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Eric Christopher1cdefae2015-02-27 00:11:34 +0000456 userValues[i]->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000457}
Florian Hahn6b3216a2017-07-31 10:07:49 +0000458#endif
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000459
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000460void UserValue::mapVirtRegs(LDVImpl *LDV) {
461 for (unsigned i = 0, e = locations.size(); i != e; ++i)
462 if (locations[i].isReg() &&
463 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
464 LDV->mapVirtReg(locations[i].getReg(), this);
465}
466
Reid Kleckner4e040282017-09-20 18:19:08 +0000467UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000468 const DIExpression *Expr, const DebugLoc &DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000469 UserValue *&Leader = userVarMap[Var];
470 if (Leader) {
471 UserValue *UV = Leader->getLeader();
472 Leader = UV;
473 for (; UV; UV = UV->getNext())
Reid Kleckner04e25e02017-10-03 17:59:02 +0000474 if (UV->match(Var, Expr, DL->getInlinedAt()))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000475 return UV;
476 }
477
David Blaikie2b1dfa72014-04-21 20:37:07 +0000478 userValues.push_back(
Reid Kleckner04e25e02017-10-03 17:59:02 +0000479 llvm::make_unique<UserValue>(Var, Expr, DL, allocator));
David Blaikie2b1dfa72014-04-21 20:37:07 +0000480 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000481 Leader = UserValue::merge(Leader, UV);
482 return UV;
483}
484
485void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
486 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000487 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000488 Leader = UserValue::merge(Leader, EC);
489}
490
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000491UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000492 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000493 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000494 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000495}
496
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000497bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000498 // DBG_VALUE loc, offset, variable
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000499 if (MI.getNumOperands() != 4 ||
500 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
501 !MI.getOperand(2).isMetadata()) {
502 DEBUG(dbgs() << "Can't handle " << MI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000503 return false;
504 }
505
Reid Kleckner04e25e02017-10-03 17:59:02 +0000506 // Get or create the UserValue for (variable,offset) here.
Reid Kleckner4e040282017-09-20 18:19:08 +0000507 bool IsIndirect = MI.getOperand(1).isImm();
Adrian Prantlb2811e52017-07-28 23:06:50 +0000508 if (IsIndirect)
509 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
Reid Kleckner4e040282017-09-20 18:19:08 +0000510 const DILocalVariable *Var = MI.getDebugVariable();
511 const DIExpression *Expr = MI.getDebugExpression();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000512 UserValue *UV =
513 getUserValue(Var, Expr, MI.getDebugLoc());
514 UV->addDef(Idx, MI.getOperand(0), IsIndirect);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000515 return true;
516}
517
518bool LDVImpl::collectDebugValues(MachineFunction &mf) {
519 bool Changed = false;
520 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
521 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000522 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000523 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
524 MBBI != MBBE;) {
525 if (!MBBI->isDebugValue()) {
526 ++MBBI;
527 continue;
528 }
529 // DBG_VALUE has no slot index, use the previous instruction instead.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000530 SlotIndex Idx =
531 MBBI == MBB->begin()
532 ? LIS->getMBBStartIdx(MBB)
533 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000534 // Handle consecutive DBG_VALUE instructions with the same slot index.
535 do {
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000536 if (handleDebugValue(*MBBI, Idx)) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000537 MBBI = MBB->erase(MBBI);
538 Changed = true;
539 } else
540 ++MBBI;
541 } while (MBBI != MBBE && MBBI->isDebugValue());
542 }
543 }
544 return Changed;
545}
546
Adrian Prantlce858132015-12-21 20:03:00 +0000547/// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
548/// data-flow analysis to propagate them beyond basic block boundaries.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000549void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
Adrian Prantlce858132015-12-21 20:03:00 +0000550 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000551 LiveIntervals &LIS) {
Adrian Prantlce858132015-12-21 20:03:00 +0000552 SlotIndex Start = Idx;
553 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
554 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
555 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000556
Adrian Prantlce858132015-12-21 20:03:00 +0000557 // Limit to VNI's live range.
558 bool ToEnd = true;
559 if (LR && VNI) {
560 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
561 if (!Segment || Segment->valno != VNI) {
562 if (Kills)
563 Kills->push_back(Start);
564 return;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000565 }
Richard Trieu7a083812016-02-18 22:09:30 +0000566 if (Segment->end < Stop) {
567 Stop = Segment->end;
568 ToEnd = false;
569 }
Adrian Prantlce858132015-12-21 20:03:00 +0000570 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000571
Adrian Prantlce858132015-12-21 20:03:00 +0000572 // There could already be a short def at Start.
573 if (I.valid() && I.start() <= Start) {
574 // Stop when meeting a different location or an already extended interval.
575 Start = Start.getNextSlot();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000576 if (I.value() != Loc || I.stop() != Start)
Adrian Prantlce858132015-12-21 20:03:00 +0000577 return;
578 // This is a one-slot placeholder. Just skip it.
579 ++I;
580 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000581
Adrian Prantlce858132015-12-21 20:03:00 +0000582 // Limited by the next def.
Richard Trieu7a083812016-02-18 22:09:30 +0000583 if (I.valid() && I.start() < Stop) {
584 Stop = I.start();
585 ToEnd = false;
586 }
Adrian Prantlce858132015-12-21 20:03:00 +0000587 // Limited by VNI's live range.
588 else if (!ToEnd && Kills)
589 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000590
Adrian Prantlce858132015-12-21 20:03:00 +0000591 if (Start < Stop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000592 I.insert(Start, Stop, Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000593}
594
Reid Kleckner04e25e02017-10-03 17:59:02 +0000595void UserValue::addDefsFromCopies(
596 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
597 const SmallVectorImpl<SlotIndex> &Kills,
598 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
599 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000600 if (Kills.empty())
601 return;
602 // Don't track copies from physregs, there are too many uses.
603 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
604 return;
605
606 // Collect all the (vreg, valno) pairs that are copies of LI.
607 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000608 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
609 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000610 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000611 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000612 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000613 unsigned DstReg = MI->getOperand(0).getReg();
614
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000615 // Don't follow copies to physregs. These are usually setting up call
616 // arguments, and the argument registers are always call clobbered. We are
617 // better off in the source register which could be a callee-saved register,
618 // or it could be spilled.
619 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
620 continue;
621
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000622 // Is LocNo extended to reach this copy? If not, another def may be blocking
623 // it, or we are looking at a wrong value of LI.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000624 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000625 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000626 if (!I.valid() || I.value().locNo() != LocNo)
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000627 continue;
628
629 if (!LIS.hasInterval(DstReg))
630 continue;
631 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000632 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
633 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000634 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
635 }
636
637 if (CopyValues.empty())
638 return;
639
640 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
641
642 // Try to add defs of the copied values for each kill point.
643 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
644 SlotIndex Idx = Kills[i];
645 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
646 LiveInterval *DstLI = CopyValues[j].first;
647 const VNInfo *DstVNI = CopyValues[j].second;
648 if (DstLI->getVNInfoAt(Idx) != DstVNI)
649 continue;
650 // Check that there isn't already a def at Idx
651 LocMap::iterator I = locInts.find(Idx);
652 if (I.valid() && I.start() <= Idx)
653 continue;
654 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
655 << DstVNI->id << " in " << *DstLI << '\n');
656 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
657 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
658 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000659 DbgValueLocation NewLoc(LocNo, WasIndirect);
660 I.insert(Idx, Idx.getNextSlot(), NewLoc);
661 NewDefs.push_back(std::make_pair(Idx, NewLoc));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000662 break;
663 }
664 }
665}
666
Robert Lougher10f740d2017-08-03 11:54:02 +0000667void UserValue::computeIntervals(MachineRegisterInfo &MRI,
668 const TargetRegisterInfo &TRI,
669 LiveIntervals &LIS, LexicalScopes &LS) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000670 SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000671
672 // Collect all defs to be extended (Skipping undefs).
673 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000674 if (!I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000675 Defs.push_back(std::make_pair(I.start(), I.value()));
676
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000677 // Extend all defs, and possibly add new ones along the way.
678 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000679 SlotIndex Idx = Defs[i].first;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000680 DbgValueLocation Loc = Defs[i].second;
681 const MachineOperand &LocMO = locations[Loc.locNo()];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000682
Reid Kleckner04e25e02017-10-03 17:59:02 +0000683 if (!LocMO.isReg()) {
684 extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000685 continue;
686 }
687
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000688 // Register locations are constrained to where the register value is live.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000689 if (TargetRegisterInfo::isVirtualRegister(LocMO.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000690 LiveInterval *LI = nullptr;
691 const VNInfo *VNI = nullptr;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000692 if (LIS.hasInterval(LocMO.getReg())) {
693 LI = &LIS.getInterval(LocMO.getReg());
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000694 VNI = LI->getVNInfoAt(Idx);
695 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000696 SmallVector<SlotIndex, 16> Kills;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000697 extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000698 if (LI)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000699 addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI,
700 LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000701 continue;
702 }
703
Bjorn Pettersson715a5ef2017-09-28 13:10:06 +0000704 // For physregs, we only mark the start slot idx. DwarfDebug will see it
705 // as if the DBG_VALUE is valid up until the end of the basic block, or
706 // the next def of the physical register. So we do not need to extend the
707 // range. It might actually happen that the DBG_VALUE is the last use of
708 // the physical register (e.g. if this is an unused input argument to a
709 // function).
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000710 }
711
Robert Lougher10f740d2017-08-03 11:54:02 +0000712 // Erase all the undefs.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000713 for (LocMap::iterator I = locInts.begin(); I.valid();)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000714 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000715 I.erase();
716 else
717 ++I;
Robert Lougher10f740d2017-08-03 11:54:02 +0000718
719 // The computed intervals may extend beyond the range of the debug
720 // location's lexical scope. In this case, splitting of an interval
721 // can result in an interval outside of the scope being created,
722 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
723 // this, trim the intervals to the lexical scope.
724
725 LexicalScope *Scope = LS.findLexicalScope(dl);
726 if (!Scope)
727 return;
728
729 SlotIndex PrevEnd;
730 LocMap::iterator I = locInts.begin();
731
732 // Iterate over the lexical scope ranges. Each time round the loop
733 // we check the intervals for overlap with the end of the previous
734 // range and the start of the next. The first range is handled as
735 // a special case where there is no PrevEnd.
736 for (const InsnRange &Range : Scope->getRanges()) {
737 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
738 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
739
740 // At the start of each iteration I has been advanced so that
741 // I.stop() >= PrevEnd. Check for overlap.
742 if (PrevEnd && I.start() < PrevEnd) {
743 SlotIndex IStop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000744 DbgValueLocation Loc = I.value();
Robert Lougher10f740d2017-08-03 11:54:02 +0000745
746 // Stop overlaps previous end - trim the end of the interval to
747 // the scope range.
748 I.setStopUnchecked(PrevEnd);
749 ++I;
750
751 // If the interval also overlaps the start of the "next" (i.e.
752 // current) range create a new interval for the remainder (which
753 // may be further trimmed).
754 if (RStart < IStop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000755 I.insert(RStart, IStop, Loc);
Robert Lougher10f740d2017-08-03 11:54:02 +0000756 }
757
758 // Advance I so that I.stop() >= RStart, and check for overlap.
759 I.advanceTo(RStart);
760 if (!I.valid())
761 return;
762
763 if (I.start() < RStart) {
764 // Interval start overlaps range - trim to the scope range.
765 I.setStartUnchecked(RStart);
766 // Remember that this interval was trimmed.
767 trimmedDefs.insert(RStart);
768 }
769
770 // The end of a lexical scope range is the last instruction in the
771 // range. To convert to an interval we need the index of the
772 // instruction after it.
773 REnd = REnd.getNextIndex();
774
775 // Advance I to first interval outside current range.
776 I.advanceTo(REnd);
777 if (!I.valid())
778 return;
779
780 PrevEnd = REnd;
781 }
782
783 // Check for overlap with end of final range.
784 if (PrevEnd && I.start() < PrevEnd)
785 I.setStopUnchecked(PrevEnd);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000786}
787
788void LDVImpl::computeIntervals() {
Robert Lougher10f740d2017-08-03 11:54:02 +0000789 LexicalScopes LS;
790 LS.initialize(*MF);
791
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000792 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Robert Lougher10f740d2017-08-03 11:54:02 +0000793 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000794 userValues[i]->mapVirtRegs(this);
795 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000796}
797
798bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000799 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000800 MF = &mf;
801 LIS = &pass.getAnalysis<LiveIntervals>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000802 TRI = mf.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000803 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikiec8c29202012-08-22 17:18:53 +0000804 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000805
806 bool Changed = collectDebugValues(mf);
807 computeIntervals();
808 DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000809 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000810 return Changed;
811}
812
David Blaikie2f040112014-07-25 16:10:16 +0000813static void removeDebugValues(MachineFunction &mf) {
814 for (MachineBasicBlock &MBB : mf) {
815 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
816 if (!MBBI->isDebugValue()) {
817 ++MBBI;
818 continue;
819 }
820 MBBI = MBB.erase(MBBI);
821 }
822 }
823}
824
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000825bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000826 if (!EnableLDV)
827 return false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000828 if (!mf.getFunction()->getSubprogram()) {
David Blaikie2f040112014-07-25 16:10:16 +0000829 removeDebugValues(mf);
830 return false;
831 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000832 if (!pImpl)
833 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000834 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000835}
836
837void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000838 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000839 static_cast<LDVImpl*>(pImpl)->clear();
840}
841
842LiveDebugVariables::~LiveDebugVariables() {
843 if (pImpl)
844 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000845}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000846
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000847//===----------------------------------------------------------------------===//
848// Live Range Splitting
849//===----------------------------------------------------------------------===//
850
851bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000852UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
853 LiveIntervals& LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000854 DEBUG({
855 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +0000856 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000857 });
858 bool DidChange = false;
859 LocMap::iterator LocMapI;
860 LocMapI.setMap(locInts);
861 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000862 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000863 if (LI->empty())
864 continue;
865
866 // Don't allocate the new LocNo until it is needed.
Reid Klecknereed09732017-09-15 22:08:50 +0000867 unsigned NewLocNo = UndefLocNo;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000868
869 // Iterate over the overlaps between locInts and LI.
870 LocMapI.find(LI->beginIndex());
871 if (!LocMapI.valid())
872 continue;
873 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
874 LiveInterval::iterator LIE = LI->end();
875 while (LocMapI.valid() && LII != LIE) {
876 // At this point, we know that LocMapI.stop() > LII->start.
877 LII = LI->advanceTo(LII, LocMapI.start());
878 if (LII == LIE)
879 break;
880
881 // Now LII->end > LocMapI.start(). Do we have an overlap?
Reid Kleckner04e25e02017-10-03 17:59:02 +0000882 if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000883 // Overlapping correct location. Allocate NewLocNo now.
Reid Klecknereed09732017-09-15 22:08:50 +0000884 if (NewLocNo == UndefLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000885 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
886 MO.setSubReg(locations[OldLocNo].getSubReg());
887 NewLocNo = getLocationNo(MO);
888 DidChange = true;
889 }
890
891 SlotIndex LStart = LocMapI.start();
892 SlotIndex LStop = LocMapI.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000893 DbgValueLocation OldLoc = LocMapI.value();
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000894
895 // Trim LocMapI down to the LII overlap.
896 if (LStart < LII->start)
897 LocMapI.setStartUnchecked(LII->start);
898 if (LStop > LII->end)
899 LocMapI.setStopUnchecked(LII->end);
900
901 // Change the value in the overlap. This may trigger coalescing.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000902 LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000903
904 // Re-insert any removed OldLocNo ranges.
905 if (LStart < LocMapI.start()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000906 LocMapI.insert(LStart, LocMapI.start(), OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000907 ++LocMapI;
908 assert(LocMapI.valid() && "Unexpected coalescing");
909 }
910 if (LStop > LocMapI.stop()) {
911 ++LocMapI;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000912 LocMapI.insert(LII->end, LStop, OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000913 --LocMapI;
914 }
915 }
916
917 // Advance to the next overlap.
918 if (LII->end < LocMapI.stop()) {
919 if (++LII == LIE)
920 break;
921 LocMapI.advanceTo(LII->start);
922 } else {
923 ++LocMapI;
924 if (!LocMapI.valid())
925 break;
926 LII = LI->advanceTo(LII, LocMapI.start());
927 }
928 }
929 }
930
931 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
932 locations.erase(locations.begin() + OldLocNo);
933 LocMapI.goToBegin();
934 while (LocMapI.valid()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000935 DbgValueLocation v = LocMapI.value();
936 if (v.locNo() == OldLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000937 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
938 << LocMapI.stop() << ")\n");
939 LocMapI.erase();
940 } else {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000941 if (v.locNo() > OldLocNo)
942 LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000943 ++LocMapI;
944 }
945 }
946
Craig Topperc0196b12014-04-14 00:51:57 +0000947 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000948 return DidChange;
949}
950
951bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000952UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
953 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000954 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000955 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +0000956 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +0000957 for (unsigned i = locations.size(); i ; --i) {
958 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000959 const MachineOperand *Loc = &locations[LocNo];
960 if (!Loc->isReg() || Loc->getReg() != OldReg)
961 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000962 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000963 }
964 return DidChange;
965}
966
Mark Laceyf9ea8852013-08-14 23:50:04 +0000967void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000968 bool DidChange = false;
969 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +0000970 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000971
972 if (!DidChange)
973 return;
974
975 // Map all of the new virtual registers.
976 UserValue *UV = lookupVirtReg(OldReg);
977 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +0000978 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000979}
980
981void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +0000982splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000983 if (pImpl)
984 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
985}
986
Reid Kleckner4e040282017-09-20 18:19:08 +0000987void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
988 BitVector &SpilledLocations) {
Reid Kleckner92687d42017-09-20 17:32:54 +0000989 // Build a set of new locations with new numbers so we can coalesce our
990 // IntervalMap if two vreg intervals collapse to the same physical location.
991 // Use MapVector instead of SetVector because MapVector::insert returns the
Reid Kleckner4e040282017-09-20 18:19:08 +0000992 // position of the previously or newly inserted element. The boolean value
993 // tracks if the location was produced by a spill.
994 // FIXME: This will be problematic if we ever support direct and indirect
995 // frame index locations, i.e. expressing both variables in memory and
996 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
Reid Kleckner92687d42017-09-20 17:32:54 +0000997 MapVector<MachineOperand, bool> NewLocations;
998 SmallVector<unsigned, 4> LocNoMap(locations.size());
999 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001000 bool Spilled = false;
Reid Kleckner92687d42017-09-20 17:32:54 +00001001 MachineOperand Loc = locations[I];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001002 // Only virtual registers are rewritten.
Reid Kleckner92687d42017-09-20 17:32:54 +00001003 if (Loc.isReg() && Loc.getReg() &&
1004 TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
1005 unsigned VirtReg = Loc.getReg();
1006 if (VRM.isAssignedReg(VirtReg) &&
1007 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
1008 // This can create a %noreg operand in rare cases when the sub-register
1009 // index is no longer available. That means the user value is in a
1010 // non-existent sub-register, and %noreg is exactly what we want.
1011 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1012 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
1013 // FIXME: Translate SubIdx to a stackslot offset.
1014 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Reid Kleckner4e040282017-09-20 18:19:08 +00001015 Spilled = true;
Reid Kleckner92687d42017-09-20 17:32:54 +00001016 } else {
1017 Loc.setReg(0);
1018 Loc.setSubReg(0);
1019 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001020 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001021
1022 // Insert this location if it doesn't already exist and record a mapping
1023 // from the old number to the new number.
Reid Kleckner4e040282017-09-20 18:19:08 +00001024 auto InsertResult = NewLocations.insert({Loc, Spilled});
1025 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1026 LocNoMap[I] = NewLocNo;
Reid Kleckner92687d42017-09-20 17:32:54 +00001027 }
1028
Reid Kleckner4e040282017-09-20 18:19:08 +00001029 // Rewrite the locations and record which ones were spill slots.
Reid Kleckner92687d42017-09-20 17:32:54 +00001030 locations.clear();
Reid Kleckner4e040282017-09-20 18:19:08 +00001031 SpilledLocations.clear();
1032 SpilledLocations.resize(NewLocations.size());
1033 for (auto &Pair : NewLocations) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001034 locations.push_back(Pair.first);
Reid Kleckner4e040282017-09-20 18:19:08 +00001035 if (Pair.second) {
1036 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1037 SpilledLocations.set(NewLocNo);
1038 }
1039 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001040
1041 // Update the interval map, but only coalesce left, since intervals to the
1042 // right use the old location numbers. This should merge two contiguous
1043 // DBG_VALUE intervals with different vregs that were allocated to the same
1044 // physical register.
1045 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001046 DbgValueLocation Loc = I.value();
1047 unsigned NewLocNo = LocNoMap[Loc.locNo()];
1048 I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
Reid Kleckner92687d42017-09-20 17:32:54 +00001049 I.setStart(I.start());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001050 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001051}
1052
Devang Patel26ffa012011-02-04 01:43:25 +00001053/// findInsertLocation - Find an iterator for inserting a DBG_VALUE
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001054/// instruction.
1055static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +00001056findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001057 LiveIntervals &LIS) {
1058 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1059 Idx = Idx.getBaseIndex();
1060
1061 // Try to find an insert location by going backwards from Idx.
1062 MachineInstr *MI;
1063 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1064 // We've reached the beginning of MBB.
1065 if (Idx == Start) {
Keith Walker830a8c12016-09-16 14:07:29 +00001066 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001067 return I;
1068 }
1069 Idx = Idx.getPrevIndex();
1070 }
Devang Patel26ffa012011-02-04 01:43:25 +00001071
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +00001072 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001073 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001074 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001075}
1076
1077void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
Reid Kleckner04e25e02017-10-03 17:59:02 +00001078 DbgValueLocation Loc, bool Spilled,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001079 LiveIntervals &LIS,
1080 const TargetInstrInfo &TII) {
Devang Patel26ffa012011-02-04 01:43:25 +00001081 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
Reid Kleckner04e25e02017-10-03 17:59:02 +00001082 MachineOperand &MO = locations[Loc.locNo()];
Devang Pateleabc3cea2011-08-04 20:42:11 +00001083 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001084
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001085 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001086 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001087 "Expected inlined-at fields to agree");
Reid Kleckner4e040282017-09-20 18:19:08 +00001088
1089 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1090 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1091 // that the original virtual register was a pointer.
Reid Kleckner4e040282017-09-20 18:19:08 +00001092 const DIExpression *Expr = Expression;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001093 bool IsIndirect = Loc.wasIndirect();
1094 if (Spilled) {
1095 if (IsIndirect)
1096 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
1097 IsIndirect = true;
1098 }
Reid Kleckner4e040282017-09-20 18:19:08 +00001099
Reid Kleckner04e25e02017-10-03 17:59:02 +00001100 assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
Reid Kleckner4e040282017-09-20 18:19:08 +00001101
1102 MachineInstrBuilder MIB =
1103 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Reid Kleckner04e25e02017-10-03 17:59:02 +00001104 .add(MO);
1105 if (IsIndirect)
Reid Kleckner4e040282017-09-20 18:19:08 +00001106 MIB.addImm(0U);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001107 else
Reid Kleckner4e040282017-09-20 18:19:08 +00001108 MIB.addReg(0U, RegState::Debug);
1109 MIB.addMetadata(Variable).addMetadata(Expr);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001110}
1111
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001112void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +00001113 const TargetInstrInfo &TII,
1114 const BitVector &SpilledLocations) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001115 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1116
1117 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1118 SlotIndex Start = I.start();
1119 SlotIndex Stop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001120 DbgValueLocation Loc = I.value();
1121 bool Spilled = !Loc.isUndef() ? SpilledLocations.test(Loc.locNo()) : false;
Robert Lougher10f740d2017-08-03 11:54:02 +00001122
1123 // If the interval start was trimmed to the lexical scope insert the
1124 // DBG_VALUE at the previous index (otherwise it appears after the
1125 // first instruction in the range).
1126 if (trimmedDefs.count(Start))
1127 Start = Start.getPrevIndex();
1128
Reid Kleckner04e25e02017-10-03 17:59:02 +00001129 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001130 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1131 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001132
1133 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
Reid Kleckner04e25e02017-10-03 17:59:02 +00001134 insertDebugValue(&*MBB, Start, Loc, Spilled, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001135 // This interval may span multiple basic blocks.
1136 // Insert a DBG_VALUE into each one.
1137 while(Stop > MBBEnd) {
1138 // Move to the next block.
1139 Start = MBBEnd;
1140 if (++MBB == MFEnd)
1141 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001142 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001143 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
Reid Kleckner04e25e02017-10-03 17:59:02 +00001144 insertDebugValue(&*MBB, Start, Loc, Spilled, LIS, TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001145 }
1146 DEBUG(dbgs() << '\n');
1147 if (MBB == MFEnd)
1148 break;
1149
1150 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001151 }
1152}
1153
1154void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1155 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001156 if (!MF)
1157 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001158 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Reid Kleckner4e040282017-09-20 18:19:08 +00001159 BitVector SpilledLocations;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001160 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001161 DEBUG(userValues[i]->print(dbgs(), TRI));
Reid Kleckner4e040282017-09-20 18:19:08 +00001162 userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations);
1163 userValues[i]->emitDebugValues(VRM, *LIS, *TII, SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001164 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001165 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001166}
1167
1168void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001169 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001170 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1171}
1172
David Blaikie2f040112014-07-25 16:10:16 +00001173bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001174 return Pass::doInitialization(M);
1175}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001176
Matthias Braun8c209aa2017-01-28 02:02:38 +00001177#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +00001178LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001179 if (pImpl)
1180 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1181}
1182#endif