blob: eeddba5037ac6dd16171fce49b6188fb11152bde [file] [log] [blame]
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +00001//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the LiveDebugVariables analysis.
10//
11// Remove all DBG_VALUE instructions referencing virtual registers and replace
12// them with a data structure tracking where live user variables are kept - in a
13// virtual register or in a stack slot.
14//
15// Allow the data structure to be updated during register allocation when values
16// are moved between registers and stack slots. Finally emit new DBG_VALUE
17// instructions after register allocation is complete.
18//
19//===----------------------------------------------------------------------===//
20
21#include "LiveDebugVariables.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000022#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000024#include "llvm/ADT/IntervalMap.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000025#include "llvm/ADT/STLExtras.h"
Robert Lougher10f740d2017-08-03 11:54:02 +000026#include "llvm/ADT/SmallSet.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000027#include "llvm/ADT/SmallVector.h"
Devang Patelb4568662011-08-04 18:45:38 +000028#include "llvm/ADT/Statistic.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000029#include "llvm/ADT/StringRef.h"
Robert Lougher10f740d2017-08-03 11:54:02 +000030#include "llvm/CodeGen/LexicalScopes.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000031#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunf8422972017-12-13 02:51:04 +000032#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000034#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000035#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000036#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +000037#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000038#include "llvm/CodeGen/MachineOperand.h"
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +000039#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000040#include "llvm/CodeGen/SlotIndexes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000041#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000042#include "llvm/CodeGen/TargetOpcodes.h"
43#include "llvm/CodeGen/TargetRegisterInfo.h"
44#include "llvm/CodeGen/TargetSubtargetInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000045#include "llvm/CodeGen/VirtRegMap.h"
Nico Weber432a3882018-04-30 14:59:11 +000046#include "llvm/Config/llvm-config.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000047#include "llvm/IR/DebugInfoMetadata.h"
48#include "llvm/IR/DebugLoc.h"
49#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000050#include "llvm/IR/Metadata.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000051#include "llvm/MC/MCRegisterInfo.h"
52#include "llvm/Pass.h"
53#include "llvm/Support/Casting.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000054#include "llvm/Support/CommandLine.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000055#include "llvm/Support/Compiler.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000056#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000057#include "llvm/Support/raw_ostream.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");
Hsiangkai Wang66609a82019-01-18 07:17:09 +000073STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
Eugene Zelenko5df3d892017-08-24 21:21:39 +000074
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000075char LiveDebugVariables::ID = 0;
76
Matthias Braun1527baa2017-05-25 21:26:32 +000077INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000078 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000079INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000080INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Matthias Braun1527baa2017-05-25 21:26:32 +000081INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000082 "Debug Variable Analysis", false, false)
83
84void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000085 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000086 AU.addRequiredTransitive<LiveIntervals>();
87 AU.setPreservesAll();
88 MachineFunctionPass::getAnalysisUsage(AU);
89}
90
Eugene Zelenko5df3d892017-08-24 21:21:39 +000091LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000092 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
93}
94
Reid Kleckner04e25e02017-10-03 17:59:02 +000095enum : unsigned { UndefLocNo = ~0U };
96
97/// Describes a location by number along with some flags about the original
98/// usage of the location.
99class DbgValueLocation {
100public:
101 DbgValueLocation(unsigned LocNo, bool WasIndirect)
102 : LocNo(LocNo), WasIndirect(WasIndirect) {
103 static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
104 assert(locNo() == LocNo && "location truncation");
105 }
106
107 DbgValueLocation() : LocNo(0), WasIndirect(0) {}
108
109 unsigned locNo() const {
110 // Fix up the undef location number, which gets truncated.
111 return LocNo == INT_MAX ? UndefLocNo : LocNo;
112 }
113 bool wasIndirect() const { return WasIndirect; }
114 bool isUndef() const { return locNo() == UndefLocNo; }
115
116 DbgValueLocation changeLocNo(unsigned NewLocNo) const {
117 return DbgValueLocation(NewLocNo, WasIndirect);
118 }
119
Reid Klecknerb4569de72017-10-03 18:30:11 +0000120 friend inline bool operator==(const DbgValueLocation &LHS,
121 const DbgValueLocation &RHS) {
122 return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000123 }
Reid Klecknerb4569de72017-10-03 18:30:11 +0000124
125 friend inline bool operator!=(const DbgValueLocation &LHS,
126 const DbgValueLocation &RHS) {
127 return !(LHS == RHS);
128 }
Reid Kleckner04e25e02017-10-03 17:59:02 +0000129
130private:
131 unsigned LocNo : 31;
132 unsigned WasIndirect : 1;
133};
134
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000135/// Map of where a user value is live, and its location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000136using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000137
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000138/// Map of stack slot offsets for spilled locations.
David Stenberg45acc962018-09-07 13:54:07 +0000139/// Non-spilled locations are not added to the map.
140using SpillOffsetMap = DenseMap<unsigned, unsigned>;
141
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000142namespace {
143
144class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000145
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000146/// A user value is a part of a debug info user variable.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000147///
148/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
149/// holds part of a user variable. The part is identified by a byte offset.
150///
151/// UserValues are grouped into equivalence classes for easier searching. Two
152/// user values are related if they refer to the same variable, or if they are
153/// held by the same virtual register. The equivalence class is the transitive
154/// closure of that relation.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000155class UserValue {
Reid Kleckner4e040282017-09-20 18:19:08 +0000156 const DILocalVariable *Variable; ///< The debug info variable we are part of.
157 const DIExpression *Expression; ///< Any complex address expression.
Devang Patel26ffa012011-02-04 01:43:25 +0000158 DebugLoc dl; ///< The debug location for the variable. This is
159 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000160 UserValue *leader; ///< Equivalence class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000161 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000162
163 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000164 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000165
166 /// Map of slot indices where this value is live.
167 LocMap locInts;
168
Robert Lougher10f740d2017-08-03 11:54:02 +0000169 /// Set of interval start indexes that have been trimmed to the
170 /// lexical scope.
171 SmallSet<SlotIndex, 2> trimmedDefs;
172
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000173 /// Insert a DBG_VALUE into MBB at Idx for LocNo.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000174 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
David Stenberg45acc962018-09-07 13:54:07 +0000175 SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled,
176 unsigned SpillOffset, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000177 const TargetInstrInfo &TII,
178 const TargetRegisterInfo &TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000179
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000180 /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000181 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000182 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
183 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000184
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000185public:
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000186 /// Create a new UserValue.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000187 UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
188 LocMap::Allocator &alloc)
189 : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
190 locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000191
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000192 /// Get the leader of this value's equivalence class.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000193 UserValue *getLeader() {
194 UserValue *l = leader;
195 while (l != l->leader)
196 l = l->leader;
197 return leader = l;
198 }
199
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000200 /// Return the next UserValue in the equivalence class.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000201 UserValue *getNext() const { return next; }
202
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000203 /// Does this UserValue match the parameters?
Reid Kleckner4e040282017-09-20 18:19:08 +0000204 bool match(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000205 const DILocation *IA) const {
206 // FIXME: The fragment should be part of the equivalence class, but not
207 // other things in the expression like stack values.
208 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000209 }
210
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000211 /// Merge equivalence classes.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000212 static UserValue *merge(UserValue *L1, UserValue *L2) {
213 L2 = L2->getLeader();
214 if (!L1)
215 return L2;
216 L1 = L1->getLeader();
217 if (L1 == L2)
218 return L1;
219 // Splice L2 before L1's members.
220 UserValue *End = L2;
Richard Trieu7a083812016-02-18 22:09:30 +0000221 while (End->next) {
222 End->leader = L1;
223 End = End->next;
224 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000225 End->leader = L1;
226 End->next = L1->next;
227 L1->next = L2;
228 return L1;
229 }
230
Mikael Holmen57b33f62018-06-21 07:02:46 +0000231 /// Return the location number that matches Loc.
232 ///
233 /// For undef values we always return location number UndefLocNo without
234 /// inserting anything in locations. Since locations is a vector and the
235 /// location number is the position in the vector and UndefLocNo is ~0,
236 /// we would need a very big vector to put the value at the right position.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000237 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000238 if (LocMO.isReg()) {
239 if (LocMO.getReg() == 0)
Reid Klecknereed09732017-09-15 22:08:50 +0000240 return UndefLocNo;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000241 // For register locations we dont care about use/def and other flags.
242 for (unsigned i = 0, e = locations.size(); i != e; ++i)
243 if (locations[i].isReg() &&
244 locations[i].getReg() == LocMO.getReg() &&
245 locations[i].getSubReg() == LocMO.getSubReg())
246 return i;
247 } else
248 for (unsigned i = 0, e = locations.size(); i != e; ++i)
249 if (LocMO.isIdenticalTo(locations[i]))
250 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000251 locations.push_back(LocMO);
252 // We are storing a MachineOperand outside a MachineInstr.
253 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000254 // Don't store def operands.
Geoff Berryb3d126d2017-12-29 21:01:09 +0000255 if (locations.back().isReg()) {
256 if (locations.back().isDef())
257 locations.back().setIsDead(false);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000258 locations.back().setIsUse();
Geoff Berryb3d126d2017-12-29 21:01:09 +0000259 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000260 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000261 }
262
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000263 /// Ensure that all virtual register locations are mapped.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000264 void mapVirtRegs(LDVImpl *LDV);
265
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000266 /// Add a definition point to this value.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000267 void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) {
268 DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000269 // Add a singular (Idx,Idx) -> Loc mapping.
270 LocMap::iterator I = locInts.find(Idx);
271 if (!I.valid() || I.start() != Idx)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000272 I.insert(Idx, Idx.getNextSlot(), Loc);
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000273 else
274 // A later DBG_VALUE at the same SlotIndex overrides the old location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000275 I.setValue(Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000276 }
277
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000278 /// Extend the current definition as far as possible down.
279 ///
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000280 /// Stop when meeting an existing def or when leaving the live
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000281 /// range of VNI. End points where VNI is no longer live are added to Kills.
282 ///
283 /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
284 /// data-flow analysis to propagate them beyond basic block boundaries.
285 ///
286 /// \param Idx Starting point for the definition.
287 /// \param Loc Location number to propagate.
288 /// \param LR Restrict liveness to where LR has the value VNI. May be null.
289 /// \param VNI When LR is not null, this is the value to restrict to.
290 /// \param [out] Kills Append end points of VNI's live range to Kills.
291 /// \param LIS Live intervals analysis.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000292 void extendDef(SlotIndex Idx, DbgValueLocation Loc,
Matthias Braun34e1be92013-10-10 21:29:02 +0000293 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000294 SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000295 LiveIntervals &LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000296
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000297 /// The value in LI/LocNo may be copies to other registers. Determine if
298 /// any of the copies are available at the kill points, and add defs if
299 /// possible.
300 ///
301 /// \param LI Scan for copies of the value in LI->reg.
302 /// \param LocNo Location number of LI->reg.
303 /// \param WasIndirect Indicates if the original use of LI->reg was indirect
304 /// \param Kills Points where the range of LocNo could be extended.
305 /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000306 void addDefsFromCopies(
307 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
308 const SmallVectorImpl<SlotIndex> &Kills,
309 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
310 MachineRegisterInfo &MRI, LiveIntervals &LIS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000311
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000312 /// Compute the live intervals of all locations after collecting all their
313 /// def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000314 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Robert Lougher10f740d2017-08-03 11:54:02 +0000315 LiveIntervals &LIS, LexicalScopes &LS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000316
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000317 /// Replace OldReg ranges with NewRegs ranges where NewRegs is
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000318 /// live. Returns true if any changes were made.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000319 bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000320 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000321
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000322 /// Rewrite virtual register locations according to the provided virtual
323 /// register map. Record the stack slot offsets for the locations that
324 /// were spilled.
David Stenberg45acc962018-09-07 13:54:07 +0000325 void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
326 const TargetInstrInfo &TII,
327 const TargetRegisterInfo &TRI,
328 SpillOffsetMap &SpillOffsets);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000329
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000330 /// Recreate DBG_VALUE instruction from data structures.
Reid Kleckner4e040282017-09-20 18:19:08 +0000331 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000332 const TargetInstrInfo &TII,
333 const TargetRegisterInfo &TRI,
David Stenberg45acc962018-09-07 13:54:07 +0000334 const SpillOffsetMap &SpillOffsets);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000335
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000336 /// Return DebugLoc of this UserValue.
Devang Patelf9e2ae92011-09-13 18:40:53 +0000337 DebugLoc getDebugLoc() { return dl;}
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000338
Eric Christopher1cdefae2015-02-27 00:11:34 +0000339 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000340};
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000341
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000342/// A user label is a part of a debug info user label.
343class UserLabel {
344 const DILabel *Label; ///< The debug info label we are part of.
345 DebugLoc dl; ///< The debug location for the label. This is
346 ///< used by dwarf writer to find lexical scope.
347 SlotIndex loc; ///< Slot used by the debug label.
348
349 /// Insert a DBG_LABEL into MBB at Idx.
350 void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
351 LiveIntervals &LIS, const TargetInstrInfo &TII);
352
353public:
354 /// Create a new UserLabel.
355 UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
356 : Label(label), dl(std::move(L)), loc(Idx) {}
357
358 /// Does this UserLabel match the parameters?
359 bool match(const DILabel *L, const DILocation *IA,
360 const SlotIndex Index) const {
361 return Label == L && dl->getInlinedAt() == IA && loc == Index;
362 }
363
364 /// Recreate DBG_LABEL instruction from data structures.
365 void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
366
367 /// Return DebugLoc of this UserLabel.
368 DebugLoc getDebugLoc() { return dl; }
369
370 void print(raw_ostream &, const TargetRegisterInfo *);
371};
372
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000373/// Implementation of the LiveDebugVariables pass.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000374class LDVImpl {
375 LiveDebugVariables &pass;
376 LocMap::Allocator allocator;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000377 MachineFunction *MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000378 LiveIntervals *LIS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000379 const TargetRegisterInfo *TRI;
380
Manman Ren7a4c8a72013-02-13 20:23:48 +0000381 /// Whether emitDebugValues is called.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000382 bool EmitDone = false;
383
Manman Ren7a4c8a72013-02-13 20:23:48 +0000384 /// Whether the machine function is modified during the pass.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000385 bool ModifiedMF = false;
Manman Ren7a4c8a72013-02-13 20:23:48 +0000386
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000387 /// All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000388 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000389
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000390 /// All allocated UserLabel instances.
391 SmallVector<std::unique_ptr<UserLabel>, 2> userLabels;
392
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000393 /// Map virtual register to eq class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000394 using VRMap = DenseMap<unsigned, UserValue *>;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000395 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000396
397 /// Map user variable to eq class leader.
Reid Kleckner4e040282017-09-20 18:19:08 +0000398 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000399 UVMap userVarMap;
400
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000401 /// Find or create a UserValue.
Reid Kleckner4e040282017-09-20 18:19:08 +0000402 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000403 const DebugLoc &DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000404
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000405 /// Find the EC leader for VirtReg or null.
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000406 UserValue *lookupVirtReg(unsigned VirtReg);
407
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000408 /// Add DBG_VALUE instruction to our maps.
409 ///
410 /// \param MI DBG_VALUE instruction
411 /// \param Idx Last valid SLotIndex before instruction.
412 ///
413 /// \returns True if the DBG_VALUE instruction should be deleted.
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000414 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000415
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000416 /// Add DBG_LABEL instruction to UserLabel.
417 ///
418 /// \param MI DBG_LABEL instruction
419 /// \param Idx Last valid SlotIndex before instruction.
420 ///
421 /// \returns True if the DBG_LABEL instruction should be deleted.
422 bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
423
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000424 /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
425 /// for each instruction.
426 ///
427 /// \param mf MachineFunction to be scanned.
428 ///
429 /// \returns True if any debug values were found.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000430 bool collectDebugValues(MachineFunction &mf);
431
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000432 /// Compute the live intervals of all user values after collecting all
433 /// their def points.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000434 void computeIntervals();
435
436public:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000437 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
438
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000439 bool runOnMachineFunction(MachineFunction &mf);
440
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000441 /// Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000442 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000443 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000444 userValues.clear();
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000445 userLabels.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000446 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000447 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000448 // Make sure we call emitDebugValues if the machine function was modified.
449 assert((!ModifiedMF || EmitDone) &&
450 "Dbg values are not emitted in LDV");
451 EmitDone = false;
452 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000453 }
454
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000455 /// Map virtual register to an equivalence class.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000456 void mapVirtReg(unsigned VirtReg, UserValue *EC);
457
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000458 /// Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000459 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000460
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000461 /// Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000462 void emitDebugValues(VirtRegMap *VRM);
463
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000464 void print(raw_ostream&);
465};
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000466
467} // end anonymous namespace
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000468
Aaron Ballman615eb472017-10-15 14:32:27 +0000469#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000470static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000471 const LLVMContext &Ctx) {
472 if (!DL)
473 return;
474
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000475 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000476 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000477 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000478 CommentOS << ':' << DL.getLine();
479 if (DL.getCol() != 0)
480 CommentOS << ':' << DL.getCol();
481
482 DebugLoc InlinedAtDL = DL.getInlinedAt();
483 if (!InlinedAtDL)
484 return;
485
486 CommentOS << " @[ ";
487 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
488 CommentOS << " ]";
489}
490
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000491static void printExtendedName(raw_ostream &OS, const DINode *Node,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000492 const DILocation *DL) {
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000493 const LLVMContext &Ctx = Node->getContext();
494 StringRef Res;
495 unsigned Line;
496 if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
497 Res = V->getName();
498 Line = V->getLine();
499 } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
500 Res = L->getName();
501 Line = L->getLine();
502 }
503
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000504 if (!Res.empty())
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000505 OS << Res << "," << Line;
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000506 if (auto *InlinedAt = DL->getInlinedAt()) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000507 if (DebugLoc InlinedAtDL = InlinedAt) {
508 OS << " @[";
509 printDebugLoc(InlinedAtDL, OS, Ctx);
510 OS << "]";
511 }
512 }
513}
514
Eric Christopher1cdefae2015-02-27 00:11:34 +0000515void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Frederic Risse6bb1872014-08-07 20:04:00 +0000516 OS << "!\"";
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000517 printExtendedName(OS, Variable, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000518
Devang Patel6c1ed312011-08-09 01:03:35 +0000519 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000520 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
521 OS << " [" << I.start() << ';' << I.stop() << "):";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000522 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000523 OS << "undef";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000524 else {
525 OS << I.value().locNo();
526 if (I.value().wasIndirect())
527 OS << " ind";
528 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000529 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000530 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
531 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000532 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000533 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000534 OS << '\n';
535}
536
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000537void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
538 OS << "!\"";
539 printExtendedName(OS, Label, dl);
540
541 OS << "\"\t";
542 OS << loc;
543 OS << '\n';
544}
545
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000546void LDVImpl::print(raw_ostream &OS) {
547 OS << "********** DEBUG VARIABLES **********\n";
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000548 for (auto &userValue : userValues)
549 userValue->print(OS, TRI);
550 OS << "********** DEBUG LABELS **********\n";
551 for (auto &userLabel : userLabels)
552 userLabel->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000553}
Florian Hahn6b3216a2017-07-31 10:07:49 +0000554#endif
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000555
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000556void UserValue::mapVirtRegs(LDVImpl *LDV) {
557 for (unsigned i = 0, e = locations.size(); i != e; ++i)
558 if (locations[i].isReg() &&
559 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
560 LDV->mapVirtReg(locations[i].getReg(), this);
561}
562
Reid Kleckner4e040282017-09-20 18:19:08 +0000563UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000564 const DIExpression *Expr, const DebugLoc &DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000565 UserValue *&Leader = userVarMap[Var];
566 if (Leader) {
567 UserValue *UV = Leader->getLeader();
568 Leader = UV;
569 for (; UV; UV = UV->getNext())
Reid Kleckner04e25e02017-10-03 17:59:02 +0000570 if (UV->match(Var, Expr, DL->getInlinedAt()))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000571 return UV;
572 }
573
David Blaikie2b1dfa72014-04-21 20:37:07 +0000574 userValues.push_back(
Reid Kleckner04e25e02017-10-03 17:59:02 +0000575 llvm::make_unique<UserValue>(Var, Expr, DL, allocator));
David Blaikie2b1dfa72014-04-21 20:37:07 +0000576 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000577 Leader = UserValue::merge(Leader, UV);
578 return UV;
579}
580
581void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
582 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000583 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000584 Leader = UserValue::merge(Leader, EC);
585}
586
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000587UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000588 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000589 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000590 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000591}
592
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000593bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000594 // DBG_VALUE loc, offset, variable
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000595 if (MI.getNumOperands() != 4 ||
596 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
597 !MI.getOperand(2).isMetadata()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000598 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000599 return false;
600 }
601
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000602 // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
603 // register that hasn't been defined yet. If we do not remove those here, then
604 // the re-insertion of the DBG_VALUE instruction after register allocation
605 // will be incorrect.
606 // TODO: If earlier passes are corrected to generate sane debug information
607 // (and if the machine verifier is improved to catch this), then these checks
608 // could be removed or replaced by asserts.
609 bool Discard = false;
610 if (MI.getOperand(0).isReg() &&
611 TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg())) {
612 const unsigned Reg = MI.getOperand(0).getReg();
613 if (!LIS->hasInterval(Reg)) {
614 // The DBG_VALUE is described by a virtual register that does not have a
615 // live interval. Discard the DBG_VALUE.
616 Discard = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000617 LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
618 << " " << MI);
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000619 } else {
620 // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
621 // is defined dead at Idx (where Idx is the slot index for the instruction
Jordan Rupprecht6387fa22019-02-23 01:28:32 +0000622 // preceding the DBG_VALUE).
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000623 const LiveInterval &LI = LIS->getInterval(Reg);
624 LiveQueryResult LRQ = LI.Query(Idx);
625 if (!LRQ.valueOutOrDead()) {
626 // We have found a DBG_VALUE with the value in a virtual register that
627 // is not live. Discard the DBG_VALUE.
628 Discard = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000629 LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
630 << " " << MI);
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000631 }
632 }
633 }
634
Reid Kleckner04e25e02017-10-03 17:59:02 +0000635 // Get or create the UserValue for (variable,offset) here.
Reid Kleckner4e040282017-09-20 18:19:08 +0000636 bool IsIndirect = MI.getOperand(1).isImm();
Adrian Prantlb2811e52017-07-28 23:06:50 +0000637 if (IsIndirect)
638 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
Reid Kleckner4e040282017-09-20 18:19:08 +0000639 const DILocalVariable *Var = MI.getDebugVariable();
640 const DIExpression *Expr = MI.getDebugExpression();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000641 UserValue *UV =
642 getUserValue(Var, Expr, MI.getDebugLoc());
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000643 if (!Discard)
644 UV->addDef(Idx, MI.getOperand(0), IsIndirect);
Bjorn Petterssone0050d72018-03-06 13:23:28 +0000645 else {
646 MachineOperand MO = MachineOperand::CreateReg(0U, false);
647 MO.setIsDebug();
648 UV->addDef(Idx, MO, false);
649 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000650 return true;
651}
652
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000653bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
654 // DBG_LABEL label
655 if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
656 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
657 return false;
658 }
659
660 // Get or create the UserLabel for label here.
661 const DILabel *Label = MI.getDebugLabel();
662 const DebugLoc &DL = MI.getDebugLoc();
663 bool Found = false;
664 for (auto const &L : userLabels) {
665 if (L->match(Label, DL->getInlinedAt(), Idx)) {
666 Found = true;
667 break;
668 }
669 }
670 if (!Found)
671 userLabels.push_back(llvm::make_unique<UserLabel>(Label, DL, Idx));
672
673 return true;
674}
675
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000676bool LDVImpl::collectDebugValues(MachineFunction &mf) {
677 bool Changed = false;
678 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
679 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000680 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000681 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
682 MBBI != MBBE;) {
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000683 // Use the first debug instruction in the sequence to get a SlotIndex
684 // for following consecutive debug instructions.
685 if (!MBBI->isDebugInstr()) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000686 ++MBBI;
687 continue;
688 }
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000689 // Debug instructions has no slot index. Use the previous
690 // non-debug instruction's SlotIndex as its SlotIndex.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000691 SlotIndex Idx =
692 MBBI == MBB->begin()
693 ? LIS->getMBBStartIdx(MBB)
694 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000695 // Handle consecutive debug instructions with the same slot index.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000696 do {
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000697 // Only handle DBG_VALUE in handleDebugValue(). Skip all other
698 // kinds of debug instructions.
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000699 if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
700 (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000701 MBBI = MBB->erase(MBBI);
702 Changed = true;
703 } else
704 ++MBBI;
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000705 } while (MBBI != MBBE && MBBI->isDebugInstr());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000706 }
707 }
708 return Changed;
709}
710
Reid Kleckner04e25e02017-10-03 17:59:02 +0000711void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
Adrian Prantlce858132015-12-21 20:03:00 +0000712 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000713 LiveIntervals &LIS) {
Adrian Prantlce858132015-12-21 20:03:00 +0000714 SlotIndex Start = Idx;
715 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
716 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
717 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000718
Adrian Prantlce858132015-12-21 20:03:00 +0000719 // Limit to VNI's live range.
720 bool ToEnd = true;
721 if (LR && VNI) {
722 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
723 if (!Segment || Segment->valno != VNI) {
724 if (Kills)
725 Kills->push_back(Start);
726 return;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000727 }
Richard Trieu7a083812016-02-18 22:09:30 +0000728 if (Segment->end < Stop) {
729 Stop = Segment->end;
730 ToEnd = false;
731 }
Adrian Prantlce858132015-12-21 20:03:00 +0000732 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000733
Adrian Prantlce858132015-12-21 20:03:00 +0000734 // There could already be a short def at Start.
735 if (I.valid() && I.start() <= Start) {
736 // Stop when meeting a different location or an already extended interval.
737 Start = Start.getNextSlot();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000738 if (I.value() != Loc || I.stop() != Start)
Adrian Prantlce858132015-12-21 20:03:00 +0000739 return;
740 // This is a one-slot placeholder. Just skip it.
741 ++I;
742 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000743
Adrian Prantlce858132015-12-21 20:03:00 +0000744 // Limited by the next def.
Richard Trieu7a083812016-02-18 22:09:30 +0000745 if (I.valid() && I.start() < Stop) {
746 Stop = I.start();
747 ToEnd = false;
748 }
Adrian Prantlce858132015-12-21 20:03:00 +0000749 // Limited by VNI's live range.
750 else if (!ToEnd && Kills)
751 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000752
Adrian Prantlce858132015-12-21 20:03:00 +0000753 if (Start < Stop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000754 I.insert(Start, Stop, Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000755}
756
Reid Kleckner04e25e02017-10-03 17:59:02 +0000757void UserValue::addDefsFromCopies(
758 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
759 const SmallVectorImpl<SlotIndex> &Kills,
760 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
761 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000762 if (Kills.empty())
763 return;
764 // Don't track copies from physregs, there are too many uses.
765 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
766 return;
767
768 // Collect all the (vreg, valno) pairs that are copies of LI.
769 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000770 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
771 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000772 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000773 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000774 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000775 unsigned DstReg = MI->getOperand(0).getReg();
776
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000777 // Don't follow copies to physregs. These are usually setting up call
778 // arguments, and the argument registers are always call clobbered. We are
779 // better off in the source register which could be a callee-saved register,
780 // or it could be spilled.
781 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
782 continue;
783
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000784 // Is LocNo extended to reach this copy? If not, another def may be blocking
785 // it, or we are looking at a wrong value of LI.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000786 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000787 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000788 if (!I.valid() || I.value().locNo() != LocNo)
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000789 continue;
790
791 if (!LIS.hasInterval(DstReg))
792 continue;
793 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000794 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
795 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000796 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
797 }
798
799 if (CopyValues.empty())
800 return;
801
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000802 LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
803 << '\n');
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000804
805 // Try to add defs of the copied values for each kill point.
806 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
807 SlotIndex Idx = Kills[i];
808 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
809 LiveInterval *DstLI = CopyValues[j].first;
810 const VNInfo *DstVNI = CopyValues[j].second;
811 if (DstLI->getVNInfoAt(Idx) != DstVNI)
812 continue;
813 // Check that there isn't already a def at Idx
814 LocMap::iterator I = locInts.find(Idx);
815 if (I.valid() && I.start() <= Idx)
816 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000817 LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
818 << DstVNI->id << " in " << *DstLI << '\n');
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000819 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
820 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
821 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000822 DbgValueLocation NewLoc(LocNo, WasIndirect);
823 I.insert(Idx, Idx.getNextSlot(), NewLoc);
824 NewDefs.push_back(std::make_pair(Idx, NewLoc));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000825 break;
826 }
827 }
828}
829
Robert Lougher10f740d2017-08-03 11:54:02 +0000830void UserValue::computeIntervals(MachineRegisterInfo &MRI,
831 const TargetRegisterInfo &TRI,
832 LiveIntervals &LIS, LexicalScopes &LS) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000833 SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000834
835 // Collect all defs to be extended (Skipping undefs).
836 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000837 if (!I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000838 Defs.push_back(std::make_pair(I.start(), I.value()));
839
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000840 // Extend all defs, and possibly add new ones along the way.
841 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000842 SlotIndex Idx = Defs[i].first;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000843 DbgValueLocation Loc = Defs[i].second;
844 const MachineOperand &LocMO = locations[Loc.locNo()];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000845
Reid Kleckner04e25e02017-10-03 17:59:02 +0000846 if (!LocMO.isReg()) {
847 extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000848 continue;
849 }
850
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000851 // Register locations are constrained to where the register value is live.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000852 if (TargetRegisterInfo::isVirtualRegister(LocMO.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000853 LiveInterval *LI = nullptr;
854 const VNInfo *VNI = nullptr;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000855 if (LIS.hasInterval(LocMO.getReg())) {
856 LI = &LIS.getInterval(LocMO.getReg());
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000857 VNI = LI->getVNInfoAt(Idx);
858 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000859 SmallVector<SlotIndex, 16> Kills;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000860 extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
Bjorn Pettersson84830042018-08-25 10:02:03 +0000861 // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
862 // if the original location for example is %vreg0:sub_hi, and we find a
863 // full register copy in addDefsFromCopies (at the moment it only handles
864 // full register copies), then we must add the sub1 sub-register index to
865 // the new location. However, that is only possible if the new virtual
866 // register is of the same regclass (or if there is an equivalent
867 // sub-register in that regclass). For now, simply skip handling copies if
868 // a sub-register is involved.
869 if (LI && !LocMO.getSubReg())
Reid Kleckner04e25e02017-10-03 17:59:02 +0000870 addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI,
871 LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000872 continue;
873 }
874
Bjorn Pettersson715a5ef2017-09-28 13:10:06 +0000875 // For physregs, we only mark the start slot idx. DwarfDebug will see it
876 // as if the DBG_VALUE is valid up until the end of the basic block, or
877 // the next def of the physical register. So we do not need to extend the
878 // range. It might actually happen that the DBG_VALUE is the last use of
879 // the physical register (e.g. if this is an unused input argument to a
880 // function).
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000881 }
882
Robert Lougher10f740d2017-08-03 11:54:02 +0000883 // The computed intervals may extend beyond the range of the debug
884 // location's lexical scope. In this case, splitting of an interval
885 // can result in an interval outside of the scope being created,
886 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
887 // this, trim the intervals to the lexical scope.
888
889 LexicalScope *Scope = LS.findLexicalScope(dl);
890 if (!Scope)
891 return;
892
893 SlotIndex PrevEnd;
894 LocMap::iterator I = locInts.begin();
895
896 // Iterate over the lexical scope ranges. Each time round the loop
897 // we check the intervals for overlap with the end of the previous
898 // range and the start of the next. The first range is handled as
899 // a special case where there is no PrevEnd.
900 for (const InsnRange &Range : Scope->getRanges()) {
901 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
902 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
903
904 // At the start of each iteration I has been advanced so that
905 // I.stop() >= PrevEnd. Check for overlap.
906 if (PrevEnd && I.start() < PrevEnd) {
907 SlotIndex IStop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000908 DbgValueLocation Loc = I.value();
Robert Lougher10f740d2017-08-03 11:54:02 +0000909
910 // Stop overlaps previous end - trim the end of the interval to
911 // the scope range.
912 I.setStopUnchecked(PrevEnd);
913 ++I;
914
915 // If the interval also overlaps the start of the "next" (i.e.
916 // current) range create a new interval for the remainder (which
917 // may be further trimmed).
918 if (RStart < IStop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000919 I.insert(RStart, IStop, Loc);
Robert Lougher10f740d2017-08-03 11:54:02 +0000920 }
921
922 // Advance I so that I.stop() >= RStart, and check for overlap.
923 I.advanceTo(RStart);
924 if (!I.valid())
925 return;
926
927 if (I.start() < RStart) {
928 // Interval start overlaps range - trim to the scope range.
929 I.setStartUnchecked(RStart);
930 // Remember that this interval was trimmed.
931 trimmedDefs.insert(RStart);
932 }
933
934 // The end of a lexical scope range is the last instruction in the
935 // range. To convert to an interval we need the index of the
936 // instruction after it.
937 REnd = REnd.getNextIndex();
938
939 // Advance I to first interval outside current range.
940 I.advanceTo(REnd);
941 if (!I.valid())
942 return;
943
944 PrevEnd = REnd;
945 }
946
947 // Check for overlap with end of final range.
948 if (PrevEnd && I.start() < PrevEnd)
949 I.setStopUnchecked(PrevEnd);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000950}
951
952void LDVImpl::computeIntervals() {
Robert Lougher10f740d2017-08-03 11:54:02 +0000953 LexicalScopes LS;
954 LS.initialize(*MF);
955
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000956 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Robert Lougher10f740d2017-08-03 11:54:02 +0000957 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000958 userValues[i]->mapVirtRegs(this);
959 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000960}
961
962bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000963 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000964 MF = &mf;
965 LIS = &pass.getAnalysis<LiveIntervals>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000966 TRI = mf.getSubtarget().getRegisterInfo();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000967 LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
968 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000969
970 bool Changed = collectDebugValues(mf);
971 computeIntervals();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000972 LLVM_DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000973 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000974 return Changed;
975}
976
David Blaikie2f040112014-07-25 16:10:16 +0000977static void removeDebugValues(MachineFunction &mf) {
978 for (MachineBasicBlock &MBB : mf) {
979 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
980 if (!MBBI->isDebugValue()) {
981 ++MBBI;
982 continue;
983 }
984 MBBI = MBB.erase(MBBI);
985 }
986 }
987}
988
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000989bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000990 if (!EnableLDV)
991 return false;
Matthias Braunf1caa282017-12-15 22:22:58 +0000992 if (!mf.getFunction().getSubprogram()) {
David Blaikie2f040112014-07-25 16:10:16 +0000993 removeDebugValues(mf);
994 return false;
995 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000996 if (!pImpl)
997 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000998 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000999}
1000
1001void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001002 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +00001003 static_cast<LDVImpl*>(pImpl)->clear();
1004}
1005
1006LiveDebugVariables::~LiveDebugVariables() {
1007 if (pImpl)
1008 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +00001009}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001010
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001011//===----------------------------------------------------------------------===//
1012// Live Range Splitting
1013//===----------------------------------------------------------------------===//
1014
1015bool
Mark Laceyf9ea8852013-08-14 23:50:04 +00001016UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
1017 LiveIntervals& LIS) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001018 LLVM_DEBUG({
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001019 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +00001020 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001021 });
1022 bool DidChange = false;
1023 LocMap::iterator LocMapI;
1024 LocMapI.setMap(locInts);
1025 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +00001026 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001027 if (LI->empty())
1028 continue;
1029
1030 // Don't allocate the new LocNo until it is needed.
Reid Klecknereed09732017-09-15 22:08:50 +00001031 unsigned NewLocNo = UndefLocNo;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001032
1033 // Iterate over the overlaps between locInts and LI.
1034 LocMapI.find(LI->beginIndex());
1035 if (!LocMapI.valid())
1036 continue;
1037 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
1038 LiveInterval::iterator LIE = LI->end();
1039 while (LocMapI.valid() && LII != LIE) {
1040 // At this point, we know that LocMapI.stop() > LII->start.
1041 LII = LI->advanceTo(LII, LocMapI.start());
1042 if (LII == LIE)
1043 break;
1044
1045 // Now LII->end > LocMapI.start(). Do we have an overlap?
Reid Kleckner04e25e02017-10-03 17:59:02 +00001046 if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001047 // Overlapping correct location. Allocate NewLocNo now.
Reid Klecknereed09732017-09-15 22:08:50 +00001048 if (NewLocNo == UndefLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001049 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
1050 MO.setSubReg(locations[OldLocNo].getSubReg());
1051 NewLocNo = getLocationNo(MO);
1052 DidChange = true;
1053 }
1054
1055 SlotIndex LStart = LocMapI.start();
1056 SlotIndex LStop = LocMapI.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001057 DbgValueLocation OldLoc = LocMapI.value();
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001058
1059 // Trim LocMapI down to the LII overlap.
1060 if (LStart < LII->start)
1061 LocMapI.setStartUnchecked(LII->start);
1062 if (LStop > LII->end)
1063 LocMapI.setStopUnchecked(LII->end);
1064
1065 // Change the value in the overlap. This may trigger coalescing.
Reid Kleckner04e25e02017-10-03 17:59:02 +00001066 LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001067
1068 // Re-insert any removed OldLocNo ranges.
1069 if (LStart < LocMapI.start()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001070 LocMapI.insert(LStart, LocMapI.start(), OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001071 ++LocMapI;
1072 assert(LocMapI.valid() && "Unexpected coalescing");
1073 }
1074 if (LStop > LocMapI.stop()) {
1075 ++LocMapI;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001076 LocMapI.insert(LII->end, LStop, OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001077 --LocMapI;
1078 }
1079 }
1080
1081 // Advance to the next overlap.
1082 if (LII->end < LocMapI.stop()) {
1083 if (++LII == LIE)
1084 break;
1085 LocMapI.advanceTo(LII->start);
1086 } else {
1087 ++LocMapI;
1088 if (!LocMapI.valid())
1089 break;
1090 LII = LI->advanceTo(LII, LocMapI.start());
1091 }
1092 }
1093 }
1094
1095 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
1096 locations.erase(locations.begin() + OldLocNo);
1097 LocMapI.goToBegin();
1098 while (LocMapI.valid()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001099 DbgValueLocation v = LocMapI.value();
1100 if (v.locNo() == OldLocNo) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001101 LLVM_DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
1102 << LocMapI.stop() << ")\n");
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001103 LocMapI.erase();
1104 } else {
Mikael Holmen57b33f62018-06-21 07:02:46 +00001105 // Undef values always have location number UndefLocNo, so don't change
1106 // locNo in that case. See getLocationNo().
1107 if (!v.isUndef() && v.locNo() > OldLocNo)
Reid Kleckner04e25e02017-10-03 17:59:02 +00001108 LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001109 ++LocMapI;
1110 }
1111 }
1112
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001113 LLVM_DEBUG({
1114 dbgs() << "Split result: \t";
1115 print(dbgs(), nullptr);
1116 });
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001117 return DidChange;
1118}
1119
1120bool
Mark Laceyf9ea8852013-08-14 23:50:04 +00001121UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1122 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001123 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001124 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +00001125 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001126 for (unsigned i = locations.size(); i ; --i) {
1127 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001128 const MachineOperand *Loc = &locations[LocNo];
1129 if (!Loc->isReg() || Loc->getReg() != OldReg)
1130 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +00001131 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001132 }
1133 return DidChange;
1134}
1135
Mark Laceyf9ea8852013-08-14 23:50:04 +00001136void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001137 bool DidChange = false;
1138 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +00001139 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001140
1141 if (!DidChange)
1142 return;
1143
1144 // Map all of the new virtual registers.
1145 UserValue *UV = lookupVirtReg(OldReg);
1146 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +00001147 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001148}
1149
1150void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +00001151splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001152 if (pImpl)
1153 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1154}
1155
David Stenberg45acc962018-09-07 13:54:07 +00001156void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1157 const TargetInstrInfo &TII,
1158 const TargetRegisterInfo &TRI,
1159 SpillOffsetMap &SpillOffsets) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001160 // Build a set of new locations with new numbers so we can coalesce our
1161 // IntervalMap if two vreg intervals collapse to the same physical location.
1162 // Use MapVector instead of SetVector because MapVector::insert returns the
Reid Kleckner4e040282017-09-20 18:19:08 +00001163 // position of the previously or newly inserted element. The boolean value
1164 // tracks if the location was produced by a spill.
1165 // FIXME: This will be problematic if we ever support direct and indirect
1166 // frame index locations, i.e. expressing both variables in memory and
1167 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
David Stenberg45acc962018-09-07 13:54:07 +00001168 MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations;
Reid Kleckner92687d42017-09-20 17:32:54 +00001169 SmallVector<unsigned, 4> LocNoMap(locations.size());
1170 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001171 bool Spilled = false;
David Stenberg45acc962018-09-07 13:54:07 +00001172 unsigned SpillOffset = 0;
Reid Kleckner92687d42017-09-20 17:32:54 +00001173 MachineOperand Loc = locations[I];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001174 // Only virtual registers are rewritten.
Reid Kleckner92687d42017-09-20 17:32:54 +00001175 if (Loc.isReg() && Loc.getReg() &&
1176 TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
1177 unsigned VirtReg = Loc.getReg();
1178 if (VRM.isAssignedReg(VirtReg) &&
1179 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
1180 // This can create a %noreg operand in rare cases when the sub-register
1181 // index is no longer available. That means the user value is in a
1182 // non-existent sub-register, and %noreg is exactly what we want.
1183 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1184 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
David Stenberg45acc962018-09-07 13:54:07 +00001185 // Retrieve the stack slot offset.
1186 unsigned SpillSize;
1187 const MachineRegisterInfo &MRI = MF.getRegInfo();
1188 const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1189 bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1190 SpillOffset, MF);
1191
1192 // FIXME: Invalidate the location if the offset couldn't be calculated.
1193 (void)Success;
1194
Reid Kleckner92687d42017-09-20 17:32:54 +00001195 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Reid Kleckner4e040282017-09-20 18:19:08 +00001196 Spilled = true;
Reid Kleckner92687d42017-09-20 17:32:54 +00001197 } else {
1198 Loc.setReg(0);
1199 Loc.setSubReg(0);
1200 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001201 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001202
1203 // Insert this location if it doesn't already exist and record a mapping
1204 // from the old number to the new number.
David Stenberg45acc962018-09-07 13:54:07 +00001205 auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
Reid Kleckner4e040282017-09-20 18:19:08 +00001206 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1207 LocNoMap[I] = NewLocNo;
Reid Kleckner92687d42017-09-20 17:32:54 +00001208 }
1209
David Stenberg45acc962018-09-07 13:54:07 +00001210 // Rewrite the locations and record the stack slot offsets for spills.
Reid Kleckner92687d42017-09-20 17:32:54 +00001211 locations.clear();
David Stenberg45acc962018-09-07 13:54:07 +00001212 SpillOffsets.clear();
Reid Kleckner4e040282017-09-20 18:19:08 +00001213 for (auto &Pair : NewLocations) {
David Stenberg45acc962018-09-07 13:54:07 +00001214 bool Spilled;
1215 unsigned SpillOffset;
1216 std::tie(Spilled, SpillOffset) = Pair.second;
Reid Kleckner92687d42017-09-20 17:32:54 +00001217 locations.push_back(Pair.first);
David Stenberg45acc962018-09-07 13:54:07 +00001218 if (Spilled) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001219 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
David Stenberg45acc962018-09-07 13:54:07 +00001220 SpillOffsets[NewLocNo] = SpillOffset;
Reid Kleckner4e040282017-09-20 18:19:08 +00001221 }
1222 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001223
1224 // Update the interval map, but only coalesce left, since intervals to the
1225 // right use the old location numbers. This should merge two contiguous
1226 // DBG_VALUE intervals with different vregs that were allocated to the same
1227 // physical register.
1228 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001229 DbgValueLocation Loc = I.value();
Mikael Holmen57b33f62018-06-21 07:02:46 +00001230 // Undef values don't exist in locations (and thus not in LocNoMap either)
1231 // so skip over them. See getLocationNo().
1232 if (Loc.isUndef())
1233 continue;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001234 unsigned NewLocNo = LocNoMap[Loc.locNo()];
1235 I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
Reid Kleckner92687d42017-09-20 17:32:54 +00001236 I.setStart(I.start());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001237 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001238}
1239
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001240/// Find an iterator for inserting a DBG_VALUE instruction.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001241static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +00001242findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001243 LiveIntervals &LIS) {
1244 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1245 Idx = Idx.getBaseIndex();
1246
1247 // Try to find an insert location by going backwards from Idx.
1248 MachineInstr *MI;
1249 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1250 // We've reached the beginning of MBB.
1251 if (Idx == Start) {
Keith Walker830a8c12016-09-16 14:07:29 +00001252 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001253 return I;
1254 }
1255 Idx = Idx.getPrevIndex();
1256 }
Devang Patel26ffa012011-02-04 01:43:25 +00001257
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +00001258 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001259 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001260 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001261}
1262
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001263/// Find an iterator for inserting the next DBG_VALUE instruction
1264/// (or end if no more insert locations found).
1265static MachineBasicBlock::iterator
1266findNextInsertLocation(MachineBasicBlock *MBB,
1267 MachineBasicBlock::iterator I,
1268 SlotIndex StopIdx, MachineOperand &LocMO,
1269 LiveIntervals &LIS,
1270 const TargetRegisterInfo &TRI) {
1271 if (!LocMO.isReg())
1272 return MBB->instr_end();
1273 unsigned Reg = LocMO.getReg();
1274
1275 // Find the next instruction in the MBB that define the register Reg.
Stefan Maksimovic991af7a2018-02-09 14:03:26 +00001276 while (I != MBB->end() && !I->isTerminator()) {
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001277 if (!LIS.isNotInMIMap(*I) &&
1278 SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
1279 break;
1280 if (I->definesRegister(Reg, &TRI))
1281 // The insert location is directly after the instruction/bundle.
1282 return std::next(I);
1283 ++I;
1284 }
1285 return MBB->end();
1286}
1287
1288void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
David Stenberg45acc962018-09-07 13:54:07 +00001289 SlotIndex StopIdx, DbgValueLocation Loc,
1290 bool Spilled, unsigned SpillOffset,
1291 LiveIntervals &LIS, const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001292 const TargetRegisterInfo &TRI) {
1293 SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1294 // Only search within the current MBB.
1295 StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1296 MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
Mikael Holmen57b33f62018-06-21 07:02:46 +00001297 // Undef values don't exist in locations so create new "noreg" register MOs
1298 // for them. See getLocationNo().
1299 MachineOperand MO = !Loc.isUndef() ?
1300 locations[Loc.locNo()] :
1301 MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false,
1302 /* isKill */ false, /* isDead */ false,
1303 /* isUndef */ false, /* isEarlyClobber */ false,
1304 /* SubReg */ 0, /* isDebug */ true);
1305
Devang Pateleabc3cea2011-08-04 20:42:11 +00001306 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001307
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001308 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001309 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001310 "Expected inlined-at fields to agree");
Reid Kleckner4e040282017-09-20 18:19:08 +00001311
1312 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1313 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
David Stenberg45acc962018-09-07 13:54:07 +00001314 // that the original virtual register was a pointer. Also, add the stack slot
1315 // offset for the spilled register to the expression.
Reid Kleckner4e040282017-09-20 18:19:08 +00001316 const DIExpression *Expr = Expression;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001317 bool IsIndirect = Loc.wasIndirect();
1318 if (Spilled) {
David Stenberg45acc962018-09-07 13:54:07 +00001319 auto Deref = IsIndirect ? DIExpression::WithDeref : DIExpression::NoDeref;
1320 Expr =
1321 DIExpression::prepend(Expr, DIExpression::NoDeref, SpillOffset, Deref);
Reid Kleckner04e25e02017-10-03 17:59:02 +00001322 IsIndirect = true;
1323 }
Reid Kleckner4e040282017-09-20 18:19:08 +00001324
Reid Kleckner04e25e02017-10-03 17:59:02 +00001325 assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
Reid Kleckner4e040282017-09-20 18:19:08 +00001326
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001327 do {
Mikael Holmen42f7bc92018-06-21 10:03:34 +00001328 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
1329 IsIndirect, MO, Variable, Expr);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001330
1331 // Continue and insert DBG_VALUES after every redefinition of register
1332 // associated with the debug value within the range
1333 I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
1334 } while (I != MBB->end());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001335}
1336
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001337void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1338 LiveIntervals &LIS,
1339 const TargetInstrInfo &TII) {
1340 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
1341 ++NumInsertedDebugLabels;
1342 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1343 .addMetadata(Label);
1344}
1345
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001346void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +00001347 const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001348 const TargetRegisterInfo &TRI,
David Stenberg45acc962018-09-07 13:54:07 +00001349 const SpillOffsetMap &SpillOffsets) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001350 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1351
1352 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1353 SlotIndex Start = I.start();
1354 SlotIndex Stop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001355 DbgValueLocation Loc = I.value();
David Stenberg45acc962018-09-07 13:54:07 +00001356 auto SpillIt =
1357 !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1358 bool Spilled = SpillIt != SpillOffsets.end();
1359 unsigned SpillOffset = Spilled ? SpillIt->second : 0;
Robert Lougher10f740d2017-08-03 11:54:02 +00001360
1361 // If the interval start was trimmed to the lexical scope insert the
1362 // DBG_VALUE at the previous index (otherwise it appears after the
1363 // first instruction in the range).
1364 if (trimmedDefs.count(Start))
1365 Start = Start.getPrevIndex();
1366
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001367 LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001368 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1369 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001370
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001371 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
David Stenberg45acc962018-09-07 13:54:07 +00001372 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1373 TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001374 // This interval may span multiple basic blocks.
1375 // Insert a DBG_VALUE into each one.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001376 while (Stop > MBBEnd) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001377 // Move to the next block.
1378 Start = MBBEnd;
1379 if (++MBB == MFEnd)
1380 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001381 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001382 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
David Stenberg45acc962018-09-07 13:54:07 +00001383 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1384 TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001385 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001386 LLVM_DEBUG(dbgs() << '\n');
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001387 if (MBB == MFEnd)
1388 break;
1389
1390 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001391 }
1392}
1393
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001394void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1395 LLVM_DEBUG(dbgs() << "\t" << loc);
1396 MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1397
1398 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1399 insertDebugLabel(&*MBB, loc, LIS, TII);
1400
1401 LLVM_DEBUG(dbgs() << '\n');
1402}
1403
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001404void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001405 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001406 if (!MF)
1407 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001408 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
David Stenberg45acc962018-09-07 13:54:07 +00001409 SpillOffsetMap SpillOffsets;
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001410 for (auto &userValue : userValues) {
1411 LLVM_DEBUG(userValue->print(dbgs(), TRI));
1412 userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1413 userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1414 }
1415 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
1416 for (auto &userLabel : userLabels) {
1417 LLVM_DEBUG(userLabel->print(dbgs(), TRI));
1418 userLabel->emitDebugLabel(*LIS, *TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001419 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001420 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001421}
1422
1423void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001424 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001425 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1426}
1427
David Blaikie2f040112014-07-25 16:10:16 +00001428bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001429 return Pass::doInitialization(M);
1430}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001431
Aaron Ballman615eb472017-10-15 14:32:27 +00001432#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +00001433LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001434 if (pImpl)
1435 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1436}
1437#endif